intel_display.c 255 KB

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