intel_pm.c 224 KB

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