intel_pm.c 205 KB

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