intel_pm.c 225 KB

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