intel_display.c 257 KB

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