intel_display.c 182 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939
  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/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include "drmP.h"
  35. #include "intel_drv.h"
  36. #include "i915_drm.h"
  37. #include "i915_drv.h"
  38. #include "i915_trace.h"
  39. #include "drm_dp_helper.h"
  40. #include "drm_crtc_helper.h"
  41. #include <linux/dma_remapping.h>
  42. #define HAS_eDP (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  43. bool intel_pipe_has_type(struct drm_crtc *crtc, int type);
  44. static void intel_increase_pllclock(struct drm_crtc *crtc);
  45. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  46. typedef struct {
  47. /* given values */
  48. int n;
  49. int m1, m2;
  50. int p1, p2;
  51. /* derived values */
  52. int dot;
  53. int vco;
  54. int m;
  55. int p;
  56. } intel_clock_t;
  57. typedef struct {
  58. int min, max;
  59. } intel_range_t;
  60. typedef struct {
  61. int dot_limit;
  62. int p2_slow, p2_fast;
  63. } intel_p2_t;
  64. #define INTEL_P2_NUM 2
  65. typedef struct intel_limit intel_limit_t;
  66. struct intel_limit {
  67. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  68. intel_p2_t p2;
  69. bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
  70. int, int, intel_clock_t *, intel_clock_t *);
  71. };
  72. /* FDI */
  73. #define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
  74. static bool
  75. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  76. int target, int refclk, intel_clock_t *match_clock,
  77. intel_clock_t *best_clock);
  78. static bool
  79. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  80. int target, int refclk, intel_clock_t *match_clock,
  81. intel_clock_t *best_clock);
  82. static bool
  83. intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
  84. int target, int refclk, intel_clock_t *match_clock,
  85. intel_clock_t *best_clock);
  86. static bool
  87. intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
  88. int target, int refclk, intel_clock_t *match_clock,
  89. intel_clock_t *best_clock);
  90. static inline u32 /* units of 100MHz */
  91. intel_fdi_link_freq(struct drm_device *dev)
  92. {
  93. if (IS_GEN5(dev)) {
  94. struct drm_i915_private *dev_priv = dev->dev_private;
  95. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  96. } else
  97. return 27;
  98. }
  99. static const intel_limit_t intel_limits_i8xx_dvo = {
  100. .dot = { .min = 25000, .max = 350000 },
  101. .vco = { .min = 930000, .max = 1400000 },
  102. .n = { .min = 3, .max = 16 },
  103. .m = { .min = 96, .max = 140 },
  104. .m1 = { .min = 18, .max = 26 },
  105. .m2 = { .min = 6, .max = 16 },
  106. .p = { .min = 4, .max = 128 },
  107. .p1 = { .min = 2, .max = 33 },
  108. .p2 = { .dot_limit = 165000,
  109. .p2_slow = 4, .p2_fast = 2 },
  110. .find_pll = intel_find_best_PLL,
  111. };
  112. static const intel_limit_t intel_limits_i8xx_lvds = {
  113. .dot = { .min = 25000, .max = 350000 },
  114. .vco = { .min = 930000, .max = 1400000 },
  115. .n = { .min = 3, .max = 16 },
  116. .m = { .min = 96, .max = 140 },
  117. .m1 = { .min = 18, .max = 26 },
  118. .m2 = { .min = 6, .max = 16 },
  119. .p = { .min = 4, .max = 128 },
  120. .p1 = { .min = 1, .max = 6 },
  121. .p2 = { .dot_limit = 165000,
  122. .p2_slow = 14, .p2_fast = 7 },
  123. .find_pll = intel_find_best_PLL,
  124. };
  125. static const intel_limit_t intel_limits_i9xx_sdvo = {
  126. .dot = { .min = 20000, .max = 400000 },
  127. .vco = { .min = 1400000, .max = 2800000 },
  128. .n = { .min = 1, .max = 6 },
  129. .m = { .min = 70, .max = 120 },
  130. .m1 = { .min = 10, .max = 22 },
  131. .m2 = { .min = 5, .max = 9 },
  132. .p = { .min = 5, .max = 80 },
  133. .p1 = { .min = 1, .max = 8 },
  134. .p2 = { .dot_limit = 200000,
  135. .p2_slow = 10, .p2_fast = 5 },
  136. .find_pll = intel_find_best_PLL,
  137. };
  138. static const intel_limit_t intel_limits_i9xx_lvds = {
  139. .dot = { .min = 20000, .max = 400000 },
  140. .vco = { .min = 1400000, .max = 2800000 },
  141. .n = { .min = 1, .max = 6 },
  142. .m = { .min = 70, .max = 120 },
  143. .m1 = { .min = 10, .max = 22 },
  144. .m2 = { .min = 5, .max = 9 },
  145. .p = { .min = 7, .max = 98 },
  146. .p1 = { .min = 1, .max = 8 },
  147. .p2 = { .dot_limit = 112000,
  148. .p2_slow = 14, .p2_fast = 7 },
  149. .find_pll = intel_find_best_PLL,
  150. };
  151. static const intel_limit_t intel_limits_g4x_sdvo = {
  152. .dot = { .min = 25000, .max = 270000 },
  153. .vco = { .min = 1750000, .max = 3500000},
  154. .n = { .min = 1, .max = 4 },
  155. .m = { .min = 104, .max = 138 },
  156. .m1 = { .min = 17, .max = 23 },
  157. .m2 = { .min = 5, .max = 11 },
  158. .p = { .min = 10, .max = 30 },
  159. .p1 = { .min = 1, .max = 3},
  160. .p2 = { .dot_limit = 270000,
  161. .p2_slow = 10,
  162. .p2_fast = 10
  163. },
  164. .find_pll = intel_g4x_find_best_PLL,
  165. };
  166. static const intel_limit_t intel_limits_g4x_hdmi = {
  167. .dot = { .min = 22000, .max = 400000 },
  168. .vco = { .min = 1750000, .max = 3500000},
  169. .n = { .min = 1, .max = 4 },
  170. .m = { .min = 104, .max = 138 },
  171. .m1 = { .min = 16, .max = 23 },
  172. .m2 = { .min = 5, .max = 11 },
  173. .p = { .min = 5, .max = 80 },
  174. .p1 = { .min = 1, .max = 8},
  175. .p2 = { .dot_limit = 165000,
  176. .p2_slow = 10, .p2_fast = 5 },
  177. .find_pll = intel_g4x_find_best_PLL,
  178. };
  179. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  180. .dot = { .min = 20000, .max = 115000 },
  181. .vco = { .min = 1750000, .max = 3500000 },
  182. .n = { .min = 1, .max = 3 },
  183. .m = { .min = 104, .max = 138 },
  184. .m1 = { .min = 17, .max = 23 },
  185. .m2 = { .min = 5, .max = 11 },
  186. .p = { .min = 28, .max = 112 },
  187. .p1 = { .min = 2, .max = 8 },
  188. .p2 = { .dot_limit = 0,
  189. .p2_slow = 14, .p2_fast = 14
  190. },
  191. .find_pll = intel_g4x_find_best_PLL,
  192. };
  193. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  194. .dot = { .min = 80000, .max = 224000 },
  195. .vco = { .min = 1750000, .max = 3500000 },
  196. .n = { .min = 1, .max = 3 },
  197. .m = { .min = 104, .max = 138 },
  198. .m1 = { .min = 17, .max = 23 },
  199. .m2 = { .min = 5, .max = 11 },
  200. .p = { .min = 14, .max = 42 },
  201. .p1 = { .min = 2, .max = 6 },
  202. .p2 = { .dot_limit = 0,
  203. .p2_slow = 7, .p2_fast = 7
  204. },
  205. .find_pll = intel_g4x_find_best_PLL,
  206. };
  207. static const intel_limit_t intel_limits_g4x_display_port = {
  208. .dot = { .min = 161670, .max = 227000 },
  209. .vco = { .min = 1750000, .max = 3500000},
  210. .n = { .min = 1, .max = 2 },
  211. .m = { .min = 97, .max = 108 },
  212. .m1 = { .min = 0x10, .max = 0x12 },
  213. .m2 = { .min = 0x05, .max = 0x06 },
  214. .p = { .min = 10, .max = 20 },
  215. .p1 = { .min = 1, .max = 2},
  216. .p2 = { .dot_limit = 0,
  217. .p2_slow = 10, .p2_fast = 10 },
  218. .find_pll = intel_find_pll_g4x_dp,
  219. };
  220. static const intel_limit_t intel_limits_pineview_sdvo = {
  221. .dot = { .min = 20000, .max = 400000},
  222. .vco = { .min = 1700000, .max = 3500000 },
  223. /* Pineview's Ncounter is a ring counter */
  224. .n = { .min = 3, .max = 6 },
  225. .m = { .min = 2, .max = 256 },
  226. /* Pineview only has one combined m divider, which we treat as m2. */
  227. .m1 = { .min = 0, .max = 0 },
  228. .m2 = { .min = 0, .max = 254 },
  229. .p = { .min = 5, .max = 80 },
  230. .p1 = { .min = 1, .max = 8 },
  231. .p2 = { .dot_limit = 200000,
  232. .p2_slow = 10, .p2_fast = 5 },
  233. .find_pll = intel_find_best_PLL,
  234. };
  235. static const intel_limit_t intel_limits_pineview_lvds = {
  236. .dot = { .min = 20000, .max = 400000 },
  237. .vco = { .min = 1700000, .max = 3500000 },
  238. .n = { .min = 3, .max = 6 },
  239. .m = { .min = 2, .max = 256 },
  240. .m1 = { .min = 0, .max = 0 },
  241. .m2 = { .min = 0, .max = 254 },
  242. .p = { .min = 7, .max = 112 },
  243. .p1 = { .min = 1, .max = 8 },
  244. .p2 = { .dot_limit = 112000,
  245. .p2_slow = 14, .p2_fast = 14 },
  246. .find_pll = intel_find_best_PLL,
  247. };
  248. /* Ironlake / Sandybridge
  249. *
  250. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  251. * the range value for them is (actual_value - 2).
  252. */
  253. static const intel_limit_t intel_limits_ironlake_dac = {
  254. .dot = { .min = 25000, .max = 350000 },
  255. .vco = { .min = 1760000, .max = 3510000 },
  256. .n = { .min = 1, .max = 5 },
  257. .m = { .min = 79, .max = 127 },
  258. .m1 = { .min = 12, .max = 22 },
  259. .m2 = { .min = 5, .max = 9 },
  260. .p = { .min = 5, .max = 80 },
  261. .p1 = { .min = 1, .max = 8 },
  262. .p2 = { .dot_limit = 225000,
  263. .p2_slow = 10, .p2_fast = 5 },
  264. .find_pll = intel_g4x_find_best_PLL,
  265. };
  266. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  267. .dot = { .min = 25000, .max = 350000 },
  268. .vco = { .min = 1760000, .max = 3510000 },
  269. .n = { .min = 1, .max = 3 },
  270. .m = { .min = 79, .max = 118 },
  271. .m1 = { .min = 12, .max = 22 },
  272. .m2 = { .min = 5, .max = 9 },
  273. .p = { .min = 28, .max = 112 },
  274. .p1 = { .min = 2, .max = 8 },
  275. .p2 = { .dot_limit = 225000,
  276. .p2_slow = 14, .p2_fast = 14 },
  277. .find_pll = intel_g4x_find_best_PLL,
  278. };
  279. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  280. .dot = { .min = 25000, .max = 350000 },
  281. .vco = { .min = 1760000, .max = 3510000 },
  282. .n = { .min = 1, .max = 3 },
  283. .m = { .min = 79, .max = 127 },
  284. .m1 = { .min = 12, .max = 22 },
  285. .m2 = { .min = 5, .max = 9 },
  286. .p = { .min = 14, .max = 56 },
  287. .p1 = { .min = 2, .max = 8 },
  288. .p2 = { .dot_limit = 225000,
  289. .p2_slow = 7, .p2_fast = 7 },
  290. .find_pll = intel_g4x_find_best_PLL,
  291. };
  292. /* LVDS 100mhz refclk limits. */
  293. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  294. .dot = { .min = 25000, .max = 350000 },
  295. .vco = { .min = 1760000, .max = 3510000 },
  296. .n = { .min = 1, .max = 2 },
  297. .m = { .min = 79, .max = 126 },
  298. .m1 = { .min = 12, .max = 22 },
  299. .m2 = { .min = 5, .max = 9 },
  300. .p = { .min = 28, .max = 112 },
  301. .p1 = { .min = 2, .max = 8 },
  302. .p2 = { .dot_limit = 225000,
  303. .p2_slow = 14, .p2_fast = 14 },
  304. .find_pll = intel_g4x_find_best_PLL,
  305. };
  306. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  307. .dot = { .min = 25000, .max = 350000 },
  308. .vco = { .min = 1760000, .max = 3510000 },
  309. .n = { .min = 1, .max = 3 },
  310. .m = { .min = 79, .max = 126 },
  311. .m1 = { .min = 12, .max = 22 },
  312. .m2 = { .min = 5, .max = 9 },
  313. .p = { .min = 14, .max = 42 },
  314. .p1 = { .min = 2, .max = 6 },
  315. .p2 = { .dot_limit = 225000,
  316. .p2_slow = 7, .p2_fast = 7 },
  317. .find_pll = intel_g4x_find_best_PLL,
  318. };
  319. static const intel_limit_t intel_limits_ironlake_display_port = {
  320. .dot = { .min = 25000, .max = 350000 },
  321. .vco = { .min = 1760000, .max = 3510000},
  322. .n = { .min = 1, .max = 2 },
  323. .m = { .min = 81, .max = 90 },
  324. .m1 = { .min = 12, .max = 22 },
  325. .m2 = { .min = 5, .max = 9 },
  326. .p = { .min = 10, .max = 20 },
  327. .p1 = { .min = 1, .max = 2},
  328. .p2 = { .dot_limit = 0,
  329. .p2_slow = 10, .p2_fast = 10 },
  330. .find_pll = intel_find_pll_ironlake_dp,
  331. };
  332. u32 intel_dpio_read(struct drm_i915_private *dev_priv, int reg)
  333. {
  334. unsigned long flags;
  335. u32 val = 0;
  336. spin_lock_irqsave(&dev_priv->dpio_lock, flags);
  337. if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
  338. DRM_ERROR("DPIO idle wait timed out\n");
  339. goto out_unlock;
  340. }
  341. I915_WRITE(DPIO_REG, reg);
  342. I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_READ | DPIO_PORTID |
  343. DPIO_BYTE);
  344. if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
  345. DRM_ERROR("DPIO read wait timed out\n");
  346. goto out_unlock;
  347. }
  348. val = I915_READ(DPIO_DATA);
  349. out_unlock:
  350. spin_unlock_irqrestore(&dev_priv->dpio_lock, flags);
  351. return val;
  352. }
  353. static void intel_dpio_write(struct drm_i915_private *dev_priv, int reg,
  354. u32 val)
  355. {
  356. unsigned long flags;
  357. spin_lock_irqsave(&dev_priv->dpio_lock, flags);
  358. if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
  359. DRM_ERROR("DPIO idle wait timed out\n");
  360. goto out_unlock;
  361. }
  362. I915_WRITE(DPIO_DATA, val);
  363. I915_WRITE(DPIO_REG, reg);
  364. I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_WRITE | DPIO_PORTID |
  365. DPIO_BYTE);
  366. if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100))
  367. DRM_ERROR("DPIO write wait timed out\n");
  368. out_unlock:
  369. spin_unlock_irqrestore(&dev_priv->dpio_lock, flags);
  370. }
  371. static void vlv_init_dpio(struct drm_device *dev)
  372. {
  373. struct drm_i915_private *dev_priv = dev->dev_private;
  374. /* Reset the DPIO config */
  375. I915_WRITE(DPIO_CTL, 0);
  376. POSTING_READ(DPIO_CTL);
  377. I915_WRITE(DPIO_CTL, 1);
  378. POSTING_READ(DPIO_CTL);
  379. }
  380. static int intel_dual_link_lvds_callback(const struct dmi_system_id *id)
  381. {
  382. DRM_INFO("Forcing lvds to dual link mode on %s\n", id->ident);
  383. return 1;
  384. }
  385. static const struct dmi_system_id intel_dual_link_lvds[] = {
  386. {
  387. .callback = intel_dual_link_lvds_callback,
  388. .ident = "Apple MacBook Pro (Core i5/i7 Series)",
  389. .matches = {
  390. DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
  391. DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro8,2"),
  392. },
  393. },
  394. { } /* terminating entry */
  395. };
  396. static bool is_dual_link_lvds(struct drm_i915_private *dev_priv,
  397. unsigned int reg)
  398. {
  399. unsigned int val;
  400. /* use the module option value if specified */
  401. if (i915_lvds_channel_mode > 0)
  402. return i915_lvds_channel_mode == 2;
  403. if (dmi_check_system(intel_dual_link_lvds))
  404. return true;
  405. if (dev_priv->lvds_val)
  406. val = dev_priv->lvds_val;
  407. else {
  408. /* BIOS should set the proper LVDS register value at boot, but
  409. * in reality, it doesn't set the value when the lid is closed;
  410. * we need to check "the value to be set" in VBT when LVDS
  411. * register is uninitialized.
  412. */
  413. val = I915_READ(reg);
  414. if (!(val & ~LVDS_DETECTED))
  415. val = dev_priv->bios_lvds_val;
  416. dev_priv->lvds_val = val;
  417. }
  418. return (val & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP;
  419. }
  420. static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
  421. int refclk)
  422. {
  423. struct drm_device *dev = crtc->dev;
  424. struct drm_i915_private *dev_priv = dev->dev_private;
  425. const intel_limit_t *limit;
  426. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  427. if (is_dual_link_lvds(dev_priv, PCH_LVDS)) {
  428. /* LVDS dual channel */
  429. if (refclk == 100000)
  430. limit = &intel_limits_ironlake_dual_lvds_100m;
  431. else
  432. limit = &intel_limits_ironlake_dual_lvds;
  433. } else {
  434. if (refclk == 100000)
  435. limit = &intel_limits_ironlake_single_lvds_100m;
  436. else
  437. limit = &intel_limits_ironlake_single_lvds;
  438. }
  439. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  440. HAS_eDP)
  441. limit = &intel_limits_ironlake_display_port;
  442. else
  443. limit = &intel_limits_ironlake_dac;
  444. return limit;
  445. }
  446. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  447. {
  448. struct drm_device *dev = crtc->dev;
  449. struct drm_i915_private *dev_priv = dev->dev_private;
  450. const intel_limit_t *limit;
  451. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  452. if (is_dual_link_lvds(dev_priv, LVDS))
  453. /* LVDS with dual channel */
  454. limit = &intel_limits_g4x_dual_channel_lvds;
  455. else
  456. /* LVDS with dual channel */
  457. limit = &intel_limits_g4x_single_channel_lvds;
  458. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  459. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  460. limit = &intel_limits_g4x_hdmi;
  461. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  462. limit = &intel_limits_g4x_sdvo;
  463. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  464. limit = &intel_limits_g4x_display_port;
  465. } else /* The option is for other outputs */
  466. limit = &intel_limits_i9xx_sdvo;
  467. return limit;
  468. }
  469. static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
  470. {
  471. struct drm_device *dev = crtc->dev;
  472. const intel_limit_t *limit;
  473. if (HAS_PCH_SPLIT(dev))
  474. limit = intel_ironlake_limit(crtc, refclk);
  475. else if (IS_G4X(dev)) {
  476. limit = intel_g4x_limit(crtc);
  477. } else if (IS_PINEVIEW(dev)) {
  478. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  479. limit = &intel_limits_pineview_lvds;
  480. else
  481. limit = &intel_limits_pineview_sdvo;
  482. } else if (!IS_GEN2(dev)) {
  483. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  484. limit = &intel_limits_i9xx_lvds;
  485. else
  486. limit = &intel_limits_i9xx_sdvo;
  487. } else {
  488. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  489. limit = &intel_limits_i8xx_lvds;
  490. else
  491. limit = &intel_limits_i8xx_dvo;
  492. }
  493. return limit;
  494. }
  495. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  496. static void pineview_clock(int refclk, intel_clock_t *clock)
  497. {
  498. clock->m = clock->m2 + 2;
  499. clock->p = clock->p1 * clock->p2;
  500. clock->vco = refclk * clock->m / clock->n;
  501. clock->dot = clock->vco / clock->p;
  502. }
  503. static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
  504. {
  505. if (IS_PINEVIEW(dev)) {
  506. pineview_clock(refclk, clock);
  507. return;
  508. }
  509. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  510. clock->p = clock->p1 * clock->p2;
  511. clock->vco = refclk * clock->m / (clock->n + 2);
  512. clock->dot = clock->vco / clock->p;
  513. }
  514. /**
  515. * Returns whether any output on the specified pipe is of the specified type
  516. */
  517. bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
  518. {
  519. struct drm_device *dev = crtc->dev;
  520. struct drm_mode_config *mode_config = &dev->mode_config;
  521. struct intel_encoder *encoder;
  522. list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
  523. if (encoder->base.crtc == crtc && encoder->type == type)
  524. return true;
  525. return false;
  526. }
  527. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  528. /**
  529. * Returns whether the given set of divisors are valid for a given refclk with
  530. * the given connectors.
  531. */
  532. static bool intel_PLL_is_valid(struct drm_device *dev,
  533. const intel_limit_t *limit,
  534. const intel_clock_t *clock)
  535. {
  536. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  537. INTELPllInvalid("p1 out of range\n");
  538. if (clock->p < limit->p.min || limit->p.max < clock->p)
  539. INTELPllInvalid("p out of range\n");
  540. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  541. INTELPllInvalid("m2 out of range\n");
  542. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  543. INTELPllInvalid("m1 out of range\n");
  544. if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
  545. INTELPllInvalid("m1 <= m2\n");
  546. if (clock->m < limit->m.min || limit->m.max < clock->m)
  547. INTELPllInvalid("m out of range\n");
  548. if (clock->n < limit->n.min || limit->n.max < clock->n)
  549. INTELPllInvalid("n out of range\n");
  550. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  551. INTELPllInvalid("vco out of range\n");
  552. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  553. * connector, etc., rather than just a single range.
  554. */
  555. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  556. INTELPllInvalid("dot out of range\n");
  557. return true;
  558. }
  559. static bool
  560. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  561. int target, int refclk, intel_clock_t *match_clock,
  562. intel_clock_t *best_clock)
  563. {
  564. struct drm_device *dev = crtc->dev;
  565. struct drm_i915_private *dev_priv = dev->dev_private;
  566. intel_clock_t clock;
  567. int err = target;
  568. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  569. (I915_READ(LVDS)) != 0) {
  570. /*
  571. * For LVDS, if the panel is on, just rely on its current
  572. * settings for dual-channel. We haven't figured out how to
  573. * reliably set up different single/dual channel state, if we
  574. * even can.
  575. */
  576. if (is_dual_link_lvds(dev_priv, LVDS))
  577. clock.p2 = limit->p2.p2_fast;
  578. else
  579. clock.p2 = limit->p2.p2_slow;
  580. } else {
  581. if (target < limit->p2.dot_limit)
  582. clock.p2 = limit->p2.p2_slow;
  583. else
  584. clock.p2 = limit->p2.p2_fast;
  585. }
  586. memset(best_clock, 0, sizeof(*best_clock));
  587. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  588. clock.m1++) {
  589. for (clock.m2 = limit->m2.min;
  590. clock.m2 <= limit->m2.max; clock.m2++) {
  591. /* m1 is always 0 in Pineview */
  592. if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
  593. break;
  594. for (clock.n = limit->n.min;
  595. clock.n <= limit->n.max; clock.n++) {
  596. for (clock.p1 = limit->p1.min;
  597. clock.p1 <= limit->p1.max; clock.p1++) {
  598. int this_err;
  599. intel_clock(dev, refclk, &clock);
  600. if (!intel_PLL_is_valid(dev, limit,
  601. &clock))
  602. continue;
  603. if (match_clock &&
  604. clock.p != match_clock->p)
  605. continue;
  606. this_err = abs(clock.dot - target);
  607. if (this_err < err) {
  608. *best_clock = clock;
  609. err = this_err;
  610. }
  611. }
  612. }
  613. }
  614. }
  615. return (err != target);
  616. }
  617. static bool
  618. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  619. int target, int refclk, intel_clock_t *match_clock,
  620. intel_clock_t *best_clock)
  621. {
  622. struct drm_device *dev = crtc->dev;
  623. struct drm_i915_private *dev_priv = dev->dev_private;
  624. intel_clock_t clock;
  625. int max_n;
  626. bool found;
  627. /* approximately equals target * 0.00585 */
  628. int err_most = (target >> 8) + (target >> 9);
  629. found = false;
  630. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  631. int lvds_reg;
  632. if (HAS_PCH_SPLIT(dev))
  633. lvds_reg = PCH_LVDS;
  634. else
  635. lvds_reg = LVDS;
  636. if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==
  637. LVDS_CLKB_POWER_UP)
  638. clock.p2 = limit->p2.p2_fast;
  639. else
  640. clock.p2 = limit->p2.p2_slow;
  641. } else {
  642. if (target < limit->p2.dot_limit)
  643. clock.p2 = limit->p2.p2_slow;
  644. else
  645. clock.p2 = limit->p2.p2_fast;
  646. }
  647. memset(best_clock, 0, sizeof(*best_clock));
  648. max_n = limit->n.max;
  649. /* based on hardware requirement, prefer smaller n to precision */
  650. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  651. /* based on hardware requirement, prefere larger m1,m2 */
  652. for (clock.m1 = limit->m1.max;
  653. clock.m1 >= limit->m1.min; clock.m1--) {
  654. for (clock.m2 = limit->m2.max;
  655. clock.m2 >= limit->m2.min; clock.m2--) {
  656. for (clock.p1 = limit->p1.max;
  657. clock.p1 >= limit->p1.min; clock.p1--) {
  658. int this_err;
  659. intel_clock(dev, refclk, &clock);
  660. if (!intel_PLL_is_valid(dev, limit,
  661. &clock))
  662. continue;
  663. if (match_clock &&
  664. clock.p != match_clock->p)
  665. continue;
  666. this_err = abs(clock.dot - target);
  667. if (this_err < err_most) {
  668. *best_clock = clock;
  669. err_most = this_err;
  670. max_n = clock.n;
  671. found = true;
  672. }
  673. }
  674. }
  675. }
  676. }
  677. return found;
  678. }
  679. static bool
  680. intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  681. int target, int refclk, intel_clock_t *match_clock,
  682. intel_clock_t *best_clock)
  683. {
  684. struct drm_device *dev = crtc->dev;
  685. intel_clock_t clock;
  686. if (target < 200000) {
  687. clock.n = 1;
  688. clock.p1 = 2;
  689. clock.p2 = 10;
  690. clock.m1 = 12;
  691. clock.m2 = 9;
  692. } else {
  693. clock.n = 2;
  694. clock.p1 = 1;
  695. clock.p2 = 10;
  696. clock.m1 = 14;
  697. clock.m2 = 8;
  698. }
  699. intel_clock(dev, refclk, &clock);
  700. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  701. return true;
  702. }
  703. /* DisplayPort has only two frequencies, 162MHz and 270MHz */
  704. static bool
  705. intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  706. int target, int refclk, intel_clock_t *match_clock,
  707. intel_clock_t *best_clock)
  708. {
  709. intel_clock_t clock;
  710. if (target < 200000) {
  711. clock.p1 = 2;
  712. clock.p2 = 10;
  713. clock.n = 2;
  714. clock.m1 = 23;
  715. clock.m2 = 8;
  716. } else {
  717. clock.p1 = 1;
  718. clock.p2 = 10;
  719. clock.n = 1;
  720. clock.m1 = 14;
  721. clock.m2 = 2;
  722. }
  723. clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
  724. clock.p = (clock.p1 * clock.p2);
  725. clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
  726. clock.vco = 0;
  727. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  728. return true;
  729. }
  730. /**
  731. * intel_wait_for_vblank - wait for vblank on a given pipe
  732. * @dev: drm device
  733. * @pipe: pipe to wait for
  734. *
  735. * Wait for vblank to occur on a given pipe. Needed for various bits of
  736. * mode setting code.
  737. */
  738. void intel_wait_for_vblank(struct drm_device *dev, int pipe)
  739. {
  740. struct drm_i915_private *dev_priv = dev->dev_private;
  741. int pipestat_reg = PIPESTAT(pipe);
  742. /* Clear existing vblank status. Note this will clear any other
  743. * sticky status fields as well.
  744. *
  745. * This races with i915_driver_irq_handler() with the result
  746. * that either function could miss a vblank event. Here it is not
  747. * fatal, as we will either wait upon the next vblank interrupt or
  748. * timeout. Generally speaking intel_wait_for_vblank() is only
  749. * called during modeset at which time the GPU should be idle and
  750. * should *not* be performing page flips and thus not waiting on
  751. * vblanks...
  752. * Currently, the result of us stealing a vblank from the irq
  753. * handler is that a single frame will be skipped during swapbuffers.
  754. */
  755. I915_WRITE(pipestat_reg,
  756. I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
  757. /* Wait for vblank interrupt bit to set */
  758. if (wait_for(I915_READ(pipestat_reg) &
  759. PIPE_VBLANK_INTERRUPT_STATUS,
  760. 50))
  761. DRM_DEBUG_KMS("vblank wait timed out\n");
  762. }
  763. /*
  764. * intel_wait_for_pipe_off - wait for pipe to turn off
  765. * @dev: drm device
  766. * @pipe: pipe to wait for
  767. *
  768. * After disabling a pipe, we can't wait for vblank in the usual way,
  769. * spinning on the vblank interrupt status bit, since we won't actually
  770. * see an interrupt when the pipe is disabled.
  771. *
  772. * On Gen4 and above:
  773. * wait for the pipe register state bit to turn off
  774. *
  775. * Otherwise:
  776. * wait for the display line value to settle (it usually
  777. * ends up stopping at the start of the next frame).
  778. *
  779. */
  780. void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
  781. {
  782. struct drm_i915_private *dev_priv = dev->dev_private;
  783. if (INTEL_INFO(dev)->gen >= 4) {
  784. int reg = PIPECONF(pipe);
  785. /* Wait for the Pipe State to go off */
  786. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  787. 100))
  788. DRM_DEBUG_KMS("pipe_off wait timed out\n");
  789. } else {
  790. u32 last_line;
  791. int reg = PIPEDSL(pipe);
  792. unsigned long timeout = jiffies + msecs_to_jiffies(100);
  793. /* Wait for the display line to settle */
  794. do {
  795. last_line = I915_READ(reg) & DSL_LINEMASK;
  796. mdelay(5);
  797. } while (((I915_READ(reg) & DSL_LINEMASK) != last_line) &&
  798. time_after(timeout, jiffies));
  799. if (time_after(jiffies, timeout))
  800. DRM_DEBUG_KMS("pipe_off wait timed out\n");
  801. }
  802. }
  803. static const char *state_string(bool enabled)
  804. {
  805. return enabled ? "on" : "off";
  806. }
  807. /* Only for pre-ILK configs */
  808. static void assert_pll(struct drm_i915_private *dev_priv,
  809. enum pipe pipe, bool state)
  810. {
  811. int reg;
  812. u32 val;
  813. bool cur_state;
  814. reg = DPLL(pipe);
  815. val = I915_READ(reg);
  816. cur_state = !!(val & DPLL_VCO_ENABLE);
  817. WARN(cur_state != state,
  818. "PLL state assertion failure (expected %s, current %s)\n",
  819. state_string(state), state_string(cur_state));
  820. }
  821. #define assert_pll_enabled(d, p) assert_pll(d, p, true)
  822. #define assert_pll_disabled(d, p) assert_pll(d, p, false)
  823. /* For ILK+ */
  824. static void assert_pch_pll(struct drm_i915_private *dev_priv,
  825. struct intel_crtc *intel_crtc, bool state)
  826. {
  827. int reg;
  828. u32 val;
  829. bool cur_state;
  830. if (!intel_crtc->pch_pll) {
  831. WARN(1, "asserting PCH PLL enabled with no PLL\n");
  832. return;
  833. }
  834. if (HAS_PCH_CPT(dev_priv->dev)) {
  835. u32 pch_dpll;
  836. pch_dpll = I915_READ(PCH_DPLL_SEL);
  837. /* Make sure the selected PLL is enabled to the transcoder */
  838. WARN(!((pch_dpll >> (4 * intel_crtc->pipe)) & 8),
  839. "transcoder %d PLL not enabled\n", intel_crtc->pipe);
  840. }
  841. reg = intel_crtc->pch_pll->pll_reg;
  842. val = I915_READ(reg);
  843. cur_state = !!(val & DPLL_VCO_ENABLE);
  844. WARN(cur_state != state,
  845. "PCH PLL state assertion failure (expected %s, current %s)\n",
  846. state_string(state), state_string(cur_state));
  847. }
  848. #define assert_pch_pll_enabled(d, p) assert_pch_pll(d, p, true)
  849. #define assert_pch_pll_disabled(d, p) assert_pch_pll(d, p, false)
  850. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  851. enum pipe pipe, bool state)
  852. {
  853. int reg;
  854. u32 val;
  855. bool cur_state;
  856. reg = FDI_TX_CTL(pipe);
  857. val = I915_READ(reg);
  858. cur_state = !!(val & FDI_TX_ENABLE);
  859. WARN(cur_state != state,
  860. "FDI TX state assertion failure (expected %s, current %s)\n",
  861. state_string(state), state_string(cur_state));
  862. }
  863. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  864. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  865. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  866. enum pipe pipe, bool state)
  867. {
  868. int reg;
  869. u32 val;
  870. bool cur_state;
  871. reg = FDI_RX_CTL(pipe);
  872. val = I915_READ(reg);
  873. cur_state = !!(val & FDI_RX_ENABLE);
  874. WARN(cur_state != state,
  875. "FDI RX state assertion failure (expected %s, current %s)\n",
  876. state_string(state), state_string(cur_state));
  877. }
  878. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  879. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  880. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  881. enum pipe pipe)
  882. {
  883. int reg;
  884. u32 val;
  885. /* ILK FDI PLL is always enabled */
  886. if (dev_priv->info->gen == 5)
  887. return;
  888. reg = FDI_TX_CTL(pipe);
  889. val = I915_READ(reg);
  890. WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  891. }
  892. static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
  893. enum pipe pipe)
  894. {
  895. int reg;
  896. u32 val;
  897. reg = FDI_RX_CTL(pipe);
  898. val = I915_READ(reg);
  899. WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
  900. }
  901. static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  902. enum pipe pipe)
  903. {
  904. int pp_reg, lvds_reg;
  905. u32 val;
  906. enum pipe panel_pipe = PIPE_A;
  907. bool locked = true;
  908. if (HAS_PCH_SPLIT(dev_priv->dev)) {
  909. pp_reg = PCH_PP_CONTROL;
  910. lvds_reg = PCH_LVDS;
  911. } else {
  912. pp_reg = PP_CONTROL;
  913. lvds_reg = LVDS;
  914. }
  915. val = I915_READ(pp_reg);
  916. if (!(val & PANEL_POWER_ON) ||
  917. ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
  918. locked = false;
  919. if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
  920. panel_pipe = PIPE_B;
  921. WARN(panel_pipe == pipe && locked,
  922. "panel assertion failure, pipe %c regs locked\n",
  923. pipe_name(pipe));
  924. }
  925. void assert_pipe(struct drm_i915_private *dev_priv,
  926. enum pipe pipe, bool state)
  927. {
  928. int reg;
  929. u32 val;
  930. bool cur_state;
  931. /* if we need the pipe A quirk it must be always on */
  932. if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
  933. state = true;
  934. reg = PIPECONF(pipe);
  935. val = I915_READ(reg);
  936. cur_state = !!(val & PIPECONF_ENABLE);
  937. WARN(cur_state != state,
  938. "pipe %c assertion failure (expected %s, current %s)\n",
  939. pipe_name(pipe), state_string(state), state_string(cur_state));
  940. }
  941. static void assert_plane(struct drm_i915_private *dev_priv,
  942. enum plane plane, bool state)
  943. {
  944. int reg;
  945. u32 val;
  946. bool cur_state;
  947. reg = DSPCNTR(plane);
  948. val = I915_READ(reg);
  949. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  950. WARN(cur_state != state,
  951. "plane %c assertion failure (expected %s, current %s)\n",
  952. plane_name(plane), state_string(state), state_string(cur_state));
  953. }
  954. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  955. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  956. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  957. enum pipe pipe)
  958. {
  959. int reg, i;
  960. u32 val;
  961. int cur_pipe;
  962. /* Planes are fixed to pipes on ILK+ */
  963. if (HAS_PCH_SPLIT(dev_priv->dev)) {
  964. reg = DSPCNTR(pipe);
  965. val = I915_READ(reg);
  966. WARN((val & DISPLAY_PLANE_ENABLE),
  967. "plane %c assertion failure, should be disabled but not\n",
  968. plane_name(pipe));
  969. return;
  970. }
  971. /* Need to check both planes against the pipe */
  972. for (i = 0; i < 2; i++) {
  973. reg = DSPCNTR(i);
  974. val = I915_READ(reg);
  975. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  976. DISPPLANE_SEL_PIPE_SHIFT;
  977. WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  978. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  979. plane_name(i), pipe_name(pipe));
  980. }
  981. }
  982. static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  983. {
  984. u32 val;
  985. bool enabled;
  986. val = I915_READ(PCH_DREF_CONTROL);
  987. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  988. DREF_SUPERSPREAD_SOURCE_MASK));
  989. WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  990. }
  991. static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
  992. enum pipe pipe)
  993. {
  994. int reg;
  995. u32 val;
  996. bool enabled;
  997. reg = TRANSCONF(pipe);
  998. val = I915_READ(reg);
  999. enabled = !!(val & TRANS_ENABLE);
  1000. WARN(enabled,
  1001. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1002. pipe_name(pipe));
  1003. }
  1004. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1005. enum pipe pipe, u32 port_sel, u32 val)
  1006. {
  1007. if ((val & DP_PORT_EN) == 0)
  1008. return false;
  1009. if (HAS_PCH_CPT(dev_priv->dev)) {
  1010. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1011. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1012. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1013. return false;
  1014. } else {
  1015. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1016. return false;
  1017. }
  1018. return true;
  1019. }
  1020. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1021. enum pipe pipe, u32 val)
  1022. {
  1023. if ((val & PORT_ENABLE) == 0)
  1024. return false;
  1025. if (HAS_PCH_CPT(dev_priv->dev)) {
  1026. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1027. return false;
  1028. } else {
  1029. if ((val & TRANSCODER_MASK) != TRANSCODER(pipe))
  1030. return false;
  1031. }
  1032. return true;
  1033. }
  1034. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1035. enum pipe pipe, u32 val)
  1036. {
  1037. if ((val & LVDS_PORT_EN) == 0)
  1038. return false;
  1039. if (HAS_PCH_CPT(dev_priv->dev)) {
  1040. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1041. return false;
  1042. } else {
  1043. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1044. return false;
  1045. }
  1046. return true;
  1047. }
  1048. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1049. enum pipe pipe, u32 val)
  1050. {
  1051. if ((val & ADPA_DAC_ENABLE) == 0)
  1052. return false;
  1053. if (HAS_PCH_CPT(dev_priv->dev)) {
  1054. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1055. return false;
  1056. } else {
  1057. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1058. return false;
  1059. }
  1060. return true;
  1061. }
  1062. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1063. enum pipe pipe, int reg, u32 port_sel)
  1064. {
  1065. u32 val = I915_READ(reg);
  1066. WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1067. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1068. reg, pipe_name(pipe));
  1069. }
  1070. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1071. enum pipe pipe, int reg)
  1072. {
  1073. u32 val = I915_READ(reg);
  1074. WARN(hdmi_pipe_enabled(dev_priv, val, pipe),
  1075. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1076. reg, pipe_name(pipe));
  1077. }
  1078. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1079. enum pipe pipe)
  1080. {
  1081. int reg;
  1082. u32 val;
  1083. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1084. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1085. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1086. reg = PCH_ADPA;
  1087. val = I915_READ(reg);
  1088. WARN(adpa_pipe_enabled(dev_priv, val, pipe),
  1089. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1090. pipe_name(pipe));
  1091. reg = PCH_LVDS;
  1092. val = I915_READ(reg);
  1093. WARN(lvds_pipe_enabled(dev_priv, val, pipe),
  1094. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1095. pipe_name(pipe));
  1096. assert_pch_hdmi_disabled(dev_priv, pipe, HDMIB);
  1097. assert_pch_hdmi_disabled(dev_priv, pipe, HDMIC);
  1098. assert_pch_hdmi_disabled(dev_priv, pipe, HDMID);
  1099. }
  1100. /**
  1101. * intel_enable_pll - enable a PLL
  1102. * @dev_priv: i915 private structure
  1103. * @pipe: pipe PLL to enable
  1104. *
  1105. * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
  1106. * make sure the PLL reg is writable first though, since the panel write
  1107. * protect mechanism may be enabled.
  1108. *
  1109. * Note! This is for pre-ILK only.
  1110. */
  1111. static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1112. {
  1113. int reg;
  1114. u32 val;
  1115. /* No really, not for ILK+ */
  1116. BUG_ON(dev_priv->info->gen >= 5);
  1117. /* PLL is protected by panel, make sure we can write it */
  1118. if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
  1119. assert_panel_unlocked(dev_priv, pipe);
  1120. reg = DPLL(pipe);
  1121. val = I915_READ(reg);
  1122. val |= DPLL_VCO_ENABLE;
  1123. /* We do this three times for luck */
  1124. I915_WRITE(reg, val);
  1125. POSTING_READ(reg);
  1126. udelay(150); /* wait for warmup */
  1127. I915_WRITE(reg, val);
  1128. POSTING_READ(reg);
  1129. udelay(150); /* wait for warmup */
  1130. I915_WRITE(reg, val);
  1131. POSTING_READ(reg);
  1132. udelay(150); /* wait for warmup */
  1133. }
  1134. /**
  1135. * intel_disable_pll - disable a PLL
  1136. * @dev_priv: i915 private structure
  1137. * @pipe: pipe PLL to disable
  1138. *
  1139. * Disable the PLL for @pipe, making sure the pipe is off first.
  1140. *
  1141. * Note! This is for pre-ILK only.
  1142. */
  1143. static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1144. {
  1145. int reg;
  1146. u32 val;
  1147. /* Don't disable pipe A or pipe A PLLs if needed */
  1148. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1149. return;
  1150. /* Make sure the pipe isn't still relying on us */
  1151. assert_pipe_disabled(dev_priv, pipe);
  1152. reg = DPLL(pipe);
  1153. val = I915_READ(reg);
  1154. val &= ~DPLL_VCO_ENABLE;
  1155. I915_WRITE(reg, val);
  1156. POSTING_READ(reg);
  1157. }
  1158. /**
  1159. * intel_enable_pch_pll - enable PCH PLL
  1160. * @dev_priv: i915 private structure
  1161. * @pipe: pipe PLL to enable
  1162. *
  1163. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1164. * drives the transcoder clock.
  1165. */
  1166. static void intel_enable_pch_pll(struct intel_crtc *intel_crtc)
  1167. {
  1168. struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
  1169. struct intel_pch_pll *pll = intel_crtc->pch_pll;
  1170. int reg;
  1171. u32 val;
  1172. /* PCH only available on ILK+ */
  1173. BUG_ON(dev_priv->info->gen < 5);
  1174. BUG_ON(pll == NULL);
  1175. BUG_ON(pll->refcount == 0);
  1176. DRM_DEBUG_KMS("enable PCH PLL %x (active %d, on? %d)for crtc %d\n",
  1177. pll->pll_reg, pll->active, pll->on,
  1178. intel_crtc->base.base.id);
  1179. /* PCH refclock must be enabled first */
  1180. assert_pch_refclk_enabled(dev_priv);
  1181. if (pll->active++ && pll->on) {
  1182. assert_pch_pll_enabled(dev_priv, intel_crtc);
  1183. return;
  1184. }
  1185. DRM_DEBUG_KMS("enabling PCH PLL %x\n", pll->pll_reg);
  1186. reg = pll->pll_reg;
  1187. val = I915_READ(reg);
  1188. val |= DPLL_VCO_ENABLE;
  1189. I915_WRITE(reg, val);
  1190. POSTING_READ(reg);
  1191. udelay(200);
  1192. pll->on = true;
  1193. }
  1194. static void intel_disable_pch_pll(struct intel_crtc *intel_crtc)
  1195. {
  1196. struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
  1197. struct intel_pch_pll *pll = intel_crtc->pch_pll;
  1198. int reg;
  1199. u32 val;
  1200. /* PCH only available on ILK+ */
  1201. BUG_ON(dev_priv->info->gen < 5);
  1202. if (pll == NULL)
  1203. return;
  1204. BUG_ON(pll->refcount == 0);
  1205. DRM_DEBUG_KMS("disable PCH PLL %x (active %d, on? %d) for crtc %d\n",
  1206. pll->pll_reg, pll->active, pll->on,
  1207. intel_crtc->base.base.id);
  1208. BUG_ON(pll->active == 0);
  1209. if (--pll->active) {
  1210. assert_pch_pll_enabled(dev_priv, intel_crtc);
  1211. return;
  1212. }
  1213. DRM_DEBUG_KMS("disabling PCH PLL %x\n", pll->pll_reg);
  1214. /* Make sure transcoder isn't still depending on us */
  1215. assert_transcoder_disabled(dev_priv, intel_crtc->pipe);
  1216. reg = pll->pll_reg;
  1217. val = I915_READ(reg);
  1218. val &= ~DPLL_VCO_ENABLE;
  1219. I915_WRITE(reg, val);
  1220. POSTING_READ(reg);
  1221. udelay(200);
  1222. pll->on = false;
  1223. }
  1224. static void intel_enable_transcoder(struct drm_i915_private *dev_priv,
  1225. enum pipe pipe)
  1226. {
  1227. int reg;
  1228. u32 val, pipeconf_val;
  1229. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1230. /* PCH only available on ILK+ */
  1231. BUG_ON(dev_priv->info->gen < 5);
  1232. /* Make sure PCH DPLL is enabled */
  1233. assert_pch_pll_enabled(dev_priv, to_intel_crtc(crtc));
  1234. /* FDI must be feeding us bits for PCH ports */
  1235. assert_fdi_tx_enabled(dev_priv, pipe);
  1236. assert_fdi_rx_enabled(dev_priv, pipe);
  1237. reg = TRANSCONF(pipe);
  1238. val = I915_READ(reg);
  1239. pipeconf_val = I915_READ(PIPECONF(pipe));
  1240. if (HAS_PCH_IBX(dev_priv->dev)) {
  1241. /*
  1242. * make the BPC in transcoder be consistent with
  1243. * that in pipeconf reg.
  1244. */
  1245. val &= ~PIPE_BPC_MASK;
  1246. val |= pipeconf_val & PIPE_BPC_MASK;
  1247. }
  1248. val &= ~TRANS_INTERLACE_MASK;
  1249. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1250. if (HAS_PCH_IBX(dev_priv->dev) &&
  1251. intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
  1252. val |= TRANS_LEGACY_INTERLACED_ILK;
  1253. else
  1254. val |= TRANS_INTERLACED;
  1255. else
  1256. val |= TRANS_PROGRESSIVE;
  1257. I915_WRITE(reg, val | TRANS_ENABLE);
  1258. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1259. DRM_ERROR("failed to enable transcoder %d\n", pipe);
  1260. }
  1261. static void intel_disable_transcoder(struct drm_i915_private *dev_priv,
  1262. enum pipe pipe)
  1263. {
  1264. int reg;
  1265. u32 val;
  1266. /* FDI relies on the transcoder */
  1267. assert_fdi_tx_disabled(dev_priv, pipe);
  1268. assert_fdi_rx_disabled(dev_priv, pipe);
  1269. /* Ports must be off as well */
  1270. assert_pch_ports_disabled(dev_priv, pipe);
  1271. reg = TRANSCONF(pipe);
  1272. val = I915_READ(reg);
  1273. val &= ~TRANS_ENABLE;
  1274. I915_WRITE(reg, val);
  1275. /* wait for PCH transcoder off, transcoder state */
  1276. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1277. DRM_ERROR("failed to disable transcoder %d\n", pipe);
  1278. }
  1279. /**
  1280. * intel_enable_pipe - enable a pipe, asserting requirements
  1281. * @dev_priv: i915 private structure
  1282. * @pipe: pipe to enable
  1283. * @pch_port: on ILK+, is this pipe driving a PCH port or not
  1284. *
  1285. * Enable @pipe, making sure that various hardware specific requirements
  1286. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1287. *
  1288. * @pipe should be %PIPE_A or %PIPE_B.
  1289. *
  1290. * Will wait until the pipe is actually running (i.e. first vblank) before
  1291. * returning.
  1292. */
  1293. static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
  1294. bool pch_port)
  1295. {
  1296. int reg;
  1297. u32 val;
  1298. /*
  1299. * A pipe without a PLL won't actually be able to drive bits from
  1300. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1301. * need the check.
  1302. */
  1303. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1304. assert_pll_enabled(dev_priv, pipe);
  1305. else {
  1306. if (pch_port) {
  1307. /* if driving the PCH, we need FDI enabled */
  1308. assert_fdi_rx_pll_enabled(dev_priv, pipe);
  1309. assert_fdi_tx_pll_enabled(dev_priv, pipe);
  1310. }
  1311. /* FIXME: assert CPU port conditions for SNB+ */
  1312. }
  1313. reg = PIPECONF(pipe);
  1314. val = I915_READ(reg);
  1315. if (val & PIPECONF_ENABLE)
  1316. return;
  1317. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1318. intel_wait_for_vblank(dev_priv->dev, pipe);
  1319. }
  1320. /**
  1321. * intel_disable_pipe - disable a pipe, asserting requirements
  1322. * @dev_priv: i915 private structure
  1323. * @pipe: pipe to disable
  1324. *
  1325. * Disable @pipe, making sure that various hardware specific requirements
  1326. * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
  1327. *
  1328. * @pipe should be %PIPE_A or %PIPE_B.
  1329. *
  1330. * Will wait until the pipe has shut down before returning.
  1331. */
  1332. static void intel_disable_pipe(struct drm_i915_private *dev_priv,
  1333. enum pipe pipe)
  1334. {
  1335. int reg;
  1336. u32 val;
  1337. /*
  1338. * Make sure planes won't keep trying to pump pixels to us,
  1339. * or we might hang the display.
  1340. */
  1341. assert_planes_disabled(dev_priv, pipe);
  1342. /* Don't disable pipe A or pipe A PLLs if needed */
  1343. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1344. return;
  1345. reg = PIPECONF(pipe);
  1346. val = I915_READ(reg);
  1347. if ((val & PIPECONF_ENABLE) == 0)
  1348. return;
  1349. I915_WRITE(reg, val & ~PIPECONF_ENABLE);
  1350. intel_wait_for_pipe_off(dev_priv->dev, pipe);
  1351. }
  1352. /*
  1353. * Plane regs are double buffered, going from enabled->disabled needs a
  1354. * trigger in order to latch. The display address reg provides this.
  1355. */
  1356. void intel_flush_display_plane(struct drm_i915_private *dev_priv,
  1357. enum plane plane)
  1358. {
  1359. I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
  1360. I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
  1361. }
  1362. /**
  1363. * intel_enable_plane - enable a display plane on a given pipe
  1364. * @dev_priv: i915 private structure
  1365. * @plane: plane to enable
  1366. * @pipe: pipe being fed
  1367. *
  1368. * Enable @plane on @pipe, making sure that @pipe is running first.
  1369. */
  1370. static void intel_enable_plane(struct drm_i915_private *dev_priv,
  1371. enum plane plane, enum pipe pipe)
  1372. {
  1373. int reg;
  1374. u32 val;
  1375. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1376. assert_pipe_enabled(dev_priv, pipe);
  1377. reg = DSPCNTR(plane);
  1378. val = I915_READ(reg);
  1379. if (val & DISPLAY_PLANE_ENABLE)
  1380. return;
  1381. I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
  1382. intel_flush_display_plane(dev_priv, plane);
  1383. intel_wait_for_vblank(dev_priv->dev, pipe);
  1384. }
  1385. /**
  1386. * intel_disable_plane - disable a display plane
  1387. * @dev_priv: i915 private structure
  1388. * @plane: plane to disable
  1389. * @pipe: pipe consuming the data
  1390. *
  1391. * Disable @plane; should be an independent operation.
  1392. */
  1393. static void intel_disable_plane(struct drm_i915_private *dev_priv,
  1394. enum plane plane, enum pipe pipe)
  1395. {
  1396. int reg;
  1397. u32 val;
  1398. reg = DSPCNTR(plane);
  1399. val = I915_READ(reg);
  1400. if ((val & DISPLAY_PLANE_ENABLE) == 0)
  1401. return;
  1402. I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
  1403. intel_flush_display_plane(dev_priv, plane);
  1404. intel_wait_for_vblank(dev_priv->dev, pipe);
  1405. }
  1406. static void disable_pch_dp(struct drm_i915_private *dev_priv,
  1407. enum pipe pipe, int reg, u32 port_sel)
  1408. {
  1409. u32 val = I915_READ(reg);
  1410. if (dp_pipe_enabled(dev_priv, pipe, port_sel, val)) {
  1411. DRM_DEBUG_KMS("Disabling pch dp %x on pipe %d\n", reg, pipe);
  1412. I915_WRITE(reg, val & ~DP_PORT_EN);
  1413. }
  1414. }
  1415. static void disable_pch_hdmi(struct drm_i915_private *dev_priv,
  1416. enum pipe pipe, int reg)
  1417. {
  1418. u32 val = I915_READ(reg);
  1419. if (hdmi_pipe_enabled(dev_priv, val, pipe)) {
  1420. DRM_DEBUG_KMS("Disabling pch HDMI %x on pipe %d\n",
  1421. reg, pipe);
  1422. I915_WRITE(reg, val & ~PORT_ENABLE);
  1423. }
  1424. }
  1425. /* Disable any ports connected to this transcoder */
  1426. static void intel_disable_pch_ports(struct drm_i915_private *dev_priv,
  1427. enum pipe pipe)
  1428. {
  1429. u32 reg, val;
  1430. val = I915_READ(PCH_PP_CONTROL);
  1431. I915_WRITE(PCH_PP_CONTROL, val | PANEL_UNLOCK_REGS);
  1432. disable_pch_dp(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1433. disable_pch_dp(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1434. disable_pch_dp(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1435. reg = PCH_ADPA;
  1436. val = I915_READ(reg);
  1437. if (adpa_pipe_enabled(dev_priv, val, pipe))
  1438. I915_WRITE(reg, val & ~ADPA_DAC_ENABLE);
  1439. reg = PCH_LVDS;
  1440. val = I915_READ(reg);
  1441. if (lvds_pipe_enabled(dev_priv, val, pipe)) {
  1442. DRM_DEBUG_KMS("disable lvds on pipe %d val 0x%08x\n", pipe, val);
  1443. I915_WRITE(reg, val & ~LVDS_PORT_EN);
  1444. POSTING_READ(reg);
  1445. udelay(100);
  1446. }
  1447. disable_pch_hdmi(dev_priv, pipe, HDMIB);
  1448. disable_pch_hdmi(dev_priv, pipe, HDMIC);
  1449. disable_pch_hdmi(dev_priv, pipe, HDMID);
  1450. }
  1451. int
  1452. intel_pin_and_fence_fb_obj(struct drm_device *dev,
  1453. struct drm_i915_gem_object *obj,
  1454. struct intel_ring_buffer *pipelined)
  1455. {
  1456. struct drm_i915_private *dev_priv = dev->dev_private;
  1457. u32 alignment;
  1458. int ret;
  1459. switch (obj->tiling_mode) {
  1460. case I915_TILING_NONE:
  1461. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1462. alignment = 128 * 1024;
  1463. else if (INTEL_INFO(dev)->gen >= 4)
  1464. alignment = 4 * 1024;
  1465. else
  1466. alignment = 64 * 1024;
  1467. break;
  1468. case I915_TILING_X:
  1469. /* pin() will align the object as required by fence */
  1470. alignment = 0;
  1471. break;
  1472. case I915_TILING_Y:
  1473. /* FIXME: Is this true? */
  1474. DRM_ERROR("Y tiled not allowed for scan out buffers\n");
  1475. return -EINVAL;
  1476. default:
  1477. BUG();
  1478. }
  1479. dev_priv->mm.interruptible = false;
  1480. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1481. if (ret)
  1482. goto err_interruptible;
  1483. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1484. * fence, whereas 965+ only requires a fence if using
  1485. * framebuffer compression. For simplicity, we always install
  1486. * a fence as the cost is not that onerous.
  1487. */
  1488. ret = i915_gem_object_get_fence(obj);
  1489. if (ret)
  1490. goto err_unpin;
  1491. i915_gem_object_pin_fence(obj);
  1492. dev_priv->mm.interruptible = true;
  1493. return 0;
  1494. err_unpin:
  1495. i915_gem_object_unpin(obj);
  1496. err_interruptible:
  1497. dev_priv->mm.interruptible = true;
  1498. return ret;
  1499. }
  1500. void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
  1501. {
  1502. i915_gem_object_unpin_fence(obj);
  1503. i915_gem_object_unpin(obj);
  1504. }
  1505. static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1506. int x, int y)
  1507. {
  1508. struct drm_device *dev = crtc->dev;
  1509. struct drm_i915_private *dev_priv = dev->dev_private;
  1510. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1511. struct intel_framebuffer *intel_fb;
  1512. struct drm_i915_gem_object *obj;
  1513. int plane = intel_crtc->plane;
  1514. unsigned long Start, Offset;
  1515. u32 dspcntr;
  1516. u32 reg;
  1517. switch (plane) {
  1518. case 0:
  1519. case 1:
  1520. break;
  1521. default:
  1522. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1523. return -EINVAL;
  1524. }
  1525. intel_fb = to_intel_framebuffer(fb);
  1526. obj = intel_fb->obj;
  1527. reg = DSPCNTR(plane);
  1528. dspcntr = I915_READ(reg);
  1529. /* Mask out pixel format bits in case we change it */
  1530. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1531. switch (fb->bits_per_pixel) {
  1532. case 8:
  1533. dspcntr |= DISPPLANE_8BPP;
  1534. break;
  1535. case 16:
  1536. if (fb->depth == 15)
  1537. dspcntr |= DISPPLANE_15_16BPP;
  1538. else
  1539. dspcntr |= DISPPLANE_16BPP;
  1540. break;
  1541. case 24:
  1542. case 32:
  1543. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  1544. break;
  1545. default:
  1546. DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
  1547. return -EINVAL;
  1548. }
  1549. if (INTEL_INFO(dev)->gen >= 4) {
  1550. if (obj->tiling_mode != I915_TILING_NONE)
  1551. dspcntr |= DISPPLANE_TILED;
  1552. else
  1553. dspcntr &= ~DISPPLANE_TILED;
  1554. }
  1555. I915_WRITE(reg, dspcntr);
  1556. Start = obj->gtt_offset;
  1557. Offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  1558. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  1559. Start, Offset, x, y, fb->pitches[0]);
  1560. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  1561. if (INTEL_INFO(dev)->gen >= 4) {
  1562. I915_MODIFY_DISPBASE(DSPSURF(plane), Start);
  1563. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  1564. I915_WRITE(DSPADDR(plane), Offset);
  1565. } else
  1566. I915_WRITE(DSPADDR(plane), Start + Offset);
  1567. POSTING_READ(reg);
  1568. return 0;
  1569. }
  1570. static int ironlake_update_plane(struct drm_crtc *crtc,
  1571. struct drm_framebuffer *fb, int x, int y)
  1572. {
  1573. struct drm_device *dev = crtc->dev;
  1574. struct drm_i915_private *dev_priv = dev->dev_private;
  1575. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1576. struct intel_framebuffer *intel_fb;
  1577. struct drm_i915_gem_object *obj;
  1578. int plane = intel_crtc->plane;
  1579. unsigned long Start, Offset;
  1580. u32 dspcntr;
  1581. u32 reg;
  1582. switch (plane) {
  1583. case 0:
  1584. case 1:
  1585. case 2:
  1586. break;
  1587. default:
  1588. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1589. return -EINVAL;
  1590. }
  1591. intel_fb = to_intel_framebuffer(fb);
  1592. obj = intel_fb->obj;
  1593. reg = DSPCNTR(plane);
  1594. dspcntr = I915_READ(reg);
  1595. /* Mask out pixel format bits in case we change it */
  1596. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1597. switch (fb->bits_per_pixel) {
  1598. case 8:
  1599. dspcntr |= DISPPLANE_8BPP;
  1600. break;
  1601. case 16:
  1602. if (fb->depth != 16)
  1603. return -EINVAL;
  1604. dspcntr |= DISPPLANE_16BPP;
  1605. break;
  1606. case 24:
  1607. case 32:
  1608. if (fb->depth == 24)
  1609. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  1610. else if (fb->depth == 30)
  1611. dspcntr |= DISPPLANE_32BPP_30BIT_NO_ALPHA;
  1612. else
  1613. return -EINVAL;
  1614. break;
  1615. default:
  1616. DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
  1617. return -EINVAL;
  1618. }
  1619. if (obj->tiling_mode != I915_TILING_NONE)
  1620. dspcntr |= DISPPLANE_TILED;
  1621. else
  1622. dspcntr &= ~DISPPLANE_TILED;
  1623. /* must disable */
  1624. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  1625. I915_WRITE(reg, dspcntr);
  1626. Start = obj->gtt_offset;
  1627. Offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  1628. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  1629. Start, Offset, x, y, fb->pitches[0]);
  1630. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  1631. I915_MODIFY_DISPBASE(DSPSURF(plane), Start);
  1632. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  1633. I915_WRITE(DSPADDR(plane), Offset);
  1634. POSTING_READ(reg);
  1635. return 0;
  1636. }
  1637. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  1638. static int
  1639. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1640. int x, int y, enum mode_set_atomic state)
  1641. {
  1642. struct drm_device *dev = crtc->dev;
  1643. struct drm_i915_private *dev_priv = dev->dev_private;
  1644. if (dev_priv->display.disable_fbc)
  1645. dev_priv->display.disable_fbc(dev);
  1646. intel_increase_pllclock(crtc);
  1647. return dev_priv->display.update_plane(crtc, fb, x, y);
  1648. }
  1649. static int
  1650. intel_finish_fb(struct drm_framebuffer *old_fb)
  1651. {
  1652. struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
  1653. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1654. bool was_interruptible = dev_priv->mm.interruptible;
  1655. int ret;
  1656. wait_event(dev_priv->pending_flip_queue,
  1657. atomic_read(&dev_priv->mm.wedged) ||
  1658. atomic_read(&obj->pending_flip) == 0);
  1659. /* Big Hammer, we also need to ensure that any pending
  1660. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  1661. * current scanout is retired before unpinning the old
  1662. * framebuffer.
  1663. *
  1664. * This should only fail upon a hung GPU, in which case we
  1665. * can safely continue.
  1666. */
  1667. dev_priv->mm.interruptible = false;
  1668. ret = i915_gem_object_finish_gpu(obj);
  1669. dev_priv->mm.interruptible = was_interruptible;
  1670. return ret;
  1671. }
  1672. static int
  1673. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  1674. struct drm_framebuffer *old_fb)
  1675. {
  1676. struct drm_device *dev = crtc->dev;
  1677. struct drm_i915_private *dev_priv = dev->dev_private;
  1678. struct drm_i915_master_private *master_priv;
  1679. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1680. int ret;
  1681. /* no fb bound */
  1682. if (!crtc->fb) {
  1683. DRM_ERROR("No FB bound\n");
  1684. return 0;
  1685. }
  1686. switch (intel_crtc->plane) {
  1687. case 0:
  1688. case 1:
  1689. break;
  1690. case 2:
  1691. if (IS_IVYBRIDGE(dev))
  1692. break;
  1693. /* fall through otherwise */
  1694. default:
  1695. DRM_ERROR("no plane for crtc\n");
  1696. return -EINVAL;
  1697. }
  1698. mutex_lock(&dev->struct_mutex);
  1699. ret = intel_pin_and_fence_fb_obj(dev,
  1700. to_intel_framebuffer(crtc->fb)->obj,
  1701. NULL);
  1702. if (ret != 0) {
  1703. mutex_unlock(&dev->struct_mutex);
  1704. DRM_ERROR("pin & fence failed\n");
  1705. return ret;
  1706. }
  1707. if (old_fb)
  1708. intel_finish_fb(old_fb);
  1709. ret = dev_priv->display.update_plane(crtc, crtc->fb, x, y);
  1710. if (ret) {
  1711. intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
  1712. mutex_unlock(&dev->struct_mutex);
  1713. DRM_ERROR("failed to update base address\n");
  1714. return ret;
  1715. }
  1716. if (old_fb) {
  1717. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1718. intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
  1719. }
  1720. intel_update_fbc(dev);
  1721. mutex_unlock(&dev->struct_mutex);
  1722. if (!dev->primary->master)
  1723. return 0;
  1724. master_priv = dev->primary->master->driver_priv;
  1725. if (!master_priv->sarea_priv)
  1726. return 0;
  1727. if (intel_crtc->pipe) {
  1728. master_priv->sarea_priv->pipeB_x = x;
  1729. master_priv->sarea_priv->pipeB_y = y;
  1730. } else {
  1731. master_priv->sarea_priv->pipeA_x = x;
  1732. master_priv->sarea_priv->pipeA_y = y;
  1733. }
  1734. return 0;
  1735. }
  1736. static void ironlake_set_pll_edp(struct drm_crtc *crtc, int clock)
  1737. {
  1738. struct drm_device *dev = crtc->dev;
  1739. struct drm_i915_private *dev_priv = dev->dev_private;
  1740. u32 dpa_ctl;
  1741. DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
  1742. dpa_ctl = I915_READ(DP_A);
  1743. dpa_ctl &= ~DP_PLL_FREQ_MASK;
  1744. if (clock < 200000) {
  1745. u32 temp;
  1746. dpa_ctl |= DP_PLL_FREQ_160MHZ;
  1747. /* workaround for 160Mhz:
  1748. 1) program 0x4600c bits 15:0 = 0x8124
  1749. 2) program 0x46010 bit 0 = 1
  1750. 3) program 0x46034 bit 24 = 1
  1751. 4) program 0x64000 bit 14 = 1
  1752. */
  1753. temp = I915_READ(0x4600c);
  1754. temp &= 0xffff0000;
  1755. I915_WRITE(0x4600c, temp | 0x8124);
  1756. temp = I915_READ(0x46010);
  1757. I915_WRITE(0x46010, temp | 1);
  1758. temp = I915_READ(0x46034);
  1759. I915_WRITE(0x46034, temp | (1 << 24));
  1760. } else {
  1761. dpa_ctl |= DP_PLL_FREQ_270MHZ;
  1762. }
  1763. I915_WRITE(DP_A, dpa_ctl);
  1764. POSTING_READ(DP_A);
  1765. udelay(500);
  1766. }
  1767. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  1768. {
  1769. struct drm_device *dev = crtc->dev;
  1770. struct drm_i915_private *dev_priv = dev->dev_private;
  1771. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1772. int pipe = intel_crtc->pipe;
  1773. u32 reg, temp;
  1774. /* enable normal train */
  1775. reg = FDI_TX_CTL(pipe);
  1776. temp = I915_READ(reg);
  1777. if (IS_IVYBRIDGE(dev)) {
  1778. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  1779. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  1780. } else {
  1781. temp &= ~FDI_LINK_TRAIN_NONE;
  1782. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  1783. }
  1784. I915_WRITE(reg, temp);
  1785. reg = FDI_RX_CTL(pipe);
  1786. temp = I915_READ(reg);
  1787. if (HAS_PCH_CPT(dev)) {
  1788. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1789. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  1790. } else {
  1791. temp &= ~FDI_LINK_TRAIN_NONE;
  1792. temp |= FDI_LINK_TRAIN_NONE;
  1793. }
  1794. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  1795. /* wait one idle pattern time */
  1796. POSTING_READ(reg);
  1797. udelay(1000);
  1798. /* IVB wants error correction enabled */
  1799. if (IS_IVYBRIDGE(dev))
  1800. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  1801. FDI_FE_ERRC_ENABLE);
  1802. }
  1803. static void cpt_phase_pointer_enable(struct drm_device *dev, int pipe)
  1804. {
  1805. struct drm_i915_private *dev_priv = dev->dev_private;
  1806. u32 flags = I915_READ(SOUTH_CHICKEN1);
  1807. flags |= FDI_PHASE_SYNC_OVR(pipe);
  1808. I915_WRITE(SOUTH_CHICKEN1, flags); /* once to unlock... */
  1809. flags |= FDI_PHASE_SYNC_EN(pipe);
  1810. I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to enable */
  1811. POSTING_READ(SOUTH_CHICKEN1);
  1812. }
  1813. /* The FDI link training functions for ILK/Ibexpeak. */
  1814. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  1815. {
  1816. struct drm_device *dev = crtc->dev;
  1817. struct drm_i915_private *dev_priv = dev->dev_private;
  1818. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1819. int pipe = intel_crtc->pipe;
  1820. int plane = intel_crtc->plane;
  1821. u32 reg, temp, tries;
  1822. /* FDI needs bits from pipe & plane first */
  1823. assert_pipe_enabled(dev_priv, pipe);
  1824. assert_plane_enabled(dev_priv, plane);
  1825. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  1826. for train result */
  1827. reg = FDI_RX_IMR(pipe);
  1828. temp = I915_READ(reg);
  1829. temp &= ~FDI_RX_SYMBOL_LOCK;
  1830. temp &= ~FDI_RX_BIT_LOCK;
  1831. I915_WRITE(reg, temp);
  1832. I915_READ(reg);
  1833. udelay(150);
  1834. /* enable CPU FDI TX and PCH FDI RX */
  1835. reg = FDI_TX_CTL(pipe);
  1836. temp = I915_READ(reg);
  1837. temp &= ~(7 << 19);
  1838. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  1839. temp &= ~FDI_LINK_TRAIN_NONE;
  1840. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1841. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  1842. reg = FDI_RX_CTL(pipe);
  1843. temp = I915_READ(reg);
  1844. temp &= ~FDI_LINK_TRAIN_NONE;
  1845. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1846. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  1847. POSTING_READ(reg);
  1848. udelay(150);
  1849. /* Ironlake workaround, enable clock pointer after FDI enable*/
  1850. if (HAS_PCH_IBX(dev)) {
  1851. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  1852. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  1853. FDI_RX_PHASE_SYNC_POINTER_EN);
  1854. }
  1855. reg = FDI_RX_IIR(pipe);
  1856. for (tries = 0; tries < 5; tries++) {
  1857. temp = I915_READ(reg);
  1858. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1859. if ((temp & FDI_RX_BIT_LOCK)) {
  1860. DRM_DEBUG_KMS("FDI train 1 done.\n");
  1861. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  1862. break;
  1863. }
  1864. }
  1865. if (tries == 5)
  1866. DRM_ERROR("FDI train 1 fail!\n");
  1867. /* Train 2 */
  1868. reg = FDI_TX_CTL(pipe);
  1869. temp = I915_READ(reg);
  1870. temp &= ~FDI_LINK_TRAIN_NONE;
  1871. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1872. I915_WRITE(reg, temp);
  1873. reg = FDI_RX_CTL(pipe);
  1874. temp = I915_READ(reg);
  1875. temp &= ~FDI_LINK_TRAIN_NONE;
  1876. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1877. I915_WRITE(reg, temp);
  1878. POSTING_READ(reg);
  1879. udelay(150);
  1880. reg = FDI_RX_IIR(pipe);
  1881. for (tries = 0; tries < 5; tries++) {
  1882. temp = I915_READ(reg);
  1883. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1884. if (temp & FDI_RX_SYMBOL_LOCK) {
  1885. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  1886. DRM_DEBUG_KMS("FDI train 2 done.\n");
  1887. break;
  1888. }
  1889. }
  1890. if (tries == 5)
  1891. DRM_ERROR("FDI train 2 fail!\n");
  1892. DRM_DEBUG_KMS("FDI train done\n");
  1893. }
  1894. static const int snb_b_fdi_train_param[] = {
  1895. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  1896. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  1897. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  1898. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  1899. };
  1900. /* The FDI link training functions for SNB/Cougarpoint. */
  1901. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  1902. {
  1903. struct drm_device *dev = crtc->dev;
  1904. struct drm_i915_private *dev_priv = dev->dev_private;
  1905. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1906. int pipe = intel_crtc->pipe;
  1907. u32 reg, temp, i, retry;
  1908. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  1909. for train result */
  1910. reg = FDI_RX_IMR(pipe);
  1911. temp = I915_READ(reg);
  1912. temp &= ~FDI_RX_SYMBOL_LOCK;
  1913. temp &= ~FDI_RX_BIT_LOCK;
  1914. I915_WRITE(reg, temp);
  1915. POSTING_READ(reg);
  1916. udelay(150);
  1917. /* enable CPU FDI TX and PCH FDI RX */
  1918. reg = FDI_TX_CTL(pipe);
  1919. temp = I915_READ(reg);
  1920. temp &= ~(7 << 19);
  1921. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  1922. temp &= ~FDI_LINK_TRAIN_NONE;
  1923. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1924. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1925. /* SNB-B */
  1926. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  1927. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  1928. reg = FDI_RX_CTL(pipe);
  1929. temp = I915_READ(reg);
  1930. if (HAS_PCH_CPT(dev)) {
  1931. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1932. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  1933. } else {
  1934. temp &= ~FDI_LINK_TRAIN_NONE;
  1935. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1936. }
  1937. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  1938. POSTING_READ(reg);
  1939. udelay(150);
  1940. if (HAS_PCH_CPT(dev))
  1941. cpt_phase_pointer_enable(dev, pipe);
  1942. for (i = 0; i < 4; i++) {
  1943. reg = FDI_TX_CTL(pipe);
  1944. temp = I915_READ(reg);
  1945. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1946. temp |= snb_b_fdi_train_param[i];
  1947. I915_WRITE(reg, temp);
  1948. POSTING_READ(reg);
  1949. udelay(500);
  1950. for (retry = 0; retry < 5; retry++) {
  1951. reg = FDI_RX_IIR(pipe);
  1952. temp = I915_READ(reg);
  1953. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1954. if (temp & FDI_RX_BIT_LOCK) {
  1955. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  1956. DRM_DEBUG_KMS("FDI train 1 done.\n");
  1957. break;
  1958. }
  1959. udelay(50);
  1960. }
  1961. if (retry < 5)
  1962. break;
  1963. }
  1964. if (i == 4)
  1965. DRM_ERROR("FDI train 1 fail!\n");
  1966. /* Train 2 */
  1967. reg = FDI_TX_CTL(pipe);
  1968. temp = I915_READ(reg);
  1969. temp &= ~FDI_LINK_TRAIN_NONE;
  1970. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1971. if (IS_GEN6(dev)) {
  1972. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1973. /* SNB-B */
  1974. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  1975. }
  1976. I915_WRITE(reg, temp);
  1977. reg = FDI_RX_CTL(pipe);
  1978. temp = I915_READ(reg);
  1979. if (HAS_PCH_CPT(dev)) {
  1980. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1981. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  1982. } else {
  1983. temp &= ~FDI_LINK_TRAIN_NONE;
  1984. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1985. }
  1986. I915_WRITE(reg, temp);
  1987. POSTING_READ(reg);
  1988. udelay(150);
  1989. for (i = 0; i < 4; i++) {
  1990. reg = FDI_TX_CTL(pipe);
  1991. temp = I915_READ(reg);
  1992. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1993. temp |= snb_b_fdi_train_param[i];
  1994. I915_WRITE(reg, temp);
  1995. POSTING_READ(reg);
  1996. udelay(500);
  1997. for (retry = 0; retry < 5; retry++) {
  1998. reg = FDI_RX_IIR(pipe);
  1999. temp = I915_READ(reg);
  2000. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2001. if (temp & FDI_RX_SYMBOL_LOCK) {
  2002. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2003. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2004. break;
  2005. }
  2006. udelay(50);
  2007. }
  2008. if (retry < 5)
  2009. break;
  2010. }
  2011. if (i == 4)
  2012. DRM_ERROR("FDI train 2 fail!\n");
  2013. DRM_DEBUG_KMS("FDI train done.\n");
  2014. }
  2015. /* Manual link training for Ivy Bridge A0 parts */
  2016. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2017. {
  2018. struct drm_device *dev = crtc->dev;
  2019. struct drm_i915_private *dev_priv = dev->dev_private;
  2020. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2021. int pipe = intel_crtc->pipe;
  2022. u32 reg, temp, i;
  2023. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2024. for train result */
  2025. reg = FDI_RX_IMR(pipe);
  2026. temp = I915_READ(reg);
  2027. temp &= ~FDI_RX_SYMBOL_LOCK;
  2028. temp &= ~FDI_RX_BIT_LOCK;
  2029. I915_WRITE(reg, temp);
  2030. POSTING_READ(reg);
  2031. udelay(150);
  2032. /* enable CPU FDI TX and PCH FDI RX */
  2033. reg = FDI_TX_CTL(pipe);
  2034. temp = I915_READ(reg);
  2035. temp &= ~(7 << 19);
  2036. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2037. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2038. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2039. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2040. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2041. temp |= FDI_COMPOSITE_SYNC;
  2042. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2043. reg = FDI_RX_CTL(pipe);
  2044. temp = I915_READ(reg);
  2045. temp &= ~FDI_LINK_TRAIN_AUTO;
  2046. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2047. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2048. temp |= FDI_COMPOSITE_SYNC;
  2049. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2050. POSTING_READ(reg);
  2051. udelay(150);
  2052. if (HAS_PCH_CPT(dev))
  2053. cpt_phase_pointer_enable(dev, pipe);
  2054. for (i = 0; i < 4; i++) {
  2055. reg = FDI_TX_CTL(pipe);
  2056. temp = I915_READ(reg);
  2057. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2058. temp |= snb_b_fdi_train_param[i];
  2059. I915_WRITE(reg, temp);
  2060. POSTING_READ(reg);
  2061. udelay(500);
  2062. reg = FDI_RX_IIR(pipe);
  2063. temp = I915_READ(reg);
  2064. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2065. if (temp & FDI_RX_BIT_LOCK ||
  2066. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2067. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2068. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2069. break;
  2070. }
  2071. }
  2072. if (i == 4)
  2073. DRM_ERROR("FDI train 1 fail!\n");
  2074. /* Train 2 */
  2075. reg = FDI_TX_CTL(pipe);
  2076. temp = I915_READ(reg);
  2077. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2078. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2079. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2080. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2081. I915_WRITE(reg, temp);
  2082. reg = FDI_RX_CTL(pipe);
  2083. temp = I915_READ(reg);
  2084. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2085. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2086. I915_WRITE(reg, temp);
  2087. POSTING_READ(reg);
  2088. udelay(150);
  2089. for (i = 0; i < 4; i++) {
  2090. reg = FDI_TX_CTL(pipe);
  2091. temp = I915_READ(reg);
  2092. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2093. temp |= snb_b_fdi_train_param[i];
  2094. I915_WRITE(reg, temp);
  2095. POSTING_READ(reg);
  2096. udelay(500);
  2097. reg = FDI_RX_IIR(pipe);
  2098. temp = I915_READ(reg);
  2099. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2100. if (temp & FDI_RX_SYMBOL_LOCK) {
  2101. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2102. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2103. break;
  2104. }
  2105. }
  2106. if (i == 4)
  2107. DRM_ERROR("FDI train 2 fail!\n");
  2108. DRM_DEBUG_KMS("FDI train done.\n");
  2109. }
  2110. static void ironlake_fdi_pll_enable(struct drm_crtc *crtc)
  2111. {
  2112. struct drm_device *dev = crtc->dev;
  2113. struct drm_i915_private *dev_priv = dev->dev_private;
  2114. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2115. int pipe = intel_crtc->pipe;
  2116. u32 reg, temp;
  2117. /* Write the TU size bits so error detection works */
  2118. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  2119. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  2120. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2121. reg = FDI_RX_CTL(pipe);
  2122. temp = I915_READ(reg);
  2123. temp &= ~((0x7 << 19) | (0x7 << 16));
  2124. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2125. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2126. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2127. POSTING_READ(reg);
  2128. udelay(200);
  2129. /* Switch from Rawclk to PCDclk */
  2130. temp = I915_READ(reg);
  2131. I915_WRITE(reg, temp | FDI_PCDCLK);
  2132. POSTING_READ(reg);
  2133. udelay(200);
  2134. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2135. reg = FDI_TX_CTL(pipe);
  2136. temp = I915_READ(reg);
  2137. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2138. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2139. POSTING_READ(reg);
  2140. udelay(100);
  2141. }
  2142. }
  2143. static void cpt_phase_pointer_disable(struct drm_device *dev, int pipe)
  2144. {
  2145. struct drm_i915_private *dev_priv = dev->dev_private;
  2146. u32 flags = I915_READ(SOUTH_CHICKEN1);
  2147. flags &= ~(FDI_PHASE_SYNC_EN(pipe));
  2148. I915_WRITE(SOUTH_CHICKEN1, flags); /* once to disable... */
  2149. flags &= ~(FDI_PHASE_SYNC_OVR(pipe));
  2150. I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to lock */
  2151. POSTING_READ(SOUTH_CHICKEN1);
  2152. }
  2153. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2154. {
  2155. struct drm_device *dev = crtc->dev;
  2156. struct drm_i915_private *dev_priv = dev->dev_private;
  2157. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2158. int pipe = intel_crtc->pipe;
  2159. u32 reg, temp;
  2160. /* disable CPU FDI tx and PCH FDI rx */
  2161. reg = FDI_TX_CTL(pipe);
  2162. temp = I915_READ(reg);
  2163. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2164. POSTING_READ(reg);
  2165. reg = FDI_RX_CTL(pipe);
  2166. temp = I915_READ(reg);
  2167. temp &= ~(0x7 << 16);
  2168. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2169. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2170. POSTING_READ(reg);
  2171. udelay(100);
  2172. /* Ironlake workaround, disable clock pointer after downing FDI */
  2173. if (HAS_PCH_IBX(dev)) {
  2174. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2175. I915_WRITE(FDI_RX_CHICKEN(pipe),
  2176. I915_READ(FDI_RX_CHICKEN(pipe) &
  2177. ~FDI_RX_PHASE_SYNC_POINTER_EN));
  2178. } else if (HAS_PCH_CPT(dev)) {
  2179. cpt_phase_pointer_disable(dev, pipe);
  2180. }
  2181. /* still set train pattern 1 */
  2182. reg = FDI_TX_CTL(pipe);
  2183. temp = I915_READ(reg);
  2184. temp &= ~FDI_LINK_TRAIN_NONE;
  2185. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2186. I915_WRITE(reg, temp);
  2187. reg = FDI_RX_CTL(pipe);
  2188. temp = I915_READ(reg);
  2189. if (HAS_PCH_CPT(dev)) {
  2190. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2191. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2192. } else {
  2193. temp &= ~FDI_LINK_TRAIN_NONE;
  2194. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2195. }
  2196. /* BPC in FDI rx is consistent with that in PIPECONF */
  2197. temp &= ~(0x07 << 16);
  2198. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2199. I915_WRITE(reg, temp);
  2200. POSTING_READ(reg);
  2201. udelay(100);
  2202. }
  2203. /*
  2204. * When we disable a pipe, we need to clear any pending scanline wait events
  2205. * to avoid hanging the ring, which we assume we are waiting on.
  2206. */
  2207. static void intel_clear_scanline_wait(struct drm_device *dev)
  2208. {
  2209. struct drm_i915_private *dev_priv = dev->dev_private;
  2210. struct intel_ring_buffer *ring;
  2211. u32 tmp;
  2212. if (IS_GEN2(dev))
  2213. /* Can't break the hang on i8xx */
  2214. return;
  2215. ring = LP_RING(dev_priv);
  2216. tmp = I915_READ_CTL(ring);
  2217. if (tmp & RING_WAIT)
  2218. I915_WRITE_CTL(ring, tmp);
  2219. }
  2220. static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  2221. {
  2222. struct drm_device *dev = crtc->dev;
  2223. if (crtc->fb == NULL)
  2224. return;
  2225. mutex_lock(&dev->struct_mutex);
  2226. intel_finish_fb(crtc->fb);
  2227. mutex_unlock(&dev->struct_mutex);
  2228. }
  2229. static bool intel_crtc_driving_pch(struct drm_crtc *crtc)
  2230. {
  2231. struct drm_device *dev = crtc->dev;
  2232. struct drm_mode_config *mode_config = &dev->mode_config;
  2233. struct intel_encoder *encoder;
  2234. /*
  2235. * If there's a non-PCH eDP on this crtc, it must be DP_A, and that
  2236. * must be driven by its own crtc; no sharing is possible.
  2237. */
  2238. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  2239. if (encoder->base.crtc != crtc)
  2240. continue;
  2241. switch (encoder->type) {
  2242. case INTEL_OUTPUT_EDP:
  2243. if (!intel_encoder_is_pch_edp(&encoder->base))
  2244. return false;
  2245. continue;
  2246. }
  2247. }
  2248. return true;
  2249. }
  2250. /*
  2251. * Enable PCH resources required for PCH ports:
  2252. * - PCH PLLs
  2253. * - FDI training & RX/TX
  2254. * - update transcoder timings
  2255. * - DP transcoding bits
  2256. * - transcoder
  2257. */
  2258. static void ironlake_pch_enable(struct drm_crtc *crtc)
  2259. {
  2260. struct drm_device *dev = crtc->dev;
  2261. struct drm_i915_private *dev_priv = dev->dev_private;
  2262. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2263. int pipe = intel_crtc->pipe;
  2264. u32 reg, temp;
  2265. /* For PCH output, training FDI link */
  2266. dev_priv->display.fdi_link_train(crtc);
  2267. intel_enable_pch_pll(intel_crtc);
  2268. if (HAS_PCH_CPT(dev)) {
  2269. u32 sel;
  2270. temp = I915_READ(PCH_DPLL_SEL);
  2271. switch (pipe) {
  2272. default:
  2273. case 0:
  2274. temp |= TRANSA_DPLL_ENABLE;
  2275. sel = TRANSA_DPLLB_SEL;
  2276. break;
  2277. case 1:
  2278. temp |= TRANSB_DPLL_ENABLE;
  2279. sel = TRANSB_DPLLB_SEL;
  2280. break;
  2281. case 2:
  2282. temp |= TRANSC_DPLL_ENABLE;
  2283. sel = TRANSC_DPLLB_SEL;
  2284. break;
  2285. }
  2286. if (intel_crtc->pch_pll->pll_reg == _PCH_DPLL_B)
  2287. temp |= sel;
  2288. else
  2289. temp &= ~sel;
  2290. I915_WRITE(PCH_DPLL_SEL, temp);
  2291. }
  2292. /* set transcoder timing, panel must allow it */
  2293. assert_panel_unlocked(dev_priv, pipe);
  2294. I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
  2295. I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
  2296. I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
  2297. I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
  2298. I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
  2299. I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
  2300. I915_WRITE(TRANS_VSYNCSHIFT(pipe), I915_READ(VSYNCSHIFT(pipe)));
  2301. intel_fdi_normal_train(crtc);
  2302. /* For PCH DP, enable TRANS_DP_CTL */
  2303. if (HAS_PCH_CPT(dev) &&
  2304. (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  2305. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2306. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) >> 5;
  2307. reg = TRANS_DP_CTL(pipe);
  2308. temp = I915_READ(reg);
  2309. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  2310. TRANS_DP_SYNC_MASK |
  2311. TRANS_DP_BPC_MASK);
  2312. temp |= (TRANS_DP_OUTPUT_ENABLE |
  2313. TRANS_DP_ENH_FRAMING);
  2314. temp |= bpc << 9; /* same format but at 11:9 */
  2315. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  2316. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  2317. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  2318. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  2319. switch (intel_trans_dp_port_sel(crtc)) {
  2320. case PCH_DP_B:
  2321. temp |= TRANS_DP_PORT_SEL_B;
  2322. break;
  2323. case PCH_DP_C:
  2324. temp |= TRANS_DP_PORT_SEL_C;
  2325. break;
  2326. case PCH_DP_D:
  2327. temp |= TRANS_DP_PORT_SEL_D;
  2328. break;
  2329. default:
  2330. DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
  2331. temp |= TRANS_DP_PORT_SEL_B;
  2332. break;
  2333. }
  2334. I915_WRITE(reg, temp);
  2335. }
  2336. intel_enable_transcoder(dev_priv, pipe);
  2337. }
  2338. static void intel_put_pch_pll(struct intel_crtc *intel_crtc)
  2339. {
  2340. struct intel_pch_pll *pll = intel_crtc->pch_pll;
  2341. if (pll == NULL)
  2342. return;
  2343. if (pll->refcount == 0) {
  2344. WARN(1, "bad PCH PLL refcount\n");
  2345. return;
  2346. }
  2347. --pll->refcount;
  2348. intel_crtc->pch_pll = NULL;
  2349. }
  2350. static struct intel_pch_pll *intel_get_pch_pll(struct intel_crtc *intel_crtc, u32 dpll, u32 fp)
  2351. {
  2352. struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
  2353. struct intel_pch_pll *pll;
  2354. int i;
  2355. pll = intel_crtc->pch_pll;
  2356. if (pll) {
  2357. DRM_DEBUG_KMS("CRTC:%d reusing existing PCH PLL %x\n",
  2358. intel_crtc->base.base.id, pll->pll_reg);
  2359. goto prepare;
  2360. }
  2361. for (i = 0; i < dev_priv->num_pch_pll; i++) {
  2362. pll = &dev_priv->pch_plls[i];
  2363. /* Only want to check enabled timings first */
  2364. if (pll->refcount == 0)
  2365. continue;
  2366. if (dpll == (I915_READ(pll->pll_reg) & 0x7fffffff) &&
  2367. fp == I915_READ(pll->fp0_reg)) {
  2368. DRM_DEBUG_KMS("CRTC:%d sharing existing PCH PLL %x (refcount %d, ative %d)\n",
  2369. intel_crtc->base.base.id,
  2370. pll->pll_reg, pll->refcount, pll->active);
  2371. goto found;
  2372. }
  2373. }
  2374. /* Ok no matching timings, maybe there's a free one? */
  2375. for (i = 0; i < dev_priv->num_pch_pll; i++) {
  2376. pll = &dev_priv->pch_plls[i];
  2377. if (pll->refcount == 0) {
  2378. DRM_DEBUG_KMS("CRTC:%d allocated PCH PLL %x\n",
  2379. intel_crtc->base.base.id, pll->pll_reg);
  2380. goto found;
  2381. }
  2382. }
  2383. return NULL;
  2384. found:
  2385. intel_crtc->pch_pll = pll;
  2386. pll->refcount++;
  2387. DRM_DEBUG_DRIVER("using pll %d for pipe %d\n", i, intel_crtc->pipe);
  2388. prepare: /* separate function? */
  2389. DRM_DEBUG_DRIVER("switching PLL %x off\n", pll->pll_reg);
  2390. I915_WRITE(pll->fp0_reg, fp);
  2391. I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
  2392. POSTING_READ(pll->pll_reg);
  2393. udelay(150);
  2394. pll->on = false;
  2395. return pll;
  2396. }
  2397. void intel_cpt_verify_modeset(struct drm_device *dev, int pipe)
  2398. {
  2399. struct drm_i915_private *dev_priv = dev->dev_private;
  2400. int dslreg = PIPEDSL(pipe), tc2reg = TRANS_CHICKEN2(pipe);
  2401. u32 temp;
  2402. temp = I915_READ(dslreg);
  2403. udelay(500);
  2404. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  2405. /* Without this, mode sets may fail silently on FDI */
  2406. I915_WRITE(tc2reg, TRANS_AUTOTRAIN_GEN_STALL_DIS);
  2407. udelay(250);
  2408. I915_WRITE(tc2reg, 0);
  2409. if (wait_for(I915_READ(dslreg) != temp, 5))
  2410. DRM_ERROR("mode set failed: pipe %d stuck\n", pipe);
  2411. }
  2412. }
  2413. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  2414. {
  2415. struct drm_device *dev = crtc->dev;
  2416. struct drm_i915_private *dev_priv = dev->dev_private;
  2417. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2418. int pipe = intel_crtc->pipe;
  2419. int plane = intel_crtc->plane;
  2420. u32 temp;
  2421. bool is_pch_port;
  2422. if (intel_crtc->active)
  2423. return;
  2424. intel_crtc->active = true;
  2425. intel_update_watermarks(dev);
  2426. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  2427. temp = I915_READ(PCH_LVDS);
  2428. if ((temp & LVDS_PORT_EN) == 0)
  2429. I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
  2430. }
  2431. is_pch_port = intel_crtc_driving_pch(crtc);
  2432. if (is_pch_port)
  2433. ironlake_fdi_pll_enable(crtc);
  2434. else
  2435. ironlake_fdi_disable(crtc);
  2436. /* Enable panel fitting for LVDS */
  2437. if (dev_priv->pch_pf_size &&
  2438. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) || HAS_eDP)) {
  2439. /* Force use of hard-coded filter coefficients
  2440. * as some pre-programmed values are broken,
  2441. * e.g. x201.
  2442. */
  2443. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  2444. I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
  2445. I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
  2446. }
  2447. /*
  2448. * On ILK+ LUT must be loaded before the pipe is running but with
  2449. * clocks enabled
  2450. */
  2451. intel_crtc_load_lut(crtc);
  2452. intel_enable_pipe(dev_priv, pipe, is_pch_port);
  2453. intel_enable_plane(dev_priv, plane, pipe);
  2454. if (is_pch_port)
  2455. ironlake_pch_enable(crtc);
  2456. mutex_lock(&dev->struct_mutex);
  2457. intel_update_fbc(dev);
  2458. mutex_unlock(&dev->struct_mutex);
  2459. intel_crtc_update_cursor(crtc, true);
  2460. }
  2461. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  2462. {
  2463. struct drm_device *dev = crtc->dev;
  2464. struct drm_i915_private *dev_priv = dev->dev_private;
  2465. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2466. int pipe = intel_crtc->pipe;
  2467. int plane = intel_crtc->plane;
  2468. u32 reg, temp;
  2469. if (!intel_crtc->active)
  2470. return;
  2471. intel_crtc_wait_for_pending_flips(crtc);
  2472. drm_vblank_off(dev, pipe);
  2473. intel_crtc_update_cursor(crtc, false);
  2474. intel_disable_plane(dev_priv, plane, pipe);
  2475. if (dev_priv->cfb_plane == plane)
  2476. intel_disable_fbc(dev);
  2477. intel_disable_pipe(dev_priv, pipe);
  2478. /* Disable PF */
  2479. I915_WRITE(PF_CTL(pipe), 0);
  2480. I915_WRITE(PF_WIN_SZ(pipe), 0);
  2481. ironlake_fdi_disable(crtc);
  2482. /* This is a horrible layering violation; we should be doing this in
  2483. * the connector/encoder ->prepare instead, but we don't always have
  2484. * enough information there about the config to know whether it will
  2485. * actually be necessary or just cause undesired flicker.
  2486. */
  2487. intel_disable_pch_ports(dev_priv, pipe);
  2488. intel_disable_transcoder(dev_priv, pipe);
  2489. if (HAS_PCH_CPT(dev)) {
  2490. /* disable TRANS_DP_CTL */
  2491. reg = TRANS_DP_CTL(pipe);
  2492. temp = I915_READ(reg);
  2493. temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
  2494. temp |= TRANS_DP_PORT_SEL_NONE;
  2495. I915_WRITE(reg, temp);
  2496. /* disable DPLL_SEL */
  2497. temp = I915_READ(PCH_DPLL_SEL);
  2498. switch (pipe) {
  2499. case 0:
  2500. temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
  2501. break;
  2502. case 1:
  2503. temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  2504. break;
  2505. case 2:
  2506. /* C shares PLL A or B */
  2507. temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
  2508. break;
  2509. default:
  2510. BUG(); /* wtf */
  2511. }
  2512. I915_WRITE(PCH_DPLL_SEL, temp);
  2513. }
  2514. /* disable PCH DPLL */
  2515. intel_disable_pch_pll(intel_crtc);
  2516. /* Switch from PCDclk to Rawclk */
  2517. reg = FDI_RX_CTL(pipe);
  2518. temp = I915_READ(reg);
  2519. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2520. /* Disable CPU FDI TX PLL */
  2521. reg = FDI_TX_CTL(pipe);
  2522. temp = I915_READ(reg);
  2523. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2524. POSTING_READ(reg);
  2525. udelay(100);
  2526. reg = FDI_RX_CTL(pipe);
  2527. temp = I915_READ(reg);
  2528. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2529. /* Wait for the clocks to turn off. */
  2530. POSTING_READ(reg);
  2531. udelay(100);
  2532. intel_crtc->active = false;
  2533. intel_update_watermarks(dev);
  2534. mutex_lock(&dev->struct_mutex);
  2535. intel_update_fbc(dev);
  2536. intel_clear_scanline_wait(dev);
  2537. mutex_unlock(&dev->struct_mutex);
  2538. }
  2539. static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
  2540. {
  2541. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2542. int pipe = intel_crtc->pipe;
  2543. int plane = intel_crtc->plane;
  2544. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2545. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2546. */
  2547. switch (mode) {
  2548. case DRM_MODE_DPMS_ON:
  2549. case DRM_MODE_DPMS_STANDBY:
  2550. case DRM_MODE_DPMS_SUSPEND:
  2551. DRM_DEBUG_KMS("crtc %d/%d dpms on\n", pipe, plane);
  2552. ironlake_crtc_enable(crtc);
  2553. break;
  2554. case DRM_MODE_DPMS_OFF:
  2555. DRM_DEBUG_KMS("crtc %d/%d dpms off\n", pipe, plane);
  2556. ironlake_crtc_disable(crtc);
  2557. break;
  2558. }
  2559. }
  2560. static void ironlake_crtc_off(struct drm_crtc *crtc)
  2561. {
  2562. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2563. intel_put_pch_pll(intel_crtc);
  2564. }
  2565. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  2566. {
  2567. if (!enable && intel_crtc->overlay) {
  2568. struct drm_device *dev = intel_crtc->base.dev;
  2569. struct drm_i915_private *dev_priv = dev->dev_private;
  2570. mutex_lock(&dev->struct_mutex);
  2571. dev_priv->mm.interruptible = false;
  2572. (void) intel_overlay_switch_off(intel_crtc->overlay);
  2573. dev_priv->mm.interruptible = true;
  2574. mutex_unlock(&dev->struct_mutex);
  2575. }
  2576. /* Let userspace switch the overlay on again. In most cases userspace
  2577. * has to recompute where to put it anyway.
  2578. */
  2579. }
  2580. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  2581. {
  2582. struct drm_device *dev = crtc->dev;
  2583. struct drm_i915_private *dev_priv = dev->dev_private;
  2584. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2585. int pipe = intel_crtc->pipe;
  2586. int plane = intel_crtc->plane;
  2587. if (intel_crtc->active)
  2588. return;
  2589. intel_crtc->active = true;
  2590. intel_update_watermarks(dev);
  2591. intel_enable_pll(dev_priv, pipe);
  2592. intel_enable_pipe(dev_priv, pipe, false);
  2593. intel_enable_plane(dev_priv, plane, pipe);
  2594. intel_crtc_load_lut(crtc);
  2595. intel_update_fbc(dev);
  2596. /* Give the overlay scaler a chance to enable if it's on this pipe */
  2597. intel_crtc_dpms_overlay(intel_crtc, true);
  2598. intel_crtc_update_cursor(crtc, true);
  2599. }
  2600. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  2601. {
  2602. struct drm_device *dev = crtc->dev;
  2603. struct drm_i915_private *dev_priv = dev->dev_private;
  2604. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2605. int pipe = intel_crtc->pipe;
  2606. int plane = intel_crtc->plane;
  2607. if (!intel_crtc->active)
  2608. return;
  2609. /* Give the overlay scaler a chance to disable if it's on this pipe */
  2610. intel_crtc_wait_for_pending_flips(crtc);
  2611. drm_vblank_off(dev, pipe);
  2612. intel_crtc_dpms_overlay(intel_crtc, false);
  2613. intel_crtc_update_cursor(crtc, false);
  2614. if (dev_priv->cfb_plane == plane)
  2615. intel_disable_fbc(dev);
  2616. intel_disable_plane(dev_priv, plane, pipe);
  2617. intel_disable_pipe(dev_priv, pipe);
  2618. intel_disable_pll(dev_priv, pipe);
  2619. intel_crtc->active = false;
  2620. intel_update_fbc(dev);
  2621. intel_update_watermarks(dev);
  2622. intel_clear_scanline_wait(dev);
  2623. }
  2624. static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
  2625. {
  2626. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2627. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2628. */
  2629. switch (mode) {
  2630. case DRM_MODE_DPMS_ON:
  2631. case DRM_MODE_DPMS_STANDBY:
  2632. case DRM_MODE_DPMS_SUSPEND:
  2633. i9xx_crtc_enable(crtc);
  2634. break;
  2635. case DRM_MODE_DPMS_OFF:
  2636. i9xx_crtc_disable(crtc);
  2637. break;
  2638. }
  2639. }
  2640. static void i9xx_crtc_off(struct drm_crtc *crtc)
  2641. {
  2642. }
  2643. /**
  2644. * Sets the power management mode of the pipe and plane.
  2645. */
  2646. static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
  2647. {
  2648. struct drm_device *dev = crtc->dev;
  2649. struct drm_i915_private *dev_priv = dev->dev_private;
  2650. struct drm_i915_master_private *master_priv;
  2651. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2652. int pipe = intel_crtc->pipe;
  2653. bool enabled;
  2654. if (intel_crtc->dpms_mode == mode)
  2655. return;
  2656. intel_crtc->dpms_mode = mode;
  2657. dev_priv->display.dpms(crtc, mode);
  2658. if (!dev->primary->master)
  2659. return;
  2660. master_priv = dev->primary->master->driver_priv;
  2661. if (!master_priv->sarea_priv)
  2662. return;
  2663. enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
  2664. switch (pipe) {
  2665. case 0:
  2666. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  2667. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  2668. break;
  2669. case 1:
  2670. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  2671. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  2672. break;
  2673. default:
  2674. DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
  2675. break;
  2676. }
  2677. }
  2678. static void intel_crtc_disable(struct drm_crtc *crtc)
  2679. {
  2680. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  2681. struct drm_device *dev = crtc->dev;
  2682. struct drm_i915_private *dev_priv = dev->dev_private;
  2683. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  2684. dev_priv->display.off(crtc);
  2685. assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
  2686. assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
  2687. if (crtc->fb) {
  2688. mutex_lock(&dev->struct_mutex);
  2689. intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
  2690. mutex_unlock(&dev->struct_mutex);
  2691. }
  2692. }
  2693. /* Prepare for a mode set.
  2694. *
  2695. * Note we could be a lot smarter here. We need to figure out which outputs
  2696. * will be enabled, which disabled (in short, how the config will changes)
  2697. * and perform the minimum necessary steps to accomplish that, e.g. updating
  2698. * watermarks, FBC configuration, making sure PLLs are programmed correctly,
  2699. * panel fitting is in the proper state, etc.
  2700. */
  2701. static void i9xx_crtc_prepare(struct drm_crtc *crtc)
  2702. {
  2703. i9xx_crtc_disable(crtc);
  2704. }
  2705. static void i9xx_crtc_commit(struct drm_crtc *crtc)
  2706. {
  2707. i9xx_crtc_enable(crtc);
  2708. }
  2709. static void ironlake_crtc_prepare(struct drm_crtc *crtc)
  2710. {
  2711. ironlake_crtc_disable(crtc);
  2712. }
  2713. static void ironlake_crtc_commit(struct drm_crtc *crtc)
  2714. {
  2715. ironlake_crtc_enable(crtc);
  2716. }
  2717. void intel_encoder_prepare(struct drm_encoder *encoder)
  2718. {
  2719. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2720. /* lvds has its own version of prepare see intel_lvds_prepare */
  2721. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  2722. }
  2723. void intel_encoder_commit(struct drm_encoder *encoder)
  2724. {
  2725. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2726. struct drm_device *dev = encoder->dev;
  2727. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  2728. struct intel_crtc *intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  2729. /* lvds has its own version of commit see intel_lvds_commit */
  2730. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  2731. if (HAS_PCH_CPT(dev))
  2732. intel_cpt_verify_modeset(dev, intel_crtc->pipe);
  2733. }
  2734. void intel_encoder_destroy(struct drm_encoder *encoder)
  2735. {
  2736. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  2737. drm_encoder_cleanup(encoder);
  2738. kfree(intel_encoder);
  2739. }
  2740. static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
  2741. struct drm_display_mode *mode,
  2742. struct drm_display_mode *adjusted_mode)
  2743. {
  2744. struct drm_device *dev = crtc->dev;
  2745. if (HAS_PCH_SPLIT(dev)) {
  2746. /* FDI link clock is fixed at 2.7G */
  2747. if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
  2748. return false;
  2749. }
  2750. /* All interlaced capable intel hw wants timings in frames. */
  2751. drm_mode_set_crtcinfo(adjusted_mode, 0);
  2752. return true;
  2753. }
  2754. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  2755. {
  2756. return 400000; /* FIXME */
  2757. }
  2758. static int i945_get_display_clock_speed(struct drm_device *dev)
  2759. {
  2760. return 400000;
  2761. }
  2762. static int i915_get_display_clock_speed(struct drm_device *dev)
  2763. {
  2764. return 333000;
  2765. }
  2766. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  2767. {
  2768. return 200000;
  2769. }
  2770. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  2771. {
  2772. u16 gcfgc = 0;
  2773. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  2774. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  2775. return 133000;
  2776. else {
  2777. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  2778. case GC_DISPLAY_CLOCK_333_MHZ:
  2779. return 333000;
  2780. default:
  2781. case GC_DISPLAY_CLOCK_190_200_MHZ:
  2782. return 190000;
  2783. }
  2784. }
  2785. }
  2786. static int i865_get_display_clock_speed(struct drm_device *dev)
  2787. {
  2788. return 266000;
  2789. }
  2790. static int i855_get_display_clock_speed(struct drm_device *dev)
  2791. {
  2792. u16 hpllcc = 0;
  2793. /* Assume that the hardware is in the high speed state. This
  2794. * should be the default.
  2795. */
  2796. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  2797. case GC_CLOCK_133_200:
  2798. case GC_CLOCK_100_200:
  2799. return 200000;
  2800. case GC_CLOCK_166_250:
  2801. return 250000;
  2802. case GC_CLOCK_100_133:
  2803. return 133000;
  2804. }
  2805. /* Shouldn't happen */
  2806. return 0;
  2807. }
  2808. static int i830_get_display_clock_speed(struct drm_device *dev)
  2809. {
  2810. return 133000;
  2811. }
  2812. struct fdi_m_n {
  2813. u32 tu;
  2814. u32 gmch_m;
  2815. u32 gmch_n;
  2816. u32 link_m;
  2817. u32 link_n;
  2818. };
  2819. static void
  2820. fdi_reduce_ratio(u32 *num, u32 *den)
  2821. {
  2822. while (*num > 0xffffff || *den > 0xffffff) {
  2823. *num >>= 1;
  2824. *den >>= 1;
  2825. }
  2826. }
  2827. static void
  2828. ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
  2829. int link_clock, struct fdi_m_n *m_n)
  2830. {
  2831. m_n->tu = 64; /* default size */
  2832. /* BUG_ON(pixel_clock > INT_MAX / 36); */
  2833. m_n->gmch_m = bits_per_pixel * pixel_clock;
  2834. m_n->gmch_n = link_clock * nlanes * 8;
  2835. fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
  2836. m_n->link_m = pixel_clock;
  2837. m_n->link_n = link_clock;
  2838. fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
  2839. }
  2840. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  2841. {
  2842. if (i915_panel_use_ssc >= 0)
  2843. return i915_panel_use_ssc != 0;
  2844. return dev_priv->lvds_use_ssc
  2845. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  2846. }
  2847. /**
  2848. * intel_choose_pipe_bpp_dither - figure out what color depth the pipe should send
  2849. * @crtc: CRTC structure
  2850. * @mode: requested mode
  2851. *
  2852. * A pipe may be connected to one or more outputs. Based on the depth of the
  2853. * attached framebuffer, choose a good color depth to use on the pipe.
  2854. *
  2855. * If possible, match the pipe depth to the fb depth. In some cases, this
  2856. * isn't ideal, because the connected output supports a lesser or restricted
  2857. * set of depths. Resolve that here:
  2858. * LVDS typically supports only 6bpc, so clamp down in that case
  2859. * HDMI supports only 8bpc or 12bpc, so clamp to 8bpc with dither for 10bpc
  2860. * Displays may support a restricted set as well, check EDID and clamp as
  2861. * appropriate.
  2862. * DP may want to dither down to 6bpc to fit larger modes
  2863. *
  2864. * RETURNS:
  2865. * Dithering requirement (i.e. false if display bpc and pipe bpc match,
  2866. * true if they don't match).
  2867. */
  2868. static bool intel_choose_pipe_bpp_dither(struct drm_crtc *crtc,
  2869. unsigned int *pipe_bpp,
  2870. struct drm_display_mode *mode)
  2871. {
  2872. struct drm_device *dev = crtc->dev;
  2873. struct drm_i915_private *dev_priv = dev->dev_private;
  2874. struct drm_encoder *encoder;
  2875. struct drm_connector *connector;
  2876. unsigned int display_bpc = UINT_MAX, bpc;
  2877. /* Walk the encoders & connectors on this crtc, get min bpc */
  2878. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  2879. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  2880. if (encoder->crtc != crtc)
  2881. continue;
  2882. if (intel_encoder->type == INTEL_OUTPUT_LVDS) {
  2883. unsigned int lvds_bpc;
  2884. if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) ==
  2885. LVDS_A3_POWER_UP)
  2886. lvds_bpc = 8;
  2887. else
  2888. lvds_bpc = 6;
  2889. if (lvds_bpc < display_bpc) {
  2890. DRM_DEBUG_KMS("clamping display bpc (was %d) to LVDS (%d)\n", display_bpc, lvds_bpc);
  2891. display_bpc = lvds_bpc;
  2892. }
  2893. continue;
  2894. }
  2895. if (intel_encoder->type == INTEL_OUTPUT_EDP) {
  2896. /* Use VBT settings if we have an eDP panel */
  2897. unsigned int edp_bpc = dev_priv->edp.bpp / 3;
  2898. if (edp_bpc < display_bpc) {
  2899. DRM_DEBUG_KMS("clamping display bpc (was %d) to eDP (%d)\n", display_bpc, edp_bpc);
  2900. display_bpc = edp_bpc;
  2901. }
  2902. continue;
  2903. }
  2904. /* Not one of the known troublemakers, check the EDID */
  2905. list_for_each_entry(connector, &dev->mode_config.connector_list,
  2906. head) {
  2907. if (connector->encoder != encoder)
  2908. continue;
  2909. /* Don't use an invalid EDID bpc value */
  2910. if (connector->display_info.bpc &&
  2911. connector->display_info.bpc < display_bpc) {
  2912. DRM_DEBUG_KMS("clamping display bpc (was %d) to EDID reported max of %d\n", display_bpc, connector->display_info.bpc);
  2913. display_bpc = connector->display_info.bpc;
  2914. }
  2915. }
  2916. /*
  2917. * HDMI is either 12 or 8, so if the display lets 10bpc sneak
  2918. * through, clamp it down. (Note: >12bpc will be caught below.)
  2919. */
  2920. if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
  2921. if (display_bpc > 8 && display_bpc < 12) {
  2922. DRM_DEBUG_KMS("forcing bpc to 12 for HDMI\n");
  2923. display_bpc = 12;
  2924. } else {
  2925. DRM_DEBUG_KMS("forcing bpc to 8 for HDMI\n");
  2926. display_bpc = 8;
  2927. }
  2928. }
  2929. }
  2930. if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
  2931. DRM_DEBUG_KMS("Dithering DP to 6bpc\n");
  2932. display_bpc = 6;
  2933. }
  2934. /*
  2935. * We could just drive the pipe at the highest bpc all the time and
  2936. * enable dithering as needed, but that costs bandwidth. So choose
  2937. * the minimum value that expresses the full color range of the fb but
  2938. * also stays within the max display bpc discovered above.
  2939. */
  2940. switch (crtc->fb->depth) {
  2941. case 8:
  2942. bpc = 8; /* since we go through a colormap */
  2943. break;
  2944. case 15:
  2945. case 16:
  2946. bpc = 6; /* min is 18bpp */
  2947. break;
  2948. case 24:
  2949. bpc = 8;
  2950. break;
  2951. case 30:
  2952. bpc = 10;
  2953. break;
  2954. case 48:
  2955. bpc = 12;
  2956. break;
  2957. default:
  2958. DRM_DEBUG("unsupported depth, assuming 24 bits\n");
  2959. bpc = min((unsigned int)8, display_bpc);
  2960. break;
  2961. }
  2962. display_bpc = min(display_bpc, bpc);
  2963. DRM_DEBUG_KMS("setting pipe bpc to %d (max display bpc %d)\n",
  2964. bpc, display_bpc);
  2965. *pipe_bpp = display_bpc * 3;
  2966. return display_bpc != bpc;
  2967. }
  2968. static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
  2969. {
  2970. struct drm_device *dev = crtc->dev;
  2971. struct drm_i915_private *dev_priv = dev->dev_private;
  2972. int refclk;
  2973. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  2974. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  2975. refclk = dev_priv->lvds_ssc_freq * 1000;
  2976. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  2977. refclk / 1000);
  2978. } else if (!IS_GEN2(dev)) {
  2979. refclk = 96000;
  2980. } else {
  2981. refclk = 48000;
  2982. }
  2983. return refclk;
  2984. }
  2985. static void i9xx_adjust_sdvo_tv_clock(struct drm_display_mode *adjusted_mode,
  2986. intel_clock_t *clock)
  2987. {
  2988. /* SDVO TV has fixed PLL values depend on its clock range,
  2989. this mirrors vbios setting. */
  2990. if (adjusted_mode->clock >= 100000
  2991. && adjusted_mode->clock < 140500) {
  2992. clock->p1 = 2;
  2993. clock->p2 = 10;
  2994. clock->n = 3;
  2995. clock->m1 = 16;
  2996. clock->m2 = 8;
  2997. } else if (adjusted_mode->clock >= 140500
  2998. && adjusted_mode->clock <= 200000) {
  2999. clock->p1 = 1;
  3000. clock->p2 = 10;
  3001. clock->n = 6;
  3002. clock->m1 = 12;
  3003. clock->m2 = 8;
  3004. }
  3005. }
  3006. static void i9xx_update_pll_dividers(struct drm_crtc *crtc,
  3007. intel_clock_t *clock,
  3008. intel_clock_t *reduced_clock)
  3009. {
  3010. struct drm_device *dev = crtc->dev;
  3011. struct drm_i915_private *dev_priv = dev->dev_private;
  3012. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3013. int pipe = intel_crtc->pipe;
  3014. u32 fp, fp2 = 0;
  3015. if (IS_PINEVIEW(dev)) {
  3016. fp = (1 << clock->n) << 16 | clock->m1 << 8 | clock->m2;
  3017. if (reduced_clock)
  3018. fp2 = (1 << reduced_clock->n) << 16 |
  3019. reduced_clock->m1 << 8 | reduced_clock->m2;
  3020. } else {
  3021. fp = clock->n << 16 | clock->m1 << 8 | clock->m2;
  3022. if (reduced_clock)
  3023. fp2 = reduced_clock->n << 16 | reduced_clock->m1 << 8 |
  3024. reduced_clock->m2;
  3025. }
  3026. I915_WRITE(FP0(pipe), fp);
  3027. intel_crtc->lowfreq_avail = false;
  3028. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  3029. reduced_clock && i915_powersave) {
  3030. I915_WRITE(FP1(pipe), fp2);
  3031. intel_crtc->lowfreq_avail = true;
  3032. } else {
  3033. I915_WRITE(FP1(pipe), fp);
  3034. }
  3035. }
  3036. static void intel_update_lvds(struct drm_crtc *crtc, intel_clock_t *clock,
  3037. struct drm_display_mode *adjusted_mode)
  3038. {
  3039. struct drm_device *dev = crtc->dev;
  3040. struct drm_i915_private *dev_priv = dev->dev_private;
  3041. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3042. int pipe = intel_crtc->pipe;
  3043. u32 temp;
  3044. temp = I915_READ(LVDS);
  3045. temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  3046. if (pipe == 1) {
  3047. temp |= LVDS_PIPEB_SELECT;
  3048. } else {
  3049. temp &= ~LVDS_PIPEB_SELECT;
  3050. }
  3051. /* set the corresponsding LVDS_BORDER bit */
  3052. temp |= dev_priv->lvds_border_bits;
  3053. /* Set the B0-B3 data pairs corresponding to whether we're going to
  3054. * set the DPLLs for dual-channel mode or not.
  3055. */
  3056. if (clock->p2 == 7)
  3057. temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  3058. else
  3059. temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  3060. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  3061. * appropriately here, but we need to look more thoroughly into how
  3062. * panels behave in the two modes.
  3063. */
  3064. /* set the dithering flag on LVDS as needed */
  3065. if (INTEL_INFO(dev)->gen >= 4) {
  3066. if (dev_priv->lvds_dither)
  3067. temp |= LVDS_ENABLE_DITHER;
  3068. else
  3069. temp &= ~LVDS_ENABLE_DITHER;
  3070. }
  3071. temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
  3072. if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
  3073. temp |= LVDS_HSYNC_POLARITY;
  3074. if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
  3075. temp |= LVDS_VSYNC_POLARITY;
  3076. I915_WRITE(LVDS, temp);
  3077. }
  3078. static void i9xx_update_pll(struct drm_crtc *crtc,
  3079. struct drm_display_mode *mode,
  3080. struct drm_display_mode *adjusted_mode,
  3081. intel_clock_t *clock, intel_clock_t *reduced_clock,
  3082. int num_connectors)
  3083. {
  3084. struct drm_device *dev = crtc->dev;
  3085. struct drm_i915_private *dev_priv = dev->dev_private;
  3086. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3087. int pipe = intel_crtc->pipe;
  3088. u32 dpll;
  3089. bool is_sdvo;
  3090. is_sdvo = intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO) ||
  3091. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
  3092. dpll = DPLL_VGA_MODE_DIS;
  3093. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  3094. dpll |= DPLLB_MODE_LVDS;
  3095. else
  3096. dpll |= DPLLB_MODE_DAC_SERIAL;
  3097. if (is_sdvo) {
  3098. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  3099. if (pixel_multiplier > 1) {
  3100. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  3101. dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
  3102. }
  3103. dpll |= DPLL_DVO_HIGH_SPEED;
  3104. }
  3105. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
  3106. dpll |= DPLL_DVO_HIGH_SPEED;
  3107. /* compute bitmask from p1 value */
  3108. if (IS_PINEVIEW(dev))
  3109. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  3110. else {
  3111. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3112. if (IS_G4X(dev) && reduced_clock)
  3113. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  3114. }
  3115. switch (clock->p2) {
  3116. case 5:
  3117. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  3118. break;
  3119. case 7:
  3120. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  3121. break;
  3122. case 10:
  3123. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  3124. break;
  3125. case 14:
  3126. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  3127. break;
  3128. }
  3129. if (INTEL_INFO(dev)->gen >= 4)
  3130. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  3131. if (is_sdvo && intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
  3132. dpll |= PLL_REF_INPUT_TVCLKINBC;
  3133. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
  3134. /* XXX: just matching BIOS for now */
  3135. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  3136. dpll |= 3;
  3137. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  3138. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  3139. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  3140. else
  3141. dpll |= PLL_REF_INPUT_DREFCLK;
  3142. dpll |= DPLL_VCO_ENABLE;
  3143. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
  3144. POSTING_READ(DPLL(pipe));
  3145. udelay(150);
  3146. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  3147. * This is an exception to the general rule that mode_set doesn't turn
  3148. * things on.
  3149. */
  3150. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  3151. intel_update_lvds(crtc, clock, adjusted_mode);
  3152. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
  3153. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  3154. I915_WRITE(DPLL(pipe), dpll);
  3155. /* Wait for the clocks to stabilize. */
  3156. POSTING_READ(DPLL(pipe));
  3157. udelay(150);
  3158. if (INTEL_INFO(dev)->gen >= 4) {
  3159. u32 temp = 0;
  3160. if (is_sdvo) {
  3161. temp = intel_mode_get_pixel_multiplier(adjusted_mode);
  3162. if (temp > 1)
  3163. temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  3164. else
  3165. temp = 0;
  3166. }
  3167. I915_WRITE(DPLL_MD(pipe), temp);
  3168. } else {
  3169. /* The pixel multiplier can only be updated once the
  3170. * DPLL is enabled and the clocks are stable.
  3171. *
  3172. * So write it again.
  3173. */
  3174. I915_WRITE(DPLL(pipe), dpll);
  3175. }
  3176. }
  3177. static void i8xx_update_pll(struct drm_crtc *crtc,
  3178. struct drm_display_mode *adjusted_mode,
  3179. intel_clock_t *clock,
  3180. int num_connectors)
  3181. {
  3182. struct drm_device *dev = crtc->dev;
  3183. struct drm_i915_private *dev_priv = dev->dev_private;
  3184. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3185. int pipe = intel_crtc->pipe;
  3186. u32 dpll;
  3187. dpll = DPLL_VGA_MODE_DIS;
  3188. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  3189. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3190. } else {
  3191. if (clock->p1 == 2)
  3192. dpll |= PLL_P1_DIVIDE_BY_TWO;
  3193. else
  3194. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3195. if (clock->p2 == 4)
  3196. dpll |= PLL_P2_DIVIDE_BY_4;
  3197. }
  3198. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
  3199. /* XXX: just matching BIOS for now */
  3200. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  3201. dpll |= 3;
  3202. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  3203. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  3204. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  3205. else
  3206. dpll |= PLL_REF_INPUT_DREFCLK;
  3207. dpll |= DPLL_VCO_ENABLE;
  3208. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
  3209. POSTING_READ(DPLL(pipe));
  3210. udelay(150);
  3211. I915_WRITE(DPLL(pipe), dpll);
  3212. /* Wait for the clocks to stabilize. */
  3213. POSTING_READ(DPLL(pipe));
  3214. udelay(150);
  3215. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  3216. * This is an exception to the general rule that mode_set doesn't turn
  3217. * things on.
  3218. */
  3219. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  3220. intel_update_lvds(crtc, clock, adjusted_mode);
  3221. /* The pixel multiplier can only be updated once the
  3222. * DPLL is enabled and the clocks are stable.
  3223. *
  3224. * So write it again.
  3225. */
  3226. I915_WRITE(DPLL(pipe), dpll);
  3227. }
  3228. static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
  3229. struct drm_display_mode *mode,
  3230. struct drm_display_mode *adjusted_mode,
  3231. int x, int y,
  3232. struct drm_framebuffer *old_fb)
  3233. {
  3234. struct drm_device *dev = crtc->dev;
  3235. struct drm_i915_private *dev_priv = dev->dev_private;
  3236. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3237. int pipe = intel_crtc->pipe;
  3238. int plane = intel_crtc->plane;
  3239. int refclk, num_connectors = 0;
  3240. intel_clock_t clock, reduced_clock;
  3241. u32 dspcntr, pipeconf, vsyncshift;
  3242. bool ok, has_reduced_clock = false, is_sdvo = false;
  3243. bool is_lvds = false, is_tv = false, is_dp = false;
  3244. struct drm_mode_config *mode_config = &dev->mode_config;
  3245. struct intel_encoder *encoder;
  3246. const intel_limit_t *limit;
  3247. int ret;
  3248. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  3249. if (encoder->base.crtc != crtc)
  3250. continue;
  3251. switch (encoder->type) {
  3252. case INTEL_OUTPUT_LVDS:
  3253. is_lvds = true;
  3254. break;
  3255. case INTEL_OUTPUT_SDVO:
  3256. case INTEL_OUTPUT_HDMI:
  3257. is_sdvo = true;
  3258. if (encoder->needs_tv_clock)
  3259. is_tv = true;
  3260. break;
  3261. case INTEL_OUTPUT_TVOUT:
  3262. is_tv = true;
  3263. break;
  3264. case INTEL_OUTPUT_DISPLAYPORT:
  3265. is_dp = true;
  3266. break;
  3267. }
  3268. num_connectors++;
  3269. }
  3270. refclk = i9xx_get_refclk(crtc, num_connectors);
  3271. /*
  3272. * Returns a set of divisors for the desired target clock with the given
  3273. * refclk, or FALSE. The returned values represent the clock equation:
  3274. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  3275. */
  3276. limit = intel_limit(crtc, refclk);
  3277. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
  3278. &clock);
  3279. if (!ok) {
  3280. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  3281. return -EINVAL;
  3282. }
  3283. /* Ensure that the cursor is valid for the new mode before changing... */
  3284. intel_crtc_update_cursor(crtc, true);
  3285. if (is_lvds && dev_priv->lvds_downclock_avail) {
  3286. /*
  3287. * Ensure we match the reduced clock's P to the target clock.
  3288. * If the clocks don't match, we can't switch the display clock
  3289. * by using the FP0/FP1. In such case we will disable the LVDS
  3290. * downclock feature.
  3291. */
  3292. has_reduced_clock = limit->find_pll(limit, crtc,
  3293. dev_priv->lvds_downclock,
  3294. refclk,
  3295. &clock,
  3296. &reduced_clock);
  3297. }
  3298. if (is_sdvo && is_tv)
  3299. i9xx_adjust_sdvo_tv_clock(adjusted_mode, &clock);
  3300. i9xx_update_pll_dividers(crtc, &clock, has_reduced_clock ?
  3301. &reduced_clock : NULL);
  3302. if (IS_GEN2(dev))
  3303. i8xx_update_pll(crtc, adjusted_mode, &clock, num_connectors);
  3304. else
  3305. i9xx_update_pll(crtc, mode, adjusted_mode, &clock,
  3306. has_reduced_clock ? &reduced_clock : NULL,
  3307. num_connectors);
  3308. /* setup pipeconf */
  3309. pipeconf = I915_READ(PIPECONF(pipe));
  3310. /* Set up the display plane register */
  3311. dspcntr = DISPPLANE_GAMMA_ENABLE;
  3312. if (pipe == 0)
  3313. dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
  3314. else
  3315. dspcntr |= DISPPLANE_SEL_PIPE_B;
  3316. if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
  3317. /* Enable pixel doubling when the dot clock is > 90% of the (display)
  3318. * core speed.
  3319. *
  3320. * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
  3321. * pipe == 0 check?
  3322. */
  3323. if (mode->clock >
  3324. dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
  3325. pipeconf |= PIPECONF_DOUBLE_WIDE;
  3326. else
  3327. pipeconf &= ~PIPECONF_DOUBLE_WIDE;
  3328. }
  3329. /* default to 8bpc */
  3330. pipeconf &= ~(PIPECONF_BPP_MASK | PIPECONF_DITHER_EN);
  3331. if (is_dp) {
  3332. if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
  3333. pipeconf |= PIPECONF_BPP_6 |
  3334. PIPECONF_DITHER_EN |
  3335. PIPECONF_DITHER_TYPE_SP;
  3336. }
  3337. }
  3338. DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  3339. drm_mode_debug_printmodeline(mode);
  3340. if (HAS_PIPE_CXSR(dev)) {
  3341. if (intel_crtc->lowfreq_avail) {
  3342. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  3343. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  3344. } else {
  3345. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  3346. pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
  3347. }
  3348. }
  3349. pipeconf &= ~PIPECONF_INTERLACE_MASK;
  3350. if (!IS_GEN2(dev) &&
  3351. adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  3352. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  3353. /* the chip adds 2 halflines automatically */
  3354. adjusted_mode->crtc_vtotal -= 1;
  3355. adjusted_mode->crtc_vblank_end -= 1;
  3356. vsyncshift = adjusted_mode->crtc_hsync_start
  3357. - adjusted_mode->crtc_htotal/2;
  3358. } else {
  3359. pipeconf |= PIPECONF_PROGRESSIVE;
  3360. vsyncshift = 0;
  3361. }
  3362. if (!IS_GEN3(dev))
  3363. I915_WRITE(VSYNCSHIFT(pipe), vsyncshift);
  3364. I915_WRITE(HTOTAL(pipe),
  3365. (adjusted_mode->crtc_hdisplay - 1) |
  3366. ((adjusted_mode->crtc_htotal - 1) << 16));
  3367. I915_WRITE(HBLANK(pipe),
  3368. (adjusted_mode->crtc_hblank_start - 1) |
  3369. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  3370. I915_WRITE(HSYNC(pipe),
  3371. (adjusted_mode->crtc_hsync_start - 1) |
  3372. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  3373. I915_WRITE(VTOTAL(pipe),
  3374. (adjusted_mode->crtc_vdisplay - 1) |
  3375. ((adjusted_mode->crtc_vtotal - 1) << 16));
  3376. I915_WRITE(VBLANK(pipe),
  3377. (adjusted_mode->crtc_vblank_start - 1) |
  3378. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  3379. I915_WRITE(VSYNC(pipe),
  3380. (adjusted_mode->crtc_vsync_start - 1) |
  3381. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  3382. /* pipesrc and dspsize control the size that is scaled from,
  3383. * which should always be the user's requested size.
  3384. */
  3385. I915_WRITE(DSPSIZE(plane),
  3386. ((mode->vdisplay - 1) << 16) |
  3387. (mode->hdisplay - 1));
  3388. I915_WRITE(DSPPOS(plane), 0);
  3389. I915_WRITE(PIPESRC(pipe),
  3390. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  3391. I915_WRITE(PIPECONF(pipe), pipeconf);
  3392. POSTING_READ(PIPECONF(pipe));
  3393. intel_enable_pipe(dev_priv, pipe, false);
  3394. intel_wait_for_vblank(dev, pipe);
  3395. I915_WRITE(DSPCNTR(plane), dspcntr);
  3396. POSTING_READ(DSPCNTR(plane));
  3397. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  3398. intel_update_watermarks(dev);
  3399. return ret;
  3400. }
  3401. /*
  3402. * Initialize reference clocks when the driver loads
  3403. */
  3404. void ironlake_init_pch_refclk(struct drm_device *dev)
  3405. {
  3406. struct drm_i915_private *dev_priv = dev->dev_private;
  3407. struct drm_mode_config *mode_config = &dev->mode_config;
  3408. struct intel_encoder *encoder;
  3409. u32 temp;
  3410. bool has_lvds = false;
  3411. bool has_cpu_edp = false;
  3412. bool has_pch_edp = false;
  3413. bool has_panel = false;
  3414. bool has_ck505 = false;
  3415. bool can_ssc = false;
  3416. /* We need to take the global config into account */
  3417. list_for_each_entry(encoder, &mode_config->encoder_list,
  3418. base.head) {
  3419. switch (encoder->type) {
  3420. case INTEL_OUTPUT_LVDS:
  3421. has_panel = true;
  3422. has_lvds = true;
  3423. break;
  3424. case INTEL_OUTPUT_EDP:
  3425. has_panel = true;
  3426. if (intel_encoder_is_pch_edp(&encoder->base))
  3427. has_pch_edp = true;
  3428. else
  3429. has_cpu_edp = true;
  3430. break;
  3431. }
  3432. }
  3433. if (HAS_PCH_IBX(dev)) {
  3434. has_ck505 = dev_priv->display_clock_mode;
  3435. can_ssc = has_ck505;
  3436. } else {
  3437. has_ck505 = false;
  3438. can_ssc = true;
  3439. }
  3440. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_pch_edp %d has_cpu_edp %d has_ck505 %d\n",
  3441. has_panel, has_lvds, has_pch_edp, has_cpu_edp,
  3442. has_ck505);
  3443. /* Ironlake: try to setup display ref clock before DPLL
  3444. * enabling. This is only under driver's control after
  3445. * PCH B stepping, previous chipset stepping should be
  3446. * ignoring this setting.
  3447. */
  3448. temp = I915_READ(PCH_DREF_CONTROL);
  3449. /* Always enable nonspread source */
  3450. temp &= ~DREF_NONSPREAD_SOURCE_MASK;
  3451. if (has_ck505)
  3452. temp |= DREF_NONSPREAD_CK505_ENABLE;
  3453. else
  3454. temp |= DREF_NONSPREAD_SOURCE_ENABLE;
  3455. if (has_panel) {
  3456. temp &= ~DREF_SSC_SOURCE_MASK;
  3457. temp |= DREF_SSC_SOURCE_ENABLE;
  3458. /* SSC must be turned on before enabling the CPU output */
  3459. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  3460. DRM_DEBUG_KMS("Using SSC on panel\n");
  3461. temp |= DREF_SSC1_ENABLE;
  3462. } else
  3463. temp &= ~DREF_SSC1_ENABLE;
  3464. /* Get SSC going before enabling the outputs */
  3465. I915_WRITE(PCH_DREF_CONTROL, temp);
  3466. POSTING_READ(PCH_DREF_CONTROL);
  3467. udelay(200);
  3468. temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  3469. /* Enable CPU source on CPU attached eDP */
  3470. if (has_cpu_edp) {
  3471. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  3472. DRM_DEBUG_KMS("Using SSC on eDP\n");
  3473. temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  3474. }
  3475. else
  3476. temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  3477. } else
  3478. temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  3479. I915_WRITE(PCH_DREF_CONTROL, temp);
  3480. POSTING_READ(PCH_DREF_CONTROL);
  3481. udelay(200);
  3482. } else {
  3483. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  3484. temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  3485. /* Turn off CPU output */
  3486. temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  3487. I915_WRITE(PCH_DREF_CONTROL, temp);
  3488. POSTING_READ(PCH_DREF_CONTROL);
  3489. udelay(200);
  3490. /* Turn off the SSC source */
  3491. temp &= ~DREF_SSC_SOURCE_MASK;
  3492. temp |= DREF_SSC_SOURCE_DISABLE;
  3493. /* Turn off SSC1 */
  3494. temp &= ~ DREF_SSC1_ENABLE;
  3495. I915_WRITE(PCH_DREF_CONTROL, temp);
  3496. POSTING_READ(PCH_DREF_CONTROL);
  3497. udelay(200);
  3498. }
  3499. }
  3500. static int ironlake_get_refclk(struct drm_crtc *crtc)
  3501. {
  3502. struct drm_device *dev = crtc->dev;
  3503. struct drm_i915_private *dev_priv = dev->dev_private;
  3504. struct intel_encoder *encoder;
  3505. struct drm_mode_config *mode_config = &dev->mode_config;
  3506. struct intel_encoder *edp_encoder = NULL;
  3507. int num_connectors = 0;
  3508. bool is_lvds = false;
  3509. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  3510. if (encoder->base.crtc != crtc)
  3511. continue;
  3512. switch (encoder->type) {
  3513. case INTEL_OUTPUT_LVDS:
  3514. is_lvds = true;
  3515. break;
  3516. case INTEL_OUTPUT_EDP:
  3517. edp_encoder = encoder;
  3518. break;
  3519. }
  3520. num_connectors++;
  3521. }
  3522. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  3523. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  3524. dev_priv->lvds_ssc_freq);
  3525. return dev_priv->lvds_ssc_freq * 1000;
  3526. }
  3527. return 120000;
  3528. }
  3529. static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
  3530. struct drm_display_mode *mode,
  3531. struct drm_display_mode *adjusted_mode,
  3532. int x, int y,
  3533. struct drm_framebuffer *old_fb)
  3534. {
  3535. struct drm_device *dev = crtc->dev;
  3536. struct drm_i915_private *dev_priv = dev->dev_private;
  3537. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3538. int pipe = intel_crtc->pipe;
  3539. int plane = intel_crtc->plane;
  3540. int refclk, num_connectors = 0;
  3541. intel_clock_t clock, reduced_clock;
  3542. u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
  3543. bool ok, has_reduced_clock = false, is_sdvo = false;
  3544. bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
  3545. struct drm_mode_config *mode_config = &dev->mode_config;
  3546. struct intel_encoder *encoder, *edp_encoder = NULL;
  3547. const intel_limit_t *limit;
  3548. int ret;
  3549. struct fdi_m_n m_n = {0};
  3550. u32 temp;
  3551. int target_clock, pixel_multiplier, lane, link_bw, factor;
  3552. unsigned int pipe_bpp;
  3553. bool dither;
  3554. bool is_cpu_edp = false, is_pch_edp = false;
  3555. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  3556. if (encoder->base.crtc != crtc)
  3557. continue;
  3558. switch (encoder->type) {
  3559. case INTEL_OUTPUT_LVDS:
  3560. is_lvds = true;
  3561. break;
  3562. case INTEL_OUTPUT_SDVO:
  3563. case INTEL_OUTPUT_HDMI:
  3564. is_sdvo = true;
  3565. if (encoder->needs_tv_clock)
  3566. is_tv = true;
  3567. break;
  3568. case INTEL_OUTPUT_TVOUT:
  3569. is_tv = true;
  3570. break;
  3571. case INTEL_OUTPUT_ANALOG:
  3572. is_crt = true;
  3573. break;
  3574. case INTEL_OUTPUT_DISPLAYPORT:
  3575. is_dp = true;
  3576. break;
  3577. case INTEL_OUTPUT_EDP:
  3578. is_dp = true;
  3579. if (intel_encoder_is_pch_edp(&encoder->base))
  3580. is_pch_edp = true;
  3581. else
  3582. is_cpu_edp = true;
  3583. edp_encoder = encoder;
  3584. break;
  3585. }
  3586. num_connectors++;
  3587. }
  3588. refclk = ironlake_get_refclk(crtc);
  3589. /*
  3590. * Returns a set of divisors for the desired target clock with the given
  3591. * refclk, or FALSE. The returned values represent the clock equation:
  3592. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  3593. */
  3594. limit = intel_limit(crtc, refclk);
  3595. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
  3596. &clock);
  3597. if (!ok) {
  3598. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  3599. return -EINVAL;
  3600. }
  3601. /* Ensure that the cursor is valid for the new mode before changing... */
  3602. intel_crtc_update_cursor(crtc, true);
  3603. if (is_lvds && dev_priv->lvds_downclock_avail) {
  3604. /*
  3605. * Ensure we match the reduced clock's P to the target clock.
  3606. * If the clocks don't match, we can't switch the display clock
  3607. * by using the FP0/FP1. In such case we will disable the LVDS
  3608. * downclock feature.
  3609. */
  3610. has_reduced_clock = limit->find_pll(limit, crtc,
  3611. dev_priv->lvds_downclock,
  3612. refclk,
  3613. &clock,
  3614. &reduced_clock);
  3615. }
  3616. /* SDVO TV has fixed PLL values depend on its clock range,
  3617. this mirrors vbios setting. */
  3618. if (is_sdvo && is_tv) {
  3619. if (adjusted_mode->clock >= 100000
  3620. && adjusted_mode->clock < 140500) {
  3621. clock.p1 = 2;
  3622. clock.p2 = 10;
  3623. clock.n = 3;
  3624. clock.m1 = 16;
  3625. clock.m2 = 8;
  3626. } else if (adjusted_mode->clock >= 140500
  3627. && adjusted_mode->clock <= 200000) {
  3628. clock.p1 = 1;
  3629. clock.p2 = 10;
  3630. clock.n = 6;
  3631. clock.m1 = 12;
  3632. clock.m2 = 8;
  3633. }
  3634. }
  3635. /* FDI link */
  3636. pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  3637. lane = 0;
  3638. /* CPU eDP doesn't require FDI link, so just set DP M/N
  3639. according to current link config */
  3640. if (is_cpu_edp) {
  3641. target_clock = mode->clock;
  3642. intel_edp_link_config(edp_encoder, &lane, &link_bw);
  3643. } else {
  3644. /* [e]DP over FDI requires target mode clock
  3645. instead of link clock */
  3646. if (is_dp)
  3647. target_clock = mode->clock;
  3648. else
  3649. target_clock = adjusted_mode->clock;
  3650. /* FDI is a binary signal running at ~2.7GHz, encoding
  3651. * each output octet as 10 bits. The actual frequency
  3652. * is stored as a divider into a 100MHz clock, and the
  3653. * mode pixel clock is stored in units of 1KHz.
  3654. * Hence the bw of each lane in terms of the mode signal
  3655. * is:
  3656. */
  3657. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  3658. }
  3659. /* determine panel color depth */
  3660. temp = I915_READ(PIPECONF(pipe));
  3661. temp &= ~PIPE_BPC_MASK;
  3662. dither = intel_choose_pipe_bpp_dither(crtc, &pipe_bpp, mode);
  3663. switch (pipe_bpp) {
  3664. case 18:
  3665. temp |= PIPE_6BPC;
  3666. break;
  3667. case 24:
  3668. temp |= PIPE_8BPC;
  3669. break;
  3670. case 30:
  3671. temp |= PIPE_10BPC;
  3672. break;
  3673. case 36:
  3674. temp |= PIPE_12BPC;
  3675. break;
  3676. default:
  3677. WARN(1, "intel_choose_pipe_bpp returned invalid value %d\n",
  3678. pipe_bpp);
  3679. temp |= PIPE_8BPC;
  3680. pipe_bpp = 24;
  3681. break;
  3682. }
  3683. intel_crtc->bpp = pipe_bpp;
  3684. I915_WRITE(PIPECONF(pipe), temp);
  3685. if (!lane) {
  3686. /*
  3687. * Account for spread spectrum to avoid
  3688. * oversubscribing the link. Max center spread
  3689. * is 2.5%; use 5% for safety's sake.
  3690. */
  3691. u32 bps = target_clock * intel_crtc->bpp * 21 / 20;
  3692. lane = bps / (link_bw * 8) + 1;
  3693. }
  3694. intel_crtc->fdi_lanes = lane;
  3695. if (pixel_multiplier > 1)
  3696. link_bw *= pixel_multiplier;
  3697. ironlake_compute_m_n(intel_crtc->bpp, lane, target_clock, link_bw,
  3698. &m_n);
  3699. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  3700. if (has_reduced_clock)
  3701. fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
  3702. reduced_clock.m2;
  3703. /* Enable autotuning of the PLL clock (if permissible) */
  3704. factor = 21;
  3705. if (is_lvds) {
  3706. if ((intel_panel_use_ssc(dev_priv) &&
  3707. dev_priv->lvds_ssc_freq == 100) ||
  3708. (I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP)
  3709. factor = 25;
  3710. } else if (is_sdvo && is_tv)
  3711. factor = 20;
  3712. if (clock.m < factor * clock.n)
  3713. fp |= FP_CB_TUNE;
  3714. dpll = 0;
  3715. if (is_lvds)
  3716. dpll |= DPLLB_MODE_LVDS;
  3717. else
  3718. dpll |= DPLLB_MODE_DAC_SERIAL;
  3719. if (is_sdvo) {
  3720. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  3721. if (pixel_multiplier > 1) {
  3722. dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  3723. }
  3724. dpll |= DPLL_DVO_HIGH_SPEED;
  3725. }
  3726. if (is_dp && !is_cpu_edp)
  3727. dpll |= DPLL_DVO_HIGH_SPEED;
  3728. /* compute bitmask from p1 value */
  3729. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3730. /* also FPA1 */
  3731. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  3732. switch (clock.p2) {
  3733. case 5:
  3734. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  3735. break;
  3736. case 7:
  3737. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  3738. break;
  3739. case 10:
  3740. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  3741. break;
  3742. case 14:
  3743. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  3744. break;
  3745. }
  3746. if (is_sdvo && is_tv)
  3747. dpll |= PLL_REF_INPUT_TVCLKINBC;
  3748. else if (is_tv)
  3749. /* XXX: just matching BIOS for now */
  3750. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  3751. dpll |= 3;
  3752. else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  3753. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  3754. else
  3755. dpll |= PLL_REF_INPUT_DREFCLK;
  3756. /* setup pipeconf */
  3757. pipeconf = I915_READ(PIPECONF(pipe));
  3758. /* Set up the display plane register */
  3759. dspcntr = DISPPLANE_GAMMA_ENABLE;
  3760. DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
  3761. drm_mode_debug_printmodeline(mode);
  3762. /* CPU eDP is the only output that doesn't need a PCH PLL of its own */
  3763. if (!is_cpu_edp) {
  3764. struct intel_pch_pll *pll;
  3765. pll = intel_get_pch_pll(intel_crtc, dpll, fp);
  3766. if (pll == NULL) {
  3767. DRM_DEBUG_DRIVER("failed to find PLL for pipe %d\n",
  3768. pipe);
  3769. return -EINVAL;
  3770. }
  3771. } else
  3772. intel_put_pch_pll(intel_crtc);
  3773. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  3774. * This is an exception to the general rule that mode_set doesn't turn
  3775. * things on.
  3776. */
  3777. if (is_lvds) {
  3778. temp = I915_READ(PCH_LVDS);
  3779. temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  3780. if (HAS_PCH_CPT(dev)) {
  3781. temp &= ~PORT_TRANS_SEL_MASK;
  3782. temp |= PORT_TRANS_SEL_CPT(pipe);
  3783. } else {
  3784. if (pipe == 1)
  3785. temp |= LVDS_PIPEB_SELECT;
  3786. else
  3787. temp &= ~LVDS_PIPEB_SELECT;
  3788. }
  3789. /* set the corresponsding LVDS_BORDER bit */
  3790. temp |= dev_priv->lvds_border_bits;
  3791. /* Set the B0-B3 data pairs corresponding to whether we're going to
  3792. * set the DPLLs for dual-channel mode or not.
  3793. */
  3794. if (clock.p2 == 7)
  3795. temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  3796. else
  3797. temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  3798. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  3799. * appropriately here, but we need to look more thoroughly into how
  3800. * panels behave in the two modes.
  3801. */
  3802. temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
  3803. if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
  3804. temp |= LVDS_HSYNC_POLARITY;
  3805. if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
  3806. temp |= LVDS_VSYNC_POLARITY;
  3807. I915_WRITE(PCH_LVDS, temp);
  3808. }
  3809. pipeconf &= ~PIPECONF_DITHER_EN;
  3810. pipeconf &= ~PIPECONF_DITHER_TYPE_MASK;
  3811. if ((is_lvds && dev_priv->lvds_dither) || dither) {
  3812. pipeconf |= PIPECONF_DITHER_EN;
  3813. pipeconf |= PIPECONF_DITHER_TYPE_SP;
  3814. }
  3815. if (is_dp && !is_cpu_edp) {
  3816. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  3817. } else {
  3818. /* For non-DP output, clear any trans DP clock recovery setting.*/
  3819. I915_WRITE(TRANSDATA_M1(pipe), 0);
  3820. I915_WRITE(TRANSDATA_N1(pipe), 0);
  3821. I915_WRITE(TRANSDPLINK_M1(pipe), 0);
  3822. I915_WRITE(TRANSDPLINK_N1(pipe), 0);
  3823. }
  3824. if (intel_crtc->pch_pll) {
  3825. I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
  3826. /* Wait for the clocks to stabilize. */
  3827. POSTING_READ(intel_crtc->pch_pll->pll_reg);
  3828. udelay(150);
  3829. /* The pixel multiplier can only be updated once the
  3830. * DPLL is enabled and the clocks are stable.
  3831. *
  3832. * So write it again.
  3833. */
  3834. I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
  3835. }
  3836. intel_crtc->lowfreq_avail = false;
  3837. if (intel_crtc->pch_pll) {
  3838. if (is_lvds && has_reduced_clock && i915_powersave) {
  3839. I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp2);
  3840. intel_crtc->lowfreq_avail = true;
  3841. if (HAS_PIPE_CXSR(dev)) {
  3842. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  3843. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  3844. }
  3845. } else {
  3846. I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp);
  3847. if (HAS_PIPE_CXSR(dev)) {
  3848. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  3849. pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
  3850. }
  3851. }
  3852. }
  3853. pipeconf &= ~PIPECONF_INTERLACE_MASK;
  3854. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  3855. pipeconf |= PIPECONF_INTERLACED_ILK;
  3856. /* the chip adds 2 halflines automatically */
  3857. adjusted_mode->crtc_vtotal -= 1;
  3858. adjusted_mode->crtc_vblank_end -= 1;
  3859. I915_WRITE(VSYNCSHIFT(pipe),
  3860. adjusted_mode->crtc_hsync_start
  3861. - adjusted_mode->crtc_htotal/2);
  3862. } else {
  3863. pipeconf |= PIPECONF_PROGRESSIVE;
  3864. I915_WRITE(VSYNCSHIFT(pipe), 0);
  3865. }
  3866. I915_WRITE(HTOTAL(pipe),
  3867. (adjusted_mode->crtc_hdisplay - 1) |
  3868. ((adjusted_mode->crtc_htotal - 1) << 16));
  3869. I915_WRITE(HBLANK(pipe),
  3870. (adjusted_mode->crtc_hblank_start - 1) |
  3871. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  3872. I915_WRITE(HSYNC(pipe),
  3873. (adjusted_mode->crtc_hsync_start - 1) |
  3874. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  3875. I915_WRITE(VTOTAL(pipe),
  3876. (adjusted_mode->crtc_vdisplay - 1) |
  3877. ((adjusted_mode->crtc_vtotal - 1) << 16));
  3878. I915_WRITE(VBLANK(pipe),
  3879. (adjusted_mode->crtc_vblank_start - 1) |
  3880. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  3881. I915_WRITE(VSYNC(pipe),
  3882. (adjusted_mode->crtc_vsync_start - 1) |
  3883. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  3884. /* pipesrc controls the size that is scaled from, which should
  3885. * always be the user's requested size.
  3886. */
  3887. I915_WRITE(PIPESRC(pipe),
  3888. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  3889. I915_WRITE(PIPE_DATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);
  3890. I915_WRITE(PIPE_DATA_N1(pipe), m_n.gmch_n);
  3891. I915_WRITE(PIPE_LINK_M1(pipe), m_n.link_m);
  3892. I915_WRITE(PIPE_LINK_N1(pipe), m_n.link_n);
  3893. if (is_cpu_edp)
  3894. ironlake_set_pll_edp(crtc, adjusted_mode->clock);
  3895. I915_WRITE(PIPECONF(pipe), pipeconf);
  3896. POSTING_READ(PIPECONF(pipe));
  3897. intel_wait_for_vblank(dev, pipe);
  3898. I915_WRITE(DSPCNTR(plane), dspcntr);
  3899. POSTING_READ(DSPCNTR(plane));
  3900. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  3901. intel_update_watermarks(dev);
  3902. return ret;
  3903. }
  3904. static int intel_crtc_mode_set(struct drm_crtc *crtc,
  3905. struct drm_display_mode *mode,
  3906. struct drm_display_mode *adjusted_mode,
  3907. int x, int y,
  3908. struct drm_framebuffer *old_fb)
  3909. {
  3910. struct drm_device *dev = crtc->dev;
  3911. struct drm_i915_private *dev_priv = dev->dev_private;
  3912. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3913. int pipe = intel_crtc->pipe;
  3914. int ret;
  3915. drm_vblank_pre_modeset(dev, pipe);
  3916. ret = dev_priv->display.crtc_mode_set(crtc, mode, adjusted_mode,
  3917. x, y, old_fb);
  3918. drm_vblank_post_modeset(dev, pipe);
  3919. if (ret)
  3920. intel_crtc->dpms_mode = DRM_MODE_DPMS_OFF;
  3921. else
  3922. intel_crtc->dpms_mode = DRM_MODE_DPMS_ON;
  3923. return ret;
  3924. }
  3925. static bool intel_eld_uptodate(struct drm_connector *connector,
  3926. int reg_eldv, uint32_t bits_eldv,
  3927. int reg_elda, uint32_t bits_elda,
  3928. int reg_edid)
  3929. {
  3930. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  3931. uint8_t *eld = connector->eld;
  3932. uint32_t i;
  3933. i = I915_READ(reg_eldv);
  3934. i &= bits_eldv;
  3935. if (!eld[0])
  3936. return !i;
  3937. if (!i)
  3938. return false;
  3939. i = I915_READ(reg_elda);
  3940. i &= ~bits_elda;
  3941. I915_WRITE(reg_elda, i);
  3942. for (i = 0; i < eld[2]; i++)
  3943. if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
  3944. return false;
  3945. return true;
  3946. }
  3947. static void g4x_write_eld(struct drm_connector *connector,
  3948. struct drm_crtc *crtc)
  3949. {
  3950. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  3951. uint8_t *eld = connector->eld;
  3952. uint32_t eldv;
  3953. uint32_t len;
  3954. uint32_t i;
  3955. i = I915_READ(G4X_AUD_VID_DID);
  3956. if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
  3957. eldv = G4X_ELDV_DEVCL_DEVBLC;
  3958. else
  3959. eldv = G4X_ELDV_DEVCTG;
  3960. if (intel_eld_uptodate(connector,
  3961. G4X_AUD_CNTL_ST, eldv,
  3962. G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
  3963. G4X_HDMIW_HDMIEDID))
  3964. return;
  3965. i = I915_READ(G4X_AUD_CNTL_ST);
  3966. i &= ~(eldv | G4X_ELD_ADDR);
  3967. len = (i >> 9) & 0x1f; /* ELD buffer size */
  3968. I915_WRITE(G4X_AUD_CNTL_ST, i);
  3969. if (!eld[0])
  3970. return;
  3971. len = min_t(uint8_t, eld[2], len);
  3972. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  3973. for (i = 0; i < len; i++)
  3974. I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
  3975. i = I915_READ(G4X_AUD_CNTL_ST);
  3976. i |= eldv;
  3977. I915_WRITE(G4X_AUD_CNTL_ST, i);
  3978. }
  3979. static void ironlake_write_eld(struct drm_connector *connector,
  3980. struct drm_crtc *crtc)
  3981. {
  3982. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  3983. uint8_t *eld = connector->eld;
  3984. uint32_t eldv;
  3985. uint32_t i;
  3986. int len;
  3987. int hdmiw_hdmiedid;
  3988. int aud_config;
  3989. int aud_cntl_st;
  3990. int aud_cntrl_st2;
  3991. if (HAS_PCH_IBX(connector->dev)) {
  3992. hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID_A;
  3993. aud_config = IBX_AUD_CONFIG_A;
  3994. aud_cntl_st = IBX_AUD_CNTL_ST_A;
  3995. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  3996. } else {
  3997. hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID_A;
  3998. aud_config = CPT_AUD_CONFIG_A;
  3999. aud_cntl_st = CPT_AUD_CNTL_ST_A;
  4000. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  4001. }
  4002. i = to_intel_crtc(crtc)->pipe;
  4003. hdmiw_hdmiedid += i * 0x100;
  4004. aud_cntl_st += i * 0x100;
  4005. aud_config += i * 0x100;
  4006. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(i));
  4007. i = I915_READ(aud_cntl_st);
  4008. i = (i >> 29) & 0x3; /* DIP_Port_Select, 0x1 = PortB */
  4009. if (!i) {
  4010. DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
  4011. /* operate blindly on all ports */
  4012. eldv = IBX_ELD_VALIDB;
  4013. eldv |= IBX_ELD_VALIDB << 4;
  4014. eldv |= IBX_ELD_VALIDB << 8;
  4015. } else {
  4016. DRM_DEBUG_DRIVER("ELD on port %c\n", 'A' + i);
  4017. eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
  4018. }
  4019. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  4020. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  4021. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  4022. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  4023. } else
  4024. I915_WRITE(aud_config, 0);
  4025. if (intel_eld_uptodate(connector,
  4026. aud_cntrl_st2, eldv,
  4027. aud_cntl_st, IBX_ELD_ADDRESS,
  4028. hdmiw_hdmiedid))
  4029. return;
  4030. i = I915_READ(aud_cntrl_st2);
  4031. i &= ~eldv;
  4032. I915_WRITE(aud_cntrl_st2, i);
  4033. if (!eld[0])
  4034. return;
  4035. i = I915_READ(aud_cntl_st);
  4036. i &= ~IBX_ELD_ADDRESS;
  4037. I915_WRITE(aud_cntl_st, i);
  4038. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  4039. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  4040. for (i = 0; i < len; i++)
  4041. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  4042. i = I915_READ(aud_cntrl_st2);
  4043. i |= eldv;
  4044. I915_WRITE(aud_cntrl_st2, i);
  4045. }
  4046. void intel_write_eld(struct drm_encoder *encoder,
  4047. struct drm_display_mode *mode)
  4048. {
  4049. struct drm_crtc *crtc = encoder->crtc;
  4050. struct drm_connector *connector;
  4051. struct drm_device *dev = encoder->dev;
  4052. struct drm_i915_private *dev_priv = dev->dev_private;
  4053. connector = drm_select_eld(encoder, mode);
  4054. if (!connector)
  4055. return;
  4056. DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  4057. connector->base.id,
  4058. drm_get_connector_name(connector),
  4059. connector->encoder->base.id,
  4060. drm_get_encoder_name(connector->encoder));
  4061. connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
  4062. if (dev_priv->display.write_eld)
  4063. dev_priv->display.write_eld(connector, crtc);
  4064. }
  4065. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  4066. void intel_crtc_load_lut(struct drm_crtc *crtc)
  4067. {
  4068. struct drm_device *dev = crtc->dev;
  4069. struct drm_i915_private *dev_priv = dev->dev_private;
  4070. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4071. int palreg = PALETTE(intel_crtc->pipe);
  4072. int i;
  4073. /* The clocks have to be on to load the palette. */
  4074. if (!crtc->enabled || !intel_crtc->active)
  4075. return;
  4076. /* use legacy palette for Ironlake */
  4077. if (HAS_PCH_SPLIT(dev))
  4078. palreg = LGC_PALETTE(intel_crtc->pipe);
  4079. for (i = 0; i < 256; i++) {
  4080. I915_WRITE(palreg + 4 * i,
  4081. (intel_crtc->lut_r[i] << 16) |
  4082. (intel_crtc->lut_g[i] << 8) |
  4083. intel_crtc->lut_b[i]);
  4084. }
  4085. }
  4086. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  4087. {
  4088. struct drm_device *dev = crtc->dev;
  4089. struct drm_i915_private *dev_priv = dev->dev_private;
  4090. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4091. bool visible = base != 0;
  4092. u32 cntl;
  4093. if (intel_crtc->cursor_visible == visible)
  4094. return;
  4095. cntl = I915_READ(_CURACNTR);
  4096. if (visible) {
  4097. /* On these chipsets we can only modify the base whilst
  4098. * the cursor is disabled.
  4099. */
  4100. I915_WRITE(_CURABASE, base);
  4101. cntl &= ~(CURSOR_FORMAT_MASK);
  4102. /* XXX width must be 64, stride 256 => 0x00 << 28 */
  4103. cntl |= CURSOR_ENABLE |
  4104. CURSOR_GAMMA_ENABLE |
  4105. CURSOR_FORMAT_ARGB;
  4106. } else
  4107. cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
  4108. I915_WRITE(_CURACNTR, cntl);
  4109. intel_crtc->cursor_visible = visible;
  4110. }
  4111. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  4112. {
  4113. struct drm_device *dev = crtc->dev;
  4114. struct drm_i915_private *dev_priv = dev->dev_private;
  4115. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4116. int pipe = intel_crtc->pipe;
  4117. bool visible = base != 0;
  4118. if (intel_crtc->cursor_visible != visible) {
  4119. uint32_t cntl = I915_READ(CURCNTR(pipe));
  4120. if (base) {
  4121. cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
  4122. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  4123. cntl |= pipe << 28; /* Connect to correct pipe */
  4124. } else {
  4125. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  4126. cntl |= CURSOR_MODE_DISABLE;
  4127. }
  4128. I915_WRITE(CURCNTR(pipe), cntl);
  4129. intel_crtc->cursor_visible = visible;
  4130. }
  4131. /* and commit changes on next vblank */
  4132. I915_WRITE(CURBASE(pipe), base);
  4133. }
  4134. static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
  4135. {
  4136. struct drm_device *dev = crtc->dev;
  4137. struct drm_i915_private *dev_priv = dev->dev_private;
  4138. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4139. int pipe = intel_crtc->pipe;
  4140. bool visible = base != 0;
  4141. if (intel_crtc->cursor_visible != visible) {
  4142. uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
  4143. if (base) {
  4144. cntl &= ~CURSOR_MODE;
  4145. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  4146. } else {
  4147. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  4148. cntl |= CURSOR_MODE_DISABLE;
  4149. }
  4150. I915_WRITE(CURCNTR_IVB(pipe), cntl);
  4151. intel_crtc->cursor_visible = visible;
  4152. }
  4153. /* and commit changes on next vblank */
  4154. I915_WRITE(CURBASE_IVB(pipe), base);
  4155. }
  4156. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  4157. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  4158. bool on)
  4159. {
  4160. struct drm_device *dev = crtc->dev;
  4161. struct drm_i915_private *dev_priv = dev->dev_private;
  4162. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4163. int pipe = intel_crtc->pipe;
  4164. int x = intel_crtc->cursor_x;
  4165. int y = intel_crtc->cursor_y;
  4166. u32 base, pos;
  4167. bool visible;
  4168. pos = 0;
  4169. if (on && crtc->enabled && crtc->fb) {
  4170. base = intel_crtc->cursor_addr;
  4171. if (x > (int) crtc->fb->width)
  4172. base = 0;
  4173. if (y > (int) crtc->fb->height)
  4174. base = 0;
  4175. } else
  4176. base = 0;
  4177. if (x < 0) {
  4178. if (x + intel_crtc->cursor_width < 0)
  4179. base = 0;
  4180. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  4181. x = -x;
  4182. }
  4183. pos |= x << CURSOR_X_SHIFT;
  4184. if (y < 0) {
  4185. if (y + intel_crtc->cursor_height < 0)
  4186. base = 0;
  4187. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  4188. y = -y;
  4189. }
  4190. pos |= y << CURSOR_Y_SHIFT;
  4191. visible = base != 0;
  4192. if (!visible && !intel_crtc->cursor_visible)
  4193. return;
  4194. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  4195. I915_WRITE(CURPOS_IVB(pipe), pos);
  4196. ivb_update_cursor(crtc, base);
  4197. } else {
  4198. I915_WRITE(CURPOS(pipe), pos);
  4199. if (IS_845G(dev) || IS_I865G(dev))
  4200. i845_update_cursor(crtc, base);
  4201. else
  4202. i9xx_update_cursor(crtc, base);
  4203. }
  4204. if (visible)
  4205. intel_mark_busy(dev, to_intel_framebuffer(crtc->fb)->obj);
  4206. }
  4207. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  4208. struct drm_file *file,
  4209. uint32_t handle,
  4210. uint32_t width, uint32_t height)
  4211. {
  4212. struct drm_device *dev = crtc->dev;
  4213. struct drm_i915_private *dev_priv = dev->dev_private;
  4214. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4215. struct drm_i915_gem_object *obj;
  4216. uint32_t addr;
  4217. int ret;
  4218. DRM_DEBUG_KMS("\n");
  4219. /* if we want to turn off the cursor ignore width and height */
  4220. if (!handle) {
  4221. DRM_DEBUG_KMS("cursor off\n");
  4222. addr = 0;
  4223. obj = NULL;
  4224. mutex_lock(&dev->struct_mutex);
  4225. goto finish;
  4226. }
  4227. /* Currently we only support 64x64 cursors */
  4228. if (width != 64 || height != 64) {
  4229. DRM_ERROR("we currently only support 64x64 cursors\n");
  4230. return -EINVAL;
  4231. }
  4232. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  4233. if (&obj->base == NULL)
  4234. return -ENOENT;
  4235. if (obj->base.size < width * height * 4) {
  4236. DRM_ERROR("buffer is to small\n");
  4237. ret = -ENOMEM;
  4238. goto fail;
  4239. }
  4240. /* we only need to pin inside GTT if cursor is non-phy */
  4241. mutex_lock(&dev->struct_mutex);
  4242. if (!dev_priv->info->cursor_needs_physical) {
  4243. if (obj->tiling_mode) {
  4244. DRM_ERROR("cursor cannot be tiled\n");
  4245. ret = -EINVAL;
  4246. goto fail_locked;
  4247. }
  4248. ret = i915_gem_object_pin_to_display_plane(obj, 0, NULL);
  4249. if (ret) {
  4250. DRM_ERROR("failed to move cursor bo into the GTT\n");
  4251. goto fail_locked;
  4252. }
  4253. ret = i915_gem_object_put_fence(obj);
  4254. if (ret) {
  4255. DRM_ERROR("failed to release fence for cursor");
  4256. goto fail_unpin;
  4257. }
  4258. addr = obj->gtt_offset;
  4259. } else {
  4260. int align = IS_I830(dev) ? 16 * 1024 : 256;
  4261. ret = i915_gem_attach_phys_object(dev, obj,
  4262. (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
  4263. align);
  4264. if (ret) {
  4265. DRM_ERROR("failed to attach phys object\n");
  4266. goto fail_locked;
  4267. }
  4268. addr = obj->phys_obj->handle->busaddr;
  4269. }
  4270. if (IS_GEN2(dev))
  4271. I915_WRITE(CURSIZE, (height << 12) | width);
  4272. finish:
  4273. if (intel_crtc->cursor_bo) {
  4274. if (dev_priv->info->cursor_needs_physical) {
  4275. if (intel_crtc->cursor_bo != obj)
  4276. i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
  4277. } else
  4278. i915_gem_object_unpin(intel_crtc->cursor_bo);
  4279. drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
  4280. }
  4281. mutex_unlock(&dev->struct_mutex);
  4282. intel_crtc->cursor_addr = addr;
  4283. intel_crtc->cursor_bo = obj;
  4284. intel_crtc->cursor_width = width;
  4285. intel_crtc->cursor_height = height;
  4286. intel_crtc_update_cursor(crtc, true);
  4287. return 0;
  4288. fail_unpin:
  4289. i915_gem_object_unpin(obj);
  4290. fail_locked:
  4291. mutex_unlock(&dev->struct_mutex);
  4292. fail:
  4293. drm_gem_object_unreference_unlocked(&obj->base);
  4294. return ret;
  4295. }
  4296. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  4297. {
  4298. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4299. intel_crtc->cursor_x = x;
  4300. intel_crtc->cursor_y = y;
  4301. intel_crtc_update_cursor(crtc, true);
  4302. return 0;
  4303. }
  4304. /** Sets the color ramps on behalf of RandR */
  4305. void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  4306. u16 blue, int regno)
  4307. {
  4308. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4309. intel_crtc->lut_r[regno] = red >> 8;
  4310. intel_crtc->lut_g[regno] = green >> 8;
  4311. intel_crtc->lut_b[regno] = blue >> 8;
  4312. }
  4313. void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  4314. u16 *blue, int regno)
  4315. {
  4316. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4317. *red = intel_crtc->lut_r[regno] << 8;
  4318. *green = intel_crtc->lut_g[regno] << 8;
  4319. *blue = intel_crtc->lut_b[regno] << 8;
  4320. }
  4321. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  4322. u16 *blue, uint32_t start, uint32_t size)
  4323. {
  4324. int end = (start + size > 256) ? 256 : start + size, i;
  4325. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4326. for (i = start; i < end; i++) {
  4327. intel_crtc->lut_r[i] = red[i] >> 8;
  4328. intel_crtc->lut_g[i] = green[i] >> 8;
  4329. intel_crtc->lut_b[i] = blue[i] >> 8;
  4330. }
  4331. intel_crtc_load_lut(crtc);
  4332. }
  4333. /**
  4334. * Get a pipe with a simple mode set on it for doing load-based monitor
  4335. * detection.
  4336. *
  4337. * It will be up to the load-detect code to adjust the pipe as appropriate for
  4338. * its requirements. The pipe will be connected to no other encoders.
  4339. *
  4340. * Currently this code will only succeed if there is a pipe with no encoders
  4341. * configured for it. In the future, it could choose to temporarily disable
  4342. * some outputs to free up a pipe for its use.
  4343. *
  4344. * \return crtc, or NULL if no pipes are available.
  4345. */
  4346. /* VESA 640x480x72Hz mode to set on the pipe */
  4347. static struct drm_display_mode load_detect_mode = {
  4348. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  4349. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  4350. };
  4351. static struct drm_framebuffer *
  4352. intel_framebuffer_create(struct drm_device *dev,
  4353. struct drm_mode_fb_cmd2 *mode_cmd,
  4354. struct drm_i915_gem_object *obj)
  4355. {
  4356. struct intel_framebuffer *intel_fb;
  4357. int ret;
  4358. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  4359. if (!intel_fb) {
  4360. drm_gem_object_unreference_unlocked(&obj->base);
  4361. return ERR_PTR(-ENOMEM);
  4362. }
  4363. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  4364. if (ret) {
  4365. drm_gem_object_unreference_unlocked(&obj->base);
  4366. kfree(intel_fb);
  4367. return ERR_PTR(ret);
  4368. }
  4369. return &intel_fb->base;
  4370. }
  4371. static u32
  4372. intel_framebuffer_pitch_for_width(int width, int bpp)
  4373. {
  4374. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  4375. return ALIGN(pitch, 64);
  4376. }
  4377. static u32
  4378. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  4379. {
  4380. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  4381. return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
  4382. }
  4383. static struct drm_framebuffer *
  4384. intel_framebuffer_create_for_mode(struct drm_device *dev,
  4385. struct drm_display_mode *mode,
  4386. int depth, int bpp)
  4387. {
  4388. struct drm_i915_gem_object *obj;
  4389. struct drm_mode_fb_cmd2 mode_cmd;
  4390. obj = i915_gem_alloc_object(dev,
  4391. intel_framebuffer_size_for_mode(mode, bpp));
  4392. if (obj == NULL)
  4393. return ERR_PTR(-ENOMEM);
  4394. mode_cmd.width = mode->hdisplay;
  4395. mode_cmd.height = mode->vdisplay;
  4396. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  4397. bpp);
  4398. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  4399. return intel_framebuffer_create(dev, &mode_cmd, obj);
  4400. }
  4401. static struct drm_framebuffer *
  4402. mode_fits_in_fbdev(struct drm_device *dev,
  4403. struct drm_display_mode *mode)
  4404. {
  4405. struct drm_i915_private *dev_priv = dev->dev_private;
  4406. struct drm_i915_gem_object *obj;
  4407. struct drm_framebuffer *fb;
  4408. if (dev_priv->fbdev == NULL)
  4409. return NULL;
  4410. obj = dev_priv->fbdev->ifb.obj;
  4411. if (obj == NULL)
  4412. return NULL;
  4413. fb = &dev_priv->fbdev->ifb.base;
  4414. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  4415. fb->bits_per_pixel))
  4416. return NULL;
  4417. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  4418. return NULL;
  4419. return fb;
  4420. }
  4421. bool intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,
  4422. struct drm_connector *connector,
  4423. struct drm_display_mode *mode,
  4424. struct intel_load_detect_pipe *old)
  4425. {
  4426. struct intel_crtc *intel_crtc;
  4427. struct drm_crtc *possible_crtc;
  4428. struct drm_encoder *encoder = &intel_encoder->base;
  4429. struct drm_crtc *crtc = NULL;
  4430. struct drm_device *dev = encoder->dev;
  4431. struct drm_framebuffer *old_fb;
  4432. int i = -1;
  4433. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  4434. connector->base.id, drm_get_connector_name(connector),
  4435. encoder->base.id, drm_get_encoder_name(encoder));
  4436. /*
  4437. * Algorithm gets a little messy:
  4438. *
  4439. * - if the connector already has an assigned crtc, use it (but make
  4440. * sure it's on first)
  4441. *
  4442. * - try to find the first unused crtc that can drive this connector,
  4443. * and use that if we find one
  4444. */
  4445. /* See if we already have a CRTC for this connector */
  4446. if (encoder->crtc) {
  4447. crtc = encoder->crtc;
  4448. intel_crtc = to_intel_crtc(crtc);
  4449. old->dpms_mode = intel_crtc->dpms_mode;
  4450. old->load_detect_temp = false;
  4451. /* Make sure the crtc and connector are running */
  4452. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  4453. struct drm_encoder_helper_funcs *encoder_funcs;
  4454. struct drm_crtc_helper_funcs *crtc_funcs;
  4455. crtc_funcs = crtc->helper_private;
  4456. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  4457. encoder_funcs = encoder->helper_private;
  4458. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  4459. }
  4460. return true;
  4461. }
  4462. /* Find an unused one (if possible) */
  4463. list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
  4464. i++;
  4465. if (!(encoder->possible_crtcs & (1 << i)))
  4466. continue;
  4467. if (!possible_crtc->enabled) {
  4468. crtc = possible_crtc;
  4469. break;
  4470. }
  4471. }
  4472. /*
  4473. * If we didn't find an unused CRTC, don't use any.
  4474. */
  4475. if (!crtc) {
  4476. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  4477. return false;
  4478. }
  4479. encoder->crtc = crtc;
  4480. connector->encoder = encoder;
  4481. intel_crtc = to_intel_crtc(crtc);
  4482. old->dpms_mode = intel_crtc->dpms_mode;
  4483. old->load_detect_temp = true;
  4484. old->release_fb = NULL;
  4485. if (!mode)
  4486. mode = &load_detect_mode;
  4487. old_fb = crtc->fb;
  4488. /* We need a framebuffer large enough to accommodate all accesses
  4489. * that the plane may generate whilst we perform load detection.
  4490. * We can not rely on the fbcon either being present (we get called
  4491. * during its initialisation to detect all boot displays, or it may
  4492. * not even exist) or that it is large enough to satisfy the
  4493. * requested mode.
  4494. */
  4495. crtc->fb = mode_fits_in_fbdev(dev, mode);
  4496. if (crtc->fb == NULL) {
  4497. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  4498. crtc->fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  4499. old->release_fb = crtc->fb;
  4500. } else
  4501. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  4502. if (IS_ERR(crtc->fb)) {
  4503. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  4504. crtc->fb = old_fb;
  4505. return false;
  4506. }
  4507. if (!drm_crtc_helper_set_mode(crtc, mode, 0, 0, old_fb)) {
  4508. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  4509. if (old->release_fb)
  4510. old->release_fb->funcs->destroy(old->release_fb);
  4511. crtc->fb = old_fb;
  4512. return false;
  4513. }
  4514. /* let the connector get through one full cycle before testing */
  4515. intel_wait_for_vblank(dev, intel_crtc->pipe);
  4516. return true;
  4517. }
  4518. void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,
  4519. struct drm_connector *connector,
  4520. struct intel_load_detect_pipe *old)
  4521. {
  4522. struct drm_encoder *encoder = &intel_encoder->base;
  4523. struct drm_device *dev = encoder->dev;
  4524. struct drm_crtc *crtc = encoder->crtc;
  4525. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  4526. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  4527. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  4528. connector->base.id, drm_get_connector_name(connector),
  4529. encoder->base.id, drm_get_encoder_name(encoder));
  4530. if (old->load_detect_temp) {
  4531. connector->encoder = NULL;
  4532. drm_helper_disable_unused_functions(dev);
  4533. if (old->release_fb)
  4534. old->release_fb->funcs->destroy(old->release_fb);
  4535. return;
  4536. }
  4537. /* Switch crtc and encoder back off if necessary */
  4538. if (old->dpms_mode != DRM_MODE_DPMS_ON) {
  4539. encoder_funcs->dpms(encoder, old->dpms_mode);
  4540. crtc_funcs->dpms(crtc, old->dpms_mode);
  4541. }
  4542. }
  4543. /* Returns the clock of the currently programmed mode of the given pipe. */
  4544. static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
  4545. {
  4546. struct drm_i915_private *dev_priv = dev->dev_private;
  4547. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4548. int pipe = intel_crtc->pipe;
  4549. u32 dpll = I915_READ(DPLL(pipe));
  4550. u32 fp;
  4551. intel_clock_t clock;
  4552. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  4553. fp = I915_READ(FP0(pipe));
  4554. else
  4555. fp = I915_READ(FP1(pipe));
  4556. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  4557. if (IS_PINEVIEW(dev)) {
  4558. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  4559. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  4560. } else {
  4561. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  4562. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  4563. }
  4564. if (!IS_GEN2(dev)) {
  4565. if (IS_PINEVIEW(dev))
  4566. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  4567. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  4568. else
  4569. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  4570. DPLL_FPA01_P1_POST_DIV_SHIFT);
  4571. switch (dpll & DPLL_MODE_MASK) {
  4572. case DPLLB_MODE_DAC_SERIAL:
  4573. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  4574. 5 : 10;
  4575. break;
  4576. case DPLLB_MODE_LVDS:
  4577. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  4578. 7 : 14;
  4579. break;
  4580. default:
  4581. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  4582. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  4583. return 0;
  4584. }
  4585. /* XXX: Handle the 100Mhz refclk */
  4586. intel_clock(dev, 96000, &clock);
  4587. } else {
  4588. bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
  4589. if (is_lvds) {
  4590. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  4591. DPLL_FPA01_P1_POST_DIV_SHIFT);
  4592. clock.p2 = 14;
  4593. if ((dpll & PLL_REF_INPUT_MASK) ==
  4594. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  4595. /* XXX: might not be 66MHz */
  4596. intel_clock(dev, 66000, &clock);
  4597. } else
  4598. intel_clock(dev, 48000, &clock);
  4599. } else {
  4600. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  4601. clock.p1 = 2;
  4602. else {
  4603. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  4604. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  4605. }
  4606. if (dpll & PLL_P2_DIVIDE_BY_4)
  4607. clock.p2 = 4;
  4608. else
  4609. clock.p2 = 2;
  4610. intel_clock(dev, 48000, &clock);
  4611. }
  4612. }
  4613. /* XXX: It would be nice to validate the clocks, but we can't reuse
  4614. * i830PllIsValid() because it relies on the xf86_config connector
  4615. * configuration being accurate, which it isn't necessarily.
  4616. */
  4617. return clock.dot;
  4618. }
  4619. /** Returns the currently programmed mode of the given pipe. */
  4620. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  4621. struct drm_crtc *crtc)
  4622. {
  4623. struct drm_i915_private *dev_priv = dev->dev_private;
  4624. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4625. int pipe = intel_crtc->pipe;
  4626. struct drm_display_mode *mode;
  4627. int htot = I915_READ(HTOTAL(pipe));
  4628. int hsync = I915_READ(HSYNC(pipe));
  4629. int vtot = I915_READ(VTOTAL(pipe));
  4630. int vsync = I915_READ(VSYNC(pipe));
  4631. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  4632. if (!mode)
  4633. return NULL;
  4634. mode->clock = intel_crtc_clock_get(dev, crtc);
  4635. mode->hdisplay = (htot & 0xffff) + 1;
  4636. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  4637. mode->hsync_start = (hsync & 0xffff) + 1;
  4638. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  4639. mode->vdisplay = (vtot & 0xffff) + 1;
  4640. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  4641. mode->vsync_start = (vsync & 0xffff) + 1;
  4642. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  4643. drm_mode_set_name(mode);
  4644. drm_mode_set_crtcinfo(mode, 0);
  4645. return mode;
  4646. }
  4647. #define GPU_IDLE_TIMEOUT 500 /* ms */
  4648. /* When this timer fires, we've been idle for awhile */
  4649. static void intel_gpu_idle_timer(unsigned long arg)
  4650. {
  4651. struct drm_device *dev = (struct drm_device *)arg;
  4652. drm_i915_private_t *dev_priv = dev->dev_private;
  4653. if (!list_empty(&dev_priv->mm.active_list)) {
  4654. /* Still processing requests, so just re-arm the timer. */
  4655. mod_timer(&dev_priv->idle_timer, jiffies +
  4656. msecs_to_jiffies(GPU_IDLE_TIMEOUT));
  4657. return;
  4658. }
  4659. dev_priv->busy = false;
  4660. queue_work(dev_priv->wq, &dev_priv->idle_work);
  4661. }
  4662. #define CRTC_IDLE_TIMEOUT 1000 /* ms */
  4663. static void intel_crtc_idle_timer(unsigned long arg)
  4664. {
  4665. struct intel_crtc *intel_crtc = (struct intel_crtc *)arg;
  4666. struct drm_crtc *crtc = &intel_crtc->base;
  4667. drm_i915_private_t *dev_priv = crtc->dev->dev_private;
  4668. struct intel_framebuffer *intel_fb;
  4669. intel_fb = to_intel_framebuffer(crtc->fb);
  4670. if (intel_fb && intel_fb->obj->active) {
  4671. /* The framebuffer is still being accessed by the GPU. */
  4672. mod_timer(&intel_crtc->idle_timer, jiffies +
  4673. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  4674. return;
  4675. }
  4676. intel_crtc->busy = false;
  4677. queue_work(dev_priv->wq, &dev_priv->idle_work);
  4678. }
  4679. static void intel_increase_pllclock(struct drm_crtc *crtc)
  4680. {
  4681. struct drm_device *dev = crtc->dev;
  4682. drm_i915_private_t *dev_priv = dev->dev_private;
  4683. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4684. int pipe = intel_crtc->pipe;
  4685. int dpll_reg = DPLL(pipe);
  4686. int dpll;
  4687. if (HAS_PCH_SPLIT(dev))
  4688. return;
  4689. if (!dev_priv->lvds_downclock_avail)
  4690. return;
  4691. dpll = I915_READ(dpll_reg);
  4692. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  4693. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  4694. assert_panel_unlocked(dev_priv, pipe);
  4695. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  4696. I915_WRITE(dpll_reg, dpll);
  4697. intel_wait_for_vblank(dev, pipe);
  4698. dpll = I915_READ(dpll_reg);
  4699. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  4700. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  4701. }
  4702. /* Schedule downclock */
  4703. mod_timer(&intel_crtc->idle_timer, jiffies +
  4704. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  4705. }
  4706. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  4707. {
  4708. struct drm_device *dev = crtc->dev;
  4709. drm_i915_private_t *dev_priv = dev->dev_private;
  4710. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4711. int pipe = intel_crtc->pipe;
  4712. int dpll_reg = DPLL(pipe);
  4713. int dpll = I915_READ(dpll_reg);
  4714. if (HAS_PCH_SPLIT(dev))
  4715. return;
  4716. if (!dev_priv->lvds_downclock_avail)
  4717. return;
  4718. /*
  4719. * Since this is called by a timer, we should never get here in
  4720. * the manual case.
  4721. */
  4722. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  4723. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  4724. assert_panel_unlocked(dev_priv, pipe);
  4725. dpll |= DISPLAY_RATE_SELECT_FPA1;
  4726. I915_WRITE(dpll_reg, dpll);
  4727. intel_wait_for_vblank(dev, pipe);
  4728. dpll = I915_READ(dpll_reg);
  4729. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  4730. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  4731. }
  4732. }
  4733. /**
  4734. * intel_idle_update - adjust clocks for idleness
  4735. * @work: work struct
  4736. *
  4737. * Either the GPU or display (or both) went idle. Check the busy status
  4738. * here and adjust the CRTC and GPU clocks as necessary.
  4739. */
  4740. static void intel_idle_update(struct work_struct *work)
  4741. {
  4742. drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
  4743. idle_work);
  4744. struct drm_device *dev = dev_priv->dev;
  4745. struct drm_crtc *crtc;
  4746. struct intel_crtc *intel_crtc;
  4747. if (!i915_powersave)
  4748. return;
  4749. mutex_lock(&dev->struct_mutex);
  4750. i915_update_gfx_val(dev_priv);
  4751. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  4752. /* Skip inactive CRTCs */
  4753. if (!crtc->fb)
  4754. continue;
  4755. intel_crtc = to_intel_crtc(crtc);
  4756. if (!intel_crtc->busy)
  4757. intel_decrease_pllclock(crtc);
  4758. }
  4759. mutex_unlock(&dev->struct_mutex);
  4760. }
  4761. /**
  4762. * intel_mark_busy - mark the GPU and possibly the display busy
  4763. * @dev: drm device
  4764. * @obj: object we're operating on
  4765. *
  4766. * Callers can use this function to indicate that the GPU is busy processing
  4767. * commands. If @obj matches one of the CRTC objects (i.e. it's a scanout
  4768. * buffer), we'll also mark the display as busy, so we know to increase its
  4769. * clock frequency.
  4770. */
  4771. void intel_mark_busy(struct drm_device *dev, struct drm_i915_gem_object *obj)
  4772. {
  4773. drm_i915_private_t *dev_priv = dev->dev_private;
  4774. struct drm_crtc *crtc = NULL;
  4775. struct intel_framebuffer *intel_fb;
  4776. struct intel_crtc *intel_crtc;
  4777. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4778. return;
  4779. if (!dev_priv->busy)
  4780. dev_priv->busy = true;
  4781. else
  4782. mod_timer(&dev_priv->idle_timer, jiffies +
  4783. msecs_to_jiffies(GPU_IDLE_TIMEOUT));
  4784. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  4785. if (!crtc->fb)
  4786. continue;
  4787. intel_crtc = to_intel_crtc(crtc);
  4788. intel_fb = to_intel_framebuffer(crtc->fb);
  4789. if (intel_fb->obj == obj) {
  4790. if (!intel_crtc->busy) {
  4791. /* Non-busy -> busy, upclock */
  4792. intel_increase_pllclock(crtc);
  4793. intel_crtc->busy = true;
  4794. } else {
  4795. /* Busy -> busy, put off timer */
  4796. mod_timer(&intel_crtc->idle_timer, jiffies +
  4797. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  4798. }
  4799. }
  4800. }
  4801. }
  4802. static void intel_crtc_destroy(struct drm_crtc *crtc)
  4803. {
  4804. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4805. struct drm_device *dev = crtc->dev;
  4806. struct intel_unpin_work *work;
  4807. unsigned long flags;
  4808. spin_lock_irqsave(&dev->event_lock, flags);
  4809. work = intel_crtc->unpin_work;
  4810. intel_crtc->unpin_work = NULL;
  4811. spin_unlock_irqrestore(&dev->event_lock, flags);
  4812. if (work) {
  4813. cancel_work_sync(&work->work);
  4814. kfree(work);
  4815. }
  4816. drm_crtc_cleanup(crtc);
  4817. kfree(intel_crtc);
  4818. }
  4819. static void intel_unpin_work_fn(struct work_struct *__work)
  4820. {
  4821. struct intel_unpin_work *work =
  4822. container_of(__work, struct intel_unpin_work, work);
  4823. mutex_lock(&work->dev->struct_mutex);
  4824. intel_unpin_fb_obj(work->old_fb_obj);
  4825. drm_gem_object_unreference(&work->pending_flip_obj->base);
  4826. drm_gem_object_unreference(&work->old_fb_obj->base);
  4827. intel_update_fbc(work->dev);
  4828. mutex_unlock(&work->dev->struct_mutex);
  4829. kfree(work);
  4830. }
  4831. static void do_intel_finish_page_flip(struct drm_device *dev,
  4832. struct drm_crtc *crtc)
  4833. {
  4834. drm_i915_private_t *dev_priv = dev->dev_private;
  4835. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4836. struct intel_unpin_work *work;
  4837. struct drm_i915_gem_object *obj;
  4838. struct drm_pending_vblank_event *e;
  4839. struct timeval tnow, tvbl;
  4840. unsigned long flags;
  4841. /* Ignore early vblank irqs */
  4842. if (intel_crtc == NULL)
  4843. return;
  4844. do_gettimeofday(&tnow);
  4845. spin_lock_irqsave(&dev->event_lock, flags);
  4846. work = intel_crtc->unpin_work;
  4847. if (work == NULL || !work->pending) {
  4848. spin_unlock_irqrestore(&dev->event_lock, flags);
  4849. return;
  4850. }
  4851. intel_crtc->unpin_work = NULL;
  4852. if (work->event) {
  4853. e = work->event;
  4854. e->event.sequence = drm_vblank_count_and_time(dev, intel_crtc->pipe, &tvbl);
  4855. /* Called before vblank count and timestamps have
  4856. * been updated for the vblank interval of flip
  4857. * completion? Need to increment vblank count and
  4858. * add one videorefresh duration to returned timestamp
  4859. * to account for this. We assume this happened if we
  4860. * get called over 0.9 frame durations after the last
  4861. * timestamped vblank.
  4862. *
  4863. * This calculation can not be used with vrefresh rates
  4864. * below 5Hz (10Hz to be on the safe side) without
  4865. * promoting to 64 integers.
  4866. */
  4867. if (10 * (timeval_to_ns(&tnow) - timeval_to_ns(&tvbl)) >
  4868. 9 * crtc->framedur_ns) {
  4869. e->event.sequence++;
  4870. tvbl = ns_to_timeval(timeval_to_ns(&tvbl) +
  4871. crtc->framedur_ns);
  4872. }
  4873. e->event.tv_sec = tvbl.tv_sec;
  4874. e->event.tv_usec = tvbl.tv_usec;
  4875. list_add_tail(&e->base.link,
  4876. &e->base.file_priv->event_list);
  4877. wake_up_interruptible(&e->base.file_priv->event_wait);
  4878. }
  4879. drm_vblank_put(dev, intel_crtc->pipe);
  4880. spin_unlock_irqrestore(&dev->event_lock, flags);
  4881. obj = work->old_fb_obj;
  4882. atomic_clear_mask(1 << intel_crtc->plane,
  4883. &obj->pending_flip.counter);
  4884. if (atomic_read(&obj->pending_flip) == 0)
  4885. wake_up(&dev_priv->pending_flip_queue);
  4886. schedule_work(&work->work);
  4887. trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
  4888. }
  4889. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  4890. {
  4891. drm_i915_private_t *dev_priv = dev->dev_private;
  4892. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  4893. do_intel_finish_page_flip(dev, crtc);
  4894. }
  4895. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  4896. {
  4897. drm_i915_private_t *dev_priv = dev->dev_private;
  4898. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  4899. do_intel_finish_page_flip(dev, crtc);
  4900. }
  4901. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  4902. {
  4903. drm_i915_private_t *dev_priv = dev->dev_private;
  4904. struct intel_crtc *intel_crtc =
  4905. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  4906. unsigned long flags;
  4907. spin_lock_irqsave(&dev->event_lock, flags);
  4908. if (intel_crtc->unpin_work) {
  4909. if ((++intel_crtc->unpin_work->pending) > 1)
  4910. DRM_ERROR("Prepared flip multiple times\n");
  4911. } else {
  4912. DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
  4913. }
  4914. spin_unlock_irqrestore(&dev->event_lock, flags);
  4915. }
  4916. static int intel_gen2_queue_flip(struct drm_device *dev,
  4917. struct drm_crtc *crtc,
  4918. struct drm_framebuffer *fb,
  4919. struct drm_i915_gem_object *obj)
  4920. {
  4921. struct drm_i915_private *dev_priv = dev->dev_private;
  4922. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4923. unsigned long offset;
  4924. u32 flip_mask;
  4925. int ret;
  4926. ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
  4927. if (ret)
  4928. goto err;
  4929. /* Offset into the new buffer for cases of shared fbs between CRTCs */
  4930. offset = crtc->y * fb->pitches[0] + crtc->x * fb->bits_per_pixel/8;
  4931. ret = BEGIN_LP_RING(6);
  4932. if (ret)
  4933. goto err_unpin;
  4934. /* Can't queue multiple flips, so wait for the previous
  4935. * one to finish before executing the next.
  4936. */
  4937. if (intel_crtc->plane)
  4938. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  4939. else
  4940. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  4941. OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
  4942. OUT_RING(MI_NOOP);
  4943. OUT_RING(MI_DISPLAY_FLIP |
  4944. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  4945. OUT_RING(fb->pitches[0]);
  4946. OUT_RING(obj->gtt_offset + offset);
  4947. OUT_RING(0); /* aux display base address, unused */
  4948. ADVANCE_LP_RING();
  4949. return 0;
  4950. err_unpin:
  4951. intel_unpin_fb_obj(obj);
  4952. err:
  4953. return ret;
  4954. }
  4955. static int intel_gen3_queue_flip(struct drm_device *dev,
  4956. struct drm_crtc *crtc,
  4957. struct drm_framebuffer *fb,
  4958. struct drm_i915_gem_object *obj)
  4959. {
  4960. struct drm_i915_private *dev_priv = dev->dev_private;
  4961. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4962. unsigned long offset;
  4963. u32 flip_mask;
  4964. int ret;
  4965. ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
  4966. if (ret)
  4967. goto err;
  4968. /* Offset into the new buffer for cases of shared fbs between CRTCs */
  4969. offset = crtc->y * fb->pitches[0] + crtc->x * fb->bits_per_pixel/8;
  4970. ret = BEGIN_LP_RING(6);
  4971. if (ret)
  4972. goto err_unpin;
  4973. if (intel_crtc->plane)
  4974. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  4975. else
  4976. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  4977. OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
  4978. OUT_RING(MI_NOOP);
  4979. OUT_RING(MI_DISPLAY_FLIP_I915 |
  4980. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  4981. OUT_RING(fb->pitches[0]);
  4982. OUT_RING(obj->gtt_offset + offset);
  4983. OUT_RING(MI_NOOP);
  4984. ADVANCE_LP_RING();
  4985. return 0;
  4986. err_unpin:
  4987. intel_unpin_fb_obj(obj);
  4988. err:
  4989. return ret;
  4990. }
  4991. static int intel_gen4_queue_flip(struct drm_device *dev,
  4992. struct drm_crtc *crtc,
  4993. struct drm_framebuffer *fb,
  4994. struct drm_i915_gem_object *obj)
  4995. {
  4996. struct drm_i915_private *dev_priv = dev->dev_private;
  4997. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4998. uint32_t pf, pipesrc;
  4999. int ret;
  5000. ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
  5001. if (ret)
  5002. goto err;
  5003. ret = BEGIN_LP_RING(4);
  5004. if (ret)
  5005. goto err_unpin;
  5006. /* i965+ uses the linear or tiled offsets from the
  5007. * Display Registers (which do not change across a page-flip)
  5008. * so we need only reprogram the base address.
  5009. */
  5010. OUT_RING(MI_DISPLAY_FLIP |
  5011. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5012. OUT_RING(fb->pitches[0]);
  5013. OUT_RING(obj->gtt_offset | obj->tiling_mode);
  5014. /* XXX Enabling the panel-fitter across page-flip is so far
  5015. * untested on non-native modes, so ignore it for now.
  5016. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  5017. */
  5018. pf = 0;
  5019. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  5020. OUT_RING(pf | pipesrc);
  5021. ADVANCE_LP_RING();
  5022. return 0;
  5023. err_unpin:
  5024. intel_unpin_fb_obj(obj);
  5025. err:
  5026. return ret;
  5027. }
  5028. static int intel_gen6_queue_flip(struct drm_device *dev,
  5029. struct drm_crtc *crtc,
  5030. struct drm_framebuffer *fb,
  5031. struct drm_i915_gem_object *obj)
  5032. {
  5033. struct drm_i915_private *dev_priv = dev->dev_private;
  5034. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5035. uint32_t pf, pipesrc;
  5036. int ret;
  5037. ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
  5038. if (ret)
  5039. goto err;
  5040. ret = BEGIN_LP_RING(4);
  5041. if (ret)
  5042. goto err_unpin;
  5043. OUT_RING(MI_DISPLAY_FLIP |
  5044. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5045. OUT_RING(fb->pitches[0] | obj->tiling_mode);
  5046. OUT_RING(obj->gtt_offset);
  5047. pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  5048. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  5049. OUT_RING(pf | pipesrc);
  5050. ADVANCE_LP_RING();
  5051. return 0;
  5052. err_unpin:
  5053. intel_unpin_fb_obj(obj);
  5054. err:
  5055. return ret;
  5056. }
  5057. /*
  5058. * On gen7 we currently use the blit ring because (in early silicon at least)
  5059. * the render ring doesn't give us interrpts for page flip completion, which
  5060. * means clients will hang after the first flip is queued. Fortunately the
  5061. * blit ring generates interrupts properly, so use it instead.
  5062. */
  5063. static int intel_gen7_queue_flip(struct drm_device *dev,
  5064. struct drm_crtc *crtc,
  5065. struct drm_framebuffer *fb,
  5066. struct drm_i915_gem_object *obj)
  5067. {
  5068. struct drm_i915_private *dev_priv = dev->dev_private;
  5069. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5070. struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
  5071. int ret;
  5072. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  5073. if (ret)
  5074. goto err;
  5075. ret = intel_ring_begin(ring, 4);
  5076. if (ret)
  5077. goto err_unpin;
  5078. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | (intel_crtc->plane << 19));
  5079. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  5080. intel_ring_emit(ring, (obj->gtt_offset));
  5081. intel_ring_emit(ring, (MI_NOOP));
  5082. intel_ring_advance(ring);
  5083. return 0;
  5084. err_unpin:
  5085. intel_unpin_fb_obj(obj);
  5086. err:
  5087. return ret;
  5088. }
  5089. static int intel_default_queue_flip(struct drm_device *dev,
  5090. struct drm_crtc *crtc,
  5091. struct drm_framebuffer *fb,
  5092. struct drm_i915_gem_object *obj)
  5093. {
  5094. return -ENODEV;
  5095. }
  5096. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  5097. struct drm_framebuffer *fb,
  5098. struct drm_pending_vblank_event *event)
  5099. {
  5100. struct drm_device *dev = crtc->dev;
  5101. struct drm_i915_private *dev_priv = dev->dev_private;
  5102. struct intel_framebuffer *intel_fb;
  5103. struct drm_i915_gem_object *obj;
  5104. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5105. struct intel_unpin_work *work;
  5106. unsigned long flags;
  5107. int ret;
  5108. work = kzalloc(sizeof *work, GFP_KERNEL);
  5109. if (work == NULL)
  5110. return -ENOMEM;
  5111. work->event = event;
  5112. work->dev = crtc->dev;
  5113. intel_fb = to_intel_framebuffer(crtc->fb);
  5114. work->old_fb_obj = intel_fb->obj;
  5115. INIT_WORK(&work->work, intel_unpin_work_fn);
  5116. ret = drm_vblank_get(dev, intel_crtc->pipe);
  5117. if (ret)
  5118. goto free_work;
  5119. /* We borrow the event spin lock for protecting unpin_work */
  5120. spin_lock_irqsave(&dev->event_lock, flags);
  5121. if (intel_crtc->unpin_work) {
  5122. spin_unlock_irqrestore(&dev->event_lock, flags);
  5123. kfree(work);
  5124. drm_vblank_put(dev, intel_crtc->pipe);
  5125. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  5126. return -EBUSY;
  5127. }
  5128. intel_crtc->unpin_work = work;
  5129. spin_unlock_irqrestore(&dev->event_lock, flags);
  5130. intel_fb = to_intel_framebuffer(fb);
  5131. obj = intel_fb->obj;
  5132. mutex_lock(&dev->struct_mutex);
  5133. /* Reference the objects for the scheduled work. */
  5134. drm_gem_object_reference(&work->old_fb_obj->base);
  5135. drm_gem_object_reference(&obj->base);
  5136. crtc->fb = fb;
  5137. work->pending_flip_obj = obj;
  5138. work->enable_stall_check = true;
  5139. /* Block clients from rendering to the new back buffer until
  5140. * the flip occurs and the object is no longer visible.
  5141. */
  5142. atomic_add(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
  5143. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
  5144. if (ret)
  5145. goto cleanup_pending;
  5146. intel_disable_fbc(dev);
  5147. mutex_unlock(&dev->struct_mutex);
  5148. trace_i915_flip_request(intel_crtc->plane, obj);
  5149. return 0;
  5150. cleanup_pending:
  5151. atomic_sub(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
  5152. drm_gem_object_unreference(&work->old_fb_obj->base);
  5153. drm_gem_object_unreference(&obj->base);
  5154. mutex_unlock(&dev->struct_mutex);
  5155. spin_lock_irqsave(&dev->event_lock, flags);
  5156. intel_crtc->unpin_work = NULL;
  5157. spin_unlock_irqrestore(&dev->event_lock, flags);
  5158. drm_vblank_put(dev, intel_crtc->pipe);
  5159. free_work:
  5160. kfree(work);
  5161. return ret;
  5162. }
  5163. static void intel_sanitize_modesetting(struct drm_device *dev,
  5164. int pipe, int plane)
  5165. {
  5166. struct drm_i915_private *dev_priv = dev->dev_private;
  5167. u32 reg, val;
  5168. /* Clear any frame start delays used for debugging left by the BIOS */
  5169. for_each_pipe(pipe) {
  5170. reg = PIPECONF(pipe);
  5171. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  5172. }
  5173. if (HAS_PCH_SPLIT(dev))
  5174. return;
  5175. /* Who knows what state these registers were left in by the BIOS or
  5176. * grub?
  5177. *
  5178. * If we leave the registers in a conflicting state (e.g. with the
  5179. * display plane reading from the other pipe than the one we intend
  5180. * to use) then when we attempt to teardown the active mode, we will
  5181. * not disable the pipes and planes in the correct order -- leaving
  5182. * a plane reading from a disabled pipe and possibly leading to
  5183. * undefined behaviour.
  5184. */
  5185. reg = DSPCNTR(plane);
  5186. val = I915_READ(reg);
  5187. if ((val & DISPLAY_PLANE_ENABLE) == 0)
  5188. return;
  5189. if (!!(val & DISPPLANE_SEL_PIPE_MASK) == pipe)
  5190. return;
  5191. /* This display plane is active and attached to the other CPU pipe. */
  5192. pipe = !pipe;
  5193. /* Disable the plane and wait for it to stop reading from the pipe. */
  5194. intel_disable_plane(dev_priv, plane, pipe);
  5195. intel_disable_pipe(dev_priv, pipe);
  5196. }
  5197. static void intel_crtc_reset(struct drm_crtc *crtc)
  5198. {
  5199. struct drm_device *dev = crtc->dev;
  5200. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5201. /* Reset flags back to the 'unknown' status so that they
  5202. * will be correctly set on the initial modeset.
  5203. */
  5204. intel_crtc->dpms_mode = -1;
  5205. /* We need to fix up any BIOS configuration that conflicts with
  5206. * our expectations.
  5207. */
  5208. intel_sanitize_modesetting(dev, intel_crtc->pipe, intel_crtc->plane);
  5209. }
  5210. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  5211. .dpms = intel_crtc_dpms,
  5212. .mode_fixup = intel_crtc_mode_fixup,
  5213. .mode_set = intel_crtc_mode_set,
  5214. .mode_set_base = intel_pipe_set_base,
  5215. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  5216. .load_lut = intel_crtc_load_lut,
  5217. .disable = intel_crtc_disable,
  5218. };
  5219. static const struct drm_crtc_funcs intel_crtc_funcs = {
  5220. .reset = intel_crtc_reset,
  5221. .cursor_set = intel_crtc_cursor_set,
  5222. .cursor_move = intel_crtc_cursor_move,
  5223. .gamma_set = intel_crtc_gamma_set,
  5224. .set_config = drm_crtc_helper_set_config,
  5225. .destroy = intel_crtc_destroy,
  5226. .page_flip = intel_crtc_page_flip,
  5227. };
  5228. static void intel_pch_pll_init(struct drm_device *dev)
  5229. {
  5230. drm_i915_private_t *dev_priv = dev->dev_private;
  5231. int i;
  5232. if (dev_priv->num_pch_pll == 0) {
  5233. DRM_DEBUG_KMS("No PCH PLLs on this hardware, skipping initialisation\n");
  5234. return;
  5235. }
  5236. for (i = 0; i < dev_priv->num_pch_pll; i++) {
  5237. dev_priv->pch_plls[i].pll_reg = _PCH_DPLL(i);
  5238. dev_priv->pch_plls[i].fp0_reg = _PCH_FP0(i);
  5239. dev_priv->pch_plls[i].fp1_reg = _PCH_FP1(i);
  5240. }
  5241. }
  5242. static void intel_crtc_init(struct drm_device *dev, int pipe)
  5243. {
  5244. drm_i915_private_t *dev_priv = dev->dev_private;
  5245. struct intel_crtc *intel_crtc;
  5246. int i;
  5247. intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  5248. if (intel_crtc == NULL)
  5249. return;
  5250. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  5251. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  5252. for (i = 0; i < 256; i++) {
  5253. intel_crtc->lut_r[i] = i;
  5254. intel_crtc->lut_g[i] = i;
  5255. intel_crtc->lut_b[i] = i;
  5256. }
  5257. /* Swap pipes & planes for FBC on pre-965 */
  5258. intel_crtc->pipe = pipe;
  5259. intel_crtc->plane = pipe;
  5260. if (IS_MOBILE(dev) && IS_GEN3(dev)) {
  5261. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  5262. intel_crtc->plane = !pipe;
  5263. }
  5264. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  5265. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  5266. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  5267. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  5268. intel_crtc_reset(&intel_crtc->base);
  5269. intel_crtc->active = true; /* force the pipe off on setup_init_config */
  5270. intel_crtc->bpp = 24; /* default for pre-Ironlake */
  5271. if (HAS_PCH_SPLIT(dev)) {
  5272. intel_helper_funcs.prepare = ironlake_crtc_prepare;
  5273. intel_helper_funcs.commit = ironlake_crtc_commit;
  5274. } else {
  5275. intel_helper_funcs.prepare = i9xx_crtc_prepare;
  5276. intel_helper_funcs.commit = i9xx_crtc_commit;
  5277. }
  5278. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  5279. intel_crtc->busy = false;
  5280. setup_timer(&intel_crtc->idle_timer, intel_crtc_idle_timer,
  5281. (unsigned long)intel_crtc);
  5282. }
  5283. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  5284. struct drm_file *file)
  5285. {
  5286. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  5287. struct drm_mode_object *drmmode_obj;
  5288. struct intel_crtc *crtc;
  5289. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  5290. return -ENODEV;
  5291. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  5292. DRM_MODE_OBJECT_CRTC);
  5293. if (!drmmode_obj) {
  5294. DRM_ERROR("no such CRTC id\n");
  5295. return -EINVAL;
  5296. }
  5297. crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
  5298. pipe_from_crtc_id->pipe = crtc->pipe;
  5299. return 0;
  5300. }
  5301. static int intel_encoder_clones(struct drm_device *dev, int type_mask)
  5302. {
  5303. struct intel_encoder *encoder;
  5304. int index_mask = 0;
  5305. int entry = 0;
  5306. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  5307. if (type_mask & encoder->clone_mask)
  5308. index_mask |= (1 << entry);
  5309. entry++;
  5310. }
  5311. return index_mask;
  5312. }
  5313. static bool has_edp_a(struct drm_device *dev)
  5314. {
  5315. struct drm_i915_private *dev_priv = dev->dev_private;
  5316. if (!IS_MOBILE(dev))
  5317. return false;
  5318. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  5319. return false;
  5320. if (IS_GEN5(dev) &&
  5321. (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
  5322. return false;
  5323. return true;
  5324. }
  5325. static void intel_setup_outputs(struct drm_device *dev)
  5326. {
  5327. struct drm_i915_private *dev_priv = dev->dev_private;
  5328. struct intel_encoder *encoder;
  5329. bool dpd_is_edp = false;
  5330. bool has_lvds;
  5331. has_lvds = intel_lvds_init(dev);
  5332. if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
  5333. /* disable the panel fitter on everything but LVDS */
  5334. I915_WRITE(PFIT_CONTROL, 0);
  5335. }
  5336. if (HAS_PCH_SPLIT(dev)) {
  5337. dpd_is_edp = intel_dpd_is_edp(dev);
  5338. if (has_edp_a(dev))
  5339. intel_dp_init(dev, DP_A);
  5340. if (dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  5341. intel_dp_init(dev, PCH_DP_D);
  5342. }
  5343. intel_crt_init(dev);
  5344. if (HAS_PCH_SPLIT(dev)) {
  5345. int found;
  5346. if (I915_READ(HDMIB) & PORT_DETECTED) {
  5347. /* PCH SDVOB multiplex with HDMIB */
  5348. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  5349. if (!found)
  5350. intel_hdmi_init(dev, HDMIB);
  5351. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  5352. intel_dp_init(dev, PCH_DP_B);
  5353. }
  5354. if (I915_READ(HDMIC) & PORT_DETECTED)
  5355. intel_hdmi_init(dev, HDMIC);
  5356. if (I915_READ(HDMID) & PORT_DETECTED)
  5357. intel_hdmi_init(dev, HDMID);
  5358. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  5359. intel_dp_init(dev, PCH_DP_C);
  5360. if (!dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  5361. intel_dp_init(dev, PCH_DP_D);
  5362. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  5363. bool found = false;
  5364. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  5365. DRM_DEBUG_KMS("probing SDVOB\n");
  5366. found = intel_sdvo_init(dev, SDVOB, true);
  5367. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  5368. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  5369. intel_hdmi_init(dev, SDVOB);
  5370. }
  5371. if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
  5372. DRM_DEBUG_KMS("probing DP_B\n");
  5373. intel_dp_init(dev, DP_B);
  5374. }
  5375. }
  5376. /* Before G4X SDVOC doesn't have its own detect register */
  5377. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  5378. DRM_DEBUG_KMS("probing SDVOC\n");
  5379. found = intel_sdvo_init(dev, SDVOC, false);
  5380. }
  5381. if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
  5382. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  5383. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  5384. intel_hdmi_init(dev, SDVOC);
  5385. }
  5386. if (SUPPORTS_INTEGRATED_DP(dev)) {
  5387. DRM_DEBUG_KMS("probing DP_C\n");
  5388. intel_dp_init(dev, DP_C);
  5389. }
  5390. }
  5391. if (SUPPORTS_INTEGRATED_DP(dev) &&
  5392. (I915_READ(DP_D) & DP_DETECTED)) {
  5393. DRM_DEBUG_KMS("probing DP_D\n");
  5394. intel_dp_init(dev, DP_D);
  5395. }
  5396. } else if (IS_GEN2(dev))
  5397. intel_dvo_init(dev);
  5398. if (SUPPORTS_TV(dev))
  5399. intel_tv_init(dev);
  5400. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  5401. encoder->base.possible_crtcs = encoder->crtc_mask;
  5402. encoder->base.possible_clones =
  5403. intel_encoder_clones(dev, encoder->clone_mask);
  5404. }
  5405. /* disable all the possible outputs/crtcs before entering KMS mode */
  5406. drm_helper_disable_unused_functions(dev);
  5407. if (HAS_PCH_SPLIT(dev))
  5408. ironlake_init_pch_refclk(dev);
  5409. }
  5410. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  5411. {
  5412. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  5413. drm_framebuffer_cleanup(fb);
  5414. drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
  5415. kfree(intel_fb);
  5416. }
  5417. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  5418. struct drm_file *file,
  5419. unsigned int *handle)
  5420. {
  5421. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  5422. struct drm_i915_gem_object *obj = intel_fb->obj;
  5423. return drm_gem_handle_create(file, &obj->base, handle);
  5424. }
  5425. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  5426. .destroy = intel_user_framebuffer_destroy,
  5427. .create_handle = intel_user_framebuffer_create_handle,
  5428. };
  5429. int intel_framebuffer_init(struct drm_device *dev,
  5430. struct intel_framebuffer *intel_fb,
  5431. struct drm_mode_fb_cmd2 *mode_cmd,
  5432. struct drm_i915_gem_object *obj)
  5433. {
  5434. int ret;
  5435. if (obj->tiling_mode == I915_TILING_Y)
  5436. return -EINVAL;
  5437. if (mode_cmd->pitches[0] & 63)
  5438. return -EINVAL;
  5439. switch (mode_cmd->pixel_format) {
  5440. case DRM_FORMAT_RGB332:
  5441. case DRM_FORMAT_RGB565:
  5442. case DRM_FORMAT_XRGB8888:
  5443. case DRM_FORMAT_XBGR8888:
  5444. case DRM_FORMAT_ARGB8888:
  5445. case DRM_FORMAT_XRGB2101010:
  5446. case DRM_FORMAT_ARGB2101010:
  5447. /* RGB formats are common across chipsets */
  5448. break;
  5449. case DRM_FORMAT_YUYV:
  5450. case DRM_FORMAT_UYVY:
  5451. case DRM_FORMAT_YVYU:
  5452. case DRM_FORMAT_VYUY:
  5453. break;
  5454. default:
  5455. DRM_DEBUG_KMS("unsupported pixel format %u\n",
  5456. mode_cmd->pixel_format);
  5457. return -EINVAL;
  5458. }
  5459. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  5460. if (ret) {
  5461. DRM_ERROR("framebuffer init failed %d\n", ret);
  5462. return ret;
  5463. }
  5464. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  5465. intel_fb->obj = obj;
  5466. return 0;
  5467. }
  5468. static struct drm_framebuffer *
  5469. intel_user_framebuffer_create(struct drm_device *dev,
  5470. struct drm_file *filp,
  5471. struct drm_mode_fb_cmd2 *mode_cmd)
  5472. {
  5473. struct drm_i915_gem_object *obj;
  5474. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  5475. mode_cmd->handles[0]));
  5476. if (&obj->base == NULL)
  5477. return ERR_PTR(-ENOENT);
  5478. return intel_framebuffer_create(dev, mode_cmd, obj);
  5479. }
  5480. static const struct drm_mode_config_funcs intel_mode_funcs = {
  5481. .fb_create = intel_user_framebuffer_create,
  5482. .output_poll_changed = intel_fb_output_poll_changed,
  5483. };
  5484. /* Set up chip specific display functions */
  5485. static void intel_init_display(struct drm_device *dev)
  5486. {
  5487. struct drm_i915_private *dev_priv = dev->dev_private;
  5488. /* We always want a DPMS function */
  5489. if (HAS_PCH_SPLIT(dev)) {
  5490. dev_priv->display.dpms = ironlake_crtc_dpms;
  5491. dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
  5492. dev_priv->display.off = ironlake_crtc_off;
  5493. dev_priv->display.update_plane = ironlake_update_plane;
  5494. } else {
  5495. dev_priv->display.dpms = i9xx_crtc_dpms;
  5496. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  5497. dev_priv->display.off = i9xx_crtc_off;
  5498. dev_priv->display.update_plane = i9xx_update_plane;
  5499. }
  5500. /* Returns the core display clock speed */
  5501. if (IS_VALLEYVIEW(dev))
  5502. dev_priv->display.get_display_clock_speed =
  5503. valleyview_get_display_clock_speed;
  5504. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  5505. dev_priv->display.get_display_clock_speed =
  5506. i945_get_display_clock_speed;
  5507. else if (IS_I915G(dev))
  5508. dev_priv->display.get_display_clock_speed =
  5509. i915_get_display_clock_speed;
  5510. else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
  5511. dev_priv->display.get_display_clock_speed =
  5512. i9xx_misc_get_display_clock_speed;
  5513. else if (IS_I915GM(dev))
  5514. dev_priv->display.get_display_clock_speed =
  5515. i915gm_get_display_clock_speed;
  5516. else if (IS_I865G(dev))
  5517. dev_priv->display.get_display_clock_speed =
  5518. i865_get_display_clock_speed;
  5519. else if (IS_I85X(dev))
  5520. dev_priv->display.get_display_clock_speed =
  5521. i855_get_display_clock_speed;
  5522. else /* 852, 830 */
  5523. dev_priv->display.get_display_clock_speed =
  5524. i830_get_display_clock_speed;
  5525. if (HAS_PCH_SPLIT(dev)) {
  5526. if (IS_GEN5(dev)) {
  5527. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  5528. dev_priv->display.write_eld = ironlake_write_eld;
  5529. } else if (IS_GEN6(dev)) {
  5530. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  5531. dev_priv->display.write_eld = ironlake_write_eld;
  5532. } else if (IS_IVYBRIDGE(dev)) {
  5533. /* FIXME: detect B0+ stepping and use auto training */
  5534. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  5535. dev_priv->display.write_eld = ironlake_write_eld;
  5536. } else
  5537. dev_priv->display.update_wm = NULL;
  5538. } else if (IS_VALLEYVIEW(dev)) {
  5539. dev_priv->display.force_wake_get = vlv_force_wake_get;
  5540. dev_priv->display.force_wake_put = vlv_force_wake_put;
  5541. } else if (IS_G4X(dev)) {
  5542. dev_priv->display.write_eld = g4x_write_eld;
  5543. }
  5544. /* Default just returns -ENODEV to indicate unsupported */
  5545. dev_priv->display.queue_flip = intel_default_queue_flip;
  5546. switch (INTEL_INFO(dev)->gen) {
  5547. case 2:
  5548. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  5549. break;
  5550. case 3:
  5551. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  5552. break;
  5553. case 4:
  5554. case 5:
  5555. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  5556. break;
  5557. case 6:
  5558. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  5559. break;
  5560. case 7:
  5561. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  5562. break;
  5563. }
  5564. }
  5565. /*
  5566. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  5567. * resume, or other times. This quirk makes sure that's the case for
  5568. * affected systems.
  5569. */
  5570. static void quirk_pipea_force(struct drm_device *dev)
  5571. {
  5572. struct drm_i915_private *dev_priv = dev->dev_private;
  5573. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  5574. DRM_INFO("applying pipe a force quirk\n");
  5575. }
  5576. /*
  5577. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  5578. */
  5579. static void quirk_ssc_force_disable(struct drm_device *dev)
  5580. {
  5581. struct drm_i915_private *dev_priv = dev->dev_private;
  5582. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  5583. DRM_INFO("applying lvds SSC disable quirk\n");
  5584. }
  5585. /*
  5586. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  5587. * brightness value
  5588. */
  5589. static void quirk_invert_brightness(struct drm_device *dev)
  5590. {
  5591. struct drm_i915_private *dev_priv = dev->dev_private;
  5592. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  5593. DRM_INFO("applying inverted panel brightness quirk\n");
  5594. }
  5595. struct intel_quirk {
  5596. int device;
  5597. int subsystem_vendor;
  5598. int subsystem_device;
  5599. void (*hook)(struct drm_device *dev);
  5600. };
  5601. static struct intel_quirk intel_quirks[] = {
  5602. /* HP Mini needs pipe A force quirk (LP: #322104) */
  5603. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  5604. /* Thinkpad R31 needs pipe A force quirk */
  5605. { 0x3577, 0x1014, 0x0505, quirk_pipea_force },
  5606. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  5607. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  5608. /* ThinkPad X30 needs pipe A force quirk (LP: #304614) */
  5609. { 0x3577, 0x1014, 0x0513, quirk_pipea_force },
  5610. /* ThinkPad X40 needs pipe A force quirk */
  5611. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  5612. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  5613. /* 855 & before need to leave pipe A & dpll A up */
  5614. { 0x3582, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  5615. { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  5616. /* Lenovo U160 cannot use SSC on LVDS */
  5617. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  5618. /* Sony Vaio Y cannot use SSC on LVDS */
  5619. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  5620. /* Acer Aspire 5734Z must invert backlight brightness */
  5621. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  5622. };
  5623. static void intel_init_quirks(struct drm_device *dev)
  5624. {
  5625. struct pci_dev *d = dev->pdev;
  5626. int i;
  5627. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  5628. struct intel_quirk *q = &intel_quirks[i];
  5629. if (d->device == q->device &&
  5630. (d->subsystem_vendor == q->subsystem_vendor ||
  5631. q->subsystem_vendor == PCI_ANY_ID) &&
  5632. (d->subsystem_device == q->subsystem_device ||
  5633. q->subsystem_device == PCI_ANY_ID))
  5634. q->hook(dev);
  5635. }
  5636. }
  5637. /* Disable the VGA plane that we never use */
  5638. static void i915_disable_vga(struct drm_device *dev)
  5639. {
  5640. struct drm_i915_private *dev_priv = dev->dev_private;
  5641. u8 sr1;
  5642. u32 vga_reg;
  5643. if (HAS_PCH_SPLIT(dev))
  5644. vga_reg = CPU_VGACNTRL;
  5645. else
  5646. vga_reg = VGACNTRL;
  5647. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  5648. outb(SR01, VGA_SR_INDEX);
  5649. sr1 = inb(VGA_SR_DATA);
  5650. outb(sr1 | 1<<5, VGA_SR_DATA);
  5651. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  5652. udelay(300);
  5653. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  5654. POSTING_READ(vga_reg);
  5655. }
  5656. static void ivb_pch_pwm_override(struct drm_device *dev)
  5657. {
  5658. struct drm_i915_private *dev_priv = dev->dev_private;
  5659. /*
  5660. * IVB has CPU eDP backlight regs too, set things up to let the
  5661. * PCH regs control the backlight
  5662. */
  5663. I915_WRITE(BLC_PWM_CPU_CTL2, PWM_ENABLE);
  5664. I915_WRITE(BLC_PWM_CPU_CTL, 0);
  5665. I915_WRITE(BLC_PWM_PCH_CTL1, PWM_ENABLE | (1<<30));
  5666. }
  5667. void intel_modeset_init_hw(struct drm_device *dev)
  5668. {
  5669. struct drm_i915_private *dev_priv = dev->dev_private;
  5670. intel_init_clock_gating(dev);
  5671. if (IS_IRONLAKE_M(dev)) {
  5672. ironlake_enable_drps(dev);
  5673. intel_init_emon(dev);
  5674. }
  5675. if ((IS_GEN6(dev) || IS_GEN7(dev)) && !IS_VALLEYVIEW(dev)) {
  5676. gen6_enable_rps(dev_priv);
  5677. gen6_update_ring_freq(dev_priv);
  5678. }
  5679. if (IS_IVYBRIDGE(dev))
  5680. ivb_pch_pwm_override(dev);
  5681. }
  5682. void intel_modeset_init(struct drm_device *dev)
  5683. {
  5684. struct drm_i915_private *dev_priv = dev->dev_private;
  5685. int i, ret;
  5686. drm_mode_config_init(dev);
  5687. dev->mode_config.min_width = 0;
  5688. dev->mode_config.min_height = 0;
  5689. dev->mode_config.preferred_depth = 24;
  5690. dev->mode_config.prefer_shadow = 1;
  5691. dev->mode_config.funcs = (void *)&intel_mode_funcs;
  5692. intel_init_quirks(dev);
  5693. intel_init_pm(dev);
  5694. intel_init_display(dev);
  5695. if (IS_GEN2(dev)) {
  5696. dev->mode_config.max_width = 2048;
  5697. dev->mode_config.max_height = 2048;
  5698. } else if (IS_GEN3(dev)) {
  5699. dev->mode_config.max_width = 4096;
  5700. dev->mode_config.max_height = 4096;
  5701. } else {
  5702. dev->mode_config.max_width = 8192;
  5703. dev->mode_config.max_height = 8192;
  5704. }
  5705. dev->mode_config.fb_base = dev->agp->base;
  5706. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  5707. dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
  5708. for (i = 0; i < dev_priv->num_pipe; i++) {
  5709. intel_crtc_init(dev, i);
  5710. ret = intel_plane_init(dev, i);
  5711. if (ret)
  5712. DRM_DEBUG_KMS("plane %d init failed: %d\n", i, ret);
  5713. }
  5714. intel_pch_pll_init(dev);
  5715. /* Just disable it once at startup */
  5716. i915_disable_vga(dev);
  5717. intel_setup_outputs(dev);
  5718. intel_modeset_init_hw(dev);
  5719. INIT_WORK(&dev_priv->idle_work, intel_idle_update);
  5720. setup_timer(&dev_priv->idle_timer, intel_gpu_idle_timer,
  5721. (unsigned long)dev);
  5722. }
  5723. void intel_modeset_gem_init(struct drm_device *dev)
  5724. {
  5725. if (IS_IRONLAKE_M(dev))
  5726. ironlake_enable_rc6(dev);
  5727. intel_setup_overlay(dev);
  5728. }
  5729. void intel_modeset_cleanup(struct drm_device *dev)
  5730. {
  5731. struct drm_i915_private *dev_priv = dev->dev_private;
  5732. struct drm_crtc *crtc;
  5733. struct intel_crtc *intel_crtc;
  5734. drm_kms_helper_poll_fini(dev);
  5735. mutex_lock(&dev->struct_mutex);
  5736. intel_unregister_dsm_handler();
  5737. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5738. /* Skip inactive CRTCs */
  5739. if (!crtc->fb)
  5740. continue;
  5741. intel_crtc = to_intel_crtc(crtc);
  5742. intel_increase_pllclock(crtc);
  5743. }
  5744. intel_disable_fbc(dev);
  5745. if (IS_IRONLAKE_M(dev))
  5746. ironlake_disable_drps(dev);
  5747. if ((IS_GEN6(dev) || IS_GEN7(dev)) && !IS_VALLEYVIEW(dev))
  5748. gen6_disable_rps(dev);
  5749. if (IS_IRONLAKE_M(dev))
  5750. ironlake_disable_rc6(dev);
  5751. if (IS_VALLEYVIEW(dev))
  5752. vlv_init_dpio(dev);
  5753. mutex_unlock(&dev->struct_mutex);
  5754. /* Disable the irq before mode object teardown, for the irq might
  5755. * enqueue unpin/hotplug work. */
  5756. drm_irq_uninstall(dev);
  5757. cancel_work_sync(&dev_priv->hotplug_work);
  5758. cancel_work_sync(&dev_priv->rps_work);
  5759. /* flush any delayed tasks or pending work */
  5760. flush_scheduled_work();
  5761. /* Shut off idle work before the crtcs get freed. */
  5762. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5763. intel_crtc = to_intel_crtc(crtc);
  5764. del_timer_sync(&intel_crtc->idle_timer);
  5765. }
  5766. del_timer_sync(&dev_priv->idle_timer);
  5767. cancel_work_sync(&dev_priv->idle_work);
  5768. drm_mode_config_cleanup(dev);
  5769. }
  5770. /*
  5771. * Return which encoder is currently attached for connector.
  5772. */
  5773. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  5774. {
  5775. return &intel_attached_encoder(connector)->base;
  5776. }
  5777. void intel_connector_attach_encoder(struct intel_connector *connector,
  5778. struct intel_encoder *encoder)
  5779. {
  5780. connector->encoder = encoder;
  5781. drm_mode_connector_attach_encoder(&connector->base,
  5782. &encoder->base);
  5783. }
  5784. /*
  5785. * set vga decode state - true == enable VGA decode
  5786. */
  5787. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  5788. {
  5789. struct drm_i915_private *dev_priv = dev->dev_private;
  5790. u16 gmch_ctrl;
  5791. pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
  5792. if (state)
  5793. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  5794. else
  5795. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  5796. pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
  5797. return 0;
  5798. }
  5799. #ifdef CONFIG_DEBUG_FS
  5800. #include <linux/seq_file.h>
  5801. struct intel_display_error_state {
  5802. struct intel_cursor_error_state {
  5803. u32 control;
  5804. u32 position;
  5805. u32 base;
  5806. u32 size;
  5807. } cursor[2];
  5808. struct intel_pipe_error_state {
  5809. u32 conf;
  5810. u32 source;
  5811. u32 htotal;
  5812. u32 hblank;
  5813. u32 hsync;
  5814. u32 vtotal;
  5815. u32 vblank;
  5816. u32 vsync;
  5817. } pipe[2];
  5818. struct intel_plane_error_state {
  5819. u32 control;
  5820. u32 stride;
  5821. u32 size;
  5822. u32 pos;
  5823. u32 addr;
  5824. u32 surface;
  5825. u32 tile_offset;
  5826. } plane[2];
  5827. };
  5828. struct intel_display_error_state *
  5829. intel_display_capture_error_state(struct drm_device *dev)
  5830. {
  5831. drm_i915_private_t *dev_priv = dev->dev_private;
  5832. struct intel_display_error_state *error;
  5833. int i;
  5834. error = kmalloc(sizeof(*error), GFP_ATOMIC);
  5835. if (error == NULL)
  5836. return NULL;
  5837. for (i = 0; i < 2; i++) {
  5838. error->cursor[i].control = I915_READ(CURCNTR(i));
  5839. error->cursor[i].position = I915_READ(CURPOS(i));
  5840. error->cursor[i].base = I915_READ(CURBASE(i));
  5841. error->plane[i].control = I915_READ(DSPCNTR(i));
  5842. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  5843. error->plane[i].size = I915_READ(DSPSIZE(i));
  5844. error->plane[i].pos = I915_READ(DSPPOS(i));
  5845. error->plane[i].addr = I915_READ(DSPADDR(i));
  5846. if (INTEL_INFO(dev)->gen >= 4) {
  5847. error->plane[i].surface = I915_READ(DSPSURF(i));
  5848. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  5849. }
  5850. error->pipe[i].conf = I915_READ(PIPECONF(i));
  5851. error->pipe[i].source = I915_READ(PIPESRC(i));
  5852. error->pipe[i].htotal = I915_READ(HTOTAL(i));
  5853. error->pipe[i].hblank = I915_READ(HBLANK(i));
  5854. error->pipe[i].hsync = I915_READ(HSYNC(i));
  5855. error->pipe[i].vtotal = I915_READ(VTOTAL(i));
  5856. error->pipe[i].vblank = I915_READ(VBLANK(i));
  5857. error->pipe[i].vsync = I915_READ(VSYNC(i));
  5858. }
  5859. return error;
  5860. }
  5861. void
  5862. intel_display_print_error_state(struct seq_file *m,
  5863. struct drm_device *dev,
  5864. struct intel_display_error_state *error)
  5865. {
  5866. int i;
  5867. for (i = 0; i < 2; i++) {
  5868. seq_printf(m, "Pipe [%d]:\n", i);
  5869. seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
  5870. seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
  5871. seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
  5872. seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
  5873. seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
  5874. seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
  5875. seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
  5876. seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
  5877. seq_printf(m, "Plane [%d]:\n", i);
  5878. seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
  5879. seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  5880. seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
  5881. seq_printf(m, " POS: %08x\n", error->plane[i].pos);
  5882. seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  5883. if (INTEL_INFO(dev)->gen >= 4) {
  5884. seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
  5885. seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  5886. }
  5887. seq_printf(m, "Cursor [%d]:\n", i);
  5888. seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  5889. seq_printf(m, " POS: %08x\n", error->cursor[i].position);
  5890. seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
  5891. }
  5892. }
  5893. #endif