intel_pm.c 218 KB

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