intel_display.c 241 KB

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