intel_display.c 269 KB

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