intel_pm.c 228 KB

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