intel_pm.c 224 KB

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