intel_display.c 226 KB

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