intel_display.c 344 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525
  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. #define DIV_ROUND_CLOSEST_ULL(ll, d) \
  43. ({ unsigned long long _tmp = (ll)+(d)/2; do_div(_tmp, d); _tmp; })
  44. static void intel_increase_pllclock(struct drm_crtc *crtc);
  45. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  46. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  47. struct intel_crtc_config *pipe_config);
  48. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  49. struct intel_crtc_config *pipe_config);
  50. static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
  51. int x, int y, struct drm_framebuffer *old_fb);
  52. static int intel_framebuffer_init(struct drm_device *dev,
  53. struct intel_framebuffer *ifb,
  54. struct drm_mode_fb_cmd2 *mode_cmd,
  55. struct drm_i915_gem_object *obj);
  56. static void intel_dp_set_m_n(struct intel_crtc *crtc);
  57. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  58. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  59. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  60. struct intel_link_m_n *m_n);
  61. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  62. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  63. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  64. static void vlv_prepare_pll(struct intel_crtc *crtc);
  65. typedef struct {
  66. int min, max;
  67. } intel_range_t;
  68. typedef struct {
  69. int dot_limit;
  70. int p2_slow, p2_fast;
  71. } intel_p2_t;
  72. typedef struct intel_limit intel_limit_t;
  73. struct intel_limit {
  74. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  75. intel_p2_t p2;
  76. };
  77. int
  78. intel_pch_rawclk(struct drm_device *dev)
  79. {
  80. struct drm_i915_private *dev_priv = dev->dev_private;
  81. WARN_ON(!HAS_PCH_SPLIT(dev));
  82. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  83. }
  84. static inline u32 /* units of 100MHz */
  85. intel_fdi_link_freq(struct drm_device *dev)
  86. {
  87. if (IS_GEN5(dev)) {
  88. struct drm_i915_private *dev_priv = dev->dev_private;
  89. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  90. } else
  91. return 27;
  92. }
  93. static const intel_limit_t intel_limits_i8xx_dac = {
  94. .dot = { .min = 25000, .max = 350000 },
  95. .vco = { .min = 908000, .max = 1512000 },
  96. .n = { .min = 2, .max = 16 },
  97. .m = { .min = 96, .max = 140 },
  98. .m1 = { .min = 18, .max = 26 },
  99. .m2 = { .min = 6, .max = 16 },
  100. .p = { .min = 4, .max = 128 },
  101. .p1 = { .min = 2, .max = 33 },
  102. .p2 = { .dot_limit = 165000,
  103. .p2_slow = 4, .p2_fast = 2 },
  104. };
  105. static const intel_limit_t intel_limits_i8xx_dvo = {
  106. .dot = { .min = 25000, .max = 350000 },
  107. .vco = { .min = 908000, .max = 1512000 },
  108. .n = { .min = 2, .max = 16 },
  109. .m = { .min = 96, .max = 140 },
  110. .m1 = { .min = 18, .max = 26 },
  111. .m2 = { .min = 6, .max = 16 },
  112. .p = { .min = 4, .max = 128 },
  113. .p1 = { .min = 2, .max = 33 },
  114. .p2 = { .dot_limit = 165000,
  115. .p2_slow = 4, .p2_fast = 4 },
  116. };
  117. static const intel_limit_t intel_limits_i8xx_lvds = {
  118. .dot = { .min = 25000, .max = 350000 },
  119. .vco = { .min = 908000, .max = 1512000 },
  120. .n = { .min = 2, .max = 16 },
  121. .m = { .min = 96, .max = 140 },
  122. .m1 = { .min = 18, .max = 26 },
  123. .m2 = { .min = 6, .max = 16 },
  124. .p = { .min = 4, .max = 128 },
  125. .p1 = { .min = 1, .max = 6 },
  126. .p2 = { .dot_limit = 165000,
  127. .p2_slow = 14, .p2_fast = 7 },
  128. };
  129. static const intel_limit_t intel_limits_i9xx_sdvo = {
  130. .dot = { .min = 20000, .max = 400000 },
  131. .vco = { .min = 1400000, .max = 2800000 },
  132. .n = { .min = 1, .max = 6 },
  133. .m = { .min = 70, .max = 120 },
  134. .m1 = { .min = 8, .max = 18 },
  135. .m2 = { .min = 3, .max = 7 },
  136. .p = { .min = 5, .max = 80 },
  137. .p1 = { .min = 1, .max = 8 },
  138. .p2 = { .dot_limit = 200000,
  139. .p2_slow = 10, .p2_fast = 5 },
  140. };
  141. static const intel_limit_t intel_limits_i9xx_lvds = {
  142. .dot = { .min = 20000, .max = 400000 },
  143. .vco = { .min = 1400000, .max = 2800000 },
  144. .n = { .min = 1, .max = 6 },
  145. .m = { .min = 70, .max = 120 },
  146. .m1 = { .min = 8, .max = 18 },
  147. .m2 = { .min = 3, .max = 7 },
  148. .p = { .min = 7, .max = 98 },
  149. .p1 = { .min = 1, .max = 8 },
  150. .p2 = { .dot_limit = 112000,
  151. .p2_slow = 14, .p2_fast = 7 },
  152. };
  153. static const intel_limit_t intel_limits_g4x_sdvo = {
  154. .dot = { .min = 25000, .max = 270000 },
  155. .vco = { .min = 1750000, .max = 3500000},
  156. .n = { .min = 1, .max = 4 },
  157. .m = { .min = 104, .max = 138 },
  158. .m1 = { .min = 17, .max = 23 },
  159. .m2 = { .min = 5, .max = 11 },
  160. .p = { .min = 10, .max = 30 },
  161. .p1 = { .min = 1, .max = 3},
  162. .p2 = { .dot_limit = 270000,
  163. .p2_slow = 10,
  164. .p2_fast = 10
  165. },
  166. };
  167. static const intel_limit_t intel_limits_g4x_hdmi = {
  168. .dot = { .min = 22000, .max = 400000 },
  169. .vco = { .min = 1750000, .max = 3500000},
  170. .n = { .min = 1, .max = 4 },
  171. .m = { .min = 104, .max = 138 },
  172. .m1 = { .min = 16, .max = 23 },
  173. .m2 = { .min = 5, .max = 11 },
  174. .p = { .min = 5, .max = 80 },
  175. .p1 = { .min = 1, .max = 8},
  176. .p2 = { .dot_limit = 165000,
  177. .p2_slow = 10, .p2_fast = 5 },
  178. };
  179. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  180. .dot = { .min = 20000, .max = 115000 },
  181. .vco = { .min = 1750000, .max = 3500000 },
  182. .n = { .min = 1, .max = 3 },
  183. .m = { .min = 104, .max = 138 },
  184. .m1 = { .min = 17, .max = 23 },
  185. .m2 = { .min = 5, .max = 11 },
  186. .p = { .min = 28, .max = 112 },
  187. .p1 = { .min = 2, .max = 8 },
  188. .p2 = { .dot_limit = 0,
  189. .p2_slow = 14, .p2_fast = 14
  190. },
  191. };
  192. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  193. .dot = { .min = 80000, .max = 224000 },
  194. .vco = { .min = 1750000, .max = 3500000 },
  195. .n = { .min = 1, .max = 3 },
  196. .m = { .min = 104, .max = 138 },
  197. .m1 = { .min = 17, .max = 23 },
  198. .m2 = { .min = 5, .max = 11 },
  199. .p = { .min = 14, .max = 42 },
  200. .p1 = { .min = 2, .max = 6 },
  201. .p2 = { .dot_limit = 0,
  202. .p2_slow = 7, .p2_fast = 7
  203. },
  204. };
  205. static const intel_limit_t intel_limits_pineview_sdvo = {
  206. .dot = { .min = 20000, .max = 400000},
  207. .vco = { .min = 1700000, .max = 3500000 },
  208. /* Pineview's Ncounter is a ring counter */
  209. .n = { .min = 3, .max = 6 },
  210. .m = { .min = 2, .max = 256 },
  211. /* Pineview only has one combined m divider, which we treat as m2. */
  212. .m1 = { .min = 0, .max = 0 },
  213. .m2 = { .min = 0, .max = 254 },
  214. .p = { .min = 5, .max = 80 },
  215. .p1 = { .min = 1, .max = 8 },
  216. .p2 = { .dot_limit = 200000,
  217. .p2_slow = 10, .p2_fast = 5 },
  218. };
  219. static const intel_limit_t intel_limits_pineview_lvds = {
  220. .dot = { .min = 20000, .max = 400000 },
  221. .vco = { .min = 1700000, .max = 3500000 },
  222. .n = { .min = 3, .max = 6 },
  223. .m = { .min = 2, .max = 256 },
  224. .m1 = { .min = 0, .max = 0 },
  225. .m2 = { .min = 0, .max = 254 },
  226. .p = { .min = 7, .max = 112 },
  227. .p1 = { .min = 1, .max = 8 },
  228. .p2 = { .dot_limit = 112000,
  229. .p2_slow = 14, .p2_fast = 14 },
  230. };
  231. /* Ironlake / Sandybridge
  232. *
  233. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  234. * the range value for them is (actual_value - 2).
  235. */
  236. static const intel_limit_t intel_limits_ironlake_dac = {
  237. .dot = { .min = 25000, .max = 350000 },
  238. .vco = { .min = 1760000, .max = 3510000 },
  239. .n = { .min = 1, .max = 5 },
  240. .m = { .min = 79, .max = 127 },
  241. .m1 = { .min = 12, .max = 22 },
  242. .m2 = { .min = 5, .max = 9 },
  243. .p = { .min = 5, .max = 80 },
  244. .p1 = { .min = 1, .max = 8 },
  245. .p2 = { .dot_limit = 225000,
  246. .p2_slow = 10, .p2_fast = 5 },
  247. };
  248. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  249. .dot = { .min = 25000, .max = 350000 },
  250. .vco = { .min = 1760000, .max = 3510000 },
  251. .n = { .min = 1, .max = 3 },
  252. .m = { .min = 79, .max = 118 },
  253. .m1 = { .min = 12, .max = 22 },
  254. .m2 = { .min = 5, .max = 9 },
  255. .p = { .min = 28, .max = 112 },
  256. .p1 = { .min = 2, .max = 8 },
  257. .p2 = { .dot_limit = 225000,
  258. .p2_slow = 14, .p2_fast = 14 },
  259. };
  260. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  261. .dot = { .min = 25000, .max = 350000 },
  262. .vco = { .min = 1760000, .max = 3510000 },
  263. .n = { .min = 1, .max = 3 },
  264. .m = { .min = 79, .max = 127 },
  265. .m1 = { .min = 12, .max = 22 },
  266. .m2 = { .min = 5, .max = 9 },
  267. .p = { .min = 14, .max = 56 },
  268. .p1 = { .min = 2, .max = 8 },
  269. .p2 = { .dot_limit = 225000,
  270. .p2_slow = 7, .p2_fast = 7 },
  271. };
  272. /* LVDS 100mhz refclk limits. */
  273. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  274. .dot = { .min = 25000, .max = 350000 },
  275. .vco = { .min = 1760000, .max = 3510000 },
  276. .n = { .min = 1, .max = 2 },
  277. .m = { .min = 79, .max = 126 },
  278. .m1 = { .min = 12, .max = 22 },
  279. .m2 = { .min = 5, .max = 9 },
  280. .p = { .min = 28, .max = 112 },
  281. .p1 = { .min = 2, .max = 8 },
  282. .p2 = { .dot_limit = 225000,
  283. .p2_slow = 14, .p2_fast = 14 },
  284. };
  285. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  286. .dot = { .min = 25000, .max = 350000 },
  287. .vco = { .min = 1760000, .max = 3510000 },
  288. .n = { .min = 1, .max = 3 },
  289. .m = { .min = 79, .max = 126 },
  290. .m1 = { .min = 12, .max = 22 },
  291. .m2 = { .min = 5, .max = 9 },
  292. .p = { .min = 14, .max = 42 },
  293. .p1 = { .min = 2, .max = 6 },
  294. .p2 = { .dot_limit = 225000,
  295. .p2_slow = 7, .p2_fast = 7 },
  296. };
  297. static const intel_limit_t intel_limits_vlv = {
  298. /*
  299. * These are the data rate limits (measured in fast clocks)
  300. * since those are the strictest limits we have. The fast
  301. * clock and actual rate limits are more relaxed, so checking
  302. * them would make no difference.
  303. */
  304. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  305. .vco = { .min = 4000000, .max = 6000000 },
  306. .n = { .min = 1, .max = 7 },
  307. .m1 = { .min = 2, .max = 3 },
  308. .m2 = { .min = 11, .max = 156 },
  309. .p1 = { .min = 2, .max = 3 },
  310. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  311. };
  312. static const intel_limit_t intel_limits_chv = {
  313. /*
  314. * These are the data rate limits (measured in fast clocks)
  315. * since those are the strictest limits we have. The fast
  316. * clock and actual rate limits are more relaxed, so checking
  317. * them would make no difference.
  318. */
  319. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  320. .vco = { .min = 4860000, .max = 6700000 },
  321. .n = { .min = 1, .max = 1 },
  322. .m1 = { .min = 2, .max = 2 },
  323. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  324. .p1 = { .min = 2, .max = 4 },
  325. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  326. };
  327. static void vlv_clock(int refclk, intel_clock_t *clock)
  328. {
  329. clock->m = clock->m1 * clock->m2;
  330. clock->p = clock->p1 * clock->p2;
  331. if (WARN_ON(clock->n == 0 || clock->p == 0))
  332. return;
  333. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  334. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  335. }
  336. /**
  337. * Returns whether any output on the specified pipe is of the specified type
  338. */
  339. static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
  340. {
  341. struct drm_device *dev = crtc->dev;
  342. struct intel_encoder *encoder;
  343. for_each_encoder_on_crtc(dev, crtc, encoder)
  344. if (encoder->type == type)
  345. return true;
  346. return false;
  347. }
  348. static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
  349. int refclk)
  350. {
  351. struct drm_device *dev = crtc->dev;
  352. const intel_limit_t *limit;
  353. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  354. if (intel_is_dual_link_lvds(dev)) {
  355. if (refclk == 100000)
  356. limit = &intel_limits_ironlake_dual_lvds_100m;
  357. else
  358. limit = &intel_limits_ironlake_dual_lvds;
  359. } else {
  360. if (refclk == 100000)
  361. limit = &intel_limits_ironlake_single_lvds_100m;
  362. else
  363. limit = &intel_limits_ironlake_single_lvds;
  364. }
  365. } else
  366. limit = &intel_limits_ironlake_dac;
  367. return limit;
  368. }
  369. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  370. {
  371. struct drm_device *dev = crtc->dev;
  372. const intel_limit_t *limit;
  373. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  374. if (intel_is_dual_link_lvds(dev))
  375. limit = &intel_limits_g4x_dual_channel_lvds;
  376. else
  377. limit = &intel_limits_g4x_single_channel_lvds;
  378. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  379. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  380. limit = &intel_limits_g4x_hdmi;
  381. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  382. limit = &intel_limits_g4x_sdvo;
  383. } else /* The option is for other outputs */
  384. limit = &intel_limits_i9xx_sdvo;
  385. return limit;
  386. }
  387. static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
  388. {
  389. struct drm_device *dev = crtc->dev;
  390. const intel_limit_t *limit;
  391. if (HAS_PCH_SPLIT(dev))
  392. limit = intel_ironlake_limit(crtc, refclk);
  393. else if (IS_G4X(dev)) {
  394. limit = intel_g4x_limit(crtc);
  395. } else if (IS_PINEVIEW(dev)) {
  396. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  397. limit = &intel_limits_pineview_lvds;
  398. else
  399. limit = &intel_limits_pineview_sdvo;
  400. } else if (IS_CHERRYVIEW(dev)) {
  401. limit = &intel_limits_chv;
  402. } else if (IS_VALLEYVIEW(dev)) {
  403. limit = &intel_limits_vlv;
  404. } else if (!IS_GEN2(dev)) {
  405. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  406. limit = &intel_limits_i9xx_lvds;
  407. else
  408. limit = &intel_limits_i9xx_sdvo;
  409. } else {
  410. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  411. limit = &intel_limits_i8xx_lvds;
  412. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
  413. limit = &intel_limits_i8xx_dvo;
  414. else
  415. limit = &intel_limits_i8xx_dac;
  416. }
  417. return limit;
  418. }
  419. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  420. static void pineview_clock(int refclk, intel_clock_t *clock)
  421. {
  422. clock->m = clock->m2 + 2;
  423. clock->p = clock->p1 * clock->p2;
  424. if (WARN_ON(clock->n == 0 || clock->p == 0))
  425. return;
  426. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  427. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  428. }
  429. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  430. {
  431. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  432. }
  433. static void i9xx_clock(int refclk, intel_clock_t *clock)
  434. {
  435. clock->m = i9xx_dpll_compute_m(clock);
  436. clock->p = clock->p1 * clock->p2;
  437. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  438. return;
  439. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  440. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  441. }
  442. static void chv_clock(int refclk, intel_clock_t *clock)
  443. {
  444. clock->m = clock->m1 * clock->m2;
  445. clock->p = clock->p1 * clock->p2;
  446. if (WARN_ON(clock->n == 0 || clock->p == 0))
  447. return;
  448. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  449. clock->n << 22);
  450. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  451. }
  452. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  453. /**
  454. * Returns whether the given set of divisors are valid for a given refclk with
  455. * the given connectors.
  456. */
  457. static bool intel_PLL_is_valid(struct drm_device *dev,
  458. const intel_limit_t *limit,
  459. const intel_clock_t *clock)
  460. {
  461. if (clock->n < limit->n.min || limit->n.max < clock->n)
  462. INTELPllInvalid("n out of range\n");
  463. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  464. INTELPllInvalid("p1 out of range\n");
  465. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  466. INTELPllInvalid("m2 out of range\n");
  467. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  468. INTELPllInvalid("m1 out of range\n");
  469. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
  470. if (clock->m1 <= clock->m2)
  471. INTELPllInvalid("m1 <= m2\n");
  472. if (!IS_VALLEYVIEW(dev)) {
  473. if (clock->p < limit->p.min || limit->p.max < clock->p)
  474. INTELPllInvalid("p out of range\n");
  475. if (clock->m < limit->m.min || limit->m.max < clock->m)
  476. INTELPllInvalid("m out of range\n");
  477. }
  478. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  479. INTELPllInvalid("vco out of range\n");
  480. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  481. * connector, etc., rather than just a single range.
  482. */
  483. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  484. INTELPllInvalid("dot out of range\n");
  485. return true;
  486. }
  487. static bool
  488. i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  489. int target, int refclk, intel_clock_t *match_clock,
  490. intel_clock_t *best_clock)
  491. {
  492. struct drm_device *dev = crtc->dev;
  493. intel_clock_t clock;
  494. int err = target;
  495. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  496. /*
  497. * For LVDS just rely on its current settings for dual-channel.
  498. * We haven't figured out how to reliably set up different
  499. * single/dual channel state, if we even can.
  500. */
  501. if (intel_is_dual_link_lvds(dev))
  502. clock.p2 = limit->p2.p2_fast;
  503. else
  504. clock.p2 = limit->p2.p2_slow;
  505. } else {
  506. if (target < limit->p2.dot_limit)
  507. clock.p2 = limit->p2.p2_slow;
  508. else
  509. clock.p2 = limit->p2.p2_fast;
  510. }
  511. memset(best_clock, 0, sizeof(*best_clock));
  512. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  513. clock.m1++) {
  514. for (clock.m2 = limit->m2.min;
  515. clock.m2 <= limit->m2.max; clock.m2++) {
  516. if (clock.m2 >= clock.m1)
  517. break;
  518. for (clock.n = limit->n.min;
  519. clock.n <= limit->n.max; clock.n++) {
  520. for (clock.p1 = limit->p1.min;
  521. clock.p1 <= limit->p1.max; clock.p1++) {
  522. int this_err;
  523. i9xx_clock(refclk, &clock);
  524. if (!intel_PLL_is_valid(dev, limit,
  525. &clock))
  526. continue;
  527. if (match_clock &&
  528. clock.p != match_clock->p)
  529. continue;
  530. this_err = abs(clock.dot - target);
  531. if (this_err < err) {
  532. *best_clock = clock;
  533. err = this_err;
  534. }
  535. }
  536. }
  537. }
  538. }
  539. return (err != target);
  540. }
  541. static bool
  542. pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  543. int target, int refclk, intel_clock_t *match_clock,
  544. intel_clock_t *best_clock)
  545. {
  546. struct drm_device *dev = crtc->dev;
  547. intel_clock_t clock;
  548. int err = target;
  549. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  550. /*
  551. * For LVDS just rely on its current settings for dual-channel.
  552. * We haven't figured out how to reliably set up different
  553. * single/dual channel state, if we even can.
  554. */
  555. if (intel_is_dual_link_lvds(dev))
  556. clock.p2 = limit->p2.p2_fast;
  557. else
  558. clock.p2 = limit->p2.p2_slow;
  559. } else {
  560. if (target < limit->p2.dot_limit)
  561. clock.p2 = limit->p2.p2_slow;
  562. else
  563. clock.p2 = limit->p2.p2_fast;
  564. }
  565. memset(best_clock, 0, sizeof(*best_clock));
  566. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  567. clock.m1++) {
  568. for (clock.m2 = limit->m2.min;
  569. clock.m2 <= limit->m2.max; clock.m2++) {
  570. for (clock.n = limit->n.min;
  571. clock.n <= limit->n.max; clock.n++) {
  572. for (clock.p1 = limit->p1.min;
  573. clock.p1 <= limit->p1.max; clock.p1++) {
  574. int this_err;
  575. pineview_clock(refclk, &clock);
  576. if (!intel_PLL_is_valid(dev, limit,
  577. &clock))
  578. continue;
  579. if (match_clock &&
  580. clock.p != match_clock->p)
  581. continue;
  582. this_err = abs(clock.dot - target);
  583. if (this_err < err) {
  584. *best_clock = clock;
  585. err = this_err;
  586. }
  587. }
  588. }
  589. }
  590. }
  591. return (err != target);
  592. }
  593. static bool
  594. g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  595. int target, int refclk, intel_clock_t *match_clock,
  596. intel_clock_t *best_clock)
  597. {
  598. struct drm_device *dev = crtc->dev;
  599. intel_clock_t clock;
  600. int max_n;
  601. bool found;
  602. /* approximately equals target * 0.00585 */
  603. int err_most = (target >> 8) + (target >> 9);
  604. found = false;
  605. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  606. if (intel_is_dual_link_lvds(dev))
  607. clock.p2 = limit->p2.p2_fast;
  608. else
  609. clock.p2 = limit->p2.p2_slow;
  610. } else {
  611. if (target < limit->p2.dot_limit)
  612. clock.p2 = limit->p2.p2_slow;
  613. else
  614. clock.p2 = limit->p2.p2_fast;
  615. }
  616. memset(best_clock, 0, sizeof(*best_clock));
  617. max_n = limit->n.max;
  618. /* based on hardware requirement, prefer smaller n to precision */
  619. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  620. /* based on hardware requirement, prefere larger m1,m2 */
  621. for (clock.m1 = limit->m1.max;
  622. clock.m1 >= limit->m1.min; clock.m1--) {
  623. for (clock.m2 = limit->m2.max;
  624. clock.m2 >= limit->m2.min; clock.m2--) {
  625. for (clock.p1 = limit->p1.max;
  626. clock.p1 >= limit->p1.min; clock.p1--) {
  627. int this_err;
  628. i9xx_clock(refclk, &clock);
  629. if (!intel_PLL_is_valid(dev, limit,
  630. &clock))
  631. continue;
  632. this_err = abs(clock.dot - target);
  633. if (this_err < err_most) {
  634. *best_clock = clock;
  635. err_most = this_err;
  636. max_n = clock.n;
  637. found = true;
  638. }
  639. }
  640. }
  641. }
  642. }
  643. return found;
  644. }
  645. static bool
  646. vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  647. int target, int refclk, intel_clock_t *match_clock,
  648. intel_clock_t *best_clock)
  649. {
  650. struct drm_device *dev = crtc->dev;
  651. intel_clock_t clock;
  652. unsigned int bestppm = 1000000;
  653. /* min update 19.2 MHz */
  654. int max_n = min(limit->n.max, refclk / 19200);
  655. bool found = false;
  656. target *= 5; /* fast clock */
  657. memset(best_clock, 0, sizeof(*best_clock));
  658. /* based on hardware requirement, prefer smaller n to precision */
  659. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  660. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  661. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  662. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  663. clock.p = clock.p1 * clock.p2;
  664. /* based on hardware requirement, prefer bigger m1,m2 values */
  665. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  666. unsigned int ppm, diff;
  667. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  668. refclk * clock.m1);
  669. vlv_clock(refclk, &clock);
  670. if (!intel_PLL_is_valid(dev, limit,
  671. &clock))
  672. continue;
  673. diff = abs(clock.dot - target);
  674. ppm = div_u64(1000000ULL * diff, target);
  675. if (ppm < 100 && clock.p > best_clock->p) {
  676. bestppm = 0;
  677. *best_clock = clock;
  678. found = true;
  679. }
  680. if (bestppm >= 10 && ppm < bestppm - 10) {
  681. bestppm = ppm;
  682. *best_clock = clock;
  683. found = true;
  684. }
  685. }
  686. }
  687. }
  688. }
  689. return found;
  690. }
  691. static bool
  692. chv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  693. int target, int refclk, intel_clock_t *match_clock,
  694. intel_clock_t *best_clock)
  695. {
  696. struct drm_device *dev = crtc->dev;
  697. intel_clock_t clock;
  698. uint64_t m2;
  699. int found = false;
  700. memset(best_clock, 0, sizeof(*best_clock));
  701. /*
  702. * Based on hardware doc, the n always set to 1, and m1 always
  703. * set to 2. If requires to support 200Mhz refclk, we need to
  704. * revisit this because n may not 1 anymore.
  705. */
  706. clock.n = 1, clock.m1 = 2;
  707. target *= 5; /* fast clock */
  708. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  709. for (clock.p2 = limit->p2.p2_fast;
  710. clock.p2 >= limit->p2.p2_slow;
  711. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  712. clock.p = clock.p1 * clock.p2;
  713. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  714. clock.n) << 22, refclk * clock.m1);
  715. if (m2 > INT_MAX/clock.m1)
  716. continue;
  717. clock.m2 = m2;
  718. chv_clock(refclk, &clock);
  719. if (!intel_PLL_is_valid(dev, limit, &clock))
  720. continue;
  721. /* based on hardware requirement, prefer bigger p
  722. */
  723. if (clock.p > best_clock->p) {
  724. *best_clock = clock;
  725. found = true;
  726. }
  727. }
  728. }
  729. return found;
  730. }
  731. bool intel_crtc_active(struct drm_crtc *crtc)
  732. {
  733. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  734. /* Be paranoid as we can arrive here with only partial
  735. * state retrieved from the hardware during setup.
  736. *
  737. * We can ditch the adjusted_mode.crtc_clock check as soon
  738. * as Haswell has gained clock readout/fastboot support.
  739. *
  740. * We can ditch the crtc->primary->fb check as soon as we can
  741. * properly reconstruct framebuffers.
  742. */
  743. return intel_crtc->active && crtc->primary->fb &&
  744. intel_crtc->config.adjusted_mode.crtc_clock;
  745. }
  746. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  747. enum pipe pipe)
  748. {
  749. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  750. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  751. return intel_crtc->config.cpu_transcoder;
  752. }
  753. static void g4x_wait_for_vblank(struct drm_device *dev, int pipe)
  754. {
  755. struct drm_i915_private *dev_priv = dev->dev_private;
  756. u32 frame, frame_reg = PIPE_FRMCOUNT_GM45(pipe);
  757. frame = I915_READ(frame_reg);
  758. if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
  759. WARN(1, "vblank wait timed out\n");
  760. }
  761. /**
  762. * intel_wait_for_vblank - wait for vblank on a given pipe
  763. * @dev: drm device
  764. * @pipe: pipe to wait for
  765. *
  766. * Wait for vblank to occur on a given pipe. Needed for various bits of
  767. * mode setting code.
  768. */
  769. void intel_wait_for_vblank(struct drm_device *dev, int pipe)
  770. {
  771. struct drm_i915_private *dev_priv = dev->dev_private;
  772. int pipestat_reg = PIPESTAT(pipe);
  773. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
  774. g4x_wait_for_vblank(dev, pipe);
  775. return;
  776. }
  777. /* Clear existing vblank status. Note this will clear any other
  778. * sticky status fields as well.
  779. *
  780. * This races with i915_driver_irq_handler() with the result
  781. * that either function could miss a vblank event. Here it is not
  782. * fatal, as we will either wait upon the next vblank interrupt or
  783. * timeout. Generally speaking intel_wait_for_vblank() is only
  784. * called during modeset at which time the GPU should be idle and
  785. * should *not* be performing page flips and thus not waiting on
  786. * vblanks...
  787. * Currently, the result of us stealing a vblank from the irq
  788. * handler is that a single frame will be skipped during swapbuffers.
  789. */
  790. I915_WRITE(pipestat_reg,
  791. I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
  792. /* Wait for vblank interrupt bit to set */
  793. if (wait_for(I915_READ(pipestat_reg) &
  794. PIPE_VBLANK_INTERRUPT_STATUS,
  795. 50))
  796. DRM_DEBUG_KMS("vblank wait timed out\n");
  797. }
  798. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  799. {
  800. struct drm_i915_private *dev_priv = dev->dev_private;
  801. u32 reg = PIPEDSL(pipe);
  802. u32 line1, line2;
  803. u32 line_mask;
  804. if (IS_GEN2(dev))
  805. line_mask = DSL_LINEMASK_GEN2;
  806. else
  807. line_mask = DSL_LINEMASK_GEN3;
  808. line1 = I915_READ(reg) & line_mask;
  809. mdelay(5);
  810. line2 = I915_READ(reg) & line_mask;
  811. return line1 == line2;
  812. }
  813. /*
  814. * intel_wait_for_pipe_off - wait for pipe to turn off
  815. * @dev: drm device
  816. * @pipe: pipe to wait for
  817. *
  818. * After disabling a pipe, we can't wait for vblank in the usual way,
  819. * spinning on the vblank interrupt status bit, since we won't actually
  820. * see an interrupt when the pipe is disabled.
  821. *
  822. * On Gen4 and above:
  823. * wait for the pipe register state bit to turn off
  824. *
  825. * Otherwise:
  826. * wait for the display line value to settle (it usually
  827. * ends up stopping at the start of the next frame).
  828. *
  829. */
  830. void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
  831. {
  832. struct drm_i915_private *dev_priv = dev->dev_private;
  833. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  834. pipe);
  835. if (INTEL_INFO(dev)->gen >= 4) {
  836. int reg = PIPECONF(cpu_transcoder);
  837. /* Wait for the Pipe State to go off */
  838. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  839. 100))
  840. WARN(1, "pipe_off wait timed out\n");
  841. } else {
  842. /* Wait for the display line to settle */
  843. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  844. WARN(1, "pipe_off wait timed out\n");
  845. }
  846. }
  847. /*
  848. * ibx_digital_port_connected - is the specified port connected?
  849. * @dev_priv: i915 private structure
  850. * @port: the port to test
  851. *
  852. * Returns true if @port is connected, false otherwise.
  853. */
  854. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  855. struct intel_digital_port *port)
  856. {
  857. u32 bit;
  858. if (HAS_PCH_IBX(dev_priv->dev)) {
  859. switch (port->port) {
  860. case PORT_B:
  861. bit = SDE_PORTB_HOTPLUG;
  862. break;
  863. case PORT_C:
  864. bit = SDE_PORTC_HOTPLUG;
  865. break;
  866. case PORT_D:
  867. bit = SDE_PORTD_HOTPLUG;
  868. break;
  869. default:
  870. return true;
  871. }
  872. } else {
  873. switch (port->port) {
  874. case PORT_B:
  875. bit = SDE_PORTB_HOTPLUG_CPT;
  876. break;
  877. case PORT_C:
  878. bit = SDE_PORTC_HOTPLUG_CPT;
  879. break;
  880. case PORT_D:
  881. bit = SDE_PORTD_HOTPLUG_CPT;
  882. break;
  883. default:
  884. return true;
  885. }
  886. }
  887. return I915_READ(SDEISR) & bit;
  888. }
  889. static const char *state_string(bool enabled)
  890. {
  891. return enabled ? "on" : "off";
  892. }
  893. /* Only for pre-ILK configs */
  894. void assert_pll(struct drm_i915_private *dev_priv,
  895. enum pipe pipe, bool state)
  896. {
  897. int reg;
  898. u32 val;
  899. bool cur_state;
  900. reg = DPLL(pipe);
  901. val = I915_READ(reg);
  902. cur_state = !!(val & DPLL_VCO_ENABLE);
  903. WARN(cur_state != state,
  904. "PLL state assertion failure (expected %s, current %s)\n",
  905. state_string(state), state_string(cur_state));
  906. }
  907. /* XXX: the dsi pll is shared between MIPI DSI ports */
  908. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  909. {
  910. u32 val;
  911. bool cur_state;
  912. mutex_lock(&dev_priv->dpio_lock);
  913. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  914. mutex_unlock(&dev_priv->dpio_lock);
  915. cur_state = val & DSI_PLL_VCO_EN;
  916. WARN(cur_state != state,
  917. "DSI PLL state assertion failure (expected %s, current %s)\n",
  918. state_string(state), state_string(cur_state));
  919. }
  920. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  921. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  922. struct intel_shared_dpll *
  923. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  924. {
  925. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  926. if (crtc->config.shared_dpll < 0)
  927. return NULL;
  928. return &dev_priv->shared_dplls[crtc->config.shared_dpll];
  929. }
  930. /* For ILK+ */
  931. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  932. struct intel_shared_dpll *pll,
  933. bool state)
  934. {
  935. bool cur_state;
  936. struct intel_dpll_hw_state hw_state;
  937. if (HAS_PCH_LPT(dev_priv->dev)) {
  938. DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
  939. return;
  940. }
  941. if (WARN (!pll,
  942. "asserting DPLL %s with no DPLL\n", state_string(state)))
  943. return;
  944. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  945. WARN(cur_state != state,
  946. "%s assertion failure (expected %s, current %s)\n",
  947. pll->name, state_string(state), state_string(cur_state));
  948. }
  949. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  950. enum pipe pipe, bool state)
  951. {
  952. int reg;
  953. u32 val;
  954. bool cur_state;
  955. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  956. pipe);
  957. if (HAS_DDI(dev_priv->dev)) {
  958. /* DDI does not have a specific FDI_TX register */
  959. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  960. val = I915_READ(reg);
  961. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  962. } else {
  963. reg = FDI_TX_CTL(pipe);
  964. val = I915_READ(reg);
  965. cur_state = !!(val & FDI_TX_ENABLE);
  966. }
  967. WARN(cur_state != state,
  968. "FDI TX state assertion failure (expected %s, current %s)\n",
  969. state_string(state), state_string(cur_state));
  970. }
  971. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  972. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  973. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  974. enum pipe pipe, bool state)
  975. {
  976. int reg;
  977. u32 val;
  978. bool cur_state;
  979. reg = FDI_RX_CTL(pipe);
  980. val = I915_READ(reg);
  981. cur_state = !!(val & FDI_RX_ENABLE);
  982. WARN(cur_state != state,
  983. "FDI RX state assertion failure (expected %s, current %s)\n",
  984. state_string(state), state_string(cur_state));
  985. }
  986. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  987. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  988. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  989. enum pipe pipe)
  990. {
  991. int reg;
  992. u32 val;
  993. /* ILK FDI PLL is always enabled */
  994. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  995. return;
  996. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  997. if (HAS_DDI(dev_priv->dev))
  998. return;
  999. reg = FDI_TX_CTL(pipe);
  1000. val = I915_READ(reg);
  1001. WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1002. }
  1003. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1004. enum pipe pipe, bool state)
  1005. {
  1006. int reg;
  1007. u32 val;
  1008. bool cur_state;
  1009. reg = FDI_RX_CTL(pipe);
  1010. val = I915_READ(reg);
  1011. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1012. WARN(cur_state != state,
  1013. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1014. state_string(state), state_string(cur_state));
  1015. }
  1016. static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1017. enum pipe pipe)
  1018. {
  1019. int pp_reg, lvds_reg;
  1020. u32 val;
  1021. enum pipe panel_pipe = PIPE_A;
  1022. bool locked = true;
  1023. if (HAS_PCH_SPLIT(dev_priv->dev)) {
  1024. pp_reg = PCH_PP_CONTROL;
  1025. lvds_reg = PCH_LVDS;
  1026. } else {
  1027. pp_reg = PP_CONTROL;
  1028. lvds_reg = LVDS;
  1029. }
  1030. val = I915_READ(pp_reg);
  1031. if (!(val & PANEL_POWER_ON) ||
  1032. ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
  1033. locked = false;
  1034. if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
  1035. panel_pipe = PIPE_B;
  1036. WARN(panel_pipe == pipe && locked,
  1037. "panel assertion failure, pipe %c regs locked\n",
  1038. pipe_name(pipe));
  1039. }
  1040. static void assert_cursor(struct drm_i915_private *dev_priv,
  1041. enum pipe pipe, bool state)
  1042. {
  1043. struct drm_device *dev = dev_priv->dev;
  1044. bool cur_state;
  1045. if (IS_845G(dev) || IS_I865G(dev))
  1046. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1047. else
  1048. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1049. WARN(cur_state != state,
  1050. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1051. pipe_name(pipe), state_string(state), state_string(cur_state));
  1052. }
  1053. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1054. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1055. void assert_pipe(struct drm_i915_private *dev_priv,
  1056. enum pipe pipe, bool state)
  1057. {
  1058. int reg;
  1059. u32 val;
  1060. bool cur_state;
  1061. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1062. pipe);
  1063. /* if we need the pipe A quirk it must be always on */
  1064. if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
  1065. state = true;
  1066. if (!intel_display_power_enabled(dev_priv,
  1067. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1068. cur_state = false;
  1069. } else {
  1070. reg = PIPECONF(cpu_transcoder);
  1071. val = I915_READ(reg);
  1072. cur_state = !!(val & PIPECONF_ENABLE);
  1073. }
  1074. WARN(cur_state != state,
  1075. "pipe %c assertion failure (expected %s, current %s)\n",
  1076. pipe_name(pipe), state_string(state), state_string(cur_state));
  1077. }
  1078. static void assert_plane(struct drm_i915_private *dev_priv,
  1079. enum plane plane, bool state)
  1080. {
  1081. int reg;
  1082. u32 val;
  1083. bool cur_state;
  1084. reg = DSPCNTR(plane);
  1085. val = I915_READ(reg);
  1086. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1087. WARN(cur_state != state,
  1088. "plane %c assertion failure (expected %s, current %s)\n",
  1089. plane_name(plane), state_string(state), state_string(cur_state));
  1090. }
  1091. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1092. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1093. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1094. enum pipe pipe)
  1095. {
  1096. struct drm_device *dev = dev_priv->dev;
  1097. int reg, i;
  1098. u32 val;
  1099. int cur_pipe;
  1100. /* Primary planes are fixed to pipes on gen4+ */
  1101. if (INTEL_INFO(dev)->gen >= 4) {
  1102. reg = DSPCNTR(pipe);
  1103. val = I915_READ(reg);
  1104. WARN(val & DISPLAY_PLANE_ENABLE,
  1105. "plane %c assertion failure, should be disabled but not\n",
  1106. plane_name(pipe));
  1107. return;
  1108. }
  1109. /* Need to check both planes against the pipe */
  1110. for_each_pipe(i) {
  1111. reg = DSPCNTR(i);
  1112. val = I915_READ(reg);
  1113. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1114. DISPPLANE_SEL_PIPE_SHIFT;
  1115. WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1116. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1117. plane_name(i), pipe_name(pipe));
  1118. }
  1119. }
  1120. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1121. enum pipe pipe)
  1122. {
  1123. struct drm_device *dev = dev_priv->dev;
  1124. int reg, sprite;
  1125. u32 val;
  1126. if (IS_VALLEYVIEW(dev)) {
  1127. for_each_sprite(pipe, sprite) {
  1128. reg = SPCNTR(pipe, sprite);
  1129. val = I915_READ(reg);
  1130. WARN(val & SP_ENABLE,
  1131. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1132. sprite_name(pipe, sprite), pipe_name(pipe));
  1133. }
  1134. } else if (INTEL_INFO(dev)->gen >= 7) {
  1135. reg = SPRCTL(pipe);
  1136. val = I915_READ(reg);
  1137. WARN(val & SPRITE_ENABLE,
  1138. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1139. plane_name(pipe), pipe_name(pipe));
  1140. } else if (INTEL_INFO(dev)->gen >= 5) {
  1141. reg = DVSCNTR(pipe);
  1142. val = I915_READ(reg);
  1143. WARN(val & DVS_ENABLE,
  1144. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1145. plane_name(pipe), pipe_name(pipe));
  1146. }
  1147. }
  1148. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1149. {
  1150. u32 val;
  1151. bool enabled;
  1152. WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1153. val = I915_READ(PCH_DREF_CONTROL);
  1154. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1155. DREF_SUPERSPREAD_SOURCE_MASK));
  1156. WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1157. }
  1158. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1159. enum pipe pipe)
  1160. {
  1161. int reg;
  1162. u32 val;
  1163. bool enabled;
  1164. reg = PCH_TRANSCONF(pipe);
  1165. val = I915_READ(reg);
  1166. enabled = !!(val & TRANS_ENABLE);
  1167. WARN(enabled,
  1168. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1169. pipe_name(pipe));
  1170. }
  1171. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1172. enum pipe pipe, u32 port_sel, u32 val)
  1173. {
  1174. if ((val & DP_PORT_EN) == 0)
  1175. return false;
  1176. if (HAS_PCH_CPT(dev_priv->dev)) {
  1177. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1178. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1179. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1180. return false;
  1181. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1182. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1183. return false;
  1184. } else {
  1185. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1186. return false;
  1187. }
  1188. return true;
  1189. }
  1190. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1191. enum pipe pipe, u32 val)
  1192. {
  1193. if ((val & SDVO_ENABLE) == 0)
  1194. return false;
  1195. if (HAS_PCH_CPT(dev_priv->dev)) {
  1196. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1197. return false;
  1198. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1199. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1200. return false;
  1201. } else {
  1202. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1203. return false;
  1204. }
  1205. return true;
  1206. }
  1207. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1208. enum pipe pipe, u32 val)
  1209. {
  1210. if ((val & LVDS_PORT_EN) == 0)
  1211. return false;
  1212. if (HAS_PCH_CPT(dev_priv->dev)) {
  1213. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1214. return false;
  1215. } else {
  1216. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1217. return false;
  1218. }
  1219. return true;
  1220. }
  1221. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1222. enum pipe pipe, u32 val)
  1223. {
  1224. if ((val & ADPA_DAC_ENABLE) == 0)
  1225. return false;
  1226. if (HAS_PCH_CPT(dev_priv->dev)) {
  1227. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1228. return false;
  1229. } else {
  1230. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1231. return false;
  1232. }
  1233. return true;
  1234. }
  1235. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1236. enum pipe pipe, int reg, u32 port_sel)
  1237. {
  1238. u32 val = I915_READ(reg);
  1239. WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1240. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1241. reg, pipe_name(pipe));
  1242. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1243. && (val & DP_PIPEB_SELECT),
  1244. "IBX PCH dp port still using transcoder B\n");
  1245. }
  1246. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1247. enum pipe pipe, int reg)
  1248. {
  1249. u32 val = I915_READ(reg);
  1250. WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1251. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1252. reg, pipe_name(pipe));
  1253. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1254. && (val & SDVO_PIPE_B_SELECT),
  1255. "IBX PCH hdmi port still using transcoder B\n");
  1256. }
  1257. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1258. enum pipe pipe)
  1259. {
  1260. int reg;
  1261. u32 val;
  1262. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1263. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1264. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1265. reg = PCH_ADPA;
  1266. val = I915_READ(reg);
  1267. WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1268. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1269. pipe_name(pipe));
  1270. reg = PCH_LVDS;
  1271. val = I915_READ(reg);
  1272. WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1273. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1274. pipe_name(pipe));
  1275. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1276. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1277. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1278. }
  1279. static void intel_init_dpio(struct drm_device *dev)
  1280. {
  1281. struct drm_i915_private *dev_priv = dev->dev_private;
  1282. if (!IS_VALLEYVIEW(dev))
  1283. return;
  1284. /*
  1285. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1286. * CHV x1 PHY (DP/HDMI D)
  1287. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1288. */
  1289. if (IS_CHERRYVIEW(dev)) {
  1290. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1291. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1292. } else {
  1293. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1294. }
  1295. }
  1296. static void intel_reset_dpio(struct drm_device *dev)
  1297. {
  1298. struct drm_i915_private *dev_priv = dev->dev_private;
  1299. if (!IS_VALLEYVIEW(dev))
  1300. return;
  1301. if (IS_CHERRYVIEW(dev)) {
  1302. enum dpio_phy phy;
  1303. u32 val;
  1304. for (phy = DPIO_PHY0; phy < I915_NUM_PHYS_VLV; phy++) {
  1305. /* Poll for phypwrgood signal */
  1306. if (wait_for(I915_READ(DISPLAY_PHY_STATUS) &
  1307. PHY_POWERGOOD(phy), 1))
  1308. DRM_ERROR("Display PHY %d is not power up\n", phy);
  1309. /*
  1310. * Deassert common lane reset for PHY.
  1311. *
  1312. * This should only be done on init and resume from S3
  1313. * with both PLLs disabled, or we risk losing DPIO and
  1314. * PLL synchronization.
  1315. */
  1316. val = I915_READ(DISPLAY_PHY_CONTROL);
  1317. I915_WRITE(DISPLAY_PHY_CONTROL,
  1318. PHY_COM_LANE_RESET_DEASSERT(phy, val));
  1319. }
  1320. } else {
  1321. /*
  1322. * If DPIO has already been reset, e.g. by BIOS, just skip all
  1323. * this.
  1324. */
  1325. if (I915_READ(DPIO_CTL) & DPIO_CMNRST)
  1326. return;
  1327. /*
  1328. * From VLV2A0_DP_eDP_HDMI_DPIO_driver_vbios_notes_11.docx:
  1329. * Need to assert and de-assert PHY SB reset by gating the
  1330. * common lane power, then un-gating it.
  1331. * Simply ungating isn't enough to reset the PHY enough to get
  1332. * ports and lanes running.
  1333. */
  1334. __vlv_set_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC,
  1335. false);
  1336. __vlv_set_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC,
  1337. true);
  1338. }
  1339. }
  1340. static void vlv_enable_pll(struct intel_crtc *crtc)
  1341. {
  1342. struct drm_device *dev = crtc->base.dev;
  1343. struct drm_i915_private *dev_priv = dev->dev_private;
  1344. int reg = DPLL(crtc->pipe);
  1345. u32 dpll = crtc->config.dpll_hw_state.dpll;
  1346. assert_pipe_disabled(dev_priv, crtc->pipe);
  1347. /* No really, not for ILK+ */
  1348. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1349. /* PLL is protected by panel, make sure we can write it */
  1350. if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
  1351. assert_panel_unlocked(dev_priv, crtc->pipe);
  1352. I915_WRITE(reg, dpll);
  1353. POSTING_READ(reg);
  1354. udelay(150);
  1355. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1356. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1357. I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
  1358. POSTING_READ(DPLL_MD(crtc->pipe));
  1359. /* We do this three times for luck */
  1360. I915_WRITE(reg, dpll);
  1361. POSTING_READ(reg);
  1362. udelay(150); /* wait for warmup */
  1363. I915_WRITE(reg, dpll);
  1364. POSTING_READ(reg);
  1365. udelay(150); /* wait for warmup */
  1366. I915_WRITE(reg, dpll);
  1367. POSTING_READ(reg);
  1368. udelay(150); /* wait for warmup */
  1369. }
  1370. static void chv_enable_pll(struct intel_crtc *crtc)
  1371. {
  1372. struct drm_device *dev = crtc->base.dev;
  1373. struct drm_i915_private *dev_priv = dev->dev_private;
  1374. int pipe = crtc->pipe;
  1375. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1376. u32 tmp;
  1377. assert_pipe_disabled(dev_priv, crtc->pipe);
  1378. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1379. mutex_lock(&dev_priv->dpio_lock);
  1380. /* Enable back the 10bit clock to display controller */
  1381. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1382. tmp |= DPIO_DCLKP_EN;
  1383. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1384. /*
  1385. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1386. */
  1387. udelay(1);
  1388. /* Enable PLL */
  1389. I915_WRITE(DPLL(pipe), crtc->config.dpll_hw_state.dpll);
  1390. /* Check PLL is locked */
  1391. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1392. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1393. /* not sure when this should be written */
  1394. I915_WRITE(DPLL_MD(pipe), crtc->config.dpll_hw_state.dpll_md);
  1395. POSTING_READ(DPLL_MD(pipe));
  1396. mutex_unlock(&dev_priv->dpio_lock);
  1397. }
  1398. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1399. {
  1400. struct drm_device *dev = crtc->base.dev;
  1401. struct drm_i915_private *dev_priv = dev->dev_private;
  1402. int reg = DPLL(crtc->pipe);
  1403. u32 dpll = crtc->config.dpll_hw_state.dpll;
  1404. assert_pipe_disabled(dev_priv, crtc->pipe);
  1405. /* No really, not for ILK+ */
  1406. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1407. /* PLL is protected by panel, make sure we can write it */
  1408. if (IS_MOBILE(dev) && !IS_I830(dev))
  1409. assert_panel_unlocked(dev_priv, crtc->pipe);
  1410. I915_WRITE(reg, dpll);
  1411. /* Wait for the clocks to stabilize. */
  1412. POSTING_READ(reg);
  1413. udelay(150);
  1414. if (INTEL_INFO(dev)->gen >= 4) {
  1415. I915_WRITE(DPLL_MD(crtc->pipe),
  1416. crtc->config.dpll_hw_state.dpll_md);
  1417. } else {
  1418. /* The pixel multiplier can only be updated once the
  1419. * DPLL is enabled and the clocks are stable.
  1420. *
  1421. * So write it again.
  1422. */
  1423. I915_WRITE(reg, dpll);
  1424. }
  1425. /* We do this three times for luck */
  1426. I915_WRITE(reg, dpll);
  1427. POSTING_READ(reg);
  1428. udelay(150); /* wait for warmup */
  1429. I915_WRITE(reg, dpll);
  1430. POSTING_READ(reg);
  1431. udelay(150); /* wait for warmup */
  1432. I915_WRITE(reg, dpll);
  1433. POSTING_READ(reg);
  1434. udelay(150); /* wait for warmup */
  1435. }
  1436. /**
  1437. * i9xx_disable_pll - disable a PLL
  1438. * @dev_priv: i915 private structure
  1439. * @pipe: pipe PLL to disable
  1440. *
  1441. * Disable the PLL for @pipe, making sure the pipe is off first.
  1442. *
  1443. * Note! This is for pre-ILK only.
  1444. */
  1445. static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1446. {
  1447. /* Don't disable pipe A or pipe A PLLs if needed */
  1448. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1449. return;
  1450. /* Make sure the pipe isn't still relying on us */
  1451. assert_pipe_disabled(dev_priv, pipe);
  1452. I915_WRITE(DPLL(pipe), 0);
  1453. POSTING_READ(DPLL(pipe));
  1454. }
  1455. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1456. {
  1457. u32 val = 0;
  1458. /* Make sure the pipe isn't still relying on us */
  1459. assert_pipe_disabled(dev_priv, pipe);
  1460. /*
  1461. * Leave integrated clock source and reference clock enabled for pipe B.
  1462. * The latter is needed for VGA hotplug / manual detection.
  1463. */
  1464. if (pipe == PIPE_B)
  1465. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1466. I915_WRITE(DPLL(pipe), val);
  1467. POSTING_READ(DPLL(pipe));
  1468. }
  1469. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1470. {
  1471. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1472. u32 val;
  1473. /* Make sure the pipe isn't still relying on us */
  1474. assert_pipe_disabled(dev_priv, pipe);
  1475. /* Set PLL en = 0 */
  1476. val = DPLL_SSC_REF_CLOCK_CHV;
  1477. if (pipe != PIPE_A)
  1478. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1479. I915_WRITE(DPLL(pipe), val);
  1480. POSTING_READ(DPLL(pipe));
  1481. mutex_lock(&dev_priv->dpio_lock);
  1482. /* Disable 10bit clock to display controller */
  1483. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1484. val &= ~DPIO_DCLKP_EN;
  1485. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1486. mutex_unlock(&dev_priv->dpio_lock);
  1487. }
  1488. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1489. struct intel_digital_port *dport)
  1490. {
  1491. u32 port_mask;
  1492. int dpll_reg;
  1493. switch (dport->port) {
  1494. case PORT_B:
  1495. port_mask = DPLL_PORTB_READY_MASK;
  1496. dpll_reg = DPLL(0);
  1497. break;
  1498. case PORT_C:
  1499. port_mask = DPLL_PORTC_READY_MASK;
  1500. dpll_reg = DPLL(0);
  1501. break;
  1502. case PORT_D:
  1503. port_mask = DPLL_PORTD_READY_MASK;
  1504. dpll_reg = DPIO_PHY_STATUS;
  1505. break;
  1506. default:
  1507. BUG();
  1508. }
  1509. if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
  1510. WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
  1511. port_name(dport->port), I915_READ(dpll_reg));
  1512. }
  1513. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1514. {
  1515. struct drm_device *dev = crtc->base.dev;
  1516. struct drm_i915_private *dev_priv = dev->dev_private;
  1517. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1518. WARN_ON(!pll->refcount);
  1519. if (pll->active == 0) {
  1520. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1521. WARN_ON(pll->on);
  1522. assert_shared_dpll_disabled(dev_priv, pll);
  1523. pll->mode_set(dev_priv, pll);
  1524. }
  1525. }
  1526. /**
  1527. * intel_enable_shared_dpll - enable PCH PLL
  1528. * @dev_priv: i915 private structure
  1529. * @pipe: pipe PLL to enable
  1530. *
  1531. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1532. * drives the transcoder clock.
  1533. */
  1534. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1535. {
  1536. struct drm_device *dev = crtc->base.dev;
  1537. struct drm_i915_private *dev_priv = dev->dev_private;
  1538. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1539. if (WARN_ON(pll == NULL))
  1540. return;
  1541. if (WARN_ON(pll->refcount == 0))
  1542. return;
  1543. DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
  1544. pll->name, pll->active, pll->on,
  1545. crtc->base.base.id);
  1546. if (pll->active++) {
  1547. WARN_ON(!pll->on);
  1548. assert_shared_dpll_enabled(dev_priv, pll);
  1549. return;
  1550. }
  1551. WARN_ON(pll->on);
  1552. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1553. pll->enable(dev_priv, pll);
  1554. pll->on = true;
  1555. }
  1556. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1557. {
  1558. struct drm_device *dev = crtc->base.dev;
  1559. struct drm_i915_private *dev_priv = dev->dev_private;
  1560. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1561. /* PCH only available on ILK+ */
  1562. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1563. if (WARN_ON(pll == NULL))
  1564. return;
  1565. if (WARN_ON(pll->refcount == 0))
  1566. return;
  1567. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1568. pll->name, pll->active, pll->on,
  1569. crtc->base.base.id);
  1570. if (WARN_ON(pll->active == 0)) {
  1571. assert_shared_dpll_disabled(dev_priv, pll);
  1572. return;
  1573. }
  1574. assert_shared_dpll_enabled(dev_priv, pll);
  1575. WARN_ON(!pll->on);
  1576. if (--pll->active)
  1577. return;
  1578. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1579. pll->disable(dev_priv, pll);
  1580. pll->on = false;
  1581. }
  1582. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1583. enum pipe pipe)
  1584. {
  1585. struct drm_device *dev = dev_priv->dev;
  1586. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1587. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1588. uint32_t reg, val, pipeconf_val;
  1589. /* PCH only available on ILK+ */
  1590. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1591. /* Make sure PCH DPLL is enabled */
  1592. assert_shared_dpll_enabled(dev_priv,
  1593. intel_crtc_to_shared_dpll(intel_crtc));
  1594. /* FDI must be feeding us bits for PCH ports */
  1595. assert_fdi_tx_enabled(dev_priv, pipe);
  1596. assert_fdi_rx_enabled(dev_priv, pipe);
  1597. if (HAS_PCH_CPT(dev)) {
  1598. /* Workaround: Set the timing override bit before enabling the
  1599. * pch transcoder. */
  1600. reg = TRANS_CHICKEN2(pipe);
  1601. val = I915_READ(reg);
  1602. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1603. I915_WRITE(reg, val);
  1604. }
  1605. reg = PCH_TRANSCONF(pipe);
  1606. val = I915_READ(reg);
  1607. pipeconf_val = I915_READ(PIPECONF(pipe));
  1608. if (HAS_PCH_IBX(dev_priv->dev)) {
  1609. /*
  1610. * make the BPC in transcoder be consistent with
  1611. * that in pipeconf reg.
  1612. */
  1613. val &= ~PIPECONF_BPC_MASK;
  1614. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1615. }
  1616. val &= ~TRANS_INTERLACE_MASK;
  1617. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1618. if (HAS_PCH_IBX(dev_priv->dev) &&
  1619. intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
  1620. val |= TRANS_LEGACY_INTERLACED_ILK;
  1621. else
  1622. val |= TRANS_INTERLACED;
  1623. else
  1624. val |= TRANS_PROGRESSIVE;
  1625. I915_WRITE(reg, val | TRANS_ENABLE);
  1626. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1627. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1628. }
  1629. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1630. enum transcoder cpu_transcoder)
  1631. {
  1632. u32 val, pipeconf_val;
  1633. /* PCH only available on ILK+ */
  1634. BUG_ON(INTEL_INFO(dev_priv->dev)->gen < 5);
  1635. /* FDI must be feeding us bits for PCH ports */
  1636. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1637. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1638. /* Workaround: set timing override bit. */
  1639. val = I915_READ(_TRANSA_CHICKEN2);
  1640. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1641. I915_WRITE(_TRANSA_CHICKEN2, val);
  1642. val = TRANS_ENABLE;
  1643. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1644. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1645. PIPECONF_INTERLACED_ILK)
  1646. val |= TRANS_INTERLACED;
  1647. else
  1648. val |= TRANS_PROGRESSIVE;
  1649. I915_WRITE(LPT_TRANSCONF, val);
  1650. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1651. DRM_ERROR("Failed to enable PCH transcoder\n");
  1652. }
  1653. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1654. enum pipe pipe)
  1655. {
  1656. struct drm_device *dev = dev_priv->dev;
  1657. uint32_t reg, val;
  1658. /* FDI relies on the transcoder */
  1659. assert_fdi_tx_disabled(dev_priv, pipe);
  1660. assert_fdi_rx_disabled(dev_priv, pipe);
  1661. /* Ports must be off as well */
  1662. assert_pch_ports_disabled(dev_priv, pipe);
  1663. reg = PCH_TRANSCONF(pipe);
  1664. val = I915_READ(reg);
  1665. val &= ~TRANS_ENABLE;
  1666. I915_WRITE(reg, val);
  1667. /* wait for PCH transcoder off, transcoder state */
  1668. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1669. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1670. if (!HAS_PCH_IBX(dev)) {
  1671. /* Workaround: Clear the timing override chicken bit again. */
  1672. reg = TRANS_CHICKEN2(pipe);
  1673. val = I915_READ(reg);
  1674. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1675. I915_WRITE(reg, val);
  1676. }
  1677. }
  1678. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1679. {
  1680. u32 val;
  1681. val = I915_READ(LPT_TRANSCONF);
  1682. val &= ~TRANS_ENABLE;
  1683. I915_WRITE(LPT_TRANSCONF, val);
  1684. /* wait for PCH transcoder off, transcoder state */
  1685. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1686. DRM_ERROR("Failed to disable PCH transcoder\n");
  1687. /* Workaround: clear timing override bit. */
  1688. val = I915_READ(_TRANSA_CHICKEN2);
  1689. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1690. I915_WRITE(_TRANSA_CHICKEN2, val);
  1691. }
  1692. /**
  1693. * intel_enable_pipe - enable a pipe, asserting requirements
  1694. * @crtc: crtc responsible for the pipe
  1695. *
  1696. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1697. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1698. */
  1699. static void intel_enable_pipe(struct intel_crtc *crtc)
  1700. {
  1701. struct drm_device *dev = crtc->base.dev;
  1702. struct drm_i915_private *dev_priv = dev->dev_private;
  1703. enum pipe pipe = crtc->pipe;
  1704. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1705. pipe);
  1706. enum pipe pch_transcoder;
  1707. int reg;
  1708. u32 val;
  1709. assert_planes_disabled(dev_priv, pipe);
  1710. assert_cursor_disabled(dev_priv, pipe);
  1711. assert_sprites_disabled(dev_priv, pipe);
  1712. if (HAS_PCH_LPT(dev_priv->dev))
  1713. pch_transcoder = TRANSCODER_A;
  1714. else
  1715. pch_transcoder = pipe;
  1716. /*
  1717. * A pipe without a PLL won't actually be able to drive bits from
  1718. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1719. * need the check.
  1720. */
  1721. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1722. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DSI))
  1723. assert_dsi_pll_enabled(dev_priv);
  1724. else
  1725. assert_pll_enabled(dev_priv, pipe);
  1726. else {
  1727. if (crtc->config.has_pch_encoder) {
  1728. /* if driving the PCH, we need FDI enabled */
  1729. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1730. assert_fdi_tx_pll_enabled(dev_priv,
  1731. (enum pipe) cpu_transcoder);
  1732. }
  1733. /* FIXME: assert CPU port conditions for SNB+ */
  1734. }
  1735. reg = PIPECONF(cpu_transcoder);
  1736. val = I915_READ(reg);
  1737. if (val & PIPECONF_ENABLE) {
  1738. WARN_ON(!(pipe == PIPE_A &&
  1739. dev_priv->quirks & QUIRK_PIPEA_FORCE));
  1740. return;
  1741. }
  1742. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1743. POSTING_READ(reg);
  1744. }
  1745. /**
  1746. * intel_disable_pipe - disable a pipe, asserting requirements
  1747. * @dev_priv: i915 private structure
  1748. * @pipe: pipe to disable
  1749. *
  1750. * Disable @pipe, making sure that various hardware specific requirements
  1751. * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
  1752. *
  1753. * @pipe should be %PIPE_A or %PIPE_B.
  1754. *
  1755. * Will wait until the pipe has shut down before returning.
  1756. */
  1757. static void intel_disable_pipe(struct drm_i915_private *dev_priv,
  1758. enum pipe pipe)
  1759. {
  1760. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1761. pipe);
  1762. int reg;
  1763. u32 val;
  1764. /*
  1765. * Make sure planes won't keep trying to pump pixels to us,
  1766. * or we might hang the display.
  1767. */
  1768. assert_planes_disabled(dev_priv, pipe);
  1769. assert_cursor_disabled(dev_priv, pipe);
  1770. assert_sprites_disabled(dev_priv, pipe);
  1771. /* Don't disable pipe A or pipe A PLLs if needed */
  1772. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1773. return;
  1774. reg = PIPECONF(cpu_transcoder);
  1775. val = I915_READ(reg);
  1776. if ((val & PIPECONF_ENABLE) == 0)
  1777. return;
  1778. I915_WRITE(reg, val & ~PIPECONF_ENABLE);
  1779. intel_wait_for_pipe_off(dev_priv->dev, pipe);
  1780. }
  1781. /*
  1782. * Plane regs are double buffered, going from enabled->disabled needs a
  1783. * trigger in order to latch. The display address reg provides this.
  1784. */
  1785. void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
  1786. enum plane plane)
  1787. {
  1788. struct drm_device *dev = dev_priv->dev;
  1789. u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
  1790. I915_WRITE(reg, I915_READ(reg));
  1791. POSTING_READ(reg);
  1792. }
  1793. /**
  1794. * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  1795. * @dev_priv: i915 private structure
  1796. * @plane: plane to enable
  1797. * @pipe: pipe being fed
  1798. *
  1799. * Enable @plane on @pipe, making sure that @pipe is running first.
  1800. */
  1801. static void intel_enable_primary_hw_plane(struct drm_i915_private *dev_priv,
  1802. enum plane plane, enum pipe pipe)
  1803. {
  1804. struct intel_crtc *intel_crtc =
  1805. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  1806. int reg;
  1807. u32 val;
  1808. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1809. assert_pipe_enabled(dev_priv, pipe);
  1810. if (intel_crtc->primary_enabled)
  1811. return;
  1812. intel_crtc->primary_enabled = true;
  1813. reg = DSPCNTR(plane);
  1814. val = I915_READ(reg);
  1815. WARN_ON(val & DISPLAY_PLANE_ENABLE);
  1816. I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
  1817. intel_flush_primary_plane(dev_priv, plane);
  1818. }
  1819. /**
  1820. * intel_disable_primary_hw_plane - disable the primary hardware plane
  1821. * @dev_priv: i915 private structure
  1822. * @plane: plane to disable
  1823. * @pipe: pipe consuming the data
  1824. *
  1825. * Disable @plane; should be an independent operation.
  1826. */
  1827. static void intel_disable_primary_hw_plane(struct drm_i915_private *dev_priv,
  1828. enum plane plane, enum pipe pipe)
  1829. {
  1830. struct intel_crtc *intel_crtc =
  1831. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  1832. int reg;
  1833. u32 val;
  1834. if (!intel_crtc->primary_enabled)
  1835. return;
  1836. intel_crtc->primary_enabled = false;
  1837. reg = DSPCNTR(plane);
  1838. val = I915_READ(reg);
  1839. WARN_ON((val & DISPLAY_PLANE_ENABLE) == 0);
  1840. I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
  1841. intel_flush_primary_plane(dev_priv, plane);
  1842. }
  1843. static bool need_vtd_wa(struct drm_device *dev)
  1844. {
  1845. #ifdef CONFIG_INTEL_IOMMU
  1846. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1847. return true;
  1848. #endif
  1849. return false;
  1850. }
  1851. static int intel_align_height(struct drm_device *dev, int height, bool tiled)
  1852. {
  1853. int tile_height;
  1854. tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
  1855. return ALIGN(height, tile_height);
  1856. }
  1857. int
  1858. intel_pin_and_fence_fb_obj(struct drm_device *dev,
  1859. struct drm_i915_gem_object *obj,
  1860. struct intel_engine_cs *pipelined)
  1861. {
  1862. struct drm_i915_private *dev_priv = dev->dev_private;
  1863. u32 alignment;
  1864. int ret;
  1865. switch (obj->tiling_mode) {
  1866. case I915_TILING_NONE:
  1867. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1868. alignment = 128 * 1024;
  1869. else if (INTEL_INFO(dev)->gen >= 4)
  1870. alignment = 4 * 1024;
  1871. else
  1872. alignment = 64 * 1024;
  1873. break;
  1874. case I915_TILING_X:
  1875. /* pin() will align the object as required by fence */
  1876. alignment = 0;
  1877. break;
  1878. case I915_TILING_Y:
  1879. WARN(1, "Y tiled bo slipped through, driver bug!\n");
  1880. return -EINVAL;
  1881. default:
  1882. BUG();
  1883. }
  1884. /* Note that the w/a also requires 64 PTE of padding following the
  1885. * bo. We currently fill all unused PTE with the shadow page and so
  1886. * we should always have valid PTE following the scanout preventing
  1887. * the VT-d warning.
  1888. */
  1889. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1890. alignment = 256 * 1024;
  1891. dev_priv->mm.interruptible = false;
  1892. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1893. if (ret)
  1894. goto err_interruptible;
  1895. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1896. * fence, whereas 965+ only requires a fence if using
  1897. * framebuffer compression. For simplicity, we always install
  1898. * a fence as the cost is not that onerous.
  1899. */
  1900. ret = i915_gem_object_get_fence(obj);
  1901. if (ret)
  1902. goto err_unpin;
  1903. i915_gem_object_pin_fence(obj);
  1904. dev_priv->mm.interruptible = true;
  1905. return 0;
  1906. err_unpin:
  1907. i915_gem_object_unpin_from_display_plane(obj);
  1908. err_interruptible:
  1909. dev_priv->mm.interruptible = true;
  1910. return ret;
  1911. }
  1912. void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
  1913. {
  1914. i915_gem_object_unpin_fence(obj);
  1915. i915_gem_object_unpin_from_display_plane(obj);
  1916. }
  1917. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  1918. * is assumed to be a power-of-two. */
  1919. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  1920. unsigned int tiling_mode,
  1921. unsigned int cpp,
  1922. unsigned int pitch)
  1923. {
  1924. if (tiling_mode != I915_TILING_NONE) {
  1925. unsigned int tile_rows, tiles;
  1926. tile_rows = *y / 8;
  1927. *y %= 8;
  1928. tiles = *x / (512/cpp);
  1929. *x %= 512/cpp;
  1930. return tile_rows * pitch * 8 + tiles * 4096;
  1931. } else {
  1932. unsigned int offset;
  1933. offset = *y * pitch + *x * cpp;
  1934. *y = 0;
  1935. *x = (offset & 4095) / cpp;
  1936. return offset & -4096;
  1937. }
  1938. }
  1939. int intel_format_to_fourcc(int format)
  1940. {
  1941. switch (format) {
  1942. case DISPPLANE_8BPP:
  1943. return DRM_FORMAT_C8;
  1944. case DISPPLANE_BGRX555:
  1945. return DRM_FORMAT_XRGB1555;
  1946. case DISPPLANE_BGRX565:
  1947. return DRM_FORMAT_RGB565;
  1948. default:
  1949. case DISPPLANE_BGRX888:
  1950. return DRM_FORMAT_XRGB8888;
  1951. case DISPPLANE_RGBX888:
  1952. return DRM_FORMAT_XBGR8888;
  1953. case DISPPLANE_BGRX101010:
  1954. return DRM_FORMAT_XRGB2101010;
  1955. case DISPPLANE_RGBX101010:
  1956. return DRM_FORMAT_XBGR2101010;
  1957. }
  1958. }
  1959. static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
  1960. struct intel_plane_config *plane_config)
  1961. {
  1962. struct drm_device *dev = crtc->base.dev;
  1963. struct drm_i915_gem_object *obj = NULL;
  1964. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  1965. u32 base = plane_config->base;
  1966. if (plane_config->size == 0)
  1967. return false;
  1968. obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
  1969. plane_config->size);
  1970. if (!obj)
  1971. return false;
  1972. if (plane_config->tiled) {
  1973. obj->tiling_mode = I915_TILING_X;
  1974. obj->stride = crtc->base.primary->fb->pitches[0];
  1975. }
  1976. mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
  1977. mode_cmd.width = crtc->base.primary->fb->width;
  1978. mode_cmd.height = crtc->base.primary->fb->height;
  1979. mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
  1980. mutex_lock(&dev->struct_mutex);
  1981. if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
  1982. &mode_cmd, obj)) {
  1983. DRM_DEBUG_KMS("intel fb init failed\n");
  1984. goto out_unref_obj;
  1985. }
  1986. mutex_unlock(&dev->struct_mutex);
  1987. DRM_DEBUG_KMS("plane fb obj %p\n", obj);
  1988. return true;
  1989. out_unref_obj:
  1990. drm_gem_object_unreference(&obj->base);
  1991. mutex_unlock(&dev->struct_mutex);
  1992. return false;
  1993. }
  1994. static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
  1995. struct intel_plane_config *plane_config)
  1996. {
  1997. struct drm_device *dev = intel_crtc->base.dev;
  1998. struct drm_crtc *c;
  1999. struct intel_crtc *i;
  2000. struct intel_framebuffer *fb;
  2001. if (!intel_crtc->base.primary->fb)
  2002. return;
  2003. if (intel_alloc_plane_obj(intel_crtc, plane_config))
  2004. return;
  2005. kfree(intel_crtc->base.primary->fb);
  2006. intel_crtc->base.primary->fb = NULL;
  2007. /*
  2008. * Failed to alloc the obj, check to see if we should share
  2009. * an fb with another CRTC instead
  2010. */
  2011. for_each_crtc(dev, c) {
  2012. i = to_intel_crtc(c);
  2013. if (c == &intel_crtc->base)
  2014. continue;
  2015. if (!i->active || !c->primary->fb)
  2016. continue;
  2017. fb = to_intel_framebuffer(c->primary->fb);
  2018. if (i915_gem_obj_ggtt_offset(fb->obj) == plane_config->base) {
  2019. drm_framebuffer_reference(c->primary->fb);
  2020. intel_crtc->base.primary->fb = c->primary->fb;
  2021. break;
  2022. }
  2023. }
  2024. }
  2025. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2026. struct drm_framebuffer *fb,
  2027. int x, int y)
  2028. {
  2029. struct drm_device *dev = crtc->dev;
  2030. struct drm_i915_private *dev_priv = dev->dev_private;
  2031. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2032. struct intel_framebuffer *intel_fb;
  2033. struct drm_i915_gem_object *obj;
  2034. int plane = intel_crtc->plane;
  2035. unsigned long linear_offset;
  2036. u32 dspcntr;
  2037. u32 reg;
  2038. intel_fb = to_intel_framebuffer(fb);
  2039. obj = intel_fb->obj;
  2040. reg = DSPCNTR(plane);
  2041. dspcntr = I915_READ(reg);
  2042. /* Mask out pixel format bits in case we change it */
  2043. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  2044. switch (fb->pixel_format) {
  2045. case DRM_FORMAT_C8:
  2046. dspcntr |= DISPPLANE_8BPP;
  2047. break;
  2048. case DRM_FORMAT_XRGB1555:
  2049. case DRM_FORMAT_ARGB1555:
  2050. dspcntr |= DISPPLANE_BGRX555;
  2051. break;
  2052. case DRM_FORMAT_RGB565:
  2053. dspcntr |= DISPPLANE_BGRX565;
  2054. break;
  2055. case DRM_FORMAT_XRGB8888:
  2056. case DRM_FORMAT_ARGB8888:
  2057. dspcntr |= DISPPLANE_BGRX888;
  2058. break;
  2059. case DRM_FORMAT_XBGR8888:
  2060. case DRM_FORMAT_ABGR8888:
  2061. dspcntr |= DISPPLANE_RGBX888;
  2062. break;
  2063. case DRM_FORMAT_XRGB2101010:
  2064. case DRM_FORMAT_ARGB2101010:
  2065. dspcntr |= DISPPLANE_BGRX101010;
  2066. break;
  2067. case DRM_FORMAT_XBGR2101010:
  2068. case DRM_FORMAT_ABGR2101010:
  2069. dspcntr |= DISPPLANE_RGBX101010;
  2070. break;
  2071. default:
  2072. BUG();
  2073. }
  2074. if (INTEL_INFO(dev)->gen >= 4) {
  2075. if (obj->tiling_mode != I915_TILING_NONE)
  2076. dspcntr |= DISPPLANE_TILED;
  2077. else
  2078. dspcntr &= ~DISPPLANE_TILED;
  2079. }
  2080. if (IS_G4X(dev))
  2081. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2082. I915_WRITE(reg, dspcntr);
  2083. linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  2084. if (INTEL_INFO(dev)->gen >= 4) {
  2085. intel_crtc->dspaddr_offset =
  2086. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2087. fb->bits_per_pixel / 8,
  2088. fb->pitches[0]);
  2089. linear_offset -= intel_crtc->dspaddr_offset;
  2090. } else {
  2091. intel_crtc->dspaddr_offset = linear_offset;
  2092. }
  2093. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2094. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2095. fb->pitches[0]);
  2096. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2097. if (INTEL_INFO(dev)->gen >= 4) {
  2098. I915_WRITE(DSPSURF(plane),
  2099. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2100. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2101. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2102. } else
  2103. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2104. POSTING_READ(reg);
  2105. }
  2106. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2107. struct drm_framebuffer *fb,
  2108. int x, int y)
  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. struct intel_framebuffer *intel_fb;
  2114. struct drm_i915_gem_object *obj;
  2115. int plane = intel_crtc->plane;
  2116. unsigned long linear_offset;
  2117. u32 dspcntr;
  2118. u32 reg;
  2119. intel_fb = to_intel_framebuffer(fb);
  2120. obj = intel_fb->obj;
  2121. reg = DSPCNTR(plane);
  2122. dspcntr = I915_READ(reg);
  2123. /* Mask out pixel format bits in case we change it */
  2124. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  2125. switch (fb->pixel_format) {
  2126. case DRM_FORMAT_C8:
  2127. dspcntr |= DISPPLANE_8BPP;
  2128. break;
  2129. case DRM_FORMAT_RGB565:
  2130. dspcntr |= DISPPLANE_BGRX565;
  2131. break;
  2132. case DRM_FORMAT_XRGB8888:
  2133. case DRM_FORMAT_ARGB8888:
  2134. dspcntr |= DISPPLANE_BGRX888;
  2135. break;
  2136. case DRM_FORMAT_XBGR8888:
  2137. case DRM_FORMAT_ABGR8888:
  2138. dspcntr |= DISPPLANE_RGBX888;
  2139. break;
  2140. case DRM_FORMAT_XRGB2101010:
  2141. case DRM_FORMAT_ARGB2101010:
  2142. dspcntr |= DISPPLANE_BGRX101010;
  2143. break;
  2144. case DRM_FORMAT_XBGR2101010:
  2145. case DRM_FORMAT_ABGR2101010:
  2146. dspcntr |= DISPPLANE_RGBX101010;
  2147. break;
  2148. default:
  2149. BUG();
  2150. }
  2151. if (obj->tiling_mode != I915_TILING_NONE)
  2152. dspcntr |= DISPPLANE_TILED;
  2153. else
  2154. dspcntr &= ~DISPPLANE_TILED;
  2155. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2156. dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
  2157. else
  2158. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2159. I915_WRITE(reg, dspcntr);
  2160. linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  2161. intel_crtc->dspaddr_offset =
  2162. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2163. fb->bits_per_pixel / 8,
  2164. fb->pitches[0]);
  2165. linear_offset -= intel_crtc->dspaddr_offset;
  2166. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2167. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2168. fb->pitches[0]);
  2169. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2170. I915_WRITE(DSPSURF(plane),
  2171. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2172. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2173. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2174. } else {
  2175. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2176. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2177. }
  2178. POSTING_READ(reg);
  2179. }
  2180. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2181. static int
  2182. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2183. int x, int y, enum mode_set_atomic state)
  2184. {
  2185. struct drm_device *dev = crtc->dev;
  2186. struct drm_i915_private *dev_priv = dev->dev_private;
  2187. if (dev_priv->display.disable_fbc)
  2188. dev_priv->display.disable_fbc(dev);
  2189. intel_increase_pllclock(crtc);
  2190. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2191. return 0;
  2192. }
  2193. void intel_display_handle_reset(struct drm_device *dev)
  2194. {
  2195. struct drm_i915_private *dev_priv = dev->dev_private;
  2196. struct drm_crtc *crtc;
  2197. /*
  2198. * Flips in the rings have been nuked by the reset,
  2199. * so complete all pending flips so that user space
  2200. * will get its events and not get stuck.
  2201. *
  2202. * Also update the base address of all primary
  2203. * planes to the the last fb to make sure we're
  2204. * showing the correct fb after a reset.
  2205. *
  2206. * Need to make two loops over the crtcs so that we
  2207. * don't try to grab a crtc mutex before the
  2208. * pending_flip_queue really got woken up.
  2209. */
  2210. for_each_crtc(dev, crtc) {
  2211. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2212. enum plane plane = intel_crtc->plane;
  2213. intel_prepare_page_flip(dev, plane);
  2214. intel_finish_page_flip_plane(dev, plane);
  2215. }
  2216. for_each_crtc(dev, crtc) {
  2217. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2218. drm_modeset_lock(&crtc->mutex, NULL);
  2219. /*
  2220. * FIXME: Once we have proper support for primary planes (and
  2221. * disabling them without disabling the entire crtc) allow again
  2222. * a NULL crtc->primary->fb.
  2223. */
  2224. if (intel_crtc->active && crtc->primary->fb)
  2225. dev_priv->display.update_primary_plane(crtc,
  2226. crtc->primary->fb,
  2227. crtc->x,
  2228. crtc->y);
  2229. drm_modeset_unlock(&crtc->mutex);
  2230. }
  2231. }
  2232. static int
  2233. intel_finish_fb(struct drm_framebuffer *old_fb)
  2234. {
  2235. struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
  2236. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2237. bool was_interruptible = dev_priv->mm.interruptible;
  2238. int ret;
  2239. /* Big Hammer, we also need to ensure that any pending
  2240. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2241. * current scanout is retired before unpinning the old
  2242. * framebuffer.
  2243. *
  2244. * This should only fail upon a hung GPU, in which case we
  2245. * can safely continue.
  2246. */
  2247. dev_priv->mm.interruptible = false;
  2248. ret = i915_gem_object_finish_gpu(obj);
  2249. dev_priv->mm.interruptible = was_interruptible;
  2250. return ret;
  2251. }
  2252. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2253. {
  2254. struct drm_device *dev = crtc->dev;
  2255. struct drm_i915_private *dev_priv = dev->dev_private;
  2256. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2257. unsigned long flags;
  2258. bool pending;
  2259. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2260. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2261. return false;
  2262. spin_lock_irqsave(&dev->event_lock, flags);
  2263. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2264. spin_unlock_irqrestore(&dev->event_lock, flags);
  2265. return pending;
  2266. }
  2267. static int
  2268. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  2269. struct drm_framebuffer *fb)
  2270. {
  2271. struct drm_device *dev = crtc->dev;
  2272. struct drm_i915_private *dev_priv = dev->dev_private;
  2273. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2274. struct drm_framebuffer *old_fb;
  2275. int ret;
  2276. if (intel_crtc_has_pending_flip(crtc)) {
  2277. DRM_ERROR("pipe is still busy with an old pageflip\n");
  2278. return -EBUSY;
  2279. }
  2280. /* no fb bound */
  2281. if (!fb) {
  2282. DRM_ERROR("No FB bound\n");
  2283. return 0;
  2284. }
  2285. if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
  2286. DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
  2287. plane_name(intel_crtc->plane),
  2288. INTEL_INFO(dev)->num_pipes);
  2289. return -EINVAL;
  2290. }
  2291. mutex_lock(&dev->struct_mutex);
  2292. ret = intel_pin_and_fence_fb_obj(dev,
  2293. to_intel_framebuffer(fb)->obj,
  2294. NULL);
  2295. mutex_unlock(&dev->struct_mutex);
  2296. if (ret != 0) {
  2297. DRM_ERROR("pin & fence failed\n");
  2298. return ret;
  2299. }
  2300. /*
  2301. * Update pipe size and adjust fitter if needed: the reason for this is
  2302. * that in compute_mode_changes we check the native mode (not the pfit
  2303. * mode) to see if we can flip rather than do a full mode set. In the
  2304. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2305. * pfit state, we'll end up with a big fb scanned out into the wrong
  2306. * sized surface.
  2307. *
  2308. * To fix this properly, we need to hoist the checks up into
  2309. * compute_mode_changes (or above), check the actual pfit state and
  2310. * whether the platform allows pfit disable with pipe active, and only
  2311. * then update the pipesrc and pfit state, even on the flip path.
  2312. */
  2313. if (i915.fastboot) {
  2314. const struct drm_display_mode *adjusted_mode =
  2315. &intel_crtc->config.adjusted_mode;
  2316. I915_WRITE(PIPESRC(intel_crtc->pipe),
  2317. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2318. (adjusted_mode->crtc_vdisplay - 1));
  2319. if (!intel_crtc->config.pch_pfit.enabled &&
  2320. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2321. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2322. I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
  2323. I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
  2324. I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
  2325. }
  2326. intel_crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
  2327. intel_crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
  2328. }
  2329. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2330. old_fb = crtc->primary->fb;
  2331. crtc->primary->fb = fb;
  2332. crtc->x = x;
  2333. crtc->y = y;
  2334. if (old_fb) {
  2335. if (intel_crtc->active && old_fb != fb)
  2336. intel_wait_for_vblank(dev, intel_crtc->pipe);
  2337. mutex_lock(&dev->struct_mutex);
  2338. intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
  2339. mutex_unlock(&dev->struct_mutex);
  2340. }
  2341. mutex_lock(&dev->struct_mutex);
  2342. intel_update_fbc(dev);
  2343. intel_edp_psr_update(dev);
  2344. mutex_unlock(&dev->struct_mutex);
  2345. return 0;
  2346. }
  2347. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2348. {
  2349. struct drm_device *dev = crtc->dev;
  2350. struct drm_i915_private *dev_priv = dev->dev_private;
  2351. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2352. int pipe = intel_crtc->pipe;
  2353. u32 reg, temp;
  2354. /* enable normal train */
  2355. reg = FDI_TX_CTL(pipe);
  2356. temp = I915_READ(reg);
  2357. if (IS_IVYBRIDGE(dev)) {
  2358. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2359. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2360. } else {
  2361. temp &= ~FDI_LINK_TRAIN_NONE;
  2362. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2363. }
  2364. I915_WRITE(reg, temp);
  2365. reg = FDI_RX_CTL(pipe);
  2366. temp = I915_READ(reg);
  2367. if (HAS_PCH_CPT(dev)) {
  2368. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2369. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2370. } else {
  2371. temp &= ~FDI_LINK_TRAIN_NONE;
  2372. temp |= FDI_LINK_TRAIN_NONE;
  2373. }
  2374. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2375. /* wait one idle pattern time */
  2376. POSTING_READ(reg);
  2377. udelay(1000);
  2378. /* IVB wants error correction enabled */
  2379. if (IS_IVYBRIDGE(dev))
  2380. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2381. FDI_FE_ERRC_ENABLE);
  2382. }
  2383. static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
  2384. {
  2385. return crtc->base.enabled && crtc->active &&
  2386. crtc->config.has_pch_encoder;
  2387. }
  2388. static void ivb_modeset_global_resources(struct drm_device *dev)
  2389. {
  2390. struct drm_i915_private *dev_priv = dev->dev_private;
  2391. struct intel_crtc *pipe_B_crtc =
  2392. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  2393. struct intel_crtc *pipe_C_crtc =
  2394. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
  2395. uint32_t temp;
  2396. /*
  2397. * When everything is off disable fdi C so that we could enable fdi B
  2398. * with all lanes. Note that we don't care about enabled pipes without
  2399. * an enabled pch encoder.
  2400. */
  2401. if (!pipe_has_enabled_pch(pipe_B_crtc) &&
  2402. !pipe_has_enabled_pch(pipe_C_crtc)) {
  2403. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2404. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2405. temp = I915_READ(SOUTH_CHICKEN1);
  2406. temp &= ~FDI_BC_BIFURCATION_SELECT;
  2407. DRM_DEBUG_KMS("disabling fdi C rx\n");
  2408. I915_WRITE(SOUTH_CHICKEN1, temp);
  2409. }
  2410. }
  2411. /* The FDI link training functions for ILK/Ibexpeak. */
  2412. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2413. {
  2414. struct drm_device *dev = crtc->dev;
  2415. struct drm_i915_private *dev_priv = dev->dev_private;
  2416. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2417. int pipe = intel_crtc->pipe;
  2418. u32 reg, temp, tries;
  2419. /* FDI needs bits from pipe first */
  2420. assert_pipe_enabled(dev_priv, pipe);
  2421. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2422. for train result */
  2423. reg = FDI_RX_IMR(pipe);
  2424. temp = I915_READ(reg);
  2425. temp &= ~FDI_RX_SYMBOL_LOCK;
  2426. temp &= ~FDI_RX_BIT_LOCK;
  2427. I915_WRITE(reg, temp);
  2428. I915_READ(reg);
  2429. udelay(150);
  2430. /* enable CPU FDI TX and PCH FDI RX */
  2431. reg = FDI_TX_CTL(pipe);
  2432. temp = I915_READ(reg);
  2433. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2434. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2435. temp &= ~FDI_LINK_TRAIN_NONE;
  2436. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2437. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2438. reg = FDI_RX_CTL(pipe);
  2439. temp = I915_READ(reg);
  2440. temp &= ~FDI_LINK_TRAIN_NONE;
  2441. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2442. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2443. POSTING_READ(reg);
  2444. udelay(150);
  2445. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2446. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2447. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2448. FDI_RX_PHASE_SYNC_POINTER_EN);
  2449. reg = FDI_RX_IIR(pipe);
  2450. for (tries = 0; tries < 5; tries++) {
  2451. temp = I915_READ(reg);
  2452. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2453. if ((temp & FDI_RX_BIT_LOCK)) {
  2454. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2455. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2456. break;
  2457. }
  2458. }
  2459. if (tries == 5)
  2460. DRM_ERROR("FDI train 1 fail!\n");
  2461. /* Train 2 */
  2462. reg = FDI_TX_CTL(pipe);
  2463. temp = I915_READ(reg);
  2464. temp &= ~FDI_LINK_TRAIN_NONE;
  2465. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2466. I915_WRITE(reg, temp);
  2467. reg = FDI_RX_CTL(pipe);
  2468. temp = I915_READ(reg);
  2469. temp &= ~FDI_LINK_TRAIN_NONE;
  2470. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2471. I915_WRITE(reg, temp);
  2472. POSTING_READ(reg);
  2473. udelay(150);
  2474. reg = FDI_RX_IIR(pipe);
  2475. for (tries = 0; tries < 5; tries++) {
  2476. temp = I915_READ(reg);
  2477. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2478. if (temp & FDI_RX_SYMBOL_LOCK) {
  2479. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2480. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2481. break;
  2482. }
  2483. }
  2484. if (tries == 5)
  2485. DRM_ERROR("FDI train 2 fail!\n");
  2486. DRM_DEBUG_KMS("FDI train done\n");
  2487. }
  2488. static const int snb_b_fdi_train_param[] = {
  2489. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2490. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2491. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2492. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2493. };
  2494. /* The FDI link training functions for SNB/Cougarpoint. */
  2495. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2496. {
  2497. struct drm_device *dev = crtc->dev;
  2498. struct drm_i915_private *dev_priv = dev->dev_private;
  2499. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2500. int pipe = intel_crtc->pipe;
  2501. u32 reg, temp, i, retry;
  2502. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2503. for train result */
  2504. reg = FDI_RX_IMR(pipe);
  2505. temp = I915_READ(reg);
  2506. temp &= ~FDI_RX_SYMBOL_LOCK;
  2507. temp &= ~FDI_RX_BIT_LOCK;
  2508. I915_WRITE(reg, temp);
  2509. POSTING_READ(reg);
  2510. udelay(150);
  2511. /* enable CPU FDI TX and PCH FDI RX */
  2512. reg = FDI_TX_CTL(pipe);
  2513. temp = I915_READ(reg);
  2514. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2515. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2516. temp &= ~FDI_LINK_TRAIN_NONE;
  2517. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2518. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2519. /* SNB-B */
  2520. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2521. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2522. I915_WRITE(FDI_RX_MISC(pipe),
  2523. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2524. reg = FDI_RX_CTL(pipe);
  2525. temp = I915_READ(reg);
  2526. if (HAS_PCH_CPT(dev)) {
  2527. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2528. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2529. } else {
  2530. temp &= ~FDI_LINK_TRAIN_NONE;
  2531. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2532. }
  2533. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2534. POSTING_READ(reg);
  2535. udelay(150);
  2536. for (i = 0; i < 4; i++) {
  2537. reg = FDI_TX_CTL(pipe);
  2538. temp = I915_READ(reg);
  2539. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2540. temp |= snb_b_fdi_train_param[i];
  2541. I915_WRITE(reg, temp);
  2542. POSTING_READ(reg);
  2543. udelay(500);
  2544. for (retry = 0; retry < 5; retry++) {
  2545. reg = FDI_RX_IIR(pipe);
  2546. temp = I915_READ(reg);
  2547. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2548. if (temp & FDI_RX_BIT_LOCK) {
  2549. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2550. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2551. break;
  2552. }
  2553. udelay(50);
  2554. }
  2555. if (retry < 5)
  2556. break;
  2557. }
  2558. if (i == 4)
  2559. DRM_ERROR("FDI train 1 fail!\n");
  2560. /* Train 2 */
  2561. reg = FDI_TX_CTL(pipe);
  2562. temp = I915_READ(reg);
  2563. temp &= ~FDI_LINK_TRAIN_NONE;
  2564. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2565. if (IS_GEN6(dev)) {
  2566. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2567. /* SNB-B */
  2568. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2569. }
  2570. I915_WRITE(reg, temp);
  2571. reg = FDI_RX_CTL(pipe);
  2572. temp = I915_READ(reg);
  2573. if (HAS_PCH_CPT(dev)) {
  2574. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2575. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2576. } else {
  2577. temp &= ~FDI_LINK_TRAIN_NONE;
  2578. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2579. }
  2580. I915_WRITE(reg, temp);
  2581. POSTING_READ(reg);
  2582. udelay(150);
  2583. for (i = 0; i < 4; i++) {
  2584. reg = FDI_TX_CTL(pipe);
  2585. temp = I915_READ(reg);
  2586. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2587. temp |= snb_b_fdi_train_param[i];
  2588. I915_WRITE(reg, temp);
  2589. POSTING_READ(reg);
  2590. udelay(500);
  2591. for (retry = 0; retry < 5; retry++) {
  2592. reg = FDI_RX_IIR(pipe);
  2593. temp = I915_READ(reg);
  2594. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2595. if (temp & FDI_RX_SYMBOL_LOCK) {
  2596. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2597. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2598. break;
  2599. }
  2600. udelay(50);
  2601. }
  2602. if (retry < 5)
  2603. break;
  2604. }
  2605. if (i == 4)
  2606. DRM_ERROR("FDI train 2 fail!\n");
  2607. DRM_DEBUG_KMS("FDI train done.\n");
  2608. }
  2609. /* Manual link training for Ivy Bridge A0 parts */
  2610. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2611. {
  2612. struct drm_device *dev = crtc->dev;
  2613. struct drm_i915_private *dev_priv = dev->dev_private;
  2614. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2615. int pipe = intel_crtc->pipe;
  2616. u32 reg, temp, i, j;
  2617. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2618. for train result */
  2619. reg = FDI_RX_IMR(pipe);
  2620. temp = I915_READ(reg);
  2621. temp &= ~FDI_RX_SYMBOL_LOCK;
  2622. temp &= ~FDI_RX_BIT_LOCK;
  2623. I915_WRITE(reg, temp);
  2624. POSTING_READ(reg);
  2625. udelay(150);
  2626. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  2627. I915_READ(FDI_RX_IIR(pipe)));
  2628. /* Try each vswing and preemphasis setting twice before moving on */
  2629. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  2630. /* disable first in case we need to retry */
  2631. reg = FDI_TX_CTL(pipe);
  2632. temp = I915_READ(reg);
  2633. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2634. temp &= ~FDI_TX_ENABLE;
  2635. I915_WRITE(reg, temp);
  2636. reg = FDI_RX_CTL(pipe);
  2637. temp = I915_READ(reg);
  2638. temp &= ~FDI_LINK_TRAIN_AUTO;
  2639. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2640. temp &= ~FDI_RX_ENABLE;
  2641. I915_WRITE(reg, temp);
  2642. /* enable CPU FDI TX and PCH FDI RX */
  2643. reg = FDI_TX_CTL(pipe);
  2644. temp = I915_READ(reg);
  2645. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2646. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2647. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2648. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2649. temp |= snb_b_fdi_train_param[j/2];
  2650. temp |= FDI_COMPOSITE_SYNC;
  2651. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2652. I915_WRITE(FDI_RX_MISC(pipe),
  2653. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2654. reg = FDI_RX_CTL(pipe);
  2655. temp = I915_READ(reg);
  2656. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2657. temp |= FDI_COMPOSITE_SYNC;
  2658. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2659. POSTING_READ(reg);
  2660. udelay(1); /* should be 0.5us */
  2661. for (i = 0; i < 4; i++) {
  2662. reg = FDI_RX_IIR(pipe);
  2663. temp = I915_READ(reg);
  2664. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2665. if (temp & FDI_RX_BIT_LOCK ||
  2666. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2667. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2668. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  2669. i);
  2670. break;
  2671. }
  2672. udelay(1); /* should be 0.5us */
  2673. }
  2674. if (i == 4) {
  2675. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  2676. continue;
  2677. }
  2678. /* Train 2 */
  2679. reg = FDI_TX_CTL(pipe);
  2680. temp = I915_READ(reg);
  2681. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2682. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2683. I915_WRITE(reg, temp);
  2684. reg = FDI_RX_CTL(pipe);
  2685. temp = I915_READ(reg);
  2686. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2687. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2688. I915_WRITE(reg, temp);
  2689. POSTING_READ(reg);
  2690. udelay(2); /* should be 1.5us */
  2691. for (i = 0; i < 4; i++) {
  2692. reg = FDI_RX_IIR(pipe);
  2693. temp = I915_READ(reg);
  2694. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2695. if (temp & FDI_RX_SYMBOL_LOCK ||
  2696. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  2697. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2698. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  2699. i);
  2700. goto train_done;
  2701. }
  2702. udelay(2); /* should be 1.5us */
  2703. }
  2704. if (i == 4)
  2705. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  2706. }
  2707. train_done:
  2708. DRM_DEBUG_KMS("FDI train done.\n");
  2709. }
  2710. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  2711. {
  2712. struct drm_device *dev = intel_crtc->base.dev;
  2713. struct drm_i915_private *dev_priv = dev->dev_private;
  2714. int pipe = intel_crtc->pipe;
  2715. u32 reg, temp;
  2716. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2717. reg = FDI_RX_CTL(pipe);
  2718. temp = I915_READ(reg);
  2719. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  2720. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2721. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2722. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2723. POSTING_READ(reg);
  2724. udelay(200);
  2725. /* Switch from Rawclk to PCDclk */
  2726. temp = I915_READ(reg);
  2727. I915_WRITE(reg, temp | FDI_PCDCLK);
  2728. POSTING_READ(reg);
  2729. udelay(200);
  2730. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2731. reg = FDI_TX_CTL(pipe);
  2732. temp = I915_READ(reg);
  2733. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2734. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2735. POSTING_READ(reg);
  2736. udelay(100);
  2737. }
  2738. }
  2739. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  2740. {
  2741. struct drm_device *dev = intel_crtc->base.dev;
  2742. struct drm_i915_private *dev_priv = dev->dev_private;
  2743. int pipe = intel_crtc->pipe;
  2744. u32 reg, temp;
  2745. /* Switch from PCDclk to Rawclk */
  2746. reg = FDI_RX_CTL(pipe);
  2747. temp = I915_READ(reg);
  2748. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2749. /* Disable CPU FDI TX PLL */
  2750. reg = FDI_TX_CTL(pipe);
  2751. temp = I915_READ(reg);
  2752. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2753. POSTING_READ(reg);
  2754. udelay(100);
  2755. reg = FDI_RX_CTL(pipe);
  2756. temp = I915_READ(reg);
  2757. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2758. /* Wait for the clocks to turn off. */
  2759. POSTING_READ(reg);
  2760. udelay(100);
  2761. }
  2762. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2763. {
  2764. struct drm_device *dev = crtc->dev;
  2765. struct drm_i915_private *dev_priv = dev->dev_private;
  2766. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2767. int pipe = intel_crtc->pipe;
  2768. u32 reg, temp;
  2769. /* disable CPU FDI tx and PCH FDI rx */
  2770. reg = FDI_TX_CTL(pipe);
  2771. temp = I915_READ(reg);
  2772. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2773. POSTING_READ(reg);
  2774. reg = FDI_RX_CTL(pipe);
  2775. temp = I915_READ(reg);
  2776. temp &= ~(0x7 << 16);
  2777. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2778. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2779. POSTING_READ(reg);
  2780. udelay(100);
  2781. /* Ironlake workaround, disable clock pointer after downing FDI */
  2782. if (HAS_PCH_IBX(dev))
  2783. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2784. /* still set train pattern 1 */
  2785. reg = FDI_TX_CTL(pipe);
  2786. temp = I915_READ(reg);
  2787. temp &= ~FDI_LINK_TRAIN_NONE;
  2788. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2789. I915_WRITE(reg, temp);
  2790. reg = FDI_RX_CTL(pipe);
  2791. temp = I915_READ(reg);
  2792. if (HAS_PCH_CPT(dev)) {
  2793. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2794. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2795. } else {
  2796. temp &= ~FDI_LINK_TRAIN_NONE;
  2797. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2798. }
  2799. /* BPC in FDI rx is consistent with that in PIPECONF */
  2800. temp &= ~(0x07 << 16);
  2801. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2802. I915_WRITE(reg, temp);
  2803. POSTING_READ(reg);
  2804. udelay(100);
  2805. }
  2806. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  2807. {
  2808. struct intel_crtc *crtc;
  2809. /* Note that we don't need to be called with mode_config.lock here
  2810. * as our list of CRTC objects is static for the lifetime of the
  2811. * device and so cannot disappear as we iterate. Similarly, we can
  2812. * happily treat the predicates as racy, atomic checks as userspace
  2813. * cannot claim and pin a new fb without at least acquring the
  2814. * struct_mutex and so serialising with us.
  2815. */
  2816. for_each_intel_crtc(dev, crtc) {
  2817. if (atomic_read(&crtc->unpin_work_count) == 0)
  2818. continue;
  2819. if (crtc->unpin_work)
  2820. intel_wait_for_vblank(dev, crtc->pipe);
  2821. return true;
  2822. }
  2823. return false;
  2824. }
  2825. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  2826. {
  2827. struct drm_device *dev = crtc->dev;
  2828. struct drm_i915_private *dev_priv = dev->dev_private;
  2829. if (crtc->primary->fb == NULL)
  2830. return;
  2831. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  2832. WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  2833. !intel_crtc_has_pending_flip(crtc),
  2834. 60*HZ) == 0);
  2835. mutex_lock(&dev->struct_mutex);
  2836. intel_finish_fb(crtc->primary->fb);
  2837. mutex_unlock(&dev->struct_mutex);
  2838. }
  2839. /* Program iCLKIP clock to the desired frequency */
  2840. static void lpt_program_iclkip(struct drm_crtc *crtc)
  2841. {
  2842. struct drm_device *dev = crtc->dev;
  2843. struct drm_i915_private *dev_priv = dev->dev_private;
  2844. int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
  2845. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  2846. u32 temp;
  2847. mutex_lock(&dev_priv->dpio_lock);
  2848. /* It is necessary to ungate the pixclk gate prior to programming
  2849. * the divisors, and gate it back when it is done.
  2850. */
  2851. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  2852. /* Disable SSCCTL */
  2853. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  2854. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  2855. SBI_SSCCTL_DISABLE,
  2856. SBI_ICLK);
  2857. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  2858. if (clock == 20000) {
  2859. auxdiv = 1;
  2860. divsel = 0x41;
  2861. phaseinc = 0x20;
  2862. } else {
  2863. /* The iCLK virtual clock root frequency is in MHz,
  2864. * but the adjusted_mode->crtc_clock in in KHz. To get the
  2865. * divisors, it is necessary to divide one by another, so we
  2866. * convert the virtual clock precision to KHz here for higher
  2867. * precision.
  2868. */
  2869. u32 iclk_virtual_root_freq = 172800 * 1000;
  2870. u32 iclk_pi_range = 64;
  2871. u32 desired_divisor, msb_divisor_value, pi_value;
  2872. desired_divisor = (iclk_virtual_root_freq / clock);
  2873. msb_divisor_value = desired_divisor / iclk_pi_range;
  2874. pi_value = desired_divisor % iclk_pi_range;
  2875. auxdiv = 0;
  2876. divsel = msb_divisor_value - 2;
  2877. phaseinc = pi_value;
  2878. }
  2879. /* This should not happen with any sane values */
  2880. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  2881. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  2882. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  2883. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  2884. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  2885. clock,
  2886. auxdiv,
  2887. divsel,
  2888. phasedir,
  2889. phaseinc);
  2890. /* Program SSCDIVINTPHASE6 */
  2891. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  2892. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  2893. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  2894. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  2895. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  2896. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  2897. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  2898. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  2899. /* Program SSCAUXDIV */
  2900. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  2901. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  2902. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  2903. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  2904. /* Enable modulator and associated divider */
  2905. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  2906. temp &= ~SBI_SSCCTL_DISABLE;
  2907. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  2908. /* Wait for initialization time */
  2909. udelay(24);
  2910. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  2911. mutex_unlock(&dev_priv->dpio_lock);
  2912. }
  2913. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  2914. enum pipe pch_transcoder)
  2915. {
  2916. struct drm_device *dev = crtc->base.dev;
  2917. struct drm_i915_private *dev_priv = dev->dev_private;
  2918. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  2919. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  2920. I915_READ(HTOTAL(cpu_transcoder)));
  2921. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  2922. I915_READ(HBLANK(cpu_transcoder)));
  2923. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  2924. I915_READ(HSYNC(cpu_transcoder)));
  2925. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  2926. I915_READ(VTOTAL(cpu_transcoder)));
  2927. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  2928. I915_READ(VBLANK(cpu_transcoder)));
  2929. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  2930. I915_READ(VSYNC(cpu_transcoder)));
  2931. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  2932. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  2933. }
  2934. static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
  2935. {
  2936. struct drm_i915_private *dev_priv = dev->dev_private;
  2937. uint32_t temp;
  2938. temp = I915_READ(SOUTH_CHICKEN1);
  2939. if (temp & FDI_BC_BIFURCATION_SELECT)
  2940. return;
  2941. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2942. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2943. temp |= FDI_BC_BIFURCATION_SELECT;
  2944. DRM_DEBUG_KMS("enabling fdi C rx\n");
  2945. I915_WRITE(SOUTH_CHICKEN1, temp);
  2946. POSTING_READ(SOUTH_CHICKEN1);
  2947. }
  2948. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  2949. {
  2950. struct drm_device *dev = intel_crtc->base.dev;
  2951. struct drm_i915_private *dev_priv = dev->dev_private;
  2952. switch (intel_crtc->pipe) {
  2953. case PIPE_A:
  2954. break;
  2955. case PIPE_B:
  2956. if (intel_crtc->config.fdi_lanes > 2)
  2957. WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
  2958. else
  2959. cpt_enable_fdi_bc_bifurcation(dev);
  2960. break;
  2961. case PIPE_C:
  2962. cpt_enable_fdi_bc_bifurcation(dev);
  2963. break;
  2964. default:
  2965. BUG();
  2966. }
  2967. }
  2968. /*
  2969. * Enable PCH resources required for PCH ports:
  2970. * - PCH PLLs
  2971. * - FDI training & RX/TX
  2972. * - update transcoder timings
  2973. * - DP transcoding bits
  2974. * - transcoder
  2975. */
  2976. static void ironlake_pch_enable(struct drm_crtc *crtc)
  2977. {
  2978. struct drm_device *dev = crtc->dev;
  2979. struct drm_i915_private *dev_priv = dev->dev_private;
  2980. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2981. int pipe = intel_crtc->pipe;
  2982. u32 reg, temp;
  2983. assert_pch_transcoder_disabled(dev_priv, pipe);
  2984. if (IS_IVYBRIDGE(dev))
  2985. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  2986. /* Write the TU size bits before fdi link training, so that error
  2987. * detection works. */
  2988. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  2989. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  2990. /* For PCH output, training FDI link */
  2991. dev_priv->display.fdi_link_train(crtc);
  2992. /* We need to program the right clock selection before writing the pixel
  2993. * mutliplier into the DPLL. */
  2994. if (HAS_PCH_CPT(dev)) {
  2995. u32 sel;
  2996. temp = I915_READ(PCH_DPLL_SEL);
  2997. temp |= TRANS_DPLL_ENABLE(pipe);
  2998. sel = TRANS_DPLLB_SEL(pipe);
  2999. if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
  3000. temp |= sel;
  3001. else
  3002. temp &= ~sel;
  3003. I915_WRITE(PCH_DPLL_SEL, temp);
  3004. }
  3005. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3006. * transcoder, and we actually should do this to not upset any PCH
  3007. * transcoder that already use the clock when we share it.
  3008. *
  3009. * Note that enable_shared_dpll tries to do the right thing, but
  3010. * get_shared_dpll unconditionally resets the pll - we need that to have
  3011. * the right LVDS enable sequence. */
  3012. intel_enable_shared_dpll(intel_crtc);
  3013. /* set transcoder timing, panel must allow it */
  3014. assert_panel_unlocked(dev_priv, pipe);
  3015. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3016. intel_fdi_normal_train(crtc);
  3017. /* For PCH DP, enable TRANS_DP_CTL */
  3018. if (HAS_PCH_CPT(dev) &&
  3019. (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  3020. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  3021. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3022. reg = TRANS_DP_CTL(pipe);
  3023. temp = I915_READ(reg);
  3024. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3025. TRANS_DP_SYNC_MASK |
  3026. TRANS_DP_BPC_MASK);
  3027. temp |= (TRANS_DP_OUTPUT_ENABLE |
  3028. TRANS_DP_ENH_FRAMING);
  3029. temp |= bpc << 9; /* same format but at 11:9 */
  3030. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3031. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3032. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3033. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3034. switch (intel_trans_dp_port_sel(crtc)) {
  3035. case PCH_DP_B:
  3036. temp |= TRANS_DP_PORT_SEL_B;
  3037. break;
  3038. case PCH_DP_C:
  3039. temp |= TRANS_DP_PORT_SEL_C;
  3040. break;
  3041. case PCH_DP_D:
  3042. temp |= TRANS_DP_PORT_SEL_D;
  3043. break;
  3044. default:
  3045. BUG();
  3046. }
  3047. I915_WRITE(reg, temp);
  3048. }
  3049. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3050. }
  3051. static void lpt_pch_enable(struct drm_crtc *crtc)
  3052. {
  3053. struct drm_device *dev = crtc->dev;
  3054. struct drm_i915_private *dev_priv = dev->dev_private;
  3055. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3056. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3057. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3058. lpt_program_iclkip(crtc);
  3059. /* Set transcoder timing. */
  3060. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3061. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3062. }
  3063. static void intel_put_shared_dpll(struct intel_crtc *crtc)
  3064. {
  3065. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3066. if (pll == NULL)
  3067. return;
  3068. if (pll->refcount == 0) {
  3069. WARN(1, "bad %s refcount\n", pll->name);
  3070. return;
  3071. }
  3072. if (--pll->refcount == 0) {
  3073. WARN_ON(pll->on);
  3074. WARN_ON(pll->active);
  3075. }
  3076. crtc->config.shared_dpll = DPLL_ID_PRIVATE;
  3077. }
  3078. static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
  3079. {
  3080. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3081. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3082. enum intel_dpll_id i;
  3083. if (pll) {
  3084. DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
  3085. crtc->base.base.id, pll->name);
  3086. intel_put_shared_dpll(crtc);
  3087. }
  3088. if (HAS_PCH_IBX(dev_priv->dev)) {
  3089. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3090. i = (enum intel_dpll_id) crtc->pipe;
  3091. pll = &dev_priv->shared_dplls[i];
  3092. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3093. crtc->base.base.id, pll->name);
  3094. WARN_ON(pll->refcount);
  3095. goto found;
  3096. }
  3097. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3098. pll = &dev_priv->shared_dplls[i];
  3099. /* Only want to check enabled timings first */
  3100. if (pll->refcount == 0)
  3101. continue;
  3102. if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
  3103. sizeof(pll->hw_state)) == 0) {
  3104. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
  3105. crtc->base.base.id,
  3106. pll->name, pll->refcount, pll->active);
  3107. goto found;
  3108. }
  3109. }
  3110. /* Ok no matching timings, maybe there's a free one? */
  3111. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3112. pll = &dev_priv->shared_dplls[i];
  3113. if (pll->refcount == 0) {
  3114. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3115. crtc->base.base.id, pll->name);
  3116. goto found;
  3117. }
  3118. }
  3119. return NULL;
  3120. found:
  3121. if (pll->refcount == 0)
  3122. pll->hw_state = crtc->config.dpll_hw_state;
  3123. crtc->config.shared_dpll = i;
  3124. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3125. pipe_name(crtc->pipe));
  3126. pll->refcount++;
  3127. return pll;
  3128. }
  3129. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3130. {
  3131. struct drm_i915_private *dev_priv = dev->dev_private;
  3132. int dslreg = PIPEDSL(pipe);
  3133. u32 temp;
  3134. temp = I915_READ(dslreg);
  3135. udelay(500);
  3136. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3137. if (wait_for(I915_READ(dslreg) != temp, 5))
  3138. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3139. }
  3140. }
  3141. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3142. {
  3143. struct drm_device *dev = crtc->base.dev;
  3144. struct drm_i915_private *dev_priv = dev->dev_private;
  3145. int pipe = crtc->pipe;
  3146. if (crtc->config.pch_pfit.enabled) {
  3147. /* Force use of hard-coded filter coefficients
  3148. * as some pre-programmed values are broken,
  3149. * e.g. x201.
  3150. */
  3151. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3152. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3153. PF_PIPE_SEL_IVB(pipe));
  3154. else
  3155. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3156. I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
  3157. I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
  3158. }
  3159. }
  3160. static void intel_enable_planes(struct drm_crtc *crtc)
  3161. {
  3162. struct drm_device *dev = crtc->dev;
  3163. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3164. struct drm_plane *plane;
  3165. struct intel_plane *intel_plane;
  3166. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3167. intel_plane = to_intel_plane(plane);
  3168. if (intel_plane->pipe == pipe)
  3169. intel_plane_restore(&intel_plane->base);
  3170. }
  3171. }
  3172. static void intel_disable_planes(struct drm_crtc *crtc)
  3173. {
  3174. struct drm_device *dev = crtc->dev;
  3175. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3176. struct drm_plane *plane;
  3177. struct intel_plane *intel_plane;
  3178. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3179. intel_plane = to_intel_plane(plane);
  3180. if (intel_plane->pipe == pipe)
  3181. intel_plane_disable(&intel_plane->base);
  3182. }
  3183. }
  3184. void hsw_enable_ips(struct intel_crtc *crtc)
  3185. {
  3186. struct drm_device *dev = crtc->base.dev;
  3187. struct drm_i915_private *dev_priv = dev->dev_private;
  3188. if (!crtc->config.ips_enabled)
  3189. return;
  3190. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3191. intel_wait_for_vblank(dev, crtc->pipe);
  3192. assert_plane_enabled(dev_priv, crtc->plane);
  3193. if (IS_BROADWELL(dev)) {
  3194. mutex_lock(&dev_priv->rps.hw_lock);
  3195. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3196. mutex_unlock(&dev_priv->rps.hw_lock);
  3197. /* Quoting Art Runyan: "its not safe to expect any particular
  3198. * value in IPS_CTL bit 31 after enabling IPS through the
  3199. * mailbox." Moreover, the mailbox may return a bogus state,
  3200. * so we need to just enable it and continue on.
  3201. */
  3202. } else {
  3203. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3204. /* The bit only becomes 1 in the next vblank, so this wait here
  3205. * is essentially intel_wait_for_vblank. If we don't have this
  3206. * and don't wait for vblanks until the end of crtc_enable, then
  3207. * the HW state readout code will complain that the expected
  3208. * IPS_CTL value is not the one we read. */
  3209. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3210. DRM_ERROR("Timed out waiting for IPS enable\n");
  3211. }
  3212. }
  3213. void hsw_disable_ips(struct intel_crtc *crtc)
  3214. {
  3215. struct drm_device *dev = crtc->base.dev;
  3216. struct drm_i915_private *dev_priv = dev->dev_private;
  3217. if (!crtc->config.ips_enabled)
  3218. return;
  3219. assert_plane_enabled(dev_priv, crtc->plane);
  3220. if (IS_BROADWELL(dev)) {
  3221. mutex_lock(&dev_priv->rps.hw_lock);
  3222. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3223. mutex_unlock(&dev_priv->rps.hw_lock);
  3224. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3225. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3226. DRM_ERROR("Timed out waiting for IPS disable\n");
  3227. } else {
  3228. I915_WRITE(IPS_CTL, 0);
  3229. POSTING_READ(IPS_CTL);
  3230. }
  3231. /* We need to wait for a vblank before we can disable the plane. */
  3232. intel_wait_for_vblank(dev, crtc->pipe);
  3233. }
  3234. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3235. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3236. {
  3237. struct drm_device *dev = crtc->dev;
  3238. struct drm_i915_private *dev_priv = dev->dev_private;
  3239. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3240. enum pipe pipe = intel_crtc->pipe;
  3241. int palreg = PALETTE(pipe);
  3242. int i;
  3243. bool reenable_ips = false;
  3244. /* The clocks have to be on to load the palette. */
  3245. if (!crtc->enabled || !intel_crtc->active)
  3246. return;
  3247. if (!HAS_PCH_SPLIT(dev_priv->dev)) {
  3248. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  3249. assert_dsi_pll_enabled(dev_priv);
  3250. else
  3251. assert_pll_enabled(dev_priv, pipe);
  3252. }
  3253. /* use legacy palette for Ironlake */
  3254. if (HAS_PCH_SPLIT(dev))
  3255. palreg = LGC_PALETTE(pipe);
  3256. /* Workaround : Do not read or write the pipe palette/gamma data while
  3257. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3258. */
  3259. if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
  3260. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3261. GAMMA_MODE_MODE_SPLIT)) {
  3262. hsw_disable_ips(intel_crtc);
  3263. reenable_ips = true;
  3264. }
  3265. for (i = 0; i < 256; i++) {
  3266. I915_WRITE(palreg + 4 * i,
  3267. (intel_crtc->lut_r[i] << 16) |
  3268. (intel_crtc->lut_g[i] << 8) |
  3269. intel_crtc->lut_b[i]);
  3270. }
  3271. if (reenable_ips)
  3272. hsw_enable_ips(intel_crtc);
  3273. }
  3274. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  3275. {
  3276. if (!enable && intel_crtc->overlay) {
  3277. struct drm_device *dev = intel_crtc->base.dev;
  3278. struct drm_i915_private *dev_priv = dev->dev_private;
  3279. mutex_lock(&dev->struct_mutex);
  3280. dev_priv->mm.interruptible = false;
  3281. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3282. dev_priv->mm.interruptible = true;
  3283. mutex_unlock(&dev->struct_mutex);
  3284. }
  3285. /* Let userspace switch the overlay on again. In most cases userspace
  3286. * has to recompute where to put it anyway.
  3287. */
  3288. }
  3289. /**
  3290. * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
  3291. * cursor plane briefly if not already running after enabling the display
  3292. * plane.
  3293. * This workaround avoids occasional blank screens when self refresh is
  3294. * enabled.
  3295. */
  3296. static void
  3297. g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
  3298. {
  3299. u32 cntl = I915_READ(CURCNTR(pipe));
  3300. if ((cntl & CURSOR_MODE) == 0) {
  3301. u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
  3302. I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
  3303. I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
  3304. intel_wait_for_vblank(dev_priv->dev, pipe);
  3305. I915_WRITE(CURCNTR(pipe), cntl);
  3306. I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
  3307. I915_WRITE(FW_BLC_SELF, fw_bcl_self);
  3308. }
  3309. }
  3310. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  3311. {
  3312. struct drm_device *dev = crtc->dev;
  3313. struct drm_i915_private *dev_priv = dev->dev_private;
  3314. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3315. int pipe = intel_crtc->pipe;
  3316. int plane = intel_crtc->plane;
  3317. intel_enable_primary_hw_plane(dev_priv, plane, pipe);
  3318. intel_enable_planes(crtc);
  3319. /* The fixup needs to happen before cursor is enabled */
  3320. if (IS_G4X(dev))
  3321. g4x_fixup_plane(dev_priv, pipe);
  3322. intel_crtc_update_cursor(crtc, true);
  3323. intel_crtc_dpms_overlay(intel_crtc, true);
  3324. hsw_enable_ips(intel_crtc);
  3325. mutex_lock(&dev->struct_mutex);
  3326. intel_update_fbc(dev);
  3327. intel_edp_psr_update(dev);
  3328. mutex_unlock(&dev->struct_mutex);
  3329. }
  3330. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  3331. {
  3332. struct drm_device *dev = crtc->dev;
  3333. struct drm_i915_private *dev_priv = dev->dev_private;
  3334. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3335. int pipe = intel_crtc->pipe;
  3336. int plane = intel_crtc->plane;
  3337. intel_crtc_wait_for_pending_flips(crtc);
  3338. drm_crtc_vblank_off(crtc);
  3339. if (dev_priv->fbc.plane == plane)
  3340. intel_disable_fbc(dev);
  3341. hsw_disable_ips(intel_crtc);
  3342. intel_crtc_dpms_overlay(intel_crtc, false);
  3343. intel_crtc_update_cursor(crtc, false);
  3344. intel_disable_planes(crtc);
  3345. intel_disable_primary_hw_plane(dev_priv, plane, pipe);
  3346. }
  3347. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  3348. {
  3349. struct drm_device *dev = crtc->dev;
  3350. struct drm_i915_private *dev_priv = dev->dev_private;
  3351. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3352. struct intel_encoder *encoder;
  3353. int pipe = intel_crtc->pipe;
  3354. enum plane plane = intel_crtc->plane;
  3355. WARN_ON(!crtc->enabled);
  3356. if (intel_crtc->active)
  3357. return;
  3358. if (intel_crtc->config.has_pch_encoder)
  3359. intel_prepare_shared_dpll(intel_crtc);
  3360. if (intel_crtc->config.has_dp_encoder)
  3361. intel_dp_set_m_n(intel_crtc);
  3362. intel_set_pipe_timings(intel_crtc);
  3363. if (intel_crtc->config.has_pch_encoder) {
  3364. intel_cpu_transcoder_set_m_n(intel_crtc,
  3365. &intel_crtc->config.fdi_m_n);
  3366. }
  3367. ironlake_set_pipeconf(crtc);
  3368. /* Set up the display plane register */
  3369. I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
  3370. POSTING_READ(DSPCNTR(plane));
  3371. dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
  3372. crtc->x, crtc->y);
  3373. intel_crtc->active = true;
  3374. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3375. intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
  3376. for_each_encoder_on_crtc(dev, crtc, encoder)
  3377. if (encoder->pre_enable)
  3378. encoder->pre_enable(encoder);
  3379. if (intel_crtc->config.has_pch_encoder) {
  3380. /* Note: FDI PLL enabling _must_ be done before we enable the
  3381. * cpu pipes, hence this is separate from all the other fdi/pch
  3382. * enabling. */
  3383. ironlake_fdi_pll_enable(intel_crtc);
  3384. } else {
  3385. assert_fdi_tx_disabled(dev_priv, pipe);
  3386. assert_fdi_rx_disabled(dev_priv, pipe);
  3387. }
  3388. ironlake_pfit_enable(intel_crtc);
  3389. /*
  3390. * On ILK+ LUT must be loaded before the pipe is running but with
  3391. * clocks enabled
  3392. */
  3393. intel_crtc_load_lut(crtc);
  3394. intel_update_watermarks(crtc);
  3395. intel_enable_pipe(intel_crtc);
  3396. if (intel_crtc->config.has_pch_encoder)
  3397. ironlake_pch_enable(crtc);
  3398. for_each_encoder_on_crtc(dev, crtc, encoder)
  3399. encoder->enable(encoder);
  3400. if (HAS_PCH_CPT(dev))
  3401. cpt_verify_modeset(dev, intel_crtc->pipe);
  3402. intel_crtc_enable_planes(crtc);
  3403. drm_crtc_vblank_on(crtc);
  3404. }
  3405. /* IPS only exists on ULT machines and is tied to pipe A. */
  3406. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  3407. {
  3408. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  3409. }
  3410. /*
  3411. * This implements the workaround described in the "notes" section of the mode
  3412. * set sequence documentation. When going from no pipes or single pipe to
  3413. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  3414. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  3415. */
  3416. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  3417. {
  3418. struct drm_device *dev = crtc->base.dev;
  3419. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  3420. /* We want to get the other_active_crtc only if there's only 1 other
  3421. * active crtc. */
  3422. for_each_intel_crtc(dev, crtc_it) {
  3423. if (!crtc_it->active || crtc_it == crtc)
  3424. continue;
  3425. if (other_active_crtc)
  3426. return;
  3427. other_active_crtc = crtc_it;
  3428. }
  3429. if (!other_active_crtc)
  3430. return;
  3431. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3432. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3433. }
  3434. static void haswell_crtc_enable(struct drm_crtc *crtc)
  3435. {
  3436. struct drm_device *dev = crtc->dev;
  3437. struct drm_i915_private *dev_priv = dev->dev_private;
  3438. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3439. struct intel_encoder *encoder;
  3440. int pipe = intel_crtc->pipe;
  3441. enum plane plane = intel_crtc->plane;
  3442. WARN_ON(!crtc->enabled);
  3443. if (intel_crtc->active)
  3444. return;
  3445. if (intel_crtc->config.has_dp_encoder)
  3446. intel_dp_set_m_n(intel_crtc);
  3447. intel_set_pipe_timings(intel_crtc);
  3448. if (intel_crtc->config.has_pch_encoder) {
  3449. intel_cpu_transcoder_set_m_n(intel_crtc,
  3450. &intel_crtc->config.fdi_m_n);
  3451. }
  3452. haswell_set_pipeconf(crtc);
  3453. intel_set_pipe_csc(crtc);
  3454. /* Set up the display plane register */
  3455. I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
  3456. POSTING_READ(DSPCNTR(plane));
  3457. dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
  3458. crtc->x, crtc->y);
  3459. intel_crtc->active = true;
  3460. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3461. if (intel_crtc->config.has_pch_encoder)
  3462. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
  3463. if (intel_crtc->config.has_pch_encoder)
  3464. dev_priv->display.fdi_link_train(crtc);
  3465. for_each_encoder_on_crtc(dev, crtc, encoder)
  3466. if (encoder->pre_enable)
  3467. encoder->pre_enable(encoder);
  3468. intel_ddi_enable_pipe_clock(intel_crtc);
  3469. ironlake_pfit_enable(intel_crtc);
  3470. /*
  3471. * On ILK+ LUT must be loaded before the pipe is running but with
  3472. * clocks enabled
  3473. */
  3474. intel_crtc_load_lut(crtc);
  3475. intel_ddi_set_pipe_settings(crtc);
  3476. intel_ddi_enable_transcoder_func(crtc);
  3477. intel_update_watermarks(crtc);
  3478. intel_enable_pipe(intel_crtc);
  3479. if (intel_crtc->config.has_pch_encoder)
  3480. lpt_pch_enable(crtc);
  3481. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3482. encoder->enable(encoder);
  3483. intel_opregion_notify_encoder(encoder, true);
  3484. }
  3485. /* If we change the relative order between pipe/planes enabling, we need
  3486. * to change the workaround. */
  3487. haswell_mode_set_planes_workaround(intel_crtc);
  3488. intel_crtc_enable_planes(crtc);
  3489. drm_crtc_vblank_on(crtc);
  3490. }
  3491. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  3492. {
  3493. struct drm_device *dev = crtc->base.dev;
  3494. struct drm_i915_private *dev_priv = dev->dev_private;
  3495. int pipe = crtc->pipe;
  3496. /* To avoid upsetting the power well on haswell only disable the pfit if
  3497. * it's in use. The hw state code will make sure we get this right. */
  3498. if (crtc->config.pch_pfit.enabled) {
  3499. I915_WRITE(PF_CTL(pipe), 0);
  3500. I915_WRITE(PF_WIN_POS(pipe), 0);
  3501. I915_WRITE(PF_WIN_SZ(pipe), 0);
  3502. }
  3503. }
  3504. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  3505. {
  3506. struct drm_device *dev = crtc->dev;
  3507. struct drm_i915_private *dev_priv = dev->dev_private;
  3508. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3509. struct intel_encoder *encoder;
  3510. int pipe = intel_crtc->pipe;
  3511. u32 reg, temp;
  3512. if (!intel_crtc->active)
  3513. return;
  3514. intel_crtc_disable_planes(crtc);
  3515. for_each_encoder_on_crtc(dev, crtc, encoder)
  3516. encoder->disable(encoder);
  3517. if (intel_crtc->config.has_pch_encoder)
  3518. intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
  3519. intel_disable_pipe(dev_priv, pipe);
  3520. ironlake_pfit_disable(intel_crtc);
  3521. for_each_encoder_on_crtc(dev, crtc, encoder)
  3522. if (encoder->post_disable)
  3523. encoder->post_disable(encoder);
  3524. if (intel_crtc->config.has_pch_encoder) {
  3525. ironlake_fdi_disable(crtc);
  3526. ironlake_disable_pch_transcoder(dev_priv, pipe);
  3527. intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
  3528. if (HAS_PCH_CPT(dev)) {
  3529. /* disable TRANS_DP_CTL */
  3530. reg = TRANS_DP_CTL(pipe);
  3531. temp = I915_READ(reg);
  3532. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  3533. TRANS_DP_PORT_SEL_MASK);
  3534. temp |= TRANS_DP_PORT_SEL_NONE;
  3535. I915_WRITE(reg, temp);
  3536. /* disable DPLL_SEL */
  3537. temp = I915_READ(PCH_DPLL_SEL);
  3538. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  3539. I915_WRITE(PCH_DPLL_SEL, temp);
  3540. }
  3541. /* disable PCH DPLL */
  3542. intel_disable_shared_dpll(intel_crtc);
  3543. ironlake_fdi_pll_disable(intel_crtc);
  3544. }
  3545. intel_crtc->active = false;
  3546. intel_update_watermarks(crtc);
  3547. mutex_lock(&dev->struct_mutex);
  3548. intel_update_fbc(dev);
  3549. intel_edp_psr_update(dev);
  3550. mutex_unlock(&dev->struct_mutex);
  3551. }
  3552. static void haswell_crtc_disable(struct drm_crtc *crtc)
  3553. {
  3554. struct drm_device *dev = crtc->dev;
  3555. struct drm_i915_private *dev_priv = dev->dev_private;
  3556. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3557. struct intel_encoder *encoder;
  3558. int pipe = intel_crtc->pipe;
  3559. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3560. if (!intel_crtc->active)
  3561. return;
  3562. intel_crtc_disable_planes(crtc);
  3563. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3564. intel_opregion_notify_encoder(encoder, false);
  3565. encoder->disable(encoder);
  3566. }
  3567. if (intel_crtc->config.has_pch_encoder)
  3568. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
  3569. intel_disable_pipe(dev_priv, pipe);
  3570. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  3571. ironlake_pfit_disable(intel_crtc);
  3572. intel_ddi_disable_pipe_clock(intel_crtc);
  3573. for_each_encoder_on_crtc(dev, crtc, encoder)
  3574. if (encoder->post_disable)
  3575. encoder->post_disable(encoder);
  3576. if (intel_crtc->config.has_pch_encoder) {
  3577. lpt_disable_pch_transcoder(dev_priv);
  3578. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
  3579. intel_ddi_fdi_disable(crtc);
  3580. }
  3581. intel_crtc->active = false;
  3582. intel_update_watermarks(crtc);
  3583. mutex_lock(&dev->struct_mutex);
  3584. intel_update_fbc(dev);
  3585. intel_edp_psr_update(dev);
  3586. mutex_unlock(&dev->struct_mutex);
  3587. }
  3588. static void ironlake_crtc_off(struct drm_crtc *crtc)
  3589. {
  3590. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3591. intel_put_shared_dpll(intel_crtc);
  3592. }
  3593. static void haswell_crtc_off(struct drm_crtc *crtc)
  3594. {
  3595. intel_ddi_put_crtc_pll(crtc);
  3596. }
  3597. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  3598. {
  3599. struct drm_device *dev = crtc->base.dev;
  3600. struct drm_i915_private *dev_priv = dev->dev_private;
  3601. struct intel_crtc_config *pipe_config = &crtc->config;
  3602. if (!crtc->config.gmch_pfit.control)
  3603. return;
  3604. /*
  3605. * The panel fitter should only be adjusted whilst the pipe is disabled,
  3606. * according to register description and PRM.
  3607. */
  3608. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  3609. assert_pipe_disabled(dev_priv, crtc->pipe);
  3610. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  3611. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  3612. /* Border color in case we don't scale up to the full screen. Black by
  3613. * default, change to something else for debugging. */
  3614. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  3615. }
  3616. #define for_each_power_domain(domain, mask) \
  3617. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  3618. if ((1 << (domain)) & (mask))
  3619. enum intel_display_power_domain
  3620. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  3621. {
  3622. struct drm_device *dev = intel_encoder->base.dev;
  3623. struct intel_digital_port *intel_dig_port;
  3624. switch (intel_encoder->type) {
  3625. case INTEL_OUTPUT_UNKNOWN:
  3626. /* Only DDI platforms should ever use this output type */
  3627. WARN_ON_ONCE(!HAS_DDI(dev));
  3628. case INTEL_OUTPUT_DISPLAYPORT:
  3629. case INTEL_OUTPUT_HDMI:
  3630. case INTEL_OUTPUT_EDP:
  3631. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  3632. switch (intel_dig_port->port) {
  3633. case PORT_A:
  3634. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  3635. case PORT_B:
  3636. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  3637. case PORT_C:
  3638. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  3639. case PORT_D:
  3640. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  3641. default:
  3642. WARN_ON_ONCE(1);
  3643. return POWER_DOMAIN_PORT_OTHER;
  3644. }
  3645. case INTEL_OUTPUT_ANALOG:
  3646. return POWER_DOMAIN_PORT_CRT;
  3647. case INTEL_OUTPUT_DSI:
  3648. return POWER_DOMAIN_PORT_DSI;
  3649. default:
  3650. return POWER_DOMAIN_PORT_OTHER;
  3651. }
  3652. }
  3653. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  3654. {
  3655. struct drm_device *dev = crtc->dev;
  3656. struct intel_encoder *intel_encoder;
  3657. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3658. enum pipe pipe = intel_crtc->pipe;
  3659. bool pfit_enabled = intel_crtc->config.pch_pfit.enabled;
  3660. unsigned long mask;
  3661. enum transcoder transcoder;
  3662. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  3663. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  3664. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  3665. if (pfit_enabled)
  3666. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  3667. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  3668. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  3669. return mask;
  3670. }
  3671. void intel_display_set_init_power(struct drm_i915_private *dev_priv,
  3672. bool enable)
  3673. {
  3674. if (dev_priv->power_domains.init_power_on == enable)
  3675. return;
  3676. if (enable)
  3677. intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
  3678. else
  3679. intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
  3680. dev_priv->power_domains.init_power_on = enable;
  3681. }
  3682. static void modeset_update_crtc_power_domains(struct drm_device *dev)
  3683. {
  3684. struct drm_i915_private *dev_priv = dev->dev_private;
  3685. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  3686. struct intel_crtc *crtc;
  3687. /*
  3688. * First get all needed power domains, then put all unneeded, to avoid
  3689. * any unnecessary toggling of the power wells.
  3690. */
  3691. for_each_intel_crtc(dev, crtc) {
  3692. enum intel_display_power_domain domain;
  3693. if (!crtc->base.enabled)
  3694. continue;
  3695. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  3696. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  3697. intel_display_power_get(dev_priv, domain);
  3698. }
  3699. for_each_intel_crtc(dev, crtc) {
  3700. enum intel_display_power_domain domain;
  3701. for_each_power_domain(domain, crtc->enabled_power_domains)
  3702. intel_display_power_put(dev_priv, domain);
  3703. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  3704. }
  3705. intel_display_set_init_power(dev_priv, false);
  3706. }
  3707. int valleyview_get_vco(struct drm_i915_private *dev_priv)
  3708. {
  3709. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  3710. /* Obtain SKU information */
  3711. mutex_lock(&dev_priv->dpio_lock);
  3712. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  3713. CCK_FUSE_HPLL_FREQ_MASK;
  3714. mutex_unlock(&dev_priv->dpio_lock);
  3715. return vco_freq[hpll_freq];
  3716. }
  3717. /* Adjust CDclk dividers to allow high res or save power if possible */
  3718. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  3719. {
  3720. struct drm_i915_private *dev_priv = dev->dev_private;
  3721. u32 val, cmd;
  3722. WARN_ON(valleyview_cur_cdclk(dev_priv) != dev_priv->vlv_cdclk_freq);
  3723. dev_priv->vlv_cdclk_freq = cdclk;
  3724. if (cdclk >= 320) /* jump to highest voltage for 400MHz too */
  3725. cmd = 2;
  3726. else if (cdclk == 266)
  3727. cmd = 1;
  3728. else
  3729. cmd = 0;
  3730. mutex_lock(&dev_priv->rps.hw_lock);
  3731. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  3732. val &= ~DSPFREQGUAR_MASK;
  3733. val |= (cmd << DSPFREQGUAR_SHIFT);
  3734. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  3735. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  3736. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  3737. 50)) {
  3738. DRM_ERROR("timed out waiting for CDclk change\n");
  3739. }
  3740. mutex_unlock(&dev_priv->rps.hw_lock);
  3741. if (cdclk == 400) {
  3742. u32 divider, vco;
  3743. vco = valleyview_get_vco(dev_priv);
  3744. divider = ((vco << 1) / cdclk) - 1;
  3745. mutex_lock(&dev_priv->dpio_lock);
  3746. /* adjust cdclk divider */
  3747. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  3748. val &= ~0xf;
  3749. val |= divider;
  3750. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  3751. mutex_unlock(&dev_priv->dpio_lock);
  3752. }
  3753. mutex_lock(&dev_priv->dpio_lock);
  3754. /* adjust self-refresh exit latency value */
  3755. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  3756. val &= ~0x7f;
  3757. /*
  3758. * For high bandwidth configs, we set a higher latency in the bunit
  3759. * so that the core display fetch happens in time to avoid underruns.
  3760. */
  3761. if (cdclk == 400)
  3762. val |= 4500 / 250; /* 4.5 usec */
  3763. else
  3764. val |= 3000 / 250; /* 3.0 usec */
  3765. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  3766. mutex_unlock(&dev_priv->dpio_lock);
  3767. /* Since we changed the CDclk, we need to update the GMBUSFREQ too */
  3768. intel_i2c_reset(dev);
  3769. }
  3770. int valleyview_cur_cdclk(struct drm_i915_private *dev_priv)
  3771. {
  3772. int cur_cdclk, vco;
  3773. int divider;
  3774. vco = valleyview_get_vco(dev_priv);
  3775. mutex_lock(&dev_priv->dpio_lock);
  3776. divider = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  3777. mutex_unlock(&dev_priv->dpio_lock);
  3778. divider &= 0xf;
  3779. cur_cdclk = (vco << 1) / (divider + 1);
  3780. return cur_cdclk;
  3781. }
  3782. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  3783. int max_pixclk)
  3784. {
  3785. /*
  3786. * Really only a few cases to deal with, as only 4 CDclks are supported:
  3787. * 200MHz
  3788. * 267MHz
  3789. * 320MHz
  3790. * 400MHz
  3791. * So we check to see whether we're above 90% of the lower bin and
  3792. * adjust if needed.
  3793. */
  3794. if (max_pixclk > 288000) {
  3795. return 400;
  3796. } else if (max_pixclk > 240000) {
  3797. return 320;
  3798. } else
  3799. return 266;
  3800. /* Looks like the 200MHz CDclk freq doesn't work on some configs */
  3801. }
  3802. /* compute the max pixel clock for new configuration */
  3803. static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
  3804. {
  3805. struct drm_device *dev = dev_priv->dev;
  3806. struct intel_crtc *intel_crtc;
  3807. int max_pixclk = 0;
  3808. for_each_intel_crtc(dev, intel_crtc) {
  3809. if (intel_crtc->new_enabled)
  3810. max_pixclk = max(max_pixclk,
  3811. intel_crtc->new_config->adjusted_mode.crtc_clock);
  3812. }
  3813. return max_pixclk;
  3814. }
  3815. static void valleyview_modeset_global_pipes(struct drm_device *dev,
  3816. unsigned *prepare_pipes)
  3817. {
  3818. struct drm_i915_private *dev_priv = dev->dev_private;
  3819. struct intel_crtc *intel_crtc;
  3820. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  3821. if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
  3822. dev_priv->vlv_cdclk_freq)
  3823. return;
  3824. /* disable/enable all currently active pipes while we change cdclk */
  3825. for_each_intel_crtc(dev, intel_crtc)
  3826. if (intel_crtc->base.enabled)
  3827. *prepare_pipes |= (1 << intel_crtc->pipe);
  3828. }
  3829. static void valleyview_modeset_global_resources(struct drm_device *dev)
  3830. {
  3831. struct drm_i915_private *dev_priv = dev->dev_private;
  3832. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  3833. int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  3834. if (req_cdclk != dev_priv->vlv_cdclk_freq)
  3835. valleyview_set_cdclk(dev, req_cdclk);
  3836. modeset_update_crtc_power_domains(dev);
  3837. }
  3838. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  3839. {
  3840. struct drm_device *dev = crtc->dev;
  3841. struct drm_i915_private *dev_priv = dev->dev_private;
  3842. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3843. struct intel_encoder *encoder;
  3844. int pipe = intel_crtc->pipe;
  3845. int plane = intel_crtc->plane;
  3846. bool is_dsi;
  3847. u32 dspcntr;
  3848. WARN_ON(!crtc->enabled);
  3849. if (intel_crtc->active)
  3850. return;
  3851. vlv_prepare_pll(intel_crtc);
  3852. /* Set up the display plane register */
  3853. dspcntr = DISPPLANE_GAMMA_ENABLE;
  3854. if (intel_crtc->config.has_dp_encoder)
  3855. intel_dp_set_m_n(intel_crtc);
  3856. intel_set_pipe_timings(intel_crtc);
  3857. /* pipesrc and dspsize control the size that is scaled from,
  3858. * which should always be the user's requested size.
  3859. */
  3860. I915_WRITE(DSPSIZE(plane),
  3861. ((intel_crtc->config.pipe_src_h - 1) << 16) |
  3862. (intel_crtc->config.pipe_src_w - 1));
  3863. I915_WRITE(DSPPOS(plane), 0);
  3864. i9xx_set_pipeconf(intel_crtc);
  3865. I915_WRITE(DSPCNTR(plane), dspcntr);
  3866. POSTING_READ(DSPCNTR(plane));
  3867. dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
  3868. crtc->x, crtc->y);
  3869. intel_crtc->active = true;
  3870. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3871. for_each_encoder_on_crtc(dev, crtc, encoder)
  3872. if (encoder->pre_pll_enable)
  3873. encoder->pre_pll_enable(encoder);
  3874. is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
  3875. if (!is_dsi) {
  3876. if (IS_CHERRYVIEW(dev))
  3877. chv_enable_pll(intel_crtc);
  3878. else
  3879. vlv_enable_pll(intel_crtc);
  3880. }
  3881. for_each_encoder_on_crtc(dev, crtc, encoder)
  3882. if (encoder->pre_enable)
  3883. encoder->pre_enable(encoder);
  3884. i9xx_pfit_enable(intel_crtc);
  3885. intel_crtc_load_lut(crtc);
  3886. intel_update_watermarks(crtc);
  3887. intel_enable_pipe(intel_crtc);
  3888. for_each_encoder_on_crtc(dev, crtc, encoder)
  3889. encoder->enable(encoder);
  3890. intel_crtc_enable_planes(crtc);
  3891. drm_crtc_vblank_on(crtc);
  3892. /* Underruns don't raise interrupts, so check manually. */
  3893. i9xx_check_fifo_underruns(dev);
  3894. }
  3895. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  3896. {
  3897. struct drm_device *dev = crtc->base.dev;
  3898. struct drm_i915_private *dev_priv = dev->dev_private;
  3899. I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
  3900. I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
  3901. }
  3902. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  3903. {
  3904. struct drm_device *dev = crtc->dev;
  3905. struct drm_i915_private *dev_priv = dev->dev_private;
  3906. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3907. struct intel_encoder *encoder;
  3908. int pipe = intel_crtc->pipe;
  3909. int plane = intel_crtc->plane;
  3910. u32 dspcntr;
  3911. WARN_ON(!crtc->enabled);
  3912. if (intel_crtc->active)
  3913. return;
  3914. i9xx_set_pll_dividers(intel_crtc);
  3915. /* Set up the display plane register */
  3916. dspcntr = DISPPLANE_GAMMA_ENABLE;
  3917. if (pipe == 0)
  3918. dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
  3919. else
  3920. dspcntr |= DISPPLANE_SEL_PIPE_B;
  3921. if (intel_crtc->config.has_dp_encoder)
  3922. intel_dp_set_m_n(intel_crtc);
  3923. intel_set_pipe_timings(intel_crtc);
  3924. /* pipesrc and dspsize control the size that is scaled from,
  3925. * which should always be the user's requested size.
  3926. */
  3927. I915_WRITE(DSPSIZE(plane),
  3928. ((intel_crtc->config.pipe_src_h - 1) << 16) |
  3929. (intel_crtc->config.pipe_src_w - 1));
  3930. I915_WRITE(DSPPOS(plane), 0);
  3931. i9xx_set_pipeconf(intel_crtc);
  3932. I915_WRITE(DSPCNTR(plane), dspcntr);
  3933. POSTING_READ(DSPCNTR(plane));
  3934. dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
  3935. crtc->x, crtc->y);
  3936. intel_crtc->active = true;
  3937. if (!IS_GEN2(dev))
  3938. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3939. for_each_encoder_on_crtc(dev, crtc, encoder)
  3940. if (encoder->pre_enable)
  3941. encoder->pre_enable(encoder);
  3942. i9xx_enable_pll(intel_crtc);
  3943. i9xx_pfit_enable(intel_crtc);
  3944. intel_crtc_load_lut(crtc);
  3945. intel_update_watermarks(crtc);
  3946. intel_enable_pipe(intel_crtc);
  3947. for_each_encoder_on_crtc(dev, crtc, encoder)
  3948. encoder->enable(encoder);
  3949. intel_crtc_enable_planes(crtc);
  3950. /*
  3951. * Gen2 reports pipe underruns whenever all planes are disabled.
  3952. * So don't enable underrun reporting before at least some planes
  3953. * are enabled.
  3954. * FIXME: Need to fix the logic to work when we turn off all planes
  3955. * but leave the pipe running.
  3956. */
  3957. if (IS_GEN2(dev))
  3958. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3959. drm_crtc_vblank_on(crtc);
  3960. /* Underruns don't raise interrupts, so check manually. */
  3961. i9xx_check_fifo_underruns(dev);
  3962. }
  3963. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  3964. {
  3965. struct drm_device *dev = crtc->base.dev;
  3966. struct drm_i915_private *dev_priv = dev->dev_private;
  3967. if (!crtc->config.gmch_pfit.control)
  3968. return;
  3969. assert_pipe_disabled(dev_priv, crtc->pipe);
  3970. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  3971. I915_READ(PFIT_CONTROL));
  3972. I915_WRITE(PFIT_CONTROL, 0);
  3973. }
  3974. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  3975. {
  3976. struct drm_device *dev = crtc->dev;
  3977. struct drm_i915_private *dev_priv = dev->dev_private;
  3978. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3979. struct intel_encoder *encoder;
  3980. int pipe = intel_crtc->pipe;
  3981. if (!intel_crtc->active)
  3982. return;
  3983. /*
  3984. * Gen2 reports pipe underruns whenever all planes are disabled.
  3985. * So diasble underrun reporting before all the planes get disabled.
  3986. * FIXME: Need to fix the logic to work when we turn off all planes
  3987. * but leave the pipe running.
  3988. */
  3989. if (IS_GEN2(dev))
  3990. intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
  3991. intel_crtc_disable_planes(crtc);
  3992. for_each_encoder_on_crtc(dev, crtc, encoder)
  3993. encoder->disable(encoder);
  3994. /*
  3995. * On gen2 planes are double buffered but the pipe isn't, so we must
  3996. * wait for planes to fully turn off before disabling the pipe.
  3997. */
  3998. if (IS_GEN2(dev))
  3999. intel_wait_for_vblank(dev, pipe);
  4000. intel_disable_pipe(dev_priv, pipe);
  4001. i9xx_pfit_disable(intel_crtc);
  4002. for_each_encoder_on_crtc(dev, crtc, encoder)
  4003. if (encoder->post_disable)
  4004. encoder->post_disable(encoder);
  4005. if (!intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI)) {
  4006. if (IS_CHERRYVIEW(dev))
  4007. chv_disable_pll(dev_priv, pipe);
  4008. else if (IS_VALLEYVIEW(dev))
  4009. vlv_disable_pll(dev_priv, pipe);
  4010. else
  4011. i9xx_disable_pll(dev_priv, pipe);
  4012. }
  4013. if (!IS_GEN2(dev))
  4014. intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
  4015. intel_crtc->active = false;
  4016. intel_update_watermarks(crtc);
  4017. mutex_lock(&dev->struct_mutex);
  4018. intel_update_fbc(dev);
  4019. intel_edp_psr_update(dev);
  4020. mutex_unlock(&dev->struct_mutex);
  4021. }
  4022. static void i9xx_crtc_off(struct drm_crtc *crtc)
  4023. {
  4024. }
  4025. static void intel_crtc_update_sarea(struct drm_crtc *crtc,
  4026. bool enabled)
  4027. {
  4028. struct drm_device *dev = crtc->dev;
  4029. struct drm_i915_master_private *master_priv;
  4030. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4031. int pipe = intel_crtc->pipe;
  4032. if (!dev->primary->master)
  4033. return;
  4034. master_priv = dev->primary->master->driver_priv;
  4035. if (!master_priv->sarea_priv)
  4036. return;
  4037. switch (pipe) {
  4038. case 0:
  4039. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  4040. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  4041. break;
  4042. case 1:
  4043. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  4044. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  4045. break;
  4046. default:
  4047. DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
  4048. break;
  4049. }
  4050. }
  4051. /**
  4052. * Sets the power management mode of the pipe and plane.
  4053. */
  4054. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  4055. {
  4056. struct drm_device *dev = crtc->dev;
  4057. struct drm_i915_private *dev_priv = dev->dev_private;
  4058. struct intel_encoder *intel_encoder;
  4059. bool enable = false;
  4060. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4061. enable |= intel_encoder->connectors_active;
  4062. if (enable)
  4063. dev_priv->display.crtc_enable(crtc);
  4064. else
  4065. dev_priv->display.crtc_disable(crtc);
  4066. intel_crtc_update_sarea(crtc, enable);
  4067. }
  4068. static void intel_crtc_disable(struct drm_crtc *crtc)
  4069. {
  4070. struct drm_device *dev = crtc->dev;
  4071. struct drm_connector *connector;
  4072. struct drm_i915_private *dev_priv = dev->dev_private;
  4073. /* crtc should still be enabled when we disable it. */
  4074. WARN_ON(!crtc->enabled);
  4075. dev_priv->display.crtc_disable(crtc);
  4076. intel_crtc_update_sarea(crtc, false);
  4077. dev_priv->display.off(crtc);
  4078. assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
  4079. assert_cursor_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
  4080. assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
  4081. if (crtc->primary->fb) {
  4082. mutex_lock(&dev->struct_mutex);
  4083. intel_unpin_fb_obj(to_intel_framebuffer(crtc->primary->fb)->obj);
  4084. mutex_unlock(&dev->struct_mutex);
  4085. crtc->primary->fb = NULL;
  4086. }
  4087. /* Update computed state. */
  4088. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  4089. if (!connector->encoder || !connector->encoder->crtc)
  4090. continue;
  4091. if (connector->encoder->crtc != crtc)
  4092. continue;
  4093. connector->dpms = DRM_MODE_DPMS_OFF;
  4094. to_intel_encoder(connector->encoder)->connectors_active = false;
  4095. }
  4096. }
  4097. void intel_encoder_destroy(struct drm_encoder *encoder)
  4098. {
  4099. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4100. drm_encoder_cleanup(encoder);
  4101. kfree(intel_encoder);
  4102. }
  4103. /* Simple dpms helper for encoders with just one connector, no cloning and only
  4104. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  4105. * state of the entire output pipe. */
  4106. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  4107. {
  4108. if (mode == DRM_MODE_DPMS_ON) {
  4109. encoder->connectors_active = true;
  4110. intel_crtc_update_dpms(encoder->base.crtc);
  4111. } else {
  4112. encoder->connectors_active = false;
  4113. intel_crtc_update_dpms(encoder->base.crtc);
  4114. }
  4115. }
  4116. /* Cross check the actual hw state with our own modeset state tracking (and it's
  4117. * internal consistency). */
  4118. static void intel_connector_check_state(struct intel_connector *connector)
  4119. {
  4120. if (connector->get_hw_state(connector)) {
  4121. struct intel_encoder *encoder = connector->encoder;
  4122. struct drm_crtc *crtc;
  4123. bool encoder_enabled;
  4124. enum pipe pipe;
  4125. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4126. connector->base.base.id,
  4127. connector->base.name);
  4128. WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  4129. "wrong connector dpms state\n");
  4130. WARN(connector->base.encoder != &encoder->base,
  4131. "active connector not linked to encoder\n");
  4132. WARN(!encoder->connectors_active,
  4133. "encoder->connectors_active not set\n");
  4134. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  4135. WARN(!encoder_enabled, "encoder not enabled\n");
  4136. if (WARN_ON(!encoder->base.crtc))
  4137. return;
  4138. crtc = encoder->base.crtc;
  4139. WARN(!crtc->enabled, "crtc not enabled\n");
  4140. WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  4141. WARN(pipe != to_intel_crtc(crtc)->pipe,
  4142. "encoder active on the wrong pipe\n");
  4143. }
  4144. }
  4145. /* Even simpler default implementation, if there's really no special case to
  4146. * consider. */
  4147. void intel_connector_dpms(struct drm_connector *connector, int mode)
  4148. {
  4149. /* All the simple cases only support two dpms states. */
  4150. if (mode != DRM_MODE_DPMS_ON)
  4151. mode = DRM_MODE_DPMS_OFF;
  4152. if (mode == connector->dpms)
  4153. return;
  4154. connector->dpms = mode;
  4155. /* Only need to change hw state when actually enabled */
  4156. if (connector->encoder)
  4157. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  4158. intel_modeset_check_state(connector->dev);
  4159. }
  4160. /* Simple connector->get_hw_state implementation for encoders that support only
  4161. * one connector and no cloning and hence the encoder state determines the state
  4162. * of the connector. */
  4163. bool intel_connector_get_hw_state(struct intel_connector *connector)
  4164. {
  4165. enum pipe pipe = 0;
  4166. struct intel_encoder *encoder = connector->encoder;
  4167. return encoder->get_hw_state(encoder, &pipe);
  4168. }
  4169. static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  4170. struct intel_crtc_config *pipe_config)
  4171. {
  4172. struct drm_i915_private *dev_priv = dev->dev_private;
  4173. struct intel_crtc *pipe_B_crtc =
  4174. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  4175. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  4176. pipe_name(pipe), pipe_config->fdi_lanes);
  4177. if (pipe_config->fdi_lanes > 4) {
  4178. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  4179. pipe_name(pipe), pipe_config->fdi_lanes);
  4180. return false;
  4181. }
  4182. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  4183. if (pipe_config->fdi_lanes > 2) {
  4184. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  4185. pipe_config->fdi_lanes);
  4186. return false;
  4187. } else {
  4188. return true;
  4189. }
  4190. }
  4191. if (INTEL_INFO(dev)->num_pipes == 2)
  4192. return true;
  4193. /* Ivybridge 3 pipe is really complicated */
  4194. switch (pipe) {
  4195. case PIPE_A:
  4196. return true;
  4197. case PIPE_B:
  4198. if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
  4199. pipe_config->fdi_lanes > 2) {
  4200. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4201. pipe_name(pipe), pipe_config->fdi_lanes);
  4202. return false;
  4203. }
  4204. return true;
  4205. case PIPE_C:
  4206. if (!pipe_has_enabled_pch(pipe_B_crtc) ||
  4207. pipe_B_crtc->config.fdi_lanes <= 2) {
  4208. if (pipe_config->fdi_lanes > 2) {
  4209. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4210. pipe_name(pipe), pipe_config->fdi_lanes);
  4211. return false;
  4212. }
  4213. } else {
  4214. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  4215. return false;
  4216. }
  4217. return true;
  4218. default:
  4219. BUG();
  4220. }
  4221. }
  4222. #define RETRY 1
  4223. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  4224. struct intel_crtc_config *pipe_config)
  4225. {
  4226. struct drm_device *dev = intel_crtc->base.dev;
  4227. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4228. int lane, link_bw, fdi_dotclock;
  4229. bool setup_ok, needs_recompute = false;
  4230. retry:
  4231. /* FDI is a binary signal running at ~2.7GHz, encoding
  4232. * each output octet as 10 bits. The actual frequency
  4233. * is stored as a divider into a 100MHz clock, and the
  4234. * mode pixel clock is stored in units of 1KHz.
  4235. * Hence the bw of each lane in terms of the mode signal
  4236. * is:
  4237. */
  4238. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4239. fdi_dotclock = adjusted_mode->crtc_clock;
  4240. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  4241. pipe_config->pipe_bpp);
  4242. pipe_config->fdi_lanes = lane;
  4243. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  4244. link_bw, &pipe_config->fdi_m_n);
  4245. setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  4246. intel_crtc->pipe, pipe_config);
  4247. if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
  4248. pipe_config->pipe_bpp -= 2*3;
  4249. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  4250. pipe_config->pipe_bpp);
  4251. needs_recompute = true;
  4252. pipe_config->bw_constrained = true;
  4253. goto retry;
  4254. }
  4255. if (needs_recompute)
  4256. return RETRY;
  4257. return setup_ok ? 0 : -EINVAL;
  4258. }
  4259. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  4260. struct intel_crtc_config *pipe_config)
  4261. {
  4262. pipe_config->ips_enabled = i915.enable_ips &&
  4263. hsw_crtc_supports_ips(crtc) &&
  4264. pipe_config->pipe_bpp <= 24;
  4265. }
  4266. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  4267. struct intel_crtc_config *pipe_config)
  4268. {
  4269. struct drm_device *dev = crtc->base.dev;
  4270. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4271. /* FIXME should check pixel clock limits on all platforms */
  4272. if (INTEL_INFO(dev)->gen < 4) {
  4273. struct drm_i915_private *dev_priv = dev->dev_private;
  4274. int clock_limit =
  4275. dev_priv->display.get_display_clock_speed(dev);
  4276. /*
  4277. * Enable pixel doubling when the dot clock
  4278. * is > 90% of the (display) core speed.
  4279. *
  4280. * GDG double wide on either pipe,
  4281. * otherwise pipe A only.
  4282. */
  4283. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  4284. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  4285. clock_limit *= 2;
  4286. pipe_config->double_wide = true;
  4287. }
  4288. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  4289. return -EINVAL;
  4290. }
  4291. /*
  4292. * Pipe horizontal size must be even in:
  4293. * - DVO ganged mode
  4294. * - LVDS dual channel mode
  4295. * - Double wide pipe
  4296. */
  4297. if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4298. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  4299. pipe_config->pipe_src_w &= ~1;
  4300. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  4301. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  4302. */
  4303. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  4304. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  4305. return -EINVAL;
  4306. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
  4307. pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
  4308. } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
  4309. /* only a 8bpc pipe, with 6bpc dither through the panel fitter
  4310. * for lvds. */
  4311. pipe_config->pipe_bpp = 8*3;
  4312. }
  4313. if (HAS_IPS(dev))
  4314. hsw_compute_ips_config(crtc, pipe_config);
  4315. /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
  4316. * clock survives for now. */
  4317. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  4318. pipe_config->shared_dpll = crtc->config.shared_dpll;
  4319. if (pipe_config->has_pch_encoder)
  4320. return ironlake_fdi_compute_config(crtc, pipe_config);
  4321. return 0;
  4322. }
  4323. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  4324. {
  4325. return 400000; /* FIXME */
  4326. }
  4327. static int i945_get_display_clock_speed(struct drm_device *dev)
  4328. {
  4329. return 400000;
  4330. }
  4331. static int i915_get_display_clock_speed(struct drm_device *dev)
  4332. {
  4333. return 333000;
  4334. }
  4335. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  4336. {
  4337. return 200000;
  4338. }
  4339. static int pnv_get_display_clock_speed(struct drm_device *dev)
  4340. {
  4341. u16 gcfgc = 0;
  4342. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4343. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4344. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  4345. return 267000;
  4346. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  4347. return 333000;
  4348. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  4349. return 444000;
  4350. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  4351. return 200000;
  4352. default:
  4353. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  4354. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  4355. return 133000;
  4356. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  4357. return 167000;
  4358. }
  4359. }
  4360. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  4361. {
  4362. u16 gcfgc = 0;
  4363. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4364. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  4365. return 133000;
  4366. else {
  4367. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4368. case GC_DISPLAY_CLOCK_333_MHZ:
  4369. return 333000;
  4370. default:
  4371. case GC_DISPLAY_CLOCK_190_200_MHZ:
  4372. return 190000;
  4373. }
  4374. }
  4375. }
  4376. static int i865_get_display_clock_speed(struct drm_device *dev)
  4377. {
  4378. return 266000;
  4379. }
  4380. static int i855_get_display_clock_speed(struct drm_device *dev)
  4381. {
  4382. u16 hpllcc = 0;
  4383. /* Assume that the hardware is in the high speed state. This
  4384. * should be the default.
  4385. */
  4386. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  4387. case GC_CLOCK_133_200:
  4388. case GC_CLOCK_100_200:
  4389. return 200000;
  4390. case GC_CLOCK_166_250:
  4391. return 250000;
  4392. case GC_CLOCK_100_133:
  4393. return 133000;
  4394. }
  4395. /* Shouldn't happen */
  4396. return 0;
  4397. }
  4398. static int i830_get_display_clock_speed(struct drm_device *dev)
  4399. {
  4400. return 133000;
  4401. }
  4402. static void
  4403. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  4404. {
  4405. while (*num > DATA_LINK_M_N_MASK ||
  4406. *den > DATA_LINK_M_N_MASK) {
  4407. *num >>= 1;
  4408. *den >>= 1;
  4409. }
  4410. }
  4411. static void compute_m_n(unsigned int m, unsigned int n,
  4412. uint32_t *ret_m, uint32_t *ret_n)
  4413. {
  4414. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  4415. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  4416. intel_reduce_m_n_ratio(ret_m, ret_n);
  4417. }
  4418. void
  4419. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  4420. int pixel_clock, int link_clock,
  4421. struct intel_link_m_n *m_n)
  4422. {
  4423. m_n->tu = 64;
  4424. compute_m_n(bits_per_pixel * pixel_clock,
  4425. link_clock * nlanes * 8,
  4426. &m_n->gmch_m, &m_n->gmch_n);
  4427. compute_m_n(pixel_clock, link_clock,
  4428. &m_n->link_m, &m_n->link_n);
  4429. }
  4430. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  4431. {
  4432. if (i915.panel_use_ssc >= 0)
  4433. return i915.panel_use_ssc != 0;
  4434. return dev_priv->vbt.lvds_use_ssc
  4435. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  4436. }
  4437. static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
  4438. {
  4439. struct drm_device *dev = crtc->dev;
  4440. struct drm_i915_private *dev_priv = dev->dev_private;
  4441. int refclk;
  4442. if (IS_VALLEYVIEW(dev)) {
  4443. refclk = 100000;
  4444. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  4445. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4446. refclk = dev_priv->vbt.lvds_ssc_freq;
  4447. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  4448. } else if (!IS_GEN2(dev)) {
  4449. refclk = 96000;
  4450. } else {
  4451. refclk = 48000;
  4452. }
  4453. return refclk;
  4454. }
  4455. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  4456. {
  4457. return (1 << dpll->n) << 16 | dpll->m2;
  4458. }
  4459. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  4460. {
  4461. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  4462. }
  4463. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  4464. intel_clock_t *reduced_clock)
  4465. {
  4466. struct drm_device *dev = crtc->base.dev;
  4467. u32 fp, fp2 = 0;
  4468. if (IS_PINEVIEW(dev)) {
  4469. fp = pnv_dpll_compute_fp(&crtc->config.dpll);
  4470. if (reduced_clock)
  4471. fp2 = pnv_dpll_compute_fp(reduced_clock);
  4472. } else {
  4473. fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
  4474. if (reduced_clock)
  4475. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  4476. }
  4477. crtc->config.dpll_hw_state.fp0 = fp;
  4478. crtc->lowfreq_avail = false;
  4479. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4480. reduced_clock && i915.powersave) {
  4481. crtc->config.dpll_hw_state.fp1 = fp2;
  4482. crtc->lowfreq_avail = true;
  4483. } else {
  4484. crtc->config.dpll_hw_state.fp1 = fp;
  4485. }
  4486. }
  4487. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  4488. pipe)
  4489. {
  4490. u32 reg_val;
  4491. /*
  4492. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  4493. * and set it to a reasonable value instead.
  4494. */
  4495. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4496. reg_val &= 0xffffff00;
  4497. reg_val |= 0x00000030;
  4498. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4499. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4500. reg_val &= 0x8cffffff;
  4501. reg_val = 0x8c000000;
  4502. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4503. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4504. reg_val &= 0xffffff00;
  4505. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4506. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4507. reg_val &= 0x00ffffff;
  4508. reg_val |= 0xb0000000;
  4509. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4510. }
  4511. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  4512. struct intel_link_m_n *m_n)
  4513. {
  4514. struct drm_device *dev = crtc->base.dev;
  4515. struct drm_i915_private *dev_priv = dev->dev_private;
  4516. int pipe = crtc->pipe;
  4517. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4518. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  4519. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  4520. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  4521. }
  4522. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  4523. struct intel_link_m_n *m_n)
  4524. {
  4525. struct drm_device *dev = crtc->base.dev;
  4526. struct drm_i915_private *dev_priv = dev->dev_private;
  4527. int pipe = crtc->pipe;
  4528. enum transcoder transcoder = crtc->config.cpu_transcoder;
  4529. if (INTEL_INFO(dev)->gen >= 5) {
  4530. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4531. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  4532. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  4533. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  4534. } else {
  4535. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4536. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  4537. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  4538. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  4539. }
  4540. }
  4541. static void intel_dp_set_m_n(struct intel_crtc *crtc)
  4542. {
  4543. if (crtc->config.has_pch_encoder)
  4544. intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
  4545. else
  4546. intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
  4547. }
  4548. static void vlv_update_pll(struct intel_crtc *crtc)
  4549. {
  4550. u32 dpll, dpll_md;
  4551. /*
  4552. * Enable DPIO clock input. We should never disable the reference
  4553. * clock for pipe B, since VGA hotplug / manual detection depends
  4554. * on it.
  4555. */
  4556. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  4557. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  4558. /* We should never disable this, set it here for state tracking */
  4559. if (crtc->pipe == PIPE_B)
  4560. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4561. dpll |= DPLL_VCO_ENABLE;
  4562. crtc->config.dpll_hw_state.dpll = dpll;
  4563. dpll_md = (crtc->config.pixel_multiplier - 1)
  4564. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4565. crtc->config.dpll_hw_state.dpll_md = dpll_md;
  4566. }
  4567. static void vlv_prepare_pll(struct intel_crtc *crtc)
  4568. {
  4569. struct drm_device *dev = crtc->base.dev;
  4570. struct drm_i915_private *dev_priv = dev->dev_private;
  4571. int pipe = crtc->pipe;
  4572. u32 mdiv;
  4573. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  4574. u32 coreclk, reg_val;
  4575. mutex_lock(&dev_priv->dpio_lock);
  4576. bestn = crtc->config.dpll.n;
  4577. bestm1 = crtc->config.dpll.m1;
  4578. bestm2 = crtc->config.dpll.m2;
  4579. bestp1 = crtc->config.dpll.p1;
  4580. bestp2 = crtc->config.dpll.p2;
  4581. /* See eDP HDMI DPIO driver vbios notes doc */
  4582. /* PLL B needs special handling */
  4583. if (pipe == PIPE_B)
  4584. vlv_pllb_recal_opamp(dev_priv, pipe);
  4585. /* Set up Tx target for periodic Rcomp update */
  4586. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  4587. /* Disable target IRef on PLL */
  4588. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  4589. reg_val &= 0x00ffffff;
  4590. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  4591. /* Disable fast lock */
  4592. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  4593. /* Set idtafcrecal before PLL is enabled */
  4594. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  4595. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  4596. mdiv |= ((bestn << DPIO_N_SHIFT));
  4597. mdiv |= (1 << DPIO_K_SHIFT);
  4598. /*
  4599. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  4600. * but we don't support that).
  4601. * Note: don't use the DAC post divider as it seems unstable.
  4602. */
  4603. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  4604. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4605. mdiv |= DPIO_ENABLE_CALIBRATION;
  4606. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4607. /* Set HBR and RBR LPF coefficients */
  4608. if (crtc->config.port_clock == 162000 ||
  4609. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
  4610. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
  4611. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4612. 0x009f0003);
  4613. else
  4614. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4615. 0x00d0000f);
  4616. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
  4617. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
  4618. /* Use SSC source */
  4619. if (pipe == PIPE_A)
  4620. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4621. 0x0df40000);
  4622. else
  4623. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4624. 0x0df70000);
  4625. } else { /* HDMI or VGA */
  4626. /* Use bend source */
  4627. if (pipe == PIPE_A)
  4628. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4629. 0x0df70000);
  4630. else
  4631. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4632. 0x0df40000);
  4633. }
  4634. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  4635. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  4636. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
  4637. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
  4638. coreclk |= 0x01000000;
  4639. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  4640. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  4641. mutex_unlock(&dev_priv->dpio_lock);
  4642. }
  4643. static void chv_update_pll(struct intel_crtc *crtc)
  4644. {
  4645. struct drm_device *dev = crtc->base.dev;
  4646. struct drm_i915_private *dev_priv = dev->dev_private;
  4647. int pipe = crtc->pipe;
  4648. int dpll_reg = DPLL(crtc->pipe);
  4649. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  4650. u32 loopfilter, intcoeff;
  4651. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  4652. int refclk;
  4653. crtc->config.dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  4654. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  4655. DPLL_VCO_ENABLE;
  4656. if (pipe != PIPE_A)
  4657. crtc->config.dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4658. crtc->config.dpll_hw_state.dpll_md =
  4659. (crtc->config.pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4660. bestn = crtc->config.dpll.n;
  4661. bestm2_frac = crtc->config.dpll.m2 & 0x3fffff;
  4662. bestm1 = crtc->config.dpll.m1;
  4663. bestm2 = crtc->config.dpll.m2 >> 22;
  4664. bestp1 = crtc->config.dpll.p1;
  4665. bestp2 = crtc->config.dpll.p2;
  4666. /*
  4667. * Enable Refclk and SSC
  4668. */
  4669. I915_WRITE(dpll_reg,
  4670. crtc->config.dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  4671. mutex_lock(&dev_priv->dpio_lock);
  4672. /* p1 and p2 divider */
  4673. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  4674. 5 << DPIO_CHV_S1_DIV_SHIFT |
  4675. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  4676. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  4677. 1 << DPIO_CHV_K_DIV_SHIFT);
  4678. /* Feedback post-divider - m2 */
  4679. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  4680. /* Feedback refclk divider - n and m1 */
  4681. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  4682. DPIO_CHV_M1_DIV_BY_2 |
  4683. 1 << DPIO_CHV_N_DIV_SHIFT);
  4684. /* M2 fraction division */
  4685. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  4686. /* M2 fraction division enable */
  4687. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
  4688. DPIO_CHV_FRAC_DIV_EN |
  4689. (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
  4690. /* Loop filter */
  4691. refclk = i9xx_get_refclk(&crtc->base, 0);
  4692. loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
  4693. 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
  4694. if (refclk == 100000)
  4695. intcoeff = 11;
  4696. else if (refclk == 38400)
  4697. intcoeff = 10;
  4698. else
  4699. intcoeff = 9;
  4700. loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
  4701. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  4702. /* AFC Recal */
  4703. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  4704. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  4705. DPIO_AFC_RECAL);
  4706. mutex_unlock(&dev_priv->dpio_lock);
  4707. }
  4708. static void i9xx_update_pll(struct intel_crtc *crtc,
  4709. intel_clock_t *reduced_clock,
  4710. int num_connectors)
  4711. {
  4712. struct drm_device *dev = crtc->base.dev;
  4713. struct drm_i915_private *dev_priv = dev->dev_private;
  4714. u32 dpll;
  4715. bool is_sdvo;
  4716. struct dpll *clock = &crtc->config.dpll;
  4717. i9xx_update_pll_dividers(crtc, reduced_clock);
  4718. is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
  4719. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
  4720. dpll = DPLL_VGA_MODE_DIS;
  4721. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
  4722. dpll |= DPLLB_MODE_LVDS;
  4723. else
  4724. dpll |= DPLLB_MODE_DAC_SERIAL;
  4725. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  4726. dpll |= (crtc->config.pixel_multiplier - 1)
  4727. << SDVO_MULTIPLIER_SHIFT_HIRES;
  4728. }
  4729. if (is_sdvo)
  4730. dpll |= DPLL_SDVO_HIGH_SPEED;
  4731. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
  4732. dpll |= DPLL_SDVO_HIGH_SPEED;
  4733. /* compute bitmask from p1 value */
  4734. if (IS_PINEVIEW(dev))
  4735. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  4736. else {
  4737. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4738. if (IS_G4X(dev) && reduced_clock)
  4739. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  4740. }
  4741. switch (clock->p2) {
  4742. case 5:
  4743. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  4744. break;
  4745. case 7:
  4746. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  4747. break;
  4748. case 10:
  4749. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  4750. break;
  4751. case 14:
  4752. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  4753. break;
  4754. }
  4755. if (INTEL_INFO(dev)->gen >= 4)
  4756. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  4757. if (crtc->config.sdvo_tv_clock)
  4758. dpll |= PLL_REF_INPUT_TVCLKINBC;
  4759. else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4760. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4761. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4762. else
  4763. dpll |= PLL_REF_INPUT_DREFCLK;
  4764. dpll |= DPLL_VCO_ENABLE;
  4765. crtc->config.dpll_hw_state.dpll = dpll;
  4766. if (INTEL_INFO(dev)->gen >= 4) {
  4767. u32 dpll_md = (crtc->config.pixel_multiplier - 1)
  4768. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4769. crtc->config.dpll_hw_state.dpll_md = dpll_md;
  4770. }
  4771. }
  4772. static void i8xx_update_pll(struct intel_crtc *crtc,
  4773. intel_clock_t *reduced_clock,
  4774. int num_connectors)
  4775. {
  4776. struct drm_device *dev = crtc->base.dev;
  4777. struct drm_i915_private *dev_priv = dev->dev_private;
  4778. u32 dpll;
  4779. struct dpll *clock = &crtc->config.dpll;
  4780. i9xx_update_pll_dividers(crtc, reduced_clock);
  4781. dpll = DPLL_VGA_MODE_DIS;
  4782. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
  4783. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4784. } else {
  4785. if (clock->p1 == 2)
  4786. dpll |= PLL_P1_DIVIDE_BY_TWO;
  4787. else
  4788. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4789. if (clock->p2 == 4)
  4790. dpll |= PLL_P2_DIVIDE_BY_4;
  4791. }
  4792. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
  4793. dpll |= DPLL_DVO_2X_MODE;
  4794. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4795. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4796. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4797. else
  4798. dpll |= PLL_REF_INPUT_DREFCLK;
  4799. dpll |= DPLL_VCO_ENABLE;
  4800. crtc->config.dpll_hw_state.dpll = dpll;
  4801. }
  4802. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  4803. {
  4804. struct drm_device *dev = intel_crtc->base.dev;
  4805. struct drm_i915_private *dev_priv = dev->dev_private;
  4806. enum pipe pipe = intel_crtc->pipe;
  4807. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  4808. struct drm_display_mode *adjusted_mode =
  4809. &intel_crtc->config.adjusted_mode;
  4810. uint32_t crtc_vtotal, crtc_vblank_end;
  4811. int vsyncshift = 0;
  4812. /* We need to be careful not to changed the adjusted mode, for otherwise
  4813. * the hw state checker will get angry at the mismatch. */
  4814. crtc_vtotal = adjusted_mode->crtc_vtotal;
  4815. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  4816. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4817. /* the chip adds 2 halflines automatically */
  4818. crtc_vtotal -= 1;
  4819. crtc_vblank_end -= 1;
  4820. if (intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
  4821. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  4822. else
  4823. vsyncshift = adjusted_mode->crtc_hsync_start -
  4824. adjusted_mode->crtc_htotal / 2;
  4825. if (vsyncshift < 0)
  4826. vsyncshift += adjusted_mode->crtc_htotal;
  4827. }
  4828. if (INTEL_INFO(dev)->gen > 3)
  4829. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  4830. I915_WRITE(HTOTAL(cpu_transcoder),
  4831. (adjusted_mode->crtc_hdisplay - 1) |
  4832. ((adjusted_mode->crtc_htotal - 1) << 16));
  4833. I915_WRITE(HBLANK(cpu_transcoder),
  4834. (adjusted_mode->crtc_hblank_start - 1) |
  4835. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  4836. I915_WRITE(HSYNC(cpu_transcoder),
  4837. (adjusted_mode->crtc_hsync_start - 1) |
  4838. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  4839. I915_WRITE(VTOTAL(cpu_transcoder),
  4840. (adjusted_mode->crtc_vdisplay - 1) |
  4841. ((crtc_vtotal - 1) << 16));
  4842. I915_WRITE(VBLANK(cpu_transcoder),
  4843. (adjusted_mode->crtc_vblank_start - 1) |
  4844. ((crtc_vblank_end - 1) << 16));
  4845. I915_WRITE(VSYNC(cpu_transcoder),
  4846. (adjusted_mode->crtc_vsync_start - 1) |
  4847. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  4848. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  4849. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  4850. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  4851. * bits. */
  4852. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  4853. (pipe == PIPE_B || pipe == PIPE_C))
  4854. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  4855. /* pipesrc controls the size that is scaled from, which should
  4856. * always be the user's requested size.
  4857. */
  4858. I915_WRITE(PIPESRC(pipe),
  4859. ((intel_crtc->config.pipe_src_w - 1) << 16) |
  4860. (intel_crtc->config.pipe_src_h - 1));
  4861. }
  4862. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  4863. struct intel_crtc_config *pipe_config)
  4864. {
  4865. struct drm_device *dev = crtc->base.dev;
  4866. struct drm_i915_private *dev_priv = dev->dev_private;
  4867. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  4868. uint32_t tmp;
  4869. tmp = I915_READ(HTOTAL(cpu_transcoder));
  4870. pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  4871. pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  4872. tmp = I915_READ(HBLANK(cpu_transcoder));
  4873. pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  4874. pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  4875. tmp = I915_READ(HSYNC(cpu_transcoder));
  4876. pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  4877. pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  4878. tmp = I915_READ(VTOTAL(cpu_transcoder));
  4879. pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  4880. pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  4881. tmp = I915_READ(VBLANK(cpu_transcoder));
  4882. pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  4883. pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  4884. tmp = I915_READ(VSYNC(cpu_transcoder));
  4885. pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  4886. pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  4887. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  4888. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  4889. pipe_config->adjusted_mode.crtc_vtotal += 1;
  4890. pipe_config->adjusted_mode.crtc_vblank_end += 1;
  4891. }
  4892. tmp = I915_READ(PIPESRC(crtc->pipe));
  4893. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  4894. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  4895. pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
  4896. pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
  4897. }
  4898. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  4899. struct intel_crtc_config *pipe_config)
  4900. {
  4901. mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
  4902. mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
  4903. mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
  4904. mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
  4905. mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
  4906. mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
  4907. mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
  4908. mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
  4909. mode->flags = pipe_config->adjusted_mode.flags;
  4910. mode->clock = pipe_config->adjusted_mode.crtc_clock;
  4911. mode->flags |= pipe_config->adjusted_mode.flags;
  4912. }
  4913. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  4914. {
  4915. struct drm_device *dev = intel_crtc->base.dev;
  4916. struct drm_i915_private *dev_priv = dev->dev_private;
  4917. uint32_t pipeconf;
  4918. pipeconf = 0;
  4919. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  4920. I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
  4921. pipeconf |= PIPECONF_ENABLE;
  4922. if (intel_crtc->config.double_wide)
  4923. pipeconf |= PIPECONF_DOUBLE_WIDE;
  4924. /* only g4x and later have fancy bpc/dither controls */
  4925. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  4926. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  4927. if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
  4928. pipeconf |= PIPECONF_DITHER_EN |
  4929. PIPECONF_DITHER_TYPE_SP;
  4930. switch (intel_crtc->config.pipe_bpp) {
  4931. case 18:
  4932. pipeconf |= PIPECONF_6BPC;
  4933. break;
  4934. case 24:
  4935. pipeconf |= PIPECONF_8BPC;
  4936. break;
  4937. case 30:
  4938. pipeconf |= PIPECONF_10BPC;
  4939. break;
  4940. default:
  4941. /* Case prevented by intel_choose_pipe_bpp_dither. */
  4942. BUG();
  4943. }
  4944. }
  4945. if (HAS_PIPE_CXSR(dev)) {
  4946. if (intel_crtc->lowfreq_avail) {
  4947. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  4948. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  4949. } else {
  4950. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  4951. }
  4952. }
  4953. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  4954. if (INTEL_INFO(dev)->gen < 4 ||
  4955. intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
  4956. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  4957. else
  4958. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  4959. } else
  4960. pipeconf |= PIPECONF_PROGRESSIVE;
  4961. if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
  4962. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  4963. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  4964. POSTING_READ(PIPECONF(intel_crtc->pipe));
  4965. }
  4966. static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
  4967. int x, int y,
  4968. struct drm_framebuffer *fb)
  4969. {
  4970. struct drm_device *dev = crtc->dev;
  4971. struct drm_i915_private *dev_priv = dev->dev_private;
  4972. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4973. int refclk, num_connectors = 0;
  4974. intel_clock_t clock, reduced_clock;
  4975. bool ok, has_reduced_clock = false;
  4976. bool is_lvds = false, is_dsi = false;
  4977. struct intel_encoder *encoder;
  4978. const intel_limit_t *limit;
  4979. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4980. switch (encoder->type) {
  4981. case INTEL_OUTPUT_LVDS:
  4982. is_lvds = true;
  4983. break;
  4984. case INTEL_OUTPUT_DSI:
  4985. is_dsi = true;
  4986. break;
  4987. }
  4988. num_connectors++;
  4989. }
  4990. if (is_dsi)
  4991. return 0;
  4992. if (!intel_crtc->config.clock_set) {
  4993. refclk = i9xx_get_refclk(crtc, num_connectors);
  4994. /*
  4995. * Returns a set of divisors for the desired target clock with
  4996. * the given refclk, or FALSE. The returned values represent
  4997. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  4998. * 2) / p1 / p2.
  4999. */
  5000. limit = intel_limit(crtc, refclk);
  5001. ok = dev_priv->display.find_dpll(limit, crtc,
  5002. intel_crtc->config.port_clock,
  5003. refclk, NULL, &clock);
  5004. if (!ok) {
  5005. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5006. return -EINVAL;
  5007. }
  5008. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5009. /*
  5010. * Ensure we match the reduced clock's P to the target
  5011. * clock. If the clocks don't match, we can't switch
  5012. * the display clock by using the FP0/FP1. In such case
  5013. * we will disable the LVDS downclock feature.
  5014. */
  5015. has_reduced_clock =
  5016. dev_priv->display.find_dpll(limit, crtc,
  5017. dev_priv->lvds_downclock,
  5018. refclk, &clock,
  5019. &reduced_clock);
  5020. }
  5021. /* Compat-code for transition, will disappear. */
  5022. intel_crtc->config.dpll.n = clock.n;
  5023. intel_crtc->config.dpll.m1 = clock.m1;
  5024. intel_crtc->config.dpll.m2 = clock.m2;
  5025. intel_crtc->config.dpll.p1 = clock.p1;
  5026. intel_crtc->config.dpll.p2 = clock.p2;
  5027. }
  5028. if (IS_GEN2(dev)) {
  5029. i8xx_update_pll(intel_crtc,
  5030. has_reduced_clock ? &reduced_clock : NULL,
  5031. num_connectors);
  5032. } else if (IS_CHERRYVIEW(dev)) {
  5033. chv_update_pll(intel_crtc);
  5034. } else if (IS_VALLEYVIEW(dev)) {
  5035. vlv_update_pll(intel_crtc);
  5036. } else {
  5037. i9xx_update_pll(intel_crtc,
  5038. has_reduced_clock ? &reduced_clock : NULL,
  5039. num_connectors);
  5040. }
  5041. return 0;
  5042. }
  5043. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  5044. struct intel_crtc_config *pipe_config)
  5045. {
  5046. struct drm_device *dev = crtc->base.dev;
  5047. struct drm_i915_private *dev_priv = dev->dev_private;
  5048. uint32_t tmp;
  5049. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  5050. return;
  5051. tmp = I915_READ(PFIT_CONTROL);
  5052. if (!(tmp & PFIT_ENABLE))
  5053. return;
  5054. /* Check whether the pfit is attached to our pipe. */
  5055. if (INTEL_INFO(dev)->gen < 4) {
  5056. if (crtc->pipe != PIPE_B)
  5057. return;
  5058. } else {
  5059. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  5060. return;
  5061. }
  5062. pipe_config->gmch_pfit.control = tmp;
  5063. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  5064. if (INTEL_INFO(dev)->gen < 5)
  5065. pipe_config->gmch_pfit.lvds_border_bits =
  5066. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  5067. }
  5068. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  5069. struct intel_crtc_config *pipe_config)
  5070. {
  5071. struct drm_device *dev = crtc->base.dev;
  5072. struct drm_i915_private *dev_priv = dev->dev_private;
  5073. int pipe = pipe_config->cpu_transcoder;
  5074. intel_clock_t clock;
  5075. u32 mdiv;
  5076. int refclk = 100000;
  5077. mutex_lock(&dev_priv->dpio_lock);
  5078. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  5079. mutex_unlock(&dev_priv->dpio_lock);
  5080. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  5081. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  5082. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  5083. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  5084. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  5085. vlv_clock(refclk, &clock);
  5086. /* clock.dot is the fast clock */
  5087. pipe_config->port_clock = clock.dot / 5;
  5088. }
  5089. static void i9xx_get_plane_config(struct intel_crtc *crtc,
  5090. struct intel_plane_config *plane_config)
  5091. {
  5092. struct drm_device *dev = crtc->base.dev;
  5093. struct drm_i915_private *dev_priv = dev->dev_private;
  5094. u32 val, base, offset;
  5095. int pipe = crtc->pipe, plane = crtc->plane;
  5096. int fourcc, pixel_format;
  5097. int aligned_height;
  5098. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  5099. if (!crtc->base.primary->fb) {
  5100. DRM_DEBUG_KMS("failed to alloc fb\n");
  5101. return;
  5102. }
  5103. val = I915_READ(DSPCNTR(plane));
  5104. if (INTEL_INFO(dev)->gen >= 4)
  5105. if (val & DISPPLANE_TILED)
  5106. plane_config->tiled = true;
  5107. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  5108. fourcc = intel_format_to_fourcc(pixel_format);
  5109. crtc->base.primary->fb->pixel_format = fourcc;
  5110. crtc->base.primary->fb->bits_per_pixel =
  5111. drm_format_plane_cpp(fourcc, 0) * 8;
  5112. if (INTEL_INFO(dev)->gen >= 4) {
  5113. if (plane_config->tiled)
  5114. offset = I915_READ(DSPTILEOFF(plane));
  5115. else
  5116. offset = I915_READ(DSPLINOFF(plane));
  5117. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  5118. } else {
  5119. base = I915_READ(DSPADDR(plane));
  5120. }
  5121. plane_config->base = base;
  5122. val = I915_READ(PIPESRC(pipe));
  5123. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  5124. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  5125. val = I915_READ(DSPSTRIDE(pipe));
  5126. crtc->base.primary->fb->pitches[0] = val & 0xffffff80;
  5127. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  5128. plane_config->tiled);
  5129. plane_config->size = ALIGN(crtc->base.primary->fb->pitches[0] *
  5130. aligned_height, PAGE_SIZE);
  5131. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  5132. pipe, plane, crtc->base.primary->fb->width,
  5133. crtc->base.primary->fb->height,
  5134. crtc->base.primary->fb->bits_per_pixel, base,
  5135. crtc->base.primary->fb->pitches[0],
  5136. plane_config->size);
  5137. }
  5138. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  5139. struct intel_crtc_config *pipe_config)
  5140. {
  5141. struct drm_device *dev = crtc->base.dev;
  5142. struct drm_i915_private *dev_priv = dev->dev_private;
  5143. int pipe = pipe_config->cpu_transcoder;
  5144. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5145. intel_clock_t clock;
  5146. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  5147. int refclk = 100000;
  5148. mutex_lock(&dev_priv->dpio_lock);
  5149. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  5150. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  5151. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  5152. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  5153. mutex_unlock(&dev_priv->dpio_lock);
  5154. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  5155. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  5156. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  5157. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  5158. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  5159. chv_clock(refclk, &clock);
  5160. /* clock.dot is the fast clock */
  5161. pipe_config->port_clock = clock.dot / 5;
  5162. }
  5163. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  5164. struct intel_crtc_config *pipe_config)
  5165. {
  5166. struct drm_device *dev = crtc->base.dev;
  5167. struct drm_i915_private *dev_priv = dev->dev_private;
  5168. uint32_t tmp;
  5169. if (!intel_display_power_enabled(dev_priv,
  5170. POWER_DOMAIN_PIPE(crtc->pipe)))
  5171. return false;
  5172. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5173. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5174. tmp = I915_READ(PIPECONF(crtc->pipe));
  5175. if (!(tmp & PIPECONF_ENABLE))
  5176. return false;
  5177. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5178. switch (tmp & PIPECONF_BPC_MASK) {
  5179. case PIPECONF_6BPC:
  5180. pipe_config->pipe_bpp = 18;
  5181. break;
  5182. case PIPECONF_8BPC:
  5183. pipe_config->pipe_bpp = 24;
  5184. break;
  5185. case PIPECONF_10BPC:
  5186. pipe_config->pipe_bpp = 30;
  5187. break;
  5188. default:
  5189. break;
  5190. }
  5191. }
  5192. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  5193. pipe_config->limited_color_range = true;
  5194. if (INTEL_INFO(dev)->gen < 4)
  5195. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  5196. intel_get_pipe_timings(crtc, pipe_config);
  5197. i9xx_get_pfit_config(crtc, pipe_config);
  5198. if (INTEL_INFO(dev)->gen >= 4) {
  5199. tmp = I915_READ(DPLL_MD(crtc->pipe));
  5200. pipe_config->pixel_multiplier =
  5201. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  5202. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  5203. pipe_config->dpll_hw_state.dpll_md = tmp;
  5204. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5205. tmp = I915_READ(DPLL(crtc->pipe));
  5206. pipe_config->pixel_multiplier =
  5207. ((tmp & SDVO_MULTIPLIER_MASK)
  5208. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  5209. } else {
  5210. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  5211. * port and will be fixed up in the encoder->get_config
  5212. * function. */
  5213. pipe_config->pixel_multiplier = 1;
  5214. }
  5215. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  5216. if (!IS_VALLEYVIEW(dev)) {
  5217. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  5218. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  5219. } else {
  5220. /* Mask out read-only status bits. */
  5221. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  5222. DPLL_PORTC_READY_MASK |
  5223. DPLL_PORTB_READY_MASK);
  5224. }
  5225. if (IS_CHERRYVIEW(dev))
  5226. chv_crtc_clock_get(crtc, pipe_config);
  5227. else if (IS_VALLEYVIEW(dev))
  5228. vlv_crtc_clock_get(crtc, pipe_config);
  5229. else
  5230. i9xx_crtc_clock_get(crtc, pipe_config);
  5231. return true;
  5232. }
  5233. static void ironlake_init_pch_refclk(struct drm_device *dev)
  5234. {
  5235. struct drm_i915_private *dev_priv = dev->dev_private;
  5236. struct drm_mode_config *mode_config = &dev->mode_config;
  5237. struct intel_encoder *encoder;
  5238. u32 val, final;
  5239. bool has_lvds = false;
  5240. bool has_cpu_edp = false;
  5241. bool has_panel = false;
  5242. bool has_ck505 = false;
  5243. bool can_ssc = false;
  5244. /* We need to take the global config into account */
  5245. list_for_each_entry(encoder, &mode_config->encoder_list,
  5246. base.head) {
  5247. switch (encoder->type) {
  5248. case INTEL_OUTPUT_LVDS:
  5249. has_panel = true;
  5250. has_lvds = true;
  5251. break;
  5252. case INTEL_OUTPUT_EDP:
  5253. has_panel = true;
  5254. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  5255. has_cpu_edp = true;
  5256. break;
  5257. }
  5258. }
  5259. if (HAS_PCH_IBX(dev)) {
  5260. has_ck505 = dev_priv->vbt.display_clock_mode;
  5261. can_ssc = has_ck505;
  5262. } else {
  5263. has_ck505 = false;
  5264. can_ssc = true;
  5265. }
  5266. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  5267. has_panel, has_lvds, has_ck505);
  5268. /* Ironlake: try to setup display ref clock before DPLL
  5269. * enabling. This is only under driver's control after
  5270. * PCH B stepping, previous chipset stepping should be
  5271. * ignoring this setting.
  5272. */
  5273. val = I915_READ(PCH_DREF_CONTROL);
  5274. /* As we must carefully and slowly disable/enable each source in turn,
  5275. * compute the final state we want first and check if we need to
  5276. * make any changes at all.
  5277. */
  5278. final = val;
  5279. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  5280. if (has_ck505)
  5281. final |= DREF_NONSPREAD_CK505_ENABLE;
  5282. else
  5283. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  5284. final &= ~DREF_SSC_SOURCE_MASK;
  5285. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5286. final &= ~DREF_SSC1_ENABLE;
  5287. if (has_panel) {
  5288. final |= DREF_SSC_SOURCE_ENABLE;
  5289. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5290. final |= DREF_SSC1_ENABLE;
  5291. if (has_cpu_edp) {
  5292. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5293. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5294. else
  5295. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5296. } else
  5297. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5298. } else {
  5299. final |= DREF_SSC_SOURCE_DISABLE;
  5300. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5301. }
  5302. if (final == val)
  5303. return;
  5304. /* Always enable nonspread source */
  5305. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  5306. if (has_ck505)
  5307. val |= DREF_NONSPREAD_CK505_ENABLE;
  5308. else
  5309. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  5310. if (has_panel) {
  5311. val &= ~DREF_SSC_SOURCE_MASK;
  5312. val |= DREF_SSC_SOURCE_ENABLE;
  5313. /* SSC must be turned on before enabling the CPU output */
  5314. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5315. DRM_DEBUG_KMS("Using SSC on panel\n");
  5316. val |= DREF_SSC1_ENABLE;
  5317. } else
  5318. val &= ~DREF_SSC1_ENABLE;
  5319. /* Get SSC going before enabling the outputs */
  5320. I915_WRITE(PCH_DREF_CONTROL, val);
  5321. POSTING_READ(PCH_DREF_CONTROL);
  5322. udelay(200);
  5323. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5324. /* Enable CPU source on CPU attached eDP */
  5325. if (has_cpu_edp) {
  5326. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5327. DRM_DEBUG_KMS("Using SSC on eDP\n");
  5328. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5329. } else
  5330. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5331. } else
  5332. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5333. I915_WRITE(PCH_DREF_CONTROL, val);
  5334. POSTING_READ(PCH_DREF_CONTROL);
  5335. udelay(200);
  5336. } else {
  5337. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  5338. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5339. /* Turn off CPU output */
  5340. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5341. I915_WRITE(PCH_DREF_CONTROL, val);
  5342. POSTING_READ(PCH_DREF_CONTROL);
  5343. udelay(200);
  5344. /* Turn off the SSC source */
  5345. val &= ~DREF_SSC_SOURCE_MASK;
  5346. val |= DREF_SSC_SOURCE_DISABLE;
  5347. /* Turn off SSC1 */
  5348. val &= ~DREF_SSC1_ENABLE;
  5349. I915_WRITE(PCH_DREF_CONTROL, val);
  5350. POSTING_READ(PCH_DREF_CONTROL);
  5351. udelay(200);
  5352. }
  5353. BUG_ON(val != final);
  5354. }
  5355. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  5356. {
  5357. uint32_t tmp;
  5358. tmp = I915_READ(SOUTH_CHICKEN2);
  5359. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  5360. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5361. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  5362. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  5363. DRM_ERROR("FDI mPHY reset assert timeout\n");
  5364. tmp = I915_READ(SOUTH_CHICKEN2);
  5365. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  5366. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5367. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  5368. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  5369. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  5370. }
  5371. /* WaMPhyProgramming:hsw */
  5372. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  5373. {
  5374. uint32_t tmp;
  5375. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  5376. tmp &= ~(0xFF << 24);
  5377. tmp |= (0x12 << 24);
  5378. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  5379. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  5380. tmp |= (1 << 11);
  5381. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  5382. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  5383. tmp |= (1 << 11);
  5384. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  5385. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  5386. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5387. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  5388. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  5389. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5390. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  5391. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  5392. tmp &= ~(7 << 13);
  5393. tmp |= (5 << 13);
  5394. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  5395. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  5396. tmp &= ~(7 << 13);
  5397. tmp |= (5 << 13);
  5398. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  5399. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  5400. tmp &= ~0xFF;
  5401. tmp |= 0x1C;
  5402. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  5403. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  5404. tmp &= ~0xFF;
  5405. tmp |= 0x1C;
  5406. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  5407. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  5408. tmp &= ~(0xFF << 16);
  5409. tmp |= (0x1C << 16);
  5410. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  5411. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  5412. tmp &= ~(0xFF << 16);
  5413. tmp |= (0x1C << 16);
  5414. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  5415. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  5416. tmp |= (1 << 27);
  5417. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  5418. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  5419. tmp |= (1 << 27);
  5420. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  5421. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  5422. tmp &= ~(0xF << 28);
  5423. tmp |= (4 << 28);
  5424. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  5425. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  5426. tmp &= ~(0xF << 28);
  5427. tmp |= (4 << 28);
  5428. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  5429. }
  5430. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  5431. * Programming" based on the parameters passed:
  5432. * - Sequence to enable CLKOUT_DP
  5433. * - Sequence to enable CLKOUT_DP without spread
  5434. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  5435. */
  5436. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  5437. bool with_fdi)
  5438. {
  5439. struct drm_i915_private *dev_priv = dev->dev_private;
  5440. uint32_t reg, tmp;
  5441. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  5442. with_spread = true;
  5443. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  5444. with_fdi, "LP PCH doesn't have FDI\n"))
  5445. with_fdi = false;
  5446. mutex_lock(&dev_priv->dpio_lock);
  5447. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5448. tmp &= ~SBI_SSCCTL_DISABLE;
  5449. tmp |= SBI_SSCCTL_PATHALT;
  5450. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5451. udelay(24);
  5452. if (with_spread) {
  5453. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5454. tmp &= ~SBI_SSCCTL_PATHALT;
  5455. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5456. if (with_fdi) {
  5457. lpt_reset_fdi_mphy(dev_priv);
  5458. lpt_program_fdi_mphy(dev_priv);
  5459. }
  5460. }
  5461. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5462. SBI_GEN0 : SBI_DBUFF0;
  5463. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5464. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5465. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5466. mutex_unlock(&dev_priv->dpio_lock);
  5467. }
  5468. /* Sequence to disable CLKOUT_DP */
  5469. static void lpt_disable_clkout_dp(struct drm_device *dev)
  5470. {
  5471. struct drm_i915_private *dev_priv = dev->dev_private;
  5472. uint32_t reg, tmp;
  5473. mutex_lock(&dev_priv->dpio_lock);
  5474. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5475. SBI_GEN0 : SBI_DBUFF0;
  5476. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5477. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5478. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5479. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5480. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  5481. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  5482. tmp |= SBI_SSCCTL_PATHALT;
  5483. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5484. udelay(32);
  5485. }
  5486. tmp |= SBI_SSCCTL_DISABLE;
  5487. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5488. }
  5489. mutex_unlock(&dev_priv->dpio_lock);
  5490. }
  5491. static void lpt_init_pch_refclk(struct drm_device *dev)
  5492. {
  5493. struct drm_mode_config *mode_config = &dev->mode_config;
  5494. struct intel_encoder *encoder;
  5495. bool has_vga = false;
  5496. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  5497. switch (encoder->type) {
  5498. case INTEL_OUTPUT_ANALOG:
  5499. has_vga = true;
  5500. break;
  5501. }
  5502. }
  5503. if (has_vga)
  5504. lpt_enable_clkout_dp(dev, true, true);
  5505. else
  5506. lpt_disable_clkout_dp(dev);
  5507. }
  5508. /*
  5509. * Initialize reference clocks when the driver loads
  5510. */
  5511. void intel_init_pch_refclk(struct drm_device *dev)
  5512. {
  5513. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  5514. ironlake_init_pch_refclk(dev);
  5515. else if (HAS_PCH_LPT(dev))
  5516. lpt_init_pch_refclk(dev);
  5517. }
  5518. static int ironlake_get_refclk(struct drm_crtc *crtc)
  5519. {
  5520. struct drm_device *dev = crtc->dev;
  5521. struct drm_i915_private *dev_priv = dev->dev_private;
  5522. struct intel_encoder *encoder;
  5523. int num_connectors = 0;
  5524. bool is_lvds = false;
  5525. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5526. switch (encoder->type) {
  5527. case INTEL_OUTPUT_LVDS:
  5528. is_lvds = true;
  5529. break;
  5530. }
  5531. num_connectors++;
  5532. }
  5533. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5534. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  5535. dev_priv->vbt.lvds_ssc_freq);
  5536. return dev_priv->vbt.lvds_ssc_freq;
  5537. }
  5538. return 120000;
  5539. }
  5540. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  5541. {
  5542. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  5543. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5544. int pipe = intel_crtc->pipe;
  5545. uint32_t val;
  5546. val = 0;
  5547. switch (intel_crtc->config.pipe_bpp) {
  5548. case 18:
  5549. val |= PIPECONF_6BPC;
  5550. break;
  5551. case 24:
  5552. val |= PIPECONF_8BPC;
  5553. break;
  5554. case 30:
  5555. val |= PIPECONF_10BPC;
  5556. break;
  5557. case 36:
  5558. val |= PIPECONF_12BPC;
  5559. break;
  5560. default:
  5561. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5562. BUG();
  5563. }
  5564. if (intel_crtc->config.dither)
  5565. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  5566. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  5567. val |= PIPECONF_INTERLACED_ILK;
  5568. else
  5569. val |= PIPECONF_PROGRESSIVE;
  5570. if (intel_crtc->config.limited_color_range)
  5571. val |= PIPECONF_COLOR_RANGE_SELECT;
  5572. I915_WRITE(PIPECONF(pipe), val);
  5573. POSTING_READ(PIPECONF(pipe));
  5574. }
  5575. /*
  5576. * Set up the pipe CSC unit.
  5577. *
  5578. * Currently only full range RGB to limited range RGB conversion
  5579. * is supported, but eventually this should handle various
  5580. * RGB<->YCbCr scenarios as well.
  5581. */
  5582. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  5583. {
  5584. struct drm_device *dev = crtc->dev;
  5585. struct drm_i915_private *dev_priv = dev->dev_private;
  5586. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5587. int pipe = intel_crtc->pipe;
  5588. uint16_t coeff = 0x7800; /* 1.0 */
  5589. /*
  5590. * TODO: Check what kind of values actually come out of the pipe
  5591. * with these coeff/postoff values and adjust to get the best
  5592. * accuracy. Perhaps we even need to take the bpc value into
  5593. * consideration.
  5594. */
  5595. if (intel_crtc->config.limited_color_range)
  5596. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  5597. /*
  5598. * GY/GU and RY/RU should be the other way around according
  5599. * to BSpec, but reality doesn't agree. Just set them up in
  5600. * a way that results in the correct picture.
  5601. */
  5602. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  5603. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  5604. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  5605. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  5606. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  5607. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  5608. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  5609. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  5610. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  5611. if (INTEL_INFO(dev)->gen > 6) {
  5612. uint16_t postoff = 0;
  5613. if (intel_crtc->config.limited_color_range)
  5614. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  5615. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  5616. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  5617. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  5618. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  5619. } else {
  5620. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  5621. if (intel_crtc->config.limited_color_range)
  5622. mode |= CSC_BLACK_SCREEN_OFFSET;
  5623. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  5624. }
  5625. }
  5626. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  5627. {
  5628. struct drm_device *dev = crtc->dev;
  5629. struct drm_i915_private *dev_priv = dev->dev_private;
  5630. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5631. enum pipe pipe = intel_crtc->pipe;
  5632. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  5633. uint32_t val;
  5634. val = 0;
  5635. if (IS_HASWELL(dev) && intel_crtc->config.dither)
  5636. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  5637. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  5638. val |= PIPECONF_INTERLACED_ILK;
  5639. else
  5640. val |= PIPECONF_PROGRESSIVE;
  5641. I915_WRITE(PIPECONF(cpu_transcoder), val);
  5642. POSTING_READ(PIPECONF(cpu_transcoder));
  5643. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  5644. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  5645. if (IS_BROADWELL(dev)) {
  5646. val = 0;
  5647. switch (intel_crtc->config.pipe_bpp) {
  5648. case 18:
  5649. val |= PIPEMISC_DITHER_6_BPC;
  5650. break;
  5651. case 24:
  5652. val |= PIPEMISC_DITHER_8_BPC;
  5653. break;
  5654. case 30:
  5655. val |= PIPEMISC_DITHER_10_BPC;
  5656. break;
  5657. case 36:
  5658. val |= PIPEMISC_DITHER_12_BPC;
  5659. break;
  5660. default:
  5661. /* Case prevented by pipe_config_set_bpp. */
  5662. BUG();
  5663. }
  5664. if (intel_crtc->config.dither)
  5665. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  5666. I915_WRITE(PIPEMISC(pipe), val);
  5667. }
  5668. }
  5669. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  5670. intel_clock_t *clock,
  5671. bool *has_reduced_clock,
  5672. intel_clock_t *reduced_clock)
  5673. {
  5674. struct drm_device *dev = crtc->dev;
  5675. struct drm_i915_private *dev_priv = dev->dev_private;
  5676. struct intel_encoder *intel_encoder;
  5677. int refclk;
  5678. const intel_limit_t *limit;
  5679. bool ret, is_lvds = false;
  5680. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  5681. switch (intel_encoder->type) {
  5682. case INTEL_OUTPUT_LVDS:
  5683. is_lvds = true;
  5684. break;
  5685. }
  5686. }
  5687. refclk = ironlake_get_refclk(crtc);
  5688. /*
  5689. * Returns a set of divisors for the desired target clock with the given
  5690. * refclk, or FALSE. The returned values represent the clock equation:
  5691. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  5692. */
  5693. limit = intel_limit(crtc, refclk);
  5694. ret = dev_priv->display.find_dpll(limit, crtc,
  5695. to_intel_crtc(crtc)->config.port_clock,
  5696. refclk, NULL, clock);
  5697. if (!ret)
  5698. return false;
  5699. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5700. /*
  5701. * Ensure we match the reduced clock's P to the target clock.
  5702. * If the clocks don't match, we can't switch the display clock
  5703. * by using the FP0/FP1. In such case we will disable the LVDS
  5704. * downclock feature.
  5705. */
  5706. *has_reduced_clock =
  5707. dev_priv->display.find_dpll(limit, crtc,
  5708. dev_priv->lvds_downclock,
  5709. refclk, clock,
  5710. reduced_clock);
  5711. }
  5712. return true;
  5713. }
  5714. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  5715. {
  5716. /*
  5717. * Account for spread spectrum to avoid
  5718. * oversubscribing the link. Max center spread
  5719. * is 2.5%; use 5% for safety's sake.
  5720. */
  5721. u32 bps = target_clock * bpp * 21 / 20;
  5722. return DIV_ROUND_UP(bps, link_bw * 8);
  5723. }
  5724. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  5725. {
  5726. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  5727. }
  5728. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  5729. u32 *fp,
  5730. intel_clock_t *reduced_clock, u32 *fp2)
  5731. {
  5732. struct drm_crtc *crtc = &intel_crtc->base;
  5733. struct drm_device *dev = crtc->dev;
  5734. struct drm_i915_private *dev_priv = dev->dev_private;
  5735. struct intel_encoder *intel_encoder;
  5736. uint32_t dpll;
  5737. int factor, num_connectors = 0;
  5738. bool is_lvds = false, is_sdvo = false;
  5739. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  5740. switch (intel_encoder->type) {
  5741. case INTEL_OUTPUT_LVDS:
  5742. is_lvds = true;
  5743. break;
  5744. case INTEL_OUTPUT_SDVO:
  5745. case INTEL_OUTPUT_HDMI:
  5746. is_sdvo = true;
  5747. break;
  5748. }
  5749. num_connectors++;
  5750. }
  5751. /* Enable autotuning of the PLL clock (if permissible) */
  5752. factor = 21;
  5753. if (is_lvds) {
  5754. if ((intel_panel_use_ssc(dev_priv) &&
  5755. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  5756. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  5757. factor = 25;
  5758. } else if (intel_crtc->config.sdvo_tv_clock)
  5759. factor = 20;
  5760. if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
  5761. *fp |= FP_CB_TUNE;
  5762. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  5763. *fp2 |= FP_CB_TUNE;
  5764. dpll = 0;
  5765. if (is_lvds)
  5766. dpll |= DPLLB_MODE_LVDS;
  5767. else
  5768. dpll |= DPLLB_MODE_DAC_SERIAL;
  5769. dpll |= (intel_crtc->config.pixel_multiplier - 1)
  5770. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  5771. if (is_sdvo)
  5772. dpll |= DPLL_SDVO_HIGH_SPEED;
  5773. if (intel_crtc->config.has_dp_encoder)
  5774. dpll |= DPLL_SDVO_HIGH_SPEED;
  5775. /* compute bitmask from p1 value */
  5776. dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5777. /* also FPA1 */
  5778. dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5779. switch (intel_crtc->config.dpll.p2) {
  5780. case 5:
  5781. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5782. break;
  5783. case 7:
  5784. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5785. break;
  5786. case 10:
  5787. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5788. break;
  5789. case 14:
  5790. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5791. break;
  5792. }
  5793. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5794. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5795. else
  5796. dpll |= PLL_REF_INPUT_DREFCLK;
  5797. return dpll | DPLL_VCO_ENABLE;
  5798. }
  5799. static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
  5800. int x, int y,
  5801. struct drm_framebuffer *fb)
  5802. {
  5803. struct drm_device *dev = crtc->dev;
  5804. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5805. int num_connectors = 0;
  5806. intel_clock_t clock, reduced_clock;
  5807. u32 dpll = 0, fp = 0, fp2 = 0;
  5808. bool ok, has_reduced_clock = false;
  5809. bool is_lvds = false;
  5810. struct intel_encoder *encoder;
  5811. struct intel_shared_dpll *pll;
  5812. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5813. switch (encoder->type) {
  5814. case INTEL_OUTPUT_LVDS:
  5815. is_lvds = true;
  5816. break;
  5817. }
  5818. num_connectors++;
  5819. }
  5820. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  5821. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  5822. ok = ironlake_compute_clocks(crtc, &clock,
  5823. &has_reduced_clock, &reduced_clock);
  5824. if (!ok && !intel_crtc->config.clock_set) {
  5825. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5826. return -EINVAL;
  5827. }
  5828. /* Compat-code for transition, will disappear. */
  5829. if (!intel_crtc->config.clock_set) {
  5830. intel_crtc->config.dpll.n = clock.n;
  5831. intel_crtc->config.dpll.m1 = clock.m1;
  5832. intel_crtc->config.dpll.m2 = clock.m2;
  5833. intel_crtc->config.dpll.p1 = clock.p1;
  5834. intel_crtc->config.dpll.p2 = clock.p2;
  5835. }
  5836. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  5837. if (intel_crtc->config.has_pch_encoder) {
  5838. fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
  5839. if (has_reduced_clock)
  5840. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  5841. dpll = ironlake_compute_dpll(intel_crtc,
  5842. &fp, &reduced_clock,
  5843. has_reduced_clock ? &fp2 : NULL);
  5844. intel_crtc->config.dpll_hw_state.dpll = dpll;
  5845. intel_crtc->config.dpll_hw_state.fp0 = fp;
  5846. if (has_reduced_clock)
  5847. intel_crtc->config.dpll_hw_state.fp1 = fp2;
  5848. else
  5849. intel_crtc->config.dpll_hw_state.fp1 = fp;
  5850. pll = intel_get_shared_dpll(intel_crtc);
  5851. if (pll == NULL) {
  5852. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  5853. pipe_name(intel_crtc->pipe));
  5854. return -EINVAL;
  5855. }
  5856. } else
  5857. intel_put_shared_dpll(intel_crtc);
  5858. if (is_lvds && has_reduced_clock && i915.powersave)
  5859. intel_crtc->lowfreq_avail = true;
  5860. else
  5861. intel_crtc->lowfreq_avail = false;
  5862. return 0;
  5863. }
  5864. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  5865. struct intel_link_m_n *m_n)
  5866. {
  5867. struct drm_device *dev = crtc->base.dev;
  5868. struct drm_i915_private *dev_priv = dev->dev_private;
  5869. enum pipe pipe = crtc->pipe;
  5870. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  5871. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  5872. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  5873. & ~TU_SIZE_MASK;
  5874. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  5875. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  5876. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  5877. }
  5878. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  5879. enum transcoder transcoder,
  5880. struct intel_link_m_n *m_n)
  5881. {
  5882. struct drm_device *dev = crtc->base.dev;
  5883. struct drm_i915_private *dev_priv = dev->dev_private;
  5884. enum pipe pipe = crtc->pipe;
  5885. if (INTEL_INFO(dev)->gen >= 5) {
  5886. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  5887. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  5888. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  5889. & ~TU_SIZE_MASK;
  5890. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  5891. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  5892. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  5893. } else {
  5894. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  5895. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  5896. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  5897. & ~TU_SIZE_MASK;
  5898. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  5899. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  5900. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  5901. }
  5902. }
  5903. void intel_dp_get_m_n(struct intel_crtc *crtc,
  5904. struct intel_crtc_config *pipe_config)
  5905. {
  5906. if (crtc->config.has_pch_encoder)
  5907. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  5908. else
  5909. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  5910. &pipe_config->dp_m_n);
  5911. }
  5912. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  5913. struct intel_crtc_config *pipe_config)
  5914. {
  5915. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  5916. &pipe_config->fdi_m_n);
  5917. }
  5918. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  5919. struct intel_crtc_config *pipe_config)
  5920. {
  5921. struct drm_device *dev = crtc->base.dev;
  5922. struct drm_i915_private *dev_priv = dev->dev_private;
  5923. uint32_t tmp;
  5924. tmp = I915_READ(PF_CTL(crtc->pipe));
  5925. if (tmp & PF_ENABLE) {
  5926. pipe_config->pch_pfit.enabled = true;
  5927. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  5928. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  5929. /* We currently do not free assignements of panel fitters on
  5930. * ivb/hsw (since we don't use the higher upscaling modes which
  5931. * differentiates them) so just WARN about this case for now. */
  5932. if (IS_GEN7(dev)) {
  5933. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  5934. PF_PIPE_SEL_IVB(crtc->pipe));
  5935. }
  5936. }
  5937. }
  5938. static void ironlake_get_plane_config(struct intel_crtc *crtc,
  5939. struct intel_plane_config *plane_config)
  5940. {
  5941. struct drm_device *dev = crtc->base.dev;
  5942. struct drm_i915_private *dev_priv = dev->dev_private;
  5943. u32 val, base, offset;
  5944. int pipe = crtc->pipe, plane = crtc->plane;
  5945. int fourcc, pixel_format;
  5946. int aligned_height;
  5947. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  5948. if (!crtc->base.primary->fb) {
  5949. DRM_DEBUG_KMS("failed to alloc fb\n");
  5950. return;
  5951. }
  5952. val = I915_READ(DSPCNTR(plane));
  5953. if (INTEL_INFO(dev)->gen >= 4)
  5954. if (val & DISPPLANE_TILED)
  5955. plane_config->tiled = true;
  5956. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  5957. fourcc = intel_format_to_fourcc(pixel_format);
  5958. crtc->base.primary->fb->pixel_format = fourcc;
  5959. crtc->base.primary->fb->bits_per_pixel =
  5960. drm_format_plane_cpp(fourcc, 0) * 8;
  5961. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  5962. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5963. offset = I915_READ(DSPOFFSET(plane));
  5964. } else {
  5965. if (plane_config->tiled)
  5966. offset = I915_READ(DSPTILEOFF(plane));
  5967. else
  5968. offset = I915_READ(DSPLINOFF(plane));
  5969. }
  5970. plane_config->base = base;
  5971. val = I915_READ(PIPESRC(pipe));
  5972. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  5973. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  5974. val = I915_READ(DSPSTRIDE(pipe));
  5975. crtc->base.primary->fb->pitches[0] = val & 0xffffff80;
  5976. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  5977. plane_config->tiled);
  5978. plane_config->size = ALIGN(crtc->base.primary->fb->pitches[0] *
  5979. aligned_height, PAGE_SIZE);
  5980. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  5981. pipe, plane, crtc->base.primary->fb->width,
  5982. crtc->base.primary->fb->height,
  5983. crtc->base.primary->fb->bits_per_pixel, base,
  5984. crtc->base.primary->fb->pitches[0],
  5985. plane_config->size);
  5986. }
  5987. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  5988. struct intel_crtc_config *pipe_config)
  5989. {
  5990. struct drm_device *dev = crtc->base.dev;
  5991. struct drm_i915_private *dev_priv = dev->dev_private;
  5992. uint32_t tmp;
  5993. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5994. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5995. tmp = I915_READ(PIPECONF(crtc->pipe));
  5996. if (!(tmp & PIPECONF_ENABLE))
  5997. return false;
  5998. switch (tmp & PIPECONF_BPC_MASK) {
  5999. case PIPECONF_6BPC:
  6000. pipe_config->pipe_bpp = 18;
  6001. break;
  6002. case PIPECONF_8BPC:
  6003. pipe_config->pipe_bpp = 24;
  6004. break;
  6005. case PIPECONF_10BPC:
  6006. pipe_config->pipe_bpp = 30;
  6007. break;
  6008. case PIPECONF_12BPC:
  6009. pipe_config->pipe_bpp = 36;
  6010. break;
  6011. default:
  6012. break;
  6013. }
  6014. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  6015. pipe_config->limited_color_range = true;
  6016. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  6017. struct intel_shared_dpll *pll;
  6018. pipe_config->has_pch_encoder = true;
  6019. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  6020. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6021. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6022. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6023. if (HAS_PCH_IBX(dev_priv->dev)) {
  6024. pipe_config->shared_dpll =
  6025. (enum intel_dpll_id) crtc->pipe;
  6026. } else {
  6027. tmp = I915_READ(PCH_DPLL_SEL);
  6028. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  6029. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  6030. else
  6031. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  6032. }
  6033. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6034. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6035. &pipe_config->dpll_hw_state));
  6036. tmp = pipe_config->dpll_hw_state.dpll;
  6037. pipe_config->pixel_multiplier =
  6038. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  6039. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  6040. ironlake_pch_clock_get(crtc, pipe_config);
  6041. } else {
  6042. pipe_config->pixel_multiplier = 1;
  6043. }
  6044. intel_get_pipe_timings(crtc, pipe_config);
  6045. ironlake_get_pfit_config(crtc, pipe_config);
  6046. return true;
  6047. }
  6048. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  6049. {
  6050. struct drm_device *dev = dev_priv->dev;
  6051. struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
  6052. struct intel_crtc *crtc;
  6053. for_each_intel_crtc(dev, crtc)
  6054. WARN(crtc->active, "CRTC for pipe %c enabled\n",
  6055. pipe_name(crtc->pipe));
  6056. WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  6057. WARN(plls->spll_refcount, "SPLL enabled\n");
  6058. WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
  6059. WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
  6060. WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  6061. WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  6062. "CPU PWM1 enabled\n");
  6063. WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  6064. "CPU PWM2 enabled\n");
  6065. WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  6066. "PCH PWM1 enabled\n");
  6067. WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  6068. "Utility pin enabled\n");
  6069. WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  6070. /*
  6071. * In theory we can still leave IRQs enabled, as long as only the HPD
  6072. * interrupts remain enabled. We used to check for that, but since it's
  6073. * gen-specific and since we only disable LCPLL after we fully disable
  6074. * the interrupts, the check below should be enough.
  6075. */
  6076. WARN(!dev_priv->pm.irqs_disabled, "IRQs enabled\n");
  6077. }
  6078. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  6079. {
  6080. struct drm_device *dev = dev_priv->dev;
  6081. if (IS_HASWELL(dev)) {
  6082. mutex_lock(&dev_priv->rps.hw_lock);
  6083. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  6084. val))
  6085. DRM_ERROR("Failed to disable D_COMP\n");
  6086. mutex_unlock(&dev_priv->rps.hw_lock);
  6087. } else {
  6088. I915_WRITE(D_COMP, val);
  6089. }
  6090. POSTING_READ(D_COMP);
  6091. }
  6092. /*
  6093. * This function implements pieces of two sequences from BSpec:
  6094. * - Sequence for display software to disable LCPLL
  6095. * - Sequence for display software to allow package C8+
  6096. * The steps implemented here are just the steps that actually touch the LCPLL
  6097. * register. Callers should take care of disabling all the display engine
  6098. * functions, doing the mode unset, fixing interrupts, etc.
  6099. */
  6100. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  6101. bool switch_to_fclk, bool allow_power_down)
  6102. {
  6103. uint32_t val;
  6104. assert_can_disable_lcpll(dev_priv);
  6105. val = I915_READ(LCPLL_CTL);
  6106. if (switch_to_fclk) {
  6107. val |= LCPLL_CD_SOURCE_FCLK;
  6108. I915_WRITE(LCPLL_CTL, val);
  6109. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  6110. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  6111. DRM_ERROR("Switching to FCLK failed\n");
  6112. val = I915_READ(LCPLL_CTL);
  6113. }
  6114. val |= LCPLL_PLL_DISABLE;
  6115. I915_WRITE(LCPLL_CTL, val);
  6116. POSTING_READ(LCPLL_CTL);
  6117. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  6118. DRM_ERROR("LCPLL still locked\n");
  6119. val = I915_READ(D_COMP);
  6120. val |= D_COMP_COMP_DISABLE;
  6121. hsw_write_dcomp(dev_priv, val);
  6122. ndelay(100);
  6123. if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
  6124. DRM_ERROR("D_COMP RCOMP still in progress\n");
  6125. if (allow_power_down) {
  6126. val = I915_READ(LCPLL_CTL);
  6127. val |= LCPLL_POWER_DOWN_ALLOW;
  6128. I915_WRITE(LCPLL_CTL, val);
  6129. POSTING_READ(LCPLL_CTL);
  6130. }
  6131. }
  6132. /*
  6133. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  6134. * source.
  6135. */
  6136. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  6137. {
  6138. uint32_t val;
  6139. unsigned long irqflags;
  6140. val = I915_READ(LCPLL_CTL);
  6141. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  6142. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  6143. return;
  6144. /*
  6145. * Make sure we're not on PC8 state before disabling PC8, otherwise
  6146. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  6147. *
  6148. * The other problem is that hsw_restore_lcpll() is called as part of
  6149. * the runtime PM resume sequence, so we can't just call
  6150. * gen6_gt_force_wake_get() because that function calls
  6151. * intel_runtime_pm_get(), and we can't change the runtime PM refcount
  6152. * while we are on the resume sequence. So to solve this problem we have
  6153. * to call special forcewake code that doesn't touch runtime PM and
  6154. * doesn't enable the forcewake delayed work.
  6155. */
  6156. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  6157. if (dev_priv->uncore.forcewake_count++ == 0)
  6158. dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
  6159. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  6160. if (val & LCPLL_POWER_DOWN_ALLOW) {
  6161. val &= ~LCPLL_POWER_DOWN_ALLOW;
  6162. I915_WRITE(LCPLL_CTL, val);
  6163. POSTING_READ(LCPLL_CTL);
  6164. }
  6165. val = I915_READ(D_COMP);
  6166. val |= D_COMP_COMP_FORCE;
  6167. val &= ~D_COMP_COMP_DISABLE;
  6168. hsw_write_dcomp(dev_priv, val);
  6169. val = I915_READ(LCPLL_CTL);
  6170. val &= ~LCPLL_PLL_DISABLE;
  6171. I915_WRITE(LCPLL_CTL, val);
  6172. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  6173. DRM_ERROR("LCPLL not locked yet\n");
  6174. if (val & LCPLL_CD_SOURCE_FCLK) {
  6175. val = I915_READ(LCPLL_CTL);
  6176. val &= ~LCPLL_CD_SOURCE_FCLK;
  6177. I915_WRITE(LCPLL_CTL, val);
  6178. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  6179. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  6180. DRM_ERROR("Switching back to LCPLL failed\n");
  6181. }
  6182. /* See the big comment above. */
  6183. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  6184. if (--dev_priv->uncore.forcewake_count == 0)
  6185. dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
  6186. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  6187. }
  6188. /*
  6189. * Package states C8 and deeper are really deep PC states that can only be
  6190. * reached when all the devices on the system allow it, so even if the graphics
  6191. * device allows PC8+, it doesn't mean the system will actually get to these
  6192. * states. Our driver only allows PC8+ when going into runtime PM.
  6193. *
  6194. * The requirements for PC8+ are that all the outputs are disabled, the power
  6195. * well is disabled and most interrupts are disabled, and these are also
  6196. * requirements for runtime PM. When these conditions are met, we manually do
  6197. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  6198. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  6199. * hang the machine.
  6200. *
  6201. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  6202. * the state of some registers, so when we come back from PC8+ we need to
  6203. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  6204. * need to take care of the registers kept by RC6. Notice that this happens even
  6205. * if we don't put the device in PCI D3 state (which is what currently happens
  6206. * because of the runtime PM support).
  6207. *
  6208. * For more, read "Display Sequences for Package C8" on the hardware
  6209. * documentation.
  6210. */
  6211. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  6212. {
  6213. struct drm_device *dev = dev_priv->dev;
  6214. uint32_t val;
  6215. DRM_DEBUG_KMS("Enabling package C8+\n");
  6216. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6217. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6218. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  6219. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6220. }
  6221. lpt_disable_clkout_dp(dev);
  6222. hsw_disable_lcpll(dev_priv, true, true);
  6223. }
  6224. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  6225. {
  6226. struct drm_device *dev = dev_priv->dev;
  6227. uint32_t val;
  6228. DRM_DEBUG_KMS("Disabling package C8+\n");
  6229. hsw_restore_lcpll(dev_priv);
  6230. lpt_init_pch_refclk(dev);
  6231. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6232. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6233. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  6234. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6235. }
  6236. intel_prepare_ddi(dev);
  6237. }
  6238. static void snb_modeset_global_resources(struct drm_device *dev)
  6239. {
  6240. modeset_update_crtc_power_domains(dev);
  6241. }
  6242. static void haswell_modeset_global_resources(struct drm_device *dev)
  6243. {
  6244. modeset_update_crtc_power_domains(dev);
  6245. }
  6246. static int haswell_crtc_mode_set(struct drm_crtc *crtc,
  6247. int x, int y,
  6248. struct drm_framebuffer *fb)
  6249. {
  6250. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6251. if (!intel_ddi_pll_select(intel_crtc))
  6252. return -EINVAL;
  6253. intel_ddi_pll_enable(intel_crtc);
  6254. intel_crtc->lowfreq_avail = false;
  6255. return 0;
  6256. }
  6257. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  6258. struct intel_crtc_config *pipe_config)
  6259. {
  6260. struct drm_device *dev = crtc->base.dev;
  6261. struct drm_i915_private *dev_priv = dev->dev_private;
  6262. enum intel_display_power_domain pfit_domain;
  6263. uint32_t tmp;
  6264. if (!intel_display_power_enabled(dev_priv,
  6265. POWER_DOMAIN_PIPE(crtc->pipe)))
  6266. return false;
  6267. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6268. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6269. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  6270. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  6271. enum pipe trans_edp_pipe;
  6272. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  6273. default:
  6274. WARN(1, "unknown pipe linked to edp transcoder\n");
  6275. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  6276. case TRANS_DDI_EDP_INPUT_A_ON:
  6277. trans_edp_pipe = PIPE_A;
  6278. break;
  6279. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  6280. trans_edp_pipe = PIPE_B;
  6281. break;
  6282. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  6283. trans_edp_pipe = PIPE_C;
  6284. break;
  6285. }
  6286. if (trans_edp_pipe == crtc->pipe)
  6287. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  6288. }
  6289. if (!intel_display_power_enabled(dev_priv,
  6290. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  6291. return false;
  6292. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  6293. if (!(tmp & PIPECONF_ENABLE))
  6294. return false;
  6295. /*
  6296. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  6297. * DDI E. So just check whether this pipe is wired to DDI E and whether
  6298. * the PCH transcoder is on.
  6299. */
  6300. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  6301. if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
  6302. I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  6303. pipe_config->has_pch_encoder = true;
  6304. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  6305. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6306. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6307. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6308. }
  6309. intel_get_pipe_timings(crtc, pipe_config);
  6310. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  6311. if (intel_display_power_enabled(dev_priv, pfit_domain))
  6312. ironlake_get_pfit_config(crtc, pipe_config);
  6313. if (IS_HASWELL(dev))
  6314. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  6315. (I915_READ(IPS_CTL) & IPS_ENABLE);
  6316. pipe_config->pixel_multiplier = 1;
  6317. return true;
  6318. }
  6319. static struct {
  6320. int clock;
  6321. u32 config;
  6322. } hdmi_audio_clock[] = {
  6323. { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
  6324. { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
  6325. { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
  6326. { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
  6327. { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
  6328. { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
  6329. { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
  6330. { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
  6331. { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
  6332. { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
  6333. };
  6334. /* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
  6335. static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
  6336. {
  6337. int i;
  6338. for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
  6339. if (mode->clock == hdmi_audio_clock[i].clock)
  6340. break;
  6341. }
  6342. if (i == ARRAY_SIZE(hdmi_audio_clock)) {
  6343. DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
  6344. i = 1;
  6345. }
  6346. DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
  6347. hdmi_audio_clock[i].clock,
  6348. hdmi_audio_clock[i].config);
  6349. return hdmi_audio_clock[i].config;
  6350. }
  6351. static bool intel_eld_uptodate(struct drm_connector *connector,
  6352. int reg_eldv, uint32_t bits_eldv,
  6353. int reg_elda, uint32_t bits_elda,
  6354. int reg_edid)
  6355. {
  6356. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6357. uint8_t *eld = connector->eld;
  6358. uint32_t i;
  6359. i = I915_READ(reg_eldv);
  6360. i &= bits_eldv;
  6361. if (!eld[0])
  6362. return !i;
  6363. if (!i)
  6364. return false;
  6365. i = I915_READ(reg_elda);
  6366. i &= ~bits_elda;
  6367. I915_WRITE(reg_elda, i);
  6368. for (i = 0; i < eld[2]; i++)
  6369. if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
  6370. return false;
  6371. return true;
  6372. }
  6373. static void g4x_write_eld(struct drm_connector *connector,
  6374. struct drm_crtc *crtc,
  6375. struct drm_display_mode *mode)
  6376. {
  6377. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6378. uint8_t *eld = connector->eld;
  6379. uint32_t eldv;
  6380. uint32_t len;
  6381. uint32_t i;
  6382. i = I915_READ(G4X_AUD_VID_DID);
  6383. if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
  6384. eldv = G4X_ELDV_DEVCL_DEVBLC;
  6385. else
  6386. eldv = G4X_ELDV_DEVCTG;
  6387. if (intel_eld_uptodate(connector,
  6388. G4X_AUD_CNTL_ST, eldv,
  6389. G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
  6390. G4X_HDMIW_HDMIEDID))
  6391. return;
  6392. i = I915_READ(G4X_AUD_CNTL_ST);
  6393. i &= ~(eldv | G4X_ELD_ADDR);
  6394. len = (i >> 9) & 0x1f; /* ELD buffer size */
  6395. I915_WRITE(G4X_AUD_CNTL_ST, i);
  6396. if (!eld[0])
  6397. return;
  6398. len = min_t(uint8_t, eld[2], len);
  6399. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6400. for (i = 0; i < len; i++)
  6401. I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
  6402. i = I915_READ(G4X_AUD_CNTL_ST);
  6403. i |= eldv;
  6404. I915_WRITE(G4X_AUD_CNTL_ST, i);
  6405. }
  6406. static void haswell_write_eld(struct drm_connector *connector,
  6407. struct drm_crtc *crtc,
  6408. struct drm_display_mode *mode)
  6409. {
  6410. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6411. uint8_t *eld = connector->eld;
  6412. uint32_t eldv;
  6413. uint32_t i;
  6414. int len;
  6415. int pipe = to_intel_crtc(crtc)->pipe;
  6416. int tmp;
  6417. int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
  6418. int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
  6419. int aud_config = HSW_AUD_CFG(pipe);
  6420. int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
  6421. /* Audio output enable */
  6422. DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
  6423. tmp = I915_READ(aud_cntrl_st2);
  6424. tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
  6425. I915_WRITE(aud_cntrl_st2, tmp);
  6426. POSTING_READ(aud_cntrl_st2);
  6427. assert_pipe_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
  6428. /* Set ELD valid state */
  6429. tmp = I915_READ(aud_cntrl_st2);
  6430. DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
  6431. tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
  6432. I915_WRITE(aud_cntrl_st2, tmp);
  6433. tmp = I915_READ(aud_cntrl_st2);
  6434. DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
  6435. /* Enable HDMI mode */
  6436. tmp = I915_READ(aud_config);
  6437. DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
  6438. /* clear N_programing_enable and N_value_index */
  6439. tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
  6440. I915_WRITE(aud_config, tmp);
  6441. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  6442. eldv = AUDIO_ELD_VALID_A << (pipe * 4);
  6443. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  6444. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  6445. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  6446. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  6447. } else {
  6448. I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
  6449. }
  6450. if (intel_eld_uptodate(connector,
  6451. aud_cntrl_st2, eldv,
  6452. aud_cntl_st, IBX_ELD_ADDRESS,
  6453. hdmiw_hdmiedid))
  6454. return;
  6455. i = I915_READ(aud_cntrl_st2);
  6456. i &= ~eldv;
  6457. I915_WRITE(aud_cntrl_st2, i);
  6458. if (!eld[0])
  6459. return;
  6460. i = I915_READ(aud_cntl_st);
  6461. i &= ~IBX_ELD_ADDRESS;
  6462. I915_WRITE(aud_cntl_st, i);
  6463. i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
  6464. DRM_DEBUG_DRIVER("port num:%d\n", i);
  6465. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  6466. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6467. for (i = 0; i < len; i++)
  6468. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  6469. i = I915_READ(aud_cntrl_st2);
  6470. i |= eldv;
  6471. I915_WRITE(aud_cntrl_st2, i);
  6472. }
  6473. static void ironlake_write_eld(struct drm_connector *connector,
  6474. struct drm_crtc *crtc,
  6475. struct drm_display_mode *mode)
  6476. {
  6477. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6478. uint8_t *eld = connector->eld;
  6479. uint32_t eldv;
  6480. uint32_t i;
  6481. int len;
  6482. int hdmiw_hdmiedid;
  6483. int aud_config;
  6484. int aud_cntl_st;
  6485. int aud_cntrl_st2;
  6486. int pipe = to_intel_crtc(crtc)->pipe;
  6487. if (HAS_PCH_IBX(connector->dev)) {
  6488. hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
  6489. aud_config = IBX_AUD_CFG(pipe);
  6490. aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
  6491. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  6492. } else if (IS_VALLEYVIEW(connector->dev)) {
  6493. hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
  6494. aud_config = VLV_AUD_CFG(pipe);
  6495. aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
  6496. aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
  6497. } else {
  6498. hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
  6499. aud_config = CPT_AUD_CFG(pipe);
  6500. aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
  6501. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  6502. }
  6503. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  6504. if (IS_VALLEYVIEW(connector->dev)) {
  6505. struct intel_encoder *intel_encoder;
  6506. struct intel_digital_port *intel_dig_port;
  6507. intel_encoder = intel_attached_encoder(connector);
  6508. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  6509. i = intel_dig_port->port;
  6510. } else {
  6511. i = I915_READ(aud_cntl_st);
  6512. i = (i >> 29) & DIP_PORT_SEL_MASK;
  6513. /* DIP_Port_Select, 0x1 = PortB */
  6514. }
  6515. if (!i) {
  6516. DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
  6517. /* operate blindly on all ports */
  6518. eldv = IBX_ELD_VALIDB;
  6519. eldv |= IBX_ELD_VALIDB << 4;
  6520. eldv |= IBX_ELD_VALIDB << 8;
  6521. } else {
  6522. DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
  6523. eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
  6524. }
  6525. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  6526. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  6527. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  6528. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  6529. } else {
  6530. I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
  6531. }
  6532. if (intel_eld_uptodate(connector,
  6533. aud_cntrl_st2, eldv,
  6534. aud_cntl_st, IBX_ELD_ADDRESS,
  6535. hdmiw_hdmiedid))
  6536. return;
  6537. i = I915_READ(aud_cntrl_st2);
  6538. i &= ~eldv;
  6539. I915_WRITE(aud_cntrl_st2, i);
  6540. if (!eld[0])
  6541. return;
  6542. i = I915_READ(aud_cntl_st);
  6543. i &= ~IBX_ELD_ADDRESS;
  6544. I915_WRITE(aud_cntl_st, i);
  6545. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  6546. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6547. for (i = 0; i < len; i++)
  6548. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  6549. i = I915_READ(aud_cntrl_st2);
  6550. i |= eldv;
  6551. I915_WRITE(aud_cntrl_st2, i);
  6552. }
  6553. void intel_write_eld(struct drm_encoder *encoder,
  6554. struct drm_display_mode *mode)
  6555. {
  6556. struct drm_crtc *crtc = encoder->crtc;
  6557. struct drm_connector *connector;
  6558. struct drm_device *dev = encoder->dev;
  6559. struct drm_i915_private *dev_priv = dev->dev_private;
  6560. connector = drm_select_eld(encoder, mode);
  6561. if (!connector)
  6562. return;
  6563. DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  6564. connector->base.id,
  6565. connector->name,
  6566. connector->encoder->base.id,
  6567. connector->encoder->name);
  6568. connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
  6569. if (dev_priv->display.write_eld)
  6570. dev_priv->display.write_eld(connector, crtc, mode);
  6571. }
  6572. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  6573. {
  6574. struct drm_device *dev = crtc->dev;
  6575. struct drm_i915_private *dev_priv = dev->dev_private;
  6576. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6577. uint32_t cntl;
  6578. if (base != intel_crtc->cursor_base) {
  6579. /* On these chipsets we can only modify the base whilst
  6580. * the cursor is disabled.
  6581. */
  6582. if (intel_crtc->cursor_cntl) {
  6583. I915_WRITE(_CURACNTR, 0);
  6584. POSTING_READ(_CURACNTR);
  6585. intel_crtc->cursor_cntl = 0;
  6586. }
  6587. I915_WRITE(_CURABASE, base);
  6588. POSTING_READ(_CURABASE);
  6589. }
  6590. /* XXX width must be 64, stride 256 => 0x00 << 28 */
  6591. cntl = 0;
  6592. if (base)
  6593. cntl = (CURSOR_ENABLE |
  6594. CURSOR_GAMMA_ENABLE |
  6595. CURSOR_FORMAT_ARGB);
  6596. if (intel_crtc->cursor_cntl != cntl) {
  6597. I915_WRITE(_CURACNTR, cntl);
  6598. POSTING_READ(_CURACNTR);
  6599. intel_crtc->cursor_cntl = cntl;
  6600. }
  6601. }
  6602. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  6603. {
  6604. struct drm_device *dev = crtc->dev;
  6605. struct drm_i915_private *dev_priv = dev->dev_private;
  6606. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6607. int pipe = intel_crtc->pipe;
  6608. uint32_t cntl;
  6609. cntl = 0;
  6610. if (base) {
  6611. cntl = MCURSOR_GAMMA_ENABLE;
  6612. switch (intel_crtc->cursor_width) {
  6613. case 64:
  6614. cntl |= CURSOR_MODE_64_ARGB_AX;
  6615. break;
  6616. case 128:
  6617. cntl |= CURSOR_MODE_128_ARGB_AX;
  6618. break;
  6619. case 256:
  6620. cntl |= CURSOR_MODE_256_ARGB_AX;
  6621. break;
  6622. default:
  6623. WARN_ON(1);
  6624. return;
  6625. }
  6626. cntl |= pipe << 28; /* Connect to correct pipe */
  6627. }
  6628. if (intel_crtc->cursor_cntl != cntl) {
  6629. I915_WRITE(CURCNTR(pipe), cntl);
  6630. POSTING_READ(CURCNTR(pipe));
  6631. intel_crtc->cursor_cntl = cntl;
  6632. }
  6633. /* and commit changes on next vblank */
  6634. I915_WRITE(CURBASE(pipe), base);
  6635. POSTING_READ(CURBASE(pipe));
  6636. }
  6637. static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
  6638. {
  6639. struct drm_device *dev = crtc->dev;
  6640. struct drm_i915_private *dev_priv = dev->dev_private;
  6641. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6642. int pipe = intel_crtc->pipe;
  6643. uint32_t cntl;
  6644. cntl = 0;
  6645. if (base) {
  6646. cntl = MCURSOR_GAMMA_ENABLE;
  6647. switch (intel_crtc->cursor_width) {
  6648. case 64:
  6649. cntl |= CURSOR_MODE_64_ARGB_AX;
  6650. break;
  6651. case 128:
  6652. cntl |= CURSOR_MODE_128_ARGB_AX;
  6653. break;
  6654. case 256:
  6655. cntl |= CURSOR_MODE_256_ARGB_AX;
  6656. break;
  6657. default:
  6658. WARN_ON(1);
  6659. return;
  6660. }
  6661. }
  6662. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  6663. cntl |= CURSOR_PIPE_CSC_ENABLE;
  6664. if (intel_crtc->cursor_cntl != cntl) {
  6665. I915_WRITE(CURCNTR(pipe), cntl);
  6666. POSTING_READ(CURCNTR(pipe));
  6667. intel_crtc->cursor_cntl = cntl;
  6668. }
  6669. /* and commit changes on next vblank */
  6670. I915_WRITE(CURBASE(pipe), base);
  6671. POSTING_READ(CURBASE(pipe));
  6672. }
  6673. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  6674. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  6675. bool on)
  6676. {
  6677. struct drm_device *dev = crtc->dev;
  6678. struct drm_i915_private *dev_priv = dev->dev_private;
  6679. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6680. int pipe = intel_crtc->pipe;
  6681. int x = intel_crtc->cursor_x;
  6682. int y = intel_crtc->cursor_y;
  6683. u32 base = 0, pos = 0;
  6684. if (on)
  6685. base = intel_crtc->cursor_addr;
  6686. if (x >= intel_crtc->config.pipe_src_w)
  6687. base = 0;
  6688. if (y >= intel_crtc->config.pipe_src_h)
  6689. base = 0;
  6690. if (x < 0) {
  6691. if (x + intel_crtc->cursor_width <= 0)
  6692. base = 0;
  6693. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  6694. x = -x;
  6695. }
  6696. pos |= x << CURSOR_X_SHIFT;
  6697. if (y < 0) {
  6698. if (y + intel_crtc->cursor_height <= 0)
  6699. base = 0;
  6700. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  6701. y = -y;
  6702. }
  6703. pos |= y << CURSOR_Y_SHIFT;
  6704. if (base == 0 && intel_crtc->cursor_base == 0)
  6705. return;
  6706. I915_WRITE(CURPOS(pipe), pos);
  6707. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev) || IS_BROADWELL(dev))
  6708. ivb_update_cursor(crtc, base);
  6709. else if (IS_845G(dev) || IS_I865G(dev))
  6710. i845_update_cursor(crtc, base);
  6711. else
  6712. i9xx_update_cursor(crtc, base);
  6713. intel_crtc->cursor_base = base;
  6714. }
  6715. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  6716. struct drm_file *file,
  6717. uint32_t handle,
  6718. uint32_t width, uint32_t height)
  6719. {
  6720. struct drm_device *dev = crtc->dev;
  6721. struct drm_i915_private *dev_priv = dev->dev_private;
  6722. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6723. struct drm_i915_gem_object *obj;
  6724. unsigned old_width;
  6725. uint32_t addr;
  6726. int ret;
  6727. /* if we want to turn off the cursor ignore width and height */
  6728. if (!handle) {
  6729. DRM_DEBUG_KMS("cursor off\n");
  6730. addr = 0;
  6731. obj = NULL;
  6732. mutex_lock(&dev->struct_mutex);
  6733. goto finish;
  6734. }
  6735. /* Check for which cursor types we support */
  6736. if (!((width == 64 && height == 64) ||
  6737. (width == 128 && height == 128 && !IS_GEN2(dev)) ||
  6738. (width == 256 && height == 256 && !IS_GEN2(dev)))) {
  6739. DRM_DEBUG("Cursor dimension not supported\n");
  6740. return -EINVAL;
  6741. }
  6742. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  6743. if (&obj->base == NULL)
  6744. return -ENOENT;
  6745. if (obj->base.size < width * height * 4) {
  6746. DRM_DEBUG_KMS("buffer is to small\n");
  6747. ret = -ENOMEM;
  6748. goto fail;
  6749. }
  6750. /* we only need to pin inside GTT if cursor is non-phy */
  6751. mutex_lock(&dev->struct_mutex);
  6752. if (!INTEL_INFO(dev)->cursor_needs_physical) {
  6753. unsigned alignment;
  6754. if (obj->tiling_mode) {
  6755. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  6756. ret = -EINVAL;
  6757. goto fail_locked;
  6758. }
  6759. /* Note that the w/a also requires 2 PTE of padding following
  6760. * the bo. We currently fill all unused PTE with the shadow
  6761. * page and so we should always have valid PTE following the
  6762. * cursor preventing the VT-d warning.
  6763. */
  6764. alignment = 0;
  6765. if (need_vtd_wa(dev))
  6766. alignment = 64*1024;
  6767. ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
  6768. if (ret) {
  6769. DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
  6770. goto fail_locked;
  6771. }
  6772. ret = i915_gem_object_put_fence(obj);
  6773. if (ret) {
  6774. DRM_DEBUG_KMS("failed to release fence for cursor");
  6775. goto fail_unpin;
  6776. }
  6777. addr = i915_gem_obj_ggtt_offset(obj);
  6778. } else {
  6779. int align = IS_I830(dev) ? 16 * 1024 : 256;
  6780. ret = i915_gem_object_attach_phys(obj, align);
  6781. if (ret) {
  6782. DRM_DEBUG_KMS("failed to attach phys object\n");
  6783. goto fail_locked;
  6784. }
  6785. addr = obj->phys_handle->busaddr;
  6786. }
  6787. if (IS_GEN2(dev))
  6788. I915_WRITE(CURSIZE, (height << 12) | width);
  6789. finish:
  6790. if (intel_crtc->cursor_bo) {
  6791. if (!INTEL_INFO(dev)->cursor_needs_physical)
  6792. i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
  6793. drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
  6794. }
  6795. mutex_unlock(&dev->struct_mutex);
  6796. old_width = intel_crtc->cursor_width;
  6797. intel_crtc->cursor_addr = addr;
  6798. intel_crtc->cursor_bo = obj;
  6799. intel_crtc->cursor_width = width;
  6800. intel_crtc->cursor_height = height;
  6801. if (intel_crtc->active) {
  6802. if (old_width != width)
  6803. intel_update_watermarks(crtc);
  6804. intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
  6805. }
  6806. return 0;
  6807. fail_unpin:
  6808. i915_gem_object_unpin_from_display_plane(obj);
  6809. fail_locked:
  6810. mutex_unlock(&dev->struct_mutex);
  6811. fail:
  6812. drm_gem_object_unreference_unlocked(&obj->base);
  6813. return ret;
  6814. }
  6815. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  6816. {
  6817. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6818. intel_crtc->cursor_x = clamp_t(int, x, SHRT_MIN, SHRT_MAX);
  6819. intel_crtc->cursor_y = clamp_t(int, y, SHRT_MIN, SHRT_MAX);
  6820. if (intel_crtc->active)
  6821. intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
  6822. return 0;
  6823. }
  6824. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  6825. u16 *blue, uint32_t start, uint32_t size)
  6826. {
  6827. int end = (start + size > 256) ? 256 : start + size, i;
  6828. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6829. for (i = start; i < end; i++) {
  6830. intel_crtc->lut_r[i] = red[i] >> 8;
  6831. intel_crtc->lut_g[i] = green[i] >> 8;
  6832. intel_crtc->lut_b[i] = blue[i] >> 8;
  6833. }
  6834. intel_crtc_load_lut(crtc);
  6835. }
  6836. /* VESA 640x480x72Hz mode to set on the pipe */
  6837. static struct drm_display_mode load_detect_mode = {
  6838. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  6839. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  6840. };
  6841. struct drm_framebuffer *
  6842. __intel_framebuffer_create(struct drm_device *dev,
  6843. struct drm_mode_fb_cmd2 *mode_cmd,
  6844. struct drm_i915_gem_object *obj)
  6845. {
  6846. struct intel_framebuffer *intel_fb;
  6847. int ret;
  6848. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6849. if (!intel_fb) {
  6850. drm_gem_object_unreference_unlocked(&obj->base);
  6851. return ERR_PTR(-ENOMEM);
  6852. }
  6853. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  6854. if (ret)
  6855. goto err;
  6856. return &intel_fb->base;
  6857. err:
  6858. drm_gem_object_unreference_unlocked(&obj->base);
  6859. kfree(intel_fb);
  6860. return ERR_PTR(ret);
  6861. }
  6862. static struct drm_framebuffer *
  6863. intel_framebuffer_create(struct drm_device *dev,
  6864. struct drm_mode_fb_cmd2 *mode_cmd,
  6865. struct drm_i915_gem_object *obj)
  6866. {
  6867. struct drm_framebuffer *fb;
  6868. int ret;
  6869. ret = i915_mutex_lock_interruptible(dev);
  6870. if (ret)
  6871. return ERR_PTR(ret);
  6872. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  6873. mutex_unlock(&dev->struct_mutex);
  6874. return fb;
  6875. }
  6876. static u32
  6877. intel_framebuffer_pitch_for_width(int width, int bpp)
  6878. {
  6879. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  6880. return ALIGN(pitch, 64);
  6881. }
  6882. static u32
  6883. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  6884. {
  6885. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  6886. return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
  6887. }
  6888. static struct drm_framebuffer *
  6889. intel_framebuffer_create_for_mode(struct drm_device *dev,
  6890. struct drm_display_mode *mode,
  6891. int depth, int bpp)
  6892. {
  6893. struct drm_i915_gem_object *obj;
  6894. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  6895. obj = i915_gem_alloc_object(dev,
  6896. intel_framebuffer_size_for_mode(mode, bpp));
  6897. if (obj == NULL)
  6898. return ERR_PTR(-ENOMEM);
  6899. mode_cmd.width = mode->hdisplay;
  6900. mode_cmd.height = mode->vdisplay;
  6901. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  6902. bpp);
  6903. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  6904. return intel_framebuffer_create(dev, &mode_cmd, obj);
  6905. }
  6906. static struct drm_framebuffer *
  6907. mode_fits_in_fbdev(struct drm_device *dev,
  6908. struct drm_display_mode *mode)
  6909. {
  6910. #ifdef CONFIG_DRM_I915_FBDEV
  6911. struct drm_i915_private *dev_priv = dev->dev_private;
  6912. struct drm_i915_gem_object *obj;
  6913. struct drm_framebuffer *fb;
  6914. if (!dev_priv->fbdev)
  6915. return NULL;
  6916. if (!dev_priv->fbdev->fb)
  6917. return NULL;
  6918. obj = dev_priv->fbdev->fb->obj;
  6919. BUG_ON(!obj);
  6920. fb = &dev_priv->fbdev->fb->base;
  6921. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  6922. fb->bits_per_pixel))
  6923. return NULL;
  6924. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  6925. return NULL;
  6926. return fb;
  6927. #else
  6928. return NULL;
  6929. #endif
  6930. }
  6931. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  6932. struct drm_display_mode *mode,
  6933. struct intel_load_detect_pipe *old,
  6934. struct drm_modeset_acquire_ctx *ctx)
  6935. {
  6936. struct intel_crtc *intel_crtc;
  6937. struct intel_encoder *intel_encoder =
  6938. intel_attached_encoder(connector);
  6939. struct drm_crtc *possible_crtc;
  6940. struct drm_encoder *encoder = &intel_encoder->base;
  6941. struct drm_crtc *crtc = NULL;
  6942. struct drm_device *dev = encoder->dev;
  6943. struct drm_framebuffer *fb;
  6944. struct drm_mode_config *config = &dev->mode_config;
  6945. int ret, i = -1;
  6946. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  6947. connector->base.id, connector->name,
  6948. encoder->base.id, encoder->name);
  6949. drm_modeset_acquire_init(ctx, 0);
  6950. retry:
  6951. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  6952. if (ret)
  6953. goto fail_unlock;
  6954. /*
  6955. * Algorithm gets a little messy:
  6956. *
  6957. * - if the connector already has an assigned crtc, use it (but make
  6958. * sure it's on first)
  6959. *
  6960. * - try to find the first unused crtc that can drive this connector,
  6961. * and use that if we find one
  6962. */
  6963. /* See if we already have a CRTC for this connector */
  6964. if (encoder->crtc) {
  6965. crtc = encoder->crtc;
  6966. ret = drm_modeset_lock(&crtc->mutex, ctx);
  6967. if (ret)
  6968. goto fail_unlock;
  6969. old->dpms_mode = connector->dpms;
  6970. old->load_detect_temp = false;
  6971. /* Make sure the crtc and connector are running */
  6972. if (connector->dpms != DRM_MODE_DPMS_ON)
  6973. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  6974. return true;
  6975. }
  6976. /* Find an unused one (if possible) */
  6977. for_each_crtc(dev, possible_crtc) {
  6978. i++;
  6979. if (!(encoder->possible_crtcs & (1 << i)))
  6980. continue;
  6981. if (!possible_crtc->enabled) {
  6982. crtc = possible_crtc;
  6983. break;
  6984. }
  6985. }
  6986. /*
  6987. * If we didn't find an unused CRTC, don't use any.
  6988. */
  6989. if (!crtc) {
  6990. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  6991. goto fail_unlock;
  6992. }
  6993. ret = drm_modeset_lock(&crtc->mutex, ctx);
  6994. if (ret)
  6995. goto fail_unlock;
  6996. intel_encoder->new_crtc = to_intel_crtc(crtc);
  6997. to_intel_connector(connector)->new_encoder = intel_encoder;
  6998. intel_crtc = to_intel_crtc(crtc);
  6999. intel_crtc->new_enabled = true;
  7000. intel_crtc->new_config = &intel_crtc->config;
  7001. old->dpms_mode = connector->dpms;
  7002. old->load_detect_temp = true;
  7003. old->release_fb = NULL;
  7004. if (!mode)
  7005. mode = &load_detect_mode;
  7006. /* We need a framebuffer large enough to accommodate all accesses
  7007. * that the plane may generate whilst we perform load detection.
  7008. * We can not rely on the fbcon either being present (we get called
  7009. * during its initialisation to detect all boot displays, or it may
  7010. * not even exist) or that it is large enough to satisfy the
  7011. * requested mode.
  7012. */
  7013. fb = mode_fits_in_fbdev(dev, mode);
  7014. if (fb == NULL) {
  7015. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  7016. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  7017. old->release_fb = fb;
  7018. } else
  7019. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  7020. if (IS_ERR(fb)) {
  7021. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  7022. goto fail;
  7023. }
  7024. if (intel_set_mode(crtc, mode, 0, 0, fb)) {
  7025. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  7026. if (old->release_fb)
  7027. old->release_fb->funcs->destroy(old->release_fb);
  7028. goto fail;
  7029. }
  7030. /* let the connector get through one full cycle before testing */
  7031. intel_wait_for_vblank(dev, intel_crtc->pipe);
  7032. return true;
  7033. fail:
  7034. intel_crtc->new_enabled = crtc->enabled;
  7035. if (intel_crtc->new_enabled)
  7036. intel_crtc->new_config = &intel_crtc->config;
  7037. else
  7038. intel_crtc->new_config = NULL;
  7039. fail_unlock:
  7040. if (ret == -EDEADLK) {
  7041. drm_modeset_backoff(ctx);
  7042. goto retry;
  7043. }
  7044. drm_modeset_drop_locks(ctx);
  7045. drm_modeset_acquire_fini(ctx);
  7046. return false;
  7047. }
  7048. void intel_release_load_detect_pipe(struct drm_connector *connector,
  7049. struct intel_load_detect_pipe *old,
  7050. struct drm_modeset_acquire_ctx *ctx)
  7051. {
  7052. struct intel_encoder *intel_encoder =
  7053. intel_attached_encoder(connector);
  7054. struct drm_encoder *encoder = &intel_encoder->base;
  7055. struct drm_crtc *crtc = encoder->crtc;
  7056. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7057. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7058. connector->base.id, connector->name,
  7059. encoder->base.id, encoder->name);
  7060. if (old->load_detect_temp) {
  7061. to_intel_connector(connector)->new_encoder = NULL;
  7062. intel_encoder->new_crtc = NULL;
  7063. intel_crtc->new_enabled = false;
  7064. intel_crtc->new_config = NULL;
  7065. intel_set_mode(crtc, NULL, 0, 0, NULL);
  7066. if (old->release_fb) {
  7067. drm_framebuffer_unregister_private(old->release_fb);
  7068. drm_framebuffer_unreference(old->release_fb);
  7069. }
  7070. goto unlock;
  7071. return;
  7072. }
  7073. /* Switch crtc and encoder back off if necessary */
  7074. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  7075. connector->funcs->dpms(connector, old->dpms_mode);
  7076. unlock:
  7077. drm_modeset_drop_locks(ctx);
  7078. drm_modeset_acquire_fini(ctx);
  7079. }
  7080. static int i9xx_pll_refclk(struct drm_device *dev,
  7081. const struct intel_crtc_config *pipe_config)
  7082. {
  7083. struct drm_i915_private *dev_priv = dev->dev_private;
  7084. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7085. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  7086. return dev_priv->vbt.lvds_ssc_freq;
  7087. else if (HAS_PCH_SPLIT(dev))
  7088. return 120000;
  7089. else if (!IS_GEN2(dev))
  7090. return 96000;
  7091. else
  7092. return 48000;
  7093. }
  7094. /* Returns the clock of the currently programmed mode of the given pipe. */
  7095. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  7096. struct intel_crtc_config *pipe_config)
  7097. {
  7098. struct drm_device *dev = crtc->base.dev;
  7099. struct drm_i915_private *dev_priv = dev->dev_private;
  7100. int pipe = pipe_config->cpu_transcoder;
  7101. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7102. u32 fp;
  7103. intel_clock_t clock;
  7104. int refclk = i9xx_pll_refclk(dev, pipe_config);
  7105. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  7106. fp = pipe_config->dpll_hw_state.fp0;
  7107. else
  7108. fp = pipe_config->dpll_hw_state.fp1;
  7109. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  7110. if (IS_PINEVIEW(dev)) {
  7111. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  7112. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7113. } else {
  7114. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  7115. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7116. }
  7117. if (!IS_GEN2(dev)) {
  7118. if (IS_PINEVIEW(dev))
  7119. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  7120. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  7121. else
  7122. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  7123. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7124. switch (dpll & DPLL_MODE_MASK) {
  7125. case DPLLB_MODE_DAC_SERIAL:
  7126. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  7127. 5 : 10;
  7128. break;
  7129. case DPLLB_MODE_LVDS:
  7130. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  7131. 7 : 14;
  7132. break;
  7133. default:
  7134. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  7135. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  7136. return;
  7137. }
  7138. if (IS_PINEVIEW(dev))
  7139. pineview_clock(refclk, &clock);
  7140. else
  7141. i9xx_clock(refclk, &clock);
  7142. } else {
  7143. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  7144. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  7145. if (is_lvds) {
  7146. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  7147. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7148. if (lvds & LVDS_CLKB_POWER_UP)
  7149. clock.p2 = 7;
  7150. else
  7151. clock.p2 = 14;
  7152. } else {
  7153. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  7154. clock.p1 = 2;
  7155. else {
  7156. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  7157. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  7158. }
  7159. if (dpll & PLL_P2_DIVIDE_BY_4)
  7160. clock.p2 = 4;
  7161. else
  7162. clock.p2 = 2;
  7163. }
  7164. i9xx_clock(refclk, &clock);
  7165. }
  7166. /*
  7167. * This value includes pixel_multiplier. We will use
  7168. * port_clock to compute adjusted_mode.crtc_clock in the
  7169. * encoder's get_config() function.
  7170. */
  7171. pipe_config->port_clock = clock.dot;
  7172. }
  7173. int intel_dotclock_calculate(int link_freq,
  7174. const struct intel_link_m_n *m_n)
  7175. {
  7176. /*
  7177. * The calculation for the data clock is:
  7178. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  7179. * But we want to avoid losing precison if possible, so:
  7180. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  7181. *
  7182. * and the link clock is simpler:
  7183. * link_clock = (m * link_clock) / n
  7184. */
  7185. if (!m_n->link_n)
  7186. return 0;
  7187. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  7188. }
  7189. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  7190. struct intel_crtc_config *pipe_config)
  7191. {
  7192. struct drm_device *dev = crtc->base.dev;
  7193. /* read out port_clock from the DPLL */
  7194. i9xx_crtc_clock_get(crtc, pipe_config);
  7195. /*
  7196. * This value does not include pixel_multiplier.
  7197. * We will check that port_clock and adjusted_mode.crtc_clock
  7198. * agree once we know their relationship in the encoder's
  7199. * get_config() function.
  7200. */
  7201. pipe_config->adjusted_mode.crtc_clock =
  7202. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  7203. &pipe_config->fdi_m_n);
  7204. }
  7205. /** Returns the currently programmed mode of the given pipe. */
  7206. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  7207. struct drm_crtc *crtc)
  7208. {
  7209. struct drm_i915_private *dev_priv = dev->dev_private;
  7210. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7211. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  7212. struct drm_display_mode *mode;
  7213. struct intel_crtc_config pipe_config;
  7214. int htot = I915_READ(HTOTAL(cpu_transcoder));
  7215. int hsync = I915_READ(HSYNC(cpu_transcoder));
  7216. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  7217. int vsync = I915_READ(VSYNC(cpu_transcoder));
  7218. enum pipe pipe = intel_crtc->pipe;
  7219. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  7220. if (!mode)
  7221. return NULL;
  7222. /*
  7223. * Construct a pipe_config sufficient for getting the clock info
  7224. * back out of crtc_clock_get.
  7225. *
  7226. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  7227. * to use a real value here instead.
  7228. */
  7229. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  7230. pipe_config.pixel_multiplier = 1;
  7231. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  7232. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  7233. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  7234. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  7235. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  7236. mode->hdisplay = (htot & 0xffff) + 1;
  7237. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  7238. mode->hsync_start = (hsync & 0xffff) + 1;
  7239. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  7240. mode->vdisplay = (vtot & 0xffff) + 1;
  7241. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  7242. mode->vsync_start = (vsync & 0xffff) + 1;
  7243. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  7244. drm_mode_set_name(mode);
  7245. return mode;
  7246. }
  7247. static void intel_increase_pllclock(struct drm_crtc *crtc)
  7248. {
  7249. struct drm_device *dev = crtc->dev;
  7250. struct drm_i915_private *dev_priv = dev->dev_private;
  7251. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7252. int pipe = intel_crtc->pipe;
  7253. int dpll_reg = DPLL(pipe);
  7254. int dpll;
  7255. if (HAS_PCH_SPLIT(dev))
  7256. return;
  7257. if (!dev_priv->lvds_downclock_avail)
  7258. return;
  7259. dpll = I915_READ(dpll_reg);
  7260. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  7261. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  7262. assert_panel_unlocked(dev_priv, pipe);
  7263. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  7264. I915_WRITE(dpll_reg, dpll);
  7265. intel_wait_for_vblank(dev, pipe);
  7266. dpll = I915_READ(dpll_reg);
  7267. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  7268. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  7269. }
  7270. }
  7271. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  7272. {
  7273. struct drm_device *dev = crtc->dev;
  7274. struct drm_i915_private *dev_priv = dev->dev_private;
  7275. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7276. if (HAS_PCH_SPLIT(dev))
  7277. return;
  7278. if (!dev_priv->lvds_downclock_avail)
  7279. return;
  7280. /*
  7281. * Since this is called by a timer, we should never get here in
  7282. * the manual case.
  7283. */
  7284. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  7285. int pipe = intel_crtc->pipe;
  7286. int dpll_reg = DPLL(pipe);
  7287. int dpll;
  7288. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  7289. assert_panel_unlocked(dev_priv, pipe);
  7290. dpll = I915_READ(dpll_reg);
  7291. dpll |= DISPLAY_RATE_SELECT_FPA1;
  7292. I915_WRITE(dpll_reg, dpll);
  7293. intel_wait_for_vblank(dev, pipe);
  7294. dpll = I915_READ(dpll_reg);
  7295. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  7296. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  7297. }
  7298. }
  7299. void intel_mark_busy(struct drm_device *dev)
  7300. {
  7301. struct drm_i915_private *dev_priv = dev->dev_private;
  7302. if (dev_priv->mm.busy)
  7303. return;
  7304. intel_runtime_pm_get(dev_priv);
  7305. i915_update_gfx_val(dev_priv);
  7306. dev_priv->mm.busy = true;
  7307. }
  7308. void intel_mark_idle(struct drm_device *dev)
  7309. {
  7310. struct drm_i915_private *dev_priv = dev->dev_private;
  7311. struct drm_crtc *crtc;
  7312. if (!dev_priv->mm.busy)
  7313. return;
  7314. dev_priv->mm.busy = false;
  7315. if (!i915.powersave)
  7316. goto out;
  7317. for_each_crtc(dev, crtc) {
  7318. if (!crtc->primary->fb)
  7319. continue;
  7320. intel_decrease_pllclock(crtc);
  7321. }
  7322. if (INTEL_INFO(dev)->gen >= 6)
  7323. gen6_rps_idle(dev->dev_private);
  7324. out:
  7325. intel_runtime_pm_put(dev_priv);
  7326. }
  7327. void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
  7328. struct intel_engine_cs *ring)
  7329. {
  7330. struct drm_device *dev = obj->base.dev;
  7331. struct drm_crtc *crtc;
  7332. if (!i915.powersave)
  7333. return;
  7334. for_each_crtc(dev, crtc) {
  7335. if (!crtc->primary->fb)
  7336. continue;
  7337. if (to_intel_framebuffer(crtc->primary->fb)->obj != obj)
  7338. continue;
  7339. intel_increase_pllclock(crtc);
  7340. if (ring && intel_fbc_enabled(dev))
  7341. ring->fbc_dirty = true;
  7342. }
  7343. }
  7344. static void intel_crtc_destroy(struct drm_crtc *crtc)
  7345. {
  7346. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7347. struct drm_device *dev = crtc->dev;
  7348. struct intel_unpin_work *work;
  7349. unsigned long flags;
  7350. spin_lock_irqsave(&dev->event_lock, flags);
  7351. work = intel_crtc->unpin_work;
  7352. intel_crtc->unpin_work = NULL;
  7353. spin_unlock_irqrestore(&dev->event_lock, flags);
  7354. if (work) {
  7355. cancel_work_sync(&work->work);
  7356. kfree(work);
  7357. }
  7358. intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
  7359. drm_crtc_cleanup(crtc);
  7360. kfree(intel_crtc);
  7361. }
  7362. static void intel_unpin_work_fn(struct work_struct *__work)
  7363. {
  7364. struct intel_unpin_work *work =
  7365. container_of(__work, struct intel_unpin_work, work);
  7366. struct drm_device *dev = work->crtc->dev;
  7367. mutex_lock(&dev->struct_mutex);
  7368. intel_unpin_fb_obj(work->old_fb_obj);
  7369. drm_gem_object_unreference(&work->pending_flip_obj->base);
  7370. drm_gem_object_unreference(&work->old_fb_obj->base);
  7371. intel_update_fbc(dev);
  7372. mutex_unlock(&dev->struct_mutex);
  7373. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  7374. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  7375. kfree(work);
  7376. }
  7377. static void do_intel_finish_page_flip(struct drm_device *dev,
  7378. struct drm_crtc *crtc)
  7379. {
  7380. struct drm_i915_private *dev_priv = dev->dev_private;
  7381. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7382. struct intel_unpin_work *work;
  7383. unsigned long flags;
  7384. /* Ignore early vblank irqs */
  7385. if (intel_crtc == NULL)
  7386. return;
  7387. spin_lock_irqsave(&dev->event_lock, flags);
  7388. work = intel_crtc->unpin_work;
  7389. /* Ensure we don't miss a work->pending update ... */
  7390. smp_rmb();
  7391. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  7392. spin_unlock_irqrestore(&dev->event_lock, flags);
  7393. return;
  7394. }
  7395. /* and that the unpin work is consistent wrt ->pending. */
  7396. smp_rmb();
  7397. intel_crtc->unpin_work = NULL;
  7398. if (work->event)
  7399. drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
  7400. drm_crtc_vblank_put(crtc);
  7401. spin_unlock_irqrestore(&dev->event_lock, flags);
  7402. wake_up_all(&dev_priv->pending_flip_queue);
  7403. queue_work(dev_priv->wq, &work->work);
  7404. trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
  7405. }
  7406. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  7407. {
  7408. struct drm_i915_private *dev_priv = dev->dev_private;
  7409. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  7410. do_intel_finish_page_flip(dev, crtc);
  7411. }
  7412. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  7413. {
  7414. struct drm_i915_private *dev_priv = dev->dev_private;
  7415. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  7416. do_intel_finish_page_flip(dev, crtc);
  7417. }
  7418. /* Is 'a' after or equal to 'b'? */
  7419. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  7420. {
  7421. return !((a - b) & 0x80000000);
  7422. }
  7423. static bool page_flip_finished(struct intel_crtc *crtc)
  7424. {
  7425. struct drm_device *dev = crtc->base.dev;
  7426. struct drm_i915_private *dev_priv = dev->dev_private;
  7427. /*
  7428. * The relevant registers doen't exist on pre-ctg.
  7429. * As the flip done interrupt doesn't trigger for mmio
  7430. * flips on gmch platforms, a flip count check isn't
  7431. * really needed there. But since ctg has the registers,
  7432. * include it in the check anyway.
  7433. */
  7434. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  7435. return true;
  7436. /*
  7437. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  7438. * used the same base address. In that case the mmio flip might
  7439. * have completed, but the CS hasn't even executed the flip yet.
  7440. *
  7441. * A flip count check isn't enough as the CS might have updated
  7442. * the base address just after start of vblank, but before we
  7443. * managed to process the interrupt. This means we'd complete the
  7444. * CS flip too soon.
  7445. *
  7446. * Combining both checks should get us a good enough result. It may
  7447. * still happen that the CS flip has been executed, but has not
  7448. * yet actually completed. But in case the base address is the same
  7449. * anyway, we don't really care.
  7450. */
  7451. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  7452. crtc->unpin_work->gtt_offset &&
  7453. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  7454. crtc->unpin_work->flip_count);
  7455. }
  7456. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  7457. {
  7458. struct drm_i915_private *dev_priv = dev->dev_private;
  7459. struct intel_crtc *intel_crtc =
  7460. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  7461. unsigned long flags;
  7462. /* NB: An MMIO update of the plane base pointer will also
  7463. * generate a page-flip completion irq, i.e. every modeset
  7464. * is also accompanied by a spurious intel_prepare_page_flip().
  7465. */
  7466. spin_lock_irqsave(&dev->event_lock, flags);
  7467. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  7468. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  7469. spin_unlock_irqrestore(&dev->event_lock, flags);
  7470. }
  7471. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  7472. {
  7473. /* Ensure that the work item is consistent when activating it ... */
  7474. smp_wmb();
  7475. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  7476. /* and that it is marked active as soon as the irq could fire. */
  7477. smp_wmb();
  7478. }
  7479. static int intel_gen2_queue_flip(struct drm_device *dev,
  7480. struct drm_crtc *crtc,
  7481. struct drm_framebuffer *fb,
  7482. struct drm_i915_gem_object *obj,
  7483. struct intel_engine_cs *ring,
  7484. uint32_t flags)
  7485. {
  7486. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7487. u32 flip_mask;
  7488. int ret;
  7489. ret = intel_ring_begin(ring, 6);
  7490. if (ret)
  7491. return ret;
  7492. /* Can't queue multiple flips, so wait for the previous
  7493. * one to finish before executing the next.
  7494. */
  7495. if (intel_crtc->plane)
  7496. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7497. else
  7498. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7499. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7500. intel_ring_emit(ring, MI_NOOP);
  7501. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7502. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7503. intel_ring_emit(ring, fb->pitches[0]);
  7504. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7505. intel_ring_emit(ring, 0); /* aux display base address, unused */
  7506. intel_mark_page_flip_active(intel_crtc);
  7507. __intel_ring_advance(ring);
  7508. return 0;
  7509. }
  7510. static int intel_gen3_queue_flip(struct drm_device *dev,
  7511. struct drm_crtc *crtc,
  7512. struct drm_framebuffer *fb,
  7513. struct drm_i915_gem_object *obj,
  7514. struct intel_engine_cs *ring,
  7515. uint32_t flags)
  7516. {
  7517. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7518. u32 flip_mask;
  7519. int ret;
  7520. ret = intel_ring_begin(ring, 6);
  7521. if (ret)
  7522. return ret;
  7523. if (intel_crtc->plane)
  7524. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7525. else
  7526. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7527. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7528. intel_ring_emit(ring, MI_NOOP);
  7529. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  7530. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7531. intel_ring_emit(ring, fb->pitches[0]);
  7532. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7533. intel_ring_emit(ring, MI_NOOP);
  7534. intel_mark_page_flip_active(intel_crtc);
  7535. __intel_ring_advance(ring);
  7536. return 0;
  7537. }
  7538. static int intel_gen4_queue_flip(struct drm_device *dev,
  7539. struct drm_crtc *crtc,
  7540. struct drm_framebuffer *fb,
  7541. struct drm_i915_gem_object *obj,
  7542. struct intel_engine_cs *ring,
  7543. uint32_t flags)
  7544. {
  7545. struct drm_i915_private *dev_priv = dev->dev_private;
  7546. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7547. uint32_t pf, pipesrc;
  7548. int ret;
  7549. ret = intel_ring_begin(ring, 4);
  7550. if (ret)
  7551. return ret;
  7552. /* i965+ uses the linear or tiled offsets from the
  7553. * Display Registers (which do not change across a page-flip)
  7554. * so we need only reprogram the base address.
  7555. */
  7556. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7557. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7558. intel_ring_emit(ring, fb->pitches[0]);
  7559. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  7560. obj->tiling_mode);
  7561. /* XXX Enabling the panel-fitter across page-flip is so far
  7562. * untested on non-native modes, so ignore it for now.
  7563. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  7564. */
  7565. pf = 0;
  7566. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7567. intel_ring_emit(ring, pf | pipesrc);
  7568. intel_mark_page_flip_active(intel_crtc);
  7569. __intel_ring_advance(ring);
  7570. return 0;
  7571. }
  7572. static int intel_gen6_queue_flip(struct drm_device *dev,
  7573. struct drm_crtc *crtc,
  7574. struct drm_framebuffer *fb,
  7575. struct drm_i915_gem_object *obj,
  7576. struct intel_engine_cs *ring,
  7577. uint32_t flags)
  7578. {
  7579. struct drm_i915_private *dev_priv = dev->dev_private;
  7580. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7581. uint32_t pf, pipesrc;
  7582. int ret;
  7583. ret = intel_ring_begin(ring, 4);
  7584. if (ret)
  7585. return ret;
  7586. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7587. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7588. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  7589. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7590. /* Contrary to the suggestions in the documentation,
  7591. * "Enable Panel Fitter" does not seem to be required when page
  7592. * flipping with a non-native mode, and worse causes a normal
  7593. * modeset to fail.
  7594. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  7595. */
  7596. pf = 0;
  7597. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7598. intel_ring_emit(ring, pf | pipesrc);
  7599. intel_mark_page_flip_active(intel_crtc);
  7600. __intel_ring_advance(ring);
  7601. return 0;
  7602. }
  7603. static int intel_gen7_queue_flip(struct drm_device *dev,
  7604. struct drm_crtc *crtc,
  7605. struct drm_framebuffer *fb,
  7606. struct drm_i915_gem_object *obj,
  7607. struct intel_engine_cs *ring,
  7608. uint32_t flags)
  7609. {
  7610. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7611. uint32_t plane_bit = 0;
  7612. int len, ret;
  7613. switch (intel_crtc->plane) {
  7614. case PLANE_A:
  7615. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  7616. break;
  7617. case PLANE_B:
  7618. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  7619. break;
  7620. case PLANE_C:
  7621. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  7622. break;
  7623. default:
  7624. WARN_ONCE(1, "unknown plane in flip command\n");
  7625. return -ENODEV;
  7626. }
  7627. len = 4;
  7628. if (ring->id == RCS) {
  7629. len += 6;
  7630. /*
  7631. * On Gen 8, SRM is now taking an extra dword to accommodate
  7632. * 48bits addresses, and we need a NOOP for the batch size to
  7633. * stay even.
  7634. */
  7635. if (IS_GEN8(dev))
  7636. len += 2;
  7637. }
  7638. /*
  7639. * BSpec MI_DISPLAY_FLIP for IVB:
  7640. * "The full packet must be contained within the same cache line."
  7641. *
  7642. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  7643. * cacheline, if we ever start emitting more commands before
  7644. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  7645. * then do the cacheline alignment, and finally emit the
  7646. * MI_DISPLAY_FLIP.
  7647. */
  7648. ret = intel_ring_cacheline_align(ring);
  7649. if (ret)
  7650. return ret;
  7651. ret = intel_ring_begin(ring, len);
  7652. if (ret)
  7653. return ret;
  7654. /* Unmask the flip-done completion message. Note that the bspec says that
  7655. * we should do this for both the BCS and RCS, and that we must not unmask
  7656. * more than one flip event at any time (or ensure that one flip message
  7657. * can be sent by waiting for flip-done prior to queueing new flips).
  7658. * Experimentation says that BCS works despite DERRMR masking all
  7659. * flip-done completion events and that unmasking all planes at once
  7660. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  7661. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  7662. */
  7663. if (ring->id == RCS) {
  7664. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  7665. intel_ring_emit(ring, DERRMR);
  7666. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  7667. DERRMR_PIPEB_PRI_FLIP_DONE |
  7668. DERRMR_PIPEC_PRI_FLIP_DONE));
  7669. if (IS_GEN8(dev))
  7670. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  7671. MI_SRM_LRM_GLOBAL_GTT);
  7672. else
  7673. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  7674. MI_SRM_LRM_GLOBAL_GTT);
  7675. intel_ring_emit(ring, DERRMR);
  7676. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  7677. if (IS_GEN8(dev)) {
  7678. intel_ring_emit(ring, 0);
  7679. intel_ring_emit(ring, MI_NOOP);
  7680. }
  7681. }
  7682. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  7683. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  7684. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7685. intel_ring_emit(ring, (MI_NOOP));
  7686. intel_mark_page_flip_active(intel_crtc);
  7687. __intel_ring_advance(ring);
  7688. return 0;
  7689. }
  7690. static int intel_default_queue_flip(struct drm_device *dev,
  7691. struct drm_crtc *crtc,
  7692. struct drm_framebuffer *fb,
  7693. struct drm_i915_gem_object *obj,
  7694. struct intel_engine_cs *ring,
  7695. uint32_t flags)
  7696. {
  7697. return -ENODEV;
  7698. }
  7699. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  7700. struct drm_framebuffer *fb,
  7701. struct drm_pending_vblank_event *event,
  7702. uint32_t page_flip_flags)
  7703. {
  7704. struct drm_device *dev = crtc->dev;
  7705. struct drm_i915_private *dev_priv = dev->dev_private;
  7706. struct drm_framebuffer *old_fb = crtc->primary->fb;
  7707. struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
  7708. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7709. struct intel_unpin_work *work;
  7710. struct intel_engine_cs *ring;
  7711. unsigned long flags;
  7712. int ret;
  7713. /* Can't change pixel format via MI display flips. */
  7714. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  7715. return -EINVAL;
  7716. /*
  7717. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  7718. * Note that pitch changes could also affect these register.
  7719. */
  7720. if (INTEL_INFO(dev)->gen > 3 &&
  7721. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  7722. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  7723. return -EINVAL;
  7724. if (i915_terminally_wedged(&dev_priv->gpu_error))
  7725. goto out_hang;
  7726. work = kzalloc(sizeof(*work), GFP_KERNEL);
  7727. if (work == NULL)
  7728. return -ENOMEM;
  7729. work->event = event;
  7730. work->crtc = crtc;
  7731. work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
  7732. INIT_WORK(&work->work, intel_unpin_work_fn);
  7733. ret = drm_crtc_vblank_get(crtc);
  7734. if (ret)
  7735. goto free_work;
  7736. /* We borrow the event spin lock for protecting unpin_work */
  7737. spin_lock_irqsave(&dev->event_lock, flags);
  7738. if (intel_crtc->unpin_work) {
  7739. spin_unlock_irqrestore(&dev->event_lock, flags);
  7740. kfree(work);
  7741. drm_crtc_vblank_put(crtc);
  7742. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  7743. return -EBUSY;
  7744. }
  7745. intel_crtc->unpin_work = work;
  7746. spin_unlock_irqrestore(&dev->event_lock, flags);
  7747. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  7748. flush_workqueue(dev_priv->wq);
  7749. ret = i915_mutex_lock_interruptible(dev);
  7750. if (ret)
  7751. goto cleanup;
  7752. /* Reference the objects for the scheduled work. */
  7753. drm_gem_object_reference(&work->old_fb_obj->base);
  7754. drm_gem_object_reference(&obj->base);
  7755. crtc->primary->fb = fb;
  7756. work->pending_flip_obj = obj;
  7757. work->enable_stall_check = true;
  7758. atomic_inc(&intel_crtc->unpin_work_count);
  7759. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  7760. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  7761. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(intel_crtc->pipe)) + 1;
  7762. if (IS_VALLEYVIEW(dev)) {
  7763. ring = &dev_priv->ring[BCS];
  7764. } else if (INTEL_INFO(dev)->gen >= 7) {
  7765. ring = obj->ring;
  7766. if (ring == NULL || ring->id != RCS)
  7767. ring = &dev_priv->ring[BCS];
  7768. } else {
  7769. ring = &dev_priv->ring[RCS];
  7770. }
  7771. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  7772. if (ret)
  7773. goto cleanup_pending;
  7774. work->gtt_offset =
  7775. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
  7776. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring, page_flip_flags);
  7777. if (ret)
  7778. goto cleanup_unpin;
  7779. intel_disable_fbc(dev);
  7780. intel_mark_fb_busy(obj, NULL);
  7781. mutex_unlock(&dev->struct_mutex);
  7782. trace_i915_flip_request(intel_crtc->plane, obj);
  7783. return 0;
  7784. cleanup_unpin:
  7785. intel_unpin_fb_obj(obj);
  7786. cleanup_pending:
  7787. atomic_dec(&intel_crtc->unpin_work_count);
  7788. crtc->primary->fb = old_fb;
  7789. drm_gem_object_unreference(&work->old_fb_obj->base);
  7790. drm_gem_object_unreference(&obj->base);
  7791. mutex_unlock(&dev->struct_mutex);
  7792. cleanup:
  7793. spin_lock_irqsave(&dev->event_lock, flags);
  7794. intel_crtc->unpin_work = NULL;
  7795. spin_unlock_irqrestore(&dev->event_lock, flags);
  7796. drm_crtc_vblank_put(crtc);
  7797. free_work:
  7798. kfree(work);
  7799. if (ret == -EIO) {
  7800. out_hang:
  7801. intel_crtc_wait_for_pending_flips(crtc);
  7802. ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
  7803. if (ret == 0 && event)
  7804. drm_send_vblank_event(dev, intel_crtc->pipe, event);
  7805. }
  7806. return ret;
  7807. }
  7808. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  7809. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  7810. .load_lut = intel_crtc_load_lut,
  7811. };
  7812. /**
  7813. * intel_modeset_update_staged_output_state
  7814. *
  7815. * Updates the staged output configuration state, e.g. after we've read out the
  7816. * current hw state.
  7817. */
  7818. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  7819. {
  7820. struct intel_crtc *crtc;
  7821. struct intel_encoder *encoder;
  7822. struct intel_connector *connector;
  7823. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7824. base.head) {
  7825. connector->new_encoder =
  7826. to_intel_encoder(connector->base.encoder);
  7827. }
  7828. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  7829. base.head) {
  7830. encoder->new_crtc =
  7831. to_intel_crtc(encoder->base.crtc);
  7832. }
  7833. for_each_intel_crtc(dev, crtc) {
  7834. crtc->new_enabled = crtc->base.enabled;
  7835. if (crtc->new_enabled)
  7836. crtc->new_config = &crtc->config;
  7837. else
  7838. crtc->new_config = NULL;
  7839. }
  7840. }
  7841. /**
  7842. * intel_modeset_commit_output_state
  7843. *
  7844. * This function copies the stage display pipe configuration to the real one.
  7845. */
  7846. static void intel_modeset_commit_output_state(struct drm_device *dev)
  7847. {
  7848. struct intel_crtc *crtc;
  7849. struct intel_encoder *encoder;
  7850. struct intel_connector *connector;
  7851. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7852. base.head) {
  7853. connector->base.encoder = &connector->new_encoder->base;
  7854. }
  7855. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  7856. base.head) {
  7857. encoder->base.crtc = &encoder->new_crtc->base;
  7858. }
  7859. for_each_intel_crtc(dev, crtc) {
  7860. crtc->base.enabled = crtc->new_enabled;
  7861. }
  7862. }
  7863. static void
  7864. connected_sink_compute_bpp(struct intel_connector *connector,
  7865. struct intel_crtc_config *pipe_config)
  7866. {
  7867. int bpp = pipe_config->pipe_bpp;
  7868. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  7869. connector->base.base.id,
  7870. connector->base.name);
  7871. /* Don't use an invalid EDID bpc value */
  7872. if (connector->base.display_info.bpc &&
  7873. connector->base.display_info.bpc * 3 < bpp) {
  7874. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  7875. bpp, connector->base.display_info.bpc*3);
  7876. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  7877. }
  7878. /* Clamp bpp to 8 on screens without EDID 1.4 */
  7879. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  7880. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  7881. bpp);
  7882. pipe_config->pipe_bpp = 24;
  7883. }
  7884. }
  7885. static int
  7886. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  7887. struct drm_framebuffer *fb,
  7888. struct intel_crtc_config *pipe_config)
  7889. {
  7890. struct drm_device *dev = crtc->base.dev;
  7891. struct intel_connector *connector;
  7892. int bpp;
  7893. switch (fb->pixel_format) {
  7894. case DRM_FORMAT_C8:
  7895. bpp = 8*3; /* since we go through a colormap */
  7896. break;
  7897. case DRM_FORMAT_XRGB1555:
  7898. case DRM_FORMAT_ARGB1555:
  7899. /* checked in intel_framebuffer_init already */
  7900. if (WARN_ON(INTEL_INFO(dev)->gen > 3))
  7901. return -EINVAL;
  7902. case DRM_FORMAT_RGB565:
  7903. bpp = 6*3; /* min is 18bpp */
  7904. break;
  7905. case DRM_FORMAT_XBGR8888:
  7906. case DRM_FORMAT_ABGR8888:
  7907. /* checked in intel_framebuffer_init already */
  7908. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  7909. return -EINVAL;
  7910. case DRM_FORMAT_XRGB8888:
  7911. case DRM_FORMAT_ARGB8888:
  7912. bpp = 8*3;
  7913. break;
  7914. case DRM_FORMAT_XRGB2101010:
  7915. case DRM_FORMAT_ARGB2101010:
  7916. case DRM_FORMAT_XBGR2101010:
  7917. case DRM_FORMAT_ABGR2101010:
  7918. /* checked in intel_framebuffer_init already */
  7919. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  7920. return -EINVAL;
  7921. bpp = 10*3;
  7922. break;
  7923. /* TODO: gen4+ supports 16 bpc floating point, too. */
  7924. default:
  7925. DRM_DEBUG_KMS("unsupported depth\n");
  7926. return -EINVAL;
  7927. }
  7928. pipe_config->pipe_bpp = bpp;
  7929. /* Clamp display bpp to EDID value */
  7930. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7931. base.head) {
  7932. if (!connector->new_encoder ||
  7933. connector->new_encoder->new_crtc != crtc)
  7934. continue;
  7935. connected_sink_compute_bpp(connector, pipe_config);
  7936. }
  7937. return bpp;
  7938. }
  7939. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  7940. {
  7941. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  7942. "type: 0x%x flags: 0x%x\n",
  7943. mode->crtc_clock,
  7944. mode->crtc_hdisplay, mode->crtc_hsync_start,
  7945. mode->crtc_hsync_end, mode->crtc_htotal,
  7946. mode->crtc_vdisplay, mode->crtc_vsync_start,
  7947. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  7948. }
  7949. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  7950. struct intel_crtc_config *pipe_config,
  7951. const char *context)
  7952. {
  7953. DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
  7954. context, pipe_name(crtc->pipe));
  7955. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  7956. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  7957. pipe_config->pipe_bpp, pipe_config->dither);
  7958. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  7959. pipe_config->has_pch_encoder,
  7960. pipe_config->fdi_lanes,
  7961. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  7962. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  7963. pipe_config->fdi_m_n.tu);
  7964. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  7965. pipe_config->has_dp_encoder,
  7966. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  7967. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  7968. pipe_config->dp_m_n.tu);
  7969. DRM_DEBUG_KMS("requested mode:\n");
  7970. drm_mode_debug_printmodeline(&pipe_config->requested_mode);
  7971. DRM_DEBUG_KMS("adjusted mode:\n");
  7972. drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
  7973. intel_dump_crtc_timings(&pipe_config->adjusted_mode);
  7974. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  7975. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  7976. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  7977. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  7978. pipe_config->gmch_pfit.control,
  7979. pipe_config->gmch_pfit.pgm_ratios,
  7980. pipe_config->gmch_pfit.lvds_border_bits);
  7981. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  7982. pipe_config->pch_pfit.pos,
  7983. pipe_config->pch_pfit.size,
  7984. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  7985. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  7986. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  7987. }
  7988. static bool encoders_cloneable(const struct intel_encoder *a,
  7989. const struct intel_encoder *b)
  7990. {
  7991. /* masks could be asymmetric, so check both ways */
  7992. return a == b || (a->cloneable & (1 << b->type) &&
  7993. b->cloneable & (1 << a->type));
  7994. }
  7995. static bool check_single_encoder_cloning(struct intel_crtc *crtc,
  7996. struct intel_encoder *encoder)
  7997. {
  7998. struct drm_device *dev = crtc->base.dev;
  7999. struct intel_encoder *source_encoder;
  8000. list_for_each_entry(source_encoder,
  8001. &dev->mode_config.encoder_list, base.head) {
  8002. if (source_encoder->new_crtc != crtc)
  8003. continue;
  8004. if (!encoders_cloneable(encoder, source_encoder))
  8005. return false;
  8006. }
  8007. return true;
  8008. }
  8009. static bool check_encoder_cloning(struct intel_crtc *crtc)
  8010. {
  8011. struct drm_device *dev = crtc->base.dev;
  8012. struct intel_encoder *encoder;
  8013. list_for_each_entry(encoder,
  8014. &dev->mode_config.encoder_list, base.head) {
  8015. if (encoder->new_crtc != crtc)
  8016. continue;
  8017. if (!check_single_encoder_cloning(crtc, encoder))
  8018. return false;
  8019. }
  8020. return true;
  8021. }
  8022. static struct intel_crtc_config *
  8023. intel_modeset_pipe_config(struct drm_crtc *crtc,
  8024. struct drm_framebuffer *fb,
  8025. struct drm_display_mode *mode)
  8026. {
  8027. struct drm_device *dev = crtc->dev;
  8028. struct intel_encoder *encoder;
  8029. struct intel_crtc_config *pipe_config;
  8030. int plane_bpp, ret = -EINVAL;
  8031. bool retry = true;
  8032. if (!check_encoder_cloning(to_intel_crtc(crtc))) {
  8033. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  8034. return ERR_PTR(-EINVAL);
  8035. }
  8036. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8037. if (!pipe_config)
  8038. return ERR_PTR(-ENOMEM);
  8039. drm_mode_copy(&pipe_config->adjusted_mode, mode);
  8040. drm_mode_copy(&pipe_config->requested_mode, mode);
  8041. pipe_config->cpu_transcoder =
  8042. (enum transcoder) to_intel_crtc(crtc)->pipe;
  8043. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8044. /*
  8045. * Sanitize sync polarity flags based on requested ones. If neither
  8046. * positive or negative polarity is requested, treat this as meaning
  8047. * negative polarity.
  8048. */
  8049. if (!(pipe_config->adjusted_mode.flags &
  8050. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  8051. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  8052. if (!(pipe_config->adjusted_mode.flags &
  8053. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  8054. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  8055. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  8056. * plane pixel format and any sink constraints into account. Returns the
  8057. * source plane bpp so that dithering can be selected on mismatches
  8058. * after encoders and crtc also have had their say. */
  8059. plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  8060. fb, pipe_config);
  8061. if (plane_bpp < 0)
  8062. goto fail;
  8063. /*
  8064. * Determine the real pipe dimensions. Note that stereo modes can
  8065. * increase the actual pipe size due to the frame doubling and
  8066. * insertion of additional space for blanks between the frame. This
  8067. * is stored in the crtc timings. We use the requested mode to do this
  8068. * computation to clearly distinguish it from the adjusted mode, which
  8069. * can be changed by the connectors in the below retry loop.
  8070. */
  8071. drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
  8072. pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
  8073. pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
  8074. encoder_retry:
  8075. /* Ensure the port clock defaults are reset when retrying. */
  8076. pipe_config->port_clock = 0;
  8077. pipe_config->pixel_multiplier = 1;
  8078. /* Fill in default crtc timings, allow encoders to overwrite them. */
  8079. drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
  8080. /* Pass our mode to the connectors and the CRTC to give them a chance to
  8081. * adjust it according to limitations or connector properties, and also
  8082. * a chance to reject the mode entirely.
  8083. */
  8084. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8085. base.head) {
  8086. if (&encoder->new_crtc->base != crtc)
  8087. continue;
  8088. if (!(encoder->compute_config(encoder, pipe_config))) {
  8089. DRM_DEBUG_KMS("Encoder config failure\n");
  8090. goto fail;
  8091. }
  8092. }
  8093. /* Set default port clock if not overwritten by the encoder. Needs to be
  8094. * done afterwards in case the encoder adjusts the mode. */
  8095. if (!pipe_config->port_clock)
  8096. pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
  8097. * pipe_config->pixel_multiplier;
  8098. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  8099. if (ret < 0) {
  8100. DRM_DEBUG_KMS("CRTC fixup failed\n");
  8101. goto fail;
  8102. }
  8103. if (ret == RETRY) {
  8104. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  8105. ret = -EINVAL;
  8106. goto fail;
  8107. }
  8108. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  8109. retry = false;
  8110. goto encoder_retry;
  8111. }
  8112. pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
  8113. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  8114. plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  8115. return pipe_config;
  8116. fail:
  8117. kfree(pipe_config);
  8118. return ERR_PTR(ret);
  8119. }
  8120. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  8121. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  8122. static void
  8123. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  8124. unsigned *prepare_pipes, unsigned *disable_pipes)
  8125. {
  8126. struct intel_crtc *intel_crtc;
  8127. struct drm_device *dev = crtc->dev;
  8128. struct intel_encoder *encoder;
  8129. struct intel_connector *connector;
  8130. struct drm_crtc *tmp_crtc;
  8131. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  8132. /* Check which crtcs have changed outputs connected to them, these need
  8133. * to be part of the prepare_pipes mask. We don't (yet) support global
  8134. * modeset across multiple crtcs, so modeset_pipes will only have one
  8135. * bit set at most. */
  8136. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8137. base.head) {
  8138. if (connector->base.encoder == &connector->new_encoder->base)
  8139. continue;
  8140. if (connector->base.encoder) {
  8141. tmp_crtc = connector->base.encoder->crtc;
  8142. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8143. }
  8144. if (connector->new_encoder)
  8145. *prepare_pipes |=
  8146. 1 << connector->new_encoder->new_crtc->pipe;
  8147. }
  8148. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8149. base.head) {
  8150. if (encoder->base.crtc == &encoder->new_crtc->base)
  8151. continue;
  8152. if (encoder->base.crtc) {
  8153. tmp_crtc = encoder->base.crtc;
  8154. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8155. }
  8156. if (encoder->new_crtc)
  8157. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  8158. }
  8159. /* Check for pipes that will be enabled/disabled ... */
  8160. for_each_intel_crtc(dev, intel_crtc) {
  8161. if (intel_crtc->base.enabled == intel_crtc->new_enabled)
  8162. continue;
  8163. if (!intel_crtc->new_enabled)
  8164. *disable_pipes |= 1 << intel_crtc->pipe;
  8165. else
  8166. *prepare_pipes |= 1 << intel_crtc->pipe;
  8167. }
  8168. /* set_mode is also used to update properties on life display pipes. */
  8169. intel_crtc = to_intel_crtc(crtc);
  8170. if (intel_crtc->new_enabled)
  8171. *prepare_pipes |= 1 << intel_crtc->pipe;
  8172. /*
  8173. * For simplicity do a full modeset on any pipe where the output routing
  8174. * changed. We could be more clever, but that would require us to be
  8175. * more careful with calling the relevant encoder->mode_set functions.
  8176. */
  8177. if (*prepare_pipes)
  8178. *modeset_pipes = *prepare_pipes;
  8179. /* ... and mask these out. */
  8180. *modeset_pipes &= ~(*disable_pipes);
  8181. *prepare_pipes &= ~(*disable_pipes);
  8182. /*
  8183. * HACK: We don't (yet) fully support global modesets. intel_set_config
  8184. * obies this rule, but the modeset restore mode of
  8185. * intel_modeset_setup_hw_state does not.
  8186. */
  8187. *modeset_pipes &= 1 << intel_crtc->pipe;
  8188. *prepare_pipes &= 1 << intel_crtc->pipe;
  8189. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  8190. *modeset_pipes, *prepare_pipes, *disable_pipes);
  8191. }
  8192. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  8193. {
  8194. struct drm_encoder *encoder;
  8195. struct drm_device *dev = crtc->dev;
  8196. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  8197. if (encoder->crtc == crtc)
  8198. return true;
  8199. return false;
  8200. }
  8201. static void
  8202. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  8203. {
  8204. struct intel_encoder *intel_encoder;
  8205. struct intel_crtc *intel_crtc;
  8206. struct drm_connector *connector;
  8207. list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
  8208. base.head) {
  8209. if (!intel_encoder->base.crtc)
  8210. continue;
  8211. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  8212. if (prepare_pipes & (1 << intel_crtc->pipe))
  8213. intel_encoder->connectors_active = false;
  8214. }
  8215. intel_modeset_commit_output_state(dev);
  8216. /* Double check state. */
  8217. for_each_intel_crtc(dev, intel_crtc) {
  8218. WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
  8219. WARN_ON(intel_crtc->new_config &&
  8220. intel_crtc->new_config != &intel_crtc->config);
  8221. WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
  8222. }
  8223. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  8224. if (!connector->encoder || !connector->encoder->crtc)
  8225. continue;
  8226. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  8227. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  8228. struct drm_property *dpms_property =
  8229. dev->mode_config.dpms_property;
  8230. connector->dpms = DRM_MODE_DPMS_ON;
  8231. drm_object_property_set_value(&connector->base,
  8232. dpms_property,
  8233. DRM_MODE_DPMS_ON);
  8234. intel_encoder = to_intel_encoder(connector->encoder);
  8235. intel_encoder->connectors_active = true;
  8236. }
  8237. }
  8238. }
  8239. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  8240. {
  8241. int diff;
  8242. if (clock1 == clock2)
  8243. return true;
  8244. if (!clock1 || !clock2)
  8245. return false;
  8246. diff = abs(clock1 - clock2);
  8247. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  8248. return true;
  8249. return false;
  8250. }
  8251. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  8252. list_for_each_entry((intel_crtc), \
  8253. &(dev)->mode_config.crtc_list, \
  8254. base.head) \
  8255. if (mask & (1 <<(intel_crtc)->pipe))
  8256. static bool
  8257. intel_pipe_config_compare(struct drm_device *dev,
  8258. struct intel_crtc_config *current_config,
  8259. struct intel_crtc_config *pipe_config)
  8260. {
  8261. #define PIPE_CONF_CHECK_X(name) \
  8262. if (current_config->name != pipe_config->name) { \
  8263. DRM_ERROR("mismatch in " #name " " \
  8264. "(expected 0x%08x, found 0x%08x)\n", \
  8265. current_config->name, \
  8266. pipe_config->name); \
  8267. return false; \
  8268. }
  8269. #define PIPE_CONF_CHECK_I(name) \
  8270. if (current_config->name != pipe_config->name) { \
  8271. DRM_ERROR("mismatch in " #name " " \
  8272. "(expected %i, found %i)\n", \
  8273. current_config->name, \
  8274. pipe_config->name); \
  8275. return false; \
  8276. }
  8277. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  8278. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  8279. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  8280. "(expected %i, found %i)\n", \
  8281. current_config->name & (mask), \
  8282. pipe_config->name & (mask)); \
  8283. return false; \
  8284. }
  8285. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  8286. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  8287. DRM_ERROR("mismatch in " #name " " \
  8288. "(expected %i, found %i)\n", \
  8289. current_config->name, \
  8290. pipe_config->name); \
  8291. return false; \
  8292. }
  8293. #define PIPE_CONF_QUIRK(quirk) \
  8294. ((current_config->quirks | pipe_config->quirks) & (quirk))
  8295. PIPE_CONF_CHECK_I(cpu_transcoder);
  8296. PIPE_CONF_CHECK_I(has_pch_encoder);
  8297. PIPE_CONF_CHECK_I(fdi_lanes);
  8298. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  8299. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  8300. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  8301. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  8302. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  8303. PIPE_CONF_CHECK_I(has_dp_encoder);
  8304. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  8305. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  8306. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  8307. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  8308. PIPE_CONF_CHECK_I(dp_m_n.tu);
  8309. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
  8310. PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
  8311. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
  8312. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
  8313. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
  8314. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
  8315. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
  8316. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
  8317. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
  8318. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
  8319. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
  8320. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
  8321. PIPE_CONF_CHECK_I(pixel_multiplier);
  8322. PIPE_CONF_CHECK_I(has_hdmi_sink);
  8323. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  8324. IS_VALLEYVIEW(dev))
  8325. PIPE_CONF_CHECK_I(limited_color_range);
  8326. PIPE_CONF_CHECK_I(has_audio);
  8327. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8328. DRM_MODE_FLAG_INTERLACE);
  8329. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  8330. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8331. DRM_MODE_FLAG_PHSYNC);
  8332. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8333. DRM_MODE_FLAG_NHSYNC);
  8334. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8335. DRM_MODE_FLAG_PVSYNC);
  8336. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8337. DRM_MODE_FLAG_NVSYNC);
  8338. }
  8339. PIPE_CONF_CHECK_I(pipe_src_w);
  8340. PIPE_CONF_CHECK_I(pipe_src_h);
  8341. /*
  8342. * FIXME: BIOS likes to set up a cloned config with lvds+external
  8343. * screen. Since we don't yet re-compute the pipe config when moving
  8344. * just the lvds port away to another pipe the sw tracking won't match.
  8345. *
  8346. * Proper atomic modesets with recomputed global state will fix this.
  8347. * Until then just don't check gmch state for inherited modes.
  8348. */
  8349. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  8350. PIPE_CONF_CHECK_I(gmch_pfit.control);
  8351. /* pfit ratios are autocomputed by the hw on gen4+ */
  8352. if (INTEL_INFO(dev)->gen < 4)
  8353. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  8354. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  8355. }
  8356. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  8357. if (current_config->pch_pfit.enabled) {
  8358. PIPE_CONF_CHECK_I(pch_pfit.pos);
  8359. PIPE_CONF_CHECK_I(pch_pfit.size);
  8360. }
  8361. /* BDW+ don't expose a synchronous way to read the state */
  8362. if (IS_HASWELL(dev))
  8363. PIPE_CONF_CHECK_I(ips_enabled);
  8364. PIPE_CONF_CHECK_I(double_wide);
  8365. PIPE_CONF_CHECK_I(shared_dpll);
  8366. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  8367. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  8368. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  8369. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  8370. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  8371. PIPE_CONF_CHECK_I(pipe_bpp);
  8372. PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
  8373. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  8374. #undef PIPE_CONF_CHECK_X
  8375. #undef PIPE_CONF_CHECK_I
  8376. #undef PIPE_CONF_CHECK_FLAGS
  8377. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  8378. #undef PIPE_CONF_QUIRK
  8379. return true;
  8380. }
  8381. static void
  8382. check_connector_state(struct drm_device *dev)
  8383. {
  8384. struct intel_connector *connector;
  8385. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8386. base.head) {
  8387. /* This also checks the encoder/connector hw state with the
  8388. * ->get_hw_state callbacks. */
  8389. intel_connector_check_state(connector);
  8390. WARN(&connector->new_encoder->base != connector->base.encoder,
  8391. "connector's staged encoder doesn't match current encoder\n");
  8392. }
  8393. }
  8394. static void
  8395. check_encoder_state(struct drm_device *dev)
  8396. {
  8397. struct intel_encoder *encoder;
  8398. struct intel_connector *connector;
  8399. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8400. base.head) {
  8401. bool enabled = false;
  8402. bool active = false;
  8403. enum pipe pipe, tracked_pipe;
  8404. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  8405. encoder->base.base.id,
  8406. encoder->base.name);
  8407. WARN(&encoder->new_crtc->base != encoder->base.crtc,
  8408. "encoder's stage crtc doesn't match current crtc\n");
  8409. WARN(encoder->connectors_active && !encoder->base.crtc,
  8410. "encoder's active_connectors set, but no crtc\n");
  8411. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8412. base.head) {
  8413. if (connector->base.encoder != &encoder->base)
  8414. continue;
  8415. enabled = true;
  8416. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  8417. active = true;
  8418. }
  8419. WARN(!!encoder->base.crtc != enabled,
  8420. "encoder's enabled state mismatch "
  8421. "(expected %i, found %i)\n",
  8422. !!encoder->base.crtc, enabled);
  8423. WARN(active && !encoder->base.crtc,
  8424. "active encoder with no crtc\n");
  8425. WARN(encoder->connectors_active != active,
  8426. "encoder's computed active state doesn't match tracked active state "
  8427. "(expected %i, found %i)\n", active, encoder->connectors_active);
  8428. active = encoder->get_hw_state(encoder, &pipe);
  8429. WARN(active != encoder->connectors_active,
  8430. "encoder's hw state doesn't match sw tracking "
  8431. "(expected %i, found %i)\n",
  8432. encoder->connectors_active, active);
  8433. if (!encoder->base.crtc)
  8434. continue;
  8435. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  8436. WARN(active && pipe != tracked_pipe,
  8437. "active encoder's pipe doesn't match"
  8438. "(expected %i, found %i)\n",
  8439. tracked_pipe, pipe);
  8440. }
  8441. }
  8442. static void
  8443. check_crtc_state(struct drm_device *dev)
  8444. {
  8445. struct drm_i915_private *dev_priv = dev->dev_private;
  8446. struct intel_crtc *crtc;
  8447. struct intel_encoder *encoder;
  8448. struct intel_crtc_config pipe_config;
  8449. for_each_intel_crtc(dev, crtc) {
  8450. bool enabled = false;
  8451. bool active = false;
  8452. memset(&pipe_config, 0, sizeof(pipe_config));
  8453. DRM_DEBUG_KMS("[CRTC:%d]\n",
  8454. crtc->base.base.id);
  8455. WARN(crtc->active && !crtc->base.enabled,
  8456. "active crtc, but not enabled in sw tracking\n");
  8457. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8458. base.head) {
  8459. if (encoder->base.crtc != &crtc->base)
  8460. continue;
  8461. enabled = true;
  8462. if (encoder->connectors_active)
  8463. active = true;
  8464. }
  8465. WARN(active != crtc->active,
  8466. "crtc's computed active state doesn't match tracked active state "
  8467. "(expected %i, found %i)\n", active, crtc->active);
  8468. WARN(enabled != crtc->base.enabled,
  8469. "crtc's computed enabled state doesn't match tracked enabled state "
  8470. "(expected %i, found %i)\n", enabled, crtc->base.enabled);
  8471. active = dev_priv->display.get_pipe_config(crtc,
  8472. &pipe_config);
  8473. /* hw state is inconsistent with the pipe A quirk */
  8474. if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
  8475. active = crtc->active;
  8476. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8477. base.head) {
  8478. enum pipe pipe;
  8479. if (encoder->base.crtc != &crtc->base)
  8480. continue;
  8481. if (encoder->get_hw_state(encoder, &pipe))
  8482. encoder->get_config(encoder, &pipe_config);
  8483. }
  8484. WARN(crtc->active != active,
  8485. "crtc active state doesn't match with hw state "
  8486. "(expected %i, found %i)\n", crtc->active, active);
  8487. if (active &&
  8488. !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
  8489. WARN(1, "pipe state doesn't match!\n");
  8490. intel_dump_pipe_config(crtc, &pipe_config,
  8491. "[hw state]");
  8492. intel_dump_pipe_config(crtc, &crtc->config,
  8493. "[sw state]");
  8494. }
  8495. }
  8496. }
  8497. static void
  8498. check_shared_dpll_state(struct drm_device *dev)
  8499. {
  8500. struct drm_i915_private *dev_priv = dev->dev_private;
  8501. struct intel_crtc *crtc;
  8502. struct intel_dpll_hw_state dpll_hw_state;
  8503. int i;
  8504. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  8505. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  8506. int enabled_crtcs = 0, active_crtcs = 0;
  8507. bool active;
  8508. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  8509. DRM_DEBUG_KMS("%s\n", pll->name);
  8510. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  8511. WARN(pll->active > pll->refcount,
  8512. "more active pll users than references: %i vs %i\n",
  8513. pll->active, pll->refcount);
  8514. WARN(pll->active && !pll->on,
  8515. "pll in active use but not on in sw tracking\n");
  8516. WARN(pll->on && !pll->active,
  8517. "pll in on but not on in use in sw tracking\n");
  8518. WARN(pll->on != active,
  8519. "pll on state mismatch (expected %i, found %i)\n",
  8520. pll->on, active);
  8521. for_each_intel_crtc(dev, crtc) {
  8522. if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
  8523. enabled_crtcs++;
  8524. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  8525. active_crtcs++;
  8526. }
  8527. WARN(pll->active != active_crtcs,
  8528. "pll active crtcs mismatch (expected %i, found %i)\n",
  8529. pll->active, active_crtcs);
  8530. WARN(pll->refcount != enabled_crtcs,
  8531. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  8532. pll->refcount, enabled_crtcs);
  8533. WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
  8534. sizeof(dpll_hw_state)),
  8535. "pll hw state mismatch\n");
  8536. }
  8537. }
  8538. void
  8539. intel_modeset_check_state(struct drm_device *dev)
  8540. {
  8541. check_connector_state(dev);
  8542. check_encoder_state(dev);
  8543. check_crtc_state(dev);
  8544. check_shared_dpll_state(dev);
  8545. }
  8546. void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
  8547. int dotclock)
  8548. {
  8549. /*
  8550. * FDI already provided one idea for the dotclock.
  8551. * Yell if the encoder disagrees.
  8552. */
  8553. WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
  8554. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  8555. pipe_config->adjusted_mode.crtc_clock, dotclock);
  8556. }
  8557. static void update_scanline_offset(struct intel_crtc *crtc)
  8558. {
  8559. struct drm_device *dev = crtc->base.dev;
  8560. /*
  8561. * The scanline counter increments at the leading edge of hsync.
  8562. *
  8563. * On most platforms it starts counting from vtotal-1 on the
  8564. * first active line. That means the scanline counter value is
  8565. * always one less than what we would expect. Ie. just after
  8566. * start of vblank, which also occurs at start of hsync (on the
  8567. * last active line), the scanline counter will read vblank_start-1.
  8568. *
  8569. * On gen2 the scanline counter starts counting from 1 instead
  8570. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  8571. * to keep the value positive), instead of adding one.
  8572. *
  8573. * On HSW+ the behaviour of the scanline counter depends on the output
  8574. * type. For DP ports it behaves like most other platforms, but on HDMI
  8575. * there's an extra 1 line difference. So we need to add two instead of
  8576. * one to the value.
  8577. */
  8578. if (IS_GEN2(dev)) {
  8579. const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
  8580. int vtotal;
  8581. vtotal = mode->crtc_vtotal;
  8582. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  8583. vtotal /= 2;
  8584. crtc->scanline_offset = vtotal - 1;
  8585. } else if (HAS_DDI(dev) &&
  8586. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI)) {
  8587. crtc->scanline_offset = 2;
  8588. } else
  8589. crtc->scanline_offset = 1;
  8590. }
  8591. static int __intel_set_mode(struct drm_crtc *crtc,
  8592. struct drm_display_mode *mode,
  8593. int x, int y, struct drm_framebuffer *fb)
  8594. {
  8595. struct drm_device *dev = crtc->dev;
  8596. struct drm_i915_private *dev_priv = dev->dev_private;
  8597. struct drm_display_mode *saved_mode;
  8598. struct intel_crtc_config *pipe_config = NULL;
  8599. struct intel_crtc *intel_crtc;
  8600. unsigned disable_pipes, prepare_pipes, modeset_pipes;
  8601. int ret = 0;
  8602. saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
  8603. if (!saved_mode)
  8604. return -ENOMEM;
  8605. intel_modeset_affected_pipes(crtc, &modeset_pipes,
  8606. &prepare_pipes, &disable_pipes);
  8607. *saved_mode = crtc->mode;
  8608. /* Hack: Because we don't (yet) support global modeset on multiple
  8609. * crtcs, we don't keep track of the new mode for more than one crtc.
  8610. * Hence simply check whether any bit is set in modeset_pipes in all the
  8611. * pieces of code that are not yet converted to deal with mutliple crtcs
  8612. * changing their mode at the same time. */
  8613. if (modeset_pipes) {
  8614. pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
  8615. if (IS_ERR(pipe_config)) {
  8616. ret = PTR_ERR(pipe_config);
  8617. pipe_config = NULL;
  8618. goto out;
  8619. }
  8620. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  8621. "[modeset]");
  8622. to_intel_crtc(crtc)->new_config = pipe_config;
  8623. }
  8624. /*
  8625. * See if the config requires any additional preparation, e.g.
  8626. * to adjust global state with pipes off. We need to do this
  8627. * here so we can get the modeset_pipe updated config for the new
  8628. * mode set on this crtc. For other crtcs we need to use the
  8629. * adjusted_mode bits in the crtc directly.
  8630. */
  8631. if (IS_VALLEYVIEW(dev)) {
  8632. valleyview_modeset_global_pipes(dev, &prepare_pipes);
  8633. /* may have added more to prepare_pipes than we should */
  8634. prepare_pipes &= ~disable_pipes;
  8635. }
  8636. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  8637. intel_crtc_disable(&intel_crtc->base);
  8638. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  8639. if (intel_crtc->base.enabled)
  8640. dev_priv->display.crtc_disable(&intel_crtc->base);
  8641. }
  8642. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  8643. * to set it here already despite that we pass it down the callchain.
  8644. */
  8645. if (modeset_pipes) {
  8646. crtc->mode = *mode;
  8647. /* mode_set/enable/disable functions rely on a correct pipe
  8648. * config. */
  8649. to_intel_crtc(crtc)->config = *pipe_config;
  8650. to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
  8651. /*
  8652. * Calculate and store various constants which
  8653. * are later needed by vblank and swap-completion
  8654. * timestamping. They are derived from true hwmode.
  8655. */
  8656. drm_calc_timestamping_constants(crtc,
  8657. &pipe_config->adjusted_mode);
  8658. }
  8659. /* Only after disabling all output pipelines that will be changed can we
  8660. * update the the output configuration. */
  8661. intel_modeset_update_state(dev, prepare_pipes);
  8662. if (dev_priv->display.modeset_global_resources)
  8663. dev_priv->display.modeset_global_resources(dev);
  8664. /* Set up the DPLL and any encoders state that needs to adjust or depend
  8665. * on the DPLL.
  8666. */
  8667. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  8668. struct drm_framebuffer *old_fb;
  8669. mutex_lock(&dev->struct_mutex);
  8670. ret = intel_pin_and_fence_fb_obj(dev,
  8671. to_intel_framebuffer(fb)->obj,
  8672. NULL);
  8673. if (ret != 0) {
  8674. DRM_ERROR("pin & fence failed\n");
  8675. mutex_unlock(&dev->struct_mutex);
  8676. goto done;
  8677. }
  8678. old_fb = crtc->primary->fb;
  8679. if (old_fb)
  8680. intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
  8681. mutex_unlock(&dev->struct_mutex);
  8682. crtc->primary->fb = fb;
  8683. crtc->x = x;
  8684. crtc->y = y;
  8685. ret = dev_priv->display.crtc_mode_set(&intel_crtc->base,
  8686. x, y, fb);
  8687. if (ret)
  8688. goto done;
  8689. }
  8690. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  8691. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  8692. update_scanline_offset(intel_crtc);
  8693. dev_priv->display.crtc_enable(&intel_crtc->base);
  8694. }
  8695. /* FIXME: add subpixel order */
  8696. done:
  8697. if (ret && crtc->enabled)
  8698. crtc->mode = *saved_mode;
  8699. out:
  8700. kfree(pipe_config);
  8701. kfree(saved_mode);
  8702. return ret;
  8703. }
  8704. static int intel_set_mode(struct drm_crtc *crtc,
  8705. struct drm_display_mode *mode,
  8706. int x, int y, struct drm_framebuffer *fb)
  8707. {
  8708. int ret;
  8709. ret = __intel_set_mode(crtc, mode, x, y, fb);
  8710. if (ret == 0)
  8711. intel_modeset_check_state(crtc->dev);
  8712. return ret;
  8713. }
  8714. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  8715. {
  8716. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
  8717. }
  8718. #undef for_each_intel_crtc_masked
  8719. static void intel_set_config_free(struct intel_set_config *config)
  8720. {
  8721. if (!config)
  8722. return;
  8723. kfree(config->save_connector_encoders);
  8724. kfree(config->save_encoder_crtcs);
  8725. kfree(config->save_crtc_enabled);
  8726. kfree(config);
  8727. }
  8728. static int intel_set_config_save_state(struct drm_device *dev,
  8729. struct intel_set_config *config)
  8730. {
  8731. struct drm_crtc *crtc;
  8732. struct drm_encoder *encoder;
  8733. struct drm_connector *connector;
  8734. int count;
  8735. config->save_crtc_enabled =
  8736. kcalloc(dev->mode_config.num_crtc,
  8737. sizeof(bool), GFP_KERNEL);
  8738. if (!config->save_crtc_enabled)
  8739. return -ENOMEM;
  8740. config->save_encoder_crtcs =
  8741. kcalloc(dev->mode_config.num_encoder,
  8742. sizeof(struct drm_crtc *), GFP_KERNEL);
  8743. if (!config->save_encoder_crtcs)
  8744. return -ENOMEM;
  8745. config->save_connector_encoders =
  8746. kcalloc(dev->mode_config.num_connector,
  8747. sizeof(struct drm_encoder *), GFP_KERNEL);
  8748. if (!config->save_connector_encoders)
  8749. return -ENOMEM;
  8750. /* Copy data. Note that driver private data is not affected.
  8751. * Should anything bad happen only the expected state is
  8752. * restored, not the drivers personal bookkeeping.
  8753. */
  8754. count = 0;
  8755. for_each_crtc(dev, crtc) {
  8756. config->save_crtc_enabled[count++] = crtc->enabled;
  8757. }
  8758. count = 0;
  8759. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  8760. config->save_encoder_crtcs[count++] = encoder->crtc;
  8761. }
  8762. count = 0;
  8763. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  8764. config->save_connector_encoders[count++] = connector->encoder;
  8765. }
  8766. return 0;
  8767. }
  8768. static void intel_set_config_restore_state(struct drm_device *dev,
  8769. struct intel_set_config *config)
  8770. {
  8771. struct intel_crtc *crtc;
  8772. struct intel_encoder *encoder;
  8773. struct intel_connector *connector;
  8774. int count;
  8775. count = 0;
  8776. for_each_intel_crtc(dev, crtc) {
  8777. crtc->new_enabled = config->save_crtc_enabled[count++];
  8778. if (crtc->new_enabled)
  8779. crtc->new_config = &crtc->config;
  8780. else
  8781. crtc->new_config = NULL;
  8782. }
  8783. count = 0;
  8784. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  8785. encoder->new_crtc =
  8786. to_intel_crtc(config->save_encoder_crtcs[count++]);
  8787. }
  8788. count = 0;
  8789. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  8790. connector->new_encoder =
  8791. to_intel_encoder(config->save_connector_encoders[count++]);
  8792. }
  8793. }
  8794. static bool
  8795. is_crtc_connector_off(struct drm_mode_set *set)
  8796. {
  8797. int i;
  8798. if (set->num_connectors == 0)
  8799. return false;
  8800. if (WARN_ON(set->connectors == NULL))
  8801. return false;
  8802. for (i = 0; i < set->num_connectors; i++)
  8803. if (set->connectors[i]->encoder &&
  8804. set->connectors[i]->encoder->crtc == set->crtc &&
  8805. set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
  8806. return true;
  8807. return false;
  8808. }
  8809. static void
  8810. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  8811. struct intel_set_config *config)
  8812. {
  8813. /* We should be able to check here if the fb has the same properties
  8814. * and then just flip_or_move it */
  8815. if (is_crtc_connector_off(set)) {
  8816. config->mode_changed = true;
  8817. } else if (set->crtc->primary->fb != set->fb) {
  8818. /* If we have no fb then treat it as a full mode set */
  8819. if (set->crtc->primary->fb == NULL) {
  8820. struct intel_crtc *intel_crtc =
  8821. to_intel_crtc(set->crtc);
  8822. if (intel_crtc->active && i915.fastboot) {
  8823. DRM_DEBUG_KMS("crtc has no fb, will flip\n");
  8824. config->fb_changed = true;
  8825. } else {
  8826. DRM_DEBUG_KMS("inactive crtc, full mode set\n");
  8827. config->mode_changed = true;
  8828. }
  8829. } else if (set->fb == NULL) {
  8830. config->mode_changed = true;
  8831. } else if (set->fb->pixel_format !=
  8832. set->crtc->primary->fb->pixel_format) {
  8833. config->mode_changed = true;
  8834. } else {
  8835. config->fb_changed = true;
  8836. }
  8837. }
  8838. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  8839. config->fb_changed = true;
  8840. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  8841. DRM_DEBUG_KMS("modes are different, full mode set\n");
  8842. drm_mode_debug_printmodeline(&set->crtc->mode);
  8843. drm_mode_debug_printmodeline(set->mode);
  8844. config->mode_changed = true;
  8845. }
  8846. DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
  8847. set->crtc->base.id, config->mode_changed, config->fb_changed);
  8848. }
  8849. static int
  8850. intel_modeset_stage_output_state(struct drm_device *dev,
  8851. struct drm_mode_set *set,
  8852. struct intel_set_config *config)
  8853. {
  8854. struct intel_connector *connector;
  8855. struct intel_encoder *encoder;
  8856. struct intel_crtc *crtc;
  8857. int ro;
  8858. /* The upper layers ensure that we either disable a crtc or have a list
  8859. * of connectors. For paranoia, double-check this. */
  8860. WARN_ON(!set->fb && (set->num_connectors != 0));
  8861. WARN_ON(set->fb && (set->num_connectors == 0));
  8862. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8863. base.head) {
  8864. /* Otherwise traverse passed in connector list and get encoders
  8865. * for them. */
  8866. for (ro = 0; ro < set->num_connectors; ro++) {
  8867. if (set->connectors[ro] == &connector->base) {
  8868. connector->new_encoder = connector->encoder;
  8869. break;
  8870. }
  8871. }
  8872. /* If we disable the crtc, disable all its connectors. Also, if
  8873. * the connector is on the changing crtc but not on the new
  8874. * connector list, disable it. */
  8875. if ((!set->fb || ro == set->num_connectors) &&
  8876. connector->base.encoder &&
  8877. connector->base.encoder->crtc == set->crtc) {
  8878. connector->new_encoder = NULL;
  8879. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  8880. connector->base.base.id,
  8881. connector->base.name);
  8882. }
  8883. if (&connector->new_encoder->base != connector->base.encoder) {
  8884. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  8885. config->mode_changed = true;
  8886. }
  8887. }
  8888. /* connector->new_encoder is now updated for all connectors. */
  8889. /* Update crtc of enabled connectors. */
  8890. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8891. base.head) {
  8892. struct drm_crtc *new_crtc;
  8893. if (!connector->new_encoder)
  8894. continue;
  8895. new_crtc = connector->new_encoder->base.crtc;
  8896. for (ro = 0; ro < set->num_connectors; ro++) {
  8897. if (set->connectors[ro] == &connector->base)
  8898. new_crtc = set->crtc;
  8899. }
  8900. /* Make sure the new CRTC will work with the encoder */
  8901. if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
  8902. new_crtc)) {
  8903. return -EINVAL;
  8904. }
  8905. connector->encoder->new_crtc = to_intel_crtc(new_crtc);
  8906. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  8907. connector->base.base.id,
  8908. connector->base.name,
  8909. new_crtc->base.id);
  8910. }
  8911. /* Check for any encoders that needs to be disabled. */
  8912. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8913. base.head) {
  8914. int num_connectors = 0;
  8915. list_for_each_entry(connector,
  8916. &dev->mode_config.connector_list,
  8917. base.head) {
  8918. if (connector->new_encoder == encoder) {
  8919. WARN_ON(!connector->new_encoder->new_crtc);
  8920. num_connectors++;
  8921. }
  8922. }
  8923. if (num_connectors == 0)
  8924. encoder->new_crtc = NULL;
  8925. else if (num_connectors > 1)
  8926. return -EINVAL;
  8927. /* Only now check for crtc changes so we don't miss encoders
  8928. * that will be disabled. */
  8929. if (&encoder->new_crtc->base != encoder->base.crtc) {
  8930. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  8931. config->mode_changed = true;
  8932. }
  8933. }
  8934. /* Now we've also updated encoder->new_crtc for all encoders. */
  8935. for_each_intel_crtc(dev, crtc) {
  8936. crtc->new_enabled = false;
  8937. list_for_each_entry(encoder,
  8938. &dev->mode_config.encoder_list,
  8939. base.head) {
  8940. if (encoder->new_crtc == crtc) {
  8941. crtc->new_enabled = true;
  8942. break;
  8943. }
  8944. }
  8945. if (crtc->new_enabled != crtc->base.enabled) {
  8946. DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
  8947. crtc->new_enabled ? "en" : "dis");
  8948. config->mode_changed = true;
  8949. }
  8950. if (crtc->new_enabled)
  8951. crtc->new_config = &crtc->config;
  8952. else
  8953. crtc->new_config = NULL;
  8954. }
  8955. return 0;
  8956. }
  8957. static void disable_crtc_nofb(struct intel_crtc *crtc)
  8958. {
  8959. struct drm_device *dev = crtc->base.dev;
  8960. struct intel_encoder *encoder;
  8961. struct intel_connector *connector;
  8962. DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
  8963. pipe_name(crtc->pipe));
  8964. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  8965. if (connector->new_encoder &&
  8966. connector->new_encoder->new_crtc == crtc)
  8967. connector->new_encoder = NULL;
  8968. }
  8969. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  8970. if (encoder->new_crtc == crtc)
  8971. encoder->new_crtc = NULL;
  8972. }
  8973. crtc->new_enabled = false;
  8974. crtc->new_config = NULL;
  8975. }
  8976. static int intel_crtc_set_config(struct drm_mode_set *set)
  8977. {
  8978. struct drm_device *dev;
  8979. struct drm_mode_set save_set;
  8980. struct intel_set_config *config;
  8981. int ret;
  8982. BUG_ON(!set);
  8983. BUG_ON(!set->crtc);
  8984. BUG_ON(!set->crtc->helper_private);
  8985. /* Enforce sane interface api - has been abused by the fb helper. */
  8986. BUG_ON(!set->mode && set->fb);
  8987. BUG_ON(set->fb && set->num_connectors == 0);
  8988. if (set->fb) {
  8989. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  8990. set->crtc->base.id, set->fb->base.id,
  8991. (int)set->num_connectors, set->x, set->y);
  8992. } else {
  8993. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  8994. }
  8995. dev = set->crtc->dev;
  8996. ret = -ENOMEM;
  8997. config = kzalloc(sizeof(*config), GFP_KERNEL);
  8998. if (!config)
  8999. goto out_config;
  9000. ret = intel_set_config_save_state(dev, config);
  9001. if (ret)
  9002. goto out_config;
  9003. save_set.crtc = set->crtc;
  9004. save_set.mode = &set->crtc->mode;
  9005. save_set.x = set->crtc->x;
  9006. save_set.y = set->crtc->y;
  9007. save_set.fb = set->crtc->primary->fb;
  9008. /* Compute whether we need a full modeset, only an fb base update or no
  9009. * change at all. In the future we might also check whether only the
  9010. * mode changed, e.g. for LVDS where we only change the panel fitter in
  9011. * such cases. */
  9012. intel_set_config_compute_mode_changes(set, config);
  9013. ret = intel_modeset_stage_output_state(dev, set, config);
  9014. if (ret)
  9015. goto fail;
  9016. if (config->mode_changed) {
  9017. ret = intel_set_mode(set->crtc, set->mode,
  9018. set->x, set->y, set->fb);
  9019. } else if (config->fb_changed) {
  9020. intel_crtc_wait_for_pending_flips(set->crtc);
  9021. ret = intel_pipe_set_base(set->crtc,
  9022. set->x, set->y, set->fb);
  9023. /*
  9024. * In the fastboot case this may be our only check of the
  9025. * state after boot. It would be better to only do it on
  9026. * the first update, but we don't have a nice way of doing that
  9027. * (and really, set_config isn't used much for high freq page
  9028. * flipping, so increasing its cost here shouldn't be a big
  9029. * deal).
  9030. */
  9031. if (i915.fastboot && ret == 0)
  9032. intel_modeset_check_state(set->crtc->dev);
  9033. }
  9034. if (ret) {
  9035. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  9036. set->crtc->base.id, ret);
  9037. fail:
  9038. intel_set_config_restore_state(dev, config);
  9039. /*
  9040. * HACK: if the pipe was on, but we didn't have a framebuffer,
  9041. * force the pipe off to avoid oopsing in the modeset code
  9042. * due to fb==NULL. This should only happen during boot since
  9043. * we don't yet reconstruct the FB from the hardware state.
  9044. */
  9045. if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
  9046. disable_crtc_nofb(to_intel_crtc(save_set.crtc));
  9047. /* Try to restore the config */
  9048. if (config->mode_changed &&
  9049. intel_set_mode(save_set.crtc, save_set.mode,
  9050. save_set.x, save_set.y, save_set.fb))
  9051. DRM_ERROR("failed to restore config after modeset failure\n");
  9052. }
  9053. out_config:
  9054. intel_set_config_free(config);
  9055. return ret;
  9056. }
  9057. static const struct drm_crtc_funcs intel_crtc_funcs = {
  9058. .cursor_set = intel_crtc_cursor_set,
  9059. .cursor_move = intel_crtc_cursor_move,
  9060. .gamma_set = intel_crtc_gamma_set,
  9061. .set_config = intel_crtc_set_config,
  9062. .destroy = intel_crtc_destroy,
  9063. .page_flip = intel_crtc_page_flip,
  9064. };
  9065. static void intel_cpu_pll_init(struct drm_device *dev)
  9066. {
  9067. if (HAS_DDI(dev))
  9068. intel_ddi_pll_init(dev);
  9069. }
  9070. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  9071. struct intel_shared_dpll *pll,
  9072. struct intel_dpll_hw_state *hw_state)
  9073. {
  9074. uint32_t val;
  9075. val = I915_READ(PCH_DPLL(pll->id));
  9076. hw_state->dpll = val;
  9077. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  9078. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  9079. return val & DPLL_VCO_ENABLE;
  9080. }
  9081. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  9082. struct intel_shared_dpll *pll)
  9083. {
  9084. I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
  9085. I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
  9086. }
  9087. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  9088. struct intel_shared_dpll *pll)
  9089. {
  9090. /* PCH refclock must be enabled first */
  9091. ibx_assert_pch_refclk_enabled(dev_priv);
  9092. I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
  9093. /* Wait for the clocks to stabilize. */
  9094. POSTING_READ(PCH_DPLL(pll->id));
  9095. udelay(150);
  9096. /* The pixel multiplier can only be updated once the
  9097. * DPLL is enabled and the clocks are stable.
  9098. *
  9099. * So write it again.
  9100. */
  9101. I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
  9102. POSTING_READ(PCH_DPLL(pll->id));
  9103. udelay(200);
  9104. }
  9105. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  9106. struct intel_shared_dpll *pll)
  9107. {
  9108. struct drm_device *dev = dev_priv->dev;
  9109. struct intel_crtc *crtc;
  9110. /* Make sure no transcoder isn't still depending on us. */
  9111. for_each_intel_crtc(dev, crtc) {
  9112. if (intel_crtc_to_shared_dpll(crtc) == pll)
  9113. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  9114. }
  9115. I915_WRITE(PCH_DPLL(pll->id), 0);
  9116. POSTING_READ(PCH_DPLL(pll->id));
  9117. udelay(200);
  9118. }
  9119. static char *ibx_pch_dpll_names[] = {
  9120. "PCH DPLL A",
  9121. "PCH DPLL B",
  9122. };
  9123. static void ibx_pch_dpll_init(struct drm_device *dev)
  9124. {
  9125. struct drm_i915_private *dev_priv = dev->dev_private;
  9126. int i;
  9127. dev_priv->num_shared_dpll = 2;
  9128. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9129. dev_priv->shared_dplls[i].id = i;
  9130. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  9131. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  9132. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  9133. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  9134. dev_priv->shared_dplls[i].get_hw_state =
  9135. ibx_pch_dpll_get_hw_state;
  9136. }
  9137. }
  9138. static void intel_shared_dpll_init(struct drm_device *dev)
  9139. {
  9140. struct drm_i915_private *dev_priv = dev->dev_private;
  9141. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  9142. ibx_pch_dpll_init(dev);
  9143. else
  9144. dev_priv->num_shared_dpll = 0;
  9145. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  9146. }
  9147. static void intel_crtc_init(struct drm_device *dev, int pipe)
  9148. {
  9149. struct drm_i915_private *dev_priv = dev->dev_private;
  9150. struct intel_crtc *intel_crtc;
  9151. int i;
  9152. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  9153. if (intel_crtc == NULL)
  9154. return;
  9155. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  9156. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  9157. for (i = 0; i < 256; i++) {
  9158. intel_crtc->lut_r[i] = i;
  9159. intel_crtc->lut_g[i] = i;
  9160. intel_crtc->lut_b[i] = i;
  9161. }
  9162. /*
  9163. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  9164. * is hooked to plane B. Hence we want plane A feeding pipe B.
  9165. */
  9166. intel_crtc->pipe = pipe;
  9167. intel_crtc->plane = pipe;
  9168. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  9169. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  9170. intel_crtc->plane = !pipe;
  9171. }
  9172. intel_crtc->cursor_base = ~0;
  9173. intel_crtc->cursor_cntl = ~0;
  9174. init_waitqueue_head(&intel_crtc->vbl_wait);
  9175. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  9176. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  9177. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  9178. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  9179. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  9180. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  9181. }
  9182. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  9183. {
  9184. struct drm_encoder *encoder = connector->base.encoder;
  9185. struct drm_device *dev = connector->base.dev;
  9186. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  9187. if (!encoder)
  9188. return INVALID_PIPE;
  9189. return to_intel_crtc(encoder->crtc)->pipe;
  9190. }
  9191. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  9192. struct drm_file *file)
  9193. {
  9194. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  9195. struct drm_mode_object *drmmode_obj;
  9196. struct intel_crtc *crtc;
  9197. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  9198. return -ENODEV;
  9199. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  9200. DRM_MODE_OBJECT_CRTC);
  9201. if (!drmmode_obj) {
  9202. DRM_ERROR("no such CRTC id\n");
  9203. return -ENOENT;
  9204. }
  9205. crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
  9206. pipe_from_crtc_id->pipe = crtc->pipe;
  9207. return 0;
  9208. }
  9209. static int intel_encoder_clones(struct intel_encoder *encoder)
  9210. {
  9211. struct drm_device *dev = encoder->base.dev;
  9212. struct intel_encoder *source_encoder;
  9213. int index_mask = 0;
  9214. int entry = 0;
  9215. list_for_each_entry(source_encoder,
  9216. &dev->mode_config.encoder_list, base.head) {
  9217. if (encoders_cloneable(encoder, source_encoder))
  9218. index_mask |= (1 << entry);
  9219. entry++;
  9220. }
  9221. return index_mask;
  9222. }
  9223. static bool has_edp_a(struct drm_device *dev)
  9224. {
  9225. struct drm_i915_private *dev_priv = dev->dev_private;
  9226. if (!IS_MOBILE(dev))
  9227. return false;
  9228. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  9229. return false;
  9230. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  9231. return false;
  9232. return true;
  9233. }
  9234. const char *intel_output_name(int output)
  9235. {
  9236. static const char *names[] = {
  9237. [INTEL_OUTPUT_UNUSED] = "Unused",
  9238. [INTEL_OUTPUT_ANALOG] = "Analog",
  9239. [INTEL_OUTPUT_DVO] = "DVO",
  9240. [INTEL_OUTPUT_SDVO] = "SDVO",
  9241. [INTEL_OUTPUT_LVDS] = "LVDS",
  9242. [INTEL_OUTPUT_TVOUT] = "TV",
  9243. [INTEL_OUTPUT_HDMI] = "HDMI",
  9244. [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
  9245. [INTEL_OUTPUT_EDP] = "eDP",
  9246. [INTEL_OUTPUT_DSI] = "DSI",
  9247. [INTEL_OUTPUT_UNKNOWN] = "Unknown",
  9248. };
  9249. if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
  9250. return "Invalid";
  9251. return names[output];
  9252. }
  9253. static void intel_setup_outputs(struct drm_device *dev)
  9254. {
  9255. struct drm_i915_private *dev_priv = dev->dev_private;
  9256. struct intel_encoder *encoder;
  9257. bool dpd_is_edp = false;
  9258. intel_lvds_init(dev);
  9259. if (!IS_ULT(dev) && !IS_CHERRYVIEW(dev) && dev_priv->vbt.int_crt_support)
  9260. intel_crt_init(dev);
  9261. if (HAS_DDI(dev)) {
  9262. int found;
  9263. /* Haswell uses DDI functions to detect digital outputs */
  9264. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  9265. /* DDI A only supports eDP */
  9266. if (found)
  9267. intel_ddi_init(dev, PORT_A);
  9268. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  9269. * register */
  9270. found = I915_READ(SFUSE_STRAP);
  9271. if (found & SFUSE_STRAP_DDIB_DETECTED)
  9272. intel_ddi_init(dev, PORT_B);
  9273. if (found & SFUSE_STRAP_DDIC_DETECTED)
  9274. intel_ddi_init(dev, PORT_C);
  9275. if (found & SFUSE_STRAP_DDID_DETECTED)
  9276. intel_ddi_init(dev, PORT_D);
  9277. } else if (HAS_PCH_SPLIT(dev)) {
  9278. int found;
  9279. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  9280. if (has_edp_a(dev))
  9281. intel_dp_init(dev, DP_A, PORT_A);
  9282. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  9283. /* PCH SDVOB multiplex with HDMIB */
  9284. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  9285. if (!found)
  9286. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  9287. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  9288. intel_dp_init(dev, PCH_DP_B, PORT_B);
  9289. }
  9290. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  9291. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  9292. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  9293. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  9294. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  9295. intel_dp_init(dev, PCH_DP_C, PORT_C);
  9296. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  9297. intel_dp_init(dev, PCH_DP_D, PORT_D);
  9298. } else if (IS_VALLEYVIEW(dev)) {
  9299. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
  9300. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  9301. PORT_B);
  9302. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
  9303. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  9304. }
  9305. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
  9306. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  9307. PORT_C);
  9308. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
  9309. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  9310. }
  9311. if (IS_CHERRYVIEW(dev)) {
  9312. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED) {
  9313. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  9314. PORT_D);
  9315. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  9316. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  9317. }
  9318. }
  9319. intel_dsi_init(dev);
  9320. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  9321. bool found = false;
  9322. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  9323. DRM_DEBUG_KMS("probing SDVOB\n");
  9324. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  9325. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  9326. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  9327. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  9328. }
  9329. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  9330. intel_dp_init(dev, DP_B, PORT_B);
  9331. }
  9332. /* Before G4X SDVOC doesn't have its own detect register */
  9333. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  9334. DRM_DEBUG_KMS("probing SDVOC\n");
  9335. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  9336. }
  9337. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  9338. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  9339. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  9340. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  9341. }
  9342. if (SUPPORTS_INTEGRATED_DP(dev))
  9343. intel_dp_init(dev, DP_C, PORT_C);
  9344. }
  9345. if (SUPPORTS_INTEGRATED_DP(dev) &&
  9346. (I915_READ(DP_D) & DP_DETECTED))
  9347. intel_dp_init(dev, DP_D, PORT_D);
  9348. } else if (IS_GEN2(dev))
  9349. intel_dvo_init(dev);
  9350. if (SUPPORTS_TV(dev))
  9351. intel_tv_init(dev);
  9352. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  9353. encoder->base.possible_crtcs = encoder->crtc_mask;
  9354. encoder->base.possible_clones =
  9355. intel_encoder_clones(encoder);
  9356. }
  9357. intel_init_pch_refclk(dev);
  9358. drm_helper_move_panel_connectors_to_head(dev);
  9359. }
  9360. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  9361. {
  9362. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  9363. drm_framebuffer_cleanup(fb);
  9364. WARN_ON(!intel_fb->obj->framebuffer_references--);
  9365. drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
  9366. kfree(intel_fb);
  9367. }
  9368. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  9369. struct drm_file *file,
  9370. unsigned int *handle)
  9371. {
  9372. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  9373. struct drm_i915_gem_object *obj = intel_fb->obj;
  9374. return drm_gem_handle_create(file, &obj->base, handle);
  9375. }
  9376. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  9377. .destroy = intel_user_framebuffer_destroy,
  9378. .create_handle = intel_user_framebuffer_create_handle,
  9379. };
  9380. static int intel_framebuffer_init(struct drm_device *dev,
  9381. struct intel_framebuffer *intel_fb,
  9382. struct drm_mode_fb_cmd2 *mode_cmd,
  9383. struct drm_i915_gem_object *obj)
  9384. {
  9385. int aligned_height;
  9386. int pitch_limit;
  9387. int ret;
  9388. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  9389. if (obj->tiling_mode == I915_TILING_Y) {
  9390. DRM_DEBUG("hardware does not support tiling Y\n");
  9391. return -EINVAL;
  9392. }
  9393. if (mode_cmd->pitches[0] & 63) {
  9394. DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
  9395. mode_cmd->pitches[0]);
  9396. return -EINVAL;
  9397. }
  9398. if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
  9399. pitch_limit = 32*1024;
  9400. } else if (INTEL_INFO(dev)->gen >= 4) {
  9401. if (obj->tiling_mode)
  9402. pitch_limit = 16*1024;
  9403. else
  9404. pitch_limit = 32*1024;
  9405. } else if (INTEL_INFO(dev)->gen >= 3) {
  9406. if (obj->tiling_mode)
  9407. pitch_limit = 8*1024;
  9408. else
  9409. pitch_limit = 16*1024;
  9410. } else
  9411. /* XXX DSPC is limited to 4k tiled */
  9412. pitch_limit = 8*1024;
  9413. if (mode_cmd->pitches[0] > pitch_limit) {
  9414. DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
  9415. obj->tiling_mode ? "tiled" : "linear",
  9416. mode_cmd->pitches[0], pitch_limit);
  9417. return -EINVAL;
  9418. }
  9419. if (obj->tiling_mode != I915_TILING_NONE &&
  9420. mode_cmd->pitches[0] != obj->stride) {
  9421. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  9422. mode_cmd->pitches[0], obj->stride);
  9423. return -EINVAL;
  9424. }
  9425. /* Reject formats not supported by any plane early. */
  9426. switch (mode_cmd->pixel_format) {
  9427. case DRM_FORMAT_C8:
  9428. case DRM_FORMAT_RGB565:
  9429. case DRM_FORMAT_XRGB8888:
  9430. case DRM_FORMAT_ARGB8888:
  9431. break;
  9432. case DRM_FORMAT_XRGB1555:
  9433. case DRM_FORMAT_ARGB1555:
  9434. if (INTEL_INFO(dev)->gen > 3) {
  9435. DRM_DEBUG("unsupported pixel format: %s\n",
  9436. drm_get_format_name(mode_cmd->pixel_format));
  9437. return -EINVAL;
  9438. }
  9439. break;
  9440. case DRM_FORMAT_XBGR8888:
  9441. case DRM_FORMAT_ABGR8888:
  9442. case DRM_FORMAT_XRGB2101010:
  9443. case DRM_FORMAT_ARGB2101010:
  9444. case DRM_FORMAT_XBGR2101010:
  9445. case DRM_FORMAT_ABGR2101010:
  9446. if (INTEL_INFO(dev)->gen < 4) {
  9447. DRM_DEBUG("unsupported pixel format: %s\n",
  9448. drm_get_format_name(mode_cmd->pixel_format));
  9449. return -EINVAL;
  9450. }
  9451. break;
  9452. case DRM_FORMAT_YUYV:
  9453. case DRM_FORMAT_UYVY:
  9454. case DRM_FORMAT_YVYU:
  9455. case DRM_FORMAT_VYUY:
  9456. if (INTEL_INFO(dev)->gen < 5) {
  9457. DRM_DEBUG("unsupported pixel format: %s\n",
  9458. drm_get_format_name(mode_cmd->pixel_format));
  9459. return -EINVAL;
  9460. }
  9461. break;
  9462. default:
  9463. DRM_DEBUG("unsupported pixel format: %s\n",
  9464. drm_get_format_name(mode_cmd->pixel_format));
  9465. return -EINVAL;
  9466. }
  9467. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  9468. if (mode_cmd->offsets[0] != 0)
  9469. return -EINVAL;
  9470. aligned_height = intel_align_height(dev, mode_cmd->height,
  9471. obj->tiling_mode);
  9472. /* FIXME drm helper for size checks (especially planar formats)? */
  9473. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  9474. return -EINVAL;
  9475. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  9476. intel_fb->obj = obj;
  9477. intel_fb->obj->framebuffer_references++;
  9478. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  9479. if (ret) {
  9480. DRM_ERROR("framebuffer init failed %d\n", ret);
  9481. return ret;
  9482. }
  9483. return 0;
  9484. }
  9485. static struct drm_framebuffer *
  9486. intel_user_framebuffer_create(struct drm_device *dev,
  9487. struct drm_file *filp,
  9488. struct drm_mode_fb_cmd2 *mode_cmd)
  9489. {
  9490. struct drm_i915_gem_object *obj;
  9491. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  9492. mode_cmd->handles[0]));
  9493. if (&obj->base == NULL)
  9494. return ERR_PTR(-ENOENT);
  9495. return intel_framebuffer_create(dev, mode_cmd, obj);
  9496. }
  9497. #ifndef CONFIG_DRM_I915_FBDEV
  9498. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  9499. {
  9500. }
  9501. #endif
  9502. static const struct drm_mode_config_funcs intel_mode_funcs = {
  9503. .fb_create = intel_user_framebuffer_create,
  9504. .output_poll_changed = intel_fbdev_output_poll_changed,
  9505. };
  9506. /* Set up chip specific display functions */
  9507. static void intel_init_display(struct drm_device *dev)
  9508. {
  9509. struct drm_i915_private *dev_priv = dev->dev_private;
  9510. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  9511. dev_priv->display.find_dpll = g4x_find_best_dpll;
  9512. else if (IS_CHERRYVIEW(dev))
  9513. dev_priv->display.find_dpll = chv_find_best_dpll;
  9514. else if (IS_VALLEYVIEW(dev))
  9515. dev_priv->display.find_dpll = vlv_find_best_dpll;
  9516. else if (IS_PINEVIEW(dev))
  9517. dev_priv->display.find_dpll = pnv_find_best_dpll;
  9518. else
  9519. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  9520. if (HAS_DDI(dev)) {
  9521. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  9522. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  9523. dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
  9524. dev_priv->display.crtc_enable = haswell_crtc_enable;
  9525. dev_priv->display.crtc_disable = haswell_crtc_disable;
  9526. dev_priv->display.off = haswell_crtc_off;
  9527. dev_priv->display.update_primary_plane =
  9528. ironlake_update_primary_plane;
  9529. } else if (HAS_PCH_SPLIT(dev)) {
  9530. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  9531. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  9532. dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
  9533. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  9534. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  9535. dev_priv->display.off = ironlake_crtc_off;
  9536. dev_priv->display.update_primary_plane =
  9537. ironlake_update_primary_plane;
  9538. } else if (IS_VALLEYVIEW(dev)) {
  9539. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  9540. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  9541. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  9542. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  9543. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  9544. dev_priv->display.off = i9xx_crtc_off;
  9545. dev_priv->display.update_primary_plane =
  9546. i9xx_update_primary_plane;
  9547. } else {
  9548. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  9549. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  9550. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  9551. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  9552. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  9553. dev_priv->display.off = i9xx_crtc_off;
  9554. dev_priv->display.update_primary_plane =
  9555. i9xx_update_primary_plane;
  9556. }
  9557. /* Returns the core display clock speed */
  9558. if (IS_VALLEYVIEW(dev))
  9559. dev_priv->display.get_display_clock_speed =
  9560. valleyview_get_display_clock_speed;
  9561. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  9562. dev_priv->display.get_display_clock_speed =
  9563. i945_get_display_clock_speed;
  9564. else if (IS_I915G(dev))
  9565. dev_priv->display.get_display_clock_speed =
  9566. i915_get_display_clock_speed;
  9567. else if (IS_I945GM(dev) || IS_845G(dev))
  9568. dev_priv->display.get_display_clock_speed =
  9569. i9xx_misc_get_display_clock_speed;
  9570. else if (IS_PINEVIEW(dev))
  9571. dev_priv->display.get_display_clock_speed =
  9572. pnv_get_display_clock_speed;
  9573. else if (IS_I915GM(dev))
  9574. dev_priv->display.get_display_clock_speed =
  9575. i915gm_get_display_clock_speed;
  9576. else if (IS_I865G(dev))
  9577. dev_priv->display.get_display_clock_speed =
  9578. i865_get_display_clock_speed;
  9579. else if (IS_I85X(dev))
  9580. dev_priv->display.get_display_clock_speed =
  9581. i855_get_display_clock_speed;
  9582. else /* 852, 830 */
  9583. dev_priv->display.get_display_clock_speed =
  9584. i830_get_display_clock_speed;
  9585. if (HAS_PCH_SPLIT(dev)) {
  9586. if (IS_GEN5(dev)) {
  9587. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  9588. dev_priv->display.write_eld = ironlake_write_eld;
  9589. } else if (IS_GEN6(dev)) {
  9590. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  9591. dev_priv->display.write_eld = ironlake_write_eld;
  9592. dev_priv->display.modeset_global_resources =
  9593. snb_modeset_global_resources;
  9594. } else if (IS_IVYBRIDGE(dev)) {
  9595. /* FIXME: detect B0+ stepping and use auto training */
  9596. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  9597. dev_priv->display.write_eld = ironlake_write_eld;
  9598. dev_priv->display.modeset_global_resources =
  9599. ivb_modeset_global_resources;
  9600. } else if (IS_HASWELL(dev) || IS_GEN8(dev)) {
  9601. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  9602. dev_priv->display.write_eld = haswell_write_eld;
  9603. dev_priv->display.modeset_global_resources =
  9604. haswell_modeset_global_resources;
  9605. }
  9606. } else if (IS_G4X(dev)) {
  9607. dev_priv->display.write_eld = g4x_write_eld;
  9608. } else if (IS_VALLEYVIEW(dev)) {
  9609. dev_priv->display.modeset_global_resources =
  9610. valleyview_modeset_global_resources;
  9611. dev_priv->display.write_eld = ironlake_write_eld;
  9612. }
  9613. /* Default just returns -ENODEV to indicate unsupported */
  9614. dev_priv->display.queue_flip = intel_default_queue_flip;
  9615. switch (INTEL_INFO(dev)->gen) {
  9616. case 2:
  9617. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  9618. break;
  9619. case 3:
  9620. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  9621. break;
  9622. case 4:
  9623. case 5:
  9624. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  9625. break;
  9626. case 6:
  9627. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  9628. break;
  9629. case 7:
  9630. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  9631. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  9632. break;
  9633. }
  9634. intel_panel_init_backlight_funcs(dev);
  9635. }
  9636. /*
  9637. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  9638. * resume, or other times. This quirk makes sure that's the case for
  9639. * affected systems.
  9640. */
  9641. static void quirk_pipea_force(struct drm_device *dev)
  9642. {
  9643. struct drm_i915_private *dev_priv = dev->dev_private;
  9644. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  9645. DRM_INFO("applying pipe a force quirk\n");
  9646. }
  9647. /*
  9648. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  9649. */
  9650. static void quirk_ssc_force_disable(struct drm_device *dev)
  9651. {
  9652. struct drm_i915_private *dev_priv = dev->dev_private;
  9653. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  9654. DRM_INFO("applying lvds SSC disable quirk\n");
  9655. }
  9656. /*
  9657. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  9658. * brightness value
  9659. */
  9660. static void quirk_invert_brightness(struct drm_device *dev)
  9661. {
  9662. struct drm_i915_private *dev_priv = dev->dev_private;
  9663. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  9664. DRM_INFO("applying inverted panel brightness quirk\n");
  9665. }
  9666. struct intel_quirk {
  9667. int device;
  9668. int subsystem_vendor;
  9669. int subsystem_device;
  9670. void (*hook)(struct drm_device *dev);
  9671. };
  9672. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  9673. struct intel_dmi_quirk {
  9674. void (*hook)(struct drm_device *dev);
  9675. const struct dmi_system_id (*dmi_id_list)[];
  9676. };
  9677. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  9678. {
  9679. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  9680. return 1;
  9681. }
  9682. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  9683. {
  9684. .dmi_id_list = &(const struct dmi_system_id[]) {
  9685. {
  9686. .callback = intel_dmi_reverse_brightness,
  9687. .ident = "NCR Corporation",
  9688. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  9689. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  9690. },
  9691. },
  9692. { } /* terminating entry */
  9693. },
  9694. .hook = quirk_invert_brightness,
  9695. },
  9696. };
  9697. static struct intel_quirk intel_quirks[] = {
  9698. /* HP Mini needs pipe A force quirk (LP: #322104) */
  9699. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  9700. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  9701. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  9702. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  9703. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  9704. /* Lenovo U160 cannot use SSC on LVDS */
  9705. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  9706. /* Sony Vaio Y cannot use SSC on LVDS */
  9707. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  9708. /* Acer Aspire 5734Z must invert backlight brightness */
  9709. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  9710. /* Acer/eMachines G725 */
  9711. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  9712. /* Acer/eMachines e725 */
  9713. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  9714. /* Acer/Packard Bell NCL20 */
  9715. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  9716. /* Acer Aspire 4736Z */
  9717. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  9718. /* Acer Aspire 5336 */
  9719. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  9720. };
  9721. static void intel_init_quirks(struct drm_device *dev)
  9722. {
  9723. struct pci_dev *d = dev->pdev;
  9724. int i;
  9725. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  9726. struct intel_quirk *q = &intel_quirks[i];
  9727. if (d->device == q->device &&
  9728. (d->subsystem_vendor == q->subsystem_vendor ||
  9729. q->subsystem_vendor == PCI_ANY_ID) &&
  9730. (d->subsystem_device == q->subsystem_device ||
  9731. q->subsystem_device == PCI_ANY_ID))
  9732. q->hook(dev);
  9733. }
  9734. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  9735. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  9736. intel_dmi_quirks[i].hook(dev);
  9737. }
  9738. }
  9739. /* Disable the VGA plane that we never use */
  9740. static void i915_disable_vga(struct drm_device *dev)
  9741. {
  9742. struct drm_i915_private *dev_priv = dev->dev_private;
  9743. u8 sr1;
  9744. u32 vga_reg = i915_vgacntrl_reg(dev);
  9745. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  9746. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  9747. outb(SR01, VGA_SR_INDEX);
  9748. sr1 = inb(VGA_SR_DATA);
  9749. outb(sr1 | 1<<5, VGA_SR_DATA);
  9750. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  9751. udelay(300);
  9752. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  9753. POSTING_READ(vga_reg);
  9754. }
  9755. void intel_modeset_init_hw(struct drm_device *dev)
  9756. {
  9757. intel_prepare_ddi(dev);
  9758. intel_init_clock_gating(dev);
  9759. intel_reset_dpio(dev);
  9760. intel_enable_gt_powersave(dev);
  9761. }
  9762. void intel_modeset_suspend_hw(struct drm_device *dev)
  9763. {
  9764. intel_suspend_hw(dev);
  9765. }
  9766. void intel_modeset_init(struct drm_device *dev)
  9767. {
  9768. struct drm_i915_private *dev_priv = dev->dev_private;
  9769. int sprite, ret;
  9770. enum pipe pipe;
  9771. struct intel_crtc *crtc;
  9772. drm_mode_config_init(dev);
  9773. dev->mode_config.min_width = 0;
  9774. dev->mode_config.min_height = 0;
  9775. dev->mode_config.preferred_depth = 24;
  9776. dev->mode_config.prefer_shadow = 1;
  9777. dev->mode_config.funcs = &intel_mode_funcs;
  9778. intel_init_quirks(dev);
  9779. intel_init_pm(dev);
  9780. if (INTEL_INFO(dev)->num_pipes == 0)
  9781. return;
  9782. intel_init_display(dev);
  9783. if (IS_GEN2(dev)) {
  9784. dev->mode_config.max_width = 2048;
  9785. dev->mode_config.max_height = 2048;
  9786. } else if (IS_GEN3(dev)) {
  9787. dev->mode_config.max_width = 4096;
  9788. dev->mode_config.max_height = 4096;
  9789. } else {
  9790. dev->mode_config.max_width = 8192;
  9791. dev->mode_config.max_height = 8192;
  9792. }
  9793. if (IS_GEN2(dev)) {
  9794. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  9795. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  9796. } else {
  9797. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  9798. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  9799. }
  9800. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  9801. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  9802. INTEL_INFO(dev)->num_pipes,
  9803. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  9804. for_each_pipe(pipe) {
  9805. intel_crtc_init(dev, pipe);
  9806. for_each_sprite(pipe, sprite) {
  9807. ret = intel_plane_init(dev, pipe, sprite);
  9808. if (ret)
  9809. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  9810. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  9811. }
  9812. }
  9813. intel_init_dpio(dev);
  9814. intel_reset_dpio(dev);
  9815. intel_cpu_pll_init(dev);
  9816. intel_shared_dpll_init(dev);
  9817. /* Just disable it once at startup */
  9818. i915_disable_vga(dev);
  9819. intel_setup_outputs(dev);
  9820. /* Just in case the BIOS is doing something questionable. */
  9821. intel_disable_fbc(dev);
  9822. drm_modeset_lock_all(dev);
  9823. intel_modeset_setup_hw_state(dev, false);
  9824. drm_modeset_unlock_all(dev);
  9825. for_each_intel_crtc(dev, crtc) {
  9826. if (!crtc->active)
  9827. continue;
  9828. /*
  9829. * Note that reserving the BIOS fb up front prevents us
  9830. * from stuffing other stolen allocations like the ring
  9831. * on top. This prevents some ugliness at boot time, and
  9832. * can even allow for smooth boot transitions if the BIOS
  9833. * fb is large enough for the active pipe configuration.
  9834. */
  9835. if (dev_priv->display.get_plane_config) {
  9836. dev_priv->display.get_plane_config(crtc,
  9837. &crtc->plane_config);
  9838. /*
  9839. * If the fb is shared between multiple heads, we'll
  9840. * just get the first one.
  9841. */
  9842. intel_find_plane_obj(crtc, &crtc->plane_config);
  9843. }
  9844. }
  9845. }
  9846. static void intel_enable_pipe_a(struct drm_device *dev)
  9847. {
  9848. struct intel_connector *connector;
  9849. struct drm_connector *crt = NULL;
  9850. struct intel_load_detect_pipe load_detect_temp;
  9851. struct drm_modeset_acquire_ctx ctx;
  9852. /* We can't just switch on the pipe A, we need to set things up with a
  9853. * proper mode and output configuration. As a gross hack, enable pipe A
  9854. * by enabling the load detect pipe once. */
  9855. list_for_each_entry(connector,
  9856. &dev->mode_config.connector_list,
  9857. base.head) {
  9858. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  9859. crt = &connector->base;
  9860. break;
  9861. }
  9862. }
  9863. if (!crt)
  9864. return;
  9865. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, &ctx))
  9866. intel_release_load_detect_pipe(crt, &load_detect_temp, &ctx);
  9867. }
  9868. static bool
  9869. intel_check_plane_mapping(struct intel_crtc *crtc)
  9870. {
  9871. struct drm_device *dev = crtc->base.dev;
  9872. struct drm_i915_private *dev_priv = dev->dev_private;
  9873. u32 reg, val;
  9874. if (INTEL_INFO(dev)->num_pipes == 1)
  9875. return true;
  9876. reg = DSPCNTR(!crtc->plane);
  9877. val = I915_READ(reg);
  9878. if ((val & DISPLAY_PLANE_ENABLE) &&
  9879. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  9880. return false;
  9881. return true;
  9882. }
  9883. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  9884. {
  9885. struct drm_device *dev = crtc->base.dev;
  9886. struct drm_i915_private *dev_priv = dev->dev_private;
  9887. u32 reg;
  9888. /* Clear any frame start delays used for debugging left by the BIOS */
  9889. reg = PIPECONF(crtc->config.cpu_transcoder);
  9890. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  9891. /* restore vblank interrupts to correct state */
  9892. if (crtc->active)
  9893. drm_vblank_on(dev, crtc->pipe);
  9894. else
  9895. drm_vblank_off(dev, crtc->pipe);
  9896. /* We need to sanitize the plane -> pipe mapping first because this will
  9897. * disable the crtc (and hence change the state) if it is wrong. Note
  9898. * that gen4+ has a fixed plane -> pipe mapping. */
  9899. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  9900. struct intel_connector *connector;
  9901. bool plane;
  9902. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  9903. crtc->base.base.id);
  9904. /* Pipe has the wrong plane attached and the plane is active.
  9905. * Temporarily change the plane mapping and disable everything
  9906. * ... */
  9907. plane = crtc->plane;
  9908. crtc->plane = !plane;
  9909. dev_priv->display.crtc_disable(&crtc->base);
  9910. crtc->plane = plane;
  9911. /* ... and break all links. */
  9912. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9913. base.head) {
  9914. if (connector->encoder->base.crtc != &crtc->base)
  9915. continue;
  9916. connector->base.dpms = DRM_MODE_DPMS_OFF;
  9917. connector->base.encoder = NULL;
  9918. }
  9919. /* multiple connectors may have the same encoder:
  9920. * handle them and break crtc link separately */
  9921. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9922. base.head)
  9923. if (connector->encoder->base.crtc == &crtc->base) {
  9924. connector->encoder->base.crtc = NULL;
  9925. connector->encoder->connectors_active = false;
  9926. }
  9927. WARN_ON(crtc->active);
  9928. crtc->base.enabled = false;
  9929. }
  9930. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  9931. crtc->pipe == PIPE_A && !crtc->active) {
  9932. /* BIOS forgot to enable pipe A, this mostly happens after
  9933. * resume. Force-enable the pipe to fix this, the update_dpms
  9934. * call below we restore the pipe to the right state, but leave
  9935. * the required bits on. */
  9936. intel_enable_pipe_a(dev);
  9937. }
  9938. /* Adjust the state of the output pipe according to whether we
  9939. * have active connectors/encoders. */
  9940. intel_crtc_update_dpms(&crtc->base);
  9941. if (crtc->active != crtc->base.enabled) {
  9942. struct intel_encoder *encoder;
  9943. /* This can happen either due to bugs in the get_hw_state
  9944. * functions or because the pipe is force-enabled due to the
  9945. * pipe A quirk. */
  9946. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  9947. crtc->base.base.id,
  9948. crtc->base.enabled ? "enabled" : "disabled",
  9949. crtc->active ? "enabled" : "disabled");
  9950. crtc->base.enabled = crtc->active;
  9951. /* Because we only establish the connector -> encoder ->
  9952. * crtc links if something is active, this means the
  9953. * crtc is now deactivated. Break the links. connector
  9954. * -> encoder links are only establish when things are
  9955. * actually up, hence no need to break them. */
  9956. WARN_ON(crtc->active);
  9957. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  9958. WARN_ON(encoder->connectors_active);
  9959. encoder->base.crtc = NULL;
  9960. }
  9961. }
  9962. if (crtc->active || IS_VALLEYVIEW(dev) || INTEL_INFO(dev)->gen < 5) {
  9963. /*
  9964. * We start out with underrun reporting disabled to avoid races.
  9965. * For correct bookkeeping mark this on active crtcs.
  9966. *
  9967. * Also on gmch platforms we dont have any hardware bits to
  9968. * disable the underrun reporting. Which means we need to start
  9969. * out with underrun reporting disabled also on inactive pipes,
  9970. * since otherwise we'll complain about the garbage we read when
  9971. * e.g. coming up after runtime pm.
  9972. *
  9973. * No protection against concurrent access is required - at
  9974. * worst a fifo underrun happens which also sets this to false.
  9975. */
  9976. crtc->cpu_fifo_underrun_disabled = true;
  9977. crtc->pch_fifo_underrun_disabled = true;
  9978. update_scanline_offset(crtc);
  9979. }
  9980. }
  9981. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  9982. {
  9983. struct intel_connector *connector;
  9984. struct drm_device *dev = encoder->base.dev;
  9985. /* We need to check both for a crtc link (meaning that the
  9986. * encoder is active and trying to read from a pipe) and the
  9987. * pipe itself being active. */
  9988. bool has_active_crtc = encoder->base.crtc &&
  9989. to_intel_crtc(encoder->base.crtc)->active;
  9990. if (encoder->connectors_active && !has_active_crtc) {
  9991. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  9992. encoder->base.base.id,
  9993. encoder->base.name);
  9994. /* Connector is active, but has no active pipe. This is
  9995. * fallout from our resume register restoring. Disable
  9996. * the encoder manually again. */
  9997. if (encoder->base.crtc) {
  9998. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  9999. encoder->base.base.id,
  10000. encoder->base.name);
  10001. encoder->disable(encoder);
  10002. }
  10003. encoder->base.crtc = NULL;
  10004. encoder->connectors_active = false;
  10005. /* Inconsistent output/port/pipe state happens presumably due to
  10006. * a bug in one of the get_hw_state functions. Or someplace else
  10007. * in our code, like the register restore mess on resume. Clamp
  10008. * things to off as a safer default. */
  10009. list_for_each_entry(connector,
  10010. &dev->mode_config.connector_list,
  10011. base.head) {
  10012. if (connector->encoder != encoder)
  10013. continue;
  10014. connector->base.dpms = DRM_MODE_DPMS_OFF;
  10015. connector->base.encoder = NULL;
  10016. }
  10017. }
  10018. /* Enabled encoders without active connectors will be fixed in
  10019. * the crtc fixup. */
  10020. }
  10021. void i915_redisable_vga_power_on(struct drm_device *dev)
  10022. {
  10023. struct drm_i915_private *dev_priv = dev->dev_private;
  10024. u32 vga_reg = i915_vgacntrl_reg(dev);
  10025. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  10026. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  10027. i915_disable_vga(dev);
  10028. }
  10029. }
  10030. void i915_redisable_vga(struct drm_device *dev)
  10031. {
  10032. struct drm_i915_private *dev_priv = dev->dev_private;
  10033. /* This function can be called both from intel_modeset_setup_hw_state or
  10034. * at a very early point in our resume sequence, where the power well
  10035. * structures are not yet restored. Since this function is at a very
  10036. * paranoid "someone might have enabled VGA while we were not looking"
  10037. * level, just check if the power well is enabled instead of trying to
  10038. * follow the "don't touch the power well if we don't need it" policy
  10039. * the rest of the driver uses. */
  10040. if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_VGA))
  10041. return;
  10042. i915_redisable_vga_power_on(dev);
  10043. }
  10044. static bool primary_get_hw_state(struct intel_crtc *crtc)
  10045. {
  10046. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  10047. if (!crtc->active)
  10048. return false;
  10049. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  10050. }
  10051. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  10052. {
  10053. struct drm_i915_private *dev_priv = dev->dev_private;
  10054. enum pipe pipe;
  10055. struct intel_crtc *crtc;
  10056. struct intel_encoder *encoder;
  10057. struct intel_connector *connector;
  10058. int i;
  10059. for_each_intel_crtc(dev, crtc) {
  10060. memset(&crtc->config, 0, sizeof(crtc->config));
  10061. crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  10062. crtc->active = dev_priv->display.get_pipe_config(crtc,
  10063. &crtc->config);
  10064. crtc->base.enabled = crtc->active;
  10065. crtc->primary_enabled = primary_get_hw_state(crtc);
  10066. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  10067. crtc->base.base.id,
  10068. crtc->active ? "enabled" : "disabled");
  10069. }
  10070. /* FIXME: Smash this into the new shared dpll infrastructure. */
  10071. if (HAS_DDI(dev))
  10072. intel_ddi_setup_hw_pll_state(dev);
  10073. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10074. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10075. pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
  10076. pll->active = 0;
  10077. for_each_intel_crtc(dev, crtc) {
  10078. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10079. pll->active++;
  10080. }
  10081. pll->refcount = pll->active;
  10082. DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
  10083. pll->name, pll->refcount, pll->on);
  10084. }
  10085. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  10086. base.head) {
  10087. pipe = 0;
  10088. if (encoder->get_hw_state(encoder, &pipe)) {
  10089. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  10090. encoder->base.crtc = &crtc->base;
  10091. encoder->get_config(encoder, &crtc->config);
  10092. } else {
  10093. encoder->base.crtc = NULL;
  10094. }
  10095. encoder->connectors_active = false;
  10096. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  10097. encoder->base.base.id,
  10098. encoder->base.name,
  10099. encoder->base.crtc ? "enabled" : "disabled",
  10100. pipe_name(pipe));
  10101. }
  10102. list_for_each_entry(connector, &dev->mode_config.connector_list,
  10103. base.head) {
  10104. if (connector->get_hw_state(connector)) {
  10105. connector->base.dpms = DRM_MODE_DPMS_ON;
  10106. connector->encoder->connectors_active = true;
  10107. connector->base.encoder = &connector->encoder->base;
  10108. } else {
  10109. connector->base.dpms = DRM_MODE_DPMS_OFF;
  10110. connector->base.encoder = NULL;
  10111. }
  10112. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  10113. connector->base.base.id,
  10114. connector->base.name,
  10115. connector->base.encoder ? "enabled" : "disabled");
  10116. }
  10117. }
  10118. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  10119. * and i915 state tracking structures. */
  10120. void intel_modeset_setup_hw_state(struct drm_device *dev,
  10121. bool force_restore)
  10122. {
  10123. struct drm_i915_private *dev_priv = dev->dev_private;
  10124. enum pipe pipe;
  10125. struct intel_crtc *crtc;
  10126. struct intel_encoder *encoder;
  10127. int i;
  10128. intel_modeset_readout_hw_state(dev);
  10129. /*
  10130. * Now that we have the config, copy it to each CRTC struct
  10131. * Note that this could go away if we move to using crtc_config
  10132. * checking everywhere.
  10133. */
  10134. for_each_intel_crtc(dev, crtc) {
  10135. if (crtc->active && i915.fastboot) {
  10136. intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
  10137. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  10138. crtc->base.base.id);
  10139. drm_mode_debug_printmodeline(&crtc->base.mode);
  10140. }
  10141. }
  10142. /* HW state is read out, now we need to sanitize this mess. */
  10143. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  10144. base.head) {
  10145. intel_sanitize_encoder(encoder);
  10146. }
  10147. for_each_pipe(pipe) {
  10148. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  10149. intel_sanitize_crtc(crtc);
  10150. intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
  10151. }
  10152. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10153. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10154. if (!pll->on || pll->active)
  10155. continue;
  10156. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  10157. pll->disable(dev_priv, pll);
  10158. pll->on = false;
  10159. }
  10160. if (HAS_PCH_SPLIT(dev))
  10161. ilk_wm_get_hw_state(dev);
  10162. if (force_restore) {
  10163. i915_redisable_vga(dev);
  10164. /*
  10165. * We need to use raw interfaces for restoring state to avoid
  10166. * checking (bogus) intermediate states.
  10167. */
  10168. for_each_pipe(pipe) {
  10169. struct drm_crtc *crtc =
  10170. dev_priv->pipe_to_crtc_mapping[pipe];
  10171. __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
  10172. crtc->primary->fb);
  10173. }
  10174. } else {
  10175. intel_modeset_update_staged_output_state(dev);
  10176. }
  10177. intel_modeset_check_state(dev);
  10178. }
  10179. void intel_modeset_gem_init(struct drm_device *dev)
  10180. {
  10181. struct drm_crtc *c;
  10182. struct intel_framebuffer *fb;
  10183. mutex_lock(&dev->struct_mutex);
  10184. intel_init_gt_powersave(dev);
  10185. mutex_unlock(&dev->struct_mutex);
  10186. intel_modeset_init_hw(dev);
  10187. intel_setup_overlay(dev);
  10188. /*
  10189. * Make sure any fbs we allocated at startup are properly
  10190. * pinned & fenced. When we do the allocation it's too early
  10191. * for this.
  10192. */
  10193. mutex_lock(&dev->struct_mutex);
  10194. for_each_crtc(dev, c) {
  10195. if (!c->primary->fb)
  10196. continue;
  10197. fb = to_intel_framebuffer(c->primary->fb);
  10198. if (intel_pin_and_fence_fb_obj(dev, fb->obj, NULL)) {
  10199. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  10200. to_intel_crtc(c)->pipe);
  10201. drm_framebuffer_unreference(c->primary->fb);
  10202. c->primary->fb = NULL;
  10203. }
  10204. }
  10205. mutex_unlock(&dev->struct_mutex);
  10206. }
  10207. void intel_connector_unregister(struct intel_connector *intel_connector)
  10208. {
  10209. struct drm_connector *connector = &intel_connector->base;
  10210. intel_panel_destroy_backlight(connector);
  10211. drm_sysfs_connector_remove(connector);
  10212. }
  10213. void intel_modeset_cleanup(struct drm_device *dev)
  10214. {
  10215. struct drm_i915_private *dev_priv = dev->dev_private;
  10216. struct drm_crtc *crtc;
  10217. struct drm_connector *connector;
  10218. /*
  10219. * Interrupts and polling as the first thing to avoid creating havoc.
  10220. * Too much stuff here (turning of rps, connectors, ...) would
  10221. * experience fancy races otherwise.
  10222. */
  10223. drm_irq_uninstall(dev);
  10224. cancel_work_sync(&dev_priv->hotplug_work);
  10225. /*
  10226. * Due to the hpd irq storm handling the hotplug work can re-arm the
  10227. * poll handlers. Hence disable polling after hpd handling is shut down.
  10228. */
  10229. drm_kms_helper_poll_fini(dev);
  10230. mutex_lock(&dev->struct_mutex);
  10231. intel_unregister_dsm_handler();
  10232. for_each_crtc(dev, crtc) {
  10233. /* Skip inactive CRTCs */
  10234. if (!crtc->primary->fb)
  10235. continue;
  10236. intel_increase_pllclock(crtc);
  10237. }
  10238. intel_disable_fbc(dev);
  10239. intel_disable_gt_powersave(dev);
  10240. ironlake_teardown_rc6(dev);
  10241. mutex_unlock(&dev->struct_mutex);
  10242. /* flush any delayed tasks or pending work */
  10243. flush_scheduled_work();
  10244. /* destroy the backlight and sysfs files before encoders/connectors */
  10245. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  10246. struct intel_connector *intel_connector;
  10247. intel_connector = to_intel_connector(connector);
  10248. intel_connector->unregister(intel_connector);
  10249. }
  10250. drm_mode_config_cleanup(dev);
  10251. intel_cleanup_overlay(dev);
  10252. mutex_lock(&dev->struct_mutex);
  10253. intel_cleanup_gt_powersave(dev);
  10254. mutex_unlock(&dev->struct_mutex);
  10255. }
  10256. /*
  10257. * Return which encoder is currently attached for connector.
  10258. */
  10259. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  10260. {
  10261. return &intel_attached_encoder(connector)->base;
  10262. }
  10263. void intel_connector_attach_encoder(struct intel_connector *connector,
  10264. struct intel_encoder *encoder)
  10265. {
  10266. connector->encoder = encoder;
  10267. drm_mode_connector_attach_encoder(&connector->base,
  10268. &encoder->base);
  10269. }
  10270. /*
  10271. * set vga decode state - true == enable VGA decode
  10272. */
  10273. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  10274. {
  10275. struct drm_i915_private *dev_priv = dev->dev_private;
  10276. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  10277. u16 gmch_ctrl;
  10278. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  10279. DRM_ERROR("failed to read control word\n");
  10280. return -EIO;
  10281. }
  10282. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  10283. return 0;
  10284. if (state)
  10285. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  10286. else
  10287. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  10288. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  10289. DRM_ERROR("failed to write control word\n");
  10290. return -EIO;
  10291. }
  10292. return 0;
  10293. }
  10294. struct intel_display_error_state {
  10295. u32 power_well_driver;
  10296. int num_transcoders;
  10297. struct intel_cursor_error_state {
  10298. u32 control;
  10299. u32 position;
  10300. u32 base;
  10301. u32 size;
  10302. } cursor[I915_MAX_PIPES];
  10303. struct intel_pipe_error_state {
  10304. bool power_domain_on;
  10305. u32 source;
  10306. u32 stat;
  10307. } pipe[I915_MAX_PIPES];
  10308. struct intel_plane_error_state {
  10309. u32 control;
  10310. u32 stride;
  10311. u32 size;
  10312. u32 pos;
  10313. u32 addr;
  10314. u32 surface;
  10315. u32 tile_offset;
  10316. } plane[I915_MAX_PIPES];
  10317. struct intel_transcoder_error_state {
  10318. bool power_domain_on;
  10319. enum transcoder cpu_transcoder;
  10320. u32 conf;
  10321. u32 htotal;
  10322. u32 hblank;
  10323. u32 hsync;
  10324. u32 vtotal;
  10325. u32 vblank;
  10326. u32 vsync;
  10327. } transcoder[4];
  10328. };
  10329. struct intel_display_error_state *
  10330. intel_display_capture_error_state(struct drm_device *dev)
  10331. {
  10332. struct drm_i915_private *dev_priv = dev->dev_private;
  10333. struct intel_display_error_state *error;
  10334. int transcoders[] = {
  10335. TRANSCODER_A,
  10336. TRANSCODER_B,
  10337. TRANSCODER_C,
  10338. TRANSCODER_EDP,
  10339. };
  10340. int i;
  10341. if (INTEL_INFO(dev)->num_pipes == 0)
  10342. return NULL;
  10343. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  10344. if (error == NULL)
  10345. return NULL;
  10346. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  10347. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  10348. for_each_pipe(i) {
  10349. error->pipe[i].power_domain_on =
  10350. intel_display_power_enabled_sw(dev_priv,
  10351. POWER_DOMAIN_PIPE(i));
  10352. if (!error->pipe[i].power_domain_on)
  10353. continue;
  10354. error->cursor[i].control = I915_READ(CURCNTR(i));
  10355. error->cursor[i].position = I915_READ(CURPOS(i));
  10356. error->cursor[i].base = I915_READ(CURBASE(i));
  10357. error->plane[i].control = I915_READ(DSPCNTR(i));
  10358. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  10359. if (INTEL_INFO(dev)->gen <= 3) {
  10360. error->plane[i].size = I915_READ(DSPSIZE(i));
  10361. error->plane[i].pos = I915_READ(DSPPOS(i));
  10362. }
  10363. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  10364. error->plane[i].addr = I915_READ(DSPADDR(i));
  10365. if (INTEL_INFO(dev)->gen >= 4) {
  10366. error->plane[i].surface = I915_READ(DSPSURF(i));
  10367. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  10368. }
  10369. error->pipe[i].source = I915_READ(PIPESRC(i));
  10370. if (!HAS_PCH_SPLIT(dev))
  10371. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  10372. }
  10373. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  10374. if (HAS_DDI(dev_priv->dev))
  10375. error->num_transcoders++; /* Account for eDP. */
  10376. for (i = 0; i < error->num_transcoders; i++) {
  10377. enum transcoder cpu_transcoder = transcoders[i];
  10378. error->transcoder[i].power_domain_on =
  10379. intel_display_power_enabled_sw(dev_priv,
  10380. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  10381. if (!error->transcoder[i].power_domain_on)
  10382. continue;
  10383. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  10384. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  10385. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  10386. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  10387. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  10388. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  10389. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  10390. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  10391. }
  10392. return error;
  10393. }
  10394. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  10395. void
  10396. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  10397. struct drm_device *dev,
  10398. struct intel_display_error_state *error)
  10399. {
  10400. int i;
  10401. if (!error)
  10402. return;
  10403. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  10404. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  10405. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  10406. error->power_well_driver);
  10407. for_each_pipe(i) {
  10408. err_printf(m, "Pipe [%d]:\n", i);
  10409. err_printf(m, " Power: %s\n",
  10410. error->pipe[i].power_domain_on ? "on" : "off");
  10411. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  10412. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  10413. err_printf(m, "Plane [%d]:\n", i);
  10414. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  10415. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  10416. if (INTEL_INFO(dev)->gen <= 3) {
  10417. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  10418. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  10419. }
  10420. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  10421. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  10422. if (INTEL_INFO(dev)->gen >= 4) {
  10423. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  10424. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  10425. }
  10426. err_printf(m, "Cursor [%d]:\n", i);
  10427. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  10428. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  10429. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  10430. }
  10431. for (i = 0; i < error->num_transcoders; i++) {
  10432. err_printf(m, "CPU transcoder: %c\n",
  10433. transcoder_name(error->transcoder[i].cpu_transcoder));
  10434. err_printf(m, " Power: %s\n",
  10435. error->transcoder[i].power_domain_on ? "on" : "off");
  10436. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  10437. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  10438. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  10439. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  10440. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  10441. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  10442. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  10443. }
  10444. }