intel_display.c 249 KB

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