intel_pm.c 234 KB

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