intel_display.c 225 KB

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