intel_display.c 257 KB

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