intel_display.c 249 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259
  1. /*
  2. * Copyright © 2006-2007 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
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/cpufreq.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include "drmP.h"
  35. #include "intel_drv.h"
  36. #include "i915_drm.h"
  37. #include "i915_drv.h"
  38. #include "i915_trace.h"
  39. #include "drm_dp_helper.h"
  40. #include "drm_crtc_helper.h"
  41. #include <linux/dma_remapping.h>
  42. #define HAS_eDP (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  43. bool intel_pipe_has_type(struct drm_crtc *crtc, int type);
  44. static void intel_update_watermarks(struct drm_device *dev);
  45. static void intel_increase_pllclock(struct drm_crtc *crtc);
  46. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  47. typedef struct {
  48. /* given values */
  49. int n;
  50. int m1, m2;
  51. int p1, p2;
  52. /* derived values */
  53. int dot;
  54. int vco;
  55. int m;
  56. int p;
  57. } intel_clock_t;
  58. typedef struct {
  59. int min, max;
  60. } intel_range_t;
  61. typedef struct {
  62. int dot_limit;
  63. int p2_slow, p2_fast;
  64. } intel_p2_t;
  65. #define INTEL_P2_NUM 2
  66. typedef struct intel_limit intel_limit_t;
  67. struct intel_limit {
  68. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  69. intel_p2_t p2;
  70. bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
  71. int, int, intel_clock_t *);
  72. };
  73. /* FDI */
  74. #define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
  75. static bool
  76. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  77. int target, int refclk, intel_clock_t *best_clock);
  78. static bool
  79. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  80. int target, int refclk, intel_clock_t *best_clock);
  81. static bool
  82. intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
  83. int target, int refclk, intel_clock_t *best_clock);
  84. static bool
  85. intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
  86. int target, int refclk, intel_clock_t *best_clock);
  87. static inline u32 /* units of 100MHz */
  88. intel_fdi_link_freq(struct drm_device *dev)
  89. {
  90. if (IS_GEN5(dev)) {
  91. struct drm_i915_private *dev_priv = dev->dev_private;
  92. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  93. } else
  94. return 27;
  95. }
  96. static const intel_limit_t intel_limits_i8xx_dvo = {
  97. .dot = { .min = 25000, .max = 350000 },
  98. .vco = { .min = 930000, .max = 1400000 },
  99. .n = { .min = 3, .max = 16 },
  100. .m = { .min = 96, .max = 140 },
  101. .m1 = { .min = 18, .max = 26 },
  102. .m2 = { .min = 6, .max = 16 },
  103. .p = { .min = 4, .max = 128 },
  104. .p1 = { .min = 2, .max = 33 },
  105. .p2 = { .dot_limit = 165000,
  106. .p2_slow = 4, .p2_fast = 2 },
  107. .find_pll = intel_find_best_PLL,
  108. };
  109. static const intel_limit_t intel_limits_i8xx_lvds = {
  110. .dot = { .min = 25000, .max = 350000 },
  111. .vco = { .min = 930000, .max = 1400000 },
  112. .n = { .min = 3, .max = 16 },
  113. .m = { .min = 96, .max = 140 },
  114. .m1 = { .min = 18, .max = 26 },
  115. .m2 = { .min = 6, .max = 16 },
  116. .p = { .min = 4, .max = 128 },
  117. .p1 = { .min = 1, .max = 6 },
  118. .p2 = { .dot_limit = 165000,
  119. .p2_slow = 14, .p2_fast = 7 },
  120. .find_pll = intel_find_best_PLL,
  121. };
  122. static const intel_limit_t intel_limits_i9xx_sdvo = {
  123. .dot = { .min = 20000, .max = 400000 },
  124. .vco = { .min = 1400000, .max = 2800000 },
  125. .n = { .min = 1, .max = 6 },
  126. .m = { .min = 70, .max = 120 },
  127. .m1 = { .min = 10, .max = 22 },
  128. .m2 = { .min = 5, .max = 9 },
  129. .p = { .min = 5, .max = 80 },
  130. .p1 = { .min = 1, .max = 8 },
  131. .p2 = { .dot_limit = 200000,
  132. .p2_slow = 10, .p2_fast = 5 },
  133. .find_pll = intel_find_best_PLL,
  134. };
  135. static const intel_limit_t intel_limits_i9xx_lvds = {
  136. .dot = { .min = 20000, .max = 400000 },
  137. .vco = { .min = 1400000, .max = 2800000 },
  138. .n = { .min = 1, .max = 6 },
  139. .m = { .min = 70, .max = 120 },
  140. .m1 = { .min = 10, .max = 22 },
  141. .m2 = { .min = 5, .max = 9 },
  142. .p = { .min = 7, .max = 98 },
  143. .p1 = { .min = 1, .max = 8 },
  144. .p2 = { .dot_limit = 112000,
  145. .p2_slow = 14, .p2_fast = 7 },
  146. .find_pll = intel_find_best_PLL,
  147. };
  148. static const intel_limit_t intel_limits_g4x_sdvo = {
  149. .dot = { .min = 25000, .max = 270000 },
  150. .vco = { .min = 1750000, .max = 3500000},
  151. .n = { .min = 1, .max = 4 },
  152. .m = { .min = 104, .max = 138 },
  153. .m1 = { .min = 17, .max = 23 },
  154. .m2 = { .min = 5, .max = 11 },
  155. .p = { .min = 10, .max = 30 },
  156. .p1 = { .min = 1, .max = 3},
  157. .p2 = { .dot_limit = 270000,
  158. .p2_slow = 10,
  159. .p2_fast = 10
  160. },
  161. .find_pll = intel_g4x_find_best_PLL,
  162. };
  163. static const intel_limit_t intel_limits_g4x_hdmi = {
  164. .dot = { .min = 22000, .max = 400000 },
  165. .vco = { .min = 1750000, .max = 3500000},
  166. .n = { .min = 1, .max = 4 },
  167. .m = { .min = 104, .max = 138 },
  168. .m1 = { .min = 16, .max = 23 },
  169. .m2 = { .min = 5, .max = 11 },
  170. .p = { .min = 5, .max = 80 },
  171. .p1 = { .min = 1, .max = 8},
  172. .p2 = { .dot_limit = 165000,
  173. .p2_slow = 10, .p2_fast = 5 },
  174. .find_pll = intel_g4x_find_best_PLL,
  175. };
  176. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  177. .dot = { .min = 20000, .max = 115000 },
  178. .vco = { .min = 1750000, .max = 3500000 },
  179. .n = { .min = 1, .max = 3 },
  180. .m = { .min = 104, .max = 138 },
  181. .m1 = { .min = 17, .max = 23 },
  182. .m2 = { .min = 5, .max = 11 },
  183. .p = { .min = 28, .max = 112 },
  184. .p1 = { .min = 2, .max = 8 },
  185. .p2 = { .dot_limit = 0,
  186. .p2_slow = 14, .p2_fast = 14
  187. },
  188. .find_pll = intel_g4x_find_best_PLL,
  189. };
  190. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  191. .dot = { .min = 80000, .max = 224000 },
  192. .vco = { .min = 1750000, .max = 3500000 },
  193. .n = { .min = 1, .max = 3 },
  194. .m = { .min = 104, .max = 138 },
  195. .m1 = { .min = 17, .max = 23 },
  196. .m2 = { .min = 5, .max = 11 },
  197. .p = { .min = 14, .max = 42 },
  198. .p1 = { .min = 2, .max = 6 },
  199. .p2 = { .dot_limit = 0,
  200. .p2_slow = 7, .p2_fast = 7
  201. },
  202. .find_pll = intel_g4x_find_best_PLL,
  203. };
  204. static const intel_limit_t intel_limits_g4x_display_port = {
  205. .dot = { .min = 161670, .max = 227000 },
  206. .vco = { .min = 1750000, .max = 3500000},
  207. .n = { .min = 1, .max = 2 },
  208. .m = { .min = 97, .max = 108 },
  209. .m1 = { .min = 0x10, .max = 0x12 },
  210. .m2 = { .min = 0x05, .max = 0x06 },
  211. .p = { .min = 10, .max = 20 },
  212. .p1 = { .min = 1, .max = 2},
  213. .p2 = { .dot_limit = 0,
  214. .p2_slow = 10, .p2_fast = 10 },
  215. .find_pll = intel_find_pll_g4x_dp,
  216. };
  217. static const intel_limit_t intel_limits_pineview_sdvo = {
  218. .dot = { .min = 20000, .max = 400000},
  219. .vco = { .min = 1700000, .max = 3500000 },
  220. /* Pineview's Ncounter is a ring counter */
  221. .n = { .min = 3, .max = 6 },
  222. .m = { .min = 2, .max = 256 },
  223. /* Pineview only has one combined m divider, which we treat as m2. */
  224. .m1 = { .min = 0, .max = 0 },
  225. .m2 = { .min = 0, .max = 254 },
  226. .p = { .min = 5, .max = 80 },
  227. .p1 = { .min = 1, .max = 8 },
  228. .p2 = { .dot_limit = 200000,
  229. .p2_slow = 10, .p2_fast = 5 },
  230. .find_pll = intel_find_best_PLL,
  231. };
  232. static const intel_limit_t intel_limits_pineview_lvds = {
  233. .dot = { .min = 20000, .max = 400000 },
  234. .vco = { .min = 1700000, .max = 3500000 },
  235. .n = { .min = 3, .max = 6 },
  236. .m = { .min = 2, .max = 256 },
  237. .m1 = { .min = 0, .max = 0 },
  238. .m2 = { .min = 0, .max = 254 },
  239. .p = { .min = 7, .max = 112 },
  240. .p1 = { .min = 1, .max = 8 },
  241. .p2 = { .dot_limit = 112000,
  242. .p2_slow = 14, .p2_fast = 14 },
  243. .find_pll = intel_find_best_PLL,
  244. };
  245. /* Ironlake / Sandybridge
  246. *
  247. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  248. * the range value for them is (actual_value - 2).
  249. */
  250. static const intel_limit_t intel_limits_ironlake_dac = {
  251. .dot = { .min = 25000, .max = 350000 },
  252. .vco = { .min = 1760000, .max = 3510000 },
  253. .n = { .min = 1, .max = 5 },
  254. .m = { .min = 79, .max = 127 },
  255. .m1 = { .min = 12, .max = 22 },
  256. .m2 = { .min = 5, .max = 9 },
  257. .p = { .min = 5, .max = 80 },
  258. .p1 = { .min = 1, .max = 8 },
  259. .p2 = { .dot_limit = 225000,
  260. .p2_slow = 10, .p2_fast = 5 },
  261. .find_pll = intel_g4x_find_best_PLL,
  262. };
  263. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  264. .dot = { .min = 25000, .max = 350000 },
  265. .vco = { .min = 1760000, .max = 3510000 },
  266. .n = { .min = 1, .max = 3 },
  267. .m = { .min = 79, .max = 118 },
  268. .m1 = { .min = 12, .max = 22 },
  269. .m2 = { .min = 5, .max = 9 },
  270. .p = { .min = 28, .max = 112 },
  271. .p1 = { .min = 2, .max = 8 },
  272. .p2 = { .dot_limit = 225000,
  273. .p2_slow = 14, .p2_fast = 14 },
  274. .find_pll = intel_g4x_find_best_PLL,
  275. };
  276. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  277. .dot = { .min = 25000, .max = 350000 },
  278. .vco = { .min = 1760000, .max = 3510000 },
  279. .n = { .min = 1, .max = 3 },
  280. .m = { .min = 79, .max = 127 },
  281. .m1 = { .min = 12, .max = 22 },
  282. .m2 = { .min = 5, .max = 9 },
  283. .p = { .min = 14, .max = 56 },
  284. .p1 = { .min = 2, .max = 8 },
  285. .p2 = { .dot_limit = 225000,
  286. .p2_slow = 7, .p2_fast = 7 },
  287. .find_pll = intel_g4x_find_best_PLL,
  288. };
  289. /* LVDS 100mhz refclk limits. */
  290. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  291. .dot = { .min = 25000, .max = 350000 },
  292. .vco = { .min = 1760000, .max = 3510000 },
  293. .n = { .min = 1, .max = 2 },
  294. .m = { .min = 79, .max = 126 },
  295. .m1 = { .min = 12, .max = 22 },
  296. .m2 = { .min = 5, .max = 9 },
  297. .p = { .min = 28, .max = 112 },
  298. .p1 = { .min = 2, .max = 8 },
  299. .p2 = { .dot_limit = 225000,
  300. .p2_slow = 14, .p2_fast = 14 },
  301. .find_pll = intel_g4x_find_best_PLL,
  302. };
  303. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  304. .dot = { .min = 25000, .max = 350000 },
  305. .vco = { .min = 1760000, .max = 3510000 },
  306. .n = { .min = 1, .max = 3 },
  307. .m = { .min = 79, .max = 126 },
  308. .m1 = { .min = 12, .max = 22 },
  309. .m2 = { .min = 5, .max = 9 },
  310. .p = { .min = 14, .max = 42 },
  311. .p1 = { .min = 2, .max = 6 },
  312. .p2 = { .dot_limit = 225000,
  313. .p2_slow = 7, .p2_fast = 7 },
  314. .find_pll = intel_g4x_find_best_PLL,
  315. };
  316. static const intel_limit_t intel_limits_ironlake_display_port = {
  317. .dot = { .min = 25000, .max = 350000 },
  318. .vco = { .min = 1760000, .max = 3510000},
  319. .n = { .min = 1, .max = 2 },
  320. .m = { .min = 81, .max = 90 },
  321. .m1 = { .min = 12, .max = 22 },
  322. .m2 = { .min = 5, .max = 9 },
  323. .p = { .min = 10, .max = 20 },
  324. .p1 = { .min = 1, .max = 2},
  325. .p2 = { .dot_limit = 0,
  326. .p2_slow = 10, .p2_fast = 10 },
  327. .find_pll = intel_find_pll_ironlake_dp,
  328. };
  329. static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
  330. int refclk)
  331. {
  332. struct drm_device *dev = crtc->dev;
  333. struct drm_i915_private *dev_priv = dev->dev_private;
  334. const intel_limit_t *limit;
  335. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  336. if ((I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) ==
  337. LVDS_CLKB_POWER_UP) {
  338. /* LVDS dual channel */
  339. if (refclk == 100000)
  340. limit = &intel_limits_ironlake_dual_lvds_100m;
  341. else
  342. limit = &intel_limits_ironlake_dual_lvds;
  343. } else {
  344. if (refclk == 100000)
  345. limit = &intel_limits_ironlake_single_lvds_100m;
  346. else
  347. limit = &intel_limits_ironlake_single_lvds;
  348. }
  349. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  350. HAS_eDP)
  351. limit = &intel_limits_ironlake_display_port;
  352. else
  353. limit = &intel_limits_ironlake_dac;
  354. return limit;
  355. }
  356. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  357. {
  358. struct drm_device *dev = crtc->dev;
  359. struct drm_i915_private *dev_priv = dev->dev_private;
  360. const intel_limit_t *limit;
  361. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  362. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  363. LVDS_CLKB_POWER_UP)
  364. /* LVDS with dual channel */
  365. limit = &intel_limits_g4x_dual_channel_lvds;
  366. else
  367. /* LVDS with dual channel */
  368. limit = &intel_limits_g4x_single_channel_lvds;
  369. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  370. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  371. limit = &intel_limits_g4x_hdmi;
  372. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  373. limit = &intel_limits_g4x_sdvo;
  374. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  375. limit = &intel_limits_g4x_display_port;
  376. } else /* The option is for other outputs */
  377. limit = &intel_limits_i9xx_sdvo;
  378. return limit;
  379. }
  380. static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
  381. {
  382. struct drm_device *dev = crtc->dev;
  383. const intel_limit_t *limit;
  384. if (HAS_PCH_SPLIT(dev))
  385. limit = intel_ironlake_limit(crtc, refclk);
  386. else if (IS_G4X(dev)) {
  387. limit = intel_g4x_limit(crtc);
  388. } else if (IS_PINEVIEW(dev)) {
  389. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  390. limit = &intel_limits_pineview_lvds;
  391. else
  392. limit = &intel_limits_pineview_sdvo;
  393. } else if (!IS_GEN2(dev)) {
  394. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  395. limit = &intel_limits_i9xx_lvds;
  396. else
  397. limit = &intel_limits_i9xx_sdvo;
  398. } else {
  399. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  400. limit = &intel_limits_i8xx_lvds;
  401. else
  402. limit = &intel_limits_i8xx_dvo;
  403. }
  404. return limit;
  405. }
  406. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  407. static void pineview_clock(int refclk, intel_clock_t *clock)
  408. {
  409. clock->m = clock->m2 + 2;
  410. clock->p = clock->p1 * clock->p2;
  411. clock->vco = refclk * clock->m / clock->n;
  412. clock->dot = clock->vco / clock->p;
  413. }
  414. static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
  415. {
  416. if (IS_PINEVIEW(dev)) {
  417. pineview_clock(refclk, clock);
  418. return;
  419. }
  420. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  421. clock->p = clock->p1 * clock->p2;
  422. clock->vco = refclk * clock->m / (clock->n + 2);
  423. clock->dot = clock->vco / clock->p;
  424. }
  425. /**
  426. * Returns whether any output on the specified pipe is of the specified type
  427. */
  428. bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
  429. {
  430. struct drm_device *dev = crtc->dev;
  431. struct drm_mode_config *mode_config = &dev->mode_config;
  432. struct intel_encoder *encoder;
  433. list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
  434. if (encoder->base.crtc == crtc && encoder->type == type)
  435. return true;
  436. return false;
  437. }
  438. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  439. /**
  440. * Returns whether the given set of divisors are valid for a given refclk with
  441. * the given connectors.
  442. */
  443. static bool intel_PLL_is_valid(struct drm_device *dev,
  444. const intel_limit_t *limit,
  445. const intel_clock_t *clock)
  446. {
  447. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  448. INTELPllInvalid("p1 out of range\n");
  449. if (clock->p < limit->p.min || limit->p.max < clock->p)
  450. INTELPllInvalid("p out of range\n");
  451. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  452. INTELPllInvalid("m2 out of range\n");
  453. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  454. INTELPllInvalid("m1 out of range\n");
  455. if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
  456. INTELPllInvalid("m1 <= m2\n");
  457. if (clock->m < limit->m.min || limit->m.max < clock->m)
  458. INTELPllInvalid("m out of range\n");
  459. if (clock->n < limit->n.min || limit->n.max < clock->n)
  460. INTELPllInvalid("n out of range\n");
  461. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  462. INTELPllInvalid("vco out of range\n");
  463. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  464. * connector, etc., rather than just a single range.
  465. */
  466. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  467. INTELPllInvalid("dot out of range\n");
  468. return true;
  469. }
  470. static bool
  471. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  472. int target, int refclk, intel_clock_t *best_clock)
  473. {
  474. struct drm_device *dev = crtc->dev;
  475. struct drm_i915_private *dev_priv = dev->dev_private;
  476. intel_clock_t clock;
  477. int err = target;
  478. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  479. (I915_READ(LVDS)) != 0) {
  480. /*
  481. * For LVDS, if the panel is on, just rely on its current
  482. * settings for dual-channel. We haven't figured out how to
  483. * reliably set up different single/dual channel state, if we
  484. * even can.
  485. */
  486. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  487. LVDS_CLKB_POWER_UP)
  488. clock.p2 = limit->p2.p2_fast;
  489. else
  490. clock.p2 = limit->p2.p2_slow;
  491. } else {
  492. if (target < limit->p2.dot_limit)
  493. clock.p2 = limit->p2.p2_slow;
  494. else
  495. clock.p2 = limit->p2.p2_fast;
  496. }
  497. memset(best_clock, 0, sizeof(*best_clock));
  498. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  499. clock.m1++) {
  500. for (clock.m2 = limit->m2.min;
  501. clock.m2 <= limit->m2.max; clock.m2++) {
  502. /* m1 is always 0 in Pineview */
  503. if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
  504. break;
  505. for (clock.n = limit->n.min;
  506. clock.n <= limit->n.max; clock.n++) {
  507. for (clock.p1 = limit->p1.min;
  508. clock.p1 <= limit->p1.max; clock.p1++) {
  509. int this_err;
  510. intel_clock(dev, refclk, &clock);
  511. if (!intel_PLL_is_valid(dev, limit,
  512. &clock))
  513. continue;
  514. this_err = abs(clock.dot - target);
  515. if (this_err < err) {
  516. *best_clock = clock;
  517. err = this_err;
  518. }
  519. }
  520. }
  521. }
  522. }
  523. return (err != target);
  524. }
  525. static bool
  526. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  527. int target, int refclk, intel_clock_t *best_clock)
  528. {
  529. struct drm_device *dev = crtc->dev;
  530. struct drm_i915_private *dev_priv = dev->dev_private;
  531. intel_clock_t clock;
  532. int max_n;
  533. bool found;
  534. /* approximately equals target * 0.00585 */
  535. int err_most = (target >> 8) + (target >> 9);
  536. found = false;
  537. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  538. int lvds_reg;
  539. if (HAS_PCH_SPLIT(dev))
  540. lvds_reg = PCH_LVDS;
  541. else
  542. lvds_reg = LVDS;
  543. if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==
  544. LVDS_CLKB_POWER_UP)
  545. clock.p2 = limit->p2.p2_fast;
  546. else
  547. clock.p2 = limit->p2.p2_slow;
  548. } else {
  549. if (target < limit->p2.dot_limit)
  550. clock.p2 = limit->p2.p2_slow;
  551. else
  552. clock.p2 = limit->p2.p2_fast;
  553. }
  554. memset(best_clock, 0, sizeof(*best_clock));
  555. max_n = limit->n.max;
  556. /* based on hardware requirement, prefer smaller n to precision */
  557. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  558. /* based on hardware requirement, prefere larger m1,m2 */
  559. for (clock.m1 = limit->m1.max;
  560. clock.m1 >= limit->m1.min; clock.m1--) {
  561. for (clock.m2 = limit->m2.max;
  562. clock.m2 >= limit->m2.min; clock.m2--) {
  563. for (clock.p1 = limit->p1.max;
  564. clock.p1 >= limit->p1.min; clock.p1--) {
  565. int this_err;
  566. intel_clock(dev, refclk, &clock);
  567. if (!intel_PLL_is_valid(dev, limit,
  568. &clock))
  569. continue;
  570. this_err = abs(clock.dot - target);
  571. if (this_err < err_most) {
  572. *best_clock = clock;
  573. err_most = this_err;
  574. max_n = clock.n;
  575. found = true;
  576. }
  577. }
  578. }
  579. }
  580. }
  581. return found;
  582. }
  583. static bool
  584. intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  585. int target, int refclk, intel_clock_t *best_clock)
  586. {
  587. struct drm_device *dev = crtc->dev;
  588. intel_clock_t clock;
  589. if (target < 200000) {
  590. clock.n = 1;
  591. clock.p1 = 2;
  592. clock.p2 = 10;
  593. clock.m1 = 12;
  594. clock.m2 = 9;
  595. } else {
  596. clock.n = 2;
  597. clock.p1 = 1;
  598. clock.p2 = 10;
  599. clock.m1 = 14;
  600. clock.m2 = 8;
  601. }
  602. intel_clock(dev, refclk, &clock);
  603. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  604. return true;
  605. }
  606. /* DisplayPort has only two frequencies, 162MHz and 270MHz */
  607. static bool
  608. intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  609. int target, int refclk, intel_clock_t *best_clock)
  610. {
  611. intel_clock_t clock;
  612. if (target < 200000) {
  613. clock.p1 = 2;
  614. clock.p2 = 10;
  615. clock.n = 2;
  616. clock.m1 = 23;
  617. clock.m2 = 8;
  618. } else {
  619. clock.p1 = 1;
  620. clock.p2 = 10;
  621. clock.n = 1;
  622. clock.m1 = 14;
  623. clock.m2 = 2;
  624. }
  625. clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
  626. clock.p = (clock.p1 * clock.p2);
  627. clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
  628. clock.vco = 0;
  629. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  630. return true;
  631. }
  632. /**
  633. * intel_wait_for_vblank - wait for vblank on a given pipe
  634. * @dev: drm device
  635. * @pipe: pipe to wait for
  636. *
  637. * Wait for vblank to occur on a given pipe. Needed for various bits of
  638. * mode setting code.
  639. */
  640. void intel_wait_for_vblank(struct drm_device *dev, int pipe)
  641. {
  642. struct drm_i915_private *dev_priv = dev->dev_private;
  643. int pipestat_reg = PIPESTAT(pipe);
  644. /* Clear existing vblank status. Note this will clear any other
  645. * sticky status fields as well.
  646. *
  647. * This races with i915_driver_irq_handler() with the result
  648. * that either function could miss a vblank event. Here it is not
  649. * fatal, as we will either wait upon the next vblank interrupt or
  650. * timeout. Generally speaking intel_wait_for_vblank() is only
  651. * called during modeset at which time the GPU should be idle and
  652. * should *not* be performing page flips and thus not waiting on
  653. * vblanks...
  654. * Currently, the result of us stealing a vblank from the irq
  655. * handler is that a single frame will be skipped during swapbuffers.
  656. */
  657. I915_WRITE(pipestat_reg,
  658. I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
  659. /* Wait for vblank interrupt bit to set */
  660. if (wait_for(I915_READ(pipestat_reg) &
  661. PIPE_VBLANK_INTERRUPT_STATUS,
  662. 50))
  663. DRM_DEBUG_KMS("vblank wait timed out\n");
  664. }
  665. /*
  666. * intel_wait_for_pipe_off - wait for pipe to turn off
  667. * @dev: drm device
  668. * @pipe: pipe to wait for
  669. *
  670. * After disabling a pipe, we can't wait for vblank in the usual way,
  671. * spinning on the vblank interrupt status bit, since we won't actually
  672. * see an interrupt when the pipe is disabled.
  673. *
  674. * On Gen4 and above:
  675. * wait for the pipe register state bit to turn off
  676. *
  677. * Otherwise:
  678. * wait for the display line value to settle (it usually
  679. * ends up stopping at the start of the next frame).
  680. *
  681. */
  682. void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
  683. {
  684. struct drm_i915_private *dev_priv = dev->dev_private;
  685. if (INTEL_INFO(dev)->gen >= 4) {
  686. int reg = PIPECONF(pipe);
  687. /* Wait for the Pipe State to go off */
  688. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  689. 100))
  690. DRM_DEBUG_KMS("pipe_off wait timed out\n");
  691. } else {
  692. u32 last_line;
  693. int reg = PIPEDSL(pipe);
  694. unsigned long timeout = jiffies + msecs_to_jiffies(100);
  695. /* Wait for the display line to settle */
  696. do {
  697. last_line = I915_READ(reg) & DSL_LINEMASK;
  698. mdelay(5);
  699. } while (((I915_READ(reg) & DSL_LINEMASK) != last_line) &&
  700. time_after(timeout, jiffies));
  701. if (time_after(jiffies, timeout))
  702. DRM_DEBUG_KMS("pipe_off wait timed out\n");
  703. }
  704. }
  705. static const char *state_string(bool enabled)
  706. {
  707. return enabled ? "on" : "off";
  708. }
  709. /* Only for pre-ILK configs */
  710. static void assert_pll(struct drm_i915_private *dev_priv,
  711. enum pipe pipe, bool state)
  712. {
  713. int reg;
  714. u32 val;
  715. bool cur_state;
  716. reg = DPLL(pipe);
  717. val = I915_READ(reg);
  718. cur_state = !!(val & DPLL_VCO_ENABLE);
  719. WARN(cur_state != state,
  720. "PLL state assertion failure (expected %s, current %s)\n",
  721. state_string(state), state_string(cur_state));
  722. }
  723. #define assert_pll_enabled(d, p) assert_pll(d, p, true)
  724. #define assert_pll_disabled(d, p) assert_pll(d, p, false)
  725. /* For ILK+ */
  726. static void assert_pch_pll(struct drm_i915_private *dev_priv,
  727. enum pipe pipe, bool state)
  728. {
  729. int reg;
  730. u32 val;
  731. bool cur_state;
  732. if (HAS_PCH_CPT(dev_priv->dev)) {
  733. u32 pch_dpll;
  734. pch_dpll = I915_READ(PCH_DPLL_SEL);
  735. /* Make sure the selected PLL is enabled to the transcoder */
  736. WARN(!((pch_dpll >> (4 * pipe)) & 8),
  737. "transcoder %d PLL not enabled\n", pipe);
  738. /* Convert the transcoder pipe number to a pll pipe number */
  739. pipe = (pch_dpll >> (4 * pipe)) & 1;
  740. }
  741. reg = PCH_DPLL(pipe);
  742. val = I915_READ(reg);
  743. cur_state = !!(val & DPLL_VCO_ENABLE);
  744. WARN(cur_state != state,
  745. "PCH PLL state assertion failure (expected %s, current %s)\n",
  746. state_string(state), state_string(cur_state));
  747. }
  748. #define assert_pch_pll_enabled(d, p) assert_pch_pll(d, p, true)
  749. #define assert_pch_pll_disabled(d, p) assert_pch_pll(d, p, false)
  750. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  751. enum pipe pipe, bool state)
  752. {
  753. int reg;
  754. u32 val;
  755. bool cur_state;
  756. reg = FDI_TX_CTL(pipe);
  757. val = I915_READ(reg);
  758. cur_state = !!(val & FDI_TX_ENABLE);
  759. WARN(cur_state != state,
  760. "FDI TX state assertion failure (expected %s, current %s)\n",
  761. state_string(state), state_string(cur_state));
  762. }
  763. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  764. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  765. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  766. enum pipe pipe, bool state)
  767. {
  768. int reg;
  769. u32 val;
  770. bool cur_state;
  771. reg = FDI_RX_CTL(pipe);
  772. val = I915_READ(reg);
  773. cur_state = !!(val & FDI_RX_ENABLE);
  774. WARN(cur_state != state,
  775. "FDI RX state assertion failure (expected %s, current %s)\n",
  776. state_string(state), state_string(cur_state));
  777. }
  778. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  779. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  780. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  781. enum pipe pipe)
  782. {
  783. int reg;
  784. u32 val;
  785. /* ILK FDI PLL is always enabled */
  786. if (dev_priv->info->gen == 5)
  787. return;
  788. reg = FDI_TX_CTL(pipe);
  789. val = I915_READ(reg);
  790. WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  791. }
  792. static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
  793. enum pipe pipe)
  794. {
  795. int reg;
  796. u32 val;
  797. reg = FDI_RX_CTL(pipe);
  798. val = I915_READ(reg);
  799. WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
  800. }
  801. static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  802. enum pipe pipe)
  803. {
  804. int pp_reg, lvds_reg;
  805. u32 val;
  806. enum pipe panel_pipe = PIPE_A;
  807. bool locked = true;
  808. if (HAS_PCH_SPLIT(dev_priv->dev)) {
  809. pp_reg = PCH_PP_CONTROL;
  810. lvds_reg = PCH_LVDS;
  811. } else {
  812. pp_reg = PP_CONTROL;
  813. lvds_reg = LVDS;
  814. }
  815. val = I915_READ(pp_reg);
  816. if (!(val & PANEL_POWER_ON) ||
  817. ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
  818. locked = false;
  819. if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
  820. panel_pipe = PIPE_B;
  821. WARN(panel_pipe == pipe && locked,
  822. "panel assertion failure, pipe %c regs locked\n",
  823. pipe_name(pipe));
  824. }
  825. void assert_pipe(struct drm_i915_private *dev_priv,
  826. enum pipe pipe, bool state)
  827. {
  828. int reg;
  829. u32 val;
  830. bool cur_state;
  831. reg = PIPECONF(pipe);
  832. val = I915_READ(reg);
  833. cur_state = !!(val & PIPECONF_ENABLE);
  834. WARN(cur_state != state,
  835. "pipe %c assertion failure (expected %s, current %s)\n",
  836. pipe_name(pipe), state_string(state), state_string(cur_state));
  837. }
  838. static void assert_plane_enabled(struct drm_i915_private *dev_priv,
  839. enum plane plane)
  840. {
  841. int reg;
  842. u32 val;
  843. reg = DSPCNTR(plane);
  844. val = I915_READ(reg);
  845. WARN(!(val & DISPLAY_PLANE_ENABLE),
  846. "plane %c assertion failure, should be active but is disabled\n",
  847. plane_name(plane));
  848. }
  849. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  850. enum pipe pipe)
  851. {
  852. int reg, i;
  853. u32 val;
  854. int cur_pipe;
  855. /* Planes are fixed to pipes on ILK+ */
  856. if (HAS_PCH_SPLIT(dev_priv->dev))
  857. return;
  858. /* Need to check both planes against the pipe */
  859. for (i = 0; i < 2; i++) {
  860. reg = DSPCNTR(i);
  861. val = I915_READ(reg);
  862. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  863. DISPPLANE_SEL_PIPE_SHIFT;
  864. WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  865. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  866. plane_name(i), pipe_name(pipe));
  867. }
  868. }
  869. static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  870. {
  871. u32 val;
  872. bool enabled;
  873. val = I915_READ(PCH_DREF_CONTROL);
  874. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  875. DREF_SUPERSPREAD_SOURCE_MASK));
  876. WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  877. }
  878. static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
  879. enum pipe pipe)
  880. {
  881. int reg;
  882. u32 val;
  883. bool enabled;
  884. reg = TRANSCONF(pipe);
  885. val = I915_READ(reg);
  886. enabled = !!(val & TRANS_ENABLE);
  887. WARN(enabled,
  888. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  889. pipe_name(pipe));
  890. }
  891. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  892. enum pipe pipe, u32 port_sel, u32 val)
  893. {
  894. if ((val & DP_PORT_EN) == 0)
  895. return false;
  896. if (HAS_PCH_CPT(dev_priv->dev)) {
  897. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  898. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  899. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  900. return false;
  901. } else {
  902. if ((val & DP_PIPE_MASK) != (pipe << 30))
  903. return false;
  904. }
  905. return true;
  906. }
  907. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  908. enum pipe pipe, u32 val)
  909. {
  910. if ((val & PORT_ENABLE) == 0)
  911. return false;
  912. if (HAS_PCH_CPT(dev_priv->dev)) {
  913. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  914. return false;
  915. } else {
  916. if ((val & TRANSCODER_MASK) != TRANSCODER(pipe))
  917. return false;
  918. }
  919. return true;
  920. }
  921. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  922. enum pipe pipe, u32 val)
  923. {
  924. if ((val & LVDS_PORT_EN) == 0)
  925. return false;
  926. if (HAS_PCH_CPT(dev_priv->dev)) {
  927. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  928. return false;
  929. } else {
  930. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  931. return false;
  932. }
  933. return true;
  934. }
  935. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  936. enum pipe pipe, u32 val)
  937. {
  938. if ((val & ADPA_DAC_ENABLE) == 0)
  939. return false;
  940. if (HAS_PCH_CPT(dev_priv->dev)) {
  941. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  942. return false;
  943. } else {
  944. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  945. return false;
  946. }
  947. return true;
  948. }
  949. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  950. enum pipe pipe, int reg, u32 port_sel)
  951. {
  952. u32 val = I915_READ(reg);
  953. WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  954. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  955. reg, pipe_name(pipe));
  956. }
  957. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  958. enum pipe pipe, int reg)
  959. {
  960. u32 val = I915_READ(reg);
  961. WARN(hdmi_pipe_enabled(dev_priv, val, pipe),
  962. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  963. reg, pipe_name(pipe));
  964. }
  965. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  966. enum pipe pipe)
  967. {
  968. int reg;
  969. u32 val;
  970. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  971. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  972. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  973. reg = PCH_ADPA;
  974. val = I915_READ(reg);
  975. WARN(adpa_pipe_enabled(dev_priv, val, pipe),
  976. "PCH VGA enabled on transcoder %c, should be disabled\n",
  977. pipe_name(pipe));
  978. reg = PCH_LVDS;
  979. val = I915_READ(reg);
  980. WARN(lvds_pipe_enabled(dev_priv, val, pipe),
  981. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  982. pipe_name(pipe));
  983. assert_pch_hdmi_disabled(dev_priv, pipe, HDMIB);
  984. assert_pch_hdmi_disabled(dev_priv, pipe, HDMIC);
  985. assert_pch_hdmi_disabled(dev_priv, pipe, HDMID);
  986. }
  987. /**
  988. * intel_enable_pll - enable a PLL
  989. * @dev_priv: i915 private structure
  990. * @pipe: pipe PLL to enable
  991. *
  992. * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
  993. * make sure the PLL reg is writable first though, since the panel write
  994. * protect mechanism may be enabled.
  995. *
  996. * Note! This is for pre-ILK only.
  997. */
  998. static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  999. {
  1000. int reg;
  1001. u32 val;
  1002. /* No really, not for ILK+ */
  1003. BUG_ON(dev_priv->info->gen >= 5);
  1004. /* PLL is protected by panel, make sure we can write it */
  1005. if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
  1006. assert_panel_unlocked(dev_priv, pipe);
  1007. reg = DPLL(pipe);
  1008. val = I915_READ(reg);
  1009. val |= DPLL_VCO_ENABLE;
  1010. /* We do this three times for luck */
  1011. I915_WRITE(reg, val);
  1012. POSTING_READ(reg);
  1013. udelay(150); /* wait for warmup */
  1014. I915_WRITE(reg, val);
  1015. POSTING_READ(reg);
  1016. udelay(150); /* wait for warmup */
  1017. I915_WRITE(reg, val);
  1018. POSTING_READ(reg);
  1019. udelay(150); /* wait for warmup */
  1020. }
  1021. /**
  1022. * intel_disable_pll - disable a PLL
  1023. * @dev_priv: i915 private structure
  1024. * @pipe: pipe PLL to disable
  1025. *
  1026. * Disable the PLL for @pipe, making sure the pipe is off first.
  1027. *
  1028. * Note! This is for pre-ILK only.
  1029. */
  1030. static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1031. {
  1032. int reg;
  1033. u32 val;
  1034. /* Don't disable pipe A or pipe A PLLs if needed */
  1035. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1036. return;
  1037. /* Make sure the pipe isn't still relying on us */
  1038. assert_pipe_disabled(dev_priv, pipe);
  1039. reg = DPLL(pipe);
  1040. val = I915_READ(reg);
  1041. val &= ~DPLL_VCO_ENABLE;
  1042. I915_WRITE(reg, val);
  1043. POSTING_READ(reg);
  1044. }
  1045. /**
  1046. * intel_enable_pch_pll - enable PCH PLL
  1047. * @dev_priv: i915 private structure
  1048. * @pipe: pipe PLL to enable
  1049. *
  1050. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1051. * drives the transcoder clock.
  1052. */
  1053. static void intel_enable_pch_pll(struct drm_i915_private *dev_priv,
  1054. enum pipe pipe)
  1055. {
  1056. int reg;
  1057. u32 val;
  1058. if (pipe > 1)
  1059. return;
  1060. /* PCH only available on ILK+ */
  1061. BUG_ON(dev_priv->info->gen < 5);
  1062. /* PCH refclock must be enabled first */
  1063. assert_pch_refclk_enabled(dev_priv);
  1064. reg = PCH_DPLL(pipe);
  1065. val = I915_READ(reg);
  1066. val |= DPLL_VCO_ENABLE;
  1067. I915_WRITE(reg, val);
  1068. POSTING_READ(reg);
  1069. udelay(200);
  1070. }
  1071. static void intel_disable_pch_pll(struct drm_i915_private *dev_priv,
  1072. enum pipe pipe)
  1073. {
  1074. int reg;
  1075. u32 val, pll_mask = TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL,
  1076. pll_sel = TRANSC_DPLL_ENABLE;
  1077. if (pipe > 1)
  1078. return;
  1079. /* PCH only available on ILK+ */
  1080. BUG_ON(dev_priv->info->gen < 5);
  1081. /* Make sure transcoder isn't still depending on us */
  1082. assert_transcoder_disabled(dev_priv, pipe);
  1083. if (pipe == 0)
  1084. pll_sel |= TRANSC_DPLLA_SEL;
  1085. else if (pipe == 1)
  1086. pll_sel |= TRANSC_DPLLB_SEL;
  1087. if ((I915_READ(PCH_DPLL_SEL) & pll_mask) == pll_sel)
  1088. return;
  1089. reg = PCH_DPLL(pipe);
  1090. val = I915_READ(reg);
  1091. val &= ~DPLL_VCO_ENABLE;
  1092. I915_WRITE(reg, val);
  1093. POSTING_READ(reg);
  1094. udelay(200);
  1095. }
  1096. static void intel_enable_transcoder(struct drm_i915_private *dev_priv,
  1097. enum pipe pipe)
  1098. {
  1099. int reg;
  1100. u32 val;
  1101. /* PCH only available on ILK+ */
  1102. BUG_ON(dev_priv->info->gen < 5);
  1103. /* Make sure PCH DPLL is enabled */
  1104. assert_pch_pll_enabled(dev_priv, pipe);
  1105. /* FDI must be feeding us bits for PCH ports */
  1106. assert_fdi_tx_enabled(dev_priv, pipe);
  1107. assert_fdi_rx_enabled(dev_priv, pipe);
  1108. reg = TRANSCONF(pipe);
  1109. val = I915_READ(reg);
  1110. if (HAS_PCH_IBX(dev_priv->dev)) {
  1111. /*
  1112. * make the BPC in transcoder be consistent with
  1113. * that in pipeconf reg.
  1114. */
  1115. val &= ~PIPE_BPC_MASK;
  1116. val |= I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK;
  1117. }
  1118. I915_WRITE(reg, val | TRANS_ENABLE);
  1119. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1120. DRM_ERROR("failed to enable transcoder %d\n", pipe);
  1121. }
  1122. static void intel_disable_transcoder(struct drm_i915_private *dev_priv,
  1123. enum pipe pipe)
  1124. {
  1125. int reg;
  1126. u32 val;
  1127. /* FDI relies on the transcoder */
  1128. assert_fdi_tx_disabled(dev_priv, pipe);
  1129. assert_fdi_rx_disabled(dev_priv, pipe);
  1130. /* Ports must be off as well */
  1131. assert_pch_ports_disabled(dev_priv, pipe);
  1132. reg = TRANSCONF(pipe);
  1133. val = I915_READ(reg);
  1134. val &= ~TRANS_ENABLE;
  1135. I915_WRITE(reg, val);
  1136. /* wait for PCH transcoder off, transcoder state */
  1137. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1138. DRM_ERROR("failed to disable transcoder %d\n", pipe);
  1139. }
  1140. /**
  1141. * intel_enable_pipe - enable a pipe, asserting requirements
  1142. * @dev_priv: i915 private structure
  1143. * @pipe: pipe to enable
  1144. * @pch_port: on ILK+, is this pipe driving a PCH port or not
  1145. *
  1146. * Enable @pipe, making sure that various hardware specific requirements
  1147. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1148. *
  1149. * @pipe should be %PIPE_A or %PIPE_B.
  1150. *
  1151. * Will wait until the pipe is actually running (i.e. first vblank) before
  1152. * returning.
  1153. */
  1154. static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
  1155. bool pch_port)
  1156. {
  1157. int reg;
  1158. u32 val;
  1159. /*
  1160. * A pipe without a PLL won't actually be able to drive bits from
  1161. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1162. * need the check.
  1163. */
  1164. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1165. assert_pll_enabled(dev_priv, pipe);
  1166. else {
  1167. if (pch_port) {
  1168. /* if driving the PCH, we need FDI enabled */
  1169. assert_fdi_rx_pll_enabled(dev_priv, pipe);
  1170. assert_fdi_tx_pll_enabled(dev_priv, pipe);
  1171. }
  1172. /* FIXME: assert CPU port conditions for SNB+ */
  1173. }
  1174. reg = PIPECONF(pipe);
  1175. val = I915_READ(reg);
  1176. if (val & PIPECONF_ENABLE)
  1177. return;
  1178. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1179. intel_wait_for_vblank(dev_priv->dev, pipe);
  1180. }
  1181. /**
  1182. * intel_disable_pipe - disable a pipe, asserting requirements
  1183. * @dev_priv: i915 private structure
  1184. * @pipe: pipe to disable
  1185. *
  1186. * Disable @pipe, making sure that various hardware specific requirements
  1187. * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
  1188. *
  1189. * @pipe should be %PIPE_A or %PIPE_B.
  1190. *
  1191. * Will wait until the pipe has shut down before returning.
  1192. */
  1193. static void intel_disable_pipe(struct drm_i915_private *dev_priv,
  1194. enum pipe pipe)
  1195. {
  1196. int reg;
  1197. u32 val;
  1198. /*
  1199. * Make sure planes won't keep trying to pump pixels to us,
  1200. * or we might hang the display.
  1201. */
  1202. assert_planes_disabled(dev_priv, pipe);
  1203. /* Don't disable pipe A or pipe A PLLs if needed */
  1204. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1205. return;
  1206. reg = PIPECONF(pipe);
  1207. val = I915_READ(reg);
  1208. if ((val & PIPECONF_ENABLE) == 0)
  1209. return;
  1210. I915_WRITE(reg, val & ~PIPECONF_ENABLE);
  1211. intel_wait_for_pipe_off(dev_priv->dev, pipe);
  1212. }
  1213. /*
  1214. * Plane regs are double buffered, going from enabled->disabled needs a
  1215. * trigger in order to latch. The display address reg provides this.
  1216. */
  1217. static void intel_flush_display_plane(struct drm_i915_private *dev_priv,
  1218. enum plane plane)
  1219. {
  1220. I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
  1221. I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
  1222. }
  1223. /**
  1224. * intel_enable_plane - enable a display plane on a given pipe
  1225. * @dev_priv: i915 private structure
  1226. * @plane: plane to enable
  1227. * @pipe: pipe being fed
  1228. *
  1229. * Enable @plane on @pipe, making sure that @pipe is running first.
  1230. */
  1231. static void intel_enable_plane(struct drm_i915_private *dev_priv,
  1232. enum plane plane, enum pipe pipe)
  1233. {
  1234. int reg;
  1235. u32 val;
  1236. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1237. assert_pipe_enabled(dev_priv, pipe);
  1238. reg = DSPCNTR(plane);
  1239. val = I915_READ(reg);
  1240. if (val & DISPLAY_PLANE_ENABLE)
  1241. return;
  1242. I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
  1243. intel_flush_display_plane(dev_priv, plane);
  1244. intel_wait_for_vblank(dev_priv->dev, pipe);
  1245. }
  1246. /**
  1247. * intel_disable_plane - disable a display plane
  1248. * @dev_priv: i915 private structure
  1249. * @plane: plane to disable
  1250. * @pipe: pipe consuming the data
  1251. *
  1252. * Disable @plane; should be an independent operation.
  1253. */
  1254. static void intel_disable_plane(struct drm_i915_private *dev_priv,
  1255. enum plane plane, enum pipe pipe)
  1256. {
  1257. int reg;
  1258. u32 val;
  1259. reg = DSPCNTR(plane);
  1260. val = I915_READ(reg);
  1261. if ((val & DISPLAY_PLANE_ENABLE) == 0)
  1262. return;
  1263. I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
  1264. intel_flush_display_plane(dev_priv, plane);
  1265. intel_wait_for_vblank(dev_priv->dev, pipe);
  1266. }
  1267. static void disable_pch_dp(struct drm_i915_private *dev_priv,
  1268. enum pipe pipe, int reg, u32 port_sel)
  1269. {
  1270. u32 val = I915_READ(reg);
  1271. if (dp_pipe_enabled(dev_priv, pipe, port_sel, val)) {
  1272. DRM_DEBUG_KMS("Disabling pch dp %x on pipe %d\n", reg, pipe);
  1273. I915_WRITE(reg, val & ~DP_PORT_EN);
  1274. }
  1275. }
  1276. static void disable_pch_hdmi(struct drm_i915_private *dev_priv,
  1277. enum pipe pipe, int reg)
  1278. {
  1279. u32 val = I915_READ(reg);
  1280. if (hdmi_pipe_enabled(dev_priv, val, pipe)) {
  1281. DRM_DEBUG_KMS("Disabling pch HDMI %x on pipe %d\n",
  1282. reg, pipe);
  1283. I915_WRITE(reg, val & ~PORT_ENABLE);
  1284. }
  1285. }
  1286. /* Disable any ports connected to this transcoder */
  1287. static void intel_disable_pch_ports(struct drm_i915_private *dev_priv,
  1288. enum pipe pipe)
  1289. {
  1290. u32 reg, val;
  1291. val = I915_READ(PCH_PP_CONTROL);
  1292. I915_WRITE(PCH_PP_CONTROL, val | PANEL_UNLOCK_REGS);
  1293. disable_pch_dp(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1294. disable_pch_dp(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1295. disable_pch_dp(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1296. reg = PCH_ADPA;
  1297. val = I915_READ(reg);
  1298. if (adpa_pipe_enabled(dev_priv, val, pipe))
  1299. I915_WRITE(reg, val & ~ADPA_DAC_ENABLE);
  1300. reg = PCH_LVDS;
  1301. val = I915_READ(reg);
  1302. if (lvds_pipe_enabled(dev_priv, val, pipe)) {
  1303. DRM_DEBUG_KMS("disable lvds on pipe %d val 0x%08x\n", pipe, val);
  1304. I915_WRITE(reg, val & ~LVDS_PORT_EN);
  1305. POSTING_READ(reg);
  1306. udelay(100);
  1307. }
  1308. disable_pch_hdmi(dev_priv, pipe, HDMIB);
  1309. disable_pch_hdmi(dev_priv, pipe, HDMIC);
  1310. disable_pch_hdmi(dev_priv, pipe, HDMID);
  1311. }
  1312. static void i8xx_disable_fbc(struct drm_device *dev)
  1313. {
  1314. struct drm_i915_private *dev_priv = dev->dev_private;
  1315. u32 fbc_ctl;
  1316. /* Disable compression */
  1317. fbc_ctl = I915_READ(FBC_CONTROL);
  1318. if ((fbc_ctl & FBC_CTL_EN) == 0)
  1319. return;
  1320. fbc_ctl &= ~FBC_CTL_EN;
  1321. I915_WRITE(FBC_CONTROL, fbc_ctl);
  1322. /* Wait for compressing bit to clear */
  1323. if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
  1324. DRM_DEBUG_KMS("FBC idle timed out\n");
  1325. return;
  1326. }
  1327. DRM_DEBUG_KMS("disabled FBC\n");
  1328. }
  1329. static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1330. {
  1331. struct drm_device *dev = crtc->dev;
  1332. struct drm_i915_private *dev_priv = dev->dev_private;
  1333. struct drm_framebuffer *fb = crtc->fb;
  1334. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1335. struct drm_i915_gem_object *obj = intel_fb->obj;
  1336. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1337. int cfb_pitch;
  1338. int plane, i;
  1339. u32 fbc_ctl, fbc_ctl2;
  1340. cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
  1341. if (fb->pitches[0] < cfb_pitch)
  1342. cfb_pitch = fb->pitches[0];
  1343. /* FBC_CTL wants 64B units */
  1344. cfb_pitch = (cfb_pitch / 64) - 1;
  1345. plane = intel_crtc->plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
  1346. /* Clear old tags */
  1347. for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
  1348. I915_WRITE(FBC_TAG + (i * 4), 0);
  1349. /* Set it up... */
  1350. fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
  1351. fbc_ctl2 |= plane;
  1352. I915_WRITE(FBC_CONTROL2, fbc_ctl2);
  1353. I915_WRITE(FBC_FENCE_OFF, crtc->y);
  1354. /* enable it... */
  1355. fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
  1356. if (IS_I945GM(dev))
  1357. fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
  1358. fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
  1359. fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
  1360. fbc_ctl |= obj->fence_reg;
  1361. I915_WRITE(FBC_CONTROL, fbc_ctl);
  1362. DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %d, ",
  1363. cfb_pitch, crtc->y, intel_crtc->plane);
  1364. }
  1365. static bool i8xx_fbc_enabled(struct drm_device *dev)
  1366. {
  1367. struct drm_i915_private *dev_priv = dev->dev_private;
  1368. return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
  1369. }
  1370. static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1371. {
  1372. struct drm_device *dev = crtc->dev;
  1373. struct drm_i915_private *dev_priv = dev->dev_private;
  1374. struct drm_framebuffer *fb = crtc->fb;
  1375. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1376. struct drm_i915_gem_object *obj = intel_fb->obj;
  1377. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1378. int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
  1379. unsigned long stall_watermark = 200;
  1380. u32 dpfc_ctl;
  1381. dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
  1382. dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
  1383. I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
  1384. I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  1385. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  1386. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  1387. I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
  1388. /* enable it... */
  1389. I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
  1390. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  1391. }
  1392. static void g4x_disable_fbc(struct drm_device *dev)
  1393. {
  1394. struct drm_i915_private *dev_priv = dev->dev_private;
  1395. u32 dpfc_ctl;
  1396. /* Disable compression */
  1397. dpfc_ctl = I915_READ(DPFC_CONTROL);
  1398. if (dpfc_ctl & DPFC_CTL_EN) {
  1399. dpfc_ctl &= ~DPFC_CTL_EN;
  1400. I915_WRITE(DPFC_CONTROL, dpfc_ctl);
  1401. DRM_DEBUG_KMS("disabled FBC\n");
  1402. }
  1403. }
  1404. static bool g4x_fbc_enabled(struct drm_device *dev)
  1405. {
  1406. struct drm_i915_private *dev_priv = dev->dev_private;
  1407. return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
  1408. }
  1409. static void sandybridge_blit_fbc_update(struct drm_device *dev)
  1410. {
  1411. struct drm_i915_private *dev_priv = dev->dev_private;
  1412. u32 blt_ecoskpd;
  1413. /* Make sure blitter notifies FBC of writes */
  1414. gen6_gt_force_wake_get(dev_priv);
  1415. blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
  1416. blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
  1417. GEN6_BLITTER_LOCK_SHIFT;
  1418. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  1419. blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
  1420. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  1421. blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
  1422. GEN6_BLITTER_LOCK_SHIFT);
  1423. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  1424. POSTING_READ(GEN6_BLITTER_ECOSKPD);
  1425. gen6_gt_force_wake_put(dev_priv);
  1426. }
  1427. static void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1428. {
  1429. struct drm_device *dev = crtc->dev;
  1430. struct drm_i915_private *dev_priv = dev->dev_private;
  1431. struct drm_framebuffer *fb = crtc->fb;
  1432. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1433. struct drm_i915_gem_object *obj = intel_fb->obj;
  1434. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1435. int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
  1436. unsigned long stall_watermark = 200;
  1437. u32 dpfc_ctl;
  1438. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  1439. dpfc_ctl &= DPFC_RESERVED;
  1440. dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
  1441. /* Set persistent mode for front-buffer rendering, ala X. */
  1442. dpfc_ctl |= DPFC_CTL_PERSISTENT_MODE;
  1443. dpfc_ctl |= (DPFC_CTL_FENCE_EN | obj->fence_reg);
  1444. I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
  1445. I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  1446. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  1447. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  1448. I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
  1449. I915_WRITE(ILK_FBC_RT_BASE, obj->gtt_offset | ILK_FBC_RT_VALID);
  1450. /* enable it... */
  1451. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
  1452. if (IS_GEN6(dev)) {
  1453. I915_WRITE(SNB_DPFC_CTL_SA,
  1454. SNB_CPU_FENCE_ENABLE | obj->fence_reg);
  1455. I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
  1456. sandybridge_blit_fbc_update(dev);
  1457. }
  1458. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  1459. }
  1460. static void ironlake_disable_fbc(struct drm_device *dev)
  1461. {
  1462. struct drm_i915_private *dev_priv = dev->dev_private;
  1463. u32 dpfc_ctl;
  1464. /* Disable compression */
  1465. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  1466. if (dpfc_ctl & DPFC_CTL_EN) {
  1467. dpfc_ctl &= ~DPFC_CTL_EN;
  1468. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
  1469. DRM_DEBUG_KMS("disabled FBC\n");
  1470. }
  1471. }
  1472. static bool ironlake_fbc_enabled(struct drm_device *dev)
  1473. {
  1474. struct drm_i915_private *dev_priv = dev->dev_private;
  1475. return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
  1476. }
  1477. bool intel_fbc_enabled(struct drm_device *dev)
  1478. {
  1479. struct drm_i915_private *dev_priv = dev->dev_private;
  1480. if (!dev_priv->display.fbc_enabled)
  1481. return false;
  1482. return dev_priv->display.fbc_enabled(dev);
  1483. }
  1484. static void intel_fbc_work_fn(struct work_struct *__work)
  1485. {
  1486. struct intel_fbc_work *work =
  1487. container_of(to_delayed_work(__work),
  1488. struct intel_fbc_work, work);
  1489. struct drm_device *dev = work->crtc->dev;
  1490. struct drm_i915_private *dev_priv = dev->dev_private;
  1491. mutex_lock(&dev->struct_mutex);
  1492. if (work == dev_priv->fbc_work) {
  1493. /* Double check that we haven't switched fb without cancelling
  1494. * the prior work.
  1495. */
  1496. if (work->crtc->fb == work->fb) {
  1497. dev_priv->display.enable_fbc(work->crtc,
  1498. work->interval);
  1499. dev_priv->cfb_plane = to_intel_crtc(work->crtc)->plane;
  1500. dev_priv->cfb_fb = work->crtc->fb->base.id;
  1501. dev_priv->cfb_y = work->crtc->y;
  1502. }
  1503. dev_priv->fbc_work = NULL;
  1504. }
  1505. mutex_unlock(&dev->struct_mutex);
  1506. kfree(work);
  1507. }
  1508. static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
  1509. {
  1510. if (dev_priv->fbc_work == NULL)
  1511. return;
  1512. DRM_DEBUG_KMS("cancelling pending FBC enable\n");
  1513. /* Synchronisation is provided by struct_mutex and checking of
  1514. * dev_priv->fbc_work, so we can perform the cancellation
  1515. * entirely asynchronously.
  1516. */
  1517. if (cancel_delayed_work(&dev_priv->fbc_work->work))
  1518. /* tasklet was killed before being run, clean up */
  1519. kfree(dev_priv->fbc_work);
  1520. /* Mark the work as no longer wanted so that if it does
  1521. * wake-up (because the work was already running and waiting
  1522. * for our mutex), it will discover that is no longer
  1523. * necessary to run.
  1524. */
  1525. dev_priv->fbc_work = NULL;
  1526. }
  1527. static void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1528. {
  1529. struct intel_fbc_work *work;
  1530. struct drm_device *dev = crtc->dev;
  1531. struct drm_i915_private *dev_priv = dev->dev_private;
  1532. if (!dev_priv->display.enable_fbc)
  1533. return;
  1534. intel_cancel_fbc_work(dev_priv);
  1535. work = kzalloc(sizeof *work, GFP_KERNEL);
  1536. if (work == NULL) {
  1537. dev_priv->display.enable_fbc(crtc, interval);
  1538. return;
  1539. }
  1540. work->crtc = crtc;
  1541. work->fb = crtc->fb;
  1542. work->interval = interval;
  1543. INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
  1544. dev_priv->fbc_work = work;
  1545. DRM_DEBUG_KMS("scheduling delayed FBC enable\n");
  1546. /* Delay the actual enabling to let pageflipping cease and the
  1547. * display to settle before starting the compression. Note that
  1548. * this delay also serves a second purpose: it allows for a
  1549. * vblank to pass after disabling the FBC before we attempt
  1550. * to modify the control registers.
  1551. *
  1552. * A more complicated solution would involve tracking vblanks
  1553. * following the termination of the page-flipping sequence
  1554. * and indeed performing the enable as a co-routine and not
  1555. * waiting synchronously upon the vblank.
  1556. */
  1557. schedule_delayed_work(&work->work, msecs_to_jiffies(50));
  1558. }
  1559. void intel_disable_fbc(struct drm_device *dev)
  1560. {
  1561. struct drm_i915_private *dev_priv = dev->dev_private;
  1562. intel_cancel_fbc_work(dev_priv);
  1563. if (!dev_priv->display.disable_fbc)
  1564. return;
  1565. dev_priv->display.disable_fbc(dev);
  1566. dev_priv->cfb_plane = -1;
  1567. }
  1568. /**
  1569. * intel_update_fbc - enable/disable FBC as needed
  1570. * @dev: the drm_device
  1571. *
  1572. * Set up the framebuffer compression hardware at mode set time. We
  1573. * enable it if possible:
  1574. * - plane A only (on pre-965)
  1575. * - no pixel mulitply/line duplication
  1576. * - no alpha buffer discard
  1577. * - no dual wide
  1578. * - framebuffer <= 2048 in width, 1536 in height
  1579. *
  1580. * We can't assume that any compression will take place (worst case),
  1581. * so the compressed buffer has to be the same size as the uncompressed
  1582. * one. It also must reside (along with the line length buffer) in
  1583. * stolen memory.
  1584. *
  1585. * We need to enable/disable FBC on a global basis.
  1586. */
  1587. static void intel_update_fbc(struct drm_device *dev)
  1588. {
  1589. struct drm_i915_private *dev_priv = dev->dev_private;
  1590. struct drm_crtc *crtc = NULL, *tmp_crtc;
  1591. struct intel_crtc *intel_crtc;
  1592. struct drm_framebuffer *fb;
  1593. struct intel_framebuffer *intel_fb;
  1594. struct drm_i915_gem_object *obj;
  1595. int enable_fbc;
  1596. DRM_DEBUG_KMS("\n");
  1597. if (!i915_powersave)
  1598. return;
  1599. if (!I915_HAS_FBC(dev))
  1600. return;
  1601. /*
  1602. * If FBC is already on, we just have to verify that we can
  1603. * keep it that way...
  1604. * Need to disable if:
  1605. * - more than one pipe is active
  1606. * - changing FBC params (stride, fence, mode)
  1607. * - new fb is too large to fit in compressed buffer
  1608. * - going to an unsupported config (interlace, pixel multiply, etc.)
  1609. */
  1610. list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
  1611. if (tmp_crtc->enabled && tmp_crtc->fb) {
  1612. if (crtc) {
  1613. DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
  1614. dev_priv->no_fbc_reason = FBC_MULTIPLE_PIPES;
  1615. goto out_disable;
  1616. }
  1617. crtc = tmp_crtc;
  1618. }
  1619. }
  1620. if (!crtc || crtc->fb == NULL) {
  1621. DRM_DEBUG_KMS("no output, disabling\n");
  1622. dev_priv->no_fbc_reason = FBC_NO_OUTPUT;
  1623. goto out_disable;
  1624. }
  1625. intel_crtc = to_intel_crtc(crtc);
  1626. fb = crtc->fb;
  1627. intel_fb = to_intel_framebuffer(fb);
  1628. obj = intel_fb->obj;
  1629. enable_fbc = i915_enable_fbc;
  1630. if (enable_fbc < 0) {
  1631. DRM_DEBUG_KMS("fbc set to per-chip default\n");
  1632. enable_fbc = 1;
  1633. if (INTEL_INFO(dev)->gen <= 6)
  1634. enable_fbc = 0;
  1635. }
  1636. if (!enable_fbc) {
  1637. DRM_DEBUG_KMS("fbc disabled per module param\n");
  1638. dev_priv->no_fbc_reason = FBC_MODULE_PARAM;
  1639. goto out_disable;
  1640. }
  1641. if (intel_fb->obj->base.size > dev_priv->cfb_size) {
  1642. DRM_DEBUG_KMS("framebuffer too large, disabling "
  1643. "compression\n");
  1644. dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
  1645. goto out_disable;
  1646. }
  1647. if ((crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) ||
  1648. (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)) {
  1649. DRM_DEBUG_KMS("mode incompatible with compression, "
  1650. "disabling\n");
  1651. dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;
  1652. goto out_disable;
  1653. }
  1654. if ((crtc->mode.hdisplay > 2048) ||
  1655. (crtc->mode.vdisplay > 1536)) {
  1656. DRM_DEBUG_KMS("mode too large for compression, disabling\n");
  1657. dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;
  1658. goto out_disable;
  1659. }
  1660. if ((IS_I915GM(dev) || IS_I945GM(dev)) && intel_crtc->plane != 0) {
  1661. DRM_DEBUG_KMS("plane not 0, disabling compression\n");
  1662. dev_priv->no_fbc_reason = FBC_BAD_PLANE;
  1663. goto out_disable;
  1664. }
  1665. /* The use of a CPU fence is mandatory in order to detect writes
  1666. * by the CPU to the scanout and trigger updates to the FBC.
  1667. */
  1668. if (obj->tiling_mode != I915_TILING_X ||
  1669. obj->fence_reg == I915_FENCE_REG_NONE) {
  1670. DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
  1671. dev_priv->no_fbc_reason = FBC_NOT_TILED;
  1672. goto out_disable;
  1673. }
  1674. /* If the kernel debugger is active, always disable compression */
  1675. if (in_dbg_master())
  1676. goto out_disable;
  1677. /* If the scanout has not changed, don't modify the FBC settings.
  1678. * Note that we make the fundamental assumption that the fb->obj
  1679. * cannot be unpinned (and have its GTT offset and fence revoked)
  1680. * without first being decoupled from the scanout and FBC disabled.
  1681. */
  1682. if (dev_priv->cfb_plane == intel_crtc->plane &&
  1683. dev_priv->cfb_fb == fb->base.id &&
  1684. dev_priv->cfb_y == crtc->y)
  1685. return;
  1686. if (intel_fbc_enabled(dev)) {
  1687. /* We update FBC along two paths, after changing fb/crtc
  1688. * configuration (modeswitching) and after page-flipping
  1689. * finishes. For the latter, we know that not only did
  1690. * we disable the FBC at the start of the page-flip
  1691. * sequence, but also more than one vblank has passed.
  1692. *
  1693. * For the former case of modeswitching, it is possible
  1694. * to switch between two FBC valid configurations
  1695. * instantaneously so we do need to disable the FBC
  1696. * before we can modify its control registers. We also
  1697. * have to wait for the next vblank for that to take
  1698. * effect. However, since we delay enabling FBC we can
  1699. * assume that a vblank has passed since disabling and
  1700. * that we can safely alter the registers in the deferred
  1701. * callback.
  1702. *
  1703. * In the scenario that we go from a valid to invalid
  1704. * and then back to valid FBC configuration we have
  1705. * no strict enforcement that a vblank occurred since
  1706. * disabling the FBC. However, along all current pipe
  1707. * disabling paths we do need to wait for a vblank at
  1708. * some point. And we wait before enabling FBC anyway.
  1709. */
  1710. DRM_DEBUG_KMS("disabling active FBC for update\n");
  1711. intel_disable_fbc(dev);
  1712. }
  1713. intel_enable_fbc(crtc, 500);
  1714. return;
  1715. out_disable:
  1716. /* Multiple disables should be harmless */
  1717. if (intel_fbc_enabled(dev)) {
  1718. DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
  1719. intel_disable_fbc(dev);
  1720. }
  1721. }
  1722. int
  1723. intel_pin_and_fence_fb_obj(struct drm_device *dev,
  1724. struct drm_i915_gem_object *obj,
  1725. struct intel_ring_buffer *pipelined)
  1726. {
  1727. struct drm_i915_private *dev_priv = dev->dev_private;
  1728. u32 alignment;
  1729. int ret;
  1730. switch (obj->tiling_mode) {
  1731. case I915_TILING_NONE:
  1732. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1733. alignment = 128 * 1024;
  1734. else if (INTEL_INFO(dev)->gen >= 4)
  1735. alignment = 4 * 1024;
  1736. else
  1737. alignment = 64 * 1024;
  1738. break;
  1739. case I915_TILING_X:
  1740. /* pin() will align the object as required by fence */
  1741. alignment = 0;
  1742. break;
  1743. case I915_TILING_Y:
  1744. /* FIXME: Is this true? */
  1745. DRM_ERROR("Y tiled not allowed for scan out buffers\n");
  1746. return -EINVAL;
  1747. default:
  1748. BUG();
  1749. }
  1750. dev_priv->mm.interruptible = false;
  1751. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1752. if (ret)
  1753. goto err_interruptible;
  1754. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1755. * fence, whereas 965+ only requires a fence if using
  1756. * framebuffer compression. For simplicity, we always install
  1757. * a fence as the cost is not that onerous.
  1758. */
  1759. if (obj->tiling_mode != I915_TILING_NONE) {
  1760. ret = i915_gem_object_get_fence(obj, pipelined);
  1761. if (ret)
  1762. goto err_unpin;
  1763. }
  1764. dev_priv->mm.interruptible = true;
  1765. return 0;
  1766. err_unpin:
  1767. i915_gem_object_unpin(obj);
  1768. err_interruptible:
  1769. dev_priv->mm.interruptible = true;
  1770. return ret;
  1771. }
  1772. static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1773. int x, int y)
  1774. {
  1775. struct drm_device *dev = crtc->dev;
  1776. struct drm_i915_private *dev_priv = dev->dev_private;
  1777. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1778. struct intel_framebuffer *intel_fb;
  1779. struct drm_i915_gem_object *obj;
  1780. int plane = intel_crtc->plane;
  1781. unsigned long Start, Offset;
  1782. u32 dspcntr;
  1783. u32 reg;
  1784. switch (plane) {
  1785. case 0:
  1786. case 1:
  1787. break;
  1788. default:
  1789. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1790. return -EINVAL;
  1791. }
  1792. intel_fb = to_intel_framebuffer(fb);
  1793. obj = intel_fb->obj;
  1794. reg = DSPCNTR(plane);
  1795. dspcntr = I915_READ(reg);
  1796. /* Mask out pixel format bits in case we change it */
  1797. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1798. switch (fb->bits_per_pixel) {
  1799. case 8:
  1800. dspcntr |= DISPPLANE_8BPP;
  1801. break;
  1802. case 16:
  1803. if (fb->depth == 15)
  1804. dspcntr |= DISPPLANE_15_16BPP;
  1805. else
  1806. dspcntr |= DISPPLANE_16BPP;
  1807. break;
  1808. case 24:
  1809. case 32:
  1810. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  1811. break;
  1812. default:
  1813. DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
  1814. return -EINVAL;
  1815. }
  1816. if (INTEL_INFO(dev)->gen >= 4) {
  1817. if (obj->tiling_mode != I915_TILING_NONE)
  1818. dspcntr |= DISPPLANE_TILED;
  1819. else
  1820. dspcntr &= ~DISPPLANE_TILED;
  1821. }
  1822. I915_WRITE(reg, dspcntr);
  1823. Start = obj->gtt_offset;
  1824. Offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  1825. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  1826. Start, Offset, x, y, fb->pitches[0]);
  1827. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  1828. if (INTEL_INFO(dev)->gen >= 4) {
  1829. I915_WRITE(DSPSURF(plane), Start);
  1830. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  1831. I915_WRITE(DSPADDR(plane), Offset);
  1832. } else
  1833. I915_WRITE(DSPADDR(plane), Start + Offset);
  1834. POSTING_READ(reg);
  1835. return 0;
  1836. }
  1837. static int ironlake_update_plane(struct drm_crtc *crtc,
  1838. struct drm_framebuffer *fb, int x, int y)
  1839. {
  1840. struct drm_device *dev = crtc->dev;
  1841. struct drm_i915_private *dev_priv = dev->dev_private;
  1842. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1843. struct intel_framebuffer *intel_fb;
  1844. struct drm_i915_gem_object *obj;
  1845. int plane = intel_crtc->plane;
  1846. unsigned long Start, Offset;
  1847. u32 dspcntr;
  1848. u32 reg;
  1849. switch (plane) {
  1850. case 0:
  1851. case 1:
  1852. case 2:
  1853. break;
  1854. default:
  1855. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1856. return -EINVAL;
  1857. }
  1858. intel_fb = to_intel_framebuffer(fb);
  1859. obj = intel_fb->obj;
  1860. reg = DSPCNTR(plane);
  1861. dspcntr = I915_READ(reg);
  1862. /* Mask out pixel format bits in case we change it */
  1863. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1864. switch (fb->bits_per_pixel) {
  1865. case 8:
  1866. dspcntr |= DISPPLANE_8BPP;
  1867. break;
  1868. case 16:
  1869. if (fb->depth != 16)
  1870. return -EINVAL;
  1871. dspcntr |= DISPPLANE_16BPP;
  1872. break;
  1873. case 24:
  1874. case 32:
  1875. if (fb->depth == 24)
  1876. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  1877. else if (fb->depth == 30)
  1878. dspcntr |= DISPPLANE_32BPP_30BIT_NO_ALPHA;
  1879. else
  1880. return -EINVAL;
  1881. break;
  1882. default:
  1883. DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
  1884. return -EINVAL;
  1885. }
  1886. if (obj->tiling_mode != I915_TILING_NONE)
  1887. dspcntr |= DISPPLANE_TILED;
  1888. else
  1889. dspcntr &= ~DISPPLANE_TILED;
  1890. /* must disable */
  1891. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  1892. I915_WRITE(reg, dspcntr);
  1893. Start = obj->gtt_offset;
  1894. Offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  1895. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  1896. Start, Offset, x, y, fb->pitches[0]);
  1897. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  1898. I915_WRITE(DSPSURF(plane), Start);
  1899. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  1900. I915_WRITE(DSPADDR(plane), Offset);
  1901. POSTING_READ(reg);
  1902. return 0;
  1903. }
  1904. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  1905. static int
  1906. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1907. int x, int y, enum mode_set_atomic state)
  1908. {
  1909. struct drm_device *dev = crtc->dev;
  1910. struct drm_i915_private *dev_priv = dev->dev_private;
  1911. int ret;
  1912. ret = dev_priv->display.update_plane(crtc, fb, x, y);
  1913. if (ret)
  1914. return ret;
  1915. intel_update_fbc(dev);
  1916. intel_increase_pllclock(crtc);
  1917. return 0;
  1918. }
  1919. static int
  1920. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  1921. struct drm_framebuffer *old_fb)
  1922. {
  1923. struct drm_device *dev = crtc->dev;
  1924. struct drm_i915_master_private *master_priv;
  1925. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1926. int ret;
  1927. /* no fb bound */
  1928. if (!crtc->fb) {
  1929. DRM_ERROR("No FB bound\n");
  1930. return 0;
  1931. }
  1932. switch (intel_crtc->plane) {
  1933. case 0:
  1934. case 1:
  1935. break;
  1936. case 2:
  1937. if (IS_IVYBRIDGE(dev))
  1938. break;
  1939. /* fall through otherwise */
  1940. default:
  1941. DRM_ERROR("no plane for crtc\n");
  1942. return -EINVAL;
  1943. }
  1944. mutex_lock(&dev->struct_mutex);
  1945. ret = intel_pin_and_fence_fb_obj(dev,
  1946. to_intel_framebuffer(crtc->fb)->obj,
  1947. NULL);
  1948. if (ret != 0) {
  1949. mutex_unlock(&dev->struct_mutex);
  1950. DRM_ERROR("pin & fence failed\n");
  1951. return ret;
  1952. }
  1953. if (old_fb) {
  1954. struct drm_i915_private *dev_priv = dev->dev_private;
  1955. struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
  1956. wait_event(dev_priv->pending_flip_queue,
  1957. atomic_read(&dev_priv->mm.wedged) ||
  1958. atomic_read(&obj->pending_flip) == 0);
  1959. /* Big Hammer, we also need to ensure that any pending
  1960. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  1961. * current scanout is retired before unpinning the old
  1962. * framebuffer.
  1963. *
  1964. * This should only fail upon a hung GPU, in which case we
  1965. * can safely continue.
  1966. */
  1967. ret = i915_gem_object_finish_gpu(obj);
  1968. (void) ret;
  1969. }
  1970. ret = intel_pipe_set_base_atomic(crtc, crtc->fb, x, y,
  1971. LEAVE_ATOMIC_MODE_SET);
  1972. if (ret) {
  1973. i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
  1974. mutex_unlock(&dev->struct_mutex);
  1975. DRM_ERROR("failed to update base address\n");
  1976. return ret;
  1977. }
  1978. if (old_fb) {
  1979. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1980. i915_gem_object_unpin(to_intel_framebuffer(old_fb)->obj);
  1981. }
  1982. mutex_unlock(&dev->struct_mutex);
  1983. if (!dev->primary->master)
  1984. return 0;
  1985. master_priv = dev->primary->master->driver_priv;
  1986. if (!master_priv->sarea_priv)
  1987. return 0;
  1988. if (intel_crtc->pipe) {
  1989. master_priv->sarea_priv->pipeB_x = x;
  1990. master_priv->sarea_priv->pipeB_y = y;
  1991. } else {
  1992. master_priv->sarea_priv->pipeA_x = x;
  1993. master_priv->sarea_priv->pipeA_y = y;
  1994. }
  1995. return 0;
  1996. }
  1997. static void ironlake_set_pll_edp(struct drm_crtc *crtc, int clock)
  1998. {
  1999. struct drm_device *dev = crtc->dev;
  2000. struct drm_i915_private *dev_priv = dev->dev_private;
  2001. u32 dpa_ctl;
  2002. DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
  2003. dpa_ctl = I915_READ(DP_A);
  2004. dpa_ctl &= ~DP_PLL_FREQ_MASK;
  2005. if (clock < 200000) {
  2006. u32 temp;
  2007. dpa_ctl |= DP_PLL_FREQ_160MHZ;
  2008. /* workaround for 160Mhz:
  2009. 1) program 0x4600c bits 15:0 = 0x8124
  2010. 2) program 0x46010 bit 0 = 1
  2011. 3) program 0x46034 bit 24 = 1
  2012. 4) program 0x64000 bit 14 = 1
  2013. */
  2014. temp = I915_READ(0x4600c);
  2015. temp &= 0xffff0000;
  2016. I915_WRITE(0x4600c, temp | 0x8124);
  2017. temp = I915_READ(0x46010);
  2018. I915_WRITE(0x46010, temp | 1);
  2019. temp = I915_READ(0x46034);
  2020. I915_WRITE(0x46034, temp | (1 << 24));
  2021. } else {
  2022. dpa_ctl |= DP_PLL_FREQ_270MHZ;
  2023. }
  2024. I915_WRITE(DP_A, dpa_ctl);
  2025. POSTING_READ(DP_A);
  2026. udelay(500);
  2027. }
  2028. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2029. {
  2030. struct drm_device *dev = crtc->dev;
  2031. struct drm_i915_private *dev_priv = dev->dev_private;
  2032. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2033. int pipe = intel_crtc->pipe;
  2034. u32 reg, temp;
  2035. /* enable normal train */
  2036. reg = FDI_TX_CTL(pipe);
  2037. temp = I915_READ(reg);
  2038. if (IS_IVYBRIDGE(dev)) {
  2039. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2040. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2041. } else {
  2042. temp &= ~FDI_LINK_TRAIN_NONE;
  2043. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2044. }
  2045. I915_WRITE(reg, temp);
  2046. reg = FDI_RX_CTL(pipe);
  2047. temp = I915_READ(reg);
  2048. if (HAS_PCH_CPT(dev)) {
  2049. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2050. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2051. } else {
  2052. temp &= ~FDI_LINK_TRAIN_NONE;
  2053. temp |= FDI_LINK_TRAIN_NONE;
  2054. }
  2055. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2056. /* wait one idle pattern time */
  2057. POSTING_READ(reg);
  2058. udelay(1000);
  2059. /* IVB wants error correction enabled */
  2060. if (IS_IVYBRIDGE(dev))
  2061. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2062. FDI_FE_ERRC_ENABLE);
  2063. }
  2064. static void cpt_phase_pointer_enable(struct drm_device *dev, int pipe)
  2065. {
  2066. struct drm_i915_private *dev_priv = dev->dev_private;
  2067. u32 flags = I915_READ(SOUTH_CHICKEN1);
  2068. flags |= FDI_PHASE_SYNC_OVR(pipe);
  2069. I915_WRITE(SOUTH_CHICKEN1, flags); /* once to unlock... */
  2070. flags |= FDI_PHASE_SYNC_EN(pipe);
  2071. I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to enable */
  2072. POSTING_READ(SOUTH_CHICKEN1);
  2073. }
  2074. /* The FDI link training functions for ILK/Ibexpeak. */
  2075. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2076. {
  2077. struct drm_device *dev = crtc->dev;
  2078. struct drm_i915_private *dev_priv = dev->dev_private;
  2079. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2080. int pipe = intel_crtc->pipe;
  2081. int plane = intel_crtc->plane;
  2082. u32 reg, temp, tries;
  2083. /* FDI needs bits from pipe & plane first */
  2084. assert_pipe_enabled(dev_priv, pipe);
  2085. assert_plane_enabled(dev_priv, plane);
  2086. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2087. for train result */
  2088. reg = FDI_RX_IMR(pipe);
  2089. temp = I915_READ(reg);
  2090. temp &= ~FDI_RX_SYMBOL_LOCK;
  2091. temp &= ~FDI_RX_BIT_LOCK;
  2092. I915_WRITE(reg, temp);
  2093. I915_READ(reg);
  2094. udelay(150);
  2095. /* enable CPU FDI TX and PCH FDI RX */
  2096. reg = FDI_TX_CTL(pipe);
  2097. temp = I915_READ(reg);
  2098. temp &= ~(7 << 19);
  2099. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2100. temp &= ~FDI_LINK_TRAIN_NONE;
  2101. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2102. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2103. reg = FDI_RX_CTL(pipe);
  2104. temp = I915_READ(reg);
  2105. temp &= ~FDI_LINK_TRAIN_NONE;
  2106. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2107. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2108. POSTING_READ(reg);
  2109. udelay(150);
  2110. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2111. if (HAS_PCH_IBX(dev)) {
  2112. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2113. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2114. FDI_RX_PHASE_SYNC_POINTER_EN);
  2115. }
  2116. reg = FDI_RX_IIR(pipe);
  2117. for (tries = 0; tries < 5; tries++) {
  2118. temp = I915_READ(reg);
  2119. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2120. if ((temp & FDI_RX_BIT_LOCK)) {
  2121. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2122. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2123. break;
  2124. }
  2125. }
  2126. if (tries == 5)
  2127. DRM_ERROR("FDI train 1 fail!\n");
  2128. /* Train 2 */
  2129. reg = FDI_TX_CTL(pipe);
  2130. temp = I915_READ(reg);
  2131. temp &= ~FDI_LINK_TRAIN_NONE;
  2132. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2133. I915_WRITE(reg, temp);
  2134. reg = FDI_RX_CTL(pipe);
  2135. temp = I915_READ(reg);
  2136. temp &= ~FDI_LINK_TRAIN_NONE;
  2137. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2138. I915_WRITE(reg, temp);
  2139. POSTING_READ(reg);
  2140. udelay(150);
  2141. reg = FDI_RX_IIR(pipe);
  2142. for (tries = 0; tries < 5; tries++) {
  2143. temp = I915_READ(reg);
  2144. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2145. if (temp & FDI_RX_SYMBOL_LOCK) {
  2146. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2147. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2148. break;
  2149. }
  2150. }
  2151. if (tries == 5)
  2152. DRM_ERROR("FDI train 2 fail!\n");
  2153. DRM_DEBUG_KMS("FDI train done\n");
  2154. }
  2155. static const int snb_b_fdi_train_param[] = {
  2156. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2157. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2158. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2159. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2160. };
  2161. /* The FDI link training functions for SNB/Cougarpoint. */
  2162. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2163. {
  2164. struct drm_device *dev = crtc->dev;
  2165. struct drm_i915_private *dev_priv = dev->dev_private;
  2166. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2167. int pipe = intel_crtc->pipe;
  2168. u32 reg, temp, i;
  2169. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2170. for train result */
  2171. reg = FDI_RX_IMR(pipe);
  2172. temp = I915_READ(reg);
  2173. temp &= ~FDI_RX_SYMBOL_LOCK;
  2174. temp &= ~FDI_RX_BIT_LOCK;
  2175. I915_WRITE(reg, temp);
  2176. POSTING_READ(reg);
  2177. udelay(150);
  2178. /* enable CPU FDI TX and PCH FDI RX */
  2179. reg = FDI_TX_CTL(pipe);
  2180. temp = I915_READ(reg);
  2181. temp &= ~(7 << 19);
  2182. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2183. temp &= ~FDI_LINK_TRAIN_NONE;
  2184. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2185. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2186. /* SNB-B */
  2187. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2188. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2189. reg = FDI_RX_CTL(pipe);
  2190. temp = I915_READ(reg);
  2191. if (HAS_PCH_CPT(dev)) {
  2192. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2193. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2194. } else {
  2195. temp &= ~FDI_LINK_TRAIN_NONE;
  2196. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2197. }
  2198. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2199. POSTING_READ(reg);
  2200. udelay(150);
  2201. if (HAS_PCH_CPT(dev))
  2202. cpt_phase_pointer_enable(dev, pipe);
  2203. for (i = 0; i < 4; i++) {
  2204. reg = FDI_TX_CTL(pipe);
  2205. temp = I915_READ(reg);
  2206. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2207. temp |= snb_b_fdi_train_param[i];
  2208. I915_WRITE(reg, temp);
  2209. POSTING_READ(reg);
  2210. udelay(500);
  2211. reg = FDI_RX_IIR(pipe);
  2212. temp = I915_READ(reg);
  2213. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2214. if (temp & FDI_RX_BIT_LOCK) {
  2215. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2216. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2217. break;
  2218. }
  2219. }
  2220. if (i == 4)
  2221. DRM_ERROR("FDI train 1 fail!\n");
  2222. /* Train 2 */
  2223. reg = FDI_TX_CTL(pipe);
  2224. temp = I915_READ(reg);
  2225. temp &= ~FDI_LINK_TRAIN_NONE;
  2226. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2227. if (IS_GEN6(dev)) {
  2228. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2229. /* SNB-B */
  2230. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2231. }
  2232. I915_WRITE(reg, temp);
  2233. reg = FDI_RX_CTL(pipe);
  2234. temp = I915_READ(reg);
  2235. if (HAS_PCH_CPT(dev)) {
  2236. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2237. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2238. } else {
  2239. temp &= ~FDI_LINK_TRAIN_NONE;
  2240. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2241. }
  2242. I915_WRITE(reg, temp);
  2243. POSTING_READ(reg);
  2244. udelay(150);
  2245. for (i = 0; i < 4; i++) {
  2246. reg = FDI_TX_CTL(pipe);
  2247. temp = I915_READ(reg);
  2248. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2249. temp |= snb_b_fdi_train_param[i];
  2250. I915_WRITE(reg, temp);
  2251. POSTING_READ(reg);
  2252. udelay(500);
  2253. reg = FDI_RX_IIR(pipe);
  2254. temp = I915_READ(reg);
  2255. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2256. if (temp & FDI_RX_SYMBOL_LOCK) {
  2257. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2258. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2259. break;
  2260. }
  2261. }
  2262. if (i == 4)
  2263. DRM_ERROR("FDI train 2 fail!\n");
  2264. DRM_DEBUG_KMS("FDI train done.\n");
  2265. }
  2266. /* Manual link training for Ivy Bridge A0 parts */
  2267. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2268. {
  2269. struct drm_device *dev = crtc->dev;
  2270. struct drm_i915_private *dev_priv = dev->dev_private;
  2271. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2272. int pipe = intel_crtc->pipe;
  2273. u32 reg, temp, i;
  2274. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2275. for train result */
  2276. reg = FDI_RX_IMR(pipe);
  2277. temp = I915_READ(reg);
  2278. temp &= ~FDI_RX_SYMBOL_LOCK;
  2279. temp &= ~FDI_RX_BIT_LOCK;
  2280. I915_WRITE(reg, temp);
  2281. POSTING_READ(reg);
  2282. udelay(150);
  2283. /* enable CPU FDI TX and PCH FDI RX */
  2284. reg = FDI_TX_CTL(pipe);
  2285. temp = I915_READ(reg);
  2286. temp &= ~(7 << 19);
  2287. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2288. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2289. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2290. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2291. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2292. temp |= FDI_COMPOSITE_SYNC;
  2293. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2294. reg = FDI_RX_CTL(pipe);
  2295. temp = I915_READ(reg);
  2296. temp &= ~FDI_LINK_TRAIN_AUTO;
  2297. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2298. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2299. temp |= FDI_COMPOSITE_SYNC;
  2300. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2301. POSTING_READ(reg);
  2302. udelay(150);
  2303. if (HAS_PCH_CPT(dev))
  2304. cpt_phase_pointer_enable(dev, pipe);
  2305. for (i = 0; i < 4; i++) {
  2306. reg = FDI_TX_CTL(pipe);
  2307. temp = I915_READ(reg);
  2308. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2309. temp |= snb_b_fdi_train_param[i];
  2310. I915_WRITE(reg, temp);
  2311. POSTING_READ(reg);
  2312. udelay(500);
  2313. reg = FDI_RX_IIR(pipe);
  2314. temp = I915_READ(reg);
  2315. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2316. if (temp & FDI_RX_BIT_LOCK ||
  2317. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2318. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2319. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2320. break;
  2321. }
  2322. }
  2323. if (i == 4)
  2324. DRM_ERROR("FDI train 1 fail!\n");
  2325. /* Train 2 */
  2326. reg = FDI_TX_CTL(pipe);
  2327. temp = I915_READ(reg);
  2328. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2329. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2330. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2331. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2332. I915_WRITE(reg, temp);
  2333. reg = FDI_RX_CTL(pipe);
  2334. temp = I915_READ(reg);
  2335. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2336. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2337. I915_WRITE(reg, temp);
  2338. POSTING_READ(reg);
  2339. udelay(150);
  2340. for (i = 0; i < 4; i++) {
  2341. reg = FDI_TX_CTL(pipe);
  2342. temp = I915_READ(reg);
  2343. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2344. temp |= snb_b_fdi_train_param[i];
  2345. I915_WRITE(reg, temp);
  2346. POSTING_READ(reg);
  2347. udelay(500);
  2348. reg = FDI_RX_IIR(pipe);
  2349. temp = I915_READ(reg);
  2350. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2351. if (temp & FDI_RX_SYMBOL_LOCK) {
  2352. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2353. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2354. break;
  2355. }
  2356. }
  2357. if (i == 4)
  2358. DRM_ERROR("FDI train 2 fail!\n");
  2359. DRM_DEBUG_KMS("FDI train done.\n");
  2360. }
  2361. static void ironlake_fdi_pll_enable(struct drm_crtc *crtc)
  2362. {
  2363. struct drm_device *dev = crtc->dev;
  2364. struct drm_i915_private *dev_priv = dev->dev_private;
  2365. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2366. int pipe = intel_crtc->pipe;
  2367. u32 reg, temp;
  2368. /* Write the TU size bits so error detection works */
  2369. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  2370. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  2371. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2372. reg = FDI_RX_CTL(pipe);
  2373. temp = I915_READ(reg);
  2374. temp &= ~((0x7 << 19) | (0x7 << 16));
  2375. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2376. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2377. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2378. POSTING_READ(reg);
  2379. udelay(200);
  2380. /* Switch from Rawclk to PCDclk */
  2381. temp = I915_READ(reg);
  2382. I915_WRITE(reg, temp | FDI_PCDCLK);
  2383. POSTING_READ(reg);
  2384. udelay(200);
  2385. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2386. reg = FDI_TX_CTL(pipe);
  2387. temp = I915_READ(reg);
  2388. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2389. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2390. POSTING_READ(reg);
  2391. udelay(100);
  2392. }
  2393. }
  2394. static void cpt_phase_pointer_disable(struct drm_device *dev, int pipe)
  2395. {
  2396. struct drm_i915_private *dev_priv = dev->dev_private;
  2397. u32 flags = I915_READ(SOUTH_CHICKEN1);
  2398. flags &= ~(FDI_PHASE_SYNC_EN(pipe));
  2399. I915_WRITE(SOUTH_CHICKEN1, flags); /* once to disable... */
  2400. flags &= ~(FDI_PHASE_SYNC_OVR(pipe));
  2401. I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to lock */
  2402. POSTING_READ(SOUTH_CHICKEN1);
  2403. }
  2404. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2405. {
  2406. struct drm_device *dev = crtc->dev;
  2407. struct drm_i915_private *dev_priv = dev->dev_private;
  2408. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2409. int pipe = intel_crtc->pipe;
  2410. u32 reg, temp;
  2411. /* disable CPU FDI tx and PCH FDI rx */
  2412. reg = FDI_TX_CTL(pipe);
  2413. temp = I915_READ(reg);
  2414. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2415. POSTING_READ(reg);
  2416. reg = FDI_RX_CTL(pipe);
  2417. temp = I915_READ(reg);
  2418. temp &= ~(0x7 << 16);
  2419. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2420. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2421. POSTING_READ(reg);
  2422. udelay(100);
  2423. /* Ironlake workaround, disable clock pointer after downing FDI */
  2424. if (HAS_PCH_IBX(dev)) {
  2425. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2426. I915_WRITE(FDI_RX_CHICKEN(pipe),
  2427. I915_READ(FDI_RX_CHICKEN(pipe) &
  2428. ~FDI_RX_PHASE_SYNC_POINTER_EN));
  2429. } else if (HAS_PCH_CPT(dev)) {
  2430. cpt_phase_pointer_disable(dev, pipe);
  2431. }
  2432. /* still set train pattern 1 */
  2433. reg = FDI_TX_CTL(pipe);
  2434. temp = I915_READ(reg);
  2435. temp &= ~FDI_LINK_TRAIN_NONE;
  2436. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2437. I915_WRITE(reg, temp);
  2438. reg = FDI_RX_CTL(pipe);
  2439. temp = I915_READ(reg);
  2440. if (HAS_PCH_CPT(dev)) {
  2441. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2442. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2443. } else {
  2444. temp &= ~FDI_LINK_TRAIN_NONE;
  2445. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2446. }
  2447. /* BPC in FDI rx is consistent with that in PIPECONF */
  2448. temp &= ~(0x07 << 16);
  2449. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2450. I915_WRITE(reg, temp);
  2451. POSTING_READ(reg);
  2452. udelay(100);
  2453. }
  2454. /*
  2455. * When we disable a pipe, we need to clear any pending scanline wait events
  2456. * to avoid hanging the ring, which we assume we are waiting on.
  2457. */
  2458. static void intel_clear_scanline_wait(struct drm_device *dev)
  2459. {
  2460. struct drm_i915_private *dev_priv = dev->dev_private;
  2461. struct intel_ring_buffer *ring;
  2462. u32 tmp;
  2463. if (IS_GEN2(dev))
  2464. /* Can't break the hang on i8xx */
  2465. return;
  2466. ring = LP_RING(dev_priv);
  2467. tmp = I915_READ_CTL(ring);
  2468. if (tmp & RING_WAIT)
  2469. I915_WRITE_CTL(ring, tmp);
  2470. }
  2471. static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  2472. {
  2473. struct drm_i915_gem_object *obj;
  2474. struct drm_i915_private *dev_priv;
  2475. if (crtc->fb == NULL)
  2476. return;
  2477. obj = to_intel_framebuffer(crtc->fb)->obj;
  2478. dev_priv = crtc->dev->dev_private;
  2479. wait_event(dev_priv->pending_flip_queue,
  2480. atomic_read(&obj->pending_flip) == 0);
  2481. }
  2482. static bool intel_crtc_driving_pch(struct drm_crtc *crtc)
  2483. {
  2484. struct drm_device *dev = crtc->dev;
  2485. struct drm_mode_config *mode_config = &dev->mode_config;
  2486. struct intel_encoder *encoder;
  2487. /*
  2488. * If there's a non-PCH eDP on this crtc, it must be DP_A, and that
  2489. * must be driven by its own crtc; no sharing is possible.
  2490. */
  2491. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  2492. if (encoder->base.crtc != crtc)
  2493. continue;
  2494. switch (encoder->type) {
  2495. case INTEL_OUTPUT_EDP:
  2496. if (!intel_encoder_is_pch_edp(&encoder->base))
  2497. return false;
  2498. continue;
  2499. }
  2500. }
  2501. return true;
  2502. }
  2503. /*
  2504. * Enable PCH resources required for PCH ports:
  2505. * - PCH PLLs
  2506. * - FDI training & RX/TX
  2507. * - update transcoder timings
  2508. * - DP transcoding bits
  2509. * - transcoder
  2510. */
  2511. static void ironlake_pch_enable(struct drm_crtc *crtc)
  2512. {
  2513. struct drm_device *dev = crtc->dev;
  2514. struct drm_i915_private *dev_priv = dev->dev_private;
  2515. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2516. int pipe = intel_crtc->pipe;
  2517. u32 reg, temp, transc_sel;
  2518. /* For PCH output, training FDI link */
  2519. dev_priv->display.fdi_link_train(crtc);
  2520. intel_enable_pch_pll(dev_priv, pipe);
  2521. if (HAS_PCH_CPT(dev)) {
  2522. transc_sel = intel_crtc->use_pll_a ? TRANSC_DPLLA_SEL :
  2523. TRANSC_DPLLB_SEL;
  2524. /* Be sure PCH DPLL SEL is set */
  2525. temp = I915_READ(PCH_DPLL_SEL);
  2526. if (pipe == 0) {
  2527. temp &= ~(TRANSA_DPLLB_SEL);
  2528. temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
  2529. } else if (pipe == 1) {
  2530. temp &= ~(TRANSB_DPLLB_SEL);
  2531. temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  2532. } else if (pipe == 2) {
  2533. temp &= ~(TRANSC_DPLLB_SEL);
  2534. temp |= (TRANSC_DPLL_ENABLE | transc_sel);
  2535. }
  2536. I915_WRITE(PCH_DPLL_SEL, temp);
  2537. }
  2538. /* set transcoder timing, panel must allow it */
  2539. assert_panel_unlocked(dev_priv, pipe);
  2540. I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
  2541. I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
  2542. I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
  2543. I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
  2544. I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
  2545. I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
  2546. intel_fdi_normal_train(crtc);
  2547. /* For PCH DP, enable TRANS_DP_CTL */
  2548. if (HAS_PCH_CPT(dev) &&
  2549. (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  2550. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2551. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) >> 5;
  2552. reg = TRANS_DP_CTL(pipe);
  2553. temp = I915_READ(reg);
  2554. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  2555. TRANS_DP_SYNC_MASK |
  2556. TRANS_DP_BPC_MASK);
  2557. temp |= (TRANS_DP_OUTPUT_ENABLE |
  2558. TRANS_DP_ENH_FRAMING);
  2559. temp |= bpc << 9; /* same format but at 11:9 */
  2560. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  2561. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  2562. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  2563. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  2564. switch (intel_trans_dp_port_sel(crtc)) {
  2565. case PCH_DP_B:
  2566. temp |= TRANS_DP_PORT_SEL_B;
  2567. break;
  2568. case PCH_DP_C:
  2569. temp |= TRANS_DP_PORT_SEL_C;
  2570. break;
  2571. case PCH_DP_D:
  2572. temp |= TRANS_DP_PORT_SEL_D;
  2573. break;
  2574. default:
  2575. DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
  2576. temp |= TRANS_DP_PORT_SEL_B;
  2577. break;
  2578. }
  2579. I915_WRITE(reg, temp);
  2580. }
  2581. intel_enable_transcoder(dev_priv, pipe);
  2582. }
  2583. void intel_cpt_verify_modeset(struct drm_device *dev, int pipe)
  2584. {
  2585. struct drm_i915_private *dev_priv = dev->dev_private;
  2586. int dslreg = PIPEDSL(pipe), tc2reg = TRANS_CHICKEN2(pipe);
  2587. u32 temp;
  2588. temp = I915_READ(dslreg);
  2589. udelay(500);
  2590. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  2591. /* Without this, mode sets may fail silently on FDI */
  2592. I915_WRITE(tc2reg, TRANS_AUTOTRAIN_GEN_STALL_DIS);
  2593. udelay(250);
  2594. I915_WRITE(tc2reg, 0);
  2595. if (wait_for(I915_READ(dslreg) != temp, 5))
  2596. DRM_ERROR("mode set failed: pipe %d stuck\n", pipe);
  2597. }
  2598. }
  2599. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  2600. {
  2601. struct drm_device *dev = crtc->dev;
  2602. struct drm_i915_private *dev_priv = dev->dev_private;
  2603. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2604. int pipe = intel_crtc->pipe;
  2605. int plane = intel_crtc->plane;
  2606. u32 temp;
  2607. bool is_pch_port;
  2608. if (intel_crtc->active)
  2609. return;
  2610. intel_crtc->active = true;
  2611. intel_update_watermarks(dev);
  2612. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  2613. temp = I915_READ(PCH_LVDS);
  2614. if ((temp & LVDS_PORT_EN) == 0)
  2615. I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
  2616. }
  2617. is_pch_port = intel_crtc_driving_pch(crtc);
  2618. if (is_pch_port)
  2619. ironlake_fdi_pll_enable(crtc);
  2620. else
  2621. ironlake_fdi_disable(crtc);
  2622. /* Enable panel fitting for LVDS */
  2623. if (dev_priv->pch_pf_size &&
  2624. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) || HAS_eDP)) {
  2625. /* Force use of hard-coded filter coefficients
  2626. * as some pre-programmed values are broken,
  2627. * e.g. x201.
  2628. */
  2629. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  2630. I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
  2631. I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
  2632. }
  2633. /*
  2634. * On ILK+ LUT must be loaded before the pipe is running but with
  2635. * clocks enabled
  2636. */
  2637. intel_crtc_load_lut(crtc);
  2638. intel_enable_pipe(dev_priv, pipe, is_pch_port);
  2639. intel_enable_plane(dev_priv, plane, pipe);
  2640. if (is_pch_port)
  2641. ironlake_pch_enable(crtc);
  2642. mutex_lock(&dev->struct_mutex);
  2643. intel_update_fbc(dev);
  2644. mutex_unlock(&dev->struct_mutex);
  2645. intel_crtc_update_cursor(crtc, true);
  2646. }
  2647. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  2648. {
  2649. struct drm_device *dev = crtc->dev;
  2650. struct drm_i915_private *dev_priv = dev->dev_private;
  2651. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2652. int pipe = intel_crtc->pipe;
  2653. int plane = intel_crtc->plane;
  2654. u32 reg, temp;
  2655. if (!intel_crtc->active)
  2656. return;
  2657. intel_crtc_wait_for_pending_flips(crtc);
  2658. drm_vblank_off(dev, pipe);
  2659. intel_crtc_update_cursor(crtc, false);
  2660. intel_disable_plane(dev_priv, plane, pipe);
  2661. if (dev_priv->cfb_plane == plane)
  2662. intel_disable_fbc(dev);
  2663. intel_disable_pipe(dev_priv, pipe);
  2664. /* Disable PF */
  2665. I915_WRITE(PF_CTL(pipe), 0);
  2666. I915_WRITE(PF_WIN_SZ(pipe), 0);
  2667. ironlake_fdi_disable(crtc);
  2668. /* This is a horrible layering violation; we should be doing this in
  2669. * the connector/encoder ->prepare instead, but we don't always have
  2670. * enough information there about the config to know whether it will
  2671. * actually be necessary or just cause undesired flicker.
  2672. */
  2673. intel_disable_pch_ports(dev_priv, pipe);
  2674. intel_disable_transcoder(dev_priv, pipe);
  2675. if (HAS_PCH_CPT(dev)) {
  2676. /* disable TRANS_DP_CTL */
  2677. reg = TRANS_DP_CTL(pipe);
  2678. temp = I915_READ(reg);
  2679. temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
  2680. temp |= TRANS_DP_PORT_SEL_NONE;
  2681. I915_WRITE(reg, temp);
  2682. /* disable DPLL_SEL */
  2683. temp = I915_READ(PCH_DPLL_SEL);
  2684. switch (pipe) {
  2685. case 0:
  2686. temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
  2687. break;
  2688. case 1:
  2689. temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  2690. break;
  2691. case 2:
  2692. /* C shares PLL A or B */
  2693. temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
  2694. break;
  2695. default:
  2696. BUG(); /* wtf */
  2697. }
  2698. I915_WRITE(PCH_DPLL_SEL, temp);
  2699. }
  2700. /* disable PCH DPLL */
  2701. if (!intel_crtc->no_pll)
  2702. intel_disable_pch_pll(dev_priv, pipe);
  2703. /* Switch from PCDclk to Rawclk */
  2704. reg = FDI_RX_CTL(pipe);
  2705. temp = I915_READ(reg);
  2706. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2707. /* Disable CPU FDI TX PLL */
  2708. reg = FDI_TX_CTL(pipe);
  2709. temp = I915_READ(reg);
  2710. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2711. POSTING_READ(reg);
  2712. udelay(100);
  2713. reg = FDI_RX_CTL(pipe);
  2714. temp = I915_READ(reg);
  2715. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2716. /* Wait for the clocks to turn off. */
  2717. POSTING_READ(reg);
  2718. udelay(100);
  2719. intel_crtc->active = false;
  2720. intel_update_watermarks(dev);
  2721. mutex_lock(&dev->struct_mutex);
  2722. intel_update_fbc(dev);
  2723. intel_clear_scanline_wait(dev);
  2724. mutex_unlock(&dev->struct_mutex);
  2725. }
  2726. static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
  2727. {
  2728. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2729. int pipe = intel_crtc->pipe;
  2730. int plane = intel_crtc->plane;
  2731. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2732. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2733. */
  2734. switch (mode) {
  2735. case DRM_MODE_DPMS_ON:
  2736. case DRM_MODE_DPMS_STANDBY:
  2737. case DRM_MODE_DPMS_SUSPEND:
  2738. DRM_DEBUG_KMS("crtc %d/%d dpms on\n", pipe, plane);
  2739. ironlake_crtc_enable(crtc);
  2740. break;
  2741. case DRM_MODE_DPMS_OFF:
  2742. DRM_DEBUG_KMS("crtc %d/%d dpms off\n", pipe, plane);
  2743. ironlake_crtc_disable(crtc);
  2744. break;
  2745. }
  2746. }
  2747. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  2748. {
  2749. if (!enable && intel_crtc->overlay) {
  2750. struct drm_device *dev = intel_crtc->base.dev;
  2751. struct drm_i915_private *dev_priv = dev->dev_private;
  2752. mutex_lock(&dev->struct_mutex);
  2753. dev_priv->mm.interruptible = false;
  2754. (void) intel_overlay_switch_off(intel_crtc->overlay);
  2755. dev_priv->mm.interruptible = true;
  2756. mutex_unlock(&dev->struct_mutex);
  2757. }
  2758. /* Let userspace switch the overlay on again. In most cases userspace
  2759. * has to recompute where to put it anyway.
  2760. */
  2761. }
  2762. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  2763. {
  2764. struct drm_device *dev = crtc->dev;
  2765. struct drm_i915_private *dev_priv = dev->dev_private;
  2766. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2767. int pipe = intel_crtc->pipe;
  2768. int plane = intel_crtc->plane;
  2769. if (intel_crtc->active)
  2770. return;
  2771. intel_crtc->active = true;
  2772. intel_update_watermarks(dev);
  2773. intel_enable_pll(dev_priv, pipe);
  2774. intel_enable_pipe(dev_priv, pipe, false);
  2775. intel_enable_plane(dev_priv, plane, pipe);
  2776. intel_crtc_load_lut(crtc);
  2777. intel_update_fbc(dev);
  2778. /* Give the overlay scaler a chance to enable if it's on this pipe */
  2779. intel_crtc_dpms_overlay(intel_crtc, true);
  2780. intel_crtc_update_cursor(crtc, true);
  2781. }
  2782. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  2783. {
  2784. struct drm_device *dev = crtc->dev;
  2785. struct drm_i915_private *dev_priv = dev->dev_private;
  2786. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2787. int pipe = intel_crtc->pipe;
  2788. int plane = intel_crtc->plane;
  2789. if (!intel_crtc->active)
  2790. return;
  2791. /* Give the overlay scaler a chance to disable if it's on this pipe */
  2792. intel_crtc_wait_for_pending_flips(crtc);
  2793. drm_vblank_off(dev, pipe);
  2794. intel_crtc_dpms_overlay(intel_crtc, false);
  2795. intel_crtc_update_cursor(crtc, false);
  2796. if (dev_priv->cfb_plane == plane)
  2797. intel_disable_fbc(dev);
  2798. intel_disable_plane(dev_priv, plane, pipe);
  2799. intel_disable_pipe(dev_priv, pipe);
  2800. intel_disable_pll(dev_priv, pipe);
  2801. intel_crtc->active = false;
  2802. intel_update_fbc(dev);
  2803. intel_update_watermarks(dev);
  2804. intel_clear_scanline_wait(dev);
  2805. }
  2806. static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
  2807. {
  2808. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2809. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2810. */
  2811. switch (mode) {
  2812. case DRM_MODE_DPMS_ON:
  2813. case DRM_MODE_DPMS_STANDBY:
  2814. case DRM_MODE_DPMS_SUSPEND:
  2815. i9xx_crtc_enable(crtc);
  2816. break;
  2817. case DRM_MODE_DPMS_OFF:
  2818. i9xx_crtc_disable(crtc);
  2819. break;
  2820. }
  2821. }
  2822. /**
  2823. * Sets the power management mode of the pipe and plane.
  2824. */
  2825. static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
  2826. {
  2827. struct drm_device *dev = crtc->dev;
  2828. struct drm_i915_private *dev_priv = dev->dev_private;
  2829. struct drm_i915_master_private *master_priv;
  2830. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2831. int pipe = intel_crtc->pipe;
  2832. bool enabled;
  2833. if (intel_crtc->dpms_mode == mode)
  2834. return;
  2835. intel_crtc->dpms_mode = mode;
  2836. dev_priv->display.dpms(crtc, mode);
  2837. if (!dev->primary->master)
  2838. return;
  2839. master_priv = dev->primary->master->driver_priv;
  2840. if (!master_priv->sarea_priv)
  2841. return;
  2842. enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
  2843. switch (pipe) {
  2844. case 0:
  2845. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  2846. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  2847. break;
  2848. case 1:
  2849. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  2850. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  2851. break;
  2852. default:
  2853. DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
  2854. break;
  2855. }
  2856. }
  2857. static void intel_crtc_disable(struct drm_crtc *crtc)
  2858. {
  2859. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  2860. struct drm_device *dev = crtc->dev;
  2861. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  2862. if (crtc->fb) {
  2863. mutex_lock(&dev->struct_mutex);
  2864. i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
  2865. mutex_unlock(&dev->struct_mutex);
  2866. }
  2867. }
  2868. /* Prepare for a mode set.
  2869. *
  2870. * Note we could be a lot smarter here. We need to figure out which outputs
  2871. * will be enabled, which disabled (in short, how the config will changes)
  2872. * and perform the minimum necessary steps to accomplish that, e.g. updating
  2873. * watermarks, FBC configuration, making sure PLLs are programmed correctly,
  2874. * panel fitting is in the proper state, etc.
  2875. */
  2876. static void i9xx_crtc_prepare(struct drm_crtc *crtc)
  2877. {
  2878. i9xx_crtc_disable(crtc);
  2879. }
  2880. static void i9xx_crtc_commit(struct drm_crtc *crtc)
  2881. {
  2882. i9xx_crtc_enable(crtc);
  2883. }
  2884. static void ironlake_crtc_prepare(struct drm_crtc *crtc)
  2885. {
  2886. ironlake_crtc_disable(crtc);
  2887. }
  2888. static void ironlake_crtc_commit(struct drm_crtc *crtc)
  2889. {
  2890. ironlake_crtc_enable(crtc);
  2891. }
  2892. void intel_encoder_prepare(struct drm_encoder *encoder)
  2893. {
  2894. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2895. /* lvds has its own version of prepare see intel_lvds_prepare */
  2896. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  2897. }
  2898. void intel_encoder_commit(struct drm_encoder *encoder)
  2899. {
  2900. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2901. struct drm_device *dev = encoder->dev;
  2902. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  2903. struct intel_crtc *intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  2904. /* lvds has its own version of commit see intel_lvds_commit */
  2905. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  2906. if (HAS_PCH_CPT(dev))
  2907. intel_cpt_verify_modeset(dev, intel_crtc->pipe);
  2908. }
  2909. void intel_encoder_destroy(struct drm_encoder *encoder)
  2910. {
  2911. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  2912. drm_encoder_cleanup(encoder);
  2913. kfree(intel_encoder);
  2914. }
  2915. static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
  2916. struct drm_display_mode *mode,
  2917. struct drm_display_mode *adjusted_mode)
  2918. {
  2919. struct drm_device *dev = crtc->dev;
  2920. if (HAS_PCH_SPLIT(dev)) {
  2921. /* FDI link clock is fixed at 2.7G */
  2922. if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
  2923. return false;
  2924. }
  2925. /* XXX some encoders set the crtcinfo, others don't.
  2926. * Obviously we need some form of conflict resolution here...
  2927. */
  2928. if (adjusted_mode->crtc_htotal == 0)
  2929. drm_mode_set_crtcinfo(adjusted_mode, 0);
  2930. return true;
  2931. }
  2932. static int i945_get_display_clock_speed(struct drm_device *dev)
  2933. {
  2934. return 400000;
  2935. }
  2936. static int i915_get_display_clock_speed(struct drm_device *dev)
  2937. {
  2938. return 333000;
  2939. }
  2940. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  2941. {
  2942. return 200000;
  2943. }
  2944. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  2945. {
  2946. u16 gcfgc = 0;
  2947. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  2948. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  2949. return 133000;
  2950. else {
  2951. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  2952. case GC_DISPLAY_CLOCK_333_MHZ:
  2953. return 333000;
  2954. default:
  2955. case GC_DISPLAY_CLOCK_190_200_MHZ:
  2956. return 190000;
  2957. }
  2958. }
  2959. }
  2960. static int i865_get_display_clock_speed(struct drm_device *dev)
  2961. {
  2962. return 266000;
  2963. }
  2964. static int i855_get_display_clock_speed(struct drm_device *dev)
  2965. {
  2966. u16 hpllcc = 0;
  2967. /* Assume that the hardware is in the high speed state. This
  2968. * should be the default.
  2969. */
  2970. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  2971. case GC_CLOCK_133_200:
  2972. case GC_CLOCK_100_200:
  2973. return 200000;
  2974. case GC_CLOCK_166_250:
  2975. return 250000;
  2976. case GC_CLOCK_100_133:
  2977. return 133000;
  2978. }
  2979. /* Shouldn't happen */
  2980. return 0;
  2981. }
  2982. static int i830_get_display_clock_speed(struct drm_device *dev)
  2983. {
  2984. return 133000;
  2985. }
  2986. struct fdi_m_n {
  2987. u32 tu;
  2988. u32 gmch_m;
  2989. u32 gmch_n;
  2990. u32 link_m;
  2991. u32 link_n;
  2992. };
  2993. static void
  2994. fdi_reduce_ratio(u32 *num, u32 *den)
  2995. {
  2996. while (*num > 0xffffff || *den > 0xffffff) {
  2997. *num >>= 1;
  2998. *den >>= 1;
  2999. }
  3000. }
  3001. static void
  3002. ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
  3003. int link_clock, struct fdi_m_n *m_n)
  3004. {
  3005. m_n->tu = 64; /* default size */
  3006. /* BUG_ON(pixel_clock > INT_MAX / 36); */
  3007. m_n->gmch_m = bits_per_pixel * pixel_clock;
  3008. m_n->gmch_n = link_clock * nlanes * 8;
  3009. fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
  3010. m_n->link_m = pixel_clock;
  3011. m_n->link_n = link_clock;
  3012. fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
  3013. }
  3014. struct intel_watermark_params {
  3015. unsigned long fifo_size;
  3016. unsigned long max_wm;
  3017. unsigned long default_wm;
  3018. unsigned long guard_size;
  3019. unsigned long cacheline_size;
  3020. };
  3021. /* Pineview has different values for various configs */
  3022. static const struct intel_watermark_params pineview_display_wm = {
  3023. PINEVIEW_DISPLAY_FIFO,
  3024. PINEVIEW_MAX_WM,
  3025. PINEVIEW_DFT_WM,
  3026. PINEVIEW_GUARD_WM,
  3027. PINEVIEW_FIFO_LINE_SIZE
  3028. };
  3029. static const struct intel_watermark_params pineview_display_hplloff_wm = {
  3030. PINEVIEW_DISPLAY_FIFO,
  3031. PINEVIEW_MAX_WM,
  3032. PINEVIEW_DFT_HPLLOFF_WM,
  3033. PINEVIEW_GUARD_WM,
  3034. PINEVIEW_FIFO_LINE_SIZE
  3035. };
  3036. static const struct intel_watermark_params pineview_cursor_wm = {
  3037. PINEVIEW_CURSOR_FIFO,
  3038. PINEVIEW_CURSOR_MAX_WM,
  3039. PINEVIEW_CURSOR_DFT_WM,
  3040. PINEVIEW_CURSOR_GUARD_WM,
  3041. PINEVIEW_FIFO_LINE_SIZE,
  3042. };
  3043. static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
  3044. PINEVIEW_CURSOR_FIFO,
  3045. PINEVIEW_CURSOR_MAX_WM,
  3046. PINEVIEW_CURSOR_DFT_WM,
  3047. PINEVIEW_CURSOR_GUARD_WM,
  3048. PINEVIEW_FIFO_LINE_SIZE
  3049. };
  3050. static const struct intel_watermark_params g4x_wm_info = {
  3051. G4X_FIFO_SIZE,
  3052. G4X_MAX_WM,
  3053. G4X_MAX_WM,
  3054. 2,
  3055. G4X_FIFO_LINE_SIZE,
  3056. };
  3057. static const struct intel_watermark_params g4x_cursor_wm_info = {
  3058. I965_CURSOR_FIFO,
  3059. I965_CURSOR_MAX_WM,
  3060. I965_CURSOR_DFT_WM,
  3061. 2,
  3062. G4X_FIFO_LINE_SIZE,
  3063. };
  3064. static const struct intel_watermark_params i965_cursor_wm_info = {
  3065. I965_CURSOR_FIFO,
  3066. I965_CURSOR_MAX_WM,
  3067. I965_CURSOR_DFT_WM,
  3068. 2,
  3069. I915_FIFO_LINE_SIZE,
  3070. };
  3071. static const struct intel_watermark_params i945_wm_info = {
  3072. I945_FIFO_SIZE,
  3073. I915_MAX_WM,
  3074. 1,
  3075. 2,
  3076. I915_FIFO_LINE_SIZE
  3077. };
  3078. static const struct intel_watermark_params i915_wm_info = {
  3079. I915_FIFO_SIZE,
  3080. I915_MAX_WM,
  3081. 1,
  3082. 2,
  3083. I915_FIFO_LINE_SIZE
  3084. };
  3085. static const struct intel_watermark_params i855_wm_info = {
  3086. I855GM_FIFO_SIZE,
  3087. I915_MAX_WM,
  3088. 1,
  3089. 2,
  3090. I830_FIFO_LINE_SIZE
  3091. };
  3092. static const struct intel_watermark_params i830_wm_info = {
  3093. I830_FIFO_SIZE,
  3094. I915_MAX_WM,
  3095. 1,
  3096. 2,
  3097. I830_FIFO_LINE_SIZE
  3098. };
  3099. static const struct intel_watermark_params ironlake_display_wm_info = {
  3100. ILK_DISPLAY_FIFO,
  3101. ILK_DISPLAY_MAXWM,
  3102. ILK_DISPLAY_DFTWM,
  3103. 2,
  3104. ILK_FIFO_LINE_SIZE
  3105. };
  3106. static const struct intel_watermark_params ironlake_cursor_wm_info = {
  3107. ILK_CURSOR_FIFO,
  3108. ILK_CURSOR_MAXWM,
  3109. ILK_CURSOR_DFTWM,
  3110. 2,
  3111. ILK_FIFO_LINE_SIZE
  3112. };
  3113. static const struct intel_watermark_params ironlake_display_srwm_info = {
  3114. ILK_DISPLAY_SR_FIFO,
  3115. ILK_DISPLAY_MAX_SRWM,
  3116. ILK_DISPLAY_DFT_SRWM,
  3117. 2,
  3118. ILK_FIFO_LINE_SIZE
  3119. };
  3120. static const struct intel_watermark_params ironlake_cursor_srwm_info = {
  3121. ILK_CURSOR_SR_FIFO,
  3122. ILK_CURSOR_MAX_SRWM,
  3123. ILK_CURSOR_DFT_SRWM,
  3124. 2,
  3125. ILK_FIFO_LINE_SIZE
  3126. };
  3127. static const struct intel_watermark_params sandybridge_display_wm_info = {
  3128. SNB_DISPLAY_FIFO,
  3129. SNB_DISPLAY_MAXWM,
  3130. SNB_DISPLAY_DFTWM,
  3131. 2,
  3132. SNB_FIFO_LINE_SIZE
  3133. };
  3134. static const struct intel_watermark_params sandybridge_cursor_wm_info = {
  3135. SNB_CURSOR_FIFO,
  3136. SNB_CURSOR_MAXWM,
  3137. SNB_CURSOR_DFTWM,
  3138. 2,
  3139. SNB_FIFO_LINE_SIZE
  3140. };
  3141. static const struct intel_watermark_params sandybridge_display_srwm_info = {
  3142. SNB_DISPLAY_SR_FIFO,
  3143. SNB_DISPLAY_MAX_SRWM,
  3144. SNB_DISPLAY_DFT_SRWM,
  3145. 2,
  3146. SNB_FIFO_LINE_SIZE
  3147. };
  3148. static const struct intel_watermark_params sandybridge_cursor_srwm_info = {
  3149. SNB_CURSOR_SR_FIFO,
  3150. SNB_CURSOR_MAX_SRWM,
  3151. SNB_CURSOR_DFT_SRWM,
  3152. 2,
  3153. SNB_FIFO_LINE_SIZE
  3154. };
  3155. /**
  3156. * intel_calculate_wm - calculate watermark level
  3157. * @clock_in_khz: pixel clock
  3158. * @wm: chip FIFO params
  3159. * @pixel_size: display pixel size
  3160. * @latency_ns: memory latency for the platform
  3161. *
  3162. * Calculate the watermark level (the level at which the display plane will
  3163. * start fetching from memory again). Each chip has a different display
  3164. * FIFO size and allocation, so the caller needs to figure that out and pass
  3165. * in the correct intel_watermark_params structure.
  3166. *
  3167. * As the pixel clock runs, the FIFO will be drained at a rate that depends
  3168. * on the pixel size. When it reaches the watermark level, it'll start
  3169. * fetching FIFO line sized based chunks from memory until the FIFO fills
  3170. * past the watermark point. If the FIFO drains completely, a FIFO underrun
  3171. * will occur, and a display engine hang could result.
  3172. */
  3173. static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
  3174. const struct intel_watermark_params *wm,
  3175. int fifo_size,
  3176. int pixel_size,
  3177. unsigned long latency_ns)
  3178. {
  3179. long entries_required, wm_size;
  3180. /*
  3181. * Note: we need to make sure we don't overflow for various clock &
  3182. * latency values.
  3183. * clocks go from a few thousand to several hundred thousand.
  3184. * latency is usually a few thousand
  3185. */
  3186. entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
  3187. 1000;
  3188. entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
  3189. DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
  3190. wm_size = fifo_size - (entries_required + wm->guard_size);
  3191. DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
  3192. /* Don't promote wm_size to unsigned... */
  3193. if (wm_size > (long)wm->max_wm)
  3194. wm_size = wm->max_wm;
  3195. if (wm_size <= 0)
  3196. wm_size = wm->default_wm;
  3197. return wm_size;
  3198. }
  3199. struct cxsr_latency {
  3200. int is_desktop;
  3201. int is_ddr3;
  3202. unsigned long fsb_freq;
  3203. unsigned long mem_freq;
  3204. unsigned long display_sr;
  3205. unsigned long display_hpll_disable;
  3206. unsigned long cursor_sr;
  3207. unsigned long cursor_hpll_disable;
  3208. };
  3209. static const struct cxsr_latency cxsr_latency_table[] = {
  3210. {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
  3211. {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
  3212. {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
  3213. {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
  3214. {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
  3215. {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
  3216. {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
  3217. {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
  3218. {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
  3219. {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
  3220. {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
  3221. {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
  3222. {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
  3223. {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
  3224. {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
  3225. {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
  3226. {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
  3227. {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
  3228. {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
  3229. {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
  3230. {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
  3231. {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
  3232. {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
  3233. {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
  3234. {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
  3235. {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
  3236. {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
  3237. {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
  3238. {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
  3239. {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
  3240. };
  3241. static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
  3242. int is_ddr3,
  3243. int fsb,
  3244. int mem)
  3245. {
  3246. const struct cxsr_latency *latency;
  3247. int i;
  3248. if (fsb == 0 || mem == 0)
  3249. return NULL;
  3250. for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
  3251. latency = &cxsr_latency_table[i];
  3252. if (is_desktop == latency->is_desktop &&
  3253. is_ddr3 == latency->is_ddr3 &&
  3254. fsb == latency->fsb_freq && mem == latency->mem_freq)
  3255. return latency;
  3256. }
  3257. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  3258. return NULL;
  3259. }
  3260. static void pineview_disable_cxsr(struct drm_device *dev)
  3261. {
  3262. struct drm_i915_private *dev_priv = dev->dev_private;
  3263. /* deactivate cxsr */
  3264. I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
  3265. }
  3266. /*
  3267. * Latency for FIFO fetches is dependent on several factors:
  3268. * - memory configuration (speed, channels)
  3269. * - chipset
  3270. * - current MCH state
  3271. * It can be fairly high in some situations, so here we assume a fairly
  3272. * pessimal value. It's a tradeoff between extra memory fetches (if we
  3273. * set this value too high, the FIFO will fetch frequently to stay full)
  3274. * and power consumption (set it too low to save power and we might see
  3275. * FIFO underruns and display "flicker").
  3276. *
  3277. * A value of 5us seems to be a good balance; safe for very low end
  3278. * platforms but not overly aggressive on lower latency configs.
  3279. */
  3280. static const int latency_ns = 5000;
  3281. static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
  3282. {
  3283. struct drm_i915_private *dev_priv = dev->dev_private;
  3284. uint32_t dsparb = I915_READ(DSPARB);
  3285. int size;
  3286. size = dsparb & 0x7f;
  3287. if (plane)
  3288. size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
  3289. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  3290. plane ? "B" : "A", size);
  3291. return size;
  3292. }
  3293. static int i85x_get_fifo_size(struct drm_device *dev, int plane)
  3294. {
  3295. struct drm_i915_private *dev_priv = dev->dev_private;
  3296. uint32_t dsparb = I915_READ(DSPARB);
  3297. int size;
  3298. size = dsparb & 0x1ff;
  3299. if (plane)
  3300. size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
  3301. size >>= 1; /* Convert to cachelines */
  3302. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  3303. plane ? "B" : "A", size);
  3304. return size;
  3305. }
  3306. static int i845_get_fifo_size(struct drm_device *dev, int plane)
  3307. {
  3308. struct drm_i915_private *dev_priv = dev->dev_private;
  3309. uint32_t dsparb = I915_READ(DSPARB);
  3310. int size;
  3311. size = dsparb & 0x7f;
  3312. size >>= 2; /* Convert to cachelines */
  3313. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  3314. plane ? "B" : "A",
  3315. size);
  3316. return size;
  3317. }
  3318. static int i830_get_fifo_size(struct drm_device *dev, int plane)
  3319. {
  3320. struct drm_i915_private *dev_priv = dev->dev_private;
  3321. uint32_t dsparb = I915_READ(DSPARB);
  3322. int size;
  3323. size = dsparb & 0x7f;
  3324. size >>= 1; /* Convert to cachelines */
  3325. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  3326. plane ? "B" : "A", size);
  3327. return size;
  3328. }
  3329. static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
  3330. {
  3331. struct drm_crtc *crtc, *enabled = NULL;
  3332. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  3333. if (crtc->enabled && crtc->fb) {
  3334. if (enabled)
  3335. return NULL;
  3336. enabled = crtc;
  3337. }
  3338. }
  3339. return enabled;
  3340. }
  3341. static void pineview_update_wm(struct drm_device *dev)
  3342. {
  3343. struct drm_i915_private *dev_priv = dev->dev_private;
  3344. struct drm_crtc *crtc;
  3345. const struct cxsr_latency *latency;
  3346. u32 reg;
  3347. unsigned long wm;
  3348. latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
  3349. dev_priv->fsb_freq, dev_priv->mem_freq);
  3350. if (!latency) {
  3351. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  3352. pineview_disable_cxsr(dev);
  3353. return;
  3354. }
  3355. crtc = single_enabled_crtc(dev);
  3356. if (crtc) {
  3357. int clock = crtc->mode.clock;
  3358. int pixel_size = crtc->fb->bits_per_pixel / 8;
  3359. /* Display SR */
  3360. wm = intel_calculate_wm(clock, &pineview_display_wm,
  3361. pineview_display_wm.fifo_size,
  3362. pixel_size, latency->display_sr);
  3363. reg = I915_READ(DSPFW1);
  3364. reg &= ~DSPFW_SR_MASK;
  3365. reg |= wm << DSPFW_SR_SHIFT;
  3366. I915_WRITE(DSPFW1, reg);
  3367. DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
  3368. /* cursor SR */
  3369. wm = intel_calculate_wm(clock, &pineview_cursor_wm,
  3370. pineview_display_wm.fifo_size,
  3371. pixel_size, latency->cursor_sr);
  3372. reg = I915_READ(DSPFW3);
  3373. reg &= ~DSPFW_CURSOR_SR_MASK;
  3374. reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
  3375. I915_WRITE(DSPFW3, reg);
  3376. /* Display HPLL off SR */
  3377. wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
  3378. pineview_display_hplloff_wm.fifo_size,
  3379. pixel_size, latency->display_hpll_disable);
  3380. reg = I915_READ(DSPFW3);
  3381. reg &= ~DSPFW_HPLL_SR_MASK;
  3382. reg |= wm & DSPFW_HPLL_SR_MASK;
  3383. I915_WRITE(DSPFW3, reg);
  3384. /* cursor HPLL off SR */
  3385. wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
  3386. pineview_display_hplloff_wm.fifo_size,
  3387. pixel_size, latency->cursor_hpll_disable);
  3388. reg = I915_READ(DSPFW3);
  3389. reg &= ~DSPFW_HPLL_CURSOR_MASK;
  3390. reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
  3391. I915_WRITE(DSPFW3, reg);
  3392. DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
  3393. /* activate cxsr */
  3394. I915_WRITE(DSPFW3,
  3395. I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
  3396. DRM_DEBUG_KMS("Self-refresh is enabled\n");
  3397. } else {
  3398. pineview_disable_cxsr(dev);
  3399. DRM_DEBUG_KMS("Self-refresh is disabled\n");
  3400. }
  3401. }
  3402. static bool g4x_compute_wm0(struct drm_device *dev,
  3403. int plane,
  3404. const struct intel_watermark_params *display,
  3405. int display_latency_ns,
  3406. const struct intel_watermark_params *cursor,
  3407. int cursor_latency_ns,
  3408. int *plane_wm,
  3409. int *cursor_wm)
  3410. {
  3411. struct drm_crtc *crtc;
  3412. int htotal, hdisplay, clock, pixel_size;
  3413. int line_time_us, line_count;
  3414. int entries, tlb_miss;
  3415. crtc = intel_get_crtc_for_plane(dev, plane);
  3416. if (crtc->fb == NULL || !crtc->enabled) {
  3417. *cursor_wm = cursor->guard_size;
  3418. *plane_wm = display->guard_size;
  3419. return false;
  3420. }
  3421. htotal = crtc->mode.htotal;
  3422. hdisplay = crtc->mode.hdisplay;
  3423. clock = crtc->mode.clock;
  3424. pixel_size = crtc->fb->bits_per_pixel / 8;
  3425. /* Use the small buffer method to calculate plane watermark */
  3426. entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
  3427. tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
  3428. if (tlb_miss > 0)
  3429. entries += tlb_miss;
  3430. entries = DIV_ROUND_UP(entries, display->cacheline_size);
  3431. *plane_wm = entries + display->guard_size;
  3432. if (*plane_wm > (int)display->max_wm)
  3433. *plane_wm = display->max_wm;
  3434. /* Use the large buffer method to calculate cursor watermark */
  3435. line_time_us = ((htotal * 1000) / clock);
  3436. line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
  3437. entries = line_count * 64 * pixel_size;
  3438. tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
  3439. if (tlb_miss > 0)
  3440. entries += tlb_miss;
  3441. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  3442. *cursor_wm = entries + cursor->guard_size;
  3443. if (*cursor_wm > (int)cursor->max_wm)
  3444. *cursor_wm = (int)cursor->max_wm;
  3445. return true;
  3446. }
  3447. /*
  3448. * Check the wm result.
  3449. *
  3450. * If any calculated watermark values is larger than the maximum value that
  3451. * can be programmed into the associated watermark register, that watermark
  3452. * must be disabled.
  3453. */
  3454. static bool g4x_check_srwm(struct drm_device *dev,
  3455. int display_wm, int cursor_wm,
  3456. const struct intel_watermark_params *display,
  3457. const struct intel_watermark_params *cursor)
  3458. {
  3459. DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
  3460. display_wm, cursor_wm);
  3461. if (display_wm > display->max_wm) {
  3462. DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
  3463. display_wm, display->max_wm);
  3464. return false;
  3465. }
  3466. if (cursor_wm > cursor->max_wm) {
  3467. DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
  3468. cursor_wm, cursor->max_wm);
  3469. return false;
  3470. }
  3471. if (!(display_wm || cursor_wm)) {
  3472. DRM_DEBUG_KMS("SR latency is 0, disabling\n");
  3473. return false;
  3474. }
  3475. return true;
  3476. }
  3477. static bool g4x_compute_srwm(struct drm_device *dev,
  3478. int plane,
  3479. int latency_ns,
  3480. const struct intel_watermark_params *display,
  3481. const struct intel_watermark_params *cursor,
  3482. int *display_wm, int *cursor_wm)
  3483. {
  3484. struct drm_crtc *crtc;
  3485. int hdisplay, htotal, pixel_size, clock;
  3486. unsigned long line_time_us;
  3487. int line_count, line_size;
  3488. int small, large;
  3489. int entries;
  3490. if (!latency_ns) {
  3491. *display_wm = *cursor_wm = 0;
  3492. return false;
  3493. }
  3494. crtc = intel_get_crtc_for_plane(dev, plane);
  3495. hdisplay = crtc->mode.hdisplay;
  3496. htotal = crtc->mode.htotal;
  3497. clock = crtc->mode.clock;
  3498. pixel_size = crtc->fb->bits_per_pixel / 8;
  3499. line_time_us = (htotal * 1000) / clock;
  3500. line_count = (latency_ns / line_time_us + 1000) / 1000;
  3501. line_size = hdisplay * pixel_size;
  3502. /* Use the minimum of the small and large buffer method for primary */
  3503. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  3504. large = line_count * line_size;
  3505. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  3506. *display_wm = entries + display->guard_size;
  3507. /* calculate the self-refresh watermark for display cursor */
  3508. entries = line_count * pixel_size * 64;
  3509. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  3510. *cursor_wm = entries + cursor->guard_size;
  3511. return g4x_check_srwm(dev,
  3512. *display_wm, *cursor_wm,
  3513. display, cursor);
  3514. }
  3515. #define single_plane_enabled(mask) is_power_of_2(mask)
  3516. static void g4x_update_wm(struct drm_device *dev)
  3517. {
  3518. static const int sr_latency_ns = 12000;
  3519. struct drm_i915_private *dev_priv = dev->dev_private;
  3520. int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
  3521. int plane_sr, cursor_sr;
  3522. unsigned int enabled = 0;
  3523. if (g4x_compute_wm0(dev, 0,
  3524. &g4x_wm_info, latency_ns,
  3525. &g4x_cursor_wm_info, latency_ns,
  3526. &planea_wm, &cursora_wm))
  3527. enabled |= 1;
  3528. if (g4x_compute_wm0(dev, 1,
  3529. &g4x_wm_info, latency_ns,
  3530. &g4x_cursor_wm_info, latency_ns,
  3531. &planeb_wm, &cursorb_wm))
  3532. enabled |= 2;
  3533. plane_sr = cursor_sr = 0;
  3534. if (single_plane_enabled(enabled) &&
  3535. g4x_compute_srwm(dev, ffs(enabled) - 1,
  3536. sr_latency_ns,
  3537. &g4x_wm_info,
  3538. &g4x_cursor_wm_info,
  3539. &plane_sr, &cursor_sr))
  3540. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  3541. else
  3542. I915_WRITE(FW_BLC_SELF,
  3543. I915_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN);
  3544. DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
  3545. planea_wm, cursora_wm,
  3546. planeb_wm, cursorb_wm,
  3547. plane_sr, cursor_sr);
  3548. I915_WRITE(DSPFW1,
  3549. (plane_sr << DSPFW_SR_SHIFT) |
  3550. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  3551. (planeb_wm << DSPFW_PLANEB_SHIFT) |
  3552. planea_wm);
  3553. I915_WRITE(DSPFW2,
  3554. (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
  3555. (cursora_wm << DSPFW_CURSORA_SHIFT));
  3556. /* HPLL off in SR has some issues on G4x... disable it */
  3557. I915_WRITE(DSPFW3,
  3558. (I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |
  3559. (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  3560. }
  3561. static void i965_update_wm(struct drm_device *dev)
  3562. {
  3563. struct drm_i915_private *dev_priv = dev->dev_private;
  3564. struct drm_crtc *crtc;
  3565. int srwm = 1;
  3566. int cursor_sr = 16;
  3567. /* Calc sr entries for one plane configs */
  3568. crtc = single_enabled_crtc(dev);
  3569. if (crtc) {
  3570. /* self-refresh has much higher latency */
  3571. static const int sr_latency_ns = 12000;
  3572. int clock = crtc->mode.clock;
  3573. int htotal = crtc->mode.htotal;
  3574. int hdisplay = crtc->mode.hdisplay;
  3575. int pixel_size = crtc->fb->bits_per_pixel / 8;
  3576. unsigned long line_time_us;
  3577. int entries;
  3578. line_time_us = ((htotal * 1000) / clock);
  3579. /* Use ns/us then divide to preserve precision */
  3580. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  3581. pixel_size * hdisplay;
  3582. entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
  3583. srwm = I965_FIFO_SIZE - entries;
  3584. if (srwm < 0)
  3585. srwm = 1;
  3586. srwm &= 0x1ff;
  3587. DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
  3588. entries, srwm);
  3589. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  3590. pixel_size * 64;
  3591. entries = DIV_ROUND_UP(entries,
  3592. i965_cursor_wm_info.cacheline_size);
  3593. cursor_sr = i965_cursor_wm_info.fifo_size -
  3594. (entries + i965_cursor_wm_info.guard_size);
  3595. if (cursor_sr > i965_cursor_wm_info.max_wm)
  3596. cursor_sr = i965_cursor_wm_info.max_wm;
  3597. DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
  3598. "cursor %d\n", srwm, cursor_sr);
  3599. if (IS_CRESTLINE(dev))
  3600. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  3601. } else {
  3602. /* Turn off self refresh if both pipes are enabled */
  3603. if (IS_CRESTLINE(dev))
  3604. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  3605. & ~FW_BLC_SELF_EN);
  3606. }
  3607. DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
  3608. srwm);
  3609. /* 965 has limitations... */
  3610. I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
  3611. (8 << 16) | (8 << 8) | (8 << 0));
  3612. I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
  3613. /* update cursor SR watermark */
  3614. I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  3615. }
  3616. static void i9xx_update_wm(struct drm_device *dev)
  3617. {
  3618. struct drm_i915_private *dev_priv = dev->dev_private;
  3619. const struct intel_watermark_params *wm_info;
  3620. uint32_t fwater_lo;
  3621. uint32_t fwater_hi;
  3622. int cwm, srwm = 1;
  3623. int fifo_size;
  3624. int planea_wm, planeb_wm;
  3625. struct drm_crtc *crtc, *enabled = NULL;
  3626. if (IS_I945GM(dev))
  3627. wm_info = &i945_wm_info;
  3628. else if (!IS_GEN2(dev))
  3629. wm_info = &i915_wm_info;
  3630. else
  3631. wm_info = &i855_wm_info;
  3632. fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  3633. crtc = intel_get_crtc_for_plane(dev, 0);
  3634. if (crtc->enabled && crtc->fb) {
  3635. planea_wm = intel_calculate_wm(crtc->mode.clock,
  3636. wm_info, fifo_size,
  3637. crtc->fb->bits_per_pixel / 8,
  3638. latency_ns);
  3639. enabled = crtc;
  3640. } else
  3641. planea_wm = fifo_size - wm_info->guard_size;
  3642. fifo_size = dev_priv->display.get_fifo_size(dev, 1);
  3643. crtc = intel_get_crtc_for_plane(dev, 1);
  3644. if (crtc->enabled && crtc->fb) {
  3645. planeb_wm = intel_calculate_wm(crtc->mode.clock,
  3646. wm_info, fifo_size,
  3647. crtc->fb->bits_per_pixel / 8,
  3648. latency_ns);
  3649. if (enabled == NULL)
  3650. enabled = crtc;
  3651. else
  3652. enabled = NULL;
  3653. } else
  3654. planeb_wm = fifo_size - wm_info->guard_size;
  3655. DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  3656. /*
  3657. * Overlay gets an aggressive default since video jitter is bad.
  3658. */
  3659. cwm = 2;
  3660. /* Play safe and disable self-refresh before adjusting watermarks. */
  3661. if (IS_I945G(dev) || IS_I945GM(dev))
  3662. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | 0);
  3663. else if (IS_I915GM(dev))
  3664. I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
  3665. /* Calc sr entries for one plane configs */
  3666. if (HAS_FW_BLC(dev) && enabled) {
  3667. /* self-refresh has much higher latency */
  3668. static const int sr_latency_ns = 6000;
  3669. int clock = enabled->mode.clock;
  3670. int htotal = enabled->mode.htotal;
  3671. int hdisplay = enabled->mode.hdisplay;
  3672. int pixel_size = enabled->fb->bits_per_pixel / 8;
  3673. unsigned long line_time_us;
  3674. int entries;
  3675. line_time_us = (htotal * 1000) / clock;
  3676. /* Use ns/us then divide to preserve precision */
  3677. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  3678. pixel_size * hdisplay;
  3679. entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
  3680. DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
  3681. srwm = wm_info->fifo_size - entries;
  3682. if (srwm < 0)
  3683. srwm = 1;
  3684. if (IS_I945G(dev) || IS_I945GM(dev))
  3685. I915_WRITE(FW_BLC_SELF,
  3686. FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
  3687. else if (IS_I915GM(dev))
  3688. I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
  3689. }
  3690. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
  3691. planea_wm, planeb_wm, cwm, srwm);
  3692. fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
  3693. fwater_hi = (cwm & 0x1f);
  3694. /* Set request length to 8 cachelines per fetch */
  3695. fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
  3696. fwater_hi = fwater_hi | (1 << 8);
  3697. I915_WRITE(FW_BLC, fwater_lo);
  3698. I915_WRITE(FW_BLC2, fwater_hi);
  3699. if (HAS_FW_BLC(dev)) {
  3700. if (enabled) {
  3701. if (IS_I945G(dev) || IS_I945GM(dev))
  3702. I915_WRITE(FW_BLC_SELF,
  3703. FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
  3704. else if (IS_I915GM(dev))
  3705. I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
  3706. DRM_DEBUG_KMS("memory self refresh enabled\n");
  3707. } else
  3708. DRM_DEBUG_KMS("memory self refresh disabled\n");
  3709. }
  3710. }
  3711. static void i830_update_wm(struct drm_device *dev)
  3712. {
  3713. struct drm_i915_private *dev_priv = dev->dev_private;
  3714. struct drm_crtc *crtc;
  3715. uint32_t fwater_lo;
  3716. int planea_wm;
  3717. crtc = single_enabled_crtc(dev);
  3718. if (crtc == NULL)
  3719. return;
  3720. planea_wm = intel_calculate_wm(crtc->mode.clock, &i830_wm_info,
  3721. dev_priv->display.get_fifo_size(dev, 0),
  3722. crtc->fb->bits_per_pixel / 8,
  3723. latency_ns);
  3724. fwater_lo = I915_READ(FW_BLC) & ~0xfff;
  3725. fwater_lo |= (3<<8) | planea_wm;
  3726. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
  3727. I915_WRITE(FW_BLC, fwater_lo);
  3728. }
  3729. #define ILK_LP0_PLANE_LATENCY 700
  3730. #define ILK_LP0_CURSOR_LATENCY 1300
  3731. /*
  3732. * Check the wm result.
  3733. *
  3734. * If any calculated watermark values is larger than the maximum value that
  3735. * can be programmed into the associated watermark register, that watermark
  3736. * must be disabled.
  3737. */
  3738. static bool ironlake_check_srwm(struct drm_device *dev, int level,
  3739. int fbc_wm, int display_wm, int cursor_wm,
  3740. const struct intel_watermark_params *display,
  3741. const struct intel_watermark_params *cursor)
  3742. {
  3743. struct drm_i915_private *dev_priv = dev->dev_private;
  3744. DRM_DEBUG_KMS("watermark %d: display plane %d, fbc lines %d,"
  3745. " cursor %d\n", level, display_wm, fbc_wm, cursor_wm);
  3746. if (fbc_wm > SNB_FBC_MAX_SRWM) {
  3747. DRM_DEBUG_KMS("fbc watermark(%d) is too large(%d), disabling wm%d+\n",
  3748. fbc_wm, SNB_FBC_MAX_SRWM, level);
  3749. /* fbc has it's own way to disable FBC WM */
  3750. I915_WRITE(DISP_ARB_CTL,
  3751. I915_READ(DISP_ARB_CTL) | DISP_FBC_WM_DIS);
  3752. return false;
  3753. }
  3754. if (display_wm > display->max_wm) {
  3755. DRM_DEBUG_KMS("display watermark(%d) is too large(%d), disabling wm%d+\n",
  3756. display_wm, SNB_DISPLAY_MAX_SRWM, level);
  3757. return false;
  3758. }
  3759. if (cursor_wm > cursor->max_wm) {
  3760. DRM_DEBUG_KMS("cursor watermark(%d) is too large(%d), disabling wm%d+\n",
  3761. cursor_wm, SNB_CURSOR_MAX_SRWM, level);
  3762. return false;
  3763. }
  3764. if (!(fbc_wm || display_wm || cursor_wm)) {
  3765. DRM_DEBUG_KMS("latency %d is 0, disabling wm%d+\n", level, level);
  3766. return false;
  3767. }
  3768. return true;
  3769. }
  3770. /*
  3771. * Compute watermark values of WM[1-3],
  3772. */
  3773. static bool ironlake_compute_srwm(struct drm_device *dev, int level, int plane,
  3774. int latency_ns,
  3775. const struct intel_watermark_params *display,
  3776. const struct intel_watermark_params *cursor,
  3777. int *fbc_wm, int *display_wm, int *cursor_wm)
  3778. {
  3779. struct drm_crtc *crtc;
  3780. unsigned long line_time_us;
  3781. int hdisplay, htotal, pixel_size, clock;
  3782. int line_count, line_size;
  3783. int small, large;
  3784. int entries;
  3785. if (!latency_ns) {
  3786. *fbc_wm = *display_wm = *cursor_wm = 0;
  3787. return false;
  3788. }
  3789. crtc = intel_get_crtc_for_plane(dev, plane);
  3790. hdisplay = crtc->mode.hdisplay;
  3791. htotal = crtc->mode.htotal;
  3792. clock = crtc->mode.clock;
  3793. pixel_size = crtc->fb->bits_per_pixel / 8;
  3794. line_time_us = (htotal * 1000) / clock;
  3795. line_count = (latency_ns / line_time_us + 1000) / 1000;
  3796. line_size = hdisplay * pixel_size;
  3797. /* Use the minimum of the small and large buffer method for primary */
  3798. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  3799. large = line_count * line_size;
  3800. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  3801. *display_wm = entries + display->guard_size;
  3802. /*
  3803. * Spec says:
  3804. * FBC WM = ((Final Primary WM * 64) / number of bytes per line) + 2
  3805. */
  3806. *fbc_wm = DIV_ROUND_UP(*display_wm * 64, line_size) + 2;
  3807. /* calculate the self-refresh watermark for display cursor */
  3808. entries = line_count * pixel_size * 64;
  3809. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  3810. *cursor_wm = entries + cursor->guard_size;
  3811. return ironlake_check_srwm(dev, level,
  3812. *fbc_wm, *display_wm, *cursor_wm,
  3813. display, cursor);
  3814. }
  3815. static void ironlake_update_wm(struct drm_device *dev)
  3816. {
  3817. struct drm_i915_private *dev_priv = dev->dev_private;
  3818. int fbc_wm, plane_wm, cursor_wm;
  3819. unsigned int enabled;
  3820. enabled = 0;
  3821. if (g4x_compute_wm0(dev, 0,
  3822. &ironlake_display_wm_info,
  3823. ILK_LP0_PLANE_LATENCY,
  3824. &ironlake_cursor_wm_info,
  3825. ILK_LP0_CURSOR_LATENCY,
  3826. &plane_wm, &cursor_wm)) {
  3827. I915_WRITE(WM0_PIPEA_ILK,
  3828. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3829. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  3830. " plane %d, " "cursor: %d\n",
  3831. plane_wm, cursor_wm);
  3832. enabled |= 1;
  3833. }
  3834. if (g4x_compute_wm0(dev, 1,
  3835. &ironlake_display_wm_info,
  3836. ILK_LP0_PLANE_LATENCY,
  3837. &ironlake_cursor_wm_info,
  3838. ILK_LP0_CURSOR_LATENCY,
  3839. &plane_wm, &cursor_wm)) {
  3840. I915_WRITE(WM0_PIPEB_ILK,
  3841. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3842. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  3843. " plane %d, cursor: %d\n",
  3844. plane_wm, cursor_wm);
  3845. enabled |= 2;
  3846. }
  3847. /*
  3848. * Calculate and update the self-refresh watermark only when one
  3849. * display plane is used.
  3850. */
  3851. I915_WRITE(WM3_LP_ILK, 0);
  3852. I915_WRITE(WM2_LP_ILK, 0);
  3853. I915_WRITE(WM1_LP_ILK, 0);
  3854. if (!single_plane_enabled(enabled))
  3855. return;
  3856. enabled = ffs(enabled) - 1;
  3857. /* WM1 */
  3858. if (!ironlake_compute_srwm(dev, 1, enabled,
  3859. ILK_READ_WM1_LATENCY() * 500,
  3860. &ironlake_display_srwm_info,
  3861. &ironlake_cursor_srwm_info,
  3862. &fbc_wm, &plane_wm, &cursor_wm))
  3863. return;
  3864. I915_WRITE(WM1_LP_ILK,
  3865. WM1_LP_SR_EN |
  3866. (ILK_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3867. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3868. (plane_wm << WM1_LP_SR_SHIFT) |
  3869. cursor_wm);
  3870. /* WM2 */
  3871. if (!ironlake_compute_srwm(dev, 2, enabled,
  3872. ILK_READ_WM2_LATENCY() * 500,
  3873. &ironlake_display_srwm_info,
  3874. &ironlake_cursor_srwm_info,
  3875. &fbc_wm, &plane_wm, &cursor_wm))
  3876. return;
  3877. I915_WRITE(WM2_LP_ILK,
  3878. WM2_LP_EN |
  3879. (ILK_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3880. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3881. (plane_wm << WM1_LP_SR_SHIFT) |
  3882. cursor_wm);
  3883. /*
  3884. * WM3 is unsupported on ILK, probably because we don't have latency
  3885. * data for that power state
  3886. */
  3887. }
  3888. void sandybridge_update_wm(struct drm_device *dev)
  3889. {
  3890. struct drm_i915_private *dev_priv = dev->dev_private;
  3891. int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
  3892. int fbc_wm, plane_wm, cursor_wm;
  3893. unsigned int enabled;
  3894. enabled = 0;
  3895. if (g4x_compute_wm0(dev, 0,
  3896. &sandybridge_display_wm_info, latency,
  3897. &sandybridge_cursor_wm_info, latency,
  3898. &plane_wm, &cursor_wm)) {
  3899. I915_WRITE(WM0_PIPEA_ILK,
  3900. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3901. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  3902. " plane %d, " "cursor: %d\n",
  3903. plane_wm, cursor_wm);
  3904. enabled |= 1;
  3905. }
  3906. if (g4x_compute_wm0(dev, 1,
  3907. &sandybridge_display_wm_info, latency,
  3908. &sandybridge_cursor_wm_info, latency,
  3909. &plane_wm, &cursor_wm)) {
  3910. I915_WRITE(WM0_PIPEB_ILK,
  3911. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3912. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  3913. " plane %d, cursor: %d\n",
  3914. plane_wm, cursor_wm);
  3915. enabled |= 2;
  3916. }
  3917. /* IVB has 3 pipes */
  3918. if (IS_IVYBRIDGE(dev) &&
  3919. g4x_compute_wm0(dev, 2,
  3920. &sandybridge_display_wm_info, latency,
  3921. &sandybridge_cursor_wm_info, latency,
  3922. &plane_wm, &cursor_wm)) {
  3923. I915_WRITE(WM0_PIPEC_IVB,
  3924. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3925. DRM_DEBUG_KMS("FIFO watermarks For pipe C -"
  3926. " plane %d, cursor: %d\n",
  3927. plane_wm, cursor_wm);
  3928. enabled |= 3;
  3929. }
  3930. /*
  3931. * Calculate and update the self-refresh watermark only when one
  3932. * display plane is used.
  3933. *
  3934. * SNB support 3 levels of watermark.
  3935. *
  3936. * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
  3937. * and disabled in the descending order
  3938. *
  3939. */
  3940. I915_WRITE(WM3_LP_ILK, 0);
  3941. I915_WRITE(WM2_LP_ILK, 0);
  3942. I915_WRITE(WM1_LP_ILK, 0);
  3943. if (!single_plane_enabled(enabled) ||
  3944. dev_priv->sprite_scaling_enabled)
  3945. return;
  3946. enabled = ffs(enabled) - 1;
  3947. /* WM1 */
  3948. if (!ironlake_compute_srwm(dev, 1, enabled,
  3949. SNB_READ_WM1_LATENCY() * 500,
  3950. &sandybridge_display_srwm_info,
  3951. &sandybridge_cursor_srwm_info,
  3952. &fbc_wm, &plane_wm, &cursor_wm))
  3953. return;
  3954. I915_WRITE(WM1_LP_ILK,
  3955. WM1_LP_SR_EN |
  3956. (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3957. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3958. (plane_wm << WM1_LP_SR_SHIFT) |
  3959. cursor_wm);
  3960. /* WM2 */
  3961. if (!ironlake_compute_srwm(dev, 2, enabled,
  3962. SNB_READ_WM2_LATENCY() * 500,
  3963. &sandybridge_display_srwm_info,
  3964. &sandybridge_cursor_srwm_info,
  3965. &fbc_wm, &plane_wm, &cursor_wm))
  3966. return;
  3967. I915_WRITE(WM2_LP_ILK,
  3968. WM2_LP_EN |
  3969. (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3970. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3971. (plane_wm << WM1_LP_SR_SHIFT) |
  3972. cursor_wm);
  3973. /* WM3 */
  3974. if (!ironlake_compute_srwm(dev, 3, enabled,
  3975. SNB_READ_WM3_LATENCY() * 500,
  3976. &sandybridge_display_srwm_info,
  3977. &sandybridge_cursor_srwm_info,
  3978. &fbc_wm, &plane_wm, &cursor_wm))
  3979. return;
  3980. I915_WRITE(WM3_LP_ILK,
  3981. WM3_LP_EN |
  3982. (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3983. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3984. (plane_wm << WM1_LP_SR_SHIFT) |
  3985. cursor_wm);
  3986. }
  3987. static bool
  3988. sandybridge_compute_sprite_wm(struct drm_device *dev, int plane,
  3989. uint32_t sprite_width, int pixel_size,
  3990. const struct intel_watermark_params *display,
  3991. int display_latency_ns, int *sprite_wm)
  3992. {
  3993. struct drm_crtc *crtc;
  3994. int clock;
  3995. int entries, tlb_miss;
  3996. crtc = intel_get_crtc_for_plane(dev, plane);
  3997. if (crtc->fb == NULL || !crtc->enabled) {
  3998. *sprite_wm = display->guard_size;
  3999. return false;
  4000. }
  4001. clock = crtc->mode.clock;
  4002. /* Use the small buffer method to calculate the sprite watermark */
  4003. entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
  4004. tlb_miss = display->fifo_size*display->cacheline_size -
  4005. sprite_width * 8;
  4006. if (tlb_miss > 0)
  4007. entries += tlb_miss;
  4008. entries = DIV_ROUND_UP(entries, display->cacheline_size);
  4009. *sprite_wm = entries + display->guard_size;
  4010. if (*sprite_wm > (int)display->max_wm)
  4011. *sprite_wm = display->max_wm;
  4012. return true;
  4013. }
  4014. static bool
  4015. sandybridge_compute_sprite_srwm(struct drm_device *dev, int plane,
  4016. uint32_t sprite_width, int pixel_size,
  4017. const struct intel_watermark_params *display,
  4018. int latency_ns, int *sprite_wm)
  4019. {
  4020. struct drm_crtc *crtc;
  4021. unsigned long line_time_us;
  4022. int clock;
  4023. int line_count, line_size;
  4024. int small, large;
  4025. int entries;
  4026. if (!latency_ns) {
  4027. *sprite_wm = 0;
  4028. return false;
  4029. }
  4030. crtc = intel_get_crtc_for_plane(dev, plane);
  4031. clock = crtc->mode.clock;
  4032. line_time_us = (sprite_width * 1000) / clock;
  4033. line_count = (latency_ns / line_time_us + 1000) / 1000;
  4034. line_size = sprite_width * pixel_size;
  4035. /* Use the minimum of the small and large buffer method for primary */
  4036. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  4037. large = line_count * line_size;
  4038. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  4039. *sprite_wm = entries + display->guard_size;
  4040. return *sprite_wm > 0x3ff ? false : true;
  4041. }
  4042. static void sandybridge_update_sprite_wm(struct drm_device *dev, int pipe,
  4043. uint32_t sprite_width, int pixel_size)
  4044. {
  4045. struct drm_i915_private *dev_priv = dev->dev_private;
  4046. int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
  4047. int sprite_wm, reg;
  4048. int ret;
  4049. switch (pipe) {
  4050. case 0:
  4051. reg = WM0_PIPEA_ILK;
  4052. break;
  4053. case 1:
  4054. reg = WM0_PIPEB_ILK;
  4055. break;
  4056. case 2:
  4057. reg = WM0_PIPEC_IVB;
  4058. break;
  4059. default:
  4060. return; /* bad pipe */
  4061. }
  4062. ret = sandybridge_compute_sprite_wm(dev, pipe, sprite_width, pixel_size,
  4063. &sandybridge_display_wm_info,
  4064. latency, &sprite_wm);
  4065. if (!ret) {
  4066. DRM_DEBUG_KMS("failed to compute sprite wm for pipe %d\n",
  4067. pipe);
  4068. return;
  4069. }
  4070. I915_WRITE(reg, I915_READ(reg) | (sprite_wm << WM0_PIPE_SPRITE_SHIFT));
  4071. DRM_DEBUG_KMS("sprite watermarks For pipe %d - %d\n", pipe, sprite_wm);
  4072. ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
  4073. pixel_size,
  4074. &sandybridge_display_srwm_info,
  4075. SNB_READ_WM1_LATENCY() * 500,
  4076. &sprite_wm);
  4077. if (!ret) {
  4078. DRM_DEBUG_KMS("failed to compute sprite lp1 wm on pipe %d\n",
  4079. pipe);
  4080. return;
  4081. }
  4082. I915_WRITE(WM1S_LP_ILK, sprite_wm);
  4083. /* Only IVB has two more LP watermarks for sprite */
  4084. if (!IS_IVYBRIDGE(dev))
  4085. return;
  4086. ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
  4087. pixel_size,
  4088. &sandybridge_display_srwm_info,
  4089. SNB_READ_WM2_LATENCY() * 500,
  4090. &sprite_wm);
  4091. if (!ret) {
  4092. DRM_DEBUG_KMS("failed to compute sprite lp2 wm on pipe %d\n",
  4093. pipe);
  4094. return;
  4095. }
  4096. I915_WRITE(WM2S_LP_IVB, sprite_wm);
  4097. ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
  4098. pixel_size,
  4099. &sandybridge_display_srwm_info,
  4100. SNB_READ_WM3_LATENCY() * 500,
  4101. &sprite_wm);
  4102. if (!ret) {
  4103. DRM_DEBUG_KMS("failed to compute sprite lp3 wm on pipe %d\n",
  4104. pipe);
  4105. return;
  4106. }
  4107. I915_WRITE(WM3S_LP_IVB, sprite_wm);
  4108. }
  4109. /**
  4110. * intel_update_watermarks - update FIFO watermark values based on current modes
  4111. *
  4112. * Calculate watermark values for the various WM regs based on current mode
  4113. * and plane configuration.
  4114. *
  4115. * There are several cases to deal with here:
  4116. * - normal (i.e. non-self-refresh)
  4117. * - self-refresh (SR) mode
  4118. * - lines are large relative to FIFO size (buffer can hold up to 2)
  4119. * - lines are small relative to FIFO size (buffer can hold more than 2
  4120. * lines), so need to account for TLB latency
  4121. *
  4122. * The normal calculation is:
  4123. * watermark = dotclock * bytes per pixel * latency
  4124. * where latency is platform & configuration dependent (we assume pessimal
  4125. * values here).
  4126. *
  4127. * The SR calculation is:
  4128. * watermark = (trunc(latency/line time)+1) * surface width *
  4129. * bytes per pixel
  4130. * where
  4131. * line time = htotal / dotclock
  4132. * surface width = hdisplay for normal plane and 64 for cursor
  4133. * and latency is assumed to be high, as above.
  4134. *
  4135. * The final value programmed to the register should always be rounded up,
  4136. * and include an extra 2 entries to account for clock crossings.
  4137. *
  4138. * We don't use the sprite, so we can ignore that. And on Crestline we have
  4139. * to set the non-SR watermarks to 8.
  4140. */
  4141. static void intel_update_watermarks(struct drm_device *dev)
  4142. {
  4143. struct drm_i915_private *dev_priv = dev->dev_private;
  4144. if (dev_priv->display.update_wm)
  4145. dev_priv->display.update_wm(dev);
  4146. }
  4147. void intel_update_sprite_watermarks(struct drm_device *dev, int pipe,
  4148. uint32_t sprite_width, int pixel_size)
  4149. {
  4150. struct drm_i915_private *dev_priv = dev->dev_private;
  4151. if (dev_priv->display.update_sprite_wm)
  4152. dev_priv->display.update_sprite_wm(dev, pipe, sprite_width,
  4153. pixel_size);
  4154. }
  4155. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  4156. {
  4157. if (i915_panel_use_ssc >= 0)
  4158. return i915_panel_use_ssc != 0;
  4159. return dev_priv->lvds_use_ssc
  4160. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  4161. }
  4162. /**
  4163. * intel_choose_pipe_bpp_dither - figure out what color depth the pipe should send
  4164. * @crtc: CRTC structure
  4165. * @mode: requested mode
  4166. *
  4167. * A pipe may be connected to one or more outputs. Based on the depth of the
  4168. * attached framebuffer, choose a good color depth to use on the pipe.
  4169. *
  4170. * If possible, match the pipe depth to the fb depth. In some cases, this
  4171. * isn't ideal, because the connected output supports a lesser or restricted
  4172. * set of depths. Resolve that here:
  4173. * LVDS typically supports only 6bpc, so clamp down in that case
  4174. * HDMI supports only 8bpc or 12bpc, so clamp to 8bpc with dither for 10bpc
  4175. * Displays may support a restricted set as well, check EDID and clamp as
  4176. * appropriate.
  4177. * DP may want to dither down to 6bpc to fit larger modes
  4178. *
  4179. * RETURNS:
  4180. * Dithering requirement (i.e. false if display bpc and pipe bpc match,
  4181. * true if they don't match).
  4182. */
  4183. static bool intel_choose_pipe_bpp_dither(struct drm_crtc *crtc,
  4184. unsigned int *pipe_bpp,
  4185. struct drm_display_mode *mode)
  4186. {
  4187. struct drm_device *dev = crtc->dev;
  4188. struct drm_i915_private *dev_priv = dev->dev_private;
  4189. struct drm_encoder *encoder;
  4190. struct drm_connector *connector;
  4191. unsigned int display_bpc = UINT_MAX, bpc;
  4192. /* Walk the encoders & connectors on this crtc, get min bpc */
  4193. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  4194. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4195. if (encoder->crtc != crtc)
  4196. continue;
  4197. if (intel_encoder->type == INTEL_OUTPUT_LVDS) {
  4198. unsigned int lvds_bpc;
  4199. if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) ==
  4200. LVDS_A3_POWER_UP)
  4201. lvds_bpc = 8;
  4202. else
  4203. lvds_bpc = 6;
  4204. if (lvds_bpc < display_bpc) {
  4205. DRM_DEBUG_KMS("clamping display bpc (was %d) to LVDS (%d)\n", display_bpc, lvds_bpc);
  4206. display_bpc = lvds_bpc;
  4207. }
  4208. continue;
  4209. }
  4210. if (intel_encoder->type == INTEL_OUTPUT_EDP) {
  4211. /* Use VBT settings if we have an eDP panel */
  4212. unsigned int edp_bpc = dev_priv->edp.bpp / 3;
  4213. if (edp_bpc < display_bpc) {
  4214. DRM_DEBUG_KMS("clamping display bpc (was %d) to eDP (%d)\n", display_bpc, edp_bpc);
  4215. display_bpc = edp_bpc;
  4216. }
  4217. continue;
  4218. }
  4219. /* Not one of the known troublemakers, check the EDID */
  4220. list_for_each_entry(connector, &dev->mode_config.connector_list,
  4221. head) {
  4222. if (connector->encoder != encoder)
  4223. continue;
  4224. /* Don't use an invalid EDID bpc value */
  4225. if (connector->display_info.bpc &&
  4226. connector->display_info.bpc < display_bpc) {
  4227. DRM_DEBUG_KMS("clamping display bpc (was %d) to EDID reported max of %d\n", display_bpc, connector->display_info.bpc);
  4228. display_bpc = connector->display_info.bpc;
  4229. }
  4230. }
  4231. /*
  4232. * HDMI is either 12 or 8, so if the display lets 10bpc sneak
  4233. * through, clamp it down. (Note: >12bpc will be caught below.)
  4234. */
  4235. if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
  4236. if (display_bpc > 8 && display_bpc < 12) {
  4237. DRM_DEBUG_KMS("forcing bpc to 12 for HDMI\n");
  4238. display_bpc = 12;
  4239. } else {
  4240. DRM_DEBUG_KMS("forcing bpc to 8 for HDMI\n");
  4241. display_bpc = 8;
  4242. }
  4243. }
  4244. }
  4245. if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
  4246. DRM_DEBUG_KMS("Dithering DP to 6bpc\n");
  4247. display_bpc = 6;
  4248. }
  4249. /*
  4250. * We could just drive the pipe at the highest bpc all the time and
  4251. * enable dithering as needed, but that costs bandwidth. So choose
  4252. * the minimum value that expresses the full color range of the fb but
  4253. * also stays within the max display bpc discovered above.
  4254. */
  4255. switch (crtc->fb->depth) {
  4256. case 8:
  4257. bpc = 8; /* since we go through a colormap */
  4258. break;
  4259. case 15:
  4260. case 16:
  4261. bpc = 6; /* min is 18bpp */
  4262. break;
  4263. case 24:
  4264. bpc = 8;
  4265. break;
  4266. case 30:
  4267. bpc = 10;
  4268. break;
  4269. case 48:
  4270. bpc = 12;
  4271. break;
  4272. default:
  4273. DRM_DEBUG("unsupported depth, assuming 24 bits\n");
  4274. bpc = min((unsigned int)8, display_bpc);
  4275. break;
  4276. }
  4277. display_bpc = min(display_bpc, bpc);
  4278. DRM_DEBUG_KMS("setting pipe bpc to %d (max display bpc %d)\n",
  4279. bpc, display_bpc);
  4280. *pipe_bpp = display_bpc * 3;
  4281. return display_bpc != bpc;
  4282. }
  4283. static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
  4284. struct drm_display_mode *mode,
  4285. struct drm_display_mode *adjusted_mode,
  4286. int x, int y,
  4287. struct drm_framebuffer *old_fb)
  4288. {
  4289. struct drm_device *dev = crtc->dev;
  4290. struct drm_i915_private *dev_priv = dev->dev_private;
  4291. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4292. int pipe = intel_crtc->pipe;
  4293. int plane = intel_crtc->plane;
  4294. int refclk, num_connectors = 0;
  4295. intel_clock_t clock, reduced_clock;
  4296. u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
  4297. bool ok, has_reduced_clock = false, is_sdvo = false, is_dvo = false;
  4298. bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
  4299. struct drm_mode_config *mode_config = &dev->mode_config;
  4300. struct intel_encoder *encoder;
  4301. const intel_limit_t *limit;
  4302. int ret;
  4303. u32 temp;
  4304. u32 lvds_sync = 0;
  4305. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  4306. if (encoder->base.crtc != crtc)
  4307. continue;
  4308. switch (encoder->type) {
  4309. case INTEL_OUTPUT_LVDS:
  4310. is_lvds = true;
  4311. break;
  4312. case INTEL_OUTPUT_SDVO:
  4313. case INTEL_OUTPUT_HDMI:
  4314. is_sdvo = true;
  4315. if (encoder->needs_tv_clock)
  4316. is_tv = true;
  4317. break;
  4318. case INTEL_OUTPUT_DVO:
  4319. is_dvo = true;
  4320. break;
  4321. case INTEL_OUTPUT_TVOUT:
  4322. is_tv = true;
  4323. break;
  4324. case INTEL_OUTPUT_ANALOG:
  4325. is_crt = true;
  4326. break;
  4327. case INTEL_OUTPUT_DISPLAYPORT:
  4328. is_dp = true;
  4329. break;
  4330. }
  4331. num_connectors++;
  4332. }
  4333. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4334. refclk = dev_priv->lvds_ssc_freq * 1000;
  4335. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  4336. refclk / 1000);
  4337. } else if (!IS_GEN2(dev)) {
  4338. refclk = 96000;
  4339. } else {
  4340. refclk = 48000;
  4341. }
  4342. /*
  4343. * Returns a set of divisors for the desired target clock with the given
  4344. * refclk, or FALSE. The returned values represent the clock equation:
  4345. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  4346. */
  4347. limit = intel_limit(crtc, refclk);
  4348. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
  4349. if (!ok) {
  4350. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  4351. return -EINVAL;
  4352. }
  4353. /* Ensure that the cursor is valid for the new mode before changing... */
  4354. intel_crtc_update_cursor(crtc, true);
  4355. if (is_lvds && dev_priv->lvds_downclock_avail) {
  4356. has_reduced_clock = limit->find_pll(limit, crtc,
  4357. dev_priv->lvds_downclock,
  4358. refclk,
  4359. &reduced_clock);
  4360. if (has_reduced_clock && (clock.p != reduced_clock.p)) {
  4361. /*
  4362. * If the different P is found, it means that we can't
  4363. * switch the display clock by using the FP0/FP1.
  4364. * In such case we will disable the LVDS downclock
  4365. * feature.
  4366. */
  4367. DRM_DEBUG_KMS("Different P is found for "
  4368. "LVDS clock/downclock\n");
  4369. has_reduced_clock = 0;
  4370. }
  4371. }
  4372. /* SDVO TV has fixed PLL values depend on its clock range,
  4373. this mirrors vbios setting. */
  4374. if (is_sdvo && is_tv) {
  4375. if (adjusted_mode->clock >= 100000
  4376. && adjusted_mode->clock < 140500) {
  4377. clock.p1 = 2;
  4378. clock.p2 = 10;
  4379. clock.n = 3;
  4380. clock.m1 = 16;
  4381. clock.m2 = 8;
  4382. } else if (adjusted_mode->clock >= 140500
  4383. && adjusted_mode->clock <= 200000) {
  4384. clock.p1 = 1;
  4385. clock.p2 = 10;
  4386. clock.n = 6;
  4387. clock.m1 = 12;
  4388. clock.m2 = 8;
  4389. }
  4390. }
  4391. if (IS_PINEVIEW(dev)) {
  4392. fp = (1 << clock.n) << 16 | clock.m1 << 8 | clock.m2;
  4393. if (has_reduced_clock)
  4394. fp2 = (1 << reduced_clock.n) << 16 |
  4395. reduced_clock.m1 << 8 | reduced_clock.m2;
  4396. } else {
  4397. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  4398. if (has_reduced_clock)
  4399. fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
  4400. reduced_clock.m2;
  4401. }
  4402. dpll = DPLL_VGA_MODE_DIS;
  4403. if (!IS_GEN2(dev)) {
  4404. if (is_lvds)
  4405. dpll |= DPLLB_MODE_LVDS;
  4406. else
  4407. dpll |= DPLLB_MODE_DAC_SERIAL;
  4408. if (is_sdvo) {
  4409. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  4410. if (pixel_multiplier > 1) {
  4411. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  4412. dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
  4413. }
  4414. dpll |= DPLL_DVO_HIGH_SPEED;
  4415. }
  4416. if (is_dp)
  4417. dpll |= DPLL_DVO_HIGH_SPEED;
  4418. /* compute bitmask from p1 value */
  4419. if (IS_PINEVIEW(dev))
  4420. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  4421. else {
  4422. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4423. if (IS_G4X(dev) && has_reduced_clock)
  4424. dpll |= (1 << (reduced_clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  4425. }
  4426. switch (clock.p2) {
  4427. case 5:
  4428. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  4429. break;
  4430. case 7:
  4431. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  4432. break;
  4433. case 10:
  4434. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  4435. break;
  4436. case 14:
  4437. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  4438. break;
  4439. }
  4440. if (INTEL_INFO(dev)->gen >= 4)
  4441. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  4442. } else {
  4443. if (is_lvds) {
  4444. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4445. } else {
  4446. if (clock.p1 == 2)
  4447. dpll |= PLL_P1_DIVIDE_BY_TWO;
  4448. else
  4449. dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4450. if (clock.p2 == 4)
  4451. dpll |= PLL_P2_DIVIDE_BY_4;
  4452. }
  4453. }
  4454. if (is_sdvo && is_tv)
  4455. dpll |= PLL_REF_INPUT_TVCLKINBC;
  4456. else if (is_tv)
  4457. /* XXX: just matching BIOS for now */
  4458. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  4459. dpll |= 3;
  4460. else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4461. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4462. else
  4463. dpll |= PLL_REF_INPUT_DREFCLK;
  4464. /* setup pipeconf */
  4465. pipeconf = I915_READ(PIPECONF(pipe));
  4466. /* Set up the display plane register */
  4467. dspcntr = DISPPLANE_GAMMA_ENABLE;
  4468. /* Ironlake's plane is forced to pipe, bit 24 is to
  4469. enable color space conversion */
  4470. if (pipe == 0)
  4471. dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
  4472. else
  4473. dspcntr |= DISPPLANE_SEL_PIPE_B;
  4474. if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
  4475. /* Enable pixel doubling when the dot clock is > 90% of the (display)
  4476. * core speed.
  4477. *
  4478. * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
  4479. * pipe == 0 check?
  4480. */
  4481. if (mode->clock >
  4482. dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
  4483. pipeconf |= PIPECONF_DOUBLE_WIDE;
  4484. else
  4485. pipeconf &= ~PIPECONF_DOUBLE_WIDE;
  4486. }
  4487. /* default to 8bpc */
  4488. pipeconf &= ~(PIPECONF_BPP_MASK | PIPECONF_DITHER_EN);
  4489. if (is_dp) {
  4490. if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
  4491. pipeconf |= PIPECONF_BPP_6 |
  4492. PIPECONF_DITHER_EN |
  4493. PIPECONF_DITHER_TYPE_SP;
  4494. }
  4495. }
  4496. dpll |= DPLL_VCO_ENABLE;
  4497. DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  4498. drm_mode_debug_printmodeline(mode);
  4499. I915_WRITE(FP0(pipe), fp);
  4500. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
  4501. POSTING_READ(DPLL(pipe));
  4502. udelay(150);
  4503. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  4504. * This is an exception to the general rule that mode_set doesn't turn
  4505. * things on.
  4506. */
  4507. if (is_lvds) {
  4508. temp = I915_READ(LVDS);
  4509. temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  4510. if (pipe == 1) {
  4511. temp |= LVDS_PIPEB_SELECT;
  4512. } else {
  4513. temp &= ~LVDS_PIPEB_SELECT;
  4514. }
  4515. /* set the corresponsding LVDS_BORDER bit */
  4516. temp |= dev_priv->lvds_border_bits;
  4517. /* Set the B0-B3 data pairs corresponding to whether we're going to
  4518. * set the DPLLs for dual-channel mode or not.
  4519. */
  4520. if (clock.p2 == 7)
  4521. temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  4522. else
  4523. temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  4524. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  4525. * appropriately here, but we need to look more thoroughly into how
  4526. * panels behave in the two modes.
  4527. */
  4528. /* set the dithering flag on LVDS as needed */
  4529. if (INTEL_INFO(dev)->gen >= 4) {
  4530. if (dev_priv->lvds_dither)
  4531. temp |= LVDS_ENABLE_DITHER;
  4532. else
  4533. temp &= ~LVDS_ENABLE_DITHER;
  4534. }
  4535. if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
  4536. lvds_sync |= LVDS_HSYNC_POLARITY;
  4537. if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
  4538. lvds_sync |= LVDS_VSYNC_POLARITY;
  4539. if ((temp & (LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY))
  4540. != lvds_sync) {
  4541. char flags[2] = "-+";
  4542. DRM_INFO("Changing LVDS panel from "
  4543. "(%chsync, %cvsync) to (%chsync, %cvsync)\n",
  4544. flags[!(temp & LVDS_HSYNC_POLARITY)],
  4545. flags[!(temp & LVDS_VSYNC_POLARITY)],
  4546. flags[!(lvds_sync & LVDS_HSYNC_POLARITY)],
  4547. flags[!(lvds_sync & LVDS_VSYNC_POLARITY)]);
  4548. temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
  4549. temp |= lvds_sync;
  4550. }
  4551. I915_WRITE(LVDS, temp);
  4552. }
  4553. if (is_dp) {
  4554. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  4555. }
  4556. I915_WRITE(DPLL(pipe), dpll);
  4557. /* Wait for the clocks to stabilize. */
  4558. POSTING_READ(DPLL(pipe));
  4559. udelay(150);
  4560. if (INTEL_INFO(dev)->gen >= 4) {
  4561. temp = 0;
  4562. if (is_sdvo) {
  4563. temp = intel_mode_get_pixel_multiplier(adjusted_mode);
  4564. if (temp > 1)
  4565. temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4566. else
  4567. temp = 0;
  4568. }
  4569. I915_WRITE(DPLL_MD(pipe), temp);
  4570. } else {
  4571. /* The pixel multiplier can only be updated once the
  4572. * DPLL is enabled and the clocks are stable.
  4573. *
  4574. * So write it again.
  4575. */
  4576. I915_WRITE(DPLL(pipe), dpll);
  4577. }
  4578. intel_crtc->lowfreq_avail = false;
  4579. if (is_lvds && has_reduced_clock && i915_powersave) {
  4580. I915_WRITE(FP1(pipe), fp2);
  4581. intel_crtc->lowfreq_avail = true;
  4582. if (HAS_PIPE_CXSR(dev)) {
  4583. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  4584. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  4585. }
  4586. } else {
  4587. I915_WRITE(FP1(pipe), fp);
  4588. if (HAS_PIPE_CXSR(dev)) {
  4589. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  4590. pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
  4591. }
  4592. }
  4593. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4594. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  4595. /* the chip adds 2 halflines automatically */
  4596. adjusted_mode->crtc_vdisplay -= 1;
  4597. adjusted_mode->crtc_vtotal -= 1;
  4598. adjusted_mode->crtc_vblank_start -= 1;
  4599. adjusted_mode->crtc_vblank_end -= 1;
  4600. adjusted_mode->crtc_vsync_end -= 1;
  4601. adjusted_mode->crtc_vsync_start -= 1;
  4602. } else
  4603. pipeconf &= ~PIPECONF_INTERLACE_MASK; /* progressive */
  4604. I915_WRITE(HTOTAL(pipe),
  4605. (adjusted_mode->crtc_hdisplay - 1) |
  4606. ((adjusted_mode->crtc_htotal - 1) << 16));
  4607. I915_WRITE(HBLANK(pipe),
  4608. (adjusted_mode->crtc_hblank_start - 1) |
  4609. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  4610. I915_WRITE(HSYNC(pipe),
  4611. (adjusted_mode->crtc_hsync_start - 1) |
  4612. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  4613. I915_WRITE(VTOTAL(pipe),
  4614. (adjusted_mode->crtc_vdisplay - 1) |
  4615. ((adjusted_mode->crtc_vtotal - 1) << 16));
  4616. I915_WRITE(VBLANK(pipe),
  4617. (adjusted_mode->crtc_vblank_start - 1) |
  4618. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  4619. I915_WRITE(VSYNC(pipe),
  4620. (adjusted_mode->crtc_vsync_start - 1) |
  4621. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  4622. /* pipesrc and dspsize control the size that is scaled from,
  4623. * which should always be the user's requested size.
  4624. */
  4625. I915_WRITE(DSPSIZE(plane),
  4626. ((mode->vdisplay - 1) << 16) |
  4627. (mode->hdisplay - 1));
  4628. I915_WRITE(DSPPOS(plane), 0);
  4629. I915_WRITE(PIPESRC(pipe),
  4630. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  4631. I915_WRITE(PIPECONF(pipe), pipeconf);
  4632. POSTING_READ(PIPECONF(pipe));
  4633. intel_enable_pipe(dev_priv, pipe, false);
  4634. intel_wait_for_vblank(dev, pipe);
  4635. I915_WRITE(DSPCNTR(plane), dspcntr);
  4636. POSTING_READ(DSPCNTR(plane));
  4637. intel_enable_plane(dev_priv, plane, pipe);
  4638. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  4639. intel_update_watermarks(dev);
  4640. return ret;
  4641. }
  4642. /*
  4643. * Initialize reference clocks when the driver loads
  4644. */
  4645. void ironlake_init_pch_refclk(struct drm_device *dev)
  4646. {
  4647. struct drm_i915_private *dev_priv = dev->dev_private;
  4648. struct drm_mode_config *mode_config = &dev->mode_config;
  4649. struct intel_encoder *encoder;
  4650. u32 temp;
  4651. bool has_lvds = false;
  4652. bool has_cpu_edp = false;
  4653. bool has_pch_edp = false;
  4654. bool has_panel = false;
  4655. bool has_ck505 = false;
  4656. bool can_ssc = false;
  4657. /* We need to take the global config into account */
  4658. list_for_each_entry(encoder, &mode_config->encoder_list,
  4659. base.head) {
  4660. switch (encoder->type) {
  4661. case INTEL_OUTPUT_LVDS:
  4662. has_panel = true;
  4663. has_lvds = true;
  4664. break;
  4665. case INTEL_OUTPUT_EDP:
  4666. has_panel = true;
  4667. if (intel_encoder_is_pch_edp(&encoder->base))
  4668. has_pch_edp = true;
  4669. else
  4670. has_cpu_edp = true;
  4671. break;
  4672. }
  4673. }
  4674. if (HAS_PCH_IBX(dev)) {
  4675. has_ck505 = dev_priv->display_clock_mode;
  4676. can_ssc = has_ck505;
  4677. } else {
  4678. has_ck505 = false;
  4679. can_ssc = true;
  4680. }
  4681. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_pch_edp %d has_cpu_edp %d has_ck505 %d\n",
  4682. has_panel, has_lvds, has_pch_edp, has_cpu_edp,
  4683. has_ck505);
  4684. /* Ironlake: try to setup display ref clock before DPLL
  4685. * enabling. This is only under driver's control after
  4686. * PCH B stepping, previous chipset stepping should be
  4687. * ignoring this setting.
  4688. */
  4689. temp = I915_READ(PCH_DREF_CONTROL);
  4690. /* Always enable nonspread source */
  4691. temp &= ~DREF_NONSPREAD_SOURCE_MASK;
  4692. if (has_ck505)
  4693. temp |= DREF_NONSPREAD_CK505_ENABLE;
  4694. else
  4695. temp |= DREF_NONSPREAD_SOURCE_ENABLE;
  4696. if (has_panel) {
  4697. temp &= ~DREF_SSC_SOURCE_MASK;
  4698. temp |= DREF_SSC_SOURCE_ENABLE;
  4699. /* SSC must be turned on before enabling the CPU output */
  4700. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  4701. DRM_DEBUG_KMS("Using SSC on panel\n");
  4702. temp |= DREF_SSC1_ENABLE;
  4703. }
  4704. /* Get SSC going before enabling the outputs */
  4705. I915_WRITE(PCH_DREF_CONTROL, temp);
  4706. POSTING_READ(PCH_DREF_CONTROL);
  4707. udelay(200);
  4708. temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  4709. /* Enable CPU source on CPU attached eDP */
  4710. if (has_cpu_edp) {
  4711. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  4712. DRM_DEBUG_KMS("Using SSC on eDP\n");
  4713. temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  4714. }
  4715. else
  4716. temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  4717. } else
  4718. temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  4719. I915_WRITE(PCH_DREF_CONTROL, temp);
  4720. POSTING_READ(PCH_DREF_CONTROL);
  4721. udelay(200);
  4722. } else {
  4723. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  4724. temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  4725. /* Turn off CPU output */
  4726. temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  4727. I915_WRITE(PCH_DREF_CONTROL, temp);
  4728. POSTING_READ(PCH_DREF_CONTROL);
  4729. udelay(200);
  4730. /* Turn off the SSC source */
  4731. temp &= ~DREF_SSC_SOURCE_MASK;
  4732. temp |= DREF_SSC_SOURCE_DISABLE;
  4733. /* Turn off SSC1 */
  4734. temp &= ~ DREF_SSC1_ENABLE;
  4735. I915_WRITE(PCH_DREF_CONTROL, temp);
  4736. POSTING_READ(PCH_DREF_CONTROL);
  4737. udelay(200);
  4738. }
  4739. }
  4740. static int ironlake_get_refclk(struct drm_crtc *crtc)
  4741. {
  4742. struct drm_device *dev = crtc->dev;
  4743. struct drm_i915_private *dev_priv = dev->dev_private;
  4744. struct intel_encoder *encoder;
  4745. struct drm_mode_config *mode_config = &dev->mode_config;
  4746. struct intel_encoder *edp_encoder = NULL;
  4747. int num_connectors = 0;
  4748. bool is_lvds = false;
  4749. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  4750. if (encoder->base.crtc != crtc)
  4751. continue;
  4752. switch (encoder->type) {
  4753. case INTEL_OUTPUT_LVDS:
  4754. is_lvds = true;
  4755. break;
  4756. case INTEL_OUTPUT_EDP:
  4757. edp_encoder = encoder;
  4758. break;
  4759. }
  4760. num_connectors++;
  4761. }
  4762. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4763. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  4764. dev_priv->lvds_ssc_freq);
  4765. return dev_priv->lvds_ssc_freq * 1000;
  4766. }
  4767. return 120000;
  4768. }
  4769. static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
  4770. struct drm_display_mode *mode,
  4771. struct drm_display_mode *adjusted_mode,
  4772. int x, int y,
  4773. struct drm_framebuffer *old_fb)
  4774. {
  4775. struct drm_device *dev = crtc->dev;
  4776. struct drm_i915_private *dev_priv = dev->dev_private;
  4777. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4778. int pipe = intel_crtc->pipe;
  4779. int plane = intel_crtc->plane;
  4780. int refclk, num_connectors = 0;
  4781. intel_clock_t clock, reduced_clock;
  4782. u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
  4783. bool ok, has_reduced_clock = false, is_sdvo = false;
  4784. bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
  4785. struct intel_encoder *has_edp_encoder = NULL;
  4786. struct drm_mode_config *mode_config = &dev->mode_config;
  4787. struct intel_encoder *encoder;
  4788. const intel_limit_t *limit;
  4789. int ret;
  4790. struct fdi_m_n m_n = {0};
  4791. u32 temp;
  4792. u32 lvds_sync = 0;
  4793. int target_clock, pixel_multiplier, lane, link_bw, factor;
  4794. unsigned int pipe_bpp;
  4795. bool dither;
  4796. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  4797. if (encoder->base.crtc != crtc)
  4798. continue;
  4799. switch (encoder->type) {
  4800. case INTEL_OUTPUT_LVDS:
  4801. is_lvds = true;
  4802. break;
  4803. case INTEL_OUTPUT_SDVO:
  4804. case INTEL_OUTPUT_HDMI:
  4805. is_sdvo = true;
  4806. if (encoder->needs_tv_clock)
  4807. is_tv = true;
  4808. break;
  4809. case INTEL_OUTPUT_TVOUT:
  4810. is_tv = true;
  4811. break;
  4812. case INTEL_OUTPUT_ANALOG:
  4813. is_crt = true;
  4814. break;
  4815. case INTEL_OUTPUT_DISPLAYPORT:
  4816. is_dp = true;
  4817. break;
  4818. case INTEL_OUTPUT_EDP:
  4819. has_edp_encoder = encoder;
  4820. break;
  4821. }
  4822. num_connectors++;
  4823. }
  4824. refclk = ironlake_get_refclk(crtc);
  4825. /*
  4826. * Returns a set of divisors for the desired target clock with the given
  4827. * refclk, or FALSE. The returned values represent the clock equation:
  4828. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  4829. */
  4830. limit = intel_limit(crtc, refclk);
  4831. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
  4832. if (!ok) {
  4833. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  4834. return -EINVAL;
  4835. }
  4836. /* Ensure that the cursor is valid for the new mode before changing... */
  4837. intel_crtc_update_cursor(crtc, true);
  4838. if (is_lvds && dev_priv->lvds_downclock_avail) {
  4839. has_reduced_clock = limit->find_pll(limit, crtc,
  4840. dev_priv->lvds_downclock,
  4841. refclk,
  4842. &reduced_clock);
  4843. if (has_reduced_clock && (clock.p != reduced_clock.p)) {
  4844. /*
  4845. * If the different P is found, it means that we can't
  4846. * switch the display clock by using the FP0/FP1.
  4847. * In such case we will disable the LVDS downclock
  4848. * feature.
  4849. */
  4850. DRM_DEBUG_KMS("Different P is found for "
  4851. "LVDS clock/downclock\n");
  4852. has_reduced_clock = 0;
  4853. }
  4854. }
  4855. /* SDVO TV has fixed PLL values depend on its clock range,
  4856. this mirrors vbios setting. */
  4857. if (is_sdvo && is_tv) {
  4858. if (adjusted_mode->clock >= 100000
  4859. && adjusted_mode->clock < 140500) {
  4860. clock.p1 = 2;
  4861. clock.p2 = 10;
  4862. clock.n = 3;
  4863. clock.m1 = 16;
  4864. clock.m2 = 8;
  4865. } else if (adjusted_mode->clock >= 140500
  4866. && adjusted_mode->clock <= 200000) {
  4867. clock.p1 = 1;
  4868. clock.p2 = 10;
  4869. clock.n = 6;
  4870. clock.m1 = 12;
  4871. clock.m2 = 8;
  4872. }
  4873. }
  4874. /* FDI link */
  4875. pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  4876. lane = 0;
  4877. /* CPU eDP doesn't require FDI link, so just set DP M/N
  4878. according to current link config */
  4879. if (has_edp_encoder &&
  4880. !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  4881. target_clock = mode->clock;
  4882. intel_edp_link_config(has_edp_encoder,
  4883. &lane, &link_bw);
  4884. } else {
  4885. /* [e]DP over FDI requires target mode clock
  4886. instead of link clock */
  4887. if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
  4888. target_clock = mode->clock;
  4889. else
  4890. target_clock = adjusted_mode->clock;
  4891. /* FDI is a binary signal running at ~2.7GHz, encoding
  4892. * each output octet as 10 bits. The actual frequency
  4893. * is stored as a divider into a 100MHz clock, and the
  4894. * mode pixel clock is stored in units of 1KHz.
  4895. * Hence the bw of each lane in terms of the mode signal
  4896. * is:
  4897. */
  4898. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4899. }
  4900. /* determine panel color depth */
  4901. temp = I915_READ(PIPECONF(pipe));
  4902. temp &= ~PIPE_BPC_MASK;
  4903. dither = intel_choose_pipe_bpp_dither(crtc, &pipe_bpp, mode);
  4904. switch (pipe_bpp) {
  4905. case 18:
  4906. temp |= PIPE_6BPC;
  4907. break;
  4908. case 24:
  4909. temp |= PIPE_8BPC;
  4910. break;
  4911. case 30:
  4912. temp |= PIPE_10BPC;
  4913. break;
  4914. case 36:
  4915. temp |= PIPE_12BPC;
  4916. break;
  4917. default:
  4918. WARN(1, "intel_choose_pipe_bpp returned invalid value %d\n",
  4919. pipe_bpp);
  4920. temp |= PIPE_8BPC;
  4921. pipe_bpp = 24;
  4922. break;
  4923. }
  4924. intel_crtc->bpp = pipe_bpp;
  4925. I915_WRITE(PIPECONF(pipe), temp);
  4926. if (!lane) {
  4927. /*
  4928. * Account for spread spectrum to avoid
  4929. * oversubscribing the link. Max center spread
  4930. * is 2.5%; use 5% for safety's sake.
  4931. */
  4932. u32 bps = target_clock * intel_crtc->bpp * 21 / 20;
  4933. lane = bps / (link_bw * 8) + 1;
  4934. }
  4935. intel_crtc->fdi_lanes = lane;
  4936. if (pixel_multiplier > 1)
  4937. link_bw *= pixel_multiplier;
  4938. ironlake_compute_m_n(intel_crtc->bpp, lane, target_clock, link_bw,
  4939. &m_n);
  4940. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  4941. if (has_reduced_clock)
  4942. fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
  4943. reduced_clock.m2;
  4944. /* Enable autotuning of the PLL clock (if permissible) */
  4945. factor = 21;
  4946. if (is_lvds) {
  4947. if ((intel_panel_use_ssc(dev_priv) &&
  4948. dev_priv->lvds_ssc_freq == 100) ||
  4949. (I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP)
  4950. factor = 25;
  4951. } else if (is_sdvo && is_tv)
  4952. factor = 20;
  4953. if (clock.m < factor * clock.n)
  4954. fp |= FP_CB_TUNE;
  4955. dpll = 0;
  4956. if (is_lvds)
  4957. dpll |= DPLLB_MODE_LVDS;
  4958. else
  4959. dpll |= DPLLB_MODE_DAC_SERIAL;
  4960. if (is_sdvo) {
  4961. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  4962. if (pixel_multiplier > 1) {
  4963. dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  4964. }
  4965. dpll |= DPLL_DVO_HIGH_SPEED;
  4966. }
  4967. if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
  4968. dpll |= DPLL_DVO_HIGH_SPEED;
  4969. /* compute bitmask from p1 value */
  4970. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4971. /* also FPA1 */
  4972. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  4973. switch (clock.p2) {
  4974. case 5:
  4975. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  4976. break;
  4977. case 7:
  4978. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  4979. break;
  4980. case 10:
  4981. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  4982. break;
  4983. case 14:
  4984. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  4985. break;
  4986. }
  4987. if (is_sdvo && is_tv)
  4988. dpll |= PLL_REF_INPUT_TVCLKINBC;
  4989. else if (is_tv)
  4990. /* XXX: just matching BIOS for now */
  4991. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  4992. dpll |= 3;
  4993. else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4994. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4995. else
  4996. dpll |= PLL_REF_INPUT_DREFCLK;
  4997. /* setup pipeconf */
  4998. pipeconf = I915_READ(PIPECONF(pipe));
  4999. /* Set up the display plane register */
  5000. dspcntr = DISPPLANE_GAMMA_ENABLE;
  5001. DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
  5002. drm_mode_debug_printmodeline(mode);
  5003. /* PCH eDP needs FDI, but CPU eDP does not */
  5004. if (!intel_crtc->no_pll) {
  5005. if (!has_edp_encoder ||
  5006. intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  5007. I915_WRITE(PCH_FP0(pipe), fp);
  5008. I915_WRITE(PCH_DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
  5009. POSTING_READ(PCH_DPLL(pipe));
  5010. udelay(150);
  5011. }
  5012. } else {
  5013. if (dpll == (I915_READ(PCH_DPLL(0)) & 0x7fffffff) &&
  5014. fp == I915_READ(PCH_FP0(0))) {
  5015. intel_crtc->use_pll_a = true;
  5016. DRM_DEBUG_KMS("using pipe a dpll\n");
  5017. } else if (dpll == (I915_READ(PCH_DPLL(1)) & 0x7fffffff) &&
  5018. fp == I915_READ(PCH_FP0(1))) {
  5019. intel_crtc->use_pll_a = false;
  5020. DRM_DEBUG_KMS("using pipe b dpll\n");
  5021. } else {
  5022. DRM_DEBUG_KMS("no matching PLL configuration for pipe 2\n");
  5023. return -EINVAL;
  5024. }
  5025. }
  5026. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  5027. * This is an exception to the general rule that mode_set doesn't turn
  5028. * things on.
  5029. */
  5030. if (is_lvds) {
  5031. temp = I915_READ(PCH_LVDS);
  5032. temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  5033. if (HAS_PCH_CPT(dev)) {
  5034. temp &= ~PORT_TRANS_SEL_MASK;
  5035. temp |= PORT_TRANS_SEL_CPT(pipe);
  5036. } else {
  5037. if (pipe == 1)
  5038. temp |= LVDS_PIPEB_SELECT;
  5039. else
  5040. temp &= ~LVDS_PIPEB_SELECT;
  5041. }
  5042. /* set the corresponsding LVDS_BORDER bit */
  5043. temp |= dev_priv->lvds_border_bits;
  5044. /* Set the B0-B3 data pairs corresponding to whether we're going to
  5045. * set the DPLLs for dual-channel mode or not.
  5046. */
  5047. if (clock.p2 == 7)
  5048. temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  5049. else
  5050. temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  5051. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  5052. * appropriately here, but we need to look more thoroughly into how
  5053. * panels behave in the two modes.
  5054. */
  5055. if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
  5056. lvds_sync |= LVDS_HSYNC_POLARITY;
  5057. if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
  5058. lvds_sync |= LVDS_VSYNC_POLARITY;
  5059. if ((temp & (LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY))
  5060. != lvds_sync) {
  5061. char flags[2] = "-+";
  5062. DRM_INFO("Changing LVDS panel from "
  5063. "(%chsync, %cvsync) to (%chsync, %cvsync)\n",
  5064. flags[!(temp & LVDS_HSYNC_POLARITY)],
  5065. flags[!(temp & LVDS_VSYNC_POLARITY)],
  5066. flags[!(lvds_sync & LVDS_HSYNC_POLARITY)],
  5067. flags[!(lvds_sync & LVDS_VSYNC_POLARITY)]);
  5068. temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
  5069. temp |= lvds_sync;
  5070. }
  5071. I915_WRITE(PCH_LVDS, temp);
  5072. }
  5073. pipeconf &= ~PIPECONF_DITHER_EN;
  5074. pipeconf &= ~PIPECONF_DITHER_TYPE_MASK;
  5075. if ((is_lvds && dev_priv->lvds_dither) || dither) {
  5076. pipeconf |= PIPECONF_DITHER_EN;
  5077. pipeconf |= PIPECONF_DITHER_TYPE_SP;
  5078. }
  5079. if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  5080. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  5081. } else {
  5082. /* For non-DP output, clear any trans DP clock recovery setting.*/
  5083. I915_WRITE(TRANSDATA_M1(pipe), 0);
  5084. I915_WRITE(TRANSDATA_N1(pipe), 0);
  5085. I915_WRITE(TRANSDPLINK_M1(pipe), 0);
  5086. I915_WRITE(TRANSDPLINK_N1(pipe), 0);
  5087. }
  5088. if (!intel_crtc->no_pll &&
  5089. (!has_edp_encoder ||
  5090. intel_encoder_is_pch_edp(&has_edp_encoder->base))) {
  5091. I915_WRITE(PCH_DPLL(pipe), dpll);
  5092. /* Wait for the clocks to stabilize. */
  5093. POSTING_READ(PCH_DPLL(pipe));
  5094. udelay(150);
  5095. /* The pixel multiplier can only be updated once the
  5096. * DPLL is enabled and the clocks are stable.
  5097. *
  5098. * So write it again.
  5099. */
  5100. I915_WRITE(PCH_DPLL(pipe), dpll);
  5101. }
  5102. intel_crtc->lowfreq_avail = false;
  5103. if (!intel_crtc->no_pll) {
  5104. if (is_lvds && has_reduced_clock && i915_powersave) {
  5105. I915_WRITE(PCH_FP1(pipe), fp2);
  5106. intel_crtc->lowfreq_avail = true;
  5107. if (HAS_PIPE_CXSR(dev)) {
  5108. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  5109. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  5110. }
  5111. } else {
  5112. I915_WRITE(PCH_FP1(pipe), fp);
  5113. if (HAS_PIPE_CXSR(dev)) {
  5114. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  5115. pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
  5116. }
  5117. }
  5118. }
  5119. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5120. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  5121. /* the chip adds 2 halflines automatically */
  5122. adjusted_mode->crtc_vdisplay -= 1;
  5123. adjusted_mode->crtc_vtotal -= 1;
  5124. adjusted_mode->crtc_vblank_start -= 1;
  5125. adjusted_mode->crtc_vblank_end -= 1;
  5126. adjusted_mode->crtc_vsync_end -= 1;
  5127. adjusted_mode->crtc_vsync_start -= 1;
  5128. } else
  5129. pipeconf &= ~PIPECONF_INTERLACE_W_FIELD_INDICATION; /* progressive */
  5130. I915_WRITE(HTOTAL(pipe),
  5131. (adjusted_mode->crtc_hdisplay - 1) |
  5132. ((adjusted_mode->crtc_htotal - 1) << 16));
  5133. I915_WRITE(HBLANK(pipe),
  5134. (adjusted_mode->crtc_hblank_start - 1) |
  5135. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5136. I915_WRITE(HSYNC(pipe),
  5137. (adjusted_mode->crtc_hsync_start - 1) |
  5138. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5139. I915_WRITE(VTOTAL(pipe),
  5140. (adjusted_mode->crtc_vdisplay - 1) |
  5141. ((adjusted_mode->crtc_vtotal - 1) << 16));
  5142. I915_WRITE(VBLANK(pipe),
  5143. (adjusted_mode->crtc_vblank_start - 1) |
  5144. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  5145. I915_WRITE(VSYNC(pipe),
  5146. (adjusted_mode->crtc_vsync_start - 1) |
  5147. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5148. /* pipesrc controls the size that is scaled from, which should
  5149. * always be the user's requested size.
  5150. */
  5151. I915_WRITE(PIPESRC(pipe),
  5152. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  5153. I915_WRITE(PIPE_DATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);
  5154. I915_WRITE(PIPE_DATA_N1(pipe), m_n.gmch_n);
  5155. I915_WRITE(PIPE_LINK_M1(pipe), m_n.link_m);
  5156. I915_WRITE(PIPE_LINK_N1(pipe), m_n.link_n);
  5157. if (has_edp_encoder &&
  5158. !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  5159. ironlake_set_pll_edp(crtc, adjusted_mode->clock);
  5160. }
  5161. I915_WRITE(PIPECONF(pipe), pipeconf);
  5162. POSTING_READ(PIPECONF(pipe));
  5163. intel_wait_for_vblank(dev, pipe);
  5164. if (IS_GEN5(dev)) {
  5165. /* enable address swizzle for tiling buffer */
  5166. temp = I915_READ(DISP_ARB_CTL);
  5167. I915_WRITE(DISP_ARB_CTL, temp | DISP_TILE_SURFACE_SWIZZLING);
  5168. }
  5169. I915_WRITE(DSPCNTR(plane), dspcntr);
  5170. POSTING_READ(DSPCNTR(plane));
  5171. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  5172. intel_update_watermarks(dev);
  5173. return ret;
  5174. }
  5175. static int intel_crtc_mode_set(struct drm_crtc *crtc,
  5176. struct drm_display_mode *mode,
  5177. struct drm_display_mode *adjusted_mode,
  5178. int x, int y,
  5179. struct drm_framebuffer *old_fb)
  5180. {
  5181. struct drm_device *dev = crtc->dev;
  5182. struct drm_i915_private *dev_priv = dev->dev_private;
  5183. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5184. int pipe = intel_crtc->pipe;
  5185. int ret;
  5186. drm_vblank_pre_modeset(dev, pipe);
  5187. ret = dev_priv->display.crtc_mode_set(crtc, mode, adjusted_mode,
  5188. x, y, old_fb);
  5189. drm_vblank_post_modeset(dev, pipe);
  5190. if (ret)
  5191. intel_crtc->dpms_mode = DRM_MODE_DPMS_OFF;
  5192. else
  5193. intel_crtc->dpms_mode = DRM_MODE_DPMS_ON;
  5194. return ret;
  5195. }
  5196. static bool intel_eld_uptodate(struct drm_connector *connector,
  5197. int reg_eldv, uint32_t bits_eldv,
  5198. int reg_elda, uint32_t bits_elda,
  5199. int reg_edid)
  5200. {
  5201. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  5202. uint8_t *eld = connector->eld;
  5203. uint32_t i;
  5204. i = I915_READ(reg_eldv);
  5205. i &= bits_eldv;
  5206. if (!eld[0])
  5207. return !i;
  5208. if (!i)
  5209. return false;
  5210. i = I915_READ(reg_elda);
  5211. i &= ~bits_elda;
  5212. I915_WRITE(reg_elda, i);
  5213. for (i = 0; i < eld[2]; i++)
  5214. if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
  5215. return false;
  5216. return true;
  5217. }
  5218. static void g4x_write_eld(struct drm_connector *connector,
  5219. struct drm_crtc *crtc)
  5220. {
  5221. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  5222. uint8_t *eld = connector->eld;
  5223. uint32_t eldv;
  5224. uint32_t len;
  5225. uint32_t i;
  5226. i = I915_READ(G4X_AUD_VID_DID);
  5227. if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
  5228. eldv = G4X_ELDV_DEVCL_DEVBLC;
  5229. else
  5230. eldv = G4X_ELDV_DEVCTG;
  5231. if (intel_eld_uptodate(connector,
  5232. G4X_AUD_CNTL_ST, eldv,
  5233. G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
  5234. G4X_HDMIW_HDMIEDID))
  5235. return;
  5236. i = I915_READ(G4X_AUD_CNTL_ST);
  5237. i &= ~(eldv | G4X_ELD_ADDR);
  5238. len = (i >> 9) & 0x1f; /* ELD buffer size */
  5239. I915_WRITE(G4X_AUD_CNTL_ST, i);
  5240. if (!eld[0])
  5241. return;
  5242. len = min_t(uint8_t, eld[2], len);
  5243. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  5244. for (i = 0; i < len; i++)
  5245. I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
  5246. i = I915_READ(G4X_AUD_CNTL_ST);
  5247. i |= eldv;
  5248. I915_WRITE(G4X_AUD_CNTL_ST, i);
  5249. }
  5250. static void ironlake_write_eld(struct drm_connector *connector,
  5251. struct drm_crtc *crtc)
  5252. {
  5253. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  5254. uint8_t *eld = connector->eld;
  5255. uint32_t eldv;
  5256. uint32_t i;
  5257. int len;
  5258. int hdmiw_hdmiedid;
  5259. int aud_cntl_st;
  5260. int aud_cntrl_st2;
  5261. if (HAS_PCH_IBX(connector->dev)) {
  5262. hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID_A;
  5263. aud_cntl_st = IBX_AUD_CNTL_ST_A;
  5264. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  5265. } else {
  5266. hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID_A;
  5267. aud_cntl_st = CPT_AUD_CNTL_ST_A;
  5268. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  5269. }
  5270. i = to_intel_crtc(crtc)->pipe;
  5271. hdmiw_hdmiedid += i * 0x100;
  5272. aud_cntl_st += i * 0x100;
  5273. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(i));
  5274. i = I915_READ(aud_cntl_st);
  5275. i = (i >> 29) & 0x3; /* DIP_Port_Select, 0x1 = PortB */
  5276. if (!i) {
  5277. DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
  5278. /* operate blindly on all ports */
  5279. eldv = IBX_ELD_VALIDB;
  5280. eldv |= IBX_ELD_VALIDB << 4;
  5281. eldv |= IBX_ELD_VALIDB << 8;
  5282. } else {
  5283. DRM_DEBUG_DRIVER("ELD on port %c\n", 'A' + i);
  5284. eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
  5285. }
  5286. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  5287. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  5288. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  5289. }
  5290. if (intel_eld_uptodate(connector,
  5291. aud_cntrl_st2, eldv,
  5292. aud_cntl_st, IBX_ELD_ADDRESS,
  5293. hdmiw_hdmiedid))
  5294. return;
  5295. i = I915_READ(aud_cntrl_st2);
  5296. i &= ~eldv;
  5297. I915_WRITE(aud_cntrl_st2, i);
  5298. if (!eld[0])
  5299. return;
  5300. i = I915_READ(aud_cntl_st);
  5301. i &= ~IBX_ELD_ADDRESS;
  5302. I915_WRITE(aud_cntl_st, i);
  5303. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  5304. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  5305. for (i = 0; i < len; i++)
  5306. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  5307. i = I915_READ(aud_cntrl_st2);
  5308. i |= eldv;
  5309. I915_WRITE(aud_cntrl_st2, i);
  5310. }
  5311. void intel_write_eld(struct drm_encoder *encoder,
  5312. struct drm_display_mode *mode)
  5313. {
  5314. struct drm_crtc *crtc = encoder->crtc;
  5315. struct drm_connector *connector;
  5316. struct drm_device *dev = encoder->dev;
  5317. struct drm_i915_private *dev_priv = dev->dev_private;
  5318. connector = drm_select_eld(encoder, mode);
  5319. if (!connector)
  5320. return;
  5321. DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  5322. connector->base.id,
  5323. drm_get_connector_name(connector),
  5324. connector->encoder->base.id,
  5325. drm_get_encoder_name(connector->encoder));
  5326. connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
  5327. if (dev_priv->display.write_eld)
  5328. dev_priv->display.write_eld(connector, crtc);
  5329. }
  5330. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  5331. void intel_crtc_load_lut(struct drm_crtc *crtc)
  5332. {
  5333. struct drm_device *dev = crtc->dev;
  5334. struct drm_i915_private *dev_priv = dev->dev_private;
  5335. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5336. int palreg = PALETTE(intel_crtc->pipe);
  5337. int i;
  5338. /* The clocks have to be on to load the palette. */
  5339. if (!crtc->enabled)
  5340. return;
  5341. /* use legacy palette for Ironlake */
  5342. if (HAS_PCH_SPLIT(dev))
  5343. palreg = LGC_PALETTE(intel_crtc->pipe);
  5344. for (i = 0; i < 256; i++) {
  5345. I915_WRITE(palreg + 4 * i,
  5346. (intel_crtc->lut_r[i] << 16) |
  5347. (intel_crtc->lut_g[i] << 8) |
  5348. intel_crtc->lut_b[i]);
  5349. }
  5350. }
  5351. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  5352. {
  5353. struct drm_device *dev = crtc->dev;
  5354. struct drm_i915_private *dev_priv = dev->dev_private;
  5355. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5356. bool visible = base != 0;
  5357. u32 cntl;
  5358. if (intel_crtc->cursor_visible == visible)
  5359. return;
  5360. cntl = I915_READ(_CURACNTR);
  5361. if (visible) {
  5362. /* On these chipsets we can only modify the base whilst
  5363. * the cursor is disabled.
  5364. */
  5365. I915_WRITE(_CURABASE, base);
  5366. cntl &= ~(CURSOR_FORMAT_MASK);
  5367. /* XXX width must be 64, stride 256 => 0x00 << 28 */
  5368. cntl |= CURSOR_ENABLE |
  5369. CURSOR_GAMMA_ENABLE |
  5370. CURSOR_FORMAT_ARGB;
  5371. } else
  5372. cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
  5373. I915_WRITE(_CURACNTR, cntl);
  5374. intel_crtc->cursor_visible = visible;
  5375. }
  5376. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  5377. {
  5378. struct drm_device *dev = crtc->dev;
  5379. struct drm_i915_private *dev_priv = dev->dev_private;
  5380. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5381. int pipe = intel_crtc->pipe;
  5382. bool visible = base != 0;
  5383. if (intel_crtc->cursor_visible != visible) {
  5384. uint32_t cntl = I915_READ(CURCNTR(pipe));
  5385. if (base) {
  5386. cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
  5387. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  5388. cntl |= pipe << 28; /* Connect to correct pipe */
  5389. } else {
  5390. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  5391. cntl |= CURSOR_MODE_DISABLE;
  5392. }
  5393. I915_WRITE(CURCNTR(pipe), cntl);
  5394. intel_crtc->cursor_visible = visible;
  5395. }
  5396. /* and commit changes on next vblank */
  5397. I915_WRITE(CURBASE(pipe), base);
  5398. }
  5399. static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
  5400. {
  5401. struct drm_device *dev = crtc->dev;
  5402. struct drm_i915_private *dev_priv = dev->dev_private;
  5403. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5404. int pipe = intel_crtc->pipe;
  5405. bool visible = base != 0;
  5406. if (intel_crtc->cursor_visible != visible) {
  5407. uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
  5408. if (base) {
  5409. cntl &= ~CURSOR_MODE;
  5410. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  5411. } else {
  5412. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  5413. cntl |= CURSOR_MODE_DISABLE;
  5414. }
  5415. I915_WRITE(CURCNTR_IVB(pipe), cntl);
  5416. intel_crtc->cursor_visible = visible;
  5417. }
  5418. /* and commit changes on next vblank */
  5419. I915_WRITE(CURBASE_IVB(pipe), base);
  5420. }
  5421. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  5422. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  5423. bool on)
  5424. {
  5425. struct drm_device *dev = crtc->dev;
  5426. struct drm_i915_private *dev_priv = dev->dev_private;
  5427. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5428. int pipe = intel_crtc->pipe;
  5429. int x = intel_crtc->cursor_x;
  5430. int y = intel_crtc->cursor_y;
  5431. u32 base, pos;
  5432. bool visible;
  5433. pos = 0;
  5434. if (on && crtc->enabled && crtc->fb) {
  5435. base = intel_crtc->cursor_addr;
  5436. if (x > (int) crtc->fb->width)
  5437. base = 0;
  5438. if (y > (int) crtc->fb->height)
  5439. base = 0;
  5440. } else
  5441. base = 0;
  5442. if (x < 0) {
  5443. if (x + intel_crtc->cursor_width < 0)
  5444. base = 0;
  5445. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  5446. x = -x;
  5447. }
  5448. pos |= x << CURSOR_X_SHIFT;
  5449. if (y < 0) {
  5450. if (y + intel_crtc->cursor_height < 0)
  5451. base = 0;
  5452. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  5453. y = -y;
  5454. }
  5455. pos |= y << CURSOR_Y_SHIFT;
  5456. visible = base != 0;
  5457. if (!visible && !intel_crtc->cursor_visible)
  5458. return;
  5459. if (IS_IVYBRIDGE(dev)) {
  5460. I915_WRITE(CURPOS_IVB(pipe), pos);
  5461. ivb_update_cursor(crtc, base);
  5462. } else {
  5463. I915_WRITE(CURPOS(pipe), pos);
  5464. if (IS_845G(dev) || IS_I865G(dev))
  5465. i845_update_cursor(crtc, base);
  5466. else
  5467. i9xx_update_cursor(crtc, base);
  5468. }
  5469. if (visible)
  5470. intel_mark_busy(dev, to_intel_framebuffer(crtc->fb)->obj);
  5471. }
  5472. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  5473. struct drm_file *file,
  5474. uint32_t handle,
  5475. uint32_t width, uint32_t height)
  5476. {
  5477. struct drm_device *dev = crtc->dev;
  5478. struct drm_i915_private *dev_priv = dev->dev_private;
  5479. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5480. struct drm_i915_gem_object *obj;
  5481. uint32_t addr;
  5482. int ret;
  5483. DRM_DEBUG_KMS("\n");
  5484. /* if we want to turn off the cursor ignore width and height */
  5485. if (!handle) {
  5486. DRM_DEBUG_KMS("cursor off\n");
  5487. addr = 0;
  5488. obj = NULL;
  5489. mutex_lock(&dev->struct_mutex);
  5490. goto finish;
  5491. }
  5492. /* Currently we only support 64x64 cursors */
  5493. if (width != 64 || height != 64) {
  5494. DRM_ERROR("we currently only support 64x64 cursors\n");
  5495. return -EINVAL;
  5496. }
  5497. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  5498. if (&obj->base == NULL)
  5499. return -ENOENT;
  5500. if (obj->base.size < width * height * 4) {
  5501. DRM_ERROR("buffer is to small\n");
  5502. ret = -ENOMEM;
  5503. goto fail;
  5504. }
  5505. /* we only need to pin inside GTT if cursor is non-phy */
  5506. mutex_lock(&dev->struct_mutex);
  5507. if (!dev_priv->info->cursor_needs_physical) {
  5508. if (obj->tiling_mode) {
  5509. DRM_ERROR("cursor cannot be tiled\n");
  5510. ret = -EINVAL;
  5511. goto fail_locked;
  5512. }
  5513. ret = i915_gem_object_pin_to_display_plane(obj, 0, NULL);
  5514. if (ret) {
  5515. DRM_ERROR("failed to move cursor bo into the GTT\n");
  5516. goto fail_locked;
  5517. }
  5518. ret = i915_gem_object_put_fence(obj);
  5519. if (ret) {
  5520. DRM_ERROR("failed to release fence for cursor");
  5521. goto fail_unpin;
  5522. }
  5523. addr = obj->gtt_offset;
  5524. } else {
  5525. int align = IS_I830(dev) ? 16 * 1024 : 256;
  5526. ret = i915_gem_attach_phys_object(dev, obj,
  5527. (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
  5528. align);
  5529. if (ret) {
  5530. DRM_ERROR("failed to attach phys object\n");
  5531. goto fail_locked;
  5532. }
  5533. addr = obj->phys_obj->handle->busaddr;
  5534. }
  5535. if (IS_GEN2(dev))
  5536. I915_WRITE(CURSIZE, (height << 12) | width);
  5537. finish:
  5538. if (intel_crtc->cursor_bo) {
  5539. if (dev_priv->info->cursor_needs_physical) {
  5540. if (intel_crtc->cursor_bo != obj)
  5541. i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
  5542. } else
  5543. i915_gem_object_unpin(intel_crtc->cursor_bo);
  5544. drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
  5545. }
  5546. mutex_unlock(&dev->struct_mutex);
  5547. intel_crtc->cursor_addr = addr;
  5548. intel_crtc->cursor_bo = obj;
  5549. intel_crtc->cursor_width = width;
  5550. intel_crtc->cursor_height = height;
  5551. intel_crtc_update_cursor(crtc, true);
  5552. return 0;
  5553. fail_unpin:
  5554. i915_gem_object_unpin(obj);
  5555. fail_locked:
  5556. mutex_unlock(&dev->struct_mutex);
  5557. fail:
  5558. drm_gem_object_unreference_unlocked(&obj->base);
  5559. return ret;
  5560. }
  5561. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  5562. {
  5563. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5564. intel_crtc->cursor_x = x;
  5565. intel_crtc->cursor_y = y;
  5566. intel_crtc_update_cursor(crtc, true);
  5567. return 0;
  5568. }
  5569. /** Sets the color ramps on behalf of RandR */
  5570. void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  5571. u16 blue, int regno)
  5572. {
  5573. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5574. intel_crtc->lut_r[regno] = red >> 8;
  5575. intel_crtc->lut_g[regno] = green >> 8;
  5576. intel_crtc->lut_b[regno] = blue >> 8;
  5577. }
  5578. void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  5579. u16 *blue, int regno)
  5580. {
  5581. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5582. *red = intel_crtc->lut_r[regno] << 8;
  5583. *green = intel_crtc->lut_g[regno] << 8;
  5584. *blue = intel_crtc->lut_b[regno] << 8;
  5585. }
  5586. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  5587. u16 *blue, uint32_t start, uint32_t size)
  5588. {
  5589. int end = (start + size > 256) ? 256 : start + size, i;
  5590. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5591. for (i = start; i < end; i++) {
  5592. intel_crtc->lut_r[i] = red[i] >> 8;
  5593. intel_crtc->lut_g[i] = green[i] >> 8;
  5594. intel_crtc->lut_b[i] = blue[i] >> 8;
  5595. }
  5596. intel_crtc_load_lut(crtc);
  5597. }
  5598. /**
  5599. * Get a pipe with a simple mode set on it for doing load-based monitor
  5600. * detection.
  5601. *
  5602. * It will be up to the load-detect code to adjust the pipe as appropriate for
  5603. * its requirements. The pipe will be connected to no other encoders.
  5604. *
  5605. * Currently this code will only succeed if there is a pipe with no encoders
  5606. * configured for it. In the future, it could choose to temporarily disable
  5607. * some outputs to free up a pipe for its use.
  5608. *
  5609. * \return crtc, or NULL if no pipes are available.
  5610. */
  5611. /* VESA 640x480x72Hz mode to set on the pipe */
  5612. static struct drm_display_mode load_detect_mode = {
  5613. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  5614. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  5615. };
  5616. static struct drm_framebuffer *
  5617. intel_framebuffer_create(struct drm_device *dev,
  5618. struct drm_mode_fb_cmd2 *mode_cmd,
  5619. struct drm_i915_gem_object *obj)
  5620. {
  5621. struct intel_framebuffer *intel_fb;
  5622. int ret;
  5623. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  5624. if (!intel_fb) {
  5625. drm_gem_object_unreference_unlocked(&obj->base);
  5626. return ERR_PTR(-ENOMEM);
  5627. }
  5628. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  5629. if (ret) {
  5630. drm_gem_object_unreference_unlocked(&obj->base);
  5631. kfree(intel_fb);
  5632. return ERR_PTR(ret);
  5633. }
  5634. return &intel_fb->base;
  5635. }
  5636. static u32
  5637. intel_framebuffer_pitch_for_width(int width, int bpp)
  5638. {
  5639. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  5640. return ALIGN(pitch, 64);
  5641. }
  5642. static u32
  5643. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  5644. {
  5645. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  5646. return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
  5647. }
  5648. static struct drm_framebuffer *
  5649. intel_framebuffer_create_for_mode(struct drm_device *dev,
  5650. struct drm_display_mode *mode,
  5651. int depth, int bpp)
  5652. {
  5653. struct drm_i915_gem_object *obj;
  5654. struct drm_mode_fb_cmd2 mode_cmd;
  5655. obj = i915_gem_alloc_object(dev,
  5656. intel_framebuffer_size_for_mode(mode, bpp));
  5657. if (obj == NULL)
  5658. return ERR_PTR(-ENOMEM);
  5659. mode_cmd.width = mode->hdisplay;
  5660. mode_cmd.height = mode->vdisplay;
  5661. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  5662. bpp);
  5663. mode_cmd.pixel_format = 0;
  5664. return intel_framebuffer_create(dev, &mode_cmd, obj);
  5665. }
  5666. static struct drm_framebuffer *
  5667. mode_fits_in_fbdev(struct drm_device *dev,
  5668. struct drm_display_mode *mode)
  5669. {
  5670. struct drm_i915_private *dev_priv = dev->dev_private;
  5671. struct drm_i915_gem_object *obj;
  5672. struct drm_framebuffer *fb;
  5673. if (dev_priv->fbdev == NULL)
  5674. return NULL;
  5675. obj = dev_priv->fbdev->ifb.obj;
  5676. if (obj == NULL)
  5677. return NULL;
  5678. fb = &dev_priv->fbdev->ifb.base;
  5679. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  5680. fb->bits_per_pixel))
  5681. return NULL;
  5682. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  5683. return NULL;
  5684. return fb;
  5685. }
  5686. bool intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,
  5687. struct drm_connector *connector,
  5688. struct drm_display_mode *mode,
  5689. struct intel_load_detect_pipe *old)
  5690. {
  5691. struct intel_crtc *intel_crtc;
  5692. struct drm_crtc *possible_crtc;
  5693. struct drm_encoder *encoder = &intel_encoder->base;
  5694. struct drm_crtc *crtc = NULL;
  5695. struct drm_device *dev = encoder->dev;
  5696. struct drm_framebuffer *old_fb;
  5697. int i = -1;
  5698. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  5699. connector->base.id, drm_get_connector_name(connector),
  5700. encoder->base.id, drm_get_encoder_name(encoder));
  5701. /*
  5702. * Algorithm gets a little messy:
  5703. *
  5704. * - if the connector already has an assigned crtc, use it (but make
  5705. * sure it's on first)
  5706. *
  5707. * - try to find the first unused crtc that can drive this connector,
  5708. * and use that if we find one
  5709. */
  5710. /* See if we already have a CRTC for this connector */
  5711. if (encoder->crtc) {
  5712. crtc = encoder->crtc;
  5713. intel_crtc = to_intel_crtc(crtc);
  5714. old->dpms_mode = intel_crtc->dpms_mode;
  5715. old->load_detect_temp = false;
  5716. /* Make sure the crtc and connector are running */
  5717. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  5718. struct drm_encoder_helper_funcs *encoder_funcs;
  5719. struct drm_crtc_helper_funcs *crtc_funcs;
  5720. crtc_funcs = crtc->helper_private;
  5721. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  5722. encoder_funcs = encoder->helper_private;
  5723. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  5724. }
  5725. return true;
  5726. }
  5727. /* Find an unused one (if possible) */
  5728. list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
  5729. i++;
  5730. if (!(encoder->possible_crtcs & (1 << i)))
  5731. continue;
  5732. if (!possible_crtc->enabled) {
  5733. crtc = possible_crtc;
  5734. break;
  5735. }
  5736. }
  5737. /*
  5738. * If we didn't find an unused CRTC, don't use any.
  5739. */
  5740. if (!crtc) {
  5741. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  5742. return false;
  5743. }
  5744. encoder->crtc = crtc;
  5745. connector->encoder = encoder;
  5746. intel_crtc = to_intel_crtc(crtc);
  5747. old->dpms_mode = intel_crtc->dpms_mode;
  5748. old->load_detect_temp = true;
  5749. old->release_fb = NULL;
  5750. if (!mode)
  5751. mode = &load_detect_mode;
  5752. old_fb = crtc->fb;
  5753. /* We need a framebuffer large enough to accommodate all accesses
  5754. * that the plane may generate whilst we perform load detection.
  5755. * We can not rely on the fbcon either being present (we get called
  5756. * during its initialisation to detect all boot displays, or it may
  5757. * not even exist) or that it is large enough to satisfy the
  5758. * requested mode.
  5759. */
  5760. crtc->fb = mode_fits_in_fbdev(dev, mode);
  5761. if (crtc->fb == NULL) {
  5762. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  5763. crtc->fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  5764. old->release_fb = crtc->fb;
  5765. } else
  5766. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  5767. if (IS_ERR(crtc->fb)) {
  5768. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  5769. crtc->fb = old_fb;
  5770. return false;
  5771. }
  5772. if (!drm_crtc_helper_set_mode(crtc, mode, 0, 0, old_fb)) {
  5773. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  5774. if (old->release_fb)
  5775. old->release_fb->funcs->destroy(old->release_fb);
  5776. crtc->fb = old_fb;
  5777. return false;
  5778. }
  5779. /* let the connector get through one full cycle before testing */
  5780. intel_wait_for_vblank(dev, intel_crtc->pipe);
  5781. return true;
  5782. }
  5783. void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,
  5784. struct drm_connector *connector,
  5785. struct intel_load_detect_pipe *old)
  5786. {
  5787. struct drm_encoder *encoder = &intel_encoder->base;
  5788. struct drm_device *dev = encoder->dev;
  5789. struct drm_crtc *crtc = encoder->crtc;
  5790. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  5791. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  5792. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  5793. connector->base.id, drm_get_connector_name(connector),
  5794. encoder->base.id, drm_get_encoder_name(encoder));
  5795. if (old->load_detect_temp) {
  5796. connector->encoder = NULL;
  5797. drm_helper_disable_unused_functions(dev);
  5798. if (old->release_fb)
  5799. old->release_fb->funcs->destroy(old->release_fb);
  5800. return;
  5801. }
  5802. /* Switch crtc and encoder back off if necessary */
  5803. if (old->dpms_mode != DRM_MODE_DPMS_ON) {
  5804. encoder_funcs->dpms(encoder, old->dpms_mode);
  5805. crtc_funcs->dpms(crtc, old->dpms_mode);
  5806. }
  5807. }
  5808. /* Returns the clock of the currently programmed mode of the given pipe. */
  5809. static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
  5810. {
  5811. struct drm_i915_private *dev_priv = dev->dev_private;
  5812. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5813. int pipe = intel_crtc->pipe;
  5814. u32 dpll = I915_READ(DPLL(pipe));
  5815. u32 fp;
  5816. intel_clock_t clock;
  5817. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  5818. fp = I915_READ(FP0(pipe));
  5819. else
  5820. fp = I915_READ(FP1(pipe));
  5821. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  5822. if (IS_PINEVIEW(dev)) {
  5823. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  5824. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  5825. } else {
  5826. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  5827. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  5828. }
  5829. if (!IS_GEN2(dev)) {
  5830. if (IS_PINEVIEW(dev))
  5831. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  5832. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  5833. else
  5834. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  5835. DPLL_FPA01_P1_POST_DIV_SHIFT);
  5836. switch (dpll & DPLL_MODE_MASK) {
  5837. case DPLLB_MODE_DAC_SERIAL:
  5838. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  5839. 5 : 10;
  5840. break;
  5841. case DPLLB_MODE_LVDS:
  5842. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  5843. 7 : 14;
  5844. break;
  5845. default:
  5846. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  5847. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  5848. return 0;
  5849. }
  5850. /* XXX: Handle the 100Mhz refclk */
  5851. intel_clock(dev, 96000, &clock);
  5852. } else {
  5853. bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
  5854. if (is_lvds) {
  5855. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  5856. DPLL_FPA01_P1_POST_DIV_SHIFT);
  5857. clock.p2 = 14;
  5858. if ((dpll & PLL_REF_INPUT_MASK) ==
  5859. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  5860. /* XXX: might not be 66MHz */
  5861. intel_clock(dev, 66000, &clock);
  5862. } else
  5863. intel_clock(dev, 48000, &clock);
  5864. } else {
  5865. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  5866. clock.p1 = 2;
  5867. else {
  5868. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  5869. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  5870. }
  5871. if (dpll & PLL_P2_DIVIDE_BY_4)
  5872. clock.p2 = 4;
  5873. else
  5874. clock.p2 = 2;
  5875. intel_clock(dev, 48000, &clock);
  5876. }
  5877. }
  5878. /* XXX: It would be nice to validate the clocks, but we can't reuse
  5879. * i830PllIsValid() because it relies on the xf86_config connector
  5880. * configuration being accurate, which it isn't necessarily.
  5881. */
  5882. return clock.dot;
  5883. }
  5884. /** Returns the currently programmed mode of the given pipe. */
  5885. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  5886. struct drm_crtc *crtc)
  5887. {
  5888. struct drm_i915_private *dev_priv = dev->dev_private;
  5889. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5890. int pipe = intel_crtc->pipe;
  5891. struct drm_display_mode *mode;
  5892. int htot = I915_READ(HTOTAL(pipe));
  5893. int hsync = I915_READ(HSYNC(pipe));
  5894. int vtot = I915_READ(VTOTAL(pipe));
  5895. int vsync = I915_READ(VSYNC(pipe));
  5896. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  5897. if (!mode)
  5898. return NULL;
  5899. mode->clock = intel_crtc_clock_get(dev, crtc);
  5900. mode->hdisplay = (htot & 0xffff) + 1;
  5901. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  5902. mode->hsync_start = (hsync & 0xffff) + 1;
  5903. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  5904. mode->vdisplay = (vtot & 0xffff) + 1;
  5905. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  5906. mode->vsync_start = (vsync & 0xffff) + 1;
  5907. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  5908. drm_mode_set_name(mode);
  5909. drm_mode_set_crtcinfo(mode, 0);
  5910. return mode;
  5911. }
  5912. #define GPU_IDLE_TIMEOUT 500 /* ms */
  5913. /* When this timer fires, we've been idle for awhile */
  5914. static void intel_gpu_idle_timer(unsigned long arg)
  5915. {
  5916. struct drm_device *dev = (struct drm_device *)arg;
  5917. drm_i915_private_t *dev_priv = dev->dev_private;
  5918. if (!list_empty(&dev_priv->mm.active_list)) {
  5919. /* Still processing requests, so just re-arm the timer. */
  5920. mod_timer(&dev_priv->idle_timer, jiffies +
  5921. msecs_to_jiffies(GPU_IDLE_TIMEOUT));
  5922. return;
  5923. }
  5924. dev_priv->busy = false;
  5925. queue_work(dev_priv->wq, &dev_priv->idle_work);
  5926. }
  5927. #define CRTC_IDLE_TIMEOUT 1000 /* ms */
  5928. static void intel_crtc_idle_timer(unsigned long arg)
  5929. {
  5930. struct intel_crtc *intel_crtc = (struct intel_crtc *)arg;
  5931. struct drm_crtc *crtc = &intel_crtc->base;
  5932. drm_i915_private_t *dev_priv = crtc->dev->dev_private;
  5933. struct intel_framebuffer *intel_fb;
  5934. intel_fb = to_intel_framebuffer(crtc->fb);
  5935. if (intel_fb && intel_fb->obj->active) {
  5936. /* The framebuffer is still being accessed by the GPU. */
  5937. mod_timer(&intel_crtc->idle_timer, jiffies +
  5938. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  5939. return;
  5940. }
  5941. intel_crtc->busy = false;
  5942. queue_work(dev_priv->wq, &dev_priv->idle_work);
  5943. }
  5944. static void intel_increase_pllclock(struct drm_crtc *crtc)
  5945. {
  5946. struct drm_device *dev = crtc->dev;
  5947. drm_i915_private_t *dev_priv = dev->dev_private;
  5948. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5949. int pipe = intel_crtc->pipe;
  5950. int dpll_reg = DPLL(pipe);
  5951. int dpll;
  5952. if (HAS_PCH_SPLIT(dev))
  5953. return;
  5954. if (!dev_priv->lvds_downclock_avail)
  5955. return;
  5956. dpll = I915_READ(dpll_reg);
  5957. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  5958. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  5959. /* Unlock panel regs */
  5960. I915_WRITE(PP_CONTROL,
  5961. I915_READ(PP_CONTROL) | PANEL_UNLOCK_REGS);
  5962. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  5963. I915_WRITE(dpll_reg, dpll);
  5964. intel_wait_for_vblank(dev, pipe);
  5965. dpll = I915_READ(dpll_reg);
  5966. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  5967. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  5968. /* ...and lock them again */
  5969. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
  5970. }
  5971. /* Schedule downclock */
  5972. mod_timer(&intel_crtc->idle_timer, jiffies +
  5973. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  5974. }
  5975. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  5976. {
  5977. struct drm_device *dev = crtc->dev;
  5978. drm_i915_private_t *dev_priv = dev->dev_private;
  5979. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5980. int pipe = intel_crtc->pipe;
  5981. int dpll_reg = DPLL(pipe);
  5982. int dpll = I915_READ(dpll_reg);
  5983. if (HAS_PCH_SPLIT(dev))
  5984. return;
  5985. if (!dev_priv->lvds_downclock_avail)
  5986. return;
  5987. /*
  5988. * Since this is called by a timer, we should never get here in
  5989. * the manual case.
  5990. */
  5991. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  5992. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  5993. /* Unlock panel regs */
  5994. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) |
  5995. PANEL_UNLOCK_REGS);
  5996. dpll |= DISPLAY_RATE_SELECT_FPA1;
  5997. I915_WRITE(dpll_reg, dpll);
  5998. intel_wait_for_vblank(dev, pipe);
  5999. dpll = I915_READ(dpll_reg);
  6000. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  6001. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  6002. /* ...and lock them again */
  6003. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
  6004. }
  6005. }
  6006. /**
  6007. * intel_idle_update - adjust clocks for idleness
  6008. * @work: work struct
  6009. *
  6010. * Either the GPU or display (or both) went idle. Check the busy status
  6011. * here and adjust the CRTC and GPU clocks as necessary.
  6012. */
  6013. static void intel_idle_update(struct work_struct *work)
  6014. {
  6015. drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
  6016. idle_work);
  6017. struct drm_device *dev = dev_priv->dev;
  6018. struct drm_crtc *crtc;
  6019. struct intel_crtc *intel_crtc;
  6020. if (!i915_powersave)
  6021. return;
  6022. mutex_lock(&dev->struct_mutex);
  6023. i915_update_gfx_val(dev_priv);
  6024. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  6025. /* Skip inactive CRTCs */
  6026. if (!crtc->fb)
  6027. continue;
  6028. intel_crtc = to_intel_crtc(crtc);
  6029. if (!intel_crtc->busy)
  6030. intel_decrease_pllclock(crtc);
  6031. }
  6032. mutex_unlock(&dev->struct_mutex);
  6033. }
  6034. /**
  6035. * intel_mark_busy - mark the GPU and possibly the display busy
  6036. * @dev: drm device
  6037. * @obj: object we're operating on
  6038. *
  6039. * Callers can use this function to indicate that the GPU is busy processing
  6040. * commands. If @obj matches one of the CRTC objects (i.e. it's a scanout
  6041. * buffer), we'll also mark the display as busy, so we know to increase its
  6042. * clock frequency.
  6043. */
  6044. void intel_mark_busy(struct drm_device *dev, struct drm_i915_gem_object *obj)
  6045. {
  6046. drm_i915_private_t *dev_priv = dev->dev_private;
  6047. struct drm_crtc *crtc = NULL;
  6048. struct intel_framebuffer *intel_fb;
  6049. struct intel_crtc *intel_crtc;
  6050. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  6051. return;
  6052. if (!dev_priv->busy)
  6053. dev_priv->busy = true;
  6054. else
  6055. mod_timer(&dev_priv->idle_timer, jiffies +
  6056. msecs_to_jiffies(GPU_IDLE_TIMEOUT));
  6057. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  6058. if (!crtc->fb)
  6059. continue;
  6060. intel_crtc = to_intel_crtc(crtc);
  6061. intel_fb = to_intel_framebuffer(crtc->fb);
  6062. if (intel_fb->obj == obj) {
  6063. if (!intel_crtc->busy) {
  6064. /* Non-busy -> busy, upclock */
  6065. intel_increase_pllclock(crtc);
  6066. intel_crtc->busy = true;
  6067. } else {
  6068. /* Busy -> busy, put off timer */
  6069. mod_timer(&intel_crtc->idle_timer, jiffies +
  6070. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  6071. }
  6072. }
  6073. }
  6074. }
  6075. static void intel_crtc_destroy(struct drm_crtc *crtc)
  6076. {
  6077. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6078. struct drm_device *dev = crtc->dev;
  6079. struct intel_unpin_work *work;
  6080. unsigned long flags;
  6081. spin_lock_irqsave(&dev->event_lock, flags);
  6082. work = intel_crtc->unpin_work;
  6083. intel_crtc->unpin_work = NULL;
  6084. spin_unlock_irqrestore(&dev->event_lock, flags);
  6085. if (work) {
  6086. cancel_work_sync(&work->work);
  6087. kfree(work);
  6088. }
  6089. drm_crtc_cleanup(crtc);
  6090. kfree(intel_crtc);
  6091. }
  6092. static void intel_unpin_work_fn(struct work_struct *__work)
  6093. {
  6094. struct intel_unpin_work *work =
  6095. container_of(__work, struct intel_unpin_work, work);
  6096. mutex_lock(&work->dev->struct_mutex);
  6097. i915_gem_object_unpin(work->old_fb_obj);
  6098. drm_gem_object_unreference(&work->pending_flip_obj->base);
  6099. drm_gem_object_unreference(&work->old_fb_obj->base);
  6100. intel_update_fbc(work->dev);
  6101. mutex_unlock(&work->dev->struct_mutex);
  6102. kfree(work);
  6103. }
  6104. static void do_intel_finish_page_flip(struct drm_device *dev,
  6105. struct drm_crtc *crtc)
  6106. {
  6107. drm_i915_private_t *dev_priv = dev->dev_private;
  6108. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6109. struct intel_unpin_work *work;
  6110. struct drm_i915_gem_object *obj;
  6111. struct drm_pending_vblank_event *e;
  6112. struct timeval tnow, tvbl;
  6113. unsigned long flags;
  6114. /* Ignore early vblank irqs */
  6115. if (intel_crtc == NULL)
  6116. return;
  6117. do_gettimeofday(&tnow);
  6118. spin_lock_irqsave(&dev->event_lock, flags);
  6119. work = intel_crtc->unpin_work;
  6120. if (work == NULL || !work->pending) {
  6121. spin_unlock_irqrestore(&dev->event_lock, flags);
  6122. return;
  6123. }
  6124. intel_crtc->unpin_work = NULL;
  6125. if (work->event) {
  6126. e = work->event;
  6127. e->event.sequence = drm_vblank_count_and_time(dev, intel_crtc->pipe, &tvbl);
  6128. /* Called before vblank count and timestamps have
  6129. * been updated for the vblank interval of flip
  6130. * completion? Need to increment vblank count and
  6131. * add one videorefresh duration to returned timestamp
  6132. * to account for this. We assume this happened if we
  6133. * get called over 0.9 frame durations after the last
  6134. * timestamped vblank.
  6135. *
  6136. * This calculation can not be used with vrefresh rates
  6137. * below 5Hz (10Hz to be on the safe side) without
  6138. * promoting to 64 integers.
  6139. */
  6140. if (10 * (timeval_to_ns(&tnow) - timeval_to_ns(&tvbl)) >
  6141. 9 * crtc->framedur_ns) {
  6142. e->event.sequence++;
  6143. tvbl = ns_to_timeval(timeval_to_ns(&tvbl) +
  6144. crtc->framedur_ns);
  6145. }
  6146. e->event.tv_sec = tvbl.tv_sec;
  6147. e->event.tv_usec = tvbl.tv_usec;
  6148. list_add_tail(&e->base.link,
  6149. &e->base.file_priv->event_list);
  6150. wake_up_interruptible(&e->base.file_priv->event_wait);
  6151. }
  6152. drm_vblank_put(dev, intel_crtc->pipe);
  6153. spin_unlock_irqrestore(&dev->event_lock, flags);
  6154. obj = work->old_fb_obj;
  6155. atomic_clear_mask(1 << intel_crtc->plane,
  6156. &obj->pending_flip.counter);
  6157. if (atomic_read(&obj->pending_flip) == 0)
  6158. wake_up(&dev_priv->pending_flip_queue);
  6159. schedule_work(&work->work);
  6160. trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
  6161. }
  6162. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  6163. {
  6164. drm_i915_private_t *dev_priv = dev->dev_private;
  6165. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  6166. do_intel_finish_page_flip(dev, crtc);
  6167. }
  6168. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  6169. {
  6170. drm_i915_private_t *dev_priv = dev->dev_private;
  6171. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  6172. do_intel_finish_page_flip(dev, crtc);
  6173. }
  6174. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  6175. {
  6176. drm_i915_private_t *dev_priv = dev->dev_private;
  6177. struct intel_crtc *intel_crtc =
  6178. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  6179. unsigned long flags;
  6180. spin_lock_irqsave(&dev->event_lock, flags);
  6181. if (intel_crtc->unpin_work) {
  6182. if ((++intel_crtc->unpin_work->pending) > 1)
  6183. DRM_ERROR("Prepared flip multiple times\n");
  6184. } else {
  6185. DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
  6186. }
  6187. spin_unlock_irqrestore(&dev->event_lock, flags);
  6188. }
  6189. static int intel_gen2_queue_flip(struct drm_device *dev,
  6190. struct drm_crtc *crtc,
  6191. struct drm_framebuffer *fb,
  6192. struct drm_i915_gem_object *obj)
  6193. {
  6194. struct drm_i915_private *dev_priv = dev->dev_private;
  6195. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6196. unsigned long offset;
  6197. u32 flip_mask;
  6198. int ret;
  6199. ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
  6200. if (ret)
  6201. goto out;
  6202. /* Offset into the new buffer for cases of shared fbs between CRTCs */
  6203. offset = crtc->y * fb->pitches[0] + crtc->x * fb->bits_per_pixel/8;
  6204. ret = BEGIN_LP_RING(6);
  6205. if (ret)
  6206. goto out;
  6207. /* Can't queue multiple flips, so wait for the previous
  6208. * one to finish before executing the next.
  6209. */
  6210. if (intel_crtc->plane)
  6211. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  6212. else
  6213. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  6214. OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
  6215. OUT_RING(MI_NOOP);
  6216. OUT_RING(MI_DISPLAY_FLIP |
  6217. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  6218. OUT_RING(fb->pitches[0]);
  6219. OUT_RING(obj->gtt_offset + offset);
  6220. OUT_RING(MI_NOOP);
  6221. ADVANCE_LP_RING();
  6222. out:
  6223. return ret;
  6224. }
  6225. static int intel_gen3_queue_flip(struct drm_device *dev,
  6226. struct drm_crtc *crtc,
  6227. struct drm_framebuffer *fb,
  6228. struct drm_i915_gem_object *obj)
  6229. {
  6230. struct drm_i915_private *dev_priv = dev->dev_private;
  6231. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6232. unsigned long offset;
  6233. u32 flip_mask;
  6234. int ret;
  6235. ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
  6236. if (ret)
  6237. goto out;
  6238. /* Offset into the new buffer for cases of shared fbs between CRTCs */
  6239. offset = crtc->y * fb->pitches[0] + crtc->x * fb->bits_per_pixel/8;
  6240. ret = BEGIN_LP_RING(6);
  6241. if (ret)
  6242. goto out;
  6243. if (intel_crtc->plane)
  6244. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  6245. else
  6246. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  6247. OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
  6248. OUT_RING(MI_NOOP);
  6249. OUT_RING(MI_DISPLAY_FLIP_I915 |
  6250. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  6251. OUT_RING(fb->pitches[0]);
  6252. OUT_RING(obj->gtt_offset + offset);
  6253. OUT_RING(MI_NOOP);
  6254. ADVANCE_LP_RING();
  6255. out:
  6256. return ret;
  6257. }
  6258. static int intel_gen4_queue_flip(struct drm_device *dev,
  6259. struct drm_crtc *crtc,
  6260. struct drm_framebuffer *fb,
  6261. struct drm_i915_gem_object *obj)
  6262. {
  6263. struct drm_i915_private *dev_priv = dev->dev_private;
  6264. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6265. uint32_t pf, pipesrc;
  6266. int ret;
  6267. ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
  6268. if (ret)
  6269. goto out;
  6270. ret = BEGIN_LP_RING(4);
  6271. if (ret)
  6272. goto out;
  6273. /* i965+ uses the linear or tiled offsets from the
  6274. * Display Registers (which do not change across a page-flip)
  6275. * so we need only reprogram the base address.
  6276. */
  6277. OUT_RING(MI_DISPLAY_FLIP |
  6278. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  6279. OUT_RING(fb->pitches[0]);
  6280. OUT_RING(obj->gtt_offset | obj->tiling_mode);
  6281. /* XXX Enabling the panel-fitter across page-flip is so far
  6282. * untested on non-native modes, so ignore it for now.
  6283. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  6284. */
  6285. pf = 0;
  6286. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  6287. OUT_RING(pf | pipesrc);
  6288. ADVANCE_LP_RING();
  6289. out:
  6290. return ret;
  6291. }
  6292. static int intel_gen6_queue_flip(struct drm_device *dev,
  6293. struct drm_crtc *crtc,
  6294. struct drm_framebuffer *fb,
  6295. struct drm_i915_gem_object *obj)
  6296. {
  6297. struct drm_i915_private *dev_priv = dev->dev_private;
  6298. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6299. uint32_t pf, pipesrc;
  6300. int ret;
  6301. ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
  6302. if (ret)
  6303. goto out;
  6304. ret = BEGIN_LP_RING(4);
  6305. if (ret)
  6306. goto out;
  6307. OUT_RING(MI_DISPLAY_FLIP |
  6308. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  6309. OUT_RING(fb->pitches[0] | obj->tiling_mode);
  6310. OUT_RING(obj->gtt_offset);
  6311. pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  6312. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  6313. OUT_RING(pf | pipesrc);
  6314. ADVANCE_LP_RING();
  6315. out:
  6316. return ret;
  6317. }
  6318. /*
  6319. * On gen7 we currently use the blit ring because (in early silicon at least)
  6320. * the render ring doesn't give us interrpts for page flip completion, which
  6321. * means clients will hang after the first flip is queued. Fortunately the
  6322. * blit ring generates interrupts properly, so use it instead.
  6323. */
  6324. static int intel_gen7_queue_flip(struct drm_device *dev,
  6325. struct drm_crtc *crtc,
  6326. struct drm_framebuffer *fb,
  6327. struct drm_i915_gem_object *obj)
  6328. {
  6329. struct drm_i915_private *dev_priv = dev->dev_private;
  6330. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6331. struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
  6332. int ret;
  6333. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  6334. if (ret)
  6335. goto out;
  6336. ret = intel_ring_begin(ring, 4);
  6337. if (ret)
  6338. goto out;
  6339. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | (intel_crtc->plane << 19));
  6340. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  6341. intel_ring_emit(ring, (obj->gtt_offset));
  6342. intel_ring_emit(ring, (MI_NOOP));
  6343. intel_ring_advance(ring);
  6344. out:
  6345. return ret;
  6346. }
  6347. static int intel_default_queue_flip(struct drm_device *dev,
  6348. struct drm_crtc *crtc,
  6349. struct drm_framebuffer *fb,
  6350. struct drm_i915_gem_object *obj)
  6351. {
  6352. return -ENODEV;
  6353. }
  6354. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  6355. struct drm_framebuffer *fb,
  6356. struct drm_pending_vblank_event *event)
  6357. {
  6358. struct drm_device *dev = crtc->dev;
  6359. struct drm_i915_private *dev_priv = dev->dev_private;
  6360. struct intel_framebuffer *intel_fb;
  6361. struct drm_i915_gem_object *obj;
  6362. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6363. struct intel_unpin_work *work;
  6364. unsigned long flags;
  6365. int ret;
  6366. work = kzalloc(sizeof *work, GFP_KERNEL);
  6367. if (work == NULL)
  6368. return -ENOMEM;
  6369. work->event = event;
  6370. work->dev = crtc->dev;
  6371. intel_fb = to_intel_framebuffer(crtc->fb);
  6372. work->old_fb_obj = intel_fb->obj;
  6373. INIT_WORK(&work->work, intel_unpin_work_fn);
  6374. ret = drm_vblank_get(dev, intel_crtc->pipe);
  6375. if (ret)
  6376. goto free_work;
  6377. /* We borrow the event spin lock for protecting unpin_work */
  6378. spin_lock_irqsave(&dev->event_lock, flags);
  6379. if (intel_crtc->unpin_work) {
  6380. spin_unlock_irqrestore(&dev->event_lock, flags);
  6381. kfree(work);
  6382. drm_vblank_put(dev, intel_crtc->pipe);
  6383. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  6384. return -EBUSY;
  6385. }
  6386. intel_crtc->unpin_work = work;
  6387. spin_unlock_irqrestore(&dev->event_lock, flags);
  6388. intel_fb = to_intel_framebuffer(fb);
  6389. obj = intel_fb->obj;
  6390. mutex_lock(&dev->struct_mutex);
  6391. /* Reference the objects for the scheduled work. */
  6392. drm_gem_object_reference(&work->old_fb_obj->base);
  6393. drm_gem_object_reference(&obj->base);
  6394. crtc->fb = fb;
  6395. work->pending_flip_obj = obj;
  6396. work->enable_stall_check = true;
  6397. /* Block clients from rendering to the new back buffer until
  6398. * the flip occurs and the object is no longer visible.
  6399. */
  6400. atomic_add(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
  6401. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
  6402. if (ret)
  6403. goto cleanup_pending;
  6404. intel_disable_fbc(dev);
  6405. mutex_unlock(&dev->struct_mutex);
  6406. trace_i915_flip_request(intel_crtc->plane, obj);
  6407. return 0;
  6408. cleanup_pending:
  6409. atomic_sub(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
  6410. drm_gem_object_unreference(&work->old_fb_obj->base);
  6411. drm_gem_object_unreference(&obj->base);
  6412. mutex_unlock(&dev->struct_mutex);
  6413. spin_lock_irqsave(&dev->event_lock, flags);
  6414. intel_crtc->unpin_work = NULL;
  6415. spin_unlock_irqrestore(&dev->event_lock, flags);
  6416. drm_vblank_put(dev, intel_crtc->pipe);
  6417. free_work:
  6418. kfree(work);
  6419. return ret;
  6420. }
  6421. static void intel_sanitize_modesetting(struct drm_device *dev,
  6422. int pipe, int plane)
  6423. {
  6424. struct drm_i915_private *dev_priv = dev->dev_private;
  6425. u32 reg, val;
  6426. if (HAS_PCH_SPLIT(dev))
  6427. return;
  6428. /* Who knows what state these registers were left in by the BIOS or
  6429. * grub?
  6430. *
  6431. * If we leave the registers in a conflicting state (e.g. with the
  6432. * display plane reading from the other pipe than the one we intend
  6433. * to use) then when we attempt to teardown the active mode, we will
  6434. * not disable the pipes and planes in the correct order -- leaving
  6435. * a plane reading from a disabled pipe and possibly leading to
  6436. * undefined behaviour.
  6437. */
  6438. reg = DSPCNTR(plane);
  6439. val = I915_READ(reg);
  6440. if ((val & DISPLAY_PLANE_ENABLE) == 0)
  6441. return;
  6442. if (!!(val & DISPPLANE_SEL_PIPE_MASK) == pipe)
  6443. return;
  6444. /* This display plane is active and attached to the other CPU pipe. */
  6445. pipe = !pipe;
  6446. /* Disable the plane and wait for it to stop reading from the pipe. */
  6447. intel_disable_plane(dev_priv, plane, pipe);
  6448. intel_disable_pipe(dev_priv, pipe);
  6449. }
  6450. static void intel_crtc_reset(struct drm_crtc *crtc)
  6451. {
  6452. struct drm_device *dev = crtc->dev;
  6453. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6454. /* Reset flags back to the 'unknown' status so that they
  6455. * will be correctly set on the initial modeset.
  6456. */
  6457. intel_crtc->dpms_mode = -1;
  6458. /* We need to fix up any BIOS configuration that conflicts with
  6459. * our expectations.
  6460. */
  6461. intel_sanitize_modesetting(dev, intel_crtc->pipe, intel_crtc->plane);
  6462. }
  6463. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  6464. .dpms = intel_crtc_dpms,
  6465. .mode_fixup = intel_crtc_mode_fixup,
  6466. .mode_set = intel_crtc_mode_set,
  6467. .mode_set_base = intel_pipe_set_base,
  6468. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  6469. .load_lut = intel_crtc_load_lut,
  6470. .disable = intel_crtc_disable,
  6471. };
  6472. static const struct drm_crtc_funcs intel_crtc_funcs = {
  6473. .reset = intel_crtc_reset,
  6474. .cursor_set = intel_crtc_cursor_set,
  6475. .cursor_move = intel_crtc_cursor_move,
  6476. .gamma_set = intel_crtc_gamma_set,
  6477. .set_config = drm_crtc_helper_set_config,
  6478. .destroy = intel_crtc_destroy,
  6479. .page_flip = intel_crtc_page_flip,
  6480. };
  6481. static void intel_crtc_init(struct drm_device *dev, int pipe)
  6482. {
  6483. drm_i915_private_t *dev_priv = dev->dev_private;
  6484. struct intel_crtc *intel_crtc;
  6485. int i;
  6486. intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  6487. if (intel_crtc == NULL)
  6488. return;
  6489. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  6490. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  6491. for (i = 0; i < 256; i++) {
  6492. intel_crtc->lut_r[i] = i;
  6493. intel_crtc->lut_g[i] = i;
  6494. intel_crtc->lut_b[i] = i;
  6495. }
  6496. /* Swap pipes & planes for FBC on pre-965 */
  6497. intel_crtc->pipe = pipe;
  6498. intel_crtc->plane = pipe;
  6499. if (IS_MOBILE(dev) && IS_GEN3(dev)) {
  6500. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  6501. intel_crtc->plane = !pipe;
  6502. }
  6503. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  6504. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  6505. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  6506. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  6507. intel_crtc_reset(&intel_crtc->base);
  6508. intel_crtc->active = true; /* force the pipe off on setup_init_config */
  6509. intel_crtc->bpp = 24; /* default for pre-Ironlake */
  6510. if (HAS_PCH_SPLIT(dev)) {
  6511. if (pipe == 2 && IS_IVYBRIDGE(dev))
  6512. intel_crtc->no_pll = true;
  6513. intel_helper_funcs.prepare = ironlake_crtc_prepare;
  6514. intel_helper_funcs.commit = ironlake_crtc_commit;
  6515. } else {
  6516. intel_helper_funcs.prepare = i9xx_crtc_prepare;
  6517. intel_helper_funcs.commit = i9xx_crtc_commit;
  6518. }
  6519. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  6520. intel_crtc->busy = false;
  6521. setup_timer(&intel_crtc->idle_timer, intel_crtc_idle_timer,
  6522. (unsigned long)intel_crtc);
  6523. }
  6524. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  6525. struct drm_file *file)
  6526. {
  6527. drm_i915_private_t *dev_priv = dev->dev_private;
  6528. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  6529. struct drm_mode_object *drmmode_obj;
  6530. struct intel_crtc *crtc;
  6531. if (!dev_priv) {
  6532. DRM_ERROR("called with no initialization\n");
  6533. return -EINVAL;
  6534. }
  6535. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  6536. DRM_MODE_OBJECT_CRTC);
  6537. if (!drmmode_obj) {
  6538. DRM_ERROR("no such CRTC id\n");
  6539. return -EINVAL;
  6540. }
  6541. crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
  6542. pipe_from_crtc_id->pipe = crtc->pipe;
  6543. return 0;
  6544. }
  6545. static int intel_encoder_clones(struct drm_device *dev, int type_mask)
  6546. {
  6547. struct intel_encoder *encoder;
  6548. int index_mask = 0;
  6549. int entry = 0;
  6550. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  6551. if (type_mask & encoder->clone_mask)
  6552. index_mask |= (1 << entry);
  6553. entry++;
  6554. }
  6555. return index_mask;
  6556. }
  6557. static bool has_edp_a(struct drm_device *dev)
  6558. {
  6559. struct drm_i915_private *dev_priv = dev->dev_private;
  6560. if (!IS_MOBILE(dev))
  6561. return false;
  6562. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  6563. return false;
  6564. if (IS_GEN5(dev) &&
  6565. (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
  6566. return false;
  6567. return true;
  6568. }
  6569. static void intel_setup_outputs(struct drm_device *dev)
  6570. {
  6571. struct drm_i915_private *dev_priv = dev->dev_private;
  6572. struct intel_encoder *encoder;
  6573. bool dpd_is_edp = false;
  6574. bool has_lvds = false;
  6575. if (IS_MOBILE(dev) && !IS_I830(dev))
  6576. has_lvds = intel_lvds_init(dev);
  6577. if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
  6578. /* disable the panel fitter on everything but LVDS */
  6579. I915_WRITE(PFIT_CONTROL, 0);
  6580. }
  6581. if (HAS_PCH_SPLIT(dev)) {
  6582. dpd_is_edp = intel_dpd_is_edp(dev);
  6583. if (has_edp_a(dev))
  6584. intel_dp_init(dev, DP_A);
  6585. if (dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  6586. intel_dp_init(dev, PCH_DP_D);
  6587. }
  6588. intel_crt_init(dev);
  6589. if (HAS_PCH_SPLIT(dev)) {
  6590. int found;
  6591. if (I915_READ(HDMIB) & PORT_DETECTED) {
  6592. /* PCH SDVOB multiplex with HDMIB */
  6593. found = intel_sdvo_init(dev, PCH_SDVOB);
  6594. if (!found)
  6595. intel_hdmi_init(dev, HDMIB);
  6596. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  6597. intel_dp_init(dev, PCH_DP_B);
  6598. }
  6599. if (I915_READ(HDMIC) & PORT_DETECTED)
  6600. intel_hdmi_init(dev, HDMIC);
  6601. if (I915_READ(HDMID) & PORT_DETECTED)
  6602. intel_hdmi_init(dev, HDMID);
  6603. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  6604. intel_dp_init(dev, PCH_DP_C);
  6605. if (!dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  6606. intel_dp_init(dev, PCH_DP_D);
  6607. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  6608. bool found = false;
  6609. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  6610. DRM_DEBUG_KMS("probing SDVOB\n");
  6611. found = intel_sdvo_init(dev, SDVOB);
  6612. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  6613. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  6614. intel_hdmi_init(dev, SDVOB);
  6615. }
  6616. if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
  6617. DRM_DEBUG_KMS("probing DP_B\n");
  6618. intel_dp_init(dev, DP_B);
  6619. }
  6620. }
  6621. /* Before G4X SDVOC doesn't have its own detect register */
  6622. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  6623. DRM_DEBUG_KMS("probing SDVOC\n");
  6624. found = intel_sdvo_init(dev, SDVOC);
  6625. }
  6626. if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
  6627. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  6628. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  6629. intel_hdmi_init(dev, SDVOC);
  6630. }
  6631. if (SUPPORTS_INTEGRATED_DP(dev)) {
  6632. DRM_DEBUG_KMS("probing DP_C\n");
  6633. intel_dp_init(dev, DP_C);
  6634. }
  6635. }
  6636. if (SUPPORTS_INTEGRATED_DP(dev) &&
  6637. (I915_READ(DP_D) & DP_DETECTED)) {
  6638. DRM_DEBUG_KMS("probing DP_D\n");
  6639. intel_dp_init(dev, DP_D);
  6640. }
  6641. } else if (IS_GEN2(dev))
  6642. intel_dvo_init(dev);
  6643. if (SUPPORTS_TV(dev))
  6644. intel_tv_init(dev);
  6645. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  6646. encoder->base.possible_crtcs = encoder->crtc_mask;
  6647. encoder->base.possible_clones =
  6648. intel_encoder_clones(dev, encoder->clone_mask);
  6649. }
  6650. /* disable all the possible outputs/crtcs before entering KMS mode */
  6651. drm_helper_disable_unused_functions(dev);
  6652. if (HAS_PCH_SPLIT(dev))
  6653. ironlake_init_pch_refclk(dev);
  6654. }
  6655. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  6656. {
  6657. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  6658. drm_framebuffer_cleanup(fb);
  6659. drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
  6660. kfree(intel_fb);
  6661. }
  6662. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  6663. struct drm_file *file,
  6664. unsigned int *handle)
  6665. {
  6666. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  6667. struct drm_i915_gem_object *obj = intel_fb->obj;
  6668. return drm_gem_handle_create(file, &obj->base, handle);
  6669. }
  6670. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  6671. .destroy = intel_user_framebuffer_destroy,
  6672. .create_handle = intel_user_framebuffer_create_handle,
  6673. };
  6674. int intel_framebuffer_init(struct drm_device *dev,
  6675. struct intel_framebuffer *intel_fb,
  6676. struct drm_mode_fb_cmd2 *mode_cmd,
  6677. struct drm_i915_gem_object *obj)
  6678. {
  6679. int ret;
  6680. if (obj->tiling_mode == I915_TILING_Y)
  6681. return -EINVAL;
  6682. if (mode_cmd->pitches[0] & 63)
  6683. return -EINVAL;
  6684. switch (mode_cmd->pixel_format) {
  6685. case DRM_FORMAT_RGB332:
  6686. case DRM_FORMAT_RGB565:
  6687. case DRM_FORMAT_XRGB8888:
  6688. case DRM_FORMAT_ARGB8888:
  6689. case DRM_FORMAT_XRGB2101010:
  6690. case DRM_FORMAT_ARGB2101010:
  6691. /* RGB formats are common across chipsets */
  6692. break;
  6693. case DRM_FORMAT_YUYV:
  6694. case DRM_FORMAT_UYVY:
  6695. case DRM_FORMAT_YVYU:
  6696. case DRM_FORMAT_VYUY:
  6697. break;
  6698. default:
  6699. DRM_ERROR("unsupported pixel format\n");
  6700. return -EINVAL;
  6701. }
  6702. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  6703. if (ret) {
  6704. DRM_ERROR("framebuffer init failed %d\n", ret);
  6705. return ret;
  6706. }
  6707. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  6708. intel_fb->obj = obj;
  6709. return 0;
  6710. }
  6711. static struct drm_framebuffer *
  6712. intel_user_framebuffer_create(struct drm_device *dev,
  6713. struct drm_file *filp,
  6714. struct drm_mode_fb_cmd2 *mode_cmd)
  6715. {
  6716. struct drm_i915_gem_object *obj;
  6717. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  6718. mode_cmd->handles[0]));
  6719. if (&obj->base == NULL)
  6720. return ERR_PTR(-ENOENT);
  6721. return intel_framebuffer_create(dev, mode_cmd, obj);
  6722. }
  6723. static const struct drm_mode_config_funcs intel_mode_funcs = {
  6724. .fb_create = intel_user_framebuffer_create,
  6725. .output_poll_changed = intel_fb_output_poll_changed,
  6726. };
  6727. static struct drm_i915_gem_object *
  6728. intel_alloc_context_page(struct drm_device *dev)
  6729. {
  6730. struct drm_i915_gem_object *ctx;
  6731. int ret;
  6732. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  6733. ctx = i915_gem_alloc_object(dev, 4096);
  6734. if (!ctx) {
  6735. DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
  6736. return NULL;
  6737. }
  6738. ret = i915_gem_object_pin(ctx, 4096, true);
  6739. if (ret) {
  6740. DRM_ERROR("failed to pin power context: %d\n", ret);
  6741. goto err_unref;
  6742. }
  6743. ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
  6744. if (ret) {
  6745. DRM_ERROR("failed to set-domain on power context: %d\n", ret);
  6746. goto err_unpin;
  6747. }
  6748. return ctx;
  6749. err_unpin:
  6750. i915_gem_object_unpin(ctx);
  6751. err_unref:
  6752. drm_gem_object_unreference(&ctx->base);
  6753. mutex_unlock(&dev->struct_mutex);
  6754. return NULL;
  6755. }
  6756. bool ironlake_set_drps(struct drm_device *dev, u8 val)
  6757. {
  6758. struct drm_i915_private *dev_priv = dev->dev_private;
  6759. u16 rgvswctl;
  6760. rgvswctl = I915_READ16(MEMSWCTL);
  6761. if (rgvswctl & MEMCTL_CMD_STS) {
  6762. DRM_DEBUG("gpu busy, RCS change rejected\n");
  6763. return false; /* still busy with another command */
  6764. }
  6765. rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
  6766. (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
  6767. I915_WRITE16(MEMSWCTL, rgvswctl);
  6768. POSTING_READ16(MEMSWCTL);
  6769. rgvswctl |= MEMCTL_CMD_STS;
  6770. I915_WRITE16(MEMSWCTL, rgvswctl);
  6771. return true;
  6772. }
  6773. void ironlake_enable_drps(struct drm_device *dev)
  6774. {
  6775. struct drm_i915_private *dev_priv = dev->dev_private;
  6776. u32 rgvmodectl = I915_READ(MEMMODECTL);
  6777. u8 fmax, fmin, fstart, vstart;
  6778. /* Enable temp reporting */
  6779. I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
  6780. I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
  6781. /* 100ms RC evaluation intervals */
  6782. I915_WRITE(RCUPEI, 100000);
  6783. I915_WRITE(RCDNEI, 100000);
  6784. /* Set max/min thresholds to 90ms and 80ms respectively */
  6785. I915_WRITE(RCBMAXAVG, 90000);
  6786. I915_WRITE(RCBMINAVG, 80000);
  6787. I915_WRITE(MEMIHYST, 1);
  6788. /* Set up min, max, and cur for interrupt handling */
  6789. fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
  6790. fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
  6791. fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
  6792. MEMMODE_FSTART_SHIFT;
  6793. vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
  6794. PXVFREQ_PX_SHIFT;
  6795. dev_priv->fmax = fmax; /* IPS callback will increase this */
  6796. dev_priv->fstart = fstart;
  6797. dev_priv->max_delay = fstart;
  6798. dev_priv->min_delay = fmin;
  6799. dev_priv->cur_delay = fstart;
  6800. DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
  6801. fmax, fmin, fstart);
  6802. I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
  6803. /*
  6804. * Interrupts will be enabled in ironlake_irq_postinstall
  6805. */
  6806. I915_WRITE(VIDSTART, vstart);
  6807. POSTING_READ(VIDSTART);
  6808. rgvmodectl |= MEMMODE_SWMODE_EN;
  6809. I915_WRITE(MEMMODECTL, rgvmodectl);
  6810. if (wait_for((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
  6811. DRM_ERROR("stuck trying to change perf mode\n");
  6812. msleep(1);
  6813. ironlake_set_drps(dev, fstart);
  6814. dev_priv->last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
  6815. I915_READ(0x112e0);
  6816. dev_priv->last_time1 = jiffies_to_msecs(jiffies);
  6817. dev_priv->last_count2 = I915_READ(0x112f4);
  6818. getrawmonotonic(&dev_priv->last_time2);
  6819. }
  6820. void ironlake_disable_drps(struct drm_device *dev)
  6821. {
  6822. struct drm_i915_private *dev_priv = dev->dev_private;
  6823. u16 rgvswctl = I915_READ16(MEMSWCTL);
  6824. /* Ack interrupts, disable EFC interrupt */
  6825. I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
  6826. I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
  6827. I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
  6828. I915_WRITE(DEIIR, DE_PCU_EVENT);
  6829. I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
  6830. /* Go back to the starting frequency */
  6831. ironlake_set_drps(dev, dev_priv->fstart);
  6832. msleep(1);
  6833. rgvswctl |= MEMCTL_CMD_STS;
  6834. I915_WRITE(MEMSWCTL, rgvswctl);
  6835. msleep(1);
  6836. }
  6837. void gen6_set_rps(struct drm_device *dev, u8 val)
  6838. {
  6839. struct drm_i915_private *dev_priv = dev->dev_private;
  6840. u32 swreq;
  6841. swreq = (val & 0x3ff) << 25;
  6842. I915_WRITE(GEN6_RPNSWREQ, swreq);
  6843. }
  6844. void gen6_disable_rps(struct drm_device *dev)
  6845. {
  6846. struct drm_i915_private *dev_priv = dev->dev_private;
  6847. I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
  6848. I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
  6849. I915_WRITE(GEN6_PMIER, 0);
  6850. /* Complete PM interrupt masking here doesn't race with the rps work
  6851. * item again unmasking PM interrupts because that is using a different
  6852. * register (PMIMR) to mask PM interrupts. The only risk is in leaving
  6853. * stale bits in PMIIR and PMIMR which gen6_enable_rps will clean up. */
  6854. spin_lock_irq(&dev_priv->rps_lock);
  6855. dev_priv->pm_iir = 0;
  6856. spin_unlock_irq(&dev_priv->rps_lock);
  6857. I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
  6858. }
  6859. static unsigned long intel_pxfreq(u32 vidfreq)
  6860. {
  6861. unsigned long freq;
  6862. int div = (vidfreq & 0x3f0000) >> 16;
  6863. int post = (vidfreq & 0x3000) >> 12;
  6864. int pre = (vidfreq & 0x7);
  6865. if (!pre)
  6866. return 0;
  6867. freq = ((div * 133333) / ((1<<post) * pre));
  6868. return freq;
  6869. }
  6870. void intel_init_emon(struct drm_device *dev)
  6871. {
  6872. struct drm_i915_private *dev_priv = dev->dev_private;
  6873. u32 lcfuse;
  6874. u8 pxw[16];
  6875. int i;
  6876. /* Disable to program */
  6877. I915_WRITE(ECR, 0);
  6878. POSTING_READ(ECR);
  6879. /* Program energy weights for various events */
  6880. I915_WRITE(SDEW, 0x15040d00);
  6881. I915_WRITE(CSIEW0, 0x007f0000);
  6882. I915_WRITE(CSIEW1, 0x1e220004);
  6883. I915_WRITE(CSIEW2, 0x04000004);
  6884. for (i = 0; i < 5; i++)
  6885. I915_WRITE(PEW + (i * 4), 0);
  6886. for (i = 0; i < 3; i++)
  6887. I915_WRITE(DEW + (i * 4), 0);
  6888. /* Program P-state weights to account for frequency power adjustment */
  6889. for (i = 0; i < 16; i++) {
  6890. u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
  6891. unsigned long freq = intel_pxfreq(pxvidfreq);
  6892. unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
  6893. PXVFREQ_PX_SHIFT;
  6894. unsigned long val;
  6895. val = vid * vid;
  6896. val *= (freq / 1000);
  6897. val *= 255;
  6898. val /= (127*127*900);
  6899. if (val > 0xff)
  6900. DRM_ERROR("bad pxval: %ld\n", val);
  6901. pxw[i] = val;
  6902. }
  6903. /* Render standby states get 0 weight */
  6904. pxw[14] = 0;
  6905. pxw[15] = 0;
  6906. for (i = 0; i < 4; i++) {
  6907. u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
  6908. (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
  6909. I915_WRITE(PXW + (i * 4), val);
  6910. }
  6911. /* Adjust magic regs to magic values (more experimental results) */
  6912. I915_WRITE(OGW0, 0);
  6913. I915_WRITE(OGW1, 0);
  6914. I915_WRITE(EG0, 0x00007f00);
  6915. I915_WRITE(EG1, 0x0000000e);
  6916. I915_WRITE(EG2, 0x000e0000);
  6917. I915_WRITE(EG3, 0x68000300);
  6918. I915_WRITE(EG4, 0x42000000);
  6919. I915_WRITE(EG5, 0x00140031);
  6920. I915_WRITE(EG6, 0);
  6921. I915_WRITE(EG7, 0);
  6922. for (i = 0; i < 8; i++)
  6923. I915_WRITE(PXWL + (i * 4), 0);
  6924. /* Enable PMON + select events */
  6925. I915_WRITE(ECR, 0x80000019);
  6926. lcfuse = I915_READ(LCFUSE02);
  6927. dev_priv->corr = (lcfuse & LCFUSE_HIV_MASK);
  6928. }
  6929. static bool intel_enable_rc6(struct drm_device *dev)
  6930. {
  6931. /*
  6932. * Respect the kernel parameter if it is set
  6933. */
  6934. if (i915_enable_rc6 >= 0)
  6935. return i915_enable_rc6;
  6936. /*
  6937. * Disable RC6 on Ironlake
  6938. */
  6939. if (INTEL_INFO(dev)->gen == 5)
  6940. return 0;
  6941. /*
  6942. * Disable rc6 on Sandybridge
  6943. */
  6944. if (INTEL_INFO(dev)->gen == 6) {
  6945. DRM_DEBUG_DRIVER("Sandybridge: RC6 disabled\n");
  6946. return 0;
  6947. }
  6948. DRM_DEBUG_DRIVER("RC6 enabled\n");
  6949. return 1;
  6950. }
  6951. void gen6_enable_rps(struct drm_i915_private *dev_priv)
  6952. {
  6953. u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
  6954. u32 gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
  6955. u32 pcu_mbox, rc6_mask = 0;
  6956. int cur_freq, min_freq, max_freq;
  6957. int i;
  6958. /* Here begins a magic sequence of register writes to enable
  6959. * auto-downclocking.
  6960. *
  6961. * Perhaps there might be some value in exposing these to
  6962. * userspace...
  6963. */
  6964. I915_WRITE(GEN6_RC_STATE, 0);
  6965. mutex_lock(&dev_priv->dev->struct_mutex);
  6966. gen6_gt_force_wake_get(dev_priv);
  6967. /* disable the counters and set deterministic thresholds */
  6968. I915_WRITE(GEN6_RC_CONTROL, 0);
  6969. I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
  6970. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
  6971. I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
  6972. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
  6973. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
  6974. for (i = 0; i < I915_NUM_RINGS; i++)
  6975. I915_WRITE(RING_MAX_IDLE(dev_priv->ring[i].mmio_base), 10);
  6976. I915_WRITE(GEN6_RC_SLEEP, 0);
  6977. I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
  6978. I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
  6979. I915_WRITE(GEN6_RC6p_THRESHOLD, 100000);
  6980. I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
  6981. if (intel_enable_rc6(dev_priv->dev))
  6982. rc6_mask = GEN6_RC_CTL_RC6p_ENABLE |
  6983. GEN6_RC_CTL_RC6_ENABLE;
  6984. I915_WRITE(GEN6_RC_CONTROL,
  6985. rc6_mask |
  6986. GEN6_RC_CTL_EI_MODE(1) |
  6987. GEN6_RC_CTL_HW_ENABLE);
  6988. I915_WRITE(GEN6_RPNSWREQ,
  6989. GEN6_FREQUENCY(10) |
  6990. GEN6_OFFSET(0) |
  6991. GEN6_AGGRESSIVE_TURBO);
  6992. I915_WRITE(GEN6_RC_VIDEO_FREQ,
  6993. GEN6_FREQUENCY(12));
  6994. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
  6995. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
  6996. 18 << 24 |
  6997. 6 << 16);
  6998. I915_WRITE(GEN6_RP_UP_THRESHOLD, 10000);
  6999. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 1000000);
  7000. I915_WRITE(GEN6_RP_UP_EI, 100000);
  7001. I915_WRITE(GEN6_RP_DOWN_EI, 5000000);
  7002. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  7003. I915_WRITE(GEN6_RP_CONTROL,
  7004. GEN6_RP_MEDIA_TURBO |
  7005. GEN6_RP_MEDIA_HW_MODE |
  7006. GEN6_RP_MEDIA_IS_GFX |
  7007. GEN6_RP_ENABLE |
  7008. GEN6_RP_UP_BUSY_AVG |
  7009. GEN6_RP_DOWN_IDLE_CONT);
  7010. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  7011. 500))
  7012. DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
  7013. I915_WRITE(GEN6_PCODE_DATA, 0);
  7014. I915_WRITE(GEN6_PCODE_MAILBOX,
  7015. GEN6_PCODE_READY |
  7016. GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
  7017. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  7018. 500))
  7019. DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
  7020. min_freq = (rp_state_cap & 0xff0000) >> 16;
  7021. max_freq = rp_state_cap & 0xff;
  7022. cur_freq = (gt_perf_status & 0xff00) >> 8;
  7023. /* Check for overclock support */
  7024. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  7025. 500))
  7026. DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
  7027. I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_READ_OC_PARAMS);
  7028. pcu_mbox = I915_READ(GEN6_PCODE_DATA);
  7029. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  7030. 500))
  7031. DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
  7032. if (pcu_mbox & (1<<31)) { /* OC supported */
  7033. max_freq = pcu_mbox & 0xff;
  7034. DRM_DEBUG_DRIVER("overclocking supported, adjusting frequency max to %dMHz\n", pcu_mbox * 50);
  7035. }
  7036. /* In units of 100MHz */
  7037. dev_priv->max_delay = max_freq;
  7038. dev_priv->min_delay = min_freq;
  7039. dev_priv->cur_delay = cur_freq;
  7040. /* requires MSI enabled */
  7041. I915_WRITE(GEN6_PMIER,
  7042. GEN6_PM_MBOX_EVENT |
  7043. GEN6_PM_THERMAL_EVENT |
  7044. GEN6_PM_RP_DOWN_TIMEOUT |
  7045. GEN6_PM_RP_UP_THRESHOLD |
  7046. GEN6_PM_RP_DOWN_THRESHOLD |
  7047. GEN6_PM_RP_UP_EI_EXPIRED |
  7048. GEN6_PM_RP_DOWN_EI_EXPIRED);
  7049. spin_lock_irq(&dev_priv->rps_lock);
  7050. WARN_ON(dev_priv->pm_iir != 0);
  7051. I915_WRITE(GEN6_PMIMR, 0);
  7052. spin_unlock_irq(&dev_priv->rps_lock);
  7053. /* enable all PM interrupts */
  7054. I915_WRITE(GEN6_PMINTRMSK, 0);
  7055. gen6_gt_force_wake_put(dev_priv);
  7056. mutex_unlock(&dev_priv->dev->struct_mutex);
  7057. }
  7058. void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
  7059. {
  7060. int min_freq = 15;
  7061. int gpu_freq, ia_freq, max_ia_freq;
  7062. int scaling_factor = 180;
  7063. max_ia_freq = cpufreq_quick_get_max(0);
  7064. /*
  7065. * Default to measured freq if none found, PCU will ensure we don't go
  7066. * over
  7067. */
  7068. if (!max_ia_freq)
  7069. max_ia_freq = tsc_khz;
  7070. /* Convert from kHz to MHz */
  7071. max_ia_freq /= 1000;
  7072. mutex_lock(&dev_priv->dev->struct_mutex);
  7073. /*
  7074. * For each potential GPU frequency, load a ring frequency we'd like
  7075. * to use for memory access. We do this by specifying the IA frequency
  7076. * the PCU should use as a reference to determine the ring frequency.
  7077. */
  7078. for (gpu_freq = dev_priv->max_delay; gpu_freq >= dev_priv->min_delay;
  7079. gpu_freq--) {
  7080. int diff = dev_priv->max_delay - gpu_freq;
  7081. /*
  7082. * For GPU frequencies less than 750MHz, just use the lowest
  7083. * ring freq.
  7084. */
  7085. if (gpu_freq < min_freq)
  7086. ia_freq = 800;
  7087. else
  7088. ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
  7089. ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
  7090. I915_WRITE(GEN6_PCODE_DATA,
  7091. (ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT) |
  7092. gpu_freq);
  7093. I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY |
  7094. GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
  7095. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) &
  7096. GEN6_PCODE_READY) == 0, 10)) {
  7097. DRM_ERROR("pcode write of freq table timed out\n");
  7098. continue;
  7099. }
  7100. }
  7101. mutex_unlock(&dev_priv->dev->struct_mutex);
  7102. }
  7103. static void ironlake_init_clock_gating(struct drm_device *dev)
  7104. {
  7105. struct drm_i915_private *dev_priv = dev->dev_private;
  7106. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  7107. /* Required for FBC */
  7108. dspclk_gate |= DPFCUNIT_CLOCK_GATE_DISABLE |
  7109. DPFCRUNIT_CLOCK_GATE_DISABLE |
  7110. DPFDUNIT_CLOCK_GATE_DISABLE;
  7111. /* Required for CxSR */
  7112. dspclk_gate |= DPARBUNIT_CLOCK_GATE_DISABLE;
  7113. I915_WRITE(PCH_3DCGDIS0,
  7114. MARIUNIT_CLOCK_GATE_DISABLE |
  7115. SVSMUNIT_CLOCK_GATE_DISABLE);
  7116. I915_WRITE(PCH_3DCGDIS1,
  7117. VFMUNIT_CLOCK_GATE_DISABLE);
  7118. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  7119. /*
  7120. * According to the spec the following bits should be set in
  7121. * order to enable memory self-refresh
  7122. * The bit 22/21 of 0x42004
  7123. * The bit 5 of 0x42020
  7124. * The bit 15 of 0x45000
  7125. */
  7126. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  7127. (I915_READ(ILK_DISPLAY_CHICKEN2) |
  7128. ILK_DPARB_GATE | ILK_VSDPFD_FULL));
  7129. I915_WRITE(ILK_DSPCLK_GATE,
  7130. (I915_READ(ILK_DSPCLK_GATE) |
  7131. ILK_DPARB_CLK_GATE));
  7132. I915_WRITE(DISP_ARB_CTL,
  7133. (I915_READ(DISP_ARB_CTL) |
  7134. DISP_FBC_WM_DIS));
  7135. I915_WRITE(WM3_LP_ILK, 0);
  7136. I915_WRITE(WM2_LP_ILK, 0);
  7137. I915_WRITE(WM1_LP_ILK, 0);
  7138. /*
  7139. * Based on the document from hardware guys the following bits
  7140. * should be set unconditionally in order to enable FBC.
  7141. * The bit 22 of 0x42000
  7142. * The bit 22 of 0x42004
  7143. * The bit 7,8,9 of 0x42020.
  7144. */
  7145. if (IS_IRONLAKE_M(dev)) {
  7146. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  7147. I915_READ(ILK_DISPLAY_CHICKEN1) |
  7148. ILK_FBCQ_DIS);
  7149. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  7150. I915_READ(ILK_DISPLAY_CHICKEN2) |
  7151. ILK_DPARB_GATE);
  7152. I915_WRITE(ILK_DSPCLK_GATE,
  7153. I915_READ(ILK_DSPCLK_GATE) |
  7154. ILK_DPFC_DIS1 |
  7155. ILK_DPFC_DIS2 |
  7156. ILK_CLK_FBC);
  7157. }
  7158. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  7159. I915_READ(ILK_DISPLAY_CHICKEN2) |
  7160. ILK_ELPIN_409_SELECT);
  7161. I915_WRITE(_3D_CHICKEN2,
  7162. _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
  7163. _3D_CHICKEN2_WM_READ_PIPELINED);
  7164. }
  7165. static void gen6_init_clock_gating(struct drm_device *dev)
  7166. {
  7167. struct drm_i915_private *dev_priv = dev->dev_private;
  7168. int pipe;
  7169. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  7170. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  7171. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  7172. I915_READ(ILK_DISPLAY_CHICKEN2) |
  7173. ILK_ELPIN_409_SELECT);
  7174. I915_WRITE(WM3_LP_ILK, 0);
  7175. I915_WRITE(WM2_LP_ILK, 0);
  7176. I915_WRITE(WM1_LP_ILK, 0);
  7177. /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
  7178. * gating disable must be set. Failure to set it results in
  7179. * flickering pixels due to Z write ordering failures after
  7180. * some amount of runtime in the Mesa "fire" demo, and Unigine
  7181. * Sanctuary and Tropics, and apparently anything else with
  7182. * alpha test or pixel discard.
  7183. *
  7184. * According to the spec, bit 11 (RCCUNIT) must also be set,
  7185. * but we didn't debug actual testcases to find it out.
  7186. */
  7187. I915_WRITE(GEN6_UCGCTL2,
  7188. GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
  7189. GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
  7190. /*
  7191. * According to the spec the following bits should be
  7192. * set in order to enable memory self-refresh and fbc:
  7193. * The bit21 and bit22 of 0x42000
  7194. * The bit21 and bit22 of 0x42004
  7195. * The bit5 and bit7 of 0x42020
  7196. * The bit14 of 0x70180
  7197. * The bit14 of 0x71180
  7198. */
  7199. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  7200. I915_READ(ILK_DISPLAY_CHICKEN1) |
  7201. ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
  7202. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  7203. I915_READ(ILK_DISPLAY_CHICKEN2) |
  7204. ILK_DPARB_GATE | ILK_VSDPFD_FULL);
  7205. I915_WRITE(ILK_DSPCLK_GATE,
  7206. I915_READ(ILK_DSPCLK_GATE) |
  7207. ILK_DPARB_CLK_GATE |
  7208. ILK_DPFD_CLK_GATE);
  7209. for_each_pipe(pipe) {
  7210. I915_WRITE(DSPCNTR(pipe),
  7211. I915_READ(DSPCNTR(pipe)) |
  7212. DISPPLANE_TRICKLE_FEED_DISABLE);
  7213. intel_flush_display_plane(dev_priv, pipe);
  7214. }
  7215. }
  7216. static void ivybridge_init_clock_gating(struct drm_device *dev)
  7217. {
  7218. struct drm_i915_private *dev_priv = dev->dev_private;
  7219. int pipe;
  7220. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  7221. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  7222. I915_WRITE(WM3_LP_ILK, 0);
  7223. I915_WRITE(WM2_LP_ILK, 0);
  7224. I915_WRITE(WM1_LP_ILK, 0);
  7225. I915_WRITE(ILK_DSPCLK_GATE, IVB_VRHUNIT_CLK_GATE);
  7226. I915_WRITE(IVB_CHICKEN3,
  7227. CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
  7228. CHICKEN3_DGMG_DONE_FIX_DISABLE);
  7229. for_each_pipe(pipe) {
  7230. I915_WRITE(DSPCNTR(pipe),
  7231. I915_READ(DSPCNTR(pipe)) |
  7232. DISPPLANE_TRICKLE_FEED_DISABLE);
  7233. intel_flush_display_plane(dev_priv, pipe);
  7234. }
  7235. }
  7236. static void g4x_init_clock_gating(struct drm_device *dev)
  7237. {
  7238. struct drm_i915_private *dev_priv = dev->dev_private;
  7239. uint32_t dspclk_gate;
  7240. I915_WRITE(RENCLK_GATE_D1, 0);
  7241. I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
  7242. GS_UNIT_CLOCK_GATE_DISABLE |
  7243. CL_UNIT_CLOCK_GATE_DISABLE);
  7244. I915_WRITE(RAMCLK_GATE_D, 0);
  7245. dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
  7246. OVRUNIT_CLOCK_GATE_DISABLE |
  7247. OVCUNIT_CLOCK_GATE_DISABLE;
  7248. if (IS_GM45(dev))
  7249. dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
  7250. I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
  7251. }
  7252. static void crestline_init_clock_gating(struct drm_device *dev)
  7253. {
  7254. struct drm_i915_private *dev_priv = dev->dev_private;
  7255. I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
  7256. I915_WRITE(RENCLK_GATE_D2, 0);
  7257. I915_WRITE(DSPCLK_GATE_D, 0);
  7258. I915_WRITE(RAMCLK_GATE_D, 0);
  7259. I915_WRITE16(DEUC, 0);
  7260. }
  7261. static void broadwater_init_clock_gating(struct drm_device *dev)
  7262. {
  7263. struct drm_i915_private *dev_priv = dev->dev_private;
  7264. I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
  7265. I965_RCC_CLOCK_GATE_DISABLE |
  7266. I965_RCPB_CLOCK_GATE_DISABLE |
  7267. I965_ISC_CLOCK_GATE_DISABLE |
  7268. I965_FBC_CLOCK_GATE_DISABLE);
  7269. I915_WRITE(RENCLK_GATE_D2, 0);
  7270. }
  7271. static void gen3_init_clock_gating(struct drm_device *dev)
  7272. {
  7273. struct drm_i915_private *dev_priv = dev->dev_private;
  7274. u32 dstate = I915_READ(D_STATE);
  7275. dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
  7276. DSTATE_DOT_CLOCK_GATING;
  7277. I915_WRITE(D_STATE, dstate);
  7278. }
  7279. static void i85x_init_clock_gating(struct drm_device *dev)
  7280. {
  7281. struct drm_i915_private *dev_priv = dev->dev_private;
  7282. I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
  7283. }
  7284. static void i830_init_clock_gating(struct drm_device *dev)
  7285. {
  7286. struct drm_i915_private *dev_priv = dev->dev_private;
  7287. I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
  7288. }
  7289. static void ibx_init_clock_gating(struct drm_device *dev)
  7290. {
  7291. struct drm_i915_private *dev_priv = dev->dev_private;
  7292. /*
  7293. * On Ibex Peak and Cougar Point, we need to disable clock
  7294. * gating for the panel power sequencer or it will fail to
  7295. * start up when no ports are active.
  7296. */
  7297. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
  7298. }
  7299. static void cpt_init_clock_gating(struct drm_device *dev)
  7300. {
  7301. struct drm_i915_private *dev_priv = dev->dev_private;
  7302. int pipe;
  7303. /*
  7304. * On Ibex Peak and Cougar Point, we need to disable clock
  7305. * gating for the panel power sequencer or it will fail to
  7306. * start up when no ports are active.
  7307. */
  7308. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
  7309. I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
  7310. DPLS_EDP_PPS_FIX_DIS);
  7311. /* Without this, mode sets may fail silently on FDI */
  7312. for_each_pipe(pipe)
  7313. I915_WRITE(TRANS_CHICKEN2(pipe), TRANS_AUTOTRAIN_GEN_STALL_DIS);
  7314. }
  7315. static void ironlake_teardown_rc6(struct drm_device *dev)
  7316. {
  7317. struct drm_i915_private *dev_priv = dev->dev_private;
  7318. if (dev_priv->renderctx) {
  7319. i915_gem_object_unpin(dev_priv->renderctx);
  7320. drm_gem_object_unreference(&dev_priv->renderctx->base);
  7321. dev_priv->renderctx = NULL;
  7322. }
  7323. if (dev_priv->pwrctx) {
  7324. i915_gem_object_unpin(dev_priv->pwrctx);
  7325. drm_gem_object_unreference(&dev_priv->pwrctx->base);
  7326. dev_priv->pwrctx = NULL;
  7327. }
  7328. }
  7329. static void ironlake_disable_rc6(struct drm_device *dev)
  7330. {
  7331. struct drm_i915_private *dev_priv = dev->dev_private;
  7332. if (I915_READ(PWRCTXA)) {
  7333. /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
  7334. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
  7335. wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
  7336. 50);
  7337. I915_WRITE(PWRCTXA, 0);
  7338. POSTING_READ(PWRCTXA);
  7339. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
  7340. POSTING_READ(RSTDBYCTL);
  7341. }
  7342. ironlake_teardown_rc6(dev);
  7343. }
  7344. static int ironlake_setup_rc6(struct drm_device *dev)
  7345. {
  7346. struct drm_i915_private *dev_priv = dev->dev_private;
  7347. if (dev_priv->renderctx == NULL)
  7348. dev_priv->renderctx = intel_alloc_context_page(dev);
  7349. if (!dev_priv->renderctx)
  7350. return -ENOMEM;
  7351. if (dev_priv->pwrctx == NULL)
  7352. dev_priv->pwrctx = intel_alloc_context_page(dev);
  7353. if (!dev_priv->pwrctx) {
  7354. ironlake_teardown_rc6(dev);
  7355. return -ENOMEM;
  7356. }
  7357. return 0;
  7358. }
  7359. void ironlake_enable_rc6(struct drm_device *dev)
  7360. {
  7361. struct drm_i915_private *dev_priv = dev->dev_private;
  7362. int ret;
  7363. /* rc6 disabled by default due to repeated reports of hanging during
  7364. * boot and resume.
  7365. */
  7366. if (!intel_enable_rc6(dev))
  7367. return;
  7368. mutex_lock(&dev->struct_mutex);
  7369. ret = ironlake_setup_rc6(dev);
  7370. if (ret) {
  7371. mutex_unlock(&dev->struct_mutex);
  7372. return;
  7373. }
  7374. /*
  7375. * GPU can automatically power down the render unit if given a page
  7376. * to save state.
  7377. */
  7378. ret = BEGIN_LP_RING(6);
  7379. if (ret) {
  7380. ironlake_teardown_rc6(dev);
  7381. mutex_unlock(&dev->struct_mutex);
  7382. return;
  7383. }
  7384. OUT_RING(MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
  7385. OUT_RING(MI_SET_CONTEXT);
  7386. OUT_RING(dev_priv->renderctx->gtt_offset |
  7387. MI_MM_SPACE_GTT |
  7388. MI_SAVE_EXT_STATE_EN |
  7389. MI_RESTORE_EXT_STATE_EN |
  7390. MI_RESTORE_INHIBIT);
  7391. OUT_RING(MI_SUSPEND_FLUSH);
  7392. OUT_RING(MI_NOOP);
  7393. OUT_RING(MI_FLUSH);
  7394. ADVANCE_LP_RING();
  7395. /*
  7396. * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
  7397. * does an implicit flush, combined with MI_FLUSH above, it should be
  7398. * safe to assume that renderctx is valid
  7399. */
  7400. ret = intel_wait_ring_idle(LP_RING(dev_priv));
  7401. if (ret) {
  7402. DRM_ERROR("failed to enable ironlake power power savings\n");
  7403. ironlake_teardown_rc6(dev);
  7404. mutex_unlock(&dev->struct_mutex);
  7405. return;
  7406. }
  7407. I915_WRITE(PWRCTXA, dev_priv->pwrctx->gtt_offset | PWRCTX_EN);
  7408. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
  7409. mutex_unlock(&dev->struct_mutex);
  7410. }
  7411. void intel_init_clock_gating(struct drm_device *dev)
  7412. {
  7413. struct drm_i915_private *dev_priv = dev->dev_private;
  7414. dev_priv->display.init_clock_gating(dev);
  7415. if (dev_priv->display.init_pch_clock_gating)
  7416. dev_priv->display.init_pch_clock_gating(dev);
  7417. }
  7418. /* Set up chip specific display functions */
  7419. static void intel_init_display(struct drm_device *dev)
  7420. {
  7421. struct drm_i915_private *dev_priv = dev->dev_private;
  7422. /* We always want a DPMS function */
  7423. if (HAS_PCH_SPLIT(dev)) {
  7424. dev_priv->display.dpms = ironlake_crtc_dpms;
  7425. dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
  7426. dev_priv->display.update_plane = ironlake_update_plane;
  7427. } else {
  7428. dev_priv->display.dpms = i9xx_crtc_dpms;
  7429. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  7430. dev_priv->display.update_plane = i9xx_update_plane;
  7431. }
  7432. if (I915_HAS_FBC(dev)) {
  7433. if (HAS_PCH_SPLIT(dev)) {
  7434. dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
  7435. dev_priv->display.enable_fbc = ironlake_enable_fbc;
  7436. dev_priv->display.disable_fbc = ironlake_disable_fbc;
  7437. } else if (IS_GM45(dev)) {
  7438. dev_priv->display.fbc_enabled = g4x_fbc_enabled;
  7439. dev_priv->display.enable_fbc = g4x_enable_fbc;
  7440. dev_priv->display.disable_fbc = g4x_disable_fbc;
  7441. } else if (IS_CRESTLINE(dev)) {
  7442. dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
  7443. dev_priv->display.enable_fbc = i8xx_enable_fbc;
  7444. dev_priv->display.disable_fbc = i8xx_disable_fbc;
  7445. }
  7446. /* 855GM needs testing */
  7447. }
  7448. /* Returns the core display clock speed */
  7449. if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  7450. dev_priv->display.get_display_clock_speed =
  7451. i945_get_display_clock_speed;
  7452. else if (IS_I915G(dev))
  7453. dev_priv->display.get_display_clock_speed =
  7454. i915_get_display_clock_speed;
  7455. else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
  7456. dev_priv->display.get_display_clock_speed =
  7457. i9xx_misc_get_display_clock_speed;
  7458. else if (IS_I915GM(dev))
  7459. dev_priv->display.get_display_clock_speed =
  7460. i915gm_get_display_clock_speed;
  7461. else if (IS_I865G(dev))
  7462. dev_priv->display.get_display_clock_speed =
  7463. i865_get_display_clock_speed;
  7464. else if (IS_I85X(dev))
  7465. dev_priv->display.get_display_clock_speed =
  7466. i855_get_display_clock_speed;
  7467. else /* 852, 830 */
  7468. dev_priv->display.get_display_clock_speed =
  7469. i830_get_display_clock_speed;
  7470. /* For FIFO watermark updates */
  7471. if (HAS_PCH_SPLIT(dev)) {
  7472. dev_priv->display.force_wake_get = __gen6_gt_force_wake_get;
  7473. dev_priv->display.force_wake_put = __gen6_gt_force_wake_put;
  7474. /* IVB configs may use multi-threaded forcewake */
  7475. if (IS_IVYBRIDGE(dev)) {
  7476. u32 ecobus;
  7477. /* A small trick here - if the bios hasn't configured MT forcewake,
  7478. * and if the device is in RC6, then force_wake_mt_get will not wake
  7479. * the device and the ECOBUS read will return zero. Which will be
  7480. * (correctly) interpreted by the test below as MT forcewake being
  7481. * disabled.
  7482. */
  7483. mutex_lock(&dev->struct_mutex);
  7484. __gen6_gt_force_wake_mt_get(dev_priv);
  7485. ecobus = I915_READ_NOTRACE(ECOBUS);
  7486. __gen6_gt_force_wake_mt_put(dev_priv);
  7487. mutex_unlock(&dev->struct_mutex);
  7488. if (ecobus & FORCEWAKE_MT_ENABLE) {
  7489. DRM_DEBUG_KMS("Using MT version of forcewake\n");
  7490. dev_priv->display.force_wake_get =
  7491. __gen6_gt_force_wake_mt_get;
  7492. dev_priv->display.force_wake_put =
  7493. __gen6_gt_force_wake_mt_put;
  7494. }
  7495. }
  7496. if (HAS_PCH_IBX(dev))
  7497. dev_priv->display.init_pch_clock_gating = ibx_init_clock_gating;
  7498. else if (HAS_PCH_CPT(dev))
  7499. dev_priv->display.init_pch_clock_gating = cpt_init_clock_gating;
  7500. if (IS_GEN5(dev)) {
  7501. if (I915_READ(MLTR_ILK) & ILK_SRLT_MASK)
  7502. dev_priv->display.update_wm = ironlake_update_wm;
  7503. else {
  7504. DRM_DEBUG_KMS("Failed to get proper latency. "
  7505. "Disable CxSR\n");
  7506. dev_priv->display.update_wm = NULL;
  7507. }
  7508. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  7509. dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
  7510. dev_priv->display.write_eld = ironlake_write_eld;
  7511. } else if (IS_GEN6(dev)) {
  7512. if (SNB_READ_WM0_LATENCY()) {
  7513. dev_priv->display.update_wm = sandybridge_update_wm;
  7514. dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
  7515. } else {
  7516. DRM_DEBUG_KMS("Failed to read display plane latency. "
  7517. "Disable CxSR\n");
  7518. dev_priv->display.update_wm = NULL;
  7519. }
  7520. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  7521. dev_priv->display.init_clock_gating = gen6_init_clock_gating;
  7522. dev_priv->display.write_eld = ironlake_write_eld;
  7523. } else if (IS_IVYBRIDGE(dev)) {
  7524. /* FIXME: detect B0+ stepping and use auto training */
  7525. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  7526. if (SNB_READ_WM0_LATENCY()) {
  7527. dev_priv->display.update_wm = sandybridge_update_wm;
  7528. dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
  7529. } else {
  7530. DRM_DEBUG_KMS("Failed to read display plane latency. "
  7531. "Disable CxSR\n");
  7532. dev_priv->display.update_wm = NULL;
  7533. }
  7534. dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
  7535. dev_priv->display.write_eld = ironlake_write_eld;
  7536. } else
  7537. dev_priv->display.update_wm = NULL;
  7538. } else if (IS_PINEVIEW(dev)) {
  7539. if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
  7540. dev_priv->is_ddr3,
  7541. dev_priv->fsb_freq,
  7542. dev_priv->mem_freq)) {
  7543. DRM_INFO("failed to find known CxSR latency "
  7544. "(found ddr%s fsb freq %d, mem freq %d), "
  7545. "disabling CxSR\n",
  7546. (dev_priv->is_ddr3 == 1) ? "3" : "2",
  7547. dev_priv->fsb_freq, dev_priv->mem_freq);
  7548. /* Disable CxSR and never update its watermark again */
  7549. pineview_disable_cxsr(dev);
  7550. dev_priv->display.update_wm = NULL;
  7551. } else
  7552. dev_priv->display.update_wm = pineview_update_wm;
  7553. dev_priv->display.init_clock_gating = gen3_init_clock_gating;
  7554. } else if (IS_G4X(dev)) {
  7555. dev_priv->display.write_eld = g4x_write_eld;
  7556. dev_priv->display.update_wm = g4x_update_wm;
  7557. dev_priv->display.init_clock_gating = g4x_init_clock_gating;
  7558. } else if (IS_GEN4(dev)) {
  7559. dev_priv->display.update_wm = i965_update_wm;
  7560. if (IS_CRESTLINE(dev))
  7561. dev_priv->display.init_clock_gating = crestline_init_clock_gating;
  7562. else if (IS_BROADWATER(dev))
  7563. dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
  7564. } else if (IS_GEN3(dev)) {
  7565. dev_priv->display.update_wm = i9xx_update_wm;
  7566. dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
  7567. dev_priv->display.init_clock_gating = gen3_init_clock_gating;
  7568. } else if (IS_I865G(dev)) {
  7569. dev_priv->display.update_wm = i830_update_wm;
  7570. dev_priv->display.init_clock_gating = i85x_init_clock_gating;
  7571. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  7572. } else if (IS_I85X(dev)) {
  7573. dev_priv->display.update_wm = i9xx_update_wm;
  7574. dev_priv->display.get_fifo_size = i85x_get_fifo_size;
  7575. dev_priv->display.init_clock_gating = i85x_init_clock_gating;
  7576. } else {
  7577. dev_priv->display.update_wm = i830_update_wm;
  7578. dev_priv->display.init_clock_gating = i830_init_clock_gating;
  7579. if (IS_845G(dev))
  7580. dev_priv->display.get_fifo_size = i845_get_fifo_size;
  7581. else
  7582. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  7583. }
  7584. /* Default just returns -ENODEV to indicate unsupported */
  7585. dev_priv->display.queue_flip = intel_default_queue_flip;
  7586. switch (INTEL_INFO(dev)->gen) {
  7587. case 2:
  7588. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  7589. break;
  7590. case 3:
  7591. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  7592. break;
  7593. case 4:
  7594. case 5:
  7595. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  7596. break;
  7597. case 6:
  7598. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  7599. break;
  7600. case 7:
  7601. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  7602. break;
  7603. }
  7604. }
  7605. /*
  7606. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  7607. * resume, or other times. This quirk makes sure that's the case for
  7608. * affected systems.
  7609. */
  7610. static void quirk_pipea_force(struct drm_device *dev)
  7611. {
  7612. struct drm_i915_private *dev_priv = dev->dev_private;
  7613. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  7614. DRM_DEBUG_DRIVER("applying pipe a force quirk\n");
  7615. }
  7616. /*
  7617. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  7618. */
  7619. static void quirk_ssc_force_disable(struct drm_device *dev)
  7620. {
  7621. struct drm_i915_private *dev_priv = dev->dev_private;
  7622. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  7623. }
  7624. struct intel_quirk {
  7625. int device;
  7626. int subsystem_vendor;
  7627. int subsystem_device;
  7628. void (*hook)(struct drm_device *dev);
  7629. };
  7630. struct intel_quirk intel_quirks[] = {
  7631. /* HP Compaq 2730p needs pipe A force quirk (LP: #291555) */
  7632. { 0x2a42, 0x103c, 0x30eb, quirk_pipea_force },
  7633. /* HP Mini needs pipe A force quirk (LP: #322104) */
  7634. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  7635. /* Thinkpad R31 needs pipe A force quirk */
  7636. { 0x3577, 0x1014, 0x0505, quirk_pipea_force },
  7637. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  7638. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  7639. /* ThinkPad X30 needs pipe A force quirk (LP: #304614) */
  7640. { 0x3577, 0x1014, 0x0513, quirk_pipea_force },
  7641. /* ThinkPad X40 needs pipe A force quirk */
  7642. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  7643. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  7644. /* 855 & before need to leave pipe A & dpll A up */
  7645. { 0x3582, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  7646. { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  7647. /* Lenovo U160 cannot use SSC on LVDS */
  7648. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  7649. /* Sony Vaio Y cannot use SSC on LVDS */
  7650. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  7651. };
  7652. static void intel_init_quirks(struct drm_device *dev)
  7653. {
  7654. struct pci_dev *d = dev->pdev;
  7655. int i;
  7656. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  7657. struct intel_quirk *q = &intel_quirks[i];
  7658. if (d->device == q->device &&
  7659. (d->subsystem_vendor == q->subsystem_vendor ||
  7660. q->subsystem_vendor == PCI_ANY_ID) &&
  7661. (d->subsystem_device == q->subsystem_device ||
  7662. q->subsystem_device == PCI_ANY_ID))
  7663. q->hook(dev);
  7664. }
  7665. }
  7666. /* Disable the VGA plane that we never use */
  7667. static void i915_disable_vga(struct drm_device *dev)
  7668. {
  7669. struct drm_i915_private *dev_priv = dev->dev_private;
  7670. u8 sr1;
  7671. u32 vga_reg;
  7672. if (HAS_PCH_SPLIT(dev))
  7673. vga_reg = CPU_VGACNTRL;
  7674. else
  7675. vga_reg = VGACNTRL;
  7676. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  7677. outb(1, VGA_SR_INDEX);
  7678. sr1 = inb(VGA_SR_DATA);
  7679. outb(sr1 | 1<<5, VGA_SR_DATA);
  7680. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  7681. udelay(300);
  7682. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  7683. POSTING_READ(vga_reg);
  7684. }
  7685. void intel_modeset_init(struct drm_device *dev)
  7686. {
  7687. struct drm_i915_private *dev_priv = dev->dev_private;
  7688. int i, ret;
  7689. drm_mode_config_init(dev);
  7690. dev->mode_config.min_width = 0;
  7691. dev->mode_config.min_height = 0;
  7692. dev->mode_config.funcs = (void *)&intel_mode_funcs;
  7693. intel_init_quirks(dev);
  7694. intel_init_display(dev);
  7695. if (IS_GEN2(dev)) {
  7696. dev->mode_config.max_width = 2048;
  7697. dev->mode_config.max_height = 2048;
  7698. } else if (IS_GEN3(dev)) {
  7699. dev->mode_config.max_width = 4096;
  7700. dev->mode_config.max_height = 4096;
  7701. } else {
  7702. dev->mode_config.max_width = 8192;
  7703. dev->mode_config.max_height = 8192;
  7704. }
  7705. dev->mode_config.fb_base = dev->agp->base;
  7706. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  7707. dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
  7708. for (i = 0; i < dev_priv->num_pipe; i++) {
  7709. intel_crtc_init(dev, i);
  7710. ret = intel_plane_init(dev, i);
  7711. if (ret)
  7712. DRM_DEBUG_KMS("plane %d init failed: %d\n", i, ret);
  7713. }
  7714. /* Just disable it once at startup */
  7715. i915_disable_vga(dev);
  7716. intel_setup_outputs(dev);
  7717. intel_init_clock_gating(dev);
  7718. if (IS_IRONLAKE_M(dev)) {
  7719. ironlake_enable_drps(dev);
  7720. intel_init_emon(dev);
  7721. }
  7722. if (IS_GEN6(dev) || IS_GEN7(dev)) {
  7723. gen6_enable_rps(dev_priv);
  7724. gen6_update_ring_freq(dev_priv);
  7725. }
  7726. INIT_WORK(&dev_priv->idle_work, intel_idle_update);
  7727. setup_timer(&dev_priv->idle_timer, intel_gpu_idle_timer,
  7728. (unsigned long)dev);
  7729. }
  7730. void intel_modeset_gem_init(struct drm_device *dev)
  7731. {
  7732. if (IS_IRONLAKE_M(dev))
  7733. ironlake_enable_rc6(dev);
  7734. intel_setup_overlay(dev);
  7735. }
  7736. void intel_modeset_cleanup(struct drm_device *dev)
  7737. {
  7738. struct drm_i915_private *dev_priv = dev->dev_private;
  7739. struct drm_crtc *crtc;
  7740. struct intel_crtc *intel_crtc;
  7741. drm_kms_helper_poll_fini(dev);
  7742. mutex_lock(&dev->struct_mutex);
  7743. intel_unregister_dsm_handler();
  7744. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  7745. /* Skip inactive CRTCs */
  7746. if (!crtc->fb)
  7747. continue;
  7748. intel_crtc = to_intel_crtc(crtc);
  7749. intel_increase_pllclock(crtc);
  7750. }
  7751. intel_disable_fbc(dev);
  7752. if (IS_IRONLAKE_M(dev))
  7753. ironlake_disable_drps(dev);
  7754. if (IS_GEN6(dev) || IS_GEN7(dev))
  7755. gen6_disable_rps(dev);
  7756. if (IS_IRONLAKE_M(dev))
  7757. ironlake_disable_rc6(dev);
  7758. mutex_unlock(&dev->struct_mutex);
  7759. /* Disable the irq before mode object teardown, for the irq might
  7760. * enqueue unpin/hotplug work. */
  7761. drm_irq_uninstall(dev);
  7762. cancel_work_sync(&dev_priv->hotplug_work);
  7763. cancel_work_sync(&dev_priv->rps_work);
  7764. /* flush any delayed tasks or pending work */
  7765. flush_scheduled_work();
  7766. /* Shut off idle work before the crtcs get freed. */
  7767. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  7768. intel_crtc = to_intel_crtc(crtc);
  7769. del_timer_sync(&intel_crtc->idle_timer);
  7770. }
  7771. del_timer_sync(&dev_priv->idle_timer);
  7772. cancel_work_sync(&dev_priv->idle_work);
  7773. drm_mode_config_cleanup(dev);
  7774. }
  7775. /*
  7776. * Return which encoder is currently attached for connector.
  7777. */
  7778. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  7779. {
  7780. return &intel_attached_encoder(connector)->base;
  7781. }
  7782. void intel_connector_attach_encoder(struct intel_connector *connector,
  7783. struct intel_encoder *encoder)
  7784. {
  7785. connector->encoder = encoder;
  7786. drm_mode_connector_attach_encoder(&connector->base,
  7787. &encoder->base);
  7788. }
  7789. /*
  7790. * set vga decode state - true == enable VGA decode
  7791. */
  7792. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  7793. {
  7794. struct drm_i915_private *dev_priv = dev->dev_private;
  7795. u16 gmch_ctrl;
  7796. pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
  7797. if (state)
  7798. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  7799. else
  7800. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  7801. pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
  7802. return 0;
  7803. }
  7804. #ifdef CONFIG_DEBUG_FS
  7805. #include <linux/seq_file.h>
  7806. struct intel_display_error_state {
  7807. struct intel_cursor_error_state {
  7808. u32 control;
  7809. u32 position;
  7810. u32 base;
  7811. u32 size;
  7812. } cursor[2];
  7813. struct intel_pipe_error_state {
  7814. u32 conf;
  7815. u32 source;
  7816. u32 htotal;
  7817. u32 hblank;
  7818. u32 hsync;
  7819. u32 vtotal;
  7820. u32 vblank;
  7821. u32 vsync;
  7822. } pipe[2];
  7823. struct intel_plane_error_state {
  7824. u32 control;
  7825. u32 stride;
  7826. u32 size;
  7827. u32 pos;
  7828. u32 addr;
  7829. u32 surface;
  7830. u32 tile_offset;
  7831. } plane[2];
  7832. };
  7833. struct intel_display_error_state *
  7834. intel_display_capture_error_state(struct drm_device *dev)
  7835. {
  7836. drm_i915_private_t *dev_priv = dev->dev_private;
  7837. struct intel_display_error_state *error;
  7838. int i;
  7839. error = kmalloc(sizeof(*error), GFP_ATOMIC);
  7840. if (error == NULL)
  7841. return NULL;
  7842. for (i = 0; i < 2; i++) {
  7843. error->cursor[i].control = I915_READ(CURCNTR(i));
  7844. error->cursor[i].position = I915_READ(CURPOS(i));
  7845. error->cursor[i].base = I915_READ(CURBASE(i));
  7846. error->plane[i].control = I915_READ(DSPCNTR(i));
  7847. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  7848. error->plane[i].size = I915_READ(DSPSIZE(i));
  7849. error->plane[i].pos = I915_READ(DSPPOS(i));
  7850. error->plane[i].addr = I915_READ(DSPADDR(i));
  7851. if (INTEL_INFO(dev)->gen >= 4) {
  7852. error->plane[i].surface = I915_READ(DSPSURF(i));
  7853. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  7854. }
  7855. error->pipe[i].conf = I915_READ(PIPECONF(i));
  7856. error->pipe[i].source = I915_READ(PIPESRC(i));
  7857. error->pipe[i].htotal = I915_READ(HTOTAL(i));
  7858. error->pipe[i].hblank = I915_READ(HBLANK(i));
  7859. error->pipe[i].hsync = I915_READ(HSYNC(i));
  7860. error->pipe[i].vtotal = I915_READ(VTOTAL(i));
  7861. error->pipe[i].vblank = I915_READ(VBLANK(i));
  7862. error->pipe[i].vsync = I915_READ(VSYNC(i));
  7863. }
  7864. return error;
  7865. }
  7866. void
  7867. intel_display_print_error_state(struct seq_file *m,
  7868. struct drm_device *dev,
  7869. struct intel_display_error_state *error)
  7870. {
  7871. int i;
  7872. for (i = 0; i < 2; i++) {
  7873. seq_printf(m, "Pipe [%d]:\n", i);
  7874. seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
  7875. seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
  7876. seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
  7877. seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
  7878. seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
  7879. seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
  7880. seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
  7881. seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
  7882. seq_printf(m, "Plane [%d]:\n", i);
  7883. seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
  7884. seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  7885. seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
  7886. seq_printf(m, " POS: %08x\n", error->plane[i].pos);
  7887. seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  7888. if (INTEL_INFO(dev)->gen >= 4) {
  7889. seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
  7890. seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  7891. }
  7892. seq_printf(m, "Cursor [%d]:\n", i);
  7893. seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  7894. seq_printf(m, " POS: %08x\n", error->cursor[i].position);
  7895. seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
  7896. }
  7897. }
  7898. #endif