intel_display.c 287 KB

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