intel_display.c 238 KB

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