intel_pm.c 227 KB

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