intel_display.c 251 KB

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