intel_pm.c 206 KB

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