intel_display.c 255 KB

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