intel_pm.c 269 KB

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