intel_display.c 301 KB

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