intel_display.c 253 KB

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