intel_pm.c 258 KB

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