intel_display.c 237 KB

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