intel_pm.c 211 KB

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