intel_pm.c 279 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853
  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 <linux/pm_runtime.h>
  29. #include <drm/drm_plane_helper.h>
  30. #include "i915_drv.h"
  31. #include "intel_drv.h"
  32. #include "../../../platform/x86/intel_ips.h"
  33. #include <linux/module.h>
  34. #include <drm/drm_atomic_helper.h>
  35. /**
  36. * DOC: RC6
  37. *
  38. * RC6 is a special power stage which allows the GPU to enter an very
  39. * low-voltage mode when idle, using down to 0V while at this stage. This
  40. * stage is entered automatically when the GPU is idle when RC6 support is
  41. * enabled, and as soon as new workload arises GPU wakes up automatically as well.
  42. *
  43. * There are different RC6 modes available in Intel GPU, which differentiate
  44. * among each other with the latency required to enter and leave RC6 and
  45. * voltage consumed by the GPU in different states.
  46. *
  47. * The combination of the following flags define which states GPU is allowed
  48. * to enter, while RC6 is the normal RC6 state, RC6p is the deep RC6, and
  49. * RC6pp is deepest RC6. Their support by hardware varies according to the
  50. * GPU, BIOS, chipset and platform. RC6 is usually the safest one and the one
  51. * which brings the most power savings; deeper states save more power, but
  52. * require higher latency to switch to and wake up.
  53. */
  54. static void gen9_init_clock_gating(struct drm_i915_private *dev_priv)
  55. {
  56. if (HAS_LLC(dev_priv)) {
  57. /*
  58. * WaCompressedResourceDisplayNewHashMode:skl,kbl
  59. * Display WA #0390: skl,kbl
  60. *
  61. * Must match Sampler, Pixel Back End, and Media. See
  62. * WaCompressedResourceSamplerPbeMediaNewHashMode.
  63. */
  64. I915_WRITE(CHICKEN_PAR1_1,
  65. I915_READ(CHICKEN_PAR1_1) |
  66. SKL_DE_COMPRESSED_HASH_MODE);
  67. }
  68. /* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,bxt,kbl,cfl */
  69. I915_WRITE(CHICKEN_PAR1_1,
  70. I915_READ(CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
  71. /* WaEnableChickenDCPR:skl,bxt,kbl,glk,cfl */
  72. I915_WRITE(GEN8_CHICKEN_DCPR_1,
  73. I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
  74. /* WaFbcTurnOffFbcWatermark:skl,bxt,kbl,cfl */
  75. /* WaFbcWakeMemOn:skl,bxt,kbl,glk,cfl */
  76. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  77. DISP_FBC_WM_DIS |
  78. DISP_FBC_MEMORY_WAKE);
  79. /* WaFbcHighMemBwCorruptionAvoidance:skl,bxt,kbl,cfl */
  80. I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
  81. ILK_DPFC_DISABLE_DUMMY0);
  82. if (IS_SKYLAKE(dev_priv)) {
  83. /* WaDisableDopClockGating */
  84. I915_WRITE(GEN7_MISCCPCTL, I915_READ(GEN7_MISCCPCTL)
  85. & ~GEN7_DOP_CLOCK_GATE_ENABLE);
  86. }
  87. }
  88. static void bxt_init_clock_gating(struct drm_i915_private *dev_priv)
  89. {
  90. gen9_init_clock_gating(dev_priv);
  91. /* WaDisableSDEUnitClockGating:bxt */
  92. I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
  93. GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
  94. /*
  95. * FIXME:
  96. * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
  97. */
  98. I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
  99. GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
  100. /*
  101. * Wa: Backlight PWM may stop in the asserted state, causing backlight
  102. * to stay fully on.
  103. */
  104. I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
  105. PWM1_GATING_DIS | PWM2_GATING_DIS);
  106. }
  107. static void glk_init_clock_gating(struct drm_i915_private *dev_priv)
  108. {
  109. gen9_init_clock_gating(dev_priv);
  110. /*
  111. * WaDisablePWMClockGating:glk
  112. * Backlight PWM may stop in the asserted state, causing backlight
  113. * to stay fully on.
  114. */
  115. I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
  116. PWM1_GATING_DIS | PWM2_GATING_DIS);
  117. /* WaDDIIOTimeout:glk */
  118. if (IS_GLK_REVID(dev_priv, 0, GLK_REVID_A1)) {
  119. u32 val = I915_READ(CHICKEN_MISC_2);
  120. val &= ~(GLK_CL0_PWR_DOWN |
  121. GLK_CL1_PWR_DOWN |
  122. GLK_CL2_PWR_DOWN);
  123. I915_WRITE(CHICKEN_MISC_2, val);
  124. }
  125. }
  126. static void i915_pineview_get_mem_freq(struct drm_i915_private *dev_priv)
  127. {
  128. u32 tmp;
  129. tmp = I915_READ(CLKCFG);
  130. switch (tmp & CLKCFG_FSB_MASK) {
  131. case CLKCFG_FSB_533:
  132. dev_priv->fsb_freq = 533; /* 133*4 */
  133. break;
  134. case CLKCFG_FSB_800:
  135. dev_priv->fsb_freq = 800; /* 200*4 */
  136. break;
  137. case CLKCFG_FSB_667:
  138. dev_priv->fsb_freq = 667; /* 167*4 */
  139. break;
  140. case CLKCFG_FSB_400:
  141. dev_priv->fsb_freq = 400; /* 100*4 */
  142. break;
  143. }
  144. switch (tmp & CLKCFG_MEM_MASK) {
  145. case CLKCFG_MEM_533:
  146. dev_priv->mem_freq = 533;
  147. break;
  148. case CLKCFG_MEM_667:
  149. dev_priv->mem_freq = 667;
  150. break;
  151. case CLKCFG_MEM_800:
  152. dev_priv->mem_freq = 800;
  153. break;
  154. }
  155. /* detect pineview DDR3 setting */
  156. tmp = I915_READ(CSHRDDR3CTL);
  157. dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
  158. }
  159. static void i915_ironlake_get_mem_freq(struct drm_i915_private *dev_priv)
  160. {
  161. u16 ddrpll, csipll;
  162. ddrpll = I915_READ16(DDRMPLL1);
  163. csipll = I915_READ16(CSIPLL0);
  164. switch (ddrpll & 0xff) {
  165. case 0xc:
  166. dev_priv->mem_freq = 800;
  167. break;
  168. case 0x10:
  169. dev_priv->mem_freq = 1066;
  170. break;
  171. case 0x14:
  172. dev_priv->mem_freq = 1333;
  173. break;
  174. case 0x18:
  175. dev_priv->mem_freq = 1600;
  176. break;
  177. default:
  178. DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
  179. ddrpll & 0xff);
  180. dev_priv->mem_freq = 0;
  181. break;
  182. }
  183. dev_priv->ips.r_t = dev_priv->mem_freq;
  184. switch (csipll & 0x3ff) {
  185. case 0x00c:
  186. dev_priv->fsb_freq = 3200;
  187. break;
  188. case 0x00e:
  189. dev_priv->fsb_freq = 3733;
  190. break;
  191. case 0x010:
  192. dev_priv->fsb_freq = 4266;
  193. break;
  194. case 0x012:
  195. dev_priv->fsb_freq = 4800;
  196. break;
  197. case 0x014:
  198. dev_priv->fsb_freq = 5333;
  199. break;
  200. case 0x016:
  201. dev_priv->fsb_freq = 5866;
  202. break;
  203. case 0x018:
  204. dev_priv->fsb_freq = 6400;
  205. break;
  206. default:
  207. DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
  208. csipll & 0x3ff);
  209. dev_priv->fsb_freq = 0;
  210. break;
  211. }
  212. if (dev_priv->fsb_freq == 3200) {
  213. dev_priv->ips.c_m = 0;
  214. } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
  215. dev_priv->ips.c_m = 1;
  216. } else {
  217. dev_priv->ips.c_m = 2;
  218. }
  219. }
  220. static const struct cxsr_latency cxsr_latency_table[] = {
  221. {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
  222. {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
  223. {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
  224. {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
  225. {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
  226. {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
  227. {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
  228. {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
  229. {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
  230. {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
  231. {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
  232. {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
  233. {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
  234. {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
  235. {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
  236. {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
  237. {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
  238. {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
  239. {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
  240. {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
  241. {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
  242. {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
  243. {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
  244. {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
  245. {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
  246. {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
  247. {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
  248. {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
  249. {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
  250. {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
  251. };
  252. static const struct cxsr_latency *intel_get_cxsr_latency(bool is_desktop,
  253. bool is_ddr3,
  254. int fsb,
  255. int mem)
  256. {
  257. const struct cxsr_latency *latency;
  258. int i;
  259. if (fsb == 0 || mem == 0)
  260. return NULL;
  261. for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
  262. latency = &cxsr_latency_table[i];
  263. if (is_desktop == latency->is_desktop &&
  264. is_ddr3 == latency->is_ddr3 &&
  265. fsb == latency->fsb_freq && mem == latency->mem_freq)
  266. return latency;
  267. }
  268. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  269. return NULL;
  270. }
  271. static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
  272. {
  273. u32 val;
  274. mutex_lock(&dev_priv->pcu_lock);
  275. val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
  276. if (enable)
  277. val &= ~FORCE_DDR_HIGH_FREQ;
  278. else
  279. val |= FORCE_DDR_HIGH_FREQ;
  280. val &= ~FORCE_DDR_LOW_FREQ;
  281. val |= FORCE_DDR_FREQ_REQ_ACK;
  282. vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
  283. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
  284. FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
  285. DRM_ERROR("timed out waiting for Punit DDR DVFS request\n");
  286. mutex_unlock(&dev_priv->pcu_lock);
  287. }
  288. static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
  289. {
  290. u32 val;
  291. mutex_lock(&dev_priv->pcu_lock);
  292. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  293. if (enable)
  294. val |= DSP_MAXFIFO_PM5_ENABLE;
  295. else
  296. val &= ~DSP_MAXFIFO_PM5_ENABLE;
  297. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  298. mutex_unlock(&dev_priv->pcu_lock);
  299. }
  300. #define FW_WM(value, plane) \
  301. (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
  302. static bool _intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
  303. {
  304. bool was_enabled;
  305. u32 val;
  306. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  307. was_enabled = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
  308. I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
  309. POSTING_READ(FW_BLC_SELF_VLV);
  310. } else if (IS_G4X(dev_priv) || IS_I965GM(dev_priv)) {
  311. was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
  312. I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
  313. POSTING_READ(FW_BLC_SELF);
  314. } else if (IS_PINEVIEW(dev_priv)) {
  315. val = I915_READ(DSPFW3);
  316. was_enabled = val & PINEVIEW_SELF_REFRESH_EN;
  317. if (enable)
  318. val |= PINEVIEW_SELF_REFRESH_EN;
  319. else
  320. val &= ~PINEVIEW_SELF_REFRESH_EN;
  321. I915_WRITE(DSPFW3, val);
  322. POSTING_READ(DSPFW3);
  323. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv)) {
  324. was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
  325. val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
  326. _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
  327. I915_WRITE(FW_BLC_SELF, val);
  328. POSTING_READ(FW_BLC_SELF);
  329. } else if (IS_I915GM(dev_priv)) {
  330. /*
  331. * FIXME can't find a bit like this for 915G, and
  332. * and yet it does have the related watermark in
  333. * FW_BLC_SELF. What's going on?
  334. */
  335. was_enabled = I915_READ(INSTPM) & INSTPM_SELF_EN;
  336. val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
  337. _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
  338. I915_WRITE(INSTPM, val);
  339. POSTING_READ(INSTPM);
  340. } else {
  341. return false;
  342. }
  343. trace_intel_memory_cxsr(dev_priv, was_enabled, enable);
  344. DRM_DEBUG_KMS("memory self-refresh is %s (was %s)\n",
  345. enableddisabled(enable),
  346. enableddisabled(was_enabled));
  347. return was_enabled;
  348. }
  349. /**
  350. * intel_set_memory_cxsr - Configure CxSR state
  351. * @dev_priv: i915 device
  352. * @enable: Allow vs. disallow CxSR
  353. *
  354. * Allow or disallow the system to enter a special CxSR
  355. * (C-state self refresh) state. What typically happens in CxSR mode
  356. * is that several display FIFOs may get combined into a single larger
  357. * FIFO for a particular plane (so called max FIFO mode) to allow the
  358. * system to defer memory fetches longer, and the memory will enter
  359. * self refresh.
  360. *
  361. * Note that enabling CxSR does not guarantee that the system enter
  362. * this special mode, nor does it guarantee that the system stays
  363. * in that mode once entered. So this just allows/disallows the system
  364. * to autonomously utilize the CxSR mode. Other factors such as core
  365. * C-states will affect when/if the system actually enters/exits the
  366. * CxSR mode.
  367. *
  368. * Note that on VLV/CHV this actually only controls the max FIFO mode,
  369. * and the system is free to enter/exit memory self refresh at any time
  370. * even when the use of CxSR has been disallowed.
  371. *
  372. * While the system is actually in the CxSR/max FIFO mode, some plane
  373. * control registers will not get latched on vblank. Thus in order to
  374. * guarantee the system will respond to changes in the plane registers
  375. * we must always disallow CxSR prior to making changes to those registers.
  376. * Unfortunately the system will re-evaluate the CxSR conditions at
  377. * frame start which happens after vblank start (which is when the plane
  378. * registers would get latched), so we can't proceed with the plane update
  379. * during the same frame where we disallowed CxSR.
  380. *
  381. * Certain platforms also have a deeper HPLL SR mode. Fortunately the
  382. * HPLL SR mode depends on CxSR itself, so we don't have to hand hold
  383. * the hardware w.r.t. HPLL SR when writing to plane registers.
  384. * Disallowing just CxSR is sufficient.
  385. */
  386. bool intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
  387. {
  388. bool ret;
  389. mutex_lock(&dev_priv->wm.wm_mutex);
  390. ret = _intel_set_memory_cxsr(dev_priv, enable);
  391. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  392. dev_priv->wm.vlv.cxsr = enable;
  393. else if (IS_G4X(dev_priv))
  394. dev_priv->wm.g4x.cxsr = enable;
  395. mutex_unlock(&dev_priv->wm.wm_mutex);
  396. return ret;
  397. }
  398. /*
  399. * Latency for FIFO fetches is dependent on several factors:
  400. * - memory configuration (speed, channels)
  401. * - chipset
  402. * - current MCH state
  403. * It can be fairly high in some situations, so here we assume a fairly
  404. * pessimal value. It's a tradeoff between extra memory fetches (if we
  405. * set this value too high, the FIFO will fetch frequently to stay full)
  406. * and power consumption (set it too low to save power and we might see
  407. * FIFO underruns and display "flicker").
  408. *
  409. * A value of 5us seems to be a good balance; safe for very low end
  410. * platforms but not overly aggressive on lower latency configs.
  411. */
  412. static const int pessimal_latency_ns = 5000;
  413. #define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
  414. ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
  415. static void vlv_get_fifo_size(struct intel_crtc_state *crtc_state)
  416. {
  417. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  418. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  419. struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
  420. enum pipe pipe = crtc->pipe;
  421. int sprite0_start, sprite1_start;
  422. switch (pipe) {
  423. uint32_t dsparb, dsparb2, dsparb3;
  424. case PIPE_A:
  425. dsparb = I915_READ(DSPARB);
  426. dsparb2 = I915_READ(DSPARB2);
  427. sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
  428. sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
  429. break;
  430. case PIPE_B:
  431. dsparb = I915_READ(DSPARB);
  432. dsparb2 = I915_READ(DSPARB2);
  433. sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
  434. sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
  435. break;
  436. case PIPE_C:
  437. dsparb2 = I915_READ(DSPARB2);
  438. dsparb3 = I915_READ(DSPARB3);
  439. sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
  440. sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
  441. break;
  442. default:
  443. MISSING_CASE(pipe);
  444. return;
  445. }
  446. fifo_state->plane[PLANE_PRIMARY] = sprite0_start;
  447. fifo_state->plane[PLANE_SPRITE0] = sprite1_start - sprite0_start;
  448. fifo_state->plane[PLANE_SPRITE1] = 511 - sprite1_start;
  449. fifo_state->plane[PLANE_CURSOR] = 63;
  450. }
  451. static int i9xx_get_fifo_size(struct drm_i915_private *dev_priv,
  452. enum i9xx_plane_id i9xx_plane)
  453. {
  454. uint32_t dsparb = I915_READ(DSPARB);
  455. int size;
  456. size = dsparb & 0x7f;
  457. if (i9xx_plane == PLANE_B)
  458. size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
  459. DRM_DEBUG_KMS("FIFO size - (0x%08x) %c: %d\n",
  460. dsparb, plane_name(i9xx_plane), size);
  461. return size;
  462. }
  463. static int i830_get_fifo_size(struct drm_i915_private *dev_priv,
  464. enum i9xx_plane_id i9xx_plane)
  465. {
  466. uint32_t dsparb = I915_READ(DSPARB);
  467. int size;
  468. size = dsparb & 0x1ff;
  469. if (i9xx_plane == PLANE_B)
  470. size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
  471. size >>= 1; /* Convert to cachelines */
  472. DRM_DEBUG_KMS("FIFO size - (0x%08x) %c: %d\n",
  473. dsparb, plane_name(i9xx_plane), size);
  474. return size;
  475. }
  476. static int i845_get_fifo_size(struct drm_i915_private *dev_priv,
  477. enum i9xx_plane_id i9xx_plane)
  478. {
  479. uint32_t dsparb = I915_READ(DSPARB);
  480. int size;
  481. size = dsparb & 0x7f;
  482. size >>= 2; /* Convert to cachelines */
  483. DRM_DEBUG_KMS("FIFO size - (0x%08x) %c: %d\n",
  484. dsparb, plane_name(i9xx_plane), size);
  485. return size;
  486. }
  487. /* Pineview has different values for various configs */
  488. static const struct intel_watermark_params pineview_display_wm = {
  489. .fifo_size = PINEVIEW_DISPLAY_FIFO,
  490. .max_wm = PINEVIEW_MAX_WM,
  491. .default_wm = PINEVIEW_DFT_WM,
  492. .guard_size = PINEVIEW_GUARD_WM,
  493. .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
  494. };
  495. static const struct intel_watermark_params pineview_display_hplloff_wm = {
  496. .fifo_size = PINEVIEW_DISPLAY_FIFO,
  497. .max_wm = PINEVIEW_MAX_WM,
  498. .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
  499. .guard_size = PINEVIEW_GUARD_WM,
  500. .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
  501. };
  502. static const struct intel_watermark_params pineview_cursor_wm = {
  503. .fifo_size = PINEVIEW_CURSOR_FIFO,
  504. .max_wm = PINEVIEW_CURSOR_MAX_WM,
  505. .default_wm = PINEVIEW_CURSOR_DFT_WM,
  506. .guard_size = PINEVIEW_CURSOR_GUARD_WM,
  507. .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
  508. };
  509. static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
  510. .fifo_size = PINEVIEW_CURSOR_FIFO,
  511. .max_wm = PINEVIEW_CURSOR_MAX_WM,
  512. .default_wm = PINEVIEW_CURSOR_DFT_WM,
  513. .guard_size = PINEVIEW_CURSOR_GUARD_WM,
  514. .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
  515. };
  516. static const struct intel_watermark_params i965_cursor_wm_info = {
  517. .fifo_size = I965_CURSOR_FIFO,
  518. .max_wm = I965_CURSOR_MAX_WM,
  519. .default_wm = I965_CURSOR_DFT_WM,
  520. .guard_size = 2,
  521. .cacheline_size = I915_FIFO_LINE_SIZE,
  522. };
  523. static const struct intel_watermark_params i945_wm_info = {
  524. .fifo_size = I945_FIFO_SIZE,
  525. .max_wm = I915_MAX_WM,
  526. .default_wm = 1,
  527. .guard_size = 2,
  528. .cacheline_size = I915_FIFO_LINE_SIZE,
  529. };
  530. static const struct intel_watermark_params i915_wm_info = {
  531. .fifo_size = I915_FIFO_SIZE,
  532. .max_wm = I915_MAX_WM,
  533. .default_wm = 1,
  534. .guard_size = 2,
  535. .cacheline_size = I915_FIFO_LINE_SIZE,
  536. };
  537. static const struct intel_watermark_params i830_a_wm_info = {
  538. .fifo_size = I855GM_FIFO_SIZE,
  539. .max_wm = I915_MAX_WM,
  540. .default_wm = 1,
  541. .guard_size = 2,
  542. .cacheline_size = I830_FIFO_LINE_SIZE,
  543. };
  544. static const struct intel_watermark_params i830_bc_wm_info = {
  545. .fifo_size = I855GM_FIFO_SIZE,
  546. .max_wm = I915_MAX_WM/2,
  547. .default_wm = 1,
  548. .guard_size = 2,
  549. .cacheline_size = I830_FIFO_LINE_SIZE,
  550. };
  551. static const struct intel_watermark_params i845_wm_info = {
  552. .fifo_size = I830_FIFO_SIZE,
  553. .max_wm = I915_MAX_WM,
  554. .default_wm = 1,
  555. .guard_size = 2,
  556. .cacheline_size = I830_FIFO_LINE_SIZE,
  557. };
  558. /**
  559. * intel_wm_method1 - Method 1 / "small buffer" watermark formula
  560. * @pixel_rate: Pipe pixel rate in kHz
  561. * @cpp: Plane bytes per pixel
  562. * @latency: Memory wakeup latency in 0.1us units
  563. *
  564. * Compute the watermark using the method 1 or "small buffer"
  565. * formula. The caller may additonally add extra cachelines
  566. * to account for TLB misses and clock crossings.
  567. *
  568. * This method is concerned with the short term drain rate
  569. * of the FIFO, ie. it does not account for blanking periods
  570. * which would effectively reduce the average drain rate across
  571. * a longer period. The name "small" refers to the fact the
  572. * FIFO is relatively small compared to the amount of data
  573. * fetched.
  574. *
  575. * The FIFO level vs. time graph might look something like:
  576. *
  577. * |\ |\
  578. * | \ | \
  579. * __---__---__ (- plane active, _ blanking)
  580. * -> time
  581. *
  582. * or perhaps like this:
  583. *
  584. * |\|\ |\|\
  585. * __----__----__ (- plane active, _ blanking)
  586. * -> time
  587. *
  588. * Returns:
  589. * The watermark in bytes
  590. */
  591. static unsigned int intel_wm_method1(unsigned int pixel_rate,
  592. unsigned int cpp,
  593. unsigned int latency)
  594. {
  595. uint64_t ret;
  596. ret = (uint64_t) pixel_rate * cpp * latency;
  597. ret = DIV_ROUND_UP_ULL(ret, 10000);
  598. return ret;
  599. }
  600. /**
  601. * intel_wm_method2 - Method 2 / "large buffer" watermark formula
  602. * @pixel_rate: Pipe pixel rate in kHz
  603. * @htotal: Pipe horizontal total
  604. * @width: Plane width in pixels
  605. * @cpp: Plane bytes per pixel
  606. * @latency: Memory wakeup latency in 0.1us units
  607. *
  608. * Compute the watermark using the method 2 or "large buffer"
  609. * formula. The caller may additonally add extra cachelines
  610. * to account for TLB misses and clock crossings.
  611. *
  612. * This method is concerned with the long term drain rate
  613. * of the FIFO, ie. it does account for blanking periods
  614. * which effectively reduce the average drain rate across
  615. * a longer period. The name "large" refers to the fact the
  616. * FIFO is relatively large compared to the amount of data
  617. * fetched.
  618. *
  619. * The FIFO level vs. time graph might look something like:
  620. *
  621. * |\___ |\___
  622. * | \___ | \___
  623. * | \ | \
  624. * __ --__--__--__--__--__--__ (- plane active, _ blanking)
  625. * -> time
  626. *
  627. * Returns:
  628. * The watermark in bytes
  629. */
  630. static unsigned int intel_wm_method2(unsigned int pixel_rate,
  631. unsigned int htotal,
  632. unsigned int width,
  633. unsigned int cpp,
  634. unsigned int latency)
  635. {
  636. unsigned int ret;
  637. /*
  638. * FIXME remove once all users are computing
  639. * watermarks in the correct place.
  640. */
  641. if (WARN_ON_ONCE(htotal == 0))
  642. htotal = 1;
  643. ret = (latency * pixel_rate) / (htotal * 10000);
  644. ret = (ret + 1) * width * cpp;
  645. return ret;
  646. }
  647. /**
  648. * intel_calculate_wm - calculate watermark level
  649. * @pixel_rate: pixel clock
  650. * @wm: chip FIFO params
  651. * @fifo_size: size of the FIFO buffer
  652. * @cpp: bytes per pixel
  653. * @latency_ns: memory latency for the platform
  654. *
  655. * Calculate the watermark level (the level at which the display plane will
  656. * start fetching from memory again). Each chip has a different display
  657. * FIFO size and allocation, so the caller needs to figure that out and pass
  658. * in the correct intel_watermark_params structure.
  659. *
  660. * As the pixel clock runs, the FIFO will be drained at a rate that depends
  661. * on the pixel size. When it reaches the watermark level, it'll start
  662. * fetching FIFO line sized based chunks from memory until the FIFO fills
  663. * past the watermark point. If the FIFO drains completely, a FIFO underrun
  664. * will occur, and a display engine hang could result.
  665. */
  666. static unsigned int intel_calculate_wm(int pixel_rate,
  667. const struct intel_watermark_params *wm,
  668. int fifo_size, int cpp,
  669. unsigned int latency_ns)
  670. {
  671. int entries, wm_size;
  672. /*
  673. * Note: we need to make sure we don't overflow for various clock &
  674. * latency values.
  675. * clocks go from a few thousand to several hundred thousand.
  676. * latency is usually a few thousand
  677. */
  678. entries = intel_wm_method1(pixel_rate, cpp,
  679. latency_ns / 100);
  680. entries = DIV_ROUND_UP(entries, wm->cacheline_size) +
  681. wm->guard_size;
  682. DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries);
  683. wm_size = fifo_size - entries;
  684. DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
  685. /* Don't promote wm_size to unsigned... */
  686. if (wm_size > wm->max_wm)
  687. wm_size = wm->max_wm;
  688. if (wm_size <= 0)
  689. wm_size = wm->default_wm;
  690. /*
  691. * Bspec seems to indicate that the value shouldn't be lower than
  692. * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
  693. * Lets go for 8 which is the burst size since certain platforms
  694. * already use a hardcoded 8 (which is what the spec says should be
  695. * done).
  696. */
  697. if (wm_size <= 8)
  698. wm_size = 8;
  699. return wm_size;
  700. }
  701. static bool is_disabling(int old, int new, int threshold)
  702. {
  703. return old >= threshold && new < threshold;
  704. }
  705. static bool is_enabling(int old, int new, int threshold)
  706. {
  707. return old < threshold && new >= threshold;
  708. }
  709. static int intel_wm_num_levels(struct drm_i915_private *dev_priv)
  710. {
  711. return dev_priv->wm.max_level + 1;
  712. }
  713. static bool intel_wm_plane_visible(const struct intel_crtc_state *crtc_state,
  714. const struct intel_plane_state *plane_state)
  715. {
  716. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  717. /* FIXME check the 'enable' instead */
  718. if (!crtc_state->base.active)
  719. return false;
  720. /*
  721. * Treat cursor with fb as always visible since cursor updates
  722. * can happen faster than the vrefresh rate, and the current
  723. * watermark code doesn't handle that correctly. Cursor updates
  724. * which set/clear the fb or change the cursor size are going
  725. * to get throttled by intel_legacy_cursor_update() to work
  726. * around this problem with the watermark code.
  727. */
  728. if (plane->id == PLANE_CURSOR)
  729. return plane_state->base.fb != NULL;
  730. else
  731. return plane_state->base.visible;
  732. }
  733. static struct intel_crtc *single_enabled_crtc(struct drm_i915_private *dev_priv)
  734. {
  735. struct intel_crtc *crtc, *enabled = NULL;
  736. for_each_intel_crtc(&dev_priv->drm, crtc) {
  737. if (intel_crtc_active(crtc)) {
  738. if (enabled)
  739. return NULL;
  740. enabled = crtc;
  741. }
  742. }
  743. return enabled;
  744. }
  745. static void pineview_update_wm(struct intel_crtc *unused_crtc)
  746. {
  747. struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
  748. struct intel_crtc *crtc;
  749. const struct cxsr_latency *latency;
  750. u32 reg;
  751. unsigned int wm;
  752. latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
  753. dev_priv->is_ddr3,
  754. dev_priv->fsb_freq,
  755. dev_priv->mem_freq);
  756. if (!latency) {
  757. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  758. intel_set_memory_cxsr(dev_priv, false);
  759. return;
  760. }
  761. crtc = single_enabled_crtc(dev_priv);
  762. if (crtc) {
  763. const struct drm_display_mode *adjusted_mode =
  764. &crtc->config->base.adjusted_mode;
  765. const struct drm_framebuffer *fb =
  766. crtc->base.primary->state->fb;
  767. int cpp = fb->format->cpp[0];
  768. int clock = adjusted_mode->crtc_clock;
  769. /* Display SR */
  770. wm = intel_calculate_wm(clock, &pineview_display_wm,
  771. pineview_display_wm.fifo_size,
  772. cpp, latency->display_sr);
  773. reg = I915_READ(DSPFW1);
  774. reg &= ~DSPFW_SR_MASK;
  775. reg |= FW_WM(wm, SR);
  776. I915_WRITE(DSPFW1, reg);
  777. DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
  778. /* cursor SR */
  779. wm = intel_calculate_wm(clock, &pineview_cursor_wm,
  780. pineview_display_wm.fifo_size,
  781. 4, latency->cursor_sr);
  782. reg = I915_READ(DSPFW3);
  783. reg &= ~DSPFW_CURSOR_SR_MASK;
  784. reg |= FW_WM(wm, CURSOR_SR);
  785. I915_WRITE(DSPFW3, reg);
  786. /* Display HPLL off SR */
  787. wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
  788. pineview_display_hplloff_wm.fifo_size,
  789. cpp, latency->display_hpll_disable);
  790. reg = I915_READ(DSPFW3);
  791. reg &= ~DSPFW_HPLL_SR_MASK;
  792. reg |= FW_WM(wm, HPLL_SR);
  793. I915_WRITE(DSPFW3, reg);
  794. /* cursor HPLL off SR */
  795. wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
  796. pineview_display_hplloff_wm.fifo_size,
  797. 4, latency->cursor_hpll_disable);
  798. reg = I915_READ(DSPFW3);
  799. reg &= ~DSPFW_HPLL_CURSOR_MASK;
  800. reg |= FW_WM(wm, HPLL_CURSOR);
  801. I915_WRITE(DSPFW3, reg);
  802. DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
  803. intel_set_memory_cxsr(dev_priv, true);
  804. } else {
  805. intel_set_memory_cxsr(dev_priv, false);
  806. }
  807. }
  808. /*
  809. * Documentation says:
  810. * "If the line size is small, the TLB fetches can get in the way of the
  811. * data fetches, causing some lag in the pixel data return which is not
  812. * accounted for in the above formulas. The following adjustment only
  813. * needs to be applied if eight whole lines fit in the buffer at once.
  814. * The WM is adjusted upwards by the difference between the FIFO size
  815. * and the size of 8 whole lines. This adjustment is always performed
  816. * in the actual pixel depth regardless of whether FBC is enabled or not."
  817. */
  818. static unsigned int g4x_tlb_miss_wa(int fifo_size, int width, int cpp)
  819. {
  820. int tlb_miss = fifo_size * 64 - width * cpp * 8;
  821. return max(0, tlb_miss);
  822. }
  823. static void g4x_write_wm_values(struct drm_i915_private *dev_priv,
  824. const struct g4x_wm_values *wm)
  825. {
  826. enum pipe pipe;
  827. for_each_pipe(dev_priv, pipe)
  828. trace_g4x_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
  829. I915_WRITE(DSPFW1,
  830. FW_WM(wm->sr.plane, SR) |
  831. FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
  832. FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
  833. FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
  834. I915_WRITE(DSPFW2,
  835. (wm->fbc_en ? DSPFW_FBC_SR_EN : 0) |
  836. FW_WM(wm->sr.fbc, FBC_SR) |
  837. FW_WM(wm->hpll.fbc, FBC_HPLL_SR) |
  838. FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEB) |
  839. FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
  840. FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
  841. I915_WRITE(DSPFW3,
  842. (wm->hpll_en ? DSPFW_HPLL_SR_EN : 0) |
  843. FW_WM(wm->sr.cursor, CURSOR_SR) |
  844. FW_WM(wm->hpll.cursor, HPLL_CURSOR) |
  845. FW_WM(wm->hpll.plane, HPLL_SR));
  846. POSTING_READ(DSPFW1);
  847. }
  848. #define FW_WM_VLV(value, plane) \
  849. (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
  850. static void vlv_write_wm_values(struct drm_i915_private *dev_priv,
  851. const struct vlv_wm_values *wm)
  852. {
  853. enum pipe pipe;
  854. for_each_pipe(dev_priv, pipe) {
  855. trace_vlv_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
  856. I915_WRITE(VLV_DDL(pipe),
  857. (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
  858. (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
  859. (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
  860. (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
  861. }
  862. /*
  863. * Zero the (unused) WM1 watermarks, and also clear all the
  864. * high order bits so that there are no out of bounds values
  865. * present in the registers during the reprogramming.
  866. */
  867. I915_WRITE(DSPHOWM, 0);
  868. I915_WRITE(DSPHOWM1, 0);
  869. I915_WRITE(DSPFW4, 0);
  870. I915_WRITE(DSPFW5, 0);
  871. I915_WRITE(DSPFW6, 0);
  872. I915_WRITE(DSPFW1,
  873. FW_WM(wm->sr.plane, SR) |
  874. FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
  875. FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
  876. FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
  877. I915_WRITE(DSPFW2,
  878. FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
  879. FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
  880. FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
  881. I915_WRITE(DSPFW3,
  882. FW_WM(wm->sr.cursor, CURSOR_SR));
  883. if (IS_CHERRYVIEW(dev_priv)) {
  884. I915_WRITE(DSPFW7_CHV,
  885. FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
  886. FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
  887. I915_WRITE(DSPFW8_CHV,
  888. FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
  889. FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
  890. I915_WRITE(DSPFW9_CHV,
  891. FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
  892. FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
  893. I915_WRITE(DSPHOWM,
  894. FW_WM(wm->sr.plane >> 9, SR_HI) |
  895. FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
  896. FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
  897. FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
  898. FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
  899. FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
  900. FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
  901. FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
  902. FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
  903. FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
  904. } else {
  905. I915_WRITE(DSPFW7,
  906. FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
  907. FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
  908. I915_WRITE(DSPHOWM,
  909. FW_WM(wm->sr.plane >> 9, SR_HI) |
  910. FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
  911. FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
  912. FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
  913. FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
  914. FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
  915. FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
  916. }
  917. POSTING_READ(DSPFW1);
  918. }
  919. #undef FW_WM_VLV
  920. static void g4x_setup_wm_latency(struct drm_i915_private *dev_priv)
  921. {
  922. /* all latencies in usec */
  923. dev_priv->wm.pri_latency[G4X_WM_LEVEL_NORMAL] = 5;
  924. dev_priv->wm.pri_latency[G4X_WM_LEVEL_SR] = 12;
  925. dev_priv->wm.pri_latency[G4X_WM_LEVEL_HPLL] = 35;
  926. dev_priv->wm.max_level = G4X_WM_LEVEL_HPLL;
  927. }
  928. static int g4x_plane_fifo_size(enum plane_id plane_id, int level)
  929. {
  930. /*
  931. * DSPCNTR[13] supposedly controls whether the
  932. * primary plane can use the FIFO space otherwise
  933. * reserved for the sprite plane. It's not 100% clear
  934. * what the actual FIFO size is, but it looks like we
  935. * can happily set both primary and sprite watermarks
  936. * up to 127 cachelines. So that would seem to mean
  937. * that either DSPCNTR[13] doesn't do anything, or that
  938. * the total FIFO is >= 256 cachelines in size. Either
  939. * way, we don't seem to have to worry about this
  940. * repartitioning as the maximum watermark value the
  941. * register can hold for each plane is lower than the
  942. * minimum FIFO size.
  943. */
  944. switch (plane_id) {
  945. case PLANE_CURSOR:
  946. return 63;
  947. case PLANE_PRIMARY:
  948. return level == G4X_WM_LEVEL_NORMAL ? 127 : 511;
  949. case PLANE_SPRITE0:
  950. return level == G4X_WM_LEVEL_NORMAL ? 127 : 0;
  951. default:
  952. MISSING_CASE(plane_id);
  953. return 0;
  954. }
  955. }
  956. static int g4x_fbc_fifo_size(int level)
  957. {
  958. switch (level) {
  959. case G4X_WM_LEVEL_SR:
  960. return 7;
  961. case G4X_WM_LEVEL_HPLL:
  962. return 15;
  963. default:
  964. MISSING_CASE(level);
  965. return 0;
  966. }
  967. }
  968. static uint16_t g4x_compute_wm(const struct intel_crtc_state *crtc_state,
  969. const struct intel_plane_state *plane_state,
  970. int level)
  971. {
  972. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  973. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  974. const struct drm_display_mode *adjusted_mode =
  975. &crtc_state->base.adjusted_mode;
  976. unsigned int latency = dev_priv->wm.pri_latency[level] * 10;
  977. unsigned int clock, htotal, cpp, width, wm;
  978. if (latency == 0)
  979. return USHRT_MAX;
  980. if (!intel_wm_plane_visible(crtc_state, plane_state))
  981. return 0;
  982. /*
  983. * Not 100% sure which way ELK should go here as the
  984. * spec only says CL/CTG should assume 32bpp and BW
  985. * doesn't need to. But as these things followed the
  986. * mobile vs. desktop lines on gen3 as well, let's
  987. * assume ELK doesn't need this.
  988. *
  989. * The spec also fails to list such a restriction for
  990. * the HPLL watermark, which seems a little strange.
  991. * Let's use 32bpp for the HPLL watermark as well.
  992. */
  993. if (IS_GM45(dev_priv) && plane->id == PLANE_PRIMARY &&
  994. level != G4X_WM_LEVEL_NORMAL)
  995. cpp = 4;
  996. else
  997. cpp = plane_state->base.fb->format->cpp[0];
  998. clock = adjusted_mode->crtc_clock;
  999. htotal = adjusted_mode->crtc_htotal;
  1000. if (plane->id == PLANE_CURSOR)
  1001. width = plane_state->base.crtc_w;
  1002. else
  1003. width = drm_rect_width(&plane_state->base.dst);
  1004. if (plane->id == PLANE_CURSOR) {
  1005. wm = intel_wm_method2(clock, htotal, width, cpp, latency);
  1006. } else if (plane->id == PLANE_PRIMARY &&
  1007. level == G4X_WM_LEVEL_NORMAL) {
  1008. wm = intel_wm_method1(clock, cpp, latency);
  1009. } else {
  1010. unsigned int small, large;
  1011. small = intel_wm_method1(clock, cpp, latency);
  1012. large = intel_wm_method2(clock, htotal, width, cpp, latency);
  1013. wm = min(small, large);
  1014. }
  1015. wm += g4x_tlb_miss_wa(g4x_plane_fifo_size(plane->id, level),
  1016. width, cpp);
  1017. wm = DIV_ROUND_UP(wm, 64) + 2;
  1018. return min_t(unsigned int, wm, USHRT_MAX);
  1019. }
  1020. static bool g4x_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
  1021. int level, enum plane_id plane_id, u16 value)
  1022. {
  1023. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  1024. bool dirty = false;
  1025. for (; level < intel_wm_num_levels(dev_priv); level++) {
  1026. struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
  1027. dirty |= raw->plane[plane_id] != value;
  1028. raw->plane[plane_id] = value;
  1029. }
  1030. return dirty;
  1031. }
  1032. static bool g4x_raw_fbc_wm_set(struct intel_crtc_state *crtc_state,
  1033. int level, u16 value)
  1034. {
  1035. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  1036. bool dirty = false;
  1037. /* NORMAL level doesn't have an FBC watermark */
  1038. level = max(level, G4X_WM_LEVEL_SR);
  1039. for (; level < intel_wm_num_levels(dev_priv); level++) {
  1040. struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
  1041. dirty |= raw->fbc != value;
  1042. raw->fbc = value;
  1043. }
  1044. return dirty;
  1045. }
  1046. static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
  1047. const struct intel_plane_state *pstate,
  1048. uint32_t pri_val);
  1049. static bool g4x_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
  1050. const struct intel_plane_state *plane_state)
  1051. {
  1052. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  1053. int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
  1054. enum plane_id plane_id = plane->id;
  1055. bool dirty = false;
  1056. int level;
  1057. if (!intel_wm_plane_visible(crtc_state, plane_state)) {
  1058. dirty |= g4x_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
  1059. if (plane_id == PLANE_PRIMARY)
  1060. dirty |= g4x_raw_fbc_wm_set(crtc_state, 0, 0);
  1061. goto out;
  1062. }
  1063. for (level = 0; level < num_levels; level++) {
  1064. struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
  1065. int wm, max_wm;
  1066. wm = g4x_compute_wm(crtc_state, plane_state, level);
  1067. max_wm = g4x_plane_fifo_size(plane_id, level);
  1068. if (wm > max_wm)
  1069. break;
  1070. dirty |= raw->plane[plane_id] != wm;
  1071. raw->plane[plane_id] = wm;
  1072. if (plane_id != PLANE_PRIMARY ||
  1073. level == G4X_WM_LEVEL_NORMAL)
  1074. continue;
  1075. wm = ilk_compute_fbc_wm(crtc_state, plane_state,
  1076. raw->plane[plane_id]);
  1077. max_wm = g4x_fbc_fifo_size(level);
  1078. /*
  1079. * FBC wm is not mandatory as we
  1080. * can always just disable its use.
  1081. */
  1082. if (wm > max_wm)
  1083. wm = USHRT_MAX;
  1084. dirty |= raw->fbc != wm;
  1085. raw->fbc = wm;
  1086. }
  1087. /* mark watermarks as invalid */
  1088. dirty |= g4x_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
  1089. if (plane_id == PLANE_PRIMARY)
  1090. dirty |= g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
  1091. out:
  1092. if (dirty) {
  1093. DRM_DEBUG_KMS("%s watermarks: normal=%d, SR=%d, HPLL=%d\n",
  1094. plane->base.name,
  1095. crtc_state->wm.g4x.raw[G4X_WM_LEVEL_NORMAL].plane[plane_id],
  1096. crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].plane[plane_id],
  1097. crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].plane[plane_id]);
  1098. if (plane_id == PLANE_PRIMARY)
  1099. DRM_DEBUG_KMS("FBC watermarks: SR=%d, HPLL=%d\n",
  1100. crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].fbc,
  1101. crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].fbc);
  1102. }
  1103. return dirty;
  1104. }
  1105. static bool g4x_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
  1106. enum plane_id plane_id, int level)
  1107. {
  1108. const struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
  1109. return raw->plane[plane_id] <= g4x_plane_fifo_size(plane_id, level);
  1110. }
  1111. static bool g4x_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state,
  1112. int level)
  1113. {
  1114. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  1115. if (level > dev_priv->wm.max_level)
  1116. return false;
  1117. return g4x_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
  1118. g4x_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
  1119. g4x_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
  1120. }
  1121. /* mark all levels starting from 'level' as invalid */
  1122. static void g4x_invalidate_wms(struct intel_crtc *crtc,
  1123. struct g4x_wm_state *wm_state, int level)
  1124. {
  1125. if (level <= G4X_WM_LEVEL_NORMAL) {
  1126. enum plane_id plane_id;
  1127. for_each_plane_id_on_crtc(crtc, plane_id)
  1128. wm_state->wm.plane[plane_id] = USHRT_MAX;
  1129. }
  1130. if (level <= G4X_WM_LEVEL_SR) {
  1131. wm_state->cxsr = false;
  1132. wm_state->sr.cursor = USHRT_MAX;
  1133. wm_state->sr.plane = USHRT_MAX;
  1134. wm_state->sr.fbc = USHRT_MAX;
  1135. }
  1136. if (level <= G4X_WM_LEVEL_HPLL) {
  1137. wm_state->hpll_en = false;
  1138. wm_state->hpll.cursor = USHRT_MAX;
  1139. wm_state->hpll.plane = USHRT_MAX;
  1140. wm_state->hpll.fbc = USHRT_MAX;
  1141. }
  1142. }
  1143. static int g4x_compute_pipe_wm(struct intel_crtc_state *crtc_state)
  1144. {
  1145. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  1146. struct intel_atomic_state *state =
  1147. to_intel_atomic_state(crtc_state->base.state);
  1148. struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
  1149. int num_active_planes = hweight32(crtc_state->active_planes &
  1150. ~BIT(PLANE_CURSOR));
  1151. const struct g4x_pipe_wm *raw;
  1152. const struct intel_plane_state *old_plane_state;
  1153. const struct intel_plane_state *new_plane_state;
  1154. struct intel_plane *plane;
  1155. enum plane_id plane_id;
  1156. int i, level;
  1157. unsigned int dirty = 0;
  1158. for_each_oldnew_intel_plane_in_state(state, plane,
  1159. old_plane_state,
  1160. new_plane_state, i) {
  1161. if (new_plane_state->base.crtc != &crtc->base &&
  1162. old_plane_state->base.crtc != &crtc->base)
  1163. continue;
  1164. if (g4x_raw_plane_wm_compute(crtc_state, new_plane_state))
  1165. dirty |= BIT(plane->id);
  1166. }
  1167. if (!dirty)
  1168. return 0;
  1169. level = G4X_WM_LEVEL_NORMAL;
  1170. if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
  1171. goto out;
  1172. raw = &crtc_state->wm.g4x.raw[level];
  1173. for_each_plane_id_on_crtc(crtc, plane_id)
  1174. wm_state->wm.plane[plane_id] = raw->plane[plane_id];
  1175. level = G4X_WM_LEVEL_SR;
  1176. if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
  1177. goto out;
  1178. raw = &crtc_state->wm.g4x.raw[level];
  1179. wm_state->sr.plane = raw->plane[PLANE_PRIMARY];
  1180. wm_state->sr.cursor = raw->plane[PLANE_CURSOR];
  1181. wm_state->sr.fbc = raw->fbc;
  1182. wm_state->cxsr = num_active_planes == BIT(PLANE_PRIMARY);
  1183. level = G4X_WM_LEVEL_HPLL;
  1184. if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
  1185. goto out;
  1186. raw = &crtc_state->wm.g4x.raw[level];
  1187. wm_state->hpll.plane = raw->plane[PLANE_PRIMARY];
  1188. wm_state->hpll.cursor = raw->plane[PLANE_CURSOR];
  1189. wm_state->hpll.fbc = raw->fbc;
  1190. wm_state->hpll_en = wm_state->cxsr;
  1191. level++;
  1192. out:
  1193. if (level == G4X_WM_LEVEL_NORMAL)
  1194. return -EINVAL;
  1195. /* invalidate the higher levels */
  1196. g4x_invalidate_wms(crtc, wm_state, level);
  1197. /*
  1198. * Determine if the FBC watermark(s) can be used. IF
  1199. * this isn't the case we prefer to disable the FBC
  1200. ( watermark(s) rather than disable the SR/HPLL
  1201. * level(s) entirely.
  1202. */
  1203. wm_state->fbc_en = level > G4X_WM_LEVEL_NORMAL;
  1204. if (level >= G4X_WM_LEVEL_SR &&
  1205. wm_state->sr.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_SR))
  1206. wm_state->fbc_en = false;
  1207. else if (level >= G4X_WM_LEVEL_HPLL &&
  1208. wm_state->hpll.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_HPLL))
  1209. wm_state->fbc_en = false;
  1210. return 0;
  1211. }
  1212. static int g4x_compute_intermediate_wm(struct drm_device *dev,
  1213. struct intel_crtc *crtc,
  1214. struct intel_crtc_state *new_crtc_state)
  1215. {
  1216. struct g4x_wm_state *intermediate = &new_crtc_state->wm.g4x.intermediate;
  1217. const struct g4x_wm_state *optimal = &new_crtc_state->wm.g4x.optimal;
  1218. struct intel_atomic_state *intel_state =
  1219. to_intel_atomic_state(new_crtc_state->base.state);
  1220. const struct intel_crtc_state *old_crtc_state =
  1221. intel_atomic_get_old_crtc_state(intel_state, crtc);
  1222. const struct g4x_wm_state *active = &old_crtc_state->wm.g4x.optimal;
  1223. enum plane_id plane_id;
  1224. if (!new_crtc_state->base.active || drm_atomic_crtc_needs_modeset(&new_crtc_state->base)) {
  1225. *intermediate = *optimal;
  1226. intermediate->cxsr = false;
  1227. intermediate->hpll_en = false;
  1228. goto out;
  1229. }
  1230. intermediate->cxsr = optimal->cxsr && active->cxsr &&
  1231. !new_crtc_state->disable_cxsr;
  1232. intermediate->hpll_en = optimal->hpll_en && active->hpll_en &&
  1233. !new_crtc_state->disable_cxsr;
  1234. intermediate->fbc_en = optimal->fbc_en && active->fbc_en;
  1235. for_each_plane_id_on_crtc(crtc, plane_id) {
  1236. intermediate->wm.plane[plane_id] =
  1237. max(optimal->wm.plane[plane_id],
  1238. active->wm.plane[plane_id]);
  1239. WARN_ON(intermediate->wm.plane[plane_id] >
  1240. g4x_plane_fifo_size(plane_id, G4X_WM_LEVEL_NORMAL));
  1241. }
  1242. intermediate->sr.plane = max(optimal->sr.plane,
  1243. active->sr.plane);
  1244. intermediate->sr.cursor = max(optimal->sr.cursor,
  1245. active->sr.cursor);
  1246. intermediate->sr.fbc = max(optimal->sr.fbc,
  1247. active->sr.fbc);
  1248. intermediate->hpll.plane = max(optimal->hpll.plane,
  1249. active->hpll.plane);
  1250. intermediate->hpll.cursor = max(optimal->hpll.cursor,
  1251. active->hpll.cursor);
  1252. intermediate->hpll.fbc = max(optimal->hpll.fbc,
  1253. active->hpll.fbc);
  1254. WARN_ON((intermediate->sr.plane >
  1255. g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_SR) ||
  1256. intermediate->sr.cursor >
  1257. g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_SR)) &&
  1258. intermediate->cxsr);
  1259. WARN_ON((intermediate->sr.plane >
  1260. g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_HPLL) ||
  1261. intermediate->sr.cursor >
  1262. g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_HPLL)) &&
  1263. intermediate->hpll_en);
  1264. WARN_ON(intermediate->sr.fbc > g4x_fbc_fifo_size(1) &&
  1265. intermediate->fbc_en && intermediate->cxsr);
  1266. WARN_ON(intermediate->hpll.fbc > g4x_fbc_fifo_size(2) &&
  1267. intermediate->fbc_en && intermediate->hpll_en);
  1268. out:
  1269. /*
  1270. * If our intermediate WM are identical to the final WM, then we can
  1271. * omit the post-vblank programming; only update if it's different.
  1272. */
  1273. if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
  1274. new_crtc_state->wm.need_postvbl_update = true;
  1275. return 0;
  1276. }
  1277. static void g4x_merge_wm(struct drm_i915_private *dev_priv,
  1278. struct g4x_wm_values *wm)
  1279. {
  1280. struct intel_crtc *crtc;
  1281. int num_active_crtcs = 0;
  1282. wm->cxsr = true;
  1283. wm->hpll_en = true;
  1284. wm->fbc_en = true;
  1285. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1286. const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
  1287. if (!crtc->active)
  1288. continue;
  1289. if (!wm_state->cxsr)
  1290. wm->cxsr = false;
  1291. if (!wm_state->hpll_en)
  1292. wm->hpll_en = false;
  1293. if (!wm_state->fbc_en)
  1294. wm->fbc_en = false;
  1295. num_active_crtcs++;
  1296. }
  1297. if (num_active_crtcs != 1) {
  1298. wm->cxsr = false;
  1299. wm->hpll_en = false;
  1300. wm->fbc_en = false;
  1301. }
  1302. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1303. const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
  1304. enum pipe pipe = crtc->pipe;
  1305. wm->pipe[pipe] = wm_state->wm;
  1306. if (crtc->active && wm->cxsr)
  1307. wm->sr = wm_state->sr;
  1308. if (crtc->active && wm->hpll_en)
  1309. wm->hpll = wm_state->hpll;
  1310. }
  1311. }
  1312. static void g4x_program_watermarks(struct drm_i915_private *dev_priv)
  1313. {
  1314. struct g4x_wm_values *old_wm = &dev_priv->wm.g4x;
  1315. struct g4x_wm_values new_wm = {};
  1316. g4x_merge_wm(dev_priv, &new_wm);
  1317. if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
  1318. return;
  1319. if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
  1320. _intel_set_memory_cxsr(dev_priv, false);
  1321. g4x_write_wm_values(dev_priv, &new_wm);
  1322. if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
  1323. _intel_set_memory_cxsr(dev_priv, true);
  1324. *old_wm = new_wm;
  1325. }
  1326. static void g4x_initial_watermarks(struct intel_atomic_state *state,
  1327. struct intel_crtc_state *crtc_state)
  1328. {
  1329. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  1330. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  1331. mutex_lock(&dev_priv->wm.wm_mutex);
  1332. crtc->wm.active.g4x = crtc_state->wm.g4x.intermediate;
  1333. g4x_program_watermarks(dev_priv);
  1334. mutex_unlock(&dev_priv->wm.wm_mutex);
  1335. }
  1336. static void g4x_optimize_watermarks(struct intel_atomic_state *state,
  1337. struct intel_crtc_state *crtc_state)
  1338. {
  1339. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  1340. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  1341. if (!crtc_state->wm.need_postvbl_update)
  1342. return;
  1343. mutex_lock(&dev_priv->wm.wm_mutex);
  1344. intel_crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
  1345. g4x_program_watermarks(dev_priv);
  1346. mutex_unlock(&dev_priv->wm.wm_mutex);
  1347. }
  1348. /* latency must be in 0.1us units. */
  1349. static unsigned int vlv_wm_method2(unsigned int pixel_rate,
  1350. unsigned int htotal,
  1351. unsigned int width,
  1352. unsigned int cpp,
  1353. unsigned int latency)
  1354. {
  1355. unsigned int ret;
  1356. ret = intel_wm_method2(pixel_rate, htotal,
  1357. width, cpp, latency);
  1358. ret = DIV_ROUND_UP(ret, 64);
  1359. return ret;
  1360. }
  1361. static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
  1362. {
  1363. /* all latencies in usec */
  1364. dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
  1365. dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
  1366. if (IS_CHERRYVIEW(dev_priv)) {
  1367. dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
  1368. dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
  1369. dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
  1370. }
  1371. }
  1372. static uint16_t vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
  1373. const struct intel_plane_state *plane_state,
  1374. int level)
  1375. {
  1376. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  1377. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  1378. const struct drm_display_mode *adjusted_mode =
  1379. &crtc_state->base.adjusted_mode;
  1380. unsigned int clock, htotal, cpp, width, wm;
  1381. if (dev_priv->wm.pri_latency[level] == 0)
  1382. return USHRT_MAX;
  1383. if (!intel_wm_plane_visible(crtc_state, plane_state))
  1384. return 0;
  1385. cpp = plane_state->base.fb->format->cpp[0];
  1386. clock = adjusted_mode->crtc_clock;
  1387. htotal = adjusted_mode->crtc_htotal;
  1388. width = crtc_state->pipe_src_w;
  1389. if (plane->id == PLANE_CURSOR) {
  1390. /*
  1391. * FIXME the formula gives values that are
  1392. * too big for the cursor FIFO, and hence we
  1393. * would never be able to use cursors. For
  1394. * now just hardcode the watermark.
  1395. */
  1396. wm = 63;
  1397. } else {
  1398. wm = vlv_wm_method2(clock, htotal, width, cpp,
  1399. dev_priv->wm.pri_latency[level] * 10);
  1400. }
  1401. return min_t(unsigned int, wm, USHRT_MAX);
  1402. }
  1403. static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
  1404. {
  1405. return (active_planes & (BIT(PLANE_SPRITE0) |
  1406. BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
  1407. }
  1408. static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
  1409. {
  1410. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  1411. const struct g4x_pipe_wm *raw =
  1412. &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
  1413. struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
  1414. unsigned int active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
  1415. int num_active_planes = hweight32(active_planes);
  1416. const int fifo_size = 511;
  1417. int fifo_extra, fifo_left = fifo_size;
  1418. int sprite0_fifo_extra = 0;
  1419. unsigned int total_rate;
  1420. enum plane_id plane_id;
  1421. /*
  1422. * When enabling sprite0 after sprite1 has already been enabled
  1423. * we tend to get an underrun unless sprite0 already has some
  1424. * FIFO space allcoated. Hence we always allocate at least one
  1425. * cacheline for sprite0 whenever sprite1 is enabled.
  1426. *
  1427. * All other plane enable sequences appear immune to this problem.
  1428. */
  1429. if (vlv_need_sprite0_fifo_workaround(active_planes))
  1430. sprite0_fifo_extra = 1;
  1431. total_rate = raw->plane[PLANE_PRIMARY] +
  1432. raw->plane[PLANE_SPRITE0] +
  1433. raw->plane[PLANE_SPRITE1] +
  1434. sprite0_fifo_extra;
  1435. if (total_rate > fifo_size)
  1436. return -EINVAL;
  1437. if (total_rate == 0)
  1438. total_rate = 1;
  1439. for_each_plane_id_on_crtc(crtc, plane_id) {
  1440. unsigned int rate;
  1441. if ((active_planes & BIT(plane_id)) == 0) {
  1442. fifo_state->plane[plane_id] = 0;
  1443. continue;
  1444. }
  1445. rate = raw->plane[plane_id];
  1446. fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
  1447. fifo_left -= fifo_state->plane[plane_id];
  1448. }
  1449. fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
  1450. fifo_left -= sprite0_fifo_extra;
  1451. fifo_state->plane[PLANE_CURSOR] = 63;
  1452. fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
  1453. /* spread the remainder evenly */
  1454. for_each_plane_id_on_crtc(crtc, plane_id) {
  1455. int plane_extra;
  1456. if (fifo_left == 0)
  1457. break;
  1458. if ((active_planes & BIT(plane_id)) == 0)
  1459. continue;
  1460. plane_extra = min(fifo_extra, fifo_left);
  1461. fifo_state->plane[plane_id] += plane_extra;
  1462. fifo_left -= plane_extra;
  1463. }
  1464. WARN_ON(active_planes != 0 && fifo_left != 0);
  1465. /* give it all to the first plane if none are active */
  1466. if (active_planes == 0) {
  1467. WARN_ON(fifo_left != fifo_size);
  1468. fifo_state->plane[PLANE_PRIMARY] = fifo_left;
  1469. }
  1470. return 0;
  1471. }
  1472. /* mark all levels starting from 'level' as invalid */
  1473. static void vlv_invalidate_wms(struct intel_crtc *crtc,
  1474. struct vlv_wm_state *wm_state, int level)
  1475. {
  1476. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1477. for (; level < intel_wm_num_levels(dev_priv); level++) {
  1478. enum plane_id plane_id;
  1479. for_each_plane_id_on_crtc(crtc, plane_id)
  1480. wm_state->wm[level].plane[plane_id] = USHRT_MAX;
  1481. wm_state->sr[level].cursor = USHRT_MAX;
  1482. wm_state->sr[level].plane = USHRT_MAX;
  1483. }
  1484. }
  1485. static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
  1486. {
  1487. if (wm > fifo_size)
  1488. return USHRT_MAX;
  1489. else
  1490. return fifo_size - wm;
  1491. }
  1492. /*
  1493. * Starting from 'level' set all higher
  1494. * levels to 'value' in the "raw" watermarks.
  1495. */
  1496. static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
  1497. int level, enum plane_id plane_id, u16 value)
  1498. {
  1499. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  1500. int num_levels = intel_wm_num_levels(dev_priv);
  1501. bool dirty = false;
  1502. for (; level < num_levels; level++) {
  1503. struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
  1504. dirty |= raw->plane[plane_id] != value;
  1505. raw->plane[plane_id] = value;
  1506. }
  1507. return dirty;
  1508. }
  1509. static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
  1510. const struct intel_plane_state *plane_state)
  1511. {
  1512. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  1513. enum plane_id plane_id = plane->id;
  1514. int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
  1515. int level;
  1516. bool dirty = false;
  1517. if (!intel_wm_plane_visible(crtc_state, plane_state)) {
  1518. dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
  1519. goto out;
  1520. }
  1521. for (level = 0; level < num_levels; level++) {
  1522. struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
  1523. int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
  1524. int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
  1525. if (wm > max_wm)
  1526. break;
  1527. dirty |= raw->plane[plane_id] != wm;
  1528. raw->plane[plane_id] = wm;
  1529. }
  1530. /* mark all higher levels as invalid */
  1531. dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
  1532. out:
  1533. if (dirty)
  1534. DRM_DEBUG_KMS("%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
  1535. plane->base.name,
  1536. crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
  1537. crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
  1538. crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
  1539. return dirty;
  1540. }
  1541. static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
  1542. enum plane_id plane_id, int level)
  1543. {
  1544. const struct g4x_pipe_wm *raw =
  1545. &crtc_state->wm.vlv.raw[level];
  1546. const struct vlv_fifo_state *fifo_state =
  1547. &crtc_state->wm.vlv.fifo_state;
  1548. return raw->plane[plane_id] <= fifo_state->plane[plane_id];
  1549. }
  1550. static bool vlv_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state, int level)
  1551. {
  1552. return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
  1553. vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
  1554. vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
  1555. vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
  1556. }
  1557. static int vlv_compute_pipe_wm(struct intel_crtc_state *crtc_state)
  1558. {
  1559. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  1560. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1561. struct intel_atomic_state *state =
  1562. to_intel_atomic_state(crtc_state->base.state);
  1563. struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
  1564. const struct vlv_fifo_state *fifo_state =
  1565. &crtc_state->wm.vlv.fifo_state;
  1566. int num_active_planes = hweight32(crtc_state->active_planes &
  1567. ~BIT(PLANE_CURSOR));
  1568. bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->base);
  1569. const struct intel_plane_state *old_plane_state;
  1570. const struct intel_plane_state *new_plane_state;
  1571. struct intel_plane *plane;
  1572. enum plane_id plane_id;
  1573. int level, ret, i;
  1574. unsigned int dirty = 0;
  1575. for_each_oldnew_intel_plane_in_state(state, plane,
  1576. old_plane_state,
  1577. new_plane_state, i) {
  1578. if (new_plane_state->base.crtc != &crtc->base &&
  1579. old_plane_state->base.crtc != &crtc->base)
  1580. continue;
  1581. if (vlv_raw_plane_wm_compute(crtc_state, new_plane_state))
  1582. dirty |= BIT(plane->id);
  1583. }
  1584. /*
  1585. * DSPARB registers may have been reset due to the
  1586. * power well being turned off. Make sure we restore
  1587. * them to a consistent state even if no primary/sprite
  1588. * planes are initially active.
  1589. */
  1590. if (needs_modeset)
  1591. crtc_state->fifo_changed = true;
  1592. if (!dirty)
  1593. return 0;
  1594. /* cursor changes don't warrant a FIFO recompute */
  1595. if (dirty & ~BIT(PLANE_CURSOR)) {
  1596. const struct intel_crtc_state *old_crtc_state =
  1597. intel_atomic_get_old_crtc_state(state, crtc);
  1598. const struct vlv_fifo_state *old_fifo_state =
  1599. &old_crtc_state->wm.vlv.fifo_state;
  1600. ret = vlv_compute_fifo(crtc_state);
  1601. if (ret)
  1602. return ret;
  1603. if (needs_modeset ||
  1604. memcmp(old_fifo_state, fifo_state,
  1605. sizeof(*fifo_state)) != 0)
  1606. crtc_state->fifo_changed = true;
  1607. }
  1608. /* initially allow all levels */
  1609. wm_state->num_levels = intel_wm_num_levels(dev_priv);
  1610. /*
  1611. * Note that enabling cxsr with no primary/sprite planes
  1612. * enabled can wedge the pipe. Hence we only allow cxsr
  1613. * with exactly one enabled primary/sprite plane.
  1614. */
  1615. wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
  1616. for (level = 0; level < wm_state->num_levels; level++) {
  1617. const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
  1618. const int sr_fifo_size = INTEL_INFO(dev_priv)->num_pipes * 512 - 1;
  1619. if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
  1620. break;
  1621. for_each_plane_id_on_crtc(crtc, plane_id) {
  1622. wm_state->wm[level].plane[plane_id] =
  1623. vlv_invert_wm_value(raw->plane[plane_id],
  1624. fifo_state->plane[plane_id]);
  1625. }
  1626. wm_state->sr[level].plane =
  1627. vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
  1628. raw->plane[PLANE_SPRITE0],
  1629. raw->plane[PLANE_SPRITE1]),
  1630. sr_fifo_size);
  1631. wm_state->sr[level].cursor =
  1632. vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
  1633. 63);
  1634. }
  1635. if (level == 0)
  1636. return -EINVAL;
  1637. /* limit to only levels we can actually handle */
  1638. wm_state->num_levels = level;
  1639. /* invalidate the higher levels */
  1640. vlv_invalidate_wms(crtc, wm_state, level);
  1641. return 0;
  1642. }
  1643. #define VLV_FIFO(plane, value) \
  1644. (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
  1645. static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
  1646. struct intel_crtc_state *crtc_state)
  1647. {
  1648. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  1649. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1650. const struct vlv_fifo_state *fifo_state =
  1651. &crtc_state->wm.vlv.fifo_state;
  1652. int sprite0_start, sprite1_start, fifo_size;
  1653. if (!crtc_state->fifo_changed)
  1654. return;
  1655. sprite0_start = fifo_state->plane[PLANE_PRIMARY];
  1656. sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
  1657. fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
  1658. WARN_ON(fifo_state->plane[PLANE_CURSOR] != 63);
  1659. WARN_ON(fifo_size != 511);
  1660. trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
  1661. /*
  1662. * uncore.lock serves a double purpose here. It allows us to
  1663. * use the less expensive I915_{READ,WRITE}_FW() functions, and
  1664. * it protects the DSPARB registers from getting clobbered by
  1665. * parallel updates from multiple pipes.
  1666. *
  1667. * intel_pipe_update_start() has already disabled interrupts
  1668. * for us, so a plain spin_lock() is sufficient here.
  1669. */
  1670. spin_lock(&dev_priv->uncore.lock);
  1671. switch (crtc->pipe) {
  1672. uint32_t dsparb, dsparb2, dsparb3;
  1673. case PIPE_A:
  1674. dsparb = I915_READ_FW(DSPARB);
  1675. dsparb2 = I915_READ_FW(DSPARB2);
  1676. dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
  1677. VLV_FIFO(SPRITEB, 0xff));
  1678. dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
  1679. VLV_FIFO(SPRITEB, sprite1_start));
  1680. dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
  1681. VLV_FIFO(SPRITEB_HI, 0x1));
  1682. dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
  1683. VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
  1684. I915_WRITE_FW(DSPARB, dsparb);
  1685. I915_WRITE_FW(DSPARB2, dsparb2);
  1686. break;
  1687. case PIPE_B:
  1688. dsparb = I915_READ_FW(DSPARB);
  1689. dsparb2 = I915_READ_FW(DSPARB2);
  1690. dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
  1691. VLV_FIFO(SPRITED, 0xff));
  1692. dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
  1693. VLV_FIFO(SPRITED, sprite1_start));
  1694. dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
  1695. VLV_FIFO(SPRITED_HI, 0xff));
  1696. dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
  1697. VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
  1698. I915_WRITE_FW(DSPARB, dsparb);
  1699. I915_WRITE_FW(DSPARB2, dsparb2);
  1700. break;
  1701. case PIPE_C:
  1702. dsparb3 = I915_READ_FW(DSPARB3);
  1703. dsparb2 = I915_READ_FW(DSPARB2);
  1704. dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
  1705. VLV_FIFO(SPRITEF, 0xff));
  1706. dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
  1707. VLV_FIFO(SPRITEF, sprite1_start));
  1708. dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
  1709. VLV_FIFO(SPRITEF_HI, 0xff));
  1710. dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
  1711. VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
  1712. I915_WRITE_FW(DSPARB3, dsparb3);
  1713. I915_WRITE_FW(DSPARB2, dsparb2);
  1714. break;
  1715. default:
  1716. break;
  1717. }
  1718. POSTING_READ_FW(DSPARB);
  1719. spin_unlock(&dev_priv->uncore.lock);
  1720. }
  1721. #undef VLV_FIFO
  1722. static int vlv_compute_intermediate_wm(struct drm_device *dev,
  1723. struct intel_crtc *crtc,
  1724. struct intel_crtc_state *new_crtc_state)
  1725. {
  1726. struct vlv_wm_state *intermediate = &new_crtc_state->wm.vlv.intermediate;
  1727. const struct vlv_wm_state *optimal = &new_crtc_state->wm.vlv.optimal;
  1728. struct intel_atomic_state *intel_state =
  1729. to_intel_atomic_state(new_crtc_state->base.state);
  1730. const struct intel_crtc_state *old_crtc_state =
  1731. intel_atomic_get_old_crtc_state(intel_state, crtc);
  1732. const struct vlv_wm_state *active = &old_crtc_state->wm.vlv.optimal;
  1733. int level;
  1734. if (!new_crtc_state->base.active || drm_atomic_crtc_needs_modeset(&new_crtc_state->base)) {
  1735. *intermediate = *optimal;
  1736. intermediate->cxsr = false;
  1737. goto out;
  1738. }
  1739. intermediate->num_levels = min(optimal->num_levels, active->num_levels);
  1740. intermediate->cxsr = optimal->cxsr && active->cxsr &&
  1741. !new_crtc_state->disable_cxsr;
  1742. for (level = 0; level < intermediate->num_levels; level++) {
  1743. enum plane_id plane_id;
  1744. for_each_plane_id_on_crtc(crtc, plane_id) {
  1745. intermediate->wm[level].plane[plane_id] =
  1746. min(optimal->wm[level].plane[plane_id],
  1747. active->wm[level].plane[plane_id]);
  1748. }
  1749. intermediate->sr[level].plane = min(optimal->sr[level].plane,
  1750. active->sr[level].plane);
  1751. intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
  1752. active->sr[level].cursor);
  1753. }
  1754. vlv_invalidate_wms(crtc, intermediate, level);
  1755. out:
  1756. /*
  1757. * If our intermediate WM are identical to the final WM, then we can
  1758. * omit the post-vblank programming; only update if it's different.
  1759. */
  1760. if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
  1761. new_crtc_state->wm.need_postvbl_update = true;
  1762. return 0;
  1763. }
  1764. static void vlv_merge_wm(struct drm_i915_private *dev_priv,
  1765. struct vlv_wm_values *wm)
  1766. {
  1767. struct intel_crtc *crtc;
  1768. int num_active_crtcs = 0;
  1769. wm->level = dev_priv->wm.max_level;
  1770. wm->cxsr = true;
  1771. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1772. const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
  1773. if (!crtc->active)
  1774. continue;
  1775. if (!wm_state->cxsr)
  1776. wm->cxsr = false;
  1777. num_active_crtcs++;
  1778. wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
  1779. }
  1780. if (num_active_crtcs != 1)
  1781. wm->cxsr = false;
  1782. if (num_active_crtcs > 1)
  1783. wm->level = VLV_WM_LEVEL_PM2;
  1784. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1785. const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
  1786. enum pipe pipe = crtc->pipe;
  1787. wm->pipe[pipe] = wm_state->wm[wm->level];
  1788. if (crtc->active && wm->cxsr)
  1789. wm->sr = wm_state->sr[wm->level];
  1790. wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
  1791. wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
  1792. wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
  1793. wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
  1794. }
  1795. }
  1796. static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
  1797. {
  1798. struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
  1799. struct vlv_wm_values new_wm = {};
  1800. vlv_merge_wm(dev_priv, &new_wm);
  1801. if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
  1802. return;
  1803. if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
  1804. chv_set_memory_dvfs(dev_priv, false);
  1805. if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
  1806. chv_set_memory_pm5(dev_priv, false);
  1807. if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
  1808. _intel_set_memory_cxsr(dev_priv, false);
  1809. vlv_write_wm_values(dev_priv, &new_wm);
  1810. if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
  1811. _intel_set_memory_cxsr(dev_priv, true);
  1812. if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
  1813. chv_set_memory_pm5(dev_priv, true);
  1814. if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
  1815. chv_set_memory_dvfs(dev_priv, true);
  1816. *old_wm = new_wm;
  1817. }
  1818. static void vlv_initial_watermarks(struct intel_atomic_state *state,
  1819. struct intel_crtc_state *crtc_state)
  1820. {
  1821. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  1822. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  1823. mutex_lock(&dev_priv->wm.wm_mutex);
  1824. crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
  1825. vlv_program_watermarks(dev_priv);
  1826. mutex_unlock(&dev_priv->wm.wm_mutex);
  1827. }
  1828. static void vlv_optimize_watermarks(struct intel_atomic_state *state,
  1829. struct intel_crtc_state *crtc_state)
  1830. {
  1831. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  1832. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  1833. if (!crtc_state->wm.need_postvbl_update)
  1834. return;
  1835. mutex_lock(&dev_priv->wm.wm_mutex);
  1836. intel_crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
  1837. vlv_program_watermarks(dev_priv);
  1838. mutex_unlock(&dev_priv->wm.wm_mutex);
  1839. }
  1840. static void i965_update_wm(struct intel_crtc *unused_crtc)
  1841. {
  1842. struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
  1843. struct intel_crtc *crtc;
  1844. int srwm = 1;
  1845. int cursor_sr = 16;
  1846. bool cxsr_enabled;
  1847. /* Calc sr entries for one plane configs */
  1848. crtc = single_enabled_crtc(dev_priv);
  1849. if (crtc) {
  1850. /* self-refresh has much higher latency */
  1851. static const int sr_latency_ns = 12000;
  1852. const struct drm_display_mode *adjusted_mode =
  1853. &crtc->config->base.adjusted_mode;
  1854. const struct drm_framebuffer *fb =
  1855. crtc->base.primary->state->fb;
  1856. int clock = adjusted_mode->crtc_clock;
  1857. int htotal = adjusted_mode->crtc_htotal;
  1858. int hdisplay = crtc->config->pipe_src_w;
  1859. int cpp = fb->format->cpp[0];
  1860. int entries;
  1861. entries = intel_wm_method2(clock, htotal,
  1862. hdisplay, cpp, sr_latency_ns / 100);
  1863. entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
  1864. srwm = I965_FIFO_SIZE - entries;
  1865. if (srwm < 0)
  1866. srwm = 1;
  1867. srwm &= 0x1ff;
  1868. DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
  1869. entries, srwm);
  1870. entries = intel_wm_method2(clock, htotal,
  1871. crtc->base.cursor->state->crtc_w, 4,
  1872. sr_latency_ns / 100);
  1873. entries = DIV_ROUND_UP(entries,
  1874. i965_cursor_wm_info.cacheline_size) +
  1875. i965_cursor_wm_info.guard_size;
  1876. cursor_sr = i965_cursor_wm_info.fifo_size - entries;
  1877. if (cursor_sr > i965_cursor_wm_info.max_wm)
  1878. cursor_sr = i965_cursor_wm_info.max_wm;
  1879. DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
  1880. "cursor %d\n", srwm, cursor_sr);
  1881. cxsr_enabled = true;
  1882. } else {
  1883. cxsr_enabled = false;
  1884. /* Turn off self refresh if both pipes are enabled */
  1885. intel_set_memory_cxsr(dev_priv, false);
  1886. }
  1887. DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
  1888. srwm);
  1889. /* 965 has limitations... */
  1890. I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
  1891. FW_WM(8, CURSORB) |
  1892. FW_WM(8, PLANEB) |
  1893. FW_WM(8, PLANEA));
  1894. I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
  1895. FW_WM(8, PLANEC_OLD));
  1896. /* update cursor SR watermark */
  1897. I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
  1898. if (cxsr_enabled)
  1899. intel_set_memory_cxsr(dev_priv, true);
  1900. }
  1901. #undef FW_WM
  1902. static void i9xx_update_wm(struct intel_crtc *unused_crtc)
  1903. {
  1904. struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
  1905. const struct intel_watermark_params *wm_info;
  1906. uint32_t fwater_lo;
  1907. uint32_t fwater_hi;
  1908. int cwm, srwm = 1;
  1909. int fifo_size;
  1910. int planea_wm, planeb_wm;
  1911. struct intel_crtc *crtc, *enabled = NULL;
  1912. if (IS_I945GM(dev_priv))
  1913. wm_info = &i945_wm_info;
  1914. else if (!IS_GEN2(dev_priv))
  1915. wm_info = &i915_wm_info;
  1916. else
  1917. wm_info = &i830_a_wm_info;
  1918. fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_A);
  1919. crtc = intel_get_crtc_for_plane(dev_priv, PLANE_A);
  1920. if (intel_crtc_active(crtc)) {
  1921. const struct drm_display_mode *adjusted_mode =
  1922. &crtc->config->base.adjusted_mode;
  1923. const struct drm_framebuffer *fb =
  1924. crtc->base.primary->state->fb;
  1925. int cpp;
  1926. if (IS_GEN2(dev_priv))
  1927. cpp = 4;
  1928. else
  1929. cpp = fb->format->cpp[0];
  1930. planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
  1931. wm_info, fifo_size, cpp,
  1932. pessimal_latency_ns);
  1933. enabled = crtc;
  1934. } else {
  1935. planea_wm = fifo_size - wm_info->guard_size;
  1936. if (planea_wm > (long)wm_info->max_wm)
  1937. planea_wm = wm_info->max_wm;
  1938. }
  1939. if (IS_GEN2(dev_priv))
  1940. wm_info = &i830_bc_wm_info;
  1941. fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_B);
  1942. crtc = intel_get_crtc_for_plane(dev_priv, PLANE_B);
  1943. if (intel_crtc_active(crtc)) {
  1944. const struct drm_display_mode *adjusted_mode =
  1945. &crtc->config->base.adjusted_mode;
  1946. const struct drm_framebuffer *fb =
  1947. crtc->base.primary->state->fb;
  1948. int cpp;
  1949. if (IS_GEN2(dev_priv))
  1950. cpp = 4;
  1951. else
  1952. cpp = fb->format->cpp[0];
  1953. planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
  1954. wm_info, fifo_size, cpp,
  1955. pessimal_latency_ns);
  1956. if (enabled == NULL)
  1957. enabled = crtc;
  1958. else
  1959. enabled = NULL;
  1960. } else {
  1961. planeb_wm = fifo_size - wm_info->guard_size;
  1962. if (planeb_wm > (long)wm_info->max_wm)
  1963. planeb_wm = wm_info->max_wm;
  1964. }
  1965. DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  1966. if (IS_I915GM(dev_priv) && enabled) {
  1967. struct drm_i915_gem_object *obj;
  1968. obj = intel_fb_obj(enabled->base.primary->state->fb);
  1969. /* self-refresh seems busted with untiled */
  1970. if (!i915_gem_object_is_tiled(obj))
  1971. enabled = NULL;
  1972. }
  1973. /*
  1974. * Overlay gets an aggressive default since video jitter is bad.
  1975. */
  1976. cwm = 2;
  1977. /* Play safe and disable self-refresh before adjusting watermarks. */
  1978. intel_set_memory_cxsr(dev_priv, false);
  1979. /* Calc sr entries for one plane configs */
  1980. if (HAS_FW_BLC(dev_priv) && enabled) {
  1981. /* self-refresh has much higher latency */
  1982. static const int sr_latency_ns = 6000;
  1983. const struct drm_display_mode *adjusted_mode =
  1984. &enabled->config->base.adjusted_mode;
  1985. const struct drm_framebuffer *fb =
  1986. enabled->base.primary->state->fb;
  1987. int clock = adjusted_mode->crtc_clock;
  1988. int htotal = adjusted_mode->crtc_htotal;
  1989. int hdisplay = enabled->config->pipe_src_w;
  1990. int cpp;
  1991. int entries;
  1992. if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
  1993. cpp = 4;
  1994. else
  1995. cpp = fb->format->cpp[0];
  1996. entries = intel_wm_method2(clock, htotal, hdisplay, cpp,
  1997. sr_latency_ns / 100);
  1998. entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
  1999. DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
  2000. srwm = wm_info->fifo_size - entries;
  2001. if (srwm < 0)
  2002. srwm = 1;
  2003. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
  2004. I915_WRITE(FW_BLC_SELF,
  2005. FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
  2006. else
  2007. I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
  2008. }
  2009. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
  2010. planea_wm, planeb_wm, cwm, srwm);
  2011. fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
  2012. fwater_hi = (cwm & 0x1f);
  2013. /* Set request length to 8 cachelines per fetch */
  2014. fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
  2015. fwater_hi = fwater_hi | (1 << 8);
  2016. I915_WRITE(FW_BLC, fwater_lo);
  2017. I915_WRITE(FW_BLC2, fwater_hi);
  2018. if (enabled)
  2019. intel_set_memory_cxsr(dev_priv, true);
  2020. }
  2021. static void i845_update_wm(struct intel_crtc *unused_crtc)
  2022. {
  2023. struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
  2024. struct intel_crtc *crtc;
  2025. const struct drm_display_mode *adjusted_mode;
  2026. uint32_t fwater_lo;
  2027. int planea_wm;
  2028. crtc = single_enabled_crtc(dev_priv);
  2029. if (crtc == NULL)
  2030. return;
  2031. adjusted_mode = &crtc->config->base.adjusted_mode;
  2032. planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
  2033. &i845_wm_info,
  2034. dev_priv->display.get_fifo_size(dev_priv, PLANE_A),
  2035. 4, pessimal_latency_ns);
  2036. fwater_lo = I915_READ(FW_BLC) & ~0xfff;
  2037. fwater_lo |= (3<<8) | planea_wm;
  2038. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
  2039. I915_WRITE(FW_BLC, fwater_lo);
  2040. }
  2041. /* latency must be in 0.1us units. */
  2042. static unsigned int ilk_wm_method1(unsigned int pixel_rate,
  2043. unsigned int cpp,
  2044. unsigned int latency)
  2045. {
  2046. unsigned int ret;
  2047. ret = intel_wm_method1(pixel_rate, cpp, latency);
  2048. ret = DIV_ROUND_UP(ret, 64) + 2;
  2049. return ret;
  2050. }
  2051. /* latency must be in 0.1us units. */
  2052. static unsigned int ilk_wm_method2(unsigned int pixel_rate,
  2053. unsigned int htotal,
  2054. unsigned int width,
  2055. unsigned int cpp,
  2056. unsigned int latency)
  2057. {
  2058. unsigned int ret;
  2059. ret = intel_wm_method2(pixel_rate, htotal,
  2060. width, cpp, latency);
  2061. ret = DIV_ROUND_UP(ret, 64) + 2;
  2062. return ret;
  2063. }
  2064. static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
  2065. uint8_t cpp)
  2066. {
  2067. /*
  2068. * Neither of these should be possible since this function shouldn't be
  2069. * called if the CRTC is off or the plane is invisible. But let's be
  2070. * extra paranoid to avoid a potential divide-by-zero if we screw up
  2071. * elsewhere in the driver.
  2072. */
  2073. if (WARN_ON(!cpp))
  2074. return 0;
  2075. if (WARN_ON(!horiz_pixels))
  2076. return 0;
  2077. return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
  2078. }
  2079. struct ilk_wm_maximums {
  2080. uint16_t pri;
  2081. uint16_t spr;
  2082. uint16_t cur;
  2083. uint16_t fbc;
  2084. };
  2085. /*
  2086. * For both WM_PIPE and WM_LP.
  2087. * mem_value must be in 0.1us units.
  2088. */
  2089. static uint32_t ilk_compute_pri_wm(const struct intel_crtc_state *cstate,
  2090. const struct intel_plane_state *pstate,
  2091. uint32_t mem_value,
  2092. bool is_lp)
  2093. {
  2094. uint32_t method1, method2;
  2095. int cpp;
  2096. if (mem_value == 0)
  2097. return U32_MAX;
  2098. if (!intel_wm_plane_visible(cstate, pstate))
  2099. return 0;
  2100. cpp = pstate->base.fb->format->cpp[0];
  2101. method1 = ilk_wm_method1(cstate->pixel_rate, cpp, mem_value);
  2102. if (!is_lp)
  2103. return method1;
  2104. method2 = ilk_wm_method2(cstate->pixel_rate,
  2105. cstate->base.adjusted_mode.crtc_htotal,
  2106. drm_rect_width(&pstate->base.dst),
  2107. cpp, mem_value);
  2108. return min(method1, method2);
  2109. }
  2110. /*
  2111. * For both WM_PIPE and WM_LP.
  2112. * mem_value must be in 0.1us units.
  2113. */
  2114. static uint32_t ilk_compute_spr_wm(const struct intel_crtc_state *cstate,
  2115. const struct intel_plane_state *pstate,
  2116. uint32_t mem_value)
  2117. {
  2118. uint32_t method1, method2;
  2119. int cpp;
  2120. if (mem_value == 0)
  2121. return U32_MAX;
  2122. if (!intel_wm_plane_visible(cstate, pstate))
  2123. return 0;
  2124. cpp = pstate->base.fb->format->cpp[0];
  2125. method1 = ilk_wm_method1(cstate->pixel_rate, cpp, mem_value);
  2126. method2 = ilk_wm_method2(cstate->pixel_rate,
  2127. cstate->base.adjusted_mode.crtc_htotal,
  2128. drm_rect_width(&pstate->base.dst),
  2129. cpp, mem_value);
  2130. return min(method1, method2);
  2131. }
  2132. /*
  2133. * For both WM_PIPE and WM_LP.
  2134. * mem_value must be in 0.1us units.
  2135. */
  2136. static uint32_t ilk_compute_cur_wm(const struct intel_crtc_state *cstate,
  2137. const struct intel_plane_state *pstate,
  2138. uint32_t mem_value)
  2139. {
  2140. int cpp;
  2141. if (mem_value == 0)
  2142. return U32_MAX;
  2143. if (!intel_wm_plane_visible(cstate, pstate))
  2144. return 0;
  2145. cpp = pstate->base.fb->format->cpp[0];
  2146. return ilk_wm_method2(cstate->pixel_rate,
  2147. cstate->base.adjusted_mode.crtc_htotal,
  2148. pstate->base.crtc_w, cpp, mem_value);
  2149. }
  2150. /* Only for WM_LP. */
  2151. static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
  2152. const struct intel_plane_state *pstate,
  2153. uint32_t pri_val)
  2154. {
  2155. int cpp;
  2156. if (!intel_wm_plane_visible(cstate, pstate))
  2157. return 0;
  2158. cpp = pstate->base.fb->format->cpp[0];
  2159. return ilk_wm_fbc(pri_val, drm_rect_width(&pstate->base.dst), cpp);
  2160. }
  2161. static unsigned int
  2162. ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
  2163. {
  2164. if (INTEL_GEN(dev_priv) >= 8)
  2165. return 3072;
  2166. else if (INTEL_GEN(dev_priv) >= 7)
  2167. return 768;
  2168. else
  2169. return 512;
  2170. }
  2171. static unsigned int
  2172. ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
  2173. int level, bool is_sprite)
  2174. {
  2175. if (INTEL_GEN(dev_priv) >= 8)
  2176. /* BDW primary/sprite plane watermarks */
  2177. return level == 0 ? 255 : 2047;
  2178. else if (INTEL_GEN(dev_priv) >= 7)
  2179. /* IVB/HSW primary/sprite plane watermarks */
  2180. return level == 0 ? 127 : 1023;
  2181. else if (!is_sprite)
  2182. /* ILK/SNB primary plane watermarks */
  2183. return level == 0 ? 127 : 511;
  2184. else
  2185. /* ILK/SNB sprite plane watermarks */
  2186. return level == 0 ? 63 : 255;
  2187. }
  2188. static unsigned int
  2189. ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
  2190. {
  2191. if (INTEL_GEN(dev_priv) >= 7)
  2192. return level == 0 ? 63 : 255;
  2193. else
  2194. return level == 0 ? 31 : 63;
  2195. }
  2196. static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
  2197. {
  2198. if (INTEL_GEN(dev_priv) >= 8)
  2199. return 31;
  2200. else
  2201. return 15;
  2202. }
  2203. /* Calculate the maximum primary/sprite plane watermark */
  2204. static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
  2205. int level,
  2206. const struct intel_wm_config *config,
  2207. enum intel_ddb_partitioning ddb_partitioning,
  2208. bool is_sprite)
  2209. {
  2210. struct drm_i915_private *dev_priv = to_i915(dev);
  2211. unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
  2212. /* if sprites aren't enabled, sprites get nothing */
  2213. if (is_sprite && !config->sprites_enabled)
  2214. return 0;
  2215. /* HSW allows LP1+ watermarks even with multiple pipes */
  2216. if (level == 0 || config->num_pipes_active > 1) {
  2217. fifo_size /= INTEL_INFO(dev_priv)->num_pipes;
  2218. /*
  2219. * For some reason the non self refresh
  2220. * FIFO size is only half of the self
  2221. * refresh FIFO size on ILK/SNB.
  2222. */
  2223. if (INTEL_GEN(dev_priv) <= 6)
  2224. fifo_size /= 2;
  2225. }
  2226. if (config->sprites_enabled) {
  2227. /* level 0 is always calculated with 1:1 split */
  2228. if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
  2229. if (is_sprite)
  2230. fifo_size *= 5;
  2231. fifo_size /= 6;
  2232. } else {
  2233. fifo_size /= 2;
  2234. }
  2235. }
  2236. /* clamp to max that the registers can hold */
  2237. return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
  2238. }
  2239. /* Calculate the maximum cursor plane watermark */
  2240. static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
  2241. int level,
  2242. const struct intel_wm_config *config)
  2243. {
  2244. /* HSW LP1+ watermarks w/ multiple pipes */
  2245. if (level > 0 && config->num_pipes_active > 1)
  2246. return 64;
  2247. /* otherwise just report max that registers can hold */
  2248. return ilk_cursor_wm_reg_max(to_i915(dev), level);
  2249. }
  2250. static void ilk_compute_wm_maximums(const struct drm_device *dev,
  2251. int level,
  2252. const struct intel_wm_config *config,
  2253. enum intel_ddb_partitioning ddb_partitioning,
  2254. struct ilk_wm_maximums *max)
  2255. {
  2256. max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
  2257. max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
  2258. max->cur = ilk_cursor_wm_max(dev, level, config);
  2259. max->fbc = ilk_fbc_wm_reg_max(to_i915(dev));
  2260. }
  2261. static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
  2262. int level,
  2263. struct ilk_wm_maximums *max)
  2264. {
  2265. max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
  2266. max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
  2267. max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
  2268. max->fbc = ilk_fbc_wm_reg_max(dev_priv);
  2269. }
  2270. static bool ilk_validate_wm_level(int level,
  2271. const struct ilk_wm_maximums *max,
  2272. struct intel_wm_level *result)
  2273. {
  2274. bool ret;
  2275. /* already determined to be invalid? */
  2276. if (!result->enable)
  2277. return false;
  2278. result->enable = result->pri_val <= max->pri &&
  2279. result->spr_val <= max->spr &&
  2280. result->cur_val <= max->cur;
  2281. ret = result->enable;
  2282. /*
  2283. * HACK until we can pre-compute everything,
  2284. * and thus fail gracefully if LP0 watermarks
  2285. * are exceeded...
  2286. */
  2287. if (level == 0 && !result->enable) {
  2288. if (result->pri_val > max->pri)
  2289. DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
  2290. level, result->pri_val, max->pri);
  2291. if (result->spr_val > max->spr)
  2292. DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
  2293. level, result->spr_val, max->spr);
  2294. if (result->cur_val > max->cur)
  2295. DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
  2296. level, result->cur_val, max->cur);
  2297. result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
  2298. result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
  2299. result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
  2300. result->enable = true;
  2301. }
  2302. return ret;
  2303. }
  2304. static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
  2305. const struct intel_crtc *intel_crtc,
  2306. int level,
  2307. struct intel_crtc_state *cstate,
  2308. const struct intel_plane_state *pristate,
  2309. const struct intel_plane_state *sprstate,
  2310. const struct intel_plane_state *curstate,
  2311. struct intel_wm_level *result)
  2312. {
  2313. uint16_t pri_latency = dev_priv->wm.pri_latency[level];
  2314. uint16_t spr_latency = dev_priv->wm.spr_latency[level];
  2315. uint16_t cur_latency = dev_priv->wm.cur_latency[level];
  2316. /* WM1+ latency values stored in 0.5us units */
  2317. if (level > 0) {
  2318. pri_latency *= 5;
  2319. spr_latency *= 5;
  2320. cur_latency *= 5;
  2321. }
  2322. if (pristate) {
  2323. result->pri_val = ilk_compute_pri_wm(cstate, pristate,
  2324. pri_latency, level);
  2325. result->fbc_val = ilk_compute_fbc_wm(cstate, pristate, result->pri_val);
  2326. }
  2327. if (sprstate)
  2328. result->spr_val = ilk_compute_spr_wm(cstate, sprstate, spr_latency);
  2329. if (curstate)
  2330. result->cur_val = ilk_compute_cur_wm(cstate, curstate, cur_latency);
  2331. result->enable = true;
  2332. }
  2333. static uint32_t
  2334. hsw_compute_linetime_wm(const struct intel_crtc_state *cstate)
  2335. {
  2336. const struct intel_atomic_state *intel_state =
  2337. to_intel_atomic_state(cstate->base.state);
  2338. const struct drm_display_mode *adjusted_mode =
  2339. &cstate->base.adjusted_mode;
  2340. u32 linetime, ips_linetime;
  2341. if (!cstate->base.active)
  2342. return 0;
  2343. if (WARN_ON(adjusted_mode->crtc_clock == 0))
  2344. return 0;
  2345. if (WARN_ON(intel_state->cdclk.logical.cdclk == 0))
  2346. return 0;
  2347. /* The WM are computed with base on how long it takes to fill a single
  2348. * row at the given clock rate, multiplied by 8.
  2349. * */
  2350. linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
  2351. adjusted_mode->crtc_clock);
  2352. ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
  2353. intel_state->cdclk.logical.cdclk);
  2354. return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
  2355. PIPE_WM_LINETIME_TIME(linetime);
  2356. }
  2357. static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
  2358. uint16_t wm[8])
  2359. {
  2360. if (INTEL_GEN(dev_priv) >= 9) {
  2361. uint32_t val;
  2362. int ret, i;
  2363. int level, max_level = ilk_wm_max_level(dev_priv);
  2364. /* read the first set of memory latencies[0:3] */
  2365. val = 0; /* data0 to be programmed to 0 for first set */
  2366. mutex_lock(&dev_priv->pcu_lock);
  2367. ret = sandybridge_pcode_read(dev_priv,
  2368. GEN9_PCODE_READ_MEM_LATENCY,
  2369. &val);
  2370. mutex_unlock(&dev_priv->pcu_lock);
  2371. if (ret) {
  2372. DRM_ERROR("SKL Mailbox read error = %d\n", ret);
  2373. return;
  2374. }
  2375. wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
  2376. wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
  2377. GEN9_MEM_LATENCY_LEVEL_MASK;
  2378. wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
  2379. GEN9_MEM_LATENCY_LEVEL_MASK;
  2380. wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
  2381. GEN9_MEM_LATENCY_LEVEL_MASK;
  2382. /* read the second set of memory latencies[4:7] */
  2383. val = 1; /* data0 to be programmed to 1 for second set */
  2384. mutex_lock(&dev_priv->pcu_lock);
  2385. ret = sandybridge_pcode_read(dev_priv,
  2386. GEN9_PCODE_READ_MEM_LATENCY,
  2387. &val);
  2388. mutex_unlock(&dev_priv->pcu_lock);
  2389. if (ret) {
  2390. DRM_ERROR("SKL Mailbox read error = %d\n", ret);
  2391. return;
  2392. }
  2393. wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
  2394. wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
  2395. GEN9_MEM_LATENCY_LEVEL_MASK;
  2396. wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
  2397. GEN9_MEM_LATENCY_LEVEL_MASK;
  2398. wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
  2399. GEN9_MEM_LATENCY_LEVEL_MASK;
  2400. /*
  2401. * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
  2402. * need to be disabled. We make sure to sanitize the values out
  2403. * of the punit to satisfy this requirement.
  2404. */
  2405. for (level = 1; level <= max_level; level++) {
  2406. if (wm[level] == 0) {
  2407. for (i = level + 1; i <= max_level; i++)
  2408. wm[i] = 0;
  2409. break;
  2410. }
  2411. }
  2412. /*
  2413. * WaWmMemoryReadLatency:skl+,glk
  2414. *
  2415. * punit doesn't take into account the read latency so we need
  2416. * to add 2us to the various latency levels we retrieve from the
  2417. * punit when level 0 response data us 0us.
  2418. */
  2419. if (wm[0] == 0) {
  2420. wm[0] += 2;
  2421. for (level = 1; level <= max_level; level++) {
  2422. if (wm[level] == 0)
  2423. break;
  2424. wm[level] += 2;
  2425. }
  2426. }
  2427. /*
  2428. * WA Level-0 adjustment for 16GB DIMMs: SKL+
  2429. * If we could not get dimm info enable this WA to prevent from
  2430. * any underrun. If not able to get Dimm info assume 16GB dimm
  2431. * to avoid any underrun.
  2432. */
  2433. if (dev_priv->dram_info.is_16gb_dimm)
  2434. wm[0] += 1;
  2435. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2436. uint64_t sskpd = I915_READ64(MCH_SSKPD);
  2437. wm[0] = (sskpd >> 56) & 0xFF;
  2438. if (wm[0] == 0)
  2439. wm[0] = sskpd & 0xF;
  2440. wm[1] = (sskpd >> 4) & 0xFF;
  2441. wm[2] = (sskpd >> 12) & 0xFF;
  2442. wm[3] = (sskpd >> 20) & 0x1FF;
  2443. wm[4] = (sskpd >> 32) & 0x1FF;
  2444. } else if (INTEL_GEN(dev_priv) >= 6) {
  2445. uint32_t sskpd = I915_READ(MCH_SSKPD);
  2446. wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
  2447. wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
  2448. wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
  2449. wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
  2450. } else if (INTEL_GEN(dev_priv) >= 5) {
  2451. uint32_t mltr = I915_READ(MLTR_ILK);
  2452. /* ILK primary LP0 latency is 700 ns */
  2453. wm[0] = 7;
  2454. wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
  2455. wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
  2456. } else {
  2457. MISSING_CASE(INTEL_DEVID(dev_priv));
  2458. }
  2459. }
  2460. static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
  2461. uint16_t wm[5])
  2462. {
  2463. /* ILK sprite LP0 latency is 1300 ns */
  2464. if (IS_GEN5(dev_priv))
  2465. wm[0] = 13;
  2466. }
  2467. static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
  2468. uint16_t wm[5])
  2469. {
  2470. /* ILK cursor LP0 latency is 1300 ns */
  2471. if (IS_GEN5(dev_priv))
  2472. wm[0] = 13;
  2473. }
  2474. int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
  2475. {
  2476. /* how many WM levels are we expecting */
  2477. if (INTEL_GEN(dev_priv) >= 9)
  2478. return 7;
  2479. else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2480. return 4;
  2481. else if (INTEL_GEN(dev_priv) >= 6)
  2482. return 3;
  2483. else
  2484. return 2;
  2485. }
  2486. static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
  2487. const char *name,
  2488. const uint16_t wm[8])
  2489. {
  2490. int level, max_level = ilk_wm_max_level(dev_priv);
  2491. for (level = 0; level <= max_level; level++) {
  2492. unsigned int latency = wm[level];
  2493. if (latency == 0) {
  2494. DRM_DEBUG_KMS("%s WM%d latency not provided\n",
  2495. name, level);
  2496. continue;
  2497. }
  2498. /*
  2499. * - latencies are in us on gen9.
  2500. * - before then, WM1+ latency values are in 0.5us units
  2501. */
  2502. if (INTEL_GEN(dev_priv) >= 9)
  2503. latency *= 10;
  2504. else if (level > 0)
  2505. latency *= 5;
  2506. DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
  2507. name, level, wm[level],
  2508. latency / 10, latency % 10);
  2509. }
  2510. }
  2511. static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
  2512. uint16_t wm[5], uint16_t min)
  2513. {
  2514. int level, max_level = ilk_wm_max_level(dev_priv);
  2515. if (wm[0] >= min)
  2516. return false;
  2517. wm[0] = max(wm[0], min);
  2518. for (level = 1; level <= max_level; level++)
  2519. wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
  2520. return true;
  2521. }
  2522. static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
  2523. {
  2524. bool changed;
  2525. /*
  2526. * The BIOS provided WM memory latency values are often
  2527. * inadequate for high resolution displays. Adjust them.
  2528. */
  2529. changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
  2530. ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
  2531. ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
  2532. if (!changed)
  2533. return;
  2534. DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
  2535. intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
  2536. intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
  2537. intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
  2538. }
  2539. static void snb_wm_lp3_irq_quirk(struct drm_i915_private *dev_priv)
  2540. {
  2541. /*
  2542. * On some SNB machines (Thinkpad X220 Tablet at least)
  2543. * LP3 usage can cause vblank interrupts to be lost.
  2544. * The DEIIR bit will go high but it looks like the CPU
  2545. * never gets interrupted.
  2546. *
  2547. * It's not clear whether other interrupt source could
  2548. * be affected or if this is somehow limited to vblank
  2549. * interrupts only. To play it safe we disable LP3
  2550. * watermarks entirely.
  2551. */
  2552. if (dev_priv->wm.pri_latency[3] == 0 &&
  2553. dev_priv->wm.spr_latency[3] == 0 &&
  2554. dev_priv->wm.cur_latency[3] == 0)
  2555. return;
  2556. dev_priv->wm.pri_latency[3] = 0;
  2557. dev_priv->wm.spr_latency[3] = 0;
  2558. dev_priv->wm.cur_latency[3] = 0;
  2559. DRM_DEBUG_KMS("LP3 watermarks disabled due to potential for lost interrupts\n");
  2560. intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
  2561. intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
  2562. intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
  2563. }
  2564. static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
  2565. {
  2566. intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
  2567. memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
  2568. sizeof(dev_priv->wm.pri_latency));
  2569. memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
  2570. sizeof(dev_priv->wm.pri_latency));
  2571. intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
  2572. intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
  2573. intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
  2574. intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
  2575. intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
  2576. if (IS_GEN6(dev_priv)) {
  2577. snb_wm_latency_quirk(dev_priv);
  2578. snb_wm_lp3_irq_quirk(dev_priv);
  2579. }
  2580. }
  2581. static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
  2582. {
  2583. intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
  2584. intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
  2585. }
  2586. static bool ilk_validate_pipe_wm(struct drm_device *dev,
  2587. struct intel_pipe_wm *pipe_wm)
  2588. {
  2589. /* LP0 watermark maximums depend on this pipe alone */
  2590. const struct intel_wm_config config = {
  2591. .num_pipes_active = 1,
  2592. .sprites_enabled = pipe_wm->sprites_enabled,
  2593. .sprites_scaled = pipe_wm->sprites_scaled,
  2594. };
  2595. struct ilk_wm_maximums max;
  2596. /* LP0 watermarks always use 1/2 DDB partitioning */
  2597. ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
  2598. /* At least LP0 must be valid */
  2599. if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
  2600. DRM_DEBUG_KMS("LP0 watermark invalid\n");
  2601. return false;
  2602. }
  2603. return true;
  2604. }
  2605. /* Compute new watermarks for the pipe */
  2606. static int ilk_compute_pipe_wm(struct intel_crtc_state *cstate)
  2607. {
  2608. struct drm_atomic_state *state = cstate->base.state;
  2609. struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
  2610. struct intel_pipe_wm *pipe_wm;
  2611. struct drm_device *dev = state->dev;
  2612. const struct drm_i915_private *dev_priv = to_i915(dev);
  2613. struct drm_plane *plane;
  2614. const struct drm_plane_state *plane_state;
  2615. const struct intel_plane_state *pristate = NULL;
  2616. const struct intel_plane_state *sprstate = NULL;
  2617. const struct intel_plane_state *curstate = NULL;
  2618. int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
  2619. struct ilk_wm_maximums max;
  2620. pipe_wm = &cstate->wm.ilk.optimal;
  2621. drm_atomic_crtc_state_for_each_plane_state(plane, plane_state, &cstate->base) {
  2622. const struct intel_plane_state *ps = to_intel_plane_state(plane_state);
  2623. if (plane->type == DRM_PLANE_TYPE_PRIMARY)
  2624. pristate = ps;
  2625. else if (plane->type == DRM_PLANE_TYPE_OVERLAY)
  2626. sprstate = ps;
  2627. else if (plane->type == DRM_PLANE_TYPE_CURSOR)
  2628. curstate = ps;
  2629. }
  2630. pipe_wm->pipe_enabled = cstate->base.active;
  2631. if (sprstate) {
  2632. pipe_wm->sprites_enabled = sprstate->base.visible;
  2633. pipe_wm->sprites_scaled = sprstate->base.visible &&
  2634. (drm_rect_width(&sprstate->base.dst) != drm_rect_width(&sprstate->base.src) >> 16 ||
  2635. drm_rect_height(&sprstate->base.dst) != drm_rect_height(&sprstate->base.src) >> 16);
  2636. }
  2637. usable_level = max_level;
  2638. /* ILK/SNB: LP2+ watermarks only w/o sprites */
  2639. if (INTEL_GEN(dev_priv) <= 6 && pipe_wm->sprites_enabled)
  2640. usable_level = 1;
  2641. /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
  2642. if (pipe_wm->sprites_scaled)
  2643. usable_level = 0;
  2644. memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
  2645. ilk_compute_wm_level(dev_priv, intel_crtc, 0, cstate,
  2646. pristate, sprstate, curstate, &pipe_wm->wm[0]);
  2647. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2648. pipe_wm->linetime = hsw_compute_linetime_wm(cstate);
  2649. if (!ilk_validate_pipe_wm(dev, pipe_wm))
  2650. return -EINVAL;
  2651. ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
  2652. for (level = 1; level <= usable_level; level++) {
  2653. struct intel_wm_level *wm = &pipe_wm->wm[level];
  2654. ilk_compute_wm_level(dev_priv, intel_crtc, level, cstate,
  2655. pristate, sprstate, curstate, wm);
  2656. /*
  2657. * Disable any watermark level that exceeds the
  2658. * register maximums since such watermarks are
  2659. * always invalid.
  2660. */
  2661. if (!ilk_validate_wm_level(level, &max, wm)) {
  2662. memset(wm, 0, sizeof(*wm));
  2663. break;
  2664. }
  2665. }
  2666. return 0;
  2667. }
  2668. /*
  2669. * Build a set of 'intermediate' watermark values that satisfy both the old
  2670. * state and the new state. These can be programmed to the hardware
  2671. * immediately.
  2672. */
  2673. static int ilk_compute_intermediate_wm(struct drm_device *dev,
  2674. struct intel_crtc *intel_crtc,
  2675. struct intel_crtc_state *newstate)
  2676. {
  2677. struct intel_pipe_wm *a = &newstate->wm.ilk.intermediate;
  2678. struct intel_atomic_state *intel_state =
  2679. to_intel_atomic_state(newstate->base.state);
  2680. const struct intel_crtc_state *oldstate =
  2681. intel_atomic_get_old_crtc_state(intel_state, intel_crtc);
  2682. const struct intel_pipe_wm *b = &oldstate->wm.ilk.optimal;
  2683. int level, max_level = ilk_wm_max_level(to_i915(dev));
  2684. /*
  2685. * Start with the final, target watermarks, then combine with the
  2686. * currently active watermarks to get values that are safe both before
  2687. * and after the vblank.
  2688. */
  2689. *a = newstate->wm.ilk.optimal;
  2690. if (!newstate->base.active || drm_atomic_crtc_needs_modeset(&newstate->base))
  2691. return 0;
  2692. a->pipe_enabled |= b->pipe_enabled;
  2693. a->sprites_enabled |= b->sprites_enabled;
  2694. a->sprites_scaled |= b->sprites_scaled;
  2695. for (level = 0; level <= max_level; level++) {
  2696. struct intel_wm_level *a_wm = &a->wm[level];
  2697. const struct intel_wm_level *b_wm = &b->wm[level];
  2698. a_wm->enable &= b_wm->enable;
  2699. a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
  2700. a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
  2701. a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
  2702. a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
  2703. }
  2704. /*
  2705. * We need to make sure that these merged watermark values are
  2706. * actually a valid configuration themselves. If they're not,
  2707. * there's no safe way to transition from the old state to
  2708. * the new state, so we need to fail the atomic transaction.
  2709. */
  2710. if (!ilk_validate_pipe_wm(dev, a))
  2711. return -EINVAL;
  2712. /*
  2713. * If our intermediate WM are identical to the final WM, then we can
  2714. * omit the post-vblank programming; only update if it's different.
  2715. */
  2716. if (memcmp(a, &newstate->wm.ilk.optimal, sizeof(*a)) != 0)
  2717. newstate->wm.need_postvbl_update = true;
  2718. return 0;
  2719. }
  2720. /*
  2721. * Merge the watermarks from all active pipes for a specific level.
  2722. */
  2723. static void ilk_merge_wm_level(struct drm_device *dev,
  2724. int level,
  2725. struct intel_wm_level *ret_wm)
  2726. {
  2727. const struct intel_crtc *intel_crtc;
  2728. ret_wm->enable = true;
  2729. for_each_intel_crtc(dev, intel_crtc) {
  2730. const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
  2731. const struct intel_wm_level *wm = &active->wm[level];
  2732. if (!active->pipe_enabled)
  2733. continue;
  2734. /*
  2735. * The watermark values may have been used in the past,
  2736. * so we must maintain them in the registers for some
  2737. * time even if the level is now disabled.
  2738. */
  2739. if (!wm->enable)
  2740. ret_wm->enable = false;
  2741. ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
  2742. ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
  2743. ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
  2744. ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
  2745. }
  2746. }
  2747. /*
  2748. * Merge all low power watermarks for all active pipes.
  2749. */
  2750. static void ilk_wm_merge(struct drm_device *dev,
  2751. const struct intel_wm_config *config,
  2752. const struct ilk_wm_maximums *max,
  2753. struct intel_pipe_wm *merged)
  2754. {
  2755. struct drm_i915_private *dev_priv = to_i915(dev);
  2756. int level, max_level = ilk_wm_max_level(dev_priv);
  2757. int last_enabled_level = max_level;
  2758. /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
  2759. if ((INTEL_GEN(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
  2760. config->num_pipes_active > 1)
  2761. last_enabled_level = 0;
  2762. /* ILK: FBC WM must be disabled always */
  2763. merged->fbc_wm_enabled = INTEL_GEN(dev_priv) >= 6;
  2764. /* merge each WM1+ level */
  2765. for (level = 1; level <= max_level; level++) {
  2766. struct intel_wm_level *wm = &merged->wm[level];
  2767. ilk_merge_wm_level(dev, level, wm);
  2768. if (level > last_enabled_level)
  2769. wm->enable = false;
  2770. else if (!ilk_validate_wm_level(level, max, wm))
  2771. /* make sure all following levels get disabled */
  2772. last_enabled_level = level - 1;
  2773. /*
  2774. * The spec says it is preferred to disable
  2775. * FBC WMs instead of disabling a WM level.
  2776. */
  2777. if (wm->fbc_val > max->fbc) {
  2778. if (wm->enable)
  2779. merged->fbc_wm_enabled = false;
  2780. wm->fbc_val = 0;
  2781. }
  2782. }
  2783. /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
  2784. /*
  2785. * FIXME this is racy. FBC might get enabled later.
  2786. * What we should check here is whether FBC can be
  2787. * enabled sometime later.
  2788. */
  2789. if (IS_GEN5(dev_priv) && !merged->fbc_wm_enabled &&
  2790. intel_fbc_is_active(dev_priv)) {
  2791. for (level = 2; level <= max_level; level++) {
  2792. struct intel_wm_level *wm = &merged->wm[level];
  2793. wm->enable = false;
  2794. }
  2795. }
  2796. }
  2797. static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
  2798. {
  2799. /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
  2800. return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
  2801. }
  2802. /* The value we need to program into the WM_LPx latency field */
  2803. static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
  2804. {
  2805. struct drm_i915_private *dev_priv = to_i915(dev);
  2806. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2807. return 2 * level;
  2808. else
  2809. return dev_priv->wm.pri_latency[level];
  2810. }
  2811. static void ilk_compute_wm_results(struct drm_device *dev,
  2812. const struct intel_pipe_wm *merged,
  2813. enum intel_ddb_partitioning partitioning,
  2814. struct ilk_wm_values *results)
  2815. {
  2816. struct drm_i915_private *dev_priv = to_i915(dev);
  2817. struct intel_crtc *intel_crtc;
  2818. int level, wm_lp;
  2819. results->enable_fbc_wm = merged->fbc_wm_enabled;
  2820. results->partitioning = partitioning;
  2821. /* LP1+ register values */
  2822. for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
  2823. const struct intel_wm_level *r;
  2824. level = ilk_wm_lp_to_level(wm_lp, merged);
  2825. r = &merged->wm[level];
  2826. /*
  2827. * Maintain the watermark values even if the level is
  2828. * disabled. Doing otherwise could cause underruns.
  2829. */
  2830. results->wm_lp[wm_lp - 1] =
  2831. (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
  2832. (r->pri_val << WM1_LP_SR_SHIFT) |
  2833. r->cur_val;
  2834. if (r->enable)
  2835. results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
  2836. if (INTEL_GEN(dev_priv) >= 8)
  2837. results->wm_lp[wm_lp - 1] |=
  2838. r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
  2839. else
  2840. results->wm_lp[wm_lp - 1] |=
  2841. r->fbc_val << WM1_LP_FBC_SHIFT;
  2842. /*
  2843. * Always set WM1S_LP_EN when spr_val != 0, even if the
  2844. * level is disabled. Doing otherwise could cause underruns.
  2845. */
  2846. if (INTEL_GEN(dev_priv) <= 6 && r->spr_val) {
  2847. WARN_ON(wm_lp != 1);
  2848. results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
  2849. } else
  2850. results->wm_lp_spr[wm_lp - 1] = r->spr_val;
  2851. }
  2852. /* LP0 register values */
  2853. for_each_intel_crtc(dev, intel_crtc) {
  2854. enum pipe pipe = intel_crtc->pipe;
  2855. const struct intel_wm_level *r =
  2856. &intel_crtc->wm.active.ilk.wm[0];
  2857. if (WARN_ON(!r->enable))
  2858. continue;
  2859. results->wm_linetime[pipe] = intel_crtc->wm.active.ilk.linetime;
  2860. results->wm_pipe[pipe] =
  2861. (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
  2862. (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
  2863. r->cur_val;
  2864. }
  2865. }
  2866. /* Find the result with the highest level enabled. Check for enable_fbc_wm in
  2867. * case both are at the same level. Prefer r1 in case they're the same. */
  2868. static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
  2869. struct intel_pipe_wm *r1,
  2870. struct intel_pipe_wm *r2)
  2871. {
  2872. int level, max_level = ilk_wm_max_level(to_i915(dev));
  2873. int level1 = 0, level2 = 0;
  2874. for (level = 1; level <= max_level; level++) {
  2875. if (r1->wm[level].enable)
  2876. level1 = level;
  2877. if (r2->wm[level].enable)
  2878. level2 = level;
  2879. }
  2880. if (level1 == level2) {
  2881. if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
  2882. return r2;
  2883. else
  2884. return r1;
  2885. } else if (level1 > level2) {
  2886. return r1;
  2887. } else {
  2888. return r2;
  2889. }
  2890. }
  2891. /* dirty bits used to track which watermarks need changes */
  2892. #define WM_DIRTY_PIPE(pipe) (1 << (pipe))
  2893. #define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
  2894. #define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
  2895. #define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
  2896. #define WM_DIRTY_FBC (1 << 24)
  2897. #define WM_DIRTY_DDB (1 << 25)
  2898. static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
  2899. const struct ilk_wm_values *old,
  2900. const struct ilk_wm_values *new)
  2901. {
  2902. unsigned int dirty = 0;
  2903. enum pipe pipe;
  2904. int wm_lp;
  2905. for_each_pipe(dev_priv, pipe) {
  2906. if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
  2907. dirty |= WM_DIRTY_LINETIME(pipe);
  2908. /* Must disable LP1+ watermarks too */
  2909. dirty |= WM_DIRTY_LP_ALL;
  2910. }
  2911. if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
  2912. dirty |= WM_DIRTY_PIPE(pipe);
  2913. /* Must disable LP1+ watermarks too */
  2914. dirty |= WM_DIRTY_LP_ALL;
  2915. }
  2916. }
  2917. if (old->enable_fbc_wm != new->enable_fbc_wm) {
  2918. dirty |= WM_DIRTY_FBC;
  2919. /* Must disable LP1+ watermarks too */
  2920. dirty |= WM_DIRTY_LP_ALL;
  2921. }
  2922. if (old->partitioning != new->partitioning) {
  2923. dirty |= WM_DIRTY_DDB;
  2924. /* Must disable LP1+ watermarks too */
  2925. dirty |= WM_DIRTY_LP_ALL;
  2926. }
  2927. /* LP1+ watermarks already deemed dirty, no need to continue */
  2928. if (dirty & WM_DIRTY_LP_ALL)
  2929. return dirty;
  2930. /* Find the lowest numbered LP1+ watermark in need of an update... */
  2931. for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
  2932. if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
  2933. old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
  2934. break;
  2935. }
  2936. /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
  2937. for (; wm_lp <= 3; wm_lp++)
  2938. dirty |= WM_DIRTY_LP(wm_lp);
  2939. return dirty;
  2940. }
  2941. static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
  2942. unsigned int dirty)
  2943. {
  2944. struct ilk_wm_values *previous = &dev_priv->wm.hw;
  2945. bool changed = false;
  2946. if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
  2947. previous->wm_lp[2] &= ~WM1_LP_SR_EN;
  2948. I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
  2949. changed = true;
  2950. }
  2951. if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
  2952. previous->wm_lp[1] &= ~WM1_LP_SR_EN;
  2953. I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
  2954. changed = true;
  2955. }
  2956. if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
  2957. previous->wm_lp[0] &= ~WM1_LP_SR_EN;
  2958. I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
  2959. changed = true;
  2960. }
  2961. /*
  2962. * Don't touch WM1S_LP_EN here.
  2963. * Doing so could cause underruns.
  2964. */
  2965. return changed;
  2966. }
  2967. /*
  2968. * The spec says we shouldn't write when we don't need, because every write
  2969. * causes WMs to be re-evaluated, expending some power.
  2970. */
  2971. static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
  2972. struct ilk_wm_values *results)
  2973. {
  2974. struct ilk_wm_values *previous = &dev_priv->wm.hw;
  2975. unsigned int dirty;
  2976. uint32_t val;
  2977. dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
  2978. if (!dirty)
  2979. return;
  2980. _ilk_disable_lp_wm(dev_priv, dirty);
  2981. if (dirty & WM_DIRTY_PIPE(PIPE_A))
  2982. I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
  2983. if (dirty & WM_DIRTY_PIPE(PIPE_B))
  2984. I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
  2985. if (dirty & WM_DIRTY_PIPE(PIPE_C))
  2986. I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
  2987. if (dirty & WM_DIRTY_LINETIME(PIPE_A))
  2988. I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
  2989. if (dirty & WM_DIRTY_LINETIME(PIPE_B))
  2990. I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
  2991. if (dirty & WM_DIRTY_LINETIME(PIPE_C))
  2992. I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
  2993. if (dirty & WM_DIRTY_DDB) {
  2994. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2995. val = I915_READ(WM_MISC);
  2996. if (results->partitioning == INTEL_DDB_PART_1_2)
  2997. val &= ~WM_MISC_DATA_PARTITION_5_6;
  2998. else
  2999. val |= WM_MISC_DATA_PARTITION_5_6;
  3000. I915_WRITE(WM_MISC, val);
  3001. } else {
  3002. val = I915_READ(DISP_ARB_CTL2);
  3003. if (results->partitioning == INTEL_DDB_PART_1_2)
  3004. val &= ~DISP_DATA_PARTITION_5_6;
  3005. else
  3006. val |= DISP_DATA_PARTITION_5_6;
  3007. I915_WRITE(DISP_ARB_CTL2, val);
  3008. }
  3009. }
  3010. if (dirty & WM_DIRTY_FBC) {
  3011. val = I915_READ(DISP_ARB_CTL);
  3012. if (results->enable_fbc_wm)
  3013. val &= ~DISP_FBC_WM_DIS;
  3014. else
  3015. val |= DISP_FBC_WM_DIS;
  3016. I915_WRITE(DISP_ARB_CTL, val);
  3017. }
  3018. if (dirty & WM_DIRTY_LP(1) &&
  3019. previous->wm_lp_spr[0] != results->wm_lp_spr[0])
  3020. I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
  3021. if (INTEL_GEN(dev_priv) >= 7) {
  3022. if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
  3023. I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
  3024. if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
  3025. I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
  3026. }
  3027. if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
  3028. I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
  3029. if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
  3030. I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
  3031. if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
  3032. I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
  3033. dev_priv->wm.hw = *results;
  3034. }
  3035. bool ilk_disable_lp_wm(struct drm_device *dev)
  3036. {
  3037. struct drm_i915_private *dev_priv = to_i915(dev);
  3038. return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
  3039. }
  3040. static u8 intel_enabled_dbuf_slices_num(struct drm_i915_private *dev_priv)
  3041. {
  3042. u8 enabled_slices;
  3043. /* Slice 1 will always be enabled */
  3044. enabled_slices = 1;
  3045. /* Gen prior to GEN11 have only one DBuf slice */
  3046. if (INTEL_GEN(dev_priv) < 11)
  3047. return enabled_slices;
  3048. if (I915_READ(DBUF_CTL_S2) & DBUF_POWER_STATE)
  3049. enabled_slices++;
  3050. return enabled_slices;
  3051. }
  3052. /*
  3053. * FIXME: We still don't have the proper code detect if we need to apply the WA,
  3054. * so assume we'll always need it in order to avoid underruns.
  3055. */
  3056. static bool skl_needs_memory_bw_wa(struct intel_atomic_state *state)
  3057. {
  3058. struct drm_i915_private *dev_priv = to_i915(state->base.dev);
  3059. if (IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv))
  3060. return true;
  3061. return false;
  3062. }
  3063. static bool
  3064. intel_has_sagv(struct drm_i915_private *dev_priv)
  3065. {
  3066. if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv) ||
  3067. IS_CANNONLAKE(dev_priv))
  3068. return true;
  3069. if (IS_SKYLAKE(dev_priv) &&
  3070. dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED)
  3071. return true;
  3072. return false;
  3073. }
  3074. /*
  3075. * SAGV dynamically adjusts the system agent voltage and clock frequencies
  3076. * depending on power and performance requirements. The display engine access
  3077. * to system memory is blocked during the adjustment time. Because of the
  3078. * blocking time, having this enabled can cause full system hangs and/or pipe
  3079. * underruns if we don't meet all of the following requirements:
  3080. *
  3081. * - <= 1 pipe enabled
  3082. * - All planes can enable watermarks for latencies >= SAGV engine block time
  3083. * - We're not using an interlaced display configuration
  3084. */
  3085. int
  3086. intel_enable_sagv(struct drm_i915_private *dev_priv)
  3087. {
  3088. int ret;
  3089. if (!intel_has_sagv(dev_priv))
  3090. return 0;
  3091. if (dev_priv->sagv_status == I915_SAGV_ENABLED)
  3092. return 0;
  3093. DRM_DEBUG_KMS("Enabling the SAGV\n");
  3094. mutex_lock(&dev_priv->pcu_lock);
  3095. ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
  3096. GEN9_SAGV_ENABLE);
  3097. /* We don't need to wait for the SAGV when enabling */
  3098. mutex_unlock(&dev_priv->pcu_lock);
  3099. /*
  3100. * Some skl systems, pre-release machines in particular,
  3101. * don't actually have an SAGV.
  3102. */
  3103. if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
  3104. DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
  3105. dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
  3106. return 0;
  3107. } else if (ret < 0) {
  3108. DRM_ERROR("Failed to enable the SAGV\n");
  3109. return ret;
  3110. }
  3111. dev_priv->sagv_status = I915_SAGV_ENABLED;
  3112. return 0;
  3113. }
  3114. int
  3115. intel_disable_sagv(struct drm_i915_private *dev_priv)
  3116. {
  3117. int ret;
  3118. if (!intel_has_sagv(dev_priv))
  3119. return 0;
  3120. if (dev_priv->sagv_status == I915_SAGV_DISABLED)
  3121. return 0;
  3122. DRM_DEBUG_KMS("Disabling the SAGV\n");
  3123. mutex_lock(&dev_priv->pcu_lock);
  3124. /* bspec says to keep retrying for at least 1 ms */
  3125. ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
  3126. GEN9_SAGV_DISABLE,
  3127. GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
  3128. 1);
  3129. mutex_unlock(&dev_priv->pcu_lock);
  3130. /*
  3131. * Some skl systems, pre-release machines in particular,
  3132. * don't actually have an SAGV.
  3133. */
  3134. if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
  3135. DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
  3136. dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
  3137. return 0;
  3138. } else if (ret < 0) {
  3139. DRM_ERROR("Failed to disable the SAGV (%d)\n", ret);
  3140. return ret;
  3141. }
  3142. dev_priv->sagv_status = I915_SAGV_DISABLED;
  3143. return 0;
  3144. }
  3145. bool intel_can_enable_sagv(struct drm_atomic_state *state)
  3146. {
  3147. struct drm_device *dev = state->dev;
  3148. struct drm_i915_private *dev_priv = to_i915(dev);
  3149. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  3150. struct intel_crtc *crtc;
  3151. struct intel_plane *plane;
  3152. struct intel_crtc_state *cstate;
  3153. enum pipe pipe;
  3154. int level, latency;
  3155. int sagv_block_time_us;
  3156. if (!intel_has_sagv(dev_priv))
  3157. return false;
  3158. if (IS_GEN9(dev_priv))
  3159. sagv_block_time_us = 30;
  3160. else if (IS_GEN10(dev_priv))
  3161. sagv_block_time_us = 20;
  3162. else
  3163. sagv_block_time_us = 10;
  3164. /*
  3165. * SKL+ workaround: bspec recommends we disable the SAGV when we have
  3166. * more then one pipe enabled
  3167. *
  3168. * If there are no active CRTCs, no additional checks need be performed
  3169. */
  3170. if (hweight32(intel_state->active_crtcs) == 0)
  3171. return true;
  3172. else if (hweight32(intel_state->active_crtcs) > 1)
  3173. return false;
  3174. /* Since we're now guaranteed to only have one active CRTC... */
  3175. pipe = ffs(intel_state->active_crtcs) - 1;
  3176. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  3177. cstate = to_intel_crtc_state(crtc->base.state);
  3178. if (crtc->base.state->adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  3179. return false;
  3180. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  3181. struct skl_plane_wm *wm =
  3182. &cstate->wm.skl.optimal.planes[plane->id];
  3183. /* Skip this plane if it's not enabled */
  3184. if (!wm->wm[0].plane_en)
  3185. continue;
  3186. /* Find the highest enabled wm level for this plane */
  3187. for (level = ilk_wm_max_level(dev_priv);
  3188. !wm->wm[level].plane_en; --level)
  3189. { }
  3190. latency = dev_priv->wm.skl_latency[level];
  3191. if (skl_needs_memory_bw_wa(intel_state) &&
  3192. plane->base.state->fb->modifier ==
  3193. I915_FORMAT_MOD_X_TILED)
  3194. latency += 15;
  3195. /*
  3196. * If any of the planes on this pipe don't enable wm levels that
  3197. * incur memory latencies higher than sagv_block_time_us we
  3198. * can't enable the SAGV.
  3199. */
  3200. if (latency < sagv_block_time_us)
  3201. return false;
  3202. }
  3203. return true;
  3204. }
  3205. static u16 intel_get_ddb_size(struct drm_i915_private *dev_priv,
  3206. const struct intel_crtc_state *cstate,
  3207. const unsigned int total_data_rate,
  3208. const int num_active,
  3209. struct skl_ddb_allocation *ddb)
  3210. {
  3211. const struct drm_display_mode *adjusted_mode;
  3212. u64 total_data_bw;
  3213. u16 ddb_size = INTEL_INFO(dev_priv)->ddb_size;
  3214. WARN_ON(ddb_size == 0);
  3215. if (INTEL_GEN(dev_priv) < 11)
  3216. return ddb_size - 4; /* 4 blocks for bypass path allocation */
  3217. adjusted_mode = &cstate->base.adjusted_mode;
  3218. total_data_bw = (u64)total_data_rate * drm_mode_vrefresh(adjusted_mode);
  3219. /*
  3220. * 12GB/s is maximum BW supported by single DBuf slice.
  3221. */
  3222. if (total_data_bw >= GBps(12) || num_active > 1) {
  3223. ddb->enabled_slices = 2;
  3224. } else {
  3225. ddb->enabled_slices = 1;
  3226. ddb_size /= 2;
  3227. }
  3228. return ddb_size;
  3229. }
  3230. static void
  3231. skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
  3232. const struct intel_crtc_state *cstate,
  3233. const unsigned int total_data_rate,
  3234. struct skl_ddb_allocation *ddb,
  3235. struct skl_ddb_entry *alloc, /* out */
  3236. int *num_active /* out */)
  3237. {
  3238. struct drm_atomic_state *state = cstate->base.state;
  3239. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  3240. struct drm_i915_private *dev_priv = to_i915(dev);
  3241. struct drm_crtc *for_crtc = cstate->base.crtc;
  3242. const struct drm_crtc_state *crtc_state;
  3243. const struct drm_crtc *crtc;
  3244. u32 pipe_width = 0, total_width = 0, width_before_pipe = 0;
  3245. enum pipe for_pipe = to_intel_crtc(for_crtc)->pipe;
  3246. u16 ddb_size;
  3247. u32 i;
  3248. if (WARN_ON(!state) || !cstate->base.active) {
  3249. alloc->start = 0;
  3250. alloc->end = 0;
  3251. *num_active = hweight32(dev_priv->active_crtcs);
  3252. return;
  3253. }
  3254. if (intel_state->active_pipe_changes)
  3255. *num_active = hweight32(intel_state->active_crtcs);
  3256. else
  3257. *num_active = hweight32(dev_priv->active_crtcs);
  3258. ddb_size = intel_get_ddb_size(dev_priv, cstate, total_data_rate,
  3259. *num_active, ddb);
  3260. /*
  3261. * If the state doesn't change the active CRTC's or there is no
  3262. * modeset request, then there's no need to recalculate;
  3263. * the existing pipe allocation limits should remain unchanged.
  3264. * Note that we're safe from racing commits since any racing commit
  3265. * that changes the active CRTC list or do modeset would need to
  3266. * grab _all_ crtc locks, including the one we currently hold.
  3267. */
  3268. if (!intel_state->active_pipe_changes && !intel_state->modeset) {
  3269. /*
  3270. * alloc may be cleared by clear_intel_crtc_state,
  3271. * copy from old state to be sure
  3272. */
  3273. *alloc = to_intel_crtc_state(for_crtc->state)->wm.skl.ddb;
  3274. return;
  3275. }
  3276. /*
  3277. * Watermark/ddb requirement highly depends upon width of the
  3278. * framebuffer, So instead of allocating DDB equally among pipes
  3279. * distribute DDB based on resolution/width of the display.
  3280. */
  3281. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  3282. const struct drm_display_mode *adjusted_mode;
  3283. int hdisplay, vdisplay;
  3284. enum pipe pipe;
  3285. if (!crtc_state->enable)
  3286. continue;
  3287. pipe = to_intel_crtc(crtc)->pipe;
  3288. adjusted_mode = &crtc_state->adjusted_mode;
  3289. drm_mode_get_hv_timing(adjusted_mode, &hdisplay, &vdisplay);
  3290. total_width += hdisplay;
  3291. if (pipe < for_pipe)
  3292. width_before_pipe += hdisplay;
  3293. else if (pipe == for_pipe)
  3294. pipe_width = hdisplay;
  3295. }
  3296. alloc->start = ddb_size * width_before_pipe / total_width;
  3297. alloc->end = ddb_size * (width_before_pipe + pipe_width) / total_width;
  3298. }
  3299. static unsigned int skl_cursor_allocation(int num_active)
  3300. {
  3301. if (num_active == 1)
  3302. return 32;
  3303. return 8;
  3304. }
  3305. static void skl_ddb_entry_init_from_hw(struct drm_i915_private *dev_priv,
  3306. struct skl_ddb_entry *entry, u32 reg)
  3307. {
  3308. u16 mask;
  3309. if (INTEL_GEN(dev_priv) >= 11)
  3310. mask = ICL_DDB_ENTRY_MASK;
  3311. else
  3312. mask = SKL_DDB_ENTRY_MASK;
  3313. entry->start = reg & mask;
  3314. entry->end = (reg >> DDB_ENTRY_END_SHIFT) & mask;
  3315. if (entry->end)
  3316. entry->end += 1;
  3317. }
  3318. static void
  3319. skl_ddb_get_hw_plane_state(struct drm_i915_private *dev_priv,
  3320. const enum pipe pipe,
  3321. const enum plane_id plane_id,
  3322. struct skl_ddb_allocation *ddb /* out */)
  3323. {
  3324. u32 val, val2 = 0;
  3325. int fourcc, pixel_format;
  3326. /* Cursor doesn't support NV12/planar, so no extra calculation needed */
  3327. if (plane_id == PLANE_CURSOR) {
  3328. val = I915_READ(CUR_BUF_CFG(pipe));
  3329. skl_ddb_entry_init_from_hw(dev_priv,
  3330. &ddb->plane[pipe][plane_id], val);
  3331. return;
  3332. }
  3333. val = I915_READ(PLANE_CTL(pipe, plane_id));
  3334. /* No DDB allocated for disabled planes */
  3335. if (!(val & PLANE_CTL_ENABLE))
  3336. return;
  3337. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  3338. fourcc = skl_format_to_fourcc(pixel_format,
  3339. val & PLANE_CTL_ORDER_RGBX,
  3340. val & PLANE_CTL_ALPHA_MASK);
  3341. val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
  3342. /*
  3343. * FIXME: add proper NV12 support for ICL. Avoid reading unclaimed
  3344. * registers for now.
  3345. */
  3346. if (INTEL_GEN(dev_priv) < 11)
  3347. val2 = I915_READ(PLANE_NV12_BUF_CFG(pipe, plane_id));
  3348. if (fourcc == DRM_FORMAT_NV12) {
  3349. skl_ddb_entry_init_from_hw(dev_priv,
  3350. &ddb->plane[pipe][plane_id], val2);
  3351. skl_ddb_entry_init_from_hw(dev_priv,
  3352. &ddb->uv_plane[pipe][plane_id], val);
  3353. } else {
  3354. skl_ddb_entry_init_from_hw(dev_priv,
  3355. &ddb->plane[pipe][plane_id], val);
  3356. }
  3357. }
  3358. void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
  3359. struct skl_ddb_allocation *ddb /* out */)
  3360. {
  3361. struct intel_crtc *crtc;
  3362. memset(ddb, 0, sizeof(*ddb));
  3363. ddb->enabled_slices = intel_enabled_dbuf_slices_num(dev_priv);
  3364. for_each_intel_crtc(&dev_priv->drm, crtc) {
  3365. enum intel_display_power_domain power_domain;
  3366. enum plane_id plane_id;
  3367. enum pipe pipe = crtc->pipe;
  3368. power_domain = POWER_DOMAIN_PIPE(pipe);
  3369. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  3370. continue;
  3371. for_each_plane_id_on_crtc(crtc, plane_id)
  3372. skl_ddb_get_hw_plane_state(dev_priv, pipe,
  3373. plane_id, ddb);
  3374. intel_display_power_put(dev_priv, power_domain);
  3375. }
  3376. }
  3377. /*
  3378. * Determines the downscale amount of a plane for the purposes of watermark calculations.
  3379. * The bspec defines downscale amount as:
  3380. *
  3381. * """
  3382. * Horizontal down scale amount = maximum[1, Horizontal source size /
  3383. * Horizontal destination size]
  3384. * Vertical down scale amount = maximum[1, Vertical source size /
  3385. * Vertical destination size]
  3386. * Total down scale amount = Horizontal down scale amount *
  3387. * Vertical down scale amount
  3388. * """
  3389. *
  3390. * Return value is provided in 16.16 fixed point form to retain fractional part.
  3391. * Caller should take care of dividing & rounding off the value.
  3392. */
  3393. static uint_fixed_16_16_t
  3394. skl_plane_downscale_amount(const struct intel_crtc_state *cstate,
  3395. const struct intel_plane_state *pstate)
  3396. {
  3397. struct intel_plane *plane = to_intel_plane(pstate->base.plane);
  3398. uint32_t src_w, src_h, dst_w, dst_h;
  3399. uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
  3400. uint_fixed_16_16_t downscale_h, downscale_w;
  3401. if (WARN_ON(!intel_wm_plane_visible(cstate, pstate)))
  3402. return u32_to_fixed16(0);
  3403. /* n.b., src is 16.16 fixed point, dst is whole integer */
  3404. if (plane->id == PLANE_CURSOR) {
  3405. /*
  3406. * Cursors only support 0/180 degree rotation,
  3407. * hence no need to account for rotation here.
  3408. */
  3409. src_w = pstate->base.src_w >> 16;
  3410. src_h = pstate->base.src_h >> 16;
  3411. dst_w = pstate->base.crtc_w;
  3412. dst_h = pstate->base.crtc_h;
  3413. } else {
  3414. /*
  3415. * Src coordinates are already rotated by 270 degrees for
  3416. * the 90/270 degree plane rotation cases (to match the
  3417. * GTT mapping), hence no need to account for rotation here.
  3418. */
  3419. src_w = drm_rect_width(&pstate->base.src) >> 16;
  3420. src_h = drm_rect_height(&pstate->base.src) >> 16;
  3421. dst_w = drm_rect_width(&pstate->base.dst);
  3422. dst_h = drm_rect_height(&pstate->base.dst);
  3423. }
  3424. fp_w_ratio = div_fixed16(src_w, dst_w);
  3425. fp_h_ratio = div_fixed16(src_h, dst_h);
  3426. downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
  3427. downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
  3428. return mul_fixed16(downscale_w, downscale_h);
  3429. }
  3430. static uint_fixed_16_16_t
  3431. skl_pipe_downscale_amount(const struct intel_crtc_state *crtc_state)
  3432. {
  3433. uint_fixed_16_16_t pipe_downscale = u32_to_fixed16(1);
  3434. if (!crtc_state->base.enable)
  3435. return pipe_downscale;
  3436. if (crtc_state->pch_pfit.enabled) {
  3437. uint32_t src_w, src_h, dst_w, dst_h;
  3438. uint32_t pfit_size = crtc_state->pch_pfit.size;
  3439. uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
  3440. uint_fixed_16_16_t downscale_h, downscale_w;
  3441. src_w = crtc_state->pipe_src_w;
  3442. src_h = crtc_state->pipe_src_h;
  3443. dst_w = pfit_size >> 16;
  3444. dst_h = pfit_size & 0xffff;
  3445. if (!dst_w || !dst_h)
  3446. return pipe_downscale;
  3447. fp_w_ratio = div_fixed16(src_w, dst_w);
  3448. fp_h_ratio = div_fixed16(src_h, dst_h);
  3449. downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
  3450. downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
  3451. pipe_downscale = mul_fixed16(downscale_w, downscale_h);
  3452. }
  3453. return pipe_downscale;
  3454. }
  3455. int skl_check_pipe_max_pixel_rate(struct intel_crtc *intel_crtc,
  3456. struct intel_crtc_state *cstate)
  3457. {
  3458. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3459. struct drm_crtc_state *crtc_state = &cstate->base;
  3460. struct drm_atomic_state *state = crtc_state->state;
  3461. struct drm_plane *plane;
  3462. const struct drm_plane_state *pstate;
  3463. struct intel_plane_state *intel_pstate;
  3464. int crtc_clock, dotclk;
  3465. uint32_t pipe_max_pixel_rate;
  3466. uint_fixed_16_16_t pipe_downscale;
  3467. uint_fixed_16_16_t max_downscale = u32_to_fixed16(1);
  3468. if (!cstate->base.enable)
  3469. return 0;
  3470. drm_atomic_crtc_state_for_each_plane_state(plane, pstate, crtc_state) {
  3471. uint_fixed_16_16_t plane_downscale;
  3472. uint_fixed_16_16_t fp_9_div_8 = div_fixed16(9, 8);
  3473. int bpp;
  3474. if (!intel_wm_plane_visible(cstate,
  3475. to_intel_plane_state(pstate)))
  3476. continue;
  3477. if (WARN_ON(!pstate->fb))
  3478. return -EINVAL;
  3479. intel_pstate = to_intel_plane_state(pstate);
  3480. plane_downscale = skl_plane_downscale_amount(cstate,
  3481. intel_pstate);
  3482. bpp = pstate->fb->format->cpp[0] * 8;
  3483. if (bpp == 64)
  3484. plane_downscale = mul_fixed16(plane_downscale,
  3485. fp_9_div_8);
  3486. max_downscale = max_fixed16(plane_downscale, max_downscale);
  3487. }
  3488. pipe_downscale = skl_pipe_downscale_amount(cstate);
  3489. pipe_downscale = mul_fixed16(pipe_downscale, max_downscale);
  3490. crtc_clock = crtc_state->adjusted_mode.crtc_clock;
  3491. dotclk = to_intel_atomic_state(state)->cdclk.logical.cdclk;
  3492. if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10)
  3493. dotclk *= 2;
  3494. pipe_max_pixel_rate = div_round_up_u32_fixed16(dotclk, pipe_downscale);
  3495. if (pipe_max_pixel_rate < crtc_clock) {
  3496. DRM_DEBUG_KMS("Max supported pixel clock with scaling exceeded\n");
  3497. return -EINVAL;
  3498. }
  3499. return 0;
  3500. }
  3501. static unsigned int
  3502. skl_plane_relative_data_rate(const struct intel_crtc_state *cstate,
  3503. const struct drm_plane_state *pstate,
  3504. const int plane)
  3505. {
  3506. struct intel_plane *intel_plane = to_intel_plane(pstate->plane);
  3507. struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
  3508. uint32_t data_rate;
  3509. uint32_t width = 0, height = 0;
  3510. struct drm_framebuffer *fb;
  3511. u32 format;
  3512. uint_fixed_16_16_t down_scale_amount;
  3513. if (!intel_pstate->base.visible)
  3514. return 0;
  3515. fb = pstate->fb;
  3516. format = fb->format->format;
  3517. if (intel_plane->id == PLANE_CURSOR)
  3518. return 0;
  3519. if (plane == 1 && format != DRM_FORMAT_NV12)
  3520. return 0;
  3521. /*
  3522. * Src coordinates are already rotated by 270 degrees for
  3523. * the 90/270 degree plane rotation cases (to match the
  3524. * GTT mapping), hence no need to account for rotation here.
  3525. */
  3526. width = drm_rect_width(&intel_pstate->base.src) >> 16;
  3527. height = drm_rect_height(&intel_pstate->base.src) >> 16;
  3528. /* UV plane does 1/2 pixel sub-sampling */
  3529. if (plane == 1 && format == DRM_FORMAT_NV12) {
  3530. width /= 2;
  3531. height /= 2;
  3532. }
  3533. data_rate = width * height * fb->format->cpp[plane];
  3534. down_scale_amount = skl_plane_downscale_amount(cstate, intel_pstate);
  3535. return mul_round_up_u32_fixed16(data_rate, down_scale_amount);
  3536. }
  3537. /*
  3538. * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
  3539. * a 8192x4096@32bpp framebuffer:
  3540. * 3 * 4096 * 8192 * 4 < 2^32
  3541. */
  3542. static unsigned int
  3543. skl_get_total_relative_data_rate(struct intel_crtc_state *intel_cstate,
  3544. unsigned int *plane_data_rate,
  3545. unsigned int *uv_plane_data_rate)
  3546. {
  3547. struct drm_crtc_state *cstate = &intel_cstate->base;
  3548. struct drm_atomic_state *state = cstate->state;
  3549. struct drm_plane *plane;
  3550. const struct drm_plane_state *pstate;
  3551. unsigned int total_data_rate = 0;
  3552. if (WARN_ON(!state))
  3553. return 0;
  3554. /* Calculate and cache data rate for each plane */
  3555. drm_atomic_crtc_state_for_each_plane_state(plane, pstate, cstate) {
  3556. enum plane_id plane_id = to_intel_plane(plane)->id;
  3557. unsigned int rate;
  3558. /* packed/y */
  3559. rate = skl_plane_relative_data_rate(intel_cstate,
  3560. pstate, 0);
  3561. plane_data_rate[plane_id] = rate;
  3562. total_data_rate += rate;
  3563. /* uv-plane */
  3564. rate = skl_plane_relative_data_rate(intel_cstate,
  3565. pstate, 1);
  3566. uv_plane_data_rate[plane_id] = rate;
  3567. total_data_rate += rate;
  3568. }
  3569. return total_data_rate;
  3570. }
  3571. static uint16_t
  3572. skl_ddb_min_alloc(const struct drm_plane_state *pstate, const int plane)
  3573. {
  3574. struct drm_framebuffer *fb = pstate->fb;
  3575. struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
  3576. uint32_t src_w, src_h;
  3577. uint32_t min_scanlines = 8;
  3578. uint8_t plane_bpp;
  3579. if (WARN_ON(!fb))
  3580. return 0;
  3581. /* For packed formats, and uv-plane, return 0 */
  3582. if (plane == 1 && fb->format->format != DRM_FORMAT_NV12)
  3583. return 0;
  3584. /* For Non Y-tile return 8-blocks */
  3585. if (fb->modifier != I915_FORMAT_MOD_Y_TILED &&
  3586. fb->modifier != I915_FORMAT_MOD_Yf_TILED &&
  3587. fb->modifier != I915_FORMAT_MOD_Y_TILED_CCS &&
  3588. fb->modifier != I915_FORMAT_MOD_Yf_TILED_CCS)
  3589. return 8;
  3590. /*
  3591. * Src coordinates are already rotated by 270 degrees for
  3592. * the 90/270 degree plane rotation cases (to match the
  3593. * GTT mapping), hence no need to account for rotation here.
  3594. */
  3595. src_w = drm_rect_width(&intel_pstate->base.src) >> 16;
  3596. src_h = drm_rect_height(&intel_pstate->base.src) >> 16;
  3597. /* Halve UV plane width and height for NV12 */
  3598. if (plane == 1) {
  3599. src_w /= 2;
  3600. src_h /= 2;
  3601. }
  3602. plane_bpp = fb->format->cpp[plane];
  3603. if (drm_rotation_90_or_270(pstate->rotation)) {
  3604. switch (plane_bpp) {
  3605. case 1:
  3606. min_scanlines = 32;
  3607. break;
  3608. case 2:
  3609. min_scanlines = 16;
  3610. break;
  3611. case 4:
  3612. min_scanlines = 8;
  3613. break;
  3614. case 8:
  3615. min_scanlines = 4;
  3616. break;
  3617. default:
  3618. WARN(1, "Unsupported pixel depth %u for rotation",
  3619. plane_bpp);
  3620. min_scanlines = 32;
  3621. }
  3622. }
  3623. return DIV_ROUND_UP((4 * src_w * plane_bpp), 512) * min_scanlines/4 + 3;
  3624. }
  3625. static void
  3626. skl_ddb_calc_min(const struct intel_crtc_state *cstate, int num_active,
  3627. uint16_t *minimum, uint16_t *uv_minimum)
  3628. {
  3629. const struct drm_plane_state *pstate;
  3630. struct drm_plane *plane;
  3631. drm_atomic_crtc_state_for_each_plane_state(plane, pstate, &cstate->base) {
  3632. enum plane_id plane_id = to_intel_plane(plane)->id;
  3633. if (plane_id == PLANE_CURSOR)
  3634. continue;
  3635. if (!pstate->visible)
  3636. continue;
  3637. minimum[plane_id] = skl_ddb_min_alloc(pstate, 0);
  3638. uv_minimum[plane_id] = skl_ddb_min_alloc(pstate, 1);
  3639. }
  3640. minimum[PLANE_CURSOR] = skl_cursor_allocation(num_active);
  3641. }
  3642. static int
  3643. skl_allocate_pipe_ddb(struct intel_crtc_state *cstate,
  3644. struct skl_ddb_allocation *ddb /* out */)
  3645. {
  3646. struct drm_atomic_state *state = cstate->base.state;
  3647. struct drm_crtc *crtc = cstate->base.crtc;
  3648. struct drm_device *dev = crtc->dev;
  3649. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3650. enum pipe pipe = intel_crtc->pipe;
  3651. struct skl_ddb_entry *alloc = &cstate->wm.skl.ddb;
  3652. uint16_t alloc_size, start;
  3653. uint16_t minimum[I915_MAX_PLANES] = {};
  3654. uint16_t uv_minimum[I915_MAX_PLANES] = {};
  3655. unsigned int total_data_rate;
  3656. enum plane_id plane_id;
  3657. int num_active;
  3658. unsigned int plane_data_rate[I915_MAX_PLANES] = {};
  3659. unsigned int uv_plane_data_rate[I915_MAX_PLANES] = {};
  3660. uint16_t total_min_blocks = 0;
  3661. /* Clear the partitioning for disabled planes. */
  3662. memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
  3663. memset(ddb->uv_plane[pipe], 0, sizeof(ddb->uv_plane[pipe]));
  3664. if (WARN_ON(!state))
  3665. return 0;
  3666. if (!cstate->base.active) {
  3667. alloc->start = alloc->end = 0;
  3668. return 0;
  3669. }
  3670. total_data_rate = skl_get_total_relative_data_rate(cstate,
  3671. plane_data_rate,
  3672. uv_plane_data_rate);
  3673. skl_ddb_get_pipe_allocation_limits(dev, cstate, total_data_rate, ddb,
  3674. alloc, &num_active);
  3675. alloc_size = skl_ddb_entry_size(alloc);
  3676. if (alloc_size == 0)
  3677. return 0;
  3678. skl_ddb_calc_min(cstate, num_active, minimum, uv_minimum);
  3679. /*
  3680. * 1. Allocate the mininum required blocks for each active plane
  3681. * and allocate the cursor, it doesn't require extra allocation
  3682. * proportional to the data rate.
  3683. */
  3684. for_each_plane_id_on_crtc(intel_crtc, plane_id) {
  3685. total_min_blocks += minimum[plane_id];
  3686. total_min_blocks += uv_minimum[plane_id];
  3687. }
  3688. if (total_min_blocks > alloc_size) {
  3689. DRM_DEBUG_KMS("Requested display configuration exceeds system DDB limitations");
  3690. DRM_DEBUG_KMS("minimum required %d/%d\n", total_min_blocks,
  3691. alloc_size);
  3692. return -EINVAL;
  3693. }
  3694. alloc_size -= total_min_blocks;
  3695. ddb->plane[pipe][PLANE_CURSOR].start = alloc->end - minimum[PLANE_CURSOR];
  3696. ddb->plane[pipe][PLANE_CURSOR].end = alloc->end;
  3697. /*
  3698. * 2. Distribute the remaining space in proportion to the amount of
  3699. * data each plane needs to fetch from memory.
  3700. *
  3701. * FIXME: we may not allocate every single block here.
  3702. */
  3703. if (total_data_rate == 0)
  3704. return 0;
  3705. start = alloc->start;
  3706. for_each_plane_id_on_crtc(intel_crtc, plane_id) {
  3707. unsigned int data_rate, uv_data_rate;
  3708. uint16_t plane_blocks, uv_plane_blocks;
  3709. if (plane_id == PLANE_CURSOR)
  3710. continue;
  3711. data_rate = plane_data_rate[plane_id];
  3712. /*
  3713. * allocation for (packed formats) or (uv-plane part of planar format):
  3714. * promote the expression to 64 bits to avoid overflowing, the
  3715. * result is < available as data_rate / total_data_rate < 1
  3716. */
  3717. plane_blocks = minimum[plane_id];
  3718. plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
  3719. total_data_rate);
  3720. /* Leave disabled planes at (0,0) */
  3721. if (data_rate) {
  3722. ddb->plane[pipe][plane_id].start = start;
  3723. ddb->plane[pipe][plane_id].end = start + plane_blocks;
  3724. }
  3725. start += plane_blocks;
  3726. /* Allocate DDB for UV plane for planar format/NV12 */
  3727. uv_data_rate = uv_plane_data_rate[plane_id];
  3728. uv_plane_blocks = uv_minimum[plane_id];
  3729. uv_plane_blocks += div_u64((uint64_t)alloc_size * uv_data_rate,
  3730. total_data_rate);
  3731. if (uv_data_rate) {
  3732. ddb->uv_plane[pipe][plane_id].start = start;
  3733. ddb->uv_plane[pipe][plane_id].end =
  3734. start + uv_plane_blocks;
  3735. }
  3736. start += uv_plane_blocks;
  3737. }
  3738. return 0;
  3739. }
  3740. /*
  3741. * The max latency should be 257 (max the punit can code is 255 and we add 2us
  3742. * for the read latency) and cpp should always be <= 8, so that
  3743. * should allow pixel_rate up to ~2 GHz which seems sufficient since max
  3744. * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
  3745. */
  3746. static uint_fixed_16_16_t
  3747. skl_wm_method1(const struct drm_i915_private *dev_priv, uint32_t pixel_rate,
  3748. uint8_t cpp, uint32_t latency, uint32_t dbuf_block_size)
  3749. {
  3750. uint32_t wm_intermediate_val;
  3751. uint_fixed_16_16_t ret;
  3752. if (latency == 0)
  3753. return FP_16_16_MAX;
  3754. wm_intermediate_val = latency * pixel_rate * cpp;
  3755. ret = div_fixed16(wm_intermediate_val, 1000 * dbuf_block_size);
  3756. if (INTEL_GEN(dev_priv) >= 10)
  3757. ret = add_fixed16_u32(ret, 1);
  3758. return ret;
  3759. }
  3760. static uint_fixed_16_16_t skl_wm_method2(uint32_t pixel_rate,
  3761. uint32_t pipe_htotal,
  3762. uint32_t latency,
  3763. uint_fixed_16_16_t plane_blocks_per_line)
  3764. {
  3765. uint32_t wm_intermediate_val;
  3766. uint_fixed_16_16_t ret;
  3767. if (latency == 0)
  3768. return FP_16_16_MAX;
  3769. wm_intermediate_val = latency * pixel_rate;
  3770. wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
  3771. pipe_htotal * 1000);
  3772. ret = mul_u32_fixed16(wm_intermediate_val, plane_blocks_per_line);
  3773. return ret;
  3774. }
  3775. static uint_fixed_16_16_t
  3776. intel_get_linetime_us(struct intel_crtc_state *cstate)
  3777. {
  3778. uint32_t pixel_rate;
  3779. uint32_t crtc_htotal;
  3780. uint_fixed_16_16_t linetime_us;
  3781. if (!cstate->base.active)
  3782. return u32_to_fixed16(0);
  3783. pixel_rate = cstate->pixel_rate;
  3784. if (WARN_ON(pixel_rate == 0))
  3785. return u32_to_fixed16(0);
  3786. crtc_htotal = cstate->base.adjusted_mode.crtc_htotal;
  3787. linetime_us = div_fixed16(crtc_htotal * 1000, pixel_rate);
  3788. return linetime_us;
  3789. }
  3790. static uint32_t
  3791. skl_adjusted_plane_pixel_rate(const struct intel_crtc_state *cstate,
  3792. const struct intel_plane_state *pstate)
  3793. {
  3794. uint64_t adjusted_pixel_rate;
  3795. uint_fixed_16_16_t downscale_amount;
  3796. /* Shouldn't reach here on disabled planes... */
  3797. if (WARN_ON(!intel_wm_plane_visible(cstate, pstate)))
  3798. return 0;
  3799. /*
  3800. * Adjusted plane pixel rate is just the pipe's adjusted pixel rate
  3801. * with additional adjustments for plane-specific scaling.
  3802. */
  3803. adjusted_pixel_rate = cstate->pixel_rate;
  3804. downscale_amount = skl_plane_downscale_amount(cstate, pstate);
  3805. return mul_round_up_u32_fixed16(adjusted_pixel_rate,
  3806. downscale_amount);
  3807. }
  3808. static int
  3809. skl_compute_plane_wm_params(const struct drm_i915_private *dev_priv,
  3810. struct intel_crtc_state *cstate,
  3811. const struct intel_plane_state *intel_pstate,
  3812. struct skl_wm_params *wp, int plane_id)
  3813. {
  3814. struct intel_plane *plane = to_intel_plane(intel_pstate->base.plane);
  3815. const struct drm_plane_state *pstate = &intel_pstate->base;
  3816. const struct drm_framebuffer *fb = pstate->fb;
  3817. uint32_t interm_pbpl;
  3818. struct intel_atomic_state *state =
  3819. to_intel_atomic_state(cstate->base.state);
  3820. bool apply_memory_bw_wa = skl_needs_memory_bw_wa(state);
  3821. if (!intel_wm_plane_visible(cstate, intel_pstate))
  3822. return 0;
  3823. /* only NV12 format has two planes */
  3824. if (plane_id == 1 && fb->format->format != DRM_FORMAT_NV12) {
  3825. DRM_DEBUG_KMS("Non NV12 format have single plane\n");
  3826. return -EINVAL;
  3827. }
  3828. wp->y_tiled = fb->modifier == I915_FORMAT_MOD_Y_TILED ||
  3829. fb->modifier == I915_FORMAT_MOD_Yf_TILED ||
  3830. fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  3831. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
  3832. wp->x_tiled = fb->modifier == I915_FORMAT_MOD_X_TILED;
  3833. wp->rc_surface = fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  3834. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
  3835. wp->is_planar = fb->format->format == DRM_FORMAT_NV12;
  3836. if (plane->id == PLANE_CURSOR) {
  3837. wp->width = intel_pstate->base.crtc_w;
  3838. } else {
  3839. /*
  3840. * Src coordinates are already rotated by 270 degrees for
  3841. * the 90/270 degree plane rotation cases (to match the
  3842. * GTT mapping), hence no need to account for rotation here.
  3843. */
  3844. wp->width = drm_rect_width(&intel_pstate->base.src) >> 16;
  3845. }
  3846. if (plane_id == 1 && wp->is_planar)
  3847. wp->width /= 2;
  3848. wp->cpp = fb->format->cpp[plane_id];
  3849. wp->plane_pixel_rate = skl_adjusted_plane_pixel_rate(cstate,
  3850. intel_pstate);
  3851. if (INTEL_GEN(dev_priv) >= 11 &&
  3852. fb->modifier == I915_FORMAT_MOD_Yf_TILED && wp->cpp == 8)
  3853. wp->dbuf_block_size = 256;
  3854. else
  3855. wp->dbuf_block_size = 512;
  3856. if (drm_rotation_90_or_270(pstate->rotation)) {
  3857. switch (wp->cpp) {
  3858. case 1:
  3859. wp->y_min_scanlines = 16;
  3860. break;
  3861. case 2:
  3862. wp->y_min_scanlines = 8;
  3863. break;
  3864. case 4:
  3865. wp->y_min_scanlines = 4;
  3866. break;
  3867. default:
  3868. MISSING_CASE(wp->cpp);
  3869. return -EINVAL;
  3870. }
  3871. } else {
  3872. wp->y_min_scanlines = 4;
  3873. }
  3874. if (apply_memory_bw_wa)
  3875. wp->y_min_scanlines *= 2;
  3876. wp->plane_bytes_per_line = wp->width * wp->cpp;
  3877. if (wp->y_tiled) {
  3878. interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line *
  3879. wp->y_min_scanlines,
  3880. wp->dbuf_block_size);
  3881. if (INTEL_GEN(dev_priv) >= 10)
  3882. interm_pbpl++;
  3883. wp->plane_blocks_per_line = div_fixed16(interm_pbpl,
  3884. wp->y_min_scanlines);
  3885. } else if (wp->x_tiled && IS_GEN9(dev_priv)) {
  3886. interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
  3887. wp->dbuf_block_size);
  3888. wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
  3889. } else {
  3890. interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
  3891. wp->dbuf_block_size) + 1;
  3892. wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
  3893. }
  3894. wp->y_tile_minimum = mul_u32_fixed16(wp->y_min_scanlines,
  3895. wp->plane_blocks_per_line);
  3896. wp->linetime_us = fixed16_to_u32_round_up(
  3897. intel_get_linetime_us(cstate));
  3898. return 0;
  3899. }
  3900. static int skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
  3901. struct intel_crtc_state *cstate,
  3902. const struct intel_plane_state *intel_pstate,
  3903. uint16_t ddb_allocation,
  3904. int level,
  3905. const struct skl_wm_params *wp,
  3906. const struct skl_wm_level *result_prev,
  3907. struct skl_wm_level *result /* out */)
  3908. {
  3909. const struct drm_plane_state *pstate = &intel_pstate->base;
  3910. uint32_t latency = dev_priv->wm.skl_latency[level];
  3911. uint_fixed_16_16_t method1, method2;
  3912. uint_fixed_16_16_t selected_result;
  3913. uint32_t res_blocks, res_lines;
  3914. struct intel_atomic_state *state =
  3915. to_intel_atomic_state(cstate->base.state);
  3916. bool apply_memory_bw_wa = skl_needs_memory_bw_wa(state);
  3917. uint32_t min_disp_buf_needed;
  3918. if (latency == 0 ||
  3919. !intel_wm_plane_visible(cstate, intel_pstate)) {
  3920. result->plane_en = false;
  3921. return 0;
  3922. }
  3923. /* Display WA #1141: kbl,cfl */
  3924. if ((IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv) ||
  3925. IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_B0)) &&
  3926. dev_priv->ipc_enabled)
  3927. latency += 4;
  3928. if (apply_memory_bw_wa && wp->x_tiled)
  3929. latency += 15;
  3930. method1 = skl_wm_method1(dev_priv, wp->plane_pixel_rate,
  3931. wp->cpp, latency, wp->dbuf_block_size);
  3932. method2 = skl_wm_method2(wp->plane_pixel_rate,
  3933. cstate->base.adjusted_mode.crtc_htotal,
  3934. latency,
  3935. wp->plane_blocks_per_line);
  3936. if (wp->y_tiled) {
  3937. selected_result = max_fixed16(method2, wp->y_tile_minimum);
  3938. } else {
  3939. if ((wp->cpp * cstate->base.adjusted_mode.crtc_htotal /
  3940. wp->dbuf_block_size < 1) &&
  3941. (wp->plane_bytes_per_line / wp->dbuf_block_size < 1))
  3942. selected_result = method2;
  3943. else if (ddb_allocation >=
  3944. fixed16_to_u32_round_up(wp->plane_blocks_per_line))
  3945. selected_result = min_fixed16(method1, method2);
  3946. else if (latency >= wp->linetime_us)
  3947. selected_result = min_fixed16(method1, method2);
  3948. else
  3949. selected_result = method1;
  3950. }
  3951. res_blocks = fixed16_to_u32_round_up(selected_result) + 1;
  3952. res_lines = div_round_up_fixed16(selected_result,
  3953. wp->plane_blocks_per_line);
  3954. /* Display WA #1125: skl,bxt,kbl,glk */
  3955. if (level == 0 && wp->rc_surface)
  3956. res_blocks += fixed16_to_u32_round_up(wp->y_tile_minimum);
  3957. /* Display WA #1126: skl,bxt,kbl,glk */
  3958. if (level >= 1 && level <= 7) {
  3959. if (wp->y_tiled) {
  3960. res_blocks += fixed16_to_u32_round_up(
  3961. wp->y_tile_minimum);
  3962. res_lines += wp->y_min_scanlines;
  3963. } else {
  3964. res_blocks++;
  3965. }
  3966. /*
  3967. * Make sure result blocks for higher latency levels are atleast
  3968. * as high as level below the current level.
  3969. * Assumption in DDB algorithm optimization for special cases.
  3970. * Also covers Display WA #1125 for RC.
  3971. */
  3972. if (result_prev->plane_res_b > res_blocks)
  3973. res_blocks = result_prev->plane_res_b;
  3974. }
  3975. if (INTEL_GEN(dev_priv) >= 11) {
  3976. if (wp->y_tiled) {
  3977. uint32_t extra_lines;
  3978. uint_fixed_16_16_t fp_min_disp_buf_needed;
  3979. if (res_lines % wp->y_min_scanlines == 0)
  3980. extra_lines = wp->y_min_scanlines;
  3981. else
  3982. extra_lines = wp->y_min_scanlines * 2 -
  3983. res_lines % wp->y_min_scanlines;
  3984. fp_min_disp_buf_needed = mul_u32_fixed16(res_lines +
  3985. extra_lines,
  3986. wp->plane_blocks_per_line);
  3987. min_disp_buf_needed = fixed16_to_u32_round_up(
  3988. fp_min_disp_buf_needed);
  3989. } else {
  3990. min_disp_buf_needed = DIV_ROUND_UP(res_blocks * 11, 10);
  3991. }
  3992. } else {
  3993. min_disp_buf_needed = res_blocks;
  3994. }
  3995. if ((level > 0 && res_lines > 31) ||
  3996. res_blocks >= ddb_allocation ||
  3997. min_disp_buf_needed >= ddb_allocation) {
  3998. result->plane_en = false;
  3999. /*
  4000. * If there are no valid level 0 watermarks, then we can't
  4001. * support this display configuration.
  4002. */
  4003. if (level) {
  4004. return 0;
  4005. } else {
  4006. struct drm_plane *plane = pstate->plane;
  4007. DRM_DEBUG_KMS("Requested display configuration exceeds system watermark limitations\n");
  4008. DRM_DEBUG_KMS("[PLANE:%d:%s] blocks required = %u/%u, lines required = %u/31\n",
  4009. plane->base.id, plane->name,
  4010. res_blocks, ddb_allocation, res_lines);
  4011. return -EINVAL;
  4012. }
  4013. }
  4014. /*
  4015. * Display WA #826 (SKL:ALL, BXT:ALL) & #1059 (CNL:A)
  4016. * disable wm level 1-7 on NV12 planes
  4017. */
  4018. if (wp->is_planar && level >= 1 &&
  4019. (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv) ||
  4020. IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_A0))) {
  4021. result->plane_en = false;
  4022. return 0;
  4023. }
  4024. /* The number of lines are ignored for the level 0 watermark. */
  4025. result->plane_res_b = res_blocks;
  4026. result->plane_res_l = res_lines;
  4027. result->plane_en = true;
  4028. return 0;
  4029. }
  4030. static int
  4031. skl_compute_wm_levels(const struct drm_i915_private *dev_priv,
  4032. struct skl_ddb_allocation *ddb,
  4033. struct intel_crtc_state *cstate,
  4034. const struct intel_plane_state *intel_pstate,
  4035. const struct skl_wm_params *wm_params,
  4036. struct skl_plane_wm *wm,
  4037. int plane_id)
  4038. {
  4039. struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
  4040. struct drm_plane *plane = intel_pstate->base.plane;
  4041. struct intel_plane *intel_plane = to_intel_plane(plane);
  4042. uint16_t ddb_blocks;
  4043. enum pipe pipe = intel_crtc->pipe;
  4044. int level, max_level = ilk_wm_max_level(dev_priv);
  4045. enum plane_id intel_plane_id = intel_plane->id;
  4046. int ret;
  4047. if (WARN_ON(!intel_pstate->base.fb))
  4048. return -EINVAL;
  4049. ddb_blocks = plane_id ?
  4050. skl_ddb_entry_size(&ddb->uv_plane[pipe][intel_plane_id]) :
  4051. skl_ddb_entry_size(&ddb->plane[pipe][intel_plane_id]);
  4052. for (level = 0; level <= max_level; level++) {
  4053. struct skl_wm_level *result = plane_id ? &wm->uv_wm[level] :
  4054. &wm->wm[level];
  4055. struct skl_wm_level *result_prev;
  4056. if (level)
  4057. result_prev = plane_id ? &wm->uv_wm[level - 1] :
  4058. &wm->wm[level - 1];
  4059. else
  4060. result_prev = plane_id ? &wm->uv_wm[0] : &wm->wm[0];
  4061. ret = skl_compute_plane_wm(dev_priv,
  4062. cstate,
  4063. intel_pstate,
  4064. ddb_blocks,
  4065. level,
  4066. wm_params,
  4067. result_prev,
  4068. result);
  4069. if (ret)
  4070. return ret;
  4071. }
  4072. if (intel_pstate->base.fb->format->format == DRM_FORMAT_NV12)
  4073. wm->is_planar = true;
  4074. return 0;
  4075. }
  4076. static uint32_t
  4077. skl_compute_linetime_wm(struct intel_crtc_state *cstate)
  4078. {
  4079. struct drm_atomic_state *state = cstate->base.state;
  4080. struct drm_i915_private *dev_priv = to_i915(state->dev);
  4081. uint_fixed_16_16_t linetime_us;
  4082. uint32_t linetime_wm;
  4083. linetime_us = intel_get_linetime_us(cstate);
  4084. if (is_fixed16_zero(linetime_us))
  4085. return 0;
  4086. linetime_wm = fixed16_to_u32_round_up(mul_u32_fixed16(8, linetime_us));
  4087. /* Display WA #1135: bxt:ALL GLK:ALL */
  4088. if ((IS_BROXTON(dev_priv) || IS_GEMINILAKE(dev_priv)) &&
  4089. dev_priv->ipc_enabled)
  4090. linetime_wm /= 2;
  4091. return linetime_wm;
  4092. }
  4093. static void skl_compute_transition_wm(struct intel_crtc_state *cstate,
  4094. struct skl_wm_params *wp,
  4095. struct skl_wm_level *wm_l0,
  4096. uint16_t ddb_allocation,
  4097. struct skl_wm_level *trans_wm /* out */)
  4098. {
  4099. struct drm_device *dev = cstate->base.crtc->dev;
  4100. const struct drm_i915_private *dev_priv = to_i915(dev);
  4101. uint16_t trans_min, trans_y_tile_min;
  4102. const uint16_t trans_amount = 10; /* This is configurable amount */
  4103. uint16_t trans_offset_b, res_blocks;
  4104. if (!cstate->base.active)
  4105. goto exit;
  4106. /* Transition WM are not recommended by HW team for GEN9 */
  4107. if (INTEL_GEN(dev_priv) <= 9)
  4108. goto exit;
  4109. /* Transition WM don't make any sense if ipc is disabled */
  4110. if (!dev_priv->ipc_enabled)
  4111. goto exit;
  4112. trans_min = 0;
  4113. if (INTEL_GEN(dev_priv) >= 10)
  4114. trans_min = 4;
  4115. trans_offset_b = trans_min + trans_amount;
  4116. if (wp->y_tiled) {
  4117. trans_y_tile_min = (uint16_t) mul_round_up_u32_fixed16(2,
  4118. wp->y_tile_minimum);
  4119. res_blocks = max(wm_l0->plane_res_b, trans_y_tile_min) +
  4120. trans_offset_b;
  4121. } else {
  4122. res_blocks = wm_l0->plane_res_b + trans_offset_b;
  4123. /* WA BUG:1938466 add one block for non y-tile planes */
  4124. if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_A0))
  4125. res_blocks += 1;
  4126. }
  4127. res_blocks += 1;
  4128. if (res_blocks < ddb_allocation) {
  4129. trans_wm->plane_res_b = res_blocks;
  4130. trans_wm->plane_en = true;
  4131. return;
  4132. }
  4133. exit:
  4134. trans_wm->plane_en = false;
  4135. }
  4136. static int skl_build_pipe_wm(struct intel_crtc_state *cstate,
  4137. struct skl_ddb_allocation *ddb,
  4138. struct skl_pipe_wm *pipe_wm)
  4139. {
  4140. struct drm_device *dev = cstate->base.crtc->dev;
  4141. struct drm_crtc_state *crtc_state = &cstate->base;
  4142. const struct drm_i915_private *dev_priv = to_i915(dev);
  4143. struct drm_plane *plane;
  4144. const struct drm_plane_state *pstate;
  4145. struct skl_plane_wm *wm;
  4146. int ret;
  4147. /*
  4148. * We'll only calculate watermarks for planes that are actually
  4149. * enabled, so make sure all other planes are set as disabled.
  4150. */
  4151. memset(pipe_wm->planes, 0, sizeof(pipe_wm->planes));
  4152. drm_atomic_crtc_state_for_each_plane_state(plane, pstate, crtc_state) {
  4153. const struct intel_plane_state *intel_pstate =
  4154. to_intel_plane_state(pstate);
  4155. enum plane_id plane_id = to_intel_plane(plane)->id;
  4156. struct skl_wm_params wm_params;
  4157. enum pipe pipe = to_intel_crtc(cstate->base.crtc)->pipe;
  4158. uint16_t ddb_blocks;
  4159. wm = &pipe_wm->planes[plane_id];
  4160. ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][plane_id]);
  4161. ret = skl_compute_plane_wm_params(dev_priv, cstate,
  4162. intel_pstate, &wm_params, 0);
  4163. if (ret)
  4164. return ret;
  4165. ret = skl_compute_wm_levels(dev_priv, ddb, cstate,
  4166. intel_pstate, &wm_params, wm, 0);
  4167. if (ret)
  4168. return ret;
  4169. skl_compute_transition_wm(cstate, &wm_params, &wm->wm[0],
  4170. ddb_blocks, &wm->trans_wm);
  4171. /* uv plane watermarks must also be validated for NV12/Planar */
  4172. if (wm_params.is_planar) {
  4173. memset(&wm_params, 0, sizeof(struct skl_wm_params));
  4174. wm->is_planar = true;
  4175. ret = skl_compute_plane_wm_params(dev_priv, cstate,
  4176. intel_pstate,
  4177. &wm_params, 1);
  4178. if (ret)
  4179. return ret;
  4180. ret = skl_compute_wm_levels(dev_priv, ddb, cstate,
  4181. intel_pstate, &wm_params,
  4182. wm, 1);
  4183. if (ret)
  4184. return ret;
  4185. }
  4186. }
  4187. pipe_wm->linetime = skl_compute_linetime_wm(cstate);
  4188. return 0;
  4189. }
  4190. static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
  4191. i915_reg_t reg,
  4192. const struct skl_ddb_entry *entry)
  4193. {
  4194. if (entry->end)
  4195. I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
  4196. else
  4197. I915_WRITE(reg, 0);
  4198. }
  4199. static void skl_write_wm_level(struct drm_i915_private *dev_priv,
  4200. i915_reg_t reg,
  4201. const struct skl_wm_level *level)
  4202. {
  4203. uint32_t val = 0;
  4204. if (level->plane_en) {
  4205. val |= PLANE_WM_EN;
  4206. val |= level->plane_res_b;
  4207. val |= level->plane_res_l << PLANE_WM_LINES_SHIFT;
  4208. }
  4209. I915_WRITE(reg, val);
  4210. }
  4211. static void skl_write_plane_wm(struct intel_crtc *intel_crtc,
  4212. const struct skl_plane_wm *wm,
  4213. const struct skl_ddb_allocation *ddb,
  4214. enum plane_id plane_id)
  4215. {
  4216. struct drm_crtc *crtc = &intel_crtc->base;
  4217. struct drm_device *dev = crtc->dev;
  4218. struct drm_i915_private *dev_priv = to_i915(dev);
  4219. int level, max_level = ilk_wm_max_level(dev_priv);
  4220. enum pipe pipe = intel_crtc->pipe;
  4221. for (level = 0; level <= max_level; level++) {
  4222. skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
  4223. &wm->wm[level]);
  4224. }
  4225. skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
  4226. &wm->trans_wm);
  4227. skl_ddb_entry_write(dev_priv, PLANE_BUF_CFG(pipe, plane_id),
  4228. &ddb->plane[pipe][plane_id]);
  4229. /* FIXME: add proper NV12 support for ICL. */
  4230. if (INTEL_GEN(dev_priv) >= 11)
  4231. return skl_ddb_entry_write(dev_priv,
  4232. PLANE_BUF_CFG(pipe, plane_id),
  4233. &ddb->plane[pipe][plane_id]);
  4234. if (wm->is_planar) {
  4235. skl_ddb_entry_write(dev_priv, PLANE_BUF_CFG(pipe, plane_id),
  4236. &ddb->uv_plane[pipe][plane_id]);
  4237. skl_ddb_entry_write(dev_priv,
  4238. PLANE_NV12_BUF_CFG(pipe, plane_id),
  4239. &ddb->plane[pipe][plane_id]);
  4240. } else {
  4241. skl_ddb_entry_write(dev_priv, PLANE_BUF_CFG(pipe, plane_id),
  4242. &ddb->plane[pipe][plane_id]);
  4243. I915_WRITE(PLANE_NV12_BUF_CFG(pipe, plane_id), 0x0);
  4244. }
  4245. }
  4246. static void skl_write_cursor_wm(struct intel_crtc *intel_crtc,
  4247. const struct skl_plane_wm *wm,
  4248. const struct skl_ddb_allocation *ddb)
  4249. {
  4250. struct drm_crtc *crtc = &intel_crtc->base;
  4251. struct drm_device *dev = crtc->dev;
  4252. struct drm_i915_private *dev_priv = to_i915(dev);
  4253. int level, max_level = ilk_wm_max_level(dev_priv);
  4254. enum pipe pipe = intel_crtc->pipe;
  4255. for (level = 0; level <= max_level; level++) {
  4256. skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
  4257. &wm->wm[level]);
  4258. }
  4259. skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe), &wm->trans_wm);
  4260. skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
  4261. &ddb->plane[pipe][PLANE_CURSOR]);
  4262. }
  4263. bool skl_wm_level_equals(const struct skl_wm_level *l1,
  4264. const struct skl_wm_level *l2)
  4265. {
  4266. if (l1->plane_en != l2->plane_en)
  4267. return false;
  4268. /* If both planes aren't enabled, the rest shouldn't matter */
  4269. if (!l1->plane_en)
  4270. return true;
  4271. return (l1->plane_res_l == l2->plane_res_l &&
  4272. l1->plane_res_b == l2->plane_res_b);
  4273. }
  4274. static inline bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
  4275. const struct skl_ddb_entry *b)
  4276. {
  4277. return a->start < b->end && b->start < a->end;
  4278. }
  4279. bool skl_ddb_allocation_overlaps(struct drm_i915_private *dev_priv,
  4280. const struct skl_ddb_entry **entries,
  4281. const struct skl_ddb_entry *ddb,
  4282. int ignore)
  4283. {
  4284. enum pipe pipe;
  4285. for_each_pipe(dev_priv, pipe) {
  4286. if (pipe != ignore && entries[pipe] &&
  4287. skl_ddb_entries_overlap(ddb, entries[pipe]))
  4288. return true;
  4289. }
  4290. return false;
  4291. }
  4292. static int skl_update_pipe_wm(struct drm_crtc_state *cstate,
  4293. const struct skl_pipe_wm *old_pipe_wm,
  4294. struct skl_pipe_wm *pipe_wm, /* out */
  4295. struct skl_ddb_allocation *ddb, /* out */
  4296. bool *changed /* out */)
  4297. {
  4298. struct intel_crtc_state *intel_cstate = to_intel_crtc_state(cstate);
  4299. int ret;
  4300. ret = skl_build_pipe_wm(intel_cstate, ddb, pipe_wm);
  4301. if (ret)
  4302. return ret;
  4303. if (!memcmp(old_pipe_wm, pipe_wm, sizeof(*pipe_wm)))
  4304. *changed = false;
  4305. else
  4306. *changed = true;
  4307. return 0;
  4308. }
  4309. static uint32_t
  4310. pipes_modified(struct drm_atomic_state *state)
  4311. {
  4312. struct drm_crtc *crtc;
  4313. struct drm_crtc_state *cstate;
  4314. uint32_t i, ret = 0;
  4315. for_each_new_crtc_in_state(state, crtc, cstate, i)
  4316. ret |= drm_crtc_mask(crtc);
  4317. return ret;
  4318. }
  4319. static int
  4320. skl_ddb_add_affected_planes(struct intel_crtc_state *cstate)
  4321. {
  4322. struct drm_atomic_state *state = cstate->base.state;
  4323. struct drm_device *dev = state->dev;
  4324. struct drm_crtc *crtc = cstate->base.crtc;
  4325. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4326. struct drm_i915_private *dev_priv = to_i915(dev);
  4327. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  4328. struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
  4329. struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
  4330. struct drm_plane_state *plane_state;
  4331. struct drm_plane *plane;
  4332. enum pipe pipe = intel_crtc->pipe;
  4333. drm_for_each_plane_mask(plane, dev, cstate->base.plane_mask) {
  4334. enum plane_id plane_id = to_intel_plane(plane)->id;
  4335. if (skl_ddb_entry_equal(&cur_ddb->plane[pipe][plane_id],
  4336. &new_ddb->plane[pipe][plane_id]) &&
  4337. skl_ddb_entry_equal(&cur_ddb->uv_plane[pipe][plane_id],
  4338. &new_ddb->uv_plane[pipe][plane_id]))
  4339. continue;
  4340. plane_state = drm_atomic_get_plane_state(state, plane);
  4341. if (IS_ERR(plane_state))
  4342. return PTR_ERR(plane_state);
  4343. }
  4344. return 0;
  4345. }
  4346. static int
  4347. skl_compute_ddb(struct drm_atomic_state *state)
  4348. {
  4349. const struct drm_i915_private *dev_priv = to_i915(state->dev);
  4350. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  4351. struct skl_ddb_allocation *ddb = &intel_state->wm_results.ddb;
  4352. struct intel_crtc *crtc;
  4353. struct intel_crtc_state *cstate;
  4354. int ret, i;
  4355. memcpy(ddb, &dev_priv->wm.skl_hw.ddb, sizeof(*ddb));
  4356. for_each_new_intel_crtc_in_state(intel_state, crtc, cstate, i) {
  4357. ret = skl_allocate_pipe_ddb(cstate, ddb);
  4358. if (ret)
  4359. return ret;
  4360. ret = skl_ddb_add_affected_planes(cstate);
  4361. if (ret)
  4362. return ret;
  4363. }
  4364. return 0;
  4365. }
  4366. static void
  4367. skl_print_wm_changes(const struct drm_atomic_state *state)
  4368. {
  4369. const struct drm_device *dev = state->dev;
  4370. const struct drm_i915_private *dev_priv = to_i915(dev);
  4371. const struct intel_atomic_state *intel_state =
  4372. to_intel_atomic_state(state);
  4373. const struct drm_crtc *crtc;
  4374. const struct drm_crtc_state *cstate;
  4375. const struct intel_plane *intel_plane;
  4376. const struct skl_ddb_allocation *old_ddb = &dev_priv->wm.skl_hw.ddb;
  4377. const struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
  4378. int i;
  4379. for_each_new_crtc_in_state(state, crtc, cstate, i) {
  4380. const struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4381. enum pipe pipe = intel_crtc->pipe;
  4382. for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
  4383. enum plane_id plane_id = intel_plane->id;
  4384. const struct skl_ddb_entry *old, *new;
  4385. old = &old_ddb->plane[pipe][plane_id];
  4386. new = &new_ddb->plane[pipe][plane_id];
  4387. if (skl_ddb_entry_equal(old, new))
  4388. continue;
  4389. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] ddb (%d - %d) -> (%d - %d)\n",
  4390. intel_plane->base.base.id,
  4391. intel_plane->base.name,
  4392. old->start, old->end,
  4393. new->start, new->end);
  4394. }
  4395. }
  4396. }
  4397. static int
  4398. skl_ddb_add_affected_pipes(struct drm_atomic_state *state, bool *changed)
  4399. {
  4400. struct drm_device *dev = state->dev;
  4401. const struct drm_i915_private *dev_priv = to_i915(dev);
  4402. const struct drm_crtc *crtc;
  4403. const struct drm_crtc_state *cstate;
  4404. struct intel_crtc *intel_crtc;
  4405. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  4406. uint32_t realloc_pipes = pipes_modified(state);
  4407. int ret, i;
  4408. /*
  4409. * When we distrust bios wm we always need to recompute to set the
  4410. * expected DDB allocations for each CRTC.
  4411. */
  4412. if (dev_priv->wm.distrust_bios_wm)
  4413. (*changed) = true;
  4414. /*
  4415. * If this transaction isn't actually touching any CRTC's, don't
  4416. * bother with watermark calculation. Note that if we pass this
  4417. * test, we're guaranteed to hold at least one CRTC state mutex,
  4418. * which means we can safely use values like dev_priv->active_crtcs
  4419. * since any racing commits that want to update them would need to
  4420. * hold _all_ CRTC state mutexes.
  4421. */
  4422. for_each_new_crtc_in_state(state, crtc, cstate, i)
  4423. (*changed) = true;
  4424. if (!*changed)
  4425. return 0;
  4426. /*
  4427. * If this is our first atomic update following hardware readout,
  4428. * we can't trust the DDB that the BIOS programmed for us. Let's
  4429. * pretend that all pipes switched active status so that we'll
  4430. * ensure a full DDB recompute.
  4431. */
  4432. if (dev_priv->wm.distrust_bios_wm) {
  4433. ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
  4434. state->acquire_ctx);
  4435. if (ret)
  4436. return ret;
  4437. intel_state->active_pipe_changes = ~0;
  4438. /*
  4439. * We usually only initialize intel_state->active_crtcs if we
  4440. * we're doing a modeset; make sure this field is always
  4441. * initialized during the sanitization process that happens
  4442. * on the first commit too.
  4443. */
  4444. if (!intel_state->modeset)
  4445. intel_state->active_crtcs = dev_priv->active_crtcs;
  4446. }
  4447. /*
  4448. * If the modeset changes which CRTC's are active, we need to
  4449. * recompute the DDB allocation for *all* active pipes, even
  4450. * those that weren't otherwise being modified in any way by this
  4451. * atomic commit. Due to the shrinking of the per-pipe allocations
  4452. * when new active CRTC's are added, it's possible for a pipe that
  4453. * we were already using and aren't changing at all here to suddenly
  4454. * become invalid if its DDB needs exceeds its new allocation.
  4455. *
  4456. * Note that if we wind up doing a full DDB recompute, we can't let
  4457. * any other display updates race with this transaction, so we need
  4458. * to grab the lock on *all* CRTC's.
  4459. */
  4460. if (intel_state->active_pipe_changes || intel_state->modeset) {
  4461. realloc_pipes = ~0;
  4462. intel_state->wm_results.dirty_pipes = ~0;
  4463. }
  4464. /*
  4465. * We're not recomputing for the pipes not included in the commit, so
  4466. * make sure we start with the current state.
  4467. */
  4468. for_each_intel_crtc_mask(dev, intel_crtc, realloc_pipes) {
  4469. struct intel_crtc_state *cstate;
  4470. cstate = intel_atomic_get_crtc_state(state, intel_crtc);
  4471. if (IS_ERR(cstate))
  4472. return PTR_ERR(cstate);
  4473. }
  4474. return 0;
  4475. }
  4476. static int
  4477. skl_compute_wm(struct drm_atomic_state *state)
  4478. {
  4479. struct drm_crtc *crtc;
  4480. struct drm_crtc_state *cstate;
  4481. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  4482. struct skl_ddb_values *results = &intel_state->wm_results;
  4483. struct skl_pipe_wm *pipe_wm;
  4484. bool changed = false;
  4485. int ret, i;
  4486. /* Clear all dirty flags */
  4487. results->dirty_pipes = 0;
  4488. ret = skl_ddb_add_affected_pipes(state, &changed);
  4489. if (ret || !changed)
  4490. return ret;
  4491. ret = skl_compute_ddb(state);
  4492. if (ret)
  4493. return ret;
  4494. /*
  4495. * Calculate WM's for all pipes that are part of this transaction.
  4496. * Note that the DDB allocation above may have added more CRTC's that
  4497. * weren't otherwise being modified (and set bits in dirty_pipes) if
  4498. * pipe allocations had to change.
  4499. *
  4500. * FIXME: Now that we're doing this in the atomic check phase, we
  4501. * should allow skl_update_pipe_wm() to return failure in cases where
  4502. * no suitable watermark values can be found.
  4503. */
  4504. for_each_new_crtc_in_state(state, crtc, cstate, i) {
  4505. struct intel_crtc_state *intel_cstate =
  4506. to_intel_crtc_state(cstate);
  4507. const struct skl_pipe_wm *old_pipe_wm =
  4508. &to_intel_crtc_state(crtc->state)->wm.skl.optimal;
  4509. pipe_wm = &intel_cstate->wm.skl.optimal;
  4510. ret = skl_update_pipe_wm(cstate, old_pipe_wm, pipe_wm,
  4511. &results->ddb, &changed);
  4512. if (ret)
  4513. return ret;
  4514. if (changed)
  4515. results->dirty_pipes |= drm_crtc_mask(crtc);
  4516. if ((results->dirty_pipes & drm_crtc_mask(crtc)) == 0)
  4517. /* This pipe's WM's did not change */
  4518. continue;
  4519. intel_cstate->update_wm_pre = true;
  4520. }
  4521. skl_print_wm_changes(state);
  4522. return 0;
  4523. }
  4524. static void skl_atomic_update_crtc_wm(struct intel_atomic_state *state,
  4525. struct intel_crtc_state *cstate)
  4526. {
  4527. struct intel_crtc *crtc = to_intel_crtc(cstate->base.crtc);
  4528. struct drm_i915_private *dev_priv = to_i915(state->base.dev);
  4529. struct skl_pipe_wm *pipe_wm = &cstate->wm.skl.optimal;
  4530. const struct skl_ddb_allocation *ddb = &state->wm_results.ddb;
  4531. enum pipe pipe = crtc->pipe;
  4532. enum plane_id plane_id;
  4533. if (!(state->wm_results.dirty_pipes & drm_crtc_mask(&crtc->base)))
  4534. return;
  4535. I915_WRITE(PIPE_WM_LINETIME(pipe), pipe_wm->linetime);
  4536. for_each_plane_id_on_crtc(crtc, plane_id) {
  4537. if (plane_id != PLANE_CURSOR)
  4538. skl_write_plane_wm(crtc, &pipe_wm->planes[plane_id],
  4539. ddb, plane_id);
  4540. else
  4541. skl_write_cursor_wm(crtc, &pipe_wm->planes[plane_id],
  4542. ddb);
  4543. }
  4544. }
  4545. static void skl_initial_wm(struct intel_atomic_state *state,
  4546. struct intel_crtc_state *cstate)
  4547. {
  4548. struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
  4549. struct drm_device *dev = intel_crtc->base.dev;
  4550. struct drm_i915_private *dev_priv = to_i915(dev);
  4551. struct skl_ddb_values *results = &state->wm_results;
  4552. struct skl_ddb_values *hw_vals = &dev_priv->wm.skl_hw;
  4553. enum pipe pipe = intel_crtc->pipe;
  4554. if ((results->dirty_pipes & drm_crtc_mask(&intel_crtc->base)) == 0)
  4555. return;
  4556. mutex_lock(&dev_priv->wm.wm_mutex);
  4557. if (cstate->base.active_changed)
  4558. skl_atomic_update_crtc_wm(state, cstate);
  4559. memcpy(hw_vals->ddb.uv_plane[pipe], results->ddb.uv_plane[pipe],
  4560. sizeof(hw_vals->ddb.uv_plane[pipe]));
  4561. memcpy(hw_vals->ddb.plane[pipe], results->ddb.plane[pipe],
  4562. sizeof(hw_vals->ddb.plane[pipe]));
  4563. mutex_unlock(&dev_priv->wm.wm_mutex);
  4564. }
  4565. static void ilk_compute_wm_config(struct drm_device *dev,
  4566. struct intel_wm_config *config)
  4567. {
  4568. struct intel_crtc *crtc;
  4569. /* Compute the currently _active_ config */
  4570. for_each_intel_crtc(dev, crtc) {
  4571. const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
  4572. if (!wm->pipe_enabled)
  4573. continue;
  4574. config->sprites_enabled |= wm->sprites_enabled;
  4575. config->sprites_scaled |= wm->sprites_scaled;
  4576. config->num_pipes_active++;
  4577. }
  4578. }
  4579. static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
  4580. {
  4581. struct drm_device *dev = &dev_priv->drm;
  4582. struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
  4583. struct ilk_wm_maximums max;
  4584. struct intel_wm_config config = {};
  4585. struct ilk_wm_values results = {};
  4586. enum intel_ddb_partitioning partitioning;
  4587. ilk_compute_wm_config(dev, &config);
  4588. ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
  4589. ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
  4590. /* 5/6 split only in single pipe config on IVB+ */
  4591. if (INTEL_GEN(dev_priv) >= 7 &&
  4592. config.num_pipes_active == 1 && config.sprites_enabled) {
  4593. ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
  4594. ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
  4595. best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
  4596. } else {
  4597. best_lp_wm = &lp_wm_1_2;
  4598. }
  4599. partitioning = (best_lp_wm == &lp_wm_1_2) ?
  4600. INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
  4601. ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
  4602. ilk_write_wm_values(dev_priv, &results);
  4603. }
  4604. static void ilk_initial_watermarks(struct intel_atomic_state *state,
  4605. struct intel_crtc_state *cstate)
  4606. {
  4607. struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
  4608. struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
  4609. mutex_lock(&dev_priv->wm.wm_mutex);
  4610. intel_crtc->wm.active.ilk = cstate->wm.ilk.intermediate;
  4611. ilk_program_watermarks(dev_priv);
  4612. mutex_unlock(&dev_priv->wm.wm_mutex);
  4613. }
  4614. static void ilk_optimize_watermarks(struct intel_atomic_state *state,
  4615. struct intel_crtc_state *cstate)
  4616. {
  4617. struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
  4618. struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
  4619. mutex_lock(&dev_priv->wm.wm_mutex);
  4620. if (cstate->wm.need_postvbl_update) {
  4621. intel_crtc->wm.active.ilk = cstate->wm.ilk.optimal;
  4622. ilk_program_watermarks(dev_priv);
  4623. }
  4624. mutex_unlock(&dev_priv->wm.wm_mutex);
  4625. }
  4626. static inline void skl_wm_level_from_reg_val(uint32_t val,
  4627. struct skl_wm_level *level)
  4628. {
  4629. level->plane_en = val & PLANE_WM_EN;
  4630. level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
  4631. level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
  4632. PLANE_WM_LINES_MASK;
  4633. }
  4634. void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc,
  4635. struct skl_pipe_wm *out)
  4636. {
  4637. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4638. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4639. enum pipe pipe = intel_crtc->pipe;
  4640. int level, max_level;
  4641. enum plane_id plane_id;
  4642. uint32_t val;
  4643. max_level = ilk_wm_max_level(dev_priv);
  4644. for_each_plane_id_on_crtc(intel_crtc, plane_id) {
  4645. struct skl_plane_wm *wm = &out->planes[plane_id];
  4646. for (level = 0; level <= max_level; level++) {
  4647. if (plane_id != PLANE_CURSOR)
  4648. val = I915_READ(PLANE_WM(pipe, plane_id, level));
  4649. else
  4650. val = I915_READ(CUR_WM(pipe, level));
  4651. skl_wm_level_from_reg_val(val, &wm->wm[level]);
  4652. }
  4653. if (plane_id != PLANE_CURSOR)
  4654. val = I915_READ(PLANE_WM_TRANS(pipe, plane_id));
  4655. else
  4656. val = I915_READ(CUR_WM_TRANS(pipe));
  4657. skl_wm_level_from_reg_val(val, &wm->trans_wm);
  4658. }
  4659. if (!intel_crtc->active)
  4660. return;
  4661. out->linetime = I915_READ(PIPE_WM_LINETIME(pipe));
  4662. }
  4663. void skl_wm_get_hw_state(struct drm_device *dev)
  4664. {
  4665. struct drm_i915_private *dev_priv = to_i915(dev);
  4666. struct skl_ddb_values *hw = &dev_priv->wm.skl_hw;
  4667. struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
  4668. struct drm_crtc *crtc;
  4669. struct intel_crtc *intel_crtc;
  4670. struct intel_crtc_state *cstate;
  4671. skl_ddb_get_hw_state(dev_priv, ddb);
  4672. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  4673. intel_crtc = to_intel_crtc(crtc);
  4674. cstate = to_intel_crtc_state(crtc->state);
  4675. skl_pipe_wm_get_hw_state(crtc, &cstate->wm.skl.optimal);
  4676. if (intel_crtc->active)
  4677. hw->dirty_pipes |= drm_crtc_mask(crtc);
  4678. }
  4679. if (dev_priv->active_crtcs) {
  4680. /* Fully recompute DDB on first atomic commit */
  4681. dev_priv->wm.distrust_bios_wm = true;
  4682. } else {
  4683. /*
  4684. * Easy/common case; just sanitize DDB now if everything off
  4685. * Keep dbuf slice info intact
  4686. */
  4687. memset(ddb->plane, 0, sizeof(ddb->plane));
  4688. memset(ddb->uv_plane, 0, sizeof(ddb->uv_plane));
  4689. }
  4690. }
  4691. static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
  4692. {
  4693. struct drm_device *dev = crtc->dev;
  4694. struct drm_i915_private *dev_priv = to_i915(dev);
  4695. struct ilk_wm_values *hw = &dev_priv->wm.hw;
  4696. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4697. struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
  4698. struct intel_pipe_wm *active = &cstate->wm.ilk.optimal;
  4699. enum pipe pipe = intel_crtc->pipe;
  4700. static const i915_reg_t wm0_pipe_reg[] = {
  4701. [PIPE_A] = WM0_PIPEA_ILK,
  4702. [PIPE_B] = WM0_PIPEB_ILK,
  4703. [PIPE_C] = WM0_PIPEC_IVB,
  4704. };
  4705. hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
  4706. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4707. hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
  4708. memset(active, 0, sizeof(*active));
  4709. active->pipe_enabled = intel_crtc->active;
  4710. if (active->pipe_enabled) {
  4711. u32 tmp = hw->wm_pipe[pipe];
  4712. /*
  4713. * For active pipes LP0 watermark is marked as
  4714. * enabled, and LP1+ watermaks as disabled since
  4715. * we can't really reverse compute them in case
  4716. * multiple pipes are active.
  4717. */
  4718. active->wm[0].enable = true;
  4719. active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
  4720. active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
  4721. active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
  4722. active->linetime = hw->wm_linetime[pipe];
  4723. } else {
  4724. int level, max_level = ilk_wm_max_level(dev_priv);
  4725. /*
  4726. * For inactive pipes, all watermark levels
  4727. * should be marked as enabled but zeroed,
  4728. * which is what we'd compute them to.
  4729. */
  4730. for (level = 0; level <= max_level; level++)
  4731. active->wm[level].enable = true;
  4732. }
  4733. intel_crtc->wm.active.ilk = *active;
  4734. }
  4735. #define _FW_WM(value, plane) \
  4736. (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
  4737. #define _FW_WM_VLV(value, plane) \
  4738. (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
  4739. static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
  4740. struct g4x_wm_values *wm)
  4741. {
  4742. uint32_t tmp;
  4743. tmp = I915_READ(DSPFW1);
  4744. wm->sr.plane = _FW_WM(tmp, SR);
  4745. wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
  4746. wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
  4747. wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
  4748. tmp = I915_READ(DSPFW2);
  4749. wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
  4750. wm->sr.fbc = _FW_WM(tmp, FBC_SR);
  4751. wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
  4752. wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
  4753. wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
  4754. wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
  4755. tmp = I915_READ(DSPFW3);
  4756. wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
  4757. wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
  4758. wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
  4759. wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
  4760. }
  4761. static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
  4762. struct vlv_wm_values *wm)
  4763. {
  4764. enum pipe pipe;
  4765. uint32_t tmp;
  4766. for_each_pipe(dev_priv, pipe) {
  4767. tmp = I915_READ(VLV_DDL(pipe));
  4768. wm->ddl[pipe].plane[PLANE_PRIMARY] =
  4769. (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
  4770. wm->ddl[pipe].plane[PLANE_CURSOR] =
  4771. (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
  4772. wm->ddl[pipe].plane[PLANE_SPRITE0] =
  4773. (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
  4774. wm->ddl[pipe].plane[PLANE_SPRITE1] =
  4775. (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
  4776. }
  4777. tmp = I915_READ(DSPFW1);
  4778. wm->sr.plane = _FW_WM(tmp, SR);
  4779. wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
  4780. wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
  4781. wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
  4782. tmp = I915_READ(DSPFW2);
  4783. wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
  4784. wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
  4785. wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
  4786. tmp = I915_READ(DSPFW3);
  4787. wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
  4788. if (IS_CHERRYVIEW(dev_priv)) {
  4789. tmp = I915_READ(DSPFW7_CHV);
  4790. wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
  4791. wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
  4792. tmp = I915_READ(DSPFW8_CHV);
  4793. wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
  4794. wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
  4795. tmp = I915_READ(DSPFW9_CHV);
  4796. wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
  4797. wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
  4798. tmp = I915_READ(DSPHOWM);
  4799. wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
  4800. wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
  4801. wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
  4802. wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
  4803. wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
  4804. wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
  4805. wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
  4806. wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
  4807. wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
  4808. wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
  4809. } else {
  4810. tmp = I915_READ(DSPFW7);
  4811. wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
  4812. wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
  4813. tmp = I915_READ(DSPHOWM);
  4814. wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
  4815. wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
  4816. wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
  4817. wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
  4818. wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
  4819. wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
  4820. wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
  4821. }
  4822. }
  4823. #undef _FW_WM
  4824. #undef _FW_WM_VLV
  4825. void g4x_wm_get_hw_state(struct drm_device *dev)
  4826. {
  4827. struct drm_i915_private *dev_priv = to_i915(dev);
  4828. struct g4x_wm_values *wm = &dev_priv->wm.g4x;
  4829. struct intel_crtc *crtc;
  4830. g4x_read_wm_values(dev_priv, wm);
  4831. wm->cxsr = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
  4832. for_each_intel_crtc(dev, crtc) {
  4833. struct intel_crtc_state *crtc_state =
  4834. to_intel_crtc_state(crtc->base.state);
  4835. struct g4x_wm_state *active = &crtc->wm.active.g4x;
  4836. struct g4x_pipe_wm *raw;
  4837. enum pipe pipe = crtc->pipe;
  4838. enum plane_id plane_id;
  4839. int level, max_level;
  4840. active->cxsr = wm->cxsr;
  4841. active->hpll_en = wm->hpll_en;
  4842. active->fbc_en = wm->fbc_en;
  4843. active->sr = wm->sr;
  4844. active->hpll = wm->hpll;
  4845. for_each_plane_id_on_crtc(crtc, plane_id) {
  4846. active->wm.plane[plane_id] =
  4847. wm->pipe[pipe].plane[plane_id];
  4848. }
  4849. if (wm->cxsr && wm->hpll_en)
  4850. max_level = G4X_WM_LEVEL_HPLL;
  4851. else if (wm->cxsr)
  4852. max_level = G4X_WM_LEVEL_SR;
  4853. else
  4854. max_level = G4X_WM_LEVEL_NORMAL;
  4855. level = G4X_WM_LEVEL_NORMAL;
  4856. raw = &crtc_state->wm.g4x.raw[level];
  4857. for_each_plane_id_on_crtc(crtc, plane_id)
  4858. raw->plane[plane_id] = active->wm.plane[plane_id];
  4859. if (++level > max_level)
  4860. goto out;
  4861. raw = &crtc_state->wm.g4x.raw[level];
  4862. raw->plane[PLANE_PRIMARY] = active->sr.plane;
  4863. raw->plane[PLANE_CURSOR] = active->sr.cursor;
  4864. raw->plane[PLANE_SPRITE0] = 0;
  4865. raw->fbc = active->sr.fbc;
  4866. if (++level > max_level)
  4867. goto out;
  4868. raw = &crtc_state->wm.g4x.raw[level];
  4869. raw->plane[PLANE_PRIMARY] = active->hpll.plane;
  4870. raw->plane[PLANE_CURSOR] = active->hpll.cursor;
  4871. raw->plane[PLANE_SPRITE0] = 0;
  4872. raw->fbc = active->hpll.fbc;
  4873. out:
  4874. for_each_plane_id_on_crtc(crtc, plane_id)
  4875. g4x_raw_plane_wm_set(crtc_state, level,
  4876. plane_id, USHRT_MAX);
  4877. g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
  4878. crtc_state->wm.g4x.optimal = *active;
  4879. crtc_state->wm.g4x.intermediate = *active;
  4880. DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
  4881. pipe_name(pipe),
  4882. wm->pipe[pipe].plane[PLANE_PRIMARY],
  4883. wm->pipe[pipe].plane[PLANE_CURSOR],
  4884. wm->pipe[pipe].plane[PLANE_SPRITE0]);
  4885. }
  4886. DRM_DEBUG_KMS("Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
  4887. wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
  4888. DRM_DEBUG_KMS("Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
  4889. wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
  4890. DRM_DEBUG_KMS("Initial SR=%s HPLL=%s FBC=%s\n",
  4891. yesno(wm->cxsr), yesno(wm->hpll_en), yesno(wm->fbc_en));
  4892. }
  4893. void g4x_wm_sanitize(struct drm_i915_private *dev_priv)
  4894. {
  4895. struct intel_plane *plane;
  4896. struct intel_crtc *crtc;
  4897. mutex_lock(&dev_priv->wm.wm_mutex);
  4898. for_each_intel_plane(&dev_priv->drm, plane) {
  4899. struct intel_crtc *crtc =
  4900. intel_get_crtc_for_pipe(dev_priv, plane->pipe);
  4901. struct intel_crtc_state *crtc_state =
  4902. to_intel_crtc_state(crtc->base.state);
  4903. struct intel_plane_state *plane_state =
  4904. to_intel_plane_state(plane->base.state);
  4905. struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
  4906. enum plane_id plane_id = plane->id;
  4907. int level;
  4908. if (plane_state->base.visible)
  4909. continue;
  4910. for (level = 0; level < 3; level++) {
  4911. struct g4x_pipe_wm *raw =
  4912. &crtc_state->wm.g4x.raw[level];
  4913. raw->plane[plane_id] = 0;
  4914. wm_state->wm.plane[plane_id] = 0;
  4915. }
  4916. if (plane_id == PLANE_PRIMARY) {
  4917. for (level = 0; level < 3; level++) {
  4918. struct g4x_pipe_wm *raw =
  4919. &crtc_state->wm.g4x.raw[level];
  4920. raw->fbc = 0;
  4921. }
  4922. wm_state->sr.fbc = 0;
  4923. wm_state->hpll.fbc = 0;
  4924. wm_state->fbc_en = false;
  4925. }
  4926. }
  4927. for_each_intel_crtc(&dev_priv->drm, crtc) {
  4928. struct intel_crtc_state *crtc_state =
  4929. to_intel_crtc_state(crtc->base.state);
  4930. crtc_state->wm.g4x.intermediate =
  4931. crtc_state->wm.g4x.optimal;
  4932. crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
  4933. }
  4934. g4x_program_watermarks(dev_priv);
  4935. mutex_unlock(&dev_priv->wm.wm_mutex);
  4936. }
  4937. void vlv_wm_get_hw_state(struct drm_device *dev)
  4938. {
  4939. struct drm_i915_private *dev_priv = to_i915(dev);
  4940. struct vlv_wm_values *wm = &dev_priv->wm.vlv;
  4941. struct intel_crtc *crtc;
  4942. u32 val;
  4943. vlv_read_wm_values(dev_priv, wm);
  4944. wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
  4945. wm->level = VLV_WM_LEVEL_PM2;
  4946. if (IS_CHERRYVIEW(dev_priv)) {
  4947. mutex_lock(&dev_priv->pcu_lock);
  4948. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4949. if (val & DSP_MAXFIFO_PM5_ENABLE)
  4950. wm->level = VLV_WM_LEVEL_PM5;
  4951. /*
  4952. * If DDR DVFS is disabled in the BIOS, Punit
  4953. * will never ack the request. So if that happens
  4954. * assume we don't have to enable/disable DDR DVFS
  4955. * dynamically. To test that just set the REQ_ACK
  4956. * bit to poke the Punit, but don't change the
  4957. * HIGH/LOW bits so that we don't actually change
  4958. * the current state.
  4959. */
  4960. val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
  4961. val |= FORCE_DDR_FREQ_REQ_ACK;
  4962. vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
  4963. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
  4964. FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
  4965. DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
  4966. "assuming DDR DVFS is disabled\n");
  4967. dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
  4968. } else {
  4969. val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
  4970. if ((val & FORCE_DDR_HIGH_FREQ) == 0)
  4971. wm->level = VLV_WM_LEVEL_DDR_DVFS;
  4972. }
  4973. mutex_unlock(&dev_priv->pcu_lock);
  4974. }
  4975. for_each_intel_crtc(dev, crtc) {
  4976. struct intel_crtc_state *crtc_state =
  4977. to_intel_crtc_state(crtc->base.state);
  4978. struct vlv_wm_state *active = &crtc->wm.active.vlv;
  4979. const struct vlv_fifo_state *fifo_state =
  4980. &crtc_state->wm.vlv.fifo_state;
  4981. enum pipe pipe = crtc->pipe;
  4982. enum plane_id plane_id;
  4983. int level;
  4984. vlv_get_fifo_size(crtc_state);
  4985. active->num_levels = wm->level + 1;
  4986. active->cxsr = wm->cxsr;
  4987. for (level = 0; level < active->num_levels; level++) {
  4988. struct g4x_pipe_wm *raw =
  4989. &crtc_state->wm.vlv.raw[level];
  4990. active->sr[level].plane = wm->sr.plane;
  4991. active->sr[level].cursor = wm->sr.cursor;
  4992. for_each_plane_id_on_crtc(crtc, plane_id) {
  4993. active->wm[level].plane[plane_id] =
  4994. wm->pipe[pipe].plane[plane_id];
  4995. raw->plane[plane_id] =
  4996. vlv_invert_wm_value(active->wm[level].plane[plane_id],
  4997. fifo_state->plane[plane_id]);
  4998. }
  4999. }
  5000. for_each_plane_id_on_crtc(crtc, plane_id)
  5001. vlv_raw_plane_wm_set(crtc_state, level,
  5002. plane_id, USHRT_MAX);
  5003. vlv_invalidate_wms(crtc, active, level);
  5004. crtc_state->wm.vlv.optimal = *active;
  5005. crtc_state->wm.vlv.intermediate = *active;
  5006. DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite0=%d, sprite1=%d\n",
  5007. pipe_name(pipe),
  5008. wm->pipe[pipe].plane[PLANE_PRIMARY],
  5009. wm->pipe[pipe].plane[PLANE_CURSOR],
  5010. wm->pipe[pipe].plane[PLANE_SPRITE0],
  5011. wm->pipe[pipe].plane[PLANE_SPRITE1]);
  5012. }
  5013. DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
  5014. wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
  5015. }
  5016. void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
  5017. {
  5018. struct intel_plane *plane;
  5019. struct intel_crtc *crtc;
  5020. mutex_lock(&dev_priv->wm.wm_mutex);
  5021. for_each_intel_plane(&dev_priv->drm, plane) {
  5022. struct intel_crtc *crtc =
  5023. intel_get_crtc_for_pipe(dev_priv, plane->pipe);
  5024. struct intel_crtc_state *crtc_state =
  5025. to_intel_crtc_state(crtc->base.state);
  5026. struct intel_plane_state *plane_state =
  5027. to_intel_plane_state(plane->base.state);
  5028. struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
  5029. const struct vlv_fifo_state *fifo_state =
  5030. &crtc_state->wm.vlv.fifo_state;
  5031. enum plane_id plane_id = plane->id;
  5032. int level;
  5033. if (plane_state->base.visible)
  5034. continue;
  5035. for (level = 0; level < wm_state->num_levels; level++) {
  5036. struct g4x_pipe_wm *raw =
  5037. &crtc_state->wm.vlv.raw[level];
  5038. raw->plane[plane_id] = 0;
  5039. wm_state->wm[level].plane[plane_id] =
  5040. vlv_invert_wm_value(raw->plane[plane_id],
  5041. fifo_state->plane[plane_id]);
  5042. }
  5043. }
  5044. for_each_intel_crtc(&dev_priv->drm, crtc) {
  5045. struct intel_crtc_state *crtc_state =
  5046. to_intel_crtc_state(crtc->base.state);
  5047. crtc_state->wm.vlv.intermediate =
  5048. crtc_state->wm.vlv.optimal;
  5049. crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
  5050. }
  5051. vlv_program_watermarks(dev_priv);
  5052. mutex_unlock(&dev_priv->wm.wm_mutex);
  5053. }
  5054. /*
  5055. * FIXME should probably kill this and improve
  5056. * the real watermark readout/sanitation instead
  5057. */
  5058. static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
  5059. {
  5060. I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
  5061. I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
  5062. I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
  5063. /*
  5064. * Don't touch WM1S_LP_EN here.
  5065. * Doing so could cause underruns.
  5066. */
  5067. }
  5068. void ilk_wm_get_hw_state(struct drm_device *dev)
  5069. {
  5070. struct drm_i915_private *dev_priv = to_i915(dev);
  5071. struct ilk_wm_values *hw = &dev_priv->wm.hw;
  5072. struct drm_crtc *crtc;
  5073. ilk_init_lp_watermarks(dev_priv);
  5074. for_each_crtc(dev, crtc)
  5075. ilk_pipe_wm_get_hw_state(crtc);
  5076. hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
  5077. hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
  5078. hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
  5079. hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
  5080. if (INTEL_GEN(dev_priv) >= 7) {
  5081. hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
  5082. hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
  5083. }
  5084. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  5085. hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
  5086. INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
  5087. else if (IS_IVYBRIDGE(dev_priv))
  5088. hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
  5089. INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
  5090. hw->enable_fbc_wm =
  5091. !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
  5092. }
  5093. /**
  5094. * intel_update_watermarks - update FIFO watermark values based on current modes
  5095. * @crtc: the #intel_crtc on which to compute the WM
  5096. *
  5097. * Calculate watermark values for the various WM regs based on current mode
  5098. * and plane configuration.
  5099. *
  5100. * There are several cases to deal with here:
  5101. * - normal (i.e. non-self-refresh)
  5102. * - self-refresh (SR) mode
  5103. * - lines are large relative to FIFO size (buffer can hold up to 2)
  5104. * - lines are small relative to FIFO size (buffer can hold more than 2
  5105. * lines), so need to account for TLB latency
  5106. *
  5107. * The normal calculation is:
  5108. * watermark = dotclock * bytes per pixel * latency
  5109. * where latency is platform & configuration dependent (we assume pessimal
  5110. * values here).
  5111. *
  5112. * The SR calculation is:
  5113. * watermark = (trunc(latency/line time)+1) * surface width *
  5114. * bytes per pixel
  5115. * where
  5116. * line time = htotal / dotclock
  5117. * surface width = hdisplay for normal plane and 64 for cursor
  5118. * and latency is assumed to be high, as above.
  5119. *
  5120. * The final value programmed to the register should always be rounded up,
  5121. * and include an extra 2 entries to account for clock crossings.
  5122. *
  5123. * We don't use the sprite, so we can ignore that. And on Crestline we have
  5124. * to set the non-SR watermarks to 8.
  5125. */
  5126. void intel_update_watermarks(struct intel_crtc *crtc)
  5127. {
  5128. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5129. if (dev_priv->display.update_wm)
  5130. dev_priv->display.update_wm(crtc);
  5131. }
  5132. void intel_enable_ipc(struct drm_i915_private *dev_priv)
  5133. {
  5134. u32 val;
  5135. /* Display WA #0477 WaDisableIPC: skl */
  5136. if (IS_SKYLAKE(dev_priv))
  5137. dev_priv->ipc_enabled = false;
  5138. /* Display WA #1141: SKL:all KBL:all CFL */
  5139. if ((IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv)) &&
  5140. !dev_priv->dram_info.symmetric_memory)
  5141. dev_priv->ipc_enabled = false;
  5142. val = I915_READ(DISP_ARB_CTL2);
  5143. if (dev_priv->ipc_enabled)
  5144. val |= DISP_IPC_ENABLE;
  5145. else
  5146. val &= ~DISP_IPC_ENABLE;
  5147. I915_WRITE(DISP_ARB_CTL2, val);
  5148. }
  5149. void intel_init_ipc(struct drm_i915_private *dev_priv)
  5150. {
  5151. dev_priv->ipc_enabled = false;
  5152. if (!HAS_IPC(dev_priv))
  5153. return;
  5154. dev_priv->ipc_enabled = true;
  5155. intel_enable_ipc(dev_priv);
  5156. }
  5157. /*
  5158. * Lock protecting IPS related data structures
  5159. */
  5160. DEFINE_SPINLOCK(mchdev_lock);
  5161. /* Global for IPS driver to get at the current i915 device. Protected by
  5162. * mchdev_lock. */
  5163. static struct drm_i915_private *i915_mch_dev;
  5164. bool ironlake_set_drps(struct drm_i915_private *dev_priv, u8 val)
  5165. {
  5166. u16 rgvswctl;
  5167. lockdep_assert_held(&mchdev_lock);
  5168. rgvswctl = I915_READ16(MEMSWCTL);
  5169. if (rgvswctl & MEMCTL_CMD_STS) {
  5170. DRM_DEBUG("gpu busy, RCS change rejected\n");
  5171. return false; /* still busy with another command */
  5172. }
  5173. rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
  5174. (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
  5175. I915_WRITE16(MEMSWCTL, rgvswctl);
  5176. POSTING_READ16(MEMSWCTL);
  5177. rgvswctl |= MEMCTL_CMD_STS;
  5178. I915_WRITE16(MEMSWCTL, rgvswctl);
  5179. return true;
  5180. }
  5181. static void ironlake_enable_drps(struct drm_i915_private *dev_priv)
  5182. {
  5183. u32 rgvmodectl;
  5184. u8 fmax, fmin, fstart, vstart;
  5185. spin_lock_irq(&mchdev_lock);
  5186. rgvmodectl = I915_READ(MEMMODECTL);
  5187. /* Enable temp reporting */
  5188. I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
  5189. I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
  5190. /* 100ms RC evaluation intervals */
  5191. I915_WRITE(RCUPEI, 100000);
  5192. I915_WRITE(RCDNEI, 100000);
  5193. /* Set max/min thresholds to 90ms and 80ms respectively */
  5194. I915_WRITE(RCBMAXAVG, 90000);
  5195. I915_WRITE(RCBMINAVG, 80000);
  5196. I915_WRITE(MEMIHYST, 1);
  5197. /* Set up min, max, and cur for interrupt handling */
  5198. fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
  5199. fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
  5200. fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
  5201. MEMMODE_FSTART_SHIFT;
  5202. vstart = (I915_READ(PXVFREQ(fstart)) & PXVFREQ_PX_MASK) >>
  5203. PXVFREQ_PX_SHIFT;
  5204. dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
  5205. dev_priv->ips.fstart = fstart;
  5206. dev_priv->ips.max_delay = fstart;
  5207. dev_priv->ips.min_delay = fmin;
  5208. dev_priv->ips.cur_delay = fstart;
  5209. DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
  5210. fmax, fmin, fstart);
  5211. I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
  5212. /*
  5213. * Interrupts will be enabled in ironlake_irq_postinstall
  5214. */
  5215. I915_WRITE(VIDSTART, vstart);
  5216. POSTING_READ(VIDSTART);
  5217. rgvmodectl |= MEMMODE_SWMODE_EN;
  5218. I915_WRITE(MEMMODECTL, rgvmodectl);
  5219. if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
  5220. DRM_ERROR("stuck trying to change perf mode\n");
  5221. mdelay(1);
  5222. ironlake_set_drps(dev_priv, fstart);
  5223. dev_priv->ips.last_count1 = I915_READ(DMIEC) +
  5224. I915_READ(DDREC) + I915_READ(CSIEC);
  5225. dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
  5226. dev_priv->ips.last_count2 = I915_READ(GFXEC);
  5227. dev_priv->ips.last_time2 = ktime_get_raw_ns();
  5228. spin_unlock_irq(&mchdev_lock);
  5229. }
  5230. static void ironlake_disable_drps(struct drm_i915_private *dev_priv)
  5231. {
  5232. u16 rgvswctl;
  5233. spin_lock_irq(&mchdev_lock);
  5234. rgvswctl = I915_READ16(MEMSWCTL);
  5235. /* Ack interrupts, disable EFC interrupt */
  5236. I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
  5237. I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
  5238. I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
  5239. I915_WRITE(DEIIR, DE_PCU_EVENT);
  5240. I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
  5241. /* Go back to the starting frequency */
  5242. ironlake_set_drps(dev_priv, dev_priv->ips.fstart);
  5243. mdelay(1);
  5244. rgvswctl |= MEMCTL_CMD_STS;
  5245. I915_WRITE(MEMSWCTL, rgvswctl);
  5246. mdelay(1);
  5247. spin_unlock_irq(&mchdev_lock);
  5248. }
  5249. /* There's a funny hw issue where the hw returns all 0 when reading from
  5250. * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
  5251. * ourselves, instead of doing a rmw cycle (which might result in us clearing
  5252. * all limits and the gpu stuck at whatever frequency it is at atm).
  5253. */
  5254. static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
  5255. {
  5256. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  5257. u32 limits;
  5258. /* Only set the down limit when we've reached the lowest level to avoid
  5259. * getting more interrupts, otherwise leave this clear. This prevents a
  5260. * race in the hw when coming out of rc6: There's a tiny window where
  5261. * the hw runs at the minimal clock before selecting the desired
  5262. * frequency, if the down threshold expires in that window we will not
  5263. * receive a down interrupt. */
  5264. if (INTEL_GEN(dev_priv) >= 9) {
  5265. limits = (rps->max_freq_softlimit) << 23;
  5266. if (val <= rps->min_freq_softlimit)
  5267. limits |= (rps->min_freq_softlimit) << 14;
  5268. } else {
  5269. limits = rps->max_freq_softlimit << 24;
  5270. if (val <= rps->min_freq_softlimit)
  5271. limits |= rps->min_freq_softlimit << 16;
  5272. }
  5273. return limits;
  5274. }
  5275. static void rps_set_power(struct drm_i915_private *dev_priv, int new_power)
  5276. {
  5277. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  5278. u32 threshold_up = 0, threshold_down = 0; /* in % */
  5279. u32 ei_up = 0, ei_down = 0;
  5280. lockdep_assert_held(&rps->power.mutex);
  5281. if (new_power == rps->power.mode)
  5282. return;
  5283. /* Note the units here are not exactly 1us, but 1280ns. */
  5284. switch (new_power) {
  5285. case LOW_POWER:
  5286. /* Upclock if more than 95% busy over 16ms */
  5287. ei_up = 16000;
  5288. threshold_up = 95;
  5289. /* Downclock if less than 85% busy over 32ms */
  5290. ei_down = 32000;
  5291. threshold_down = 85;
  5292. break;
  5293. case BETWEEN:
  5294. /* Upclock if more than 90% busy over 13ms */
  5295. ei_up = 13000;
  5296. threshold_up = 90;
  5297. /* Downclock if less than 75% busy over 32ms */
  5298. ei_down = 32000;
  5299. threshold_down = 75;
  5300. break;
  5301. case HIGH_POWER:
  5302. /* Upclock if more than 85% busy over 10ms */
  5303. ei_up = 10000;
  5304. threshold_up = 85;
  5305. /* Downclock if less than 60% busy over 32ms */
  5306. ei_down = 32000;
  5307. threshold_down = 60;
  5308. break;
  5309. }
  5310. /* When byt can survive without system hang with dynamic
  5311. * sw freq adjustments, this restriction can be lifted.
  5312. */
  5313. if (IS_VALLEYVIEW(dev_priv))
  5314. goto skip_hw_write;
  5315. I915_WRITE(GEN6_RP_UP_EI,
  5316. GT_INTERVAL_FROM_US(dev_priv, ei_up));
  5317. I915_WRITE(GEN6_RP_UP_THRESHOLD,
  5318. GT_INTERVAL_FROM_US(dev_priv,
  5319. ei_up * threshold_up / 100));
  5320. I915_WRITE(GEN6_RP_DOWN_EI,
  5321. GT_INTERVAL_FROM_US(dev_priv, ei_down));
  5322. I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
  5323. GT_INTERVAL_FROM_US(dev_priv,
  5324. ei_down * threshold_down / 100));
  5325. I915_WRITE(GEN6_RP_CONTROL,
  5326. GEN6_RP_MEDIA_TURBO |
  5327. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  5328. GEN6_RP_MEDIA_IS_GFX |
  5329. GEN6_RP_ENABLE |
  5330. GEN6_RP_UP_BUSY_AVG |
  5331. GEN6_RP_DOWN_IDLE_AVG);
  5332. skip_hw_write:
  5333. rps->power.mode = new_power;
  5334. rps->power.up_threshold = threshold_up;
  5335. rps->power.down_threshold = threshold_down;
  5336. }
  5337. static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
  5338. {
  5339. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  5340. int new_power;
  5341. new_power = rps->power.mode;
  5342. switch (rps->power.mode) {
  5343. case LOW_POWER:
  5344. if (val > rps->efficient_freq + 1 &&
  5345. val > rps->cur_freq)
  5346. new_power = BETWEEN;
  5347. break;
  5348. case BETWEEN:
  5349. if (val <= rps->efficient_freq &&
  5350. val < rps->cur_freq)
  5351. new_power = LOW_POWER;
  5352. else if (val >= rps->rp0_freq &&
  5353. val > rps->cur_freq)
  5354. new_power = HIGH_POWER;
  5355. break;
  5356. case HIGH_POWER:
  5357. if (val < (rps->rp1_freq + rps->rp0_freq) >> 1 &&
  5358. val < rps->cur_freq)
  5359. new_power = BETWEEN;
  5360. break;
  5361. }
  5362. /* Max/min bins are special */
  5363. if (val <= rps->min_freq_softlimit)
  5364. new_power = LOW_POWER;
  5365. if (val >= rps->max_freq_softlimit)
  5366. new_power = HIGH_POWER;
  5367. mutex_lock(&rps->power.mutex);
  5368. if (rps->power.interactive)
  5369. new_power = HIGH_POWER;
  5370. rps_set_power(dev_priv, new_power);
  5371. mutex_unlock(&rps->power.mutex);
  5372. }
  5373. void intel_rps_mark_interactive(struct drm_i915_private *i915, bool interactive)
  5374. {
  5375. struct intel_rps *rps = &i915->gt_pm.rps;
  5376. if (INTEL_GEN(i915) < 6)
  5377. return;
  5378. mutex_lock(&rps->power.mutex);
  5379. if (interactive) {
  5380. if (!rps->power.interactive++ && READ_ONCE(i915->gt.awake))
  5381. rps_set_power(i915, HIGH_POWER);
  5382. } else {
  5383. GEM_BUG_ON(!rps->power.interactive);
  5384. rps->power.interactive--;
  5385. }
  5386. mutex_unlock(&rps->power.mutex);
  5387. }
  5388. static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
  5389. {
  5390. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  5391. u32 mask = 0;
  5392. /* We use UP_EI_EXPIRED interupts for both up/down in manual mode */
  5393. if (val > rps->min_freq_softlimit)
  5394. mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
  5395. if (val < rps->max_freq_softlimit)
  5396. mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
  5397. mask &= dev_priv->pm_rps_events;
  5398. return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
  5399. }
  5400. /* gen6_set_rps is called to update the frequency request, but should also be
  5401. * called when the range (min_delay and max_delay) is modified so that we can
  5402. * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
  5403. static int gen6_set_rps(struct drm_i915_private *dev_priv, u8 val)
  5404. {
  5405. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  5406. /* min/max delay may still have been modified so be sure to
  5407. * write the limits value.
  5408. */
  5409. if (val != rps->cur_freq) {
  5410. gen6_set_rps_thresholds(dev_priv, val);
  5411. if (INTEL_GEN(dev_priv) >= 9)
  5412. I915_WRITE(GEN6_RPNSWREQ,
  5413. GEN9_FREQUENCY(val));
  5414. else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  5415. I915_WRITE(GEN6_RPNSWREQ,
  5416. HSW_FREQUENCY(val));
  5417. else
  5418. I915_WRITE(GEN6_RPNSWREQ,
  5419. GEN6_FREQUENCY(val) |
  5420. GEN6_OFFSET(0) |
  5421. GEN6_AGGRESSIVE_TURBO);
  5422. }
  5423. /* Make sure we continue to get interrupts
  5424. * until we hit the minimum or maximum frequencies.
  5425. */
  5426. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
  5427. I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
  5428. rps->cur_freq = val;
  5429. trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
  5430. return 0;
  5431. }
  5432. static int valleyview_set_rps(struct drm_i915_private *dev_priv, u8 val)
  5433. {
  5434. int err;
  5435. if (WARN_ONCE(IS_CHERRYVIEW(dev_priv) && (val & 1),
  5436. "Odd GPU freq value\n"))
  5437. val &= ~1;
  5438. I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
  5439. if (val != dev_priv->gt_pm.rps.cur_freq) {
  5440. err = vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
  5441. if (err)
  5442. return err;
  5443. gen6_set_rps_thresholds(dev_priv, val);
  5444. }
  5445. dev_priv->gt_pm.rps.cur_freq = val;
  5446. trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
  5447. return 0;
  5448. }
  5449. /* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
  5450. *
  5451. * * If Gfx is Idle, then
  5452. * 1. Forcewake Media well.
  5453. * 2. Request idle freq.
  5454. * 3. Release Forcewake of Media well.
  5455. */
  5456. static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
  5457. {
  5458. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  5459. u32 val = rps->idle_freq;
  5460. int err;
  5461. if (rps->cur_freq <= val)
  5462. return;
  5463. /* The punit delays the write of the frequency and voltage until it
  5464. * determines the GPU is awake. During normal usage we don't want to
  5465. * waste power changing the frequency if the GPU is sleeping (rc6).
  5466. * However, the GPU and driver is now idle and we do not want to delay
  5467. * switching to minimum voltage (reducing power whilst idle) as we do
  5468. * not expect to be woken in the near future and so must flush the
  5469. * change by waking the device.
  5470. *
  5471. * We choose to take the media powerwell (either would do to trick the
  5472. * punit into committing the voltage change) as that takes a lot less
  5473. * power than the render powerwell.
  5474. */
  5475. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
  5476. err = valleyview_set_rps(dev_priv, val);
  5477. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
  5478. if (err)
  5479. DRM_ERROR("Failed to set RPS for idle\n");
  5480. }
  5481. void gen6_rps_busy(struct drm_i915_private *dev_priv)
  5482. {
  5483. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  5484. mutex_lock(&dev_priv->pcu_lock);
  5485. if (rps->enabled) {
  5486. u8 freq;
  5487. if (dev_priv->pm_rps_events & GEN6_PM_RP_UP_EI_EXPIRED)
  5488. gen6_rps_reset_ei(dev_priv);
  5489. I915_WRITE(GEN6_PMINTRMSK,
  5490. gen6_rps_pm_mask(dev_priv, rps->cur_freq));
  5491. gen6_enable_rps_interrupts(dev_priv);
  5492. /* Use the user's desired frequency as a guide, but for better
  5493. * performance, jump directly to RPe as our starting frequency.
  5494. */
  5495. freq = max(rps->cur_freq,
  5496. rps->efficient_freq);
  5497. if (intel_set_rps(dev_priv,
  5498. clamp(freq,
  5499. rps->min_freq_softlimit,
  5500. rps->max_freq_softlimit)))
  5501. DRM_DEBUG_DRIVER("Failed to set idle frequency\n");
  5502. }
  5503. mutex_unlock(&dev_priv->pcu_lock);
  5504. }
  5505. void gen6_rps_idle(struct drm_i915_private *dev_priv)
  5506. {
  5507. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  5508. /* Flush our bottom-half so that it does not race with us
  5509. * setting the idle frequency and so that it is bounded by
  5510. * our rpm wakeref. And then disable the interrupts to stop any
  5511. * futher RPS reclocking whilst we are asleep.
  5512. */
  5513. gen6_disable_rps_interrupts(dev_priv);
  5514. mutex_lock(&dev_priv->pcu_lock);
  5515. if (rps->enabled) {
  5516. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  5517. vlv_set_rps_idle(dev_priv);
  5518. else
  5519. gen6_set_rps(dev_priv, rps->idle_freq);
  5520. rps->last_adj = 0;
  5521. I915_WRITE(GEN6_PMINTRMSK,
  5522. gen6_sanitize_rps_pm_mask(dev_priv, ~0));
  5523. }
  5524. mutex_unlock(&dev_priv->pcu_lock);
  5525. }
  5526. void gen6_rps_boost(struct i915_request *rq,
  5527. struct intel_rps_client *rps_client)
  5528. {
  5529. struct intel_rps *rps = &rq->i915->gt_pm.rps;
  5530. unsigned long flags;
  5531. bool boost;
  5532. /* This is intentionally racy! We peek at the state here, then
  5533. * validate inside the RPS worker.
  5534. */
  5535. if (!rps->enabled)
  5536. return;
  5537. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &rq->fence.flags))
  5538. return;
  5539. /* Serializes with i915_request_retire() */
  5540. boost = false;
  5541. spin_lock_irqsave(&rq->lock, flags);
  5542. if (!rq->waitboost && !dma_fence_is_signaled_locked(&rq->fence)) {
  5543. boost = !atomic_fetch_inc(&rps->num_waiters);
  5544. rq->waitboost = true;
  5545. }
  5546. spin_unlock_irqrestore(&rq->lock, flags);
  5547. if (!boost)
  5548. return;
  5549. if (READ_ONCE(rps->cur_freq) < rps->boost_freq)
  5550. schedule_work(&rps->work);
  5551. atomic_inc(rps_client ? &rps_client->boosts : &rps->boosts);
  5552. }
  5553. int intel_set_rps(struct drm_i915_private *dev_priv, u8 val)
  5554. {
  5555. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  5556. int err;
  5557. lockdep_assert_held(&dev_priv->pcu_lock);
  5558. GEM_BUG_ON(val > rps->max_freq);
  5559. GEM_BUG_ON(val < rps->min_freq);
  5560. if (!rps->enabled) {
  5561. rps->cur_freq = val;
  5562. return 0;
  5563. }
  5564. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  5565. err = valleyview_set_rps(dev_priv, val);
  5566. else
  5567. err = gen6_set_rps(dev_priv, val);
  5568. return err;
  5569. }
  5570. static void gen9_disable_rc6(struct drm_i915_private *dev_priv)
  5571. {
  5572. I915_WRITE(GEN6_RC_CONTROL, 0);
  5573. I915_WRITE(GEN9_PG_ENABLE, 0);
  5574. }
  5575. static void gen9_disable_rps(struct drm_i915_private *dev_priv)
  5576. {
  5577. I915_WRITE(GEN6_RP_CONTROL, 0);
  5578. }
  5579. static void gen6_disable_rc6(struct drm_i915_private *dev_priv)
  5580. {
  5581. I915_WRITE(GEN6_RC_CONTROL, 0);
  5582. }
  5583. static void gen6_disable_rps(struct drm_i915_private *dev_priv)
  5584. {
  5585. I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
  5586. I915_WRITE(GEN6_RP_CONTROL, 0);
  5587. }
  5588. static void cherryview_disable_rc6(struct drm_i915_private *dev_priv)
  5589. {
  5590. I915_WRITE(GEN6_RC_CONTROL, 0);
  5591. }
  5592. static void cherryview_disable_rps(struct drm_i915_private *dev_priv)
  5593. {
  5594. I915_WRITE(GEN6_RP_CONTROL, 0);
  5595. }
  5596. static void valleyview_disable_rc6(struct drm_i915_private *dev_priv)
  5597. {
  5598. /* We're doing forcewake before Disabling RC6,
  5599. * This what the BIOS expects when going into suspend */
  5600. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  5601. I915_WRITE(GEN6_RC_CONTROL, 0);
  5602. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  5603. }
  5604. static void valleyview_disable_rps(struct drm_i915_private *dev_priv)
  5605. {
  5606. I915_WRITE(GEN6_RP_CONTROL, 0);
  5607. }
  5608. static bool bxt_check_bios_rc6_setup(struct drm_i915_private *dev_priv)
  5609. {
  5610. bool enable_rc6 = true;
  5611. unsigned long rc6_ctx_base;
  5612. u32 rc_ctl;
  5613. int rc_sw_target;
  5614. rc_ctl = I915_READ(GEN6_RC_CONTROL);
  5615. rc_sw_target = (I915_READ(GEN6_RC_STATE) & RC_SW_TARGET_STATE_MASK) >>
  5616. RC_SW_TARGET_STATE_SHIFT;
  5617. DRM_DEBUG_DRIVER("BIOS enabled RC states: "
  5618. "HW_CTRL %s HW_RC6 %s SW_TARGET_STATE %x\n",
  5619. onoff(rc_ctl & GEN6_RC_CTL_HW_ENABLE),
  5620. onoff(rc_ctl & GEN6_RC_CTL_RC6_ENABLE),
  5621. rc_sw_target);
  5622. if (!(I915_READ(RC6_LOCATION) & RC6_CTX_IN_DRAM)) {
  5623. DRM_DEBUG_DRIVER("RC6 Base location not set properly.\n");
  5624. enable_rc6 = false;
  5625. }
  5626. /*
  5627. * The exact context size is not known for BXT, so assume a page size
  5628. * for this check.
  5629. */
  5630. rc6_ctx_base = I915_READ(RC6_CTX_BASE) & RC6_CTX_BASE_MASK;
  5631. if (!((rc6_ctx_base >= dev_priv->dsm_reserved.start) &&
  5632. (rc6_ctx_base + PAGE_SIZE < dev_priv->dsm_reserved.end))) {
  5633. DRM_DEBUG_DRIVER("RC6 Base address not as expected.\n");
  5634. enable_rc6 = false;
  5635. }
  5636. if (!(((I915_READ(PWRCTX_MAXCNT_RCSUNIT) & IDLE_TIME_MASK) > 1) &&
  5637. ((I915_READ(PWRCTX_MAXCNT_VCSUNIT0) & IDLE_TIME_MASK) > 1) &&
  5638. ((I915_READ(PWRCTX_MAXCNT_BCSUNIT) & IDLE_TIME_MASK) > 1) &&
  5639. ((I915_READ(PWRCTX_MAXCNT_VECSUNIT) & IDLE_TIME_MASK) > 1))) {
  5640. DRM_DEBUG_DRIVER("Engine Idle wait time not set properly.\n");
  5641. enable_rc6 = false;
  5642. }
  5643. if (!I915_READ(GEN8_PUSHBUS_CONTROL) ||
  5644. !I915_READ(GEN8_PUSHBUS_ENABLE) ||
  5645. !I915_READ(GEN8_PUSHBUS_SHIFT)) {
  5646. DRM_DEBUG_DRIVER("Pushbus not setup properly.\n");
  5647. enable_rc6 = false;
  5648. }
  5649. if (!I915_READ(GEN6_GFXPAUSE)) {
  5650. DRM_DEBUG_DRIVER("GFX pause not setup properly.\n");
  5651. enable_rc6 = false;
  5652. }
  5653. if (!I915_READ(GEN8_MISC_CTRL0)) {
  5654. DRM_DEBUG_DRIVER("GPM control not setup properly.\n");
  5655. enable_rc6 = false;
  5656. }
  5657. return enable_rc6;
  5658. }
  5659. static bool sanitize_rc6(struct drm_i915_private *i915)
  5660. {
  5661. struct intel_device_info *info = mkwrite_device_info(i915);
  5662. /* Powersaving is controlled by the host when inside a VM */
  5663. if (intel_vgpu_active(i915))
  5664. info->has_rc6 = 0;
  5665. if (info->has_rc6 &&
  5666. IS_GEN9_LP(i915) && !bxt_check_bios_rc6_setup(i915)) {
  5667. DRM_INFO("RC6 disabled by BIOS\n");
  5668. info->has_rc6 = 0;
  5669. }
  5670. /*
  5671. * We assume that we do not have any deep rc6 levels if we don't have
  5672. * have the previous rc6 level supported, i.e. we use HAS_RC6()
  5673. * as the initial coarse check for rc6 in general, moving on to
  5674. * progressively finer/deeper levels.
  5675. */
  5676. if (!info->has_rc6 && info->has_rc6p)
  5677. info->has_rc6p = 0;
  5678. return info->has_rc6;
  5679. }
  5680. static void gen6_init_rps_frequencies(struct drm_i915_private *dev_priv)
  5681. {
  5682. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  5683. /* All of these values are in units of 50MHz */
  5684. /* static values from HW: RP0 > RP1 > RPn (min_freq) */
  5685. if (IS_GEN9_LP(dev_priv)) {
  5686. u32 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
  5687. rps->rp0_freq = (rp_state_cap >> 16) & 0xff;
  5688. rps->rp1_freq = (rp_state_cap >> 8) & 0xff;
  5689. rps->min_freq = (rp_state_cap >> 0) & 0xff;
  5690. } else {
  5691. u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
  5692. rps->rp0_freq = (rp_state_cap >> 0) & 0xff;
  5693. rps->rp1_freq = (rp_state_cap >> 8) & 0xff;
  5694. rps->min_freq = (rp_state_cap >> 16) & 0xff;
  5695. }
  5696. /* hw_max = RP0 until we check for overclocking */
  5697. rps->max_freq = rps->rp0_freq;
  5698. rps->efficient_freq = rps->rp1_freq;
  5699. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv) ||
  5700. IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
  5701. u32 ddcc_status = 0;
  5702. if (sandybridge_pcode_read(dev_priv,
  5703. HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
  5704. &ddcc_status) == 0)
  5705. rps->efficient_freq =
  5706. clamp_t(u8,
  5707. ((ddcc_status >> 8) & 0xff),
  5708. rps->min_freq,
  5709. rps->max_freq);
  5710. }
  5711. if (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
  5712. /* Store the frequency values in 16.66 MHZ units, which is
  5713. * the natural hardware unit for SKL
  5714. */
  5715. rps->rp0_freq *= GEN9_FREQ_SCALER;
  5716. rps->rp1_freq *= GEN9_FREQ_SCALER;
  5717. rps->min_freq *= GEN9_FREQ_SCALER;
  5718. rps->max_freq *= GEN9_FREQ_SCALER;
  5719. rps->efficient_freq *= GEN9_FREQ_SCALER;
  5720. }
  5721. }
  5722. static void reset_rps(struct drm_i915_private *dev_priv,
  5723. int (*set)(struct drm_i915_private *, u8))
  5724. {
  5725. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  5726. u8 freq = rps->cur_freq;
  5727. /* force a reset */
  5728. rps->power.mode = -1;
  5729. rps->cur_freq = -1;
  5730. if (set(dev_priv, freq))
  5731. DRM_ERROR("Failed to reset RPS to initial values\n");
  5732. }
  5733. /* See the Gen9_GT_PM_Programming_Guide doc for the below */
  5734. static void gen9_enable_rps(struct drm_i915_private *dev_priv)
  5735. {
  5736. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  5737. /* Program defaults and thresholds for RPS */
  5738. if (IS_GEN9(dev_priv))
  5739. I915_WRITE(GEN6_RC_VIDEO_FREQ,
  5740. GEN9_FREQUENCY(dev_priv->gt_pm.rps.rp1_freq));
  5741. /* 1 second timeout*/
  5742. I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
  5743. GT_INTERVAL_FROM_US(dev_priv, 1000000));
  5744. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
  5745. /* Leaning on the below call to gen6_set_rps to program/setup the
  5746. * Up/Down EI & threshold registers, as well as the RP_CONTROL,
  5747. * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
  5748. reset_rps(dev_priv, gen6_set_rps);
  5749. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  5750. }
  5751. static void gen9_enable_rc6(struct drm_i915_private *dev_priv)
  5752. {
  5753. struct intel_engine_cs *engine;
  5754. enum intel_engine_id id;
  5755. u32 rc6_mode;
  5756. /* 1a: Software RC state - RC0 */
  5757. I915_WRITE(GEN6_RC_STATE, 0);
  5758. /* 1b: Get forcewake during program sequence. Although the driver
  5759. * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
  5760. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  5761. /* 2a: Disable RC states. */
  5762. I915_WRITE(GEN6_RC_CONTROL, 0);
  5763. /* 2b: Program RC6 thresholds.*/
  5764. if (INTEL_GEN(dev_priv) >= 10) {
  5765. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16 | 85);
  5766. I915_WRITE(GEN10_MEDIA_WAKE_RATE_LIMIT, 150);
  5767. } else if (IS_SKYLAKE(dev_priv)) {
  5768. /*
  5769. * WaRsDoubleRc6WrlWithCoarsePowerGating:skl Doubling WRL only
  5770. * when CPG is enabled
  5771. */
  5772. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
  5773. } else {
  5774. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
  5775. }
  5776. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
  5777. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
  5778. for_each_engine(engine, dev_priv, id)
  5779. I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
  5780. if (HAS_GUC(dev_priv))
  5781. I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
  5782. I915_WRITE(GEN6_RC_SLEEP, 0);
  5783. /*
  5784. * 2c: Program Coarse Power Gating Policies.
  5785. *
  5786. * Bspec's guidance is to use 25us (really 25 * 1280ns) here. What we
  5787. * use instead is a more conservative estimate for the maximum time
  5788. * it takes us to service a CS interrupt and submit a new ELSP - that
  5789. * is the time which the GPU is idle waiting for the CPU to select the
  5790. * next request to execute. If the idle hysteresis is less than that
  5791. * interrupt service latency, the hardware will automatically gate
  5792. * the power well and we will then incur the wake up cost on top of
  5793. * the service latency. A similar guide from intel_pstate is that we
  5794. * do not want the enable hysteresis to less than the wakeup latency.
  5795. *
  5796. * igt/gem_exec_nop/sequential provides a rough estimate for the
  5797. * service latency, and puts it around 10us for Broadwell (and other
  5798. * big core) and around 40us for Broxton (and other low power cores).
  5799. * [Note that for legacy ringbuffer submission, this is less than 1us!]
  5800. * However, the wakeup latency on Broxton is closer to 100us. To be
  5801. * conservative, we have to factor in a context switch on top (due
  5802. * to ksoftirqd).
  5803. */
  5804. I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 250);
  5805. I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 250);
  5806. /* 3a: Enable RC6 */
  5807. I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
  5808. /* WaRsUseTimeoutMode:cnl (pre-prod) */
  5809. if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_C0))
  5810. rc6_mode = GEN7_RC_CTL_TO_MODE;
  5811. else
  5812. rc6_mode = GEN6_RC_CTL_EI_MODE(1);
  5813. I915_WRITE(GEN6_RC_CONTROL,
  5814. GEN6_RC_CTL_HW_ENABLE |
  5815. GEN6_RC_CTL_RC6_ENABLE |
  5816. rc6_mode);
  5817. /*
  5818. * 3b: Enable Coarse Power Gating only when RC6 is enabled.
  5819. * WaRsDisableCoarsePowerGating:skl,cnl - Render/Media PG need to be disabled with RC6.
  5820. */
  5821. if (NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
  5822. I915_WRITE(GEN9_PG_ENABLE, 0);
  5823. else
  5824. I915_WRITE(GEN9_PG_ENABLE,
  5825. GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE);
  5826. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  5827. }
  5828. static void gen8_enable_rc6(struct drm_i915_private *dev_priv)
  5829. {
  5830. struct intel_engine_cs *engine;
  5831. enum intel_engine_id id;
  5832. /* 1a: Software RC state - RC0 */
  5833. I915_WRITE(GEN6_RC_STATE, 0);
  5834. /* 1b: Get forcewake during program sequence. Although the driver
  5835. * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
  5836. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  5837. /* 2a: Disable RC states. */
  5838. I915_WRITE(GEN6_RC_CONTROL, 0);
  5839. /* 2b: Program RC6 thresholds.*/
  5840. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
  5841. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
  5842. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
  5843. for_each_engine(engine, dev_priv, id)
  5844. I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
  5845. I915_WRITE(GEN6_RC_SLEEP, 0);
  5846. I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
  5847. /* 3: Enable RC6 */
  5848. I915_WRITE(GEN6_RC_CONTROL,
  5849. GEN6_RC_CTL_HW_ENABLE |
  5850. GEN7_RC_CTL_TO_MODE |
  5851. GEN6_RC_CTL_RC6_ENABLE);
  5852. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  5853. }
  5854. static void gen8_enable_rps(struct drm_i915_private *dev_priv)
  5855. {
  5856. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  5857. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  5858. /* 1 Program defaults and thresholds for RPS*/
  5859. I915_WRITE(GEN6_RPNSWREQ,
  5860. HSW_FREQUENCY(rps->rp1_freq));
  5861. I915_WRITE(GEN6_RC_VIDEO_FREQ,
  5862. HSW_FREQUENCY(rps->rp1_freq));
  5863. /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
  5864. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
  5865. /* Docs recommend 900MHz, and 300 MHz respectively */
  5866. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
  5867. rps->max_freq_softlimit << 24 |
  5868. rps->min_freq_softlimit << 16);
  5869. I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
  5870. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
  5871. I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
  5872. I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
  5873. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  5874. /* 2: Enable RPS */
  5875. I915_WRITE(GEN6_RP_CONTROL,
  5876. GEN6_RP_MEDIA_TURBO |
  5877. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  5878. GEN6_RP_MEDIA_IS_GFX |
  5879. GEN6_RP_ENABLE |
  5880. GEN6_RP_UP_BUSY_AVG |
  5881. GEN6_RP_DOWN_IDLE_AVG);
  5882. reset_rps(dev_priv, gen6_set_rps);
  5883. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  5884. }
  5885. static void gen6_enable_rc6(struct drm_i915_private *dev_priv)
  5886. {
  5887. struct intel_engine_cs *engine;
  5888. enum intel_engine_id id;
  5889. u32 rc6vids, rc6_mask;
  5890. u32 gtfifodbg;
  5891. int ret;
  5892. I915_WRITE(GEN6_RC_STATE, 0);
  5893. /* Clear the DBG now so we don't confuse earlier errors */
  5894. gtfifodbg = I915_READ(GTFIFODBG);
  5895. if (gtfifodbg) {
  5896. DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
  5897. I915_WRITE(GTFIFODBG, gtfifodbg);
  5898. }
  5899. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  5900. /* disable the counters and set deterministic thresholds */
  5901. I915_WRITE(GEN6_RC_CONTROL, 0);
  5902. I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
  5903. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
  5904. I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
  5905. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
  5906. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
  5907. for_each_engine(engine, dev_priv, id)
  5908. I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
  5909. I915_WRITE(GEN6_RC_SLEEP, 0);
  5910. I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
  5911. if (IS_IVYBRIDGE(dev_priv))
  5912. I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
  5913. else
  5914. I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
  5915. I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
  5916. I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
  5917. /* We don't use those on Haswell */
  5918. rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
  5919. if (HAS_RC6p(dev_priv))
  5920. rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
  5921. if (HAS_RC6pp(dev_priv))
  5922. rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
  5923. I915_WRITE(GEN6_RC_CONTROL,
  5924. rc6_mask |
  5925. GEN6_RC_CTL_EI_MODE(1) |
  5926. GEN6_RC_CTL_HW_ENABLE);
  5927. rc6vids = 0;
  5928. ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
  5929. if (IS_GEN6(dev_priv) && ret) {
  5930. DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
  5931. } else if (IS_GEN6(dev_priv) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
  5932. DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
  5933. GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
  5934. rc6vids &= 0xffff00;
  5935. rc6vids |= GEN6_ENCODE_RC6_VID(450);
  5936. ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
  5937. if (ret)
  5938. DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
  5939. }
  5940. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  5941. }
  5942. static void gen6_enable_rps(struct drm_i915_private *dev_priv)
  5943. {
  5944. /* Here begins a magic sequence of register writes to enable
  5945. * auto-downclocking.
  5946. *
  5947. * Perhaps there might be some value in exposing these to
  5948. * userspace...
  5949. */
  5950. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  5951. /* Power down if completely idle for over 50ms */
  5952. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
  5953. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  5954. reset_rps(dev_priv, gen6_set_rps);
  5955. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  5956. }
  5957. static void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
  5958. {
  5959. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  5960. const int min_freq = 15;
  5961. const int scaling_factor = 180;
  5962. unsigned int gpu_freq;
  5963. unsigned int max_ia_freq, min_ring_freq;
  5964. unsigned int max_gpu_freq, min_gpu_freq;
  5965. struct cpufreq_policy *policy;
  5966. WARN_ON(!mutex_is_locked(&dev_priv->pcu_lock));
  5967. if (rps->max_freq <= rps->min_freq)
  5968. return;
  5969. policy = cpufreq_cpu_get(0);
  5970. if (policy) {
  5971. max_ia_freq = policy->cpuinfo.max_freq;
  5972. cpufreq_cpu_put(policy);
  5973. } else {
  5974. /*
  5975. * Default to measured freq if none found, PCU will ensure we
  5976. * don't go over
  5977. */
  5978. max_ia_freq = tsc_khz;
  5979. }
  5980. /* Convert from kHz to MHz */
  5981. max_ia_freq /= 1000;
  5982. min_ring_freq = I915_READ(DCLK) & 0xf;
  5983. /* convert DDR frequency from units of 266.6MHz to bandwidth */
  5984. min_ring_freq = mult_frac(min_ring_freq, 8, 3);
  5985. min_gpu_freq = rps->min_freq;
  5986. max_gpu_freq = rps->max_freq;
  5987. if (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
  5988. /* Convert GT frequency to 50 HZ units */
  5989. min_gpu_freq /= GEN9_FREQ_SCALER;
  5990. max_gpu_freq /= GEN9_FREQ_SCALER;
  5991. }
  5992. /*
  5993. * For each potential GPU frequency, load a ring frequency we'd like
  5994. * to use for memory access. We do this by specifying the IA frequency
  5995. * the PCU should use as a reference to determine the ring frequency.
  5996. */
  5997. for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
  5998. const int diff = max_gpu_freq - gpu_freq;
  5999. unsigned int ia_freq = 0, ring_freq = 0;
  6000. if (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
  6001. /*
  6002. * ring_freq = 2 * GT. ring_freq is in 100MHz units
  6003. * No floor required for ring frequency on SKL.
  6004. */
  6005. ring_freq = gpu_freq;
  6006. } else if (INTEL_GEN(dev_priv) >= 8) {
  6007. /* max(2 * GT, DDR). NB: GT is 50MHz units */
  6008. ring_freq = max(min_ring_freq, gpu_freq);
  6009. } else if (IS_HASWELL(dev_priv)) {
  6010. ring_freq = mult_frac(gpu_freq, 5, 4);
  6011. ring_freq = max(min_ring_freq, ring_freq);
  6012. /* leave ia_freq as the default, chosen by cpufreq */
  6013. } else {
  6014. /* On older processors, there is no separate ring
  6015. * clock domain, so in order to boost the bandwidth
  6016. * of the ring, we need to upclock the CPU (ia_freq).
  6017. *
  6018. * For GPU frequencies less than 750MHz,
  6019. * just use the lowest ring freq.
  6020. */
  6021. if (gpu_freq < min_freq)
  6022. ia_freq = 800;
  6023. else
  6024. ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
  6025. ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
  6026. }
  6027. sandybridge_pcode_write(dev_priv,
  6028. GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
  6029. ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
  6030. ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
  6031. gpu_freq);
  6032. }
  6033. }
  6034. static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
  6035. {
  6036. u32 val, rp0;
  6037. val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
  6038. switch (INTEL_INFO(dev_priv)->sseu.eu_total) {
  6039. case 8:
  6040. /* (2 * 4) config */
  6041. rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
  6042. break;
  6043. case 12:
  6044. /* (2 * 6) config */
  6045. rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
  6046. break;
  6047. case 16:
  6048. /* (2 * 8) config */
  6049. default:
  6050. /* Setting (2 * 8) Min RP0 for any other combination */
  6051. rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
  6052. break;
  6053. }
  6054. rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
  6055. return rp0;
  6056. }
  6057. static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
  6058. {
  6059. u32 val, rpe;
  6060. val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
  6061. rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
  6062. return rpe;
  6063. }
  6064. static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
  6065. {
  6066. u32 val, rp1;
  6067. val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
  6068. rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
  6069. return rp1;
  6070. }
  6071. static u32 cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
  6072. {
  6073. u32 val, rpn;
  6074. val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
  6075. rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
  6076. FB_GFX_FREQ_FUSE_MASK);
  6077. return rpn;
  6078. }
  6079. static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
  6080. {
  6081. u32 val, rp1;
  6082. val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
  6083. rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
  6084. return rp1;
  6085. }
  6086. static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
  6087. {
  6088. u32 val, rp0;
  6089. val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
  6090. rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
  6091. /* Clamp to max */
  6092. rp0 = min_t(u32, rp0, 0xea);
  6093. return rp0;
  6094. }
  6095. static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
  6096. {
  6097. u32 val, rpe;
  6098. val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
  6099. rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
  6100. val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
  6101. rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
  6102. return rpe;
  6103. }
  6104. static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
  6105. {
  6106. u32 val;
  6107. val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
  6108. /*
  6109. * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
  6110. * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
  6111. * a BYT-M B0 the above register contains 0xbf. Moreover when setting
  6112. * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
  6113. * to make sure it matches what Punit accepts.
  6114. */
  6115. return max_t(u32, val, 0xc0);
  6116. }
  6117. /* Check that the pctx buffer wasn't move under us. */
  6118. static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
  6119. {
  6120. unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
  6121. WARN_ON(pctx_addr != dev_priv->dsm.start +
  6122. dev_priv->vlv_pctx->stolen->start);
  6123. }
  6124. /* Check that the pcbr address is not empty. */
  6125. static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
  6126. {
  6127. unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
  6128. WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
  6129. }
  6130. static void cherryview_setup_pctx(struct drm_i915_private *dev_priv)
  6131. {
  6132. resource_size_t pctx_paddr, paddr;
  6133. resource_size_t pctx_size = 32*1024;
  6134. u32 pcbr;
  6135. pcbr = I915_READ(VLV_PCBR);
  6136. if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
  6137. DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
  6138. paddr = dev_priv->dsm.end + 1 - pctx_size;
  6139. GEM_BUG_ON(paddr > U32_MAX);
  6140. pctx_paddr = (paddr & (~4095));
  6141. I915_WRITE(VLV_PCBR, pctx_paddr);
  6142. }
  6143. DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
  6144. }
  6145. static void valleyview_setup_pctx(struct drm_i915_private *dev_priv)
  6146. {
  6147. struct drm_i915_gem_object *pctx;
  6148. resource_size_t pctx_paddr;
  6149. resource_size_t pctx_size = 24*1024;
  6150. u32 pcbr;
  6151. pcbr = I915_READ(VLV_PCBR);
  6152. if (pcbr) {
  6153. /* BIOS set it up already, grab the pre-alloc'd space */
  6154. resource_size_t pcbr_offset;
  6155. pcbr_offset = (pcbr & (~4095)) - dev_priv->dsm.start;
  6156. pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv,
  6157. pcbr_offset,
  6158. I915_GTT_OFFSET_NONE,
  6159. pctx_size);
  6160. goto out;
  6161. }
  6162. DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
  6163. /*
  6164. * From the Gunit register HAS:
  6165. * The Gfx driver is expected to program this register and ensure
  6166. * proper allocation within Gfx stolen memory. For example, this
  6167. * register should be programmed such than the PCBR range does not
  6168. * overlap with other ranges, such as the frame buffer, protected
  6169. * memory, or any other relevant ranges.
  6170. */
  6171. pctx = i915_gem_object_create_stolen(dev_priv, pctx_size);
  6172. if (!pctx) {
  6173. DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
  6174. goto out;
  6175. }
  6176. GEM_BUG_ON(range_overflows_t(u64,
  6177. dev_priv->dsm.start,
  6178. pctx->stolen->start,
  6179. U32_MAX));
  6180. pctx_paddr = dev_priv->dsm.start + pctx->stolen->start;
  6181. I915_WRITE(VLV_PCBR, pctx_paddr);
  6182. out:
  6183. DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
  6184. dev_priv->vlv_pctx = pctx;
  6185. }
  6186. static void valleyview_cleanup_pctx(struct drm_i915_private *dev_priv)
  6187. {
  6188. struct drm_i915_gem_object *pctx;
  6189. pctx = fetch_and_zero(&dev_priv->vlv_pctx);
  6190. if (pctx)
  6191. i915_gem_object_put(pctx);
  6192. }
  6193. static void vlv_init_gpll_ref_freq(struct drm_i915_private *dev_priv)
  6194. {
  6195. dev_priv->gt_pm.rps.gpll_ref_freq =
  6196. vlv_get_cck_clock(dev_priv, "GPLL ref",
  6197. CCK_GPLL_CLOCK_CONTROL,
  6198. dev_priv->czclk_freq);
  6199. DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n",
  6200. dev_priv->gt_pm.rps.gpll_ref_freq);
  6201. }
  6202. static void valleyview_init_gt_powersave(struct drm_i915_private *dev_priv)
  6203. {
  6204. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  6205. u32 val;
  6206. valleyview_setup_pctx(dev_priv);
  6207. vlv_init_gpll_ref_freq(dev_priv);
  6208. val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
  6209. switch ((val >> 6) & 3) {
  6210. case 0:
  6211. case 1:
  6212. dev_priv->mem_freq = 800;
  6213. break;
  6214. case 2:
  6215. dev_priv->mem_freq = 1066;
  6216. break;
  6217. case 3:
  6218. dev_priv->mem_freq = 1333;
  6219. break;
  6220. }
  6221. DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
  6222. rps->max_freq = valleyview_rps_max_freq(dev_priv);
  6223. rps->rp0_freq = rps->max_freq;
  6224. DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
  6225. intel_gpu_freq(dev_priv, rps->max_freq),
  6226. rps->max_freq);
  6227. rps->efficient_freq = valleyview_rps_rpe_freq(dev_priv);
  6228. DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
  6229. intel_gpu_freq(dev_priv, rps->efficient_freq),
  6230. rps->efficient_freq);
  6231. rps->rp1_freq = valleyview_rps_guar_freq(dev_priv);
  6232. DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
  6233. intel_gpu_freq(dev_priv, rps->rp1_freq),
  6234. rps->rp1_freq);
  6235. rps->min_freq = valleyview_rps_min_freq(dev_priv);
  6236. DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
  6237. intel_gpu_freq(dev_priv, rps->min_freq),
  6238. rps->min_freq);
  6239. }
  6240. static void cherryview_init_gt_powersave(struct drm_i915_private *dev_priv)
  6241. {
  6242. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  6243. u32 val;
  6244. cherryview_setup_pctx(dev_priv);
  6245. vlv_init_gpll_ref_freq(dev_priv);
  6246. mutex_lock(&dev_priv->sb_lock);
  6247. val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
  6248. mutex_unlock(&dev_priv->sb_lock);
  6249. switch ((val >> 2) & 0x7) {
  6250. case 3:
  6251. dev_priv->mem_freq = 2000;
  6252. break;
  6253. default:
  6254. dev_priv->mem_freq = 1600;
  6255. break;
  6256. }
  6257. DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
  6258. rps->max_freq = cherryview_rps_max_freq(dev_priv);
  6259. rps->rp0_freq = rps->max_freq;
  6260. DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
  6261. intel_gpu_freq(dev_priv, rps->max_freq),
  6262. rps->max_freq);
  6263. rps->efficient_freq = cherryview_rps_rpe_freq(dev_priv);
  6264. DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
  6265. intel_gpu_freq(dev_priv, rps->efficient_freq),
  6266. rps->efficient_freq);
  6267. rps->rp1_freq = cherryview_rps_guar_freq(dev_priv);
  6268. DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
  6269. intel_gpu_freq(dev_priv, rps->rp1_freq),
  6270. rps->rp1_freq);
  6271. rps->min_freq = cherryview_rps_min_freq(dev_priv);
  6272. DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
  6273. intel_gpu_freq(dev_priv, rps->min_freq),
  6274. rps->min_freq);
  6275. WARN_ONCE((rps->max_freq | rps->efficient_freq | rps->rp1_freq |
  6276. rps->min_freq) & 1,
  6277. "Odd GPU freq values\n");
  6278. }
  6279. static void valleyview_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
  6280. {
  6281. valleyview_cleanup_pctx(dev_priv);
  6282. }
  6283. static void cherryview_enable_rc6(struct drm_i915_private *dev_priv)
  6284. {
  6285. struct intel_engine_cs *engine;
  6286. enum intel_engine_id id;
  6287. u32 gtfifodbg, rc6_mode, pcbr;
  6288. gtfifodbg = I915_READ(GTFIFODBG) & ~(GT_FIFO_SBDEDICATE_FREE_ENTRY_CHV |
  6289. GT_FIFO_FREE_ENTRIES_CHV);
  6290. if (gtfifodbg) {
  6291. DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
  6292. gtfifodbg);
  6293. I915_WRITE(GTFIFODBG, gtfifodbg);
  6294. }
  6295. cherryview_check_pctx(dev_priv);
  6296. /* 1a & 1b: Get forcewake during program sequence. Although the driver
  6297. * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
  6298. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  6299. /* Disable RC states. */
  6300. I915_WRITE(GEN6_RC_CONTROL, 0);
  6301. /* 2a: Program RC6 thresholds.*/
  6302. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
  6303. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
  6304. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
  6305. for_each_engine(engine, dev_priv, id)
  6306. I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
  6307. I915_WRITE(GEN6_RC_SLEEP, 0);
  6308. /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
  6309. I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
  6310. /* Allows RC6 residency counter to work */
  6311. I915_WRITE(VLV_COUNTER_CONTROL,
  6312. _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
  6313. VLV_MEDIA_RC6_COUNT_EN |
  6314. VLV_RENDER_RC6_COUNT_EN));
  6315. /* For now we assume BIOS is allocating and populating the PCBR */
  6316. pcbr = I915_READ(VLV_PCBR);
  6317. /* 3: Enable RC6 */
  6318. rc6_mode = 0;
  6319. if (pcbr >> VLV_PCBR_ADDR_SHIFT)
  6320. rc6_mode = GEN7_RC_CTL_TO_MODE;
  6321. I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
  6322. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  6323. }
  6324. static void cherryview_enable_rps(struct drm_i915_private *dev_priv)
  6325. {
  6326. u32 val;
  6327. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  6328. /* 1: Program defaults and thresholds for RPS*/
  6329. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
  6330. I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
  6331. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
  6332. I915_WRITE(GEN6_RP_UP_EI, 66000);
  6333. I915_WRITE(GEN6_RP_DOWN_EI, 350000);
  6334. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  6335. /* 2: Enable RPS */
  6336. I915_WRITE(GEN6_RP_CONTROL,
  6337. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  6338. GEN6_RP_MEDIA_IS_GFX |
  6339. GEN6_RP_ENABLE |
  6340. GEN6_RP_UP_BUSY_AVG |
  6341. GEN6_RP_DOWN_IDLE_AVG);
  6342. /* Setting Fixed Bias */
  6343. val = VLV_OVERRIDE_EN |
  6344. VLV_SOC_TDP_EN |
  6345. CHV_BIAS_CPU_50_SOC_50;
  6346. vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
  6347. val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
  6348. /* RPS code assumes GPLL is used */
  6349. WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
  6350. DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
  6351. DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
  6352. reset_rps(dev_priv, valleyview_set_rps);
  6353. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  6354. }
  6355. static void valleyview_enable_rc6(struct drm_i915_private *dev_priv)
  6356. {
  6357. struct intel_engine_cs *engine;
  6358. enum intel_engine_id id;
  6359. u32 gtfifodbg;
  6360. valleyview_check_pctx(dev_priv);
  6361. gtfifodbg = I915_READ(GTFIFODBG);
  6362. if (gtfifodbg) {
  6363. DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
  6364. gtfifodbg);
  6365. I915_WRITE(GTFIFODBG, gtfifodbg);
  6366. }
  6367. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  6368. /* Disable RC states. */
  6369. I915_WRITE(GEN6_RC_CONTROL, 0);
  6370. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
  6371. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
  6372. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
  6373. for_each_engine(engine, dev_priv, id)
  6374. I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
  6375. I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
  6376. /* Allows RC6 residency counter to work */
  6377. I915_WRITE(VLV_COUNTER_CONTROL,
  6378. _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
  6379. VLV_MEDIA_RC0_COUNT_EN |
  6380. VLV_RENDER_RC0_COUNT_EN |
  6381. VLV_MEDIA_RC6_COUNT_EN |
  6382. VLV_RENDER_RC6_COUNT_EN));
  6383. I915_WRITE(GEN6_RC_CONTROL,
  6384. GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL);
  6385. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  6386. }
  6387. static void valleyview_enable_rps(struct drm_i915_private *dev_priv)
  6388. {
  6389. u32 val;
  6390. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  6391. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
  6392. I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
  6393. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
  6394. I915_WRITE(GEN6_RP_UP_EI, 66000);
  6395. I915_WRITE(GEN6_RP_DOWN_EI, 350000);
  6396. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  6397. I915_WRITE(GEN6_RP_CONTROL,
  6398. GEN6_RP_MEDIA_TURBO |
  6399. GEN6_RP_MEDIA_HW_NORMAL_MODE |
  6400. GEN6_RP_MEDIA_IS_GFX |
  6401. GEN6_RP_ENABLE |
  6402. GEN6_RP_UP_BUSY_AVG |
  6403. GEN6_RP_DOWN_IDLE_CONT);
  6404. /* Setting Fixed Bias */
  6405. val = VLV_OVERRIDE_EN |
  6406. VLV_SOC_TDP_EN |
  6407. VLV_BIAS_CPU_125_SOC_875;
  6408. vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
  6409. val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
  6410. /* RPS code assumes GPLL is used */
  6411. WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
  6412. DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
  6413. DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
  6414. reset_rps(dev_priv, valleyview_set_rps);
  6415. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  6416. }
  6417. static unsigned long intel_pxfreq(u32 vidfreq)
  6418. {
  6419. unsigned long freq;
  6420. int div = (vidfreq & 0x3f0000) >> 16;
  6421. int post = (vidfreq & 0x3000) >> 12;
  6422. int pre = (vidfreq & 0x7);
  6423. if (!pre)
  6424. return 0;
  6425. freq = ((div * 133333) / ((1<<post) * pre));
  6426. return freq;
  6427. }
  6428. static const struct cparams {
  6429. u16 i;
  6430. u16 t;
  6431. u16 m;
  6432. u16 c;
  6433. } cparams[] = {
  6434. { 1, 1333, 301, 28664 },
  6435. { 1, 1066, 294, 24460 },
  6436. { 1, 800, 294, 25192 },
  6437. { 0, 1333, 276, 27605 },
  6438. { 0, 1066, 276, 27605 },
  6439. { 0, 800, 231, 23784 },
  6440. };
  6441. static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
  6442. {
  6443. u64 total_count, diff, ret;
  6444. u32 count1, count2, count3, m = 0, c = 0;
  6445. unsigned long now = jiffies_to_msecs(jiffies), diff1;
  6446. int i;
  6447. lockdep_assert_held(&mchdev_lock);
  6448. diff1 = now - dev_priv->ips.last_time1;
  6449. /* Prevent division-by-zero if we are asking too fast.
  6450. * Also, we don't get interesting results if we are polling
  6451. * faster than once in 10ms, so just return the saved value
  6452. * in such cases.
  6453. */
  6454. if (diff1 <= 10)
  6455. return dev_priv->ips.chipset_power;
  6456. count1 = I915_READ(DMIEC);
  6457. count2 = I915_READ(DDREC);
  6458. count3 = I915_READ(CSIEC);
  6459. total_count = count1 + count2 + count3;
  6460. /* FIXME: handle per-counter overflow */
  6461. if (total_count < dev_priv->ips.last_count1) {
  6462. diff = ~0UL - dev_priv->ips.last_count1;
  6463. diff += total_count;
  6464. } else {
  6465. diff = total_count - dev_priv->ips.last_count1;
  6466. }
  6467. for (i = 0; i < ARRAY_SIZE(cparams); i++) {
  6468. if (cparams[i].i == dev_priv->ips.c_m &&
  6469. cparams[i].t == dev_priv->ips.r_t) {
  6470. m = cparams[i].m;
  6471. c = cparams[i].c;
  6472. break;
  6473. }
  6474. }
  6475. diff = div_u64(diff, diff1);
  6476. ret = ((m * diff) + c);
  6477. ret = div_u64(ret, 10);
  6478. dev_priv->ips.last_count1 = total_count;
  6479. dev_priv->ips.last_time1 = now;
  6480. dev_priv->ips.chipset_power = ret;
  6481. return ret;
  6482. }
  6483. unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
  6484. {
  6485. unsigned long val;
  6486. if (!IS_GEN5(dev_priv))
  6487. return 0;
  6488. spin_lock_irq(&mchdev_lock);
  6489. val = __i915_chipset_val(dev_priv);
  6490. spin_unlock_irq(&mchdev_lock);
  6491. return val;
  6492. }
  6493. unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
  6494. {
  6495. unsigned long m, x, b;
  6496. u32 tsfs;
  6497. tsfs = I915_READ(TSFS);
  6498. m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
  6499. x = I915_READ8(TR1);
  6500. b = tsfs & TSFS_INTR_MASK;
  6501. return ((m * x) / 127) - b;
  6502. }
  6503. static int _pxvid_to_vd(u8 pxvid)
  6504. {
  6505. if (pxvid == 0)
  6506. return 0;
  6507. if (pxvid >= 8 && pxvid < 31)
  6508. pxvid = 31;
  6509. return (pxvid + 2) * 125;
  6510. }
  6511. static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
  6512. {
  6513. const int vd = _pxvid_to_vd(pxvid);
  6514. const int vm = vd - 1125;
  6515. if (INTEL_INFO(dev_priv)->is_mobile)
  6516. return vm > 0 ? vm : 0;
  6517. return vd;
  6518. }
  6519. static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
  6520. {
  6521. u64 now, diff, diffms;
  6522. u32 count;
  6523. lockdep_assert_held(&mchdev_lock);
  6524. now = ktime_get_raw_ns();
  6525. diffms = now - dev_priv->ips.last_time2;
  6526. do_div(diffms, NSEC_PER_MSEC);
  6527. /* Don't divide by 0 */
  6528. if (!diffms)
  6529. return;
  6530. count = I915_READ(GFXEC);
  6531. if (count < dev_priv->ips.last_count2) {
  6532. diff = ~0UL - dev_priv->ips.last_count2;
  6533. diff += count;
  6534. } else {
  6535. diff = count - dev_priv->ips.last_count2;
  6536. }
  6537. dev_priv->ips.last_count2 = count;
  6538. dev_priv->ips.last_time2 = now;
  6539. /* More magic constants... */
  6540. diff = diff * 1181;
  6541. diff = div_u64(diff, diffms * 10);
  6542. dev_priv->ips.gfx_power = diff;
  6543. }
  6544. void i915_update_gfx_val(struct drm_i915_private *dev_priv)
  6545. {
  6546. if (!IS_GEN5(dev_priv))
  6547. return;
  6548. spin_lock_irq(&mchdev_lock);
  6549. __i915_update_gfx_val(dev_priv);
  6550. spin_unlock_irq(&mchdev_lock);
  6551. }
  6552. static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
  6553. {
  6554. unsigned long t, corr, state1, corr2, state2;
  6555. u32 pxvid, ext_v;
  6556. lockdep_assert_held(&mchdev_lock);
  6557. pxvid = I915_READ(PXVFREQ(dev_priv->gt_pm.rps.cur_freq));
  6558. pxvid = (pxvid >> 24) & 0x7f;
  6559. ext_v = pvid_to_extvid(dev_priv, pxvid);
  6560. state1 = ext_v;
  6561. t = i915_mch_val(dev_priv);
  6562. /* Revel in the empirically derived constants */
  6563. /* Correction factor in 1/100000 units */
  6564. if (t > 80)
  6565. corr = ((t * 2349) + 135940);
  6566. else if (t >= 50)
  6567. corr = ((t * 964) + 29317);
  6568. else /* < 50 */
  6569. corr = ((t * 301) + 1004);
  6570. corr = corr * ((150142 * state1) / 10000 - 78642);
  6571. corr /= 100000;
  6572. corr2 = (corr * dev_priv->ips.corr);
  6573. state2 = (corr2 * state1) / 10000;
  6574. state2 /= 100; /* convert to mW */
  6575. __i915_update_gfx_val(dev_priv);
  6576. return dev_priv->ips.gfx_power + state2;
  6577. }
  6578. unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
  6579. {
  6580. unsigned long val;
  6581. if (!IS_GEN5(dev_priv))
  6582. return 0;
  6583. spin_lock_irq(&mchdev_lock);
  6584. val = __i915_gfx_val(dev_priv);
  6585. spin_unlock_irq(&mchdev_lock);
  6586. return val;
  6587. }
  6588. /**
  6589. * i915_read_mch_val - return value for IPS use
  6590. *
  6591. * Calculate and return a value for the IPS driver to use when deciding whether
  6592. * we have thermal and power headroom to increase CPU or GPU power budget.
  6593. */
  6594. unsigned long i915_read_mch_val(void)
  6595. {
  6596. struct drm_i915_private *dev_priv;
  6597. unsigned long chipset_val, graphics_val, ret = 0;
  6598. spin_lock_irq(&mchdev_lock);
  6599. if (!i915_mch_dev)
  6600. goto out_unlock;
  6601. dev_priv = i915_mch_dev;
  6602. chipset_val = __i915_chipset_val(dev_priv);
  6603. graphics_val = __i915_gfx_val(dev_priv);
  6604. ret = chipset_val + graphics_val;
  6605. out_unlock:
  6606. spin_unlock_irq(&mchdev_lock);
  6607. return ret;
  6608. }
  6609. EXPORT_SYMBOL_GPL(i915_read_mch_val);
  6610. /**
  6611. * i915_gpu_raise - raise GPU frequency limit
  6612. *
  6613. * Raise the limit; IPS indicates we have thermal headroom.
  6614. */
  6615. bool i915_gpu_raise(void)
  6616. {
  6617. struct drm_i915_private *dev_priv;
  6618. bool ret = true;
  6619. spin_lock_irq(&mchdev_lock);
  6620. if (!i915_mch_dev) {
  6621. ret = false;
  6622. goto out_unlock;
  6623. }
  6624. dev_priv = i915_mch_dev;
  6625. if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
  6626. dev_priv->ips.max_delay--;
  6627. out_unlock:
  6628. spin_unlock_irq(&mchdev_lock);
  6629. return ret;
  6630. }
  6631. EXPORT_SYMBOL_GPL(i915_gpu_raise);
  6632. /**
  6633. * i915_gpu_lower - lower GPU frequency limit
  6634. *
  6635. * IPS indicates we're close to a thermal limit, so throttle back the GPU
  6636. * frequency maximum.
  6637. */
  6638. bool i915_gpu_lower(void)
  6639. {
  6640. struct drm_i915_private *dev_priv;
  6641. bool ret = true;
  6642. spin_lock_irq(&mchdev_lock);
  6643. if (!i915_mch_dev) {
  6644. ret = false;
  6645. goto out_unlock;
  6646. }
  6647. dev_priv = i915_mch_dev;
  6648. if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
  6649. dev_priv->ips.max_delay++;
  6650. out_unlock:
  6651. spin_unlock_irq(&mchdev_lock);
  6652. return ret;
  6653. }
  6654. EXPORT_SYMBOL_GPL(i915_gpu_lower);
  6655. /**
  6656. * i915_gpu_busy - indicate GPU business to IPS
  6657. *
  6658. * Tell the IPS driver whether or not the GPU is busy.
  6659. */
  6660. bool i915_gpu_busy(void)
  6661. {
  6662. bool ret = false;
  6663. spin_lock_irq(&mchdev_lock);
  6664. if (i915_mch_dev)
  6665. ret = i915_mch_dev->gt.awake;
  6666. spin_unlock_irq(&mchdev_lock);
  6667. return ret;
  6668. }
  6669. EXPORT_SYMBOL_GPL(i915_gpu_busy);
  6670. /**
  6671. * i915_gpu_turbo_disable - disable graphics turbo
  6672. *
  6673. * Disable graphics turbo by resetting the max frequency and setting the
  6674. * current frequency to the default.
  6675. */
  6676. bool i915_gpu_turbo_disable(void)
  6677. {
  6678. struct drm_i915_private *dev_priv;
  6679. bool ret = true;
  6680. spin_lock_irq(&mchdev_lock);
  6681. if (!i915_mch_dev) {
  6682. ret = false;
  6683. goto out_unlock;
  6684. }
  6685. dev_priv = i915_mch_dev;
  6686. dev_priv->ips.max_delay = dev_priv->ips.fstart;
  6687. if (!ironlake_set_drps(dev_priv, dev_priv->ips.fstart))
  6688. ret = false;
  6689. out_unlock:
  6690. spin_unlock_irq(&mchdev_lock);
  6691. return ret;
  6692. }
  6693. EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
  6694. /**
  6695. * Tells the intel_ips driver that the i915 driver is now loaded, if
  6696. * IPS got loaded first.
  6697. *
  6698. * This awkward dance is so that neither module has to depend on the
  6699. * other in order for IPS to do the appropriate communication of
  6700. * GPU turbo limits to i915.
  6701. */
  6702. static void
  6703. ips_ping_for_i915_load(void)
  6704. {
  6705. void (*link)(void);
  6706. link = symbol_get(ips_link_to_i915_driver);
  6707. if (link) {
  6708. link();
  6709. symbol_put(ips_link_to_i915_driver);
  6710. }
  6711. }
  6712. void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
  6713. {
  6714. /* We only register the i915 ips part with intel-ips once everything is
  6715. * set up, to avoid intel-ips sneaking in and reading bogus values. */
  6716. spin_lock_irq(&mchdev_lock);
  6717. i915_mch_dev = dev_priv;
  6718. spin_unlock_irq(&mchdev_lock);
  6719. ips_ping_for_i915_load();
  6720. }
  6721. void intel_gpu_ips_teardown(void)
  6722. {
  6723. spin_lock_irq(&mchdev_lock);
  6724. i915_mch_dev = NULL;
  6725. spin_unlock_irq(&mchdev_lock);
  6726. }
  6727. static void intel_init_emon(struct drm_i915_private *dev_priv)
  6728. {
  6729. u32 lcfuse;
  6730. u8 pxw[16];
  6731. int i;
  6732. /* Disable to program */
  6733. I915_WRITE(ECR, 0);
  6734. POSTING_READ(ECR);
  6735. /* Program energy weights for various events */
  6736. I915_WRITE(SDEW, 0x15040d00);
  6737. I915_WRITE(CSIEW0, 0x007f0000);
  6738. I915_WRITE(CSIEW1, 0x1e220004);
  6739. I915_WRITE(CSIEW2, 0x04000004);
  6740. for (i = 0; i < 5; i++)
  6741. I915_WRITE(PEW(i), 0);
  6742. for (i = 0; i < 3; i++)
  6743. I915_WRITE(DEW(i), 0);
  6744. /* Program P-state weights to account for frequency power adjustment */
  6745. for (i = 0; i < 16; i++) {
  6746. u32 pxvidfreq = I915_READ(PXVFREQ(i));
  6747. unsigned long freq = intel_pxfreq(pxvidfreq);
  6748. unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
  6749. PXVFREQ_PX_SHIFT;
  6750. unsigned long val;
  6751. val = vid * vid;
  6752. val *= (freq / 1000);
  6753. val *= 255;
  6754. val /= (127*127*900);
  6755. if (val > 0xff)
  6756. DRM_ERROR("bad pxval: %ld\n", val);
  6757. pxw[i] = val;
  6758. }
  6759. /* Render standby states get 0 weight */
  6760. pxw[14] = 0;
  6761. pxw[15] = 0;
  6762. for (i = 0; i < 4; i++) {
  6763. u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
  6764. (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
  6765. I915_WRITE(PXW(i), val);
  6766. }
  6767. /* Adjust magic regs to magic values (more experimental results) */
  6768. I915_WRITE(OGW0, 0);
  6769. I915_WRITE(OGW1, 0);
  6770. I915_WRITE(EG0, 0x00007f00);
  6771. I915_WRITE(EG1, 0x0000000e);
  6772. I915_WRITE(EG2, 0x000e0000);
  6773. I915_WRITE(EG3, 0x68000300);
  6774. I915_WRITE(EG4, 0x42000000);
  6775. I915_WRITE(EG5, 0x00140031);
  6776. I915_WRITE(EG6, 0);
  6777. I915_WRITE(EG7, 0);
  6778. for (i = 0; i < 8; i++)
  6779. I915_WRITE(PXWL(i), 0);
  6780. /* Enable PMON + select events */
  6781. I915_WRITE(ECR, 0x80000019);
  6782. lcfuse = I915_READ(LCFUSE02);
  6783. dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
  6784. }
  6785. void intel_init_gt_powersave(struct drm_i915_private *dev_priv)
  6786. {
  6787. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  6788. /*
  6789. * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
  6790. * requirement.
  6791. */
  6792. if (!sanitize_rc6(dev_priv)) {
  6793. DRM_INFO("RC6 disabled, disabling runtime PM support\n");
  6794. pm_runtime_get(&dev_priv->drm.pdev->dev);
  6795. }
  6796. mutex_lock(&dev_priv->pcu_lock);
  6797. /* Initialize RPS limits (for userspace) */
  6798. if (IS_CHERRYVIEW(dev_priv))
  6799. cherryview_init_gt_powersave(dev_priv);
  6800. else if (IS_VALLEYVIEW(dev_priv))
  6801. valleyview_init_gt_powersave(dev_priv);
  6802. else if (INTEL_GEN(dev_priv) >= 6)
  6803. gen6_init_rps_frequencies(dev_priv);
  6804. /* Derive initial user preferences/limits from the hardware limits */
  6805. rps->idle_freq = rps->min_freq;
  6806. rps->cur_freq = rps->idle_freq;
  6807. rps->max_freq_softlimit = rps->max_freq;
  6808. rps->min_freq_softlimit = rps->min_freq;
  6809. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  6810. rps->min_freq_softlimit =
  6811. max_t(int,
  6812. rps->efficient_freq,
  6813. intel_freq_opcode(dev_priv, 450));
  6814. /* After setting max-softlimit, find the overclock max freq */
  6815. if (IS_GEN6(dev_priv) ||
  6816. IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
  6817. u32 params = 0;
  6818. sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &params);
  6819. if (params & BIT(31)) { /* OC supported */
  6820. DRM_DEBUG_DRIVER("Overclocking supported, max: %dMHz, overclock: %dMHz\n",
  6821. (rps->max_freq & 0xff) * 50,
  6822. (params & 0xff) * 50);
  6823. rps->max_freq = params & 0xff;
  6824. }
  6825. }
  6826. /* Finally allow us to boost to max by default */
  6827. rps->boost_freq = rps->max_freq;
  6828. mutex_unlock(&dev_priv->pcu_lock);
  6829. }
  6830. void intel_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
  6831. {
  6832. if (IS_VALLEYVIEW(dev_priv))
  6833. valleyview_cleanup_gt_powersave(dev_priv);
  6834. if (!HAS_RC6(dev_priv))
  6835. pm_runtime_put(&dev_priv->drm.pdev->dev);
  6836. }
  6837. /**
  6838. * intel_suspend_gt_powersave - suspend PM work and helper threads
  6839. * @dev_priv: i915 device
  6840. *
  6841. * We don't want to disable RC6 or other features here, we just want
  6842. * to make sure any work we've queued has finished and won't bother
  6843. * us while we're suspended.
  6844. */
  6845. void intel_suspend_gt_powersave(struct drm_i915_private *dev_priv)
  6846. {
  6847. if (INTEL_GEN(dev_priv) < 6)
  6848. return;
  6849. /* gen6_rps_idle() will be called later to disable interrupts */
  6850. }
  6851. void intel_sanitize_gt_powersave(struct drm_i915_private *dev_priv)
  6852. {
  6853. dev_priv->gt_pm.rps.enabled = true; /* force RPS disabling */
  6854. dev_priv->gt_pm.rc6.enabled = true; /* force RC6 disabling */
  6855. intel_disable_gt_powersave(dev_priv);
  6856. if (INTEL_GEN(dev_priv) >= 11)
  6857. gen11_reset_rps_interrupts(dev_priv);
  6858. else if (INTEL_GEN(dev_priv) >= 6)
  6859. gen6_reset_rps_interrupts(dev_priv);
  6860. }
  6861. static inline void intel_disable_llc_pstate(struct drm_i915_private *i915)
  6862. {
  6863. lockdep_assert_held(&i915->pcu_lock);
  6864. if (!i915->gt_pm.llc_pstate.enabled)
  6865. return;
  6866. /* Currently there is no HW configuration to be done to disable. */
  6867. i915->gt_pm.llc_pstate.enabled = false;
  6868. }
  6869. static void intel_disable_rc6(struct drm_i915_private *dev_priv)
  6870. {
  6871. lockdep_assert_held(&dev_priv->pcu_lock);
  6872. if (!dev_priv->gt_pm.rc6.enabled)
  6873. return;
  6874. if (INTEL_GEN(dev_priv) >= 9)
  6875. gen9_disable_rc6(dev_priv);
  6876. else if (IS_CHERRYVIEW(dev_priv))
  6877. cherryview_disable_rc6(dev_priv);
  6878. else if (IS_VALLEYVIEW(dev_priv))
  6879. valleyview_disable_rc6(dev_priv);
  6880. else if (INTEL_GEN(dev_priv) >= 6)
  6881. gen6_disable_rc6(dev_priv);
  6882. dev_priv->gt_pm.rc6.enabled = false;
  6883. }
  6884. static void intel_disable_rps(struct drm_i915_private *dev_priv)
  6885. {
  6886. lockdep_assert_held(&dev_priv->pcu_lock);
  6887. if (!dev_priv->gt_pm.rps.enabled)
  6888. return;
  6889. if (INTEL_GEN(dev_priv) >= 9)
  6890. gen9_disable_rps(dev_priv);
  6891. else if (IS_CHERRYVIEW(dev_priv))
  6892. cherryview_disable_rps(dev_priv);
  6893. else if (IS_VALLEYVIEW(dev_priv))
  6894. valleyview_disable_rps(dev_priv);
  6895. else if (INTEL_GEN(dev_priv) >= 6)
  6896. gen6_disable_rps(dev_priv);
  6897. else if (IS_IRONLAKE_M(dev_priv))
  6898. ironlake_disable_drps(dev_priv);
  6899. dev_priv->gt_pm.rps.enabled = false;
  6900. }
  6901. void intel_disable_gt_powersave(struct drm_i915_private *dev_priv)
  6902. {
  6903. mutex_lock(&dev_priv->pcu_lock);
  6904. intel_disable_rc6(dev_priv);
  6905. intel_disable_rps(dev_priv);
  6906. if (HAS_LLC(dev_priv))
  6907. intel_disable_llc_pstate(dev_priv);
  6908. mutex_unlock(&dev_priv->pcu_lock);
  6909. }
  6910. static inline void intel_enable_llc_pstate(struct drm_i915_private *i915)
  6911. {
  6912. lockdep_assert_held(&i915->pcu_lock);
  6913. if (i915->gt_pm.llc_pstate.enabled)
  6914. return;
  6915. gen6_update_ring_freq(i915);
  6916. i915->gt_pm.llc_pstate.enabled = true;
  6917. }
  6918. static void intel_enable_rc6(struct drm_i915_private *dev_priv)
  6919. {
  6920. lockdep_assert_held(&dev_priv->pcu_lock);
  6921. if (dev_priv->gt_pm.rc6.enabled)
  6922. return;
  6923. if (IS_CHERRYVIEW(dev_priv))
  6924. cherryview_enable_rc6(dev_priv);
  6925. else if (IS_VALLEYVIEW(dev_priv))
  6926. valleyview_enable_rc6(dev_priv);
  6927. else if (INTEL_GEN(dev_priv) >= 9)
  6928. gen9_enable_rc6(dev_priv);
  6929. else if (IS_BROADWELL(dev_priv))
  6930. gen8_enable_rc6(dev_priv);
  6931. else if (INTEL_GEN(dev_priv) >= 6)
  6932. gen6_enable_rc6(dev_priv);
  6933. dev_priv->gt_pm.rc6.enabled = true;
  6934. }
  6935. static void intel_enable_rps(struct drm_i915_private *dev_priv)
  6936. {
  6937. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  6938. lockdep_assert_held(&dev_priv->pcu_lock);
  6939. if (rps->enabled)
  6940. return;
  6941. if (IS_CHERRYVIEW(dev_priv)) {
  6942. cherryview_enable_rps(dev_priv);
  6943. } else if (IS_VALLEYVIEW(dev_priv)) {
  6944. valleyview_enable_rps(dev_priv);
  6945. } else if (INTEL_GEN(dev_priv) >= 9) {
  6946. gen9_enable_rps(dev_priv);
  6947. } else if (IS_BROADWELL(dev_priv)) {
  6948. gen8_enable_rps(dev_priv);
  6949. } else if (INTEL_GEN(dev_priv) >= 6) {
  6950. gen6_enable_rps(dev_priv);
  6951. } else if (IS_IRONLAKE_M(dev_priv)) {
  6952. ironlake_enable_drps(dev_priv);
  6953. intel_init_emon(dev_priv);
  6954. }
  6955. WARN_ON(rps->max_freq < rps->min_freq);
  6956. WARN_ON(rps->idle_freq > rps->max_freq);
  6957. WARN_ON(rps->efficient_freq < rps->min_freq);
  6958. WARN_ON(rps->efficient_freq > rps->max_freq);
  6959. rps->enabled = true;
  6960. }
  6961. void intel_enable_gt_powersave(struct drm_i915_private *dev_priv)
  6962. {
  6963. /* Powersaving is controlled by the host when inside a VM */
  6964. if (intel_vgpu_active(dev_priv))
  6965. return;
  6966. mutex_lock(&dev_priv->pcu_lock);
  6967. if (HAS_RC6(dev_priv))
  6968. intel_enable_rc6(dev_priv);
  6969. intel_enable_rps(dev_priv);
  6970. if (HAS_LLC(dev_priv))
  6971. intel_enable_llc_pstate(dev_priv);
  6972. mutex_unlock(&dev_priv->pcu_lock);
  6973. }
  6974. static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
  6975. {
  6976. /*
  6977. * On Ibex Peak and Cougar Point, we need to disable clock
  6978. * gating for the panel power sequencer or it will fail to
  6979. * start up when no ports are active.
  6980. */
  6981. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
  6982. }
  6983. static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
  6984. {
  6985. enum pipe pipe;
  6986. for_each_pipe(dev_priv, pipe) {
  6987. I915_WRITE(DSPCNTR(pipe),
  6988. I915_READ(DSPCNTR(pipe)) |
  6989. DISPPLANE_TRICKLE_FEED_DISABLE);
  6990. I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
  6991. POSTING_READ(DSPSURF(pipe));
  6992. }
  6993. }
  6994. static void ilk_init_clock_gating(struct drm_i915_private *dev_priv)
  6995. {
  6996. uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
  6997. /*
  6998. * Required for FBC
  6999. * WaFbcDisableDpfcClockGating:ilk
  7000. */
  7001. dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
  7002. ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
  7003. ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
  7004. I915_WRITE(PCH_3DCGDIS0,
  7005. MARIUNIT_CLOCK_GATE_DISABLE |
  7006. SVSMUNIT_CLOCK_GATE_DISABLE);
  7007. I915_WRITE(PCH_3DCGDIS1,
  7008. VFMUNIT_CLOCK_GATE_DISABLE);
  7009. /*
  7010. * According to the spec the following bits should be set in
  7011. * order to enable memory self-refresh
  7012. * The bit 22/21 of 0x42004
  7013. * The bit 5 of 0x42020
  7014. * The bit 15 of 0x45000
  7015. */
  7016. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  7017. (I915_READ(ILK_DISPLAY_CHICKEN2) |
  7018. ILK_DPARB_GATE | ILK_VSDPFD_FULL));
  7019. dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
  7020. I915_WRITE(DISP_ARB_CTL,
  7021. (I915_READ(DISP_ARB_CTL) |
  7022. DISP_FBC_WM_DIS));
  7023. /*
  7024. * Based on the document from hardware guys the following bits
  7025. * should be set unconditionally in order to enable FBC.
  7026. * The bit 22 of 0x42000
  7027. * The bit 22 of 0x42004
  7028. * The bit 7,8,9 of 0x42020.
  7029. */
  7030. if (IS_IRONLAKE_M(dev_priv)) {
  7031. /* WaFbcAsynchFlipDisableFbcQueue:ilk */
  7032. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  7033. I915_READ(ILK_DISPLAY_CHICKEN1) |
  7034. ILK_FBCQ_DIS);
  7035. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  7036. I915_READ(ILK_DISPLAY_CHICKEN2) |
  7037. ILK_DPARB_GATE);
  7038. }
  7039. I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
  7040. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  7041. I915_READ(ILK_DISPLAY_CHICKEN2) |
  7042. ILK_ELPIN_409_SELECT);
  7043. I915_WRITE(_3D_CHICKEN2,
  7044. _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
  7045. _3D_CHICKEN2_WM_READ_PIPELINED);
  7046. /* WaDisableRenderCachePipelinedFlush:ilk */
  7047. I915_WRITE(CACHE_MODE_0,
  7048. _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
  7049. /* WaDisable_RenderCache_OperationalFlush:ilk */
  7050. I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  7051. g4x_disable_trickle_feed(dev_priv);
  7052. ibx_init_clock_gating(dev_priv);
  7053. }
  7054. static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
  7055. {
  7056. int pipe;
  7057. uint32_t val;
  7058. /*
  7059. * On Ibex Peak and Cougar Point, we need to disable clock
  7060. * gating for the panel power sequencer or it will fail to
  7061. * start up when no ports are active.
  7062. */
  7063. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
  7064. PCH_DPLUNIT_CLOCK_GATE_DISABLE |
  7065. PCH_CPUNIT_CLOCK_GATE_DISABLE);
  7066. I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
  7067. DPLS_EDP_PPS_FIX_DIS);
  7068. /* The below fixes the weird display corruption, a few pixels shifted
  7069. * downward, on (only) LVDS of some HP laptops with IVY.
  7070. */
  7071. for_each_pipe(dev_priv, pipe) {
  7072. val = I915_READ(TRANS_CHICKEN2(pipe));
  7073. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  7074. val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
  7075. if (dev_priv->vbt.fdi_rx_polarity_inverted)
  7076. val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
  7077. val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
  7078. val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
  7079. val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
  7080. I915_WRITE(TRANS_CHICKEN2(pipe), val);
  7081. }
  7082. /* WADP0ClockGatingDisable */
  7083. for_each_pipe(dev_priv, pipe) {
  7084. I915_WRITE(TRANS_CHICKEN1(pipe),
  7085. TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
  7086. }
  7087. }
  7088. static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
  7089. {
  7090. uint32_t tmp;
  7091. tmp = I915_READ(MCH_SSKPD);
  7092. if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
  7093. DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
  7094. tmp);
  7095. }
  7096. static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
  7097. {
  7098. uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
  7099. I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
  7100. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  7101. I915_READ(ILK_DISPLAY_CHICKEN2) |
  7102. ILK_ELPIN_409_SELECT);
  7103. /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
  7104. I915_WRITE(_3D_CHICKEN,
  7105. _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
  7106. /* WaDisable_RenderCache_OperationalFlush:snb */
  7107. I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  7108. /*
  7109. * BSpec recoomends 8x4 when MSAA is used,
  7110. * however in practice 16x4 seems fastest.
  7111. *
  7112. * Note that PS/WM thread counts depend on the WIZ hashing
  7113. * disable bit, which we don't touch here, but it's good
  7114. * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
  7115. */
  7116. I915_WRITE(GEN6_GT_MODE,
  7117. _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
  7118. I915_WRITE(CACHE_MODE_0,
  7119. _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
  7120. I915_WRITE(GEN6_UCGCTL1,
  7121. I915_READ(GEN6_UCGCTL1) |
  7122. GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
  7123. GEN6_CSUNIT_CLOCK_GATE_DISABLE);
  7124. /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
  7125. * gating disable must be set. Failure to set it results in
  7126. * flickering pixels due to Z write ordering failures after
  7127. * some amount of runtime in the Mesa "fire" demo, and Unigine
  7128. * Sanctuary and Tropics, and apparently anything else with
  7129. * alpha test or pixel discard.
  7130. *
  7131. * According to the spec, bit 11 (RCCUNIT) must also be set,
  7132. * but we didn't debug actual testcases to find it out.
  7133. *
  7134. * WaDisableRCCUnitClockGating:snb
  7135. * WaDisableRCPBUnitClockGating:snb
  7136. */
  7137. I915_WRITE(GEN6_UCGCTL2,
  7138. GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
  7139. GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
  7140. /* WaStripsFansDisableFastClipPerformanceFix:snb */
  7141. I915_WRITE(_3D_CHICKEN3,
  7142. _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
  7143. /*
  7144. * Bspec says:
  7145. * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
  7146. * 3DSTATE_SF number of SF output attributes is more than 16."
  7147. */
  7148. I915_WRITE(_3D_CHICKEN3,
  7149. _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
  7150. /*
  7151. * According to the spec the following bits should be
  7152. * set in order to enable memory self-refresh and fbc:
  7153. * The bit21 and bit22 of 0x42000
  7154. * The bit21 and bit22 of 0x42004
  7155. * The bit5 and bit7 of 0x42020
  7156. * The bit14 of 0x70180
  7157. * The bit14 of 0x71180
  7158. *
  7159. * WaFbcAsynchFlipDisableFbcQueue:snb
  7160. */
  7161. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  7162. I915_READ(ILK_DISPLAY_CHICKEN1) |
  7163. ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
  7164. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  7165. I915_READ(ILK_DISPLAY_CHICKEN2) |
  7166. ILK_DPARB_GATE | ILK_VSDPFD_FULL);
  7167. I915_WRITE(ILK_DSPCLK_GATE_D,
  7168. I915_READ(ILK_DSPCLK_GATE_D) |
  7169. ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
  7170. ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
  7171. g4x_disable_trickle_feed(dev_priv);
  7172. cpt_init_clock_gating(dev_priv);
  7173. gen6_check_mch_setup(dev_priv);
  7174. }
  7175. static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
  7176. {
  7177. uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
  7178. /*
  7179. * WaVSThreadDispatchOverride:ivb,vlv
  7180. *
  7181. * This actually overrides the dispatch
  7182. * mode for all thread types.
  7183. */
  7184. reg &= ~GEN7_FF_SCHED_MASK;
  7185. reg |= GEN7_FF_TS_SCHED_HW;
  7186. reg |= GEN7_FF_VS_SCHED_HW;
  7187. reg |= GEN7_FF_DS_SCHED_HW;
  7188. I915_WRITE(GEN7_FF_THREAD_MODE, reg);
  7189. }
  7190. static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
  7191. {
  7192. /*
  7193. * TODO: this bit should only be enabled when really needed, then
  7194. * disabled when not needed anymore in order to save power.
  7195. */
  7196. if (HAS_PCH_LPT_LP(dev_priv))
  7197. I915_WRITE(SOUTH_DSPCLK_GATE_D,
  7198. I915_READ(SOUTH_DSPCLK_GATE_D) |
  7199. PCH_LP_PARTITION_LEVEL_DISABLE);
  7200. /* WADPOClockGatingDisable:hsw */
  7201. I915_WRITE(TRANS_CHICKEN1(PIPE_A),
  7202. I915_READ(TRANS_CHICKEN1(PIPE_A)) |
  7203. TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
  7204. }
  7205. static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
  7206. {
  7207. if (HAS_PCH_LPT_LP(dev_priv)) {
  7208. uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7209. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7210. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7211. }
  7212. }
  7213. static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
  7214. int general_prio_credits,
  7215. int high_prio_credits)
  7216. {
  7217. u32 misccpctl;
  7218. u32 val;
  7219. /* WaTempDisableDOPClkGating:bdw */
  7220. misccpctl = I915_READ(GEN7_MISCCPCTL);
  7221. I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
  7222. val = I915_READ(GEN8_L3SQCREG1);
  7223. val &= ~L3_PRIO_CREDITS_MASK;
  7224. val |= L3_GENERAL_PRIO_CREDITS(general_prio_credits);
  7225. val |= L3_HIGH_PRIO_CREDITS(high_prio_credits);
  7226. I915_WRITE(GEN8_L3SQCREG1, val);
  7227. /*
  7228. * Wait at least 100 clocks before re-enabling clock gating.
  7229. * See the definition of L3SQCREG1 in BSpec.
  7230. */
  7231. POSTING_READ(GEN8_L3SQCREG1);
  7232. udelay(1);
  7233. I915_WRITE(GEN7_MISCCPCTL, misccpctl);
  7234. }
  7235. static void icl_init_clock_gating(struct drm_i915_private *dev_priv)
  7236. {
  7237. /* This is not an Wa. Enable to reduce Sampler power */
  7238. I915_WRITE(GEN10_DFR_RATIO_EN_AND_CHICKEN,
  7239. I915_READ(GEN10_DFR_RATIO_EN_AND_CHICKEN) & ~DFR_DISABLE);
  7240. }
  7241. static void cnp_init_clock_gating(struct drm_i915_private *dev_priv)
  7242. {
  7243. if (!HAS_PCH_CNP(dev_priv))
  7244. return;
  7245. /* Display WA #1181 WaSouthDisplayDisablePWMCGEGating: cnp */
  7246. I915_WRITE(SOUTH_DSPCLK_GATE_D, I915_READ(SOUTH_DSPCLK_GATE_D) |
  7247. CNP_PWM_CGE_GATING_DISABLE);
  7248. }
  7249. static void cnl_init_clock_gating(struct drm_i915_private *dev_priv)
  7250. {
  7251. u32 val;
  7252. cnp_init_clock_gating(dev_priv);
  7253. /* This is not an Wa. Enable for better image quality */
  7254. I915_WRITE(_3D_CHICKEN3,
  7255. _MASKED_BIT_ENABLE(_3D_CHICKEN3_AA_LINE_QUALITY_FIX_ENABLE));
  7256. /* WaEnableChickenDCPR:cnl */
  7257. I915_WRITE(GEN8_CHICKEN_DCPR_1,
  7258. I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
  7259. /* WaFbcWakeMemOn:cnl */
  7260. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  7261. DISP_FBC_MEMORY_WAKE);
  7262. val = I915_READ(SLICE_UNIT_LEVEL_CLKGATE);
  7263. /* ReadHitWriteOnlyDisable:cnl */
  7264. val |= RCCUNIT_CLKGATE_DIS;
  7265. /* WaSarbUnitClockGatingDisable:cnl (pre-prod) */
  7266. if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_B0))
  7267. val |= SARBUNIT_CLKGATE_DIS;
  7268. I915_WRITE(SLICE_UNIT_LEVEL_CLKGATE, val);
  7269. /* Wa_2201832410:cnl */
  7270. val = I915_READ(SUBSLICE_UNIT_LEVEL_CLKGATE);
  7271. val |= GWUNIT_CLKGATE_DIS;
  7272. I915_WRITE(SUBSLICE_UNIT_LEVEL_CLKGATE, val);
  7273. /* WaDisableVFclkgate:cnl */
  7274. /* WaVFUnitClockGatingDisable:cnl */
  7275. val = I915_READ(UNSLICE_UNIT_LEVEL_CLKGATE);
  7276. val |= VFUNIT_CLKGATE_DIS;
  7277. I915_WRITE(UNSLICE_UNIT_LEVEL_CLKGATE, val);
  7278. }
  7279. static void cfl_init_clock_gating(struct drm_i915_private *dev_priv)
  7280. {
  7281. cnp_init_clock_gating(dev_priv);
  7282. gen9_init_clock_gating(dev_priv);
  7283. /* WaFbcNukeOnHostModify:cfl */
  7284. I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
  7285. ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
  7286. }
  7287. static void kbl_init_clock_gating(struct drm_i915_private *dev_priv)
  7288. {
  7289. gen9_init_clock_gating(dev_priv);
  7290. /* WaDisableSDEUnitClockGating:kbl */
  7291. if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
  7292. I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
  7293. GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
  7294. /* WaDisableGamClockGating:kbl */
  7295. if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
  7296. I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
  7297. GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
  7298. /* WaFbcNukeOnHostModify:kbl */
  7299. I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
  7300. ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
  7301. }
  7302. static void skl_init_clock_gating(struct drm_i915_private *dev_priv)
  7303. {
  7304. gen9_init_clock_gating(dev_priv);
  7305. /* WAC6entrylatency:skl */
  7306. I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
  7307. FBC_LLC_FULLY_OPEN);
  7308. /* WaFbcNukeOnHostModify:skl */
  7309. I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
  7310. ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
  7311. }
  7312. static void bdw_init_clock_gating(struct drm_i915_private *dev_priv)
  7313. {
  7314. /* The GTT cache must be disabled if the system is using 2M pages. */
  7315. bool can_use_gtt_cache = !HAS_PAGE_SIZES(dev_priv,
  7316. I915_GTT_PAGE_SIZE_2M);
  7317. enum pipe pipe;
  7318. /* WaSwitchSolVfFArbitrationPriority:bdw */
  7319. I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
  7320. /* WaPsrDPAMaskVBlankInSRD:bdw */
  7321. I915_WRITE(CHICKEN_PAR1_1,
  7322. I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
  7323. /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
  7324. for_each_pipe(dev_priv, pipe) {
  7325. I915_WRITE(CHICKEN_PIPESL_1(pipe),
  7326. I915_READ(CHICKEN_PIPESL_1(pipe)) |
  7327. BDW_DPRS_MASK_VBLANK_SRD);
  7328. }
  7329. /* WaVSRefCountFullforceMissDisable:bdw */
  7330. /* WaDSRefCountFullforceMissDisable:bdw */
  7331. I915_WRITE(GEN7_FF_THREAD_MODE,
  7332. I915_READ(GEN7_FF_THREAD_MODE) &
  7333. ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
  7334. I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
  7335. _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
  7336. /* WaDisableSDEUnitClockGating:bdw */
  7337. I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
  7338. GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
  7339. /* WaProgramL3SqcReg1Default:bdw */
  7340. gen8_set_l3sqc_credits(dev_priv, 30, 2);
  7341. /* WaGttCachingOffByDefault:bdw */
  7342. I915_WRITE(HSW_GTT_CACHE_EN, can_use_gtt_cache ? GTT_CACHE_EN_ALL : 0);
  7343. /* WaKVMNotificationOnConfigChange:bdw */
  7344. I915_WRITE(CHICKEN_PAR2_1, I915_READ(CHICKEN_PAR2_1)
  7345. | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
  7346. lpt_init_clock_gating(dev_priv);
  7347. /* WaDisableDopClockGating:bdw
  7348. *
  7349. * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
  7350. * clock gating.
  7351. */
  7352. I915_WRITE(GEN6_UCGCTL1,
  7353. I915_READ(GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
  7354. }
  7355. static void hsw_init_clock_gating(struct drm_i915_private *dev_priv)
  7356. {
  7357. /* L3 caching of data atomics doesn't work -- disable it. */
  7358. I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
  7359. I915_WRITE(HSW_ROW_CHICKEN3,
  7360. _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
  7361. /* This is required by WaCatErrorRejectionIssue:hsw */
  7362. I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
  7363. I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
  7364. GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
  7365. /* WaVSRefCountFullforceMissDisable:hsw */
  7366. I915_WRITE(GEN7_FF_THREAD_MODE,
  7367. I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
  7368. /* WaDisable_RenderCache_OperationalFlush:hsw */
  7369. I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  7370. /* enable HiZ Raw Stall Optimization */
  7371. I915_WRITE(CACHE_MODE_0_GEN7,
  7372. _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
  7373. /* WaDisable4x2SubspanOptimization:hsw */
  7374. I915_WRITE(CACHE_MODE_1,
  7375. _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
  7376. /*
  7377. * BSpec recommends 8x4 when MSAA is used,
  7378. * however in practice 16x4 seems fastest.
  7379. *
  7380. * Note that PS/WM thread counts depend on the WIZ hashing
  7381. * disable bit, which we don't touch here, but it's good
  7382. * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
  7383. */
  7384. I915_WRITE(GEN7_GT_MODE,
  7385. _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
  7386. /* WaSampleCChickenBitEnable:hsw */
  7387. I915_WRITE(HALF_SLICE_CHICKEN3,
  7388. _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
  7389. /* WaSwitchSolVfFArbitrationPriority:hsw */
  7390. I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
  7391. lpt_init_clock_gating(dev_priv);
  7392. }
  7393. static void ivb_init_clock_gating(struct drm_i915_private *dev_priv)
  7394. {
  7395. uint32_t snpcr;
  7396. I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
  7397. /* WaDisableEarlyCull:ivb */
  7398. I915_WRITE(_3D_CHICKEN3,
  7399. _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
  7400. /* WaDisableBackToBackFlipFix:ivb */
  7401. I915_WRITE(IVB_CHICKEN3,
  7402. CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
  7403. CHICKEN3_DGMG_DONE_FIX_DISABLE);
  7404. /* WaDisablePSDDualDispatchEnable:ivb */
  7405. if (IS_IVB_GT1(dev_priv))
  7406. I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
  7407. _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
  7408. /* WaDisable_RenderCache_OperationalFlush:ivb */
  7409. I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  7410. /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
  7411. I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
  7412. GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
  7413. /* WaApplyL3ControlAndL3ChickenMode:ivb */
  7414. I915_WRITE(GEN7_L3CNTLREG1,
  7415. GEN7_WA_FOR_GEN7_L3_CONTROL);
  7416. I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
  7417. GEN7_WA_L3_CHICKEN_MODE);
  7418. if (IS_IVB_GT1(dev_priv))
  7419. I915_WRITE(GEN7_ROW_CHICKEN2,
  7420. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  7421. else {
  7422. /* must write both registers */
  7423. I915_WRITE(GEN7_ROW_CHICKEN2,
  7424. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  7425. I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
  7426. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  7427. }
  7428. /* WaForceL3Serialization:ivb */
  7429. I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
  7430. ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
  7431. /*
  7432. * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
  7433. * This implements the WaDisableRCZUnitClockGating:ivb workaround.
  7434. */
  7435. I915_WRITE(GEN6_UCGCTL2,
  7436. GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
  7437. /* This is required by WaCatErrorRejectionIssue:ivb */
  7438. I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
  7439. I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
  7440. GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
  7441. g4x_disable_trickle_feed(dev_priv);
  7442. gen7_setup_fixed_func_scheduler(dev_priv);
  7443. if (0) { /* causes HiZ corruption on ivb:gt1 */
  7444. /* enable HiZ Raw Stall Optimization */
  7445. I915_WRITE(CACHE_MODE_0_GEN7,
  7446. _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
  7447. }
  7448. /* WaDisable4x2SubspanOptimization:ivb */
  7449. I915_WRITE(CACHE_MODE_1,
  7450. _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
  7451. /*
  7452. * BSpec recommends 8x4 when MSAA is used,
  7453. * however in practice 16x4 seems fastest.
  7454. *
  7455. * Note that PS/WM thread counts depend on the WIZ hashing
  7456. * disable bit, which we don't touch here, but it's good
  7457. * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
  7458. */
  7459. I915_WRITE(GEN7_GT_MODE,
  7460. _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
  7461. snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
  7462. snpcr &= ~GEN6_MBC_SNPCR_MASK;
  7463. snpcr |= GEN6_MBC_SNPCR_MED;
  7464. I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
  7465. if (!HAS_PCH_NOP(dev_priv))
  7466. cpt_init_clock_gating(dev_priv);
  7467. gen6_check_mch_setup(dev_priv);
  7468. }
  7469. static void vlv_init_clock_gating(struct drm_i915_private *dev_priv)
  7470. {
  7471. /* WaDisableEarlyCull:vlv */
  7472. I915_WRITE(_3D_CHICKEN3,
  7473. _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
  7474. /* WaDisableBackToBackFlipFix:vlv */
  7475. I915_WRITE(IVB_CHICKEN3,
  7476. CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
  7477. CHICKEN3_DGMG_DONE_FIX_DISABLE);
  7478. /* WaPsdDispatchEnable:vlv */
  7479. /* WaDisablePSDDualDispatchEnable:vlv */
  7480. I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
  7481. _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
  7482. GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
  7483. /* WaDisable_RenderCache_OperationalFlush:vlv */
  7484. I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  7485. /* WaForceL3Serialization:vlv */
  7486. I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
  7487. ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
  7488. /* WaDisableDopClockGating:vlv */
  7489. I915_WRITE(GEN7_ROW_CHICKEN2,
  7490. _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
  7491. /* This is required by WaCatErrorRejectionIssue:vlv */
  7492. I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
  7493. I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
  7494. GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
  7495. gen7_setup_fixed_func_scheduler(dev_priv);
  7496. /*
  7497. * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
  7498. * This implements the WaDisableRCZUnitClockGating:vlv workaround.
  7499. */
  7500. I915_WRITE(GEN6_UCGCTL2,
  7501. GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
  7502. /* WaDisableL3Bank2xClockGate:vlv
  7503. * Disabling L3 clock gating- MMIO 940c[25] = 1
  7504. * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
  7505. I915_WRITE(GEN7_UCGCTL4,
  7506. I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
  7507. /*
  7508. * BSpec says this must be set, even though
  7509. * WaDisable4x2SubspanOptimization isn't listed for VLV.
  7510. */
  7511. I915_WRITE(CACHE_MODE_1,
  7512. _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
  7513. /*
  7514. * BSpec recommends 8x4 when MSAA is used,
  7515. * however in practice 16x4 seems fastest.
  7516. *
  7517. * Note that PS/WM thread counts depend on the WIZ hashing
  7518. * disable bit, which we don't touch here, but it's good
  7519. * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
  7520. */
  7521. I915_WRITE(GEN7_GT_MODE,
  7522. _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
  7523. /*
  7524. * WaIncreaseL3CreditsForVLVB0:vlv
  7525. * This is the hardware default actually.
  7526. */
  7527. I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
  7528. /*
  7529. * WaDisableVLVClockGating_VBIIssue:vlv
  7530. * Disable clock gating on th GCFG unit to prevent a delay
  7531. * in the reporting of vblank events.
  7532. */
  7533. I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
  7534. }
  7535. static void chv_init_clock_gating(struct drm_i915_private *dev_priv)
  7536. {
  7537. /* WaVSRefCountFullforceMissDisable:chv */
  7538. /* WaDSRefCountFullforceMissDisable:chv */
  7539. I915_WRITE(GEN7_FF_THREAD_MODE,
  7540. I915_READ(GEN7_FF_THREAD_MODE) &
  7541. ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
  7542. /* WaDisableSemaphoreAndSyncFlipWait:chv */
  7543. I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
  7544. _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
  7545. /* WaDisableCSUnitClockGating:chv */
  7546. I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
  7547. GEN6_CSUNIT_CLOCK_GATE_DISABLE);
  7548. /* WaDisableSDEUnitClockGating:chv */
  7549. I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
  7550. GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
  7551. /*
  7552. * WaProgramL3SqcReg1Default:chv
  7553. * See gfxspecs/Related Documents/Performance Guide/
  7554. * LSQC Setting Recommendations.
  7555. */
  7556. gen8_set_l3sqc_credits(dev_priv, 38, 2);
  7557. /*
  7558. * GTT cache may not work with big pages, so if those
  7559. * are ever enabled GTT cache may need to be disabled.
  7560. */
  7561. I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
  7562. }
  7563. static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
  7564. {
  7565. uint32_t dspclk_gate;
  7566. I915_WRITE(RENCLK_GATE_D1, 0);
  7567. I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
  7568. GS_UNIT_CLOCK_GATE_DISABLE |
  7569. CL_UNIT_CLOCK_GATE_DISABLE);
  7570. I915_WRITE(RAMCLK_GATE_D, 0);
  7571. dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
  7572. OVRUNIT_CLOCK_GATE_DISABLE |
  7573. OVCUNIT_CLOCK_GATE_DISABLE;
  7574. if (IS_GM45(dev_priv))
  7575. dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
  7576. I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
  7577. /* WaDisableRenderCachePipelinedFlush */
  7578. I915_WRITE(CACHE_MODE_0,
  7579. _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
  7580. /* WaDisable_RenderCache_OperationalFlush:g4x */
  7581. I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  7582. g4x_disable_trickle_feed(dev_priv);
  7583. }
  7584. static void i965gm_init_clock_gating(struct drm_i915_private *dev_priv)
  7585. {
  7586. I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
  7587. I915_WRITE(RENCLK_GATE_D2, 0);
  7588. I915_WRITE(DSPCLK_GATE_D, 0);
  7589. I915_WRITE(RAMCLK_GATE_D, 0);
  7590. I915_WRITE16(DEUC, 0);
  7591. I915_WRITE(MI_ARB_STATE,
  7592. _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
  7593. /* WaDisable_RenderCache_OperationalFlush:gen4 */
  7594. I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  7595. }
  7596. static void i965g_init_clock_gating(struct drm_i915_private *dev_priv)
  7597. {
  7598. I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
  7599. I965_RCC_CLOCK_GATE_DISABLE |
  7600. I965_RCPB_CLOCK_GATE_DISABLE |
  7601. I965_ISC_CLOCK_GATE_DISABLE |
  7602. I965_FBC_CLOCK_GATE_DISABLE);
  7603. I915_WRITE(RENCLK_GATE_D2, 0);
  7604. I915_WRITE(MI_ARB_STATE,
  7605. _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
  7606. /* WaDisable_RenderCache_OperationalFlush:gen4 */
  7607. I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
  7608. }
  7609. static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
  7610. {
  7611. u32 dstate = I915_READ(D_STATE);
  7612. dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
  7613. DSTATE_DOT_CLOCK_GATING;
  7614. I915_WRITE(D_STATE, dstate);
  7615. if (IS_PINEVIEW(dev_priv))
  7616. I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
  7617. /* IIR "flip pending" means done if this bit is set */
  7618. I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
  7619. /* interrupts should cause a wake up from C3 */
  7620. I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
  7621. /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
  7622. I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
  7623. I915_WRITE(MI_ARB_STATE,
  7624. _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
  7625. }
  7626. static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
  7627. {
  7628. I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
  7629. /* interrupts should cause a wake up from C3 */
  7630. I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
  7631. _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
  7632. I915_WRITE(MEM_MODE,
  7633. _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
  7634. }
  7635. static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
  7636. {
  7637. I915_WRITE(MEM_MODE,
  7638. _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
  7639. _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
  7640. }
  7641. void intel_init_clock_gating(struct drm_i915_private *dev_priv)
  7642. {
  7643. dev_priv->display.init_clock_gating(dev_priv);
  7644. }
  7645. void intel_suspend_hw(struct drm_i915_private *dev_priv)
  7646. {
  7647. if (HAS_PCH_LPT(dev_priv))
  7648. lpt_suspend_hw(dev_priv);
  7649. }
  7650. static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
  7651. {
  7652. DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
  7653. }
  7654. /**
  7655. * intel_init_clock_gating_hooks - setup the clock gating hooks
  7656. * @dev_priv: device private
  7657. *
  7658. * Setup the hooks that configure which clocks of a given platform can be
  7659. * gated and also apply various GT and display specific workarounds for these
  7660. * platforms. Note that some GT specific workarounds are applied separately
  7661. * when GPU contexts or batchbuffers start their execution.
  7662. */
  7663. void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
  7664. {
  7665. if (IS_ICELAKE(dev_priv))
  7666. dev_priv->display.init_clock_gating = icl_init_clock_gating;
  7667. else if (IS_CANNONLAKE(dev_priv))
  7668. dev_priv->display.init_clock_gating = cnl_init_clock_gating;
  7669. else if (IS_COFFEELAKE(dev_priv))
  7670. dev_priv->display.init_clock_gating = cfl_init_clock_gating;
  7671. else if (IS_SKYLAKE(dev_priv))
  7672. dev_priv->display.init_clock_gating = skl_init_clock_gating;
  7673. else if (IS_KABYLAKE(dev_priv))
  7674. dev_priv->display.init_clock_gating = kbl_init_clock_gating;
  7675. else if (IS_BROXTON(dev_priv))
  7676. dev_priv->display.init_clock_gating = bxt_init_clock_gating;
  7677. else if (IS_GEMINILAKE(dev_priv))
  7678. dev_priv->display.init_clock_gating = glk_init_clock_gating;
  7679. else if (IS_BROADWELL(dev_priv))
  7680. dev_priv->display.init_clock_gating = bdw_init_clock_gating;
  7681. else if (IS_CHERRYVIEW(dev_priv))
  7682. dev_priv->display.init_clock_gating = chv_init_clock_gating;
  7683. else if (IS_HASWELL(dev_priv))
  7684. dev_priv->display.init_clock_gating = hsw_init_clock_gating;
  7685. else if (IS_IVYBRIDGE(dev_priv))
  7686. dev_priv->display.init_clock_gating = ivb_init_clock_gating;
  7687. else if (IS_VALLEYVIEW(dev_priv))
  7688. dev_priv->display.init_clock_gating = vlv_init_clock_gating;
  7689. else if (IS_GEN6(dev_priv))
  7690. dev_priv->display.init_clock_gating = gen6_init_clock_gating;
  7691. else if (IS_GEN5(dev_priv))
  7692. dev_priv->display.init_clock_gating = ilk_init_clock_gating;
  7693. else if (IS_G4X(dev_priv))
  7694. dev_priv->display.init_clock_gating = g4x_init_clock_gating;
  7695. else if (IS_I965GM(dev_priv))
  7696. dev_priv->display.init_clock_gating = i965gm_init_clock_gating;
  7697. else if (IS_I965G(dev_priv))
  7698. dev_priv->display.init_clock_gating = i965g_init_clock_gating;
  7699. else if (IS_GEN3(dev_priv))
  7700. dev_priv->display.init_clock_gating = gen3_init_clock_gating;
  7701. else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
  7702. dev_priv->display.init_clock_gating = i85x_init_clock_gating;
  7703. else if (IS_GEN2(dev_priv))
  7704. dev_priv->display.init_clock_gating = i830_init_clock_gating;
  7705. else {
  7706. MISSING_CASE(INTEL_DEVID(dev_priv));
  7707. dev_priv->display.init_clock_gating = nop_init_clock_gating;
  7708. }
  7709. }
  7710. /* Set up chip specific power management-related functions */
  7711. void intel_init_pm(struct drm_i915_private *dev_priv)
  7712. {
  7713. intel_fbc_init(dev_priv);
  7714. /* For cxsr */
  7715. if (IS_PINEVIEW(dev_priv))
  7716. i915_pineview_get_mem_freq(dev_priv);
  7717. else if (IS_GEN5(dev_priv))
  7718. i915_ironlake_get_mem_freq(dev_priv);
  7719. /* For FIFO watermark updates */
  7720. if (INTEL_GEN(dev_priv) >= 9) {
  7721. skl_setup_wm_latency(dev_priv);
  7722. dev_priv->display.initial_watermarks = skl_initial_wm;
  7723. dev_priv->display.atomic_update_watermarks = skl_atomic_update_crtc_wm;
  7724. dev_priv->display.compute_global_watermarks = skl_compute_wm;
  7725. } else if (HAS_PCH_SPLIT(dev_priv)) {
  7726. ilk_setup_wm_latency(dev_priv);
  7727. if ((IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[1] &&
  7728. dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
  7729. (!IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[0] &&
  7730. dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
  7731. dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
  7732. dev_priv->display.compute_intermediate_wm =
  7733. ilk_compute_intermediate_wm;
  7734. dev_priv->display.initial_watermarks =
  7735. ilk_initial_watermarks;
  7736. dev_priv->display.optimize_watermarks =
  7737. ilk_optimize_watermarks;
  7738. } else {
  7739. DRM_DEBUG_KMS("Failed to read display plane latency. "
  7740. "Disable CxSR\n");
  7741. }
  7742. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  7743. vlv_setup_wm_latency(dev_priv);
  7744. dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
  7745. dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
  7746. dev_priv->display.initial_watermarks = vlv_initial_watermarks;
  7747. dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
  7748. dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
  7749. } else if (IS_G4X(dev_priv)) {
  7750. g4x_setup_wm_latency(dev_priv);
  7751. dev_priv->display.compute_pipe_wm = g4x_compute_pipe_wm;
  7752. dev_priv->display.compute_intermediate_wm = g4x_compute_intermediate_wm;
  7753. dev_priv->display.initial_watermarks = g4x_initial_watermarks;
  7754. dev_priv->display.optimize_watermarks = g4x_optimize_watermarks;
  7755. } else if (IS_PINEVIEW(dev_priv)) {
  7756. if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
  7757. dev_priv->is_ddr3,
  7758. dev_priv->fsb_freq,
  7759. dev_priv->mem_freq)) {
  7760. DRM_INFO("failed to find known CxSR latency "
  7761. "(found ddr%s fsb freq %d, mem freq %d), "
  7762. "disabling CxSR\n",
  7763. (dev_priv->is_ddr3 == 1) ? "3" : "2",
  7764. dev_priv->fsb_freq, dev_priv->mem_freq);
  7765. /* Disable CxSR and never update its watermark again */
  7766. intel_set_memory_cxsr(dev_priv, false);
  7767. dev_priv->display.update_wm = NULL;
  7768. } else
  7769. dev_priv->display.update_wm = pineview_update_wm;
  7770. } else if (IS_GEN4(dev_priv)) {
  7771. dev_priv->display.update_wm = i965_update_wm;
  7772. } else if (IS_GEN3(dev_priv)) {
  7773. dev_priv->display.update_wm = i9xx_update_wm;
  7774. dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
  7775. } else if (IS_GEN2(dev_priv)) {
  7776. if (INTEL_INFO(dev_priv)->num_pipes == 1) {
  7777. dev_priv->display.update_wm = i845_update_wm;
  7778. dev_priv->display.get_fifo_size = i845_get_fifo_size;
  7779. } else {
  7780. dev_priv->display.update_wm = i9xx_update_wm;
  7781. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  7782. }
  7783. } else {
  7784. DRM_ERROR("unexpected fall-through in intel_init_pm\n");
  7785. }
  7786. }
  7787. static inline int gen6_check_mailbox_status(struct drm_i915_private *dev_priv)
  7788. {
  7789. uint32_t flags =
  7790. I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
  7791. switch (flags) {
  7792. case GEN6_PCODE_SUCCESS:
  7793. return 0;
  7794. case GEN6_PCODE_UNIMPLEMENTED_CMD:
  7795. return -ENODEV;
  7796. case GEN6_PCODE_ILLEGAL_CMD:
  7797. return -ENXIO;
  7798. case GEN6_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
  7799. case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
  7800. return -EOVERFLOW;
  7801. case GEN6_PCODE_TIMEOUT:
  7802. return -ETIMEDOUT;
  7803. default:
  7804. MISSING_CASE(flags);
  7805. return 0;
  7806. }
  7807. }
  7808. static inline int gen7_check_mailbox_status(struct drm_i915_private *dev_priv)
  7809. {
  7810. uint32_t flags =
  7811. I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
  7812. switch (flags) {
  7813. case GEN6_PCODE_SUCCESS:
  7814. return 0;
  7815. case GEN6_PCODE_ILLEGAL_CMD:
  7816. return -ENXIO;
  7817. case GEN7_PCODE_TIMEOUT:
  7818. return -ETIMEDOUT;
  7819. case GEN7_PCODE_ILLEGAL_DATA:
  7820. return -EINVAL;
  7821. case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
  7822. return -EOVERFLOW;
  7823. default:
  7824. MISSING_CASE(flags);
  7825. return 0;
  7826. }
  7827. }
  7828. int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
  7829. {
  7830. int status;
  7831. WARN_ON(!mutex_is_locked(&dev_priv->pcu_lock));
  7832. /* GEN6_PCODE_* are outside of the forcewake domain, we can
  7833. * use te fw I915_READ variants to reduce the amount of work
  7834. * required when reading/writing.
  7835. */
  7836. if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
  7837. DRM_DEBUG_DRIVER("warning: pcode (read from mbox %x) mailbox access failed for %ps\n",
  7838. mbox, __builtin_return_address(0));
  7839. return -EAGAIN;
  7840. }
  7841. I915_WRITE_FW(GEN6_PCODE_DATA, *val);
  7842. I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
  7843. I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
  7844. if (__intel_wait_for_register_fw(dev_priv,
  7845. GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
  7846. 500, 0, NULL)) {
  7847. DRM_ERROR("timeout waiting for pcode read (from mbox %x) to finish for %ps\n",
  7848. mbox, __builtin_return_address(0));
  7849. return -ETIMEDOUT;
  7850. }
  7851. *val = I915_READ_FW(GEN6_PCODE_DATA);
  7852. I915_WRITE_FW(GEN6_PCODE_DATA, 0);
  7853. if (INTEL_GEN(dev_priv) > 6)
  7854. status = gen7_check_mailbox_status(dev_priv);
  7855. else
  7856. status = gen6_check_mailbox_status(dev_priv);
  7857. if (status) {
  7858. DRM_DEBUG_DRIVER("warning: pcode (read from mbox %x) mailbox access failed for %ps: %d\n",
  7859. mbox, __builtin_return_address(0), status);
  7860. return status;
  7861. }
  7862. return 0;
  7863. }
  7864. int sandybridge_pcode_write_timeout(struct drm_i915_private *dev_priv,
  7865. u32 mbox, u32 val,
  7866. int fast_timeout_us, int slow_timeout_ms)
  7867. {
  7868. int status;
  7869. WARN_ON(!mutex_is_locked(&dev_priv->pcu_lock));
  7870. /* GEN6_PCODE_* are outside of the forcewake domain, we can
  7871. * use te fw I915_READ variants to reduce the amount of work
  7872. * required when reading/writing.
  7873. */
  7874. if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
  7875. DRM_DEBUG_DRIVER("warning: pcode (write of 0x%08x to mbox %x) mailbox access failed for %ps\n",
  7876. val, mbox, __builtin_return_address(0));
  7877. return -EAGAIN;
  7878. }
  7879. I915_WRITE_FW(GEN6_PCODE_DATA, val);
  7880. I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
  7881. I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
  7882. if (__intel_wait_for_register_fw(dev_priv,
  7883. GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
  7884. fast_timeout_us, slow_timeout_ms,
  7885. NULL)) {
  7886. DRM_ERROR("timeout waiting for pcode write of 0x%08x to mbox %x to finish for %ps\n",
  7887. val, mbox, __builtin_return_address(0));
  7888. return -ETIMEDOUT;
  7889. }
  7890. I915_WRITE_FW(GEN6_PCODE_DATA, 0);
  7891. if (INTEL_GEN(dev_priv) > 6)
  7892. status = gen7_check_mailbox_status(dev_priv);
  7893. else
  7894. status = gen6_check_mailbox_status(dev_priv);
  7895. if (status) {
  7896. DRM_DEBUG_DRIVER("warning: pcode (write of 0x%08x to mbox %x) mailbox access failed for %ps: %d\n",
  7897. val, mbox, __builtin_return_address(0), status);
  7898. return status;
  7899. }
  7900. return 0;
  7901. }
  7902. static bool skl_pcode_try_request(struct drm_i915_private *dev_priv, u32 mbox,
  7903. u32 request, u32 reply_mask, u32 reply,
  7904. u32 *status)
  7905. {
  7906. u32 val = request;
  7907. *status = sandybridge_pcode_read(dev_priv, mbox, &val);
  7908. return *status || ((val & reply_mask) == reply);
  7909. }
  7910. /**
  7911. * skl_pcode_request - send PCODE request until acknowledgment
  7912. * @dev_priv: device private
  7913. * @mbox: PCODE mailbox ID the request is targeted for
  7914. * @request: request ID
  7915. * @reply_mask: mask used to check for request acknowledgment
  7916. * @reply: value used to check for request acknowledgment
  7917. * @timeout_base_ms: timeout for polling with preemption enabled
  7918. *
  7919. * Keep resending the @request to @mbox until PCODE acknowledges it, PCODE
  7920. * reports an error or an overall timeout of @timeout_base_ms+50 ms expires.
  7921. * The request is acknowledged once the PCODE reply dword equals @reply after
  7922. * applying @reply_mask. Polling is first attempted with preemption enabled
  7923. * for @timeout_base_ms and if this times out for another 50 ms with
  7924. * preemption disabled.
  7925. *
  7926. * Returns 0 on success, %-ETIMEDOUT in case of a timeout, <0 in case of some
  7927. * other error as reported by PCODE.
  7928. */
  7929. int skl_pcode_request(struct drm_i915_private *dev_priv, u32 mbox, u32 request,
  7930. u32 reply_mask, u32 reply, int timeout_base_ms)
  7931. {
  7932. u32 status;
  7933. int ret;
  7934. WARN_ON(!mutex_is_locked(&dev_priv->pcu_lock));
  7935. #define COND skl_pcode_try_request(dev_priv, mbox, request, reply_mask, reply, \
  7936. &status)
  7937. /*
  7938. * Prime the PCODE by doing a request first. Normally it guarantees
  7939. * that a subsequent request, at most @timeout_base_ms later, succeeds.
  7940. * _wait_for() doesn't guarantee when its passed condition is evaluated
  7941. * first, so send the first request explicitly.
  7942. */
  7943. if (COND) {
  7944. ret = 0;
  7945. goto out;
  7946. }
  7947. ret = _wait_for(COND, timeout_base_ms * 1000, 10, 10);
  7948. if (!ret)
  7949. goto out;
  7950. /*
  7951. * The above can time out if the number of requests was low (2 in the
  7952. * worst case) _and_ PCODE was busy for some reason even after a
  7953. * (queued) request and @timeout_base_ms delay. As a workaround retry
  7954. * the poll with preemption disabled to maximize the number of
  7955. * requests. Increase the timeout from @timeout_base_ms to 50ms to
  7956. * account for interrupts that could reduce the number of these
  7957. * requests, and for any quirks of the PCODE firmware that delays
  7958. * the request completion.
  7959. */
  7960. DRM_DEBUG_KMS("PCODE timeout, retrying with preemption disabled\n");
  7961. WARN_ON_ONCE(timeout_base_ms > 3);
  7962. preempt_disable();
  7963. ret = wait_for_atomic(COND, 50);
  7964. preempt_enable();
  7965. out:
  7966. return ret ? ret : status;
  7967. #undef COND
  7968. }
  7969. static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
  7970. {
  7971. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  7972. /*
  7973. * N = val - 0xb7
  7974. * Slow = Fast = GPLL ref * N
  7975. */
  7976. return DIV_ROUND_CLOSEST(rps->gpll_ref_freq * (val - 0xb7), 1000);
  7977. }
  7978. static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
  7979. {
  7980. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  7981. return DIV_ROUND_CLOSEST(1000 * val, rps->gpll_ref_freq) + 0xb7;
  7982. }
  7983. static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
  7984. {
  7985. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  7986. /*
  7987. * N = val / 2
  7988. * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
  7989. */
  7990. return DIV_ROUND_CLOSEST(rps->gpll_ref_freq * val, 2 * 2 * 1000);
  7991. }
  7992. static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
  7993. {
  7994. struct intel_rps *rps = &dev_priv->gt_pm.rps;
  7995. /* CHV needs even values */
  7996. return DIV_ROUND_CLOSEST(2 * 1000 * val, rps->gpll_ref_freq) * 2;
  7997. }
  7998. int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
  7999. {
  8000. if (INTEL_GEN(dev_priv) >= 9)
  8001. return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
  8002. GEN9_FREQ_SCALER);
  8003. else if (IS_CHERRYVIEW(dev_priv))
  8004. return chv_gpu_freq(dev_priv, val);
  8005. else if (IS_VALLEYVIEW(dev_priv))
  8006. return byt_gpu_freq(dev_priv, val);
  8007. else
  8008. return val * GT_FREQUENCY_MULTIPLIER;
  8009. }
  8010. int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
  8011. {
  8012. if (INTEL_GEN(dev_priv) >= 9)
  8013. return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
  8014. GT_FREQUENCY_MULTIPLIER);
  8015. else if (IS_CHERRYVIEW(dev_priv))
  8016. return chv_freq_opcode(dev_priv, val);
  8017. else if (IS_VALLEYVIEW(dev_priv))
  8018. return byt_freq_opcode(dev_priv, val);
  8019. else
  8020. return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
  8021. }
  8022. void intel_pm_setup(struct drm_i915_private *dev_priv)
  8023. {
  8024. mutex_init(&dev_priv->pcu_lock);
  8025. mutex_init(&dev_priv->gt_pm.rps.power.mutex);
  8026. atomic_set(&dev_priv->gt_pm.rps.num_waiters, 0);
  8027. dev_priv->runtime_pm.suspended = false;
  8028. atomic_set(&dev_priv->runtime_pm.wakeref_count, 0);
  8029. }
  8030. static u64 vlv_residency_raw(struct drm_i915_private *dev_priv,
  8031. const i915_reg_t reg)
  8032. {
  8033. u32 lower, upper, tmp;
  8034. int loop = 2;
  8035. /*
  8036. * The register accessed do not need forcewake. We borrow
  8037. * uncore lock to prevent concurrent access to range reg.
  8038. */
  8039. lockdep_assert_held(&dev_priv->uncore.lock);
  8040. /*
  8041. * vlv and chv residency counters are 40 bits in width.
  8042. * With a control bit, we can choose between upper or lower
  8043. * 32bit window into this counter.
  8044. *
  8045. * Although we always use the counter in high-range mode elsewhere,
  8046. * userspace may attempt to read the value before rc6 is initialised,
  8047. * before we have set the default VLV_COUNTER_CONTROL value. So always
  8048. * set the high bit to be safe.
  8049. */
  8050. I915_WRITE_FW(VLV_COUNTER_CONTROL,
  8051. _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
  8052. upper = I915_READ_FW(reg);
  8053. do {
  8054. tmp = upper;
  8055. I915_WRITE_FW(VLV_COUNTER_CONTROL,
  8056. _MASKED_BIT_DISABLE(VLV_COUNT_RANGE_HIGH));
  8057. lower = I915_READ_FW(reg);
  8058. I915_WRITE_FW(VLV_COUNTER_CONTROL,
  8059. _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
  8060. upper = I915_READ_FW(reg);
  8061. } while (upper != tmp && --loop);
  8062. /*
  8063. * Everywhere else we always use VLV_COUNTER_CONTROL with the
  8064. * VLV_COUNT_RANGE_HIGH bit set - so it is safe to leave it set
  8065. * now.
  8066. */
  8067. return lower | (u64)upper << 8;
  8068. }
  8069. u64 intel_rc6_residency_ns(struct drm_i915_private *dev_priv,
  8070. const i915_reg_t reg)
  8071. {
  8072. u64 time_hw, prev_hw, overflow_hw;
  8073. unsigned int fw_domains;
  8074. unsigned long flags;
  8075. unsigned int i;
  8076. u32 mul, div;
  8077. if (!HAS_RC6(dev_priv))
  8078. return 0;
  8079. /*
  8080. * Store previous hw counter values for counter wrap-around handling.
  8081. *
  8082. * There are only four interesting registers and they live next to each
  8083. * other so we can use the relative address, compared to the smallest
  8084. * one as the index into driver storage.
  8085. */
  8086. i = (i915_mmio_reg_offset(reg) -
  8087. i915_mmio_reg_offset(GEN6_GT_GFX_RC6_LOCKED)) / sizeof(u32);
  8088. if (WARN_ON_ONCE(i >= ARRAY_SIZE(dev_priv->gt_pm.rc6.cur_residency)))
  8089. return 0;
  8090. fw_domains = intel_uncore_forcewake_for_reg(dev_priv, reg, FW_REG_READ);
  8091. spin_lock_irqsave(&dev_priv->uncore.lock, flags);
  8092. intel_uncore_forcewake_get__locked(dev_priv, fw_domains);
  8093. /* On VLV and CHV, residency time is in CZ units rather than 1.28us */
  8094. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  8095. mul = 1000000;
  8096. div = dev_priv->czclk_freq;
  8097. overflow_hw = BIT_ULL(40);
  8098. time_hw = vlv_residency_raw(dev_priv, reg);
  8099. } else {
  8100. /* 833.33ns units on Gen9LP, 1.28us elsewhere. */
  8101. if (IS_GEN9_LP(dev_priv)) {
  8102. mul = 10000;
  8103. div = 12;
  8104. } else {
  8105. mul = 1280;
  8106. div = 1;
  8107. }
  8108. overflow_hw = BIT_ULL(32);
  8109. time_hw = I915_READ_FW(reg);
  8110. }
  8111. /*
  8112. * Counter wrap handling.
  8113. *
  8114. * But relying on a sufficient frequency of queries otherwise counters
  8115. * can still wrap.
  8116. */
  8117. prev_hw = dev_priv->gt_pm.rc6.prev_hw_residency[i];
  8118. dev_priv->gt_pm.rc6.prev_hw_residency[i] = time_hw;
  8119. /* RC6 delta from last sample. */
  8120. if (time_hw >= prev_hw)
  8121. time_hw -= prev_hw;
  8122. else
  8123. time_hw += overflow_hw - prev_hw;
  8124. /* Add delta to RC6 extended raw driver copy. */
  8125. time_hw += dev_priv->gt_pm.rc6.cur_residency[i];
  8126. dev_priv->gt_pm.rc6.cur_residency[i] = time_hw;
  8127. intel_uncore_forcewake_put__locked(dev_priv, fw_domains);
  8128. spin_unlock_irqrestore(&dev_priv->uncore.lock, flags);
  8129. return mul_u64_u32_div(time_hw, mul, div);
  8130. }
  8131. u32 intel_get_cagf(struct drm_i915_private *dev_priv, u32 rpstat)
  8132. {
  8133. u32 cagf;
  8134. if (INTEL_GEN(dev_priv) >= 9)
  8135. cagf = (rpstat & GEN9_CAGF_MASK) >> GEN9_CAGF_SHIFT;
  8136. else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  8137. cagf = (rpstat & HSW_CAGF_MASK) >> HSW_CAGF_SHIFT;
  8138. else
  8139. cagf = (rpstat & GEN6_CAGF_MASK) >> GEN6_CAGF_SHIFT;
  8140. return cagf;
  8141. }