intel_pm.c 186 KB

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