intel_pm.c 239 KB

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