intel_pm.c 184 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580
  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. /**
  33. * RC6 is a special power stage which allows the GPU to enter an very
  34. * low-voltage mode when idle, using down to 0V while at this stage. This
  35. * stage is entered automatically when the GPU is idle when RC6 support is
  36. * enabled, and as soon as new workload arises GPU wakes up automatically as well.
  37. *
  38. * There are different RC6 modes available in Intel GPU, which differentiate
  39. * among each other with the latency required to enter and leave RC6 and
  40. * voltage consumed by the GPU in different states.
  41. *
  42. * The combination of the following flags define which states GPU is allowed
  43. * to enter, while RC6 is the normal RC6 state, RC6p is the deep RC6, and
  44. * RC6pp is deepest RC6. Their support by hardware varies according to the
  45. * GPU, BIOS, chipset and platform. RC6 is usually the safest one and the one
  46. * which brings the most power savings; deeper states save more power, but
  47. * require higher latency to switch to and wake up.
  48. */
  49. #define INTEL_RC6_ENABLE (1<<0)
  50. #define INTEL_RC6p_ENABLE (1<<1)
  51. #define INTEL_RC6pp_ENABLE (1<<2)
  52. static void gen9_init_clock_gating(struct drm_device *dev)
  53. {
  54. struct drm_i915_private *dev_priv = dev->dev_private;
  55. /* WaEnableLbsSlaRetryTimerDecrement:skl */
  56. I915_WRITE(BDW_SCRATCH1, I915_READ(BDW_SCRATCH1) |
  57. GEN9_LBS_SLA_RETRY_TIMER_DECREMENT_ENABLE);
  58. }
  59. static void skl_init_clock_gating(struct drm_device *dev)
  60. {
  61. struct drm_i915_private *dev_priv = dev->dev_private;
  62. gen9_init_clock_gating(dev);
  63. if (INTEL_REVID(dev) == SKL_REVID_A0) {
  64. /*
  65. * WaDisableSDEUnitClockGating:skl
  66. * WaSetGAPSunitClckGateDisable:skl
  67. */
  68. I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
  69. GEN8_GAPSUNIT_CLOCK_GATE_DISABLE |
  70. GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
  71. }
  72. if (INTEL_REVID(dev) <= SKL_REVID_D0) {
  73. /* WaDisableHDCInvalidation:skl */
  74. I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
  75. BDW_DISABLE_HDC_INVALIDATION);
  76. /* WaDisableChickenBitTSGBarrierAckForFFSliceCS:skl */
  77. I915_WRITE(FF_SLICE_CS_CHICKEN2,
  78. I915_READ(FF_SLICE_CS_CHICKEN2) |
  79. GEN9_TSG_BARRIER_ACK_DISABLE);
  80. }
  81. if (INTEL_REVID(dev) <= SKL_REVID_E0)
  82. /* WaDisableLSQCROPERFforOCL:skl */
  83. I915_WRITE(GEN8_L3SQCREG4, I915_READ(GEN8_L3SQCREG4) |
  84. GEN8_LQSC_RO_PERF_DIS);
  85. }
  86. static void i915_pineview_get_mem_freq(struct drm_device *dev)
  87. {
  88. struct drm_i915_private *dev_priv = dev->dev_private;
  89. u32 tmp;
  90. tmp = I915_READ(CLKCFG);
  91. switch (tmp & CLKCFG_FSB_MASK) {
  92. case CLKCFG_FSB_533:
  93. dev_priv->fsb_freq = 533; /* 133*4 */
  94. break;
  95. case CLKCFG_FSB_800:
  96. dev_priv->fsb_freq = 800; /* 200*4 */
  97. break;
  98. case CLKCFG_FSB_667:
  99. dev_priv->fsb_freq = 667; /* 167*4 */
  100. break;
  101. case CLKCFG_FSB_400:
  102. dev_priv->fsb_freq = 400; /* 100*4 */
  103. break;
  104. }
  105. switch (tmp & CLKCFG_MEM_MASK) {
  106. case CLKCFG_MEM_533:
  107. dev_priv->mem_freq = 533;
  108. break;
  109. case CLKCFG_MEM_667:
  110. dev_priv->mem_freq = 667;
  111. break;
  112. case CLKCFG_MEM_800:
  113. dev_priv->mem_freq = 800;
  114. break;
  115. }
  116. /* detect pineview DDR3 setting */
  117. tmp = I915_READ(CSHRDDR3CTL);
  118. dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
  119. }
  120. static void i915_ironlake_get_mem_freq(struct drm_device *dev)
  121. {
  122. struct drm_i915_private *dev_priv = dev->dev_private;
  123. u16 ddrpll, csipll;
  124. ddrpll = I915_READ16(DDRMPLL1);
  125. csipll = I915_READ16(CSIPLL0);
  126. switch (ddrpll & 0xff) {
  127. case 0xc:
  128. dev_priv->mem_freq = 800;
  129. break;
  130. case 0x10:
  131. dev_priv->mem_freq = 1066;
  132. break;
  133. case 0x14:
  134. dev_priv->mem_freq = 1333;
  135. break;
  136. case 0x18:
  137. dev_priv->mem_freq = 1600;
  138. break;
  139. default:
  140. DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
  141. ddrpll & 0xff);
  142. dev_priv->mem_freq = 0;
  143. break;
  144. }
  145. dev_priv->ips.r_t = dev_priv->mem_freq;
  146. switch (csipll & 0x3ff) {
  147. case 0x00c:
  148. dev_priv->fsb_freq = 3200;
  149. break;
  150. case 0x00e:
  151. dev_priv->fsb_freq = 3733;
  152. break;
  153. case 0x010:
  154. dev_priv->fsb_freq = 4266;
  155. break;
  156. case 0x012:
  157. dev_priv->fsb_freq = 4800;
  158. break;
  159. case 0x014:
  160. dev_priv->fsb_freq = 5333;
  161. break;
  162. case 0x016:
  163. dev_priv->fsb_freq = 5866;
  164. break;
  165. case 0x018:
  166. dev_priv->fsb_freq = 6400;
  167. break;
  168. default:
  169. DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
  170. csipll & 0x3ff);
  171. dev_priv->fsb_freq = 0;
  172. break;
  173. }
  174. if (dev_priv->fsb_freq == 3200) {
  175. dev_priv->ips.c_m = 0;
  176. } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
  177. dev_priv->ips.c_m = 1;
  178. } else {
  179. dev_priv->ips.c_m = 2;
  180. }
  181. }
  182. static const struct cxsr_latency cxsr_latency_table[] = {
  183. {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
  184. {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
  185. {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
  186. {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
  187. {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
  188. {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
  189. {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
  190. {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
  191. {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
  192. {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
  193. {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
  194. {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
  195. {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
  196. {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
  197. {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
  198. {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
  199. {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
  200. {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
  201. {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
  202. {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
  203. {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
  204. {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
  205. {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
  206. {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
  207. {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
  208. {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
  209. {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
  210. {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
  211. {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
  212. {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
  213. };
  214. static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
  215. int is_ddr3,
  216. int fsb,
  217. int mem)
  218. {
  219. const struct cxsr_latency *latency;
  220. int i;
  221. if (fsb == 0 || mem == 0)
  222. return NULL;
  223. for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
  224. latency = &cxsr_latency_table[i];
  225. if (is_desktop == latency->is_desktop &&
  226. is_ddr3 == latency->is_ddr3 &&
  227. fsb == latency->fsb_freq && mem == latency->mem_freq)
  228. return latency;
  229. }
  230. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  231. return NULL;
  232. }
  233. void intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
  234. {
  235. struct drm_device *dev = dev_priv->dev;
  236. u32 val;
  237. if (IS_VALLEYVIEW(dev)) {
  238. I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
  239. } else if (IS_G4X(dev) || IS_CRESTLINE(dev)) {
  240. I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
  241. } else if (IS_PINEVIEW(dev)) {
  242. val = I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN;
  243. val |= enable ? PINEVIEW_SELF_REFRESH_EN : 0;
  244. I915_WRITE(DSPFW3, val);
  245. } else if (IS_I945G(dev) || IS_I945GM(dev)) {
  246. val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
  247. _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
  248. I915_WRITE(FW_BLC_SELF, val);
  249. } else if (IS_I915GM(dev)) {
  250. val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
  251. _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
  252. I915_WRITE(INSTPM, val);
  253. } else {
  254. return;
  255. }
  256. DRM_DEBUG_KMS("memory self-refresh is %s\n",
  257. enable ? "enabled" : "disabled");
  258. }
  259. /*
  260. * Latency for FIFO fetches is dependent on several factors:
  261. * - memory configuration (speed, channels)
  262. * - chipset
  263. * - current MCH state
  264. * It can be fairly high in some situations, so here we assume a fairly
  265. * pessimal value. It's a tradeoff between extra memory fetches (if we
  266. * set this value too high, the FIFO will fetch frequently to stay full)
  267. * and power consumption (set it too low to save power and we might see
  268. * FIFO underruns and display "flicker").
  269. *
  270. * A value of 5us seems to be a good balance; safe for very low end
  271. * platforms but not overly aggressive on lower latency configs.
  272. */
  273. static const int pessimal_latency_ns = 5000;
  274. static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
  275. {
  276. struct drm_i915_private *dev_priv = dev->dev_private;
  277. uint32_t dsparb = I915_READ(DSPARB);
  278. int size;
  279. size = dsparb & 0x7f;
  280. if (plane)
  281. size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
  282. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  283. plane ? "B" : "A", size);
  284. return size;
  285. }
  286. static int i830_get_fifo_size(struct drm_device *dev, int plane)
  287. {
  288. struct drm_i915_private *dev_priv = dev->dev_private;
  289. uint32_t dsparb = I915_READ(DSPARB);
  290. int size;
  291. size = dsparb & 0x1ff;
  292. if (plane)
  293. size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
  294. size >>= 1; /* Convert to cachelines */
  295. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  296. plane ? "B" : "A", size);
  297. return size;
  298. }
  299. static int i845_get_fifo_size(struct drm_device *dev, int plane)
  300. {
  301. struct drm_i915_private *dev_priv = dev->dev_private;
  302. uint32_t dsparb = I915_READ(DSPARB);
  303. int size;
  304. size = dsparb & 0x7f;
  305. size >>= 2; /* Convert to cachelines */
  306. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  307. plane ? "B" : "A",
  308. size);
  309. return size;
  310. }
  311. /* Pineview has different values for various configs */
  312. static const struct intel_watermark_params pineview_display_wm = {
  313. .fifo_size = PINEVIEW_DISPLAY_FIFO,
  314. .max_wm = PINEVIEW_MAX_WM,
  315. .default_wm = PINEVIEW_DFT_WM,
  316. .guard_size = PINEVIEW_GUARD_WM,
  317. .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
  318. };
  319. static const struct intel_watermark_params pineview_display_hplloff_wm = {
  320. .fifo_size = PINEVIEW_DISPLAY_FIFO,
  321. .max_wm = PINEVIEW_MAX_WM,
  322. .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
  323. .guard_size = PINEVIEW_GUARD_WM,
  324. .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
  325. };
  326. static const struct intel_watermark_params pineview_cursor_wm = {
  327. .fifo_size = PINEVIEW_CURSOR_FIFO,
  328. .max_wm = PINEVIEW_CURSOR_MAX_WM,
  329. .default_wm = PINEVIEW_CURSOR_DFT_WM,
  330. .guard_size = PINEVIEW_CURSOR_GUARD_WM,
  331. .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
  332. };
  333. static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
  334. .fifo_size = PINEVIEW_CURSOR_FIFO,
  335. .max_wm = PINEVIEW_CURSOR_MAX_WM,
  336. .default_wm = PINEVIEW_CURSOR_DFT_WM,
  337. .guard_size = PINEVIEW_CURSOR_GUARD_WM,
  338. .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
  339. };
  340. static const struct intel_watermark_params g4x_wm_info = {
  341. .fifo_size = G4X_FIFO_SIZE,
  342. .max_wm = G4X_MAX_WM,
  343. .default_wm = G4X_MAX_WM,
  344. .guard_size = 2,
  345. .cacheline_size = G4X_FIFO_LINE_SIZE,
  346. };
  347. static const struct intel_watermark_params g4x_cursor_wm_info = {
  348. .fifo_size = I965_CURSOR_FIFO,
  349. .max_wm = I965_CURSOR_MAX_WM,
  350. .default_wm = I965_CURSOR_DFT_WM,
  351. .guard_size = 2,
  352. .cacheline_size = G4X_FIFO_LINE_SIZE,
  353. };
  354. static const struct intel_watermark_params valleyview_wm_info = {
  355. .fifo_size = VALLEYVIEW_FIFO_SIZE,
  356. .max_wm = VALLEYVIEW_MAX_WM,
  357. .default_wm = VALLEYVIEW_MAX_WM,
  358. .guard_size = 2,
  359. .cacheline_size = G4X_FIFO_LINE_SIZE,
  360. };
  361. static const struct intel_watermark_params valleyview_cursor_wm_info = {
  362. .fifo_size = I965_CURSOR_FIFO,
  363. .max_wm = VALLEYVIEW_CURSOR_MAX_WM,
  364. .default_wm = I965_CURSOR_DFT_WM,
  365. .guard_size = 2,
  366. .cacheline_size = G4X_FIFO_LINE_SIZE,
  367. };
  368. static const struct intel_watermark_params i965_cursor_wm_info = {
  369. .fifo_size = I965_CURSOR_FIFO,
  370. .max_wm = I965_CURSOR_MAX_WM,
  371. .default_wm = I965_CURSOR_DFT_WM,
  372. .guard_size = 2,
  373. .cacheline_size = I915_FIFO_LINE_SIZE,
  374. };
  375. static const struct intel_watermark_params i945_wm_info = {
  376. .fifo_size = I945_FIFO_SIZE,
  377. .max_wm = I915_MAX_WM,
  378. .default_wm = 1,
  379. .guard_size = 2,
  380. .cacheline_size = I915_FIFO_LINE_SIZE,
  381. };
  382. static const struct intel_watermark_params i915_wm_info = {
  383. .fifo_size = I915_FIFO_SIZE,
  384. .max_wm = I915_MAX_WM,
  385. .default_wm = 1,
  386. .guard_size = 2,
  387. .cacheline_size = I915_FIFO_LINE_SIZE,
  388. };
  389. static const struct intel_watermark_params i830_a_wm_info = {
  390. .fifo_size = I855GM_FIFO_SIZE,
  391. .max_wm = I915_MAX_WM,
  392. .default_wm = 1,
  393. .guard_size = 2,
  394. .cacheline_size = I830_FIFO_LINE_SIZE,
  395. };
  396. static const struct intel_watermark_params i830_bc_wm_info = {
  397. .fifo_size = I855GM_FIFO_SIZE,
  398. .max_wm = I915_MAX_WM/2,
  399. .default_wm = 1,
  400. .guard_size = 2,
  401. .cacheline_size = I830_FIFO_LINE_SIZE,
  402. };
  403. static const struct intel_watermark_params i845_wm_info = {
  404. .fifo_size = I830_FIFO_SIZE,
  405. .max_wm = I915_MAX_WM,
  406. .default_wm = 1,
  407. .guard_size = 2,
  408. .cacheline_size = I830_FIFO_LINE_SIZE,
  409. };
  410. /**
  411. * intel_calculate_wm - calculate watermark level
  412. * @clock_in_khz: pixel clock
  413. * @wm: chip FIFO params
  414. * @pixel_size: display pixel size
  415. * @latency_ns: memory latency for the platform
  416. *
  417. * Calculate the watermark level (the level at which the display plane will
  418. * start fetching from memory again). Each chip has a different display
  419. * FIFO size and allocation, so the caller needs to figure that out and pass
  420. * in the correct intel_watermark_params structure.
  421. *
  422. * As the pixel clock runs, the FIFO will be drained at a rate that depends
  423. * on the pixel size. When it reaches the watermark level, it'll start
  424. * fetching FIFO line sized based chunks from memory until the FIFO fills
  425. * past the watermark point. If the FIFO drains completely, a FIFO underrun
  426. * will occur, and a display engine hang could result.
  427. */
  428. static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
  429. const struct intel_watermark_params *wm,
  430. int fifo_size,
  431. int pixel_size,
  432. unsigned long latency_ns)
  433. {
  434. long entries_required, wm_size;
  435. /*
  436. * Note: we need to make sure we don't overflow for various clock &
  437. * latency values.
  438. * clocks go from a few thousand to several hundred thousand.
  439. * latency is usually a few thousand
  440. */
  441. entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
  442. 1000;
  443. entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
  444. DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
  445. wm_size = fifo_size - (entries_required + wm->guard_size);
  446. DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
  447. /* Don't promote wm_size to unsigned... */
  448. if (wm_size > (long)wm->max_wm)
  449. wm_size = wm->max_wm;
  450. if (wm_size <= 0)
  451. wm_size = wm->default_wm;
  452. /*
  453. * Bspec seems to indicate that the value shouldn't be lower than
  454. * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
  455. * Lets go for 8 which is the burst size since certain platforms
  456. * already use a hardcoded 8 (which is what the spec says should be
  457. * done).
  458. */
  459. if (wm_size <= 8)
  460. wm_size = 8;
  461. return wm_size;
  462. }
  463. static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
  464. {
  465. struct drm_crtc *crtc, *enabled = NULL;
  466. for_each_crtc(dev, crtc) {
  467. if (intel_crtc_active(crtc)) {
  468. if (enabled)
  469. return NULL;
  470. enabled = crtc;
  471. }
  472. }
  473. return enabled;
  474. }
  475. static void pineview_update_wm(struct drm_crtc *unused_crtc)
  476. {
  477. struct drm_device *dev = unused_crtc->dev;
  478. struct drm_i915_private *dev_priv = dev->dev_private;
  479. struct drm_crtc *crtc;
  480. const struct cxsr_latency *latency;
  481. u32 reg;
  482. unsigned long wm;
  483. latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
  484. dev_priv->fsb_freq, dev_priv->mem_freq);
  485. if (!latency) {
  486. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  487. intel_set_memory_cxsr(dev_priv, false);
  488. return;
  489. }
  490. crtc = single_enabled_crtc(dev);
  491. if (crtc) {
  492. const struct drm_display_mode *adjusted_mode;
  493. int pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
  494. int clock;
  495. adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
  496. clock = adjusted_mode->crtc_clock;
  497. /* Display SR */
  498. wm = intel_calculate_wm(clock, &pineview_display_wm,
  499. pineview_display_wm.fifo_size,
  500. pixel_size, latency->display_sr);
  501. reg = I915_READ(DSPFW1);
  502. reg &= ~DSPFW_SR_MASK;
  503. reg |= wm << DSPFW_SR_SHIFT;
  504. I915_WRITE(DSPFW1, reg);
  505. DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
  506. /* cursor SR */
  507. wm = intel_calculate_wm(clock, &pineview_cursor_wm,
  508. pineview_display_wm.fifo_size,
  509. pixel_size, latency->cursor_sr);
  510. reg = I915_READ(DSPFW3);
  511. reg &= ~DSPFW_CURSOR_SR_MASK;
  512. reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
  513. I915_WRITE(DSPFW3, reg);
  514. /* Display HPLL off SR */
  515. wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
  516. pineview_display_hplloff_wm.fifo_size,
  517. pixel_size, latency->display_hpll_disable);
  518. reg = I915_READ(DSPFW3);
  519. reg &= ~DSPFW_HPLL_SR_MASK;
  520. reg |= wm & DSPFW_HPLL_SR_MASK;
  521. I915_WRITE(DSPFW3, reg);
  522. /* cursor HPLL off SR */
  523. wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
  524. pineview_display_hplloff_wm.fifo_size,
  525. pixel_size, latency->cursor_hpll_disable);
  526. reg = I915_READ(DSPFW3);
  527. reg &= ~DSPFW_HPLL_CURSOR_MASK;
  528. reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
  529. I915_WRITE(DSPFW3, reg);
  530. DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
  531. intel_set_memory_cxsr(dev_priv, true);
  532. } else {
  533. intel_set_memory_cxsr(dev_priv, false);
  534. }
  535. }
  536. static bool g4x_compute_wm0(struct drm_device *dev,
  537. int plane,
  538. const struct intel_watermark_params *display,
  539. int display_latency_ns,
  540. const struct intel_watermark_params *cursor,
  541. int cursor_latency_ns,
  542. int *plane_wm,
  543. int *cursor_wm)
  544. {
  545. struct drm_crtc *crtc;
  546. const struct drm_display_mode *adjusted_mode;
  547. int htotal, hdisplay, clock, pixel_size;
  548. int line_time_us, line_count;
  549. int entries, tlb_miss;
  550. crtc = intel_get_crtc_for_plane(dev, plane);
  551. if (!intel_crtc_active(crtc)) {
  552. *cursor_wm = cursor->guard_size;
  553. *plane_wm = display->guard_size;
  554. return false;
  555. }
  556. adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
  557. clock = adjusted_mode->crtc_clock;
  558. htotal = adjusted_mode->crtc_htotal;
  559. hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
  560. pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
  561. /* Use the small buffer method to calculate plane watermark */
  562. entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
  563. tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
  564. if (tlb_miss > 0)
  565. entries += tlb_miss;
  566. entries = DIV_ROUND_UP(entries, display->cacheline_size);
  567. *plane_wm = entries + display->guard_size;
  568. if (*plane_wm > (int)display->max_wm)
  569. *plane_wm = display->max_wm;
  570. /* Use the large buffer method to calculate cursor watermark */
  571. line_time_us = max(htotal * 1000 / clock, 1);
  572. line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
  573. entries = line_count * crtc->cursor->state->crtc_w * pixel_size;
  574. tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
  575. if (tlb_miss > 0)
  576. entries += tlb_miss;
  577. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  578. *cursor_wm = entries + cursor->guard_size;
  579. if (*cursor_wm > (int)cursor->max_wm)
  580. *cursor_wm = (int)cursor->max_wm;
  581. return true;
  582. }
  583. /*
  584. * Check the wm result.
  585. *
  586. * If any calculated watermark values is larger than the maximum value that
  587. * can be programmed into the associated watermark register, that watermark
  588. * must be disabled.
  589. */
  590. static bool g4x_check_srwm(struct drm_device *dev,
  591. int display_wm, int cursor_wm,
  592. const struct intel_watermark_params *display,
  593. const struct intel_watermark_params *cursor)
  594. {
  595. DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
  596. display_wm, cursor_wm);
  597. if (display_wm > display->max_wm) {
  598. DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
  599. display_wm, display->max_wm);
  600. return false;
  601. }
  602. if (cursor_wm > cursor->max_wm) {
  603. DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
  604. cursor_wm, cursor->max_wm);
  605. return false;
  606. }
  607. if (!(display_wm || cursor_wm)) {
  608. DRM_DEBUG_KMS("SR latency is 0, disabling\n");
  609. return false;
  610. }
  611. return true;
  612. }
  613. static bool g4x_compute_srwm(struct drm_device *dev,
  614. int plane,
  615. int latency_ns,
  616. const struct intel_watermark_params *display,
  617. const struct intel_watermark_params *cursor,
  618. int *display_wm, int *cursor_wm)
  619. {
  620. struct drm_crtc *crtc;
  621. const struct drm_display_mode *adjusted_mode;
  622. int hdisplay, htotal, pixel_size, clock;
  623. unsigned long line_time_us;
  624. int line_count, line_size;
  625. int small, large;
  626. int entries;
  627. if (!latency_ns) {
  628. *display_wm = *cursor_wm = 0;
  629. return false;
  630. }
  631. crtc = intel_get_crtc_for_plane(dev, plane);
  632. adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
  633. clock = adjusted_mode->crtc_clock;
  634. htotal = adjusted_mode->crtc_htotal;
  635. hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
  636. pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
  637. line_time_us = max(htotal * 1000 / clock, 1);
  638. line_count = (latency_ns / line_time_us + 1000) / 1000;
  639. line_size = hdisplay * pixel_size;
  640. /* Use the minimum of the small and large buffer method for primary */
  641. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  642. large = line_count * line_size;
  643. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  644. *display_wm = entries + display->guard_size;
  645. /* calculate the self-refresh watermark for display cursor */
  646. entries = line_count * pixel_size * crtc->cursor->state->crtc_w;
  647. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  648. *cursor_wm = entries + cursor->guard_size;
  649. return g4x_check_srwm(dev,
  650. *display_wm, *cursor_wm,
  651. display, cursor);
  652. }
  653. static bool vlv_compute_drain_latency(struct drm_crtc *crtc,
  654. int pixel_size,
  655. int *prec_mult,
  656. int *drain_latency)
  657. {
  658. struct drm_device *dev = crtc->dev;
  659. int entries;
  660. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  661. if (WARN(clock == 0, "Pixel clock is zero!\n"))
  662. return false;
  663. if (WARN(pixel_size == 0, "Pixel size is zero!\n"))
  664. return false;
  665. entries = DIV_ROUND_UP(clock, 1000) * pixel_size;
  666. if (IS_CHERRYVIEW(dev))
  667. *prec_mult = (entries > 32) ? 16 : 8;
  668. else
  669. *prec_mult = (entries > 128) ? 64 : 32;
  670. *drain_latency = (64 * (*prec_mult) * 4) / entries;
  671. if (*drain_latency > DRAIN_LATENCY_MASK)
  672. *drain_latency = DRAIN_LATENCY_MASK;
  673. return true;
  674. }
  675. /*
  676. * Update drain latency registers of memory arbiter
  677. *
  678. * Valleyview SoC has a new memory arbiter and needs drain latency registers
  679. * to be programmed. Each plane has a drain latency multiplier and a drain
  680. * latency value.
  681. */
  682. static void vlv_update_drain_latency(struct drm_crtc *crtc)
  683. {
  684. struct drm_device *dev = crtc->dev;
  685. struct drm_i915_private *dev_priv = dev->dev_private;
  686. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  687. int pixel_size;
  688. int drain_latency;
  689. enum pipe pipe = intel_crtc->pipe;
  690. int plane_prec, prec_mult, plane_dl;
  691. const int high_precision = IS_CHERRYVIEW(dev) ? 16 : 64;
  692. plane_dl = I915_READ(VLV_DDL(pipe)) & ~(DDL_PLANE_PRECISION_HIGH |
  693. DRAIN_LATENCY_MASK | DDL_CURSOR_PRECISION_HIGH |
  694. (DRAIN_LATENCY_MASK << DDL_CURSOR_SHIFT));
  695. if (!intel_crtc_active(crtc)) {
  696. I915_WRITE(VLV_DDL(pipe), plane_dl);
  697. return;
  698. }
  699. /* Primary plane Drain Latency */
  700. pixel_size = crtc->primary->state->fb->bits_per_pixel / 8; /* BPP */
  701. if (vlv_compute_drain_latency(crtc, pixel_size, &prec_mult, &drain_latency)) {
  702. plane_prec = (prec_mult == high_precision) ?
  703. DDL_PLANE_PRECISION_HIGH :
  704. DDL_PLANE_PRECISION_LOW;
  705. plane_dl |= plane_prec | drain_latency;
  706. }
  707. /* Cursor Drain Latency
  708. * BPP is always 4 for cursor
  709. */
  710. pixel_size = 4;
  711. /* Program cursor DL only if it is enabled */
  712. if (intel_crtc->cursor_base &&
  713. vlv_compute_drain_latency(crtc, pixel_size, &prec_mult, &drain_latency)) {
  714. plane_prec = (prec_mult == high_precision) ?
  715. DDL_CURSOR_PRECISION_HIGH :
  716. DDL_CURSOR_PRECISION_LOW;
  717. plane_dl |= plane_prec | (drain_latency << DDL_CURSOR_SHIFT);
  718. }
  719. I915_WRITE(VLV_DDL(pipe), plane_dl);
  720. }
  721. #define single_plane_enabled(mask) is_power_of_2(mask)
  722. static void valleyview_update_wm(struct drm_crtc *crtc)
  723. {
  724. struct drm_device *dev = crtc->dev;
  725. static const int sr_latency_ns = 12000;
  726. struct drm_i915_private *dev_priv = dev->dev_private;
  727. int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
  728. int plane_sr, cursor_sr;
  729. int ignore_plane_sr, ignore_cursor_sr;
  730. unsigned int enabled = 0;
  731. bool cxsr_enabled;
  732. vlv_update_drain_latency(crtc);
  733. if (g4x_compute_wm0(dev, PIPE_A,
  734. &valleyview_wm_info, pessimal_latency_ns,
  735. &valleyview_cursor_wm_info, pessimal_latency_ns,
  736. &planea_wm, &cursora_wm))
  737. enabled |= 1 << PIPE_A;
  738. if (g4x_compute_wm0(dev, PIPE_B,
  739. &valleyview_wm_info, pessimal_latency_ns,
  740. &valleyview_cursor_wm_info, pessimal_latency_ns,
  741. &planeb_wm, &cursorb_wm))
  742. enabled |= 1 << PIPE_B;
  743. if (single_plane_enabled(enabled) &&
  744. g4x_compute_srwm(dev, ffs(enabled) - 1,
  745. sr_latency_ns,
  746. &valleyview_wm_info,
  747. &valleyview_cursor_wm_info,
  748. &plane_sr, &ignore_cursor_sr) &&
  749. g4x_compute_srwm(dev, ffs(enabled) - 1,
  750. 2*sr_latency_ns,
  751. &valleyview_wm_info,
  752. &valleyview_cursor_wm_info,
  753. &ignore_plane_sr, &cursor_sr)) {
  754. cxsr_enabled = true;
  755. } else {
  756. cxsr_enabled = false;
  757. intel_set_memory_cxsr(dev_priv, false);
  758. plane_sr = cursor_sr = 0;
  759. }
  760. DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
  761. "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
  762. planea_wm, cursora_wm,
  763. planeb_wm, cursorb_wm,
  764. plane_sr, cursor_sr);
  765. I915_WRITE(DSPFW1,
  766. (plane_sr << DSPFW_SR_SHIFT) |
  767. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  768. (planeb_wm << DSPFW_PLANEB_SHIFT) |
  769. (planea_wm << DSPFW_PLANEA_SHIFT));
  770. I915_WRITE(DSPFW2,
  771. (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
  772. (cursora_wm << DSPFW_CURSORA_SHIFT));
  773. I915_WRITE(DSPFW3,
  774. (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
  775. (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  776. if (cxsr_enabled)
  777. intel_set_memory_cxsr(dev_priv, true);
  778. }
  779. static void cherryview_update_wm(struct drm_crtc *crtc)
  780. {
  781. struct drm_device *dev = crtc->dev;
  782. static const int sr_latency_ns = 12000;
  783. struct drm_i915_private *dev_priv = dev->dev_private;
  784. int planea_wm, planeb_wm, planec_wm;
  785. int cursora_wm, cursorb_wm, cursorc_wm;
  786. int plane_sr, cursor_sr;
  787. int ignore_plane_sr, ignore_cursor_sr;
  788. unsigned int enabled = 0;
  789. bool cxsr_enabled;
  790. vlv_update_drain_latency(crtc);
  791. if (g4x_compute_wm0(dev, PIPE_A,
  792. &valleyview_wm_info, pessimal_latency_ns,
  793. &valleyview_cursor_wm_info, pessimal_latency_ns,
  794. &planea_wm, &cursora_wm))
  795. enabled |= 1 << PIPE_A;
  796. if (g4x_compute_wm0(dev, PIPE_B,
  797. &valleyview_wm_info, pessimal_latency_ns,
  798. &valleyview_cursor_wm_info, pessimal_latency_ns,
  799. &planeb_wm, &cursorb_wm))
  800. enabled |= 1 << PIPE_B;
  801. if (g4x_compute_wm0(dev, PIPE_C,
  802. &valleyview_wm_info, pessimal_latency_ns,
  803. &valleyview_cursor_wm_info, pessimal_latency_ns,
  804. &planec_wm, &cursorc_wm))
  805. enabled |= 1 << PIPE_C;
  806. if (single_plane_enabled(enabled) &&
  807. g4x_compute_srwm(dev, ffs(enabled) - 1,
  808. sr_latency_ns,
  809. &valleyview_wm_info,
  810. &valleyview_cursor_wm_info,
  811. &plane_sr, &ignore_cursor_sr) &&
  812. g4x_compute_srwm(dev, ffs(enabled) - 1,
  813. 2*sr_latency_ns,
  814. &valleyview_wm_info,
  815. &valleyview_cursor_wm_info,
  816. &ignore_plane_sr, &cursor_sr)) {
  817. cxsr_enabled = true;
  818. } else {
  819. cxsr_enabled = false;
  820. intel_set_memory_cxsr(dev_priv, false);
  821. plane_sr = cursor_sr = 0;
  822. }
  823. DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
  824. "B: plane=%d, cursor=%d, C: plane=%d, cursor=%d, "
  825. "SR: plane=%d, cursor=%d\n",
  826. planea_wm, cursora_wm,
  827. planeb_wm, cursorb_wm,
  828. planec_wm, cursorc_wm,
  829. plane_sr, cursor_sr);
  830. I915_WRITE(DSPFW1,
  831. (plane_sr << DSPFW_SR_SHIFT) |
  832. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  833. (planeb_wm << DSPFW_PLANEB_SHIFT) |
  834. (planea_wm << DSPFW_PLANEA_SHIFT));
  835. I915_WRITE(DSPFW2,
  836. (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
  837. (cursora_wm << DSPFW_CURSORA_SHIFT));
  838. I915_WRITE(DSPFW3,
  839. (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
  840. (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  841. I915_WRITE(DSPFW9_CHV,
  842. (I915_READ(DSPFW9_CHV) & ~(DSPFW_PLANEC_MASK |
  843. DSPFW_CURSORC_MASK)) |
  844. (planec_wm << DSPFW_PLANEC_SHIFT) |
  845. (cursorc_wm << DSPFW_CURSORC_SHIFT));
  846. if (cxsr_enabled)
  847. intel_set_memory_cxsr(dev_priv, true);
  848. }
  849. static void valleyview_update_sprite_wm(struct drm_plane *plane,
  850. struct drm_crtc *crtc,
  851. uint32_t sprite_width,
  852. uint32_t sprite_height,
  853. int pixel_size,
  854. bool enabled, bool scaled)
  855. {
  856. struct drm_device *dev = crtc->dev;
  857. struct drm_i915_private *dev_priv = dev->dev_private;
  858. int pipe = to_intel_plane(plane)->pipe;
  859. int sprite = to_intel_plane(plane)->plane;
  860. int drain_latency;
  861. int plane_prec;
  862. int sprite_dl;
  863. int prec_mult;
  864. const int high_precision = IS_CHERRYVIEW(dev) ? 16 : 64;
  865. sprite_dl = I915_READ(VLV_DDL(pipe)) & ~(DDL_SPRITE_PRECISION_HIGH(sprite) |
  866. (DRAIN_LATENCY_MASK << DDL_SPRITE_SHIFT(sprite)));
  867. if (enabled && vlv_compute_drain_latency(crtc, pixel_size, &prec_mult,
  868. &drain_latency)) {
  869. plane_prec = (prec_mult == high_precision) ?
  870. DDL_SPRITE_PRECISION_HIGH(sprite) :
  871. DDL_SPRITE_PRECISION_LOW(sprite);
  872. sprite_dl |= plane_prec |
  873. (drain_latency << DDL_SPRITE_SHIFT(sprite));
  874. }
  875. I915_WRITE(VLV_DDL(pipe), sprite_dl);
  876. }
  877. static void g4x_update_wm(struct drm_crtc *crtc)
  878. {
  879. struct drm_device *dev = crtc->dev;
  880. static const int sr_latency_ns = 12000;
  881. struct drm_i915_private *dev_priv = dev->dev_private;
  882. int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
  883. int plane_sr, cursor_sr;
  884. unsigned int enabled = 0;
  885. bool cxsr_enabled;
  886. if (g4x_compute_wm0(dev, PIPE_A,
  887. &g4x_wm_info, pessimal_latency_ns,
  888. &g4x_cursor_wm_info, pessimal_latency_ns,
  889. &planea_wm, &cursora_wm))
  890. enabled |= 1 << PIPE_A;
  891. if (g4x_compute_wm0(dev, PIPE_B,
  892. &g4x_wm_info, pessimal_latency_ns,
  893. &g4x_cursor_wm_info, pessimal_latency_ns,
  894. &planeb_wm, &cursorb_wm))
  895. enabled |= 1 << PIPE_B;
  896. if (single_plane_enabled(enabled) &&
  897. g4x_compute_srwm(dev, ffs(enabled) - 1,
  898. sr_latency_ns,
  899. &g4x_wm_info,
  900. &g4x_cursor_wm_info,
  901. &plane_sr, &cursor_sr)) {
  902. cxsr_enabled = true;
  903. } else {
  904. cxsr_enabled = false;
  905. intel_set_memory_cxsr(dev_priv, false);
  906. plane_sr = cursor_sr = 0;
  907. }
  908. DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
  909. "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
  910. planea_wm, cursora_wm,
  911. planeb_wm, cursorb_wm,
  912. plane_sr, cursor_sr);
  913. I915_WRITE(DSPFW1,
  914. (plane_sr << DSPFW_SR_SHIFT) |
  915. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  916. (planeb_wm << DSPFW_PLANEB_SHIFT) |
  917. (planea_wm << DSPFW_PLANEA_SHIFT));
  918. I915_WRITE(DSPFW2,
  919. (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
  920. (cursora_wm << DSPFW_CURSORA_SHIFT));
  921. /* HPLL off in SR has some issues on G4x... disable it */
  922. I915_WRITE(DSPFW3,
  923. (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
  924. (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  925. if (cxsr_enabled)
  926. intel_set_memory_cxsr(dev_priv, true);
  927. }
  928. static void i965_update_wm(struct drm_crtc *unused_crtc)
  929. {
  930. struct drm_device *dev = unused_crtc->dev;
  931. struct drm_i915_private *dev_priv = dev->dev_private;
  932. struct drm_crtc *crtc;
  933. int srwm = 1;
  934. int cursor_sr = 16;
  935. bool cxsr_enabled;
  936. /* Calc sr entries for one plane configs */
  937. crtc = single_enabled_crtc(dev);
  938. if (crtc) {
  939. /* self-refresh has much higher latency */
  940. static const int sr_latency_ns = 12000;
  941. const struct drm_display_mode *adjusted_mode =
  942. &to_intel_crtc(crtc)->config->base.adjusted_mode;
  943. int clock = adjusted_mode->crtc_clock;
  944. int htotal = adjusted_mode->crtc_htotal;
  945. int hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
  946. int pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
  947. unsigned long line_time_us;
  948. int entries;
  949. line_time_us = max(htotal * 1000 / clock, 1);
  950. /* Use ns/us then divide to preserve precision */
  951. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  952. pixel_size * hdisplay;
  953. entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
  954. srwm = I965_FIFO_SIZE - entries;
  955. if (srwm < 0)
  956. srwm = 1;
  957. srwm &= 0x1ff;
  958. DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
  959. entries, srwm);
  960. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  961. pixel_size * crtc->cursor->state->crtc_w;
  962. entries = DIV_ROUND_UP(entries,
  963. i965_cursor_wm_info.cacheline_size);
  964. cursor_sr = i965_cursor_wm_info.fifo_size -
  965. (entries + i965_cursor_wm_info.guard_size);
  966. if (cursor_sr > i965_cursor_wm_info.max_wm)
  967. cursor_sr = i965_cursor_wm_info.max_wm;
  968. DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
  969. "cursor %d\n", srwm, cursor_sr);
  970. cxsr_enabled = true;
  971. } else {
  972. cxsr_enabled = false;
  973. /* Turn off self refresh if both pipes are enabled */
  974. intel_set_memory_cxsr(dev_priv, false);
  975. }
  976. DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
  977. srwm);
  978. /* 965 has limitations... */
  979. I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
  980. (8 << DSPFW_CURSORB_SHIFT) |
  981. (8 << DSPFW_PLANEB_SHIFT) |
  982. (8 << DSPFW_PLANEA_SHIFT));
  983. I915_WRITE(DSPFW2, (8 << DSPFW_CURSORA_SHIFT) |
  984. (8 << DSPFW_PLANEC_SHIFT_OLD));
  985. /* update cursor SR watermark */
  986. I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  987. if (cxsr_enabled)
  988. intel_set_memory_cxsr(dev_priv, true);
  989. }
  990. static void i9xx_update_wm(struct drm_crtc *unused_crtc)
  991. {
  992. struct drm_device *dev = unused_crtc->dev;
  993. struct drm_i915_private *dev_priv = dev->dev_private;
  994. const struct intel_watermark_params *wm_info;
  995. uint32_t fwater_lo;
  996. uint32_t fwater_hi;
  997. int cwm, srwm = 1;
  998. int fifo_size;
  999. int planea_wm, planeb_wm;
  1000. struct drm_crtc *crtc, *enabled = NULL;
  1001. if (IS_I945GM(dev))
  1002. wm_info = &i945_wm_info;
  1003. else if (!IS_GEN2(dev))
  1004. wm_info = &i915_wm_info;
  1005. else
  1006. wm_info = &i830_a_wm_info;
  1007. fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  1008. crtc = intel_get_crtc_for_plane(dev, 0);
  1009. if (intel_crtc_active(crtc)) {
  1010. const struct drm_display_mode *adjusted_mode;
  1011. int cpp = crtc->primary->state->fb->bits_per_pixel / 8;
  1012. if (IS_GEN2(dev))
  1013. cpp = 4;
  1014. adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
  1015. planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
  1016. wm_info, fifo_size, cpp,
  1017. pessimal_latency_ns);
  1018. enabled = crtc;
  1019. } else {
  1020. planea_wm = fifo_size - wm_info->guard_size;
  1021. if (planea_wm > (long)wm_info->max_wm)
  1022. planea_wm = wm_info->max_wm;
  1023. }
  1024. if (IS_GEN2(dev))
  1025. wm_info = &i830_bc_wm_info;
  1026. fifo_size = dev_priv->display.get_fifo_size(dev, 1);
  1027. crtc = intel_get_crtc_for_plane(dev, 1);
  1028. if (intel_crtc_active(crtc)) {
  1029. const struct drm_display_mode *adjusted_mode;
  1030. int cpp = crtc->primary->state->fb->bits_per_pixel / 8;
  1031. if (IS_GEN2(dev))
  1032. cpp = 4;
  1033. adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
  1034. planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
  1035. wm_info, fifo_size, cpp,
  1036. pessimal_latency_ns);
  1037. if (enabled == NULL)
  1038. enabled = crtc;
  1039. else
  1040. enabled = NULL;
  1041. } else {
  1042. planeb_wm = fifo_size - wm_info->guard_size;
  1043. if (planeb_wm > (long)wm_info->max_wm)
  1044. planeb_wm = wm_info->max_wm;
  1045. }
  1046. DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  1047. if (IS_I915GM(dev) && enabled) {
  1048. struct drm_i915_gem_object *obj;
  1049. obj = intel_fb_obj(enabled->primary->state->fb);
  1050. /* self-refresh seems busted with untiled */
  1051. if (obj->tiling_mode == I915_TILING_NONE)
  1052. enabled = NULL;
  1053. }
  1054. /*
  1055. * Overlay gets an aggressive default since video jitter is bad.
  1056. */
  1057. cwm = 2;
  1058. /* Play safe and disable self-refresh before adjusting watermarks. */
  1059. intel_set_memory_cxsr(dev_priv, false);
  1060. /* Calc sr entries for one plane configs */
  1061. if (HAS_FW_BLC(dev) && enabled) {
  1062. /* self-refresh has much higher latency */
  1063. static const int sr_latency_ns = 6000;
  1064. const struct drm_display_mode *adjusted_mode =
  1065. &to_intel_crtc(enabled)->config->base.adjusted_mode;
  1066. int clock = adjusted_mode->crtc_clock;
  1067. int htotal = adjusted_mode->crtc_htotal;
  1068. int hdisplay = to_intel_crtc(enabled)->config->pipe_src_w;
  1069. int pixel_size = enabled->primary->state->fb->bits_per_pixel / 8;
  1070. unsigned long line_time_us;
  1071. int entries;
  1072. line_time_us = max(htotal * 1000 / clock, 1);
  1073. /* Use ns/us then divide to preserve precision */
  1074. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  1075. pixel_size * hdisplay;
  1076. entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
  1077. DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
  1078. srwm = wm_info->fifo_size - entries;
  1079. if (srwm < 0)
  1080. srwm = 1;
  1081. if (IS_I945G(dev) || IS_I945GM(dev))
  1082. I915_WRITE(FW_BLC_SELF,
  1083. FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
  1084. else if (IS_I915GM(dev))
  1085. I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
  1086. }
  1087. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
  1088. planea_wm, planeb_wm, cwm, srwm);
  1089. fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
  1090. fwater_hi = (cwm & 0x1f);
  1091. /* Set request length to 8 cachelines per fetch */
  1092. fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
  1093. fwater_hi = fwater_hi | (1 << 8);
  1094. I915_WRITE(FW_BLC, fwater_lo);
  1095. I915_WRITE(FW_BLC2, fwater_hi);
  1096. if (enabled)
  1097. intel_set_memory_cxsr(dev_priv, true);
  1098. }
  1099. static void i845_update_wm(struct drm_crtc *unused_crtc)
  1100. {
  1101. struct drm_device *dev = unused_crtc->dev;
  1102. struct drm_i915_private *dev_priv = dev->dev_private;
  1103. struct drm_crtc *crtc;
  1104. const struct drm_display_mode *adjusted_mode;
  1105. uint32_t fwater_lo;
  1106. int planea_wm;
  1107. crtc = single_enabled_crtc(dev);
  1108. if (crtc == NULL)
  1109. return;
  1110. adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
  1111. planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
  1112. &i845_wm_info,
  1113. dev_priv->display.get_fifo_size(dev, 0),
  1114. 4, pessimal_latency_ns);
  1115. fwater_lo = I915_READ(FW_BLC) & ~0xfff;
  1116. fwater_lo |= (3<<8) | planea_wm;
  1117. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
  1118. I915_WRITE(FW_BLC, fwater_lo);
  1119. }
  1120. static uint32_t ilk_pipe_pixel_rate(struct drm_device *dev,
  1121. struct drm_crtc *crtc)
  1122. {
  1123. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1124. uint32_t pixel_rate;
  1125. pixel_rate = intel_crtc->config->base.adjusted_mode.crtc_clock;
  1126. /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
  1127. * adjust the pixel_rate here. */
  1128. if (intel_crtc->config->pch_pfit.enabled) {
  1129. uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
  1130. uint32_t pfit_size = intel_crtc->config->pch_pfit.size;
  1131. pipe_w = intel_crtc->config->pipe_src_w;
  1132. pipe_h = intel_crtc->config->pipe_src_h;
  1133. pfit_w = (pfit_size >> 16) & 0xFFFF;
  1134. pfit_h = pfit_size & 0xFFFF;
  1135. if (pipe_w < pfit_w)
  1136. pipe_w = pfit_w;
  1137. if (pipe_h < pfit_h)
  1138. pipe_h = pfit_h;
  1139. pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
  1140. pfit_w * pfit_h);
  1141. }
  1142. return pixel_rate;
  1143. }
  1144. /* latency must be in 0.1us units. */
  1145. static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
  1146. uint32_t latency)
  1147. {
  1148. uint64_t ret;
  1149. if (WARN(latency == 0, "Latency value missing\n"))
  1150. return UINT_MAX;
  1151. ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
  1152. ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
  1153. return ret;
  1154. }
  1155. /* latency must be in 0.1us units. */
  1156. static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
  1157. uint32_t horiz_pixels, uint8_t bytes_per_pixel,
  1158. uint32_t latency)
  1159. {
  1160. uint32_t ret;
  1161. if (WARN(latency == 0, "Latency value missing\n"))
  1162. return UINT_MAX;
  1163. ret = (latency * pixel_rate) / (pipe_htotal * 10000);
  1164. ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
  1165. ret = DIV_ROUND_UP(ret, 64) + 2;
  1166. return ret;
  1167. }
  1168. static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
  1169. uint8_t bytes_per_pixel)
  1170. {
  1171. return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
  1172. }
  1173. struct skl_pipe_wm_parameters {
  1174. bool active;
  1175. uint32_t pipe_htotal;
  1176. uint32_t pixel_rate; /* in KHz */
  1177. struct intel_plane_wm_parameters plane[I915_MAX_PLANES];
  1178. struct intel_plane_wm_parameters cursor;
  1179. };
  1180. struct ilk_pipe_wm_parameters {
  1181. bool active;
  1182. uint32_t pipe_htotal;
  1183. uint32_t pixel_rate;
  1184. struct intel_plane_wm_parameters pri;
  1185. struct intel_plane_wm_parameters spr;
  1186. struct intel_plane_wm_parameters cur;
  1187. };
  1188. struct ilk_wm_maximums {
  1189. uint16_t pri;
  1190. uint16_t spr;
  1191. uint16_t cur;
  1192. uint16_t fbc;
  1193. };
  1194. /* used in computing the new watermarks state */
  1195. struct intel_wm_config {
  1196. unsigned int num_pipes_active;
  1197. bool sprites_enabled;
  1198. bool sprites_scaled;
  1199. };
  1200. /*
  1201. * For both WM_PIPE and WM_LP.
  1202. * mem_value must be in 0.1us units.
  1203. */
  1204. static uint32_t ilk_compute_pri_wm(const struct ilk_pipe_wm_parameters *params,
  1205. uint32_t mem_value,
  1206. bool is_lp)
  1207. {
  1208. uint32_t method1, method2;
  1209. if (!params->active || !params->pri.enabled)
  1210. return 0;
  1211. method1 = ilk_wm_method1(params->pixel_rate,
  1212. params->pri.bytes_per_pixel,
  1213. mem_value);
  1214. if (!is_lp)
  1215. return method1;
  1216. method2 = ilk_wm_method2(params->pixel_rate,
  1217. params->pipe_htotal,
  1218. params->pri.horiz_pixels,
  1219. params->pri.bytes_per_pixel,
  1220. mem_value);
  1221. return min(method1, method2);
  1222. }
  1223. /*
  1224. * For both WM_PIPE and WM_LP.
  1225. * mem_value must be in 0.1us units.
  1226. */
  1227. static uint32_t ilk_compute_spr_wm(const struct ilk_pipe_wm_parameters *params,
  1228. uint32_t mem_value)
  1229. {
  1230. uint32_t method1, method2;
  1231. if (!params->active || !params->spr.enabled)
  1232. return 0;
  1233. method1 = ilk_wm_method1(params->pixel_rate,
  1234. params->spr.bytes_per_pixel,
  1235. mem_value);
  1236. method2 = ilk_wm_method2(params->pixel_rate,
  1237. params->pipe_htotal,
  1238. params->spr.horiz_pixels,
  1239. params->spr.bytes_per_pixel,
  1240. mem_value);
  1241. return min(method1, method2);
  1242. }
  1243. /*
  1244. * For both WM_PIPE and WM_LP.
  1245. * mem_value must be in 0.1us units.
  1246. */
  1247. static uint32_t ilk_compute_cur_wm(const struct ilk_pipe_wm_parameters *params,
  1248. uint32_t mem_value)
  1249. {
  1250. if (!params->active || !params->cur.enabled)
  1251. return 0;
  1252. return ilk_wm_method2(params->pixel_rate,
  1253. params->pipe_htotal,
  1254. params->cur.horiz_pixels,
  1255. params->cur.bytes_per_pixel,
  1256. mem_value);
  1257. }
  1258. /* Only for WM_LP. */
  1259. static uint32_t ilk_compute_fbc_wm(const struct ilk_pipe_wm_parameters *params,
  1260. uint32_t pri_val)
  1261. {
  1262. if (!params->active || !params->pri.enabled)
  1263. return 0;
  1264. return ilk_wm_fbc(pri_val,
  1265. params->pri.horiz_pixels,
  1266. params->pri.bytes_per_pixel);
  1267. }
  1268. static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
  1269. {
  1270. if (INTEL_INFO(dev)->gen >= 8)
  1271. return 3072;
  1272. else if (INTEL_INFO(dev)->gen >= 7)
  1273. return 768;
  1274. else
  1275. return 512;
  1276. }
  1277. static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
  1278. int level, bool is_sprite)
  1279. {
  1280. if (INTEL_INFO(dev)->gen >= 8)
  1281. /* BDW primary/sprite plane watermarks */
  1282. return level == 0 ? 255 : 2047;
  1283. else if (INTEL_INFO(dev)->gen >= 7)
  1284. /* IVB/HSW primary/sprite plane watermarks */
  1285. return level == 0 ? 127 : 1023;
  1286. else if (!is_sprite)
  1287. /* ILK/SNB primary plane watermarks */
  1288. return level == 0 ? 127 : 511;
  1289. else
  1290. /* ILK/SNB sprite plane watermarks */
  1291. return level == 0 ? 63 : 255;
  1292. }
  1293. static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
  1294. int level)
  1295. {
  1296. if (INTEL_INFO(dev)->gen >= 7)
  1297. return level == 0 ? 63 : 255;
  1298. else
  1299. return level == 0 ? 31 : 63;
  1300. }
  1301. static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
  1302. {
  1303. if (INTEL_INFO(dev)->gen >= 8)
  1304. return 31;
  1305. else
  1306. return 15;
  1307. }
  1308. /* Calculate the maximum primary/sprite plane watermark */
  1309. static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
  1310. int level,
  1311. const struct intel_wm_config *config,
  1312. enum intel_ddb_partitioning ddb_partitioning,
  1313. bool is_sprite)
  1314. {
  1315. unsigned int fifo_size = ilk_display_fifo_size(dev);
  1316. /* if sprites aren't enabled, sprites get nothing */
  1317. if (is_sprite && !config->sprites_enabled)
  1318. return 0;
  1319. /* HSW allows LP1+ watermarks even with multiple pipes */
  1320. if (level == 0 || config->num_pipes_active > 1) {
  1321. fifo_size /= INTEL_INFO(dev)->num_pipes;
  1322. /*
  1323. * For some reason the non self refresh
  1324. * FIFO size is only half of the self
  1325. * refresh FIFO size on ILK/SNB.
  1326. */
  1327. if (INTEL_INFO(dev)->gen <= 6)
  1328. fifo_size /= 2;
  1329. }
  1330. if (config->sprites_enabled) {
  1331. /* level 0 is always calculated with 1:1 split */
  1332. if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
  1333. if (is_sprite)
  1334. fifo_size *= 5;
  1335. fifo_size /= 6;
  1336. } else {
  1337. fifo_size /= 2;
  1338. }
  1339. }
  1340. /* clamp to max that the registers can hold */
  1341. return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
  1342. }
  1343. /* Calculate the maximum cursor plane watermark */
  1344. static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
  1345. int level,
  1346. const struct intel_wm_config *config)
  1347. {
  1348. /* HSW LP1+ watermarks w/ multiple pipes */
  1349. if (level > 0 && config->num_pipes_active > 1)
  1350. return 64;
  1351. /* otherwise just report max that registers can hold */
  1352. return ilk_cursor_wm_reg_max(dev, level);
  1353. }
  1354. static void ilk_compute_wm_maximums(const struct drm_device *dev,
  1355. int level,
  1356. const struct intel_wm_config *config,
  1357. enum intel_ddb_partitioning ddb_partitioning,
  1358. struct ilk_wm_maximums *max)
  1359. {
  1360. max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
  1361. max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
  1362. max->cur = ilk_cursor_wm_max(dev, level, config);
  1363. max->fbc = ilk_fbc_wm_reg_max(dev);
  1364. }
  1365. static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
  1366. int level,
  1367. struct ilk_wm_maximums *max)
  1368. {
  1369. max->pri = ilk_plane_wm_reg_max(dev, level, false);
  1370. max->spr = ilk_plane_wm_reg_max(dev, level, true);
  1371. max->cur = ilk_cursor_wm_reg_max(dev, level);
  1372. max->fbc = ilk_fbc_wm_reg_max(dev);
  1373. }
  1374. static bool ilk_validate_wm_level(int level,
  1375. const struct ilk_wm_maximums *max,
  1376. struct intel_wm_level *result)
  1377. {
  1378. bool ret;
  1379. /* already determined to be invalid? */
  1380. if (!result->enable)
  1381. return false;
  1382. result->enable = result->pri_val <= max->pri &&
  1383. result->spr_val <= max->spr &&
  1384. result->cur_val <= max->cur;
  1385. ret = result->enable;
  1386. /*
  1387. * HACK until we can pre-compute everything,
  1388. * and thus fail gracefully if LP0 watermarks
  1389. * are exceeded...
  1390. */
  1391. if (level == 0 && !result->enable) {
  1392. if (result->pri_val > max->pri)
  1393. DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
  1394. level, result->pri_val, max->pri);
  1395. if (result->spr_val > max->spr)
  1396. DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
  1397. level, result->spr_val, max->spr);
  1398. if (result->cur_val > max->cur)
  1399. DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
  1400. level, result->cur_val, max->cur);
  1401. result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
  1402. result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
  1403. result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
  1404. result->enable = true;
  1405. }
  1406. return ret;
  1407. }
  1408. static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
  1409. int level,
  1410. const struct ilk_pipe_wm_parameters *p,
  1411. struct intel_wm_level *result)
  1412. {
  1413. uint16_t pri_latency = dev_priv->wm.pri_latency[level];
  1414. uint16_t spr_latency = dev_priv->wm.spr_latency[level];
  1415. uint16_t cur_latency = dev_priv->wm.cur_latency[level];
  1416. /* WM1+ latency values stored in 0.5us units */
  1417. if (level > 0) {
  1418. pri_latency *= 5;
  1419. spr_latency *= 5;
  1420. cur_latency *= 5;
  1421. }
  1422. result->pri_val = ilk_compute_pri_wm(p, pri_latency, level);
  1423. result->spr_val = ilk_compute_spr_wm(p, spr_latency);
  1424. result->cur_val = ilk_compute_cur_wm(p, cur_latency);
  1425. result->fbc_val = ilk_compute_fbc_wm(p, result->pri_val);
  1426. result->enable = true;
  1427. }
  1428. static uint32_t
  1429. hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
  1430. {
  1431. struct drm_i915_private *dev_priv = dev->dev_private;
  1432. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1433. struct drm_display_mode *mode = &intel_crtc->config->base.adjusted_mode;
  1434. u32 linetime, ips_linetime;
  1435. if (!intel_crtc_active(crtc))
  1436. return 0;
  1437. /* The WM are computed with base on how long it takes to fill a single
  1438. * row at the given clock rate, multiplied by 8.
  1439. * */
  1440. linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
  1441. mode->crtc_clock);
  1442. ips_linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
  1443. intel_ddi_get_cdclk_freq(dev_priv));
  1444. return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
  1445. PIPE_WM_LINETIME_TIME(linetime);
  1446. }
  1447. static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[8])
  1448. {
  1449. struct drm_i915_private *dev_priv = dev->dev_private;
  1450. if (IS_GEN9(dev)) {
  1451. uint32_t val;
  1452. int ret, i;
  1453. int level, max_level = ilk_wm_max_level(dev);
  1454. /* read the first set of memory latencies[0:3] */
  1455. val = 0; /* data0 to be programmed to 0 for first set */
  1456. mutex_lock(&dev_priv->rps.hw_lock);
  1457. ret = sandybridge_pcode_read(dev_priv,
  1458. GEN9_PCODE_READ_MEM_LATENCY,
  1459. &val);
  1460. mutex_unlock(&dev_priv->rps.hw_lock);
  1461. if (ret) {
  1462. DRM_ERROR("SKL Mailbox read error = %d\n", ret);
  1463. return;
  1464. }
  1465. wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
  1466. wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
  1467. GEN9_MEM_LATENCY_LEVEL_MASK;
  1468. wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
  1469. GEN9_MEM_LATENCY_LEVEL_MASK;
  1470. wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
  1471. GEN9_MEM_LATENCY_LEVEL_MASK;
  1472. /* read the second set of memory latencies[4:7] */
  1473. val = 1; /* data0 to be programmed to 1 for second set */
  1474. mutex_lock(&dev_priv->rps.hw_lock);
  1475. ret = sandybridge_pcode_read(dev_priv,
  1476. GEN9_PCODE_READ_MEM_LATENCY,
  1477. &val);
  1478. mutex_unlock(&dev_priv->rps.hw_lock);
  1479. if (ret) {
  1480. DRM_ERROR("SKL Mailbox read error = %d\n", ret);
  1481. return;
  1482. }
  1483. wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
  1484. wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
  1485. GEN9_MEM_LATENCY_LEVEL_MASK;
  1486. wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
  1487. GEN9_MEM_LATENCY_LEVEL_MASK;
  1488. wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
  1489. GEN9_MEM_LATENCY_LEVEL_MASK;
  1490. /*
  1491. * WaWmMemoryReadLatency:skl
  1492. *
  1493. * punit doesn't take into account the read latency so we need
  1494. * to add 2us to the various latency levels we retrieve from
  1495. * the punit.
  1496. * - W0 is a bit special in that it's the only level that
  1497. * can't be disabled if we want to have display working, so
  1498. * we always add 2us there.
  1499. * - For levels >=1, punit returns 0us latency when they are
  1500. * disabled, so we respect that and don't add 2us then
  1501. *
  1502. * Additionally, if a level n (n > 1) has a 0us latency, all
  1503. * levels m (m >= n) need to be disabled. We make sure to
  1504. * sanitize the values out of the punit to satisfy this
  1505. * requirement.
  1506. */
  1507. wm[0] += 2;
  1508. for (level = 1; level <= max_level; level++)
  1509. if (wm[level] != 0)
  1510. wm[level] += 2;
  1511. else {
  1512. for (i = level + 1; i <= max_level; i++)
  1513. wm[i] = 0;
  1514. break;
  1515. }
  1516. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  1517. uint64_t sskpd = I915_READ64(MCH_SSKPD);
  1518. wm[0] = (sskpd >> 56) & 0xFF;
  1519. if (wm[0] == 0)
  1520. wm[0] = sskpd & 0xF;
  1521. wm[1] = (sskpd >> 4) & 0xFF;
  1522. wm[2] = (sskpd >> 12) & 0xFF;
  1523. wm[3] = (sskpd >> 20) & 0x1FF;
  1524. wm[4] = (sskpd >> 32) & 0x1FF;
  1525. } else if (INTEL_INFO(dev)->gen >= 6) {
  1526. uint32_t sskpd = I915_READ(MCH_SSKPD);
  1527. wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
  1528. wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
  1529. wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
  1530. wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
  1531. } else if (INTEL_INFO(dev)->gen >= 5) {
  1532. uint32_t mltr = I915_READ(MLTR_ILK);
  1533. /* ILK primary LP0 latency is 700 ns */
  1534. wm[0] = 7;
  1535. wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
  1536. wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
  1537. }
  1538. }
  1539. static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
  1540. {
  1541. /* ILK sprite LP0 latency is 1300 ns */
  1542. if (INTEL_INFO(dev)->gen == 5)
  1543. wm[0] = 13;
  1544. }
  1545. static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
  1546. {
  1547. /* ILK cursor LP0 latency is 1300 ns */
  1548. if (INTEL_INFO(dev)->gen == 5)
  1549. wm[0] = 13;
  1550. /* WaDoubleCursorLP3Latency:ivb */
  1551. if (IS_IVYBRIDGE(dev))
  1552. wm[3] *= 2;
  1553. }
  1554. int ilk_wm_max_level(const struct drm_device *dev)
  1555. {
  1556. /* how many WM levels are we expecting */
  1557. if (IS_GEN9(dev))
  1558. return 7;
  1559. else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  1560. return 4;
  1561. else if (INTEL_INFO(dev)->gen >= 6)
  1562. return 3;
  1563. else
  1564. return 2;
  1565. }
  1566. static void intel_print_wm_latency(struct drm_device *dev,
  1567. const char *name,
  1568. const uint16_t wm[8])
  1569. {
  1570. int level, max_level = ilk_wm_max_level(dev);
  1571. for (level = 0; level <= max_level; level++) {
  1572. unsigned int latency = wm[level];
  1573. if (latency == 0) {
  1574. DRM_ERROR("%s WM%d latency not provided\n",
  1575. name, level);
  1576. continue;
  1577. }
  1578. /*
  1579. * - latencies are in us on gen9.
  1580. * - before then, WM1+ latency values are in 0.5us units
  1581. */
  1582. if (IS_GEN9(dev))
  1583. latency *= 10;
  1584. else if (level > 0)
  1585. latency *= 5;
  1586. DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
  1587. name, level, wm[level],
  1588. latency / 10, latency % 10);
  1589. }
  1590. }
  1591. static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
  1592. uint16_t wm[5], uint16_t min)
  1593. {
  1594. int level, max_level = ilk_wm_max_level(dev_priv->dev);
  1595. if (wm[0] >= min)
  1596. return false;
  1597. wm[0] = max(wm[0], min);
  1598. for (level = 1; level <= max_level; level++)
  1599. wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
  1600. return true;
  1601. }
  1602. static void snb_wm_latency_quirk(struct drm_device *dev)
  1603. {
  1604. struct drm_i915_private *dev_priv = dev->dev_private;
  1605. bool changed;
  1606. /*
  1607. * The BIOS provided WM memory latency values are often
  1608. * inadequate for high resolution displays. Adjust them.
  1609. */
  1610. changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
  1611. ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
  1612. ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
  1613. if (!changed)
  1614. return;
  1615. DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
  1616. intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
  1617. intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
  1618. intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
  1619. }
  1620. static void ilk_setup_wm_latency(struct drm_device *dev)
  1621. {
  1622. struct drm_i915_private *dev_priv = dev->dev_private;
  1623. intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
  1624. memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
  1625. sizeof(dev_priv->wm.pri_latency));
  1626. memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
  1627. sizeof(dev_priv->wm.pri_latency));
  1628. intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
  1629. intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
  1630. intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
  1631. intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
  1632. intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
  1633. if (IS_GEN6(dev))
  1634. snb_wm_latency_quirk(dev);
  1635. }
  1636. static void skl_setup_wm_latency(struct drm_device *dev)
  1637. {
  1638. struct drm_i915_private *dev_priv = dev->dev_private;
  1639. intel_read_wm_latency(dev, dev_priv->wm.skl_latency);
  1640. intel_print_wm_latency(dev, "Gen9 Plane", dev_priv->wm.skl_latency);
  1641. }
  1642. static void ilk_compute_wm_parameters(struct drm_crtc *crtc,
  1643. struct ilk_pipe_wm_parameters *p)
  1644. {
  1645. struct drm_device *dev = crtc->dev;
  1646. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1647. enum pipe pipe = intel_crtc->pipe;
  1648. struct drm_plane *plane;
  1649. if (!intel_crtc_active(crtc))
  1650. return;
  1651. p->active = true;
  1652. p->pipe_htotal = intel_crtc->config->base.adjusted_mode.crtc_htotal;
  1653. p->pixel_rate = ilk_pipe_pixel_rate(dev, crtc);
  1654. p->pri.bytes_per_pixel = crtc->primary->state->fb->bits_per_pixel / 8;
  1655. p->cur.bytes_per_pixel = 4;
  1656. p->pri.horiz_pixels = intel_crtc->config->pipe_src_w;
  1657. p->cur.horiz_pixels = intel_crtc->base.cursor->state->crtc_w;
  1658. /* TODO: for now, assume primary and cursor planes are always enabled. */
  1659. p->pri.enabled = true;
  1660. p->cur.enabled = true;
  1661. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  1662. struct intel_plane *intel_plane = to_intel_plane(plane);
  1663. if (intel_plane->pipe == pipe) {
  1664. p->spr = intel_plane->wm;
  1665. break;
  1666. }
  1667. }
  1668. }
  1669. static void ilk_compute_wm_config(struct drm_device *dev,
  1670. struct intel_wm_config *config)
  1671. {
  1672. struct intel_crtc *intel_crtc;
  1673. /* Compute the currently _active_ config */
  1674. for_each_intel_crtc(dev, intel_crtc) {
  1675. const struct intel_pipe_wm *wm = &intel_crtc->wm.active;
  1676. if (!wm->pipe_enabled)
  1677. continue;
  1678. config->sprites_enabled |= wm->sprites_enabled;
  1679. config->sprites_scaled |= wm->sprites_scaled;
  1680. config->num_pipes_active++;
  1681. }
  1682. }
  1683. /* Compute new watermarks for the pipe */
  1684. static bool intel_compute_pipe_wm(struct drm_crtc *crtc,
  1685. const struct ilk_pipe_wm_parameters *params,
  1686. struct intel_pipe_wm *pipe_wm)
  1687. {
  1688. struct drm_device *dev = crtc->dev;
  1689. const struct drm_i915_private *dev_priv = dev->dev_private;
  1690. int level, max_level = ilk_wm_max_level(dev);
  1691. /* LP0 watermark maximums depend on this pipe alone */
  1692. struct intel_wm_config config = {
  1693. .num_pipes_active = 1,
  1694. .sprites_enabled = params->spr.enabled,
  1695. .sprites_scaled = params->spr.scaled,
  1696. };
  1697. struct ilk_wm_maximums max;
  1698. pipe_wm->pipe_enabled = params->active;
  1699. pipe_wm->sprites_enabled = params->spr.enabled;
  1700. pipe_wm->sprites_scaled = params->spr.scaled;
  1701. /* ILK/SNB: LP2+ watermarks only w/o sprites */
  1702. if (INTEL_INFO(dev)->gen <= 6 && params->spr.enabled)
  1703. max_level = 1;
  1704. /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
  1705. if (params->spr.scaled)
  1706. max_level = 0;
  1707. ilk_compute_wm_level(dev_priv, 0, params, &pipe_wm->wm[0]);
  1708. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  1709. pipe_wm->linetime = hsw_compute_linetime_wm(dev, crtc);
  1710. /* LP0 watermarks always use 1/2 DDB partitioning */
  1711. ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
  1712. /* At least LP0 must be valid */
  1713. if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]))
  1714. return false;
  1715. ilk_compute_wm_reg_maximums(dev, 1, &max);
  1716. for (level = 1; level <= max_level; level++) {
  1717. struct intel_wm_level wm = {};
  1718. ilk_compute_wm_level(dev_priv, level, params, &wm);
  1719. /*
  1720. * Disable any watermark level that exceeds the
  1721. * register maximums since such watermarks are
  1722. * always invalid.
  1723. */
  1724. if (!ilk_validate_wm_level(level, &max, &wm))
  1725. break;
  1726. pipe_wm->wm[level] = wm;
  1727. }
  1728. return true;
  1729. }
  1730. /*
  1731. * Merge the watermarks from all active pipes for a specific level.
  1732. */
  1733. static void ilk_merge_wm_level(struct drm_device *dev,
  1734. int level,
  1735. struct intel_wm_level *ret_wm)
  1736. {
  1737. const struct intel_crtc *intel_crtc;
  1738. ret_wm->enable = true;
  1739. for_each_intel_crtc(dev, intel_crtc) {
  1740. const struct intel_pipe_wm *active = &intel_crtc->wm.active;
  1741. const struct intel_wm_level *wm = &active->wm[level];
  1742. if (!active->pipe_enabled)
  1743. continue;
  1744. /*
  1745. * The watermark values may have been used in the past,
  1746. * so we must maintain them in the registers for some
  1747. * time even if the level is now disabled.
  1748. */
  1749. if (!wm->enable)
  1750. ret_wm->enable = false;
  1751. ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
  1752. ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
  1753. ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
  1754. ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
  1755. }
  1756. }
  1757. /*
  1758. * Merge all low power watermarks for all active pipes.
  1759. */
  1760. static void ilk_wm_merge(struct drm_device *dev,
  1761. const struct intel_wm_config *config,
  1762. const struct ilk_wm_maximums *max,
  1763. struct intel_pipe_wm *merged)
  1764. {
  1765. int level, max_level = ilk_wm_max_level(dev);
  1766. int last_enabled_level = max_level;
  1767. /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
  1768. if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
  1769. config->num_pipes_active > 1)
  1770. return;
  1771. /* ILK: FBC WM must be disabled always */
  1772. merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
  1773. /* merge each WM1+ level */
  1774. for (level = 1; level <= max_level; level++) {
  1775. struct intel_wm_level *wm = &merged->wm[level];
  1776. ilk_merge_wm_level(dev, level, wm);
  1777. if (level > last_enabled_level)
  1778. wm->enable = false;
  1779. else if (!ilk_validate_wm_level(level, max, wm))
  1780. /* make sure all following levels get disabled */
  1781. last_enabled_level = level - 1;
  1782. /*
  1783. * The spec says it is preferred to disable
  1784. * FBC WMs instead of disabling a WM level.
  1785. */
  1786. if (wm->fbc_val > max->fbc) {
  1787. if (wm->enable)
  1788. merged->fbc_wm_enabled = false;
  1789. wm->fbc_val = 0;
  1790. }
  1791. }
  1792. /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
  1793. /*
  1794. * FIXME this is racy. FBC might get enabled later.
  1795. * What we should check here is whether FBC can be
  1796. * enabled sometime later.
  1797. */
  1798. if (IS_GEN5(dev) && !merged->fbc_wm_enabled && intel_fbc_enabled(dev)) {
  1799. for (level = 2; level <= max_level; level++) {
  1800. struct intel_wm_level *wm = &merged->wm[level];
  1801. wm->enable = false;
  1802. }
  1803. }
  1804. }
  1805. static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
  1806. {
  1807. /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
  1808. return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
  1809. }
  1810. /* The value we need to program into the WM_LPx latency field */
  1811. static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
  1812. {
  1813. struct drm_i915_private *dev_priv = dev->dev_private;
  1814. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  1815. return 2 * level;
  1816. else
  1817. return dev_priv->wm.pri_latency[level];
  1818. }
  1819. static void ilk_compute_wm_results(struct drm_device *dev,
  1820. const struct intel_pipe_wm *merged,
  1821. enum intel_ddb_partitioning partitioning,
  1822. struct ilk_wm_values *results)
  1823. {
  1824. struct intel_crtc *intel_crtc;
  1825. int level, wm_lp;
  1826. results->enable_fbc_wm = merged->fbc_wm_enabled;
  1827. results->partitioning = partitioning;
  1828. /* LP1+ register values */
  1829. for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
  1830. const struct intel_wm_level *r;
  1831. level = ilk_wm_lp_to_level(wm_lp, merged);
  1832. r = &merged->wm[level];
  1833. /*
  1834. * Maintain the watermark values even if the level is
  1835. * disabled. Doing otherwise could cause underruns.
  1836. */
  1837. results->wm_lp[wm_lp - 1] =
  1838. (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
  1839. (r->pri_val << WM1_LP_SR_SHIFT) |
  1840. r->cur_val;
  1841. if (r->enable)
  1842. results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
  1843. if (INTEL_INFO(dev)->gen >= 8)
  1844. results->wm_lp[wm_lp - 1] |=
  1845. r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
  1846. else
  1847. results->wm_lp[wm_lp - 1] |=
  1848. r->fbc_val << WM1_LP_FBC_SHIFT;
  1849. /*
  1850. * Always set WM1S_LP_EN when spr_val != 0, even if the
  1851. * level is disabled. Doing otherwise could cause underruns.
  1852. */
  1853. if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
  1854. WARN_ON(wm_lp != 1);
  1855. results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
  1856. } else
  1857. results->wm_lp_spr[wm_lp - 1] = r->spr_val;
  1858. }
  1859. /* LP0 register values */
  1860. for_each_intel_crtc(dev, intel_crtc) {
  1861. enum pipe pipe = intel_crtc->pipe;
  1862. const struct intel_wm_level *r =
  1863. &intel_crtc->wm.active.wm[0];
  1864. if (WARN_ON(!r->enable))
  1865. continue;
  1866. results->wm_linetime[pipe] = intel_crtc->wm.active.linetime;
  1867. results->wm_pipe[pipe] =
  1868. (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
  1869. (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
  1870. r->cur_val;
  1871. }
  1872. }
  1873. /* Find the result with the highest level enabled. Check for enable_fbc_wm in
  1874. * case both are at the same level. Prefer r1 in case they're the same. */
  1875. static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
  1876. struct intel_pipe_wm *r1,
  1877. struct intel_pipe_wm *r2)
  1878. {
  1879. int level, max_level = ilk_wm_max_level(dev);
  1880. int level1 = 0, level2 = 0;
  1881. for (level = 1; level <= max_level; level++) {
  1882. if (r1->wm[level].enable)
  1883. level1 = level;
  1884. if (r2->wm[level].enable)
  1885. level2 = level;
  1886. }
  1887. if (level1 == level2) {
  1888. if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
  1889. return r2;
  1890. else
  1891. return r1;
  1892. } else if (level1 > level2) {
  1893. return r1;
  1894. } else {
  1895. return r2;
  1896. }
  1897. }
  1898. /* dirty bits used to track which watermarks need changes */
  1899. #define WM_DIRTY_PIPE(pipe) (1 << (pipe))
  1900. #define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
  1901. #define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
  1902. #define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
  1903. #define WM_DIRTY_FBC (1 << 24)
  1904. #define WM_DIRTY_DDB (1 << 25)
  1905. static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
  1906. const struct ilk_wm_values *old,
  1907. const struct ilk_wm_values *new)
  1908. {
  1909. unsigned int dirty = 0;
  1910. enum pipe pipe;
  1911. int wm_lp;
  1912. for_each_pipe(dev_priv, pipe) {
  1913. if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
  1914. dirty |= WM_DIRTY_LINETIME(pipe);
  1915. /* Must disable LP1+ watermarks too */
  1916. dirty |= WM_DIRTY_LP_ALL;
  1917. }
  1918. if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
  1919. dirty |= WM_DIRTY_PIPE(pipe);
  1920. /* Must disable LP1+ watermarks too */
  1921. dirty |= WM_DIRTY_LP_ALL;
  1922. }
  1923. }
  1924. if (old->enable_fbc_wm != new->enable_fbc_wm) {
  1925. dirty |= WM_DIRTY_FBC;
  1926. /* Must disable LP1+ watermarks too */
  1927. dirty |= WM_DIRTY_LP_ALL;
  1928. }
  1929. if (old->partitioning != new->partitioning) {
  1930. dirty |= WM_DIRTY_DDB;
  1931. /* Must disable LP1+ watermarks too */
  1932. dirty |= WM_DIRTY_LP_ALL;
  1933. }
  1934. /* LP1+ watermarks already deemed dirty, no need to continue */
  1935. if (dirty & WM_DIRTY_LP_ALL)
  1936. return dirty;
  1937. /* Find the lowest numbered LP1+ watermark in need of an update... */
  1938. for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
  1939. if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
  1940. old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
  1941. break;
  1942. }
  1943. /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
  1944. for (; wm_lp <= 3; wm_lp++)
  1945. dirty |= WM_DIRTY_LP(wm_lp);
  1946. return dirty;
  1947. }
  1948. static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
  1949. unsigned int dirty)
  1950. {
  1951. struct ilk_wm_values *previous = &dev_priv->wm.hw;
  1952. bool changed = false;
  1953. if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
  1954. previous->wm_lp[2] &= ~WM1_LP_SR_EN;
  1955. I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
  1956. changed = true;
  1957. }
  1958. if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
  1959. previous->wm_lp[1] &= ~WM1_LP_SR_EN;
  1960. I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
  1961. changed = true;
  1962. }
  1963. if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
  1964. previous->wm_lp[0] &= ~WM1_LP_SR_EN;
  1965. I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
  1966. changed = true;
  1967. }
  1968. /*
  1969. * Don't touch WM1S_LP_EN here.
  1970. * Doing so could cause underruns.
  1971. */
  1972. return changed;
  1973. }
  1974. /*
  1975. * The spec says we shouldn't write when we don't need, because every write
  1976. * causes WMs to be re-evaluated, expending some power.
  1977. */
  1978. static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
  1979. struct ilk_wm_values *results)
  1980. {
  1981. struct drm_device *dev = dev_priv->dev;
  1982. struct ilk_wm_values *previous = &dev_priv->wm.hw;
  1983. unsigned int dirty;
  1984. uint32_t val;
  1985. dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
  1986. if (!dirty)
  1987. return;
  1988. _ilk_disable_lp_wm(dev_priv, dirty);
  1989. if (dirty & WM_DIRTY_PIPE(PIPE_A))
  1990. I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
  1991. if (dirty & WM_DIRTY_PIPE(PIPE_B))
  1992. I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
  1993. if (dirty & WM_DIRTY_PIPE(PIPE_C))
  1994. I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
  1995. if (dirty & WM_DIRTY_LINETIME(PIPE_A))
  1996. I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
  1997. if (dirty & WM_DIRTY_LINETIME(PIPE_B))
  1998. I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
  1999. if (dirty & WM_DIRTY_LINETIME(PIPE_C))
  2000. I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
  2001. if (dirty & WM_DIRTY_DDB) {
  2002. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2003. val = I915_READ(WM_MISC);
  2004. if (results->partitioning == INTEL_DDB_PART_1_2)
  2005. val &= ~WM_MISC_DATA_PARTITION_5_6;
  2006. else
  2007. val |= WM_MISC_DATA_PARTITION_5_6;
  2008. I915_WRITE(WM_MISC, val);
  2009. } else {
  2010. val = I915_READ(DISP_ARB_CTL2);
  2011. if (results->partitioning == INTEL_DDB_PART_1_2)
  2012. val &= ~DISP_DATA_PARTITION_5_6;
  2013. else
  2014. val |= DISP_DATA_PARTITION_5_6;
  2015. I915_WRITE(DISP_ARB_CTL2, val);
  2016. }
  2017. }
  2018. if (dirty & WM_DIRTY_FBC) {
  2019. val = I915_READ(DISP_ARB_CTL);
  2020. if (results->enable_fbc_wm)
  2021. val &= ~DISP_FBC_WM_DIS;
  2022. else
  2023. val |= DISP_FBC_WM_DIS;
  2024. I915_WRITE(DISP_ARB_CTL, val);
  2025. }
  2026. if (dirty & WM_DIRTY_LP(1) &&
  2027. previous->wm_lp_spr[0] != results->wm_lp_spr[0])
  2028. I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
  2029. if (INTEL_INFO(dev)->gen >= 7) {
  2030. if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
  2031. I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
  2032. if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
  2033. I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
  2034. }
  2035. if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
  2036. I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
  2037. if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
  2038. I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
  2039. if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
  2040. I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
  2041. dev_priv->wm.hw = *results;
  2042. }
  2043. static bool ilk_disable_lp_wm(struct drm_device *dev)
  2044. {
  2045. struct drm_i915_private *dev_priv = dev->dev_private;
  2046. return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
  2047. }
  2048. /*
  2049. * On gen9, we need to allocate Display Data Buffer (DDB) portions to the
  2050. * different active planes.
  2051. */
  2052. #define SKL_DDB_SIZE 896 /* in blocks */
  2053. static void
  2054. skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
  2055. struct drm_crtc *for_crtc,
  2056. const struct intel_wm_config *config,
  2057. const struct skl_pipe_wm_parameters *params,
  2058. struct skl_ddb_entry *alloc /* out */)
  2059. {
  2060. struct drm_crtc *crtc;
  2061. unsigned int pipe_size, ddb_size;
  2062. int nth_active_pipe;
  2063. if (!params->active) {
  2064. alloc->start = 0;
  2065. alloc->end = 0;
  2066. return;
  2067. }
  2068. ddb_size = SKL_DDB_SIZE;
  2069. ddb_size -= 4; /* 4 blocks for bypass path allocation */
  2070. nth_active_pipe = 0;
  2071. for_each_crtc(dev, crtc) {
  2072. if (!intel_crtc_active(crtc))
  2073. continue;
  2074. if (crtc == for_crtc)
  2075. break;
  2076. nth_active_pipe++;
  2077. }
  2078. pipe_size = ddb_size / config->num_pipes_active;
  2079. alloc->start = nth_active_pipe * ddb_size / config->num_pipes_active;
  2080. alloc->end = alloc->start + pipe_size;
  2081. }
  2082. static unsigned int skl_cursor_allocation(const struct intel_wm_config *config)
  2083. {
  2084. if (config->num_pipes_active == 1)
  2085. return 32;
  2086. return 8;
  2087. }
  2088. static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
  2089. {
  2090. entry->start = reg & 0x3ff;
  2091. entry->end = (reg >> 16) & 0x3ff;
  2092. if (entry->end)
  2093. entry->end += 1;
  2094. }
  2095. void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
  2096. struct skl_ddb_allocation *ddb /* out */)
  2097. {
  2098. enum pipe pipe;
  2099. int plane;
  2100. u32 val;
  2101. for_each_pipe(dev_priv, pipe) {
  2102. for_each_plane(dev_priv, pipe, plane) {
  2103. val = I915_READ(PLANE_BUF_CFG(pipe, plane));
  2104. skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane],
  2105. val);
  2106. }
  2107. val = I915_READ(CUR_BUF_CFG(pipe));
  2108. skl_ddb_entry_init_from_hw(&ddb->cursor[pipe], val);
  2109. }
  2110. }
  2111. static unsigned int
  2112. skl_plane_relative_data_rate(const struct intel_plane_wm_parameters *p)
  2113. {
  2114. return p->horiz_pixels * p->vert_pixels * p->bytes_per_pixel;
  2115. }
  2116. /*
  2117. * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
  2118. * a 8192x4096@32bpp framebuffer:
  2119. * 3 * 4096 * 8192 * 4 < 2^32
  2120. */
  2121. static unsigned int
  2122. skl_get_total_relative_data_rate(struct intel_crtc *intel_crtc,
  2123. const struct skl_pipe_wm_parameters *params)
  2124. {
  2125. unsigned int total_data_rate = 0;
  2126. int plane;
  2127. for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
  2128. const struct intel_plane_wm_parameters *p;
  2129. p = &params->plane[plane];
  2130. if (!p->enabled)
  2131. continue;
  2132. total_data_rate += skl_plane_relative_data_rate(p);
  2133. }
  2134. return total_data_rate;
  2135. }
  2136. static void
  2137. skl_allocate_pipe_ddb(struct drm_crtc *crtc,
  2138. const struct intel_wm_config *config,
  2139. const struct skl_pipe_wm_parameters *params,
  2140. struct skl_ddb_allocation *ddb /* out */)
  2141. {
  2142. struct drm_device *dev = crtc->dev;
  2143. struct drm_i915_private *dev_priv = dev->dev_private;
  2144. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2145. enum pipe pipe = intel_crtc->pipe;
  2146. struct skl_ddb_entry *alloc = &ddb->pipe[pipe];
  2147. uint16_t alloc_size, start, cursor_blocks;
  2148. uint16_t minimum[I915_MAX_PLANES];
  2149. unsigned int total_data_rate;
  2150. int plane;
  2151. skl_ddb_get_pipe_allocation_limits(dev, crtc, config, params, alloc);
  2152. alloc_size = skl_ddb_entry_size(alloc);
  2153. if (alloc_size == 0) {
  2154. memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
  2155. memset(&ddb->cursor[pipe], 0, sizeof(ddb->cursor[pipe]));
  2156. return;
  2157. }
  2158. cursor_blocks = skl_cursor_allocation(config);
  2159. ddb->cursor[pipe].start = alloc->end - cursor_blocks;
  2160. ddb->cursor[pipe].end = alloc->end;
  2161. alloc_size -= cursor_blocks;
  2162. alloc->end -= cursor_blocks;
  2163. /* 1. Allocate the mininum required blocks for each active plane */
  2164. for_each_plane(dev_priv, pipe, plane) {
  2165. const struct intel_plane_wm_parameters *p;
  2166. p = &params->plane[plane];
  2167. if (!p->enabled)
  2168. continue;
  2169. minimum[plane] = 8;
  2170. alloc_size -= minimum[plane];
  2171. }
  2172. /*
  2173. * 2. Distribute the remaining space in proportion to the amount of
  2174. * data each plane needs to fetch from memory.
  2175. *
  2176. * FIXME: we may not allocate every single block here.
  2177. */
  2178. total_data_rate = skl_get_total_relative_data_rate(intel_crtc, params);
  2179. start = alloc->start;
  2180. for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
  2181. const struct intel_plane_wm_parameters *p;
  2182. unsigned int data_rate;
  2183. uint16_t plane_blocks;
  2184. p = &params->plane[plane];
  2185. if (!p->enabled)
  2186. continue;
  2187. data_rate = skl_plane_relative_data_rate(p);
  2188. /*
  2189. * promote the expression to 64 bits to avoid overflowing, the
  2190. * result is < available as data_rate / total_data_rate < 1
  2191. */
  2192. plane_blocks = minimum[plane];
  2193. plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
  2194. total_data_rate);
  2195. ddb->plane[pipe][plane].start = start;
  2196. ddb->plane[pipe][plane].end = start + plane_blocks;
  2197. start += plane_blocks;
  2198. }
  2199. }
  2200. static uint32_t skl_pipe_pixel_rate(const struct intel_crtc_state *config)
  2201. {
  2202. /* TODO: Take into account the scalers once we support them */
  2203. return config->base.adjusted_mode.crtc_clock;
  2204. }
  2205. /*
  2206. * The max latency should be 257 (max the punit can code is 255 and we add 2us
  2207. * for the read latency) and bytes_per_pixel should always be <= 8, so that
  2208. * should allow pixel_rate up to ~2 GHz which seems sufficient since max
  2209. * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
  2210. */
  2211. static uint32_t skl_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
  2212. uint32_t latency)
  2213. {
  2214. uint32_t wm_intermediate_val, ret;
  2215. if (latency == 0)
  2216. return UINT_MAX;
  2217. wm_intermediate_val = latency * pixel_rate * bytes_per_pixel / 512;
  2218. ret = DIV_ROUND_UP(wm_intermediate_val, 1000);
  2219. return ret;
  2220. }
  2221. static uint32_t skl_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
  2222. uint32_t horiz_pixels, uint8_t bytes_per_pixel,
  2223. uint64_t tiling, uint32_t latency)
  2224. {
  2225. uint32_t ret;
  2226. uint32_t plane_bytes_per_line, plane_blocks_per_line;
  2227. uint32_t wm_intermediate_val;
  2228. if (latency == 0)
  2229. return UINT_MAX;
  2230. plane_bytes_per_line = horiz_pixels * bytes_per_pixel;
  2231. if (tiling == I915_FORMAT_MOD_Y_TILED ||
  2232. tiling == I915_FORMAT_MOD_Yf_TILED) {
  2233. plane_bytes_per_line *= 4;
  2234. plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
  2235. plane_blocks_per_line /= 4;
  2236. } else {
  2237. plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
  2238. }
  2239. wm_intermediate_val = latency * pixel_rate;
  2240. ret = DIV_ROUND_UP(wm_intermediate_val, pipe_htotal * 1000) *
  2241. plane_blocks_per_line;
  2242. return ret;
  2243. }
  2244. static bool skl_ddb_allocation_changed(const struct skl_ddb_allocation *new_ddb,
  2245. const struct intel_crtc *intel_crtc)
  2246. {
  2247. struct drm_device *dev = intel_crtc->base.dev;
  2248. struct drm_i915_private *dev_priv = dev->dev_private;
  2249. const struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
  2250. enum pipe pipe = intel_crtc->pipe;
  2251. if (memcmp(new_ddb->plane[pipe], cur_ddb->plane[pipe],
  2252. sizeof(new_ddb->plane[pipe])))
  2253. return true;
  2254. if (memcmp(&new_ddb->cursor[pipe], &cur_ddb->cursor[pipe],
  2255. sizeof(new_ddb->cursor[pipe])))
  2256. return true;
  2257. return false;
  2258. }
  2259. static void skl_compute_wm_global_parameters(struct drm_device *dev,
  2260. struct intel_wm_config *config)
  2261. {
  2262. struct drm_crtc *crtc;
  2263. struct drm_plane *plane;
  2264. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  2265. config->num_pipes_active += intel_crtc_active(crtc);
  2266. /* FIXME: I don't think we need those two global parameters on SKL */
  2267. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  2268. struct intel_plane *intel_plane = to_intel_plane(plane);
  2269. config->sprites_enabled |= intel_plane->wm.enabled;
  2270. config->sprites_scaled |= intel_plane->wm.scaled;
  2271. }
  2272. }
  2273. static void skl_compute_wm_pipe_parameters(struct drm_crtc *crtc,
  2274. struct skl_pipe_wm_parameters *p)
  2275. {
  2276. struct drm_device *dev = crtc->dev;
  2277. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2278. enum pipe pipe = intel_crtc->pipe;
  2279. struct drm_plane *plane;
  2280. struct drm_framebuffer *fb;
  2281. int i = 1; /* Index for sprite planes start */
  2282. p->active = intel_crtc_active(crtc);
  2283. if (p->active) {
  2284. p->pipe_htotal = intel_crtc->config->base.adjusted_mode.crtc_htotal;
  2285. p->pixel_rate = skl_pipe_pixel_rate(intel_crtc->config);
  2286. /*
  2287. * For now, assume primary and cursor planes are always enabled.
  2288. */
  2289. p->plane[0].enabled = true;
  2290. p->plane[0].bytes_per_pixel =
  2291. crtc->primary->state->fb->bits_per_pixel / 8;
  2292. p->plane[0].horiz_pixels = intel_crtc->config->pipe_src_w;
  2293. p->plane[0].vert_pixels = intel_crtc->config->pipe_src_h;
  2294. p->plane[0].tiling = DRM_FORMAT_MOD_NONE;
  2295. fb = crtc->primary->state->fb;
  2296. /*
  2297. * Framebuffer can be NULL on plane disable, but it does not
  2298. * matter for watermarks if we assume no tiling in that case.
  2299. */
  2300. if (fb)
  2301. p->plane[0].tiling = fb->modifier[0];
  2302. p->cursor.enabled = true;
  2303. p->cursor.bytes_per_pixel = 4;
  2304. p->cursor.horiz_pixels = intel_crtc->base.cursor->state->crtc_w ?
  2305. intel_crtc->base.cursor->state->crtc_w : 64;
  2306. }
  2307. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  2308. struct intel_plane *intel_plane = to_intel_plane(plane);
  2309. if (intel_plane->pipe == pipe &&
  2310. plane->type == DRM_PLANE_TYPE_OVERLAY)
  2311. p->plane[i++] = intel_plane->wm;
  2312. }
  2313. }
  2314. static bool skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
  2315. struct skl_pipe_wm_parameters *p,
  2316. struct intel_plane_wm_parameters *p_params,
  2317. uint16_t ddb_allocation,
  2318. int level,
  2319. uint16_t *out_blocks, /* out */
  2320. uint8_t *out_lines /* out */)
  2321. {
  2322. uint32_t latency = dev_priv->wm.skl_latency[level];
  2323. uint32_t method1, method2;
  2324. uint32_t plane_bytes_per_line, plane_blocks_per_line;
  2325. uint32_t res_blocks, res_lines;
  2326. uint32_t selected_result;
  2327. if (latency == 0 || !p->active || !p_params->enabled)
  2328. return false;
  2329. method1 = skl_wm_method1(p->pixel_rate,
  2330. p_params->bytes_per_pixel,
  2331. latency);
  2332. method2 = skl_wm_method2(p->pixel_rate,
  2333. p->pipe_htotal,
  2334. p_params->horiz_pixels,
  2335. p_params->bytes_per_pixel,
  2336. p_params->tiling,
  2337. latency);
  2338. plane_bytes_per_line = p_params->horiz_pixels *
  2339. p_params->bytes_per_pixel;
  2340. plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
  2341. if (p_params->tiling == I915_FORMAT_MOD_Y_TILED ||
  2342. p_params->tiling == I915_FORMAT_MOD_Yf_TILED) {
  2343. uint32_t y_tile_minimum = plane_blocks_per_line * 4;
  2344. selected_result = max(method2, y_tile_minimum);
  2345. } else {
  2346. if ((ddb_allocation / plane_blocks_per_line) >= 1)
  2347. selected_result = min(method1, method2);
  2348. else
  2349. selected_result = method1;
  2350. }
  2351. res_blocks = selected_result + 1;
  2352. res_lines = DIV_ROUND_UP(selected_result, plane_blocks_per_line);
  2353. if (level >= 1 && level <= 7) {
  2354. if (p_params->tiling == I915_FORMAT_MOD_Y_TILED ||
  2355. p_params->tiling == I915_FORMAT_MOD_Yf_TILED)
  2356. res_lines += 4;
  2357. else
  2358. res_blocks++;
  2359. }
  2360. if (res_blocks >= ddb_allocation || res_lines > 31)
  2361. return false;
  2362. *out_blocks = res_blocks;
  2363. *out_lines = res_lines;
  2364. return true;
  2365. }
  2366. static void skl_compute_wm_level(const struct drm_i915_private *dev_priv,
  2367. struct skl_ddb_allocation *ddb,
  2368. struct skl_pipe_wm_parameters *p,
  2369. enum pipe pipe,
  2370. int level,
  2371. int num_planes,
  2372. struct skl_wm_level *result)
  2373. {
  2374. uint16_t ddb_blocks;
  2375. int i;
  2376. for (i = 0; i < num_planes; i++) {
  2377. ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][i]);
  2378. result->plane_en[i] = skl_compute_plane_wm(dev_priv,
  2379. p, &p->plane[i],
  2380. ddb_blocks,
  2381. level,
  2382. &result->plane_res_b[i],
  2383. &result->plane_res_l[i]);
  2384. }
  2385. ddb_blocks = skl_ddb_entry_size(&ddb->cursor[pipe]);
  2386. result->cursor_en = skl_compute_plane_wm(dev_priv, p, &p->cursor,
  2387. ddb_blocks, level,
  2388. &result->cursor_res_b,
  2389. &result->cursor_res_l);
  2390. }
  2391. static uint32_t
  2392. skl_compute_linetime_wm(struct drm_crtc *crtc, struct skl_pipe_wm_parameters *p)
  2393. {
  2394. if (!intel_crtc_active(crtc))
  2395. return 0;
  2396. return DIV_ROUND_UP(8 * p->pipe_htotal * 1000, p->pixel_rate);
  2397. }
  2398. static void skl_compute_transition_wm(struct drm_crtc *crtc,
  2399. struct skl_pipe_wm_parameters *params,
  2400. struct skl_wm_level *trans_wm /* out */)
  2401. {
  2402. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2403. int i;
  2404. if (!params->active)
  2405. return;
  2406. /* Until we know more, just disable transition WMs */
  2407. for (i = 0; i < intel_num_planes(intel_crtc); i++)
  2408. trans_wm->plane_en[i] = false;
  2409. trans_wm->cursor_en = false;
  2410. }
  2411. static void skl_compute_pipe_wm(struct drm_crtc *crtc,
  2412. struct skl_ddb_allocation *ddb,
  2413. struct skl_pipe_wm_parameters *params,
  2414. struct skl_pipe_wm *pipe_wm)
  2415. {
  2416. struct drm_device *dev = crtc->dev;
  2417. const struct drm_i915_private *dev_priv = dev->dev_private;
  2418. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2419. int level, max_level = ilk_wm_max_level(dev);
  2420. for (level = 0; level <= max_level; level++) {
  2421. skl_compute_wm_level(dev_priv, ddb, params, intel_crtc->pipe,
  2422. level, intel_num_planes(intel_crtc),
  2423. &pipe_wm->wm[level]);
  2424. }
  2425. pipe_wm->linetime = skl_compute_linetime_wm(crtc, params);
  2426. skl_compute_transition_wm(crtc, params, &pipe_wm->trans_wm);
  2427. }
  2428. static void skl_compute_wm_results(struct drm_device *dev,
  2429. struct skl_pipe_wm_parameters *p,
  2430. struct skl_pipe_wm *p_wm,
  2431. struct skl_wm_values *r,
  2432. struct intel_crtc *intel_crtc)
  2433. {
  2434. int level, max_level = ilk_wm_max_level(dev);
  2435. enum pipe pipe = intel_crtc->pipe;
  2436. uint32_t temp;
  2437. int i;
  2438. for (level = 0; level <= max_level; level++) {
  2439. for (i = 0; i < intel_num_planes(intel_crtc); i++) {
  2440. temp = 0;
  2441. temp |= p_wm->wm[level].plane_res_l[i] <<
  2442. PLANE_WM_LINES_SHIFT;
  2443. temp |= p_wm->wm[level].plane_res_b[i];
  2444. if (p_wm->wm[level].plane_en[i])
  2445. temp |= PLANE_WM_EN;
  2446. r->plane[pipe][i][level] = temp;
  2447. }
  2448. temp = 0;
  2449. temp |= p_wm->wm[level].cursor_res_l << PLANE_WM_LINES_SHIFT;
  2450. temp |= p_wm->wm[level].cursor_res_b;
  2451. if (p_wm->wm[level].cursor_en)
  2452. temp |= PLANE_WM_EN;
  2453. r->cursor[pipe][level] = temp;
  2454. }
  2455. /* transition WMs */
  2456. for (i = 0; i < intel_num_planes(intel_crtc); i++) {
  2457. temp = 0;
  2458. temp |= p_wm->trans_wm.plane_res_l[i] << PLANE_WM_LINES_SHIFT;
  2459. temp |= p_wm->trans_wm.plane_res_b[i];
  2460. if (p_wm->trans_wm.plane_en[i])
  2461. temp |= PLANE_WM_EN;
  2462. r->plane_trans[pipe][i] = temp;
  2463. }
  2464. temp = 0;
  2465. temp |= p_wm->trans_wm.cursor_res_l << PLANE_WM_LINES_SHIFT;
  2466. temp |= p_wm->trans_wm.cursor_res_b;
  2467. if (p_wm->trans_wm.cursor_en)
  2468. temp |= PLANE_WM_EN;
  2469. r->cursor_trans[pipe] = temp;
  2470. r->wm_linetime[pipe] = p_wm->linetime;
  2471. }
  2472. static void skl_ddb_entry_write(struct drm_i915_private *dev_priv, uint32_t reg,
  2473. const struct skl_ddb_entry *entry)
  2474. {
  2475. if (entry->end)
  2476. I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
  2477. else
  2478. I915_WRITE(reg, 0);
  2479. }
  2480. static void skl_write_wm_values(struct drm_i915_private *dev_priv,
  2481. const struct skl_wm_values *new)
  2482. {
  2483. struct drm_device *dev = dev_priv->dev;
  2484. struct intel_crtc *crtc;
  2485. list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
  2486. int i, level, max_level = ilk_wm_max_level(dev);
  2487. enum pipe pipe = crtc->pipe;
  2488. if (!new->dirty[pipe])
  2489. continue;
  2490. I915_WRITE(PIPE_WM_LINETIME(pipe), new->wm_linetime[pipe]);
  2491. for (level = 0; level <= max_level; level++) {
  2492. for (i = 0; i < intel_num_planes(crtc); i++)
  2493. I915_WRITE(PLANE_WM(pipe, i, level),
  2494. new->plane[pipe][i][level]);
  2495. I915_WRITE(CUR_WM(pipe, level),
  2496. new->cursor[pipe][level]);
  2497. }
  2498. for (i = 0; i < intel_num_planes(crtc); i++)
  2499. I915_WRITE(PLANE_WM_TRANS(pipe, i),
  2500. new->plane_trans[pipe][i]);
  2501. I915_WRITE(CUR_WM_TRANS(pipe), new->cursor_trans[pipe]);
  2502. for (i = 0; i < intel_num_planes(crtc); i++)
  2503. skl_ddb_entry_write(dev_priv,
  2504. PLANE_BUF_CFG(pipe, i),
  2505. &new->ddb.plane[pipe][i]);
  2506. skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
  2507. &new->ddb.cursor[pipe]);
  2508. }
  2509. }
  2510. /*
  2511. * When setting up a new DDB allocation arrangement, we need to correctly
  2512. * sequence the times at which the new allocations for the pipes are taken into
  2513. * account or we'll have pipes fetching from space previously allocated to
  2514. * another pipe.
  2515. *
  2516. * Roughly the sequence looks like:
  2517. * 1. re-allocate the pipe(s) with the allocation being reduced and not
  2518. * overlapping with a previous light-up pipe (another way to put it is:
  2519. * pipes with their new allocation strickly included into their old ones).
  2520. * 2. re-allocate the other pipes that get their allocation reduced
  2521. * 3. allocate the pipes having their allocation increased
  2522. *
  2523. * Steps 1. and 2. are here to take care of the following case:
  2524. * - Initially DDB looks like this:
  2525. * | B | C |
  2526. * - enable pipe A.
  2527. * - pipe B has a reduced DDB allocation that overlaps with the old pipe C
  2528. * allocation
  2529. * | A | B | C |
  2530. *
  2531. * We need to sequence the re-allocation: C, B, A (and not B, C, A).
  2532. */
  2533. static void
  2534. skl_wm_flush_pipe(struct drm_i915_private *dev_priv, enum pipe pipe, int pass)
  2535. {
  2536. int plane;
  2537. DRM_DEBUG_KMS("flush pipe %c (pass %d)\n", pipe_name(pipe), pass);
  2538. for_each_plane(dev_priv, pipe, plane) {
  2539. I915_WRITE(PLANE_SURF(pipe, plane),
  2540. I915_READ(PLANE_SURF(pipe, plane)));
  2541. }
  2542. I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
  2543. }
  2544. static bool
  2545. skl_ddb_allocation_included(const struct skl_ddb_allocation *old,
  2546. const struct skl_ddb_allocation *new,
  2547. enum pipe pipe)
  2548. {
  2549. uint16_t old_size, new_size;
  2550. old_size = skl_ddb_entry_size(&old->pipe[pipe]);
  2551. new_size = skl_ddb_entry_size(&new->pipe[pipe]);
  2552. return old_size != new_size &&
  2553. new->pipe[pipe].start >= old->pipe[pipe].start &&
  2554. new->pipe[pipe].end <= old->pipe[pipe].end;
  2555. }
  2556. static void skl_flush_wm_values(struct drm_i915_private *dev_priv,
  2557. struct skl_wm_values *new_values)
  2558. {
  2559. struct drm_device *dev = dev_priv->dev;
  2560. struct skl_ddb_allocation *cur_ddb, *new_ddb;
  2561. bool reallocated[I915_MAX_PIPES] = {false, false, false};
  2562. struct intel_crtc *crtc;
  2563. enum pipe pipe;
  2564. new_ddb = &new_values->ddb;
  2565. cur_ddb = &dev_priv->wm.skl_hw.ddb;
  2566. /*
  2567. * First pass: flush the pipes with the new allocation contained into
  2568. * the old space.
  2569. *
  2570. * We'll wait for the vblank on those pipes to ensure we can safely
  2571. * re-allocate the freed space without this pipe fetching from it.
  2572. */
  2573. for_each_intel_crtc(dev, crtc) {
  2574. if (!crtc->active)
  2575. continue;
  2576. pipe = crtc->pipe;
  2577. if (!skl_ddb_allocation_included(cur_ddb, new_ddb, pipe))
  2578. continue;
  2579. skl_wm_flush_pipe(dev_priv, pipe, 1);
  2580. intel_wait_for_vblank(dev, pipe);
  2581. reallocated[pipe] = true;
  2582. }
  2583. /*
  2584. * Second pass: flush the pipes that are having their allocation
  2585. * reduced, but overlapping with a previous allocation.
  2586. *
  2587. * Here as well we need to wait for the vblank to make sure the freed
  2588. * space is not used anymore.
  2589. */
  2590. for_each_intel_crtc(dev, crtc) {
  2591. if (!crtc->active)
  2592. continue;
  2593. pipe = crtc->pipe;
  2594. if (reallocated[pipe])
  2595. continue;
  2596. if (skl_ddb_entry_size(&new_ddb->pipe[pipe]) <
  2597. skl_ddb_entry_size(&cur_ddb->pipe[pipe])) {
  2598. skl_wm_flush_pipe(dev_priv, pipe, 2);
  2599. intel_wait_for_vblank(dev, pipe);
  2600. reallocated[pipe] = true;
  2601. }
  2602. }
  2603. /*
  2604. * Third pass: flush the pipes that got more space allocated.
  2605. *
  2606. * We don't need to actively wait for the update here, next vblank
  2607. * will just get more DDB space with the correct WM values.
  2608. */
  2609. for_each_intel_crtc(dev, crtc) {
  2610. if (!crtc->active)
  2611. continue;
  2612. pipe = crtc->pipe;
  2613. /*
  2614. * At this point, only the pipes more space than before are
  2615. * left to re-allocate.
  2616. */
  2617. if (reallocated[pipe])
  2618. continue;
  2619. skl_wm_flush_pipe(dev_priv, pipe, 3);
  2620. }
  2621. }
  2622. static bool skl_update_pipe_wm(struct drm_crtc *crtc,
  2623. struct skl_pipe_wm_parameters *params,
  2624. struct intel_wm_config *config,
  2625. struct skl_ddb_allocation *ddb, /* out */
  2626. struct skl_pipe_wm *pipe_wm /* out */)
  2627. {
  2628. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2629. skl_compute_wm_pipe_parameters(crtc, params);
  2630. skl_allocate_pipe_ddb(crtc, config, params, ddb);
  2631. skl_compute_pipe_wm(crtc, ddb, params, pipe_wm);
  2632. if (!memcmp(&intel_crtc->wm.skl_active, pipe_wm, sizeof(*pipe_wm)))
  2633. return false;
  2634. intel_crtc->wm.skl_active = *pipe_wm;
  2635. return true;
  2636. }
  2637. static void skl_update_other_pipe_wm(struct drm_device *dev,
  2638. struct drm_crtc *crtc,
  2639. struct intel_wm_config *config,
  2640. struct skl_wm_values *r)
  2641. {
  2642. struct intel_crtc *intel_crtc;
  2643. struct intel_crtc *this_crtc = to_intel_crtc(crtc);
  2644. /*
  2645. * If the WM update hasn't changed the allocation for this_crtc (the
  2646. * crtc we are currently computing the new WM values for), other
  2647. * enabled crtcs will keep the same allocation and we don't need to
  2648. * recompute anything for them.
  2649. */
  2650. if (!skl_ddb_allocation_changed(&r->ddb, this_crtc))
  2651. return;
  2652. /*
  2653. * Otherwise, because of this_crtc being freshly enabled/disabled, the
  2654. * other active pipes need new DDB allocation and WM values.
  2655. */
  2656. list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
  2657. base.head) {
  2658. struct skl_pipe_wm_parameters params = {};
  2659. struct skl_pipe_wm pipe_wm = {};
  2660. bool wm_changed;
  2661. if (this_crtc->pipe == intel_crtc->pipe)
  2662. continue;
  2663. if (!intel_crtc->active)
  2664. continue;
  2665. wm_changed = skl_update_pipe_wm(&intel_crtc->base,
  2666. &params, config,
  2667. &r->ddb, &pipe_wm);
  2668. /*
  2669. * If we end up re-computing the other pipe WM values, it's
  2670. * because it was really needed, so we expect the WM values to
  2671. * be different.
  2672. */
  2673. WARN_ON(!wm_changed);
  2674. skl_compute_wm_results(dev, &params, &pipe_wm, r, intel_crtc);
  2675. r->dirty[intel_crtc->pipe] = true;
  2676. }
  2677. }
  2678. static void skl_update_wm(struct drm_crtc *crtc)
  2679. {
  2680. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2681. struct drm_device *dev = crtc->dev;
  2682. struct drm_i915_private *dev_priv = dev->dev_private;
  2683. struct skl_pipe_wm_parameters params = {};
  2684. struct skl_wm_values *results = &dev_priv->wm.skl_results;
  2685. struct skl_pipe_wm pipe_wm = {};
  2686. struct intel_wm_config config = {};
  2687. memset(results, 0, sizeof(*results));
  2688. skl_compute_wm_global_parameters(dev, &config);
  2689. if (!skl_update_pipe_wm(crtc, &params, &config,
  2690. &results->ddb, &pipe_wm))
  2691. return;
  2692. skl_compute_wm_results(dev, &params, &pipe_wm, results, intel_crtc);
  2693. results->dirty[intel_crtc->pipe] = true;
  2694. skl_update_other_pipe_wm(dev, crtc, &config, results);
  2695. skl_write_wm_values(dev_priv, results);
  2696. skl_flush_wm_values(dev_priv, results);
  2697. /* store the new configuration */
  2698. dev_priv->wm.skl_hw = *results;
  2699. }
  2700. static void
  2701. skl_update_sprite_wm(struct drm_plane *plane, struct drm_crtc *crtc,
  2702. uint32_t sprite_width, uint32_t sprite_height,
  2703. int pixel_size, bool enabled, bool scaled)
  2704. {
  2705. struct intel_plane *intel_plane = to_intel_plane(plane);
  2706. struct drm_framebuffer *fb = plane->state->fb;
  2707. intel_plane->wm.enabled = enabled;
  2708. intel_plane->wm.scaled = scaled;
  2709. intel_plane->wm.horiz_pixels = sprite_width;
  2710. intel_plane->wm.vert_pixels = sprite_height;
  2711. intel_plane->wm.bytes_per_pixel = pixel_size;
  2712. intel_plane->wm.tiling = DRM_FORMAT_MOD_NONE;
  2713. /*
  2714. * Framebuffer can be NULL on plane disable, but it does not
  2715. * matter for watermarks if we assume no tiling in that case.
  2716. */
  2717. if (fb)
  2718. intel_plane->wm.tiling = fb->modifier[0];
  2719. skl_update_wm(crtc);
  2720. }
  2721. static void ilk_update_wm(struct drm_crtc *crtc)
  2722. {
  2723. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2724. struct drm_device *dev = crtc->dev;
  2725. struct drm_i915_private *dev_priv = dev->dev_private;
  2726. struct ilk_wm_maximums max;
  2727. struct ilk_pipe_wm_parameters params = {};
  2728. struct ilk_wm_values results = {};
  2729. enum intel_ddb_partitioning partitioning;
  2730. struct intel_pipe_wm pipe_wm = {};
  2731. struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
  2732. struct intel_wm_config config = {};
  2733. ilk_compute_wm_parameters(crtc, &params);
  2734. intel_compute_pipe_wm(crtc, &params, &pipe_wm);
  2735. if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
  2736. return;
  2737. intel_crtc->wm.active = pipe_wm;
  2738. ilk_compute_wm_config(dev, &config);
  2739. ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
  2740. ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
  2741. /* 5/6 split only in single pipe config on IVB+ */
  2742. if (INTEL_INFO(dev)->gen >= 7 &&
  2743. config.num_pipes_active == 1 && config.sprites_enabled) {
  2744. ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
  2745. ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
  2746. best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
  2747. } else {
  2748. best_lp_wm = &lp_wm_1_2;
  2749. }
  2750. partitioning = (best_lp_wm == &lp_wm_1_2) ?
  2751. INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
  2752. ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
  2753. ilk_write_wm_values(dev_priv, &results);
  2754. }
  2755. static void
  2756. ilk_update_sprite_wm(struct drm_plane *plane,
  2757. struct drm_crtc *crtc,
  2758. uint32_t sprite_width, uint32_t sprite_height,
  2759. int pixel_size, bool enabled, bool scaled)
  2760. {
  2761. struct drm_device *dev = plane->dev;
  2762. struct intel_plane *intel_plane = to_intel_plane(plane);
  2763. intel_plane->wm.enabled = enabled;
  2764. intel_plane->wm.scaled = scaled;
  2765. intel_plane->wm.horiz_pixels = sprite_width;
  2766. intel_plane->wm.vert_pixels = sprite_width;
  2767. intel_plane->wm.bytes_per_pixel = pixel_size;
  2768. /*
  2769. * IVB workaround: must disable low power watermarks for at least
  2770. * one frame before enabling scaling. LP watermarks can be re-enabled
  2771. * when scaling is disabled.
  2772. *
  2773. * WaCxSRDisabledForSpriteScaling:ivb
  2774. */
  2775. if (IS_IVYBRIDGE(dev) && scaled && ilk_disable_lp_wm(dev))
  2776. intel_wait_for_vblank(dev, intel_plane->pipe);
  2777. ilk_update_wm(crtc);
  2778. }
  2779. static void skl_pipe_wm_active_state(uint32_t val,
  2780. struct skl_pipe_wm *active,
  2781. bool is_transwm,
  2782. bool is_cursor,
  2783. int i,
  2784. int level)
  2785. {
  2786. bool is_enabled = (val & PLANE_WM_EN) != 0;
  2787. if (!is_transwm) {
  2788. if (!is_cursor) {
  2789. active->wm[level].plane_en[i] = is_enabled;
  2790. active->wm[level].plane_res_b[i] =
  2791. val & PLANE_WM_BLOCKS_MASK;
  2792. active->wm[level].plane_res_l[i] =
  2793. (val >> PLANE_WM_LINES_SHIFT) &
  2794. PLANE_WM_LINES_MASK;
  2795. } else {
  2796. active->wm[level].cursor_en = is_enabled;
  2797. active->wm[level].cursor_res_b =
  2798. val & PLANE_WM_BLOCKS_MASK;
  2799. active->wm[level].cursor_res_l =
  2800. (val >> PLANE_WM_LINES_SHIFT) &
  2801. PLANE_WM_LINES_MASK;
  2802. }
  2803. } else {
  2804. if (!is_cursor) {
  2805. active->trans_wm.plane_en[i] = is_enabled;
  2806. active->trans_wm.plane_res_b[i] =
  2807. val & PLANE_WM_BLOCKS_MASK;
  2808. active->trans_wm.plane_res_l[i] =
  2809. (val >> PLANE_WM_LINES_SHIFT) &
  2810. PLANE_WM_LINES_MASK;
  2811. } else {
  2812. active->trans_wm.cursor_en = is_enabled;
  2813. active->trans_wm.cursor_res_b =
  2814. val & PLANE_WM_BLOCKS_MASK;
  2815. active->trans_wm.cursor_res_l =
  2816. (val >> PLANE_WM_LINES_SHIFT) &
  2817. PLANE_WM_LINES_MASK;
  2818. }
  2819. }
  2820. }
  2821. static void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc)
  2822. {
  2823. struct drm_device *dev = crtc->dev;
  2824. struct drm_i915_private *dev_priv = dev->dev_private;
  2825. struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
  2826. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2827. struct skl_pipe_wm *active = &intel_crtc->wm.skl_active;
  2828. enum pipe pipe = intel_crtc->pipe;
  2829. int level, i, max_level;
  2830. uint32_t temp;
  2831. max_level = ilk_wm_max_level(dev);
  2832. hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
  2833. for (level = 0; level <= max_level; level++) {
  2834. for (i = 0; i < intel_num_planes(intel_crtc); i++)
  2835. hw->plane[pipe][i][level] =
  2836. I915_READ(PLANE_WM(pipe, i, level));
  2837. hw->cursor[pipe][level] = I915_READ(CUR_WM(pipe, level));
  2838. }
  2839. for (i = 0; i < intel_num_planes(intel_crtc); i++)
  2840. hw->plane_trans[pipe][i] = I915_READ(PLANE_WM_TRANS(pipe, i));
  2841. hw->cursor_trans[pipe] = I915_READ(CUR_WM_TRANS(pipe));
  2842. if (!intel_crtc_active(crtc))
  2843. return;
  2844. hw->dirty[pipe] = true;
  2845. active->linetime = hw->wm_linetime[pipe];
  2846. for (level = 0; level <= max_level; level++) {
  2847. for (i = 0; i < intel_num_planes(intel_crtc); i++) {
  2848. temp = hw->plane[pipe][i][level];
  2849. skl_pipe_wm_active_state(temp, active, false,
  2850. false, i, level);
  2851. }
  2852. temp = hw->cursor[pipe][level];
  2853. skl_pipe_wm_active_state(temp, active, false, true, i, level);
  2854. }
  2855. for (i = 0; i < intel_num_planes(intel_crtc); i++) {
  2856. temp = hw->plane_trans[pipe][i];
  2857. skl_pipe_wm_active_state(temp, active, true, false, i, 0);
  2858. }
  2859. temp = hw->cursor_trans[pipe];
  2860. skl_pipe_wm_active_state(temp, active, true, true, i, 0);
  2861. }
  2862. void skl_wm_get_hw_state(struct drm_device *dev)
  2863. {
  2864. struct drm_i915_private *dev_priv = dev->dev_private;
  2865. struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
  2866. struct drm_crtc *crtc;
  2867. skl_ddb_get_hw_state(dev_priv, ddb);
  2868. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  2869. skl_pipe_wm_get_hw_state(crtc);
  2870. }
  2871. static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
  2872. {
  2873. struct drm_device *dev = crtc->dev;
  2874. struct drm_i915_private *dev_priv = dev->dev_private;
  2875. struct ilk_wm_values *hw = &dev_priv->wm.hw;
  2876. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2877. struct intel_pipe_wm *active = &intel_crtc->wm.active;
  2878. enum pipe pipe = intel_crtc->pipe;
  2879. static const unsigned int wm0_pipe_reg[] = {
  2880. [PIPE_A] = WM0_PIPEA_ILK,
  2881. [PIPE_B] = WM0_PIPEB_ILK,
  2882. [PIPE_C] = WM0_PIPEC_IVB,
  2883. };
  2884. hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
  2885. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2886. hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
  2887. active->pipe_enabled = intel_crtc_active(crtc);
  2888. if (active->pipe_enabled) {
  2889. u32 tmp = hw->wm_pipe[pipe];
  2890. /*
  2891. * For active pipes LP0 watermark is marked as
  2892. * enabled, and LP1+ watermaks as disabled since
  2893. * we can't really reverse compute them in case
  2894. * multiple pipes are active.
  2895. */
  2896. active->wm[0].enable = true;
  2897. active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
  2898. active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
  2899. active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
  2900. active->linetime = hw->wm_linetime[pipe];
  2901. } else {
  2902. int level, max_level = ilk_wm_max_level(dev);
  2903. /*
  2904. * For inactive pipes, all watermark levels
  2905. * should be marked as enabled but zeroed,
  2906. * which is what we'd compute them to.
  2907. */
  2908. for (level = 0; level <= max_level; level++)
  2909. active->wm[level].enable = true;
  2910. }
  2911. }
  2912. void ilk_wm_get_hw_state(struct drm_device *dev)
  2913. {
  2914. struct drm_i915_private *dev_priv = dev->dev_private;
  2915. struct ilk_wm_values *hw = &dev_priv->wm.hw;
  2916. struct drm_crtc *crtc;
  2917. for_each_crtc(dev, crtc)
  2918. ilk_pipe_wm_get_hw_state(crtc);
  2919. hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
  2920. hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
  2921. hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
  2922. hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
  2923. if (INTEL_INFO(dev)->gen >= 7) {
  2924. hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
  2925. hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
  2926. }
  2927. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2928. hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
  2929. INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
  2930. else if (IS_IVYBRIDGE(dev))
  2931. hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
  2932. INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
  2933. hw->enable_fbc_wm =
  2934. !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
  2935. }
  2936. /**
  2937. * intel_update_watermarks - update FIFO watermark values based on current modes
  2938. *
  2939. * Calculate watermark values for the various WM regs based on current mode
  2940. * and plane configuration.
  2941. *
  2942. * There are several cases to deal with here:
  2943. * - normal (i.e. non-self-refresh)
  2944. * - self-refresh (SR) mode
  2945. * - lines are large relative to FIFO size (buffer can hold up to 2)
  2946. * - lines are small relative to FIFO size (buffer can hold more than 2
  2947. * lines), so need to account for TLB latency
  2948. *
  2949. * The normal calculation is:
  2950. * watermark = dotclock * bytes per pixel * latency
  2951. * where latency is platform & configuration dependent (we assume pessimal
  2952. * values here).
  2953. *
  2954. * The SR calculation is:
  2955. * watermark = (trunc(latency/line time)+1) * surface width *
  2956. * bytes per pixel
  2957. * where
  2958. * line time = htotal / dotclock
  2959. * surface width = hdisplay for normal plane and 64 for cursor
  2960. * and latency is assumed to be high, as above.
  2961. *
  2962. * The final value programmed to the register should always be rounded up,
  2963. * and include an extra 2 entries to account for clock crossings.
  2964. *
  2965. * We don't use the sprite, so we can ignore that. And on Crestline we have
  2966. * to set the non-SR watermarks to 8.
  2967. */
  2968. void intel_update_watermarks(struct drm_crtc *crtc)
  2969. {
  2970. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  2971. if (dev_priv->display.update_wm)
  2972. dev_priv->display.update_wm(crtc);
  2973. }
  2974. void intel_update_sprite_watermarks(struct drm_plane *plane,
  2975. struct drm_crtc *crtc,
  2976. uint32_t sprite_width,
  2977. uint32_t sprite_height,
  2978. int pixel_size,
  2979. bool enabled, bool scaled)
  2980. {
  2981. struct drm_i915_private *dev_priv = plane->dev->dev_private;
  2982. if (dev_priv->display.update_sprite_wm)
  2983. dev_priv->display.update_sprite_wm(plane, crtc,
  2984. sprite_width, sprite_height,
  2985. pixel_size, enabled, scaled);
  2986. }
  2987. /**
  2988. * Lock protecting IPS related data structures
  2989. */
  2990. DEFINE_SPINLOCK(mchdev_lock);
  2991. /* Global for IPS driver to get at the current i915 device. Protected by
  2992. * mchdev_lock. */
  2993. static struct drm_i915_private *i915_mch_dev;
  2994. bool ironlake_set_drps(struct drm_device *dev, u8 val)
  2995. {
  2996. struct drm_i915_private *dev_priv = dev->dev_private;
  2997. u16 rgvswctl;
  2998. assert_spin_locked(&mchdev_lock);
  2999. rgvswctl = I915_READ16(MEMSWCTL);
  3000. if (rgvswctl & MEMCTL_CMD_STS) {
  3001. DRM_DEBUG("gpu busy, RCS change rejected\n");
  3002. return false; /* still busy with another command */
  3003. }
  3004. rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
  3005. (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
  3006. I915_WRITE16(MEMSWCTL, rgvswctl);
  3007. POSTING_READ16(MEMSWCTL);
  3008. rgvswctl |= MEMCTL_CMD_STS;
  3009. I915_WRITE16(MEMSWCTL, rgvswctl);
  3010. return true;
  3011. }
  3012. static void ironlake_enable_drps(struct drm_device *dev)
  3013. {
  3014. struct drm_i915_private *dev_priv = dev->dev_private;
  3015. u32 rgvmodectl = I915_READ(MEMMODECTL);
  3016. u8 fmax, fmin, fstart, vstart;
  3017. spin_lock_irq(&mchdev_lock);
  3018. /* Enable temp reporting */
  3019. I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
  3020. I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
  3021. /* 100ms RC evaluation intervals */
  3022. I915_WRITE(RCUPEI, 100000);
  3023. I915_WRITE(RCDNEI, 100000);
  3024. /* Set max/min thresholds to 90ms and 80ms respectively */
  3025. I915_WRITE(RCBMAXAVG, 90000);
  3026. I915_WRITE(RCBMINAVG, 80000);
  3027. I915_WRITE(MEMIHYST, 1);
  3028. /* Set up min, max, and cur for interrupt handling */
  3029. fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
  3030. fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
  3031. fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
  3032. MEMMODE_FSTART_SHIFT;
  3033. vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
  3034. PXVFREQ_PX_SHIFT;
  3035. dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
  3036. dev_priv->ips.fstart = fstart;
  3037. dev_priv->ips.max_delay = fstart;
  3038. dev_priv->ips.min_delay = fmin;
  3039. dev_priv->ips.cur_delay = fstart;
  3040. DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
  3041. fmax, fmin, fstart);
  3042. I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
  3043. /*
  3044. * Interrupts will be enabled in ironlake_irq_postinstall
  3045. */
  3046. I915_WRITE(VIDSTART, vstart);
  3047. POSTING_READ(VIDSTART);
  3048. rgvmodectl |= MEMMODE_SWMODE_EN;
  3049. I915_WRITE(MEMMODECTL, rgvmodectl);
  3050. if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
  3051. DRM_ERROR("stuck trying to change perf mode\n");
  3052. mdelay(1);
  3053. ironlake_set_drps(dev, fstart);
  3054. dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
  3055. I915_READ(0x112e0);
  3056. dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
  3057. dev_priv->ips.last_count2 = I915_READ(0x112f4);
  3058. dev_priv->ips.last_time2 = ktime_get_raw_ns();
  3059. spin_unlock_irq(&mchdev_lock);
  3060. }
  3061. static void ironlake_disable_drps(struct drm_device *dev)
  3062. {
  3063. struct drm_i915_private *dev_priv = dev->dev_private;
  3064. u16 rgvswctl;
  3065. spin_lock_irq(&mchdev_lock);
  3066. rgvswctl = I915_READ16(MEMSWCTL);
  3067. /* Ack interrupts, disable EFC interrupt */
  3068. I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
  3069. I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
  3070. I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
  3071. I915_WRITE(DEIIR, DE_PCU_EVENT);
  3072. I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
  3073. /* Go back to the starting frequency */
  3074. ironlake_set_drps(dev, dev_priv->ips.fstart);
  3075. mdelay(1);
  3076. rgvswctl |= MEMCTL_CMD_STS;
  3077. I915_WRITE(MEMSWCTL, rgvswctl);
  3078. mdelay(1);
  3079. spin_unlock_irq(&mchdev_lock);
  3080. }
  3081. /* There's a funny hw issue where the hw returns all 0 when reading from
  3082. * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
  3083. * ourselves, instead of doing a rmw cycle (which might result in us clearing
  3084. * all limits and the gpu stuck at whatever frequency it is at atm).
  3085. */
  3086. static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 val)
  3087. {
  3088. u32 limits;
  3089. /* Only set the down limit when we've reached the lowest level to avoid
  3090. * getting more interrupts, otherwise leave this clear. This prevents a
  3091. * race in the hw when coming out of rc6: There's a tiny window where
  3092. * the hw runs at the minimal clock before selecting the desired
  3093. * frequency, if the down threshold expires in that window we will not
  3094. * receive a down interrupt. */
  3095. limits = dev_priv->rps.max_freq_softlimit << 24;
  3096. if (val <= dev_priv->rps.min_freq_softlimit)
  3097. limits |= dev_priv->rps.min_freq_softlimit << 16;
  3098. return limits;
  3099. }
  3100. static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
  3101. {
  3102. int new_power;
  3103. new_power = dev_priv->rps.power;
  3104. switch (dev_priv->rps.power) {
  3105. case LOW_POWER:
  3106. if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
  3107. new_power = BETWEEN;
  3108. break;
  3109. case BETWEEN:
  3110. if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
  3111. new_power = LOW_POWER;
  3112. else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
  3113. new_power = HIGH_POWER;
  3114. break;
  3115. case HIGH_POWER:
  3116. if (val < (dev_priv->rps.rp1_freq + dev_priv->rps.rp0_freq) >> 1 && val < dev_priv->rps.cur_freq)
  3117. new_power = BETWEEN;
  3118. break;
  3119. }
  3120. /* Max/min bins are special */
  3121. if (val == dev_priv->rps.min_freq_softlimit)
  3122. new_power = LOW_POWER;
  3123. if (val == dev_priv->rps.max_freq_softlimit)
  3124. new_power = HIGH_POWER;
  3125. if (new_power == dev_priv->rps.power)
  3126. return;
  3127. /* Note the units here are not exactly 1us, but 1280ns. */
  3128. switch (new_power) {
  3129. case LOW_POWER:
  3130. /* Upclock if more than 95% busy over 16ms */
  3131. I915_WRITE(GEN6_RP_UP_EI, 12500);
  3132. I915_WRITE(GEN6_RP_UP_THRESHOLD, 11800);
  3133. /* Downclock if less than 85% busy over 32ms */
  3134. I915_WRITE(GEN6_RP_DOWN_EI, 25000);
  3135. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 21250);
  3136. I915_WRITE(GEN6_RP_CONTROL,
  3137. GEN6_RP_MEDIA_TURBO |
  3138. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  3139. GEN6_RP_MEDIA_IS_GFX |
  3140. GEN6_RP_ENABLE |
  3141. GEN6_RP_UP_BUSY_AVG |
  3142. GEN6_RP_DOWN_IDLE_AVG);
  3143. break;
  3144. case BETWEEN:
  3145. /* Upclock if more than 90% busy over 13ms */
  3146. I915_WRITE(GEN6_RP_UP_EI, 10250);
  3147. I915_WRITE(GEN6_RP_UP_THRESHOLD, 9225);
  3148. /* Downclock if less than 75% busy over 32ms */
  3149. I915_WRITE(GEN6_RP_DOWN_EI, 25000);
  3150. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 18750);
  3151. I915_WRITE(GEN6_RP_CONTROL,
  3152. GEN6_RP_MEDIA_TURBO |
  3153. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  3154. GEN6_RP_MEDIA_IS_GFX |
  3155. GEN6_RP_ENABLE |
  3156. GEN6_RP_UP_BUSY_AVG |
  3157. GEN6_RP_DOWN_IDLE_AVG);
  3158. break;
  3159. case HIGH_POWER:
  3160. /* Upclock if more than 85% busy over 10ms */
  3161. I915_WRITE(GEN6_RP_UP_EI, 8000);
  3162. I915_WRITE(GEN6_RP_UP_THRESHOLD, 6800);
  3163. /* Downclock if less than 60% busy over 32ms */
  3164. I915_WRITE(GEN6_RP_DOWN_EI, 25000);
  3165. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 15000);
  3166. I915_WRITE(GEN6_RP_CONTROL,
  3167. GEN6_RP_MEDIA_TURBO |
  3168. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  3169. GEN6_RP_MEDIA_IS_GFX |
  3170. GEN6_RP_ENABLE |
  3171. GEN6_RP_UP_BUSY_AVG |
  3172. GEN6_RP_DOWN_IDLE_AVG);
  3173. break;
  3174. }
  3175. dev_priv->rps.power = new_power;
  3176. dev_priv->rps.last_adj = 0;
  3177. }
  3178. static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
  3179. {
  3180. u32 mask = 0;
  3181. if (val > dev_priv->rps.min_freq_softlimit)
  3182. mask |= GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
  3183. if (val < dev_priv->rps.max_freq_softlimit)
  3184. mask |= GEN6_PM_RP_UP_THRESHOLD;
  3185. mask |= dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED);
  3186. mask &= dev_priv->pm_rps_events;
  3187. return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
  3188. }
  3189. /* gen6_set_rps is called to update the frequency request, but should also be
  3190. * called when the range (min_delay and max_delay) is modified so that we can
  3191. * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
  3192. static void gen6_set_rps(struct drm_device *dev, u8 val)
  3193. {
  3194. struct drm_i915_private *dev_priv = dev->dev_private;
  3195. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  3196. WARN_ON(val > dev_priv->rps.max_freq_softlimit);
  3197. WARN_ON(val < dev_priv->rps.min_freq_softlimit);
  3198. /* min/max delay may still have been modified so be sure to
  3199. * write the limits value.
  3200. */
  3201. if (val != dev_priv->rps.cur_freq) {
  3202. gen6_set_rps_thresholds(dev_priv, val);
  3203. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  3204. I915_WRITE(GEN6_RPNSWREQ,
  3205. HSW_FREQUENCY(val));
  3206. else
  3207. I915_WRITE(GEN6_RPNSWREQ,
  3208. GEN6_FREQUENCY(val) |
  3209. GEN6_OFFSET(0) |
  3210. GEN6_AGGRESSIVE_TURBO);
  3211. }
  3212. /* Make sure we continue to get interrupts
  3213. * until we hit the minimum or maximum frequencies.
  3214. */
  3215. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, gen6_rps_limits(dev_priv, val));
  3216. I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
  3217. POSTING_READ(GEN6_RPNSWREQ);
  3218. dev_priv->rps.cur_freq = val;
  3219. trace_intel_gpu_freq_change(val * 50);
  3220. }
  3221. static void valleyview_set_rps(struct drm_device *dev, u8 val)
  3222. {
  3223. struct drm_i915_private *dev_priv = dev->dev_private;
  3224. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  3225. WARN_ON(val > dev_priv->rps.max_freq_softlimit);
  3226. WARN_ON(val < dev_priv->rps.min_freq_softlimit);
  3227. if (WARN_ONCE(IS_CHERRYVIEW(dev) && (val & 1),
  3228. "Odd GPU freq value\n"))
  3229. val &= ~1;
  3230. if (val != dev_priv->rps.cur_freq)
  3231. vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
  3232. I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
  3233. dev_priv->rps.cur_freq = val;
  3234. trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
  3235. }
  3236. /* vlv_set_rps_idle: Set the frequency to Rpn if Gfx clocks are down
  3237. *
  3238. * * If Gfx is Idle, then
  3239. * 1. Mask Turbo interrupts
  3240. * 2. Bring up Gfx clock
  3241. * 3. Change the freq to Rpn and wait till P-Unit updates freq
  3242. * 4. Clear the Force GFX CLK ON bit so that Gfx can down
  3243. * 5. Unmask Turbo interrupts
  3244. */
  3245. static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
  3246. {
  3247. struct drm_device *dev = dev_priv->dev;
  3248. /* CHV and latest VLV don't need to force the gfx clock */
  3249. if (IS_CHERRYVIEW(dev) || dev->pdev->revision >= 0xd) {
  3250. valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
  3251. return;
  3252. }
  3253. /*
  3254. * When we are idle. Drop to min voltage state.
  3255. */
  3256. if (dev_priv->rps.cur_freq <= dev_priv->rps.min_freq_softlimit)
  3257. return;
  3258. /* Mask turbo interrupt so that they will not come in between */
  3259. I915_WRITE(GEN6_PMINTRMSK,
  3260. gen6_sanitize_rps_pm_mask(dev_priv, ~0));
  3261. vlv_force_gfx_clock(dev_priv, true);
  3262. dev_priv->rps.cur_freq = dev_priv->rps.min_freq_softlimit;
  3263. vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ,
  3264. dev_priv->rps.min_freq_softlimit);
  3265. if (wait_for(((vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS))
  3266. & GENFREQSTATUS) == 0, 100))
  3267. DRM_ERROR("timed out waiting for Punit\n");
  3268. vlv_force_gfx_clock(dev_priv, false);
  3269. I915_WRITE(GEN6_PMINTRMSK,
  3270. gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
  3271. }
  3272. void gen6_rps_idle(struct drm_i915_private *dev_priv)
  3273. {
  3274. struct drm_device *dev = dev_priv->dev;
  3275. mutex_lock(&dev_priv->rps.hw_lock);
  3276. if (dev_priv->rps.enabled) {
  3277. if (IS_VALLEYVIEW(dev))
  3278. vlv_set_rps_idle(dev_priv);
  3279. else
  3280. gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
  3281. dev_priv->rps.last_adj = 0;
  3282. }
  3283. mutex_unlock(&dev_priv->rps.hw_lock);
  3284. }
  3285. void gen6_rps_boost(struct drm_i915_private *dev_priv)
  3286. {
  3287. mutex_lock(&dev_priv->rps.hw_lock);
  3288. if (dev_priv->rps.enabled) {
  3289. intel_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
  3290. dev_priv->rps.last_adj = 0;
  3291. }
  3292. mutex_unlock(&dev_priv->rps.hw_lock);
  3293. }
  3294. void intel_set_rps(struct drm_device *dev, u8 val)
  3295. {
  3296. if (IS_VALLEYVIEW(dev))
  3297. valleyview_set_rps(dev, val);
  3298. else
  3299. gen6_set_rps(dev, val);
  3300. }
  3301. static void gen9_disable_rps(struct drm_device *dev)
  3302. {
  3303. struct drm_i915_private *dev_priv = dev->dev_private;
  3304. I915_WRITE(GEN6_RC_CONTROL, 0);
  3305. I915_WRITE(GEN9_PG_ENABLE, 0);
  3306. }
  3307. static void gen6_disable_rps(struct drm_device *dev)
  3308. {
  3309. struct drm_i915_private *dev_priv = dev->dev_private;
  3310. I915_WRITE(GEN6_RC_CONTROL, 0);
  3311. I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
  3312. }
  3313. static void cherryview_disable_rps(struct drm_device *dev)
  3314. {
  3315. struct drm_i915_private *dev_priv = dev->dev_private;
  3316. I915_WRITE(GEN6_RC_CONTROL, 0);
  3317. }
  3318. static void valleyview_disable_rps(struct drm_device *dev)
  3319. {
  3320. struct drm_i915_private *dev_priv = dev->dev_private;
  3321. /* we're doing forcewake before Disabling RC6,
  3322. * This what the BIOS expects when going into suspend */
  3323. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  3324. I915_WRITE(GEN6_RC_CONTROL, 0);
  3325. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  3326. }
  3327. static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
  3328. {
  3329. if (IS_VALLEYVIEW(dev)) {
  3330. if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
  3331. mode = GEN6_RC_CTL_RC6_ENABLE;
  3332. else
  3333. mode = 0;
  3334. }
  3335. if (HAS_RC6p(dev))
  3336. DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s RC6p %s RC6pp %s\n",
  3337. (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
  3338. (mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
  3339. (mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
  3340. else
  3341. DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s\n",
  3342. (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off");
  3343. }
  3344. static int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
  3345. {
  3346. /* No RC6 before Ironlake */
  3347. if (INTEL_INFO(dev)->gen < 5)
  3348. return 0;
  3349. /* RC6 is only on Ironlake mobile not on desktop */
  3350. if (INTEL_INFO(dev)->gen == 5 && !IS_IRONLAKE_M(dev))
  3351. return 0;
  3352. /* Respect the kernel parameter if it is set */
  3353. if (enable_rc6 >= 0) {
  3354. int mask;
  3355. if (HAS_RC6p(dev))
  3356. mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
  3357. INTEL_RC6pp_ENABLE;
  3358. else
  3359. mask = INTEL_RC6_ENABLE;
  3360. if ((enable_rc6 & mask) != enable_rc6)
  3361. DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
  3362. enable_rc6 & mask, enable_rc6, mask);
  3363. return enable_rc6 & mask;
  3364. }
  3365. /* Disable RC6 on Ironlake */
  3366. if (INTEL_INFO(dev)->gen == 5)
  3367. return 0;
  3368. if (IS_IVYBRIDGE(dev))
  3369. return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
  3370. return INTEL_RC6_ENABLE;
  3371. }
  3372. int intel_enable_rc6(const struct drm_device *dev)
  3373. {
  3374. return i915.enable_rc6;
  3375. }
  3376. static void gen6_init_rps_frequencies(struct drm_device *dev)
  3377. {
  3378. struct drm_i915_private *dev_priv = dev->dev_private;
  3379. uint32_t rp_state_cap;
  3380. u32 ddcc_status = 0;
  3381. int ret;
  3382. rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
  3383. /* All of these values are in units of 50MHz */
  3384. dev_priv->rps.cur_freq = 0;
  3385. /* static values from HW: RP0 > RP1 > RPn (min_freq) */
  3386. dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
  3387. dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
  3388. dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
  3389. /* hw_max = RP0 until we check for overclocking */
  3390. dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
  3391. dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
  3392. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  3393. ret = sandybridge_pcode_read(dev_priv,
  3394. HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
  3395. &ddcc_status);
  3396. if (0 == ret)
  3397. dev_priv->rps.efficient_freq =
  3398. clamp_t(u8,
  3399. ((ddcc_status >> 8) & 0xff),
  3400. dev_priv->rps.min_freq,
  3401. dev_priv->rps.max_freq);
  3402. }
  3403. /* Preserve min/max settings in case of re-init */
  3404. if (dev_priv->rps.max_freq_softlimit == 0)
  3405. dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
  3406. if (dev_priv->rps.min_freq_softlimit == 0) {
  3407. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  3408. dev_priv->rps.min_freq_softlimit =
  3409. /* max(RPe, 450 MHz) */
  3410. max(dev_priv->rps.efficient_freq, (u8) 9);
  3411. else
  3412. dev_priv->rps.min_freq_softlimit =
  3413. dev_priv->rps.min_freq;
  3414. }
  3415. }
  3416. /* See the Gen9_GT_PM_Programming_Guide doc for the below */
  3417. static void gen9_enable_rps(struct drm_device *dev)
  3418. {
  3419. struct drm_i915_private *dev_priv = dev->dev_private;
  3420. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  3421. gen6_init_rps_frequencies(dev);
  3422. I915_WRITE(GEN6_RPNSWREQ, 0xc800000);
  3423. I915_WRITE(GEN6_RC_VIDEO_FREQ, 0xc800000);
  3424. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 0xf4240);
  3425. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, 0x12060000);
  3426. I915_WRITE(GEN6_RP_UP_THRESHOLD, 0xe808);
  3427. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 0x3bd08);
  3428. I915_WRITE(GEN6_RP_UP_EI, 0x101d0);
  3429. I915_WRITE(GEN6_RP_DOWN_EI, 0x55730);
  3430. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
  3431. I915_WRITE(GEN6_PMINTRMSK, 0x6);
  3432. I915_WRITE(GEN6_RP_CONTROL, GEN6_RP_MEDIA_TURBO |
  3433. GEN6_RP_MEDIA_HW_MODE | GEN6_RP_MEDIA_IS_GFX |
  3434. GEN6_RP_ENABLE | GEN6_RP_UP_BUSY_AVG |
  3435. GEN6_RP_DOWN_IDLE_AVG);
  3436. gen6_enable_rps_interrupts(dev);
  3437. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  3438. }
  3439. static void gen9_enable_rc6(struct drm_device *dev)
  3440. {
  3441. struct drm_i915_private *dev_priv = dev->dev_private;
  3442. struct intel_engine_cs *ring;
  3443. uint32_t rc6_mask = 0;
  3444. int unused;
  3445. /* 1a: Software RC state - RC0 */
  3446. I915_WRITE(GEN6_RC_STATE, 0);
  3447. /* 1b: Get forcewake during program sequence. Although the driver
  3448. * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
  3449. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  3450. /* 2a: Disable RC states. */
  3451. I915_WRITE(GEN6_RC_CONTROL, 0);
  3452. /* 2b: Program RC6 thresholds.*/
  3453. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
  3454. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
  3455. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
  3456. for_each_ring(ring, dev_priv, unused)
  3457. I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
  3458. I915_WRITE(GEN6_RC_SLEEP, 0);
  3459. I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
  3460. /* 2c: Program Coarse Power Gating Policies. */
  3461. I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
  3462. I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
  3463. /* 3a: Enable RC6 */
  3464. if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
  3465. rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
  3466. DRM_INFO("RC6 %s\n", (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
  3467. "on" : "off");
  3468. I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
  3469. GEN6_RC_CTL_EI_MODE(1) |
  3470. rc6_mask);
  3471. /* 3b: Enable Coarse Power Gating only when RC6 is enabled */
  3472. I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ? 3 : 0);
  3473. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  3474. }
  3475. static void gen8_enable_rps(struct drm_device *dev)
  3476. {
  3477. struct drm_i915_private *dev_priv = dev->dev_private;
  3478. struct intel_engine_cs *ring;
  3479. uint32_t rc6_mask = 0;
  3480. int unused;
  3481. /* 1a: Software RC state - RC0 */
  3482. I915_WRITE(GEN6_RC_STATE, 0);
  3483. /* 1c & 1d: Get forcewake during program sequence. Although the driver
  3484. * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
  3485. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  3486. /* 2a: Disable RC states. */
  3487. I915_WRITE(GEN6_RC_CONTROL, 0);
  3488. /* Initialize rps frequencies */
  3489. gen6_init_rps_frequencies(dev);
  3490. /* 2b: Program RC6 thresholds.*/
  3491. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
  3492. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
  3493. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
  3494. for_each_ring(ring, dev_priv, unused)
  3495. I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
  3496. I915_WRITE(GEN6_RC_SLEEP, 0);
  3497. if (IS_BROADWELL(dev))
  3498. I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
  3499. else
  3500. I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
  3501. /* 3: Enable RC6 */
  3502. if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
  3503. rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
  3504. intel_print_rc6_info(dev, rc6_mask);
  3505. if (IS_BROADWELL(dev))
  3506. I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
  3507. GEN7_RC_CTL_TO_MODE |
  3508. rc6_mask);
  3509. else
  3510. I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
  3511. GEN6_RC_CTL_EI_MODE(1) |
  3512. rc6_mask);
  3513. /* 4 Program defaults and thresholds for RPS*/
  3514. I915_WRITE(GEN6_RPNSWREQ,
  3515. HSW_FREQUENCY(dev_priv->rps.rp1_freq));
  3516. I915_WRITE(GEN6_RC_VIDEO_FREQ,
  3517. HSW_FREQUENCY(dev_priv->rps.rp1_freq));
  3518. /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
  3519. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
  3520. /* Docs recommend 900MHz, and 300 MHz respectively */
  3521. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
  3522. dev_priv->rps.max_freq_softlimit << 24 |
  3523. dev_priv->rps.min_freq_softlimit << 16);
  3524. I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
  3525. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
  3526. I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
  3527. I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
  3528. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  3529. /* 5: Enable RPS */
  3530. I915_WRITE(GEN6_RP_CONTROL,
  3531. GEN6_RP_MEDIA_TURBO |
  3532. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  3533. GEN6_RP_MEDIA_IS_GFX |
  3534. GEN6_RP_ENABLE |
  3535. GEN6_RP_UP_BUSY_AVG |
  3536. GEN6_RP_DOWN_IDLE_AVG);
  3537. /* 6: Ring frequency + overclocking (our driver does this later */
  3538. dev_priv->rps.power = HIGH_POWER; /* force a reset */
  3539. gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
  3540. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  3541. }
  3542. static void gen6_enable_rps(struct drm_device *dev)
  3543. {
  3544. struct drm_i915_private *dev_priv = dev->dev_private;
  3545. struct intel_engine_cs *ring;
  3546. u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
  3547. u32 gtfifodbg;
  3548. int rc6_mode;
  3549. int i, ret;
  3550. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  3551. /* Here begins a magic sequence of register writes to enable
  3552. * auto-downclocking.
  3553. *
  3554. * Perhaps there might be some value in exposing these to
  3555. * userspace...
  3556. */
  3557. I915_WRITE(GEN6_RC_STATE, 0);
  3558. /* Clear the DBG now so we don't confuse earlier errors */
  3559. if ((gtfifodbg = I915_READ(GTFIFODBG))) {
  3560. DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
  3561. I915_WRITE(GTFIFODBG, gtfifodbg);
  3562. }
  3563. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  3564. /* Initialize rps frequencies */
  3565. gen6_init_rps_frequencies(dev);
  3566. /* disable the counters and set deterministic thresholds */
  3567. I915_WRITE(GEN6_RC_CONTROL, 0);
  3568. I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
  3569. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
  3570. I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
  3571. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
  3572. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
  3573. for_each_ring(ring, dev_priv, i)
  3574. I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
  3575. I915_WRITE(GEN6_RC_SLEEP, 0);
  3576. I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
  3577. if (IS_IVYBRIDGE(dev))
  3578. I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
  3579. else
  3580. I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
  3581. I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
  3582. I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
  3583. /* Check if we are enabling RC6 */
  3584. rc6_mode = intel_enable_rc6(dev_priv->dev);
  3585. if (rc6_mode & INTEL_RC6_ENABLE)
  3586. rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
  3587. /* We don't use those on Haswell */
  3588. if (!IS_HASWELL(dev)) {
  3589. if (rc6_mode & INTEL_RC6p_ENABLE)
  3590. rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
  3591. if (rc6_mode & INTEL_RC6pp_ENABLE)
  3592. rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
  3593. }
  3594. intel_print_rc6_info(dev, rc6_mask);
  3595. I915_WRITE(GEN6_RC_CONTROL,
  3596. rc6_mask |
  3597. GEN6_RC_CTL_EI_MODE(1) |
  3598. GEN6_RC_CTL_HW_ENABLE);
  3599. /* Power down if completely idle for over 50ms */
  3600. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
  3601. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  3602. ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
  3603. if (ret)
  3604. DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
  3605. ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
  3606. if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
  3607. DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
  3608. (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
  3609. (pcu_mbox & 0xff) * 50);
  3610. dev_priv->rps.max_freq = pcu_mbox & 0xff;
  3611. }
  3612. dev_priv->rps.power = HIGH_POWER; /* force a reset */
  3613. gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
  3614. rc6vids = 0;
  3615. ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
  3616. if (IS_GEN6(dev) && ret) {
  3617. DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
  3618. } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
  3619. DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
  3620. GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
  3621. rc6vids &= 0xffff00;
  3622. rc6vids |= GEN6_ENCODE_RC6_VID(450);
  3623. ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
  3624. if (ret)
  3625. DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
  3626. }
  3627. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  3628. }
  3629. static void __gen6_update_ring_freq(struct drm_device *dev)
  3630. {
  3631. struct drm_i915_private *dev_priv = dev->dev_private;
  3632. int min_freq = 15;
  3633. unsigned int gpu_freq;
  3634. unsigned int max_ia_freq, min_ring_freq;
  3635. int scaling_factor = 180;
  3636. struct cpufreq_policy *policy;
  3637. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  3638. policy = cpufreq_cpu_get(0);
  3639. if (policy) {
  3640. max_ia_freq = policy->cpuinfo.max_freq;
  3641. cpufreq_cpu_put(policy);
  3642. } else {
  3643. /*
  3644. * Default to measured freq if none found, PCU will ensure we
  3645. * don't go over
  3646. */
  3647. max_ia_freq = tsc_khz;
  3648. }
  3649. /* Convert from kHz to MHz */
  3650. max_ia_freq /= 1000;
  3651. min_ring_freq = I915_READ(DCLK) & 0xf;
  3652. /* convert DDR frequency from units of 266.6MHz to bandwidth */
  3653. min_ring_freq = mult_frac(min_ring_freq, 8, 3);
  3654. /*
  3655. * For each potential GPU frequency, load a ring frequency we'd like
  3656. * to use for memory access. We do this by specifying the IA frequency
  3657. * the PCU should use as a reference to determine the ring frequency.
  3658. */
  3659. for (gpu_freq = dev_priv->rps.max_freq; gpu_freq >= dev_priv->rps.min_freq;
  3660. gpu_freq--) {
  3661. int diff = dev_priv->rps.max_freq - gpu_freq;
  3662. unsigned int ia_freq = 0, ring_freq = 0;
  3663. if (INTEL_INFO(dev)->gen >= 8) {
  3664. /* max(2 * GT, DDR). NB: GT is 50MHz units */
  3665. ring_freq = max(min_ring_freq, gpu_freq);
  3666. } else if (IS_HASWELL(dev)) {
  3667. ring_freq = mult_frac(gpu_freq, 5, 4);
  3668. ring_freq = max(min_ring_freq, ring_freq);
  3669. /* leave ia_freq as the default, chosen by cpufreq */
  3670. } else {
  3671. /* On older processors, there is no separate ring
  3672. * clock domain, so in order to boost the bandwidth
  3673. * of the ring, we need to upclock the CPU (ia_freq).
  3674. *
  3675. * For GPU frequencies less than 750MHz,
  3676. * just use the lowest ring freq.
  3677. */
  3678. if (gpu_freq < min_freq)
  3679. ia_freq = 800;
  3680. else
  3681. ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
  3682. ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
  3683. }
  3684. sandybridge_pcode_write(dev_priv,
  3685. GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
  3686. ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
  3687. ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
  3688. gpu_freq);
  3689. }
  3690. }
  3691. void gen6_update_ring_freq(struct drm_device *dev)
  3692. {
  3693. struct drm_i915_private *dev_priv = dev->dev_private;
  3694. if (INTEL_INFO(dev)->gen < 6 || IS_VALLEYVIEW(dev))
  3695. return;
  3696. mutex_lock(&dev_priv->rps.hw_lock);
  3697. __gen6_update_ring_freq(dev);
  3698. mutex_unlock(&dev_priv->rps.hw_lock);
  3699. }
  3700. static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
  3701. {
  3702. struct drm_device *dev = dev_priv->dev;
  3703. u32 val, rp0;
  3704. if (dev->pdev->revision >= 0x20) {
  3705. val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
  3706. switch (INTEL_INFO(dev)->eu_total) {
  3707. case 8:
  3708. /* (2 * 4) config */
  3709. rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
  3710. break;
  3711. case 12:
  3712. /* (2 * 6) config */
  3713. rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
  3714. break;
  3715. case 16:
  3716. /* (2 * 8) config */
  3717. default:
  3718. /* Setting (2 * 8) Min RP0 for any other combination */
  3719. rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
  3720. break;
  3721. }
  3722. rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
  3723. } else {
  3724. /* For pre-production hardware */
  3725. val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
  3726. rp0 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) &
  3727. PUNIT_GPU_STATUS_MAX_FREQ_MASK;
  3728. }
  3729. return rp0;
  3730. }
  3731. static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
  3732. {
  3733. u32 val, rpe;
  3734. val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
  3735. rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
  3736. return rpe;
  3737. }
  3738. static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
  3739. {
  3740. struct drm_device *dev = dev_priv->dev;
  3741. u32 val, rp1;
  3742. if (dev->pdev->revision >= 0x20) {
  3743. val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
  3744. rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
  3745. } else {
  3746. /* For pre-production hardware */
  3747. val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
  3748. rp1 = ((val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) &
  3749. PUNIT_GPU_STATUS_MAX_FREQ_MASK);
  3750. }
  3751. return rp1;
  3752. }
  3753. static int cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
  3754. {
  3755. struct drm_device *dev = dev_priv->dev;
  3756. u32 val, rpn;
  3757. if (dev->pdev->revision >= 0x20) {
  3758. val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
  3759. rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
  3760. FB_GFX_FREQ_FUSE_MASK);
  3761. } else { /* For pre-production hardware */
  3762. val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
  3763. rpn = ((val >> PUNIT_GPU_STATIS_GFX_MIN_FREQ_SHIFT) &
  3764. PUNIT_GPU_STATUS_GFX_MIN_FREQ_MASK);
  3765. }
  3766. return rpn;
  3767. }
  3768. static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
  3769. {
  3770. u32 val, rp1;
  3771. val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
  3772. rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
  3773. return rp1;
  3774. }
  3775. static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
  3776. {
  3777. u32 val, rp0;
  3778. val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
  3779. rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
  3780. /* Clamp to max */
  3781. rp0 = min_t(u32, rp0, 0xea);
  3782. return rp0;
  3783. }
  3784. static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
  3785. {
  3786. u32 val, rpe;
  3787. val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
  3788. rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
  3789. val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
  3790. rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
  3791. return rpe;
  3792. }
  3793. static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
  3794. {
  3795. return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
  3796. }
  3797. /* Check that the pctx buffer wasn't move under us. */
  3798. static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
  3799. {
  3800. unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
  3801. WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
  3802. dev_priv->vlv_pctx->stolen->start);
  3803. }
  3804. /* Check that the pcbr address is not empty. */
  3805. static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
  3806. {
  3807. unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
  3808. WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
  3809. }
  3810. static void cherryview_setup_pctx(struct drm_device *dev)
  3811. {
  3812. struct drm_i915_private *dev_priv = dev->dev_private;
  3813. unsigned long pctx_paddr, paddr;
  3814. struct i915_gtt *gtt = &dev_priv->gtt;
  3815. u32 pcbr;
  3816. int pctx_size = 32*1024;
  3817. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  3818. pcbr = I915_READ(VLV_PCBR);
  3819. if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
  3820. DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
  3821. paddr = (dev_priv->mm.stolen_base +
  3822. (gtt->stolen_size - pctx_size));
  3823. pctx_paddr = (paddr & (~4095));
  3824. I915_WRITE(VLV_PCBR, pctx_paddr);
  3825. }
  3826. DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
  3827. }
  3828. static void valleyview_setup_pctx(struct drm_device *dev)
  3829. {
  3830. struct drm_i915_private *dev_priv = dev->dev_private;
  3831. struct drm_i915_gem_object *pctx;
  3832. unsigned long pctx_paddr;
  3833. u32 pcbr;
  3834. int pctx_size = 24*1024;
  3835. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  3836. pcbr = I915_READ(VLV_PCBR);
  3837. if (pcbr) {
  3838. /* BIOS set it up already, grab the pre-alloc'd space */
  3839. int pcbr_offset;
  3840. pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
  3841. pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
  3842. pcbr_offset,
  3843. I915_GTT_OFFSET_NONE,
  3844. pctx_size);
  3845. goto out;
  3846. }
  3847. DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
  3848. /*
  3849. * From the Gunit register HAS:
  3850. * The Gfx driver is expected to program this register and ensure
  3851. * proper allocation within Gfx stolen memory. For example, this
  3852. * register should be programmed such than the PCBR range does not
  3853. * overlap with other ranges, such as the frame buffer, protected
  3854. * memory, or any other relevant ranges.
  3855. */
  3856. pctx = i915_gem_object_create_stolen(dev, pctx_size);
  3857. if (!pctx) {
  3858. DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
  3859. return;
  3860. }
  3861. pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
  3862. I915_WRITE(VLV_PCBR, pctx_paddr);
  3863. out:
  3864. DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
  3865. dev_priv->vlv_pctx = pctx;
  3866. }
  3867. static void valleyview_cleanup_pctx(struct drm_device *dev)
  3868. {
  3869. struct drm_i915_private *dev_priv = dev->dev_private;
  3870. if (WARN_ON(!dev_priv->vlv_pctx))
  3871. return;
  3872. drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
  3873. dev_priv->vlv_pctx = NULL;
  3874. }
  3875. static void valleyview_init_gt_powersave(struct drm_device *dev)
  3876. {
  3877. struct drm_i915_private *dev_priv = dev->dev_private;
  3878. u32 val;
  3879. valleyview_setup_pctx(dev);
  3880. mutex_lock(&dev_priv->rps.hw_lock);
  3881. val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
  3882. switch ((val >> 6) & 3) {
  3883. case 0:
  3884. case 1:
  3885. dev_priv->mem_freq = 800;
  3886. break;
  3887. case 2:
  3888. dev_priv->mem_freq = 1066;
  3889. break;
  3890. case 3:
  3891. dev_priv->mem_freq = 1333;
  3892. break;
  3893. }
  3894. DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
  3895. dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
  3896. dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
  3897. DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
  3898. intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
  3899. dev_priv->rps.max_freq);
  3900. dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
  3901. DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
  3902. intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
  3903. dev_priv->rps.efficient_freq);
  3904. dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
  3905. DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
  3906. intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
  3907. dev_priv->rps.rp1_freq);
  3908. dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
  3909. DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
  3910. intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
  3911. dev_priv->rps.min_freq);
  3912. /* Preserve min/max settings in case of re-init */
  3913. if (dev_priv->rps.max_freq_softlimit == 0)
  3914. dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
  3915. if (dev_priv->rps.min_freq_softlimit == 0)
  3916. dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
  3917. mutex_unlock(&dev_priv->rps.hw_lock);
  3918. }
  3919. static void cherryview_init_gt_powersave(struct drm_device *dev)
  3920. {
  3921. struct drm_i915_private *dev_priv = dev->dev_private;
  3922. u32 val;
  3923. cherryview_setup_pctx(dev);
  3924. mutex_lock(&dev_priv->rps.hw_lock);
  3925. mutex_lock(&dev_priv->dpio_lock);
  3926. val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
  3927. mutex_unlock(&dev_priv->dpio_lock);
  3928. switch ((val >> 2) & 0x7) {
  3929. case 0:
  3930. case 1:
  3931. dev_priv->rps.cz_freq = 200;
  3932. dev_priv->mem_freq = 1600;
  3933. break;
  3934. case 2:
  3935. dev_priv->rps.cz_freq = 267;
  3936. dev_priv->mem_freq = 1600;
  3937. break;
  3938. case 3:
  3939. dev_priv->rps.cz_freq = 333;
  3940. dev_priv->mem_freq = 2000;
  3941. break;
  3942. case 4:
  3943. dev_priv->rps.cz_freq = 320;
  3944. dev_priv->mem_freq = 1600;
  3945. break;
  3946. case 5:
  3947. dev_priv->rps.cz_freq = 400;
  3948. dev_priv->mem_freq = 1600;
  3949. break;
  3950. }
  3951. DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
  3952. dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
  3953. dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
  3954. DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
  3955. intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
  3956. dev_priv->rps.max_freq);
  3957. dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
  3958. DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
  3959. intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
  3960. dev_priv->rps.efficient_freq);
  3961. dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
  3962. DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
  3963. intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
  3964. dev_priv->rps.rp1_freq);
  3965. dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
  3966. DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
  3967. intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
  3968. dev_priv->rps.min_freq);
  3969. WARN_ONCE((dev_priv->rps.max_freq |
  3970. dev_priv->rps.efficient_freq |
  3971. dev_priv->rps.rp1_freq |
  3972. dev_priv->rps.min_freq) & 1,
  3973. "Odd GPU freq values\n");
  3974. /* Preserve min/max settings in case of re-init */
  3975. if (dev_priv->rps.max_freq_softlimit == 0)
  3976. dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
  3977. if (dev_priv->rps.min_freq_softlimit == 0)
  3978. dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
  3979. mutex_unlock(&dev_priv->rps.hw_lock);
  3980. }
  3981. static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
  3982. {
  3983. valleyview_cleanup_pctx(dev);
  3984. }
  3985. static void cherryview_enable_rps(struct drm_device *dev)
  3986. {
  3987. struct drm_i915_private *dev_priv = dev->dev_private;
  3988. struct intel_engine_cs *ring;
  3989. u32 gtfifodbg, val, rc6_mode = 0, pcbr;
  3990. int i;
  3991. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  3992. gtfifodbg = I915_READ(GTFIFODBG);
  3993. if (gtfifodbg) {
  3994. DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
  3995. gtfifodbg);
  3996. I915_WRITE(GTFIFODBG, gtfifodbg);
  3997. }
  3998. cherryview_check_pctx(dev_priv);
  3999. /* 1a & 1b: Get forcewake during program sequence. Although the driver
  4000. * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
  4001. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  4002. /* Disable RC states. */
  4003. I915_WRITE(GEN6_RC_CONTROL, 0);
  4004. /* 2a: Program RC6 thresholds.*/
  4005. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
  4006. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
  4007. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
  4008. for_each_ring(ring, dev_priv, i)
  4009. I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
  4010. I915_WRITE(GEN6_RC_SLEEP, 0);
  4011. /* TO threshold set to 1750 us ( 0x557 * 1.28 us) */
  4012. I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
  4013. /* allows RC6 residency counter to work */
  4014. I915_WRITE(VLV_COUNTER_CONTROL,
  4015. _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
  4016. VLV_MEDIA_RC6_COUNT_EN |
  4017. VLV_RENDER_RC6_COUNT_EN));
  4018. /* For now we assume BIOS is allocating and populating the PCBR */
  4019. pcbr = I915_READ(VLV_PCBR);
  4020. /* 3: Enable RC6 */
  4021. if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
  4022. (pcbr >> VLV_PCBR_ADDR_SHIFT))
  4023. rc6_mode = GEN7_RC_CTL_TO_MODE;
  4024. I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
  4025. /* 4 Program defaults and thresholds for RPS*/
  4026. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
  4027. I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
  4028. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
  4029. I915_WRITE(GEN6_RP_UP_EI, 66000);
  4030. I915_WRITE(GEN6_RP_DOWN_EI, 350000);
  4031. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  4032. /* 5: Enable RPS */
  4033. I915_WRITE(GEN6_RP_CONTROL,
  4034. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  4035. GEN6_RP_MEDIA_IS_GFX |
  4036. GEN6_RP_ENABLE |
  4037. GEN6_RP_UP_BUSY_AVG |
  4038. GEN6_RP_DOWN_IDLE_AVG);
  4039. val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
  4040. /* RPS code assumes GPLL is used */
  4041. WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
  4042. DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & GPLLENABLE ? "yes" : "no");
  4043. DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
  4044. dev_priv->rps.cur_freq = (val >> 8) & 0xff;
  4045. DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
  4046. intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
  4047. dev_priv->rps.cur_freq);
  4048. DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
  4049. intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
  4050. dev_priv->rps.efficient_freq);
  4051. valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
  4052. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  4053. }
  4054. static void valleyview_enable_rps(struct drm_device *dev)
  4055. {
  4056. struct drm_i915_private *dev_priv = dev->dev_private;
  4057. struct intel_engine_cs *ring;
  4058. u32 gtfifodbg, val, rc6_mode = 0;
  4059. int i;
  4060. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  4061. valleyview_check_pctx(dev_priv);
  4062. if ((gtfifodbg = I915_READ(GTFIFODBG))) {
  4063. DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
  4064. gtfifodbg);
  4065. I915_WRITE(GTFIFODBG, gtfifodbg);
  4066. }
  4067. /* If VLV, Forcewake all wells, else re-direct to regular path */
  4068. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  4069. /* Disable RC states. */
  4070. I915_WRITE(GEN6_RC_CONTROL, 0);
  4071. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
  4072. I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
  4073. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
  4074. I915_WRITE(GEN6_RP_UP_EI, 66000);
  4075. I915_WRITE(GEN6_RP_DOWN_EI, 350000);
  4076. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  4077. I915_WRITE(GEN6_RP_CONTROL,
  4078. GEN6_RP_MEDIA_TURBO |
  4079. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  4080. GEN6_RP_MEDIA_IS_GFX |
  4081. GEN6_RP_ENABLE |
  4082. GEN6_RP_UP_BUSY_AVG |
  4083. GEN6_RP_DOWN_IDLE_CONT);
  4084. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
  4085. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
  4086. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
  4087. for_each_ring(ring, dev_priv, i)
  4088. I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
  4089. I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
  4090. /* allows RC6 residency counter to work */
  4091. I915_WRITE(VLV_COUNTER_CONTROL,
  4092. _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
  4093. VLV_RENDER_RC0_COUNT_EN |
  4094. VLV_MEDIA_RC6_COUNT_EN |
  4095. VLV_RENDER_RC6_COUNT_EN));
  4096. if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
  4097. rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
  4098. intel_print_rc6_info(dev, rc6_mode);
  4099. I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
  4100. val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
  4101. /* RPS code assumes GPLL is used */
  4102. WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
  4103. DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & GPLLENABLE ? "yes" : "no");
  4104. DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
  4105. dev_priv->rps.cur_freq = (val >> 8) & 0xff;
  4106. DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
  4107. intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
  4108. dev_priv->rps.cur_freq);
  4109. DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
  4110. intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
  4111. dev_priv->rps.efficient_freq);
  4112. valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
  4113. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  4114. }
  4115. static unsigned long intel_pxfreq(u32 vidfreq)
  4116. {
  4117. unsigned long freq;
  4118. int div = (vidfreq & 0x3f0000) >> 16;
  4119. int post = (vidfreq & 0x3000) >> 12;
  4120. int pre = (vidfreq & 0x7);
  4121. if (!pre)
  4122. return 0;
  4123. freq = ((div * 133333) / ((1<<post) * pre));
  4124. return freq;
  4125. }
  4126. static const struct cparams {
  4127. u16 i;
  4128. u16 t;
  4129. u16 m;
  4130. u16 c;
  4131. } cparams[] = {
  4132. { 1, 1333, 301, 28664 },
  4133. { 1, 1066, 294, 24460 },
  4134. { 1, 800, 294, 25192 },
  4135. { 0, 1333, 276, 27605 },
  4136. { 0, 1066, 276, 27605 },
  4137. { 0, 800, 231, 23784 },
  4138. };
  4139. static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
  4140. {
  4141. u64 total_count, diff, ret;
  4142. u32 count1, count2, count3, m = 0, c = 0;
  4143. unsigned long now = jiffies_to_msecs(jiffies), diff1;
  4144. int i;
  4145. assert_spin_locked(&mchdev_lock);
  4146. diff1 = now - dev_priv->ips.last_time1;
  4147. /* Prevent division-by-zero if we are asking too fast.
  4148. * Also, we don't get interesting results if we are polling
  4149. * faster than once in 10ms, so just return the saved value
  4150. * in such cases.
  4151. */
  4152. if (diff1 <= 10)
  4153. return dev_priv->ips.chipset_power;
  4154. count1 = I915_READ(DMIEC);
  4155. count2 = I915_READ(DDREC);
  4156. count3 = I915_READ(CSIEC);
  4157. total_count = count1 + count2 + count3;
  4158. /* FIXME: handle per-counter overflow */
  4159. if (total_count < dev_priv->ips.last_count1) {
  4160. diff = ~0UL - dev_priv->ips.last_count1;
  4161. diff += total_count;
  4162. } else {
  4163. diff = total_count - dev_priv->ips.last_count1;
  4164. }
  4165. for (i = 0; i < ARRAY_SIZE(cparams); i++) {
  4166. if (cparams[i].i == dev_priv->ips.c_m &&
  4167. cparams[i].t == dev_priv->ips.r_t) {
  4168. m = cparams[i].m;
  4169. c = cparams[i].c;
  4170. break;
  4171. }
  4172. }
  4173. diff = div_u64(diff, diff1);
  4174. ret = ((m * diff) + c);
  4175. ret = div_u64(ret, 10);
  4176. dev_priv->ips.last_count1 = total_count;
  4177. dev_priv->ips.last_time1 = now;
  4178. dev_priv->ips.chipset_power = ret;
  4179. return ret;
  4180. }
  4181. unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
  4182. {
  4183. struct drm_device *dev = dev_priv->dev;
  4184. unsigned long val;
  4185. if (INTEL_INFO(dev)->gen != 5)
  4186. return 0;
  4187. spin_lock_irq(&mchdev_lock);
  4188. val = __i915_chipset_val(dev_priv);
  4189. spin_unlock_irq(&mchdev_lock);
  4190. return val;
  4191. }
  4192. unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
  4193. {
  4194. unsigned long m, x, b;
  4195. u32 tsfs;
  4196. tsfs = I915_READ(TSFS);
  4197. m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
  4198. x = I915_READ8(TR1);
  4199. b = tsfs & TSFS_INTR_MASK;
  4200. return ((m * x) / 127) - b;
  4201. }
  4202. static int _pxvid_to_vd(u8 pxvid)
  4203. {
  4204. if (pxvid == 0)
  4205. return 0;
  4206. if (pxvid >= 8 && pxvid < 31)
  4207. pxvid = 31;
  4208. return (pxvid + 2) * 125;
  4209. }
  4210. static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
  4211. {
  4212. struct drm_device *dev = dev_priv->dev;
  4213. const int vd = _pxvid_to_vd(pxvid);
  4214. const int vm = vd - 1125;
  4215. if (INTEL_INFO(dev)->is_mobile)
  4216. return vm > 0 ? vm : 0;
  4217. return vd;
  4218. }
  4219. static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
  4220. {
  4221. u64 now, diff, diffms;
  4222. u32 count;
  4223. assert_spin_locked(&mchdev_lock);
  4224. now = ktime_get_raw_ns();
  4225. diffms = now - dev_priv->ips.last_time2;
  4226. do_div(diffms, NSEC_PER_MSEC);
  4227. /* Don't divide by 0 */
  4228. if (!diffms)
  4229. return;
  4230. count = I915_READ(GFXEC);
  4231. if (count < dev_priv->ips.last_count2) {
  4232. diff = ~0UL - dev_priv->ips.last_count2;
  4233. diff += count;
  4234. } else {
  4235. diff = count - dev_priv->ips.last_count2;
  4236. }
  4237. dev_priv->ips.last_count2 = count;
  4238. dev_priv->ips.last_time2 = now;
  4239. /* More magic constants... */
  4240. diff = diff * 1181;
  4241. diff = div_u64(diff, diffms * 10);
  4242. dev_priv->ips.gfx_power = diff;
  4243. }
  4244. void i915_update_gfx_val(struct drm_i915_private *dev_priv)
  4245. {
  4246. struct drm_device *dev = dev_priv->dev;
  4247. if (INTEL_INFO(dev)->gen != 5)
  4248. return;
  4249. spin_lock_irq(&mchdev_lock);
  4250. __i915_update_gfx_val(dev_priv);
  4251. spin_unlock_irq(&mchdev_lock);
  4252. }
  4253. static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
  4254. {
  4255. unsigned long t, corr, state1, corr2, state2;
  4256. u32 pxvid, ext_v;
  4257. assert_spin_locked(&mchdev_lock);
  4258. pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_freq * 4));
  4259. pxvid = (pxvid >> 24) & 0x7f;
  4260. ext_v = pvid_to_extvid(dev_priv, pxvid);
  4261. state1 = ext_v;
  4262. t = i915_mch_val(dev_priv);
  4263. /* Revel in the empirically derived constants */
  4264. /* Correction factor in 1/100000 units */
  4265. if (t > 80)
  4266. corr = ((t * 2349) + 135940);
  4267. else if (t >= 50)
  4268. corr = ((t * 964) + 29317);
  4269. else /* < 50 */
  4270. corr = ((t * 301) + 1004);
  4271. corr = corr * ((150142 * state1) / 10000 - 78642);
  4272. corr /= 100000;
  4273. corr2 = (corr * dev_priv->ips.corr);
  4274. state2 = (corr2 * state1) / 10000;
  4275. state2 /= 100; /* convert to mW */
  4276. __i915_update_gfx_val(dev_priv);
  4277. return dev_priv->ips.gfx_power + state2;
  4278. }
  4279. unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
  4280. {
  4281. struct drm_device *dev = dev_priv->dev;
  4282. unsigned long val;
  4283. if (INTEL_INFO(dev)->gen != 5)
  4284. return 0;
  4285. spin_lock_irq(&mchdev_lock);
  4286. val = __i915_gfx_val(dev_priv);
  4287. spin_unlock_irq(&mchdev_lock);
  4288. return val;
  4289. }
  4290. /**
  4291. * i915_read_mch_val - return value for IPS use
  4292. *
  4293. * Calculate and return a value for the IPS driver to use when deciding whether
  4294. * we have thermal and power headroom to increase CPU or GPU power budget.
  4295. */
  4296. unsigned long i915_read_mch_val(void)
  4297. {
  4298. struct drm_i915_private *dev_priv;
  4299. unsigned long chipset_val, graphics_val, ret = 0;
  4300. spin_lock_irq(&mchdev_lock);
  4301. if (!i915_mch_dev)
  4302. goto out_unlock;
  4303. dev_priv = i915_mch_dev;
  4304. chipset_val = __i915_chipset_val(dev_priv);
  4305. graphics_val = __i915_gfx_val(dev_priv);
  4306. ret = chipset_val + graphics_val;
  4307. out_unlock:
  4308. spin_unlock_irq(&mchdev_lock);
  4309. return ret;
  4310. }
  4311. EXPORT_SYMBOL_GPL(i915_read_mch_val);
  4312. /**
  4313. * i915_gpu_raise - raise GPU frequency limit
  4314. *
  4315. * Raise the limit; IPS indicates we have thermal headroom.
  4316. */
  4317. bool i915_gpu_raise(void)
  4318. {
  4319. struct drm_i915_private *dev_priv;
  4320. bool ret = true;
  4321. spin_lock_irq(&mchdev_lock);
  4322. if (!i915_mch_dev) {
  4323. ret = false;
  4324. goto out_unlock;
  4325. }
  4326. dev_priv = i915_mch_dev;
  4327. if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
  4328. dev_priv->ips.max_delay--;
  4329. out_unlock:
  4330. spin_unlock_irq(&mchdev_lock);
  4331. return ret;
  4332. }
  4333. EXPORT_SYMBOL_GPL(i915_gpu_raise);
  4334. /**
  4335. * i915_gpu_lower - lower GPU frequency limit
  4336. *
  4337. * IPS indicates we're close to a thermal limit, so throttle back the GPU
  4338. * frequency maximum.
  4339. */
  4340. bool i915_gpu_lower(void)
  4341. {
  4342. struct drm_i915_private *dev_priv;
  4343. bool ret = true;
  4344. spin_lock_irq(&mchdev_lock);
  4345. if (!i915_mch_dev) {
  4346. ret = false;
  4347. goto out_unlock;
  4348. }
  4349. dev_priv = i915_mch_dev;
  4350. if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
  4351. dev_priv->ips.max_delay++;
  4352. out_unlock:
  4353. spin_unlock_irq(&mchdev_lock);
  4354. return ret;
  4355. }
  4356. EXPORT_SYMBOL_GPL(i915_gpu_lower);
  4357. /**
  4358. * i915_gpu_busy - indicate GPU business to IPS
  4359. *
  4360. * Tell the IPS driver whether or not the GPU is busy.
  4361. */
  4362. bool i915_gpu_busy(void)
  4363. {
  4364. struct drm_i915_private *dev_priv;
  4365. struct intel_engine_cs *ring;
  4366. bool ret = false;
  4367. int i;
  4368. spin_lock_irq(&mchdev_lock);
  4369. if (!i915_mch_dev)
  4370. goto out_unlock;
  4371. dev_priv = i915_mch_dev;
  4372. for_each_ring(ring, dev_priv, i)
  4373. ret |= !list_empty(&ring->request_list);
  4374. out_unlock:
  4375. spin_unlock_irq(&mchdev_lock);
  4376. return ret;
  4377. }
  4378. EXPORT_SYMBOL_GPL(i915_gpu_busy);
  4379. /**
  4380. * i915_gpu_turbo_disable - disable graphics turbo
  4381. *
  4382. * Disable graphics turbo by resetting the max frequency and setting the
  4383. * current frequency to the default.
  4384. */
  4385. bool i915_gpu_turbo_disable(void)
  4386. {
  4387. struct drm_i915_private *dev_priv;
  4388. bool ret = true;
  4389. spin_lock_irq(&mchdev_lock);
  4390. if (!i915_mch_dev) {
  4391. ret = false;
  4392. goto out_unlock;
  4393. }
  4394. dev_priv = i915_mch_dev;
  4395. dev_priv->ips.max_delay = dev_priv->ips.fstart;
  4396. if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
  4397. ret = false;
  4398. out_unlock:
  4399. spin_unlock_irq(&mchdev_lock);
  4400. return ret;
  4401. }
  4402. EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
  4403. /**
  4404. * Tells the intel_ips driver that the i915 driver is now loaded, if
  4405. * IPS got loaded first.
  4406. *
  4407. * This awkward dance is so that neither module has to depend on the
  4408. * other in order for IPS to do the appropriate communication of
  4409. * GPU turbo limits to i915.
  4410. */
  4411. static void
  4412. ips_ping_for_i915_load(void)
  4413. {
  4414. void (*link)(void);
  4415. link = symbol_get(ips_link_to_i915_driver);
  4416. if (link) {
  4417. link();
  4418. symbol_put(ips_link_to_i915_driver);
  4419. }
  4420. }
  4421. void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
  4422. {
  4423. /* We only register the i915 ips part with intel-ips once everything is
  4424. * set up, to avoid intel-ips sneaking in and reading bogus values. */
  4425. spin_lock_irq(&mchdev_lock);
  4426. i915_mch_dev = dev_priv;
  4427. spin_unlock_irq(&mchdev_lock);
  4428. ips_ping_for_i915_load();
  4429. }
  4430. void intel_gpu_ips_teardown(void)
  4431. {
  4432. spin_lock_irq(&mchdev_lock);
  4433. i915_mch_dev = NULL;
  4434. spin_unlock_irq(&mchdev_lock);
  4435. }
  4436. static void intel_init_emon(struct drm_device *dev)
  4437. {
  4438. struct drm_i915_private *dev_priv = dev->dev_private;
  4439. u32 lcfuse;
  4440. u8 pxw[16];
  4441. int i;
  4442. /* Disable to program */
  4443. I915_WRITE(ECR, 0);
  4444. POSTING_READ(ECR);
  4445. /* Program energy weights for various events */
  4446. I915_WRITE(SDEW, 0x15040d00);
  4447. I915_WRITE(CSIEW0, 0x007f0000);
  4448. I915_WRITE(CSIEW1, 0x1e220004);
  4449. I915_WRITE(CSIEW2, 0x04000004);
  4450. for (i = 0; i < 5; i++)
  4451. I915_WRITE(PEW + (i * 4), 0);
  4452. for (i = 0; i < 3; i++)
  4453. I915_WRITE(DEW + (i * 4), 0);
  4454. /* Program P-state weights to account for frequency power adjustment */
  4455. for (i = 0; i < 16; i++) {
  4456. u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
  4457. unsigned long freq = intel_pxfreq(pxvidfreq);
  4458. unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
  4459. PXVFREQ_PX_SHIFT;
  4460. unsigned long val;
  4461. val = vid * vid;
  4462. val *= (freq / 1000);
  4463. val *= 255;
  4464. val /= (127*127*900);
  4465. if (val > 0xff)
  4466. DRM_ERROR("bad pxval: %ld\n", val);
  4467. pxw[i] = val;
  4468. }
  4469. /* Render standby states get 0 weight */
  4470. pxw[14] = 0;
  4471. pxw[15] = 0;
  4472. for (i = 0; i < 4; i++) {
  4473. u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
  4474. (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
  4475. I915_WRITE(PXW + (i * 4), val);
  4476. }
  4477. /* Adjust magic regs to magic values (more experimental results) */
  4478. I915_WRITE(OGW0, 0);
  4479. I915_WRITE(OGW1, 0);
  4480. I915_WRITE(EG0, 0x00007f00);
  4481. I915_WRITE(EG1, 0x0000000e);
  4482. I915_WRITE(EG2, 0x000e0000);
  4483. I915_WRITE(EG3, 0x68000300);
  4484. I915_WRITE(EG4, 0x42000000);
  4485. I915_WRITE(EG5, 0x00140031);
  4486. I915_WRITE(EG6, 0);
  4487. I915_WRITE(EG7, 0);
  4488. for (i = 0; i < 8; i++)
  4489. I915_WRITE(PXWL + (i * 4), 0);
  4490. /* Enable PMON + select events */
  4491. I915_WRITE(ECR, 0x80000019);
  4492. lcfuse = I915_READ(LCFUSE02);
  4493. dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
  4494. }
  4495. void intel_init_gt_powersave(struct drm_device *dev)
  4496. {
  4497. i915.enable_rc6 = sanitize_rc6_option(dev, i915.enable_rc6);
  4498. if (IS_CHERRYVIEW(dev))
  4499. cherryview_init_gt_powersave(dev);
  4500. else if (IS_VALLEYVIEW(dev))
  4501. valleyview_init_gt_powersave(dev);
  4502. }
  4503. void intel_cleanup_gt_powersave(struct drm_device *dev)
  4504. {
  4505. if (IS_CHERRYVIEW(dev))
  4506. return;
  4507. else if (IS_VALLEYVIEW(dev))
  4508. valleyview_cleanup_gt_powersave(dev);
  4509. }
  4510. static void gen6_suspend_rps(struct drm_device *dev)
  4511. {
  4512. struct drm_i915_private *dev_priv = dev->dev_private;
  4513. flush_delayed_work(&dev_priv->rps.delayed_resume_work);
  4514. /*
  4515. * TODO: disable RPS interrupts on GEN9+ too once RPS support
  4516. * is added for it.
  4517. */
  4518. if (INTEL_INFO(dev)->gen < 9)
  4519. gen6_disable_rps_interrupts(dev);
  4520. }
  4521. /**
  4522. * intel_suspend_gt_powersave - suspend PM work and helper threads
  4523. * @dev: drm device
  4524. *
  4525. * We don't want to disable RC6 or other features here, we just want
  4526. * to make sure any work we've queued has finished and won't bother
  4527. * us while we're suspended.
  4528. */
  4529. void intel_suspend_gt_powersave(struct drm_device *dev)
  4530. {
  4531. struct drm_i915_private *dev_priv = dev->dev_private;
  4532. if (INTEL_INFO(dev)->gen < 6)
  4533. return;
  4534. gen6_suspend_rps(dev);
  4535. /* Force GPU to min freq during suspend */
  4536. gen6_rps_idle(dev_priv);
  4537. }
  4538. void intel_disable_gt_powersave(struct drm_device *dev)
  4539. {
  4540. struct drm_i915_private *dev_priv = dev->dev_private;
  4541. if (IS_IRONLAKE_M(dev)) {
  4542. ironlake_disable_drps(dev);
  4543. } else if (INTEL_INFO(dev)->gen >= 6) {
  4544. intel_suspend_gt_powersave(dev);
  4545. mutex_lock(&dev_priv->rps.hw_lock);
  4546. if (INTEL_INFO(dev)->gen >= 9)
  4547. gen9_disable_rps(dev);
  4548. else if (IS_CHERRYVIEW(dev))
  4549. cherryview_disable_rps(dev);
  4550. else if (IS_VALLEYVIEW(dev))
  4551. valleyview_disable_rps(dev);
  4552. else
  4553. gen6_disable_rps(dev);
  4554. dev_priv->rps.enabled = false;
  4555. mutex_unlock(&dev_priv->rps.hw_lock);
  4556. }
  4557. }
  4558. static void intel_gen6_powersave_work(struct work_struct *work)
  4559. {
  4560. struct drm_i915_private *dev_priv =
  4561. container_of(work, struct drm_i915_private,
  4562. rps.delayed_resume_work.work);
  4563. struct drm_device *dev = dev_priv->dev;
  4564. mutex_lock(&dev_priv->rps.hw_lock);
  4565. /*
  4566. * TODO: reset/enable RPS interrupts on GEN9+ too, once RPS support is
  4567. * added for it.
  4568. */
  4569. if (INTEL_INFO(dev)->gen < 9)
  4570. gen6_reset_rps_interrupts(dev);
  4571. if (IS_CHERRYVIEW(dev)) {
  4572. cherryview_enable_rps(dev);
  4573. } else if (IS_VALLEYVIEW(dev)) {
  4574. valleyview_enable_rps(dev);
  4575. } else if (INTEL_INFO(dev)->gen >= 9) {
  4576. gen9_enable_rc6(dev);
  4577. gen9_enable_rps(dev);
  4578. __gen6_update_ring_freq(dev);
  4579. } else if (IS_BROADWELL(dev)) {
  4580. gen8_enable_rps(dev);
  4581. __gen6_update_ring_freq(dev);
  4582. } else {
  4583. gen6_enable_rps(dev);
  4584. __gen6_update_ring_freq(dev);
  4585. }
  4586. dev_priv->rps.enabled = true;
  4587. if (INTEL_INFO(dev)->gen < 9)
  4588. gen6_enable_rps_interrupts(dev);
  4589. mutex_unlock(&dev_priv->rps.hw_lock);
  4590. intel_runtime_pm_put(dev_priv);
  4591. }
  4592. void intel_enable_gt_powersave(struct drm_device *dev)
  4593. {
  4594. struct drm_i915_private *dev_priv = dev->dev_private;
  4595. /* Powersaving is controlled by the host when inside a VM */
  4596. if (intel_vgpu_active(dev))
  4597. return;
  4598. if (IS_IRONLAKE_M(dev)) {
  4599. mutex_lock(&dev->struct_mutex);
  4600. ironlake_enable_drps(dev);
  4601. intel_init_emon(dev);
  4602. mutex_unlock(&dev->struct_mutex);
  4603. } else if (INTEL_INFO(dev)->gen >= 6) {
  4604. /*
  4605. * PCU communication is slow and this doesn't need to be
  4606. * done at any specific time, so do this out of our fast path
  4607. * to make resume and init faster.
  4608. *
  4609. * We depend on the HW RC6 power context save/restore
  4610. * mechanism when entering D3 through runtime PM suspend. So
  4611. * disable RPM until RPS/RC6 is properly setup. We can only
  4612. * get here via the driver load/system resume/runtime resume
  4613. * paths, so the _noresume version is enough (and in case of
  4614. * runtime resume it's necessary).
  4615. */
  4616. if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
  4617. round_jiffies_up_relative(HZ)))
  4618. intel_runtime_pm_get_noresume(dev_priv);
  4619. }
  4620. }
  4621. void intel_reset_gt_powersave(struct drm_device *dev)
  4622. {
  4623. struct drm_i915_private *dev_priv = dev->dev_private;
  4624. if (INTEL_INFO(dev)->gen < 6)
  4625. return;
  4626. gen6_suspend_rps(dev);
  4627. dev_priv->rps.enabled = false;
  4628. }
  4629. static void ibx_init_clock_gating(struct drm_device *dev)
  4630. {
  4631. struct drm_i915_private *dev_priv = dev->dev_private;
  4632. /*
  4633. * On Ibex Peak and Cougar Point, we need to disable clock
  4634. * gating for the panel power sequencer or it will fail to
  4635. * start up when no ports are active.
  4636. */
  4637. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
  4638. }
  4639. static void g4x_disable_trickle_feed(struct drm_device *dev)
  4640. {
  4641. struct drm_i915_private *dev_priv = dev->dev_private;
  4642. int pipe;
  4643. for_each_pipe(dev_priv, pipe) {
  4644. I915_WRITE(DSPCNTR(pipe),
  4645. I915_READ(DSPCNTR(pipe)) |
  4646. DISPPLANE_TRICKLE_FEED_DISABLE);
  4647. intel_flush_primary_plane(dev_priv, pipe);
  4648. }
  4649. }
  4650. static void ilk_init_lp_watermarks(struct drm_device *dev)
  4651. {
  4652. struct drm_i915_private *dev_priv = dev->dev_private;
  4653. I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
  4654. I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
  4655. I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
  4656. /*
  4657. * Don't touch WM1S_LP_EN here.
  4658. * Doing so could cause underruns.
  4659. */
  4660. }
  4661. static void ironlake_init_clock_gating(struct drm_device *dev)
  4662. {
  4663. struct drm_i915_private *dev_priv = dev->dev_private;
  4664. uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
  4665. /*
  4666. * Required for FBC
  4667. * WaFbcDisableDpfcClockGating:ilk
  4668. */
  4669. dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
  4670. ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
  4671. ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
  4672. I915_WRITE(PCH_3DCGDIS0,
  4673. MARIUNIT_CLOCK_GATE_DISABLE |
  4674. SVSMUNIT_CLOCK_GATE_DISABLE);
  4675. I915_WRITE(PCH_3DCGDIS1,
  4676. VFMUNIT_CLOCK_GATE_DISABLE);
  4677. /*
  4678. * According to the spec the following bits should be set in
  4679. * order to enable memory self-refresh
  4680. * The bit 22/21 of 0x42004
  4681. * The bit 5 of 0x42020
  4682. * The bit 15 of 0x45000
  4683. */
  4684. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  4685. (I915_READ(ILK_DISPLAY_CHICKEN2) |
  4686. ILK_DPARB_GATE | ILK_VSDPFD_FULL));
  4687. dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
  4688. I915_WRITE(DISP_ARB_CTL,
  4689. (I915_READ(DISP_ARB_CTL) |
  4690. DISP_FBC_WM_DIS));
  4691. ilk_init_lp_watermarks(dev);
  4692. /*
  4693. * Based on the document from hardware guys the following bits
  4694. * should be set unconditionally in order to enable FBC.
  4695. * The bit 22 of 0x42000
  4696. * The bit 22 of 0x42004
  4697. * The bit 7,8,9 of 0x42020.
  4698. */
  4699. if (IS_IRONLAKE_M(dev)) {
  4700. /* WaFbcAsynchFlipDisableFbcQueue:ilk */
  4701. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  4702. I915_READ(ILK_DISPLAY_CHICKEN1) |
  4703. ILK_FBCQ_DIS);
  4704. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  4705. I915_READ(ILK_DISPLAY_CHICKEN2) |
  4706. ILK_DPARB_GATE);
  4707. }
  4708. I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
  4709. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  4710. I915_READ(ILK_DISPLAY_CHICKEN2) |
  4711. ILK_ELPIN_409_SELECT);
  4712. I915_WRITE(_3D_CHICKEN2,
  4713. _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
  4714. _3D_CHICKEN2_WM_READ_PIPELINED);
  4715. /* WaDisableRenderCachePipelinedFlush:ilk */
  4716. I915_WRITE(CACHE_MODE_0,
  4717. _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
  4718. /* WaDisable_RenderCache_OperationalFlush:ilk */
  4719. I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  4720. g4x_disable_trickle_feed(dev);
  4721. ibx_init_clock_gating(dev);
  4722. }
  4723. static void cpt_init_clock_gating(struct drm_device *dev)
  4724. {
  4725. struct drm_i915_private *dev_priv = dev->dev_private;
  4726. int pipe;
  4727. uint32_t val;
  4728. /*
  4729. * On Ibex Peak and Cougar Point, we need to disable clock
  4730. * gating for the panel power sequencer or it will fail to
  4731. * start up when no ports are active.
  4732. */
  4733. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
  4734. PCH_DPLUNIT_CLOCK_GATE_DISABLE |
  4735. PCH_CPUNIT_CLOCK_GATE_DISABLE);
  4736. I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
  4737. DPLS_EDP_PPS_FIX_DIS);
  4738. /* The below fixes the weird display corruption, a few pixels shifted
  4739. * downward, on (only) LVDS of some HP laptops with IVY.
  4740. */
  4741. for_each_pipe(dev_priv, pipe) {
  4742. val = I915_READ(TRANS_CHICKEN2(pipe));
  4743. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  4744. val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
  4745. if (dev_priv->vbt.fdi_rx_polarity_inverted)
  4746. val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
  4747. val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
  4748. val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
  4749. val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
  4750. I915_WRITE(TRANS_CHICKEN2(pipe), val);
  4751. }
  4752. /* WADP0ClockGatingDisable */
  4753. for_each_pipe(dev_priv, pipe) {
  4754. I915_WRITE(TRANS_CHICKEN1(pipe),
  4755. TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
  4756. }
  4757. }
  4758. static void gen6_check_mch_setup(struct drm_device *dev)
  4759. {
  4760. struct drm_i915_private *dev_priv = dev->dev_private;
  4761. uint32_t tmp;
  4762. tmp = I915_READ(MCH_SSKPD);
  4763. if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
  4764. DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
  4765. tmp);
  4766. }
  4767. static void gen6_init_clock_gating(struct drm_device *dev)
  4768. {
  4769. struct drm_i915_private *dev_priv = dev->dev_private;
  4770. uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
  4771. I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
  4772. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  4773. I915_READ(ILK_DISPLAY_CHICKEN2) |
  4774. ILK_ELPIN_409_SELECT);
  4775. /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
  4776. I915_WRITE(_3D_CHICKEN,
  4777. _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
  4778. /* WaDisable_RenderCache_OperationalFlush:snb */
  4779. I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  4780. /*
  4781. * BSpec recoomends 8x4 when MSAA is used,
  4782. * however in practice 16x4 seems fastest.
  4783. *
  4784. * Note that PS/WM thread counts depend on the WIZ hashing
  4785. * disable bit, which we don't touch here, but it's good
  4786. * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
  4787. */
  4788. I915_WRITE(GEN6_GT_MODE,
  4789. _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
  4790. ilk_init_lp_watermarks(dev);
  4791. I915_WRITE(CACHE_MODE_0,
  4792. _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
  4793. I915_WRITE(GEN6_UCGCTL1,
  4794. I915_READ(GEN6_UCGCTL1) |
  4795. GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
  4796. GEN6_CSUNIT_CLOCK_GATE_DISABLE);
  4797. /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
  4798. * gating disable must be set. Failure to set it results in
  4799. * flickering pixels due to Z write ordering failures after
  4800. * some amount of runtime in the Mesa "fire" demo, and Unigine
  4801. * Sanctuary and Tropics, and apparently anything else with
  4802. * alpha test or pixel discard.
  4803. *
  4804. * According to the spec, bit 11 (RCCUNIT) must also be set,
  4805. * but we didn't debug actual testcases to find it out.
  4806. *
  4807. * WaDisableRCCUnitClockGating:snb
  4808. * WaDisableRCPBUnitClockGating:snb
  4809. */
  4810. I915_WRITE(GEN6_UCGCTL2,
  4811. GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
  4812. GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
  4813. /* WaStripsFansDisableFastClipPerformanceFix:snb */
  4814. I915_WRITE(_3D_CHICKEN3,
  4815. _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
  4816. /*
  4817. * Bspec says:
  4818. * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
  4819. * 3DSTATE_SF number of SF output attributes is more than 16."
  4820. */
  4821. I915_WRITE(_3D_CHICKEN3,
  4822. _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
  4823. /*
  4824. * According to the spec the following bits should be
  4825. * set in order to enable memory self-refresh and fbc:
  4826. * The bit21 and bit22 of 0x42000
  4827. * The bit21 and bit22 of 0x42004
  4828. * The bit5 and bit7 of 0x42020
  4829. * The bit14 of 0x70180
  4830. * The bit14 of 0x71180
  4831. *
  4832. * WaFbcAsynchFlipDisableFbcQueue:snb
  4833. */
  4834. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  4835. I915_READ(ILK_DISPLAY_CHICKEN1) |
  4836. ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
  4837. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  4838. I915_READ(ILK_DISPLAY_CHICKEN2) |
  4839. ILK_DPARB_GATE | ILK_VSDPFD_FULL);
  4840. I915_WRITE(ILK_DSPCLK_GATE_D,
  4841. I915_READ(ILK_DSPCLK_GATE_D) |
  4842. ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
  4843. ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
  4844. g4x_disable_trickle_feed(dev);
  4845. cpt_init_clock_gating(dev);
  4846. gen6_check_mch_setup(dev);
  4847. }
  4848. static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
  4849. {
  4850. uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
  4851. /*
  4852. * WaVSThreadDispatchOverride:ivb,vlv
  4853. *
  4854. * This actually overrides the dispatch
  4855. * mode for all thread types.
  4856. */
  4857. reg &= ~GEN7_FF_SCHED_MASK;
  4858. reg |= GEN7_FF_TS_SCHED_HW;
  4859. reg |= GEN7_FF_VS_SCHED_HW;
  4860. reg |= GEN7_FF_DS_SCHED_HW;
  4861. I915_WRITE(GEN7_FF_THREAD_MODE, reg);
  4862. }
  4863. static void lpt_init_clock_gating(struct drm_device *dev)
  4864. {
  4865. struct drm_i915_private *dev_priv = dev->dev_private;
  4866. /*
  4867. * TODO: this bit should only be enabled when really needed, then
  4868. * disabled when not needed anymore in order to save power.
  4869. */
  4870. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
  4871. I915_WRITE(SOUTH_DSPCLK_GATE_D,
  4872. I915_READ(SOUTH_DSPCLK_GATE_D) |
  4873. PCH_LP_PARTITION_LEVEL_DISABLE);
  4874. /* WADPOClockGatingDisable:hsw */
  4875. I915_WRITE(_TRANSA_CHICKEN1,
  4876. I915_READ(_TRANSA_CHICKEN1) |
  4877. TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
  4878. }
  4879. static void lpt_suspend_hw(struct drm_device *dev)
  4880. {
  4881. struct drm_i915_private *dev_priv = dev->dev_private;
  4882. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  4883. uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
  4884. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  4885. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  4886. }
  4887. }
  4888. static void broadwell_init_clock_gating(struct drm_device *dev)
  4889. {
  4890. struct drm_i915_private *dev_priv = dev->dev_private;
  4891. enum pipe pipe;
  4892. I915_WRITE(WM3_LP_ILK, 0);
  4893. I915_WRITE(WM2_LP_ILK, 0);
  4894. I915_WRITE(WM1_LP_ILK, 0);
  4895. /* WaSwitchSolVfFArbitrationPriority:bdw */
  4896. I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
  4897. /* WaPsrDPAMaskVBlankInSRD:bdw */
  4898. I915_WRITE(CHICKEN_PAR1_1,
  4899. I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
  4900. /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
  4901. for_each_pipe(dev_priv, pipe) {
  4902. I915_WRITE(CHICKEN_PIPESL_1(pipe),
  4903. I915_READ(CHICKEN_PIPESL_1(pipe)) |
  4904. BDW_DPRS_MASK_VBLANK_SRD);
  4905. }
  4906. /* WaVSRefCountFullforceMissDisable:bdw */
  4907. /* WaDSRefCountFullforceMissDisable:bdw */
  4908. I915_WRITE(GEN7_FF_THREAD_MODE,
  4909. I915_READ(GEN7_FF_THREAD_MODE) &
  4910. ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
  4911. I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
  4912. _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
  4913. /* WaDisableSDEUnitClockGating:bdw */
  4914. I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
  4915. GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
  4916. lpt_init_clock_gating(dev);
  4917. }
  4918. static void haswell_init_clock_gating(struct drm_device *dev)
  4919. {
  4920. struct drm_i915_private *dev_priv = dev->dev_private;
  4921. ilk_init_lp_watermarks(dev);
  4922. /* L3 caching of data atomics doesn't work -- disable it. */
  4923. I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
  4924. I915_WRITE(HSW_ROW_CHICKEN3,
  4925. _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
  4926. /* This is required by WaCatErrorRejectionIssue:hsw */
  4927. I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
  4928. I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
  4929. GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
  4930. /* WaVSRefCountFullforceMissDisable:hsw */
  4931. I915_WRITE(GEN7_FF_THREAD_MODE,
  4932. I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
  4933. /* WaDisable_RenderCache_OperationalFlush:hsw */
  4934. I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  4935. /* enable HiZ Raw Stall Optimization */
  4936. I915_WRITE(CACHE_MODE_0_GEN7,
  4937. _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
  4938. /* WaDisable4x2SubspanOptimization:hsw */
  4939. I915_WRITE(CACHE_MODE_1,
  4940. _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
  4941. /*
  4942. * BSpec recommends 8x4 when MSAA is used,
  4943. * however in practice 16x4 seems fastest.
  4944. *
  4945. * Note that PS/WM thread counts depend on the WIZ hashing
  4946. * disable bit, which we don't touch here, but it's good
  4947. * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
  4948. */
  4949. I915_WRITE(GEN7_GT_MODE,
  4950. _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
  4951. /* WaSampleCChickenBitEnable:hsw */
  4952. I915_WRITE(HALF_SLICE_CHICKEN3,
  4953. _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
  4954. /* WaSwitchSolVfFArbitrationPriority:hsw */
  4955. I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
  4956. /* WaRsPkgCStateDisplayPMReq:hsw */
  4957. I915_WRITE(CHICKEN_PAR1_1,
  4958. I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
  4959. lpt_init_clock_gating(dev);
  4960. }
  4961. static void ivybridge_init_clock_gating(struct drm_device *dev)
  4962. {
  4963. struct drm_i915_private *dev_priv = dev->dev_private;
  4964. uint32_t snpcr;
  4965. ilk_init_lp_watermarks(dev);
  4966. I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
  4967. /* WaDisableEarlyCull:ivb */
  4968. I915_WRITE(_3D_CHICKEN3,
  4969. _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
  4970. /* WaDisableBackToBackFlipFix:ivb */
  4971. I915_WRITE(IVB_CHICKEN3,
  4972. CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
  4973. CHICKEN3_DGMG_DONE_FIX_DISABLE);
  4974. /* WaDisablePSDDualDispatchEnable:ivb */
  4975. if (IS_IVB_GT1(dev))
  4976. I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
  4977. _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
  4978. /* WaDisable_RenderCache_OperationalFlush:ivb */
  4979. I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  4980. /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
  4981. I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
  4982. GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
  4983. /* WaApplyL3ControlAndL3ChickenMode:ivb */
  4984. I915_WRITE(GEN7_L3CNTLREG1,
  4985. GEN7_WA_FOR_GEN7_L3_CONTROL);
  4986. I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
  4987. GEN7_WA_L3_CHICKEN_MODE);
  4988. if (IS_IVB_GT1(dev))
  4989. I915_WRITE(GEN7_ROW_CHICKEN2,
  4990. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  4991. else {
  4992. /* must write both registers */
  4993. I915_WRITE(GEN7_ROW_CHICKEN2,
  4994. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  4995. I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
  4996. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  4997. }
  4998. /* WaForceL3Serialization:ivb */
  4999. I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
  5000. ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
  5001. /*
  5002. * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
  5003. * This implements the WaDisableRCZUnitClockGating:ivb workaround.
  5004. */
  5005. I915_WRITE(GEN6_UCGCTL2,
  5006. GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
  5007. /* This is required by WaCatErrorRejectionIssue:ivb */
  5008. I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
  5009. I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
  5010. GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
  5011. g4x_disable_trickle_feed(dev);
  5012. gen7_setup_fixed_func_scheduler(dev_priv);
  5013. if (0) { /* causes HiZ corruption on ivb:gt1 */
  5014. /* enable HiZ Raw Stall Optimization */
  5015. I915_WRITE(CACHE_MODE_0_GEN7,
  5016. _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
  5017. }
  5018. /* WaDisable4x2SubspanOptimization:ivb */
  5019. I915_WRITE(CACHE_MODE_1,
  5020. _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
  5021. /*
  5022. * BSpec recommends 8x4 when MSAA is used,
  5023. * however in practice 16x4 seems fastest.
  5024. *
  5025. * Note that PS/WM thread counts depend on the WIZ hashing
  5026. * disable bit, which we don't touch here, but it's good
  5027. * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
  5028. */
  5029. I915_WRITE(GEN7_GT_MODE,
  5030. _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
  5031. snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
  5032. snpcr &= ~GEN6_MBC_SNPCR_MASK;
  5033. snpcr |= GEN6_MBC_SNPCR_MED;
  5034. I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
  5035. if (!HAS_PCH_NOP(dev))
  5036. cpt_init_clock_gating(dev);
  5037. gen6_check_mch_setup(dev);
  5038. }
  5039. static void valleyview_init_clock_gating(struct drm_device *dev)
  5040. {
  5041. struct drm_i915_private *dev_priv = dev->dev_private;
  5042. I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
  5043. /* WaDisableEarlyCull:vlv */
  5044. I915_WRITE(_3D_CHICKEN3,
  5045. _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
  5046. /* WaDisableBackToBackFlipFix:vlv */
  5047. I915_WRITE(IVB_CHICKEN3,
  5048. CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
  5049. CHICKEN3_DGMG_DONE_FIX_DISABLE);
  5050. /* WaPsdDispatchEnable:vlv */
  5051. /* WaDisablePSDDualDispatchEnable:vlv */
  5052. I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
  5053. _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
  5054. GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
  5055. /* WaDisable_RenderCache_OperationalFlush:vlv */
  5056. I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  5057. /* WaForceL3Serialization:vlv */
  5058. I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
  5059. ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
  5060. /* WaDisableDopClockGating:vlv */
  5061. I915_WRITE(GEN7_ROW_CHICKEN2,
  5062. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  5063. /* This is required by WaCatErrorRejectionIssue:vlv */
  5064. I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
  5065. I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
  5066. GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
  5067. gen7_setup_fixed_func_scheduler(dev_priv);
  5068. /*
  5069. * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
  5070. * This implements the WaDisableRCZUnitClockGating:vlv workaround.
  5071. */
  5072. I915_WRITE(GEN6_UCGCTL2,
  5073. GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
  5074. /* WaDisableL3Bank2xClockGate:vlv
  5075. * Disabling L3 clock gating- MMIO 940c[25] = 1
  5076. * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
  5077. I915_WRITE(GEN7_UCGCTL4,
  5078. I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
  5079. I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
  5080. /*
  5081. * BSpec says this must be set, even though
  5082. * WaDisable4x2SubspanOptimization isn't listed for VLV.
  5083. */
  5084. I915_WRITE(CACHE_MODE_1,
  5085. _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
  5086. /*
  5087. * BSpec recommends 8x4 when MSAA is used,
  5088. * however in practice 16x4 seems fastest.
  5089. *
  5090. * Note that PS/WM thread counts depend on the WIZ hashing
  5091. * disable bit, which we don't touch here, but it's good
  5092. * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
  5093. */
  5094. I915_WRITE(GEN7_GT_MODE,
  5095. _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
  5096. /*
  5097. * WaIncreaseL3CreditsForVLVB0:vlv
  5098. * This is the hardware default actually.
  5099. */
  5100. I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
  5101. /*
  5102. * WaDisableVLVClockGating_VBIIssue:vlv
  5103. * Disable clock gating on th GCFG unit to prevent a delay
  5104. * in the reporting of vblank events.
  5105. */
  5106. I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
  5107. }
  5108. static void cherryview_init_clock_gating(struct drm_device *dev)
  5109. {
  5110. struct drm_i915_private *dev_priv = dev->dev_private;
  5111. I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
  5112. I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
  5113. /* WaVSRefCountFullforceMissDisable:chv */
  5114. /* WaDSRefCountFullforceMissDisable:chv */
  5115. I915_WRITE(GEN7_FF_THREAD_MODE,
  5116. I915_READ(GEN7_FF_THREAD_MODE) &
  5117. ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
  5118. /* WaDisableSemaphoreAndSyncFlipWait:chv */
  5119. I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
  5120. _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
  5121. /* WaDisableCSUnitClockGating:chv */
  5122. I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
  5123. GEN6_CSUNIT_CLOCK_GATE_DISABLE);
  5124. /* WaDisableSDEUnitClockGating:chv */
  5125. I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
  5126. GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
  5127. }
  5128. static void g4x_init_clock_gating(struct drm_device *dev)
  5129. {
  5130. struct drm_i915_private *dev_priv = dev->dev_private;
  5131. uint32_t dspclk_gate;
  5132. I915_WRITE(RENCLK_GATE_D1, 0);
  5133. I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
  5134. GS_UNIT_CLOCK_GATE_DISABLE |
  5135. CL_UNIT_CLOCK_GATE_DISABLE);
  5136. I915_WRITE(RAMCLK_GATE_D, 0);
  5137. dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
  5138. OVRUNIT_CLOCK_GATE_DISABLE |
  5139. OVCUNIT_CLOCK_GATE_DISABLE;
  5140. if (IS_GM45(dev))
  5141. dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
  5142. I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
  5143. /* WaDisableRenderCachePipelinedFlush */
  5144. I915_WRITE(CACHE_MODE_0,
  5145. _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
  5146. /* WaDisable_RenderCache_OperationalFlush:g4x */
  5147. I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  5148. g4x_disable_trickle_feed(dev);
  5149. }
  5150. static void crestline_init_clock_gating(struct drm_device *dev)
  5151. {
  5152. struct drm_i915_private *dev_priv = dev->dev_private;
  5153. I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
  5154. I915_WRITE(RENCLK_GATE_D2, 0);
  5155. I915_WRITE(DSPCLK_GATE_D, 0);
  5156. I915_WRITE(RAMCLK_GATE_D, 0);
  5157. I915_WRITE16(DEUC, 0);
  5158. I915_WRITE(MI_ARB_STATE,
  5159. _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
  5160. /* WaDisable_RenderCache_OperationalFlush:gen4 */
  5161. I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  5162. }
  5163. static void broadwater_init_clock_gating(struct drm_device *dev)
  5164. {
  5165. struct drm_i915_private *dev_priv = dev->dev_private;
  5166. I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
  5167. I965_RCC_CLOCK_GATE_DISABLE |
  5168. I965_RCPB_CLOCK_GATE_DISABLE |
  5169. I965_ISC_CLOCK_GATE_DISABLE |
  5170. I965_FBC_CLOCK_GATE_DISABLE);
  5171. I915_WRITE(RENCLK_GATE_D2, 0);
  5172. I915_WRITE(MI_ARB_STATE,
  5173. _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
  5174. /* WaDisable_RenderCache_OperationalFlush:gen4 */
  5175. I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  5176. }
  5177. static void gen3_init_clock_gating(struct drm_device *dev)
  5178. {
  5179. struct drm_i915_private *dev_priv = dev->dev_private;
  5180. u32 dstate = I915_READ(D_STATE);
  5181. dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
  5182. DSTATE_DOT_CLOCK_GATING;
  5183. I915_WRITE(D_STATE, dstate);
  5184. if (IS_PINEVIEW(dev))
  5185. I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
  5186. /* IIR "flip pending" means done if this bit is set */
  5187. I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
  5188. /* interrupts should cause a wake up from C3 */
  5189. I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
  5190. /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
  5191. I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
  5192. I915_WRITE(MI_ARB_STATE,
  5193. _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
  5194. }
  5195. static void i85x_init_clock_gating(struct drm_device *dev)
  5196. {
  5197. struct drm_i915_private *dev_priv = dev->dev_private;
  5198. I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
  5199. /* interrupts should cause a wake up from C3 */
  5200. I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
  5201. _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
  5202. I915_WRITE(MEM_MODE,
  5203. _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
  5204. }
  5205. static void i830_init_clock_gating(struct drm_device *dev)
  5206. {
  5207. struct drm_i915_private *dev_priv = dev->dev_private;
  5208. I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
  5209. I915_WRITE(MEM_MODE,
  5210. _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
  5211. _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
  5212. }
  5213. void intel_init_clock_gating(struct drm_device *dev)
  5214. {
  5215. struct drm_i915_private *dev_priv = dev->dev_private;
  5216. if (dev_priv->display.init_clock_gating)
  5217. dev_priv->display.init_clock_gating(dev);
  5218. }
  5219. void intel_suspend_hw(struct drm_device *dev)
  5220. {
  5221. if (HAS_PCH_LPT(dev))
  5222. lpt_suspend_hw(dev);
  5223. }
  5224. /* Set up chip specific power management-related functions */
  5225. void intel_init_pm(struct drm_device *dev)
  5226. {
  5227. struct drm_i915_private *dev_priv = dev->dev_private;
  5228. intel_fbc_init(dev_priv);
  5229. /* For cxsr */
  5230. if (IS_PINEVIEW(dev))
  5231. i915_pineview_get_mem_freq(dev);
  5232. else if (IS_GEN5(dev))
  5233. i915_ironlake_get_mem_freq(dev);
  5234. /* For FIFO watermark updates */
  5235. if (INTEL_INFO(dev)->gen >= 9) {
  5236. skl_setup_wm_latency(dev);
  5237. dev_priv->display.init_clock_gating = skl_init_clock_gating;
  5238. dev_priv->display.update_wm = skl_update_wm;
  5239. dev_priv->display.update_sprite_wm = skl_update_sprite_wm;
  5240. } else if (HAS_PCH_SPLIT(dev)) {
  5241. ilk_setup_wm_latency(dev);
  5242. if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
  5243. dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
  5244. (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
  5245. dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
  5246. dev_priv->display.update_wm = ilk_update_wm;
  5247. dev_priv->display.update_sprite_wm = ilk_update_sprite_wm;
  5248. } else {
  5249. DRM_DEBUG_KMS("Failed to read display plane latency. "
  5250. "Disable CxSR\n");
  5251. }
  5252. if (IS_GEN5(dev))
  5253. dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
  5254. else if (IS_GEN6(dev))
  5255. dev_priv->display.init_clock_gating = gen6_init_clock_gating;
  5256. else if (IS_IVYBRIDGE(dev))
  5257. dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
  5258. else if (IS_HASWELL(dev))
  5259. dev_priv->display.init_clock_gating = haswell_init_clock_gating;
  5260. else if (INTEL_INFO(dev)->gen == 8)
  5261. dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
  5262. } else if (IS_CHERRYVIEW(dev)) {
  5263. dev_priv->display.update_wm = cherryview_update_wm;
  5264. dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
  5265. dev_priv->display.init_clock_gating =
  5266. cherryview_init_clock_gating;
  5267. } else if (IS_VALLEYVIEW(dev)) {
  5268. dev_priv->display.update_wm = valleyview_update_wm;
  5269. dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
  5270. dev_priv->display.init_clock_gating =
  5271. valleyview_init_clock_gating;
  5272. } else if (IS_PINEVIEW(dev)) {
  5273. if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
  5274. dev_priv->is_ddr3,
  5275. dev_priv->fsb_freq,
  5276. dev_priv->mem_freq)) {
  5277. DRM_INFO("failed to find known CxSR latency "
  5278. "(found ddr%s fsb freq %d, mem freq %d), "
  5279. "disabling CxSR\n",
  5280. (dev_priv->is_ddr3 == 1) ? "3" : "2",
  5281. dev_priv->fsb_freq, dev_priv->mem_freq);
  5282. /* Disable CxSR and never update its watermark again */
  5283. intel_set_memory_cxsr(dev_priv, false);
  5284. dev_priv->display.update_wm = NULL;
  5285. } else
  5286. dev_priv->display.update_wm = pineview_update_wm;
  5287. dev_priv->display.init_clock_gating = gen3_init_clock_gating;
  5288. } else if (IS_G4X(dev)) {
  5289. dev_priv->display.update_wm = g4x_update_wm;
  5290. dev_priv->display.init_clock_gating = g4x_init_clock_gating;
  5291. } else if (IS_GEN4(dev)) {
  5292. dev_priv->display.update_wm = i965_update_wm;
  5293. if (IS_CRESTLINE(dev))
  5294. dev_priv->display.init_clock_gating = crestline_init_clock_gating;
  5295. else if (IS_BROADWATER(dev))
  5296. dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
  5297. } else if (IS_GEN3(dev)) {
  5298. dev_priv->display.update_wm = i9xx_update_wm;
  5299. dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
  5300. dev_priv->display.init_clock_gating = gen3_init_clock_gating;
  5301. } else if (IS_GEN2(dev)) {
  5302. if (INTEL_INFO(dev)->num_pipes == 1) {
  5303. dev_priv->display.update_wm = i845_update_wm;
  5304. dev_priv->display.get_fifo_size = i845_get_fifo_size;
  5305. } else {
  5306. dev_priv->display.update_wm = i9xx_update_wm;
  5307. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  5308. }
  5309. if (IS_I85X(dev) || IS_I865G(dev))
  5310. dev_priv->display.init_clock_gating = i85x_init_clock_gating;
  5311. else
  5312. dev_priv->display.init_clock_gating = i830_init_clock_gating;
  5313. } else {
  5314. DRM_ERROR("unexpected fall-through in intel_init_pm\n");
  5315. }
  5316. }
  5317. int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
  5318. {
  5319. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  5320. if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
  5321. DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
  5322. return -EAGAIN;
  5323. }
  5324. I915_WRITE(GEN6_PCODE_DATA, *val);
  5325. I915_WRITE(GEN6_PCODE_DATA1, 0);
  5326. I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
  5327. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  5328. 500)) {
  5329. DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
  5330. return -ETIMEDOUT;
  5331. }
  5332. *val = I915_READ(GEN6_PCODE_DATA);
  5333. I915_WRITE(GEN6_PCODE_DATA, 0);
  5334. return 0;
  5335. }
  5336. int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u32 mbox, u32 val)
  5337. {
  5338. WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
  5339. if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
  5340. DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
  5341. return -EAGAIN;
  5342. }
  5343. I915_WRITE(GEN6_PCODE_DATA, val);
  5344. I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
  5345. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  5346. 500)) {
  5347. DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
  5348. return -ETIMEDOUT;
  5349. }
  5350. I915_WRITE(GEN6_PCODE_DATA, 0);
  5351. return 0;
  5352. }
  5353. static int vlv_gpu_freq_div(unsigned int czclk_freq)
  5354. {
  5355. switch (czclk_freq) {
  5356. case 200:
  5357. return 10;
  5358. case 267:
  5359. return 12;
  5360. case 320:
  5361. case 333:
  5362. return 16;
  5363. case 400:
  5364. return 20;
  5365. default:
  5366. return -1;
  5367. }
  5368. }
  5369. static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
  5370. {
  5371. int div, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->mem_freq, 4);
  5372. div = vlv_gpu_freq_div(czclk_freq);
  5373. if (div < 0)
  5374. return div;
  5375. return DIV_ROUND_CLOSEST(czclk_freq * (val + 6 - 0xbd), div);
  5376. }
  5377. static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
  5378. {
  5379. int mul, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->mem_freq, 4);
  5380. mul = vlv_gpu_freq_div(czclk_freq);
  5381. if (mul < 0)
  5382. return mul;
  5383. return DIV_ROUND_CLOSEST(mul * val, czclk_freq) + 0xbd - 6;
  5384. }
  5385. static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
  5386. {
  5387. int div, czclk_freq = dev_priv->rps.cz_freq;
  5388. div = vlv_gpu_freq_div(czclk_freq) / 2;
  5389. if (div < 0)
  5390. return div;
  5391. return DIV_ROUND_CLOSEST(czclk_freq * val, 2 * div) / 2;
  5392. }
  5393. static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
  5394. {
  5395. int mul, czclk_freq = dev_priv->rps.cz_freq;
  5396. mul = vlv_gpu_freq_div(czclk_freq) / 2;
  5397. if (mul < 0)
  5398. return mul;
  5399. /* CHV needs even values */
  5400. return DIV_ROUND_CLOSEST(val * 2 * mul, czclk_freq) * 2;
  5401. }
  5402. int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
  5403. {
  5404. if (IS_CHERRYVIEW(dev_priv->dev))
  5405. return chv_gpu_freq(dev_priv, val);
  5406. else if (IS_VALLEYVIEW(dev_priv->dev))
  5407. return byt_gpu_freq(dev_priv, val);
  5408. else
  5409. return val * GT_FREQUENCY_MULTIPLIER;
  5410. }
  5411. int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
  5412. {
  5413. if (IS_CHERRYVIEW(dev_priv->dev))
  5414. return chv_freq_opcode(dev_priv, val);
  5415. else if (IS_VALLEYVIEW(dev_priv->dev))
  5416. return byt_freq_opcode(dev_priv, val);
  5417. else
  5418. return val / GT_FREQUENCY_MULTIPLIER;
  5419. }
  5420. void intel_pm_setup(struct drm_device *dev)
  5421. {
  5422. struct drm_i915_private *dev_priv = dev->dev_private;
  5423. mutex_init(&dev_priv->rps.hw_lock);
  5424. INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
  5425. intel_gen6_powersave_work);
  5426. dev_priv->pm.suspended = false;
  5427. }