intel_pm.c 207 KB

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