intel_pm.c 206 KB

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