intel_display.c 239 KB

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