intel_display.c 181 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381
  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/module.h>
  27. #include <linux/input.h>
  28. #include <linux/i2c.h>
  29. #include <linux/kernel.h>
  30. #include <linux/slab.h>
  31. #include <linux/vgaarb.h>
  32. #include "drmP.h"
  33. #include "intel_drv.h"
  34. #include "i915_drm.h"
  35. #include "i915_drv.h"
  36. #include "i915_trace.h"
  37. #include "drm_dp_helper.h"
  38. #include "drm_crtc_helper.h"
  39. #define HAS_eDP (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  40. bool intel_pipe_has_type (struct drm_crtc *crtc, int type);
  41. static void intel_update_watermarks(struct drm_device *dev);
  42. static void intel_increase_pllclock(struct drm_crtc *crtc);
  43. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  44. typedef struct {
  45. /* given values */
  46. int n;
  47. int m1, m2;
  48. int p1, p2;
  49. /* derived values */
  50. int dot;
  51. int vco;
  52. int m;
  53. int p;
  54. } intel_clock_t;
  55. typedef struct {
  56. int min, max;
  57. } intel_range_t;
  58. typedef struct {
  59. int dot_limit;
  60. int p2_slow, p2_fast;
  61. } intel_p2_t;
  62. #define INTEL_P2_NUM 2
  63. typedef struct intel_limit intel_limit_t;
  64. struct intel_limit {
  65. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  66. intel_p2_t p2;
  67. bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
  68. int, int, intel_clock_t *);
  69. };
  70. #define I8XX_DOT_MIN 25000
  71. #define I8XX_DOT_MAX 350000
  72. #define I8XX_VCO_MIN 930000
  73. #define I8XX_VCO_MAX 1400000
  74. #define I8XX_N_MIN 3
  75. #define I8XX_N_MAX 16
  76. #define I8XX_M_MIN 96
  77. #define I8XX_M_MAX 140
  78. #define I8XX_M1_MIN 18
  79. #define I8XX_M1_MAX 26
  80. #define I8XX_M2_MIN 6
  81. #define I8XX_M2_MAX 16
  82. #define I8XX_P_MIN 4
  83. #define I8XX_P_MAX 128
  84. #define I8XX_P1_MIN 2
  85. #define I8XX_P1_MAX 33
  86. #define I8XX_P1_LVDS_MIN 1
  87. #define I8XX_P1_LVDS_MAX 6
  88. #define I8XX_P2_SLOW 4
  89. #define I8XX_P2_FAST 2
  90. #define I8XX_P2_LVDS_SLOW 14
  91. #define I8XX_P2_LVDS_FAST 7
  92. #define I8XX_P2_SLOW_LIMIT 165000
  93. #define I9XX_DOT_MIN 20000
  94. #define I9XX_DOT_MAX 400000
  95. #define I9XX_VCO_MIN 1400000
  96. #define I9XX_VCO_MAX 2800000
  97. #define PINEVIEW_VCO_MIN 1700000
  98. #define PINEVIEW_VCO_MAX 3500000
  99. #define I9XX_N_MIN 1
  100. #define I9XX_N_MAX 6
  101. /* Pineview's Ncounter is a ring counter */
  102. #define PINEVIEW_N_MIN 3
  103. #define PINEVIEW_N_MAX 6
  104. #define I9XX_M_MIN 70
  105. #define I9XX_M_MAX 120
  106. #define PINEVIEW_M_MIN 2
  107. #define PINEVIEW_M_MAX 256
  108. #define I9XX_M1_MIN 10
  109. #define I9XX_M1_MAX 22
  110. #define I9XX_M2_MIN 5
  111. #define I9XX_M2_MAX 9
  112. /* Pineview M1 is reserved, and must be 0 */
  113. #define PINEVIEW_M1_MIN 0
  114. #define PINEVIEW_M1_MAX 0
  115. #define PINEVIEW_M2_MIN 0
  116. #define PINEVIEW_M2_MAX 254
  117. #define I9XX_P_SDVO_DAC_MIN 5
  118. #define I9XX_P_SDVO_DAC_MAX 80
  119. #define I9XX_P_LVDS_MIN 7
  120. #define I9XX_P_LVDS_MAX 98
  121. #define PINEVIEW_P_LVDS_MIN 7
  122. #define PINEVIEW_P_LVDS_MAX 112
  123. #define I9XX_P1_MIN 1
  124. #define I9XX_P1_MAX 8
  125. #define I9XX_P2_SDVO_DAC_SLOW 10
  126. #define I9XX_P2_SDVO_DAC_FAST 5
  127. #define I9XX_P2_SDVO_DAC_SLOW_LIMIT 200000
  128. #define I9XX_P2_LVDS_SLOW 14
  129. #define I9XX_P2_LVDS_FAST 7
  130. #define I9XX_P2_LVDS_SLOW_LIMIT 112000
  131. /*The parameter is for SDVO on G4x platform*/
  132. #define G4X_DOT_SDVO_MIN 25000
  133. #define G4X_DOT_SDVO_MAX 270000
  134. #define G4X_VCO_MIN 1750000
  135. #define G4X_VCO_MAX 3500000
  136. #define G4X_N_SDVO_MIN 1
  137. #define G4X_N_SDVO_MAX 4
  138. #define G4X_M_SDVO_MIN 104
  139. #define G4X_M_SDVO_MAX 138
  140. #define G4X_M1_SDVO_MIN 17
  141. #define G4X_M1_SDVO_MAX 23
  142. #define G4X_M2_SDVO_MIN 5
  143. #define G4X_M2_SDVO_MAX 11
  144. #define G4X_P_SDVO_MIN 10
  145. #define G4X_P_SDVO_MAX 30
  146. #define G4X_P1_SDVO_MIN 1
  147. #define G4X_P1_SDVO_MAX 3
  148. #define G4X_P2_SDVO_SLOW 10
  149. #define G4X_P2_SDVO_FAST 10
  150. #define G4X_P2_SDVO_LIMIT 270000
  151. /*The parameter is for HDMI_DAC on G4x platform*/
  152. #define G4X_DOT_HDMI_DAC_MIN 22000
  153. #define G4X_DOT_HDMI_DAC_MAX 400000
  154. #define G4X_N_HDMI_DAC_MIN 1
  155. #define G4X_N_HDMI_DAC_MAX 4
  156. #define G4X_M_HDMI_DAC_MIN 104
  157. #define G4X_M_HDMI_DAC_MAX 138
  158. #define G4X_M1_HDMI_DAC_MIN 16
  159. #define G4X_M1_HDMI_DAC_MAX 23
  160. #define G4X_M2_HDMI_DAC_MIN 5
  161. #define G4X_M2_HDMI_DAC_MAX 11
  162. #define G4X_P_HDMI_DAC_MIN 5
  163. #define G4X_P_HDMI_DAC_MAX 80
  164. #define G4X_P1_HDMI_DAC_MIN 1
  165. #define G4X_P1_HDMI_DAC_MAX 8
  166. #define G4X_P2_HDMI_DAC_SLOW 10
  167. #define G4X_P2_HDMI_DAC_FAST 5
  168. #define G4X_P2_HDMI_DAC_LIMIT 165000
  169. /*The parameter is for SINGLE_CHANNEL_LVDS on G4x platform*/
  170. #define G4X_DOT_SINGLE_CHANNEL_LVDS_MIN 20000
  171. #define G4X_DOT_SINGLE_CHANNEL_LVDS_MAX 115000
  172. #define G4X_N_SINGLE_CHANNEL_LVDS_MIN 1
  173. #define G4X_N_SINGLE_CHANNEL_LVDS_MAX 3
  174. #define G4X_M_SINGLE_CHANNEL_LVDS_MIN 104
  175. #define G4X_M_SINGLE_CHANNEL_LVDS_MAX 138
  176. #define G4X_M1_SINGLE_CHANNEL_LVDS_MIN 17
  177. #define G4X_M1_SINGLE_CHANNEL_LVDS_MAX 23
  178. #define G4X_M2_SINGLE_CHANNEL_LVDS_MIN 5
  179. #define G4X_M2_SINGLE_CHANNEL_LVDS_MAX 11
  180. #define G4X_P_SINGLE_CHANNEL_LVDS_MIN 28
  181. #define G4X_P_SINGLE_CHANNEL_LVDS_MAX 112
  182. #define G4X_P1_SINGLE_CHANNEL_LVDS_MIN 2
  183. #define G4X_P1_SINGLE_CHANNEL_LVDS_MAX 8
  184. #define G4X_P2_SINGLE_CHANNEL_LVDS_SLOW 14
  185. #define G4X_P2_SINGLE_CHANNEL_LVDS_FAST 14
  186. #define G4X_P2_SINGLE_CHANNEL_LVDS_LIMIT 0
  187. /*The parameter is for DUAL_CHANNEL_LVDS on G4x platform*/
  188. #define G4X_DOT_DUAL_CHANNEL_LVDS_MIN 80000
  189. #define G4X_DOT_DUAL_CHANNEL_LVDS_MAX 224000
  190. #define G4X_N_DUAL_CHANNEL_LVDS_MIN 1
  191. #define G4X_N_DUAL_CHANNEL_LVDS_MAX 3
  192. #define G4X_M_DUAL_CHANNEL_LVDS_MIN 104
  193. #define G4X_M_DUAL_CHANNEL_LVDS_MAX 138
  194. #define G4X_M1_DUAL_CHANNEL_LVDS_MIN 17
  195. #define G4X_M1_DUAL_CHANNEL_LVDS_MAX 23
  196. #define G4X_M2_DUAL_CHANNEL_LVDS_MIN 5
  197. #define G4X_M2_DUAL_CHANNEL_LVDS_MAX 11
  198. #define G4X_P_DUAL_CHANNEL_LVDS_MIN 14
  199. #define G4X_P_DUAL_CHANNEL_LVDS_MAX 42
  200. #define G4X_P1_DUAL_CHANNEL_LVDS_MIN 2
  201. #define G4X_P1_DUAL_CHANNEL_LVDS_MAX 6
  202. #define G4X_P2_DUAL_CHANNEL_LVDS_SLOW 7
  203. #define G4X_P2_DUAL_CHANNEL_LVDS_FAST 7
  204. #define G4X_P2_DUAL_CHANNEL_LVDS_LIMIT 0
  205. /*The parameter is for DISPLAY PORT on G4x platform*/
  206. #define G4X_DOT_DISPLAY_PORT_MIN 161670
  207. #define G4X_DOT_DISPLAY_PORT_MAX 227000
  208. #define G4X_N_DISPLAY_PORT_MIN 1
  209. #define G4X_N_DISPLAY_PORT_MAX 2
  210. #define G4X_M_DISPLAY_PORT_MIN 97
  211. #define G4X_M_DISPLAY_PORT_MAX 108
  212. #define G4X_M1_DISPLAY_PORT_MIN 0x10
  213. #define G4X_M1_DISPLAY_PORT_MAX 0x12
  214. #define G4X_M2_DISPLAY_PORT_MIN 0x05
  215. #define G4X_M2_DISPLAY_PORT_MAX 0x06
  216. #define G4X_P_DISPLAY_PORT_MIN 10
  217. #define G4X_P_DISPLAY_PORT_MAX 20
  218. #define G4X_P1_DISPLAY_PORT_MIN 1
  219. #define G4X_P1_DISPLAY_PORT_MAX 2
  220. #define G4X_P2_DISPLAY_PORT_SLOW 10
  221. #define G4X_P2_DISPLAY_PORT_FAST 10
  222. #define G4X_P2_DISPLAY_PORT_LIMIT 0
  223. /* Ironlake / Sandybridge */
  224. /* as we calculate clock using (register_value + 2) for
  225. N/M1/M2, so here the range value for them is (actual_value-2).
  226. */
  227. #define IRONLAKE_DOT_MIN 25000
  228. #define IRONLAKE_DOT_MAX 350000
  229. #define IRONLAKE_VCO_MIN 1760000
  230. #define IRONLAKE_VCO_MAX 3510000
  231. #define IRONLAKE_M1_MIN 12
  232. #define IRONLAKE_M1_MAX 22
  233. #define IRONLAKE_M2_MIN 5
  234. #define IRONLAKE_M2_MAX 9
  235. #define IRONLAKE_P2_DOT_LIMIT 225000 /* 225Mhz */
  236. /* We have parameter ranges for different type of outputs. */
  237. /* DAC & HDMI Refclk 120Mhz */
  238. #define IRONLAKE_DAC_N_MIN 1
  239. #define IRONLAKE_DAC_N_MAX 5
  240. #define IRONLAKE_DAC_M_MIN 79
  241. #define IRONLAKE_DAC_M_MAX 127
  242. #define IRONLAKE_DAC_P_MIN 5
  243. #define IRONLAKE_DAC_P_MAX 80
  244. #define IRONLAKE_DAC_P1_MIN 1
  245. #define IRONLAKE_DAC_P1_MAX 8
  246. #define IRONLAKE_DAC_P2_SLOW 10
  247. #define IRONLAKE_DAC_P2_FAST 5
  248. /* LVDS single-channel 120Mhz refclk */
  249. #define IRONLAKE_LVDS_S_N_MIN 1
  250. #define IRONLAKE_LVDS_S_N_MAX 3
  251. #define IRONLAKE_LVDS_S_M_MIN 79
  252. #define IRONLAKE_LVDS_S_M_MAX 118
  253. #define IRONLAKE_LVDS_S_P_MIN 28
  254. #define IRONLAKE_LVDS_S_P_MAX 112
  255. #define IRONLAKE_LVDS_S_P1_MIN 2
  256. #define IRONLAKE_LVDS_S_P1_MAX 8
  257. #define IRONLAKE_LVDS_S_P2_SLOW 14
  258. #define IRONLAKE_LVDS_S_P2_FAST 14
  259. /* LVDS dual-channel 120Mhz refclk */
  260. #define IRONLAKE_LVDS_D_N_MIN 1
  261. #define IRONLAKE_LVDS_D_N_MAX 3
  262. #define IRONLAKE_LVDS_D_M_MIN 79
  263. #define IRONLAKE_LVDS_D_M_MAX 127
  264. #define IRONLAKE_LVDS_D_P_MIN 14
  265. #define IRONLAKE_LVDS_D_P_MAX 56
  266. #define IRONLAKE_LVDS_D_P1_MIN 2
  267. #define IRONLAKE_LVDS_D_P1_MAX 8
  268. #define IRONLAKE_LVDS_D_P2_SLOW 7
  269. #define IRONLAKE_LVDS_D_P2_FAST 7
  270. /* LVDS single-channel 100Mhz refclk */
  271. #define IRONLAKE_LVDS_S_SSC_N_MIN 1
  272. #define IRONLAKE_LVDS_S_SSC_N_MAX 2
  273. #define IRONLAKE_LVDS_S_SSC_M_MIN 79
  274. #define IRONLAKE_LVDS_S_SSC_M_MAX 126
  275. #define IRONLAKE_LVDS_S_SSC_P_MIN 28
  276. #define IRONLAKE_LVDS_S_SSC_P_MAX 112
  277. #define IRONLAKE_LVDS_S_SSC_P1_MIN 2
  278. #define IRONLAKE_LVDS_S_SSC_P1_MAX 8
  279. #define IRONLAKE_LVDS_S_SSC_P2_SLOW 14
  280. #define IRONLAKE_LVDS_S_SSC_P2_FAST 14
  281. /* LVDS dual-channel 100Mhz refclk */
  282. #define IRONLAKE_LVDS_D_SSC_N_MIN 1
  283. #define IRONLAKE_LVDS_D_SSC_N_MAX 3
  284. #define IRONLAKE_LVDS_D_SSC_M_MIN 79
  285. #define IRONLAKE_LVDS_D_SSC_M_MAX 126
  286. #define IRONLAKE_LVDS_D_SSC_P_MIN 14
  287. #define IRONLAKE_LVDS_D_SSC_P_MAX 42
  288. #define IRONLAKE_LVDS_D_SSC_P1_MIN 2
  289. #define IRONLAKE_LVDS_D_SSC_P1_MAX 6
  290. #define IRONLAKE_LVDS_D_SSC_P2_SLOW 7
  291. #define IRONLAKE_LVDS_D_SSC_P2_FAST 7
  292. /* DisplayPort */
  293. #define IRONLAKE_DP_N_MIN 1
  294. #define IRONLAKE_DP_N_MAX 2
  295. #define IRONLAKE_DP_M_MIN 81
  296. #define IRONLAKE_DP_M_MAX 90
  297. #define IRONLAKE_DP_P_MIN 10
  298. #define IRONLAKE_DP_P_MAX 20
  299. #define IRONLAKE_DP_P2_FAST 10
  300. #define IRONLAKE_DP_P2_SLOW 10
  301. #define IRONLAKE_DP_P2_LIMIT 0
  302. #define IRONLAKE_DP_P1_MIN 1
  303. #define IRONLAKE_DP_P1_MAX 2
  304. /* FDI */
  305. #define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
  306. static bool
  307. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  308. int target, int refclk, intel_clock_t *best_clock);
  309. static bool
  310. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  311. int target, int refclk, intel_clock_t *best_clock);
  312. static bool
  313. intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
  314. int target, int refclk, intel_clock_t *best_clock);
  315. static bool
  316. intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
  317. int target, int refclk, intel_clock_t *best_clock);
  318. static inline u32 /* units of 100MHz */
  319. intel_fdi_link_freq(struct drm_device *dev)
  320. {
  321. if (IS_GEN5(dev)) {
  322. struct drm_i915_private *dev_priv = dev->dev_private;
  323. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  324. } else
  325. return 27;
  326. }
  327. static const intel_limit_t intel_limits_i8xx_dvo = {
  328. .dot = { .min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX },
  329. .vco = { .min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX },
  330. .n = { .min = I8XX_N_MIN, .max = I8XX_N_MAX },
  331. .m = { .min = I8XX_M_MIN, .max = I8XX_M_MAX },
  332. .m1 = { .min = I8XX_M1_MIN, .max = I8XX_M1_MAX },
  333. .m2 = { .min = I8XX_M2_MIN, .max = I8XX_M2_MAX },
  334. .p = { .min = I8XX_P_MIN, .max = I8XX_P_MAX },
  335. .p1 = { .min = I8XX_P1_MIN, .max = I8XX_P1_MAX },
  336. .p2 = { .dot_limit = I8XX_P2_SLOW_LIMIT,
  337. .p2_slow = I8XX_P2_SLOW, .p2_fast = I8XX_P2_FAST },
  338. .find_pll = intel_find_best_PLL,
  339. };
  340. static const intel_limit_t intel_limits_i8xx_lvds = {
  341. .dot = { .min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX },
  342. .vco = { .min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX },
  343. .n = { .min = I8XX_N_MIN, .max = I8XX_N_MAX },
  344. .m = { .min = I8XX_M_MIN, .max = I8XX_M_MAX },
  345. .m1 = { .min = I8XX_M1_MIN, .max = I8XX_M1_MAX },
  346. .m2 = { .min = I8XX_M2_MIN, .max = I8XX_M2_MAX },
  347. .p = { .min = I8XX_P_MIN, .max = I8XX_P_MAX },
  348. .p1 = { .min = I8XX_P1_LVDS_MIN, .max = I8XX_P1_LVDS_MAX },
  349. .p2 = { .dot_limit = I8XX_P2_SLOW_LIMIT,
  350. .p2_slow = I8XX_P2_LVDS_SLOW, .p2_fast = I8XX_P2_LVDS_FAST },
  351. .find_pll = intel_find_best_PLL,
  352. };
  353. static const intel_limit_t intel_limits_i9xx_sdvo = {
  354. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
  355. .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
  356. .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
  357. .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
  358. .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
  359. .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
  360. .p = { .min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX },
  361. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  362. .p2 = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
  363. .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast = I9XX_P2_SDVO_DAC_FAST },
  364. .find_pll = intel_find_best_PLL,
  365. };
  366. static const intel_limit_t intel_limits_i9xx_lvds = {
  367. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
  368. .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
  369. .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
  370. .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
  371. .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
  372. .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
  373. .p = { .min = I9XX_P_LVDS_MIN, .max = I9XX_P_LVDS_MAX },
  374. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  375. /* The single-channel range is 25-112Mhz, and dual-channel
  376. * is 80-224Mhz. Prefer single channel as much as possible.
  377. */
  378. .p2 = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
  379. .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_FAST },
  380. .find_pll = intel_find_best_PLL,
  381. };
  382. /* below parameter and function is for G4X Chipset Family*/
  383. static const intel_limit_t intel_limits_g4x_sdvo = {
  384. .dot = { .min = G4X_DOT_SDVO_MIN, .max = G4X_DOT_SDVO_MAX },
  385. .vco = { .min = G4X_VCO_MIN, .max = G4X_VCO_MAX},
  386. .n = { .min = G4X_N_SDVO_MIN, .max = G4X_N_SDVO_MAX },
  387. .m = { .min = G4X_M_SDVO_MIN, .max = G4X_M_SDVO_MAX },
  388. .m1 = { .min = G4X_M1_SDVO_MIN, .max = G4X_M1_SDVO_MAX },
  389. .m2 = { .min = G4X_M2_SDVO_MIN, .max = G4X_M2_SDVO_MAX },
  390. .p = { .min = G4X_P_SDVO_MIN, .max = G4X_P_SDVO_MAX },
  391. .p1 = { .min = G4X_P1_SDVO_MIN, .max = G4X_P1_SDVO_MAX},
  392. .p2 = { .dot_limit = G4X_P2_SDVO_LIMIT,
  393. .p2_slow = G4X_P2_SDVO_SLOW,
  394. .p2_fast = G4X_P2_SDVO_FAST
  395. },
  396. .find_pll = intel_g4x_find_best_PLL,
  397. };
  398. static const intel_limit_t intel_limits_g4x_hdmi = {
  399. .dot = { .min = G4X_DOT_HDMI_DAC_MIN, .max = G4X_DOT_HDMI_DAC_MAX },
  400. .vco = { .min = G4X_VCO_MIN, .max = G4X_VCO_MAX},
  401. .n = { .min = G4X_N_HDMI_DAC_MIN, .max = G4X_N_HDMI_DAC_MAX },
  402. .m = { .min = G4X_M_HDMI_DAC_MIN, .max = G4X_M_HDMI_DAC_MAX },
  403. .m1 = { .min = G4X_M1_HDMI_DAC_MIN, .max = G4X_M1_HDMI_DAC_MAX },
  404. .m2 = { .min = G4X_M2_HDMI_DAC_MIN, .max = G4X_M2_HDMI_DAC_MAX },
  405. .p = { .min = G4X_P_HDMI_DAC_MIN, .max = G4X_P_HDMI_DAC_MAX },
  406. .p1 = { .min = G4X_P1_HDMI_DAC_MIN, .max = G4X_P1_HDMI_DAC_MAX},
  407. .p2 = { .dot_limit = G4X_P2_HDMI_DAC_LIMIT,
  408. .p2_slow = G4X_P2_HDMI_DAC_SLOW,
  409. .p2_fast = G4X_P2_HDMI_DAC_FAST
  410. },
  411. .find_pll = intel_g4x_find_best_PLL,
  412. };
  413. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  414. .dot = { .min = G4X_DOT_SINGLE_CHANNEL_LVDS_MIN,
  415. .max = G4X_DOT_SINGLE_CHANNEL_LVDS_MAX },
  416. .vco = { .min = G4X_VCO_MIN,
  417. .max = G4X_VCO_MAX },
  418. .n = { .min = G4X_N_SINGLE_CHANNEL_LVDS_MIN,
  419. .max = G4X_N_SINGLE_CHANNEL_LVDS_MAX },
  420. .m = { .min = G4X_M_SINGLE_CHANNEL_LVDS_MIN,
  421. .max = G4X_M_SINGLE_CHANNEL_LVDS_MAX },
  422. .m1 = { .min = G4X_M1_SINGLE_CHANNEL_LVDS_MIN,
  423. .max = G4X_M1_SINGLE_CHANNEL_LVDS_MAX },
  424. .m2 = { .min = G4X_M2_SINGLE_CHANNEL_LVDS_MIN,
  425. .max = G4X_M2_SINGLE_CHANNEL_LVDS_MAX },
  426. .p = { .min = G4X_P_SINGLE_CHANNEL_LVDS_MIN,
  427. .max = G4X_P_SINGLE_CHANNEL_LVDS_MAX },
  428. .p1 = { .min = G4X_P1_SINGLE_CHANNEL_LVDS_MIN,
  429. .max = G4X_P1_SINGLE_CHANNEL_LVDS_MAX },
  430. .p2 = { .dot_limit = G4X_P2_SINGLE_CHANNEL_LVDS_LIMIT,
  431. .p2_slow = G4X_P2_SINGLE_CHANNEL_LVDS_SLOW,
  432. .p2_fast = G4X_P2_SINGLE_CHANNEL_LVDS_FAST
  433. },
  434. .find_pll = intel_g4x_find_best_PLL,
  435. };
  436. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  437. .dot = { .min = G4X_DOT_DUAL_CHANNEL_LVDS_MIN,
  438. .max = G4X_DOT_DUAL_CHANNEL_LVDS_MAX },
  439. .vco = { .min = G4X_VCO_MIN,
  440. .max = G4X_VCO_MAX },
  441. .n = { .min = G4X_N_DUAL_CHANNEL_LVDS_MIN,
  442. .max = G4X_N_DUAL_CHANNEL_LVDS_MAX },
  443. .m = { .min = G4X_M_DUAL_CHANNEL_LVDS_MIN,
  444. .max = G4X_M_DUAL_CHANNEL_LVDS_MAX },
  445. .m1 = { .min = G4X_M1_DUAL_CHANNEL_LVDS_MIN,
  446. .max = G4X_M1_DUAL_CHANNEL_LVDS_MAX },
  447. .m2 = { .min = G4X_M2_DUAL_CHANNEL_LVDS_MIN,
  448. .max = G4X_M2_DUAL_CHANNEL_LVDS_MAX },
  449. .p = { .min = G4X_P_DUAL_CHANNEL_LVDS_MIN,
  450. .max = G4X_P_DUAL_CHANNEL_LVDS_MAX },
  451. .p1 = { .min = G4X_P1_DUAL_CHANNEL_LVDS_MIN,
  452. .max = G4X_P1_DUAL_CHANNEL_LVDS_MAX },
  453. .p2 = { .dot_limit = G4X_P2_DUAL_CHANNEL_LVDS_LIMIT,
  454. .p2_slow = G4X_P2_DUAL_CHANNEL_LVDS_SLOW,
  455. .p2_fast = G4X_P2_DUAL_CHANNEL_LVDS_FAST
  456. },
  457. .find_pll = intel_g4x_find_best_PLL,
  458. };
  459. static const intel_limit_t intel_limits_g4x_display_port = {
  460. .dot = { .min = G4X_DOT_DISPLAY_PORT_MIN,
  461. .max = G4X_DOT_DISPLAY_PORT_MAX },
  462. .vco = { .min = G4X_VCO_MIN,
  463. .max = G4X_VCO_MAX},
  464. .n = { .min = G4X_N_DISPLAY_PORT_MIN,
  465. .max = G4X_N_DISPLAY_PORT_MAX },
  466. .m = { .min = G4X_M_DISPLAY_PORT_MIN,
  467. .max = G4X_M_DISPLAY_PORT_MAX },
  468. .m1 = { .min = G4X_M1_DISPLAY_PORT_MIN,
  469. .max = G4X_M1_DISPLAY_PORT_MAX },
  470. .m2 = { .min = G4X_M2_DISPLAY_PORT_MIN,
  471. .max = G4X_M2_DISPLAY_PORT_MAX },
  472. .p = { .min = G4X_P_DISPLAY_PORT_MIN,
  473. .max = G4X_P_DISPLAY_PORT_MAX },
  474. .p1 = { .min = G4X_P1_DISPLAY_PORT_MIN,
  475. .max = G4X_P1_DISPLAY_PORT_MAX},
  476. .p2 = { .dot_limit = G4X_P2_DISPLAY_PORT_LIMIT,
  477. .p2_slow = G4X_P2_DISPLAY_PORT_SLOW,
  478. .p2_fast = G4X_P2_DISPLAY_PORT_FAST },
  479. .find_pll = intel_find_pll_g4x_dp,
  480. };
  481. static const intel_limit_t intel_limits_pineview_sdvo = {
  482. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX},
  483. .vco = { .min = PINEVIEW_VCO_MIN, .max = PINEVIEW_VCO_MAX },
  484. .n = { .min = PINEVIEW_N_MIN, .max = PINEVIEW_N_MAX },
  485. .m = { .min = PINEVIEW_M_MIN, .max = PINEVIEW_M_MAX },
  486. .m1 = { .min = PINEVIEW_M1_MIN, .max = PINEVIEW_M1_MAX },
  487. .m2 = { .min = PINEVIEW_M2_MIN, .max = PINEVIEW_M2_MAX },
  488. .p = { .min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX },
  489. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  490. .p2 = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
  491. .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast = I9XX_P2_SDVO_DAC_FAST },
  492. .find_pll = intel_find_best_PLL,
  493. };
  494. static const intel_limit_t intel_limits_pineview_lvds = {
  495. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
  496. .vco = { .min = PINEVIEW_VCO_MIN, .max = PINEVIEW_VCO_MAX },
  497. .n = { .min = PINEVIEW_N_MIN, .max = PINEVIEW_N_MAX },
  498. .m = { .min = PINEVIEW_M_MIN, .max = PINEVIEW_M_MAX },
  499. .m1 = { .min = PINEVIEW_M1_MIN, .max = PINEVIEW_M1_MAX },
  500. .m2 = { .min = PINEVIEW_M2_MIN, .max = PINEVIEW_M2_MAX },
  501. .p = { .min = PINEVIEW_P_LVDS_MIN, .max = PINEVIEW_P_LVDS_MAX },
  502. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  503. /* Pineview only supports single-channel mode. */
  504. .p2 = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
  505. .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_SLOW },
  506. .find_pll = intel_find_best_PLL,
  507. };
  508. static const intel_limit_t intel_limits_ironlake_dac = {
  509. .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
  510. .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
  511. .n = { .min = IRONLAKE_DAC_N_MIN, .max = IRONLAKE_DAC_N_MAX },
  512. .m = { .min = IRONLAKE_DAC_M_MIN, .max = IRONLAKE_DAC_M_MAX },
  513. .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
  514. .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
  515. .p = { .min = IRONLAKE_DAC_P_MIN, .max = IRONLAKE_DAC_P_MAX },
  516. .p1 = { .min = IRONLAKE_DAC_P1_MIN, .max = IRONLAKE_DAC_P1_MAX },
  517. .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
  518. .p2_slow = IRONLAKE_DAC_P2_SLOW,
  519. .p2_fast = IRONLAKE_DAC_P2_FAST },
  520. .find_pll = intel_g4x_find_best_PLL,
  521. };
  522. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  523. .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
  524. .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
  525. .n = { .min = IRONLAKE_LVDS_S_N_MIN, .max = IRONLAKE_LVDS_S_N_MAX },
  526. .m = { .min = IRONLAKE_LVDS_S_M_MIN, .max = IRONLAKE_LVDS_S_M_MAX },
  527. .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
  528. .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
  529. .p = { .min = IRONLAKE_LVDS_S_P_MIN, .max = IRONLAKE_LVDS_S_P_MAX },
  530. .p1 = { .min = IRONLAKE_LVDS_S_P1_MIN, .max = IRONLAKE_LVDS_S_P1_MAX },
  531. .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
  532. .p2_slow = IRONLAKE_LVDS_S_P2_SLOW,
  533. .p2_fast = IRONLAKE_LVDS_S_P2_FAST },
  534. .find_pll = intel_g4x_find_best_PLL,
  535. };
  536. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  537. .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
  538. .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
  539. .n = { .min = IRONLAKE_LVDS_D_N_MIN, .max = IRONLAKE_LVDS_D_N_MAX },
  540. .m = { .min = IRONLAKE_LVDS_D_M_MIN, .max = IRONLAKE_LVDS_D_M_MAX },
  541. .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
  542. .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
  543. .p = { .min = IRONLAKE_LVDS_D_P_MIN, .max = IRONLAKE_LVDS_D_P_MAX },
  544. .p1 = { .min = IRONLAKE_LVDS_D_P1_MIN, .max = IRONLAKE_LVDS_D_P1_MAX },
  545. .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
  546. .p2_slow = IRONLAKE_LVDS_D_P2_SLOW,
  547. .p2_fast = IRONLAKE_LVDS_D_P2_FAST },
  548. .find_pll = intel_g4x_find_best_PLL,
  549. };
  550. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  551. .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
  552. .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
  553. .n = { .min = IRONLAKE_LVDS_S_SSC_N_MIN, .max = IRONLAKE_LVDS_S_SSC_N_MAX },
  554. .m = { .min = IRONLAKE_LVDS_S_SSC_M_MIN, .max = IRONLAKE_LVDS_S_SSC_M_MAX },
  555. .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
  556. .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
  557. .p = { .min = IRONLAKE_LVDS_S_SSC_P_MIN, .max = IRONLAKE_LVDS_S_SSC_P_MAX },
  558. .p1 = { .min = IRONLAKE_LVDS_S_SSC_P1_MIN,.max = IRONLAKE_LVDS_S_SSC_P1_MAX },
  559. .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
  560. .p2_slow = IRONLAKE_LVDS_S_SSC_P2_SLOW,
  561. .p2_fast = IRONLAKE_LVDS_S_SSC_P2_FAST },
  562. .find_pll = intel_g4x_find_best_PLL,
  563. };
  564. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  565. .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
  566. .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
  567. .n = { .min = IRONLAKE_LVDS_D_SSC_N_MIN, .max = IRONLAKE_LVDS_D_SSC_N_MAX },
  568. .m = { .min = IRONLAKE_LVDS_D_SSC_M_MIN, .max = IRONLAKE_LVDS_D_SSC_M_MAX },
  569. .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
  570. .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
  571. .p = { .min = IRONLAKE_LVDS_D_SSC_P_MIN, .max = IRONLAKE_LVDS_D_SSC_P_MAX },
  572. .p1 = { .min = IRONLAKE_LVDS_D_SSC_P1_MIN,.max = IRONLAKE_LVDS_D_SSC_P1_MAX },
  573. .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
  574. .p2_slow = IRONLAKE_LVDS_D_SSC_P2_SLOW,
  575. .p2_fast = IRONLAKE_LVDS_D_SSC_P2_FAST },
  576. .find_pll = intel_g4x_find_best_PLL,
  577. };
  578. static const intel_limit_t intel_limits_ironlake_display_port = {
  579. .dot = { .min = IRONLAKE_DOT_MIN,
  580. .max = IRONLAKE_DOT_MAX },
  581. .vco = { .min = IRONLAKE_VCO_MIN,
  582. .max = IRONLAKE_VCO_MAX},
  583. .n = { .min = IRONLAKE_DP_N_MIN,
  584. .max = IRONLAKE_DP_N_MAX },
  585. .m = { .min = IRONLAKE_DP_M_MIN,
  586. .max = IRONLAKE_DP_M_MAX },
  587. .m1 = { .min = IRONLAKE_M1_MIN,
  588. .max = IRONLAKE_M1_MAX },
  589. .m2 = { .min = IRONLAKE_M2_MIN,
  590. .max = IRONLAKE_M2_MAX },
  591. .p = { .min = IRONLAKE_DP_P_MIN,
  592. .max = IRONLAKE_DP_P_MAX },
  593. .p1 = { .min = IRONLAKE_DP_P1_MIN,
  594. .max = IRONLAKE_DP_P1_MAX},
  595. .p2 = { .dot_limit = IRONLAKE_DP_P2_LIMIT,
  596. .p2_slow = IRONLAKE_DP_P2_SLOW,
  597. .p2_fast = IRONLAKE_DP_P2_FAST },
  598. .find_pll = intel_find_pll_ironlake_dp,
  599. };
  600. static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc)
  601. {
  602. struct drm_device *dev = crtc->dev;
  603. struct drm_i915_private *dev_priv = dev->dev_private;
  604. const intel_limit_t *limit;
  605. int refclk = 120;
  606. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  607. if (dev_priv->lvds_use_ssc && dev_priv->lvds_ssc_freq == 100)
  608. refclk = 100;
  609. if ((I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) ==
  610. LVDS_CLKB_POWER_UP) {
  611. /* LVDS dual channel */
  612. if (refclk == 100)
  613. limit = &intel_limits_ironlake_dual_lvds_100m;
  614. else
  615. limit = &intel_limits_ironlake_dual_lvds;
  616. } else {
  617. if (refclk == 100)
  618. limit = &intel_limits_ironlake_single_lvds_100m;
  619. else
  620. limit = &intel_limits_ironlake_single_lvds;
  621. }
  622. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  623. HAS_eDP)
  624. limit = &intel_limits_ironlake_display_port;
  625. else
  626. limit = &intel_limits_ironlake_dac;
  627. return limit;
  628. }
  629. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  630. {
  631. struct drm_device *dev = crtc->dev;
  632. struct drm_i915_private *dev_priv = dev->dev_private;
  633. const intel_limit_t *limit;
  634. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  635. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  636. LVDS_CLKB_POWER_UP)
  637. /* LVDS with dual channel */
  638. limit = &intel_limits_g4x_dual_channel_lvds;
  639. else
  640. /* LVDS with dual channel */
  641. limit = &intel_limits_g4x_single_channel_lvds;
  642. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  643. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  644. limit = &intel_limits_g4x_hdmi;
  645. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  646. limit = &intel_limits_g4x_sdvo;
  647. } else if (intel_pipe_has_type (crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  648. limit = &intel_limits_g4x_display_port;
  649. } else /* The option is for other outputs */
  650. limit = &intel_limits_i9xx_sdvo;
  651. return limit;
  652. }
  653. static const intel_limit_t *intel_limit(struct drm_crtc *crtc)
  654. {
  655. struct drm_device *dev = crtc->dev;
  656. const intel_limit_t *limit;
  657. if (HAS_PCH_SPLIT(dev))
  658. limit = intel_ironlake_limit(crtc);
  659. else if (IS_G4X(dev)) {
  660. limit = intel_g4x_limit(crtc);
  661. } else if (IS_PINEVIEW(dev)) {
  662. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  663. limit = &intel_limits_pineview_lvds;
  664. else
  665. limit = &intel_limits_pineview_sdvo;
  666. } else if (!IS_GEN2(dev)) {
  667. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  668. limit = &intel_limits_i9xx_lvds;
  669. else
  670. limit = &intel_limits_i9xx_sdvo;
  671. } else {
  672. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  673. limit = &intel_limits_i8xx_lvds;
  674. else
  675. limit = &intel_limits_i8xx_dvo;
  676. }
  677. return limit;
  678. }
  679. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  680. static void pineview_clock(int refclk, intel_clock_t *clock)
  681. {
  682. clock->m = clock->m2 + 2;
  683. clock->p = clock->p1 * clock->p2;
  684. clock->vco = refclk * clock->m / clock->n;
  685. clock->dot = clock->vco / clock->p;
  686. }
  687. static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
  688. {
  689. if (IS_PINEVIEW(dev)) {
  690. pineview_clock(refclk, clock);
  691. return;
  692. }
  693. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  694. clock->p = clock->p1 * clock->p2;
  695. clock->vco = refclk * clock->m / (clock->n + 2);
  696. clock->dot = clock->vco / clock->p;
  697. }
  698. /**
  699. * Returns whether any output on the specified pipe is of the specified type
  700. */
  701. bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
  702. {
  703. struct drm_device *dev = crtc->dev;
  704. struct drm_mode_config *mode_config = &dev->mode_config;
  705. struct intel_encoder *encoder;
  706. list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
  707. if (encoder->base.crtc == crtc && encoder->type == type)
  708. return true;
  709. return false;
  710. }
  711. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  712. /**
  713. * Returns whether the given set of divisors are valid for a given refclk with
  714. * the given connectors.
  715. */
  716. static bool intel_PLL_is_valid(struct drm_crtc *crtc, intel_clock_t *clock)
  717. {
  718. const intel_limit_t *limit = intel_limit (crtc);
  719. struct drm_device *dev = crtc->dev;
  720. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  721. INTELPllInvalid ("p1 out of range\n");
  722. if (clock->p < limit->p.min || limit->p.max < clock->p)
  723. INTELPllInvalid ("p out of range\n");
  724. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  725. INTELPllInvalid ("m2 out of range\n");
  726. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  727. INTELPllInvalid ("m1 out of range\n");
  728. if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
  729. INTELPllInvalid ("m1 <= m2\n");
  730. if (clock->m < limit->m.min || limit->m.max < clock->m)
  731. INTELPllInvalid ("m out of range\n");
  732. if (clock->n < limit->n.min || limit->n.max < clock->n)
  733. INTELPllInvalid ("n out of range\n");
  734. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  735. INTELPllInvalid ("vco out of range\n");
  736. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  737. * connector, etc., rather than just a single range.
  738. */
  739. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  740. INTELPllInvalid ("dot out of range\n");
  741. return true;
  742. }
  743. static bool
  744. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  745. int target, int refclk, intel_clock_t *best_clock)
  746. {
  747. struct drm_device *dev = crtc->dev;
  748. struct drm_i915_private *dev_priv = dev->dev_private;
  749. intel_clock_t clock;
  750. int err = target;
  751. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  752. (I915_READ(LVDS)) != 0) {
  753. /*
  754. * For LVDS, if the panel is on, just rely on its current
  755. * settings for dual-channel. We haven't figured out how to
  756. * reliably set up different single/dual channel state, if we
  757. * even can.
  758. */
  759. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  760. LVDS_CLKB_POWER_UP)
  761. clock.p2 = limit->p2.p2_fast;
  762. else
  763. clock.p2 = limit->p2.p2_slow;
  764. } else {
  765. if (target < limit->p2.dot_limit)
  766. clock.p2 = limit->p2.p2_slow;
  767. else
  768. clock.p2 = limit->p2.p2_fast;
  769. }
  770. memset (best_clock, 0, sizeof (*best_clock));
  771. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  772. clock.m1++) {
  773. for (clock.m2 = limit->m2.min;
  774. clock.m2 <= limit->m2.max; clock.m2++) {
  775. /* m1 is always 0 in Pineview */
  776. if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
  777. break;
  778. for (clock.n = limit->n.min;
  779. clock.n <= limit->n.max; clock.n++) {
  780. for (clock.p1 = limit->p1.min;
  781. clock.p1 <= limit->p1.max; clock.p1++) {
  782. int this_err;
  783. intel_clock(dev, refclk, &clock);
  784. if (!intel_PLL_is_valid(crtc, &clock))
  785. continue;
  786. this_err = abs(clock.dot - target);
  787. if (this_err < err) {
  788. *best_clock = clock;
  789. err = this_err;
  790. }
  791. }
  792. }
  793. }
  794. }
  795. return (err != target);
  796. }
  797. static bool
  798. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  799. int target, int refclk, intel_clock_t *best_clock)
  800. {
  801. struct drm_device *dev = crtc->dev;
  802. struct drm_i915_private *dev_priv = dev->dev_private;
  803. intel_clock_t clock;
  804. int max_n;
  805. bool found;
  806. /* approximately equals target * 0.00585 */
  807. int err_most = (target >> 8) + (target >> 9);
  808. found = false;
  809. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  810. int lvds_reg;
  811. if (HAS_PCH_SPLIT(dev))
  812. lvds_reg = PCH_LVDS;
  813. else
  814. lvds_reg = LVDS;
  815. if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==
  816. LVDS_CLKB_POWER_UP)
  817. clock.p2 = limit->p2.p2_fast;
  818. else
  819. clock.p2 = limit->p2.p2_slow;
  820. } else {
  821. if (target < limit->p2.dot_limit)
  822. clock.p2 = limit->p2.p2_slow;
  823. else
  824. clock.p2 = limit->p2.p2_fast;
  825. }
  826. memset(best_clock, 0, sizeof(*best_clock));
  827. max_n = limit->n.max;
  828. /* based on hardware requirement, prefer smaller n to precision */
  829. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  830. /* based on hardware requirement, prefere larger m1,m2 */
  831. for (clock.m1 = limit->m1.max;
  832. clock.m1 >= limit->m1.min; clock.m1--) {
  833. for (clock.m2 = limit->m2.max;
  834. clock.m2 >= limit->m2.min; clock.m2--) {
  835. for (clock.p1 = limit->p1.max;
  836. clock.p1 >= limit->p1.min; clock.p1--) {
  837. int this_err;
  838. intel_clock(dev, refclk, &clock);
  839. if (!intel_PLL_is_valid(crtc, &clock))
  840. continue;
  841. this_err = abs(clock.dot - target) ;
  842. if (this_err < err_most) {
  843. *best_clock = clock;
  844. err_most = this_err;
  845. max_n = clock.n;
  846. found = true;
  847. }
  848. }
  849. }
  850. }
  851. }
  852. return found;
  853. }
  854. static bool
  855. intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  856. int target, int refclk, intel_clock_t *best_clock)
  857. {
  858. struct drm_device *dev = crtc->dev;
  859. intel_clock_t clock;
  860. if (target < 200000) {
  861. clock.n = 1;
  862. clock.p1 = 2;
  863. clock.p2 = 10;
  864. clock.m1 = 12;
  865. clock.m2 = 9;
  866. } else {
  867. clock.n = 2;
  868. clock.p1 = 1;
  869. clock.p2 = 10;
  870. clock.m1 = 14;
  871. clock.m2 = 8;
  872. }
  873. intel_clock(dev, refclk, &clock);
  874. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  875. return true;
  876. }
  877. /* DisplayPort has only two frequencies, 162MHz and 270MHz */
  878. static bool
  879. intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  880. int target, int refclk, intel_clock_t *best_clock)
  881. {
  882. intel_clock_t clock;
  883. if (target < 200000) {
  884. clock.p1 = 2;
  885. clock.p2 = 10;
  886. clock.n = 2;
  887. clock.m1 = 23;
  888. clock.m2 = 8;
  889. } else {
  890. clock.p1 = 1;
  891. clock.p2 = 10;
  892. clock.n = 1;
  893. clock.m1 = 14;
  894. clock.m2 = 2;
  895. }
  896. clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
  897. clock.p = (clock.p1 * clock.p2);
  898. clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
  899. clock.vco = 0;
  900. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  901. return true;
  902. }
  903. /**
  904. * intel_wait_for_vblank - wait for vblank on a given pipe
  905. * @dev: drm device
  906. * @pipe: pipe to wait for
  907. *
  908. * Wait for vblank to occur on a given pipe. Needed for various bits of
  909. * mode setting code.
  910. */
  911. void intel_wait_for_vblank(struct drm_device *dev, int pipe)
  912. {
  913. struct drm_i915_private *dev_priv = dev->dev_private;
  914. int pipestat_reg = (pipe == 0 ? PIPEASTAT : PIPEBSTAT);
  915. /* Clear existing vblank status. Note this will clear any other
  916. * sticky status fields as well.
  917. *
  918. * This races with i915_driver_irq_handler() with the result
  919. * that either function could miss a vblank event. Here it is not
  920. * fatal, as we will either wait upon the next vblank interrupt or
  921. * timeout. Generally speaking intel_wait_for_vblank() is only
  922. * called during modeset at which time the GPU should be idle and
  923. * should *not* be performing page flips and thus not waiting on
  924. * vblanks...
  925. * Currently, the result of us stealing a vblank from the irq
  926. * handler is that a single frame will be skipped during swapbuffers.
  927. */
  928. I915_WRITE(pipestat_reg,
  929. I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
  930. /* Wait for vblank interrupt bit to set */
  931. if (wait_for(I915_READ(pipestat_reg) &
  932. PIPE_VBLANK_INTERRUPT_STATUS,
  933. 50))
  934. DRM_DEBUG_KMS("vblank wait timed out\n");
  935. }
  936. /*
  937. * intel_wait_for_pipe_off - wait for pipe to turn off
  938. * @dev: drm device
  939. * @pipe: pipe to wait for
  940. *
  941. * After disabling a pipe, we can't wait for vblank in the usual way,
  942. * spinning on the vblank interrupt status bit, since we won't actually
  943. * see an interrupt when the pipe is disabled.
  944. *
  945. * On Gen4 and above:
  946. * wait for the pipe register state bit to turn off
  947. *
  948. * Otherwise:
  949. * wait for the display line value to settle (it usually
  950. * ends up stopping at the start of the next frame).
  951. *
  952. */
  953. void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
  954. {
  955. struct drm_i915_private *dev_priv = dev->dev_private;
  956. if (INTEL_INFO(dev)->gen >= 4) {
  957. int reg = PIPECONF(pipe);
  958. /* Wait for the Pipe State to go off */
  959. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  960. 100))
  961. DRM_DEBUG_KMS("pipe_off wait timed out\n");
  962. } else {
  963. u32 last_line;
  964. int reg = PIPEDSL(pipe);
  965. unsigned long timeout = jiffies + msecs_to_jiffies(100);
  966. /* Wait for the display line to settle */
  967. do {
  968. last_line = I915_READ(reg) & DSL_LINEMASK;
  969. mdelay(5);
  970. } while (((I915_READ(reg) & DSL_LINEMASK) != last_line) &&
  971. time_after(timeout, jiffies));
  972. if (time_after(jiffies, timeout))
  973. DRM_DEBUG_KMS("pipe_off wait timed out\n");
  974. }
  975. }
  976. static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  977. {
  978. struct drm_device *dev = crtc->dev;
  979. struct drm_i915_private *dev_priv = dev->dev_private;
  980. struct drm_framebuffer *fb = crtc->fb;
  981. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  982. struct drm_i915_gem_object *obj_priv = to_intel_bo(intel_fb->obj);
  983. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  984. int plane, i;
  985. u32 fbc_ctl, fbc_ctl2;
  986. if (fb->pitch == dev_priv->cfb_pitch &&
  987. obj_priv->fence_reg == dev_priv->cfb_fence &&
  988. intel_crtc->plane == dev_priv->cfb_plane &&
  989. I915_READ(FBC_CONTROL) & FBC_CTL_EN)
  990. return;
  991. i8xx_disable_fbc(dev);
  992. dev_priv->cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
  993. if (fb->pitch < dev_priv->cfb_pitch)
  994. dev_priv->cfb_pitch = fb->pitch;
  995. /* FBC_CTL wants 64B units */
  996. dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
  997. dev_priv->cfb_fence = obj_priv->fence_reg;
  998. dev_priv->cfb_plane = intel_crtc->plane;
  999. plane = dev_priv->cfb_plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
  1000. /* Clear old tags */
  1001. for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
  1002. I915_WRITE(FBC_TAG + (i * 4), 0);
  1003. /* Set it up... */
  1004. fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | plane;
  1005. if (obj_priv->tiling_mode != I915_TILING_NONE)
  1006. fbc_ctl2 |= FBC_CTL_CPU_FENCE;
  1007. I915_WRITE(FBC_CONTROL2, fbc_ctl2);
  1008. I915_WRITE(FBC_FENCE_OFF, crtc->y);
  1009. /* enable it... */
  1010. fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
  1011. if (IS_I945GM(dev))
  1012. fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
  1013. fbc_ctl |= (dev_priv->cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
  1014. fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
  1015. if (obj_priv->tiling_mode != I915_TILING_NONE)
  1016. fbc_ctl |= dev_priv->cfb_fence;
  1017. I915_WRITE(FBC_CONTROL, fbc_ctl);
  1018. DRM_DEBUG_KMS("enabled FBC, pitch %ld, yoff %d, plane %d, ",
  1019. dev_priv->cfb_pitch, crtc->y, dev_priv->cfb_plane);
  1020. }
  1021. void i8xx_disable_fbc(struct drm_device *dev)
  1022. {
  1023. struct drm_i915_private *dev_priv = dev->dev_private;
  1024. u32 fbc_ctl;
  1025. /* Disable compression */
  1026. fbc_ctl = I915_READ(FBC_CONTROL);
  1027. if ((fbc_ctl & FBC_CTL_EN) == 0)
  1028. return;
  1029. fbc_ctl &= ~FBC_CTL_EN;
  1030. I915_WRITE(FBC_CONTROL, fbc_ctl);
  1031. /* Wait for compressing bit to clear */
  1032. if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
  1033. DRM_DEBUG_KMS("FBC idle timed out\n");
  1034. return;
  1035. }
  1036. DRM_DEBUG_KMS("disabled FBC\n");
  1037. }
  1038. static bool i8xx_fbc_enabled(struct drm_device *dev)
  1039. {
  1040. struct drm_i915_private *dev_priv = dev->dev_private;
  1041. return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
  1042. }
  1043. static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1044. {
  1045. struct drm_device *dev = crtc->dev;
  1046. struct drm_i915_private *dev_priv = dev->dev_private;
  1047. struct drm_framebuffer *fb = crtc->fb;
  1048. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1049. struct drm_i915_gem_object *obj_priv = to_intel_bo(intel_fb->obj);
  1050. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1051. int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
  1052. unsigned long stall_watermark = 200;
  1053. u32 dpfc_ctl;
  1054. dpfc_ctl = I915_READ(DPFC_CONTROL);
  1055. if (dpfc_ctl & DPFC_CTL_EN) {
  1056. if (dev_priv->cfb_pitch == dev_priv->cfb_pitch / 64 - 1 &&
  1057. dev_priv->cfb_fence == obj_priv->fence_reg &&
  1058. dev_priv->cfb_plane == intel_crtc->plane &&
  1059. dev_priv->cfb_y == crtc->y)
  1060. return;
  1061. I915_WRITE(DPFC_CONTROL, dpfc_ctl & ~DPFC_CTL_EN);
  1062. POSTING_READ(DPFC_CONTROL);
  1063. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1064. }
  1065. dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
  1066. dev_priv->cfb_fence = obj_priv->fence_reg;
  1067. dev_priv->cfb_plane = intel_crtc->plane;
  1068. dev_priv->cfb_y = crtc->y;
  1069. dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
  1070. if (obj_priv->tiling_mode != I915_TILING_NONE) {
  1071. dpfc_ctl |= DPFC_CTL_FENCE_EN | dev_priv->cfb_fence;
  1072. I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
  1073. } else {
  1074. I915_WRITE(DPFC_CHICKEN, ~DPFC_HT_MODIFY);
  1075. }
  1076. I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  1077. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  1078. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  1079. I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
  1080. /* enable it... */
  1081. I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
  1082. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  1083. }
  1084. void g4x_disable_fbc(struct drm_device *dev)
  1085. {
  1086. struct drm_i915_private *dev_priv = dev->dev_private;
  1087. u32 dpfc_ctl;
  1088. /* Disable compression */
  1089. dpfc_ctl = I915_READ(DPFC_CONTROL);
  1090. if (dpfc_ctl & DPFC_CTL_EN) {
  1091. dpfc_ctl &= ~DPFC_CTL_EN;
  1092. I915_WRITE(DPFC_CONTROL, dpfc_ctl);
  1093. DRM_DEBUG_KMS("disabled FBC\n");
  1094. }
  1095. }
  1096. static bool g4x_fbc_enabled(struct drm_device *dev)
  1097. {
  1098. struct drm_i915_private *dev_priv = dev->dev_private;
  1099. return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
  1100. }
  1101. static void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1102. {
  1103. struct drm_device *dev = crtc->dev;
  1104. struct drm_i915_private *dev_priv = dev->dev_private;
  1105. struct drm_framebuffer *fb = crtc->fb;
  1106. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1107. struct drm_i915_gem_object *obj_priv = to_intel_bo(intel_fb->obj);
  1108. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1109. int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
  1110. unsigned long stall_watermark = 200;
  1111. u32 dpfc_ctl;
  1112. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  1113. if (dpfc_ctl & DPFC_CTL_EN) {
  1114. if (dev_priv->cfb_pitch == dev_priv->cfb_pitch / 64 - 1 &&
  1115. dev_priv->cfb_fence == obj_priv->fence_reg &&
  1116. dev_priv->cfb_plane == intel_crtc->plane &&
  1117. dev_priv->cfb_offset == obj_priv->gtt_offset &&
  1118. dev_priv->cfb_y == crtc->y)
  1119. return;
  1120. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl & ~DPFC_CTL_EN);
  1121. POSTING_READ(ILK_DPFC_CONTROL);
  1122. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1123. }
  1124. dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
  1125. dev_priv->cfb_fence = obj_priv->fence_reg;
  1126. dev_priv->cfb_plane = intel_crtc->plane;
  1127. dev_priv->cfb_offset = obj_priv->gtt_offset;
  1128. dev_priv->cfb_y = crtc->y;
  1129. dpfc_ctl &= DPFC_RESERVED;
  1130. dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
  1131. if (obj_priv->tiling_mode != I915_TILING_NONE) {
  1132. dpfc_ctl |= (DPFC_CTL_FENCE_EN | dev_priv->cfb_fence);
  1133. I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
  1134. } else {
  1135. I915_WRITE(ILK_DPFC_CHICKEN, ~DPFC_HT_MODIFY);
  1136. }
  1137. I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  1138. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  1139. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  1140. I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
  1141. I915_WRITE(ILK_FBC_RT_BASE, obj_priv->gtt_offset | ILK_FBC_RT_VALID);
  1142. /* enable it... */
  1143. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
  1144. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  1145. }
  1146. void ironlake_disable_fbc(struct drm_device *dev)
  1147. {
  1148. struct drm_i915_private *dev_priv = dev->dev_private;
  1149. u32 dpfc_ctl;
  1150. /* Disable compression */
  1151. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  1152. if (dpfc_ctl & DPFC_CTL_EN) {
  1153. dpfc_ctl &= ~DPFC_CTL_EN;
  1154. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
  1155. DRM_DEBUG_KMS("disabled FBC\n");
  1156. }
  1157. }
  1158. static bool ironlake_fbc_enabled(struct drm_device *dev)
  1159. {
  1160. struct drm_i915_private *dev_priv = dev->dev_private;
  1161. return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
  1162. }
  1163. bool intel_fbc_enabled(struct drm_device *dev)
  1164. {
  1165. struct drm_i915_private *dev_priv = dev->dev_private;
  1166. if (!dev_priv->display.fbc_enabled)
  1167. return false;
  1168. return dev_priv->display.fbc_enabled(dev);
  1169. }
  1170. void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1171. {
  1172. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  1173. if (!dev_priv->display.enable_fbc)
  1174. return;
  1175. dev_priv->display.enable_fbc(crtc, interval);
  1176. }
  1177. void intel_disable_fbc(struct drm_device *dev)
  1178. {
  1179. struct drm_i915_private *dev_priv = dev->dev_private;
  1180. if (!dev_priv->display.disable_fbc)
  1181. return;
  1182. dev_priv->display.disable_fbc(dev);
  1183. }
  1184. /**
  1185. * intel_update_fbc - enable/disable FBC as needed
  1186. * @dev: the drm_device
  1187. *
  1188. * Set up the framebuffer compression hardware at mode set time. We
  1189. * enable it if possible:
  1190. * - plane A only (on pre-965)
  1191. * - no pixel mulitply/line duplication
  1192. * - no alpha buffer discard
  1193. * - no dual wide
  1194. * - framebuffer <= 2048 in width, 1536 in height
  1195. *
  1196. * We can't assume that any compression will take place (worst case),
  1197. * so the compressed buffer has to be the same size as the uncompressed
  1198. * one. It also must reside (along with the line length buffer) in
  1199. * stolen memory.
  1200. *
  1201. * We need to enable/disable FBC on a global basis.
  1202. */
  1203. static void intel_update_fbc(struct drm_device *dev)
  1204. {
  1205. struct drm_i915_private *dev_priv = dev->dev_private;
  1206. struct drm_crtc *crtc = NULL, *tmp_crtc;
  1207. struct intel_crtc *intel_crtc;
  1208. struct drm_framebuffer *fb;
  1209. struct intel_framebuffer *intel_fb;
  1210. struct drm_i915_gem_object *obj_priv;
  1211. DRM_DEBUG_KMS("\n");
  1212. if (!i915_powersave)
  1213. return;
  1214. if (!I915_HAS_FBC(dev))
  1215. return;
  1216. /*
  1217. * If FBC is already on, we just have to verify that we can
  1218. * keep it that way...
  1219. * Need to disable if:
  1220. * - more than one pipe is active
  1221. * - changing FBC params (stride, fence, mode)
  1222. * - new fb is too large to fit in compressed buffer
  1223. * - going to an unsupported config (interlace, pixel multiply, etc.)
  1224. */
  1225. list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
  1226. if (tmp_crtc->enabled) {
  1227. if (crtc) {
  1228. DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
  1229. dev_priv->no_fbc_reason = FBC_MULTIPLE_PIPES;
  1230. goto out_disable;
  1231. }
  1232. crtc = tmp_crtc;
  1233. }
  1234. }
  1235. if (!crtc || crtc->fb == NULL) {
  1236. DRM_DEBUG_KMS("no output, disabling\n");
  1237. dev_priv->no_fbc_reason = FBC_NO_OUTPUT;
  1238. goto out_disable;
  1239. }
  1240. intel_crtc = to_intel_crtc(crtc);
  1241. fb = crtc->fb;
  1242. intel_fb = to_intel_framebuffer(fb);
  1243. obj_priv = to_intel_bo(intel_fb->obj);
  1244. if (intel_fb->obj->size > dev_priv->cfb_size) {
  1245. DRM_DEBUG_KMS("framebuffer too large, disabling "
  1246. "compression\n");
  1247. dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
  1248. goto out_disable;
  1249. }
  1250. if ((crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) ||
  1251. (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)) {
  1252. DRM_DEBUG_KMS("mode incompatible with compression, "
  1253. "disabling\n");
  1254. dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;
  1255. goto out_disable;
  1256. }
  1257. if ((crtc->mode.hdisplay > 2048) ||
  1258. (crtc->mode.vdisplay > 1536)) {
  1259. DRM_DEBUG_KMS("mode too large for compression, disabling\n");
  1260. dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;
  1261. goto out_disable;
  1262. }
  1263. if ((IS_I915GM(dev) || IS_I945GM(dev)) && intel_crtc->plane != 0) {
  1264. DRM_DEBUG_KMS("plane not 0, disabling compression\n");
  1265. dev_priv->no_fbc_reason = FBC_BAD_PLANE;
  1266. goto out_disable;
  1267. }
  1268. if (obj_priv->tiling_mode != I915_TILING_X) {
  1269. DRM_DEBUG_KMS("framebuffer not tiled, disabling compression\n");
  1270. dev_priv->no_fbc_reason = FBC_NOT_TILED;
  1271. goto out_disable;
  1272. }
  1273. /* If the kernel debugger is active, always disable compression */
  1274. if (in_dbg_master())
  1275. goto out_disable;
  1276. intel_enable_fbc(crtc, 500);
  1277. return;
  1278. out_disable:
  1279. /* Multiple disables should be harmless */
  1280. if (intel_fbc_enabled(dev)) {
  1281. DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
  1282. intel_disable_fbc(dev);
  1283. }
  1284. }
  1285. int
  1286. intel_pin_and_fence_fb_obj(struct drm_device *dev,
  1287. struct drm_gem_object *obj,
  1288. bool pipelined)
  1289. {
  1290. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1291. u32 alignment;
  1292. int ret;
  1293. switch (obj_priv->tiling_mode) {
  1294. case I915_TILING_NONE:
  1295. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1296. alignment = 128 * 1024;
  1297. else if (INTEL_INFO(dev)->gen >= 4)
  1298. alignment = 4 * 1024;
  1299. else
  1300. alignment = 64 * 1024;
  1301. break;
  1302. case I915_TILING_X:
  1303. /* pin() will align the object as required by fence */
  1304. alignment = 0;
  1305. break;
  1306. case I915_TILING_Y:
  1307. /* FIXME: Is this true? */
  1308. DRM_ERROR("Y tiled not allowed for scan out buffers\n");
  1309. return -EINVAL;
  1310. default:
  1311. BUG();
  1312. }
  1313. ret = i915_gem_object_pin(obj, alignment, true);
  1314. if (ret)
  1315. return ret;
  1316. ret = i915_gem_object_set_to_display_plane(obj, pipelined);
  1317. if (ret)
  1318. goto err_unpin;
  1319. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1320. * fence, whereas 965+ only requires a fence if using
  1321. * framebuffer compression. For simplicity, we always install
  1322. * a fence as the cost is not that onerous.
  1323. */
  1324. if (obj_priv->tiling_mode != I915_TILING_NONE) {
  1325. ret = i915_gem_object_get_fence_reg(obj, false);
  1326. if (ret)
  1327. goto err_unpin;
  1328. }
  1329. return 0;
  1330. err_unpin:
  1331. i915_gem_object_unpin(obj);
  1332. return ret;
  1333. }
  1334. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  1335. static int
  1336. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1337. int x, int y, enum mode_set_atomic state)
  1338. {
  1339. struct drm_device *dev = crtc->dev;
  1340. struct drm_i915_private *dev_priv = dev->dev_private;
  1341. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1342. struct intel_framebuffer *intel_fb;
  1343. struct drm_i915_gem_object *obj_priv;
  1344. struct drm_gem_object *obj;
  1345. int plane = intel_crtc->plane;
  1346. unsigned long Start, Offset;
  1347. u32 dspcntr;
  1348. u32 reg;
  1349. switch (plane) {
  1350. case 0:
  1351. case 1:
  1352. break;
  1353. default:
  1354. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1355. return -EINVAL;
  1356. }
  1357. intel_fb = to_intel_framebuffer(fb);
  1358. obj = intel_fb->obj;
  1359. obj_priv = to_intel_bo(obj);
  1360. reg = DSPCNTR(plane);
  1361. dspcntr = I915_READ(reg);
  1362. /* Mask out pixel format bits in case we change it */
  1363. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1364. switch (fb->bits_per_pixel) {
  1365. case 8:
  1366. dspcntr |= DISPPLANE_8BPP;
  1367. break;
  1368. case 16:
  1369. if (fb->depth == 15)
  1370. dspcntr |= DISPPLANE_15_16BPP;
  1371. else
  1372. dspcntr |= DISPPLANE_16BPP;
  1373. break;
  1374. case 24:
  1375. case 32:
  1376. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  1377. break;
  1378. default:
  1379. DRM_ERROR("Unknown color depth\n");
  1380. return -EINVAL;
  1381. }
  1382. if (INTEL_INFO(dev)->gen >= 4) {
  1383. if (obj_priv->tiling_mode != I915_TILING_NONE)
  1384. dspcntr |= DISPPLANE_TILED;
  1385. else
  1386. dspcntr &= ~DISPPLANE_TILED;
  1387. }
  1388. if (HAS_PCH_SPLIT(dev))
  1389. /* must disable */
  1390. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  1391. I915_WRITE(reg, dspcntr);
  1392. Start = obj_priv->gtt_offset;
  1393. Offset = y * fb->pitch + x * (fb->bits_per_pixel / 8);
  1394. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  1395. Start, Offset, x, y, fb->pitch);
  1396. I915_WRITE(DSPSTRIDE(plane), fb->pitch);
  1397. if (INTEL_INFO(dev)->gen >= 4) {
  1398. I915_WRITE(DSPSURF(plane), Start);
  1399. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  1400. I915_WRITE(DSPADDR(plane), Offset);
  1401. } else
  1402. I915_WRITE(DSPADDR(plane), Start + Offset);
  1403. POSTING_READ(reg);
  1404. intel_update_fbc(dev);
  1405. intel_increase_pllclock(crtc);
  1406. return 0;
  1407. }
  1408. static int
  1409. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  1410. struct drm_framebuffer *old_fb)
  1411. {
  1412. struct drm_device *dev = crtc->dev;
  1413. struct drm_i915_master_private *master_priv;
  1414. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1415. int ret;
  1416. /* no fb bound */
  1417. if (!crtc->fb) {
  1418. DRM_DEBUG_KMS("No FB bound\n");
  1419. return 0;
  1420. }
  1421. switch (intel_crtc->plane) {
  1422. case 0:
  1423. case 1:
  1424. break;
  1425. default:
  1426. return -EINVAL;
  1427. }
  1428. mutex_lock(&dev->struct_mutex);
  1429. ret = intel_pin_and_fence_fb_obj(dev,
  1430. to_intel_framebuffer(crtc->fb)->obj,
  1431. false);
  1432. if (ret != 0) {
  1433. mutex_unlock(&dev->struct_mutex);
  1434. return ret;
  1435. }
  1436. if (old_fb) {
  1437. struct drm_i915_private *dev_priv = dev->dev_private;
  1438. struct drm_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
  1439. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1440. wait_event(dev_priv->pending_flip_queue,
  1441. atomic_read(&obj_priv->pending_flip) == 0);
  1442. /* Big Hammer, we also need to ensure that any pending
  1443. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  1444. * current scanout is retired before unpinning the old
  1445. * framebuffer.
  1446. */
  1447. ret = i915_gem_object_flush_gpu(obj_priv, false);
  1448. if (ret) {
  1449. i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
  1450. mutex_unlock(&dev->struct_mutex);
  1451. return ret;
  1452. }
  1453. }
  1454. ret = intel_pipe_set_base_atomic(crtc, crtc->fb, x, y,
  1455. LEAVE_ATOMIC_MODE_SET);
  1456. if (ret) {
  1457. i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
  1458. mutex_unlock(&dev->struct_mutex);
  1459. return ret;
  1460. }
  1461. if (old_fb)
  1462. i915_gem_object_unpin(to_intel_framebuffer(old_fb)->obj);
  1463. mutex_unlock(&dev->struct_mutex);
  1464. if (!dev->primary->master)
  1465. return 0;
  1466. master_priv = dev->primary->master->driver_priv;
  1467. if (!master_priv->sarea_priv)
  1468. return 0;
  1469. if (intel_crtc->pipe) {
  1470. master_priv->sarea_priv->pipeB_x = x;
  1471. master_priv->sarea_priv->pipeB_y = y;
  1472. } else {
  1473. master_priv->sarea_priv->pipeA_x = x;
  1474. master_priv->sarea_priv->pipeA_y = y;
  1475. }
  1476. return 0;
  1477. }
  1478. static void ironlake_set_pll_edp(struct drm_crtc *crtc, int clock)
  1479. {
  1480. struct drm_device *dev = crtc->dev;
  1481. struct drm_i915_private *dev_priv = dev->dev_private;
  1482. u32 dpa_ctl;
  1483. DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
  1484. dpa_ctl = I915_READ(DP_A);
  1485. dpa_ctl &= ~DP_PLL_FREQ_MASK;
  1486. if (clock < 200000) {
  1487. u32 temp;
  1488. dpa_ctl |= DP_PLL_FREQ_160MHZ;
  1489. /* workaround for 160Mhz:
  1490. 1) program 0x4600c bits 15:0 = 0x8124
  1491. 2) program 0x46010 bit 0 = 1
  1492. 3) program 0x46034 bit 24 = 1
  1493. 4) program 0x64000 bit 14 = 1
  1494. */
  1495. temp = I915_READ(0x4600c);
  1496. temp &= 0xffff0000;
  1497. I915_WRITE(0x4600c, temp | 0x8124);
  1498. temp = I915_READ(0x46010);
  1499. I915_WRITE(0x46010, temp | 1);
  1500. temp = I915_READ(0x46034);
  1501. I915_WRITE(0x46034, temp | (1 << 24));
  1502. } else {
  1503. dpa_ctl |= DP_PLL_FREQ_270MHZ;
  1504. }
  1505. I915_WRITE(DP_A, dpa_ctl);
  1506. POSTING_READ(DP_A);
  1507. udelay(500);
  1508. }
  1509. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  1510. {
  1511. struct drm_device *dev = crtc->dev;
  1512. struct drm_i915_private *dev_priv = dev->dev_private;
  1513. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1514. int pipe = intel_crtc->pipe;
  1515. u32 reg, temp;
  1516. /* enable normal train */
  1517. reg = FDI_TX_CTL(pipe);
  1518. temp = I915_READ(reg);
  1519. temp &= ~FDI_LINK_TRAIN_NONE;
  1520. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  1521. I915_WRITE(reg, temp);
  1522. reg = FDI_RX_CTL(pipe);
  1523. temp = I915_READ(reg);
  1524. if (HAS_PCH_CPT(dev)) {
  1525. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1526. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  1527. } else {
  1528. temp &= ~FDI_LINK_TRAIN_NONE;
  1529. temp |= FDI_LINK_TRAIN_NONE;
  1530. }
  1531. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  1532. /* wait one idle pattern time */
  1533. POSTING_READ(reg);
  1534. udelay(1000);
  1535. }
  1536. /* The FDI link training functions for ILK/Ibexpeak. */
  1537. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  1538. {
  1539. struct drm_device *dev = crtc->dev;
  1540. struct drm_i915_private *dev_priv = dev->dev_private;
  1541. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1542. int pipe = intel_crtc->pipe;
  1543. u32 reg, temp, tries;
  1544. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  1545. for train result */
  1546. reg = FDI_RX_IMR(pipe);
  1547. temp = I915_READ(reg);
  1548. temp &= ~FDI_RX_SYMBOL_LOCK;
  1549. temp &= ~FDI_RX_BIT_LOCK;
  1550. I915_WRITE(reg, temp);
  1551. I915_READ(reg);
  1552. udelay(150);
  1553. /* enable CPU FDI TX and PCH FDI RX */
  1554. reg = FDI_TX_CTL(pipe);
  1555. temp = I915_READ(reg);
  1556. temp &= ~(7 << 19);
  1557. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  1558. temp &= ~FDI_LINK_TRAIN_NONE;
  1559. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1560. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  1561. reg = FDI_RX_CTL(pipe);
  1562. temp = I915_READ(reg);
  1563. temp &= ~FDI_LINK_TRAIN_NONE;
  1564. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1565. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  1566. POSTING_READ(reg);
  1567. udelay(150);
  1568. /* Ironlake workaround, enable clock pointer after FDI enable*/
  1569. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_ENABLE);
  1570. reg = FDI_RX_IIR(pipe);
  1571. for (tries = 0; tries < 5; tries++) {
  1572. temp = I915_READ(reg);
  1573. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1574. if ((temp & FDI_RX_BIT_LOCK)) {
  1575. DRM_DEBUG_KMS("FDI train 1 done.\n");
  1576. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  1577. break;
  1578. }
  1579. }
  1580. if (tries == 5)
  1581. DRM_ERROR("FDI train 1 fail!\n");
  1582. /* Train 2 */
  1583. reg = FDI_TX_CTL(pipe);
  1584. temp = I915_READ(reg);
  1585. temp &= ~FDI_LINK_TRAIN_NONE;
  1586. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1587. I915_WRITE(reg, temp);
  1588. reg = FDI_RX_CTL(pipe);
  1589. temp = I915_READ(reg);
  1590. temp &= ~FDI_LINK_TRAIN_NONE;
  1591. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1592. I915_WRITE(reg, temp);
  1593. POSTING_READ(reg);
  1594. udelay(150);
  1595. reg = FDI_RX_IIR(pipe);
  1596. for (tries = 0; tries < 5; tries++) {
  1597. temp = I915_READ(reg);
  1598. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1599. if (temp & FDI_RX_SYMBOL_LOCK) {
  1600. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  1601. DRM_DEBUG_KMS("FDI train 2 done.\n");
  1602. break;
  1603. }
  1604. }
  1605. if (tries == 5)
  1606. DRM_ERROR("FDI train 2 fail!\n");
  1607. DRM_DEBUG_KMS("FDI train done\n");
  1608. }
  1609. static const int const snb_b_fdi_train_param [] = {
  1610. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  1611. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  1612. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  1613. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  1614. };
  1615. /* The FDI link training functions for SNB/Cougarpoint. */
  1616. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  1617. {
  1618. struct drm_device *dev = crtc->dev;
  1619. struct drm_i915_private *dev_priv = dev->dev_private;
  1620. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1621. int pipe = intel_crtc->pipe;
  1622. u32 reg, temp, i;
  1623. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  1624. for train result */
  1625. reg = FDI_RX_IMR(pipe);
  1626. temp = I915_READ(reg);
  1627. temp &= ~FDI_RX_SYMBOL_LOCK;
  1628. temp &= ~FDI_RX_BIT_LOCK;
  1629. I915_WRITE(reg, temp);
  1630. POSTING_READ(reg);
  1631. udelay(150);
  1632. /* enable CPU FDI TX and PCH FDI RX */
  1633. reg = FDI_TX_CTL(pipe);
  1634. temp = I915_READ(reg);
  1635. temp &= ~(7 << 19);
  1636. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  1637. temp &= ~FDI_LINK_TRAIN_NONE;
  1638. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1639. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1640. /* SNB-B */
  1641. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  1642. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  1643. reg = FDI_RX_CTL(pipe);
  1644. temp = I915_READ(reg);
  1645. if (HAS_PCH_CPT(dev)) {
  1646. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1647. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  1648. } else {
  1649. temp &= ~FDI_LINK_TRAIN_NONE;
  1650. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1651. }
  1652. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  1653. POSTING_READ(reg);
  1654. udelay(150);
  1655. for (i = 0; i < 4; i++ ) {
  1656. reg = FDI_TX_CTL(pipe);
  1657. temp = I915_READ(reg);
  1658. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1659. temp |= snb_b_fdi_train_param[i];
  1660. I915_WRITE(reg, temp);
  1661. POSTING_READ(reg);
  1662. udelay(500);
  1663. reg = FDI_RX_IIR(pipe);
  1664. temp = I915_READ(reg);
  1665. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1666. if (temp & FDI_RX_BIT_LOCK) {
  1667. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  1668. DRM_DEBUG_KMS("FDI train 1 done.\n");
  1669. break;
  1670. }
  1671. }
  1672. if (i == 4)
  1673. DRM_ERROR("FDI train 1 fail!\n");
  1674. /* Train 2 */
  1675. reg = FDI_TX_CTL(pipe);
  1676. temp = I915_READ(reg);
  1677. temp &= ~FDI_LINK_TRAIN_NONE;
  1678. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1679. if (IS_GEN6(dev)) {
  1680. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1681. /* SNB-B */
  1682. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  1683. }
  1684. I915_WRITE(reg, temp);
  1685. reg = FDI_RX_CTL(pipe);
  1686. temp = I915_READ(reg);
  1687. if (HAS_PCH_CPT(dev)) {
  1688. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1689. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  1690. } else {
  1691. temp &= ~FDI_LINK_TRAIN_NONE;
  1692. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1693. }
  1694. I915_WRITE(reg, temp);
  1695. POSTING_READ(reg);
  1696. udelay(150);
  1697. for (i = 0; i < 4; i++ ) {
  1698. reg = FDI_TX_CTL(pipe);
  1699. temp = I915_READ(reg);
  1700. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1701. temp |= snb_b_fdi_train_param[i];
  1702. I915_WRITE(reg, temp);
  1703. POSTING_READ(reg);
  1704. udelay(500);
  1705. reg = FDI_RX_IIR(pipe);
  1706. temp = I915_READ(reg);
  1707. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1708. if (temp & FDI_RX_SYMBOL_LOCK) {
  1709. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  1710. DRM_DEBUG_KMS("FDI train 2 done.\n");
  1711. break;
  1712. }
  1713. }
  1714. if (i == 4)
  1715. DRM_ERROR("FDI train 2 fail!\n");
  1716. DRM_DEBUG_KMS("FDI train done.\n");
  1717. }
  1718. static void ironlake_fdi_enable(struct drm_crtc *crtc)
  1719. {
  1720. struct drm_device *dev = crtc->dev;
  1721. struct drm_i915_private *dev_priv = dev->dev_private;
  1722. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1723. int pipe = intel_crtc->pipe;
  1724. u32 reg, temp;
  1725. /* Write the TU size bits so error detection works */
  1726. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  1727. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  1728. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  1729. reg = FDI_RX_CTL(pipe);
  1730. temp = I915_READ(reg);
  1731. temp &= ~((0x7 << 19) | (0x7 << 16));
  1732. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  1733. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  1734. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  1735. POSTING_READ(reg);
  1736. udelay(200);
  1737. /* Switch from Rawclk to PCDclk */
  1738. temp = I915_READ(reg);
  1739. I915_WRITE(reg, temp | FDI_PCDCLK);
  1740. POSTING_READ(reg);
  1741. udelay(200);
  1742. /* Enable CPU FDI TX PLL, always on for Ironlake */
  1743. reg = FDI_TX_CTL(pipe);
  1744. temp = I915_READ(reg);
  1745. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  1746. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  1747. POSTING_READ(reg);
  1748. udelay(100);
  1749. }
  1750. }
  1751. static void intel_flush_display_plane(struct drm_device *dev,
  1752. int plane)
  1753. {
  1754. struct drm_i915_private *dev_priv = dev->dev_private;
  1755. u32 reg = DSPADDR(plane);
  1756. I915_WRITE(reg, I915_READ(reg));
  1757. }
  1758. /*
  1759. * When we disable a pipe, we need to clear any pending scanline wait events
  1760. * to avoid hanging the ring, which we assume we are waiting on.
  1761. */
  1762. static void intel_clear_scanline_wait(struct drm_device *dev)
  1763. {
  1764. struct drm_i915_private *dev_priv = dev->dev_private;
  1765. struct intel_ring_buffer *ring;
  1766. u32 tmp;
  1767. if (IS_GEN2(dev))
  1768. /* Can't break the hang on i8xx */
  1769. return;
  1770. ring = &dev_priv->render_ring;
  1771. tmp = I915_READ_CTL(ring);
  1772. if (tmp & RING_WAIT)
  1773. I915_WRITE_CTL(ring, tmp);
  1774. }
  1775. static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  1776. {
  1777. struct drm_i915_gem_object *obj_priv;
  1778. struct drm_i915_private *dev_priv;
  1779. if (crtc->fb == NULL)
  1780. return;
  1781. obj_priv = to_intel_bo(to_intel_framebuffer(crtc->fb)->obj);
  1782. dev_priv = crtc->dev->dev_private;
  1783. wait_event(dev_priv->pending_flip_queue,
  1784. atomic_read(&obj_priv->pending_flip) == 0);
  1785. }
  1786. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  1787. {
  1788. struct drm_device *dev = crtc->dev;
  1789. struct drm_i915_private *dev_priv = dev->dev_private;
  1790. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1791. int pipe = intel_crtc->pipe;
  1792. int plane = intel_crtc->plane;
  1793. u32 reg, temp;
  1794. if (intel_crtc->active)
  1795. return;
  1796. intel_crtc->active = true;
  1797. intel_update_watermarks(dev);
  1798. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  1799. temp = I915_READ(PCH_LVDS);
  1800. if ((temp & LVDS_PORT_EN) == 0)
  1801. I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
  1802. }
  1803. ironlake_fdi_enable(crtc);
  1804. /* Enable panel fitting for LVDS */
  1805. if (dev_priv->pch_pf_size &&
  1806. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) || HAS_eDP)) {
  1807. /* Force use of hard-coded filter coefficients
  1808. * as some pre-programmed values are broken,
  1809. * e.g. x201.
  1810. */
  1811. I915_WRITE(pipe ? PFB_CTL_1 : PFA_CTL_1,
  1812. PF_ENABLE | PF_FILTER_MED_3x3);
  1813. I915_WRITE(pipe ? PFB_WIN_POS : PFA_WIN_POS,
  1814. dev_priv->pch_pf_pos);
  1815. I915_WRITE(pipe ? PFB_WIN_SZ : PFA_WIN_SZ,
  1816. dev_priv->pch_pf_size);
  1817. }
  1818. /* Enable CPU pipe */
  1819. reg = PIPECONF(pipe);
  1820. temp = I915_READ(reg);
  1821. if ((temp & PIPECONF_ENABLE) == 0) {
  1822. I915_WRITE(reg, temp | PIPECONF_ENABLE);
  1823. POSTING_READ(reg);
  1824. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1825. }
  1826. /* configure and enable CPU plane */
  1827. reg = DSPCNTR(plane);
  1828. temp = I915_READ(reg);
  1829. if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
  1830. I915_WRITE(reg, temp | DISPLAY_PLANE_ENABLE);
  1831. intel_flush_display_plane(dev, plane);
  1832. }
  1833. /* For PCH output, training FDI link */
  1834. if (IS_GEN6(dev))
  1835. gen6_fdi_link_train(crtc);
  1836. else
  1837. ironlake_fdi_link_train(crtc);
  1838. /* enable PCH DPLL */
  1839. reg = PCH_DPLL(pipe);
  1840. temp = I915_READ(reg);
  1841. if ((temp & DPLL_VCO_ENABLE) == 0) {
  1842. I915_WRITE(reg, temp | DPLL_VCO_ENABLE);
  1843. POSTING_READ(reg);
  1844. udelay(200);
  1845. }
  1846. if (HAS_PCH_CPT(dev)) {
  1847. /* Be sure PCH DPLL SEL is set */
  1848. temp = I915_READ(PCH_DPLL_SEL);
  1849. if (pipe == 0 && (temp & TRANSA_DPLL_ENABLE) == 0)
  1850. temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
  1851. else if (pipe == 1 && (temp & TRANSB_DPLL_ENABLE) == 0)
  1852. temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  1853. I915_WRITE(PCH_DPLL_SEL, temp);
  1854. }
  1855. /* set transcoder timing */
  1856. I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
  1857. I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
  1858. I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
  1859. I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
  1860. I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
  1861. I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
  1862. intel_fdi_normal_train(crtc);
  1863. /* For PCH DP, enable TRANS_DP_CTL */
  1864. if (HAS_PCH_CPT(dev) &&
  1865. intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  1866. reg = TRANS_DP_CTL(pipe);
  1867. temp = I915_READ(reg);
  1868. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  1869. TRANS_DP_SYNC_MASK);
  1870. temp |= (TRANS_DP_OUTPUT_ENABLE |
  1871. TRANS_DP_ENH_FRAMING);
  1872. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  1873. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  1874. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  1875. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  1876. switch (intel_trans_dp_port_sel(crtc)) {
  1877. case PCH_DP_B:
  1878. temp |= TRANS_DP_PORT_SEL_B;
  1879. break;
  1880. case PCH_DP_C:
  1881. temp |= TRANS_DP_PORT_SEL_C;
  1882. break;
  1883. case PCH_DP_D:
  1884. temp |= TRANS_DP_PORT_SEL_D;
  1885. break;
  1886. default:
  1887. DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
  1888. temp |= TRANS_DP_PORT_SEL_B;
  1889. break;
  1890. }
  1891. I915_WRITE(reg, temp);
  1892. }
  1893. /* enable PCH transcoder */
  1894. reg = TRANSCONF(pipe);
  1895. temp = I915_READ(reg);
  1896. /*
  1897. * make the BPC in transcoder be consistent with
  1898. * that in pipeconf reg.
  1899. */
  1900. temp &= ~PIPE_BPC_MASK;
  1901. temp |= I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK;
  1902. I915_WRITE(reg, temp | TRANS_ENABLE);
  1903. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1904. DRM_ERROR("failed to enable transcoder %d\n", pipe);
  1905. intel_crtc_load_lut(crtc);
  1906. intel_update_fbc(dev);
  1907. intel_crtc_update_cursor(crtc, true);
  1908. }
  1909. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  1910. {
  1911. struct drm_device *dev = crtc->dev;
  1912. struct drm_i915_private *dev_priv = dev->dev_private;
  1913. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1914. int pipe = intel_crtc->pipe;
  1915. int plane = intel_crtc->plane;
  1916. u32 reg, temp;
  1917. if (!intel_crtc->active)
  1918. return;
  1919. intel_crtc_wait_for_pending_flips(crtc);
  1920. drm_vblank_off(dev, pipe);
  1921. intel_crtc_update_cursor(crtc, false);
  1922. /* Disable display plane */
  1923. reg = DSPCNTR(plane);
  1924. temp = I915_READ(reg);
  1925. if (temp & DISPLAY_PLANE_ENABLE) {
  1926. I915_WRITE(reg, temp & ~DISPLAY_PLANE_ENABLE);
  1927. intel_flush_display_plane(dev, plane);
  1928. }
  1929. if (dev_priv->cfb_plane == plane &&
  1930. dev_priv->display.disable_fbc)
  1931. dev_priv->display.disable_fbc(dev);
  1932. /* disable cpu pipe, disable after all planes disabled */
  1933. reg = PIPECONF(pipe);
  1934. temp = I915_READ(reg);
  1935. if (temp & PIPECONF_ENABLE) {
  1936. I915_WRITE(reg, temp & ~PIPECONF_ENABLE);
  1937. POSTING_READ(reg);
  1938. /* wait for cpu pipe off, pipe state */
  1939. intel_wait_for_pipe_off(dev, intel_crtc->pipe);
  1940. }
  1941. /* Disable PF */
  1942. I915_WRITE(pipe ? PFB_CTL_1 : PFA_CTL_1, 0);
  1943. I915_WRITE(pipe ? PFB_WIN_SZ : PFA_WIN_SZ, 0);
  1944. /* disable CPU FDI tx and PCH FDI rx */
  1945. reg = FDI_TX_CTL(pipe);
  1946. temp = I915_READ(reg);
  1947. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  1948. POSTING_READ(reg);
  1949. reg = FDI_RX_CTL(pipe);
  1950. temp = I915_READ(reg);
  1951. temp &= ~(0x7 << 16);
  1952. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  1953. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  1954. POSTING_READ(reg);
  1955. udelay(100);
  1956. /* Ironlake workaround, disable clock pointer after downing FDI */
  1957. if (HAS_PCH_IBX(dev))
  1958. I915_WRITE(FDI_RX_CHICKEN(pipe),
  1959. I915_READ(FDI_RX_CHICKEN(pipe) &
  1960. ~FDI_RX_PHASE_SYNC_POINTER_ENABLE));
  1961. /* still set train pattern 1 */
  1962. reg = FDI_TX_CTL(pipe);
  1963. temp = I915_READ(reg);
  1964. temp &= ~FDI_LINK_TRAIN_NONE;
  1965. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1966. I915_WRITE(reg, temp);
  1967. reg = FDI_RX_CTL(pipe);
  1968. temp = I915_READ(reg);
  1969. if (HAS_PCH_CPT(dev)) {
  1970. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1971. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  1972. } else {
  1973. temp &= ~FDI_LINK_TRAIN_NONE;
  1974. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1975. }
  1976. /* BPC in FDI rx is consistent with that in PIPECONF */
  1977. temp &= ~(0x07 << 16);
  1978. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  1979. I915_WRITE(reg, temp);
  1980. POSTING_READ(reg);
  1981. udelay(100);
  1982. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  1983. temp = I915_READ(PCH_LVDS);
  1984. if (temp & LVDS_PORT_EN) {
  1985. I915_WRITE(PCH_LVDS, temp & ~LVDS_PORT_EN);
  1986. POSTING_READ(PCH_LVDS);
  1987. udelay(100);
  1988. }
  1989. }
  1990. /* disable PCH transcoder */
  1991. reg = TRANSCONF(plane);
  1992. temp = I915_READ(reg);
  1993. if (temp & TRANS_ENABLE) {
  1994. I915_WRITE(reg, temp & ~TRANS_ENABLE);
  1995. /* wait for PCH transcoder off, transcoder state */
  1996. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1997. DRM_ERROR("failed to disable transcoder\n");
  1998. }
  1999. if (HAS_PCH_CPT(dev)) {
  2000. /* disable TRANS_DP_CTL */
  2001. reg = TRANS_DP_CTL(pipe);
  2002. temp = I915_READ(reg);
  2003. temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
  2004. I915_WRITE(reg, temp);
  2005. /* disable DPLL_SEL */
  2006. temp = I915_READ(PCH_DPLL_SEL);
  2007. if (pipe == 0)
  2008. temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
  2009. else
  2010. temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  2011. I915_WRITE(PCH_DPLL_SEL, temp);
  2012. }
  2013. /* disable PCH DPLL */
  2014. reg = PCH_DPLL(pipe);
  2015. temp = I915_READ(reg);
  2016. I915_WRITE(reg, temp & ~DPLL_VCO_ENABLE);
  2017. /* Switch from PCDclk to Rawclk */
  2018. reg = FDI_RX_CTL(pipe);
  2019. temp = I915_READ(reg);
  2020. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2021. /* Disable CPU FDI TX PLL */
  2022. reg = FDI_TX_CTL(pipe);
  2023. temp = I915_READ(reg);
  2024. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2025. POSTING_READ(reg);
  2026. udelay(100);
  2027. reg = FDI_RX_CTL(pipe);
  2028. temp = I915_READ(reg);
  2029. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2030. /* Wait for the clocks to turn off. */
  2031. POSTING_READ(reg);
  2032. udelay(100);
  2033. intel_crtc->active = false;
  2034. intel_update_watermarks(dev);
  2035. intel_update_fbc(dev);
  2036. intel_clear_scanline_wait(dev);
  2037. }
  2038. static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
  2039. {
  2040. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2041. int pipe = intel_crtc->pipe;
  2042. int plane = intel_crtc->plane;
  2043. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2044. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2045. */
  2046. switch (mode) {
  2047. case DRM_MODE_DPMS_ON:
  2048. case DRM_MODE_DPMS_STANDBY:
  2049. case DRM_MODE_DPMS_SUSPEND:
  2050. DRM_DEBUG_KMS("crtc %d/%d dpms on\n", pipe, plane);
  2051. ironlake_crtc_enable(crtc);
  2052. break;
  2053. case DRM_MODE_DPMS_OFF:
  2054. DRM_DEBUG_KMS("crtc %d/%d dpms off\n", pipe, plane);
  2055. ironlake_crtc_disable(crtc);
  2056. break;
  2057. }
  2058. }
  2059. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  2060. {
  2061. if (!enable && intel_crtc->overlay) {
  2062. struct drm_device *dev = intel_crtc->base.dev;
  2063. mutex_lock(&dev->struct_mutex);
  2064. (void) intel_overlay_switch_off(intel_crtc->overlay, false);
  2065. mutex_unlock(&dev->struct_mutex);
  2066. }
  2067. /* Let userspace switch the overlay on again. In most cases userspace
  2068. * has to recompute where to put it anyway.
  2069. */
  2070. }
  2071. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  2072. {
  2073. struct drm_device *dev = crtc->dev;
  2074. struct drm_i915_private *dev_priv = dev->dev_private;
  2075. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2076. int pipe = intel_crtc->pipe;
  2077. int plane = intel_crtc->plane;
  2078. u32 reg, temp;
  2079. if (intel_crtc->active)
  2080. return;
  2081. intel_crtc->active = true;
  2082. intel_update_watermarks(dev);
  2083. /* Enable the DPLL */
  2084. reg = DPLL(pipe);
  2085. temp = I915_READ(reg);
  2086. if ((temp & DPLL_VCO_ENABLE) == 0) {
  2087. I915_WRITE(reg, temp);
  2088. /* Wait for the clocks to stabilize. */
  2089. POSTING_READ(reg);
  2090. udelay(150);
  2091. I915_WRITE(reg, temp | DPLL_VCO_ENABLE);
  2092. /* Wait for the clocks to stabilize. */
  2093. POSTING_READ(reg);
  2094. udelay(150);
  2095. I915_WRITE(reg, temp | DPLL_VCO_ENABLE);
  2096. /* Wait for the clocks to stabilize. */
  2097. POSTING_READ(reg);
  2098. udelay(150);
  2099. }
  2100. /* Enable the pipe */
  2101. reg = PIPECONF(pipe);
  2102. temp = I915_READ(reg);
  2103. if ((temp & PIPECONF_ENABLE) == 0)
  2104. I915_WRITE(reg, temp | PIPECONF_ENABLE);
  2105. /* Enable the plane */
  2106. reg = DSPCNTR(plane);
  2107. temp = I915_READ(reg);
  2108. if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
  2109. I915_WRITE(reg, temp | DISPLAY_PLANE_ENABLE);
  2110. intel_flush_display_plane(dev, plane);
  2111. }
  2112. intel_crtc_load_lut(crtc);
  2113. intel_update_fbc(dev);
  2114. /* Give the overlay scaler a chance to enable if it's on this pipe */
  2115. intel_crtc_dpms_overlay(intel_crtc, true);
  2116. intel_crtc_update_cursor(crtc, true);
  2117. }
  2118. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  2119. {
  2120. struct drm_device *dev = crtc->dev;
  2121. struct drm_i915_private *dev_priv = dev->dev_private;
  2122. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2123. int pipe = intel_crtc->pipe;
  2124. int plane = intel_crtc->plane;
  2125. u32 reg, temp;
  2126. if (!intel_crtc->active)
  2127. return;
  2128. /* Give the overlay scaler a chance to disable if it's on this pipe */
  2129. intel_crtc_wait_for_pending_flips(crtc);
  2130. drm_vblank_off(dev, pipe);
  2131. intel_crtc_dpms_overlay(intel_crtc, false);
  2132. intel_crtc_update_cursor(crtc, false);
  2133. if (dev_priv->cfb_plane == plane &&
  2134. dev_priv->display.disable_fbc)
  2135. dev_priv->display.disable_fbc(dev);
  2136. /* Disable display plane */
  2137. reg = DSPCNTR(plane);
  2138. temp = I915_READ(reg);
  2139. if (temp & DISPLAY_PLANE_ENABLE) {
  2140. I915_WRITE(reg, temp & ~DISPLAY_PLANE_ENABLE);
  2141. /* Flush the plane changes */
  2142. intel_flush_display_plane(dev, plane);
  2143. /* Wait for vblank for the disable to take effect */
  2144. if (IS_GEN2(dev))
  2145. intel_wait_for_vblank(dev, pipe);
  2146. }
  2147. /* Don't disable pipe A or pipe A PLLs if needed */
  2148. if (pipe == 0 && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  2149. goto done;
  2150. /* Next, disable display pipes */
  2151. reg = PIPECONF(pipe);
  2152. temp = I915_READ(reg);
  2153. if (temp & PIPECONF_ENABLE) {
  2154. I915_WRITE(reg, temp & ~PIPECONF_ENABLE);
  2155. /* Wait for the pipe to turn off */
  2156. POSTING_READ(reg);
  2157. intel_wait_for_pipe_off(dev, pipe);
  2158. }
  2159. reg = DPLL(pipe);
  2160. temp = I915_READ(reg);
  2161. if (temp & DPLL_VCO_ENABLE) {
  2162. I915_WRITE(reg, temp & ~DPLL_VCO_ENABLE);
  2163. /* Wait for the clocks to turn off. */
  2164. POSTING_READ(reg);
  2165. udelay(150);
  2166. }
  2167. done:
  2168. intel_crtc->active = false;
  2169. intel_update_fbc(dev);
  2170. intel_update_watermarks(dev);
  2171. intel_clear_scanline_wait(dev);
  2172. }
  2173. static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
  2174. {
  2175. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2176. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2177. */
  2178. switch (mode) {
  2179. case DRM_MODE_DPMS_ON:
  2180. case DRM_MODE_DPMS_STANDBY:
  2181. case DRM_MODE_DPMS_SUSPEND:
  2182. i9xx_crtc_enable(crtc);
  2183. break;
  2184. case DRM_MODE_DPMS_OFF:
  2185. i9xx_crtc_disable(crtc);
  2186. break;
  2187. }
  2188. }
  2189. /**
  2190. * Sets the power management mode of the pipe and plane.
  2191. */
  2192. static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
  2193. {
  2194. struct drm_device *dev = crtc->dev;
  2195. struct drm_i915_private *dev_priv = dev->dev_private;
  2196. struct drm_i915_master_private *master_priv;
  2197. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2198. int pipe = intel_crtc->pipe;
  2199. bool enabled;
  2200. if (intel_crtc->dpms_mode == mode)
  2201. return;
  2202. intel_crtc->dpms_mode = mode;
  2203. dev_priv->display.dpms(crtc, mode);
  2204. if (!dev->primary->master)
  2205. return;
  2206. master_priv = dev->primary->master->driver_priv;
  2207. if (!master_priv->sarea_priv)
  2208. return;
  2209. enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
  2210. switch (pipe) {
  2211. case 0:
  2212. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  2213. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  2214. break;
  2215. case 1:
  2216. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  2217. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  2218. break;
  2219. default:
  2220. DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
  2221. break;
  2222. }
  2223. }
  2224. static void intel_crtc_disable(struct drm_crtc *crtc)
  2225. {
  2226. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  2227. struct drm_device *dev = crtc->dev;
  2228. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  2229. if (crtc->fb) {
  2230. mutex_lock(&dev->struct_mutex);
  2231. i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
  2232. mutex_unlock(&dev->struct_mutex);
  2233. }
  2234. }
  2235. /* Prepare for a mode set.
  2236. *
  2237. * Note we could be a lot smarter here. We need to figure out which outputs
  2238. * will be enabled, which disabled (in short, how the config will changes)
  2239. * and perform the minimum necessary steps to accomplish that, e.g. updating
  2240. * watermarks, FBC configuration, making sure PLLs are programmed correctly,
  2241. * panel fitting is in the proper state, etc.
  2242. */
  2243. static void i9xx_crtc_prepare(struct drm_crtc *crtc)
  2244. {
  2245. i9xx_crtc_disable(crtc);
  2246. }
  2247. static void i9xx_crtc_commit(struct drm_crtc *crtc)
  2248. {
  2249. i9xx_crtc_enable(crtc);
  2250. }
  2251. static void ironlake_crtc_prepare(struct drm_crtc *crtc)
  2252. {
  2253. ironlake_crtc_disable(crtc);
  2254. }
  2255. static void ironlake_crtc_commit(struct drm_crtc *crtc)
  2256. {
  2257. ironlake_crtc_enable(crtc);
  2258. }
  2259. void intel_encoder_prepare (struct drm_encoder *encoder)
  2260. {
  2261. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2262. /* lvds has its own version of prepare see intel_lvds_prepare */
  2263. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  2264. }
  2265. void intel_encoder_commit (struct drm_encoder *encoder)
  2266. {
  2267. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2268. /* lvds has its own version of commit see intel_lvds_commit */
  2269. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  2270. }
  2271. void intel_encoder_destroy(struct drm_encoder *encoder)
  2272. {
  2273. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  2274. drm_encoder_cleanup(encoder);
  2275. kfree(intel_encoder);
  2276. }
  2277. static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
  2278. struct drm_display_mode *mode,
  2279. struct drm_display_mode *adjusted_mode)
  2280. {
  2281. struct drm_device *dev = crtc->dev;
  2282. if (HAS_PCH_SPLIT(dev)) {
  2283. /* FDI link clock is fixed at 2.7G */
  2284. if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
  2285. return false;
  2286. }
  2287. /* XXX some encoders set the crtcinfo, others don't.
  2288. * Obviously we need some form of conflict resolution here...
  2289. */
  2290. if (adjusted_mode->crtc_htotal == 0)
  2291. drm_mode_set_crtcinfo(adjusted_mode, 0);
  2292. return true;
  2293. }
  2294. static int i945_get_display_clock_speed(struct drm_device *dev)
  2295. {
  2296. return 400000;
  2297. }
  2298. static int i915_get_display_clock_speed(struct drm_device *dev)
  2299. {
  2300. return 333000;
  2301. }
  2302. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  2303. {
  2304. return 200000;
  2305. }
  2306. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  2307. {
  2308. u16 gcfgc = 0;
  2309. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  2310. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  2311. return 133000;
  2312. else {
  2313. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  2314. case GC_DISPLAY_CLOCK_333_MHZ:
  2315. return 333000;
  2316. default:
  2317. case GC_DISPLAY_CLOCK_190_200_MHZ:
  2318. return 190000;
  2319. }
  2320. }
  2321. }
  2322. static int i865_get_display_clock_speed(struct drm_device *dev)
  2323. {
  2324. return 266000;
  2325. }
  2326. static int i855_get_display_clock_speed(struct drm_device *dev)
  2327. {
  2328. u16 hpllcc = 0;
  2329. /* Assume that the hardware is in the high speed state. This
  2330. * should be the default.
  2331. */
  2332. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  2333. case GC_CLOCK_133_200:
  2334. case GC_CLOCK_100_200:
  2335. return 200000;
  2336. case GC_CLOCK_166_250:
  2337. return 250000;
  2338. case GC_CLOCK_100_133:
  2339. return 133000;
  2340. }
  2341. /* Shouldn't happen */
  2342. return 0;
  2343. }
  2344. static int i830_get_display_clock_speed(struct drm_device *dev)
  2345. {
  2346. return 133000;
  2347. }
  2348. struct fdi_m_n {
  2349. u32 tu;
  2350. u32 gmch_m;
  2351. u32 gmch_n;
  2352. u32 link_m;
  2353. u32 link_n;
  2354. };
  2355. static void
  2356. fdi_reduce_ratio(u32 *num, u32 *den)
  2357. {
  2358. while (*num > 0xffffff || *den > 0xffffff) {
  2359. *num >>= 1;
  2360. *den >>= 1;
  2361. }
  2362. }
  2363. #define DATA_N 0x800000
  2364. #define LINK_N 0x80000
  2365. static void
  2366. ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
  2367. int link_clock, struct fdi_m_n *m_n)
  2368. {
  2369. u64 temp;
  2370. m_n->tu = 64; /* default size */
  2371. temp = (u64) DATA_N * pixel_clock;
  2372. temp = div_u64(temp, link_clock);
  2373. m_n->gmch_m = div_u64(temp * bits_per_pixel, nlanes);
  2374. m_n->gmch_m >>= 3; /* convert to bytes_per_pixel */
  2375. m_n->gmch_n = DATA_N;
  2376. fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
  2377. temp = (u64) LINK_N * pixel_clock;
  2378. m_n->link_m = div_u64(temp, link_clock);
  2379. m_n->link_n = LINK_N;
  2380. fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
  2381. }
  2382. struct intel_watermark_params {
  2383. unsigned long fifo_size;
  2384. unsigned long max_wm;
  2385. unsigned long default_wm;
  2386. unsigned long guard_size;
  2387. unsigned long cacheline_size;
  2388. };
  2389. /* Pineview has different values for various configs */
  2390. static struct intel_watermark_params pineview_display_wm = {
  2391. PINEVIEW_DISPLAY_FIFO,
  2392. PINEVIEW_MAX_WM,
  2393. PINEVIEW_DFT_WM,
  2394. PINEVIEW_GUARD_WM,
  2395. PINEVIEW_FIFO_LINE_SIZE
  2396. };
  2397. static struct intel_watermark_params pineview_display_hplloff_wm = {
  2398. PINEVIEW_DISPLAY_FIFO,
  2399. PINEVIEW_MAX_WM,
  2400. PINEVIEW_DFT_HPLLOFF_WM,
  2401. PINEVIEW_GUARD_WM,
  2402. PINEVIEW_FIFO_LINE_SIZE
  2403. };
  2404. static struct intel_watermark_params pineview_cursor_wm = {
  2405. PINEVIEW_CURSOR_FIFO,
  2406. PINEVIEW_CURSOR_MAX_WM,
  2407. PINEVIEW_CURSOR_DFT_WM,
  2408. PINEVIEW_CURSOR_GUARD_WM,
  2409. PINEVIEW_FIFO_LINE_SIZE,
  2410. };
  2411. static struct intel_watermark_params pineview_cursor_hplloff_wm = {
  2412. PINEVIEW_CURSOR_FIFO,
  2413. PINEVIEW_CURSOR_MAX_WM,
  2414. PINEVIEW_CURSOR_DFT_WM,
  2415. PINEVIEW_CURSOR_GUARD_WM,
  2416. PINEVIEW_FIFO_LINE_SIZE
  2417. };
  2418. static struct intel_watermark_params g4x_wm_info = {
  2419. G4X_FIFO_SIZE,
  2420. G4X_MAX_WM,
  2421. G4X_MAX_WM,
  2422. 2,
  2423. G4X_FIFO_LINE_SIZE,
  2424. };
  2425. static struct intel_watermark_params g4x_cursor_wm_info = {
  2426. I965_CURSOR_FIFO,
  2427. I965_CURSOR_MAX_WM,
  2428. I965_CURSOR_DFT_WM,
  2429. 2,
  2430. G4X_FIFO_LINE_SIZE,
  2431. };
  2432. static struct intel_watermark_params i965_cursor_wm_info = {
  2433. I965_CURSOR_FIFO,
  2434. I965_CURSOR_MAX_WM,
  2435. I965_CURSOR_DFT_WM,
  2436. 2,
  2437. I915_FIFO_LINE_SIZE,
  2438. };
  2439. static struct intel_watermark_params i945_wm_info = {
  2440. I945_FIFO_SIZE,
  2441. I915_MAX_WM,
  2442. 1,
  2443. 2,
  2444. I915_FIFO_LINE_SIZE
  2445. };
  2446. static struct intel_watermark_params i915_wm_info = {
  2447. I915_FIFO_SIZE,
  2448. I915_MAX_WM,
  2449. 1,
  2450. 2,
  2451. I915_FIFO_LINE_SIZE
  2452. };
  2453. static struct intel_watermark_params i855_wm_info = {
  2454. I855GM_FIFO_SIZE,
  2455. I915_MAX_WM,
  2456. 1,
  2457. 2,
  2458. I830_FIFO_LINE_SIZE
  2459. };
  2460. static struct intel_watermark_params i830_wm_info = {
  2461. I830_FIFO_SIZE,
  2462. I915_MAX_WM,
  2463. 1,
  2464. 2,
  2465. I830_FIFO_LINE_SIZE
  2466. };
  2467. static struct intel_watermark_params ironlake_display_wm_info = {
  2468. ILK_DISPLAY_FIFO,
  2469. ILK_DISPLAY_MAXWM,
  2470. ILK_DISPLAY_DFTWM,
  2471. 2,
  2472. ILK_FIFO_LINE_SIZE
  2473. };
  2474. static struct intel_watermark_params ironlake_cursor_wm_info = {
  2475. ILK_CURSOR_FIFO,
  2476. ILK_CURSOR_MAXWM,
  2477. ILK_CURSOR_DFTWM,
  2478. 2,
  2479. ILK_FIFO_LINE_SIZE
  2480. };
  2481. static struct intel_watermark_params ironlake_display_srwm_info = {
  2482. ILK_DISPLAY_SR_FIFO,
  2483. ILK_DISPLAY_MAX_SRWM,
  2484. ILK_DISPLAY_DFT_SRWM,
  2485. 2,
  2486. ILK_FIFO_LINE_SIZE
  2487. };
  2488. static struct intel_watermark_params ironlake_cursor_srwm_info = {
  2489. ILK_CURSOR_SR_FIFO,
  2490. ILK_CURSOR_MAX_SRWM,
  2491. ILK_CURSOR_DFT_SRWM,
  2492. 2,
  2493. ILK_FIFO_LINE_SIZE
  2494. };
  2495. /**
  2496. * intel_calculate_wm - calculate watermark level
  2497. * @clock_in_khz: pixel clock
  2498. * @wm: chip FIFO params
  2499. * @pixel_size: display pixel size
  2500. * @latency_ns: memory latency for the platform
  2501. *
  2502. * Calculate the watermark level (the level at which the display plane will
  2503. * start fetching from memory again). Each chip has a different display
  2504. * FIFO size and allocation, so the caller needs to figure that out and pass
  2505. * in the correct intel_watermark_params structure.
  2506. *
  2507. * As the pixel clock runs, the FIFO will be drained at a rate that depends
  2508. * on the pixel size. When it reaches the watermark level, it'll start
  2509. * fetching FIFO line sized based chunks from memory until the FIFO fills
  2510. * past the watermark point. If the FIFO drains completely, a FIFO underrun
  2511. * will occur, and a display engine hang could result.
  2512. */
  2513. static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
  2514. struct intel_watermark_params *wm,
  2515. int pixel_size,
  2516. unsigned long latency_ns)
  2517. {
  2518. long entries_required, wm_size;
  2519. /*
  2520. * Note: we need to make sure we don't overflow for various clock &
  2521. * latency values.
  2522. * clocks go from a few thousand to several hundred thousand.
  2523. * latency is usually a few thousand
  2524. */
  2525. entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
  2526. 1000;
  2527. entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
  2528. DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries_required);
  2529. wm_size = wm->fifo_size - (entries_required + wm->guard_size);
  2530. DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
  2531. /* Don't promote wm_size to unsigned... */
  2532. if (wm_size > (long)wm->max_wm)
  2533. wm_size = wm->max_wm;
  2534. if (wm_size <= 0)
  2535. wm_size = wm->default_wm;
  2536. return wm_size;
  2537. }
  2538. struct cxsr_latency {
  2539. int is_desktop;
  2540. int is_ddr3;
  2541. unsigned long fsb_freq;
  2542. unsigned long mem_freq;
  2543. unsigned long display_sr;
  2544. unsigned long display_hpll_disable;
  2545. unsigned long cursor_sr;
  2546. unsigned long cursor_hpll_disable;
  2547. };
  2548. static const struct cxsr_latency cxsr_latency_table[] = {
  2549. {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
  2550. {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
  2551. {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
  2552. {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
  2553. {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
  2554. {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
  2555. {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
  2556. {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
  2557. {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
  2558. {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
  2559. {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
  2560. {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
  2561. {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
  2562. {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
  2563. {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
  2564. {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
  2565. {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
  2566. {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
  2567. {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
  2568. {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
  2569. {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
  2570. {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
  2571. {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
  2572. {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
  2573. {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
  2574. {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
  2575. {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
  2576. {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
  2577. {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
  2578. {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
  2579. };
  2580. static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
  2581. int is_ddr3,
  2582. int fsb,
  2583. int mem)
  2584. {
  2585. const struct cxsr_latency *latency;
  2586. int i;
  2587. if (fsb == 0 || mem == 0)
  2588. return NULL;
  2589. for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
  2590. latency = &cxsr_latency_table[i];
  2591. if (is_desktop == latency->is_desktop &&
  2592. is_ddr3 == latency->is_ddr3 &&
  2593. fsb == latency->fsb_freq && mem == latency->mem_freq)
  2594. return latency;
  2595. }
  2596. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  2597. return NULL;
  2598. }
  2599. static void pineview_disable_cxsr(struct drm_device *dev)
  2600. {
  2601. struct drm_i915_private *dev_priv = dev->dev_private;
  2602. /* deactivate cxsr */
  2603. I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
  2604. }
  2605. /*
  2606. * Latency for FIFO fetches is dependent on several factors:
  2607. * - memory configuration (speed, channels)
  2608. * - chipset
  2609. * - current MCH state
  2610. * It can be fairly high in some situations, so here we assume a fairly
  2611. * pessimal value. It's a tradeoff between extra memory fetches (if we
  2612. * set this value too high, the FIFO will fetch frequently to stay full)
  2613. * and power consumption (set it too low to save power and we might see
  2614. * FIFO underruns and display "flicker").
  2615. *
  2616. * A value of 5us seems to be a good balance; safe for very low end
  2617. * platforms but not overly aggressive on lower latency configs.
  2618. */
  2619. static const int latency_ns = 5000;
  2620. static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
  2621. {
  2622. struct drm_i915_private *dev_priv = dev->dev_private;
  2623. uint32_t dsparb = I915_READ(DSPARB);
  2624. int size;
  2625. size = dsparb & 0x7f;
  2626. if (plane)
  2627. size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
  2628. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2629. plane ? "B" : "A", size);
  2630. return size;
  2631. }
  2632. static int i85x_get_fifo_size(struct drm_device *dev, int plane)
  2633. {
  2634. struct drm_i915_private *dev_priv = dev->dev_private;
  2635. uint32_t dsparb = I915_READ(DSPARB);
  2636. int size;
  2637. size = dsparb & 0x1ff;
  2638. if (plane)
  2639. size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
  2640. size >>= 1; /* Convert to cachelines */
  2641. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2642. plane ? "B" : "A", size);
  2643. return size;
  2644. }
  2645. static int i845_get_fifo_size(struct drm_device *dev, int plane)
  2646. {
  2647. struct drm_i915_private *dev_priv = dev->dev_private;
  2648. uint32_t dsparb = I915_READ(DSPARB);
  2649. int size;
  2650. size = dsparb & 0x7f;
  2651. size >>= 2; /* Convert to cachelines */
  2652. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2653. plane ? "B" : "A",
  2654. size);
  2655. return size;
  2656. }
  2657. static int i830_get_fifo_size(struct drm_device *dev, int plane)
  2658. {
  2659. struct drm_i915_private *dev_priv = dev->dev_private;
  2660. uint32_t dsparb = I915_READ(DSPARB);
  2661. int size;
  2662. size = dsparb & 0x7f;
  2663. size >>= 1; /* Convert to cachelines */
  2664. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2665. plane ? "B" : "A", size);
  2666. return size;
  2667. }
  2668. static void pineview_update_wm(struct drm_device *dev, int planea_clock,
  2669. int planeb_clock, int sr_hdisplay, int unused,
  2670. int pixel_size)
  2671. {
  2672. struct drm_i915_private *dev_priv = dev->dev_private;
  2673. const struct cxsr_latency *latency;
  2674. u32 reg;
  2675. unsigned long wm;
  2676. int sr_clock;
  2677. latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
  2678. dev_priv->fsb_freq, dev_priv->mem_freq);
  2679. if (!latency) {
  2680. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  2681. pineview_disable_cxsr(dev);
  2682. return;
  2683. }
  2684. if (!planea_clock || !planeb_clock) {
  2685. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2686. /* Display SR */
  2687. wm = intel_calculate_wm(sr_clock, &pineview_display_wm,
  2688. pixel_size, latency->display_sr);
  2689. reg = I915_READ(DSPFW1);
  2690. reg &= ~DSPFW_SR_MASK;
  2691. reg |= wm << DSPFW_SR_SHIFT;
  2692. I915_WRITE(DSPFW1, reg);
  2693. DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
  2694. /* cursor SR */
  2695. wm = intel_calculate_wm(sr_clock, &pineview_cursor_wm,
  2696. pixel_size, latency->cursor_sr);
  2697. reg = I915_READ(DSPFW3);
  2698. reg &= ~DSPFW_CURSOR_SR_MASK;
  2699. reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
  2700. I915_WRITE(DSPFW3, reg);
  2701. /* Display HPLL off SR */
  2702. wm = intel_calculate_wm(sr_clock, &pineview_display_hplloff_wm,
  2703. pixel_size, latency->display_hpll_disable);
  2704. reg = I915_READ(DSPFW3);
  2705. reg &= ~DSPFW_HPLL_SR_MASK;
  2706. reg |= wm & DSPFW_HPLL_SR_MASK;
  2707. I915_WRITE(DSPFW3, reg);
  2708. /* cursor HPLL off SR */
  2709. wm = intel_calculate_wm(sr_clock, &pineview_cursor_hplloff_wm,
  2710. pixel_size, latency->cursor_hpll_disable);
  2711. reg = I915_READ(DSPFW3);
  2712. reg &= ~DSPFW_HPLL_CURSOR_MASK;
  2713. reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
  2714. I915_WRITE(DSPFW3, reg);
  2715. DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
  2716. /* activate cxsr */
  2717. I915_WRITE(DSPFW3,
  2718. I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
  2719. DRM_DEBUG_KMS("Self-refresh is enabled\n");
  2720. } else {
  2721. pineview_disable_cxsr(dev);
  2722. DRM_DEBUG_KMS("Self-refresh is disabled\n");
  2723. }
  2724. }
  2725. static void g4x_update_wm(struct drm_device *dev, int planea_clock,
  2726. int planeb_clock, int sr_hdisplay, int sr_htotal,
  2727. int pixel_size)
  2728. {
  2729. struct drm_i915_private *dev_priv = dev->dev_private;
  2730. int total_size, cacheline_size;
  2731. int planea_wm, planeb_wm, cursora_wm, cursorb_wm, cursor_sr;
  2732. struct intel_watermark_params planea_params, planeb_params;
  2733. unsigned long line_time_us;
  2734. int sr_clock, sr_entries = 0, entries_required;
  2735. /* Create copies of the base settings for each pipe */
  2736. planea_params = planeb_params = g4x_wm_info;
  2737. /* Grab a couple of global values before we overwrite them */
  2738. total_size = planea_params.fifo_size;
  2739. cacheline_size = planea_params.cacheline_size;
  2740. /*
  2741. * Note: we need to make sure we don't overflow for various clock &
  2742. * latency values.
  2743. * clocks go from a few thousand to several hundred thousand.
  2744. * latency is usually a few thousand
  2745. */
  2746. entries_required = ((planea_clock / 1000) * pixel_size * latency_ns) /
  2747. 1000;
  2748. entries_required = DIV_ROUND_UP(entries_required, G4X_FIFO_LINE_SIZE);
  2749. planea_wm = entries_required + planea_params.guard_size;
  2750. entries_required = ((planeb_clock / 1000) * pixel_size * latency_ns) /
  2751. 1000;
  2752. entries_required = DIV_ROUND_UP(entries_required, G4X_FIFO_LINE_SIZE);
  2753. planeb_wm = entries_required + planeb_params.guard_size;
  2754. cursora_wm = cursorb_wm = 16;
  2755. cursor_sr = 32;
  2756. DRM_DEBUG("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  2757. /* Calc sr entries for one plane configs */
  2758. if (sr_hdisplay && (!planea_clock || !planeb_clock)) {
  2759. /* self-refresh has much higher latency */
  2760. static const int sr_latency_ns = 12000;
  2761. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2762. line_time_us = ((sr_htotal * 1000) / sr_clock);
  2763. /* Use ns/us then divide to preserve precision */
  2764. sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  2765. pixel_size * sr_hdisplay;
  2766. sr_entries = DIV_ROUND_UP(sr_entries, cacheline_size);
  2767. entries_required = (((sr_latency_ns / line_time_us) +
  2768. 1000) / 1000) * pixel_size * 64;
  2769. entries_required = DIV_ROUND_UP(entries_required,
  2770. g4x_cursor_wm_info.cacheline_size);
  2771. cursor_sr = entries_required + g4x_cursor_wm_info.guard_size;
  2772. if (cursor_sr > g4x_cursor_wm_info.max_wm)
  2773. cursor_sr = g4x_cursor_wm_info.max_wm;
  2774. DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
  2775. "cursor %d\n", sr_entries, cursor_sr);
  2776. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  2777. } else {
  2778. /* Turn off self refresh if both pipes are enabled */
  2779. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  2780. & ~FW_BLC_SELF_EN);
  2781. }
  2782. DRM_DEBUG("Setting FIFO watermarks - A: %d, B: %d, SR %d\n",
  2783. planea_wm, planeb_wm, sr_entries);
  2784. planea_wm &= 0x3f;
  2785. planeb_wm &= 0x3f;
  2786. I915_WRITE(DSPFW1, (sr_entries << DSPFW_SR_SHIFT) |
  2787. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  2788. (planeb_wm << DSPFW_PLANEB_SHIFT) | planea_wm);
  2789. I915_WRITE(DSPFW2, (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
  2790. (cursora_wm << DSPFW_CURSORA_SHIFT));
  2791. /* HPLL off in SR has some issues on G4x... disable it */
  2792. I915_WRITE(DSPFW3, (I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |
  2793. (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  2794. }
  2795. static void i965_update_wm(struct drm_device *dev, int planea_clock,
  2796. int planeb_clock, int sr_hdisplay, int sr_htotal,
  2797. int pixel_size)
  2798. {
  2799. struct drm_i915_private *dev_priv = dev->dev_private;
  2800. unsigned long line_time_us;
  2801. int sr_clock, sr_entries, srwm = 1;
  2802. int cursor_sr = 16;
  2803. /* Calc sr entries for one plane configs */
  2804. if (sr_hdisplay && (!planea_clock || !planeb_clock)) {
  2805. /* self-refresh has much higher latency */
  2806. static const int sr_latency_ns = 12000;
  2807. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2808. line_time_us = ((sr_htotal * 1000) / sr_clock);
  2809. /* Use ns/us then divide to preserve precision */
  2810. sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  2811. pixel_size * sr_hdisplay;
  2812. sr_entries = DIV_ROUND_UP(sr_entries, I915_FIFO_LINE_SIZE);
  2813. DRM_DEBUG("self-refresh entries: %d\n", sr_entries);
  2814. srwm = I965_FIFO_SIZE - sr_entries;
  2815. if (srwm < 0)
  2816. srwm = 1;
  2817. srwm &= 0x1ff;
  2818. sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  2819. pixel_size * 64;
  2820. sr_entries = DIV_ROUND_UP(sr_entries,
  2821. i965_cursor_wm_info.cacheline_size);
  2822. cursor_sr = i965_cursor_wm_info.fifo_size -
  2823. (sr_entries + i965_cursor_wm_info.guard_size);
  2824. if (cursor_sr > i965_cursor_wm_info.max_wm)
  2825. cursor_sr = i965_cursor_wm_info.max_wm;
  2826. DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
  2827. "cursor %d\n", srwm, cursor_sr);
  2828. if (IS_CRESTLINE(dev))
  2829. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  2830. } else {
  2831. /* Turn off self refresh if both pipes are enabled */
  2832. if (IS_CRESTLINE(dev))
  2833. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  2834. & ~FW_BLC_SELF_EN);
  2835. }
  2836. DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
  2837. srwm);
  2838. /* 965 has limitations... */
  2839. I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) | (8 << 16) | (8 << 8) |
  2840. (8 << 0));
  2841. I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
  2842. /* update cursor SR watermark */
  2843. I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  2844. }
  2845. static void i9xx_update_wm(struct drm_device *dev, int planea_clock,
  2846. int planeb_clock, int sr_hdisplay, int sr_htotal,
  2847. int pixel_size)
  2848. {
  2849. struct drm_i915_private *dev_priv = dev->dev_private;
  2850. uint32_t fwater_lo;
  2851. uint32_t fwater_hi;
  2852. int total_size, cacheline_size, cwm, srwm = 1;
  2853. int planea_wm, planeb_wm;
  2854. struct intel_watermark_params planea_params, planeb_params;
  2855. unsigned long line_time_us;
  2856. int sr_clock, sr_entries = 0;
  2857. /* Create copies of the base settings for each pipe */
  2858. if (IS_CRESTLINE(dev) || IS_I945GM(dev))
  2859. planea_params = planeb_params = i945_wm_info;
  2860. else if (!IS_GEN2(dev))
  2861. planea_params = planeb_params = i915_wm_info;
  2862. else
  2863. planea_params = planeb_params = i855_wm_info;
  2864. /* Grab a couple of global values before we overwrite them */
  2865. total_size = planea_params.fifo_size;
  2866. cacheline_size = planea_params.cacheline_size;
  2867. /* Update per-plane FIFO sizes */
  2868. planea_params.fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  2869. planeb_params.fifo_size = dev_priv->display.get_fifo_size(dev, 1);
  2870. planea_wm = intel_calculate_wm(planea_clock, &planea_params,
  2871. pixel_size, latency_ns);
  2872. planeb_wm = intel_calculate_wm(planeb_clock, &planeb_params,
  2873. pixel_size, latency_ns);
  2874. DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  2875. /*
  2876. * Overlay gets an aggressive default since video jitter is bad.
  2877. */
  2878. cwm = 2;
  2879. /* Calc sr entries for one plane configs */
  2880. if (HAS_FW_BLC(dev) && sr_hdisplay &&
  2881. (!planea_clock || !planeb_clock)) {
  2882. /* self-refresh has much higher latency */
  2883. static const int sr_latency_ns = 6000;
  2884. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2885. line_time_us = ((sr_htotal * 1000) / sr_clock);
  2886. /* Use ns/us then divide to preserve precision */
  2887. sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  2888. pixel_size * sr_hdisplay;
  2889. sr_entries = DIV_ROUND_UP(sr_entries, cacheline_size);
  2890. DRM_DEBUG_KMS("self-refresh entries: %d\n", sr_entries);
  2891. srwm = total_size - sr_entries;
  2892. if (srwm < 0)
  2893. srwm = 1;
  2894. if (IS_I945G(dev) || IS_I945GM(dev))
  2895. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
  2896. else if (IS_I915GM(dev)) {
  2897. /* 915M has a smaller SRWM field */
  2898. I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
  2899. I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
  2900. }
  2901. } else {
  2902. /* Turn off self refresh if both pipes are enabled */
  2903. if (IS_I945G(dev) || IS_I945GM(dev)) {
  2904. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  2905. & ~FW_BLC_SELF_EN);
  2906. } else if (IS_I915GM(dev)) {
  2907. I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
  2908. }
  2909. }
  2910. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
  2911. planea_wm, planeb_wm, cwm, srwm);
  2912. fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
  2913. fwater_hi = (cwm & 0x1f);
  2914. /* Set request length to 8 cachelines per fetch */
  2915. fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
  2916. fwater_hi = fwater_hi | (1 << 8);
  2917. I915_WRITE(FW_BLC, fwater_lo);
  2918. I915_WRITE(FW_BLC2, fwater_hi);
  2919. }
  2920. static void i830_update_wm(struct drm_device *dev, int planea_clock, int unused,
  2921. int unused2, int unused3, int pixel_size)
  2922. {
  2923. struct drm_i915_private *dev_priv = dev->dev_private;
  2924. uint32_t fwater_lo = I915_READ(FW_BLC) & ~0xfff;
  2925. int planea_wm;
  2926. i830_wm_info.fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  2927. planea_wm = intel_calculate_wm(planea_clock, &i830_wm_info,
  2928. pixel_size, latency_ns);
  2929. fwater_lo |= (3<<8) | planea_wm;
  2930. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
  2931. I915_WRITE(FW_BLC, fwater_lo);
  2932. }
  2933. #define ILK_LP0_PLANE_LATENCY 700
  2934. #define ILK_LP0_CURSOR_LATENCY 1300
  2935. static bool ironlake_compute_wm0(struct drm_device *dev,
  2936. int pipe,
  2937. int *plane_wm,
  2938. int *cursor_wm)
  2939. {
  2940. struct drm_crtc *crtc;
  2941. int htotal, hdisplay, clock, pixel_size = 0;
  2942. int line_time_us, line_count, entries;
  2943. crtc = intel_get_crtc_for_pipe(dev, pipe);
  2944. if (crtc->fb == NULL || !crtc->enabled)
  2945. return false;
  2946. htotal = crtc->mode.htotal;
  2947. hdisplay = crtc->mode.hdisplay;
  2948. clock = crtc->mode.clock;
  2949. pixel_size = crtc->fb->bits_per_pixel / 8;
  2950. /* Use the small buffer method to calculate plane watermark */
  2951. entries = ((clock * pixel_size / 1000) * ILK_LP0_PLANE_LATENCY) / 1000;
  2952. entries = DIV_ROUND_UP(entries,
  2953. ironlake_display_wm_info.cacheline_size);
  2954. *plane_wm = entries + ironlake_display_wm_info.guard_size;
  2955. if (*plane_wm > (int)ironlake_display_wm_info.max_wm)
  2956. *plane_wm = ironlake_display_wm_info.max_wm;
  2957. /* Use the large buffer method to calculate cursor watermark */
  2958. line_time_us = ((htotal * 1000) / clock);
  2959. line_count = (ILK_LP0_CURSOR_LATENCY / line_time_us + 1000) / 1000;
  2960. entries = line_count * 64 * pixel_size;
  2961. entries = DIV_ROUND_UP(entries,
  2962. ironlake_cursor_wm_info.cacheline_size);
  2963. *cursor_wm = entries + ironlake_cursor_wm_info.guard_size;
  2964. if (*cursor_wm > ironlake_cursor_wm_info.max_wm)
  2965. *cursor_wm = ironlake_cursor_wm_info.max_wm;
  2966. return true;
  2967. }
  2968. static void ironlake_update_wm(struct drm_device *dev,
  2969. int planea_clock, int planeb_clock,
  2970. int sr_hdisplay, int sr_htotal,
  2971. int pixel_size)
  2972. {
  2973. struct drm_i915_private *dev_priv = dev->dev_private;
  2974. int plane_wm, cursor_wm, enabled;
  2975. int tmp;
  2976. enabled = 0;
  2977. if (ironlake_compute_wm0(dev, 0, &plane_wm, &cursor_wm)) {
  2978. I915_WRITE(WM0_PIPEA_ILK,
  2979. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  2980. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  2981. " plane %d, " "cursor: %d\n",
  2982. plane_wm, cursor_wm);
  2983. enabled++;
  2984. }
  2985. if (ironlake_compute_wm0(dev, 1, &plane_wm, &cursor_wm)) {
  2986. I915_WRITE(WM0_PIPEB_ILK,
  2987. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  2988. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  2989. " plane %d, cursor: %d\n",
  2990. plane_wm, cursor_wm);
  2991. enabled++;
  2992. }
  2993. /*
  2994. * Calculate and update the self-refresh watermark only when one
  2995. * display plane is used.
  2996. */
  2997. tmp = 0;
  2998. if (enabled == 1 && /* XXX disabled due to buggy implmentation? */ 0) {
  2999. unsigned long line_time_us;
  3000. int small, large, plane_fbc;
  3001. int sr_clock, entries;
  3002. int line_count, line_size;
  3003. /* Read the self-refresh latency. The unit is 0.5us */
  3004. int ilk_sr_latency = I915_READ(MLTR_ILK) & ILK_SRLT_MASK;
  3005. sr_clock = planea_clock ? planea_clock : planeb_clock;
  3006. line_time_us = (sr_htotal * 1000) / sr_clock;
  3007. /* Use ns/us then divide to preserve precision */
  3008. line_count = ((ilk_sr_latency * 500) / line_time_us + 1000)
  3009. / 1000;
  3010. line_size = sr_hdisplay * pixel_size;
  3011. /* Use the minimum of the small and large buffer method for primary */
  3012. small = ((sr_clock * pixel_size / 1000) * (ilk_sr_latency * 500)) / 1000;
  3013. large = line_count * line_size;
  3014. entries = DIV_ROUND_UP(min(small, large),
  3015. ironlake_display_srwm_info.cacheline_size);
  3016. plane_fbc = entries * 64;
  3017. plane_fbc = DIV_ROUND_UP(plane_fbc, line_size);
  3018. plane_wm = entries + ironlake_display_srwm_info.guard_size;
  3019. if (plane_wm > (int)ironlake_display_srwm_info.max_wm)
  3020. plane_wm = ironlake_display_srwm_info.max_wm;
  3021. /* calculate the self-refresh watermark for display cursor */
  3022. entries = line_count * pixel_size * 64;
  3023. entries = DIV_ROUND_UP(entries,
  3024. ironlake_cursor_srwm_info.cacheline_size);
  3025. cursor_wm = entries + ironlake_cursor_srwm_info.guard_size;
  3026. if (cursor_wm > (int)ironlake_cursor_srwm_info.max_wm)
  3027. cursor_wm = ironlake_cursor_srwm_info.max_wm;
  3028. /* configure watermark and enable self-refresh */
  3029. tmp = (WM1_LP_SR_EN |
  3030. (ilk_sr_latency << WM1_LP_LATENCY_SHIFT) |
  3031. (plane_fbc << WM1_LP_FBC_SHIFT) |
  3032. (plane_wm << WM1_LP_SR_SHIFT) |
  3033. cursor_wm);
  3034. DRM_DEBUG_KMS("self-refresh watermark: display plane %d, fbc lines %d,"
  3035. " cursor %d\n", plane_wm, plane_fbc, cursor_wm);
  3036. }
  3037. I915_WRITE(WM1_LP_ILK, tmp);
  3038. /* XXX setup WM2 and WM3 */
  3039. }
  3040. /**
  3041. * intel_update_watermarks - update FIFO watermark values based on current modes
  3042. *
  3043. * Calculate watermark values for the various WM regs based on current mode
  3044. * and plane configuration.
  3045. *
  3046. * There are several cases to deal with here:
  3047. * - normal (i.e. non-self-refresh)
  3048. * - self-refresh (SR) mode
  3049. * - lines are large relative to FIFO size (buffer can hold up to 2)
  3050. * - lines are small relative to FIFO size (buffer can hold more than 2
  3051. * lines), so need to account for TLB latency
  3052. *
  3053. * The normal calculation is:
  3054. * watermark = dotclock * bytes per pixel * latency
  3055. * where latency is platform & configuration dependent (we assume pessimal
  3056. * values here).
  3057. *
  3058. * The SR calculation is:
  3059. * watermark = (trunc(latency/line time)+1) * surface width *
  3060. * bytes per pixel
  3061. * where
  3062. * line time = htotal / dotclock
  3063. * surface width = hdisplay for normal plane and 64 for cursor
  3064. * and latency is assumed to be high, as above.
  3065. *
  3066. * The final value programmed to the register should always be rounded up,
  3067. * and include an extra 2 entries to account for clock crossings.
  3068. *
  3069. * We don't use the sprite, so we can ignore that. And on Crestline we have
  3070. * to set the non-SR watermarks to 8.
  3071. */
  3072. static void intel_update_watermarks(struct drm_device *dev)
  3073. {
  3074. struct drm_i915_private *dev_priv = dev->dev_private;
  3075. struct drm_crtc *crtc;
  3076. int sr_hdisplay = 0;
  3077. unsigned long planea_clock = 0, planeb_clock = 0, sr_clock = 0;
  3078. int enabled = 0, pixel_size = 0;
  3079. int sr_htotal = 0;
  3080. if (!dev_priv->display.update_wm)
  3081. return;
  3082. /* Get the clock config from both planes */
  3083. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  3084. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3085. if (intel_crtc->active) {
  3086. enabled++;
  3087. if (intel_crtc->plane == 0) {
  3088. DRM_DEBUG_KMS("plane A (pipe %d) clock: %d\n",
  3089. intel_crtc->pipe, crtc->mode.clock);
  3090. planea_clock = crtc->mode.clock;
  3091. } else {
  3092. DRM_DEBUG_KMS("plane B (pipe %d) clock: %d\n",
  3093. intel_crtc->pipe, crtc->mode.clock);
  3094. planeb_clock = crtc->mode.clock;
  3095. }
  3096. sr_hdisplay = crtc->mode.hdisplay;
  3097. sr_clock = crtc->mode.clock;
  3098. sr_htotal = crtc->mode.htotal;
  3099. if (crtc->fb)
  3100. pixel_size = crtc->fb->bits_per_pixel / 8;
  3101. else
  3102. pixel_size = 4; /* by default */
  3103. }
  3104. }
  3105. if (enabled <= 0)
  3106. return;
  3107. dev_priv->display.update_wm(dev, planea_clock, planeb_clock,
  3108. sr_hdisplay, sr_htotal, pixel_size);
  3109. }
  3110. static int intel_crtc_mode_set(struct drm_crtc *crtc,
  3111. struct drm_display_mode *mode,
  3112. struct drm_display_mode *adjusted_mode,
  3113. int x, int y,
  3114. struct drm_framebuffer *old_fb)
  3115. {
  3116. struct drm_device *dev = crtc->dev;
  3117. struct drm_i915_private *dev_priv = dev->dev_private;
  3118. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3119. int pipe = intel_crtc->pipe;
  3120. int plane = intel_crtc->plane;
  3121. u32 fp_reg, dpll_reg;
  3122. int refclk, num_connectors = 0;
  3123. intel_clock_t clock, reduced_clock;
  3124. u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
  3125. bool ok, has_reduced_clock = false, is_sdvo = false, is_dvo = false;
  3126. bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
  3127. struct intel_encoder *has_edp_encoder = NULL;
  3128. struct drm_mode_config *mode_config = &dev->mode_config;
  3129. struct intel_encoder *encoder;
  3130. const intel_limit_t *limit;
  3131. int ret;
  3132. struct fdi_m_n m_n = {0};
  3133. u32 reg, temp;
  3134. int target_clock;
  3135. drm_vblank_pre_modeset(dev, pipe);
  3136. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  3137. if (encoder->base.crtc != crtc)
  3138. continue;
  3139. switch (encoder->type) {
  3140. case INTEL_OUTPUT_LVDS:
  3141. is_lvds = true;
  3142. break;
  3143. case INTEL_OUTPUT_SDVO:
  3144. case INTEL_OUTPUT_HDMI:
  3145. is_sdvo = true;
  3146. if (encoder->needs_tv_clock)
  3147. is_tv = true;
  3148. break;
  3149. case INTEL_OUTPUT_DVO:
  3150. is_dvo = true;
  3151. break;
  3152. case INTEL_OUTPUT_TVOUT:
  3153. is_tv = true;
  3154. break;
  3155. case INTEL_OUTPUT_ANALOG:
  3156. is_crt = true;
  3157. break;
  3158. case INTEL_OUTPUT_DISPLAYPORT:
  3159. is_dp = true;
  3160. break;
  3161. case INTEL_OUTPUT_EDP:
  3162. has_edp_encoder = encoder;
  3163. break;
  3164. }
  3165. num_connectors++;
  3166. }
  3167. if (is_lvds && dev_priv->lvds_use_ssc && num_connectors < 2) {
  3168. refclk = dev_priv->lvds_ssc_freq * 1000;
  3169. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  3170. refclk / 1000);
  3171. } else if (!IS_GEN2(dev)) {
  3172. refclk = 96000;
  3173. if (HAS_PCH_SPLIT(dev) &&
  3174. (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)))
  3175. refclk = 120000; /* 120Mhz refclk */
  3176. } else {
  3177. refclk = 48000;
  3178. }
  3179. /*
  3180. * Returns a set of divisors for the desired target clock with the given
  3181. * refclk, or FALSE. The returned values represent the clock equation:
  3182. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  3183. */
  3184. limit = intel_limit(crtc);
  3185. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
  3186. if (!ok) {
  3187. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  3188. drm_vblank_post_modeset(dev, pipe);
  3189. return -EINVAL;
  3190. }
  3191. /* Ensure that the cursor is valid for the new mode before changing... */
  3192. intel_crtc_update_cursor(crtc, true);
  3193. if (is_lvds && dev_priv->lvds_downclock_avail) {
  3194. has_reduced_clock = limit->find_pll(limit, crtc,
  3195. dev_priv->lvds_downclock,
  3196. refclk,
  3197. &reduced_clock);
  3198. if (has_reduced_clock && (clock.p != reduced_clock.p)) {
  3199. /*
  3200. * If the different P is found, it means that we can't
  3201. * switch the display clock by using the FP0/FP1.
  3202. * In such case we will disable the LVDS downclock
  3203. * feature.
  3204. */
  3205. DRM_DEBUG_KMS("Different P is found for "
  3206. "LVDS clock/downclock\n");
  3207. has_reduced_clock = 0;
  3208. }
  3209. }
  3210. /* SDVO TV has fixed PLL values depend on its clock range,
  3211. this mirrors vbios setting. */
  3212. if (is_sdvo && is_tv) {
  3213. if (adjusted_mode->clock >= 100000
  3214. && adjusted_mode->clock < 140500) {
  3215. clock.p1 = 2;
  3216. clock.p2 = 10;
  3217. clock.n = 3;
  3218. clock.m1 = 16;
  3219. clock.m2 = 8;
  3220. } else if (adjusted_mode->clock >= 140500
  3221. && adjusted_mode->clock <= 200000) {
  3222. clock.p1 = 1;
  3223. clock.p2 = 10;
  3224. clock.n = 6;
  3225. clock.m1 = 12;
  3226. clock.m2 = 8;
  3227. }
  3228. }
  3229. /* FDI link */
  3230. if (HAS_PCH_SPLIT(dev)) {
  3231. int lane = 0, link_bw, bpp;
  3232. /* CPU eDP doesn't require FDI link, so just set DP M/N
  3233. according to current link config */
  3234. if (has_edp_encoder && !intel_encoder_is_pch_edp(&encoder->base)) {
  3235. target_clock = mode->clock;
  3236. intel_edp_link_config(has_edp_encoder,
  3237. &lane, &link_bw);
  3238. } else {
  3239. /* [e]DP over FDI requires target mode clock
  3240. instead of link clock */
  3241. if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
  3242. target_clock = mode->clock;
  3243. else
  3244. target_clock = adjusted_mode->clock;
  3245. /* FDI is a binary signal running at ~2.7GHz, encoding
  3246. * each output octet as 10 bits. The actual frequency
  3247. * is stored as a divider into a 100MHz clock, and the
  3248. * mode pixel clock is stored in units of 1KHz.
  3249. * Hence the bw of each lane in terms of the mode signal
  3250. * is:
  3251. */
  3252. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  3253. }
  3254. /* determine panel color depth */
  3255. temp = I915_READ(PIPECONF(pipe));
  3256. temp &= ~PIPE_BPC_MASK;
  3257. if (is_lvds) {
  3258. /* the BPC will be 6 if it is 18-bit LVDS panel */
  3259. if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) == LVDS_A3_POWER_UP)
  3260. temp |= PIPE_8BPC;
  3261. else
  3262. temp |= PIPE_6BPC;
  3263. } else if (has_edp_encoder) {
  3264. switch (dev_priv->edp.bpp/3) {
  3265. case 8:
  3266. temp |= PIPE_8BPC;
  3267. break;
  3268. case 10:
  3269. temp |= PIPE_10BPC;
  3270. break;
  3271. case 6:
  3272. temp |= PIPE_6BPC;
  3273. break;
  3274. case 12:
  3275. temp |= PIPE_12BPC;
  3276. break;
  3277. }
  3278. } else
  3279. temp |= PIPE_8BPC;
  3280. I915_WRITE(PIPECONF(pipe), temp);
  3281. switch (temp & PIPE_BPC_MASK) {
  3282. case PIPE_8BPC:
  3283. bpp = 24;
  3284. break;
  3285. case PIPE_10BPC:
  3286. bpp = 30;
  3287. break;
  3288. case PIPE_6BPC:
  3289. bpp = 18;
  3290. break;
  3291. case PIPE_12BPC:
  3292. bpp = 36;
  3293. break;
  3294. default:
  3295. DRM_ERROR("unknown pipe bpc value\n");
  3296. bpp = 24;
  3297. }
  3298. if (!lane) {
  3299. /*
  3300. * Account for spread spectrum to avoid
  3301. * oversubscribing the link. Max center spread
  3302. * is 2.5%; use 5% for safety's sake.
  3303. */
  3304. u32 bps = target_clock * bpp * 21 / 20;
  3305. lane = bps / (link_bw * 8) + 1;
  3306. }
  3307. intel_crtc->fdi_lanes = lane;
  3308. ironlake_compute_m_n(bpp, lane, target_clock, link_bw, &m_n);
  3309. }
  3310. /* Ironlake: try to setup display ref clock before DPLL
  3311. * enabling. This is only under driver's control after
  3312. * PCH B stepping, previous chipset stepping should be
  3313. * ignoring this setting.
  3314. */
  3315. if (HAS_PCH_SPLIT(dev)) {
  3316. temp = I915_READ(PCH_DREF_CONTROL);
  3317. /* Always enable nonspread source */
  3318. temp &= ~DREF_NONSPREAD_SOURCE_MASK;
  3319. temp |= DREF_NONSPREAD_SOURCE_ENABLE;
  3320. temp &= ~DREF_SSC_SOURCE_MASK;
  3321. temp |= DREF_SSC_SOURCE_ENABLE;
  3322. I915_WRITE(PCH_DREF_CONTROL, temp);
  3323. POSTING_READ(PCH_DREF_CONTROL);
  3324. udelay(200);
  3325. if (has_edp_encoder) {
  3326. if (dev_priv->lvds_use_ssc) {
  3327. temp |= DREF_SSC1_ENABLE;
  3328. I915_WRITE(PCH_DREF_CONTROL, temp);
  3329. POSTING_READ(PCH_DREF_CONTROL);
  3330. udelay(200);
  3331. }
  3332. temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  3333. /* Enable CPU source on CPU attached eDP */
  3334. if (!intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  3335. if (dev_priv->lvds_use_ssc)
  3336. temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  3337. else
  3338. temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  3339. } else {
  3340. /* Enable SSC on PCH eDP if needed */
  3341. if (dev_priv->lvds_use_ssc) {
  3342. DRM_ERROR("enabling SSC on PCH\n");
  3343. temp |= DREF_SUPERSPREAD_SOURCE_ENABLE;
  3344. }
  3345. }
  3346. I915_WRITE(PCH_DREF_CONTROL, temp);
  3347. POSTING_READ(PCH_DREF_CONTROL);
  3348. udelay(200);
  3349. }
  3350. }
  3351. if (IS_PINEVIEW(dev)) {
  3352. fp = (1 << clock.n) << 16 | clock.m1 << 8 | clock.m2;
  3353. if (has_reduced_clock)
  3354. fp2 = (1 << reduced_clock.n) << 16 |
  3355. reduced_clock.m1 << 8 | reduced_clock.m2;
  3356. } else {
  3357. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  3358. if (has_reduced_clock)
  3359. fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
  3360. reduced_clock.m2;
  3361. }
  3362. dpll = 0;
  3363. if (!HAS_PCH_SPLIT(dev))
  3364. dpll = DPLL_VGA_MODE_DIS;
  3365. if (!IS_GEN2(dev)) {
  3366. if (is_lvds)
  3367. dpll |= DPLLB_MODE_LVDS;
  3368. else
  3369. dpll |= DPLLB_MODE_DAC_SERIAL;
  3370. if (is_sdvo) {
  3371. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  3372. if (pixel_multiplier > 1) {
  3373. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  3374. dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
  3375. else if (HAS_PCH_SPLIT(dev))
  3376. dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  3377. }
  3378. dpll |= DPLL_DVO_HIGH_SPEED;
  3379. }
  3380. if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
  3381. dpll |= DPLL_DVO_HIGH_SPEED;
  3382. /* compute bitmask from p1 value */
  3383. if (IS_PINEVIEW(dev))
  3384. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  3385. else {
  3386. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3387. /* also FPA1 */
  3388. if (HAS_PCH_SPLIT(dev))
  3389. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  3390. if (IS_G4X(dev) && has_reduced_clock)
  3391. dpll |= (1 << (reduced_clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  3392. }
  3393. switch (clock.p2) {
  3394. case 5:
  3395. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  3396. break;
  3397. case 7:
  3398. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  3399. break;
  3400. case 10:
  3401. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  3402. break;
  3403. case 14:
  3404. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  3405. break;
  3406. }
  3407. if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev))
  3408. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  3409. } else {
  3410. if (is_lvds) {
  3411. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3412. } else {
  3413. if (clock.p1 == 2)
  3414. dpll |= PLL_P1_DIVIDE_BY_TWO;
  3415. else
  3416. dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3417. if (clock.p2 == 4)
  3418. dpll |= PLL_P2_DIVIDE_BY_4;
  3419. }
  3420. }
  3421. if (is_sdvo && is_tv)
  3422. dpll |= PLL_REF_INPUT_TVCLKINBC;
  3423. else if (is_tv)
  3424. /* XXX: just matching BIOS for now */
  3425. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  3426. dpll |= 3;
  3427. else if (is_lvds && dev_priv->lvds_use_ssc && num_connectors < 2)
  3428. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  3429. else
  3430. dpll |= PLL_REF_INPUT_DREFCLK;
  3431. /* setup pipeconf */
  3432. pipeconf = I915_READ(PIPECONF(pipe));
  3433. /* Set up the display plane register */
  3434. dspcntr = DISPPLANE_GAMMA_ENABLE;
  3435. /* Ironlake's plane is forced to pipe, bit 24 is to
  3436. enable color space conversion */
  3437. if (!HAS_PCH_SPLIT(dev)) {
  3438. if (pipe == 0)
  3439. dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
  3440. else
  3441. dspcntr |= DISPPLANE_SEL_PIPE_B;
  3442. }
  3443. if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
  3444. /* Enable pixel doubling when the dot clock is > 90% of the (display)
  3445. * core speed.
  3446. *
  3447. * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
  3448. * pipe == 0 check?
  3449. */
  3450. if (mode->clock >
  3451. dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
  3452. pipeconf |= PIPECONF_DOUBLE_WIDE;
  3453. else
  3454. pipeconf &= ~PIPECONF_DOUBLE_WIDE;
  3455. }
  3456. dspcntr |= DISPLAY_PLANE_ENABLE;
  3457. pipeconf |= PIPECONF_ENABLE;
  3458. dpll |= DPLL_VCO_ENABLE;
  3459. DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  3460. drm_mode_debug_printmodeline(mode);
  3461. /* assign to Ironlake registers */
  3462. if (HAS_PCH_SPLIT(dev)) {
  3463. fp_reg = PCH_FP0(pipe);
  3464. dpll_reg = PCH_DPLL(pipe);
  3465. } else {
  3466. fp_reg = FP0(pipe);
  3467. dpll_reg = DPLL(pipe);
  3468. }
  3469. /* PCH eDP needs FDI, but CPU eDP does not */
  3470. if (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  3471. I915_WRITE(fp_reg, fp);
  3472. I915_WRITE(dpll_reg, dpll & ~DPLL_VCO_ENABLE);
  3473. POSTING_READ(dpll_reg);
  3474. udelay(150);
  3475. }
  3476. /* enable transcoder DPLL */
  3477. if (HAS_PCH_CPT(dev)) {
  3478. temp = I915_READ(PCH_DPLL_SEL);
  3479. if (pipe == 0)
  3480. temp |= TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL;
  3481. else
  3482. temp |= TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL;
  3483. I915_WRITE(PCH_DPLL_SEL, temp);
  3484. POSTING_READ(PCH_DPLL_SEL);
  3485. udelay(150);
  3486. }
  3487. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  3488. * This is an exception to the general rule that mode_set doesn't turn
  3489. * things on.
  3490. */
  3491. if (is_lvds) {
  3492. reg = LVDS;
  3493. if (HAS_PCH_SPLIT(dev))
  3494. reg = PCH_LVDS;
  3495. temp = I915_READ(reg);
  3496. temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  3497. if (pipe == 1) {
  3498. if (HAS_PCH_CPT(dev))
  3499. temp |= PORT_TRANS_B_SEL_CPT;
  3500. else
  3501. temp |= LVDS_PIPEB_SELECT;
  3502. } else {
  3503. if (HAS_PCH_CPT(dev))
  3504. temp &= ~PORT_TRANS_SEL_MASK;
  3505. else
  3506. temp &= ~LVDS_PIPEB_SELECT;
  3507. }
  3508. /* set the corresponsding LVDS_BORDER bit */
  3509. temp |= dev_priv->lvds_border_bits;
  3510. /* Set the B0-B3 data pairs corresponding to whether we're going to
  3511. * set the DPLLs for dual-channel mode or not.
  3512. */
  3513. if (clock.p2 == 7)
  3514. temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  3515. else
  3516. temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  3517. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  3518. * appropriately here, but we need to look more thoroughly into how
  3519. * panels behave in the two modes.
  3520. */
  3521. /* set the dithering flag on non-PCH LVDS as needed */
  3522. if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev)) {
  3523. if (dev_priv->lvds_dither)
  3524. temp |= LVDS_ENABLE_DITHER;
  3525. else
  3526. temp &= ~LVDS_ENABLE_DITHER;
  3527. }
  3528. I915_WRITE(reg, temp);
  3529. }
  3530. /* set the dithering flag and clear for anything other than a panel. */
  3531. if (HAS_PCH_SPLIT(dev)) {
  3532. pipeconf &= ~PIPECONF_DITHER_EN;
  3533. pipeconf &= ~PIPECONF_DITHER_TYPE_MASK;
  3534. if (dev_priv->lvds_dither && (is_lvds || has_edp_encoder)) {
  3535. pipeconf |= PIPECONF_DITHER_EN;
  3536. pipeconf |= PIPECONF_DITHER_TYPE_ST1;
  3537. }
  3538. }
  3539. if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  3540. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  3541. } else if (HAS_PCH_SPLIT(dev)) {
  3542. /* For non-DP output, clear any trans DP clock recovery setting.*/
  3543. if (pipe == 0) {
  3544. I915_WRITE(TRANSA_DATA_M1, 0);
  3545. I915_WRITE(TRANSA_DATA_N1, 0);
  3546. I915_WRITE(TRANSA_DP_LINK_M1, 0);
  3547. I915_WRITE(TRANSA_DP_LINK_N1, 0);
  3548. } else {
  3549. I915_WRITE(TRANSB_DATA_M1, 0);
  3550. I915_WRITE(TRANSB_DATA_N1, 0);
  3551. I915_WRITE(TRANSB_DP_LINK_M1, 0);
  3552. I915_WRITE(TRANSB_DP_LINK_N1, 0);
  3553. }
  3554. }
  3555. if (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  3556. I915_WRITE(fp_reg, fp);
  3557. I915_WRITE(dpll_reg, dpll);
  3558. /* Wait for the clocks to stabilize. */
  3559. POSTING_READ(dpll_reg);
  3560. udelay(150);
  3561. if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev)) {
  3562. temp = 0;
  3563. if (is_sdvo) {
  3564. temp = intel_mode_get_pixel_multiplier(adjusted_mode);
  3565. if (temp > 1)
  3566. temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  3567. else
  3568. temp = 0;
  3569. }
  3570. I915_WRITE(DPLL_MD(pipe), temp);
  3571. } else {
  3572. /* write it again -- the BIOS does, after all */
  3573. I915_WRITE(dpll_reg, dpll);
  3574. }
  3575. /* Wait for the clocks to stabilize. */
  3576. POSTING_READ(dpll_reg);
  3577. udelay(150);
  3578. }
  3579. intel_crtc->lowfreq_avail = false;
  3580. if (is_lvds && has_reduced_clock && i915_powersave) {
  3581. I915_WRITE(fp_reg + 4, fp2);
  3582. intel_crtc->lowfreq_avail = true;
  3583. if (HAS_PIPE_CXSR(dev)) {
  3584. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  3585. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  3586. }
  3587. } else {
  3588. I915_WRITE(fp_reg + 4, fp);
  3589. if (HAS_PIPE_CXSR(dev)) {
  3590. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  3591. pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
  3592. }
  3593. }
  3594. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  3595. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  3596. /* the chip adds 2 halflines automatically */
  3597. adjusted_mode->crtc_vdisplay -= 1;
  3598. adjusted_mode->crtc_vtotal -= 1;
  3599. adjusted_mode->crtc_vblank_start -= 1;
  3600. adjusted_mode->crtc_vblank_end -= 1;
  3601. adjusted_mode->crtc_vsync_end -= 1;
  3602. adjusted_mode->crtc_vsync_start -= 1;
  3603. } else
  3604. pipeconf &= ~PIPECONF_INTERLACE_W_FIELD_INDICATION; /* progressive */
  3605. I915_WRITE(HTOTAL(pipe),
  3606. (adjusted_mode->crtc_hdisplay - 1) |
  3607. ((adjusted_mode->crtc_htotal - 1) << 16));
  3608. I915_WRITE(HBLANK(pipe),
  3609. (adjusted_mode->crtc_hblank_start - 1) |
  3610. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  3611. I915_WRITE(HSYNC(pipe),
  3612. (adjusted_mode->crtc_hsync_start - 1) |
  3613. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  3614. I915_WRITE(VTOTAL(pipe),
  3615. (adjusted_mode->crtc_vdisplay - 1) |
  3616. ((adjusted_mode->crtc_vtotal - 1) << 16));
  3617. I915_WRITE(VBLANK(pipe),
  3618. (adjusted_mode->crtc_vblank_start - 1) |
  3619. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  3620. I915_WRITE(VSYNC(pipe),
  3621. (adjusted_mode->crtc_vsync_start - 1) |
  3622. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  3623. /* pipesrc and dspsize control the size that is scaled from,
  3624. * which should always be the user's requested size.
  3625. */
  3626. if (!HAS_PCH_SPLIT(dev)) {
  3627. I915_WRITE(DSPSIZE(plane),
  3628. ((mode->vdisplay - 1) << 16) |
  3629. (mode->hdisplay - 1));
  3630. I915_WRITE(DSPPOS(plane), 0);
  3631. }
  3632. I915_WRITE(PIPESRC(pipe),
  3633. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  3634. if (HAS_PCH_SPLIT(dev)) {
  3635. I915_WRITE(PIPE_DATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);
  3636. I915_WRITE(PIPE_DATA_N1(pipe), m_n.gmch_n);
  3637. I915_WRITE(PIPE_LINK_M1(pipe), m_n.link_m);
  3638. I915_WRITE(PIPE_LINK_N1(pipe), m_n.link_n);
  3639. if (has_edp_encoder && !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  3640. ironlake_set_pll_edp(crtc, adjusted_mode->clock);
  3641. }
  3642. }
  3643. I915_WRITE(PIPECONF(pipe), pipeconf);
  3644. POSTING_READ(PIPECONF(pipe));
  3645. intel_wait_for_vblank(dev, pipe);
  3646. if (IS_GEN5(dev)) {
  3647. /* enable address swizzle for tiling buffer */
  3648. temp = I915_READ(DISP_ARB_CTL);
  3649. I915_WRITE(DISP_ARB_CTL, temp | DISP_TILE_SURFACE_SWIZZLING);
  3650. }
  3651. I915_WRITE(DSPCNTR(plane), dspcntr);
  3652. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  3653. intel_update_watermarks(dev);
  3654. drm_vblank_post_modeset(dev, pipe);
  3655. return ret;
  3656. }
  3657. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3658. void intel_crtc_load_lut(struct drm_crtc *crtc)
  3659. {
  3660. struct drm_device *dev = crtc->dev;
  3661. struct drm_i915_private *dev_priv = dev->dev_private;
  3662. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3663. int palreg = (intel_crtc->pipe == 0) ? PALETTE_A : PALETTE_B;
  3664. int i;
  3665. /* The clocks have to be on to load the palette. */
  3666. if (!crtc->enabled)
  3667. return;
  3668. /* use legacy palette for Ironlake */
  3669. if (HAS_PCH_SPLIT(dev))
  3670. palreg = (intel_crtc->pipe == 0) ? LGC_PALETTE_A :
  3671. LGC_PALETTE_B;
  3672. for (i = 0; i < 256; i++) {
  3673. I915_WRITE(palreg + 4 * i,
  3674. (intel_crtc->lut_r[i] << 16) |
  3675. (intel_crtc->lut_g[i] << 8) |
  3676. intel_crtc->lut_b[i]);
  3677. }
  3678. }
  3679. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  3680. {
  3681. struct drm_device *dev = crtc->dev;
  3682. struct drm_i915_private *dev_priv = dev->dev_private;
  3683. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3684. bool visible = base != 0;
  3685. u32 cntl;
  3686. if (intel_crtc->cursor_visible == visible)
  3687. return;
  3688. cntl = I915_READ(CURACNTR);
  3689. if (visible) {
  3690. /* On these chipsets we can only modify the base whilst
  3691. * the cursor is disabled.
  3692. */
  3693. I915_WRITE(CURABASE, base);
  3694. cntl &= ~(CURSOR_FORMAT_MASK);
  3695. /* XXX width must be 64, stride 256 => 0x00 << 28 */
  3696. cntl |= CURSOR_ENABLE |
  3697. CURSOR_GAMMA_ENABLE |
  3698. CURSOR_FORMAT_ARGB;
  3699. } else
  3700. cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
  3701. I915_WRITE(CURACNTR, cntl);
  3702. intel_crtc->cursor_visible = visible;
  3703. }
  3704. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  3705. {
  3706. struct drm_device *dev = crtc->dev;
  3707. struct drm_i915_private *dev_priv = dev->dev_private;
  3708. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3709. int pipe = intel_crtc->pipe;
  3710. bool visible = base != 0;
  3711. if (intel_crtc->cursor_visible != visible) {
  3712. uint32_t cntl = I915_READ(pipe == 0 ? CURACNTR : CURBCNTR);
  3713. if (base) {
  3714. cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
  3715. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  3716. cntl |= pipe << 28; /* Connect to correct pipe */
  3717. } else {
  3718. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  3719. cntl |= CURSOR_MODE_DISABLE;
  3720. }
  3721. I915_WRITE(pipe == 0 ? CURACNTR : CURBCNTR, cntl);
  3722. intel_crtc->cursor_visible = visible;
  3723. }
  3724. /* and commit changes on next vblank */
  3725. I915_WRITE(pipe == 0 ? CURABASE : CURBBASE, base);
  3726. }
  3727. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  3728. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  3729. bool on)
  3730. {
  3731. struct drm_device *dev = crtc->dev;
  3732. struct drm_i915_private *dev_priv = dev->dev_private;
  3733. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3734. int pipe = intel_crtc->pipe;
  3735. int x = intel_crtc->cursor_x;
  3736. int y = intel_crtc->cursor_y;
  3737. u32 base, pos;
  3738. bool visible;
  3739. pos = 0;
  3740. if (on && crtc->enabled && crtc->fb) {
  3741. base = intel_crtc->cursor_addr;
  3742. if (x > (int) crtc->fb->width)
  3743. base = 0;
  3744. if (y > (int) crtc->fb->height)
  3745. base = 0;
  3746. } else
  3747. base = 0;
  3748. if (x < 0) {
  3749. if (x + intel_crtc->cursor_width < 0)
  3750. base = 0;
  3751. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  3752. x = -x;
  3753. }
  3754. pos |= x << CURSOR_X_SHIFT;
  3755. if (y < 0) {
  3756. if (y + intel_crtc->cursor_height < 0)
  3757. base = 0;
  3758. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  3759. y = -y;
  3760. }
  3761. pos |= y << CURSOR_Y_SHIFT;
  3762. visible = base != 0;
  3763. if (!visible && !intel_crtc->cursor_visible)
  3764. return;
  3765. I915_WRITE(pipe == 0 ? CURAPOS : CURBPOS, pos);
  3766. if (IS_845G(dev) || IS_I865G(dev))
  3767. i845_update_cursor(crtc, base);
  3768. else
  3769. i9xx_update_cursor(crtc, base);
  3770. if (visible)
  3771. intel_mark_busy(dev, to_intel_framebuffer(crtc->fb)->obj);
  3772. }
  3773. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  3774. struct drm_file *file_priv,
  3775. uint32_t handle,
  3776. uint32_t width, uint32_t height)
  3777. {
  3778. struct drm_device *dev = crtc->dev;
  3779. struct drm_i915_private *dev_priv = dev->dev_private;
  3780. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3781. struct drm_gem_object *bo;
  3782. struct drm_i915_gem_object *obj_priv;
  3783. uint32_t addr;
  3784. int ret;
  3785. DRM_DEBUG_KMS("\n");
  3786. /* if we want to turn off the cursor ignore width and height */
  3787. if (!handle) {
  3788. DRM_DEBUG_KMS("cursor off\n");
  3789. addr = 0;
  3790. bo = NULL;
  3791. mutex_lock(&dev->struct_mutex);
  3792. goto finish;
  3793. }
  3794. /* Currently we only support 64x64 cursors */
  3795. if (width != 64 || height != 64) {
  3796. DRM_ERROR("we currently only support 64x64 cursors\n");
  3797. return -EINVAL;
  3798. }
  3799. bo = drm_gem_object_lookup(dev, file_priv, handle);
  3800. if (!bo)
  3801. return -ENOENT;
  3802. obj_priv = to_intel_bo(bo);
  3803. if (bo->size < width * height * 4) {
  3804. DRM_ERROR("buffer is to small\n");
  3805. ret = -ENOMEM;
  3806. goto fail;
  3807. }
  3808. /* we only need to pin inside GTT if cursor is non-phy */
  3809. mutex_lock(&dev->struct_mutex);
  3810. if (!dev_priv->info->cursor_needs_physical) {
  3811. ret = i915_gem_object_pin(bo, PAGE_SIZE, true);
  3812. if (ret) {
  3813. DRM_ERROR("failed to pin cursor bo\n");
  3814. goto fail_locked;
  3815. }
  3816. ret = i915_gem_object_set_to_gtt_domain(bo, 0);
  3817. if (ret) {
  3818. DRM_ERROR("failed to move cursor bo into the GTT\n");
  3819. goto fail_unpin;
  3820. }
  3821. addr = obj_priv->gtt_offset;
  3822. } else {
  3823. int align = IS_I830(dev) ? 16 * 1024 : 256;
  3824. ret = i915_gem_attach_phys_object(dev, bo,
  3825. (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
  3826. align);
  3827. if (ret) {
  3828. DRM_ERROR("failed to attach phys object\n");
  3829. goto fail_locked;
  3830. }
  3831. addr = obj_priv->phys_obj->handle->busaddr;
  3832. }
  3833. if (IS_GEN2(dev))
  3834. I915_WRITE(CURSIZE, (height << 12) | width);
  3835. finish:
  3836. if (intel_crtc->cursor_bo) {
  3837. if (dev_priv->info->cursor_needs_physical) {
  3838. if (intel_crtc->cursor_bo != bo)
  3839. i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
  3840. } else
  3841. i915_gem_object_unpin(intel_crtc->cursor_bo);
  3842. drm_gem_object_unreference(intel_crtc->cursor_bo);
  3843. }
  3844. mutex_unlock(&dev->struct_mutex);
  3845. intel_crtc->cursor_addr = addr;
  3846. intel_crtc->cursor_bo = bo;
  3847. intel_crtc->cursor_width = width;
  3848. intel_crtc->cursor_height = height;
  3849. intel_crtc_update_cursor(crtc, true);
  3850. return 0;
  3851. fail_unpin:
  3852. i915_gem_object_unpin(bo);
  3853. fail_locked:
  3854. mutex_unlock(&dev->struct_mutex);
  3855. fail:
  3856. drm_gem_object_unreference_unlocked(bo);
  3857. return ret;
  3858. }
  3859. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  3860. {
  3861. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3862. intel_crtc->cursor_x = x;
  3863. intel_crtc->cursor_y = y;
  3864. intel_crtc_update_cursor(crtc, true);
  3865. return 0;
  3866. }
  3867. /** Sets the color ramps on behalf of RandR */
  3868. void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  3869. u16 blue, int regno)
  3870. {
  3871. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3872. intel_crtc->lut_r[regno] = red >> 8;
  3873. intel_crtc->lut_g[regno] = green >> 8;
  3874. intel_crtc->lut_b[regno] = blue >> 8;
  3875. }
  3876. void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  3877. u16 *blue, int regno)
  3878. {
  3879. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3880. *red = intel_crtc->lut_r[regno] << 8;
  3881. *green = intel_crtc->lut_g[regno] << 8;
  3882. *blue = intel_crtc->lut_b[regno] << 8;
  3883. }
  3884. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  3885. u16 *blue, uint32_t start, uint32_t size)
  3886. {
  3887. int end = (start + size > 256) ? 256 : start + size, i;
  3888. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3889. for (i = start; i < end; i++) {
  3890. intel_crtc->lut_r[i] = red[i] >> 8;
  3891. intel_crtc->lut_g[i] = green[i] >> 8;
  3892. intel_crtc->lut_b[i] = blue[i] >> 8;
  3893. }
  3894. intel_crtc_load_lut(crtc);
  3895. }
  3896. /**
  3897. * Get a pipe with a simple mode set on it for doing load-based monitor
  3898. * detection.
  3899. *
  3900. * It will be up to the load-detect code to adjust the pipe as appropriate for
  3901. * its requirements. The pipe will be connected to no other encoders.
  3902. *
  3903. * Currently this code will only succeed if there is a pipe with no encoders
  3904. * configured for it. In the future, it could choose to temporarily disable
  3905. * some outputs to free up a pipe for its use.
  3906. *
  3907. * \return crtc, or NULL if no pipes are available.
  3908. */
  3909. /* VESA 640x480x72Hz mode to set on the pipe */
  3910. static struct drm_display_mode load_detect_mode = {
  3911. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  3912. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  3913. };
  3914. struct drm_crtc *intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,
  3915. struct drm_connector *connector,
  3916. struct drm_display_mode *mode,
  3917. int *dpms_mode)
  3918. {
  3919. struct intel_crtc *intel_crtc;
  3920. struct drm_crtc *possible_crtc;
  3921. struct drm_crtc *supported_crtc =NULL;
  3922. struct drm_encoder *encoder = &intel_encoder->base;
  3923. struct drm_crtc *crtc = NULL;
  3924. struct drm_device *dev = encoder->dev;
  3925. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  3926. struct drm_crtc_helper_funcs *crtc_funcs;
  3927. int i = -1;
  3928. /*
  3929. * Algorithm gets a little messy:
  3930. * - if the connector already has an assigned crtc, use it (but make
  3931. * sure it's on first)
  3932. * - try to find the first unused crtc that can drive this connector,
  3933. * and use that if we find one
  3934. * - if there are no unused crtcs available, try to use the first
  3935. * one we found that supports the connector
  3936. */
  3937. /* See if we already have a CRTC for this connector */
  3938. if (encoder->crtc) {
  3939. crtc = encoder->crtc;
  3940. /* Make sure the crtc and connector are running */
  3941. intel_crtc = to_intel_crtc(crtc);
  3942. *dpms_mode = intel_crtc->dpms_mode;
  3943. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  3944. crtc_funcs = crtc->helper_private;
  3945. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  3946. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  3947. }
  3948. return crtc;
  3949. }
  3950. /* Find an unused one (if possible) */
  3951. list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
  3952. i++;
  3953. if (!(encoder->possible_crtcs & (1 << i)))
  3954. continue;
  3955. if (!possible_crtc->enabled) {
  3956. crtc = possible_crtc;
  3957. break;
  3958. }
  3959. if (!supported_crtc)
  3960. supported_crtc = possible_crtc;
  3961. }
  3962. /*
  3963. * If we didn't find an unused CRTC, don't use any.
  3964. */
  3965. if (!crtc) {
  3966. return NULL;
  3967. }
  3968. encoder->crtc = crtc;
  3969. connector->encoder = encoder;
  3970. intel_encoder->load_detect_temp = true;
  3971. intel_crtc = to_intel_crtc(crtc);
  3972. *dpms_mode = intel_crtc->dpms_mode;
  3973. if (!crtc->enabled) {
  3974. if (!mode)
  3975. mode = &load_detect_mode;
  3976. drm_crtc_helper_set_mode(crtc, mode, 0, 0, crtc->fb);
  3977. } else {
  3978. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  3979. crtc_funcs = crtc->helper_private;
  3980. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  3981. }
  3982. /* Add this connector to the crtc */
  3983. encoder_funcs->mode_set(encoder, &crtc->mode, &crtc->mode);
  3984. encoder_funcs->commit(encoder);
  3985. }
  3986. /* let the connector get through one full cycle before testing */
  3987. intel_wait_for_vblank(dev, intel_crtc->pipe);
  3988. return crtc;
  3989. }
  3990. void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,
  3991. struct drm_connector *connector, int dpms_mode)
  3992. {
  3993. struct drm_encoder *encoder = &intel_encoder->base;
  3994. struct drm_device *dev = encoder->dev;
  3995. struct drm_crtc *crtc = encoder->crtc;
  3996. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  3997. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  3998. if (intel_encoder->load_detect_temp) {
  3999. encoder->crtc = NULL;
  4000. connector->encoder = NULL;
  4001. intel_encoder->load_detect_temp = false;
  4002. crtc->enabled = drm_helper_crtc_in_use(crtc);
  4003. drm_helper_disable_unused_functions(dev);
  4004. }
  4005. /* Switch crtc and encoder back off if necessary */
  4006. if (crtc->enabled && dpms_mode != DRM_MODE_DPMS_ON) {
  4007. if (encoder->crtc == crtc)
  4008. encoder_funcs->dpms(encoder, dpms_mode);
  4009. crtc_funcs->dpms(crtc, dpms_mode);
  4010. }
  4011. }
  4012. /* Returns the clock of the currently programmed mode of the given pipe. */
  4013. static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
  4014. {
  4015. struct drm_i915_private *dev_priv = dev->dev_private;
  4016. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4017. int pipe = intel_crtc->pipe;
  4018. u32 dpll = I915_READ((pipe == 0) ? DPLL_A : DPLL_B);
  4019. u32 fp;
  4020. intel_clock_t clock;
  4021. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  4022. fp = I915_READ((pipe == 0) ? FPA0 : FPB0);
  4023. else
  4024. fp = I915_READ((pipe == 0) ? FPA1 : FPB1);
  4025. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  4026. if (IS_PINEVIEW(dev)) {
  4027. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  4028. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  4029. } else {
  4030. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  4031. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  4032. }
  4033. if (!IS_GEN2(dev)) {
  4034. if (IS_PINEVIEW(dev))
  4035. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  4036. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  4037. else
  4038. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  4039. DPLL_FPA01_P1_POST_DIV_SHIFT);
  4040. switch (dpll & DPLL_MODE_MASK) {
  4041. case DPLLB_MODE_DAC_SERIAL:
  4042. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  4043. 5 : 10;
  4044. break;
  4045. case DPLLB_MODE_LVDS:
  4046. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  4047. 7 : 14;
  4048. break;
  4049. default:
  4050. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  4051. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  4052. return 0;
  4053. }
  4054. /* XXX: Handle the 100Mhz refclk */
  4055. intel_clock(dev, 96000, &clock);
  4056. } else {
  4057. bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
  4058. if (is_lvds) {
  4059. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  4060. DPLL_FPA01_P1_POST_DIV_SHIFT);
  4061. clock.p2 = 14;
  4062. if ((dpll & PLL_REF_INPUT_MASK) ==
  4063. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  4064. /* XXX: might not be 66MHz */
  4065. intel_clock(dev, 66000, &clock);
  4066. } else
  4067. intel_clock(dev, 48000, &clock);
  4068. } else {
  4069. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  4070. clock.p1 = 2;
  4071. else {
  4072. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  4073. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  4074. }
  4075. if (dpll & PLL_P2_DIVIDE_BY_4)
  4076. clock.p2 = 4;
  4077. else
  4078. clock.p2 = 2;
  4079. intel_clock(dev, 48000, &clock);
  4080. }
  4081. }
  4082. /* XXX: It would be nice to validate the clocks, but we can't reuse
  4083. * i830PllIsValid() because it relies on the xf86_config connector
  4084. * configuration being accurate, which it isn't necessarily.
  4085. */
  4086. return clock.dot;
  4087. }
  4088. /** Returns the currently programmed mode of the given pipe. */
  4089. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  4090. struct drm_crtc *crtc)
  4091. {
  4092. struct drm_i915_private *dev_priv = dev->dev_private;
  4093. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4094. int pipe = intel_crtc->pipe;
  4095. struct drm_display_mode *mode;
  4096. int htot = I915_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
  4097. int hsync = I915_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
  4098. int vtot = I915_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
  4099. int vsync = I915_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
  4100. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  4101. if (!mode)
  4102. return NULL;
  4103. mode->clock = intel_crtc_clock_get(dev, crtc);
  4104. mode->hdisplay = (htot & 0xffff) + 1;
  4105. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  4106. mode->hsync_start = (hsync & 0xffff) + 1;
  4107. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  4108. mode->vdisplay = (vtot & 0xffff) + 1;
  4109. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  4110. mode->vsync_start = (vsync & 0xffff) + 1;
  4111. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  4112. drm_mode_set_name(mode);
  4113. drm_mode_set_crtcinfo(mode, 0);
  4114. return mode;
  4115. }
  4116. #define GPU_IDLE_TIMEOUT 500 /* ms */
  4117. /* When this timer fires, we've been idle for awhile */
  4118. static void intel_gpu_idle_timer(unsigned long arg)
  4119. {
  4120. struct drm_device *dev = (struct drm_device *)arg;
  4121. drm_i915_private_t *dev_priv = dev->dev_private;
  4122. dev_priv->busy = false;
  4123. queue_work(dev_priv->wq, &dev_priv->idle_work);
  4124. }
  4125. #define CRTC_IDLE_TIMEOUT 1000 /* ms */
  4126. static void intel_crtc_idle_timer(unsigned long arg)
  4127. {
  4128. struct intel_crtc *intel_crtc = (struct intel_crtc *)arg;
  4129. struct drm_crtc *crtc = &intel_crtc->base;
  4130. drm_i915_private_t *dev_priv = crtc->dev->dev_private;
  4131. intel_crtc->busy = false;
  4132. queue_work(dev_priv->wq, &dev_priv->idle_work);
  4133. }
  4134. static void intel_increase_pllclock(struct drm_crtc *crtc)
  4135. {
  4136. struct drm_device *dev = crtc->dev;
  4137. drm_i915_private_t *dev_priv = dev->dev_private;
  4138. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4139. int pipe = intel_crtc->pipe;
  4140. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  4141. int dpll = I915_READ(dpll_reg);
  4142. if (HAS_PCH_SPLIT(dev))
  4143. return;
  4144. if (!dev_priv->lvds_downclock_avail)
  4145. return;
  4146. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  4147. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  4148. /* Unlock panel regs */
  4149. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) |
  4150. PANEL_UNLOCK_REGS);
  4151. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  4152. I915_WRITE(dpll_reg, dpll);
  4153. dpll = I915_READ(dpll_reg);
  4154. intel_wait_for_vblank(dev, pipe);
  4155. dpll = I915_READ(dpll_reg);
  4156. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  4157. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  4158. /* ...and lock them again */
  4159. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
  4160. }
  4161. /* Schedule downclock */
  4162. mod_timer(&intel_crtc->idle_timer, jiffies +
  4163. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  4164. }
  4165. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  4166. {
  4167. struct drm_device *dev = crtc->dev;
  4168. drm_i915_private_t *dev_priv = dev->dev_private;
  4169. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4170. int pipe = intel_crtc->pipe;
  4171. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  4172. int dpll = I915_READ(dpll_reg);
  4173. if (HAS_PCH_SPLIT(dev))
  4174. return;
  4175. if (!dev_priv->lvds_downclock_avail)
  4176. return;
  4177. /*
  4178. * Since this is called by a timer, we should never get here in
  4179. * the manual case.
  4180. */
  4181. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  4182. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  4183. /* Unlock panel regs */
  4184. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) |
  4185. PANEL_UNLOCK_REGS);
  4186. dpll |= DISPLAY_RATE_SELECT_FPA1;
  4187. I915_WRITE(dpll_reg, dpll);
  4188. dpll = I915_READ(dpll_reg);
  4189. intel_wait_for_vblank(dev, pipe);
  4190. dpll = I915_READ(dpll_reg);
  4191. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  4192. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  4193. /* ...and lock them again */
  4194. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
  4195. }
  4196. }
  4197. /**
  4198. * intel_idle_update - adjust clocks for idleness
  4199. * @work: work struct
  4200. *
  4201. * Either the GPU or display (or both) went idle. Check the busy status
  4202. * here and adjust the CRTC and GPU clocks as necessary.
  4203. */
  4204. static void intel_idle_update(struct work_struct *work)
  4205. {
  4206. drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
  4207. idle_work);
  4208. struct drm_device *dev = dev_priv->dev;
  4209. struct drm_crtc *crtc;
  4210. struct intel_crtc *intel_crtc;
  4211. int enabled = 0;
  4212. if (!i915_powersave)
  4213. return;
  4214. mutex_lock(&dev->struct_mutex);
  4215. i915_update_gfx_val(dev_priv);
  4216. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  4217. /* Skip inactive CRTCs */
  4218. if (!crtc->fb)
  4219. continue;
  4220. enabled++;
  4221. intel_crtc = to_intel_crtc(crtc);
  4222. if (!intel_crtc->busy)
  4223. intel_decrease_pllclock(crtc);
  4224. }
  4225. if ((enabled == 1) && (IS_I945G(dev) || IS_I945GM(dev))) {
  4226. DRM_DEBUG_DRIVER("enable memory self refresh on 945\n");
  4227. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
  4228. }
  4229. mutex_unlock(&dev->struct_mutex);
  4230. }
  4231. /**
  4232. * intel_mark_busy - mark the GPU and possibly the display busy
  4233. * @dev: drm device
  4234. * @obj: object we're operating on
  4235. *
  4236. * Callers can use this function to indicate that the GPU is busy processing
  4237. * commands. If @obj matches one of the CRTC objects (i.e. it's a scanout
  4238. * buffer), we'll also mark the display as busy, so we know to increase its
  4239. * clock frequency.
  4240. */
  4241. void intel_mark_busy(struct drm_device *dev, struct drm_gem_object *obj)
  4242. {
  4243. drm_i915_private_t *dev_priv = dev->dev_private;
  4244. struct drm_crtc *crtc = NULL;
  4245. struct intel_framebuffer *intel_fb;
  4246. struct intel_crtc *intel_crtc;
  4247. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4248. return;
  4249. if (!dev_priv->busy) {
  4250. if (IS_I945G(dev) || IS_I945GM(dev)) {
  4251. u32 fw_blc_self;
  4252. DRM_DEBUG_DRIVER("disable memory self refresh on 945\n");
  4253. fw_blc_self = I915_READ(FW_BLC_SELF);
  4254. fw_blc_self &= ~FW_BLC_SELF_EN;
  4255. I915_WRITE(FW_BLC_SELF, fw_blc_self | FW_BLC_SELF_EN_MASK);
  4256. }
  4257. dev_priv->busy = true;
  4258. } else
  4259. mod_timer(&dev_priv->idle_timer, jiffies +
  4260. msecs_to_jiffies(GPU_IDLE_TIMEOUT));
  4261. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  4262. if (!crtc->fb)
  4263. continue;
  4264. intel_crtc = to_intel_crtc(crtc);
  4265. intel_fb = to_intel_framebuffer(crtc->fb);
  4266. if (intel_fb->obj == obj) {
  4267. if (!intel_crtc->busy) {
  4268. if (IS_I945G(dev) || IS_I945GM(dev)) {
  4269. u32 fw_blc_self;
  4270. DRM_DEBUG_DRIVER("disable memory self refresh on 945\n");
  4271. fw_blc_self = I915_READ(FW_BLC_SELF);
  4272. fw_blc_self &= ~FW_BLC_SELF_EN;
  4273. I915_WRITE(FW_BLC_SELF, fw_blc_self | FW_BLC_SELF_EN_MASK);
  4274. }
  4275. /* Non-busy -> busy, upclock */
  4276. intel_increase_pllclock(crtc);
  4277. intel_crtc->busy = true;
  4278. } else {
  4279. /* Busy -> busy, put off timer */
  4280. mod_timer(&intel_crtc->idle_timer, jiffies +
  4281. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  4282. }
  4283. }
  4284. }
  4285. }
  4286. static void intel_crtc_destroy(struct drm_crtc *crtc)
  4287. {
  4288. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4289. struct drm_device *dev = crtc->dev;
  4290. struct intel_unpin_work *work;
  4291. unsigned long flags;
  4292. spin_lock_irqsave(&dev->event_lock, flags);
  4293. work = intel_crtc->unpin_work;
  4294. intel_crtc->unpin_work = NULL;
  4295. spin_unlock_irqrestore(&dev->event_lock, flags);
  4296. if (work) {
  4297. cancel_work_sync(&work->work);
  4298. kfree(work);
  4299. }
  4300. drm_crtc_cleanup(crtc);
  4301. kfree(intel_crtc);
  4302. }
  4303. static void intel_unpin_work_fn(struct work_struct *__work)
  4304. {
  4305. struct intel_unpin_work *work =
  4306. container_of(__work, struct intel_unpin_work, work);
  4307. mutex_lock(&work->dev->struct_mutex);
  4308. i915_gem_object_unpin(work->old_fb_obj);
  4309. drm_gem_object_unreference(work->pending_flip_obj);
  4310. drm_gem_object_unreference(work->old_fb_obj);
  4311. mutex_unlock(&work->dev->struct_mutex);
  4312. kfree(work);
  4313. }
  4314. static void do_intel_finish_page_flip(struct drm_device *dev,
  4315. struct drm_crtc *crtc)
  4316. {
  4317. drm_i915_private_t *dev_priv = dev->dev_private;
  4318. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4319. struct intel_unpin_work *work;
  4320. struct drm_i915_gem_object *obj_priv;
  4321. struct drm_pending_vblank_event *e;
  4322. struct timeval now;
  4323. unsigned long flags;
  4324. /* Ignore early vblank irqs */
  4325. if (intel_crtc == NULL)
  4326. return;
  4327. spin_lock_irqsave(&dev->event_lock, flags);
  4328. work = intel_crtc->unpin_work;
  4329. if (work == NULL || !work->pending) {
  4330. spin_unlock_irqrestore(&dev->event_lock, flags);
  4331. return;
  4332. }
  4333. intel_crtc->unpin_work = NULL;
  4334. drm_vblank_put(dev, intel_crtc->pipe);
  4335. if (work->event) {
  4336. e = work->event;
  4337. do_gettimeofday(&now);
  4338. e->event.sequence = drm_vblank_count(dev, intel_crtc->pipe);
  4339. e->event.tv_sec = now.tv_sec;
  4340. e->event.tv_usec = now.tv_usec;
  4341. list_add_tail(&e->base.link,
  4342. &e->base.file_priv->event_list);
  4343. wake_up_interruptible(&e->base.file_priv->event_wait);
  4344. }
  4345. spin_unlock_irqrestore(&dev->event_lock, flags);
  4346. obj_priv = to_intel_bo(work->old_fb_obj);
  4347. atomic_clear_mask(1 << intel_crtc->plane,
  4348. &obj_priv->pending_flip.counter);
  4349. if (atomic_read(&obj_priv->pending_flip) == 0)
  4350. wake_up(&dev_priv->pending_flip_queue);
  4351. schedule_work(&work->work);
  4352. trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
  4353. }
  4354. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  4355. {
  4356. drm_i915_private_t *dev_priv = dev->dev_private;
  4357. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  4358. do_intel_finish_page_flip(dev, crtc);
  4359. }
  4360. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  4361. {
  4362. drm_i915_private_t *dev_priv = dev->dev_private;
  4363. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  4364. do_intel_finish_page_flip(dev, crtc);
  4365. }
  4366. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  4367. {
  4368. drm_i915_private_t *dev_priv = dev->dev_private;
  4369. struct intel_crtc *intel_crtc =
  4370. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  4371. unsigned long flags;
  4372. spin_lock_irqsave(&dev->event_lock, flags);
  4373. if (intel_crtc->unpin_work) {
  4374. if ((++intel_crtc->unpin_work->pending) > 1)
  4375. DRM_ERROR("Prepared flip multiple times\n");
  4376. } else {
  4377. DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
  4378. }
  4379. spin_unlock_irqrestore(&dev->event_lock, flags);
  4380. }
  4381. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  4382. struct drm_framebuffer *fb,
  4383. struct drm_pending_vblank_event *event)
  4384. {
  4385. struct drm_device *dev = crtc->dev;
  4386. struct drm_i915_private *dev_priv = dev->dev_private;
  4387. struct intel_framebuffer *intel_fb;
  4388. struct drm_i915_gem_object *obj_priv;
  4389. struct drm_gem_object *obj;
  4390. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4391. struct intel_unpin_work *work;
  4392. unsigned long flags, offset;
  4393. int pipe = intel_crtc->pipe;
  4394. u32 pf, pipesrc;
  4395. int ret;
  4396. work = kzalloc(sizeof *work, GFP_KERNEL);
  4397. if (work == NULL)
  4398. return -ENOMEM;
  4399. work->event = event;
  4400. work->dev = crtc->dev;
  4401. intel_fb = to_intel_framebuffer(crtc->fb);
  4402. work->old_fb_obj = intel_fb->obj;
  4403. INIT_WORK(&work->work, intel_unpin_work_fn);
  4404. /* We borrow the event spin lock for protecting unpin_work */
  4405. spin_lock_irqsave(&dev->event_lock, flags);
  4406. if (intel_crtc->unpin_work) {
  4407. spin_unlock_irqrestore(&dev->event_lock, flags);
  4408. kfree(work);
  4409. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  4410. return -EBUSY;
  4411. }
  4412. intel_crtc->unpin_work = work;
  4413. spin_unlock_irqrestore(&dev->event_lock, flags);
  4414. intel_fb = to_intel_framebuffer(fb);
  4415. obj = intel_fb->obj;
  4416. mutex_lock(&dev->struct_mutex);
  4417. ret = intel_pin_and_fence_fb_obj(dev, obj, true);
  4418. if (ret)
  4419. goto cleanup_work;
  4420. /* Reference the objects for the scheduled work. */
  4421. drm_gem_object_reference(work->old_fb_obj);
  4422. drm_gem_object_reference(obj);
  4423. crtc->fb = fb;
  4424. ret = drm_vblank_get(dev, intel_crtc->pipe);
  4425. if (ret)
  4426. goto cleanup_objs;
  4427. if (IS_GEN3(dev) || IS_GEN2(dev)) {
  4428. u32 flip_mask;
  4429. /* Can't queue multiple flips, so wait for the previous
  4430. * one to finish before executing the next.
  4431. */
  4432. ret = BEGIN_LP_RING(2);
  4433. if (ret)
  4434. goto cleanup_objs;
  4435. if (intel_crtc->plane)
  4436. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  4437. else
  4438. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  4439. OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
  4440. OUT_RING(MI_NOOP);
  4441. ADVANCE_LP_RING();
  4442. }
  4443. work->pending_flip_obj = obj;
  4444. obj_priv = to_intel_bo(obj);
  4445. work->enable_stall_check = true;
  4446. /* Offset into the new buffer for cases of shared fbs between CRTCs */
  4447. offset = crtc->y * fb->pitch + crtc->x * fb->bits_per_pixel/8;
  4448. ret = BEGIN_LP_RING(4);
  4449. if (ret)
  4450. goto cleanup_objs;
  4451. /* Block clients from rendering to the new back buffer until
  4452. * the flip occurs and the object is no longer visible.
  4453. */
  4454. atomic_add(1 << intel_crtc->plane,
  4455. &to_intel_bo(work->old_fb_obj)->pending_flip);
  4456. switch (INTEL_INFO(dev)->gen) {
  4457. case 2:
  4458. OUT_RING(MI_DISPLAY_FLIP |
  4459. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  4460. OUT_RING(fb->pitch);
  4461. OUT_RING(obj_priv->gtt_offset + offset);
  4462. OUT_RING(MI_NOOP);
  4463. break;
  4464. case 3:
  4465. OUT_RING(MI_DISPLAY_FLIP_I915 |
  4466. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  4467. OUT_RING(fb->pitch);
  4468. OUT_RING(obj_priv->gtt_offset + offset);
  4469. OUT_RING(MI_NOOP);
  4470. break;
  4471. case 4:
  4472. case 5:
  4473. /* i965+ uses the linear or tiled offsets from the
  4474. * Display Registers (which do not change across a page-flip)
  4475. * so we need only reprogram the base address.
  4476. */
  4477. OUT_RING(MI_DISPLAY_FLIP |
  4478. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  4479. OUT_RING(fb->pitch);
  4480. OUT_RING(obj_priv->gtt_offset | obj_priv->tiling_mode);
  4481. /* XXX Enabling the panel-fitter across page-flip is so far
  4482. * untested on non-native modes, so ignore it for now.
  4483. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  4484. */
  4485. pf = 0;
  4486. pipesrc = I915_READ(pipe == 0 ? PIPEASRC : PIPEBSRC) & 0x0fff0fff;
  4487. OUT_RING(pf | pipesrc);
  4488. break;
  4489. case 6:
  4490. OUT_RING(MI_DISPLAY_FLIP |
  4491. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  4492. OUT_RING(fb->pitch | obj_priv->tiling_mode);
  4493. OUT_RING(obj_priv->gtt_offset);
  4494. pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  4495. pipesrc = I915_READ(pipe == 0 ? PIPEASRC : PIPEBSRC) & 0x0fff0fff;
  4496. OUT_RING(pf | pipesrc);
  4497. break;
  4498. }
  4499. ADVANCE_LP_RING();
  4500. mutex_unlock(&dev->struct_mutex);
  4501. trace_i915_flip_request(intel_crtc->plane, obj);
  4502. return 0;
  4503. cleanup_objs:
  4504. drm_gem_object_unreference(work->old_fb_obj);
  4505. drm_gem_object_unreference(obj);
  4506. cleanup_work:
  4507. mutex_unlock(&dev->struct_mutex);
  4508. spin_lock_irqsave(&dev->event_lock, flags);
  4509. intel_crtc->unpin_work = NULL;
  4510. spin_unlock_irqrestore(&dev->event_lock, flags);
  4511. kfree(work);
  4512. return ret;
  4513. }
  4514. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  4515. .dpms = intel_crtc_dpms,
  4516. .mode_fixup = intel_crtc_mode_fixup,
  4517. .mode_set = intel_crtc_mode_set,
  4518. .mode_set_base = intel_pipe_set_base,
  4519. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  4520. .load_lut = intel_crtc_load_lut,
  4521. .disable = intel_crtc_disable,
  4522. };
  4523. static const struct drm_crtc_funcs intel_crtc_funcs = {
  4524. .cursor_set = intel_crtc_cursor_set,
  4525. .cursor_move = intel_crtc_cursor_move,
  4526. .gamma_set = intel_crtc_gamma_set,
  4527. .set_config = drm_crtc_helper_set_config,
  4528. .destroy = intel_crtc_destroy,
  4529. .page_flip = intel_crtc_page_flip,
  4530. };
  4531. static void intel_crtc_init(struct drm_device *dev, int pipe)
  4532. {
  4533. drm_i915_private_t *dev_priv = dev->dev_private;
  4534. struct intel_crtc *intel_crtc;
  4535. int i;
  4536. intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  4537. if (intel_crtc == NULL)
  4538. return;
  4539. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  4540. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  4541. for (i = 0; i < 256; i++) {
  4542. intel_crtc->lut_r[i] = i;
  4543. intel_crtc->lut_g[i] = i;
  4544. intel_crtc->lut_b[i] = i;
  4545. }
  4546. /* Swap pipes & planes for FBC on pre-965 */
  4547. intel_crtc->pipe = pipe;
  4548. intel_crtc->plane = pipe;
  4549. if (IS_MOBILE(dev) && IS_GEN3(dev)) {
  4550. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  4551. intel_crtc->plane = !pipe;
  4552. }
  4553. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  4554. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  4555. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  4556. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  4557. intel_crtc->cursor_addr = 0;
  4558. intel_crtc->dpms_mode = -1;
  4559. intel_crtc->active = true; /* force the pipe off on setup_init_config */
  4560. if (HAS_PCH_SPLIT(dev)) {
  4561. intel_helper_funcs.prepare = ironlake_crtc_prepare;
  4562. intel_helper_funcs.commit = ironlake_crtc_commit;
  4563. } else {
  4564. intel_helper_funcs.prepare = i9xx_crtc_prepare;
  4565. intel_helper_funcs.commit = i9xx_crtc_commit;
  4566. }
  4567. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  4568. intel_crtc->busy = false;
  4569. setup_timer(&intel_crtc->idle_timer, intel_crtc_idle_timer,
  4570. (unsigned long)intel_crtc);
  4571. }
  4572. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  4573. struct drm_file *file_priv)
  4574. {
  4575. drm_i915_private_t *dev_priv = dev->dev_private;
  4576. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  4577. struct drm_mode_object *drmmode_obj;
  4578. struct intel_crtc *crtc;
  4579. if (!dev_priv) {
  4580. DRM_ERROR("called with no initialization\n");
  4581. return -EINVAL;
  4582. }
  4583. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  4584. DRM_MODE_OBJECT_CRTC);
  4585. if (!drmmode_obj) {
  4586. DRM_ERROR("no such CRTC id\n");
  4587. return -EINVAL;
  4588. }
  4589. crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
  4590. pipe_from_crtc_id->pipe = crtc->pipe;
  4591. return 0;
  4592. }
  4593. static int intel_encoder_clones(struct drm_device *dev, int type_mask)
  4594. {
  4595. struct intel_encoder *encoder;
  4596. int index_mask = 0;
  4597. int entry = 0;
  4598. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  4599. if (type_mask & encoder->clone_mask)
  4600. index_mask |= (1 << entry);
  4601. entry++;
  4602. }
  4603. return index_mask;
  4604. }
  4605. static void intel_setup_outputs(struct drm_device *dev)
  4606. {
  4607. struct drm_i915_private *dev_priv = dev->dev_private;
  4608. struct intel_encoder *encoder;
  4609. bool dpd_is_edp = false;
  4610. if (IS_MOBILE(dev) && !IS_I830(dev))
  4611. intel_lvds_init(dev);
  4612. if (HAS_PCH_SPLIT(dev)) {
  4613. dpd_is_edp = intel_dpd_is_edp(dev);
  4614. if (IS_MOBILE(dev) && (I915_READ(DP_A) & DP_DETECTED))
  4615. intel_dp_init(dev, DP_A);
  4616. if (dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  4617. intel_dp_init(dev, PCH_DP_D);
  4618. }
  4619. intel_crt_init(dev);
  4620. if (HAS_PCH_SPLIT(dev)) {
  4621. int found;
  4622. if (I915_READ(HDMIB) & PORT_DETECTED) {
  4623. /* PCH SDVOB multiplex with HDMIB */
  4624. found = intel_sdvo_init(dev, PCH_SDVOB);
  4625. if (!found)
  4626. intel_hdmi_init(dev, HDMIB);
  4627. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  4628. intel_dp_init(dev, PCH_DP_B);
  4629. }
  4630. if (I915_READ(HDMIC) & PORT_DETECTED)
  4631. intel_hdmi_init(dev, HDMIC);
  4632. if (I915_READ(HDMID) & PORT_DETECTED)
  4633. intel_hdmi_init(dev, HDMID);
  4634. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  4635. intel_dp_init(dev, PCH_DP_C);
  4636. if (!dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  4637. intel_dp_init(dev, PCH_DP_D);
  4638. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  4639. bool found = false;
  4640. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  4641. DRM_DEBUG_KMS("probing SDVOB\n");
  4642. found = intel_sdvo_init(dev, SDVOB);
  4643. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  4644. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  4645. intel_hdmi_init(dev, SDVOB);
  4646. }
  4647. if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
  4648. DRM_DEBUG_KMS("probing DP_B\n");
  4649. intel_dp_init(dev, DP_B);
  4650. }
  4651. }
  4652. /* Before G4X SDVOC doesn't have its own detect register */
  4653. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  4654. DRM_DEBUG_KMS("probing SDVOC\n");
  4655. found = intel_sdvo_init(dev, SDVOC);
  4656. }
  4657. if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
  4658. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  4659. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  4660. intel_hdmi_init(dev, SDVOC);
  4661. }
  4662. if (SUPPORTS_INTEGRATED_DP(dev)) {
  4663. DRM_DEBUG_KMS("probing DP_C\n");
  4664. intel_dp_init(dev, DP_C);
  4665. }
  4666. }
  4667. if (SUPPORTS_INTEGRATED_DP(dev) &&
  4668. (I915_READ(DP_D) & DP_DETECTED)) {
  4669. DRM_DEBUG_KMS("probing DP_D\n");
  4670. intel_dp_init(dev, DP_D);
  4671. }
  4672. } else if (IS_GEN2(dev))
  4673. intel_dvo_init(dev);
  4674. if (SUPPORTS_TV(dev))
  4675. intel_tv_init(dev);
  4676. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  4677. encoder->base.possible_crtcs = encoder->crtc_mask;
  4678. encoder->base.possible_clones =
  4679. intel_encoder_clones(dev, encoder->clone_mask);
  4680. }
  4681. }
  4682. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  4683. {
  4684. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  4685. drm_framebuffer_cleanup(fb);
  4686. drm_gem_object_unreference_unlocked(intel_fb->obj);
  4687. kfree(intel_fb);
  4688. }
  4689. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  4690. struct drm_file *file_priv,
  4691. unsigned int *handle)
  4692. {
  4693. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  4694. struct drm_gem_object *object = intel_fb->obj;
  4695. return drm_gem_handle_create(file_priv, object, handle);
  4696. }
  4697. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  4698. .destroy = intel_user_framebuffer_destroy,
  4699. .create_handle = intel_user_framebuffer_create_handle,
  4700. };
  4701. int intel_framebuffer_init(struct drm_device *dev,
  4702. struct intel_framebuffer *intel_fb,
  4703. struct drm_mode_fb_cmd *mode_cmd,
  4704. struct drm_gem_object *obj)
  4705. {
  4706. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  4707. int ret;
  4708. if (obj_priv->tiling_mode == I915_TILING_Y)
  4709. return -EINVAL;
  4710. if (mode_cmd->pitch & 63)
  4711. return -EINVAL;
  4712. switch (mode_cmd->bpp) {
  4713. case 8:
  4714. case 16:
  4715. case 24:
  4716. case 32:
  4717. break;
  4718. default:
  4719. return -EINVAL;
  4720. }
  4721. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  4722. if (ret) {
  4723. DRM_ERROR("framebuffer init failed %d\n", ret);
  4724. return ret;
  4725. }
  4726. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  4727. intel_fb->obj = obj;
  4728. return 0;
  4729. }
  4730. static struct drm_framebuffer *
  4731. intel_user_framebuffer_create(struct drm_device *dev,
  4732. struct drm_file *filp,
  4733. struct drm_mode_fb_cmd *mode_cmd)
  4734. {
  4735. struct drm_gem_object *obj;
  4736. struct intel_framebuffer *intel_fb;
  4737. int ret;
  4738. obj = drm_gem_object_lookup(dev, filp, mode_cmd->handle);
  4739. if (!obj)
  4740. return ERR_PTR(-ENOENT);
  4741. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  4742. if (!intel_fb)
  4743. return ERR_PTR(-ENOMEM);
  4744. ret = intel_framebuffer_init(dev, intel_fb,
  4745. mode_cmd, obj);
  4746. if (ret) {
  4747. drm_gem_object_unreference_unlocked(obj);
  4748. kfree(intel_fb);
  4749. return ERR_PTR(ret);
  4750. }
  4751. return &intel_fb->base;
  4752. }
  4753. static const struct drm_mode_config_funcs intel_mode_funcs = {
  4754. .fb_create = intel_user_framebuffer_create,
  4755. .output_poll_changed = intel_fb_output_poll_changed,
  4756. };
  4757. static struct drm_gem_object *
  4758. intel_alloc_context_page(struct drm_device *dev)
  4759. {
  4760. struct drm_gem_object *ctx;
  4761. int ret;
  4762. ctx = i915_gem_alloc_object(dev, 4096);
  4763. if (!ctx) {
  4764. DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
  4765. return NULL;
  4766. }
  4767. mutex_lock(&dev->struct_mutex);
  4768. ret = i915_gem_object_pin(ctx, 4096, true);
  4769. if (ret) {
  4770. DRM_ERROR("failed to pin power context: %d\n", ret);
  4771. goto err_unref;
  4772. }
  4773. ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
  4774. if (ret) {
  4775. DRM_ERROR("failed to set-domain on power context: %d\n", ret);
  4776. goto err_unpin;
  4777. }
  4778. mutex_unlock(&dev->struct_mutex);
  4779. return ctx;
  4780. err_unpin:
  4781. i915_gem_object_unpin(ctx);
  4782. err_unref:
  4783. drm_gem_object_unreference(ctx);
  4784. mutex_unlock(&dev->struct_mutex);
  4785. return NULL;
  4786. }
  4787. bool ironlake_set_drps(struct drm_device *dev, u8 val)
  4788. {
  4789. struct drm_i915_private *dev_priv = dev->dev_private;
  4790. u16 rgvswctl;
  4791. rgvswctl = I915_READ16(MEMSWCTL);
  4792. if (rgvswctl & MEMCTL_CMD_STS) {
  4793. DRM_DEBUG("gpu busy, RCS change rejected\n");
  4794. return false; /* still busy with another command */
  4795. }
  4796. rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
  4797. (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
  4798. I915_WRITE16(MEMSWCTL, rgvswctl);
  4799. POSTING_READ16(MEMSWCTL);
  4800. rgvswctl |= MEMCTL_CMD_STS;
  4801. I915_WRITE16(MEMSWCTL, rgvswctl);
  4802. return true;
  4803. }
  4804. void ironlake_enable_drps(struct drm_device *dev)
  4805. {
  4806. struct drm_i915_private *dev_priv = dev->dev_private;
  4807. u32 rgvmodectl = I915_READ(MEMMODECTL);
  4808. u8 fmax, fmin, fstart, vstart;
  4809. /* Enable temp reporting */
  4810. I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
  4811. I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
  4812. /* 100ms RC evaluation intervals */
  4813. I915_WRITE(RCUPEI, 100000);
  4814. I915_WRITE(RCDNEI, 100000);
  4815. /* Set max/min thresholds to 90ms and 80ms respectively */
  4816. I915_WRITE(RCBMAXAVG, 90000);
  4817. I915_WRITE(RCBMINAVG, 80000);
  4818. I915_WRITE(MEMIHYST, 1);
  4819. /* Set up min, max, and cur for interrupt handling */
  4820. fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
  4821. fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
  4822. fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
  4823. MEMMODE_FSTART_SHIFT;
  4824. vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
  4825. PXVFREQ_PX_SHIFT;
  4826. dev_priv->fmax = fmax; /* IPS callback will increase this */
  4827. dev_priv->fstart = fstart;
  4828. dev_priv->max_delay = fstart;
  4829. dev_priv->min_delay = fmin;
  4830. dev_priv->cur_delay = fstart;
  4831. DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
  4832. fmax, fmin, fstart);
  4833. I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
  4834. /*
  4835. * Interrupts will be enabled in ironlake_irq_postinstall
  4836. */
  4837. I915_WRITE(VIDSTART, vstart);
  4838. POSTING_READ(VIDSTART);
  4839. rgvmodectl |= MEMMODE_SWMODE_EN;
  4840. I915_WRITE(MEMMODECTL, rgvmodectl);
  4841. if (wait_for((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
  4842. DRM_ERROR("stuck trying to change perf mode\n");
  4843. msleep(1);
  4844. ironlake_set_drps(dev, fstart);
  4845. dev_priv->last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
  4846. I915_READ(0x112e0);
  4847. dev_priv->last_time1 = jiffies_to_msecs(jiffies);
  4848. dev_priv->last_count2 = I915_READ(0x112f4);
  4849. getrawmonotonic(&dev_priv->last_time2);
  4850. }
  4851. void ironlake_disable_drps(struct drm_device *dev)
  4852. {
  4853. struct drm_i915_private *dev_priv = dev->dev_private;
  4854. u16 rgvswctl = I915_READ16(MEMSWCTL);
  4855. /* Ack interrupts, disable EFC interrupt */
  4856. I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
  4857. I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
  4858. I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
  4859. I915_WRITE(DEIIR, DE_PCU_EVENT);
  4860. I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
  4861. /* Go back to the starting frequency */
  4862. ironlake_set_drps(dev, dev_priv->fstart);
  4863. msleep(1);
  4864. rgvswctl |= MEMCTL_CMD_STS;
  4865. I915_WRITE(MEMSWCTL, rgvswctl);
  4866. msleep(1);
  4867. }
  4868. static unsigned long intel_pxfreq(u32 vidfreq)
  4869. {
  4870. unsigned long freq;
  4871. int div = (vidfreq & 0x3f0000) >> 16;
  4872. int post = (vidfreq & 0x3000) >> 12;
  4873. int pre = (vidfreq & 0x7);
  4874. if (!pre)
  4875. return 0;
  4876. freq = ((div * 133333) / ((1<<post) * pre));
  4877. return freq;
  4878. }
  4879. void intel_init_emon(struct drm_device *dev)
  4880. {
  4881. struct drm_i915_private *dev_priv = dev->dev_private;
  4882. u32 lcfuse;
  4883. u8 pxw[16];
  4884. int i;
  4885. /* Disable to program */
  4886. I915_WRITE(ECR, 0);
  4887. POSTING_READ(ECR);
  4888. /* Program energy weights for various events */
  4889. I915_WRITE(SDEW, 0x15040d00);
  4890. I915_WRITE(CSIEW0, 0x007f0000);
  4891. I915_WRITE(CSIEW1, 0x1e220004);
  4892. I915_WRITE(CSIEW2, 0x04000004);
  4893. for (i = 0; i < 5; i++)
  4894. I915_WRITE(PEW + (i * 4), 0);
  4895. for (i = 0; i < 3; i++)
  4896. I915_WRITE(DEW + (i * 4), 0);
  4897. /* Program P-state weights to account for frequency power adjustment */
  4898. for (i = 0; i < 16; i++) {
  4899. u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
  4900. unsigned long freq = intel_pxfreq(pxvidfreq);
  4901. unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
  4902. PXVFREQ_PX_SHIFT;
  4903. unsigned long val;
  4904. val = vid * vid;
  4905. val *= (freq / 1000);
  4906. val *= 255;
  4907. val /= (127*127*900);
  4908. if (val > 0xff)
  4909. DRM_ERROR("bad pxval: %ld\n", val);
  4910. pxw[i] = val;
  4911. }
  4912. /* Render standby states get 0 weight */
  4913. pxw[14] = 0;
  4914. pxw[15] = 0;
  4915. for (i = 0; i < 4; i++) {
  4916. u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
  4917. (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
  4918. I915_WRITE(PXW + (i * 4), val);
  4919. }
  4920. /* Adjust magic regs to magic values (more experimental results) */
  4921. I915_WRITE(OGW0, 0);
  4922. I915_WRITE(OGW1, 0);
  4923. I915_WRITE(EG0, 0x00007f00);
  4924. I915_WRITE(EG1, 0x0000000e);
  4925. I915_WRITE(EG2, 0x000e0000);
  4926. I915_WRITE(EG3, 0x68000300);
  4927. I915_WRITE(EG4, 0x42000000);
  4928. I915_WRITE(EG5, 0x00140031);
  4929. I915_WRITE(EG6, 0);
  4930. I915_WRITE(EG7, 0);
  4931. for (i = 0; i < 8; i++)
  4932. I915_WRITE(PXWL + (i * 4), 0);
  4933. /* Enable PMON + select events */
  4934. I915_WRITE(ECR, 0x80000019);
  4935. lcfuse = I915_READ(LCFUSE02);
  4936. dev_priv->corr = (lcfuse & LCFUSE_HIV_MASK);
  4937. }
  4938. void intel_init_clock_gating(struct drm_device *dev)
  4939. {
  4940. struct drm_i915_private *dev_priv = dev->dev_private;
  4941. /*
  4942. * Disable clock gating reported to work incorrectly according to the
  4943. * specs, but enable as much else as we can.
  4944. */
  4945. if (HAS_PCH_SPLIT(dev)) {
  4946. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  4947. if (IS_GEN5(dev)) {
  4948. /* Required for FBC */
  4949. dspclk_gate |= DPFDUNIT_CLOCK_GATE_DISABLE;
  4950. /* Required for CxSR */
  4951. dspclk_gate |= DPARBUNIT_CLOCK_GATE_DISABLE;
  4952. I915_WRITE(PCH_3DCGDIS0,
  4953. MARIUNIT_CLOCK_GATE_DISABLE |
  4954. SVSMUNIT_CLOCK_GATE_DISABLE);
  4955. }
  4956. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  4957. /*
  4958. * On Ibex Peak and Cougar Point, we need to disable clock
  4959. * gating for the panel power sequencer or it will fail to
  4960. * start up when no ports are active.
  4961. */
  4962. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
  4963. /*
  4964. * According to the spec the following bits should be set in
  4965. * order to enable memory self-refresh
  4966. * The bit 22/21 of 0x42004
  4967. * The bit 5 of 0x42020
  4968. * The bit 15 of 0x45000
  4969. */
  4970. if (IS_GEN5(dev)) {
  4971. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  4972. (I915_READ(ILK_DISPLAY_CHICKEN2) |
  4973. ILK_DPARB_GATE | ILK_VSDPFD_FULL));
  4974. I915_WRITE(ILK_DSPCLK_GATE,
  4975. (I915_READ(ILK_DSPCLK_GATE) |
  4976. ILK_DPARB_CLK_GATE));
  4977. I915_WRITE(DISP_ARB_CTL,
  4978. (I915_READ(DISP_ARB_CTL) |
  4979. DISP_FBC_WM_DIS));
  4980. I915_WRITE(WM3_LP_ILK, 0);
  4981. I915_WRITE(WM2_LP_ILK, 0);
  4982. I915_WRITE(WM1_LP_ILK, 0);
  4983. }
  4984. /*
  4985. * Based on the document from hardware guys the following bits
  4986. * should be set unconditionally in order to enable FBC.
  4987. * The bit 22 of 0x42000
  4988. * The bit 22 of 0x42004
  4989. * The bit 7,8,9 of 0x42020.
  4990. */
  4991. if (IS_IRONLAKE_M(dev)) {
  4992. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  4993. I915_READ(ILK_DISPLAY_CHICKEN1) |
  4994. ILK_FBCQ_DIS);
  4995. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  4996. I915_READ(ILK_DISPLAY_CHICKEN2) |
  4997. ILK_DPARB_GATE);
  4998. I915_WRITE(ILK_DSPCLK_GATE,
  4999. I915_READ(ILK_DSPCLK_GATE) |
  5000. ILK_DPFC_DIS1 |
  5001. ILK_DPFC_DIS2 |
  5002. ILK_CLK_FBC);
  5003. }
  5004. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  5005. I915_READ(ILK_DISPLAY_CHICKEN2) |
  5006. ILK_ELPIN_409_SELECT);
  5007. if (IS_GEN5(dev)) {
  5008. I915_WRITE(_3D_CHICKEN2,
  5009. _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
  5010. _3D_CHICKEN2_WM_READ_PIPELINED);
  5011. }
  5012. return;
  5013. } else if (IS_G4X(dev)) {
  5014. uint32_t dspclk_gate;
  5015. I915_WRITE(RENCLK_GATE_D1, 0);
  5016. I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
  5017. GS_UNIT_CLOCK_GATE_DISABLE |
  5018. CL_UNIT_CLOCK_GATE_DISABLE);
  5019. I915_WRITE(RAMCLK_GATE_D, 0);
  5020. dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
  5021. OVRUNIT_CLOCK_GATE_DISABLE |
  5022. OVCUNIT_CLOCK_GATE_DISABLE;
  5023. if (IS_GM45(dev))
  5024. dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
  5025. I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
  5026. } else if (IS_CRESTLINE(dev)) {
  5027. I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
  5028. I915_WRITE(RENCLK_GATE_D2, 0);
  5029. I915_WRITE(DSPCLK_GATE_D, 0);
  5030. I915_WRITE(RAMCLK_GATE_D, 0);
  5031. I915_WRITE16(DEUC, 0);
  5032. } else if (IS_BROADWATER(dev)) {
  5033. I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
  5034. I965_RCC_CLOCK_GATE_DISABLE |
  5035. I965_RCPB_CLOCK_GATE_DISABLE |
  5036. I965_ISC_CLOCK_GATE_DISABLE |
  5037. I965_FBC_CLOCK_GATE_DISABLE);
  5038. I915_WRITE(RENCLK_GATE_D2, 0);
  5039. } else if (IS_GEN3(dev)) {
  5040. u32 dstate = I915_READ(D_STATE);
  5041. dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
  5042. DSTATE_DOT_CLOCK_GATING;
  5043. I915_WRITE(D_STATE, dstate);
  5044. } else if (IS_I85X(dev) || IS_I865G(dev)) {
  5045. I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
  5046. } else if (IS_I830(dev)) {
  5047. I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
  5048. }
  5049. /*
  5050. * GPU can automatically power down the render unit if given a page
  5051. * to save state.
  5052. */
  5053. if (IS_IRONLAKE_M(dev)) {
  5054. if (dev_priv->renderctx == NULL)
  5055. dev_priv->renderctx = intel_alloc_context_page(dev);
  5056. if (dev_priv->renderctx) {
  5057. struct drm_i915_gem_object *obj_priv;
  5058. obj_priv = to_intel_bo(dev_priv->renderctx);
  5059. if (obj_priv) {
  5060. if (BEGIN_LP_RING(4) == 0) {
  5061. OUT_RING(MI_SET_CONTEXT);
  5062. OUT_RING(obj_priv->gtt_offset |
  5063. MI_MM_SPACE_GTT |
  5064. MI_SAVE_EXT_STATE_EN |
  5065. MI_RESTORE_EXT_STATE_EN |
  5066. MI_RESTORE_INHIBIT);
  5067. OUT_RING(MI_NOOP);
  5068. OUT_RING(MI_FLUSH);
  5069. ADVANCE_LP_RING();
  5070. }
  5071. }
  5072. } else
  5073. DRM_DEBUG_KMS("Failed to allocate render context."
  5074. "Disable RC6\n");
  5075. }
  5076. if (I915_HAS_RC6(dev) && drm_core_check_feature(dev, DRIVER_MODESET)) {
  5077. struct drm_i915_gem_object *obj_priv = NULL;
  5078. if (dev_priv->pwrctx) {
  5079. obj_priv = to_intel_bo(dev_priv->pwrctx);
  5080. } else {
  5081. struct drm_gem_object *pwrctx;
  5082. pwrctx = intel_alloc_context_page(dev);
  5083. if (pwrctx) {
  5084. dev_priv->pwrctx = pwrctx;
  5085. obj_priv = to_intel_bo(pwrctx);
  5086. }
  5087. }
  5088. if (obj_priv) {
  5089. I915_WRITE(PWRCTXA, obj_priv->gtt_offset | PWRCTX_EN);
  5090. I915_WRITE(MCHBAR_RENDER_STANDBY,
  5091. I915_READ(MCHBAR_RENDER_STANDBY) & ~RCX_SW_EXIT);
  5092. }
  5093. }
  5094. }
  5095. /* Set up chip specific display functions */
  5096. static void intel_init_display(struct drm_device *dev)
  5097. {
  5098. struct drm_i915_private *dev_priv = dev->dev_private;
  5099. /* We always want a DPMS function */
  5100. if (HAS_PCH_SPLIT(dev))
  5101. dev_priv->display.dpms = ironlake_crtc_dpms;
  5102. else
  5103. dev_priv->display.dpms = i9xx_crtc_dpms;
  5104. if (I915_HAS_FBC(dev)) {
  5105. if (IS_IRONLAKE_M(dev)) {
  5106. dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
  5107. dev_priv->display.enable_fbc = ironlake_enable_fbc;
  5108. dev_priv->display.disable_fbc = ironlake_disable_fbc;
  5109. } else if (IS_GM45(dev)) {
  5110. dev_priv->display.fbc_enabled = g4x_fbc_enabled;
  5111. dev_priv->display.enable_fbc = g4x_enable_fbc;
  5112. dev_priv->display.disable_fbc = g4x_disable_fbc;
  5113. } else if (IS_CRESTLINE(dev)) {
  5114. dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
  5115. dev_priv->display.enable_fbc = i8xx_enable_fbc;
  5116. dev_priv->display.disable_fbc = i8xx_disable_fbc;
  5117. }
  5118. /* 855GM needs testing */
  5119. }
  5120. /* Returns the core display clock speed */
  5121. if (IS_I945G(dev) || (IS_G33(dev) && ! IS_PINEVIEW_M(dev)))
  5122. dev_priv->display.get_display_clock_speed =
  5123. i945_get_display_clock_speed;
  5124. else if (IS_I915G(dev))
  5125. dev_priv->display.get_display_clock_speed =
  5126. i915_get_display_clock_speed;
  5127. else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
  5128. dev_priv->display.get_display_clock_speed =
  5129. i9xx_misc_get_display_clock_speed;
  5130. else if (IS_I915GM(dev))
  5131. dev_priv->display.get_display_clock_speed =
  5132. i915gm_get_display_clock_speed;
  5133. else if (IS_I865G(dev))
  5134. dev_priv->display.get_display_clock_speed =
  5135. i865_get_display_clock_speed;
  5136. else if (IS_I85X(dev))
  5137. dev_priv->display.get_display_clock_speed =
  5138. i855_get_display_clock_speed;
  5139. else /* 852, 830 */
  5140. dev_priv->display.get_display_clock_speed =
  5141. i830_get_display_clock_speed;
  5142. /* For FIFO watermark updates */
  5143. if (HAS_PCH_SPLIT(dev)) {
  5144. if (IS_GEN5(dev)) {
  5145. if (I915_READ(MLTR_ILK) & ILK_SRLT_MASK)
  5146. dev_priv->display.update_wm = ironlake_update_wm;
  5147. else {
  5148. DRM_DEBUG_KMS("Failed to get proper latency. "
  5149. "Disable CxSR\n");
  5150. dev_priv->display.update_wm = NULL;
  5151. }
  5152. } else
  5153. dev_priv->display.update_wm = NULL;
  5154. } else if (IS_PINEVIEW(dev)) {
  5155. if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
  5156. dev_priv->is_ddr3,
  5157. dev_priv->fsb_freq,
  5158. dev_priv->mem_freq)) {
  5159. DRM_INFO("failed to find known CxSR latency "
  5160. "(found ddr%s fsb freq %d, mem freq %d), "
  5161. "disabling CxSR\n",
  5162. (dev_priv->is_ddr3 == 1) ? "3": "2",
  5163. dev_priv->fsb_freq, dev_priv->mem_freq);
  5164. /* Disable CxSR and never update its watermark again */
  5165. pineview_disable_cxsr(dev);
  5166. dev_priv->display.update_wm = NULL;
  5167. } else
  5168. dev_priv->display.update_wm = pineview_update_wm;
  5169. } else if (IS_G4X(dev))
  5170. dev_priv->display.update_wm = g4x_update_wm;
  5171. else if (IS_GEN4(dev))
  5172. dev_priv->display.update_wm = i965_update_wm;
  5173. else if (IS_GEN3(dev)) {
  5174. dev_priv->display.update_wm = i9xx_update_wm;
  5175. dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
  5176. } else if (IS_I85X(dev)) {
  5177. dev_priv->display.update_wm = i9xx_update_wm;
  5178. dev_priv->display.get_fifo_size = i85x_get_fifo_size;
  5179. } else {
  5180. dev_priv->display.update_wm = i830_update_wm;
  5181. if (IS_845G(dev))
  5182. dev_priv->display.get_fifo_size = i845_get_fifo_size;
  5183. else
  5184. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  5185. }
  5186. }
  5187. /*
  5188. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  5189. * resume, or other times. This quirk makes sure that's the case for
  5190. * affected systems.
  5191. */
  5192. static void quirk_pipea_force (struct drm_device *dev)
  5193. {
  5194. struct drm_i915_private *dev_priv = dev->dev_private;
  5195. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  5196. DRM_DEBUG_DRIVER("applying pipe a force quirk\n");
  5197. }
  5198. struct intel_quirk {
  5199. int device;
  5200. int subsystem_vendor;
  5201. int subsystem_device;
  5202. void (*hook)(struct drm_device *dev);
  5203. };
  5204. struct intel_quirk intel_quirks[] = {
  5205. /* HP Compaq 2730p needs pipe A force quirk (LP: #291555) */
  5206. { 0x2a42, 0x103c, 0x30eb, quirk_pipea_force },
  5207. /* HP Mini needs pipe A force quirk (LP: #322104) */
  5208. { 0x27ae,0x103c, 0x361a, quirk_pipea_force },
  5209. /* Thinkpad R31 needs pipe A force quirk */
  5210. { 0x3577, 0x1014, 0x0505, quirk_pipea_force },
  5211. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  5212. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  5213. /* ThinkPad X30 needs pipe A force quirk (LP: #304614) */
  5214. { 0x3577, 0x1014, 0x0513, quirk_pipea_force },
  5215. /* ThinkPad X40 needs pipe A force quirk */
  5216. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  5217. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  5218. /* 855 & before need to leave pipe A & dpll A up */
  5219. { 0x3582, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  5220. { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  5221. };
  5222. static void intel_init_quirks(struct drm_device *dev)
  5223. {
  5224. struct pci_dev *d = dev->pdev;
  5225. int i;
  5226. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  5227. struct intel_quirk *q = &intel_quirks[i];
  5228. if (d->device == q->device &&
  5229. (d->subsystem_vendor == q->subsystem_vendor ||
  5230. q->subsystem_vendor == PCI_ANY_ID) &&
  5231. (d->subsystem_device == q->subsystem_device ||
  5232. q->subsystem_device == PCI_ANY_ID))
  5233. q->hook(dev);
  5234. }
  5235. }
  5236. /* Disable the VGA plane that we never use */
  5237. static void i915_disable_vga(struct drm_device *dev)
  5238. {
  5239. struct drm_i915_private *dev_priv = dev->dev_private;
  5240. u8 sr1;
  5241. u32 vga_reg;
  5242. if (HAS_PCH_SPLIT(dev))
  5243. vga_reg = CPU_VGACNTRL;
  5244. else
  5245. vga_reg = VGACNTRL;
  5246. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  5247. outb(1, VGA_SR_INDEX);
  5248. sr1 = inb(VGA_SR_DATA);
  5249. outb(sr1 | 1<<5, VGA_SR_DATA);
  5250. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  5251. udelay(300);
  5252. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  5253. POSTING_READ(vga_reg);
  5254. }
  5255. void intel_modeset_init(struct drm_device *dev)
  5256. {
  5257. struct drm_i915_private *dev_priv = dev->dev_private;
  5258. int i;
  5259. drm_mode_config_init(dev);
  5260. dev->mode_config.min_width = 0;
  5261. dev->mode_config.min_height = 0;
  5262. dev->mode_config.funcs = (void *)&intel_mode_funcs;
  5263. intel_init_quirks(dev);
  5264. intel_init_display(dev);
  5265. if (IS_GEN2(dev)) {
  5266. dev->mode_config.max_width = 2048;
  5267. dev->mode_config.max_height = 2048;
  5268. } else if (IS_GEN3(dev)) {
  5269. dev->mode_config.max_width = 4096;
  5270. dev->mode_config.max_height = 4096;
  5271. } else {
  5272. dev->mode_config.max_width = 8192;
  5273. dev->mode_config.max_height = 8192;
  5274. }
  5275. /* set memory base */
  5276. if (IS_GEN2(dev))
  5277. dev->mode_config.fb_base = pci_resource_start(dev->pdev, 0);
  5278. else
  5279. dev->mode_config.fb_base = pci_resource_start(dev->pdev, 2);
  5280. if (IS_MOBILE(dev) || !IS_GEN2(dev))
  5281. dev_priv->num_pipe = 2;
  5282. else
  5283. dev_priv->num_pipe = 1;
  5284. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  5285. dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
  5286. for (i = 0; i < dev_priv->num_pipe; i++) {
  5287. intel_crtc_init(dev, i);
  5288. }
  5289. intel_setup_outputs(dev);
  5290. intel_init_clock_gating(dev);
  5291. /* Just disable it once at startup */
  5292. i915_disable_vga(dev);
  5293. if (IS_IRONLAKE_M(dev)) {
  5294. ironlake_enable_drps(dev);
  5295. intel_init_emon(dev);
  5296. }
  5297. INIT_WORK(&dev_priv->idle_work, intel_idle_update);
  5298. setup_timer(&dev_priv->idle_timer, intel_gpu_idle_timer,
  5299. (unsigned long)dev);
  5300. intel_setup_overlay(dev);
  5301. }
  5302. void intel_modeset_cleanup(struct drm_device *dev)
  5303. {
  5304. struct drm_i915_private *dev_priv = dev->dev_private;
  5305. struct drm_crtc *crtc;
  5306. struct intel_crtc *intel_crtc;
  5307. drm_kms_helper_poll_fini(dev);
  5308. mutex_lock(&dev->struct_mutex);
  5309. intel_unregister_dsm_handler();
  5310. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5311. /* Skip inactive CRTCs */
  5312. if (!crtc->fb)
  5313. continue;
  5314. intel_crtc = to_intel_crtc(crtc);
  5315. intel_increase_pllclock(crtc);
  5316. }
  5317. if (dev_priv->display.disable_fbc)
  5318. dev_priv->display.disable_fbc(dev);
  5319. if (dev_priv->renderctx) {
  5320. struct drm_i915_gem_object *obj_priv;
  5321. obj_priv = to_intel_bo(dev_priv->renderctx);
  5322. I915_WRITE(CCID, obj_priv->gtt_offset &~ CCID_EN);
  5323. I915_READ(CCID);
  5324. i915_gem_object_unpin(dev_priv->renderctx);
  5325. drm_gem_object_unreference(dev_priv->renderctx);
  5326. }
  5327. if (dev_priv->pwrctx) {
  5328. struct drm_i915_gem_object *obj_priv;
  5329. obj_priv = to_intel_bo(dev_priv->pwrctx);
  5330. I915_WRITE(PWRCTXA, obj_priv->gtt_offset &~ PWRCTX_EN);
  5331. I915_READ(PWRCTXA);
  5332. i915_gem_object_unpin(dev_priv->pwrctx);
  5333. drm_gem_object_unreference(dev_priv->pwrctx);
  5334. }
  5335. if (IS_IRONLAKE_M(dev))
  5336. ironlake_disable_drps(dev);
  5337. mutex_unlock(&dev->struct_mutex);
  5338. /* Disable the irq before mode object teardown, for the irq might
  5339. * enqueue unpin/hotplug work. */
  5340. drm_irq_uninstall(dev);
  5341. cancel_work_sync(&dev_priv->hotplug_work);
  5342. /* Shut off idle work before the crtcs get freed. */
  5343. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5344. intel_crtc = to_intel_crtc(crtc);
  5345. del_timer_sync(&intel_crtc->idle_timer);
  5346. }
  5347. del_timer_sync(&dev_priv->idle_timer);
  5348. cancel_work_sync(&dev_priv->idle_work);
  5349. drm_mode_config_cleanup(dev);
  5350. }
  5351. /*
  5352. * Return which encoder is currently attached for connector.
  5353. */
  5354. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  5355. {
  5356. return &intel_attached_encoder(connector)->base;
  5357. }
  5358. void intel_connector_attach_encoder(struct intel_connector *connector,
  5359. struct intel_encoder *encoder)
  5360. {
  5361. connector->encoder = encoder;
  5362. drm_mode_connector_attach_encoder(&connector->base,
  5363. &encoder->base);
  5364. }
  5365. /*
  5366. * set vga decode state - true == enable VGA decode
  5367. */
  5368. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  5369. {
  5370. struct drm_i915_private *dev_priv = dev->dev_private;
  5371. u16 gmch_ctrl;
  5372. pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
  5373. if (state)
  5374. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  5375. else
  5376. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  5377. pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
  5378. return 0;
  5379. }
  5380. #ifdef CONFIG_DEBUG_FS
  5381. #include <linux/seq_file.h>
  5382. struct intel_display_error_state {
  5383. struct intel_cursor_error_state {
  5384. u32 control;
  5385. u32 position;
  5386. u32 base;
  5387. u32 size;
  5388. } cursor[2];
  5389. struct intel_pipe_error_state {
  5390. u32 conf;
  5391. u32 source;
  5392. u32 htotal;
  5393. u32 hblank;
  5394. u32 hsync;
  5395. u32 vtotal;
  5396. u32 vblank;
  5397. u32 vsync;
  5398. } pipe[2];
  5399. struct intel_plane_error_state {
  5400. u32 control;
  5401. u32 stride;
  5402. u32 size;
  5403. u32 pos;
  5404. u32 addr;
  5405. u32 surface;
  5406. u32 tile_offset;
  5407. } plane[2];
  5408. };
  5409. struct intel_display_error_state *
  5410. intel_display_capture_error_state(struct drm_device *dev)
  5411. {
  5412. drm_i915_private_t *dev_priv = dev->dev_private;
  5413. struct intel_display_error_state *error;
  5414. int i;
  5415. error = kmalloc(sizeof(*error), GFP_ATOMIC);
  5416. if (error == NULL)
  5417. return NULL;
  5418. for (i = 0; i < 2; i++) {
  5419. error->cursor[i].control = I915_READ(CURCNTR(i));
  5420. error->cursor[i].position = I915_READ(CURPOS(i));
  5421. error->cursor[i].base = I915_READ(CURBASE(i));
  5422. error->plane[i].control = I915_READ(DSPCNTR(i));
  5423. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  5424. error->plane[i].size = I915_READ(DSPSIZE(i));
  5425. error->plane[i].pos= I915_READ(DSPPOS(i));
  5426. error->plane[i].addr = I915_READ(DSPADDR(i));
  5427. if (INTEL_INFO(dev)->gen >= 4) {
  5428. error->plane[i].surface = I915_READ(DSPSURF(i));
  5429. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  5430. }
  5431. error->pipe[i].conf = I915_READ(PIPECONF(i));
  5432. error->pipe[i].source = I915_READ(PIPESRC(i));
  5433. error->pipe[i].htotal = I915_READ(HTOTAL(i));
  5434. error->pipe[i].hblank = I915_READ(HBLANK(i));
  5435. error->pipe[i].hsync = I915_READ(HSYNC(i));
  5436. error->pipe[i].vtotal = I915_READ(VTOTAL(i));
  5437. error->pipe[i].vblank = I915_READ(VBLANK(i));
  5438. error->pipe[i].vsync = I915_READ(VSYNC(i));
  5439. }
  5440. return error;
  5441. }
  5442. void
  5443. intel_display_print_error_state(struct seq_file *m,
  5444. struct drm_device *dev,
  5445. struct intel_display_error_state *error)
  5446. {
  5447. int i;
  5448. for (i = 0; i < 2; i++) {
  5449. seq_printf(m, "Pipe [%d]:\n", i);
  5450. seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
  5451. seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
  5452. seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
  5453. seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
  5454. seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
  5455. seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
  5456. seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
  5457. seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
  5458. seq_printf(m, "Plane [%d]:\n", i);
  5459. seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
  5460. seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  5461. seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
  5462. seq_printf(m, " POS: %08x\n", error->plane[i].pos);
  5463. seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  5464. if (INTEL_INFO(dev)->gen >= 4) {
  5465. seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
  5466. seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  5467. }
  5468. seq_printf(m, "Cursor [%d]:\n", i);
  5469. seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  5470. seq_printf(m, " POS: %08x\n", error->cursor[i].position);
  5471. seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
  5472. }
  5473. }
  5474. #endif