intel_pm.c 213 KB

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