intel_display.c 244 KB

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