intel_display.c 384 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941
  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 <drm/drm_plane_helper.h>
  42. #include <drm/drm_rect.h>
  43. #include <linux/dma_remapping.h>
  44. /* Primary plane formats supported by all gen */
  45. #define COMMON_PRIMARY_FORMATS \
  46. DRM_FORMAT_C8, \
  47. DRM_FORMAT_RGB565, \
  48. DRM_FORMAT_XRGB8888, \
  49. DRM_FORMAT_ARGB8888
  50. /* Primary plane formats for gen <= 3 */
  51. static const uint32_t intel_primary_formats_gen2[] = {
  52. COMMON_PRIMARY_FORMATS,
  53. DRM_FORMAT_XRGB1555,
  54. DRM_FORMAT_ARGB1555,
  55. };
  56. /* Primary plane formats for gen >= 4 */
  57. static const uint32_t intel_primary_formats_gen4[] = {
  58. COMMON_PRIMARY_FORMATS, \
  59. DRM_FORMAT_XBGR8888,
  60. DRM_FORMAT_ABGR8888,
  61. DRM_FORMAT_XRGB2101010,
  62. DRM_FORMAT_ARGB2101010,
  63. DRM_FORMAT_XBGR2101010,
  64. DRM_FORMAT_ABGR2101010,
  65. };
  66. /* Cursor formats */
  67. static const uint32_t intel_cursor_formats[] = {
  68. DRM_FORMAT_ARGB8888,
  69. };
  70. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  71. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  72. struct intel_crtc_config *pipe_config);
  73. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  74. struct intel_crtc_config *pipe_config);
  75. static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
  76. int x, int y, struct drm_framebuffer *old_fb);
  77. static int intel_framebuffer_init(struct drm_device *dev,
  78. struct intel_framebuffer *ifb,
  79. struct drm_mode_fb_cmd2 *mode_cmd,
  80. struct drm_i915_gem_object *obj);
  81. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  82. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  83. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  84. struct intel_link_m_n *m_n,
  85. struct intel_link_m_n *m2_n2);
  86. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  87. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  88. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  89. static void vlv_prepare_pll(struct intel_crtc *crtc,
  90. const struct intel_crtc_config *pipe_config);
  91. static void chv_prepare_pll(struct intel_crtc *crtc,
  92. const struct intel_crtc_config *pipe_config);
  93. static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
  94. {
  95. if (!connector->mst_port)
  96. return connector->encoder;
  97. else
  98. return &connector->mst_port->mst_encoders[pipe]->base;
  99. }
  100. typedef struct {
  101. int min, max;
  102. } intel_range_t;
  103. typedef struct {
  104. int dot_limit;
  105. int p2_slow, p2_fast;
  106. } intel_p2_t;
  107. typedef struct intel_limit intel_limit_t;
  108. struct intel_limit {
  109. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  110. intel_p2_t p2;
  111. };
  112. int
  113. intel_pch_rawclk(struct drm_device *dev)
  114. {
  115. struct drm_i915_private *dev_priv = dev->dev_private;
  116. WARN_ON(!HAS_PCH_SPLIT(dev));
  117. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  118. }
  119. static inline u32 /* units of 100MHz */
  120. intel_fdi_link_freq(struct drm_device *dev)
  121. {
  122. if (IS_GEN5(dev)) {
  123. struct drm_i915_private *dev_priv = dev->dev_private;
  124. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  125. } else
  126. return 27;
  127. }
  128. static const intel_limit_t intel_limits_i8xx_dac = {
  129. .dot = { .min = 25000, .max = 350000 },
  130. .vco = { .min = 908000, .max = 1512000 },
  131. .n = { .min = 2, .max = 16 },
  132. .m = { .min = 96, .max = 140 },
  133. .m1 = { .min = 18, .max = 26 },
  134. .m2 = { .min = 6, .max = 16 },
  135. .p = { .min = 4, .max = 128 },
  136. .p1 = { .min = 2, .max = 33 },
  137. .p2 = { .dot_limit = 165000,
  138. .p2_slow = 4, .p2_fast = 2 },
  139. };
  140. static const intel_limit_t intel_limits_i8xx_dvo = {
  141. .dot = { .min = 25000, .max = 350000 },
  142. .vco = { .min = 908000, .max = 1512000 },
  143. .n = { .min = 2, .max = 16 },
  144. .m = { .min = 96, .max = 140 },
  145. .m1 = { .min = 18, .max = 26 },
  146. .m2 = { .min = 6, .max = 16 },
  147. .p = { .min = 4, .max = 128 },
  148. .p1 = { .min = 2, .max = 33 },
  149. .p2 = { .dot_limit = 165000,
  150. .p2_slow = 4, .p2_fast = 4 },
  151. };
  152. static const intel_limit_t intel_limits_i8xx_lvds = {
  153. .dot = { .min = 25000, .max = 350000 },
  154. .vco = { .min = 908000, .max = 1512000 },
  155. .n = { .min = 2, .max = 16 },
  156. .m = { .min = 96, .max = 140 },
  157. .m1 = { .min = 18, .max = 26 },
  158. .m2 = { .min = 6, .max = 16 },
  159. .p = { .min = 4, .max = 128 },
  160. .p1 = { .min = 1, .max = 6 },
  161. .p2 = { .dot_limit = 165000,
  162. .p2_slow = 14, .p2_fast = 7 },
  163. };
  164. static const intel_limit_t intel_limits_i9xx_sdvo = {
  165. .dot = { .min = 20000, .max = 400000 },
  166. .vco = { .min = 1400000, .max = 2800000 },
  167. .n = { .min = 1, .max = 6 },
  168. .m = { .min = 70, .max = 120 },
  169. .m1 = { .min = 8, .max = 18 },
  170. .m2 = { .min = 3, .max = 7 },
  171. .p = { .min = 5, .max = 80 },
  172. .p1 = { .min = 1, .max = 8 },
  173. .p2 = { .dot_limit = 200000,
  174. .p2_slow = 10, .p2_fast = 5 },
  175. };
  176. static const intel_limit_t intel_limits_i9xx_lvds = {
  177. .dot = { .min = 20000, .max = 400000 },
  178. .vco = { .min = 1400000, .max = 2800000 },
  179. .n = { .min = 1, .max = 6 },
  180. .m = { .min = 70, .max = 120 },
  181. .m1 = { .min = 8, .max = 18 },
  182. .m2 = { .min = 3, .max = 7 },
  183. .p = { .min = 7, .max = 98 },
  184. .p1 = { .min = 1, .max = 8 },
  185. .p2 = { .dot_limit = 112000,
  186. .p2_slow = 14, .p2_fast = 7 },
  187. };
  188. static const intel_limit_t intel_limits_g4x_sdvo = {
  189. .dot = { .min = 25000, .max = 270000 },
  190. .vco = { .min = 1750000, .max = 3500000},
  191. .n = { .min = 1, .max = 4 },
  192. .m = { .min = 104, .max = 138 },
  193. .m1 = { .min = 17, .max = 23 },
  194. .m2 = { .min = 5, .max = 11 },
  195. .p = { .min = 10, .max = 30 },
  196. .p1 = { .min = 1, .max = 3},
  197. .p2 = { .dot_limit = 270000,
  198. .p2_slow = 10,
  199. .p2_fast = 10
  200. },
  201. };
  202. static const intel_limit_t intel_limits_g4x_hdmi = {
  203. .dot = { .min = 22000, .max = 400000 },
  204. .vco = { .min = 1750000, .max = 3500000},
  205. .n = { .min = 1, .max = 4 },
  206. .m = { .min = 104, .max = 138 },
  207. .m1 = { .min = 16, .max = 23 },
  208. .m2 = { .min = 5, .max = 11 },
  209. .p = { .min = 5, .max = 80 },
  210. .p1 = { .min = 1, .max = 8},
  211. .p2 = { .dot_limit = 165000,
  212. .p2_slow = 10, .p2_fast = 5 },
  213. };
  214. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  215. .dot = { .min = 20000, .max = 115000 },
  216. .vco = { .min = 1750000, .max = 3500000 },
  217. .n = { .min = 1, .max = 3 },
  218. .m = { .min = 104, .max = 138 },
  219. .m1 = { .min = 17, .max = 23 },
  220. .m2 = { .min = 5, .max = 11 },
  221. .p = { .min = 28, .max = 112 },
  222. .p1 = { .min = 2, .max = 8 },
  223. .p2 = { .dot_limit = 0,
  224. .p2_slow = 14, .p2_fast = 14
  225. },
  226. };
  227. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  228. .dot = { .min = 80000, .max = 224000 },
  229. .vco = { .min = 1750000, .max = 3500000 },
  230. .n = { .min = 1, .max = 3 },
  231. .m = { .min = 104, .max = 138 },
  232. .m1 = { .min = 17, .max = 23 },
  233. .m2 = { .min = 5, .max = 11 },
  234. .p = { .min = 14, .max = 42 },
  235. .p1 = { .min = 2, .max = 6 },
  236. .p2 = { .dot_limit = 0,
  237. .p2_slow = 7, .p2_fast = 7
  238. },
  239. };
  240. static const intel_limit_t intel_limits_pineview_sdvo = {
  241. .dot = { .min = 20000, .max = 400000},
  242. .vco = { .min = 1700000, .max = 3500000 },
  243. /* Pineview's Ncounter is a ring counter */
  244. .n = { .min = 3, .max = 6 },
  245. .m = { .min = 2, .max = 256 },
  246. /* Pineview only has one combined m divider, which we treat as m2. */
  247. .m1 = { .min = 0, .max = 0 },
  248. .m2 = { .min = 0, .max = 254 },
  249. .p = { .min = 5, .max = 80 },
  250. .p1 = { .min = 1, .max = 8 },
  251. .p2 = { .dot_limit = 200000,
  252. .p2_slow = 10, .p2_fast = 5 },
  253. };
  254. static const intel_limit_t intel_limits_pineview_lvds = {
  255. .dot = { .min = 20000, .max = 400000 },
  256. .vco = { .min = 1700000, .max = 3500000 },
  257. .n = { .min = 3, .max = 6 },
  258. .m = { .min = 2, .max = 256 },
  259. .m1 = { .min = 0, .max = 0 },
  260. .m2 = { .min = 0, .max = 254 },
  261. .p = { .min = 7, .max = 112 },
  262. .p1 = { .min = 1, .max = 8 },
  263. .p2 = { .dot_limit = 112000,
  264. .p2_slow = 14, .p2_fast = 14 },
  265. };
  266. /* Ironlake / Sandybridge
  267. *
  268. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  269. * the range value for them is (actual_value - 2).
  270. */
  271. static const intel_limit_t intel_limits_ironlake_dac = {
  272. .dot = { .min = 25000, .max = 350000 },
  273. .vco = { .min = 1760000, .max = 3510000 },
  274. .n = { .min = 1, .max = 5 },
  275. .m = { .min = 79, .max = 127 },
  276. .m1 = { .min = 12, .max = 22 },
  277. .m2 = { .min = 5, .max = 9 },
  278. .p = { .min = 5, .max = 80 },
  279. .p1 = { .min = 1, .max = 8 },
  280. .p2 = { .dot_limit = 225000,
  281. .p2_slow = 10, .p2_fast = 5 },
  282. };
  283. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  284. .dot = { .min = 25000, .max = 350000 },
  285. .vco = { .min = 1760000, .max = 3510000 },
  286. .n = { .min = 1, .max = 3 },
  287. .m = { .min = 79, .max = 118 },
  288. .m1 = { .min = 12, .max = 22 },
  289. .m2 = { .min = 5, .max = 9 },
  290. .p = { .min = 28, .max = 112 },
  291. .p1 = { .min = 2, .max = 8 },
  292. .p2 = { .dot_limit = 225000,
  293. .p2_slow = 14, .p2_fast = 14 },
  294. };
  295. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  296. .dot = { .min = 25000, .max = 350000 },
  297. .vco = { .min = 1760000, .max = 3510000 },
  298. .n = { .min = 1, .max = 3 },
  299. .m = { .min = 79, .max = 127 },
  300. .m1 = { .min = 12, .max = 22 },
  301. .m2 = { .min = 5, .max = 9 },
  302. .p = { .min = 14, .max = 56 },
  303. .p1 = { .min = 2, .max = 8 },
  304. .p2 = { .dot_limit = 225000,
  305. .p2_slow = 7, .p2_fast = 7 },
  306. };
  307. /* LVDS 100mhz refclk limits. */
  308. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  309. .dot = { .min = 25000, .max = 350000 },
  310. .vco = { .min = 1760000, .max = 3510000 },
  311. .n = { .min = 1, .max = 2 },
  312. .m = { .min = 79, .max = 126 },
  313. .m1 = { .min = 12, .max = 22 },
  314. .m2 = { .min = 5, .max = 9 },
  315. .p = { .min = 28, .max = 112 },
  316. .p1 = { .min = 2, .max = 8 },
  317. .p2 = { .dot_limit = 225000,
  318. .p2_slow = 14, .p2_fast = 14 },
  319. };
  320. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  321. .dot = { .min = 25000, .max = 350000 },
  322. .vco = { .min = 1760000, .max = 3510000 },
  323. .n = { .min = 1, .max = 3 },
  324. .m = { .min = 79, .max = 126 },
  325. .m1 = { .min = 12, .max = 22 },
  326. .m2 = { .min = 5, .max = 9 },
  327. .p = { .min = 14, .max = 42 },
  328. .p1 = { .min = 2, .max = 6 },
  329. .p2 = { .dot_limit = 225000,
  330. .p2_slow = 7, .p2_fast = 7 },
  331. };
  332. static const intel_limit_t intel_limits_vlv = {
  333. /*
  334. * These are the data rate limits (measured in fast clocks)
  335. * since those are the strictest limits we have. The fast
  336. * clock and actual rate limits are more relaxed, so checking
  337. * them would make no difference.
  338. */
  339. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  340. .vco = { .min = 4000000, .max = 6000000 },
  341. .n = { .min = 1, .max = 7 },
  342. .m1 = { .min = 2, .max = 3 },
  343. .m2 = { .min = 11, .max = 156 },
  344. .p1 = { .min = 2, .max = 3 },
  345. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  346. };
  347. static const intel_limit_t intel_limits_chv = {
  348. /*
  349. * These are the data rate limits (measured in fast clocks)
  350. * since those are the strictest limits we have. The fast
  351. * clock and actual rate limits are more relaxed, so checking
  352. * them would make no difference.
  353. */
  354. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  355. .vco = { .min = 4860000, .max = 6700000 },
  356. .n = { .min = 1, .max = 1 },
  357. .m1 = { .min = 2, .max = 2 },
  358. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  359. .p1 = { .min = 2, .max = 4 },
  360. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  361. };
  362. static void vlv_clock(int refclk, intel_clock_t *clock)
  363. {
  364. clock->m = clock->m1 * clock->m2;
  365. clock->p = clock->p1 * clock->p2;
  366. if (WARN_ON(clock->n == 0 || clock->p == 0))
  367. return;
  368. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  369. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  370. }
  371. /**
  372. * Returns whether any output on the specified pipe is of the specified type
  373. */
  374. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  375. {
  376. struct drm_device *dev = crtc->base.dev;
  377. struct intel_encoder *encoder;
  378. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  379. if (encoder->type == type)
  380. return true;
  381. return false;
  382. }
  383. /**
  384. * Returns whether any output on the specified pipe will have the specified
  385. * type after a staged modeset is complete, i.e., the same as
  386. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  387. * encoder->crtc.
  388. */
  389. static bool intel_pipe_will_have_type(struct intel_crtc *crtc, int type)
  390. {
  391. struct drm_device *dev = crtc->base.dev;
  392. struct intel_encoder *encoder;
  393. for_each_intel_encoder(dev, encoder)
  394. if (encoder->new_crtc == crtc && encoder->type == type)
  395. return true;
  396. return false;
  397. }
  398. static const intel_limit_t *intel_ironlake_limit(struct intel_crtc *crtc,
  399. int refclk)
  400. {
  401. struct drm_device *dev = crtc->base.dev;
  402. const intel_limit_t *limit;
  403. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  404. if (intel_is_dual_link_lvds(dev)) {
  405. if (refclk == 100000)
  406. limit = &intel_limits_ironlake_dual_lvds_100m;
  407. else
  408. limit = &intel_limits_ironlake_dual_lvds;
  409. } else {
  410. if (refclk == 100000)
  411. limit = &intel_limits_ironlake_single_lvds_100m;
  412. else
  413. limit = &intel_limits_ironlake_single_lvds;
  414. }
  415. } else
  416. limit = &intel_limits_ironlake_dac;
  417. return limit;
  418. }
  419. static const intel_limit_t *intel_g4x_limit(struct intel_crtc *crtc)
  420. {
  421. struct drm_device *dev = crtc->base.dev;
  422. const intel_limit_t *limit;
  423. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  424. if (intel_is_dual_link_lvds(dev))
  425. limit = &intel_limits_g4x_dual_channel_lvds;
  426. else
  427. limit = &intel_limits_g4x_single_channel_lvds;
  428. } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_HDMI) ||
  429. intel_pipe_will_have_type(crtc, INTEL_OUTPUT_ANALOG)) {
  430. limit = &intel_limits_g4x_hdmi;
  431. } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_SDVO)) {
  432. limit = &intel_limits_g4x_sdvo;
  433. } else /* The option is for other outputs */
  434. limit = &intel_limits_i9xx_sdvo;
  435. return limit;
  436. }
  437. static const intel_limit_t *intel_limit(struct intel_crtc *crtc, int refclk)
  438. {
  439. struct drm_device *dev = crtc->base.dev;
  440. const intel_limit_t *limit;
  441. if (HAS_PCH_SPLIT(dev))
  442. limit = intel_ironlake_limit(crtc, refclk);
  443. else if (IS_G4X(dev)) {
  444. limit = intel_g4x_limit(crtc);
  445. } else if (IS_PINEVIEW(dev)) {
  446. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  447. limit = &intel_limits_pineview_lvds;
  448. else
  449. limit = &intel_limits_pineview_sdvo;
  450. } else if (IS_CHERRYVIEW(dev)) {
  451. limit = &intel_limits_chv;
  452. } else if (IS_VALLEYVIEW(dev)) {
  453. limit = &intel_limits_vlv;
  454. } else if (!IS_GEN2(dev)) {
  455. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  456. limit = &intel_limits_i9xx_lvds;
  457. else
  458. limit = &intel_limits_i9xx_sdvo;
  459. } else {
  460. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  461. limit = &intel_limits_i8xx_lvds;
  462. else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_DVO))
  463. limit = &intel_limits_i8xx_dvo;
  464. else
  465. limit = &intel_limits_i8xx_dac;
  466. }
  467. return limit;
  468. }
  469. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  470. static void pineview_clock(int refclk, intel_clock_t *clock)
  471. {
  472. clock->m = clock->m2 + 2;
  473. clock->p = clock->p1 * clock->p2;
  474. if (WARN_ON(clock->n == 0 || clock->p == 0))
  475. return;
  476. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  477. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  478. }
  479. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  480. {
  481. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  482. }
  483. static void i9xx_clock(int refclk, intel_clock_t *clock)
  484. {
  485. clock->m = i9xx_dpll_compute_m(clock);
  486. clock->p = clock->p1 * clock->p2;
  487. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  488. return;
  489. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  490. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  491. }
  492. static void chv_clock(int refclk, intel_clock_t *clock)
  493. {
  494. clock->m = clock->m1 * clock->m2;
  495. clock->p = clock->p1 * clock->p2;
  496. if (WARN_ON(clock->n == 0 || clock->p == 0))
  497. return;
  498. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  499. clock->n << 22);
  500. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  501. }
  502. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  503. /**
  504. * Returns whether the given set of divisors are valid for a given refclk with
  505. * the given connectors.
  506. */
  507. static bool intel_PLL_is_valid(struct drm_device *dev,
  508. const intel_limit_t *limit,
  509. const intel_clock_t *clock)
  510. {
  511. if (clock->n < limit->n.min || limit->n.max < clock->n)
  512. INTELPllInvalid("n out of range\n");
  513. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  514. INTELPllInvalid("p1 out of range\n");
  515. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  516. INTELPllInvalid("m2 out of range\n");
  517. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  518. INTELPllInvalid("m1 out of range\n");
  519. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
  520. if (clock->m1 <= clock->m2)
  521. INTELPllInvalid("m1 <= m2\n");
  522. if (!IS_VALLEYVIEW(dev)) {
  523. if (clock->p < limit->p.min || limit->p.max < clock->p)
  524. INTELPllInvalid("p out of range\n");
  525. if (clock->m < limit->m.min || limit->m.max < clock->m)
  526. INTELPllInvalid("m out of range\n");
  527. }
  528. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  529. INTELPllInvalid("vco out of range\n");
  530. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  531. * connector, etc., rather than just a single range.
  532. */
  533. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  534. INTELPllInvalid("dot out of range\n");
  535. return true;
  536. }
  537. static bool
  538. i9xx_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  539. int target, int refclk, intel_clock_t *match_clock,
  540. intel_clock_t *best_clock)
  541. {
  542. struct drm_device *dev = crtc->base.dev;
  543. intel_clock_t clock;
  544. int err = target;
  545. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  546. /*
  547. * For LVDS just rely on its current settings for dual-channel.
  548. * We haven't figured out how to reliably set up different
  549. * single/dual channel state, if we even can.
  550. */
  551. if (intel_is_dual_link_lvds(dev))
  552. clock.p2 = limit->p2.p2_fast;
  553. else
  554. clock.p2 = limit->p2.p2_slow;
  555. } else {
  556. if (target < limit->p2.dot_limit)
  557. clock.p2 = limit->p2.p2_slow;
  558. else
  559. clock.p2 = limit->p2.p2_fast;
  560. }
  561. memset(best_clock, 0, sizeof(*best_clock));
  562. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  563. clock.m1++) {
  564. for (clock.m2 = limit->m2.min;
  565. clock.m2 <= limit->m2.max; clock.m2++) {
  566. if (clock.m2 >= clock.m1)
  567. break;
  568. for (clock.n = limit->n.min;
  569. clock.n <= limit->n.max; clock.n++) {
  570. for (clock.p1 = limit->p1.min;
  571. clock.p1 <= limit->p1.max; clock.p1++) {
  572. int this_err;
  573. i9xx_clock(refclk, &clock);
  574. if (!intel_PLL_is_valid(dev, limit,
  575. &clock))
  576. continue;
  577. if (match_clock &&
  578. clock.p != match_clock->p)
  579. continue;
  580. this_err = abs(clock.dot - target);
  581. if (this_err < err) {
  582. *best_clock = clock;
  583. err = this_err;
  584. }
  585. }
  586. }
  587. }
  588. }
  589. return (err != target);
  590. }
  591. static bool
  592. pnv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  593. int target, int refclk, intel_clock_t *match_clock,
  594. intel_clock_t *best_clock)
  595. {
  596. struct drm_device *dev = crtc->base.dev;
  597. intel_clock_t clock;
  598. int err = target;
  599. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  600. /*
  601. * For LVDS just rely on its current settings for dual-channel.
  602. * We haven't figured out how to reliably set up different
  603. * single/dual channel state, if we even can.
  604. */
  605. if (intel_is_dual_link_lvds(dev))
  606. clock.p2 = limit->p2.p2_fast;
  607. else
  608. clock.p2 = limit->p2.p2_slow;
  609. } else {
  610. if (target < limit->p2.dot_limit)
  611. clock.p2 = limit->p2.p2_slow;
  612. else
  613. clock.p2 = limit->p2.p2_fast;
  614. }
  615. memset(best_clock, 0, sizeof(*best_clock));
  616. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  617. clock.m1++) {
  618. for (clock.m2 = limit->m2.min;
  619. clock.m2 <= limit->m2.max; clock.m2++) {
  620. for (clock.n = limit->n.min;
  621. clock.n <= limit->n.max; clock.n++) {
  622. for (clock.p1 = limit->p1.min;
  623. clock.p1 <= limit->p1.max; clock.p1++) {
  624. int this_err;
  625. pineview_clock(refclk, &clock);
  626. if (!intel_PLL_is_valid(dev, limit,
  627. &clock))
  628. continue;
  629. if (match_clock &&
  630. clock.p != match_clock->p)
  631. continue;
  632. this_err = abs(clock.dot - target);
  633. if (this_err < err) {
  634. *best_clock = clock;
  635. err = this_err;
  636. }
  637. }
  638. }
  639. }
  640. }
  641. return (err != target);
  642. }
  643. static bool
  644. g4x_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  645. int target, int refclk, intel_clock_t *match_clock,
  646. intel_clock_t *best_clock)
  647. {
  648. struct drm_device *dev = crtc->base.dev;
  649. intel_clock_t clock;
  650. int max_n;
  651. bool found;
  652. /* approximately equals target * 0.00585 */
  653. int err_most = (target >> 8) + (target >> 9);
  654. found = false;
  655. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  656. if (intel_is_dual_link_lvds(dev))
  657. clock.p2 = limit->p2.p2_fast;
  658. else
  659. clock.p2 = limit->p2.p2_slow;
  660. } else {
  661. if (target < limit->p2.dot_limit)
  662. clock.p2 = limit->p2.p2_slow;
  663. else
  664. clock.p2 = limit->p2.p2_fast;
  665. }
  666. memset(best_clock, 0, sizeof(*best_clock));
  667. max_n = limit->n.max;
  668. /* based on hardware requirement, prefer smaller n to precision */
  669. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  670. /* based on hardware requirement, prefere larger m1,m2 */
  671. for (clock.m1 = limit->m1.max;
  672. clock.m1 >= limit->m1.min; clock.m1--) {
  673. for (clock.m2 = limit->m2.max;
  674. clock.m2 >= limit->m2.min; clock.m2--) {
  675. for (clock.p1 = limit->p1.max;
  676. clock.p1 >= limit->p1.min; clock.p1--) {
  677. int this_err;
  678. i9xx_clock(refclk, &clock);
  679. if (!intel_PLL_is_valid(dev, limit,
  680. &clock))
  681. continue;
  682. this_err = abs(clock.dot - target);
  683. if (this_err < err_most) {
  684. *best_clock = clock;
  685. err_most = this_err;
  686. max_n = clock.n;
  687. found = true;
  688. }
  689. }
  690. }
  691. }
  692. }
  693. return found;
  694. }
  695. static bool
  696. vlv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  697. int target, int refclk, intel_clock_t *match_clock,
  698. intel_clock_t *best_clock)
  699. {
  700. struct drm_device *dev = crtc->base.dev;
  701. intel_clock_t clock;
  702. unsigned int bestppm = 1000000;
  703. /* min update 19.2 MHz */
  704. int max_n = min(limit->n.max, refclk / 19200);
  705. bool found = false;
  706. target *= 5; /* fast clock */
  707. memset(best_clock, 0, sizeof(*best_clock));
  708. /* based on hardware requirement, prefer smaller n to precision */
  709. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  710. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  711. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  712. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  713. clock.p = clock.p1 * clock.p2;
  714. /* based on hardware requirement, prefer bigger m1,m2 values */
  715. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  716. unsigned int ppm, diff;
  717. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  718. refclk * clock.m1);
  719. vlv_clock(refclk, &clock);
  720. if (!intel_PLL_is_valid(dev, limit,
  721. &clock))
  722. continue;
  723. diff = abs(clock.dot - target);
  724. ppm = div_u64(1000000ULL * diff, target);
  725. if (ppm < 100 && clock.p > best_clock->p) {
  726. bestppm = 0;
  727. *best_clock = clock;
  728. found = true;
  729. }
  730. if (bestppm >= 10 && ppm < bestppm - 10) {
  731. bestppm = ppm;
  732. *best_clock = clock;
  733. found = true;
  734. }
  735. }
  736. }
  737. }
  738. }
  739. return found;
  740. }
  741. static bool
  742. chv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  743. int target, int refclk, intel_clock_t *match_clock,
  744. intel_clock_t *best_clock)
  745. {
  746. struct drm_device *dev = crtc->base.dev;
  747. intel_clock_t clock;
  748. uint64_t m2;
  749. int found = false;
  750. memset(best_clock, 0, sizeof(*best_clock));
  751. /*
  752. * Based on hardware doc, the n always set to 1, and m1 always
  753. * set to 2. If requires to support 200Mhz refclk, we need to
  754. * revisit this because n may not 1 anymore.
  755. */
  756. clock.n = 1, clock.m1 = 2;
  757. target *= 5; /* fast clock */
  758. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  759. for (clock.p2 = limit->p2.p2_fast;
  760. clock.p2 >= limit->p2.p2_slow;
  761. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  762. clock.p = clock.p1 * clock.p2;
  763. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  764. clock.n) << 22, refclk * clock.m1);
  765. if (m2 > INT_MAX/clock.m1)
  766. continue;
  767. clock.m2 = m2;
  768. chv_clock(refclk, &clock);
  769. if (!intel_PLL_is_valid(dev, limit, &clock))
  770. continue;
  771. /* based on hardware requirement, prefer bigger p
  772. */
  773. if (clock.p > best_clock->p) {
  774. *best_clock = clock;
  775. found = true;
  776. }
  777. }
  778. }
  779. return found;
  780. }
  781. bool intel_crtc_active(struct drm_crtc *crtc)
  782. {
  783. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  784. /* Be paranoid as we can arrive here with only partial
  785. * state retrieved from the hardware during setup.
  786. *
  787. * We can ditch the adjusted_mode.crtc_clock check as soon
  788. * as Haswell has gained clock readout/fastboot support.
  789. *
  790. * We can ditch the crtc->primary->fb check as soon as we can
  791. * properly reconstruct framebuffers.
  792. */
  793. return intel_crtc->active && crtc->primary->fb &&
  794. intel_crtc->config.adjusted_mode.crtc_clock;
  795. }
  796. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  797. enum pipe pipe)
  798. {
  799. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  800. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  801. return intel_crtc->config.cpu_transcoder;
  802. }
  803. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  804. {
  805. struct drm_i915_private *dev_priv = dev->dev_private;
  806. u32 reg = PIPEDSL(pipe);
  807. u32 line1, line2;
  808. u32 line_mask;
  809. if (IS_GEN2(dev))
  810. line_mask = DSL_LINEMASK_GEN2;
  811. else
  812. line_mask = DSL_LINEMASK_GEN3;
  813. line1 = I915_READ(reg) & line_mask;
  814. mdelay(5);
  815. line2 = I915_READ(reg) & line_mask;
  816. return line1 == line2;
  817. }
  818. /*
  819. * intel_wait_for_pipe_off - wait for pipe to turn off
  820. * @crtc: crtc whose pipe to wait for
  821. *
  822. * After disabling a pipe, we can't wait for vblank in the usual way,
  823. * spinning on the vblank interrupt status bit, since we won't actually
  824. * see an interrupt when the pipe is disabled.
  825. *
  826. * On Gen4 and above:
  827. * wait for the pipe register state bit to turn off
  828. *
  829. * Otherwise:
  830. * wait for the display line value to settle (it usually
  831. * ends up stopping at the start of the next frame).
  832. *
  833. */
  834. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  835. {
  836. struct drm_device *dev = crtc->base.dev;
  837. struct drm_i915_private *dev_priv = dev->dev_private;
  838. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  839. enum pipe pipe = crtc->pipe;
  840. if (INTEL_INFO(dev)->gen >= 4) {
  841. int reg = PIPECONF(cpu_transcoder);
  842. /* Wait for the Pipe State to go off */
  843. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  844. 100))
  845. WARN(1, "pipe_off wait timed out\n");
  846. } else {
  847. /* Wait for the display line to settle */
  848. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  849. WARN(1, "pipe_off wait timed out\n");
  850. }
  851. }
  852. /*
  853. * ibx_digital_port_connected - is the specified port connected?
  854. * @dev_priv: i915 private structure
  855. * @port: the port to test
  856. *
  857. * Returns true if @port is connected, false otherwise.
  858. */
  859. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  860. struct intel_digital_port *port)
  861. {
  862. u32 bit;
  863. if (HAS_PCH_IBX(dev_priv->dev)) {
  864. switch (port->port) {
  865. case PORT_B:
  866. bit = SDE_PORTB_HOTPLUG;
  867. break;
  868. case PORT_C:
  869. bit = SDE_PORTC_HOTPLUG;
  870. break;
  871. case PORT_D:
  872. bit = SDE_PORTD_HOTPLUG;
  873. break;
  874. default:
  875. return true;
  876. }
  877. } else {
  878. switch (port->port) {
  879. case PORT_B:
  880. bit = SDE_PORTB_HOTPLUG_CPT;
  881. break;
  882. case PORT_C:
  883. bit = SDE_PORTC_HOTPLUG_CPT;
  884. break;
  885. case PORT_D:
  886. bit = SDE_PORTD_HOTPLUG_CPT;
  887. break;
  888. default:
  889. return true;
  890. }
  891. }
  892. return I915_READ(SDEISR) & bit;
  893. }
  894. static const char *state_string(bool enabled)
  895. {
  896. return enabled ? "on" : "off";
  897. }
  898. /* Only for pre-ILK configs */
  899. void assert_pll(struct drm_i915_private *dev_priv,
  900. enum pipe pipe, bool state)
  901. {
  902. int reg;
  903. u32 val;
  904. bool cur_state;
  905. reg = DPLL(pipe);
  906. val = I915_READ(reg);
  907. cur_state = !!(val & DPLL_VCO_ENABLE);
  908. WARN(cur_state != state,
  909. "PLL state assertion failure (expected %s, current %s)\n",
  910. state_string(state), state_string(cur_state));
  911. }
  912. /* XXX: the dsi pll is shared between MIPI DSI ports */
  913. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  914. {
  915. u32 val;
  916. bool cur_state;
  917. mutex_lock(&dev_priv->dpio_lock);
  918. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  919. mutex_unlock(&dev_priv->dpio_lock);
  920. cur_state = val & DSI_PLL_VCO_EN;
  921. WARN(cur_state != state,
  922. "DSI PLL state assertion failure (expected %s, current %s)\n",
  923. state_string(state), state_string(cur_state));
  924. }
  925. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  926. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  927. struct intel_shared_dpll *
  928. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  929. {
  930. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  931. if (crtc->config.shared_dpll < 0)
  932. return NULL;
  933. return &dev_priv->shared_dplls[crtc->config.shared_dpll];
  934. }
  935. /* For ILK+ */
  936. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  937. struct intel_shared_dpll *pll,
  938. bool state)
  939. {
  940. bool cur_state;
  941. struct intel_dpll_hw_state hw_state;
  942. if (WARN (!pll,
  943. "asserting DPLL %s with no DPLL\n", state_string(state)))
  944. return;
  945. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  946. WARN(cur_state != state,
  947. "%s assertion failure (expected %s, current %s)\n",
  948. pll->name, state_string(state), state_string(cur_state));
  949. }
  950. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  951. enum pipe pipe, bool state)
  952. {
  953. int reg;
  954. u32 val;
  955. bool cur_state;
  956. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  957. pipe);
  958. if (HAS_DDI(dev_priv->dev)) {
  959. /* DDI does not have a specific FDI_TX register */
  960. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  961. val = I915_READ(reg);
  962. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  963. } else {
  964. reg = FDI_TX_CTL(pipe);
  965. val = I915_READ(reg);
  966. cur_state = !!(val & FDI_TX_ENABLE);
  967. }
  968. WARN(cur_state != state,
  969. "FDI TX state assertion failure (expected %s, current %s)\n",
  970. state_string(state), state_string(cur_state));
  971. }
  972. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  973. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  974. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  975. enum pipe pipe, bool state)
  976. {
  977. int reg;
  978. u32 val;
  979. bool cur_state;
  980. reg = FDI_RX_CTL(pipe);
  981. val = I915_READ(reg);
  982. cur_state = !!(val & FDI_RX_ENABLE);
  983. WARN(cur_state != state,
  984. "FDI RX state assertion failure (expected %s, current %s)\n",
  985. state_string(state), state_string(cur_state));
  986. }
  987. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  988. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  989. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  990. enum pipe pipe)
  991. {
  992. int reg;
  993. u32 val;
  994. /* ILK FDI PLL is always enabled */
  995. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  996. return;
  997. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  998. if (HAS_DDI(dev_priv->dev))
  999. return;
  1000. reg = FDI_TX_CTL(pipe);
  1001. val = I915_READ(reg);
  1002. WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1003. }
  1004. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1005. enum pipe pipe, bool state)
  1006. {
  1007. int reg;
  1008. u32 val;
  1009. bool cur_state;
  1010. reg = FDI_RX_CTL(pipe);
  1011. val = I915_READ(reg);
  1012. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1013. WARN(cur_state != state,
  1014. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1015. state_string(state), state_string(cur_state));
  1016. }
  1017. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1018. enum pipe pipe)
  1019. {
  1020. struct drm_device *dev = dev_priv->dev;
  1021. int pp_reg;
  1022. u32 val;
  1023. enum pipe panel_pipe = PIPE_A;
  1024. bool locked = true;
  1025. if (WARN_ON(HAS_DDI(dev)))
  1026. return;
  1027. if (HAS_PCH_SPLIT(dev)) {
  1028. u32 port_sel;
  1029. pp_reg = PCH_PP_CONTROL;
  1030. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1031. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1032. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1033. panel_pipe = PIPE_B;
  1034. /* XXX: else fix for eDP */
  1035. } else if (IS_VALLEYVIEW(dev)) {
  1036. /* presumably write lock depends on pipe, not port select */
  1037. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1038. panel_pipe = pipe;
  1039. } else {
  1040. pp_reg = PP_CONTROL;
  1041. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1042. panel_pipe = PIPE_B;
  1043. }
  1044. val = I915_READ(pp_reg);
  1045. if (!(val & PANEL_POWER_ON) ||
  1046. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1047. locked = false;
  1048. WARN(panel_pipe == pipe && locked,
  1049. "panel assertion failure, pipe %c regs locked\n",
  1050. pipe_name(pipe));
  1051. }
  1052. static void assert_cursor(struct drm_i915_private *dev_priv,
  1053. enum pipe pipe, bool state)
  1054. {
  1055. struct drm_device *dev = dev_priv->dev;
  1056. bool cur_state;
  1057. if (IS_845G(dev) || IS_I865G(dev))
  1058. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1059. else
  1060. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1061. WARN(cur_state != state,
  1062. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1063. pipe_name(pipe), state_string(state), state_string(cur_state));
  1064. }
  1065. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1066. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1067. void assert_pipe(struct drm_i915_private *dev_priv,
  1068. enum pipe pipe, bool state)
  1069. {
  1070. int reg;
  1071. u32 val;
  1072. bool cur_state;
  1073. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1074. pipe);
  1075. /* if we need the pipe quirk it must be always on */
  1076. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1077. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1078. state = true;
  1079. if (!intel_display_power_is_enabled(dev_priv,
  1080. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1081. cur_state = false;
  1082. } else {
  1083. reg = PIPECONF(cpu_transcoder);
  1084. val = I915_READ(reg);
  1085. cur_state = !!(val & PIPECONF_ENABLE);
  1086. }
  1087. WARN(cur_state != state,
  1088. "pipe %c assertion failure (expected %s, current %s)\n",
  1089. pipe_name(pipe), state_string(state), state_string(cur_state));
  1090. }
  1091. static void assert_plane(struct drm_i915_private *dev_priv,
  1092. enum plane plane, bool state)
  1093. {
  1094. int reg;
  1095. u32 val;
  1096. bool cur_state;
  1097. reg = DSPCNTR(plane);
  1098. val = I915_READ(reg);
  1099. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1100. WARN(cur_state != state,
  1101. "plane %c assertion failure (expected %s, current %s)\n",
  1102. plane_name(plane), state_string(state), state_string(cur_state));
  1103. }
  1104. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1105. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1106. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1107. enum pipe pipe)
  1108. {
  1109. struct drm_device *dev = dev_priv->dev;
  1110. int reg, i;
  1111. u32 val;
  1112. int cur_pipe;
  1113. /* Primary planes are fixed to pipes on gen4+ */
  1114. if (INTEL_INFO(dev)->gen >= 4) {
  1115. reg = DSPCNTR(pipe);
  1116. val = I915_READ(reg);
  1117. WARN(val & DISPLAY_PLANE_ENABLE,
  1118. "plane %c assertion failure, should be disabled but not\n",
  1119. plane_name(pipe));
  1120. return;
  1121. }
  1122. /* Need to check both planes against the pipe */
  1123. for_each_pipe(dev_priv, i) {
  1124. reg = DSPCNTR(i);
  1125. val = I915_READ(reg);
  1126. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1127. DISPPLANE_SEL_PIPE_SHIFT;
  1128. WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1129. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1130. plane_name(i), pipe_name(pipe));
  1131. }
  1132. }
  1133. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1134. enum pipe pipe)
  1135. {
  1136. struct drm_device *dev = dev_priv->dev;
  1137. int reg, sprite;
  1138. u32 val;
  1139. if (INTEL_INFO(dev)->gen >= 9) {
  1140. for_each_sprite(pipe, sprite) {
  1141. val = I915_READ(PLANE_CTL(pipe, sprite));
  1142. WARN(val & PLANE_CTL_ENABLE,
  1143. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1144. sprite, pipe_name(pipe));
  1145. }
  1146. } else if (IS_VALLEYVIEW(dev)) {
  1147. for_each_sprite(pipe, sprite) {
  1148. reg = SPCNTR(pipe, sprite);
  1149. val = I915_READ(reg);
  1150. WARN(val & SP_ENABLE,
  1151. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1152. sprite_name(pipe, sprite), pipe_name(pipe));
  1153. }
  1154. } else if (INTEL_INFO(dev)->gen >= 7) {
  1155. reg = SPRCTL(pipe);
  1156. val = I915_READ(reg);
  1157. WARN(val & SPRITE_ENABLE,
  1158. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1159. plane_name(pipe), pipe_name(pipe));
  1160. } else if (INTEL_INFO(dev)->gen >= 5) {
  1161. reg = DVSCNTR(pipe);
  1162. val = I915_READ(reg);
  1163. WARN(val & DVS_ENABLE,
  1164. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1165. plane_name(pipe), pipe_name(pipe));
  1166. }
  1167. }
  1168. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1169. {
  1170. if (WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1171. drm_crtc_vblank_put(crtc);
  1172. }
  1173. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1174. {
  1175. u32 val;
  1176. bool enabled;
  1177. WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1178. val = I915_READ(PCH_DREF_CONTROL);
  1179. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1180. DREF_SUPERSPREAD_SOURCE_MASK));
  1181. WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1182. }
  1183. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1184. enum pipe pipe)
  1185. {
  1186. int reg;
  1187. u32 val;
  1188. bool enabled;
  1189. reg = PCH_TRANSCONF(pipe);
  1190. val = I915_READ(reg);
  1191. enabled = !!(val & TRANS_ENABLE);
  1192. WARN(enabled,
  1193. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1194. pipe_name(pipe));
  1195. }
  1196. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1197. enum pipe pipe, u32 port_sel, u32 val)
  1198. {
  1199. if ((val & DP_PORT_EN) == 0)
  1200. return false;
  1201. if (HAS_PCH_CPT(dev_priv->dev)) {
  1202. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1203. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1204. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1205. return false;
  1206. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1207. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1208. return false;
  1209. } else {
  1210. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1211. return false;
  1212. }
  1213. return true;
  1214. }
  1215. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1216. enum pipe pipe, u32 val)
  1217. {
  1218. if ((val & SDVO_ENABLE) == 0)
  1219. return false;
  1220. if (HAS_PCH_CPT(dev_priv->dev)) {
  1221. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1222. return false;
  1223. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1224. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1225. return false;
  1226. } else {
  1227. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1228. return false;
  1229. }
  1230. return true;
  1231. }
  1232. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1233. enum pipe pipe, u32 val)
  1234. {
  1235. if ((val & LVDS_PORT_EN) == 0)
  1236. return false;
  1237. if (HAS_PCH_CPT(dev_priv->dev)) {
  1238. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1239. return false;
  1240. } else {
  1241. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1242. return false;
  1243. }
  1244. return true;
  1245. }
  1246. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1247. enum pipe pipe, u32 val)
  1248. {
  1249. if ((val & ADPA_DAC_ENABLE) == 0)
  1250. return false;
  1251. if (HAS_PCH_CPT(dev_priv->dev)) {
  1252. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1253. return false;
  1254. } else {
  1255. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1256. return false;
  1257. }
  1258. return true;
  1259. }
  1260. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1261. enum pipe pipe, int reg, u32 port_sel)
  1262. {
  1263. u32 val = I915_READ(reg);
  1264. WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1265. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1266. reg, pipe_name(pipe));
  1267. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1268. && (val & DP_PIPEB_SELECT),
  1269. "IBX PCH dp port still using transcoder B\n");
  1270. }
  1271. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1272. enum pipe pipe, int reg)
  1273. {
  1274. u32 val = I915_READ(reg);
  1275. WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1276. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1277. reg, pipe_name(pipe));
  1278. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1279. && (val & SDVO_PIPE_B_SELECT),
  1280. "IBX PCH hdmi port still using transcoder B\n");
  1281. }
  1282. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1283. enum pipe pipe)
  1284. {
  1285. int reg;
  1286. u32 val;
  1287. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1288. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1289. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1290. reg = PCH_ADPA;
  1291. val = I915_READ(reg);
  1292. WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1293. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1294. pipe_name(pipe));
  1295. reg = PCH_LVDS;
  1296. val = I915_READ(reg);
  1297. WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1298. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1299. pipe_name(pipe));
  1300. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1301. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1302. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1303. }
  1304. static void intel_init_dpio(struct drm_device *dev)
  1305. {
  1306. struct drm_i915_private *dev_priv = dev->dev_private;
  1307. if (!IS_VALLEYVIEW(dev))
  1308. return;
  1309. /*
  1310. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1311. * CHV x1 PHY (DP/HDMI D)
  1312. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1313. */
  1314. if (IS_CHERRYVIEW(dev)) {
  1315. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1316. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1317. } else {
  1318. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1319. }
  1320. }
  1321. static void vlv_enable_pll(struct intel_crtc *crtc,
  1322. const struct intel_crtc_config *pipe_config)
  1323. {
  1324. struct drm_device *dev = crtc->base.dev;
  1325. struct drm_i915_private *dev_priv = dev->dev_private;
  1326. int reg = DPLL(crtc->pipe);
  1327. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1328. assert_pipe_disabled(dev_priv, crtc->pipe);
  1329. /* No really, not for ILK+ */
  1330. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1331. /* PLL is protected by panel, make sure we can write it */
  1332. if (IS_MOBILE(dev_priv->dev))
  1333. assert_panel_unlocked(dev_priv, crtc->pipe);
  1334. I915_WRITE(reg, dpll);
  1335. POSTING_READ(reg);
  1336. udelay(150);
  1337. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1338. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1339. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1340. POSTING_READ(DPLL_MD(crtc->pipe));
  1341. /* We do this three times for luck */
  1342. I915_WRITE(reg, dpll);
  1343. POSTING_READ(reg);
  1344. udelay(150); /* wait for warmup */
  1345. I915_WRITE(reg, dpll);
  1346. POSTING_READ(reg);
  1347. udelay(150); /* wait for warmup */
  1348. I915_WRITE(reg, dpll);
  1349. POSTING_READ(reg);
  1350. udelay(150); /* wait for warmup */
  1351. }
  1352. static void chv_enable_pll(struct intel_crtc *crtc,
  1353. const struct intel_crtc_config *pipe_config)
  1354. {
  1355. struct drm_device *dev = crtc->base.dev;
  1356. struct drm_i915_private *dev_priv = dev->dev_private;
  1357. int pipe = crtc->pipe;
  1358. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1359. u32 tmp;
  1360. assert_pipe_disabled(dev_priv, crtc->pipe);
  1361. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1362. mutex_lock(&dev_priv->dpio_lock);
  1363. /* Enable back the 10bit clock to display controller */
  1364. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1365. tmp |= DPIO_DCLKP_EN;
  1366. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1367. /*
  1368. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1369. */
  1370. udelay(1);
  1371. /* Enable PLL */
  1372. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1373. /* Check PLL is locked */
  1374. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1375. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1376. /* not sure when this should be written */
  1377. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1378. POSTING_READ(DPLL_MD(pipe));
  1379. mutex_unlock(&dev_priv->dpio_lock);
  1380. }
  1381. static int intel_num_dvo_pipes(struct drm_device *dev)
  1382. {
  1383. struct intel_crtc *crtc;
  1384. int count = 0;
  1385. for_each_intel_crtc(dev, crtc)
  1386. count += crtc->active &&
  1387. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1388. return count;
  1389. }
  1390. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1391. {
  1392. struct drm_device *dev = crtc->base.dev;
  1393. struct drm_i915_private *dev_priv = dev->dev_private;
  1394. int reg = DPLL(crtc->pipe);
  1395. u32 dpll = crtc->config.dpll_hw_state.dpll;
  1396. assert_pipe_disabled(dev_priv, crtc->pipe);
  1397. /* No really, not for ILK+ */
  1398. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1399. /* PLL is protected by panel, make sure we can write it */
  1400. if (IS_MOBILE(dev) && !IS_I830(dev))
  1401. assert_panel_unlocked(dev_priv, crtc->pipe);
  1402. /* Enable DVO 2x clock on both PLLs if necessary */
  1403. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1404. /*
  1405. * It appears to be important that we don't enable this
  1406. * for the current pipe before otherwise configuring the
  1407. * PLL. No idea how this should be handled if multiple
  1408. * DVO outputs are enabled simultaneosly.
  1409. */
  1410. dpll |= DPLL_DVO_2X_MODE;
  1411. I915_WRITE(DPLL(!crtc->pipe),
  1412. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1413. }
  1414. /* Wait for the clocks to stabilize. */
  1415. POSTING_READ(reg);
  1416. udelay(150);
  1417. if (INTEL_INFO(dev)->gen >= 4) {
  1418. I915_WRITE(DPLL_MD(crtc->pipe),
  1419. crtc->config.dpll_hw_state.dpll_md);
  1420. } else {
  1421. /* The pixel multiplier can only be updated once the
  1422. * DPLL is enabled and the clocks are stable.
  1423. *
  1424. * So write it again.
  1425. */
  1426. I915_WRITE(reg, dpll);
  1427. }
  1428. /* We do this three times for luck */
  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. I915_WRITE(reg, dpll);
  1436. POSTING_READ(reg);
  1437. udelay(150); /* wait for warmup */
  1438. }
  1439. /**
  1440. * i9xx_disable_pll - disable a PLL
  1441. * @dev_priv: i915 private structure
  1442. * @pipe: pipe PLL to disable
  1443. *
  1444. * Disable the PLL for @pipe, making sure the pipe is off first.
  1445. *
  1446. * Note! This is for pre-ILK only.
  1447. */
  1448. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1449. {
  1450. struct drm_device *dev = crtc->base.dev;
  1451. struct drm_i915_private *dev_priv = dev->dev_private;
  1452. enum pipe pipe = crtc->pipe;
  1453. /* Disable DVO 2x clock on both PLLs if necessary */
  1454. if (IS_I830(dev) &&
  1455. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1456. intel_num_dvo_pipes(dev) == 1) {
  1457. I915_WRITE(DPLL(PIPE_B),
  1458. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1459. I915_WRITE(DPLL(PIPE_A),
  1460. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1461. }
  1462. /* Don't disable pipe or pipe PLLs if needed */
  1463. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1464. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1465. return;
  1466. /* Make sure the pipe isn't still relying on us */
  1467. assert_pipe_disabled(dev_priv, pipe);
  1468. I915_WRITE(DPLL(pipe), 0);
  1469. POSTING_READ(DPLL(pipe));
  1470. }
  1471. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1472. {
  1473. u32 val = 0;
  1474. /* Make sure the pipe isn't still relying on us */
  1475. assert_pipe_disabled(dev_priv, pipe);
  1476. /*
  1477. * Leave integrated clock source and reference clock enabled for pipe B.
  1478. * The latter is needed for VGA hotplug / manual detection.
  1479. */
  1480. if (pipe == PIPE_B)
  1481. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1482. I915_WRITE(DPLL(pipe), val);
  1483. POSTING_READ(DPLL(pipe));
  1484. }
  1485. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1486. {
  1487. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1488. u32 val;
  1489. /* Make sure the pipe isn't still relying on us */
  1490. assert_pipe_disabled(dev_priv, pipe);
  1491. /* Set PLL en = 0 */
  1492. val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
  1493. if (pipe != PIPE_A)
  1494. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1495. I915_WRITE(DPLL(pipe), val);
  1496. POSTING_READ(DPLL(pipe));
  1497. mutex_lock(&dev_priv->dpio_lock);
  1498. /* Disable 10bit clock to display controller */
  1499. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1500. val &= ~DPIO_DCLKP_EN;
  1501. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1502. /* disable left/right clock distribution */
  1503. if (pipe != PIPE_B) {
  1504. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1505. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1506. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1507. } else {
  1508. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1509. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1510. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1511. }
  1512. mutex_unlock(&dev_priv->dpio_lock);
  1513. }
  1514. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1515. struct intel_digital_port *dport)
  1516. {
  1517. u32 port_mask;
  1518. int dpll_reg;
  1519. switch (dport->port) {
  1520. case PORT_B:
  1521. port_mask = DPLL_PORTB_READY_MASK;
  1522. dpll_reg = DPLL(0);
  1523. break;
  1524. case PORT_C:
  1525. port_mask = DPLL_PORTC_READY_MASK;
  1526. dpll_reg = DPLL(0);
  1527. break;
  1528. case PORT_D:
  1529. port_mask = DPLL_PORTD_READY_MASK;
  1530. dpll_reg = DPIO_PHY_STATUS;
  1531. break;
  1532. default:
  1533. BUG();
  1534. }
  1535. if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
  1536. WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
  1537. port_name(dport->port), I915_READ(dpll_reg));
  1538. }
  1539. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1540. {
  1541. struct drm_device *dev = crtc->base.dev;
  1542. struct drm_i915_private *dev_priv = dev->dev_private;
  1543. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1544. if (WARN_ON(pll == NULL))
  1545. return;
  1546. WARN_ON(!pll->config.crtc_mask);
  1547. if (pll->active == 0) {
  1548. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1549. WARN_ON(pll->on);
  1550. assert_shared_dpll_disabled(dev_priv, pll);
  1551. pll->mode_set(dev_priv, pll);
  1552. }
  1553. }
  1554. /**
  1555. * intel_enable_shared_dpll - enable PCH PLL
  1556. * @dev_priv: i915 private structure
  1557. * @pipe: pipe PLL to enable
  1558. *
  1559. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1560. * drives the transcoder clock.
  1561. */
  1562. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1563. {
  1564. struct drm_device *dev = crtc->base.dev;
  1565. struct drm_i915_private *dev_priv = dev->dev_private;
  1566. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1567. if (WARN_ON(pll == NULL))
  1568. return;
  1569. if (WARN_ON(pll->config.crtc_mask == 0))
  1570. return;
  1571. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1572. pll->name, pll->active, pll->on,
  1573. crtc->base.base.id);
  1574. if (pll->active++) {
  1575. WARN_ON(!pll->on);
  1576. assert_shared_dpll_enabled(dev_priv, pll);
  1577. return;
  1578. }
  1579. WARN_ON(pll->on);
  1580. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1581. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1582. pll->enable(dev_priv, pll);
  1583. pll->on = true;
  1584. }
  1585. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1586. {
  1587. struct drm_device *dev = crtc->base.dev;
  1588. struct drm_i915_private *dev_priv = dev->dev_private;
  1589. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1590. /* PCH only available on ILK+ */
  1591. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1592. if (WARN_ON(pll == NULL))
  1593. return;
  1594. if (WARN_ON(pll->config.crtc_mask == 0))
  1595. return;
  1596. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1597. pll->name, pll->active, pll->on,
  1598. crtc->base.base.id);
  1599. if (WARN_ON(pll->active == 0)) {
  1600. assert_shared_dpll_disabled(dev_priv, pll);
  1601. return;
  1602. }
  1603. assert_shared_dpll_enabled(dev_priv, pll);
  1604. WARN_ON(!pll->on);
  1605. if (--pll->active)
  1606. return;
  1607. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1608. pll->disable(dev_priv, pll);
  1609. pll->on = false;
  1610. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1611. }
  1612. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1613. enum pipe pipe)
  1614. {
  1615. struct drm_device *dev = dev_priv->dev;
  1616. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1617. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1618. uint32_t reg, val, pipeconf_val;
  1619. /* PCH only available on ILK+ */
  1620. BUG_ON(!HAS_PCH_SPLIT(dev));
  1621. /* Make sure PCH DPLL is enabled */
  1622. assert_shared_dpll_enabled(dev_priv,
  1623. intel_crtc_to_shared_dpll(intel_crtc));
  1624. /* FDI must be feeding us bits for PCH ports */
  1625. assert_fdi_tx_enabled(dev_priv, pipe);
  1626. assert_fdi_rx_enabled(dev_priv, pipe);
  1627. if (HAS_PCH_CPT(dev)) {
  1628. /* Workaround: Set the timing override bit before enabling the
  1629. * pch transcoder. */
  1630. reg = TRANS_CHICKEN2(pipe);
  1631. val = I915_READ(reg);
  1632. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1633. I915_WRITE(reg, val);
  1634. }
  1635. reg = PCH_TRANSCONF(pipe);
  1636. val = I915_READ(reg);
  1637. pipeconf_val = I915_READ(PIPECONF(pipe));
  1638. if (HAS_PCH_IBX(dev_priv->dev)) {
  1639. /*
  1640. * make the BPC in transcoder be consistent with
  1641. * that in pipeconf reg.
  1642. */
  1643. val &= ~PIPECONF_BPC_MASK;
  1644. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1645. }
  1646. val &= ~TRANS_INTERLACE_MASK;
  1647. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1648. if (HAS_PCH_IBX(dev_priv->dev) &&
  1649. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1650. val |= TRANS_LEGACY_INTERLACED_ILK;
  1651. else
  1652. val |= TRANS_INTERLACED;
  1653. else
  1654. val |= TRANS_PROGRESSIVE;
  1655. I915_WRITE(reg, val | TRANS_ENABLE);
  1656. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1657. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1658. }
  1659. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1660. enum transcoder cpu_transcoder)
  1661. {
  1662. u32 val, pipeconf_val;
  1663. /* PCH only available on ILK+ */
  1664. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1665. /* FDI must be feeding us bits for PCH ports */
  1666. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1667. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1668. /* Workaround: set timing override bit. */
  1669. val = I915_READ(_TRANSA_CHICKEN2);
  1670. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1671. I915_WRITE(_TRANSA_CHICKEN2, val);
  1672. val = TRANS_ENABLE;
  1673. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1674. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1675. PIPECONF_INTERLACED_ILK)
  1676. val |= TRANS_INTERLACED;
  1677. else
  1678. val |= TRANS_PROGRESSIVE;
  1679. I915_WRITE(LPT_TRANSCONF, val);
  1680. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1681. DRM_ERROR("Failed to enable PCH transcoder\n");
  1682. }
  1683. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1684. enum pipe pipe)
  1685. {
  1686. struct drm_device *dev = dev_priv->dev;
  1687. uint32_t reg, val;
  1688. /* FDI relies on the transcoder */
  1689. assert_fdi_tx_disabled(dev_priv, pipe);
  1690. assert_fdi_rx_disabled(dev_priv, pipe);
  1691. /* Ports must be off as well */
  1692. assert_pch_ports_disabled(dev_priv, pipe);
  1693. reg = PCH_TRANSCONF(pipe);
  1694. val = I915_READ(reg);
  1695. val &= ~TRANS_ENABLE;
  1696. I915_WRITE(reg, val);
  1697. /* wait for PCH transcoder off, transcoder state */
  1698. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1699. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1700. if (!HAS_PCH_IBX(dev)) {
  1701. /* Workaround: Clear the timing override chicken bit again. */
  1702. reg = TRANS_CHICKEN2(pipe);
  1703. val = I915_READ(reg);
  1704. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1705. I915_WRITE(reg, val);
  1706. }
  1707. }
  1708. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1709. {
  1710. u32 val;
  1711. val = I915_READ(LPT_TRANSCONF);
  1712. val &= ~TRANS_ENABLE;
  1713. I915_WRITE(LPT_TRANSCONF, val);
  1714. /* wait for PCH transcoder off, transcoder state */
  1715. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1716. DRM_ERROR("Failed to disable PCH transcoder\n");
  1717. /* Workaround: clear timing override bit. */
  1718. val = I915_READ(_TRANSA_CHICKEN2);
  1719. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1720. I915_WRITE(_TRANSA_CHICKEN2, val);
  1721. }
  1722. /**
  1723. * intel_enable_pipe - enable a pipe, asserting requirements
  1724. * @crtc: crtc responsible for the pipe
  1725. *
  1726. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1727. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1728. */
  1729. static void intel_enable_pipe(struct intel_crtc *crtc)
  1730. {
  1731. struct drm_device *dev = crtc->base.dev;
  1732. struct drm_i915_private *dev_priv = dev->dev_private;
  1733. enum pipe pipe = crtc->pipe;
  1734. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1735. pipe);
  1736. enum pipe pch_transcoder;
  1737. int reg;
  1738. u32 val;
  1739. assert_planes_disabled(dev_priv, pipe);
  1740. assert_cursor_disabled(dev_priv, pipe);
  1741. assert_sprites_disabled(dev_priv, pipe);
  1742. if (HAS_PCH_LPT(dev_priv->dev))
  1743. pch_transcoder = TRANSCODER_A;
  1744. else
  1745. pch_transcoder = pipe;
  1746. /*
  1747. * A pipe without a PLL won't actually be able to drive bits from
  1748. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1749. * need the check.
  1750. */
  1751. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1752. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1753. assert_dsi_pll_enabled(dev_priv);
  1754. else
  1755. assert_pll_enabled(dev_priv, pipe);
  1756. else {
  1757. if (crtc->config.has_pch_encoder) {
  1758. /* if driving the PCH, we need FDI enabled */
  1759. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1760. assert_fdi_tx_pll_enabled(dev_priv,
  1761. (enum pipe) cpu_transcoder);
  1762. }
  1763. /* FIXME: assert CPU port conditions for SNB+ */
  1764. }
  1765. reg = PIPECONF(cpu_transcoder);
  1766. val = I915_READ(reg);
  1767. if (val & PIPECONF_ENABLE) {
  1768. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1769. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1770. return;
  1771. }
  1772. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1773. POSTING_READ(reg);
  1774. }
  1775. /**
  1776. * intel_disable_pipe - disable a pipe, asserting requirements
  1777. * @crtc: crtc whose pipes is to be disabled
  1778. *
  1779. * Disable the pipe of @crtc, making sure that various hardware
  1780. * specific requirements are met, if applicable, e.g. plane
  1781. * disabled, panel fitter off, etc.
  1782. *
  1783. * Will wait until the pipe has shut down before returning.
  1784. */
  1785. static void intel_disable_pipe(struct intel_crtc *crtc)
  1786. {
  1787. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1788. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  1789. enum pipe pipe = crtc->pipe;
  1790. int reg;
  1791. u32 val;
  1792. /*
  1793. * Make sure planes won't keep trying to pump pixels to us,
  1794. * or we might hang the display.
  1795. */
  1796. assert_planes_disabled(dev_priv, pipe);
  1797. assert_cursor_disabled(dev_priv, pipe);
  1798. assert_sprites_disabled(dev_priv, pipe);
  1799. reg = PIPECONF(cpu_transcoder);
  1800. val = I915_READ(reg);
  1801. if ((val & PIPECONF_ENABLE) == 0)
  1802. return;
  1803. /*
  1804. * Double wide has implications for planes
  1805. * so best keep it disabled when not needed.
  1806. */
  1807. if (crtc->config.double_wide)
  1808. val &= ~PIPECONF_DOUBLE_WIDE;
  1809. /* Don't disable pipe or pipe PLLs if needed */
  1810. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1811. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1812. val &= ~PIPECONF_ENABLE;
  1813. I915_WRITE(reg, val);
  1814. if ((val & PIPECONF_ENABLE) == 0)
  1815. intel_wait_for_pipe_off(crtc);
  1816. }
  1817. /*
  1818. * Plane regs are double buffered, going from enabled->disabled needs a
  1819. * trigger in order to latch. The display address reg provides this.
  1820. */
  1821. void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
  1822. enum plane plane)
  1823. {
  1824. struct drm_device *dev = dev_priv->dev;
  1825. u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
  1826. I915_WRITE(reg, I915_READ(reg));
  1827. POSTING_READ(reg);
  1828. }
  1829. /**
  1830. * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  1831. * @plane: plane to be enabled
  1832. * @crtc: crtc for the plane
  1833. *
  1834. * Enable @plane on @crtc, making sure that the pipe is running first.
  1835. */
  1836. static void intel_enable_primary_hw_plane(struct drm_plane *plane,
  1837. struct drm_crtc *crtc)
  1838. {
  1839. struct drm_device *dev = plane->dev;
  1840. struct drm_i915_private *dev_priv = dev->dev_private;
  1841. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1842. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1843. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1844. if (intel_crtc->primary_enabled)
  1845. return;
  1846. intel_crtc->primary_enabled = true;
  1847. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1848. crtc->x, crtc->y);
  1849. /*
  1850. * BDW signals flip done immediately if the plane
  1851. * is disabled, even if the plane enable is already
  1852. * armed to occur at the next vblank :(
  1853. */
  1854. if (IS_BROADWELL(dev))
  1855. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1856. }
  1857. /**
  1858. * intel_disable_primary_hw_plane - disable the primary hardware plane
  1859. * @plane: plane to be disabled
  1860. * @crtc: crtc for the plane
  1861. *
  1862. * Disable @plane on @crtc, making sure that the pipe is running first.
  1863. */
  1864. static void intel_disable_primary_hw_plane(struct drm_plane *plane,
  1865. struct drm_crtc *crtc)
  1866. {
  1867. struct drm_device *dev = plane->dev;
  1868. struct drm_i915_private *dev_priv = dev->dev_private;
  1869. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1870. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1871. if (!intel_crtc->primary_enabled)
  1872. return;
  1873. intel_crtc->primary_enabled = false;
  1874. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1875. crtc->x, crtc->y);
  1876. }
  1877. static bool need_vtd_wa(struct drm_device *dev)
  1878. {
  1879. #ifdef CONFIG_INTEL_IOMMU
  1880. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1881. return true;
  1882. #endif
  1883. return false;
  1884. }
  1885. static int intel_align_height(struct drm_device *dev, int height, bool tiled)
  1886. {
  1887. int tile_height;
  1888. tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
  1889. return ALIGN(height, tile_height);
  1890. }
  1891. int
  1892. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  1893. struct drm_framebuffer *fb,
  1894. struct intel_engine_cs *pipelined)
  1895. {
  1896. struct drm_device *dev = fb->dev;
  1897. struct drm_i915_private *dev_priv = dev->dev_private;
  1898. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1899. u32 alignment;
  1900. int ret;
  1901. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1902. switch (obj->tiling_mode) {
  1903. case I915_TILING_NONE:
  1904. if (INTEL_INFO(dev)->gen >= 9)
  1905. alignment = 256 * 1024;
  1906. else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1907. alignment = 128 * 1024;
  1908. else if (INTEL_INFO(dev)->gen >= 4)
  1909. alignment = 4 * 1024;
  1910. else
  1911. alignment = 64 * 1024;
  1912. break;
  1913. case I915_TILING_X:
  1914. if (INTEL_INFO(dev)->gen >= 9)
  1915. alignment = 256 * 1024;
  1916. else {
  1917. /* pin() will align the object as required by fence */
  1918. alignment = 0;
  1919. }
  1920. break;
  1921. case I915_TILING_Y:
  1922. WARN(1, "Y tiled bo slipped through, driver bug!\n");
  1923. return -EINVAL;
  1924. default:
  1925. BUG();
  1926. }
  1927. /* Note that the w/a also requires 64 PTE of padding following the
  1928. * bo. We currently fill all unused PTE with the shadow page and so
  1929. * we should always have valid PTE following the scanout preventing
  1930. * the VT-d warning.
  1931. */
  1932. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1933. alignment = 256 * 1024;
  1934. /*
  1935. * Global gtt pte registers are special registers which actually forward
  1936. * writes to a chunk of system memory. Which means that there is no risk
  1937. * that the register values disappear as soon as we call
  1938. * intel_runtime_pm_put(), so it is correct to wrap only the
  1939. * pin/unpin/fence and not more.
  1940. */
  1941. intel_runtime_pm_get(dev_priv);
  1942. dev_priv->mm.interruptible = false;
  1943. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1944. if (ret)
  1945. goto err_interruptible;
  1946. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1947. * fence, whereas 965+ only requires a fence if using
  1948. * framebuffer compression. For simplicity, we always install
  1949. * a fence as the cost is not that onerous.
  1950. */
  1951. ret = i915_gem_object_get_fence(obj);
  1952. if (ret)
  1953. goto err_unpin;
  1954. i915_gem_object_pin_fence(obj);
  1955. dev_priv->mm.interruptible = true;
  1956. intel_runtime_pm_put(dev_priv);
  1957. return 0;
  1958. err_unpin:
  1959. i915_gem_object_unpin_from_display_plane(obj);
  1960. err_interruptible:
  1961. dev_priv->mm.interruptible = true;
  1962. intel_runtime_pm_put(dev_priv);
  1963. return ret;
  1964. }
  1965. void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
  1966. {
  1967. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  1968. i915_gem_object_unpin_fence(obj);
  1969. i915_gem_object_unpin_from_display_plane(obj);
  1970. }
  1971. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  1972. * is assumed to be a power-of-two. */
  1973. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  1974. unsigned int tiling_mode,
  1975. unsigned int cpp,
  1976. unsigned int pitch)
  1977. {
  1978. if (tiling_mode != I915_TILING_NONE) {
  1979. unsigned int tile_rows, tiles;
  1980. tile_rows = *y / 8;
  1981. *y %= 8;
  1982. tiles = *x / (512/cpp);
  1983. *x %= 512/cpp;
  1984. return tile_rows * pitch * 8 + tiles * 4096;
  1985. } else {
  1986. unsigned int offset;
  1987. offset = *y * pitch + *x * cpp;
  1988. *y = 0;
  1989. *x = (offset & 4095) / cpp;
  1990. return offset & -4096;
  1991. }
  1992. }
  1993. int intel_format_to_fourcc(int format)
  1994. {
  1995. switch (format) {
  1996. case DISPPLANE_8BPP:
  1997. return DRM_FORMAT_C8;
  1998. case DISPPLANE_BGRX555:
  1999. return DRM_FORMAT_XRGB1555;
  2000. case DISPPLANE_BGRX565:
  2001. return DRM_FORMAT_RGB565;
  2002. default:
  2003. case DISPPLANE_BGRX888:
  2004. return DRM_FORMAT_XRGB8888;
  2005. case DISPPLANE_RGBX888:
  2006. return DRM_FORMAT_XBGR8888;
  2007. case DISPPLANE_BGRX101010:
  2008. return DRM_FORMAT_XRGB2101010;
  2009. case DISPPLANE_RGBX101010:
  2010. return DRM_FORMAT_XBGR2101010;
  2011. }
  2012. }
  2013. static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
  2014. struct intel_plane_config *plane_config)
  2015. {
  2016. struct drm_device *dev = crtc->base.dev;
  2017. struct drm_i915_gem_object *obj = NULL;
  2018. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2019. u32 base = plane_config->base;
  2020. if (plane_config->size == 0)
  2021. return false;
  2022. obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
  2023. plane_config->size);
  2024. if (!obj)
  2025. return false;
  2026. if (plane_config->tiled) {
  2027. obj->tiling_mode = I915_TILING_X;
  2028. obj->stride = crtc->base.primary->fb->pitches[0];
  2029. }
  2030. mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
  2031. mode_cmd.width = crtc->base.primary->fb->width;
  2032. mode_cmd.height = crtc->base.primary->fb->height;
  2033. mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
  2034. mutex_lock(&dev->struct_mutex);
  2035. if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
  2036. &mode_cmd, obj)) {
  2037. DRM_DEBUG_KMS("intel fb init failed\n");
  2038. goto out_unref_obj;
  2039. }
  2040. obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
  2041. mutex_unlock(&dev->struct_mutex);
  2042. DRM_DEBUG_KMS("plane fb obj %p\n", obj);
  2043. return true;
  2044. out_unref_obj:
  2045. drm_gem_object_unreference(&obj->base);
  2046. mutex_unlock(&dev->struct_mutex);
  2047. return false;
  2048. }
  2049. static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
  2050. struct intel_plane_config *plane_config)
  2051. {
  2052. struct drm_device *dev = intel_crtc->base.dev;
  2053. struct drm_i915_private *dev_priv = dev->dev_private;
  2054. struct drm_crtc *c;
  2055. struct intel_crtc *i;
  2056. struct drm_i915_gem_object *obj;
  2057. if (!intel_crtc->base.primary->fb)
  2058. return;
  2059. if (intel_alloc_plane_obj(intel_crtc, plane_config))
  2060. return;
  2061. kfree(intel_crtc->base.primary->fb);
  2062. intel_crtc->base.primary->fb = NULL;
  2063. /*
  2064. * Failed to alloc the obj, check to see if we should share
  2065. * an fb with another CRTC instead
  2066. */
  2067. for_each_crtc(dev, c) {
  2068. i = to_intel_crtc(c);
  2069. if (c == &intel_crtc->base)
  2070. continue;
  2071. if (!i->active)
  2072. continue;
  2073. obj = intel_fb_obj(c->primary->fb);
  2074. if (obj == NULL)
  2075. continue;
  2076. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2077. if (obj->tiling_mode != I915_TILING_NONE)
  2078. dev_priv->preserve_bios_swizzle = true;
  2079. drm_framebuffer_reference(c->primary->fb);
  2080. intel_crtc->base.primary->fb = c->primary->fb;
  2081. obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  2082. break;
  2083. }
  2084. }
  2085. }
  2086. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2087. struct drm_framebuffer *fb,
  2088. int x, int y)
  2089. {
  2090. struct drm_device *dev = crtc->dev;
  2091. struct drm_i915_private *dev_priv = dev->dev_private;
  2092. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2093. struct drm_i915_gem_object *obj;
  2094. int plane = intel_crtc->plane;
  2095. unsigned long linear_offset;
  2096. u32 dspcntr;
  2097. u32 reg = DSPCNTR(plane);
  2098. int pixel_size;
  2099. if (!intel_crtc->primary_enabled) {
  2100. I915_WRITE(reg, 0);
  2101. if (INTEL_INFO(dev)->gen >= 4)
  2102. I915_WRITE(DSPSURF(plane), 0);
  2103. else
  2104. I915_WRITE(DSPADDR(plane), 0);
  2105. POSTING_READ(reg);
  2106. return;
  2107. }
  2108. obj = intel_fb_obj(fb);
  2109. if (WARN_ON(obj == NULL))
  2110. return;
  2111. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2112. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2113. dspcntr |= DISPLAY_PLANE_ENABLE;
  2114. if (INTEL_INFO(dev)->gen < 4) {
  2115. if (intel_crtc->pipe == PIPE_B)
  2116. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2117. /* pipesrc and dspsize control the size that is scaled from,
  2118. * which should always be the user's requested size.
  2119. */
  2120. I915_WRITE(DSPSIZE(plane),
  2121. ((intel_crtc->config.pipe_src_h - 1) << 16) |
  2122. (intel_crtc->config.pipe_src_w - 1));
  2123. I915_WRITE(DSPPOS(plane), 0);
  2124. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2125. I915_WRITE(PRIMSIZE(plane),
  2126. ((intel_crtc->config.pipe_src_h - 1) << 16) |
  2127. (intel_crtc->config.pipe_src_w - 1));
  2128. I915_WRITE(PRIMPOS(plane), 0);
  2129. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2130. }
  2131. switch (fb->pixel_format) {
  2132. case DRM_FORMAT_C8:
  2133. dspcntr |= DISPPLANE_8BPP;
  2134. break;
  2135. case DRM_FORMAT_XRGB1555:
  2136. case DRM_FORMAT_ARGB1555:
  2137. dspcntr |= DISPPLANE_BGRX555;
  2138. break;
  2139. case DRM_FORMAT_RGB565:
  2140. dspcntr |= DISPPLANE_BGRX565;
  2141. break;
  2142. case DRM_FORMAT_XRGB8888:
  2143. case DRM_FORMAT_ARGB8888:
  2144. dspcntr |= DISPPLANE_BGRX888;
  2145. break;
  2146. case DRM_FORMAT_XBGR8888:
  2147. case DRM_FORMAT_ABGR8888:
  2148. dspcntr |= DISPPLANE_RGBX888;
  2149. break;
  2150. case DRM_FORMAT_XRGB2101010:
  2151. case DRM_FORMAT_ARGB2101010:
  2152. dspcntr |= DISPPLANE_BGRX101010;
  2153. break;
  2154. case DRM_FORMAT_XBGR2101010:
  2155. case DRM_FORMAT_ABGR2101010:
  2156. dspcntr |= DISPPLANE_RGBX101010;
  2157. break;
  2158. default:
  2159. BUG();
  2160. }
  2161. if (INTEL_INFO(dev)->gen >= 4 &&
  2162. obj->tiling_mode != I915_TILING_NONE)
  2163. dspcntr |= DISPPLANE_TILED;
  2164. if (IS_G4X(dev))
  2165. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2166. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2167. if (INTEL_INFO(dev)->gen >= 4) {
  2168. intel_crtc->dspaddr_offset =
  2169. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2170. pixel_size,
  2171. fb->pitches[0]);
  2172. linear_offset -= intel_crtc->dspaddr_offset;
  2173. } else {
  2174. intel_crtc->dspaddr_offset = linear_offset;
  2175. }
  2176. if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
  2177. dspcntr |= DISPPLANE_ROTATE_180;
  2178. x += (intel_crtc->config.pipe_src_w - 1);
  2179. y += (intel_crtc->config.pipe_src_h - 1);
  2180. /* Finding the last pixel of the last line of the display
  2181. data and adding to linear_offset*/
  2182. linear_offset +=
  2183. (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
  2184. (intel_crtc->config.pipe_src_w - 1) * pixel_size;
  2185. }
  2186. I915_WRITE(reg, dspcntr);
  2187. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2188. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2189. fb->pitches[0]);
  2190. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2191. if (INTEL_INFO(dev)->gen >= 4) {
  2192. I915_WRITE(DSPSURF(plane),
  2193. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2194. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2195. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2196. } else
  2197. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2198. POSTING_READ(reg);
  2199. }
  2200. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2201. struct drm_framebuffer *fb,
  2202. int x, int y)
  2203. {
  2204. struct drm_device *dev = crtc->dev;
  2205. struct drm_i915_private *dev_priv = dev->dev_private;
  2206. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2207. struct drm_i915_gem_object *obj;
  2208. int plane = intel_crtc->plane;
  2209. unsigned long linear_offset;
  2210. u32 dspcntr;
  2211. u32 reg = DSPCNTR(plane);
  2212. int pixel_size;
  2213. if (!intel_crtc->primary_enabled) {
  2214. I915_WRITE(reg, 0);
  2215. I915_WRITE(DSPSURF(plane), 0);
  2216. POSTING_READ(reg);
  2217. return;
  2218. }
  2219. obj = intel_fb_obj(fb);
  2220. if (WARN_ON(obj == NULL))
  2221. return;
  2222. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2223. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2224. dspcntr |= DISPLAY_PLANE_ENABLE;
  2225. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2226. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2227. switch (fb->pixel_format) {
  2228. case DRM_FORMAT_C8:
  2229. dspcntr |= DISPPLANE_8BPP;
  2230. break;
  2231. case DRM_FORMAT_RGB565:
  2232. dspcntr |= DISPPLANE_BGRX565;
  2233. break;
  2234. case DRM_FORMAT_XRGB8888:
  2235. case DRM_FORMAT_ARGB8888:
  2236. dspcntr |= DISPPLANE_BGRX888;
  2237. break;
  2238. case DRM_FORMAT_XBGR8888:
  2239. case DRM_FORMAT_ABGR8888:
  2240. dspcntr |= DISPPLANE_RGBX888;
  2241. break;
  2242. case DRM_FORMAT_XRGB2101010:
  2243. case DRM_FORMAT_ARGB2101010:
  2244. dspcntr |= DISPPLANE_BGRX101010;
  2245. break;
  2246. case DRM_FORMAT_XBGR2101010:
  2247. case DRM_FORMAT_ABGR2101010:
  2248. dspcntr |= DISPPLANE_RGBX101010;
  2249. break;
  2250. default:
  2251. BUG();
  2252. }
  2253. if (obj->tiling_mode != I915_TILING_NONE)
  2254. dspcntr |= DISPPLANE_TILED;
  2255. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2256. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2257. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2258. intel_crtc->dspaddr_offset =
  2259. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2260. pixel_size,
  2261. fb->pitches[0]);
  2262. linear_offset -= intel_crtc->dspaddr_offset;
  2263. if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
  2264. dspcntr |= DISPPLANE_ROTATE_180;
  2265. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2266. x += (intel_crtc->config.pipe_src_w - 1);
  2267. y += (intel_crtc->config.pipe_src_h - 1);
  2268. /* Finding the last pixel of the last line of the display
  2269. data and adding to linear_offset*/
  2270. linear_offset +=
  2271. (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
  2272. (intel_crtc->config.pipe_src_w - 1) * pixel_size;
  2273. }
  2274. }
  2275. I915_WRITE(reg, dspcntr);
  2276. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2277. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2278. fb->pitches[0]);
  2279. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2280. I915_WRITE(DSPSURF(plane),
  2281. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2282. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2283. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2284. } else {
  2285. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2286. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2287. }
  2288. POSTING_READ(reg);
  2289. }
  2290. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2291. struct drm_framebuffer *fb,
  2292. int x, int y)
  2293. {
  2294. struct drm_device *dev = crtc->dev;
  2295. struct drm_i915_private *dev_priv = dev->dev_private;
  2296. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2297. struct intel_framebuffer *intel_fb;
  2298. struct drm_i915_gem_object *obj;
  2299. int pipe = intel_crtc->pipe;
  2300. u32 plane_ctl, stride;
  2301. if (!intel_crtc->primary_enabled) {
  2302. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2303. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2304. POSTING_READ(PLANE_CTL(pipe, 0));
  2305. return;
  2306. }
  2307. plane_ctl = PLANE_CTL_ENABLE |
  2308. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2309. PLANE_CTL_PIPE_CSC_ENABLE;
  2310. switch (fb->pixel_format) {
  2311. case DRM_FORMAT_RGB565:
  2312. plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
  2313. break;
  2314. case DRM_FORMAT_XRGB8888:
  2315. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2316. break;
  2317. case DRM_FORMAT_XBGR8888:
  2318. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2319. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2320. break;
  2321. case DRM_FORMAT_XRGB2101010:
  2322. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2323. break;
  2324. case DRM_FORMAT_XBGR2101010:
  2325. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2326. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2327. break;
  2328. default:
  2329. BUG();
  2330. }
  2331. intel_fb = to_intel_framebuffer(fb);
  2332. obj = intel_fb->obj;
  2333. /*
  2334. * The stride is either expressed as a multiple of 64 bytes chunks for
  2335. * linear buffers or in number of tiles for tiled buffers.
  2336. */
  2337. switch (obj->tiling_mode) {
  2338. case I915_TILING_NONE:
  2339. stride = fb->pitches[0] >> 6;
  2340. break;
  2341. case I915_TILING_X:
  2342. plane_ctl |= PLANE_CTL_TILED_X;
  2343. stride = fb->pitches[0] >> 9;
  2344. break;
  2345. default:
  2346. BUG();
  2347. }
  2348. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2349. if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180))
  2350. plane_ctl |= PLANE_CTL_ROTATE_180;
  2351. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2352. DRM_DEBUG_KMS("Writing base %08lX %d,%d,%d,%d pitch=%d\n",
  2353. i915_gem_obj_ggtt_offset(obj),
  2354. x, y, fb->width, fb->height,
  2355. fb->pitches[0]);
  2356. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2357. I915_WRITE(PLANE_OFFSET(pipe, 0), (y << 16) | x);
  2358. I915_WRITE(PLANE_SIZE(pipe, 0),
  2359. (intel_crtc->config.pipe_src_h - 1) << 16 |
  2360. (intel_crtc->config.pipe_src_w - 1));
  2361. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2362. I915_WRITE(PLANE_SURF(pipe, 0), i915_gem_obj_ggtt_offset(obj));
  2363. POSTING_READ(PLANE_SURF(pipe, 0));
  2364. }
  2365. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2366. static int
  2367. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2368. int x, int y, enum mode_set_atomic state)
  2369. {
  2370. struct drm_device *dev = crtc->dev;
  2371. struct drm_i915_private *dev_priv = dev->dev_private;
  2372. if (dev_priv->display.disable_fbc)
  2373. dev_priv->display.disable_fbc(dev);
  2374. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2375. return 0;
  2376. }
  2377. static void intel_complete_page_flips(struct drm_device *dev)
  2378. {
  2379. struct drm_crtc *crtc;
  2380. for_each_crtc(dev, crtc) {
  2381. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2382. enum plane plane = intel_crtc->plane;
  2383. intel_prepare_page_flip(dev, plane);
  2384. intel_finish_page_flip_plane(dev, plane);
  2385. }
  2386. }
  2387. static void intel_update_primary_planes(struct drm_device *dev)
  2388. {
  2389. struct drm_i915_private *dev_priv = dev->dev_private;
  2390. struct drm_crtc *crtc;
  2391. for_each_crtc(dev, crtc) {
  2392. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2393. drm_modeset_lock(&crtc->mutex, NULL);
  2394. /*
  2395. * FIXME: Once we have proper support for primary planes (and
  2396. * disabling them without disabling the entire crtc) allow again
  2397. * a NULL crtc->primary->fb.
  2398. */
  2399. if (intel_crtc->active && crtc->primary->fb)
  2400. dev_priv->display.update_primary_plane(crtc,
  2401. crtc->primary->fb,
  2402. crtc->x,
  2403. crtc->y);
  2404. drm_modeset_unlock(&crtc->mutex);
  2405. }
  2406. }
  2407. void intel_prepare_reset(struct drm_device *dev)
  2408. {
  2409. struct drm_i915_private *dev_priv = to_i915(dev);
  2410. struct intel_crtc *crtc;
  2411. /* no reset support for gen2 */
  2412. if (IS_GEN2(dev))
  2413. return;
  2414. /* reset doesn't touch the display */
  2415. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2416. return;
  2417. drm_modeset_lock_all(dev);
  2418. /*
  2419. * Disabling the crtcs gracefully seems nicer. Also the
  2420. * g33 docs say we should at least disable all the planes.
  2421. */
  2422. for_each_intel_crtc(dev, crtc) {
  2423. if (crtc->active)
  2424. dev_priv->display.crtc_disable(&crtc->base);
  2425. }
  2426. }
  2427. void intel_finish_reset(struct drm_device *dev)
  2428. {
  2429. struct drm_i915_private *dev_priv = to_i915(dev);
  2430. /*
  2431. * Flips in the rings will be nuked by the reset,
  2432. * so complete all pending flips so that user space
  2433. * will get its events and not get stuck.
  2434. */
  2435. intel_complete_page_flips(dev);
  2436. /* no reset support for gen2 */
  2437. if (IS_GEN2(dev))
  2438. return;
  2439. /* reset doesn't touch the display */
  2440. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2441. /*
  2442. * Flips in the rings have been nuked by the reset,
  2443. * so update the base address of all primary
  2444. * planes to the the last fb to make sure we're
  2445. * showing the correct fb after a reset.
  2446. */
  2447. intel_update_primary_planes(dev);
  2448. return;
  2449. }
  2450. /*
  2451. * The display has been reset as well,
  2452. * so need a full re-initialization.
  2453. */
  2454. intel_runtime_pm_disable_interrupts(dev_priv);
  2455. intel_runtime_pm_enable_interrupts(dev_priv);
  2456. intel_modeset_init_hw(dev);
  2457. spin_lock_irq(&dev_priv->irq_lock);
  2458. if (dev_priv->display.hpd_irq_setup)
  2459. dev_priv->display.hpd_irq_setup(dev);
  2460. spin_unlock_irq(&dev_priv->irq_lock);
  2461. intel_modeset_setup_hw_state(dev, true);
  2462. intel_hpd_init(dev_priv);
  2463. drm_modeset_unlock_all(dev);
  2464. }
  2465. static int
  2466. intel_finish_fb(struct drm_framebuffer *old_fb)
  2467. {
  2468. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2469. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2470. bool was_interruptible = dev_priv->mm.interruptible;
  2471. int ret;
  2472. /* Big Hammer, we also need to ensure that any pending
  2473. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2474. * current scanout is retired before unpinning the old
  2475. * framebuffer.
  2476. *
  2477. * This should only fail upon a hung GPU, in which case we
  2478. * can safely continue.
  2479. */
  2480. dev_priv->mm.interruptible = false;
  2481. ret = i915_gem_object_finish_gpu(obj);
  2482. dev_priv->mm.interruptible = was_interruptible;
  2483. return ret;
  2484. }
  2485. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2486. {
  2487. struct drm_device *dev = crtc->dev;
  2488. struct drm_i915_private *dev_priv = dev->dev_private;
  2489. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2490. bool pending;
  2491. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2492. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2493. return false;
  2494. spin_lock_irq(&dev->event_lock);
  2495. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2496. spin_unlock_irq(&dev->event_lock);
  2497. return pending;
  2498. }
  2499. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2500. {
  2501. struct drm_device *dev = crtc->base.dev;
  2502. struct drm_i915_private *dev_priv = dev->dev_private;
  2503. const struct drm_display_mode *adjusted_mode;
  2504. if (!i915.fastboot)
  2505. return;
  2506. /*
  2507. * Update pipe size and adjust fitter if needed: the reason for this is
  2508. * that in compute_mode_changes we check the native mode (not the pfit
  2509. * mode) to see if we can flip rather than do a full mode set. In the
  2510. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2511. * pfit state, we'll end up with a big fb scanned out into the wrong
  2512. * sized surface.
  2513. *
  2514. * To fix this properly, we need to hoist the checks up into
  2515. * compute_mode_changes (or above), check the actual pfit state and
  2516. * whether the platform allows pfit disable with pipe active, and only
  2517. * then update the pipesrc and pfit state, even on the flip path.
  2518. */
  2519. adjusted_mode = &crtc->config.adjusted_mode;
  2520. I915_WRITE(PIPESRC(crtc->pipe),
  2521. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2522. (adjusted_mode->crtc_vdisplay - 1));
  2523. if (!crtc->config.pch_pfit.enabled &&
  2524. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2525. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2526. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2527. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2528. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2529. }
  2530. crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
  2531. crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
  2532. }
  2533. static int
  2534. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  2535. struct drm_framebuffer *fb)
  2536. {
  2537. struct drm_device *dev = crtc->dev;
  2538. struct drm_i915_private *dev_priv = dev->dev_private;
  2539. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2540. enum pipe pipe = intel_crtc->pipe;
  2541. struct drm_framebuffer *old_fb = crtc->primary->fb;
  2542. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
  2543. int ret;
  2544. if (intel_crtc_has_pending_flip(crtc)) {
  2545. DRM_ERROR("pipe is still busy with an old pageflip\n");
  2546. return -EBUSY;
  2547. }
  2548. /* no fb bound */
  2549. if (!fb) {
  2550. DRM_ERROR("No FB bound\n");
  2551. return 0;
  2552. }
  2553. if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
  2554. DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
  2555. plane_name(intel_crtc->plane),
  2556. INTEL_INFO(dev)->num_pipes);
  2557. return -EINVAL;
  2558. }
  2559. mutex_lock(&dev->struct_mutex);
  2560. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb, NULL);
  2561. if (ret == 0)
  2562. i915_gem_track_fb(old_obj, intel_fb_obj(fb),
  2563. INTEL_FRONTBUFFER_PRIMARY(pipe));
  2564. mutex_unlock(&dev->struct_mutex);
  2565. if (ret != 0) {
  2566. DRM_ERROR("pin & fence failed\n");
  2567. return ret;
  2568. }
  2569. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2570. if (intel_crtc->active)
  2571. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  2572. crtc->primary->fb = fb;
  2573. crtc->x = x;
  2574. crtc->y = y;
  2575. if (old_fb) {
  2576. if (intel_crtc->active && old_fb != fb)
  2577. intel_wait_for_vblank(dev, intel_crtc->pipe);
  2578. mutex_lock(&dev->struct_mutex);
  2579. intel_unpin_fb_obj(old_obj);
  2580. mutex_unlock(&dev->struct_mutex);
  2581. }
  2582. mutex_lock(&dev->struct_mutex);
  2583. intel_update_fbc(dev);
  2584. mutex_unlock(&dev->struct_mutex);
  2585. return 0;
  2586. }
  2587. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2588. {
  2589. struct drm_device *dev = crtc->dev;
  2590. struct drm_i915_private *dev_priv = dev->dev_private;
  2591. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2592. int pipe = intel_crtc->pipe;
  2593. u32 reg, temp;
  2594. /* enable normal train */
  2595. reg = FDI_TX_CTL(pipe);
  2596. temp = I915_READ(reg);
  2597. if (IS_IVYBRIDGE(dev)) {
  2598. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2599. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2600. } else {
  2601. temp &= ~FDI_LINK_TRAIN_NONE;
  2602. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2603. }
  2604. I915_WRITE(reg, temp);
  2605. reg = FDI_RX_CTL(pipe);
  2606. temp = I915_READ(reg);
  2607. if (HAS_PCH_CPT(dev)) {
  2608. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2609. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2610. } else {
  2611. temp &= ~FDI_LINK_TRAIN_NONE;
  2612. temp |= FDI_LINK_TRAIN_NONE;
  2613. }
  2614. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2615. /* wait one idle pattern time */
  2616. POSTING_READ(reg);
  2617. udelay(1000);
  2618. /* IVB wants error correction enabled */
  2619. if (IS_IVYBRIDGE(dev))
  2620. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2621. FDI_FE_ERRC_ENABLE);
  2622. }
  2623. static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
  2624. {
  2625. return crtc->base.enabled && crtc->active &&
  2626. crtc->config.has_pch_encoder;
  2627. }
  2628. static void ivb_modeset_global_resources(struct drm_device *dev)
  2629. {
  2630. struct drm_i915_private *dev_priv = dev->dev_private;
  2631. struct intel_crtc *pipe_B_crtc =
  2632. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  2633. struct intel_crtc *pipe_C_crtc =
  2634. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
  2635. uint32_t temp;
  2636. /*
  2637. * When everything is off disable fdi C so that we could enable fdi B
  2638. * with all lanes. Note that we don't care about enabled pipes without
  2639. * an enabled pch encoder.
  2640. */
  2641. if (!pipe_has_enabled_pch(pipe_B_crtc) &&
  2642. !pipe_has_enabled_pch(pipe_C_crtc)) {
  2643. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2644. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2645. temp = I915_READ(SOUTH_CHICKEN1);
  2646. temp &= ~FDI_BC_BIFURCATION_SELECT;
  2647. DRM_DEBUG_KMS("disabling fdi C rx\n");
  2648. I915_WRITE(SOUTH_CHICKEN1, temp);
  2649. }
  2650. }
  2651. /* The FDI link training functions for ILK/Ibexpeak. */
  2652. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2653. {
  2654. struct drm_device *dev = crtc->dev;
  2655. struct drm_i915_private *dev_priv = dev->dev_private;
  2656. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2657. int pipe = intel_crtc->pipe;
  2658. u32 reg, temp, tries;
  2659. /* FDI needs bits from pipe first */
  2660. assert_pipe_enabled(dev_priv, pipe);
  2661. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2662. for train result */
  2663. reg = FDI_RX_IMR(pipe);
  2664. temp = I915_READ(reg);
  2665. temp &= ~FDI_RX_SYMBOL_LOCK;
  2666. temp &= ~FDI_RX_BIT_LOCK;
  2667. I915_WRITE(reg, temp);
  2668. I915_READ(reg);
  2669. udelay(150);
  2670. /* enable CPU FDI TX and PCH FDI RX */
  2671. reg = FDI_TX_CTL(pipe);
  2672. temp = I915_READ(reg);
  2673. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2674. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2675. temp &= ~FDI_LINK_TRAIN_NONE;
  2676. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2677. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2678. reg = FDI_RX_CTL(pipe);
  2679. temp = I915_READ(reg);
  2680. temp &= ~FDI_LINK_TRAIN_NONE;
  2681. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2682. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2683. POSTING_READ(reg);
  2684. udelay(150);
  2685. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2686. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2687. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2688. FDI_RX_PHASE_SYNC_POINTER_EN);
  2689. reg = FDI_RX_IIR(pipe);
  2690. for (tries = 0; tries < 5; tries++) {
  2691. temp = I915_READ(reg);
  2692. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2693. if ((temp & FDI_RX_BIT_LOCK)) {
  2694. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2695. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2696. break;
  2697. }
  2698. }
  2699. if (tries == 5)
  2700. DRM_ERROR("FDI train 1 fail!\n");
  2701. /* Train 2 */
  2702. reg = FDI_TX_CTL(pipe);
  2703. temp = I915_READ(reg);
  2704. temp &= ~FDI_LINK_TRAIN_NONE;
  2705. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2706. I915_WRITE(reg, temp);
  2707. reg = FDI_RX_CTL(pipe);
  2708. temp = I915_READ(reg);
  2709. temp &= ~FDI_LINK_TRAIN_NONE;
  2710. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2711. I915_WRITE(reg, temp);
  2712. POSTING_READ(reg);
  2713. udelay(150);
  2714. reg = FDI_RX_IIR(pipe);
  2715. for (tries = 0; tries < 5; tries++) {
  2716. temp = I915_READ(reg);
  2717. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2718. if (temp & FDI_RX_SYMBOL_LOCK) {
  2719. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2720. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2721. break;
  2722. }
  2723. }
  2724. if (tries == 5)
  2725. DRM_ERROR("FDI train 2 fail!\n");
  2726. DRM_DEBUG_KMS("FDI train done\n");
  2727. }
  2728. static const int snb_b_fdi_train_param[] = {
  2729. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2730. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2731. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2732. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2733. };
  2734. /* The FDI link training functions for SNB/Cougarpoint. */
  2735. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2736. {
  2737. struct drm_device *dev = crtc->dev;
  2738. struct drm_i915_private *dev_priv = dev->dev_private;
  2739. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2740. int pipe = intel_crtc->pipe;
  2741. u32 reg, temp, i, retry;
  2742. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2743. for train result */
  2744. reg = FDI_RX_IMR(pipe);
  2745. temp = I915_READ(reg);
  2746. temp &= ~FDI_RX_SYMBOL_LOCK;
  2747. temp &= ~FDI_RX_BIT_LOCK;
  2748. I915_WRITE(reg, temp);
  2749. POSTING_READ(reg);
  2750. udelay(150);
  2751. /* enable CPU FDI TX and PCH FDI RX */
  2752. reg = FDI_TX_CTL(pipe);
  2753. temp = I915_READ(reg);
  2754. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2755. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2756. temp &= ~FDI_LINK_TRAIN_NONE;
  2757. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2758. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2759. /* SNB-B */
  2760. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2761. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2762. I915_WRITE(FDI_RX_MISC(pipe),
  2763. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2764. reg = FDI_RX_CTL(pipe);
  2765. temp = I915_READ(reg);
  2766. if (HAS_PCH_CPT(dev)) {
  2767. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2768. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2769. } else {
  2770. temp &= ~FDI_LINK_TRAIN_NONE;
  2771. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2772. }
  2773. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2774. POSTING_READ(reg);
  2775. udelay(150);
  2776. for (i = 0; i < 4; i++) {
  2777. reg = FDI_TX_CTL(pipe);
  2778. temp = I915_READ(reg);
  2779. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2780. temp |= snb_b_fdi_train_param[i];
  2781. I915_WRITE(reg, temp);
  2782. POSTING_READ(reg);
  2783. udelay(500);
  2784. for (retry = 0; retry < 5; retry++) {
  2785. reg = FDI_RX_IIR(pipe);
  2786. temp = I915_READ(reg);
  2787. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2788. if (temp & FDI_RX_BIT_LOCK) {
  2789. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2790. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2791. break;
  2792. }
  2793. udelay(50);
  2794. }
  2795. if (retry < 5)
  2796. break;
  2797. }
  2798. if (i == 4)
  2799. DRM_ERROR("FDI train 1 fail!\n");
  2800. /* Train 2 */
  2801. reg = FDI_TX_CTL(pipe);
  2802. temp = I915_READ(reg);
  2803. temp &= ~FDI_LINK_TRAIN_NONE;
  2804. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2805. if (IS_GEN6(dev)) {
  2806. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2807. /* SNB-B */
  2808. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2809. }
  2810. I915_WRITE(reg, temp);
  2811. reg = FDI_RX_CTL(pipe);
  2812. temp = I915_READ(reg);
  2813. if (HAS_PCH_CPT(dev)) {
  2814. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2815. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2816. } else {
  2817. temp &= ~FDI_LINK_TRAIN_NONE;
  2818. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2819. }
  2820. I915_WRITE(reg, temp);
  2821. POSTING_READ(reg);
  2822. udelay(150);
  2823. for (i = 0; i < 4; i++) {
  2824. reg = FDI_TX_CTL(pipe);
  2825. temp = I915_READ(reg);
  2826. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2827. temp |= snb_b_fdi_train_param[i];
  2828. I915_WRITE(reg, temp);
  2829. POSTING_READ(reg);
  2830. udelay(500);
  2831. for (retry = 0; retry < 5; retry++) {
  2832. reg = FDI_RX_IIR(pipe);
  2833. temp = I915_READ(reg);
  2834. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2835. if (temp & FDI_RX_SYMBOL_LOCK) {
  2836. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2837. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2838. break;
  2839. }
  2840. udelay(50);
  2841. }
  2842. if (retry < 5)
  2843. break;
  2844. }
  2845. if (i == 4)
  2846. DRM_ERROR("FDI train 2 fail!\n");
  2847. DRM_DEBUG_KMS("FDI train done.\n");
  2848. }
  2849. /* Manual link training for Ivy Bridge A0 parts */
  2850. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2851. {
  2852. struct drm_device *dev = crtc->dev;
  2853. struct drm_i915_private *dev_priv = dev->dev_private;
  2854. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2855. int pipe = intel_crtc->pipe;
  2856. u32 reg, temp, i, j;
  2857. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2858. for train result */
  2859. reg = FDI_RX_IMR(pipe);
  2860. temp = I915_READ(reg);
  2861. temp &= ~FDI_RX_SYMBOL_LOCK;
  2862. temp &= ~FDI_RX_BIT_LOCK;
  2863. I915_WRITE(reg, temp);
  2864. POSTING_READ(reg);
  2865. udelay(150);
  2866. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  2867. I915_READ(FDI_RX_IIR(pipe)));
  2868. /* Try each vswing and preemphasis setting twice before moving on */
  2869. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  2870. /* disable first in case we need to retry */
  2871. reg = FDI_TX_CTL(pipe);
  2872. temp = I915_READ(reg);
  2873. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2874. temp &= ~FDI_TX_ENABLE;
  2875. I915_WRITE(reg, temp);
  2876. reg = FDI_RX_CTL(pipe);
  2877. temp = I915_READ(reg);
  2878. temp &= ~FDI_LINK_TRAIN_AUTO;
  2879. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2880. temp &= ~FDI_RX_ENABLE;
  2881. I915_WRITE(reg, temp);
  2882. /* enable CPU FDI TX and PCH FDI RX */
  2883. reg = FDI_TX_CTL(pipe);
  2884. temp = I915_READ(reg);
  2885. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2886. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2887. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2888. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2889. temp |= snb_b_fdi_train_param[j/2];
  2890. temp |= FDI_COMPOSITE_SYNC;
  2891. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2892. I915_WRITE(FDI_RX_MISC(pipe),
  2893. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2894. reg = FDI_RX_CTL(pipe);
  2895. temp = I915_READ(reg);
  2896. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2897. temp |= FDI_COMPOSITE_SYNC;
  2898. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2899. POSTING_READ(reg);
  2900. udelay(1); /* should be 0.5us */
  2901. for (i = 0; i < 4; i++) {
  2902. reg = FDI_RX_IIR(pipe);
  2903. temp = I915_READ(reg);
  2904. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2905. if (temp & FDI_RX_BIT_LOCK ||
  2906. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2907. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2908. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  2909. i);
  2910. break;
  2911. }
  2912. udelay(1); /* should be 0.5us */
  2913. }
  2914. if (i == 4) {
  2915. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  2916. continue;
  2917. }
  2918. /* Train 2 */
  2919. reg = FDI_TX_CTL(pipe);
  2920. temp = I915_READ(reg);
  2921. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2922. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2923. I915_WRITE(reg, temp);
  2924. reg = FDI_RX_CTL(pipe);
  2925. temp = I915_READ(reg);
  2926. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2927. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2928. I915_WRITE(reg, temp);
  2929. POSTING_READ(reg);
  2930. udelay(2); /* should be 1.5us */
  2931. for (i = 0; i < 4; i++) {
  2932. reg = FDI_RX_IIR(pipe);
  2933. temp = I915_READ(reg);
  2934. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2935. if (temp & FDI_RX_SYMBOL_LOCK ||
  2936. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  2937. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2938. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  2939. i);
  2940. goto train_done;
  2941. }
  2942. udelay(2); /* should be 1.5us */
  2943. }
  2944. if (i == 4)
  2945. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  2946. }
  2947. train_done:
  2948. DRM_DEBUG_KMS("FDI train done.\n");
  2949. }
  2950. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  2951. {
  2952. struct drm_device *dev = intel_crtc->base.dev;
  2953. struct drm_i915_private *dev_priv = dev->dev_private;
  2954. int pipe = intel_crtc->pipe;
  2955. u32 reg, temp;
  2956. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2957. reg = FDI_RX_CTL(pipe);
  2958. temp = I915_READ(reg);
  2959. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  2960. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2961. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2962. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2963. POSTING_READ(reg);
  2964. udelay(200);
  2965. /* Switch from Rawclk to PCDclk */
  2966. temp = I915_READ(reg);
  2967. I915_WRITE(reg, temp | FDI_PCDCLK);
  2968. POSTING_READ(reg);
  2969. udelay(200);
  2970. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2971. reg = FDI_TX_CTL(pipe);
  2972. temp = I915_READ(reg);
  2973. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2974. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2975. POSTING_READ(reg);
  2976. udelay(100);
  2977. }
  2978. }
  2979. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  2980. {
  2981. struct drm_device *dev = intel_crtc->base.dev;
  2982. struct drm_i915_private *dev_priv = dev->dev_private;
  2983. int pipe = intel_crtc->pipe;
  2984. u32 reg, temp;
  2985. /* Switch from PCDclk to Rawclk */
  2986. reg = FDI_RX_CTL(pipe);
  2987. temp = I915_READ(reg);
  2988. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2989. /* Disable CPU FDI TX PLL */
  2990. reg = FDI_TX_CTL(pipe);
  2991. temp = I915_READ(reg);
  2992. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2993. POSTING_READ(reg);
  2994. udelay(100);
  2995. reg = FDI_RX_CTL(pipe);
  2996. temp = I915_READ(reg);
  2997. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2998. /* Wait for the clocks to turn off. */
  2999. POSTING_READ(reg);
  3000. udelay(100);
  3001. }
  3002. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3003. {
  3004. struct drm_device *dev = crtc->dev;
  3005. struct drm_i915_private *dev_priv = dev->dev_private;
  3006. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3007. int pipe = intel_crtc->pipe;
  3008. u32 reg, temp;
  3009. /* disable CPU FDI tx and PCH FDI rx */
  3010. reg = FDI_TX_CTL(pipe);
  3011. temp = I915_READ(reg);
  3012. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3013. POSTING_READ(reg);
  3014. reg = FDI_RX_CTL(pipe);
  3015. temp = I915_READ(reg);
  3016. temp &= ~(0x7 << 16);
  3017. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3018. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3019. POSTING_READ(reg);
  3020. udelay(100);
  3021. /* Ironlake workaround, disable clock pointer after downing FDI */
  3022. if (HAS_PCH_IBX(dev))
  3023. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3024. /* still set train pattern 1 */
  3025. reg = FDI_TX_CTL(pipe);
  3026. temp = I915_READ(reg);
  3027. temp &= ~FDI_LINK_TRAIN_NONE;
  3028. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3029. I915_WRITE(reg, temp);
  3030. reg = FDI_RX_CTL(pipe);
  3031. temp = I915_READ(reg);
  3032. if (HAS_PCH_CPT(dev)) {
  3033. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3034. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3035. } else {
  3036. temp &= ~FDI_LINK_TRAIN_NONE;
  3037. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3038. }
  3039. /* BPC in FDI rx is consistent with that in PIPECONF */
  3040. temp &= ~(0x07 << 16);
  3041. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3042. I915_WRITE(reg, temp);
  3043. POSTING_READ(reg);
  3044. udelay(100);
  3045. }
  3046. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3047. {
  3048. struct intel_crtc *crtc;
  3049. /* Note that we don't need to be called with mode_config.lock here
  3050. * as our list of CRTC objects is static for the lifetime of the
  3051. * device and so cannot disappear as we iterate. Similarly, we can
  3052. * happily treat the predicates as racy, atomic checks as userspace
  3053. * cannot claim and pin a new fb without at least acquring the
  3054. * struct_mutex and so serialising with us.
  3055. */
  3056. for_each_intel_crtc(dev, crtc) {
  3057. if (atomic_read(&crtc->unpin_work_count) == 0)
  3058. continue;
  3059. if (crtc->unpin_work)
  3060. intel_wait_for_vblank(dev, crtc->pipe);
  3061. return true;
  3062. }
  3063. return false;
  3064. }
  3065. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3066. {
  3067. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3068. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3069. /* ensure that the unpin work is consistent wrt ->pending. */
  3070. smp_rmb();
  3071. intel_crtc->unpin_work = NULL;
  3072. if (work->event)
  3073. drm_send_vblank_event(intel_crtc->base.dev,
  3074. intel_crtc->pipe,
  3075. work->event);
  3076. drm_crtc_vblank_put(&intel_crtc->base);
  3077. wake_up_all(&dev_priv->pending_flip_queue);
  3078. queue_work(dev_priv->wq, &work->work);
  3079. trace_i915_flip_complete(intel_crtc->plane,
  3080. work->pending_flip_obj);
  3081. }
  3082. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3083. {
  3084. struct drm_device *dev = crtc->dev;
  3085. struct drm_i915_private *dev_priv = dev->dev_private;
  3086. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3087. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3088. !intel_crtc_has_pending_flip(crtc),
  3089. 60*HZ) == 0)) {
  3090. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3091. spin_lock_irq(&dev->event_lock);
  3092. if (intel_crtc->unpin_work) {
  3093. WARN_ONCE(1, "Removing stuck page flip\n");
  3094. page_flip_completed(intel_crtc);
  3095. }
  3096. spin_unlock_irq(&dev->event_lock);
  3097. }
  3098. if (crtc->primary->fb) {
  3099. mutex_lock(&dev->struct_mutex);
  3100. intel_finish_fb(crtc->primary->fb);
  3101. mutex_unlock(&dev->struct_mutex);
  3102. }
  3103. }
  3104. /* Program iCLKIP clock to the desired frequency */
  3105. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3106. {
  3107. struct drm_device *dev = crtc->dev;
  3108. struct drm_i915_private *dev_priv = dev->dev_private;
  3109. int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
  3110. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3111. u32 temp;
  3112. mutex_lock(&dev_priv->dpio_lock);
  3113. /* It is necessary to ungate the pixclk gate prior to programming
  3114. * the divisors, and gate it back when it is done.
  3115. */
  3116. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3117. /* Disable SSCCTL */
  3118. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3119. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3120. SBI_SSCCTL_DISABLE,
  3121. SBI_ICLK);
  3122. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3123. if (clock == 20000) {
  3124. auxdiv = 1;
  3125. divsel = 0x41;
  3126. phaseinc = 0x20;
  3127. } else {
  3128. /* The iCLK virtual clock root frequency is in MHz,
  3129. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3130. * divisors, it is necessary to divide one by another, so we
  3131. * convert the virtual clock precision to KHz here for higher
  3132. * precision.
  3133. */
  3134. u32 iclk_virtual_root_freq = 172800 * 1000;
  3135. u32 iclk_pi_range = 64;
  3136. u32 desired_divisor, msb_divisor_value, pi_value;
  3137. desired_divisor = (iclk_virtual_root_freq / clock);
  3138. msb_divisor_value = desired_divisor / iclk_pi_range;
  3139. pi_value = desired_divisor % iclk_pi_range;
  3140. auxdiv = 0;
  3141. divsel = msb_divisor_value - 2;
  3142. phaseinc = pi_value;
  3143. }
  3144. /* This should not happen with any sane values */
  3145. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3146. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3147. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3148. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3149. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3150. clock,
  3151. auxdiv,
  3152. divsel,
  3153. phasedir,
  3154. phaseinc);
  3155. /* Program SSCDIVINTPHASE6 */
  3156. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3157. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3158. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3159. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3160. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3161. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3162. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3163. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3164. /* Program SSCAUXDIV */
  3165. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3166. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3167. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3168. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3169. /* Enable modulator and associated divider */
  3170. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3171. temp &= ~SBI_SSCCTL_DISABLE;
  3172. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3173. /* Wait for initialization time */
  3174. udelay(24);
  3175. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3176. mutex_unlock(&dev_priv->dpio_lock);
  3177. }
  3178. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3179. enum pipe pch_transcoder)
  3180. {
  3181. struct drm_device *dev = crtc->base.dev;
  3182. struct drm_i915_private *dev_priv = dev->dev_private;
  3183. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  3184. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3185. I915_READ(HTOTAL(cpu_transcoder)));
  3186. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3187. I915_READ(HBLANK(cpu_transcoder)));
  3188. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3189. I915_READ(HSYNC(cpu_transcoder)));
  3190. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3191. I915_READ(VTOTAL(cpu_transcoder)));
  3192. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3193. I915_READ(VBLANK(cpu_transcoder)));
  3194. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3195. I915_READ(VSYNC(cpu_transcoder)));
  3196. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3197. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3198. }
  3199. static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
  3200. {
  3201. struct drm_i915_private *dev_priv = dev->dev_private;
  3202. uint32_t temp;
  3203. temp = I915_READ(SOUTH_CHICKEN1);
  3204. if (temp & FDI_BC_BIFURCATION_SELECT)
  3205. return;
  3206. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3207. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3208. temp |= FDI_BC_BIFURCATION_SELECT;
  3209. DRM_DEBUG_KMS("enabling fdi C rx\n");
  3210. I915_WRITE(SOUTH_CHICKEN1, temp);
  3211. POSTING_READ(SOUTH_CHICKEN1);
  3212. }
  3213. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3214. {
  3215. struct drm_device *dev = intel_crtc->base.dev;
  3216. struct drm_i915_private *dev_priv = dev->dev_private;
  3217. switch (intel_crtc->pipe) {
  3218. case PIPE_A:
  3219. break;
  3220. case PIPE_B:
  3221. if (intel_crtc->config.fdi_lanes > 2)
  3222. WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
  3223. else
  3224. cpt_enable_fdi_bc_bifurcation(dev);
  3225. break;
  3226. case PIPE_C:
  3227. cpt_enable_fdi_bc_bifurcation(dev);
  3228. break;
  3229. default:
  3230. BUG();
  3231. }
  3232. }
  3233. /*
  3234. * Enable PCH resources required for PCH ports:
  3235. * - PCH PLLs
  3236. * - FDI training & RX/TX
  3237. * - update transcoder timings
  3238. * - DP transcoding bits
  3239. * - transcoder
  3240. */
  3241. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3242. {
  3243. struct drm_device *dev = crtc->dev;
  3244. struct drm_i915_private *dev_priv = dev->dev_private;
  3245. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3246. int pipe = intel_crtc->pipe;
  3247. u32 reg, temp;
  3248. assert_pch_transcoder_disabled(dev_priv, pipe);
  3249. if (IS_IVYBRIDGE(dev))
  3250. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3251. /* Write the TU size bits before fdi link training, so that error
  3252. * detection works. */
  3253. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3254. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3255. /* For PCH output, training FDI link */
  3256. dev_priv->display.fdi_link_train(crtc);
  3257. /* We need to program the right clock selection before writing the pixel
  3258. * mutliplier into the DPLL. */
  3259. if (HAS_PCH_CPT(dev)) {
  3260. u32 sel;
  3261. temp = I915_READ(PCH_DPLL_SEL);
  3262. temp |= TRANS_DPLL_ENABLE(pipe);
  3263. sel = TRANS_DPLLB_SEL(pipe);
  3264. if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
  3265. temp |= sel;
  3266. else
  3267. temp &= ~sel;
  3268. I915_WRITE(PCH_DPLL_SEL, temp);
  3269. }
  3270. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3271. * transcoder, and we actually should do this to not upset any PCH
  3272. * transcoder that already use the clock when we share it.
  3273. *
  3274. * Note that enable_shared_dpll tries to do the right thing, but
  3275. * get_shared_dpll unconditionally resets the pll - we need that to have
  3276. * the right LVDS enable sequence. */
  3277. intel_enable_shared_dpll(intel_crtc);
  3278. /* set transcoder timing, panel must allow it */
  3279. assert_panel_unlocked(dev_priv, pipe);
  3280. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3281. intel_fdi_normal_train(crtc);
  3282. /* For PCH DP, enable TRANS_DP_CTL */
  3283. if (HAS_PCH_CPT(dev) && intel_crtc->config.has_dp_encoder) {
  3284. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3285. reg = TRANS_DP_CTL(pipe);
  3286. temp = I915_READ(reg);
  3287. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3288. TRANS_DP_SYNC_MASK |
  3289. TRANS_DP_BPC_MASK);
  3290. temp |= (TRANS_DP_OUTPUT_ENABLE |
  3291. TRANS_DP_ENH_FRAMING);
  3292. temp |= bpc << 9; /* same format but at 11:9 */
  3293. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3294. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3295. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3296. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3297. switch (intel_trans_dp_port_sel(crtc)) {
  3298. case PCH_DP_B:
  3299. temp |= TRANS_DP_PORT_SEL_B;
  3300. break;
  3301. case PCH_DP_C:
  3302. temp |= TRANS_DP_PORT_SEL_C;
  3303. break;
  3304. case PCH_DP_D:
  3305. temp |= TRANS_DP_PORT_SEL_D;
  3306. break;
  3307. default:
  3308. BUG();
  3309. }
  3310. I915_WRITE(reg, temp);
  3311. }
  3312. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3313. }
  3314. static void lpt_pch_enable(struct drm_crtc *crtc)
  3315. {
  3316. struct drm_device *dev = crtc->dev;
  3317. struct drm_i915_private *dev_priv = dev->dev_private;
  3318. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3319. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3320. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3321. lpt_program_iclkip(crtc);
  3322. /* Set transcoder timing. */
  3323. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3324. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3325. }
  3326. void intel_put_shared_dpll(struct intel_crtc *crtc)
  3327. {
  3328. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3329. if (pll == NULL)
  3330. return;
  3331. if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
  3332. WARN(1, "bad %s crtc mask\n", pll->name);
  3333. return;
  3334. }
  3335. pll->config.crtc_mask &= ~(1 << crtc->pipe);
  3336. if (pll->config.crtc_mask == 0) {
  3337. WARN_ON(pll->on);
  3338. WARN_ON(pll->active);
  3339. }
  3340. crtc->config.shared_dpll = DPLL_ID_PRIVATE;
  3341. }
  3342. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
  3343. {
  3344. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3345. struct intel_shared_dpll *pll;
  3346. enum intel_dpll_id i;
  3347. if (HAS_PCH_IBX(dev_priv->dev)) {
  3348. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3349. i = (enum intel_dpll_id) crtc->pipe;
  3350. pll = &dev_priv->shared_dplls[i];
  3351. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3352. crtc->base.base.id, pll->name);
  3353. WARN_ON(pll->new_config->crtc_mask);
  3354. goto found;
  3355. }
  3356. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3357. pll = &dev_priv->shared_dplls[i];
  3358. /* Only want to check enabled timings first */
  3359. if (pll->new_config->crtc_mask == 0)
  3360. continue;
  3361. if (memcmp(&crtc->new_config->dpll_hw_state,
  3362. &pll->new_config->hw_state,
  3363. sizeof(pll->new_config->hw_state)) == 0) {
  3364. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3365. crtc->base.base.id, pll->name,
  3366. pll->new_config->crtc_mask,
  3367. pll->active);
  3368. goto found;
  3369. }
  3370. }
  3371. /* Ok no matching timings, maybe there's a free one? */
  3372. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3373. pll = &dev_priv->shared_dplls[i];
  3374. if (pll->new_config->crtc_mask == 0) {
  3375. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3376. crtc->base.base.id, pll->name);
  3377. goto found;
  3378. }
  3379. }
  3380. return NULL;
  3381. found:
  3382. if (pll->new_config->crtc_mask == 0)
  3383. pll->new_config->hw_state = crtc->new_config->dpll_hw_state;
  3384. crtc->new_config->shared_dpll = i;
  3385. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3386. pipe_name(crtc->pipe));
  3387. pll->new_config->crtc_mask |= 1 << crtc->pipe;
  3388. return pll;
  3389. }
  3390. /**
  3391. * intel_shared_dpll_start_config - start a new PLL staged config
  3392. * @dev_priv: DRM device
  3393. * @clear_pipes: mask of pipes that will have their PLLs freed
  3394. *
  3395. * Starts a new PLL staged config, copying the current config but
  3396. * releasing the references of pipes specified in clear_pipes.
  3397. */
  3398. static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
  3399. unsigned clear_pipes)
  3400. {
  3401. struct intel_shared_dpll *pll;
  3402. enum intel_dpll_id i;
  3403. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3404. pll = &dev_priv->shared_dplls[i];
  3405. pll->new_config = kmemdup(&pll->config, sizeof pll->config,
  3406. GFP_KERNEL);
  3407. if (!pll->new_config)
  3408. goto cleanup;
  3409. pll->new_config->crtc_mask &= ~clear_pipes;
  3410. }
  3411. return 0;
  3412. cleanup:
  3413. while (--i >= 0) {
  3414. pll = &dev_priv->shared_dplls[i];
  3415. kfree(pll->new_config);
  3416. pll->new_config = NULL;
  3417. }
  3418. return -ENOMEM;
  3419. }
  3420. static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
  3421. {
  3422. struct intel_shared_dpll *pll;
  3423. enum intel_dpll_id i;
  3424. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3425. pll = &dev_priv->shared_dplls[i];
  3426. WARN_ON(pll->new_config == &pll->config);
  3427. pll->config = *pll->new_config;
  3428. kfree(pll->new_config);
  3429. pll->new_config = NULL;
  3430. }
  3431. }
  3432. static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
  3433. {
  3434. struct intel_shared_dpll *pll;
  3435. enum intel_dpll_id i;
  3436. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3437. pll = &dev_priv->shared_dplls[i];
  3438. WARN_ON(pll->new_config == &pll->config);
  3439. kfree(pll->new_config);
  3440. pll->new_config = NULL;
  3441. }
  3442. }
  3443. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3444. {
  3445. struct drm_i915_private *dev_priv = dev->dev_private;
  3446. int dslreg = PIPEDSL(pipe);
  3447. u32 temp;
  3448. temp = I915_READ(dslreg);
  3449. udelay(500);
  3450. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3451. if (wait_for(I915_READ(dslreg) != temp, 5))
  3452. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3453. }
  3454. }
  3455. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3456. {
  3457. struct drm_device *dev = crtc->base.dev;
  3458. struct drm_i915_private *dev_priv = dev->dev_private;
  3459. int pipe = crtc->pipe;
  3460. if (crtc->config.pch_pfit.enabled) {
  3461. I915_WRITE(PS_CTL(pipe), PS_ENABLE);
  3462. I915_WRITE(PS_WIN_POS(pipe), crtc->config.pch_pfit.pos);
  3463. I915_WRITE(PS_WIN_SZ(pipe), crtc->config.pch_pfit.size);
  3464. }
  3465. }
  3466. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3467. {
  3468. struct drm_device *dev = crtc->base.dev;
  3469. struct drm_i915_private *dev_priv = dev->dev_private;
  3470. int pipe = crtc->pipe;
  3471. if (crtc->config.pch_pfit.enabled) {
  3472. /* Force use of hard-coded filter coefficients
  3473. * as some pre-programmed values are broken,
  3474. * e.g. x201.
  3475. */
  3476. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3477. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3478. PF_PIPE_SEL_IVB(pipe));
  3479. else
  3480. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3481. I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
  3482. I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
  3483. }
  3484. }
  3485. static void intel_enable_planes(struct drm_crtc *crtc)
  3486. {
  3487. struct drm_device *dev = crtc->dev;
  3488. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3489. struct drm_plane *plane;
  3490. struct intel_plane *intel_plane;
  3491. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3492. intel_plane = to_intel_plane(plane);
  3493. if (intel_plane->pipe == pipe)
  3494. intel_plane_restore(&intel_plane->base);
  3495. }
  3496. }
  3497. static void intel_disable_planes(struct drm_crtc *crtc)
  3498. {
  3499. struct drm_device *dev = crtc->dev;
  3500. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3501. struct drm_plane *plane;
  3502. struct intel_plane *intel_plane;
  3503. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3504. intel_plane = to_intel_plane(plane);
  3505. if (intel_plane->pipe == pipe)
  3506. intel_plane_disable(&intel_plane->base);
  3507. }
  3508. }
  3509. void hsw_enable_ips(struct intel_crtc *crtc)
  3510. {
  3511. struct drm_device *dev = crtc->base.dev;
  3512. struct drm_i915_private *dev_priv = dev->dev_private;
  3513. if (!crtc->config.ips_enabled)
  3514. return;
  3515. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3516. intel_wait_for_vblank(dev, crtc->pipe);
  3517. assert_plane_enabled(dev_priv, crtc->plane);
  3518. if (IS_BROADWELL(dev)) {
  3519. mutex_lock(&dev_priv->rps.hw_lock);
  3520. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3521. mutex_unlock(&dev_priv->rps.hw_lock);
  3522. /* Quoting Art Runyan: "its not safe to expect any particular
  3523. * value in IPS_CTL bit 31 after enabling IPS through the
  3524. * mailbox." Moreover, the mailbox may return a bogus state,
  3525. * so we need to just enable it and continue on.
  3526. */
  3527. } else {
  3528. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3529. /* The bit only becomes 1 in the next vblank, so this wait here
  3530. * is essentially intel_wait_for_vblank. If we don't have this
  3531. * and don't wait for vblanks until the end of crtc_enable, then
  3532. * the HW state readout code will complain that the expected
  3533. * IPS_CTL value is not the one we read. */
  3534. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3535. DRM_ERROR("Timed out waiting for IPS enable\n");
  3536. }
  3537. }
  3538. void hsw_disable_ips(struct intel_crtc *crtc)
  3539. {
  3540. struct drm_device *dev = crtc->base.dev;
  3541. struct drm_i915_private *dev_priv = dev->dev_private;
  3542. if (!crtc->config.ips_enabled)
  3543. return;
  3544. assert_plane_enabled(dev_priv, crtc->plane);
  3545. if (IS_BROADWELL(dev)) {
  3546. mutex_lock(&dev_priv->rps.hw_lock);
  3547. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3548. mutex_unlock(&dev_priv->rps.hw_lock);
  3549. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3550. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3551. DRM_ERROR("Timed out waiting for IPS disable\n");
  3552. } else {
  3553. I915_WRITE(IPS_CTL, 0);
  3554. POSTING_READ(IPS_CTL);
  3555. }
  3556. /* We need to wait for a vblank before we can disable the plane. */
  3557. intel_wait_for_vblank(dev, crtc->pipe);
  3558. }
  3559. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3560. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3561. {
  3562. struct drm_device *dev = crtc->dev;
  3563. struct drm_i915_private *dev_priv = dev->dev_private;
  3564. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3565. enum pipe pipe = intel_crtc->pipe;
  3566. int palreg = PALETTE(pipe);
  3567. int i;
  3568. bool reenable_ips = false;
  3569. /* The clocks have to be on to load the palette. */
  3570. if (!crtc->enabled || !intel_crtc->active)
  3571. return;
  3572. if (!HAS_PCH_SPLIT(dev_priv->dev)) {
  3573. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3574. assert_dsi_pll_enabled(dev_priv);
  3575. else
  3576. assert_pll_enabled(dev_priv, pipe);
  3577. }
  3578. /* use legacy palette for Ironlake */
  3579. if (!HAS_GMCH_DISPLAY(dev))
  3580. palreg = LGC_PALETTE(pipe);
  3581. /* Workaround : Do not read or write the pipe palette/gamma data while
  3582. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3583. */
  3584. if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
  3585. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3586. GAMMA_MODE_MODE_SPLIT)) {
  3587. hsw_disable_ips(intel_crtc);
  3588. reenable_ips = true;
  3589. }
  3590. for (i = 0; i < 256; i++) {
  3591. I915_WRITE(palreg + 4 * i,
  3592. (intel_crtc->lut_r[i] << 16) |
  3593. (intel_crtc->lut_g[i] << 8) |
  3594. intel_crtc->lut_b[i]);
  3595. }
  3596. if (reenable_ips)
  3597. hsw_enable_ips(intel_crtc);
  3598. }
  3599. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  3600. {
  3601. if (!enable && intel_crtc->overlay) {
  3602. struct drm_device *dev = intel_crtc->base.dev;
  3603. struct drm_i915_private *dev_priv = dev->dev_private;
  3604. mutex_lock(&dev->struct_mutex);
  3605. dev_priv->mm.interruptible = false;
  3606. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3607. dev_priv->mm.interruptible = true;
  3608. mutex_unlock(&dev->struct_mutex);
  3609. }
  3610. /* Let userspace switch the overlay on again. In most cases userspace
  3611. * has to recompute where to put it anyway.
  3612. */
  3613. }
  3614. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  3615. {
  3616. struct drm_device *dev = crtc->dev;
  3617. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3618. int pipe = intel_crtc->pipe;
  3619. intel_enable_primary_hw_plane(crtc->primary, crtc);
  3620. intel_enable_planes(crtc);
  3621. intel_crtc_update_cursor(crtc, true);
  3622. intel_crtc_dpms_overlay(intel_crtc, true);
  3623. hsw_enable_ips(intel_crtc);
  3624. mutex_lock(&dev->struct_mutex);
  3625. intel_update_fbc(dev);
  3626. mutex_unlock(&dev->struct_mutex);
  3627. /*
  3628. * FIXME: Once we grow proper nuclear flip support out of this we need
  3629. * to compute the mask of flip planes precisely. For the time being
  3630. * consider this a flip from a NULL plane.
  3631. */
  3632. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3633. }
  3634. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  3635. {
  3636. struct drm_device *dev = crtc->dev;
  3637. struct drm_i915_private *dev_priv = dev->dev_private;
  3638. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3639. int pipe = intel_crtc->pipe;
  3640. int plane = intel_crtc->plane;
  3641. intel_crtc_wait_for_pending_flips(crtc);
  3642. if (dev_priv->fbc.plane == plane)
  3643. intel_disable_fbc(dev);
  3644. hsw_disable_ips(intel_crtc);
  3645. intel_crtc_dpms_overlay(intel_crtc, false);
  3646. intel_crtc_update_cursor(crtc, false);
  3647. intel_disable_planes(crtc);
  3648. intel_disable_primary_hw_plane(crtc->primary, crtc);
  3649. /*
  3650. * FIXME: Once we grow proper nuclear flip support out of this we need
  3651. * to compute the mask of flip planes precisely. For the time being
  3652. * consider this a flip to a NULL plane.
  3653. */
  3654. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3655. }
  3656. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  3657. {
  3658. struct drm_device *dev = crtc->dev;
  3659. struct drm_i915_private *dev_priv = dev->dev_private;
  3660. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3661. struct intel_encoder *encoder;
  3662. int pipe = intel_crtc->pipe;
  3663. WARN_ON(!crtc->enabled);
  3664. if (intel_crtc->active)
  3665. return;
  3666. if (intel_crtc->config.has_pch_encoder)
  3667. intel_prepare_shared_dpll(intel_crtc);
  3668. if (intel_crtc->config.has_dp_encoder)
  3669. intel_dp_set_m_n(intel_crtc);
  3670. intel_set_pipe_timings(intel_crtc);
  3671. if (intel_crtc->config.has_pch_encoder) {
  3672. intel_cpu_transcoder_set_m_n(intel_crtc,
  3673. &intel_crtc->config.fdi_m_n, NULL);
  3674. }
  3675. ironlake_set_pipeconf(crtc);
  3676. intel_crtc->active = true;
  3677. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3678. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  3679. for_each_encoder_on_crtc(dev, crtc, encoder)
  3680. if (encoder->pre_enable)
  3681. encoder->pre_enable(encoder);
  3682. if (intel_crtc->config.has_pch_encoder) {
  3683. /* Note: FDI PLL enabling _must_ be done before we enable the
  3684. * cpu pipes, hence this is separate from all the other fdi/pch
  3685. * enabling. */
  3686. ironlake_fdi_pll_enable(intel_crtc);
  3687. } else {
  3688. assert_fdi_tx_disabled(dev_priv, pipe);
  3689. assert_fdi_rx_disabled(dev_priv, pipe);
  3690. }
  3691. ironlake_pfit_enable(intel_crtc);
  3692. /*
  3693. * On ILK+ LUT must be loaded before the pipe is running but with
  3694. * clocks enabled
  3695. */
  3696. intel_crtc_load_lut(crtc);
  3697. intel_update_watermarks(crtc);
  3698. intel_enable_pipe(intel_crtc);
  3699. if (intel_crtc->config.has_pch_encoder)
  3700. ironlake_pch_enable(crtc);
  3701. for_each_encoder_on_crtc(dev, crtc, encoder)
  3702. encoder->enable(encoder);
  3703. if (HAS_PCH_CPT(dev))
  3704. cpt_verify_modeset(dev, intel_crtc->pipe);
  3705. assert_vblank_disabled(crtc);
  3706. drm_crtc_vblank_on(crtc);
  3707. intel_crtc_enable_planes(crtc);
  3708. }
  3709. /* IPS only exists on ULT machines and is tied to pipe A. */
  3710. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  3711. {
  3712. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  3713. }
  3714. /*
  3715. * This implements the workaround described in the "notes" section of the mode
  3716. * set sequence documentation. When going from no pipes or single pipe to
  3717. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  3718. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  3719. */
  3720. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  3721. {
  3722. struct drm_device *dev = crtc->base.dev;
  3723. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  3724. /* We want to get the other_active_crtc only if there's only 1 other
  3725. * active crtc. */
  3726. for_each_intel_crtc(dev, crtc_it) {
  3727. if (!crtc_it->active || crtc_it == crtc)
  3728. continue;
  3729. if (other_active_crtc)
  3730. return;
  3731. other_active_crtc = crtc_it;
  3732. }
  3733. if (!other_active_crtc)
  3734. return;
  3735. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3736. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3737. }
  3738. static void haswell_crtc_enable(struct drm_crtc *crtc)
  3739. {
  3740. struct drm_device *dev = crtc->dev;
  3741. struct drm_i915_private *dev_priv = dev->dev_private;
  3742. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3743. struct intel_encoder *encoder;
  3744. int pipe = intel_crtc->pipe;
  3745. WARN_ON(!crtc->enabled);
  3746. if (intel_crtc->active)
  3747. return;
  3748. if (intel_crtc_to_shared_dpll(intel_crtc))
  3749. intel_enable_shared_dpll(intel_crtc);
  3750. if (intel_crtc->config.has_dp_encoder)
  3751. intel_dp_set_m_n(intel_crtc);
  3752. intel_set_pipe_timings(intel_crtc);
  3753. if (intel_crtc->config.cpu_transcoder != TRANSCODER_EDP) {
  3754. I915_WRITE(PIPE_MULT(intel_crtc->config.cpu_transcoder),
  3755. intel_crtc->config.pixel_multiplier - 1);
  3756. }
  3757. if (intel_crtc->config.has_pch_encoder) {
  3758. intel_cpu_transcoder_set_m_n(intel_crtc,
  3759. &intel_crtc->config.fdi_m_n, NULL);
  3760. }
  3761. haswell_set_pipeconf(crtc);
  3762. intel_set_pipe_csc(crtc);
  3763. intel_crtc->active = true;
  3764. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3765. for_each_encoder_on_crtc(dev, crtc, encoder)
  3766. if (encoder->pre_enable)
  3767. encoder->pre_enable(encoder);
  3768. if (intel_crtc->config.has_pch_encoder) {
  3769. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  3770. true);
  3771. dev_priv->display.fdi_link_train(crtc);
  3772. }
  3773. intel_ddi_enable_pipe_clock(intel_crtc);
  3774. if (IS_SKYLAKE(dev))
  3775. skylake_pfit_enable(intel_crtc);
  3776. else
  3777. ironlake_pfit_enable(intel_crtc);
  3778. /*
  3779. * On ILK+ LUT must be loaded before the pipe is running but with
  3780. * clocks enabled
  3781. */
  3782. intel_crtc_load_lut(crtc);
  3783. intel_ddi_set_pipe_settings(crtc);
  3784. intel_ddi_enable_transcoder_func(crtc);
  3785. intel_update_watermarks(crtc);
  3786. intel_enable_pipe(intel_crtc);
  3787. if (intel_crtc->config.has_pch_encoder)
  3788. lpt_pch_enable(crtc);
  3789. if (intel_crtc->config.dp_encoder_is_mst)
  3790. intel_ddi_set_vc_payload_alloc(crtc, true);
  3791. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3792. encoder->enable(encoder);
  3793. intel_opregion_notify_encoder(encoder, true);
  3794. }
  3795. assert_vblank_disabled(crtc);
  3796. drm_crtc_vblank_on(crtc);
  3797. /* If we change the relative order between pipe/planes enabling, we need
  3798. * to change the workaround. */
  3799. haswell_mode_set_planes_workaround(intel_crtc);
  3800. intel_crtc_enable_planes(crtc);
  3801. }
  3802. static void skylake_pfit_disable(struct intel_crtc *crtc)
  3803. {
  3804. struct drm_device *dev = crtc->base.dev;
  3805. struct drm_i915_private *dev_priv = dev->dev_private;
  3806. int pipe = crtc->pipe;
  3807. /* To avoid upsetting the power well on haswell only disable the pfit if
  3808. * it's in use. The hw state code will make sure we get this right. */
  3809. if (crtc->config.pch_pfit.enabled) {
  3810. I915_WRITE(PS_CTL(pipe), 0);
  3811. I915_WRITE(PS_WIN_POS(pipe), 0);
  3812. I915_WRITE(PS_WIN_SZ(pipe), 0);
  3813. }
  3814. }
  3815. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  3816. {
  3817. struct drm_device *dev = crtc->base.dev;
  3818. struct drm_i915_private *dev_priv = dev->dev_private;
  3819. int pipe = crtc->pipe;
  3820. /* To avoid upsetting the power well on haswell only disable the pfit if
  3821. * it's in use. The hw state code will make sure we get this right. */
  3822. if (crtc->config.pch_pfit.enabled) {
  3823. I915_WRITE(PF_CTL(pipe), 0);
  3824. I915_WRITE(PF_WIN_POS(pipe), 0);
  3825. I915_WRITE(PF_WIN_SZ(pipe), 0);
  3826. }
  3827. }
  3828. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  3829. {
  3830. struct drm_device *dev = crtc->dev;
  3831. struct drm_i915_private *dev_priv = dev->dev_private;
  3832. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3833. struct intel_encoder *encoder;
  3834. int pipe = intel_crtc->pipe;
  3835. u32 reg, temp;
  3836. if (!intel_crtc->active)
  3837. return;
  3838. intel_crtc_disable_planes(crtc);
  3839. drm_crtc_vblank_off(crtc);
  3840. assert_vblank_disabled(crtc);
  3841. for_each_encoder_on_crtc(dev, crtc, encoder)
  3842. encoder->disable(encoder);
  3843. if (intel_crtc->config.has_pch_encoder)
  3844. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  3845. intel_disable_pipe(intel_crtc);
  3846. ironlake_pfit_disable(intel_crtc);
  3847. for_each_encoder_on_crtc(dev, crtc, encoder)
  3848. if (encoder->post_disable)
  3849. encoder->post_disable(encoder);
  3850. if (intel_crtc->config.has_pch_encoder) {
  3851. ironlake_fdi_disable(crtc);
  3852. ironlake_disable_pch_transcoder(dev_priv, pipe);
  3853. if (HAS_PCH_CPT(dev)) {
  3854. /* disable TRANS_DP_CTL */
  3855. reg = TRANS_DP_CTL(pipe);
  3856. temp = I915_READ(reg);
  3857. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  3858. TRANS_DP_PORT_SEL_MASK);
  3859. temp |= TRANS_DP_PORT_SEL_NONE;
  3860. I915_WRITE(reg, temp);
  3861. /* disable DPLL_SEL */
  3862. temp = I915_READ(PCH_DPLL_SEL);
  3863. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  3864. I915_WRITE(PCH_DPLL_SEL, temp);
  3865. }
  3866. /* disable PCH DPLL */
  3867. intel_disable_shared_dpll(intel_crtc);
  3868. ironlake_fdi_pll_disable(intel_crtc);
  3869. }
  3870. intel_crtc->active = false;
  3871. intel_update_watermarks(crtc);
  3872. mutex_lock(&dev->struct_mutex);
  3873. intel_update_fbc(dev);
  3874. mutex_unlock(&dev->struct_mutex);
  3875. }
  3876. static void haswell_crtc_disable(struct drm_crtc *crtc)
  3877. {
  3878. struct drm_device *dev = crtc->dev;
  3879. struct drm_i915_private *dev_priv = dev->dev_private;
  3880. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3881. struct intel_encoder *encoder;
  3882. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3883. if (!intel_crtc->active)
  3884. return;
  3885. intel_crtc_disable_planes(crtc);
  3886. drm_crtc_vblank_off(crtc);
  3887. assert_vblank_disabled(crtc);
  3888. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3889. intel_opregion_notify_encoder(encoder, false);
  3890. encoder->disable(encoder);
  3891. }
  3892. if (intel_crtc->config.has_pch_encoder)
  3893. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  3894. false);
  3895. intel_disable_pipe(intel_crtc);
  3896. if (intel_crtc->config.dp_encoder_is_mst)
  3897. intel_ddi_set_vc_payload_alloc(crtc, false);
  3898. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  3899. if (IS_SKYLAKE(dev))
  3900. skylake_pfit_disable(intel_crtc);
  3901. else
  3902. ironlake_pfit_disable(intel_crtc);
  3903. intel_ddi_disable_pipe_clock(intel_crtc);
  3904. if (intel_crtc->config.has_pch_encoder) {
  3905. lpt_disable_pch_transcoder(dev_priv);
  3906. intel_ddi_fdi_disable(crtc);
  3907. }
  3908. for_each_encoder_on_crtc(dev, crtc, encoder)
  3909. if (encoder->post_disable)
  3910. encoder->post_disable(encoder);
  3911. intel_crtc->active = false;
  3912. intel_update_watermarks(crtc);
  3913. mutex_lock(&dev->struct_mutex);
  3914. intel_update_fbc(dev);
  3915. mutex_unlock(&dev->struct_mutex);
  3916. if (intel_crtc_to_shared_dpll(intel_crtc))
  3917. intel_disable_shared_dpll(intel_crtc);
  3918. }
  3919. static void ironlake_crtc_off(struct drm_crtc *crtc)
  3920. {
  3921. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3922. intel_put_shared_dpll(intel_crtc);
  3923. }
  3924. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  3925. {
  3926. struct drm_device *dev = crtc->base.dev;
  3927. struct drm_i915_private *dev_priv = dev->dev_private;
  3928. struct intel_crtc_config *pipe_config = &crtc->config;
  3929. if (!crtc->config.gmch_pfit.control)
  3930. return;
  3931. /*
  3932. * The panel fitter should only be adjusted whilst the pipe is disabled,
  3933. * according to register description and PRM.
  3934. */
  3935. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  3936. assert_pipe_disabled(dev_priv, crtc->pipe);
  3937. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  3938. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  3939. /* Border color in case we don't scale up to the full screen. Black by
  3940. * default, change to something else for debugging. */
  3941. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  3942. }
  3943. static enum intel_display_power_domain port_to_power_domain(enum port port)
  3944. {
  3945. switch (port) {
  3946. case PORT_A:
  3947. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  3948. case PORT_B:
  3949. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  3950. case PORT_C:
  3951. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  3952. case PORT_D:
  3953. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  3954. default:
  3955. WARN_ON_ONCE(1);
  3956. return POWER_DOMAIN_PORT_OTHER;
  3957. }
  3958. }
  3959. #define for_each_power_domain(domain, mask) \
  3960. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  3961. if ((1 << (domain)) & (mask))
  3962. enum intel_display_power_domain
  3963. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  3964. {
  3965. struct drm_device *dev = intel_encoder->base.dev;
  3966. struct intel_digital_port *intel_dig_port;
  3967. switch (intel_encoder->type) {
  3968. case INTEL_OUTPUT_UNKNOWN:
  3969. /* Only DDI platforms should ever use this output type */
  3970. WARN_ON_ONCE(!HAS_DDI(dev));
  3971. case INTEL_OUTPUT_DISPLAYPORT:
  3972. case INTEL_OUTPUT_HDMI:
  3973. case INTEL_OUTPUT_EDP:
  3974. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  3975. return port_to_power_domain(intel_dig_port->port);
  3976. case INTEL_OUTPUT_DP_MST:
  3977. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  3978. return port_to_power_domain(intel_dig_port->port);
  3979. case INTEL_OUTPUT_ANALOG:
  3980. return POWER_DOMAIN_PORT_CRT;
  3981. case INTEL_OUTPUT_DSI:
  3982. return POWER_DOMAIN_PORT_DSI;
  3983. default:
  3984. return POWER_DOMAIN_PORT_OTHER;
  3985. }
  3986. }
  3987. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  3988. {
  3989. struct drm_device *dev = crtc->dev;
  3990. struct intel_encoder *intel_encoder;
  3991. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3992. enum pipe pipe = intel_crtc->pipe;
  3993. unsigned long mask;
  3994. enum transcoder transcoder;
  3995. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  3996. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  3997. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  3998. if (intel_crtc->config.pch_pfit.enabled ||
  3999. intel_crtc->config.pch_pfit.force_thru)
  4000. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4001. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4002. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4003. return mask;
  4004. }
  4005. static void modeset_update_crtc_power_domains(struct drm_device *dev)
  4006. {
  4007. struct drm_i915_private *dev_priv = dev->dev_private;
  4008. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  4009. struct intel_crtc *crtc;
  4010. /*
  4011. * First get all needed power domains, then put all unneeded, to avoid
  4012. * any unnecessary toggling of the power wells.
  4013. */
  4014. for_each_intel_crtc(dev, crtc) {
  4015. enum intel_display_power_domain domain;
  4016. if (!crtc->base.enabled)
  4017. continue;
  4018. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  4019. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  4020. intel_display_power_get(dev_priv, domain);
  4021. }
  4022. if (dev_priv->display.modeset_global_resources)
  4023. dev_priv->display.modeset_global_resources(dev);
  4024. for_each_intel_crtc(dev, crtc) {
  4025. enum intel_display_power_domain domain;
  4026. for_each_power_domain(domain, crtc->enabled_power_domains)
  4027. intel_display_power_put(dev_priv, domain);
  4028. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  4029. }
  4030. intel_display_set_init_power(dev_priv, false);
  4031. }
  4032. /* returns HPLL frequency in kHz */
  4033. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  4034. {
  4035. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  4036. /* Obtain SKU information */
  4037. mutex_lock(&dev_priv->dpio_lock);
  4038. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  4039. CCK_FUSE_HPLL_FREQ_MASK;
  4040. mutex_unlock(&dev_priv->dpio_lock);
  4041. return vco_freq[hpll_freq] * 1000;
  4042. }
  4043. static void vlv_update_cdclk(struct drm_device *dev)
  4044. {
  4045. struct drm_i915_private *dev_priv = dev->dev_private;
  4046. dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4047. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4048. dev_priv->vlv_cdclk_freq);
  4049. /*
  4050. * Program the gmbus_freq based on the cdclk frequency.
  4051. * BSpec erroneously claims we should aim for 4MHz, but
  4052. * in fact 1MHz is the correct frequency.
  4053. */
  4054. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->vlv_cdclk_freq, 1000));
  4055. }
  4056. /* Adjust CDclk dividers to allow high res or save power if possible */
  4057. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4058. {
  4059. struct drm_i915_private *dev_priv = dev->dev_private;
  4060. u32 val, cmd;
  4061. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  4062. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4063. cmd = 2;
  4064. else if (cdclk == 266667)
  4065. cmd = 1;
  4066. else
  4067. cmd = 0;
  4068. mutex_lock(&dev_priv->rps.hw_lock);
  4069. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4070. val &= ~DSPFREQGUAR_MASK;
  4071. val |= (cmd << DSPFREQGUAR_SHIFT);
  4072. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4073. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4074. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4075. 50)) {
  4076. DRM_ERROR("timed out waiting for CDclk change\n");
  4077. }
  4078. mutex_unlock(&dev_priv->rps.hw_lock);
  4079. if (cdclk == 400000) {
  4080. u32 divider;
  4081. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4082. mutex_lock(&dev_priv->dpio_lock);
  4083. /* adjust cdclk divider */
  4084. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4085. val &= ~DISPLAY_FREQUENCY_VALUES;
  4086. val |= divider;
  4087. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4088. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4089. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4090. 50))
  4091. DRM_ERROR("timed out waiting for CDclk change\n");
  4092. mutex_unlock(&dev_priv->dpio_lock);
  4093. }
  4094. mutex_lock(&dev_priv->dpio_lock);
  4095. /* adjust self-refresh exit latency value */
  4096. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4097. val &= ~0x7f;
  4098. /*
  4099. * For high bandwidth configs, we set a higher latency in the bunit
  4100. * so that the core display fetch happens in time to avoid underruns.
  4101. */
  4102. if (cdclk == 400000)
  4103. val |= 4500 / 250; /* 4.5 usec */
  4104. else
  4105. val |= 3000 / 250; /* 3.0 usec */
  4106. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4107. mutex_unlock(&dev_priv->dpio_lock);
  4108. vlv_update_cdclk(dev);
  4109. }
  4110. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4111. {
  4112. struct drm_i915_private *dev_priv = dev->dev_private;
  4113. u32 val, cmd;
  4114. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  4115. switch (cdclk) {
  4116. case 400000:
  4117. cmd = 3;
  4118. break;
  4119. case 333333:
  4120. case 320000:
  4121. cmd = 2;
  4122. break;
  4123. case 266667:
  4124. cmd = 1;
  4125. break;
  4126. case 200000:
  4127. cmd = 0;
  4128. break;
  4129. default:
  4130. WARN_ON(1);
  4131. return;
  4132. }
  4133. mutex_lock(&dev_priv->rps.hw_lock);
  4134. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4135. val &= ~DSPFREQGUAR_MASK_CHV;
  4136. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4137. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4138. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4139. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4140. 50)) {
  4141. DRM_ERROR("timed out waiting for CDclk change\n");
  4142. }
  4143. mutex_unlock(&dev_priv->rps.hw_lock);
  4144. vlv_update_cdclk(dev);
  4145. }
  4146. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4147. int max_pixclk)
  4148. {
  4149. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4150. /* FIXME: Punit isn't quite ready yet */
  4151. if (IS_CHERRYVIEW(dev_priv->dev))
  4152. return 400000;
  4153. /*
  4154. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4155. * 200MHz
  4156. * 267MHz
  4157. * 320/333MHz (depends on HPLL freq)
  4158. * 400MHz
  4159. * So we check to see whether we're above 90% of the lower bin and
  4160. * adjust if needed.
  4161. *
  4162. * We seem to get an unstable or solid color picture at 200MHz.
  4163. * Not sure what's wrong. For now use 200MHz only when all pipes
  4164. * are off.
  4165. */
  4166. if (max_pixclk > freq_320*9/10)
  4167. return 400000;
  4168. else if (max_pixclk > 266667*9/10)
  4169. return freq_320;
  4170. else if (max_pixclk > 0)
  4171. return 266667;
  4172. else
  4173. return 200000;
  4174. }
  4175. /* compute the max pixel clock for new configuration */
  4176. static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
  4177. {
  4178. struct drm_device *dev = dev_priv->dev;
  4179. struct intel_crtc *intel_crtc;
  4180. int max_pixclk = 0;
  4181. for_each_intel_crtc(dev, intel_crtc) {
  4182. if (intel_crtc->new_enabled)
  4183. max_pixclk = max(max_pixclk,
  4184. intel_crtc->new_config->adjusted_mode.crtc_clock);
  4185. }
  4186. return max_pixclk;
  4187. }
  4188. static void valleyview_modeset_global_pipes(struct drm_device *dev,
  4189. unsigned *prepare_pipes)
  4190. {
  4191. struct drm_i915_private *dev_priv = dev->dev_private;
  4192. struct intel_crtc *intel_crtc;
  4193. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4194. if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
  4195. dev_priv->vlv_cdclk_freq)
  4196. return;
  4197. /* disable/enable all currently active pipes while we change cdclk */
  4198. for_each_intel_crtc(dev, intel_crtc)
  4199. if (intel_crtc->base.enabled)
  4200. *prepare_pipes |= (1 << intel_crtc->pipe);
  4201. }
  4202. static void valleyview_modeset_global_resources(struct drm_device *dev)
  4203. {
  4204. struct drm_i915_private *dev_priv = dev->dev_private;
  4205. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4206. int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  4207. if (req_cdclk != dev_priv->vlv_cdclk_freq) {
  4208. /*
  4209. * FIXME: We can end up here with all power domains off, yet
  4210. * with a CDCLK frequency other than the minimum. To account
  4211. * for this take the PIPE-A power domain, which covers the HW
  4212. * blocks needed for the following programming. This can be
  4213. * removed once it's guaranteed that we get here either with
  4214. * the minimum CDCLK set, or the required power domains
  4215. * enabled.
  4216. */
  4217. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  4218. if (IS_CHERRYVIEW(dev))
  4219. cherryview_set_cdclk(dev, req_cdclk);
  4220. else
  4221. valleyview_set_cdclk(dev, req_cdclk);
  4222. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  4223. }
  4224. }
  4225. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  4226. {
  4227. struct drm_device *dev = crtc->dev;
  4228. struct drm_i915_private *dev_priv = to_i915(dev);
  4229. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4230. struct intel_encoder *encoder;
  4231. int pipe = intel_crtc->pipe;
  4232. bool is_dsi;
  4233. WARN_ON(!crtc->enabled);
  4234. if (intel_crtc->active)
  4235. return;
  4236. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  4237. if (!is_dsi) {
  4238. if (IS_CHERRYVIEW(dev))
  4239. chv_prepare_pll(intel_crtc, &intel_crtc->config);
  4240. else
  4241. vlv_prepare_pll(intel_crtc, &intel_crtc->config);
  4242. }
  4243. if (intel_crtc->config.has_dp_encoder)
  4244. intel_dp_set_m_n(intel_crtc);
  4245. intel_set_pipe_timings(intel_crtc);
  4246. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  4247. struct drm_i915_private *dev_priv = dev->dev_private;
  4248. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  4249. I915_WRITE(CHV_CANVAS(pipe), 0);
  4250. }
  4251. i9xx_set_pipeconf(intel_crtc);
  4252. intel_crtc->active = true;
  4253. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4254. for_each_encoder_on_crtc(dev, crtc, encoder)
  4255. if (encoder->pre_pll_enable)
  4256. encoder->pre_pll_enable(encoder);
  4257. if (!is_dsi) {
  4258. if (IS_CHERRYVIEW(dev))
  4259. chv_enable_pll(intel_crtc, &intel_crtc->config);
  4260. else
  4261. vlv_enable_pll(intel_crtc, &intel_crtc->config);
  4262. }
  4263. for_each_encoder_on_crtc(dev, crtc, encoder)
  4264. if (encoder->pre_enable)
  4265. encoder->pre_enable(encoder);
  4266. i9xx_pfit_enable(intel_crtc);
  4267. intel_crtc_load_lut(crtc);
  4268. intel_update_watermarks(crtc);
  4269. intel_enable_pipe(intel_crtc);
  4270. for_each_encoder_on_crtc(dev, crtc, encoder)
  4271. encoder->enable(encoder);
  4272. assert_vblank_disabled(crtc);
  4273. drm_crtc_vblank_on(crtc);
  4274. intel_crtc_enable_planes(crtc);
  4275. /* Underruns don't raise interrupts, so check manually. */
  4276. i9xx_check_fifo_underruns(dev_priv);
  4277. }
  4278. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  4279. {
  4280. struct drm_device *dev = crtc->base.dev;
  4281. struct drm_i915_private *dev_priv = dev->dev_private;
  4282. I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
  4283. I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
  4284. }
  4285. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  4286. {
  4287. struct drm_device *dev = crtc->dev;
  4288. struct drm_i915_private *dev_priv = to_i915(dev);
  4289. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4290. struct intel_encoder *encoder;
  4291. int pipe = intel_crtc->pipe;
  4292. WARN_ON(!crtc->enabled);
  4293. if (intel_crtc->active)
  4294. return;
  4295. i9xx_set_pll_dividers(intel_crtc);
  4296. if (intel_crtc->config.has_dp_encoder)
  4297. intel_dp_set_m_n(intel_crtc);
  4298. intel_set_pipe_timings(intel_crtc);
  4299. i9xx_set_pipeconf(intel_crtc);
  4300. intel_crtc->active = true;
  4301. if (!IS_GEN2(dev))
  4302. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4303. for_each_encoder_on_crtc(dev, crtc, encoder)
  4304. if (encoder->pre_enable)
  4305. encoder->pre_enable(encoder);
  4306. i9xx_enable_pll(intel_crtc);
  4307. i9xx_pfit_enable(intel_crtc);
  4308. intel_crtc_load_lut(crtc);
  4309. intel_update_watermarks(crtc);
  4310. intel_enable_pipe(intel_crtc);
  4311. for_each_encoder_on_crtc(dev, crtc, encoder)
  4312. encoder->enable(encoder);
  4313. assert_vblank_disabled(crtc);
  4314. drm_crtc_vblank_on(crtc);
  4315. intel_crtc_enable_planes(crtc);
  4316. /*
  4317. * Gen2 reports pipe underruns whenever all planes are disabled.
  4318. * So don't enable underrun reporting before at least some planes
  4319. * are enabled.
  4320. * FIXME: Need to fix the logic to work when we turn off all planes
  4321. * but leave the pipe running.
  4322. */
  4323. if (IS_GEN2(dev))
  4324. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4325. /* Underruns don't raise interrupts, so check manually. */
  4326. i9xx_check_fifo_underruns(dev_priv);
  4327. }
  4328. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4329. {
  4330. struct drm_device *dev = crtc->base.dev;
  4331. struct drm_i915_private *dev_priv = dev->dev_private;
  4332. if (!crtc->config.gmch_pfit.control)
  4333. return;
  4334. assert_pipe_disabled(dev_priv, crtc->pipe);
  4335. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4336. I915_READ(PFIT_CONTROL));
  4337. I915_WRITE(PFIT_CONTROL, 0);
  4338. }
  4339. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  4340. {
  4341. struct drm_device *dev = crtc->dev;
  4342. struct drm_i915_private *dev_priv = dev->dev_private;
  4343. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4344. struct intel_encoder *encoder;
  4345. int pipe = intel_crtc->pipe;
  4346. if (!intel_crtc->active)
  4347. return;
  4348. /*
  4349. * Gen2 reports pipe underruns whenever all planes are disabled.
  4350. * So diasble underrun reporting before all the planes get disabled.
  4351. * FIXME: Need to fix the logic to work when we turn off all planes
  4352. * but leave the pipe running.
  4353. */
  4354. if (IS_GEN2(dev))
  4355. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4356. /*
  4357. * Vblank time updates from the shadow to live plane control register
  4358. * are blocked if the memory self-refresh mode is active at that
  4359. * moment. So to make sure the plane gets truly disabled, disable
  4360. * first the self-refresh mode. The self-refresh enable bit in turn
  4361. * will be checked/applied by the HW only at the next frame start
  4362. * event which is after the vblank start event, so we need to have a
  4363. * wait-for-vblank between disabling the plane and the pipe.
  4364. */
  4365. intel_set_memory_cxsr(dev_priv, false);
  4366. intel_crtc_disable_planes(crtc);
  4367. /*
  4368. * On gen2 planes are double buffered but the pipe isn't, so we must
  4369. * wait for planes to fully turn off before disabling the pipe.
  4370. * We also need to wait on all gmch platforms because of the
  4371. * self-refresh mode constraint explained above.
  4372. */
  4373. intel_wait_for_vblank(dev, pipe);
  4374. drm_crtc_vblank_off(crtc);
  4375. assert_vblank_disabled(crtc);
  4376. for_each_encoder_on_crtc(dev, crtc, encoder)
  4377. encoder->disable(encoder);
  4378. intel_disable_pipe(intel_crtc);
  4379. i9xx_pfit_disable(intel_crtc);
  4380. for_each_encoder_on_crtc(dev, crtc, encoder)
  4381. if (encoder->post_disable)
  4382. encoder->post_disable(encoder);
  4383. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  4384. if (IS_CHERRYVIEW(dev))
  4385. chv_disable_pll(dev_priv, pipe);
  4386. else if (IS_VALLEYVIEW(dev))
  4387. vlv_disable_pll(dev_priv, pipe);
  4388. else
  4389. i9xx_disable_pll(intel_crtc);
  4390. }
  4391. if (!IS_GEN2(dev))
  4392. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4393. intel_crtc->active = false;
  4394. intel_update_watermarks(crtc);
  4395. mutex_lock(&dev->struct_mutex);
  4396. intel_update_fbc(dev);
  4397. mutex_unlock(&dev->struct_mutex);
  4398. }
  4399. static void i9xx_crtc_off(struct drm_crtc *crtc)
  4400. {
  4401. }
  4402. /* Master function to enable/disable CRTC and corresponding power wells */
  4403. void intel_crtc_control(struct drm_crtc *crtc, bool enable)
  4404. {
  4405. struct drm_device *dev = crtc->dev;
  4406. struct drm_i915_private *dev_priv = dev->dev_private;
  4407. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4408. enum intel_display_power_domain domain;
  4409. unsigned long domains;
  4410. if (enable) {
  4411. if (!intel_crtc->active) {
  4412. domains = get_crtc_power_domains(crtc);
  4413. for_each_power_domain(domain, domains)
  4414. intel_display_power_get(dev_priv, domain);
  4415. intel_crtc->enabled_power_domains = domains;
  4416. dev_priv->display.crtc_enable(crtc);
  4417. }
  4418. } else {
  4419. if (intel_crtc->active) {
  4420. dev_priv->display.crtc_disable(crtc);
  4421. domains = intel_crtc->enabled_power_domains;
  4422. for_each_power_domain(domain, domains)
  4423. intel_display_power_put(dev_priv, domain);
  4424. intel_crtc->enabled_power_domains = 0;
  4425. }
  4426. }
  4427. }
  4428. /**
  4429. * Sets the power management mode of the pipe and plane.
  4430. */
  4431. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  4432. {
  4433. struct drm_device *dev = crtc->dev;
  4434. struct intel_encoder *intel_encoder;
  4435. bool enable = false;
  4436. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4437. enable |= intel_encoder->connectors_active;
  4438. intel_crtc_control(crtc, enable);
  4439. }
  4440. static void intel_crtc_disable(struct drm_crtc *crtc)
  4441. {
  4442. struct drm_device *dev = crtc->dev;
  4443. struct drm_connector *connector;
  4444. struct drm_i915_private *dev_priv = dev->dev_private;
  4445. struct drm_i915_gem_object *old_obj = intel_fb_obj(crtc->primary->fb);
  4446. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  4447. /* crtc should still be enabled when we disable it. */
  4448. WARN_ON(!crtc->enabled);
  4449. dev_priv->display.crtc_disable(crtc);
  4450. dev_priv->display.off(crtc);
  4451. if (crtc->primary->fb) {
  4452. mutex_lock(&dev->struct_mutex);
  4453. intel_unpin_fb_obj(old_obj);
  4454. i915_gem_track_fb(old_obj, NULL,
  4455. INTEL_FRONTBUFFER_PRIMARY(pipe));
  4456. mutex_unlock(&dev->struct_mutex);
  4457. crtc->primary->fb = NULL;
  4458. }
  4459. /* Update computed state. */
  4460. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  4461. if (!connector->encoder || !connector->encoder->crtc)
  4462. continue;
  4463. if (connector->encoder->crtc != crtc)
  4464. continue;
  4465. connector->dpms = DRM_MODE_DPMS_OFF;
  4466. to_intel_encoder(connector->encoder)->connectors_active = false;
  4467. }
  4468. }
  4469. void intel_encoder_destroy(struct drm_encoder *encoder)
  4470. {
  4471. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4472. drm_encoder_cleanup(encoder);
  4473. kfree(intel_encoder);
  4474. }
  4475. /* Simple dpms helper for encoders with just one connector, no cloning and only
  4476. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  4477. * state of the entire output pipe. */
  4478. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  4479. {
  4480. if (mode == DRM_MODE_DPMS_ON) {
  4481. encoder->connectors_active = true;
  4482. intel_crtc_update_dpms(encoder->base.crtc);
  4483. } else {
  4484. encoder->connectors_active = false;
  4485. intel_crtc_update_dpms(encoder->base.crtc);
  4486. }
  4487. }
  4488. /* Cross check the actual hw state with our own modeset state tracking (and it's
  4489. * internal consistency). */
  4490. static void intel_connector_check_state(struct intel_connector *connector)
  4491. {
  4492. if (connector->get_hw_state(connector)) {
  4493. struct intel_encoder *encoder = connector->encoder;
  4494. struct drm_crtc *crtc;
  4495. bool encoder_enabled;
  4496. enum pipe pipe;
  4497. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4498. connector->base.base.id,
  4499. connector->base.name);
  4500. /* there is no real hw state for MST connectors */
  4501. if (connector->mst_port)
  4502. return;
  4503. WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  4504. "wrong connector dpms state\n");
  4505. WARN(connector->base.encoder != &encoder->base,
  4506. "active connector not linked to encoder\n");
  4507. if (encoder) {
  4508. WARN(!encoder->connectors_active,
  4509. "encoder->connectors_active not set\n");
  4510. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  4511. WARN(!encoder_enabled, "encoder not enabled\n");
  4512. if (WARN_ON(!encoder->base.crtc))
  4513. return;
  4514. crtc = encoder->base.crtc;
  4515. WARN(!crtc->enabled, "crtc not enabled\n");
  4516. WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  4517. WARN(pipe != to_intel_crtc(crtc)->pipe,
  4518. "encoder active on the wrong pipe\n");
  4519. }
  4520. }
  4521. }
  4522. /* Even simpler default implementation, if there's really no special case to
  4523. * consider. */
  4524. void intel_connector_dpms(struct drm_connector *connector, int mode)
  4525. {
  4526. /* All the simple cases only support two dpms states. */
  4527. if (mode != DRM_MODE_DPMS_ON)
  4528. mode = DRM_MODE_DPMS_OFF;
  4529. if (mode == connector->dpms)
  4530. return;
  4531. connector->dpms = mode;
  4532. /* Only need to change hw state when actually enabled */
  4533. if (connector->encoder)
  4534. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  4535. intel_modeset_check_state(connector->dev);
  4536. }
  4537. /* Simple connector->get_hw_state implementation for encoders that support only
  4538. * one connector and no cloning and hence the encoder state determines the state
  4539. * of the connector. */
  4540. bool intel_connector_get_hw_state(struct intel_connector *connector)
  4541. {
  4542. enum pipe pipe = 0;
  4543. struct intel_encoder *encoder = connector->encoder;
  4544. return encoder->get_hw_state(encoder, &pipe);
  4545. }
  4546. static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  4547. struct intel_crtc_config *pipe_config)
  4548. {
  4549. struct drm_i915_private *dev_priv = dev->dev_private;
  4550. struct intel_crtc *pipe_B_crtc =
  4551. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  4552. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  4553. pipe_name(pipe), pipe_config->fdi_lanes);
  4554. if (pipe_config->fdi_lanes > 4) {
  4555. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  4556. pipe_name(pipe), pipe_config->fdi_lanes);
  4557. return false;
  4558. }
  4559. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  4560. if (pipe_config->fdi_lanes > 2) {
  4561. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  4562. pipe_config->fdi_lanes);
  4563. return false;
  4564. } else {
  4565. return true;
  4566. }
  4567. }
  4568. if (INTEL_INFO(dev)->num_pipes == 2)
  4569. return true;
  4570. /* Ivybridge 3 pipe is really complicated */
  4571. switch (pipe) {
  4572. case PIPE_A:
  4573. return true;
  4574. case PIPE_B:
  4575. if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
  4576. pipe_config->fdi_lanes > 2) {
  4577. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4578. pipe_name(pipe), pipe_config->fdi_lanes);
  4579. return false;
  4580. }
  4581. return true;
  4582. case PIPE_C:
  4583. if (!pipe_has_enabled_pch(pipe_B_crtc) ||
  4584. pipe_B_crtc->config.fdi_lanes <= 2) {
  4585. if (pipe_config->fdi_lanes > 2) {
  4586. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4587. pipe_name(pipe), pipe_config->fdi_lanes);
  4588. return false;
  4589. }
  4590. } else {
  4591. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  4592. return false;
  4593. }
  4594. return true;
  4595. default:
  4596. BUG();
  4597. }
  4598. }
  4599. #define RETRY 1
  4600. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  4601. struct intel_crtc_config *pipe_config)
  4602. {
  4603. struct drm_device *dev = intel_crtc->base.dev;
  4604. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4605. int lane, link_bw, fdi_dotclock;
  4606. bool setup_ok, needs_recompute = false;
  4607. retry:
  4608. /* FDI is a binary signal running at ~2.7GHz, encoding
  4609. * each output octet as 10 bits. The actual frequency
  4610. * is stored as a divider into a 100MHz clock, and the
  4611. * mode pixel clock is stored in units of 1KHz.
  4612. * Hence the bw of each lane in terms of the mode signal
  4613. * is:
  4614. */
  4615. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4616. fdi_dotclock = adjusted_mode->crtc_clock;
  4617. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  4618. pipe_config->pipe_bpp);
  4619. pipe_config->fdi_lanes = lane;
  4620. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  4621. link_bw, &pipe_config->fdi_m_n);
  4622. setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  4623. intel_crtc->pipe, pipe_config);
  4624. if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
  4625. pipe_config->pipe_bpp -= 2*3;
  4626. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  4627. pipe_config->pipe_bpp);
  4628. needs_recompute = true;
  4629. pipe_config->bw_constrained = true;
  4630. goto retry;
  4631. }
  4632. if (needs_recompute)
  4633. return RETRY;
  4634. return setup_ok ? 0 : -EINVAL;
  4635. }
  4636. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  4637. struct intel_crtc_config *pipe_config)
  4638. {
  4639. pipe_config->ips_enabled = i915.enable_ips &&
  4640. hsw_crtc_supports_ips(crtc) &&
  4641. pipe_config->pipe_bpp <= 24;
  4642. }
  4643. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  4644. struct intel_crtc_config *pipe_config)
  4645. {
  4646. struct drm_device *dev = crtc->base.dev;
  4647. struct drm_i915_private *dev_priv = dev->dev_private;
  4648. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4649. /* FIXME should check pixel clock limits on all platforms */
  4650. if (INTEL_INFO(dev)->gen < 4) {
  4651. int clock_limit =
  4652. dev_priv->display.get_display_clock_speed(dev);
  4653. /*
  4654. * Enable pixel doubling when the dot clock
  4655. * is > 90% of the (display) core speed.
  4656. *
  4657. * GDG double wide on either pipe,
  4658. * otherwise pipe A only.
  4659. */
  4660. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  4661. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  4662. clock_limit *= 2;
  4663. pipe_config->double_wide = true;
  4664. }
  4665. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  4666. return -EINVAL;
  4667. }
  4668. /*
  4669. * Pipe horizontal size must be even in:
  4670. * - DVO ganged mode
  4671. * - LVDS dual channel mode
  4672. * - Double wide pipe
  4673. */
  4674. if ((intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  4675. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  4676. pipe_config->pipe_src_w &= ~1;
  4677. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  4678. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  4679. */
  4680. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  4681. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  4682. return -EINVAL;
  4683. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
  4684. pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
  4685. } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
  4686. /* only a 8bpc pipe, with 6bpc dither through the panel fitter
  4687. * for lvds. */
  4688. pipe_config->pipe_bpp = 8*3;
  4689. }
  4690. if (HAS_IPS(dev))
  4691. hsw_compute_ips_config(crtc, pipe_config);
  4692. if (pipe_config->has_pch_encoder)
  4693. return ironlake_fdi_compute_config(crtc, pipe_config);
  4694. return 0;
  4695. }
  4696. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  4697. {
  4698. struct drm_i915_private *dev_priv = dev->dev_private;
  4699. u32 val;
  4700. int divider;
  4701. /* FIXME: Punit isn't quite ready yet */
  4702. if (IS_CHERRYVIEW(dev))
  4703. return 400000;
  4704. if (dev_priv->hpll_freq == 0)
  4705. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  4706. mutex_lock(&dev_priv->dpio_lock);
  4707. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4708. mutex_unlock(&dev_priv->dpio_lock);
  4709. divider = val & DISPLAY_FREQUENCY_VALUES;
  4710. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  4711. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4712. "cdclk change in progress\n");
  4713. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  4714. }
  4715. static int i945_get_display_clock_speed(struct drm_device *dev)
  4716. {
  4717. return 400000;
  4718. }
  4719. static int i915_get_display_clock_speed(struct drm_device *dev)
  4720. {
  4721. return 333000;
  4722. }
  4723. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  4724. {
  4725. return 200000;
  4726. }
  4727. static int pnv_get_display_clock_speed(struct drm_device *dev)
  4728. {
  4729. u16 gcfgc = 0;
  4730. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4731. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4732. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  4733. return 267000;
  4734. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  4735. return 333000;
  4736. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  4737. return 444000;
  4738. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  4739. return 200000;
  4740. default:
  4741. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  4742. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  4743. return 133000;
  4744. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  4745. return 167000;
  4746. }
  4747. }
  4748. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  4749. {
  4750. u16 gcfgc = 0;
  4751. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4752. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  4753. return 133000;
  4754. else {
  4755. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4756. case GC_DISPLAY_CLOCK_333_MHZ:
  4757. return 333000;
  4758. default:
  4759. case GC_DISPLAY_CLOCK_190_200_MHZ:
  4760. return 190000;
  4761. }
  4762. }
  4763. }
  4764. static int i865_get_display_clock_speed(struct drm_device *dev)
  4765. {
  4766. return 266000;
  4767. }
  4768. static int i855_get_display_clock_speed(struct drm_device *dev)
  4769. {
  4770. u16 hpllcc = 0;
  4771. /* Assume that the hardware is in the high speed state. This
  4772. * should be the default.
  4773. */
  4774. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  4775. case GC_CLOCK_133_200:
  4776. case GC_CLOCK_100_200:
  4777. return 200000;
  4778. case GC_CLOCK_166_250:
  4779. return 250000;
  4780. case GC_CLOCK_100_133:
  4781. return 133000;
  4782. }
  4783. /* Shouldn't happen */
  4784. return 0;
  4785. }
  4786. static int i830_get_display_clock_speed(struct drm_device *dev)
  4787. {
  4788. return 133000;
  4789. }
  4790. static void
  4791. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  4792. {
  4793. while (*num > DATA_LINK_M_N_MASK ||
  4794. *den > DATA_LINK_M_N_MASK) {
  4795. *num >>= 1;
  4796. *den >>= 1;
  4797. }
  4798. }
  4799. static void compute_m_n(unsigned int m, unsigned int n,
  4800. uint32_t *ret_m, uint32_t *ret_n)
  4801. {
  4802. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  4803. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  4804. intel_reduce_m_n_ratio(ret_m, ret_n);
  4805. }
  4806. void
  4807. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  4808. int pixel_clock, int link_clock,
  4809. struct intel_link_m_n *m_n)
  4810. {
  4811. m_n->tu = 64;
  4812. compute_m_n(bits_per_pixel * pixel_clock,
  4813. link_clock * nlanes * 8,
  4814. &m_n->gmch_m, &m_n->gmch_n);
  4815. compute_m_n(pixel_clock, link_clock,
  4816. &m_n->link_m, &m_n->link_n);
  4817. }
  4818. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  4819. {
  4820. if (i915.panel_use_ssc >= 0)
  4821. return i915.panel_use_ssc != 0;
  4822. return dev_priv->vbt.lvds_use_ssc
  4823. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  4824. }
  4825. static int i9xx_get_refclk(struct intel_crtc *crtc, int num_connectors)
  4826. {
  4827. struct drm_device *dev = crtc->base.dev;
  4828. struct drm_i915_private *dev_priv = dev->dev_private;
  4829. int refclk;
  4830. if (IS_VALLEYVIEW(dev)) {
  4831. refclk = 100000;
  4832. } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  4833. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4834. refclk = dev_priv->vbt.lvds_ssc_freq;
  4835. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  4836. } else if (!IS_GEN2(dev)) {
  4837. refclk = 96000;
  4838. } else {
  4839. refclk = 48000;
  4840. }
  4841. return refclk;
  4842. }
  4843. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  4844. {
  4845. return (1 << dpll->n) << 16 | dpll->m2;
  4846. }
  4847. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  4848. {
  4849. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  4850. }
  4851. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  4852. intel_clock_t *reduced_clock)
  4853. {
  4854. struct drm_device *dev = crtc->base.dev;
  4855. u32 fp, fp2 = 0;
  4856. if (IS_PINEVIEW(dev)) {
  4857. fp = pnv_dpll_compute_fp(&crtc->new_config->dpll);
  4858. if (reduced_clock)
  4859. fp2 = pnv_dpll_compute_fp(reduced_clock);
  4860. } else {
  4861. fp = i9xx_dpll_compute_fp(&crtc->new_config->dpll);
  4862. if (reduced_clock)
  4863. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  4864. }
  4865. crtc->new_config->dpll_hw_state.fp0 = fp;
  4866. crtc->lowfreq_avail = false;
  4867. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  4868. reduced_clock && i915.powersave) {
  4869. crtc->new_config->dpll_hw_state.fp1 = fp2;
  4870. crtc->lowfreq_avail = true;
  4871. } else {
  4872. crtc->new_config->dpll_hw_state.fp1 = fp;
  4873. }
  4874. }
  4875. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  4876. pipe)
  4877. {
  4878. u32 reg_val;
  4879. /*
  4880. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  4881. * and set it to a reasonable value instead.
  4882. */
  4883. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4884. reg_val &= 0xffffff00;
  4885. reg_val |= 0x00000030;
  4886. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4887. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4888. reg_val &= 0x8cffffff;
  4889. reg_val = 0x8c000000;
  4890. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4891. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4892. reg_val &= 0xffffff00;
  4893. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4894. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4895. reg_val &= 0x00ffffff;
  4896. reg_val |= 0xb0000000;
  4897. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4898. }
  4899. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  4900. struct intel_link_m_n *m_n)
  4901. {
  4902. struct drm_device *dev = crtc->base.dev;
  4903. struct drm_i915_private *dev_priv = dev->dev_private;
  4904. int pipe = crtc->pipe;
  4905. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4906. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  4907. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  4908. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  4909. }
  4910. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  4911. struct intel_link_m_n *m_n,
  4912. struct intel_link_m_n *m2_n2)
  4913. {
  4914. struct drm_device *dev = crtc->base.dev;
  4915. struct drm_i915_private *dev_priv = dev->dev_private;
  4916. int pipe = crtc->pipe;
  4917. enum transcoder transcoder = crtc->config.cpu_transcoder;
  4918. if (INTEL_INFO(dev)->gen >= 5) {
  4919. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4920. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  4921. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  4922. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  4923. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  4924. * for gen < 8) and if DRRS is supported (to make sure the
  4925. * registers are not unnecessarily accessed).
  4926. */
  4927. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  4928. crtc->config.has_drrs) {
  4929. I915_WRITE(PIPE_DATA_M2(transcoder),
  4930. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  4931. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  4932. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  4933. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  4934. }
  4935. } else {
  4936. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4937. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  4938. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  4939. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  4940. }
  4941. }
  4942. void intel_dp_set_m_n(struct intel_crtc *crtc)
  4943. {
  4944. if (crtc->config.has_pch_encoder)
  4945. intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
  4946. else
  4947. intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n,
  4948. &crtc->config.dp_m2_n2);
  4949. }
  4950. static void vlv_update_pll(struct intel_crtc *crtc,
  4951. struct intel_crtc_config *pipe_config)
  4952. {
  4953. u32 dpll, dpll_md;
  4954. /*
  4955. * Enable DPIO clock input. We should never disable the reference
  4956. * clock for pipe B, since VGA hotplug / manual detection depends
  4957. * on it.
  4958. */
  4959. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  4960. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  4961. /* We should never disable this, set it here for state tracking */
  4962. if (crtc->pipe == PIPE_B)
  4963. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4964. dpll |= DPLL_VCO_ENABLE;
  4965. pipe_config->dpll_hw_state.dpll = dpll;
  4966. dpll_md = (pipe_config->pixel_multiplier - 1)
  4967. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4968. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  4969. }
  4970. static void vlv_prepare_pll(struct intel_crtc *crtc,
  4971. const struct intel_crtc_config *pipe_config)
  4972. {
  4973. struct drm_device *dev = crtc->base.dev;
  4974. struct drm_i915_private *dev_priv = dev->dev_private;
  4975. int pipe = crtc->pipe;
  4976. u32 mdiv;
  4977. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  4978. u32 coreclk, reg_val;
  4979. mutex_lock(&dev_priv->dpio_lock);
  4980. bestn = pipe_config->dpll.n;
  4981. bestm1 = pipe_config->dpll.m1;
  4982. bestm2 = pipe_config->dpll.m2;
  4983. bestp1 = pipe_config->dpll.p1;
  4984. bestp2 = pipe_config->dpll.p2;
  4985. /* See eDP HDMI DPIO driver vbios notes doc */
  4986. /* PLL B needs special handling */
  4987. if (pipe == PIPE_B)
  4988. vlv_pllb_recal_opamp(dev_priv, pipe);
  4989. /* Set up Tx target for periodic Rcomp update */
  4990. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  4991. /* Disable target IRef on PLL */
  4992. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  4993. reg_val &= 0x00ffffff;
  4994. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  4995. /* Disable fast lock */
  4996. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  4997. /* Set idtafcrecal before PLL is enabled */
  4998. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  4999. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  5000. mdiv |= ((bestn << DPIO_N_SHIFT));
  5001. mdiv |= (1 << DPIO_K_SHIFT);
  5002. /*
  5003. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  5004. * but we don't support that).
  5005. * Note: don't use the DAC post divider as it seems unstable.
  5006. */
  5007. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5008. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5009. mdiv |= DPIO_ENABLE_CALIBRATION;
  5010. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5011. /* Set HBR and RBR LPF coefficients */
  5012. if (pipe_config->port_clock == 162000 ||
  5013. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  5014. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  5015. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5016. 0x009f0003);
  5017. else
  5018. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5019. 0x00d0000f);
  5020. if (crtc->config.has_dp_encoder) {
  5021. /* Use SSC source */
  5022. if (pipe == PIPE_A)
  5023. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5024. 0x0df40000);
  5025. else
  5026. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5027. 0x0df70000);
  5028. } else { /* HDMI or VGA */
  5029. /* Use bend source */
  5030. if (pipe == PIPE_A)
  5031. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5032. 0x0df70000);
  5033. else
  5034. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5035. 0x0df40000);
  5036. }
  5037. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5038. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5039. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  5040. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  5041. coreclk |= 0x01000000;
  5042. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5043. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5044. mutex_unlock(&dev_priv->dpio_lock);
  5045. }
  5046. static void chv_update_pll(struct intel_crtc *crtc,
  5047. struct intel_crtc_config *pipe_config)
  5048. {
  5049. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  5050. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  5051. DPLL_VCO_ENABLE;
  5052. if (crtc->pipe != PIPE_A)
  5053. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5054. pipe_config->dpll_hw_state.dpll_md =
  5055. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5056. }
  5057. static void chv_prepare_pll(struct intel_crtc *crtc,
  5058. const struct intel_crtc_config *pipe_config)
  5059. {
  5060. struct drm_device *dev = crtc->base.dev;
  5061. struct drm_i915_private *dev_priv = dev->dev_private;
  5062. int pipe = crtc->pipe;
  5063. int dpll_reg = DPLL(crtc->pipe);
  5064. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5065. u32 loopfilter, intcoeff;
  5066. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5067. int refclk;
  5068. bestn = pipe_config->dpll.n;
  5069. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5070. bestm1 = pipe_config->dpll.m1;
  5071. bestm2 = pipe_config->dpll.m2 >> 22;
  5072. bestp1 = pipe_config->dpll.p1;
  5073. bestp2 = pipe_config->dpll.p2;
  5074. /*
  5075. * Enable Refclk and SSC
  5076. */
  5077. I915_WRITE(dpll_reg,
  5078. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5079. mutex_lock(&dev_priv->dpio_lock);
  5080. /* p1 and p2 divider */
  5081. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5082. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5083. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5084. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5085. 1 << DPIO_CHV_K_DIV_SHIFT);
  5086. /* Feedback post-divider - m2 */
  5087. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5088. /* Feedback refclk divider - n and m1 */
  5089. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5090. DPIO_CHV_M1_DIV_BY_2 |
  5091. 1 << DPIO_CHV_N_DIV_SHIFT);
  5092. /* M2 fraction division */
  5093. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5094. /* M2 fraction division enable */
  5095. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
  5096. DPIO_CHV_FRAC_DIV_EN |
  5097. (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
  5098. /* Loop filter */
  5099. refclk = i9xx_get_refclk(crtc, 0);
  5100. loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
  5101. 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
  5102. if (refclk == 100000)
  5103. intcoeff = 11;
  5104. else if (refclk == 38400)
  5105. intcoeff = 10;
  5106. else
  5107. intcoeff = 9;
  5108. loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
  5109. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5110. /* AFC Recal */
  5111. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5112. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5113. DPIO_AFC_RECAL);
  5114. mutex_unlock(&dev_priv->dpio_lock);
  5115. }
  5116. /**
  5117. * vlv_force_pll_on - forcibly enable just the PLL
  5118. * @dev_priv: i915 private structure
  5119. * @pipe: pipe PLL to enable
  5120. * @dpll: PLL configuration
  5121. *
  5122. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5123. * in cases where we need the PLL enabled even when @pipe is not going to
  5124. * be enabled.
  5125. */
  5126. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  5127. const struct dpll *dpll)
  5128. {
  5129. struct intel_crtc *crtc =
  5130. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  5131. struct intel_crtc_config pipe_config = {
  5132. .pixel_multiplier = 1,
  5133. .dpll = *dpll,
  5134. };
  5135. if (IS_CHERRYVIEW(dev)) {
  5136. chv_update_pll(crtc, &pipe_config);
  5137. chv_prepare_pll(crtc, &pipe_config);
  5138. chv_enable_pll(crtc, &pipe_config);
  5139. } else {
  5140. vlv_update_pll(crtc, &pipe_config);
  5141. vlv_prepare_pll(crtc, &pipe_config);
  5142. vlv_enable_pll(crtc, &pipe_config);
  5143. }
  5144. }
  5145. /**
  5146. * vlv_force_pll_off - forcibly disable just the PLL
  5147. * @dev_priv: i915 private structure
  5148. * @pipe: pipe PLL to disable
  5149. *
  5150. * Disable the PLL for @pipe. To be used in cases where we need
  5151. * the PLL enabled even when @pipe is not going to be enabled.
  5152. */
  5153. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  5154. {
  5155. if (IS_CHERRYVIEW(dev))
  5156. chv_disable_pll(to_i915(dev), pipe);
  5157. else
  5158. vlv_disable_pll(to_i915(dev), pipe);
  5159. }
  5160. static void i9xx_update_pll(struct intel_crtc *crtc,
  5161. intel_clock_t *reduced_clock,
  5162. int num_connectors)
  5163. {
  5164. struct drm_device *dev = crtc->base.dev;
  5165. struct drm_i915_private *dev_priv = dev->dev_private;
  5166. u32 dpll;
  5167. bool is_sdvo;
  5168. struct dpll *clock = &crtc->new_config->dpll;
  5169. i9xx_update_pll_dividers(crtc, reduced_clock);
  5170. is_sdvo = intel_pipe_will_have_type(crtc, INTEL_OUTPUT_SDVO) ||
  5171. intel_pipe_will_have_type(crtc, INTEL_OUTPUT_HDMI);
  5172. dpll = DPLL_VGA_MODE_DIS;
  5173. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  5174. dpll |= DPLLB_MODE_LVDS;
  5175. else
  5176. dpll |= DPLLB_MODE_DAC_SERIAL;
  5177. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5178. dpll |= (crtc->new_config->pixel_multiplier - 1)
  5179. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5180. }
  5181. if (is_sdvo)
  5182. dpll |= DPLL_SDVO_HIGH_SPEED;
  5183. if (crtc->new_config->has_dp_encoder)
  5184. dpll |= DPLL_SDVO_HIGH_SPEED;
  5185. /* compute bitmask from p1 value */
  5186. if (IS_PINEVIEW(dev))
  5187. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5188. else {
  5189. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5190. if (IS_G4X(dev) && reduced_clock)
  5191. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5192. }
  5193. switch (clock->p2) {
  5194. case 5:
  5195. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5196. break;
  5197. case 7:
  5198. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5199. break;
  5200. case 10:
  5201. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5202. break;
  5203. case 14:
  5204. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5205. break;
  5206. }
  5207. if (INTEL_INFO(dev)->gen >= 4)
  5208. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5209. if (crtc->new_config->sdvo_tv_clock)
  5210. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5211. else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  5212. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5213. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5214. else
  5215. dpll |= PLL_REF_INPUT_DREFCLK;
  5216. dpll |= DPLL_VCO_ENABLE;
  5217. crtc->new_config->dpll_hw_state.dpll = dpll;
  5218. if (INTEL_INFO(dev)->gen >= 4) {
  5219. u32 dpll_md = (crtc->new_config->pixel_multiplier - 1)
  5220. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5221. crtc->new_config->dpll_hw_state.dpll_md = dpll_md;
  5222. }
  5223. }
  5224. static void i8xx_update_pll(struct intel_crtc *crtc,
  5225. intel_clock_t *reduced_clock,
  5226. int num_connectors)
  5227. {
  5228. struct drm_device *dev = crtc->base.dev;
  5229. struct drm_i915_private *dev_priv = dev->dev_private;
  5230. u32 dpll;
  5231. struct dpll *clock = &crtc->new_config->dpll;
  5232. i9xx_update_pll_dividers(crtc, reduced_clock);
  5233. dpll = DPLL_VGA_MODE_DIS;
  5234. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  5235. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5236. } else {
  5237. if (clock->p1 == 2)
  5238. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5239. else
  5240. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5241. if (clock->p2 == 4)
  5242. dpll |= PLL_P2_DIVIDE_BY_4;
  5243. }
  5244. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc, INTEL_OUTPUT_DVO))
  5245. dpll |= DPLL_DVO_2X_MODE;
  5246. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  5247. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5248. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5249. else
  5250. dpll |= PLL_REF_INPUT_DREFCLK;
  5251. dpll |= DPLL_VCO_ENABLE;
  5252. crtc->new_config->dpll_hw_state.dpll = dpll;
  5253. }
  5254. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5255. {
  5256. struct drm_device *dev = intel_crtc->base.dev;
  5257. struct drm_i915_private *dev_priv = dev->dev_private;
  5258. enum pipe pipe = intel_crtc->pipe;
  5259. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  5260. struct drm_display_mode *adjusted_mode =
  5261. &intel_crtc->config.adjusted_mode;
  5262. uint32_t crtc_vtotal, crtc_vblank_end;
  5263. int vsyncshift = 0;
  5264. /* We need to be careful not to changed the adjusted mode, for otherwise
  5265. * the hw state checker will get angry at the mismatch. */
  5266. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5267. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5268. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5269. /* the chip adds 2 halflines automatically */
  5270. crtc_vtotal -= 1;
  5271. crtc_vblank_end -= 1;
  5272. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  5273. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5274. else
  5275. vsyncshift = adjusted_mode->crtc_hsync_start -
  5276. adjusted_mode->crtc_htotal / 2;
  5277. if (vsyncshift < 0)
  5278. vsyncshift += adjusted_mode->crtc_htotal;
  5279. }
  5280. if (INTEL_INFO(dev)->gen > 3)
  5281. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5282. I915_WRITE(HTOTAL(cpu_transcoder),
  5283. (adjusted_mode->crtc_hdisplay - 1) |
  5284. ((adjusted_mode->crtc_htotal - 1) << 16));
  5285. I915_WRITE(HBLANK(cpu_transcoder),
  5286. (adjusted_mode->crtc_hblank_start - 1) |
  5287. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5288. I915_WRITE(HSYNC(cpu_transcoder),
  5289. (adjusted_mode->crtc_hsync_start - 1) |
  5290. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5291. I915_WRITE(VTOTAL(cpu_transcoder),
  5292. (adjusted_mode->crtc_vdisplay - 1) |
  5293. ((crtc_vtotal - 1) << 16));
  5294. I915_WRITE(VBLANK(cpu_transcoder),
  5295. (adjusted_mode->crtc_vblank_start - 1) |
  5296. ((crtc_vblank_end - 1) << 16));
  5297. I915_WRITE(VSYNC(cpu_transcoder),
  5298. (adjusted_mode->crtc_vsync_start - 1) |
  5299. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5300. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5301. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5302. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5303. * bits. */
  5304. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  5305. (pipe == PIPE_B || pipe == PIPE_C))
  5306. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5307. /* pipesrc controls the size that is scaled from, which should
  5308. * always be the user's requested size.
  5309. */
  5310. I915_WRITE(PIPESRC(pipe),
  5311. ((intel_crtc->config.pipe_src_w - 1) << 16) |
  5312. (intel_crtc->config.pipe_src_h - 1));
  5313. }
  5314. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5315. struct intel_crtc_config *pipe_config)
  5316. {
  5317. struct drm_device *dev = crtc->base.dev;
  5318. struct drm_i915_private *dev_priv = dev->dev_private;
  5319. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5320. uint32_t tmp;
  5321. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5322. pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5323. pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5324. tmp = I915_READ(HBLANK(cpu_transcoder));
  5325. pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5326. pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5327. tmp = I915_READ(HSYNC(cpu_transcoder));
  5328. pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5329. pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5330. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5331. pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5332. pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  5333. tmp = I915_READ(VBLANK(cpu_transcoder));
  5334. pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  5335. pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  5336. tmp = I915_READ(VSYNC(cpu_transcoder));
  5337. pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  5338. pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  5339. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  5340. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  5341. pipe_config->adjusted_mode.crtc_vtotal += 1;
  5342. pipe_config->adjusted_mode.crtc_vblank_end += 1;
  5343. }
  5344. tmp = I915_READ(PIPESRC(crtc->pipe));
  5345. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  5346. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  5347. pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
  5348. pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
  5349. }
  5350. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  5351. struct intel_crtc_config *pipe_config)
  5352. {
  5353. mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
  5354. mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
  5355. mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
  5356. mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
  5357. mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
  5358. mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
  5359. mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
  5360. mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
  5361. mode->flags = pipe_config->adjusted_mode.flags;
  5362. mode->clock = pipe_config->adjusted_mode.crtc_clock;
  5363. mode->flags |= pipe_config->adjusted_mode.flags;
  5364. }
  5365. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  5366. {
  5367. struct drm_device *dev = intel_crtc->base.dev;
  5368. struct drm_i915_private *dev_priv = dev->dev_private;
  5369. uint32_t pipeconf;
  5370. pipeconf = 0;
  5371. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  5372. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  5373. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  5374. if (intel_crtc->config.double_wide)
  5375. pipeconf |= PIPECONF_DOUBLE_WIDE;
  5376. /* only g4x and later have fancy bpc/dither controls */
  5377. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5378. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  5379. if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
  5380. pipeconf |= PIPECONF_DITHER_EN |
  5381. PIPECONF_DITHER_TYPE_SP;
  5382. switch (intel_crtc->config.pipe_bpp) {
  5383. case 18:
  5384. pipeconf |= PIPECONF_6BPC;
  5385. break;
  5386. case 24:
  5387. pipeconf |= PIPECONF_8BPC;
  5388. break;
  5389. case 30:
  5390. pipeconf |= PIPECONF_10BPC;
  5391. break;
  5392. default:
  5393. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5394. BUG();
  5395. }
  5396. }
  5397. if (HAS_PIPE_CXSR(dev)) {
  5398. if (intel_crtc->lowfreq_avail) {
  5399. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  5400. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  5401. } else {
  5402. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  5403. }
  5404. }
  5405. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  5406. if (INTEL_INFO(dev)->gen < 4 ||
  5407. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  5408. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  5409. else
  5410. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  5411. } else
  5412. pipeconf |= PIPECONF_PROGRESSIVE;
  5413. if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
  5414. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  5415. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  5416. POSTING_READ(PIPECONF(intel_crtc->pipe));
  5417. }
  5418. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc)
  5419. {
  5420. struct drm_device *dev = crtc->base.dev;
  5421. struct drm_i915_private *dev_priv = dev->dev_private;
  5422. int refclk, num_connectors = 0;
  5423. intel_clock_t clock, reduced_clock;
  5424. bool ok, has_reduced_clock = false;
  5425. bool is_lvds = false, is_dsi = false;
  5426. struct intel_encoder *encoder;
  5427. const intel_limit_t *limit;
  5428. for_each_intel_encoder(dev, encoder) {
  5429. if (encoder->new_crtc != crtc)
  5430. continue;
  5431. switch (encoder->type) {
  5432. case INTEL_OUTPUT_LVDS:
  5433. is_lvds = true;
  5434. break;
  5435. case INTEL_OUTPUT_DSI:
  5436. is_dsi = true;
  5437. break;
  5438. default:
  5439. break;
  5440. }
  5441. num_connectors++;
  5442. }
  5443. if (is_dsi)
  5444. return 0;
  5445. if (!crtc->new_config->clock_set) {
  5446. refclk = i9xx_get_refclk(crtc, num_connectors);
  5447. /*
  5448. * Returns a set of divisors for the desired target clock with
  5449. * the given refclk, or FALSE. The returned values represent
  5450. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  5451. * 2) / p1 / p2.
  5452. */
  5453. limit = intel_limit(crtc, refclk);
  5454. ok = dev_priv->display.find_dpll(limit, crtc,
  5455. crtc->new_config->port_clock,
  5456. refclk, NULL, &clock);
  5457. if (!ok) {
  5458. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5459. return -EINVAL;
  5460. }
  5461. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5462. /*
  5463. * Ensure we match the reduced clock's P to the target
  5464. * clock. If the clocks don't match, we can't switch
  5465. * the display clock by using the FP0/FP1. In such case
  5466. * we will disable the LVDS downclock feature.
  5467. */
  5468. has_reduced_clock =
  5469. dev_priv->display.find_dpll(limit, crtc,
  5470. dev_priv->lvds_downclock,
  5471. refclk, &clock,
  5472. &reduced_clock);
  5473. }
  5474. /* Compat-code for transition, will disappear. */
  5475. crtc->new_config->dpll.n = clock.n;
  5476. crtc->new_config->dpll.m1 = clock.m1;
  5477. crtc->new_config->dpll.m2 = clock.m2;
  5478. crtc->new_config->dpll.p1 = clock.p1;
  5479. crtc->new_config->dpll.p2 = clock.p2;
  5480. }
  5481. if (IS_GEN2(dev)) {
  5482. i8xx_update_pll(crtc,
  5483. has_reduced_clock ? &reduced_clock : NULL,
  5484. num_connectors);
  5485. } else if (IS_CHERRYVIEW(dev)) {
  5486. chv_update_pll(crtc, crtc->new_config);
  5487. } else if (IS_VALLEYVIEW(dev)) {
  5488. vlv_update_pll(crtc, crtc->new_config);
  5489. } else {
  5490. i9xx_update_pll(crtc,
  5491. has_reduced_clock ? &reduced_clock : NULL,
  5492. num_connectors);
  5493. }
  5494. return 0;
  5495. }
  5496. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  5497. struct intel_crtc_config *pipe_config)
  5498. {
  5499. struct drm_device *dev = crtc->base.dev;
  5500. struct drm_i915_private *dev_priv = dev->dev_private;
  5501. uint32_t tmp;
  5502. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  5503. return;
  5504. tmp = I915_READ(PFIT_CONTROL);
  5505. if (!(tmp & PFIT_ENABLE))
  5506. return;
  5507. /* Check whether the pfit is attached to our pipe. */
  5508. if (INTEL_INFO(dev)->gen < 4) {
  5509. if (crtc->pipe != PIPE_B)
  5510. return;
  5511. } else {
  5512. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  5513. return;
  5514. }
  5515. pipe_config->gmch_pfit.control = tmp;
  5516. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  5517. if (INTEL_INFO(dev)->gen < 5)
  5518. pipe_config->gmch_pfit.lvds_border_bits =
  5519. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  5520. }
  5521. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  5522. struct intel_crtc_config *pipe_config)
  5523. {
  5524. struct drm_device *dev = crtc->base.dev;
  5525. struct drm_i915_private *dev_priv = dev->dev_private;
  5526. int pipe = pipe_config->cpu_transcoder;
  5527. intel_clock_t clock;
  5528. u32 mdiv;
  5529. int refclk = 100000;
  5530. /* In case of MIPI DPLL will not even be used */
  5531. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  5532. return;
  5533. mutex_lock(&dev_priv->dpio_lock);
  5534. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  5535. mutex_unlock(&dev_priv->dpio_lock);
  5536. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  5537. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  5538. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  5539. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  5540. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  5541. vlv_clock(refclk, &clock);
  5542. /* clock.dot is the fast clock */
  5543. pipe_config->port_clock = clock.dot / 5;
  5544. }
  5545. static void i9xx_get_plane_config(struct intel_crtc *crtc,
  5546. struct intel_plane_config *plane_config)
  5547. {
  5548. struct drm_device *dev = crtc->base.dev;
  5549. struct drm_i915_private *dev_priv = dev->dev_private;
  5550. u32 val, base, offset;
  5551. int pipe = crtc->pipe, plane = crtc->plane;
  5552. int fourcc, pixel_format;
  5553. int aligned_height;
  5554. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  5555. if (!crtc->base.primary->fb) {
  5556. DRM_DEBUG_KMS("failed to alloc fb\n");
  5557. return;
  5558. }
  5559. val = I915_READ(DSPCNTR(plane));
  5560. if (INTEL_INFO(dev)->gen >= 4)
  5561. if (val & DISPPLANE_TILED)
  5562. plane_config->tiled = true;
  5563. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  5564. fourcc = intel_format_to_fourcc(pixel_format);
  5565. crtc->base.primary->fb->pixel_format = fourcc;
  5566. crtc->base.primary->fb->bits_per_pixel =
  5567. drm_format_plane_cpp(fourcc, 0) * 8;
  5568. if (INTEL_INFO(dev)->gen >= 4) {
  5569. if (plane_config->tiled)
  5570. offset = I915_READ(DSPTILEOFF(plane));
  5571. else
  5572. offset = I915_READ(DSPLINOFF(plane));
  5573. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  5574. } else {
  5575. base = I915_READ(DSPADDR(plane));
  5576. }
  5577. plane_config->base = base;
  5578. val = I915_READ(PIPESRC(pipe));
  5579. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  5580. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  5581. val = I915_READ(DSPSTRIDE(pipe));
  5582. crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
  5583. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  5584. plane_config->tiled);
  5585. plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
  5586. aligned_height);
  5587. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  5588. pipe, plane, crtc->base.primary->fb->width,
  5589. crtc->base.primary->fb->height,
  5590. crtc->base.primary->fb->bits_per_pixel, base,
  5591. crtc->base.primary->fb->pitches[0],
  5592. plane_config->size);
  5593. }
  5594. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  5595. struct intel_crtc_config *pipe_config)
  5596. {
  5597. struct drm_device *dev = crtc->base.dev;
  5598. struct drm_i915_private *dev_priv = dev->dev_private;
  5599. int pipe = pipe_config->cpu_transcoder;
  5600. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5601. intel_clock_t clock;
  5602. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  5603. int refclk = 100000;
  5604. mutex_lock(&dev_priv->dpio_lock);
  5605. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  5606. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  5607. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  5608. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  5609. mutex_unlock(&dev_priv->dpio_lock);
  5610. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  5611. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  5612. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  5613. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  5614. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  5615. chv_clock(refclk, &clock);
  5616. /* clock.dot is the fast clock */
  5617. pipe_config->port_clock = clock.dot / 5;
  5618. }
  5619. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  5620. struct intel_crtc_config *pipe_config)
  5621. {
  5622. struct drm_device *dev = crtc->base.dev;
  5623. struct drm_i915_private *dev_priv = dev->dev_private;
  5624. uint32_t tmp;
  5625. if (!intel_display_power_is_enabled(dev_priv,
  5626. POWER_DOMAIN_PIPE(crtc->pipe)))
  5627. return false;
  5628. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5629. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5630. tmp = I915_READ(PIPECONF(crtc->pipe));
  5631. if (!(tmp & PIPECONF_ENABLE))
  5632. return false;
  5633. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5634. switch (tmp & PIPECONF_BPC_MASK) {
  5635. case PIPECONF_6BPC:
  5636. pipe_config->pipe_bpp = 18;
  5637. break;
  5638. case PIPECONF_8BPC:
  5639. pipe_config->pipe_bpp = 24;
  5640. break;
  5641. case PIPECONF_10BPC:
  5642. pipe_config->pipe_bpp = 30;
  5643. break;
  5644. default:
  5645. break;
  5646. }
  5647. }
  5648. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  5649. pipe_config->limited_color_range = true;
  5650. if (INTEL_INFO(dev)->gen < 4)
  5651. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  5652. intel_get_pipe_timings(crtc, pipe_config);
  5653. i9xx_get_pfit_config(crtc, pipe_config);
  5654. if (INTEL_INFO(dev)->gen >= 4) {
  5655. tmp = I915_READ(DPLL_MD(crtc->pipe));
  5656. pipe_config->pixel_multiplier =
  5657. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  5658. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  5659. pipe_config->dpll_hw_state.dpll_md = tmp;
  5660. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5661. tmp = I915_READ(DPLL(crtc->pipe));
  5662. pipe_config->pixel_multiplier =
  5663. ((tmp & SDVO_MULTIPLIER_MASK)
  5664. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  5665. } else {
  5666. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  5667. * port and will be fixed up in the encoder->get_config
  5668. * function. */
  5669. pipe_config->pixel_multiplier = 1;
  5670. }
  5671. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  5672. if (!IS_VALLEYVIEW(dev)) {
  5673. /*
  5674. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  5675. * on 830. Filter it out here so that we don't
  5676. * report errors due to that.
  5677. */
  5678. if (IS_I830(dev))
  5679. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  5680. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  5681. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  5682. } else {
  5683. /* Mask out read-only status bits. */
  5684. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  5685. DPLL_PORTC_READY_MASK |
  5686. DPLL_PORTB_READY_MASK);
  5687. }
  5688. if (IS_CHERRYVIEW(dev))
  5689. chv_crtc_clock_get(crtc, pipe_config);
  5690. else if (IS_VALLEYVIEW(dev))
  5691. vlv_crtc_clock_get(crtc, pipe_config);
  5692. else
  5693. i9xx_crtc_clock_get(crtc, pipe_config);
  5694. return true;
  5695. }
  5696. static void ironlake_init_pch_refclk(struct drm_device *dev)
  5697. {
  5698. struct drm_i915_private *dev_priv = dev->dev_private;
  5699. struct intel_encoder *encoder;
  5700. u32 val, final;
  5701. bool has_lvds = false;
  5702. bool has_cpu_edp = false;
  5703. bool has_panel = false;
  5704. bool has_ck505 = false;
  5705. bool can_ssc = false;
  5706. /* We need to take the global config into account */
  5707. for_each_intel_encoder(dev, encoder) {
  5708. switch (encoder->type) {
  5709. case INTEL_OUTPUT_LVDS:
  5710. has_panel = true;
  5711. has_lvds = true;
  5712. break;
  5713. case INTEL_OUTPUT_EDP:
  5714. has_panel = true;
  5715. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  5716. has_cpu_edp = true;
  5717. break;
  5718. default:
  5719. break;
  5720. }
  5721. }
  5722. if (HAS_PCH_IBX(dev)) {
  5723. has_ck505 = dev_priv->vbt.display_clock_mode;
  5724. can_ssc = has_ck505;
  5725. } else {
  5726. has_ck505 = false;
  5727. can_ssc = true;
  5728. }
  5729. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  5730. has_panel, has_lvds, has_ck505);
  5731. /* Ironlake: try to setup display ref clock before DPLL
  5732. * enabling. This is only under driver's control after
  5733. * PCH B stepping, previous chipset stepping should be
  5734. * ignoring this setting.
  5735. */
  5736. val = I915_READ(PCH_DREF_CONTROL);
  5737. /* As we must carefully and slowly disable/enable each source in turn,
  5738. * compute the final state we want first and check if we need to
  5739. * make any changes at all.
  5740. */
  5741. final = val;
  5742. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  5743. if (has_ck505)
  5744. final |= DREF_NONSPREAD_CK505_ENABLE;
  5745. else
  5746. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  5747. final &= ~DREF_SSC_SOURCE_MASK;
  5748. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5749. final &= ~DREF_SSC1_ENABLE;
  5750. if (has_panel) {
  5751. final |= DREF_SSC_SOURCE_ENABLE;
  5752. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5753. final |= DREF_SSC1_ENABLE;
  5754. if (has_cpu_edp) {
  5755. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5756. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5757. else
  5758. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5759. } else
  5760. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5761. } else {
  5762. final |= DREF_SSC_SOURCE_DISABLE;
  5763. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5764. }
  5765. if (final == val)
  5766. return;
  5767. /* Always enable nonspread source */
  5768. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  5769. if (has_ck505)
  5770. val |= DREF_NONSPREAD_CK505_ENABLE;
  5771. else
  5772. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  5773. if (has_panel) {
  5774. val &= ~DREF_SSC_SOURCE_MASK;
  5775. val |= DREF_SSC_SOURCE_ENABLE;
  5776. /* SSC must be turned on before enabling the CPU output */
  5777. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5778. DRM_DEBUG_KMS("Using SSC on panel\n");
  5779. val |= DREF_SSC1_ENABLE;
  5780. } else
  5781. val &= ~DREF_SSC1_ENABLE;
  5782. /* Get SSC going before enabling the outputs */
  5783. I915_WRITE(PCH_DREF_CONTROL, val);
  5784. POSTING_READ(PCH_DREF_CONTROL);
  5785. udelay(200);
  5786. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5787. /* Enable CPU source on CPU attached eDP */
  5788. if (has_cpu_edp) {
  5789. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5790. DRM_DEBUG_KMS("Using SSC on eDP\n");
  5791. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5792. } else
  5793. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5794. } else
  5795. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5796. I915_WRITE(PCH_DREF_CONTROL, val);
  5797. POSTING_READ(PCH_DREF_CONTROL);
  5798. udelay(200);
  5799. } else {
  5800. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  5801. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5802. /* Turn off CPU output */
  5803. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5804. I915_WRITE(PCH_DREF_CONTROL, val);
  5805. POSTING_READ(PCH_DREF_CONTROL);
  5806. udelay(200);
  5807. /* Turn off the SSC source */
  5808. val &= ~DREF_SSC_SOURCE_MASK;
  5809. val |= DREF_SSC_SOURCE_DISABLE;
  5810. /* Turn off SSC1 */
  5811. val &= ~DREF_SSC1_ENABLE;
  5812. I915_WRITE(PCH_DREF_CONTROL, val);
  5813. POSTING_READ(PCH_DREF_CONTROL);
  5814. udelay(200);
  5815. }
  5816. BUG_ON(val != final);
  5817. }
  5818. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  5819. {
  5820. uint32_t tmp;
  5821. tmp = I915_READ(SOUTH_CHICKEN2);
  5822. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  5823. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5824. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  5825. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  5826. DRM_ERROR("FDI mPHY reset assert timeout\n");
  5827. tmp = I915_READ(SOUTH_CHICKEN2);
  5828. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  5829. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5830. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  5831. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  5832. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  5833. }
  5834. /* WaMPhyProgramming:hsw */
  5835. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  5836. {
  5837. uint32_t tmp;
  5838. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  5839. tmp &= ~(0xFF << 24);
  5840. tmp |= (0x12 << 24);
  5841. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  5842. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  5843. tmp |= (1 << 11);
  5844. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  5845. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  5846. tmp |= (1 << 11);
  5847. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  5848. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  5849. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5850. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  5851. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  5852. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5853. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  5854. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  5855. tmp &= ~(7 << 13);
  5856. tmp |= (5 << 13);
  5857. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  5858. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  5859. tmp &= ~(7 << 13);
  5860. tmp |= (5 << 13);
  5861. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  5862. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  5863. tmp &= ~0xFF;
  5864. tmp |= 0x1C;
  5865. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  5866. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  5867. tmp &= ~0xFF;
  5868. tmp |= 0x1C;
  5869. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  5870. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  5871. tmp &= ~(0xFF << 16);
  5872. tmp |= (0x1C << 16);
  5873. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  5874. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  5875. tmp &= ~(0xFF << 16);
  5876. tmp |= (0x1C << 16);
  5877. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  5878. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  5879. tmp |= (1 << 27);
  5880. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  5881. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  5882. tmp |= (1 << 27);
  5883. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  5884. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  5885. tmp &= ~(0xF << 28);
  5886. tmp |= (4 << 28);
  5887. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  5888. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  5889. tmp &= ~(0xF << 28);
  5890. tmp |= (4 << 28);
  5891. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  5892. }
  5893. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  5894. * Programming" based on the parameters passed:
  5895. * - Sequence to enable CLKOUT_DP
  5896. * - Sequence to enable CLKOUT_DP without spread
  5897. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  5898. */
  5899. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  5900. bool with_fdi)
  5901. {
  5902. struct drm_i915_private *dev_priv = dev->dev_private;
  5903. uint32_t reg, tmp;
  5904. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  5905. with_spread = true;
  5906. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  5907. with_fdi, "LP PCH doesn't have FDI\n"))
  5908. with_fdi = false;
  5909. mutex_lock(&dev_priv->dpio_lock);
  5910. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5911. tmp &= ~SBI_SSCCTL_DISABLE;
  5912. tmp |= SBI_SSCCTL_PATHALT;
  5913. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5914. udelay(24);
  5915. if (with_spread) {
  5916. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5917. tmp &= ~SBI_SSCCTL_PATHALT;
  5918. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5919. if (with_fdi) {
  5920. lpt_reset_fdi_mphy(dev_priv);
  5921. lpt_program_fdi_mphy(dev_priv);
  5922. }
  5923. }
  5924. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5925. SBI_GEN0 : SBI_DBUFF0;
  5926. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5927. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5928. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5929. mutex_unlock(&dev_priv->dpio_lock);
  5930. }
  5931. /* Sequence to disable CLKOUT_DP */
  5932. static void lpt_disable_clkout_dp(struct drm_device *dev)
  5933. {
  5934. struct drm_i915_private *dev_priv = dev->dev_private;
  5935. uint32_t reg, tmp;
  5936. mutex_lock(&dev_priv->dpio_lock);
  5937. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5938. SBI_GEN0 : SBI_DBUFF0;
  5939. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5940. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5941. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5942. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5943. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  5944. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  5945. tmp |= SBI_SSCCTL_PATHALT;
  5946. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5947. udelay(32);
  5948. }
  5949. tmp |= SBI_SSCCTL_DISABLE;
  5950. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5951. }
  5952. mutex_unlock(&dev_priv->dpio_lock);
  5953. }
  5954. static void lpt_init_pch_refclk(struct drm_device *dev)
  5955. {
  5956. struct intel_encoder *encoder;
  5957. bool has_vga = false;
  5958. for_each_intel_encoder(dev, encoder) {
  5959. switch (encoder->type) {
  5960. case INTEL_OUTPUT_ANALOG:
  5961. has_vga = true;
  5962. break;
  5963. default:
  5964. break;
  5965. }
  5966. }
  5967. if (has_vga)
  5968. lpt_enable_clkout_dp(dev, true, true);
  5969. else
  5970. lpt_disable_clkout_dp(dev);
  5971. }
  5972. /*
  5973. * Initialize reference clocks when the driver loads
  5974. */
  5975. void intel_init_pch_refclk(struct drm_device *dev)
  5976. {
  5977. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  5978. ironlake_init_pch_refclk(dev);
  5979. else if (HAS_PCH_LPT(dev))
  5980. lpt_init_pch_refclk(dev);
  5981. }
  5982. static int ironlake_get_refclk(struct drm_crtc *crtc)
  5983. {
  5984. struct drm_device *dev = crtc->dev;
  5985. struct drm_i915_private *dev_priv = dev->dev_private;
  5986. struct intel_encoder *encoder;
  5987. int num_connectors = 0;
  5988. bool is_lvds = false;
  5989. for_each_intel_encoder(dev, encoder) {
  5990. if (encoder->new_crtc != to_intel_crtc(crtc))
  5991. continue;
  5992. switch (encoder->type) {
  5993. case INTEL_OUTPUT_LVDS:
  5994. is_lvds = true;
  5995. break;
  5996. default:
  5997. break;
  5998. }
  5999. num_connectors++;
  6000. }
  6001. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  6002. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  6003. dev_priv->vbt.lvds_ssc_freq);
  6004. return dev_priv->vbt.lvds_ssc_freq;
  6005. }
  6006. return 120000;
  6007. }
  6008. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  6009. {
  6010. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  6011. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6012. int pipe = intel_crtc->pipe;
  6013. uint32_t val;
  6014. val = 0;
  6015. switch (intel_crtc->config.pipe_bpp) {
  6016. case 18:
  6017. val |= PIPECONF_6BPC;
  6018. break;
  6019. case 24:
  6020. val |= PIPECONF_8BPC;
  6021. break;
  6022. case 30:
  6023. val |= PIPECONF_10BPC;
  6024. break;
  6025. case 36:
  6026. val |= PIPECONF_12BPC;
  6027. break;
  6028. default:
  6029. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6030. BUG();
  6031. }
  6032. if (intel_crtc->config.dither)
  6033. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6034. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6035. val |= PIPECONF_INTERLACED_ILK;
  6036. else
  6037. val |= PIPECONF_PROGRESSIVE;
  6038. if (intel_crtc->config.limited_color_range)
  6039. val |= PIPECONF_COLOR_RANGE_SELECT;
  6040. I915_WRITE(PIPECONF(pipe), val);
  6041. POSTING_READ(PIPECONF(pipe));
  6042. }
  6043. /*
  6044. * Set up the pipe CSC unit.
  6045. *
  6046. * Currently only full range RGB to limited range RGB conversion
  6047. * is supported, but eventually this should handle various
  6048. * RGB<->YCbCr scenarios as well.
  6049. */
  6050. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  6051. {
  6052. struct drm_device *dev = crtc->dev;
  6053. struct drm_i915_private *dev_priv = dev->dev_private;
  6054. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6055. int pipe = intel_crtc->pipe;
  6056. uint16_t coeff = 0x7800; /* 1.0 */
  6057. /*
  6058. * TODO: Check what kind of values actually come out of the pipe
  6059. * with these coeff/postoff values and adjust to get the best
  6060. * accuracy. Perhaps we even need to take the bpc value into
  6061. * consideration.
  6062. */
  6063. if (intel_crtc->config.limited_color_range)
  6064. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  6065. /*
  6066. * GY/GU and RY/RU should be the other way around according
  6067. * to BSpec, but reality doesn't agree. Just set them up in
  6068. * a way that results in the correct picture.
  6069. */
  6070. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  6071. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  6072. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  6073. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  6074. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  6075. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  6076. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  6077. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  6078. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  6079. if (INTEL_INFO(dev)->gen > 6) {
  6080. uint16_t postoff = 0;
  6081. if (intel_crtc->config.limited_color_range)
  6082. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  6083. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  6084. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  6085. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  6086. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  6087. } else {
  6088. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  6089. if (intel_crtc->config.limited_color_range)
  6090. mode |= CSC_BLACK_SCREEN_OFFSET;
  6091. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  6092. }
  6093. }
  6094. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6095. {
  6096. struct drm_device *dev = crtc->dev;
  6097. struct drm_i915_private *dev_priv = dev->dev_private;
  6098. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6099. enum pipe pipe = intel_crtc->pipe;
  6100. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  6101. uint32_t val;
  6102. val = 0;
  6103. if (IS_HASWELL(dev) && intel_crtc->config.dither)
  6104. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6105. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6106. val |= PIPECONF_INTERLACED_ILK;
  6107. else
  6108. val |= PIPECONF_PROGRESSIVE;
  6109. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6110. POSTING_READ(PIPECONF(cpu_transcoder));
  6111. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  6112. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  6113. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  6114. val = 0;
  6115. switch (intel_crtc->config.pipe_bpp) {
  6116. case 18:
  6117. val |= PIPEMISC_DITHER_6_BPC;
  6118. break;
  6119. case 24:
  6120. val |= PIPEMISC_DITHER_8_BPC;
  6121. break;
  6122. case 30:
  6123. val |= PIPEMISC_DITHER_10_BPC;
  6124. break;
  6125. case 36:
  6126. val |= PIPEMISC_DITHER_12_BPC;
  6127. break;
  6128. default:
  6129. /* Case prevented by pipe_config_set_bpp. */
  6130. BUG();
  6131. }
  6132. if (intel_crtc->config.dither)
  6133. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  6134. I915_WRITE(PIPEMISC(pipe), val);
  6135. }
  6136. }
  6137. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  6138. intel_clock_t *clock,
  6139. bool *has_reduced_clock,
  6140. intel_clock_t *reduced_clock)
  6141. {
  6142. struct drm_device *dev = crtc->dev;
  6143. struct drm_i915_private *dev_priv = dev->dev_private;
  6144. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6145. int refclk;
  6146. const intel_limit_t *limit;
  6147. bool ret, is_lvds = false;
  6148. is_lvds = intel_pipe_will_have_type(intel_crtc, INTEL_OUTPUT_LVDS);
  6149. refclk = ironlake_get_refclk(crtc);
  6150. /*
  6151. * Returns a set of divisors for the desired target clock with the given
  6152. * refclk, or FALSE. The returned values represent the clock equation:
  6153. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  6154. */
  6155. limit = intel_limit(intel_crtc, refclk);
  6156. ret = dev_priv->display.find_dpll(limit, intel_crtc,
  6157. intel_crtc->new_config->port_clock,
  6158. refclk, NULL, clock);
  6159. if (!ret)
  6160. return false;
  6161. if (is_lvds && dev_priv->lvds_downclock_avail) {
  6162. /*
  6163. * Ensure we match the reduced clock's P to the target clock.
  6164. * If the clocks don't match, we can't switch the display clock
  6165. * by using the FP0/FP1. In such case we will disable the LVDS
  6166. * downclock feature.
  6167. */
  6168. *has_reduced_clock =
  6169. dev_priv->display.find_dpll(limit, intel_crtc,
  6170. dev_priv->lvds_downclock,
  6171. refclk, clock,
  6172. reduced_clock);
  6173. }
  6174. return true;
  6175. }
  6176. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  6177. {
  6178. /*
  6179. * Account for spread spectrum to avoid
  6180. * oversubscribing the link. Max center spread
  6181. * is 2.5%; use 5% for safety's sake.
  6182. */
  6183. u32 bps = target_clock * bpp * 21 / 20;
  6184. return DIV_ROUND_UP(bps, link_bw * 8);
  6185. }
  6186. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  6187. {
  6188. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  6189. }
  6190. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  6191. u32 *fp,
  6192. intel_clock_t *reduced_clock, u32 *fp2)
  6193. {
  6194. struct drm_crtc *crtc = &intel_crtc->base;
  6195. struct drm_device *dev = crtc->dev;
  6196. struct drm_i915_private *dev_priv = dev->dev_private;
  6197. struct intel_encoder *intel_encoder;
  6198. uint32_t dpll;
  6199. int factor, num_connectors = 0;
  6200. bool is_lvds = false, is_sdvo = false;
  6201. for_each_intel_encoder(dev, intel_encoder) {
  6202. if (intel_encoder->new_crtc != to_intel_crtc(crtc))
  6203. continue;
  6204. switch (intel_encoder->type) {
  6205. case INTEL_OUTPUT_LVDS:
  6206. is_lvds = true;
  6207. break;
  6208. case INTEL_OUTPUT_SDVO:
  6209. case INTEL_OUTPUT_HDMI:
  6210. is_sdvo = true;
  6211. break;
  6212. default:
  6213. break;
  6214. }
  6215. num_connectors++;
  6216. }
  6217. /* Enable autotuning of the PLL clock (if permissible) */
  6218. factor = 21;
  6219. if (is_lvds) {
  6220. if ((intel_panel_use_ssc(dev_priv) &&
  6221. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6222. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  6223. factor = 25;
  6224. } else if (intel_crtc->new_config->sdvo_tv_clock)
  6225. factor = 20;
  6226. if (ironlake_needs_fb_cb_tune(&intel_crtc->new_config->dpll, factor))
  6227. *fp |= FP_CB_TUNE;
  6228. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  6229. *fp2 |= FP_CB_TUNE;
  6230. dpll = 0;
  6231. if (is_lvds)
  6232. dpll |= DPLLB_MODE_LVDS;
  6233. else
  6234. dpll |= DPLLB_MODE_DAC_SERIAL;
  6235. dpll |= (intel_crtc->new_config->pixel_multiplier - 1)
  6236. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6237. if (is_sdvo)
  6238. dpll |= DPLL_SDVO_HIGH_SPEED;
  6239. if (intel_crtc->new_config->has_dp_encoder)
  6240. dpll |= DPLL_SDVO_HIGH_SPEED;
  6241. /* compute bitmask from p1 value */
  6242. dpll |= (1 << (intel_crtc->new_config->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6243. /* also FPA1 */
  6244. dpll |= (1 << (intel_crtc->new_config->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6245. switch (intel_crtc->new_config->dpll.p2) {
  6246. case 5:
  6247. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6248. break;
  6249. case 7:
  6250. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6251. break;
  6252. case 10:
  6253. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6254. break;
  6255. case 14:
  6256. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6257. break;
  6258. }
  6259. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6260. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6261. else
  6262. dpll |= PLL_REF_INPUT_DREFCLK;
  6263. return dpll | DPLL_VCO_ENABLE;
  6264. }
  6265. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc)
  6266. {
  6267. struct drm_device *dev = crtc->base.dev;
  6268. intel_clock_t clock, reduced_clock;
  6269. u32 dpll = 0, fp = 0, fp2 = 0;
  6270. bool ok, has_reduced_clock = false;
  6271. bool is_lvds = false;
  6272. struct intel_shared_dpll *pll;
  6273. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  6274. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  6275. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  6276. ok = ironlake_compute_clocks(&crtc->base, &clock,
  6277. &has_reduced_clock, &reduced_clock);
  6278. if (!ok && !crtc->new_config->clock_set) {
  6279. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6280. return -EINVAL;
  6281. }
  6282. /* Compat-code for transition, will disappear. */
  6283. if (!crtc->new_config->clock_set) {
  6284. crtc->new_config->dpll.n = clock.n;
  6285. crtc->new_config->dpll.m1 = clock.m1;
  6286. crtc->new_config->dpll.m2 = clock.m2;
  6287. crtc->new_config->dpll.p1 = clock.p1;
  6288. crtc->new_config->dpll.p2 = clock.p2;
  6289. }
  6290. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  6291. if (crtc->new_config->has_pch_encoder) {
  6292. fp = i9xx_dpll_compute_fp(&crtc->new_config->dpll);
  6293. if (has_reduced_clock)
  6294. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  6295. dpll = ironlake_compute_dpll(crtc,
  6296. &fp, &reduced_clock,
  6297. has_reduced_clock ? &fp2 : NULL);
  6298. crtc->new_config->dpll_hw_state.dpll = dpll;
  6299. crtc->new_config->dpll_hw_state.fp0 = fp;
  6300. if (has_reduced_clock)
  6301. crtc->new_config->dpll_hw_state.fp1 = fp2;
  6302. else
  6303. crtc->new_config->dpll_hw_state.fp1 = fp;
  6304. pll = intel_get_shared_dpll(crtc);
  6305. if (pll == NULL) {
  6306. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  6307. pipe_name(crtc->pipe));
  6308. return -EINVAL;
  6309. }
  6310. }
  6311. if (is_lvds && has_reduced_clock && i915.powersave)
  6312. crtc->lowfreq_avail = true;
  6313. else
  6314. crtc->lowfreq_avail = false;
  6315. return 0;
  6316. }
  6317. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  6318. struct intel_link_m_n *m_n)
  6319. {
  6320. struct drm_device *dev = crtc->base.dev;
  6321. struct drm_i915_private *dev_priv = dev->dev_private;
  6322. enum pipe pipe = crtc->pipe;
  6323. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  6324. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  6325. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  6326. & ~TU_SIZE_MASK;
  6327. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  6328. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  6329. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6330. }
  6331. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  6332. enum transcoder transcoder,
  6333. struct intel_link_m_n *m_n,
  6334. struct intel_link_m_n *m2_n2)
  6335. {
  6336. struct drm_device *dev = crtc->base.dev;
  6337. struct drm_i915_private *dev_priv = dev->dev_private;
  6338. enum pipe pipe = crtc->pipe;
  6339. if (INTEL_INFO(dev)->gen >= 5) {
  6340. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  6341. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  6342. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  6343. & ~TU_SIZE_MASK;
  6344. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  6345. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  6346. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6347. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  6348. * gen < 8) and if DRRS is supported (to make sure the
  6349. * registers are not unnecessarily read).
  6350. */
  6351. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  6352. crtc->config.has_drrs) {
  6353. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  6354. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  6355. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  6356. & ~TU_SIZE_MASK;
  6357. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  6358. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  6359. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6360. }
  6361. } else {
  6362. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  6363. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  6364. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  6365. & ~TU_SIZE_MASK;
  6366. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  6367. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  6368. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6369. }
  6370. }
  6371. void intel_dp_get_m_n(struct intel_crtc *crtc,
  6372. struct intel_crtc_config *pipe_config)
  6373. {
  6374. if (crtc->config.has_pch_encoder)
  6375. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  6376. else
  6377. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6378. &pipe_config->dp_m_n,
  6379. &pipe_config->dp_m2_n2);
  6380. }
  6381. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  6382. struct intel_crtc_config *pipe_config)
  6383. {
  6384. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6385. &pipe_config->fdi_m_n, NULL);
  6386. }
  6387. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  6388. struct intel_crtc_config *pipe_config)
  6389. {
  6390. struct drm_device *dev = crtc->base.dev;
  6391. struct drm_i915_private *dev_priv = dev->dev_private;
  6392. uint32_t tmp;
  6393. tmp = I915_READ(PS_CTL(crtc->pipe));
  6394. if (tmp & PS_ENABLE) {
  6395. pipe_config->pch_pfit.enabled = true;
  6396. pipe_config->pch_pfit.pos = I915_READ(PS_WIN_POS(crtc->pipe));
  6397. pipe_config->pch_pfit.size = I915_READ(PS_WIN_SZ(crtc->pipe));
  6398. }
  6399. }
  6400. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  6401. struct intel_crtc_config *pipe_config)
  6402. {
  6403. struct drm_device *dev = crtc->base.dev;
  6404. struct drm_i915_private *dev_priv = dev->dev_private;
  6405. uint32_t tmp;
  6406. tmp = I915_READ(PF_CTL(crtc->pipe));
  6407. if (tmp & PF_ENABLE) {
  6408. pipe_config->pch_pfit.enabled = true;
  6409. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  6410. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  6411. /* We currently do not free assignements of panel fitters on
  6412. * ivb/hsw (since we don't use the higher upscaling modes which
  6413. * differentiates them) so just WARN about this case for now. */
  6414. if (IS_GEN7(dev)) {
  6415. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  6416. PF_PIPE_SEL_IVB(crtc->pipe));
  6417. }
  6418. }
  6419. }
  6420. static void ironlake_get_plane_config(struct intel_crtc *crtc,
  6421. struct intel_plane_config *plane_config)
  6422. {
  6423. struct drm_device *dev = crtc->base.dev;
  6424. struct drm_i915_private *dev_priv = dev->dev_private;
  6425. u32 val, base, offset;
  6426. int pipe = crtc->pipe, plane = crtc->plane;
  6427. int fourcc, pixel_format;
  6428. int aligned_height;
  6429. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  6430. if (!crtc->base.primary->fb) {
  6431. DRM_DEBUG_KMS("failed to alloc fb\n");
  6432. return;
  6433. }
  6434. val = I915_READ(DSPCNTR(plane));
  6435. if (INTEL_INFO(dev)->gen >= 4)
  6436. if (val & DISPPLANE_TILED)
  6437. plane_config->tiled = true;
  6438. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6439. fourcc = intel_format_to_fourcc(pixel_format);
  6440. crtc->base.primary->fb->pixel_format = fourcc;
  6441. crtc->base.primary->fb->bits_per_pixel =
  6442. drm_format_plane_cpp(fourcc, 0) * 8;
  6443. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6444. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  6445. offset = I915_READ(DSPOFFSET(plane));
  6446. } else {
  6447. if (plane_config->tiled)
  6448. offset = I915_READ(DSPTILEOFF(plane));
  6449. else
  6450. offset = I915_READ(DSPLINOFF(plane));
  6451. }
  6452. plane_config->base = base;
  6453. val = I915_READ(PIPESRC(pipe));
  6454. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  6455. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  6456. val = I915_READ(DSPSTRIDE(pipe));
  6457. crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
  6458. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  6459. plane_config->tiled);
  6460. plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
  6461. aligned_height);
  6462. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6463. pipe, plane, crtc->base.primary->fb->width,
  6464. crtc->base.primary->fb->height,
  6465. crtc->base.primary->fb->bits_per_pixel, base,
  6466. crtc->base.primary->fb->pitches[0],
  6467. plane_config->size);
  6468. }
  6469. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  6470. struct intel_crtc_config *pipe_config)
  6471. {
  6472. struct drm_device *dev = crtc->base.dev;
  6473. struct drm_i915_private *dev_priv = dev->dev_private;
  6474. uint32_t tmp;
  6475. if (!intel_display_power_is_enabled(dev_priv,
  6476. POWER_DOMAIN_PIPE(crtc->pipe)))
  6477. return false;
  6478. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6479. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6480. tmp = I915_READ(PIPECONF(crtc->pipe));
  6481. if (!(tmp & PIPECONF_ENABLE))
  6482. return false;
  6483. switch (tmp & PIPECONF_BPC_MASK) {
  6484. case PIPECONF_6BPC:
  6485. pipe_config->pipe_bpp = 18;
  6486. break;
  6487. case PIPECONF_8BPC:
  6488. pipe_config->pipe_bpp = 24;
  6489. break;
  6490. case PIPECONF_10BPC:
  6491. pipe_config->pipe_bpp = 30;
  6492. break;
  6493. case PIPECONF_12BPC:
  6494. pipe_config->pipe_bpp = 36;
  6495. break;
  6496. default:
  6497. break;
  6498. }
  6499. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  6500. pipe_config->limited_color_range = true;
  6501. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  6502. struct intel_shared_dpll *pll;
  6503. pipe_config->has_pch_encoder = true;
  6504. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  6505. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6506. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6507. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6508. if (HAS_PCH_IBX(dev_priv->dev)) {
  6509. pipe_config->shared_dpll =
  6510. (enum intel_dpll_id) crtc->pipe;
  6511. } else {
  6512. tmp = I915_READ(PCH_DPLL_SEL);
  6513. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  6514. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  6515. else
  6516. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  6517. }
  6518. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6519. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6520. &pipe_config->dpll_hw_state));
  6521. tmp = pipe_config->dpll_hw_state.dpll;
  6522. pipe_config->pixel_multiplier =
  6523. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  6524. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  6525. ironlake_pch_clock_get(crtc, pipe_config);
  6526. } else {
  6527. pipe_config->pixel_multiplier = 1;
  6528. }
  6529. intel_get_pipe_timings(crtc, pipe_config);
  6530. ironlake_get_pfit_config(crtc, pipe_config);
  6531. return true;
  6532. }
  6533. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  6534. {
  6535. struct drm_device *dev = dev_priv->dev;
  6536. struct intel_crtc *crtc;
  6537. for_each_intel_crtc(dev, crtc)
  6538. WARN(crtc->active, "CRTC for pipe %c enabled\n",
  6539. pipe_name(crtc->pipe));
  6540. WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  6541. WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  6542. WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  6543. WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  6544. WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  6545. WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  6546. "CPU PWM1 enabled\n");
  6547. if (IS_HASWELL(dev))
  6548. WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  6549. "CPU PWM2 enabled\n");
  6550. WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  6551. "PCH PWM1 enabled\n");
  6552. WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  6553. "Utility pin enabled\n");
  6554. WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  6555. /*
  6556. * In theory we can still leave IRQs enabled, as long as only the HPD
  6557. * interrupts remain enabled. We used to check for that, but since it's
  6558. * gen-specific and since we only disable LCPLL after we fully disable
  6559. * the interrupts, the check below should be enough.
  6560. */
  6561. WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  6562. }
  6563. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  6564. {
  6565. struct drm_device *dev = dev_priv->dev;
  6566. if (IS_HASWELL(dev))
  6567. return I915_READ(D_COMP_HSW);
  6568. else
  6569. return I915_READ(D_COMP_BDW);
  6570. }
  6571. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  6572. {
  6573. struct drm_device *dev = dev_priv->dev;
  6574. if (IS_HASWELL(dev)) {
  6575. mutex_lock(&dev_priv->rps.hw_lock);
  6576. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  6577. val))
  6578. DRM_ERROR("Failed to write to D_COMP\n");
  6579. mutex_unlock(&dev_priv->rps.hw_lock);
  6580. } else {
  6581. I915_WRITE(D_COMP_BDW, val);
  6582. POSTING_READ(D_COMP_BDW);
  6583. }
  6584. }
  6585. /*
  6586. * This function implements pieces of two sequences from BSpec:
  6587. * - Sequence for display software to disable LCPLL
  6588. * - Sequence for display software to allow package C8+
  6589. * The steps implemented here are just the steps that actually touch the LCPLL
  6590. * register. Callers should take care of disabling all the display engine
  6591. * functions, doing the mode unset, fixing interrupts, etc.
  6592. */
  6593. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  6594. bool switch_to_fclk, bool allow_power_down)
  6595. {
  6596. uint32_t val;
  6597. assert_can_disable_lcpll(dev_priv);
  6598. val = I915_READ(LCPLL_CTL);
  6599. if (switch_to_fclk) {
  6600. val |= LCPLL_CD_SOURCE_FCLK;
  6601. I915_WRITE(LCPLL_CTL, val);
  6602. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  6603. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  6604. DRM_ERROR("Switching to FCLK failed\n");
  6605. val = I915_READ(LCPLL_CTL);
  6606. }
  6607. val |= LCPLL_PLL_DISABLE;
  6608. I915_WRITE(LCPLL_CTL, val);
  6609. POSTING_READ(LCPLL_CTL);
  6610. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  6611. DRM_ERROR("LCPLL still locked\n");
  6612. val = hsw_read_dcomp(dev_priv);
  6613. val |= D_COMP_COMP_DISABLE;
  6614. hsw_write_dcomp(dev_priv, val);
  6615. ndelay(100);
  6616. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  6617. 1))
  6618. DRM_ERROR("D_COMP RCOMP still in progress\n");
  6619. if (allow_power_down) {
  6620. val = I915_READ(LCPLL_CTL);
  6621. val |= LCPLL_POWER_DOWN_ALLOW;
  6622. I915_WRITE(LCPLL_CTL, val);
  6623. POSTING_READ(LCPLL_CTL);
  6624. }
  6625. }
  6626. /*
  6627. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  6628. * source.
  6629. */
  6630. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  6631. {
  6632. uint32_t val;
  6633. val = I915_READ(LCPLL_CTL);
  6634. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  6635. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  6636. return;
  6637. /*
  6638. * Make sure we're not on PC8 state before disabling PC8, otherwise
  6639. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  6640. *
  6641. * The other problem is that hsw_restore_lcpll() is called as part of
  6642. * the runtime PM resume sequence, so we can't just call
  6643. * gen6_gt_force_wake_get() because that function calls
  6644. * intel_runtime_pm_get(), and we can't change the runtime PM refcount
  6645. * while we are on the resume sequence. So to solve this problem we have
  6646. * to call special forcewake code that doesn't touch runtime PM and
  6647. * doesn't enable the forcewake delayed work.
  6648. */
  6649. spin_lock_irq(&dev_priv->uncore.lock);
  6650. if (dev_priv->uncore.forcewake_count++ == 0)
  6651. dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
  6652. spin_unlock_irq(&dev_priv->uncore.lock);
  6653. if (val & LCPLL_POWER_DOWN_ALLOW) {
  6654. val &= ~LCPLL_POWER_DOWN_ALLOW;
  6655. I915_WRITE(LCPLL_CTL, val);
  6656. POSTING_READ(LCPLL_CTL);
  6657. }
  6658. val = hsw_read_dcomp(dev_priv);
  6659. val |= D_COMP_COMP_FORCE;
  6660. val &= ~D_COMP_COMP_DISABLE;
  6661. hsw_write_dcomp(dev_priv, val);
  6662. val = I915_READ(LCPLL_CTL);
  6663. val &= ~LCPLL_PLL_DISABLE;
  6664. I915_WRITE(LCPLL_CTL, val);
  6665. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  6666. DRM_ERROR("LCPLL not locked yet\n");
  6667. if (val & LCPLL_CD_SOURCE_FCLK) {
  6668. val = I915_READ(LCPLL_CTL);
  6669. val &= ~LCPLL_CD_SOURCE_FCLK;
  6670. I915_WRITE(LCPLL_CTL, val);
  6671. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  6672. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  6673. DRM_ERROR("Switching back to LCPLL failed\n");
  6674. }
  6675. /* See the big comment above. */
  6676. spin_lock_irq(&dev_priv->uncore.lock);
  6677. if (--dev_priv->uncore.forcewake_count == 0)
  6678. dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
  6679. spin_unlock_irq(&dev_priv->uncore.lock);
  6680. }
  6681. /*
  6682. * Package states C8 and deeper are really deep PC states that can only be
  6683. * reached when all the devices on the system allow it, so even if the graphics
  6684. * device allows PC8+, it doesn't mean the system will actually get to these
  6685. * states. Our driver only allows PC8+ when going into runtime PM.
  6686. *
  6687. * The requirements for PC8+ are that all the outputs are disabled, the power
  6688. * well is disabled and most interrupts are disabled, and these are also
  6689. * requirements for runtime PM. When these conditions are met, we manually do
  6690. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  6691. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  6692. * hang the machine.
  6693. *
  6694. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  6695. * the state of some registers, so when we come back from PC8+ we need to
  6696. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  6697. * need to take care of the registers kept by RC6. Notice that this happens even
  6698. * if we don't put the device in PCI D3 state (which is what currently happens
  6699. * because of the runtime PM support).
  6700. *
  6701. * For more, read "Display Sequences for Package C8" on the hardware
  6702. * documentation.
  6703. */
  6704. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  6705. {
  6706. struct drm_device *dev = dev_priv->dev;
  6707. uint32_t val;
  6708. DRM_DEBUG_KMS("Enabling package C8+\n");
  6709. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6710. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6711. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  6712. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6713. }
  6714. lpt_disable_clkout_dp(dev);
  6715. hsw_disable_lcpll(dev_priv, true, true);
  6716. }
  6717. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  6718. {
  6719. struct drm_device *dev = dev_priv->dev;
  6720. uint32_t val;
  6721. DRM_DEBUG_KMS("Disabling package C8+\n");
  6722. hsw_restore_lcpll(dev_priv);
  6723. lpt_init_pch_refclk(dev);
  6724. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6725. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6726. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  6727. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6728. }
  6729. intel_prepare_ddi(dev);
  6730. }
  6731. static int haswell_crtc_compute_clock(struct intel_crtc *crtc)
  6732. {
  6733. if (!intel_ddi_pll_select(crtc))
  6734. return -EINVAL;
  6735. crtc->lowfreq_avail = false;
  6736. return 0;
  6737. }
  6738. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  6739. enum port port,
  6740. struct intel_crtc_config *pipe_config)
  6741. {
  6742. u32 temp;
  6743. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  6744. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  6745. switch (pipe_config->ddi_pll_sel) {
  6746. case SKL_DPLL1:
  6747. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  6748. break;
  6749. case SKL_DPLL2:
  6750. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  6751. break;
  6752. case SKL_DPLL3:
  6753. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  6754. break;
  6755. }
  6756. }
  6757. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  6758. enum port port,
  6759. struct intel_crtc_config *pipe_config)
  6760. {
  6761. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  6762. switch (pipe_config->ddi_pll_sel) {
  6763. case PORT_CLK_SEL_WRPLL1:
  6764. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  6765. break;
  6766. case PORT_CLK_SEL_WRPLL2:
  6767. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  6768. break;
  6769. }
  6770. }
  6771. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  6772. struct intel_crtc_config *pipe_config)
  6773. {
  6774. struct drm_device *dev = crtc->base.dev;
  6775. struct drm_i915_private *dev_priv = dev->dev_private;
  6776. struct intel_shared_dpll *pll;
  6777. enum port port;
  6778. uint32_t tmp;
  6779. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  6780. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  6781. if (IS_SKYLAKE(dev))
  6782. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  6783. else
  6784. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  6785. if (pipe_config->shared_dpll >= 0) {
  6786. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6787. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6788. &pipe_config->dpll_hw_state));
  6789. }
  6790. /*
  6791. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  6792. * DDI E. So just check whether this pipe is wired to DDI E and whether
  6793. * the PCH transcoder is on.
  6794. */
  6795. if (INTEL_INFO(dev)->gen < 9 &&
  6796. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  6797. pipe_config->has_pch_encoder = true;
  6798. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  6799. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6800. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6801. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6802. }
  6803. }
  6804. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  6805. struct intel_crtc_config *pipe_config)
  6806. {
  6807. struct drm_device *dev = crtc->base.dev;
  6808. struct drm_i915_private *dev_priv = dev->dev_private;
  6809. enum intel_display_power_domain pfit_domain;
  6810. uint32_t tmp;
  6811. if (!intel_display_power_is_enabled(dev_priv,
  6812. POWER_DOMAIN_PIPE(crtc->pipe)))
  6813. return false;
  6814. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6815. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6816. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  6817. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  6818. enum pipe trans_edp_pipe;
  6819. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  6820. default:
  6821. WARN(1, "unknown pipe linked to edp transcoder\n");
  6822. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  6823. case TRANS_DDI_EDP_INPUT_A_ON:
  6824. trans_edp_pipe = PIPE_A;
  6825. break;
  6826. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  6827. trans_edp_pipe = PIPE_B;
  6828. break;
  6829. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  6830. trans_edp_pipe = PIPE_C;
  6831. break;
  6832. }
  6833. if (trans_edp_pipe == crtc->pipe)
  6834. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  6835. }
  6836. if (!intel_display_power_is_enabled(dev_priv,
  6837. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  6838. return false;
  6839. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  6840. if (!(tmp & PIPECONF_ENABLE))
  6841. return false;
  6842. haswell_get_ddi_port_state(crtc, pipe_config);
  6843. intel_get_pipe_timings(crtc, pipe_config);
  6844. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  6845. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  6846. if (IS_SKYLAKE(dev))
  6847. skylake_get_pfit_config(crtc, pipe_config);
  6848. else
  6849. ironlake_get_pfit_config(crtc, pipe_config);
  6850. }
  6851. if (IS_HASWELL(dev))
  6852. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  6853. (I915_READ(IPS_CTL) & IPS_ENABLE);
  6854. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  6855. pipe_config->pixel_multiplier =
  6856. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  6857. } else {
  6858. pipe_config->pixel_multiplier = 1;
  6859. }
  6860. return true;
  6861. }
  6862. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  6863. {
  6864. struct drm_device *dev = crtc->dev;
  6865. struct drm_i915_private *dev_priv = dev->dev_private;
  6866. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6867. uint32_t cntl = 0, size = 0;
  6868. if (base) {
  6869. unsigned int width = intel_crtc->cursor_width;
  6870. unsigned int height = intel_crtc->cursor_height;
  6871. unsigned int stride = roundup_pow_of_two(width) * 4;
  6872. switch (stride) {
  6873. default:
  6874. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  6875. width, stride);
  6876. stride = 256;
  6877. /* fallthrough */
  6878. case 256:
  6879. case 512:
  6880. case 1024:
  6881. case 2048:
  6882. break;
  6883. }
  6884. cntl |= CURSOR_ENABLE |
  6885. CURSOR_GAMMA_ENABLE |
  6886. CURSOR_FORMAT_ARGB |
  6887. CURSOR_STRIDE(stride);
  6888. size = (height << 12) | width;
  6889. }
  6890. if (intel_crtc->cursor_cntl != 0 &&
  6891. (intel_crtc->cursor_base != base ||
  6892. intel_crtc->cursor_size != size ||
  6893. intel_crtc->cursor_cntl != cntl)) {
  6894. /* On these chipsets we can only modify the base/size/stride
  6895. * whilst the cursor is disabled.
  6896. */
  6897. I915_WRITE(_CURACNTR, 0);
  6898. POSTING_READ(_CURACNTR);
  6899. intel_crtc->cursor_cntl = 0;
  6900. }
  6901. if (intel_crtc->cursor_base != base) {
  6902. I915_WRITE(_CURABASE, base);
  6903. intel_crtc->cursor_base = base;
  6904. }
  6905. if (intel_crtc->cursor_size != size) {
  6906. I915_WRITE(CURSIZE, size);
  6907. intel_crtc->cursor_size = size;
  6908. }
  6909. if (intel_crtc->cursor_cntl != cntl) {
  6910. I915_WRITE(_CURACNTR, cntl);
  6911. POSTING_READ(_CURACNTR);
  6912. intel_crtc->cursor_cntl = cntl;
  6913. }
  6914. }
  6915. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  6916. {
  6917. struct drm_device *dev = crtc->dev;
  6918. struct drm_i915_private *dev_priv = dev->dev_private;
  6919. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6920. int pipe = intel_crtc->pipe;
  6921. uint32_t cntl;
  6922. cntl = 0;
  6923. if (base) {
  6924. cntl = MCURSOR_GAMMA_ENABLE;
  6925. switch (intel_crtc->cursor_width) {
  6926. case 64:
  6927. cntl |= CURSOR_MODE_64_ARGB_AX;
  6928. break;
  6929. case 128:
  6930. cntl |= CURSOR_MODE_128_ARGB_AX;
  6931. break;
  6932. case 256:
  6933. cntl |= CURSOR_MODE_256_ARGB_AX;
  6934. break;
  6935. default:
  6936. WARN_ON(1);
  6937. return;
  6938. }
  6939. cntl |= pipe << 28; /* Connect to correct pipe */
  6940. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  6941. cntl |= CURSOR_PIPE_CSC_ENABLE;
  6942. }
  6943. if (to_intel_plane(crtc->cursor)->rotation == BIT(DRM_ROTATE_180))
  6944. cntl |= CURSOR_ROTATE_180;
  6945. if (intel_crtc->cursor_cntl != cntl) {
  6946. I915_WRITE(CURCNTR(pipe), cntl);
  6947. POSTING_READ(CURCNTR(pipe));
  6948. intel_crtc->cursor_cntl = cntl;
  6949. }
  6950. /* and commit changes on next vblank */
  6951. I915_WRITE(CURBASE(pipe), base);
  6952. POSTING_READ(CURBASE(pipe));
  6953. intel_crtc->cursor_base = base;
  6954. }
  6955. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  6956. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  6957. bool on)
  6958. {
  6959. struct drm_device *dev = crtc->dev;
  6960. struct drm_i915_private *dev_priv = dev->dev_private;
  6961. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6962. int pipe = intel_crtc->pipe;
  6963. int x = crtc->cursor_x;
  6964. int y = crtc->cursor_y;
  6965. u32 base = 0, pos = 0;
  6966. if (on)
  6967. base = intel_crtc->cursor_addr;
  6968. if (x >= intel_crtc->config.pipe_src_w)
  6969. base = 0;
  6970. if (y >= intel_crtc->config.pipe_src_h)
  6971. base = 0;
  6972. if (x < 0) {
  6973. if (x + intel_crtc->cursor_width <= 0)
  6974. base = 0;
  6975. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  6976. x = -x;
  6977. }
  6978. pos |= x << CURSOR_X_SHIFT;
  6979. if (y < 0) {
  6980. if (y + intel_crtc->cursor_height <= 0)
  6981. base = 0;
  6982. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  6983. y = -y;
  6984. }
  6985. pos |= y << CURSOR_Y_SHIFT;
  6986. if (base == 0 && intel_crtc->cursor_base == 0)
  6987. return;
  6988. I915_WRITE(CURPOS(pipe), pos);
  6989. /* ILK+ do this automagically */
  6990. if (HAS_GMCH_DISPLAY(dev) &&
  6991. to_intel_plane(crtc->cursor)->rotation == BIT(DRM_ROTATE_180)) {
  6992. base += (intel_crtc->cursor_height *
  6993. intel_crtc->cursor_width - 1) * 4;
  6994. }
  6995. if (IS_845G(dev) || IS_I865G(dev))
  6996. i845_update_cursor(crtc, base);
  6997. else
  6998. i9xx_update_cursor(crtc, base);
  6999. }
  7000. static bool cursor_size_ok(struct drm_device *dev,
  7001. uint32_t width, uint32_t height)
  7002. {
  7003. if (width == 0 || height == 0)
  7004. return false;
  7005. /*
  7006. * 845g/865g are special in that they are only limited by
  7007. * the width of their cursors, the height is arbitrary up to
  7008. * the precision of the register. Everything else requires
  7009. * square cursors, limited to a few power-of-two sizes.
  7010. */
  7011. if (IS_845G(dev) || IS_I865G(dev)) {
  7012. if ((width & 63) != 0)
  7013. return false;
  7014. if (width > (IS_845G(dev) ? 64 : 512))
  7015. return false;
  7016. if (height > 1023)
  7017. return false;
  7018. } else {
  7019. switch (width | height) {
  7020. case 256:
  7021. case 128:
  7022. if (IS_GEN2(dev))
  7023. return false;
  7024. case 64:
  7025. break;
  7026. default:
  7027. return false;
  7028. }
  7029. }
  7030. return true;
  7031. }
  7032. static int intel_crtc_cursor_set_obj(struct drm_crtc *crtc,
  7033. struct drm_i915_gem_object *obj,
  7034. uint32_t width, uint32_t height)
  7035. {
  7036. struct drm_device *dev = crtc->dev;
  7037. struct drm_i915_private *dev_priv = to_i915(dev);
  7038. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7039. enum pipe pipe = intel_crtc->pipe;
  7040. unsigned old_width;
  7041. uint32_t addr;
  7042. int ret;
  7043. /* if we want to turn off the cursor ignore width and height */
  7044. if (!obj) {
  7045. DRM_DEBUG_KMS("cursor off\n");
  7046. addr = 0;
  7047. mutex_lock(&dev->struct_mutex);
  7048. goto finish;
  7049. }
  7050. /* we only need to pin inside GTT if cursor is non-phy */
  7051. mutex_lock(&dev->struct_mutex);
  7052. if (!INTEL_INFO(dev)->cursor_needs_physical) {
  7053. unsigned alignment;
  7054. /*
  7055. * Global gtt pte registers are special registers which actually
  7056. * forward writes to a chunk of system memory. Which means that
  7057. * there is no risk that the register values disappear as soon
  7058. * as we call intel_runtime_pm_put(), so it is correct to wrap
  7059. * only the pin/unpin/fence and not more.
  7060. */
  7061. intel_runtime_pm_get(dev_priv);
  7062. /* Note that the w/a also requires 2 PTE of padding following
  7063. * the bo. We currently fill all unused PTE with the shadow
  7064. * page and so we should always have valid PTE following the
  7065. * cursor preventing the VT-d warning.
  7066. */
  7067. alignment = 0;
  7068. if (need_vtd_wa(dev))
  7069. alignment = 64*1024;
  7070. ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
  7071. if (ret) {
  7072. DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
  7073. intel_runtime_pm_put(dev_priv);
  7074. goto fail_locked;
  7075. }
  7076. ret = i915_gem_object_put_fence(obj);
  7077. if (ret) {
  7078. DRM_DEBUG_KMS("failed to release fence for cursor");
  7079. intel_runtime_pm_put(dev_priv);
  7080. goto fail_unpin;
  7081. }
  7082. addr = i915_gem_obj_ggtt_offset(obj);
  7083. intel_runtime_pm_put(dev_priv);
  7084. } else {
  7085. int align = IS_I830(dev) ? 16 * 1024 : 256;
  7086. ret = i915_gem_object_attach_phys(obj, align);
  7087. if (ret) {
  7088. DRM_DEBUG_KMS("failed to attach phys object\n");
  7089. goto fail_locked;
  7090. }
  7091. addr = obj->phys_handle->busaddr;
  7092. }
  7093. finish:
  7094. if (intel_crtc->cursor_bo) {
  7095. if (!INTEL_INFO(dev)->cursor_needs_physical)
  7096. i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
  7097. }
  7098. i915_gem_track_fb(intel_crtc->cursor_bo, obj,
  7099. INTEL_FRONTBUFFER_CURSOR(pipe));
  7100. mutex_unlock(&dev->struct_mutex);
  7101. old_width = intel_crtc->cursor_width;
  7102. intel_crtc->cursor_addr = addr;
  7103. intel_crtc->cursor_bo = obj;
  7104. intel_crtc->cursor_width = width;
  7105. intel_crtc->cursor_height = height;
  7106. if (intel_crtc->active) {
  7107. if (old_width != width)
  7108. intel_update_watermarks(crtc);
  7109. intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
  7110. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_CURSOR(pipe));
  7111. }
  7112. return 0;
  7113. fail_unpin:
  7114. i915_gem_object_unpin_from_display_plane(obj);
  7115. fail_locked:
  7116. mutex_unlock(&dev->struct_mutex);
  7117. return ret;
  7118. }
  7119. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  7120. u16 *blue, uint32_t start, uint32_t size)
  7121. {
  7122. int end = (start + size > 256) ? 256 : start + size, i;
  7123. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7124. for (i = start; i < end; i++) {
  7125. intel_crtc->lut_r[i] = red[i] >> 8;
  7126. intel_crtc->lut_g[i] = green[i] >> 8;
  7127. intel_crtc->lut_b[i] = blue[i] >> 8;
  7128. }
  7129. intel_crtc_load_lut(crtc);
  7130. }
  7131. /* VESA 640x480x72Hz mode to set on the pipe */
  7132. static struct drm_display_mode load_detect_mode = {
  7133. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  7134. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  7135. };
  7136. struct drm_framebuffer *
  7137. __intel_framebuffer_create(struct drm_device *dev,
  7138. struct drm_mode_fb_cmd2 *mode_cmd,
  7139. struct drm_i915_gem_object *obj)
  7140. {
  7141. struct intel_framebuffer *intel_fb;
  7142. int ret;
  7143. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7144. if (!intel_fb) {
  7145. drm_gem_object_unreference(&obj->base);
  7146. return ERR_PTR(-ENOMEM);
  7147. }
  7148. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  7149. if (ret)
  7150. goto err;
  7151. return &intel_fb->base;
  7152. err:
  7153. drm_gem_object_unreference(&obj->base);
  7154. kfree(intel_fb);
  7155. return ERR_PTR(ret);
  7156. }
  7157. static struct drm_framebuffer *
  7158. intel_framebuffer_create(struct drm_device *dev,
  7159. struct drm_mode_fb_cmd2 *mode_cmd,
  7160. struct drm_i915_gem_object *obj)
  7161. {
  7162. struct drm_framebuffer *fb;
  7163. int ret;
  7164. ret = i915_mutex_lock_interruptible(dev);
  7165. if (ret)
  7166. return ERR_PTR(ret);
  7167. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  7168. mutex_unlock(&dev->struct_mutex);
  7169. return fb;
  7170. }
  7171. static u32
  7172. intel_framebuffer_pitch_for_width(int width, int bpp)
  7173. {
  7174. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  7175. return ALIGN(pitch, 64);
  7176. }
  7177. static u32
  7178. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  7179. {
  7180. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  7181. return PAGE_ALIGN(pitch * mode->vdisplay);
  7182. }
  7183. static struct drm_framebuffer *
  7184. intel_framebuffer_create_for_mode(struct drm_device *dev,
  7185. struct drm_display_mode *mode,
  7186. int depth, int bpp)
  7187. {
  7188. struct drm_i915_gem_object *obj;
  7189. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  7190. obj = i915_gem_alloc_object(dev,
  7191. intel_framebuffer_size_for_mode(mode, bpp));
  7192. if (obj == NULL)
  7193. return ERR_PTR(-ENOMEM);
  7194. mode_cmd.width = mode->hdisplay;
  7195. mode_cmd.height = mode->vdisplay;
  7196. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  7197. bpp);
  7198. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  7199. return intel_framebuffer_create(dev, &mode_cmd, obj);
  7200. }
  7201. static struct drm_framebuffer *
  7202. mode_fits_in_fbdev(struct drm_device *dev,
  7203. struct drm_display_mode *mode)
  7204. {
  7205. #ifdef CONFIG_DRM_I915_FBDEV
  7206. struct drm_i915_private *dev_priv = dev->dev_private;
  7207. struct drm_i915_gem_object *obj;
  7208. struct drm_framebuffer *fb;
  7209. if (!dev_priv->fbdev)
  7210. return NULL;
  7211. if (!dev_priv->fbdev->fb)
  7212. return NULL;
  7213. obj = dev_priv->fbdev->fb->obj;
  7214. BUG_ON(!obj);
  7215. fb = &dev_priv->fbdev->fb->base;
  7216. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  7217. fb->bits_per_pixel))
  7218. return NULL;
  7219. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  7220. return NULL;
  7221. return fb;
  7222. #else
  7223. return NULL;
  7224. #endif
  7225. }
  7226. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  7227. struct drm_display_mode *mode,
  7228. struct intel_load_detect_pipe *old,
  7229. struct drm_modeset_acquire_ctx *ctx)
  7230. {
  7231. struct intel_crtc *intel_crtc;
  7232. struct intel_encoder *intel_encoder =
  7233. intel_attached_encoder(connector);
  7234. struct drm_crtc *possible_crtc;
  7235. struct drm_encoder *encoder = &intel_encoder->base;
  7236. struct drm_crtc *crtc = NULL;
  7237. struct drm_device *dev = encoder->dev;
  7238. struct drm_framebuffer *fb;
  7239. struct drm_mode_config *config = &dev->mode_config;
  7240. int ret, i = -1;
  7241. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7242. connector->base.id, connector->name,
  7243. encoder->base.id, encoder->name);
  7244. retry:
  7245. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  7246. if (ret)
  7247. goto fail_unlock;
  7248. /*
  7249. * Algorithm gets a little messy:
  7250. *
  7251. * - if the connector already has an assigned crtc, use it (but make
  7252. * sure it's on first)
  7253. *
  7254. * - try to find the first unused crtc that can drive this connector,
  7255. * and use that if we find one
  7256. */
  7257. /* See if we already have a CRTC for this connector */
  7258. if (encoder->crtc) {
  7259. crtc = encoder->crtc;
  7260. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7261. if (ret)
  7262. goto fail_unlock;
  7263. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  7264. if (ret)
  7265. goto fail_unlock;
  7266. old->dpms_mode = connector->dpms;
  7267. old->load_detect_temp = false;
  7268. /* Make sure the crtc and connector are running */
  7269. if (connector->dpms != DRM_MODE_DPMS_ON)
  7270. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  7271. return true;
  7272. }
  7273. /* Find an unused one (if possible) */
  7274. for_each_crtc(dev, possible_crtc) {
  7275. i++;
  7276. if (!(encoder->possible_crtcs & (1 << i)))
  7277. continue;
  7278. if (possible_crtc->enabled)
  7279. continue;
  7280. /* This can occur when applying the pipe A quirk on resume. */
  7281. if (to_intel_crtc(possible_crtc)->new_enabled)
  7282. continue;
  7283. crtc = possible_crtc;
  7284. break;
  7285. }
  7286. /*
  7287. * If we didn't find an unused CRTC, don't use any.
  7288. */
  7289. if (!crtc) {
  7290. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  7291. goto fail_unlock;
  7292. }
  7293. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7294. if (ret)
  7295. goto fail_unlock;
  7296. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  7297. if (ret)
  7298. goto fail_unlock;
  7299. intel_encoder->new_crtc = to_intel_crtc(crtc);
  7300. to_intel_connector(connector)->new_encoder = intel_encoder;
  7301. intel_crtc = to_intel_crtc(crtc);
  7302. intel_crtc->new_enabled = true;
  7303. intel_crtc->new_config = &intel_crtc->config;
  7304. old->dpms_mode = connector->dpms;
  7305. old->load_detect_temp = true;
  7306. old->release_fb = NULL;
  7307. if (!mode)
  7308. mode = &load_detect_mode;
  7309. /* We need a framebuffer large enough to accommodate all accesses
  7310. * that the plane may generate whilst we perform load detection.
  7311. * We can not rely on the fbcon either being present (we get called
  7312. * during its initialisation to detect all boot displays, or it may
  7313. * not even exist) or that it is large enough to satisfy the
  7314. * requested mode.
  7315. */
  7316. fb = mode_fits_in_fbdev(dev, mode);
  7317. if (fb == NULL) {
  7318. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  7319. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  7320. old->release_fb = fb;
  7321. } else
  7322. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  7323. if (IS_ERR(fb)) {
  7324. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  7325. goto fail;
  7326. }
  7327. if (intel_set_mode(crtc, mode, 0, 0, fb)) {
  7328. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  7329. if (old->release_fb)
  7330. old->release_fb->funcs->destroy(old->release_fb);
  7331. goto fail;
  7332. }
  7333. /* let the connector get through one full cycle before testing */
  7334. intel_wait_for_vblank(dev, intel_crtc->pipe);
  7335. return true;
  7336. fail:
  7337. intel_crtc->new_enabled = crtc->enabled;
  7338. if (intel_crtc->new_enabled)
  7339. intel_crtc->new_config = &intel_crtc->config;
  7340. else
  7341. intel_crtc->new_config = NULL;
  7342. fail_unlock:
  7343. if (ret == -EDEADLK) {
  7344. drm_modeset_backoff(ctx);
  7345. goto retry;
  7346. }
  7347. return false;
  7348. }
  7349. void intel_release_load_detect_pipe(struct drm_connector *connector,
  7350. struct intel_load_detect_pipe *old)
  7351. {
  7352. struct intel_encoder *intel_encoder =
  7353. intel_attached_encoder(connector);
  7354. struct drm_encoder *encoder = &intel_encoder->base;
  7355. struct drm_crtc *crtc = encoder->crtc;
  7356. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7357. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7358. connector->base.id, connector->name,
  7359. encoder->base.id, encoder->name);
  7360. if (old->load_detect_temp) {
  7361. to_intel_connector(connector)->new_encoder = NULL;
  7362. intel_encoder->new_crtc = NULL;
  7363. intel_crtc->new_enabled = false;
  7364. intel_crtc->new_config = NULL;
  7365. intel_set_mode(crtc, NULL, 0, 0, NULL);
  7366. if (old->release_fb) {
  7367. drm_framebuffer_unregister_private(old->release_fb);
  7368. drm_framebuffer_unreference(old->release_fb);
  7369. }
  7370. return;
  7371. }
  7372. /* Switch crtc and encoder back off if necessary */
  7373. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  7374. connector->funcs->dpms(connector, old->dpms_mode);
  7375. }
  7376. static int i9xx_pll_refclk(struct drm_device *dev,
  7377. const struct intel_crtc_config *pipe_config)
  7378. {
  7379. struct drm_i915_private *dev_priv = dev->dev_private;
  7380. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7381. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  7382. return dev_priv->vbt.lvds_ssc_freq;
  7383. else if (HAS_PCH_SPLIT(dev))
  7384. return 120000;
  7385. else if (!IS_GEN2(dev))
  7386. return 96000;
  7387. else
  7388. return 48000;
  7389. }
  7390. /* Returns the clock of the currently programmed mode of the given pipe. */
  7391. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  7392. struct intel_crtc_config *pipe_config)
  7393. {
  7394. struct drm_device *dev = crtc->base.dev;
  7395. struct drm_i915_private *dev_priv = dev->dev_private;
  7396. int pipe = pipe_config->cpu_transcoder;
  7397. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7398. u32 fp;
  7399. intel_clock_t clock;
  7400. int refclk = i9xx_pll_refclk(dev, pipe_config);
  7401. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  7402. fp = pipe_config->dpll_hw_state.fp0;
  7403. else
  7404. fp = pipe_config->dpll_hw_state.fp1;
  7405. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  7406. if (IS_PINEVIEW(dev)) {
  7407. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  7408. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7409. } else {
  7410. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  7411. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7412. }
  7413. if (!IS_GEN2(dev)) {
  7414. if (IS_PINEVIEW(dev))
  7415. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  7416. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  7417. else
  7418. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  7419. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7420. switch (dpll & DPLL_MODE_MASK) {
  7421. case DPLLB_MODE_DAC_SERIAL:
  7422. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  7423. 5 : 10;
  7424. break;
  7425. case DPLLB_MODE_LVDS:
  7426. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  7427. 7 : 14;
  7428. break;
  7429. default:
  7430. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  7431. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  7432. return;
  7433. }
  7434. if (IS_PINEVIEW(dev))
  7435. pineview_clock(refclk, &clock);
  7436. else
  7437. i9xx_clock(refclk, &clock);
  7438. } else {
  7439. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  7440. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  7441. if (is_lvds) {
  7442. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  7443. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7444. if (lvds & LVDS_CLKB_POWER_UP)
  7445. clock.p2 = 7;
  7446. else
  7447. clock.p2 = 14;
  7448. } else {
  7449. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  7450. clock.p1 = 2;
  7451. else {
  7452. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  7453. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  7454. }
  7455. if (dpll & PLL_P2_DIVIDE_BY_4)
  7456. clock.p2 = 4;
  7457. else
  7458. clock.p2 = 2;
  7459. }
  7460. i9xx_clock(refclk, &clock);
  7461. }
  7462. /*
  7463. * This value includes pixel_multiplier. We will use
  7464. * port_clock to compute adjusted_mode.crtc_clock in the
  7465. * encoder's get_config() function.
  7466. */
  7467. pipe_config->port_clock = clock.dot;
  7468. }
  7469. int intel_dotclock_calculate(int link_freq,
  7470. const struct intel_link_m_n *m_n)
  7471. {
  7472. /*
  7473. * The calculation for the data clock is:
  7474. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  7475. * But we want to avoid losing precison if possible, so:
  7476. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  7477. *
  7478. * and the link clock is simpler:
  7479. * link_clock = (m * link_clock) / n
  7480. */
  7481. if (!m_n->link_n)
  7482. return 0;
  7483. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  7484. }
  7485. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  7486. struct intel_crtc_config *pipe_config)
  7487. {
  7488. struct drm_device *dev = crtc->base.dev;
  7489. /* read out port_clock from the DPLL */
  7490. i9xx_crtc_clock_get(crtc, pipe_config);
  7491. /*
  7492. * This value does not include pixel_multiplier.
  7493. * We will check that port_clock and adjusted_mode.crtc_clock
  7494. * agree once we know their relationship in the encoder's
  7495. * get_config() function.
  7496. */
  7497. pipe_config->adjusted_mode.crtc_clock =
  7498. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  7499. &pipe_config->fdi_m_n);
  7500. }
  7501. /** Returns the currently programmed mode of the given pipe. */
  7502. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  7503. struct drm_crtc *crtc)
  7504. {
  7505. struct drm_i915_private *dev_priv = dev->dev_private;
  7506. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7507. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  7508. struct drm_display_mode *mode;
  7509. struct intel_crtc_config pipe_config;
  7510. int htot = I915_READ(HTOTAL(cpu_transcoder));
  7511. int hsync = I915_READ(HSYNC(cpu_transcoder));
  7512. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  7513. int vsync = I915_READ(VSYNC(cpu_transcoder));
  7514. enum pipe pipe = intel_crtc->pipe;
  7515. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  7516. if (!mode)
  7517. return NULL;
  7518. /*
  7519. * Construct a pipe_config sufficient for getting the clock info
  7520. * back out of crtc_clock_get.
  7521. *
  7522. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  7523. * to use a real value here instead.
  7524. */
  7525. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  7526. pipe_config.pixel_multiplier = 1;
  7527. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  7528. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  7529. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  7530. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  7531. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  7532. mode->hdisplay = (htot & 0xffff) + 1;
  7533. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  7534. mode->hsync_start = (hsync & 0xffff) + 1;
  7535. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  7536. mode->vdisplay = (vtot & 0xffff) + 1;
  7537. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  7538. mode->vsync_start = (vsync & 0xffff) + 1;
  7539. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  7540. drm_mode_set_name(mode);
  7541. return mode;
  7542. }
  7543. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  7544. {
  7545. struct drm_device *dev = crtc->dev;
  7546. struct drm_i915_private *dev_priv = dev->dev_private;
  7547. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7548. if (!HAS_GMCH_DISPLAY(dev))
  7549. return;
  7550. if (!dev_priv->lvds_downclock_avail)
  7551. return;
  7552. /*
  7553. * Since this is called by a timer, we should never get here in
  7554. * the manual case.
  7555. */
  7556. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  7557. int pipe = intel_crtc->pipe;
  7558. int dpll_reg = DPLL(pipe);
  7559. int dpll;
  7560. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  7561. assert_panel_unlocked(dev_priv, pipe);
  7562. dpll = I915_READ(dpll_reg);
  7563. dpll |= DISPLAY_RATE_SELECT_FPA1;
  7564. I915_WRITE(dpll_reg, dpll);
  7565. intel_wait_for_vblank(dev, pipe);
  7566. dpll = I915_READ(dpll_reg);
  7567. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  7568. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  7569. }
  7570. }
  7571. void intel_mark_busy(struct drm_device *dev)
  7572. {
  7573. struct drm_i915_private *dev_priv = dev->dev_private;
  7574. if (dev_priv->mm.busy)
  7575. return;
  7576. intel_runtime_pm_get(dev_priv);
  7577. i915_update_gfx_val(dev_priv);
  7578. dev_priv->mm.busy = true;
  7579. }
  7580. void intel_mark_idle(struct drm_device *dev)
  7581. {
  7582. struct drm_i915_private *dev_priv = dev->dev_private;
  7583. struct drm_crtc *crtc;
  7584. if (!dev_priv->mm.busy)
  7585. return;
  7586. dev_priv->mm.busy = false;
  7587. if (!i915.powersave)
  7588. goto out;
  7589. for_each_crtc(dev, crtc) {
  7590. if (!crtc->primary->fb)
  7591. continue;
  7592. intel_decrease_pllclock(crtc);
  7593. }
  7594. if (INTEL_INFO(dev)->gen >= 6)
  7595. gen6_rps_idle(dev->dev_private);
  7596. out:
  7597. intel_runtime_pm_put(dev_priv);
  7598. }
  7599. static void intel_crtc_destroy(struct drm_crtc *crtc)
  7600. {
  7601. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7602. struct drm_device *dev = crtc->dev;
  7603. struct intel_unpin_work *work;
  7604. spin_lock_irq(&dev->event_lock);
  7605. work = intel_crtc->unpin_work;
  7606. intel_crtc->unpin_work = NULL;
  7607. spin_unlock_irq(&dev->event_lock);
  7608. if (work) {
  7609. cancel_work_sync(&work->work);
  7610. kfree(work);
  7611. }
  7612. drm_crtc_cleanup(crtc);
  7613. kfree(intel_crtc);
  7614. }
  7615. static void intel_unpin_work_fn(struct work_struct *__work)
  7616. {
  7617. struct intel_unpin_work *work =
  7618. container_of(__work, struct intel_unpin_work, work);
  7619. struct drm_device *dev = work->crtc->dev;
  7620. enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
  7621. mutex_lock(&dev->struct_mutex);
  7622. intel_unpin_fb_obj(work->old_fb_obj);
  7623. drm_gem_object_unreference(&work->pending_flip_obj->base);
  7624. drm_gem_object_unreference(&work->old_fb_obj->base);
  7625. intel_update_fbc(dev);
  7626. mutex_unlock(&dev->struct_mutex);
  7627. intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  7628. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  7629. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  7630. kfree(work);
  7631. }
  7632. static void do_intel_finish_page_flip(struct drm_device *dev,
  7633. struct drm_crtc *crtc)
  7634. {
  7635. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7636. struct intel_unpin_work *work;
  7637. unsigned long flags;
  7638. /* Ignore early vblank irqs */
  7639. if (intel_crtc == NULL)
  7640. return;
  7641. /*
  7642. * This is called both by irq handlers and the reset code (to complete
  7643. * lost pageflips) so needs the full irqsave spinlocks.
  7644. */
  7645. spin_lock_irqsave(&dev->event_lock, flags);
  7646. work = intel_crtc->unpin_work;
  7647. /* Ensure we don't miss a work->pending update ... */
  7648. smp_rmb();
  7649. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  7650. spin_unlock_irqrestore(&dev->event_lock, flags);
  7651. return;
  7652. }
  7653. page_flip_completed(intel_crtc);
  7654. spin_unlock_irqrestore(&dev->event_lock, flags);
  7655. }
  7656. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  7657. {
  7658. struct drm_i915_private *dev_priv = dev->dev_private;
  7659. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  7660. do_intel_finish_page_flip(dev, crtc);
  7661. }
  7662. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  7663. {
  7664. struct drm_i915_private *dev_priv = dev->dev_private;
  7665. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  7666. do_intel_finish_page_flip(dev, crtc);
  7667. }
  7668. /* Is 'a' after or equal to 'b'? */
  7669. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  7670. {
  7671. return !((a - b) & 0x80000000);
  7672. }
  7673. static bool page_flip_finished(struct intel_crtc *crtc)
  7674. {
  7675. struct drm_device *dev = crtc->base.dev;
  7676. struct drm_i915_private *dev_priv = dev->dev_private;
  7677. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  7678. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  7679. return true;
  7680. /*
  7681. * The relevant registers doen't exist on pre-ctg.
  7682. * As the flip done interrupt doesn't trigger for mmio
  7683. * flips on gmch platforms, a flip count check isn't
  7684. * really needed there. But since ctg has the registers,
  7685. * include it in the check anyway.
  7686. */
  7687. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  7688. return true;
  7689. /*
  7690. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  7691. * used the same base address. In that case the mmio flip might
  7692. * have completed, but the CS hasn't even executed the flip yet.
  7693. *
  7694. * A flip count check isn't enough as the CS might have updated
  7695. * the base address just after start of vblank, but before we
  7696. * managed to process the interrupt. This means we'd complete the
  7697. * CS flip too soon.
  7698. *
  7699. * Combining both checks should get us a good enough result. It may
  7700. * still happen that the CS flip has been executed, but has not
  7701. * yet actually completed. But in case the base address is the same
  7702. * anyway, we don't really care.
  7703. */
  7704. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  7705. crtc->unpin_work->gtt_offset &&
  7706. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  7707. crtc->unpin_work->flip_count);
  7708. }
  7709. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  7710. {
  7711. struct drm_i915_private *dev_priv = dev->dev_private;
  7712. struct intel_crtc *intel_crtc =
  7713. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  7714. unsigned long flags;
  7715. /*
  7716. * This is called both by irq handlers and the reset code (to complete
  7717. * lost pageflips) so needs the full irqsave spinlocks.
  7718. *
  7719. * NB: An MMIO update of the plane base pointer will also
  7720. * generate a page-flip completion irq, i.e. every modeset
  7721. * is also accompanied by a spurious intel_prepare_page_flip().
  7722. */
  7723. spin_lock_irqsave(&dev->event_lock, flags);
  7724. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  7725. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  7726. spin_unlock_irqrestore(&dev->event_lock, flags);
  7727. }
  7728. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  7729. {
  7730. /* Ensure that the work item is consistent when activating it ... */
  7731. smp_wmb();
  7732. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  7733. /* and that it is marked active as soon as the irq could fire. */
  7734. smp_wmb();
  7735. }
  7736. static int intel_gen2_queue_flip(struct drm_device *dev,
  7737. struct drm_crtc *crtc,
  7738. struct drm_framebuffer *fb,
  7739. struct drm_i915_gem_object *obj,
  7740. struct intel_engine_cs *ring,
  7741. uint32_t flags)
  7742. {
  7743. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7744. u32 flip_mask;
  7745. int ret;
  7746. ret = intel_ring_begin(ring, 6);
  7747. if (ret)
  7748. return ret;
  7749. /* Can't queue multiple flips, so wait for the previous
  7750. * one to finish before executing the next.
  7751. */
  7752. if (intel_crtc->plane)
  7753. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7754. else
  7755. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7756. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7757. intel_ring_emit(ring, MI_NOOP);
  7758. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7759. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7760. intel_ring_emit(ring, fb->pitches[0]);
  7761. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7762. intel_ring_emit(ring, 0); /* aux display base address, unused */
  7763. intel_mark_page_flip_active(intel_crtc);
  7764. __intel_ring_advance(ring);
  7765. return 0;
  7766. }
  7767. static int intel_gen3_queue_flip(struct drm_device *dev,
  7768. struct drm_crtc *crtc,
  7769. struct drm_framebuffer *fb,
  7770. struct drm_i915_gem_object *obj,
  7771. struct intel_engine_cs *ring,
  7772. uint32_t flags)
  7773. {
  7774. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7775. u32 flip_mask;
  7776. int ret;
  7777. ret = intel_ring_begin(ring, 6);
  7778. if (ret)
  7779. return ret;
  7780. if (intel_crtc->plane)
  7781. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7782. else
  7783. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7784. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7785. intel_ring_emit(ring, MI_NOOP);
  7786. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  7787. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7788. intel_ring_emit(ring, fb->pitches[0]);
  7789. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7790. intel_ring_emit(ring, MI_NOOP);
  7791. intel_mark_page_flip_active(intel_crtc);
  7792. __intel_ring_advance(ring);
  7793. return 0;
  7794. }
  7795. static int intel_gen4_queue_flip(struct drm_device *dev,
  7796. struct drm_crtc *crtc,
  7797. struct drm_framebuffer *fb,
  7798. struct drm_i915_gem_object *obj,
  7799. struct intel_engine_cs *ring,
  7800. uint32_t flags)
  7801. {
  7802. struct drm_i915_private *dev_priv = dev->dev_private;
  7803. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7804. uint32_t pf, pipesrc;
  7805. int ret;
  7806. ret = intel_ring_begin(ring, 4);
  7807. if (ret)
  7808. return ret;
  7809. /* i965+ uses the linear or tiled offsets from the
  7810. * Display Registers (which do not change across a page-flip)
  7811. * so we need only reprogram the base address.
  7812. */
  7813. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7814. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7815. intel_ring_emit(ring, fb->pitches[0]);
  7816. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  7817. obj->tiling_mode);
  7818. /* XXX Enabling the panel-fitter across page-flip is so far
  7819. * untested on non-native modes, so ignore it for now.
  7820. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  7821. */
  7822. pf = 0;
  7823. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7824. intel_ring_emit(ring, pf | pipesrc);
  7825. intel_mark_page_flip_active(intel_crtc);
  7826. __intel_ring_advance(ring);
  7827. return 0;
  7828. }
  7829. static int intel_gen6_queue_flip(struct drm_device *dev,
  7830. struct drm_crtc *crtc,
  7831. struct drm_framebuffer *fb,
  7832. struct drm_i915_gem_object *obj,
  7833. struct intel_engine_cs *ring,
  7834. uint32_t flags)
  7835. {
  7836. struct drm_i915_private *dev_priv = dev->dev_private;
  7837. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7838. uint32_t pf, pipesrc;
  7839. int ret;
  7840. ret = intel_ring_begin(ring, 4);
  7841. if (ret)
  7842. return ret;
  7843. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7844. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7845. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  7846. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7847. /* Contrary to the suggestions in the documentation,
  7848. * "Enable Panel Fitter" does not seem to be required when page
  7849. * flipping with a non-native mode, and worse causes a normal
  7850. * modeset to fail.
  7851. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  7852. */
  7853. pf = 0;
  7854. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7855. intel_ring_emit(ring, pf | pipesrc);
  7856. intel_mark_page_flip_active(intel_crtc);
  7857. __intel_ring_advance(ring);
  7858. return 0;
  7859. }
  7860. static int intel_gen7_queue_flip(struct drm_device *dev,
  7861. struct drm_crtc *crtc,
  7862. struct drm_framebuffer *fb,
  7863. struct drm_i915_gem_object *obj,
  7864. struct intel_engine_cs *ring,
  7865. uint32_t flags)
  7866. {
  7867. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7868. uint32_t plane_bit = 0;
  7869. int len, ret;
  7870. switch (intel_crtc->plane) {
  7871. case PLANE_A:
  7872. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  7873. break;
  7874. case PLANE_B:
  7875. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  7876. break;
  7877. case PLANE_C:
  7878. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  7879. break;
  7880. default:
  7881. WARN_ONCE(1, "unknown plane in flip command\n");
  7882. return -ENODEV;
  7883. }
  7884. len = 4;
  7885. if (ring->id == RCS) {
  7886. len += 6;
  7887. /*
  7888. * On Gen 8, SRM is now taking an extra dword to accommodate
  7889. * 48bits addresses, and we need a NOOP for the batch size to
  7890. * stay even.
  7891. */
  7892. if (IS_GEN8(dev))
  7893. len += 2;
  7894. }
  7895. /*
  7896. * BSpec MI_DISPLAY_FLIP for IVB:
  7897. * "The full packet must be contained within the same cache line."
  7898. *
  7899. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  7900. * cacheline, if we ever start emitting more commands before
  7901. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  7902. * then do the cacheline alignment, and finally emit the
  7903. * MI_DISPLAY_FLIP.
  7904. */
  7905. ret = intel_ring_cacheline_align(ring);
  7906. if (ret)
  7907. return ret;
  7908. ret = intel_ring_begin(ring, len);
  7909. if (ret)
  7910. return ret;
  7911. /* Unmask the flip-done completion message. Note that the bspec says that
  7912. * we should do this for both the BCS and RCS, and that we must not unmask
  7913. * more than one flip event at any time (or ensure that one flip message
  7914. * can be sent by waiting for flip-done prior to queueing new flips).
  7915. * Experimentation says that BCS works despite DERRMR masking all
  7916. * flip-done completion events and that unmasking all planes at once
  7917. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  7918. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  7919. */
  7920. if (ring->id == RCS) {
  7921. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  7922. intel_ring_emit(ring, DERRMR);
  7923. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  7924. DERRMR_PIPEB_PRI_FLIP_DONE |
  7925. DERRMR_PIPEC_PRI_FLIP_DONE));
  7926. if (IS_GEN8(dev))
  7927. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  7928. MI_SRM_LRM_GLOBAL_GTT);
  7929. else
  7930. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  7931. MI_SRM_LRM_GLOBAL_GTT);
  7932. intel_ring_emit(ring, DERRMR);
  7933. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  7934. if (IS_GEN8(dev)) {
  7935. intel_ring_emit(ring, 0);
  7936. intel_ring_emit(ring, MI_NOOP);
  7937. }
  7938. }
  7939. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  7940. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  7941. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7942. intel_ring_emit(ring, (MI_NOOP));
  7943. intel_mark_page_flip_active(intel_crtc);
  7944. __intel_ring_advance(ring);
  7945. return 0;
  7946. }
  7947. static bool use_mmio_flip(struct intel_engine_cs *ring,
  7948. struct drm_i915_gem_object *obj)
  7949. {
  7950. /*
  7951. * This is not being used for older platforms, because
  7952. * non-availability of flip done interrupt forces us to use
  7953. * CS flips. Older platforms derive flip done using some clever
  7954. * tricks involving the flip_pending status bits and vblank irqs.
  7955. * So using MMIO flips there would disrupt this mechanism.
  7956. */
  7957. if (ring == NULL)
  7958. return true;
  7959. if (INTEL_INFO(ring->dev)->gen < 5)
  7960. return false;
  7961. if (i915.use_mmio_flip < 0)
  7962. return false;
  7963. else if (i915.use_mmio_flip > 0)
  7964. return true;
  7965. else if (i915.enable_execlists)
  7966. return true;
  7967. else
  7968. return ring != obj->ring;
  7969. }
  7970. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  7971. {
  7972. struct drm_device *dev = intel_crtc->base.dev;
  7973. struct drm_i915_private *dev_priv = dev->dev_private;
  7974. struct intel_framebuffer *intel_fb =
  7975. to_intel_framebuffer(intel_crtc->base.primary->fb);
  7976. struct drm_i915_gem_object *obj = intel_fb->obj;
  7977. bool atomic_update;
  7978. u32 start_vbl_count;
  7979. u32 dspcntr;
  7980. u32 reg;
  7981. intel_mark_page_flip_active(intel_crtc);
  7982. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  7983. reg = DSPCNTR(intel_crtc->plane);
  7984. dspcntr = I915_READ(reg);
  7985. if (obj->tiling_mode != I915_TILING_NONE)
  7986. dspcntr |= DISPPLANE_TILED;
  7987. else
  7988. dspcntr &= ~DISPPLANE_TILED;
  7989. I915_WRITE(reg, dspcntr);
  7990. I915_WRITE(DSPSURF(intel_crtc->plane),
  7991. intel_crtc->unpin_work->gtt_offset);
  7992. POSTING_READ(DSPSURF(intel_crtc->plane));
  7993. if (atomic_update)
  7994. intel_pipe_update_end(intel_crtc, start_vbl_count);
  7995. }
  7996. static void intel_mmio_flip_work_func(struct work_struct *work)
  7997. {
  7998. struct intel_crtc *intel_crtc =
  7999. container_of(work, struct intel_crtc, mmio_flip.work);
  8000. struct intel_engine_cs *ring;
  8001. uint32_t seqno;
  8002. seqno = intel_crtc->mmio_flip.seqno;
  8003. ring = intel_crtc->mmio_flip.ring;
  8004. if (seqno)
  8005. WARN_ON(__i915_wait_seqno(ring, seqno,
  8006. intel_crtc->reset_counter,
  8007. false, NULL, NULL) != 0);
  8008. intel_do_mmio_flip(intel_crtc);
  8009. }
  8010. static int intel_queue_mmio_flip(struct drm_device *dev,
  8011. struct drm_crtc *crtc,
  8012. struct drm_framebuffer *fb,
  8013. struct drm_i915_gem_object *obj,
  8014. struct intel_engine_cs *ring,
  8015. uint32_t flags)
  8016. {
  8017. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8018. intel_crtc->mmio_flip.seqno = obj->last_write_seqno;
  8019. intel_crtc->mmio_flip.ring = obj->ring;
  8020. schedule_work(&intel_crtc->mmio_flip.work);
  8021. return 0;
  8022. }
  8023. static int intel_gen9_queue_flip(struct drm_device *dev,
  8024. struct drm_crtc *crtc,
  8025. struct drm_framebuffer *fb,
  8026. struct drm_i915_gem_object *obj,
  8027. struct intel_engine_cs *ring,
  8028. uint32_t flags)
  8029. {
  8030. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8031. uint32_t plane = 0, stride;
  8032. int ret;
  8033. switch(intel_crtc->pipe) {
  8034. case PIPE_A:
  8035. plane = MI_DISPLAY_FLIP_SKL_PLANE_1_A;
  8036. break;
  8037. case PIPE_B:
  8038. plane = MI_DISPLAY_FLIP_SKL_PLANE_1_B;
  8039. break;
  8040. case PIPE_C:
  8041. plane = MI_DISPLAY_FLIP_SKL_PLANE_1_C;
  8042. break;
  8043. default:
  8044. WARN_ONCE(1, "unknown plane in flip command\n");
  8045. return -ENODEV;
  8046. }
  8047. switch (obj->tiling_mode) {
  8048. case I915_TILING_NONE:
  8049. stride = fb->pitches[0] >> 6;
  8050. break;
  8051. case I915_TILING_X:
  8052. stride = fb->pitches[0] >> 9;
  8053. break;
  8054. default:
  8055. WARN_ONCE(1, "unknown tiling in flip command\n");
  8056. return -ENODEV;
  8057. }
  8058. ret = intel_ring_begin(ring, 10);
  8059. if (ret)
  8060. return ret;
  8061. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  8062. intel_ring_emit(ring, DERRMR);
  8063. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  8064. DERRMR_PIPEB_PRI_FLIP_DONE |
  8065. DERRMR_PIPEC_PRI_FLIP_DONE));
  8066. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  8067. MI_SRM_LRM_GLOBAL_GTT);
  8068. intel_ring_emit(ring, DERRMR);
  8069. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  8070. intel_ring_emit(ring, 0);
  8071. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane);
  8072. intel_ring_emit(ring, stride << 6 | obj->tiling_mode);
  8073. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8074. intel_mark_page_flip_active(intel_crtc);
  8075. __intel_ring_advance(ring);
  8076. return 0;
  8077. }
  8078. static int intel_default_queue_flip(struct drm_device *dev,
  8079. struct drm_crtc *crtc,
  8080. struct drm_framebuffer *fb,
  8081. struct drm_i915_gem_object *obj,
  8082. struct intel_engine_cs *ring,
  8083. uint32_t flags)
  8084. {
  8085. return -ENODEV;
  8086. }
  8087. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  8088. struct drm_crtc *crtc)
  8089. {
  8090. struct drm_i915_private *dev_priv = dev->dev_private;
  8091. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8092. struct intel_unpin_work *work = intel_crtc->unpin_work;
  8093. u32 addr;
  8094. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  8095. return true;
  8096. if (!work->enable_stall_check)
  8097. return false;
  8098. if (work->flip_ready_vblank == 0) {
  8099. if (work->flip_queued_ring &&
  8100. !i915_seqno_passed(work->flip_queued_ring->get_seqno(work->flip_queued_ring, true),
  8101. work->flip_queued_seqno))
  8102. return false;
  8103. work->flip_ready_vblank = drm_vblank_count(dev, intel_crtc->pipe);
  8104. }
  8105. if (drm_vblank_count(dev, intel_crtc->pipe) - work->flip_ready_vblank < 3)
  8106. return false;
  8107. /* Potential stall - if we see that the flip has happened,
  8108. * assume a missed interrupt. */
  8109. if (INTEL_INFO(dev)->gen >= 4)
  8110. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  8111. else
  8112. addr = I915_READ(DSPADDR(intel_crtc->plane));
  8113. /* There is a potential issue here with a false positive after a flip
  8114. * to the same address. We could address this by checking for a
  8115. * non-incrementing frame counter.
  8116. */
  8117. return addr == work->gtt_offset;
  8118. }
  8119. void intel_check_page_flip(struct drm_device *dev, int pipe)
  8120. {
  8121. struct drm_i915_private *dev_priv = dev->dev_private;
  8122. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  8123. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8124. WARN_ON(!in_irq());
  8125. if (crtc == NULL)
  8126. return;
  8127. spin_lock(&dev->event_lock);
  8128. if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
  8129. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  8130. intel_crtc->unpin_work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  8131. page_flip_completed(intel_crtc);
  8132. }
  8133. spin_unlock(&dev->event_lock);
  8134. }
  8135. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  8136. struct drm_framebuffer *fb,
  8137. struct drm_pending_vblank_event *event,
  8138. uint32_t page_flip_flags)
  8139. {
  8140. struct drm_device *dev = crtc->dev;
  8141. struct drm_i915_private *dev_priv = dev->dev_private;
  8142. struct drm_framebuffer *old_fb = crtc->primary->fb;
  8143. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  8144. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8145. enum pipe pipe = intel_crtc->pipe;
  8146. struct intel_unpin_work *work;
  8147. struct intel_engine_cs *ring;
  8148. int ret;
  8149. /*
  8150. * drm_mode_page_flip_ioctl() should already catch this, but double
  8151. * check to be safe. In the future we may enable pageflipping from
  8152. * a disabled primary plane.
  8153. */
  8154. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  8155. return -EBUSY;
  8156. /* Can't change pixel format via MI display flips. */
  8157. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  8158. return -EINVAL;
  8159. /*
  8160. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  8161. * Note that pitch changes could also affect these register.
  8162. */
  8163. if (INTEL_INFO(dev)->gen > 3 &&
  8164. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  8165. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  8166. return -EINVAL;
  8167. if (i915_terminally_wedged(&dev_priv->gpu_error))
  8168. goto out_hang;
  8169. work = kzalloc(sizeof(*work), GFP_KERNEL);
  8170. if (work == NULL)
  8171. return -ENOMEM;
  8172. work->event = event;
  8173. work->crtc = crtc;
  8174. work->old_fb_obj = intel_fb_obj(old_fb);
  8175. INIT_WORK(&work->work, intel_unpin_work_fn);
  8176. ret = drm_crtc_vblank_get(crtc);
  8177. if (ret)
  8178. goto free_work;
  8179. /* We borrow the event spin lock for protecting unpin_work */
  8180. spin_lock_irq(&dev->event_lock);
  8181. if (intel_crtc->unpin_work) {
  8182. /* Before declaring the flip queue wedged, check if
  8183. * the hardware completed the operation behind our backs.
  8184. */
  8185. if (__intel_pageflip_stall_check(dev, crtc)) {
  8186. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  8187. page_flip_completed(intel_crtc);
  8188. } else {
  8189. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  8190. spin_unlock_irq(&dev->event_lock);
  8191. drm_crtc_vblank_put(crtc);
  8192. kfree(work);
  8193. return -EBUSY;
  8194. }
  8195. }
  8196. intel_crtc->unpin_work = work;
  8197. spin_unlock_irq(&dev->event_lock);
  8198. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  8199. flush_workqueue(dev_priv->wq);
  8200. ret = i915_mutex_lock_interruptible(dev);
  8201. if (ret)
  8202. goto cleanup;
  8203. /* Reference the objects for the scheduled work. */
  8204. drm_gem_object_reference(&work->old_fb_obj->base);
  8205. drm_gem_object_reference(&obj->base);
  8206. crtc->primary->fb = fb;
  8207. work->pending_flip_obj = obj;
  8208. atomic_inc(&intel_crtc->unpin_work_count);
  8209. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  8210. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  8211. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  8212. if (IS_VALLEYVIEW(dev)) {
  8213. ring = &dev_priv->ring[BCS];
  8214. if (obj->tiling_mode != work->old_fb_obj->tiling_mode)
  8215. /* vlv: DISPLAY_FLIP fails to change tiling */
  8216. ring = NULL;
  8217. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  8218. ring = &dev_priv->ring[BCS];
  8219. } else if (INTEL_INFO(dev)->gen >= 7) {
  8220. ring = obj->ring;
  8221. if (ring == NULL || ring->id != RCS)
  8222. ring = &dev_priv->ring[BCS];
  8223. } else {
  8224. ring = &dev_priv->ring[RCS];
  8225. }
  8226. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb, ring);
  8227. if (ret)
  8228. goto cleanup_pending;
  8229. work->gtt_offset =
  8230. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
  8231. if (use_mmio_flip(ring, obj)) {
  8232. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  8233. page_flip_flags);
  8234. if (ret)
  8235. goto cleanup_unpin;
  8236. work->flip_queued_seqno = obj->last_write_seqno;
  8237. work->flip_queued_ring = obj->ring;
  8238. } else {
  8239. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
  8240. page_flip_flags);
  8241. if (ret)
  8242. goto cleanup_unpin;
  8243. work->flip_queued_seqno = intel_ring_get_seqno(ring);
  8244. work->flip_queued_ring = ring;
  8245. }
  8246. work->flip_queued_vblank = drm_vblank_count(dev, intel_crtc->pipe);
  8247. work->enable_stall_check = true;
  8248. i915_gem_track_fb(work->old_fb_obj, obj,
  8249. INTEL_FRONTBUFFER_PRIMARY(pipe));
  8250. intel_disable_fbc(dev);
  8251. intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  8252. mutex_unlock(&dev->struct_mutex);
  8253. trace_i915_flip_request(intel_crtc->plane, obj);
  8254. return 0;
  8255. cleanup_unpin:
  8256. intel_unpin_fb_obj(obj);
  8257. cleanup_pending:
  8258. atomic_dec(&intel_crtc->unpin_work_count);
  8259. crtc->primary->fb = old_fb;
  8260. drm_gem_object_unreference(&work->old_fb_obj->base);
  8261. drm_gem_object_unreference(&obj->base);
  8262. mutex_unlock(&dev->struct_mutex);
  8263. cleanup:
  8264. spin_lock_irq(&dev->event_lock);
  8265. intel_crtc->unpin_work = NULL;
  8266. spin_unlock_irq(&dev->event_lock);
  8267. drm_crtc_vblank_put(crtc);
  8268. free_work:
  8269. kfree(work);
  8270. if (ret == -EIO) {
  8271. out_hang:
  8272. intel_crtc_wait_for_pending_flips(crtc);
  8273. ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
  8274. if (ret == 0 && event) {
  8275. spin_lock_irq(&dev->event_lock);
  8276. drm_send_vblank_event(dev, pipe, event);
  8277. spin_unlock_irq(&dev->event_lock);
  8278. }
  8279. }
  8280. return ret;
  8281. }
  8282. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  8283. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  8284. .load_lut = intel_crtc_load_lut,
  8285. };
  8286. /**
  8287. * intel_modeset_update_staged_output_state
  8288. *
  8289. * Updates the staged output configuration state, e.g. after we've read out the
  8290. * current hw state.
  8291. */
  8292. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  8293. {
  8294. struct intel_crtc *crtc;
  8295. struct intel_encoder *encoder;
  8296. struct intel_connector *connector;
  8297. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8298. base.head) {
  8299. connector->new_encoder =
  8300. to_intel_encoder(connector->base.encoder);
  8301. }
  8302. for_each_intel_encoder(dev, encoder) {
  8303. encoder->new_crtc =
  8304. to_intel_crtc(encoder->base.crtc);
  8305. }
  8306. for_each_intel_crtc(dev, crtc) {
  8307. crtc->new_enabled = crtc->base.enabled;
  8308. if (crtc->new_enabled)
  8309. crtc->new_config = &crtc->config;
  8310. else
  8311. crtc->new_config = NULL;
  8312. }
  8313. }
  8314. /**
  8315. * intel_modeset_commit_output_state
  8316. *
  8317. * This function copies the stage display pipe configuration to the real one.
  8318. */
  8319. static void intel_modeset_commit_output_state(struct drm_device *dev)
  8320. {
  8321. struct intel_crtc *crtc;
  8322. struct intel_encoder *encoder;
  8323. struct intel_connector *connector;
  8324. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8325. base.head) {
  8326. connector->base.encoder = &connector->new_encoder->base;
  8327. }
  8328. for_each_intel_encoder(dev, encoder) {
  8329. encoder->base.crtc = &encoder->new_crtc->base;
  8330. }
  8331. for_each_intel_crtc(dev, crtc) {
  8332. crtc->base.enabled = crtc->new_enabled;
  8333. }
  8334. }
  8335. static void
  8336. connected_sink_compute_bpp(struct intel_connector *connector,
  8337. struct intel_crtc_config *pipe_config)
  8338. {
  8339. int bpp = pipe_config->pipe_bpp;
  8340. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8341. connector->base.base.id,
  8342. connector->base.name);
  8343. /* Don't use an invalid EDID bpc value */
  8344. if (connector->base.display_info.bpc &&
  8345. connector->base.display_info.bpc * 3 < bpp) {
  8346. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8347. bpp, connector->base.display_info.bpc*3);
  8348. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  8349. }
  8350. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8351. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  8352. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8353. bpp);
  8354. pipe_config->pipe_bpp = 24;
  8355. }
  8356. }
  8357. static int
  8358. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8359. struct drm_framebuffer *fb,
  8360. struct intel_crtc_config *pipe_config)
  8361. {
  8362. struct drm_device *dev = crtc->base.dev;
  8363. struct intel_connector *connector;
  8364. int bpp;
  8365. switch (fb->pixel_format) {
  8366. case DRM_FORMAT_C8:
  8367. bpp = 8*3; /* since we go through a colormap */
  8368. break;
  8369. case DRM_FORMAT_XRGB1555:
  8370. case DRM_FORMAT_ARGB1555:
  8371. /* checked in intel_framebuffer_init already */
  8372. if (WARN_ON(INTEL_INFO(dev)->gen > 3))
  8373. return -EINVAL;
  8374. case DRM_FORMAT_RGB565:
  8375. bpp = 6*3; /* min is 18bpp */
  8376. break;
  8377. case DRM_FORMAT_XBGR8888:
  8378. case DRM_FORMAT_ABGR8888:
  8379. /* checked in intel_framebuffer_init already */
  8380. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8381. return -EINVAL;
  8382. case DRM_FORMAT_XRGB8888:
  8383. case DRM_FORMAT_ARGB8888:
  8384. bpp = 8*3;
  8385. break;
  8386. case DRM_FORMAT_XRGB2101010:
  8387. case DRM_FORMAT_ARGB2101010:
  8388. case DRM_FORMAT_XBGR2101010:
  8389. case DRM_FORMAT_ABGR2101010:
  8390. /* checked in intel_framebuffer_init already */
  8391. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8392. return -EINVAL;
  8393. bpp = 10*3;
  8394. break;
  8395. /* TODO: gen4+ supports 16 bpc floating point, too. */
  8396. default:
  8397. DRM_DEBUG_KMS("unsupported depth\n");
  8398. return -EINVAL;
  8399. }
  8400. pipe_config->pipe_bpp = bpp;
  8401. /* Clamp display bpp to EDID value */
  8402. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8403. base.head) {
  8404. if (!connector->new_encoder ||
  8405. connector->new_encoder->new_crtc != crtc)
  8406. continue;
  8407. connected_sink_compute_bpp(connector, pipe_config);
  8408. }
  8409. return bpp;
  8410. }
  8411. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  8412. {
  8413. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  8414. "type: 0x%x flags: 0x%x\n",
  8415. mode->crtc_clock,
  8416. mode->crtc_hdisplay, mode->crtc_hsync_start,
  8417. mode->crtc_hsync_end, mode->crtc_htotal,
  8418. mode->crtc_vdisplay, mode->crtc_vsync_start,
  8419. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  8420. }
  8421. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  8422. struct intel_crtc_config *pipe_config,
  8423. const char *context)
  8424. {
  8425. DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
  8426. context, pipe_name(crtc->pipe));
  8427. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  8428. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  8429. pipe_config->pipe_bpp, pipe_config->dither);
  8430. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8431. pipe_config->has_pch_encoder,
  8432. pipe_config->fdi_lanes,
  8433. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  8434. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  8435. pipe_config->fdi_m_n.tu);
  8436. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8437. pipe_config->has_dp_encoder,
  8438. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  8439. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  8440. pipe_config->dp_m_n.tu);
  8441. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  8442. pipe_config->has_dp_encoder,
  8443. pipe_config->dp_m2_n2.gmch_m,
  8444. pipe_config->dp_m2_n2.gmch_n,
  8445. pipe_config->dp_m2_n2.link_m,
  8446. pipe_config->dp_m2_n2.link_n,
  8447. pipe_config->dp_m2_n2.tu);
  8448. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  8449. pipe_config->has_audio,
  8450. pipe_config->has_infoframe);
  8451. DRM_DEBUG_KMS("requested mode:\n");
  8452. drm_mode_debug_printmodeline(&pipe_config->requested_mode);
  8453. DRM_DEBUG_KMS("adjusted mode:\n");
  8454. drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
  8455. intel_dump_crtc_timings(&pipe_config->adjusted_mode);
  8456. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  8457. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  8458. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  8459. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  8460. pipe_config->gmch_pfit.control,
  8461. pipe_config->gmch_pfit.pgm_ratios,
  8462. pipe_config->gmch_pfit.lvds_border_bits);
  8463. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  8464. pipe_config->pch_pfit.pos,
  8465. pipe_config->pch_pfit.size,
  8466. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  8467. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  8468. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  8469. }
  8470. static bool encoders_cloneable(const struct intel_encoder *a,
  8471. const struct intel_encoder *b)
  8472. {
  8473. /* masks could be asymmetric, so check both ways */
  8474. return a == b || (a->cloneable & (1 << b->type) &&
  8475. b->cloneable & (1 << a->type));
  8476. }
  8477. static bool check_single_encoder_cloning(struct intel_crtc *crtc,
  8478. struct intel_encoder *encoder)
  8479. {
  8480. struct drm_device *dev = crtc->base.dev;
  8481. struct intel_encoder *source_encoder;
  8482. for_each_intel_encoder(dev, source_encoder) {
  8483. if (source_encoder->new_crtc != crtc)
  8484. continue;
  8485. if (!encoders_cloneable(encoder, source_encoder))
  8486. return false;
  8487. }
  8488. return true;
  8489. }
  8490. static bool check_encoder_cloning(struct intel_crtc *crtc)
  8491. {
  8492. struct drm_device *dev = crtc->base.dev;
  8493. struct intel_encoder *encoder;
  8494. for_each_intel_encoder(dev, encoder) {
  8495. if (encoder->new_crtc != crtc)
  8496. continue;
  8497. if (!check_single_encoder_cloning(crtc, encoder))
  8498. return false;
  8499. }
  8500. return true;
  8501. }
  8502. static bool check_digital_port_conflicts(struct drm_device *dev)
  8503. {
  8504. struct intel_connector *connector;
  8505. unsigned int used_ports = 0;
  8506. /*
  8507. * Walk the connector list instead of the encoder
  8508. * list to detect the problem on ddi platforms
  8509. * where there's just one encoder per digital port.
  8510. */
  8511. list_for_each_entry(connector,
  8512. &dev->mode_config.connector_list, base.head) {
  8513. struct intel_encoder *encoder = connector->new_encoder;
  8514. if (!encoder)
  8515. continue;
  8516. WARN_ON(!encoder->new_crtc);
  8517. switch (encoder->type) {
  8518. unsigned int port_mask;
  8519. case INTEL_OUTPUT_UNKNOWN:
  8520. if (WARN_ON(!HAS_DDI(dev)))
  8521. break;
  8522. case INTEL_OUTPUT_DISPLAYPORT:
  8523. case INTEL_OUTPUT_HDMI:
  8524. case INTEL_OUTPUT_EDP:
  8525. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  8526. /* the same port mustn't appear more than once */
  8527. if (used_ports & port_mask)
  8528. return false;
  8529. used_ports |= port_mask;
  8530. default:
  8531. break;
  8532. }
  8533. }
  8534. return true;
  8535. }
  8536. static struct intel_crtc_config *
  8537. intel_modeset_pipe_config(struct drm_crtc *crtc,
  8538. struct drm_framebuffer *fb,
  8539. struct drm_display_mode *mode)
  8540. {
  8541. struct drm_device *dev = crtc->dev;
  8542. struct intel_encoder *encoder;
  8543. struct intel_crtc_config *pipe_config;
  8544. int plane_bpp, ret = -EINVAL;
  8545. bool retry = true;
  8546. if (!check_encoder_cloning(to_intel_crtc(crtc))) {
  8547. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  8548. return ERR_PTR(-EINVAL);
  8549. }
  8550. if (!check_digital_port_conflicts(dev)) {
  8551. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  8552. return ERR_PTR(-EINVAL);
  8553. }
  8554. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8555. if (!pipe_config)
  8556. return ERR_PTR(-ENOMEM);
  8557. drm_mode_copy(&pipe_config->adjusted_mode, mode);
  8558. drm_mode_copy(&pipe_config->requested_mode, mode);
  8559. pipe_config->cpu_transcoder =
  8560. (enum transcoder) to_intel_crtc(crtc)->pipe;
  8561. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8562. /*
  8563. * Sanitize sync polarity flags based on requested ones. If neither
  8564. * positive or negative polarity is requested, treat this as meaning
  8565. * negative polarity.
  8566. */
  8567. if (!(pipe_config->adjusted_mode.flags &
  8568. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  8569. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  8570. if (!(pipe_config->adjusted_mode.flags &
  8571. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  8572. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  8573. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  8574. * plane pixel format and any sink constraints into account. Returns the
  8575. * source plane bpp so that dithering can be selected on mismatches
  8576. * after encoders and crtc also have had their say. */
  8577. plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  8578. fb, pipe_config);
  8579. if (plane_bpp < 0)
  8580. goto fail;
  8581. /*
  8582. * Determine the real pipe dimensions. Note that stereo modes can
  8583. * increase the actual pipe size due to the frame doubling and
  8584. * insertion of additional space for blanks between the frame. This
  8585. * is stored in the crtc timings. We use the requested mode to do this
  8586. * computation to clearly distinguish it from the adjusted mode, which
  8587. * can be changed by the connectors in the below retry loop.
  8588. */
  8589. drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
  8590. pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
  8591. pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
  8592. encoder_retry:
  8593. /* Ensure the port clock defaults are reset when retrying. */
  8594. pipe_config->port_clock = 0;
  8595. pipe_config->pixel_multiplier = 1;
  8596. /* Fill in default crtc timings, allow encoders to overwrite them. */
  8597. drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
  8598. /* Pass our mode to the connectors and the CRTC to give them a chance to
  8599. * adjust it according to limitations or connector properties, and also
  8600. * a chance to reject the mode entirely.
  8601. */
  8602. for_each_intel_encoder(dev, encoder) {
  8603. if (&encoder->new_crtc->base != crtc)
  8604. continue;
  8605. if (!(encoder->compute_config(encoder, pipe_config))) {
  8606. DRM_DEBUG_KMS("Encoder config failure\n");
  8607. goto fail;
  8608. }
  8609. }
  8610. /* Set default port clock if not overwritten by the encoder. Needs to be
  8611. * done afterwards in case the encoder adjusts the mode. */
  8612. if (!pipe_config->port_clock)
  8613. pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
  8614. * pipe_config->pixel_multiplier;
  8615. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  8616. if (ret < 0) {
  8617. DRM_DEBUG_KMS("CRTC fixup failed\n");
  8618. goto fail;
  8619. }
  8620. if (ret == RETRY) {
  8621. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  8622. ret = -EINVAL;
  8623. goto fail;
  8624. }
  8625. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  8626. retry = false;
  8627. goto encoder_retry;
  8628. }
  8629. pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
  8630. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  8631. plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  8632. return pipe_config;
  8633. fail:
  8634. kfree(pipe_config);
  8635. return ERR_PTR(ret);
  8636. }
  8637. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  8638. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  8639. static void
  8640. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  8641. unsigned *prepare_pipes, unsigned *disable_pipes)
  8642. {
  8643. struct intel_crtc *intel_crtc;
  8644. struct drm_device *dev = crtc->dev;
  8645. struct intel_encoder *encoder;
  8646. struct intel_connector *connector;
  8647. struct drm_crtc *tmp_crtc;
  8648. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  8649. /* Check which crtcs have changed outputs connected to them, these need
  8650. * to be part of the prepare_pipes mask. We don't (yet) support global
  8651. * modeset across multiple crtcs, so modeset_pipes will only have one
  8652. * bit set at most. */
  8653. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8654. base.head) {
  8655. if (connector->base.encoder == &connector->new_encoder->base)
  8656. continue;
  8657. if (connector->base.encoder) {
  8658. tmp_crtc = connector->base.encoder->crtc;
  8659. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8660. }
  8661. if (connector->new_encoder)
  8662. *prepare_pipes |=
  8663. 1 << connector->new_encoder->new_crtc->pipe;
  8664. }
  8665. for_each_intel_encoder(dev, encoder) {
  8666. if (encoder->base.crtc == &encoder->new_crtc->base)
  8667. continue;
  8668. if (encoder->base.crtc) {
  8669. tmp_crtc = encoder->base.crtc;
  8670. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8671. }
  8672. if (encoder->new_crtc)
  8673. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  8674. }
  8675. /* Check for pipes that will be enabled/disabled ... */
  8676. for_each_intel_crtc(dev, intel_crtc) {
  8677. if (intel_crtc->base.enabled == intel_crtc->new_enabled)
  8678. continue;
  8679. if (!intel_crtc->new_enabled)
  8680. *disable_pipes |= 1 << intel_crtc->pipe;
  8681. else
  8682. *prepare_pipes |= 1 << intel_crtc->pipe;
  8683. }
  8684. /* set_mode is also used to update properties on life display pipes. */
  8685. intel_crtc = to_intel_crtc(crtc);
  8686. if (intel_crtc->new_enabled)
  8687. *prepare_pipes |= 1 << intel_crtc->pipe;
  8688. /*
  8689. * For simplicity do a full modeset on any pipe where the output routing
  8690. * changed. We could be more clever, but that would require us to be
  8691. * more careful with calling the relevant encoder->mode_set functions.
  8692. */
  8693. if (*prepare_pipes)
  8694. *modeset_pipes = *prepare_pipes;
  8695. /* ... and mask these out. */
  8696. *modeset_pipes &= ~(*disable_pipes);
  8697. *prepare_pipes &= ~(*disable_pipes);
  8698. /*
  8699. * HACK: We don't (yet) fully support global modesets. intel_set_config
  8700. * obies this rule, but the modeset restore mode of
  8701. * intel_modeset_setup_hw_state does not.
  8702. */
  8703. *modeset_pipes &= 1 << intel_crtc->pipe;
  8704. *prepare_pipes &= 1 << intel_crtc->pipe;
  8705. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  8706. *modeset_pipes, *prepare_pipes, *disable_pipes);
  8707. }
  8708. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  8709. {
  8710. struct drm_encoder *encoder;
  8711. struct drm_device *dev = crtc->dev;
  8712. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  8713. if (encoder->crtc == crtc)
  8714. return true;
  8715. return false;
  8716. }
  8717. static void
  8718. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  8719. {
  8720. struct drm_i915_private *dev_priv = dev->dev_private;
  8721. struct intel_encoder *intel_encoder;
  8722. struct intel_crtc *intel_crtc;
  8723. struct drm_connector *connector;
  8724. intel_shared_dpll_commit(dev_priv);
  8725. for_each_intel_encoder(dev, intel_encoder) {
  8726. if (!intel_encoder->base.crtc)
  8727. continue;
  8728. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  8729. if (prepare_pipes & (1 << intel_crtc->pipe))
  8730. intel_encoder->connectors_active = false;
  8731. }
  8732. intel_modeset_commit_output_state(dev);
  8733. /* Double check state. */
  8734. for_each_intel_crtc(dev, intel_crtc) {
  8735. WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
  8736. WARN_ON(intel_crtc->new_config &&
  8737. intel_crtc->new_config != &intel_crtc->config);
  8738. WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
  8739. }
  8740. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  8741. if (!connector->encoder || !connector->encoder->crtc)
  8742. continue;
  8743. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  8744. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  8745. struct drm_property *dpms_property =
  8746. dev->mode_config.dpms_property;
  8747. connector->dpms = DRM_MODE_DPMS_ON;
  8748. drm_object_property_set_value(&connector->base,
  8749. dpms_property,
  8750. DRM_MODE_DPMS_ON);
  8751. intel_encoder = to_intel_encoder(connector->encoder);
  8752. intel_encoder->connectors_active = true;
  8753. }
  8754. }
  8755. }
  8756. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  8757. {
  8758. int diff;
  8759. if (clock1 == clock2)
  8760. return true;
  8761. if (!clock1 || !clock2)
  8762. return false;
  8763. diff = abs(clock1 - clock2);
  8764. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  8765. return true;
  8766. return false;
  8767. }
  8768. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  8769. list_for_each_entry((intel_crtc), \
  8770. &(dev)->mode_config.crtc_list, \
  8771. base.head) \
  8772. if (mask & (1 <<(intel_crtc)->pipe))
  8773. static bool
  8774. intel_pipe_config_compare(struct drm_device *dev,
  8775. struct intel_crtc_config *current_config,
  8776. struct intel_crtc_config *pipe_config)
  8777. {
  8778. #define PIPE_CONF_CHECK_X(name) \
  8779. if (current_config->name != pipe_config->name) { \
  8780. DRM_ERROR("mismatch in " #name " " \
  8781. "(expected 0x%08x, found 0x%08x)\n", \
  8782. current_config->name, \
  8783. pipe_config->name); \
  8784. return false; \
  8785. }
  8786. #define PIPE_CONF_CHECK_I(name) \
  8787. if (current_config->name != pipe_config->name) { \
  8788. DRM_ERROR("mismatch in " #name " " \
  8789. "(expected %i, found %i)\n", \
  8790. current_config->name, \
  8791. pipe_config->name); \
  8792. return false; \
  8793. }
  8794. /* This is required for BDW+ where there is only one set of registers for
  8795. * switching between high and low RR.
  8796. * This macro can be used whenever a comparison has to be made between one
  8797. * hw state and multiple sw state variables.
  8798. */
  8799. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  8800. if ((current_config->name != pipe_config->name) && \
  8801. (current_config->alt_name != pipe_config->name)) { \
  8802. DRM_ERROR("mismatch in " #name " " \
  8803. "(expected %i or %i, found %i)\n", \
  8804. current_config->name, \
  8805. current_config->alt_name, \
  8806. pipe_config->name); \
  8807. return false; \
  8808. }
  8809. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  8810. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  8811. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  8812. "(expected %i, found %i)\n", \
  8813. current_config->name & (mask), \
  8814. pipe_config->name & (mask)); \
  8815. return false; \
  8816. }
  8817. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  8818. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  8819. DRM_ERROR("mismatch in " #name " " \
  8820. "(expected %i, found %i)\n", \
  8821. current_config->name, \
  8822. pipe_config->name); \
  8823. return false; \
  8824. }
  8825. #define PIPE_CONF_QUIRK(quirk) \
  8826. ((current_config->quirks | pipe_config->quirks) & (quirk))
  8827. PIPE_CONF_CHECK_I(cpu_transcoder);
  8828. PIPE_CONF_CHECK_I(has_pch_encoder);
  8829. PIPE_CONF_CHECK_I(fdi_lanes);
  8830. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  8831. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  8832. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  8833. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  8834. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  8835. PIPE_CONF_CHECK_I(has_dp_encoder);
  8836. if (INTEL_INFO(dev)->gen < 8) {
  8837. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  8838. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  8839. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  8840. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  8841. PIPE_CONF_CHECK_I(dp_m_n.tu);
  8842. if (current_config->has_drrs) {
  8843. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
  8844. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
  8845. PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
  8846. PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
  8847. PIPE_CONF_CHECK_I(dp_m2_n2.tu);
  8848. }
  8849. } else {
  8850. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
  8851. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
  8852. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
  8853. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
  8854. PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
  8855. }
  8856. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
  8857. PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
  8858. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
  8859. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
  8860. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
  8861. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
  8862. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
  8863. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
  8864. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
  8865. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
  8866. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
  8867. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
  8868. PIPE_CONF_CHECK_I(pixel_multiplier);
  8869. PIPE_CONF_CHECK_I(has_hdmi_sink);
  8870. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  8871. IS_VALLEYVIEW(dev))
  8872. PIPE_CONF_CHECK_I(limited_color_range);
  8873. PIPE_CONF_CHECK_I(has_infoframe);
  8874. PIPE_CONF_CHECK_I(has_audio);
  8875. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8876. DRM_MODE_FLAG_INTERLACE);
  8877. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  8878. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8879. DRM_MODE_FLAG_PHSYNC);
  8880. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8881. DRM_MODE_FLAG_NHSYNC);
  8882. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8883. DRM_MODE_FLAG_PVSYNC);
  8884. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8885. DRM_MODE_FLAG_NVSYNC);
  8886. }
  8887. PIPE_CONF_CHECK_I(pipe_src_w);
  8888. PIPE_CONF_CHECK_I(pipe_src_h);
  8889. /*
  8890. * FIXME: BIOS likes to set up a cloned config with lvds+external
  8891. * screen. Since we don't yet re-compute the pipe config when moving
  8892. * just the lvds port away to another pipe the sw tracking won't match.
  8893. *
  8894. * Proper atomic modesets with recomputed global state will fix this.
  8895. * Until then just don't check gmch state for inherited modes.
  8896. */
  8897. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  8898. PIPE_CONF_CHECK_I(gmch_pfit.control);
  8899. /* pfit ratios are autocomputed by the hw on gen4+ */
  8900. if (INTEL_INFO(dev)->gen < 4)
  8901. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  8902. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  8903. }
  8904. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  8905. if (current_config->pch_pfit.enabled) {
  8906. PIPE_CONF_CHECK_I(pch_pfit.pos);
  8907. PIPE_CONF_CHECK_I(pch_pfit.size);
  8908. }
  8909. /* BDW+ don't expose a synchronous way to read the state */
  8910. if (IS_HASWELL(dev))
  8911. PIPE_CONF_CHECK_I(ips_enabled);
  8912. PIPE_CONF_CHECK_I(double_wide);
  8913. PIPE_CONF_CHECK_X(ddi_pll_sel);
  8914. PIPE_CONF_CHECK_I(shared_dpll);
  8915. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  8916. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  8917. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  8918. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  8919. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  8920. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  8921. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  8922. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  8923. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  8924. PIPE_CONF_CHECK_I(pipe_bpp);
  8925. PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
  8926. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  8927. #undef PIPE_CONF_CHECK_X
  8928. #undef PIPE_CONF_CHECK_I
  8929. #undef PIPE_CONF_CHECK_I_ALT
  8930. #undef PIPE_CONF_CHECK_FLAGS
  8931. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  8932. #undef PIPE_CONF_QUIRK
  8933. return true;
  8934. }
  8935. static void check_wm_state(struct drm_device *dev)
  8936. {
  8937. struct drm_i915_private *dev_priv = dev->dev_private;
  8938. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  8939. struct intel_crtc *intel_crtc;
  8940. int plane;
  8941. if (INTEL_INFO(dev)->gen < 9)
  8942. return;
  8943. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  8944. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  8945. for_each_intel_crtc(dev, intel_crtc) {
  8946. struct skl_ddb_entry *hw_entry, *sw_entry;
  8947. const enum pipe pipe = intel_crtc->pipe;
  8948. if (!intel_crtc->active)
  8949. continue;
  8950. /* planes */
  8951. for_each_plane(pipe, plane) {
  8952. hw_entry = &hw_ddb.plane[pipe][plane];
  8953. sw_entry = &sw_ddb->plane[pipe][plane];
  8954. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  8955. continue;
  8956. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  8957. "(expected (%u,%u), found (%u,%u))\n",
  8958. pipe_name(pipe), plane + 1,
  8959. sw_entry->start, sw_entry->end,
  8960. hw_entry->start, hw_entry->end);
  8961. }
  8962. /* cursor */
  8963. hw_entry = &hw_ddb.cursor[pipe];
  8964. sw_entry = &sw_ddb->cursor[pipe];
  8965. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  8966. continue;
  8967. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  8968. "(expected (%u,%u), found (%u,%u))\n",
  8969. pipe_name(pipe),
  8970. sw_entry->start, sw_entry->end,
  8971. hw_entry->start, hw_entry->end);
  8972. }
  8973. }
  8974. static void
  8975. check_connector_state(struct drm_device *dev)
  8976. {
  8977. struct intel_connector *connector;
  8978. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8979. base.head) {
  8980. /* This also checks the encoder/connector hw state with the
  8981. * ->get_hw_state callbacks. */
  8982. intel_connector_check_state(connector);
  8983. WARN(&connector->new_encoder->base != connector->base.encoder,
  8984. "connector's staged encoder doesn't match current encoder\n");
  8985. }
  8986. }
  8987. static void
  8988. check_encoder_state(struct drm_device *dev)
  8989. {
  8990. struct intel_encoder *encoder;
  8991. struct intel_connector *connector;
  8992. for_each_intel_encoder(dev, encoder) {
  8993. bool enabled = false;
  8994. bool active = false;
  8995. enum pipe pipe, tracked_pipe;
  8996. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  8997. encoder->base.base.id,
  8998. encoder->base.name);
  8999. WARN(&encoder->new_crtc->base != encoder->base.crtc,
  9000. "encoder's stage crtc doesn't match current crtc\n");
  9001. WARN(encoder->connectors_active && !encoder->base.crtc,
  9002. "encoder's active_connectors set, but no crtc\n");
  9003. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9004. base.head) {
  9005. if (connector->base.encoder != &encoder->base)
  9006. continue;
  9007. enabled = true;
  9008. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  9009. active = true;
  9010. }
  9011. /*
  9012. * for MST connectors if we unplug the connector is gone
  9013. * away but the encoder is still connected to a crtc
  9014. * until a modeset happens in response to the hotplug.
  9015. */
  9016. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  9017. continue;
  9018. WARN(!!encoder->base.crtc != enabled,
  9019. "encoder's enabled state mismatch "
  9020. "(expected %i, found %i)\n",
  9021. !!encoder->base.crtc, enabled);
  9022. WARN(active && !encoder->base.crtc,
  9023. "active encoder with no crtc\n");
  9024. WARN(encoder->connectors_active != active,
  9025. "encoder's computed active state doesn't match tracked active state "
  9026. "(expected %i, found %i)\n", active, encoder->connectors_active);
  9027. active = encoder->get_hw_state(encoder, &pipe);
  9028. WARN(active != encoder->connectors_active,
  9029. "encoder's hw state doesn't match sw tracking "
  9030. "(expected %i, found %i)\n",
  9031. encoder->connectors_active, active);
  9032. if (!encoder->base.crtc)
  9033. continue;
  9034. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  9035. WARN(active && pipe != tracked_pipe,
  9036. "active encoder's pipe doesn't match"
  9037. "(expected %i, found %i)\n",
  9038. tracked_pipe, pipe);
  9039. }
  9040. }
  9041. static void
  9042. check_crtc_state(struct drm_device *dev)
  9043. {
  9044. struct drm_i915_private *dev_priv = dev->dev_private;
  9045. struct intel_crtc *crtc;
  9046. struct intel_encoder *encoder;
  9047. struct intel_crtc_config pipe_config;
  9048. for_each_intel_crtc(dev, crtc) {
  9049. bool enabled = false;
  9050. bool active = false;
  9051. memset(&pipe_config, 0, sizeof(pipe_config));
  9052. DRM_DEBUG_KMS("[CRTC:%d]\n",
  9053. crtc->base.base.id);
  9054. WARN(crtc->active && !crtc->base.enabled,
  9055. "active crtc, but not enabled in sw tracking\n");
  9056. for_each_intel_encoder(dev, encoder) {
  9057. if (encoder->base.crtc != &crtc->base)
  9058. continue;
  9059. enabled = true;
  9060. if (encoder->connectors_active)
  9061. active = true;
  9062. }
  9063. WARN(active != crtc->active,
  9064. "crtc's computed active state doesn't match tracked active state "
  9065. "(expected %i, found %i)\n", active, crtc->active);
  9066. WARN(enabled != crtc->base.enabled,
  9067. "crtc's computed enabled state doesn't match tracked enabled state "
  9068. "(expected %i, found %i)\n", enabled, crtc->base.enabled);
  9069. active = dev_priv->display.get_pipe_config(crtc,
  9070. &pipe_config);
  9071. /* hw state is inconsistent with the pipe quirk */
  9072. if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  9073. (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  9074. active = crtc->active;
  9075. for_each_intel_encoder(dev, encoder) {
  9076. enum pipe pipe;
  9077. if (encoder->base.crtc != &crtc->base)
  9078. continue;
  9079. if (encoder->get_hw_state(encoder, &pipe))
  9080. encoder->get_config(encoder, &pipe_config);
  9081. }
  9082. WARN(crtc->active != active,
  9083. "crtc active state doesn't match with hw state "
  9084. "(expected %i, found %i)\n", crtc->active, active);
  9085. if (active &&
  9086. !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
  9087. WARN(1, "pipe state doesn't match!\n");
  9088. intel_dump_pipe_config(crtc, &pipe_config,
  9089. "[hw state]");
  9090. intel_dump_pipe_config(crtc, &crtc->config,
  9091. "[sw state]");
  9092. }
  9093. }
  9094. }
  9095. static void
  9096. check_shared_dpll_state(struct drm_device *dev)
  9097. {
  9098. struct drm_i915_private *dev_priv = dev->dev_private;
  9099. struct intel_crtc *crtc;
  9100. struct intel_dpll_hw_state dpll_hw_state;
  9101. int i;
  9102. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9103. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  9104. int enabled_crtcs = 0, active_crtcs = 0;
  9105. bool active;
  9106. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  9107. DRM_DEBUG_KMS("%s\n", pll->name);
  9108. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  9109. WARN(pll->active > hweight32(pll->config.crtc_mask),
  9110. "more active pll users than references: %i vs %i\n",
  9111. pll->active, hweight32(pll->config.crtc_mask));
  9112. WARN(pll->active && !pll->on,
  9113. "pll in active use but not on in sw tracking\n");
  9114. WARN(pll->on && !pll->active,
  9115. "pll in on but not on in use in sw tracking\n");
  9116. WARN(pll->on != active,
  9117. "pll on state mismatch (expected %i, found %i)\n",
  9118. pll->on, active);
  9119. for_each_intel_crtc(dev, crtc) {
  9120. if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
  9121. enabled_crtcs++;
  9122. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  9123. active_crtcs++;
  9124. }
  9125. WARN(pll->active != active_crtcs,
  9126. "pll active crtcs mismatch (expected %i, found %i)\n",
  9127. pll->active, active_crtcs);
  9128. WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  9129. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  9130. hweight32(pll->config.crtc_mask), enabled_crtcs);
  9131. WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  9132. sizeof(dpll_hw_state)),
  9133. "pll hw state mismatch\n");
  9134. }
  9135. }
  9136. void
  9137. intel_modeset_check_state(struct drm_device *dev)
  9138. {
  9139. check_wm_state(dev);
  9140. check_connector_state(dev);
  9141. check_encoder_state(dev);
  9142. check_crtc_state(dev);
  9143. check_shared_dpll_state(dev);
  9144. }
  9145. void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
  9146. int dotclock)
  9147. {
  9148. /*
  9149. * FDI already provided one idea for the dotclock.
  9150. * Yell if the encoder disagrees.
  9151. */
  9152. WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
  9153. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9154. pipe_config->adjusted_mode.crtc_clock, dotclock);
  9155. }
  9156. static void update_scanline_offset(struct intel_crtc *crtc)
  9157. {
  9158. struct drm_device *dev = crtc->base.dev;
  9159. /*
  9160. * The scanline counter increments at the leading edge of hsync.
  9161. *
  9162. * On most platforms it starts counting from vtotal-1 on the
  9163. * first active line. That means the scanline counter value is
  9164. * always one less than what we would expect. Ie. just after
  9165. * start of vblank, which also occurs at start of hsync (on the
  9166. * last active line), the scanline counter will read vblank_start-1.
  9167. *
  9168. * On gen2 the scanline counter starts counting from 1 instead
  9169. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  9170. * to keep the value positive), instead of adding one.
  9171. *
  9172. * On HSW+ the behaviour of the scanline counter depends on the output
  9173. * type. For DP ports it behaves like most other platforms, but on HDMI
  9174. * there's an extra 1 line difference. So we need to add two instead of
  9175. * one to the value.
  9176. */
  9177. if (IS_GEN2(dev)) {
  9178. const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
  9179. int vtotal;
  9180. vtotal = mode->crtc_vtotal;
  9181. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  9182. vtotal /= 2;
  9183. crtc->scanline_offset = vtotal - 1;
  9184. } else if (HAS_DDI(dev) &&
  9185. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  9186. crtc->scanline_offset = 2;
  9187. } else
  9188. crtc->scanline_offset = 1;
  9189. }
  9190. static struct intel_crtc_config *
  9191. intel_modeset_compute_config(struct drm_crtc *crtc,
  9192. struct drm_display_mode *mode,
  9193. struct drm_framebuffer *fb,
  9194. unsigned *modeset_pipes,
  9195. unsigned *prepare_pipes,
  9196. unsigned *disable_pipes)
  9197. {
  9198. struct intel_crtc_config *pipe_config = NULL;
  9199. intel_modeset_affected_pipes(crtc, modeset_pipes,
  9200. prepare_pipes, disable_pipes);
  9201. if ((*modeset_pipes) == 0)
  9202. goto out;
  9203. /*
  9204. * Note this needs changes when we start tracking multiple modes
  9205. * and crtcs. At that point we'll need to compute the whole config
  9206. * (i.e. one pipe_config for each crtc) rather than just the one
  9207. * for this crtc.
  9208. */
  9209. pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
  9210. if (IS_ERR(pipe_config)) {
  9211. goto out;
  9212. }
  9213. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  9214. "[modeset]");
  9215. out:
  9216. return pipe_config;
  9217. }
  9218. static int __intel_set_mode(struct drm_crtc *crtc,
  9219. struct drm_display_mode *mode,
  9220. int x, int y, struct drm_framebuffer *fb,
  9221. struct intel_crtc_config *pipe_config,
  9222. unsigned modeset_pipes,
  9223. unsigned prepare_pipes,
  9224. unsigned disable_pipes)
  9225. {
  9226. struct drm_device *dev = crtc->dev;
  9227. struct drm_i915_private *dev_priv = dev->dev_private;
  9228. struct drm_display_mode *saved_mode;
  9229. struct intel_crtc *intel_crtc;
  9230. int ret = 0;
  9231. saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
  9232. if (!saved_mode)
  9233. return -ENOMEM;
  9234. *saved_mode = crtc->mode;
  9235. if (modeset_pipes)
  9236. to_intel_crtc(crtc)->new_config = pipe_config;
  9237. /*
  9238. * See if the config requires any additional preparation, e.g.
  9239. * to adjust global state with pipes off. We need to do this
  9240. * here so we can get the modeset_pipe updated config for the new
  9241. * mode set on this crtc. For other crtcs we need to use the
  9242. * adjusted_mode bits in the crtc directly.
  9243. */
  9244. if (IS_VALLEYVIEW(dev)) {
  9245. valleyview_modeset_global_pipes(dev, &prepare_pipes);
  9246. /* may have added more to prepare_pipes than we should */
  9247. prepare_pipes &= ~disable_pipes;
  9248. }
  9249. if (dev_priv->display.crtc_compute_clock) {
  9250. unsigned clear_pipes = modeset_pipes | disable_pipes;
  9251. ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
  9252. if (ret)
  9253. goto done;
  9254. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9255. ret = dev_priv->display.crtc_compute_clock(intel_crtc);
  9256. if (ret) {
  9257. intel_shared_dpll_abort_config(dev_priv);
  9258. goto done;
  9259. }
  9260. }
  9261. }
  9262. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  9263. intel_crtc_disable(&intel_crtc->base);
  9264. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9265. if (intel_crtc->base.enabled)
  9266. dev_priv->display.crtc_disable(&intel_crtc->base);
  9267. }
  9268. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  9269. * to set it here already despite that we pass it down the callchain.
  9270. *
  9271. * Note we'll need to fix this up when we start tracking multiple
  9272. * pipes; here we assume a single modeset_pipe and only track the
  9273. * single crtc and mode.
  9274. */
  9275. if (modeset_pipes) {
  9276. crtc->mode = *mode;
  9277. /* mode_set/enable/disable functions rely on a correct pipe
  9278. * config. */
  9279. to_intel_crtc(crtc)->config = *pipe_config;
  9280. to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
  9281. /*
  9282. * Calculate and store various constants which
  9283. * are later needed by vblank and swap-completion
  9284. * timestamping. They are derived from true hwmode.
  9285. */
  9286. drm_calc_timestamping_constants(crtc,
  9287. &pipe_config->adjusted_mode);
  9288. }
  9289. /* Only after disabling all output pipelines that will be changed can we
  9290. * update the the output configuration. */
  9291. intel_modeset_update_state(dev, prepare_pipes);
  9292. modeset_update_crtc_power_domains(dev);
  9293. /* Set up the DPLL and any encoders state that needs to adjust or depend
  9294. * on the DPLL.
  9295. */
  9296. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9297. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9298. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
  9299. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9300. mutex_lock(&dev->struct_mutex);
  9301. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb, NULL);
  9302. if (ret != 0) {
  9303. DRM_ERROR("pin & fence failed\n");
  9304. mutex_unlock(&dev->struct_mutex);
  9305. goto done;
  9306. }
  9307. if (old_fb)
  9308. intel_unpin_fb_obj(old_obj);
  9309. i915_gem_track_fb(old_obj, obj,
  9310. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
  9311. mutex_unlock(&dev->struct_mutex);
  9312. crtc->primary->fb = fb;
  9313. crtc->x = x;
  9314. crtc->y = y;
  9315. }
  9316. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  9317. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9318. update_scanline_offset(intel_crtc);
  9319. dev_priv->display.crtc_enable(&intel_crtc->base);
  9320. }
  9321. /* FIXME: add subpixel order */
  9322. done:
  9323. if (ret && crtc->enabled)
  9324. crtc->mode = *saved_mode;
  9325. kfree(pipe_config);
  9326. kfree(saved_mode);
  9327. return ret;
  9328. }
  9329. static int intel_set_mode_pipes(struct drm_crtc *crtc,
  9330. struct drm_display_mode *mode,
  9331. int x, int y, struct drm_framebuffer *fb,
  9332. struct intel_crtc_config *pipe_config,
  9333. unsigned modeset_pipes,
  9334. unsigned prepare_pipes,
  9335. unsigned disable_pipes)
  9336. {
  9337. int ret;
  9338. ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
  9339. prepare_pipes, disable_pipes);
  9340. if (ret == 0)
  9341. intel_modeset_check_state(crtc->dev);
  9342. return ret;
  9343. }
  9344. static int intel_set_mode(struct drm_crtc *crtc,
  9345. struct drm_display_mode *mode,
  9346. int x, int y, struct drm_framebuffer *fb)
  9347. {
  9348. struct intel_crtc_config *pipe_config;
  9349. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  9350. pipe_config = intel_modeset_compute_config(crtc, mode, fb,
  9351. &modeset_pipes,
  9352. &prepare_pipes,
  9353. &disable_pipes);
  9354. if (IS_ERR(pipe_config))
  9355. return PTR_ERR(pipe_config);
  9356. return intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
  9357. modeset_pipes, prepare_pipes,
  9358. disable_pipes);
  9359. }
  9360. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  9361. {
  9362. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
  9363. }
  9364. #undef for_each_intel_crtc_masked
  9365. static void intel_set_config_free(struct intel_set_config *config)
  9366. {
  9367. if (!config)
  9368. return;
  9369. kfree(config->save_connector_encoders);
  9370. kfree(config->save_encoder_crtcs);
  9371. kfree(config->save_crtc_enabled);
  9372. kfree(config);
  9373. }
  9374. static int intel_set_config_save_state(struct drm_device *dev,
  9375. struct intel_set_config *config)
  9376. {
  9377. struct drm_crtc *crtc;
  9378. struct drm_encoder *encoder;
  9379. struct drm_connector *connector;
  9380. int count;
  9381. config->save_crtc_enabled =
  9382. kcalloc(dev->mode_config.num_crtc,
  9383. sizeof(bool), GFP_KERNEL);
  9384. if (!config->save_crtc_enabled)
  9385. return -ENOMEM;
  9386. config->save_encoder_crtcs =
  9387. kcalloc(dev->mode_config.num_encoder,
  9388. sizeof(struct drm_crtc *), GFP_KERNEL);
  9389. if (!config->save_encoder_crtcs)
  9390. return -ENOMEM;
  9391. config->save_connector_encoders =
  9392. kcalloc(dev->mode_config.num_connector,
  9393. sizeof(struct drm_encoder *), GFP_KERNEL);
  9394. if (!config->save_connector_encoders)
  9395. return -ENOMEM;
  9396. /* Copy data. Note that driver private data is not affected.
  9397. * Should anything bad happen only the expected state is
  9398. * restored, not the drivers personal bookkeeping.
  9399. */
  9400. count = 0;
  9401. for_each_crtc(dev, crtc) {
  9402. config->save_crtc_enabled[count++] = crtc->enabled;
  9403. }
  9404. count = 0;
  9405. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  9406. config->save_encoder_crtcs[count++] = encoder->crtc;
  9407. }
  9408. count = 0;
  9409. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  9410. config->save_connector_encoders[count++] = connector->encoder;
  9411. }
  9412. return 0;
  9413. }
  9414. static void intel_set_config_restore_state(struct drm_device *dev,
  9415. struct intel_set_config *config)
  9416. {
  9417. struct intel_crtc *crtc;
  9418. struct intel_encoder *encoder;
  9419. struct intel_connector *connector;
  9420. int count;
  9421. count = 0;
  9422. for_each_intel_crtc(dev, crtc) {
  9423. crtc->new_enabled = config->save_crtc_enabled[count++];
  9424. if (crtc->new_enabled)
  9425. crtc->new_config = &crtc->config;
  9426. else
  9427. crtc->new_config = NULL;
  9428. }
  9429. count = 0;
  9430. for_each_intel_encoder(dev, encoder) {
  9431. encoder->new_crtc =
  9432. to_intel_crtc(config->save_encoder_crtcs[count++]);
  9433. }
  9434. count = 0;
  9435. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  9436. connector->new_encoder =
  9437. to_intel_encoder(config->save_connector_encoders[count++]);
  9438. }
  9439. }
  9440. static bool
  9441. is_crtc_connector_off(struct drm_mode_set *set)
  9442. {
  9443. int i;
  9444. if (set->num_connectors == 0)
  9445. return false;
  9446. if (WARN_ON(set->connectors == NULL))
  9447. return false;
  9448. for (i = 0; i < set->num_connectors; i++)
  9449. if (set->connectors[i]->encoder &&
  9450. set->connectors[i]->encoder->crtc == set->crtc &&
  9451. set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
  9452. return true;
  9453. return false;
  9454. }
  9455. static void
  9456. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  9457. struct intel_set_config *config)
  9458. {
  9459. /* We should be able to check here if the fb has the same properties
  9460. * and then just flip_or_move it */
  9461. if (is_crtc_connector_off(set)) {
  9462. config->mode_changed = true;
  9463. } else if (set->crtc->primary->fb != set->fb) {
  9464. /*
  9465. * If we have no fb, we can only flip as long as the crtc is
  9466. * active, otherwise we need a full mode set. The crtc may
  9467. * be active if we've only disabled the primary plane, or
  9468. * in fastboot situations.
  9469. */
  9470. if (set->crtc->primary->fb == NULL) {
  9471. struct intel_crtc *intel_crtc =
  9472. to_intel_crtc(set->crtc);
  9473. if (intel_crtc->active) {
  9474. DRM_DEBUG_KMS("crtc has no fb, will flip\n");
  9475. config->fb_changed = true;
  9476. } else {
  9477. DRM_DEBUG_KMS("inactive crtc, full mode set\n");
  9478. config->mode_changed = true;
  9479. }
  9480. } else if (set->fb == NULL) {
  9481. config->mode_changed = true;
  9482. } else if (set->fb->pixel_format !=
  9483. set->crtc->primary->fb->pixel_format) {
  9484. config->mode_changed = true;
  9485. } else {
  9486. config->fb_changed = true;
  9487. }
  9488. }
  9489. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  9490. config->fb_changed = true;
  9491. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  9492. DRM_DEBUG_KMS("modes are different, full mode set\n");
  9493. drm_mode_debug_printmodeline(&set->crtc->mode);
  9494. drm_mode_debug_printmodeline(set->mode);
  9495. config->mode_changed = true;
  9496. }
  9497. DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
  9498. set->crtc->base.id, config->mode_changed, config->fb_changed);
  9499. }
  9500. static int
  9501. intel_modeset_stage_output_state(struct drm_device *dev,
  9502. struct drm_mode_set *set,
  9503. struct intel_set_config *config)
  9504. {
  9505. struct intel_connector *connector;
  9506. struct intel_encoder *encoder;
  9507. struct intel_crtc *crtc;
  9508. int ro;
  9509. /* The upper layers ensure that we either disable a crtc or have a list
  9510. * of connectors. For paranoia, double-check this. */
  9511. WARN_ON(!set->fb && (set->num_connectors != 0));
  9512. WARN_ON(set->fb && (set->num_connectors == 0));
  9513. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9514. base.head) {
  9515. /* Otherwise traverse passed in connector list and get encoders
  9516. * for them. */
  9517. for (ro = 0; ro < set->num_connectors; ro++) {
  9518. if (set->connectors[ro] == &connector->base) {
  9519. connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
  9520. break;
  9521. }
  9522. }
  9523. /* If we disable the crtc, disable all its connectors. Also, if
  9524. * the connector is on the changing crtc but not on the new
  9525. * connector list, disable it. */
  9526. if ((!set->fb || ro == set->num_connectors) &&
  9527. connector->base.encoder &&
  9528. connector->base.encoder->crtc == set->crtc) {
  9529. connector->new_encoder = NULL;
  9530. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  9531. connector->base.base.id,
  9532. connector->base.name);
  9533. }
  9534. if (&connector->new_encoder->base != connector->base.encoder) {
  9535. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  9536. config->mode_changed = true;
  9537. }
  9538. }
  9539. /* connector->new_encoder is now updated for all connectors. */
  9540. /* Update crtc of enabled connectors. */
  9541. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9542. base.head) {
  9543. struct drm_crtc *new_crtc;
  9544. if (!connector->new_encoder)
  9545. continue;
  9546. new_crtc = connector->new_encoder->base.crtc;
  9547. for (ro = 0; ro < set->num_connectors; ro++) {
  9548. if (set->connectors[ro] == &connector->base)
  9549. new_crtc = set->crtc;
  9550. }
  9551. /* Make sure the new CRTC will work with the encoder */
  9552. if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
  9553. new_crtc)) {
  9554. return -EINVAL;
  9555. }
  9556. connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
  9557. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  9558. connector->base.base.id,
  9559. connector->base.name,
  9560. new_crtc->base.id);
  9561. }
  9562. /* Check for any encoders that needs to be disabled. */
  9563. for_each_intel_encoder(dev, encoder) {
  9564. int num_connectors = 0;
  9565. list_for_each_entry(connector,
  9566. &dev->mode_config.connector_list,
  9567. base.head) {
  9568. if (connector->new_encoder == encoder) {
  9569. WARN_ON(!connector->new_encoder->new_crtc);
  9570. num_connectors++;
  9571. }
  9572. }
  9573. if (num_connectors == 0)
  9574. encoder->new_crtc = NULL;
  9575. else if (num_connectors > 1)
  9576. return -EINVAL;
  9577. /* Only now check for crtc changes so we don't miss encoders
  9578. * that will be disabled. */
  9579. if (&encoder->new_crtc->base != encoder->base.crtc) {
  9580. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  9581. config->mode_changed = true;
  9582. }
  9583. }
  9584. /* Now we've also updated encoder->new_crtc for all encoders. */
  9585. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9586. base.head) {
  9587. if (connector->new_encoder)
  9588. if (connector->new_encoder != connector->encoder)
  9589. connector->encoder = connector->new_encoder;
  9590. }
  9591. for_each_intel_crtc(dev, crtc) {
  9592. crtc->new_enabled = false;
  9593. for_each_intel_encoder(dev, encoder) {
  9594. if (encoder->new_crtc == crtc) {
  9595. crtc->new_enabled = true;
  9596. break;
  9597. }
  9598. }
  9599. if (crtc->new_enabled != crtc->base.enabled) {
  9600. DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
  9601. crtc->new_enabled ? "en" : "dis");
  9602. config->mode_changed = true;
  9603. }
  9604. if (crtc->new_enabled)
  9605. crtc->new_config = &crtc->config;
  9606. else
  9607. crtc->new_config = NULL;
  9608. }
  9609. return 0;
  9610. }
  9611. static void disable_crtc_nofb(struct intel_crtc *crtc)
  9612. {
  9613. struct drm_device *dev = crtc->base.dev;
  9614. struct intel_encoder *encoder;
  9615. struct intel_connector *connector;
  9616. DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
  9617. pipe_name(crtc->pipe));
  9618. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  9619. if (connector->new_encoder &&
  9620. connector->new_encoder->new_crtc == crtc)
  9621. connector->new_encoder = NULL;
  9622. }
  9623. for_each_intel_encoder(dev, encoder) {
  9624. if (encoder->new_crtc == crtc)
  9625. encoder->new_crtc = NULL;
  9626. }
  9627. crtc->new_enabled = false;
  9628. crtc->new_config = NULL;
  9629. }
  9630. static int intel_crtc_set_config(struct drm_mode_set *set)
  9631. {
  9632. struct drm_device *dev;
  9633. struct drm_mode_set save_set;
  9634. struct intel_set_config *config;
  9635. struct intel_crtc_config *pipe_config;
  9636. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  9637. int ret;
  9638. BUG_ON(!set);
  9639. BUG_ON(!set->crtc);
  9640. BUG_ON(!set->crtc->helper_private);
  9641. /* Enforce sane interface api - has been abused by the fb helper. */
  9642. BUG_ON(!set->mode && set->fb);
  9643. BUG_ON(set->fb && set->num_connectors == 0);
  9644. if (set->fb) {
  9645. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  9646. set->crtc->base.id, set->fb->base.id,
  9647. (int)set->num_connectors, set->x, set->y);
  9648. } else {
  9649. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  9650. }
  9651. dev = set->crtc->dev;
  9652. ret = -ENOMEM;
  9653. config = kzalloc(sizeof(*config), GFP_KERNEL);
  9654. if (!config)
  9655. goto out_config;
  9656. ret = intel_set_config_save_state(dev, config);
  9657. if (ret)
  9658. goto out_config;
  9659. save_set.crtc = set->crtc;
  9660. save_set.mode = &set->crtc->mode;
  9661. save_set.x = set->crtc->x;
  9662. save_set.y = set->crtc->y;
  9663. save_set.fb = set->crtc->primary->fb;
  9664. /* Compute whether we need a full modeset, only an fb base update or no
  9665. * change at all. In the future we might also check whether only the
  9666. * mode changed, e.g. for LVDS where we only change the panel fitter in
  9667. * such cases. */
  9668. intel_set_config_compute_mode_changes(set, config);
  9669. ret = intel_modeset_stage_output_state(dev, set, config);
  9670. if (ret)
  9671. goto fail;
  9672. pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
  9673. set->fb,
  9674. &modeset_pipes,
  9675. &prepare_pipes,
  9676. &disable_pipes);
  9677. if (IS_ERR(pipe_config)) {
  9678. ret = PTR_ERR(pipe_config);
  9679. goto fail;
  9680. } else if (pipe_config) {
  9681. if (pipe_config->has_audio !=
  9682. to_intel_crtc(set->crtc)->config.has_audio)
  9683. config->mode_changed = true;
  9684. /*
  9685. * Note we have an issue here with infoframes: current code
  9686. * only updates them on the full mode set path per hw
  9687. * requirements. So here we should be checking for any
  9688. * required changes and forcing a mode set.
  9689. */
  9690. }
  9691. /* set_mode will free it in the mode_changed case */
  9692. if (!config->mode_changed)
  9693. kfree(pipe_config);
  9694. intel_update_pipe_size(to_intel_crtc(set->crtc));
  9695. if (config->mode_changed) {
  9696. ret = intel_set_mode_pipes(set->crtc, set->mode,
  9697. set->x, set->y, set->fb, pipe_config,
  9698. modeset_pipes, prepare_pipes,
  9699. disable_pipes);
  9700. } else if (config->fb_changed) {
  9701. struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
  9702. intel_crtc_wait_for_pending_flips(set->crtc);
  9703. ret = intel_pipe_set_base(set->crtc,
  9704. set->x, set->y, set->fb);
  9705. /*
  9706. * We need to make sure the primary plane is re-enabled if it
  9707. * has previously been turned off.
  9708. */
  9709. if (!intel_crtc->primary_enabled && ret == 0) {
  9710. WARN_ON(!intel_crtc->active);
  9711. intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
  9712. }
  9713. /*
  9714. * In the fastboot case this may be our only check of the
  9715. * state after boot. It would be better to only do it on
  9716. * the first update, but we don't have a nice way of doing that
  9717. * (and really, set_config isn't used much for high freq page
  9718. * flipping, so increasing its cost here shouldn't be a big
  9719. * deal).
  9720. */
  9721. if (i915.fastboot && ret == 0)
  9722. intel_modeset_check_state(set->crtc->dev);
  9723. }
  9724. if (ret) {
  9725. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  9726. set->crtc->base.id, ret);
  9727. fail:
  9728. intel_set_config_restore_state(dev, config);
  9729. /*
  9730. * HACK: if the pipe was on, but we didn't have a framebuffer,
  9731. * force the pipe off to avoid oopsing in the modeset code
  9732. * due to fb==NULL. This should only happen during boot since
  9733. * we don't yet reconstruct the FB from the hardware state.
  9734. */
  9735. if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
  9736. disable_crtc_nofb(to_intel_crtc(save_set.crtc));
  9737. /* Try to restore the config */
  9738. if (config->mode_changed &&
  9739. intel_set_mode(save_set.crtc, save_set.mode,
  9740. save_set.x, save_set.y, save_set.fb))
  9741. DRM_ERROR("failed to restore config after modeset failure\n");
  9742. }
  9743. out_config:
  9744. intel_set_config_free(config);
  9745. return ret;
  9746. }
  9747. static const struct drm_crtc_funcs intel_crtc_funcs = {
  9748. .gamma_set = intel_crtc_gamma_set,
  9749. .set_config = intel_crtc_set_config,
  9750. .destroy = intel_crtc_destroy,
  9751. .page_flip = intel_crtc_page_flip,
  9752. };
  9753. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  9754. struct intel_shared_dpll *pll,
  9755. struct intel_dpll_hw_state *hw_state)
  9756. {
  9757. uint32_t val;
  9758. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  9759. return false;
  9760. val = I915_READ(PCH_DPLL(pll->id));
  9761. hw_state->dpll = val;
  9762. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  9763. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  9764. return val & DPLL_VCO_ENABLE;
  9765. }
  9766. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  9767. struct intel_shared_dpll *pll)
  9768. {
  9769. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  9770. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  9771. }
  9772. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  9773. struct intel_shared_dpll *pll)
  9774. {
  9775. /* PCH refclock must be enabled first */
  9776. ibx_assert_pch_refclk_enabled(dev_priv);
  9777. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  9778. /* Wait for the clocks to stabilize. */
  9779. POSTING_READ(PCH_DPLL(pll->id));
  9780. udelay(150);
  9781. /* The pixel multiplier can only be updated once the
  9782. * DPLL is enabled and the clocks are stable.
  9783. *
  9784. * So write it again.
  9785. */
  9786. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  9787. POSTING_READ(PCH_DPLL(pll->id));
  9788. udelay(200);
  9789. }
  9790. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  9791. struct intel_shared_dpll *pll)
  9792. {
  9793. struct drm_device *dev = dev_priv->dev;
  9794. struct intel_crtc *crtc;
  9795. /* Make sure no transcoder isn't still depending on us. */
  9796. for_each_intel_crtc(dev, crtc) {
  9797. if (intel_crtc_to_shared_dpll(crtc) == pll)
  9798. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  9799. }
  9800. I915_WRITE(PCH_DPLL(pll->id), 0);
  9801. POSTING_READ(PCH_DPLL(pll->id));
  9802. udelay(200);
  9803. }
  9804. static char *ibx_pch_dpll_names[] = {
  9805. "PCH DPLL A",
  9806. "PCH DPLL B",
  9807. };
  9808. static void ibx_pch_dpll_init(struct drm_device *dev)
  9809. {
  9810. struct drm_i915_private *dev_priv = dev->dev_private;
  9811. int i;
  9812. dev_priv->num_shared_dpll = 2;
  9813. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9814. dev_priv->shared_dplls[i].id = i;
  9815. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  9816. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  9817. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  9818. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  9819. dev_priv->shared_dplls[i].get_hw_state =
  9820. ibx_pch_dpll_get_hw_state;
  9821. }
  9822. }
  9823. static void intel_shared_dpll_init(struct drm_device *dev)
  9824. {
  9825. struct drm_i915_private *dev_priv = dev->dev_private;
  9826. if (HAS_DDI(dev))
  9827. intel_ddi_pll_init(dev);
  9828. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  9829. ibx_pch_dpll_init(dev);
  9830. else
  9831. dev_priv->num_shared_dpll = 0;
  9832. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  9833. }
  9834. static int
  9835. intel_primary_plane_disable(struct drm_plane *plane)
  9836. {
  9837. struct drm_device *dev = plane->dev;
  9838. struct intel_crtc *intel_crtc;
  9839. if (!plane->fb)
  9840. return 0;
  9841. BUG_ON(!plane->crtc);
  9842. intel_crtc = to_intel_crtc(plane->crtc);
  9843. /*
  9844. * Even though we checked plane->fb above, it's still possible that
  9845. * the primary plane has been implicitly disabled because the crtc
  9846. * coordinates given weren't visible, or because we detected
  9847. * that it was 100% covered by a sprite plane. Or, the CRTC may be
  9848. * off and we've set a fb, but haven't actually turned on the CRTC yet.
  9849. * In either case, we need to unpin the FB and let the fb pointer get
  9850. * updated, but otherwise we don't need to touch the hardware.
  9851. */
  9852. if (!intel_crtc->primary_enabled)
  9853. goto disable_unpin;
  9854. intel_crtc_wait_for_pending_flips(plane->crtc);
  9855. intel_disable_primary_hw_plane(plane, plane->crtc);
  9856. disable_unpin:
  9857. mutex_lock(&dev->struct_mutex);
  9858. i915_gem_track_fb(intel_fb_obj(plane->fb), NULL,
  9859. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
  9860. intel_unpin_fb_obj(intel_fb_obj(plane->fb));
  9861. mutex_unlock(&dev->struct_mutex);
  9862. plane->fb = NULL;
  9863. return 0;
  9864. }
  9865. static int
  9866. intel_check_primary_plane(struct drm_plane *plane,
  9867. struct intel_plane_state *state)
  9868. {
  9869. struct drm_crtc *crtc = state->crtc;
  9870. struct drm_framebuffer *fb = state->fb;
  9871. struct drm_rect *dest = &state->dst;
  9872. struct drm_rect *src = &state->src;
  9873. const struct drm_rect *clip = &state->clip;
  9874. return drm_plane_helper_check_update(plane, crtc, fb,
  9875. src, dest, clip,
  9876. DRM_PLANE_HELPER_NO_SCALING,
  9877. DRM_PLANE_HELPER_NO_SCALING,
  9878. false, true, &state->visible);
  9879. }
  9880. static int
  9881. intel_prepare_primary_plane(struct drm_plane *plane,
  9882. struct intel_plane_state *state)
  9883. {
  9884. struct drm_crtc *crtc = state->crtc;
  9885. struct drm_framebuffer *fb = state->fb;
  9886. struct drm_device *dev = crtc->dev;
  9887. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9888. enum pipe pipe = intel_crtc->pipe;
  9889. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9890. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  9891. int ret;
  9892. intel_crtc_wait_for_pending_flips(crtc);
  9893. if (intel_crtc_has_pending_flip(crtc)) {
  9894. DRM_ERROR("pipe is still busy with an old pageflip\n");
  9895. return -EBUSY;
  9896. }
  9897. if (old_obj != obj) {
  9898. mutex_lock(&dev->struct_mutex);
  9899. ret = intel_pin_and_fence_fb_obj(plane, fb, NULL);
  9900. if (ret == 0)
  9901. i915_gem_track_fb(old_obj, obj,
  9902. INTEL_FRONTBUFFER_PRIMARY(pipe));
  9903. mutex_unlock(&dev->struct_mutex);
  9904. if (ret != 0) {
  9905. DRM_DEBUG_KMS("pin & fence failed\n");
  9906. return ret;
  9907. }
  9908. }
  9909. return 0;
  9910. }
  9911. static void
  9912. intel_commit_primary_plane(struct drm_plane *plane,
  9913. struct intel_plane_state *state)
  9914. {
  9915. struct drm_crtc *crtc = state->crtc;
  9916. struct drm_framebuffer *fb = state->fb;
  9917. struct drm_device *dev = crtc->dev;
  9918. struct drm_i915_private *dev_priv = dev->dev_private;
  9919. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9920. enum pipe pipe = intel_crtc->pipe;
  9921. struct drm_framebuffer *old_fb = plane->fb;
  9922. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9923. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  9924. struct intel_plane *intel_plane = to_intel_plane(plane);
  9925. struct drm_rect *src = &state->src;
  9926. crtc->primary->fb = fb;
  9927. crtc->x = src->x1 >> 16;
  9928. crtc->y = src->y1 >> 16;
  9929. intel_plane->crtc_x = state->orig_dst.x1;
  9930. intel_plane->crtc_y = state->orig_dst.y1;
  9931. intel_plane->crtc_w = drm_rect_width(&state->orig_dst);
  9932. intel_plane->crtc_h = drm_rect_height(&state->orig_dst);
  9933. intel_plane->src_x = state->orig_src.x1;
  9934. intel_plane->src_y = state->orig_src.y1;
  9935. intel_plane->src_w = drm_rect_width(&state->orig_src);
  9936. intel_plane->src_h = drm_rect_height(&state->orig_src);
  9937. intel_plane->obj = obj;
  9938. if (intel_crtc->active) {
  9939. /*
  9940. * FBC does not work on some platforms for rotated
  9941. * planes, so disable it when rotation is not 0 and
  9942. * update it when rotation is set back to 0.
  9943. *
  9944. * FIXME: This is redundant with the fbc update done in
  9945. * the primary plane enable function except that that
  9946. * one is done too late. We eventually need to unify
  9947. * this.
  9948. */
  9949. if (intel_crtc->primary_enabled &&
  9950. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  9951. dev_priv->fbc.plane == intel_crtc->plane &&
  9952. intel_plane->rotation != BIT(DRM_ROTATE_0)) {
  9953. intel_disable_fbc(dev);
  9954. }
  9955. if (state->visible) {
  9956. bool was_enabled = intel_crtc->primary_enabled;
  9957. /* FIXME: kill this fastboot hack */
  9958. intel_update_pipe_size(intel_crtc);
  9959. intel_crtc->primary_enabled = true;
  9960. dev_priv->display.update_primary_plane(crtc, plane->fb,
  9961. crtc->x, crtc->y);
  9962. /*
  9963. * BDW signals flip done immediately if the plane
  9964. * is disabled, even if the plane enable is already
  9965. * armed to occur at the next vblank :(
  9966. */
  9967. if (IS_BROADWELL(dev) && !was_enabled)
  9968. intel_wait_for_vblank(dev, intel_crtc->pipe);
  9969. } else {
  9970. /*
  9971. * If clipping results in a non-visible primary plane,
  9972. * we'll disable the primary plane. Note that this is
  9973. * a bit different than what happens if userspace
  9974. * explicitly disables the plane by passing fb=0
  9975. * because plane->fb still gets set and pinned.
  9976. */
  9977. intel_disable_primary_hw_plane(plane, crtc);
  9978. }
  9979. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  9980. mutex_lock(&dev->struct_mutex);
  9981. intel_update_fbc(dev);
  9982. mutex_unlock(&dev->struct_mutex);
  9983. }
  9984. if (old_fb && old_fb != fb) {
  9985. if (intel_crtc->active)
  9986. intel_wait_for_vblank(dev, intel_crtc->pipe);
  9987. mutex_lock(&dev->struct_mutex);
  9988. intel_unpin_fb_obj(old_obj);
  9989. mutex_unlock(&dev->struct_mutex);
  9990. }
  9991. }
  9992. static int
  9993. intel_primary_plane_setplane(struct drm_plane *plane, struct drm_crtc *crtc,
  9994. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  9995. unsigned int crtc_w, unsigned int crtc_h,
  9996. uint32_t src_x, uint32_t src_y,
  9997. uint32_t src_w, uint32_t src_h)
  9998. {
  9999. struct intel_plane_state state;
  10000. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10001. int ret;
  10002. state.crtc = crtc;
  10003. state.fb = fb;
  10004. /* sample coordinates in 16.16 fixed point */
  10005. state.src.x1 = src_x;
  10006. state.src.x2 = src_x + src_w;
  10007. state.src.y1 = src_y;
  10008. state.src.y2 = src_y + src_h;
  10009. /* integer pixels */
  10010. state.dst.x1 = crtc_x;
  10011. state.dst.x2 = crtc_x + crtc_w;
  10012. state.dst.y1 = crtc_y;
  10013. state.dst.y2 = crtc_y + crtc_h;
  10014. state.clip.x1 = 0;
  10015. state.clip.y1 = 0;
  10016. state.clip.x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0;
  10017. state.clip.y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0;
  10018. state.orig_src = state.src;
  10019. state.orig_dst = state.dst;
  10020. ret = intel_check_primary_plane(plane, &state);
  10021. if (ret)
  10022. return ret;
  10023. ret = intel_prepare_primary_plane(plane, &state);
  10024. if (ret)
  10025. return ret;
  10026. intel_commit_primary_plane(plane, &state);
  10027. return 0;
  10028. }
  10029. /* Common destruction function for both primary and cursor planes */
  10030. static void intel_plane_destroy(struct drm_plane *plane)
  10031. {
  10032. struct intel_plane *intel_plane = to_intel_plane(plane);
  10033. drm_plane_cleanup(plane);
  10034. kfree(intel_plane);
  10035. }
  10036. static const struct drm_plane_funcs intel_primary_plane_funcs = {
  10037. .update_plane = intel_primary_plane_setplane,
  10038. .disable_plane = intel_primary_plane_disable,
  10039. .destroy = intel_plane_destroy,
  10040. .set_property = intel_plane_set_property
  10041. };
  10042. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  10043. int pipe)
  10044. {
  10045. struct intel_plane *primary;
  10046. const uint32_t *intel_primary_formats;
  10047. int num_formats;
  10048. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  10049. if (primary == NULL)
  10050. return NULL;
  10051. primary->can_scale = false;
  10052. primary->max_downscale = 1;
  10053. primary->pipe = pipe;
  10054. primary->plane = pipe;
  10055. primary->rotation = BIT(DRM_ROTATE_0);
  10056. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  10057. primary->plane = !pipe;
  10058. if (INTEL_INFO(dev)->gen <= 3) {
  10059. intel_primary_formats = intel_primary_formats_gen2;
  10060. num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
  10061. } else {
  10062. intel_primary_formats = intel_primary_formats_gen4;
  10063. num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
  10064. }
  10065. drm_universal_plane_init(dev, &primary->base, 0,
  10066. &intel_primary_plane_funcs,
  10067. intel_primary_formats, num_formats,
  10068. DRM_PLANE_TYPE_PRIMARY);
  10069. if (INTEL_INFO(dev)->gen >= 4) {
  10070. if (!dev->mode_config.rotation_property)
  10071. dev->mode_config.rotation_property =
  10072. drm_mode_create_rotation_property(dev,
  10073. BIT(DRM_ROTATE_0) |
  10074. BIT(DRM_ROTATE_180));
  10075. if (dev->mode_config.rotation_property)
  10076. drm_object_attach_property(&primary->base.base,
  10077. dev->mode_config.rotation_property,
  10078. primary->rotation);
  10079. }
  10080. return &primary->base;
  10081. }
  10082. static int
  10083. intel_cursor_plane_disable(struct drm_plane *plane)
  10084. {
  10085. if (!plane->fb)
  10086. return 0;
  10087. BUG_ON(!plane->crtc);
  10088. return intel_crtc_cursor_set_obj(plane->crtc, NULL, 0, 0);
  10089. }
  10090. static int
  10091. intel_check_cursor_plane(struct drm_plane *plane,
  10092. struct intel_plane_state *state)
  10093. {
  10094. struct drm_crtc *crtc = state->crtc;
  10095. struct drm_device *dev = crtc->dev;
  10096. struct drm_framebuffer *fb = state->fb;
  10097. struct drm_rect *dest = &state->dst;
  10098. struct drm_rect *src = &state->src;
  10099. const struct drm_rect *clip = &state->clip;
  10100. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10101. int crtc_w, crtc_h;
  10102. unsigned stride;
  10103. int ret;
  10104. ret = drm_plane_helper_check_update(plane, crtc, fb,
  10105. src, dest, clip,
  10106. DRM_PLANE_HELPER_NO_SCALING,
  10107. DRM_PLANE_HELPER_NO_SCALING,
  10108. true, true, &state->visible);
  10109. if (ret)
  10110. return ret;
  10111. /* if we want to turn off the cursor ignore width and height */
  10112. if (!obj)
  10113. return 0;
  10114. /* Check for which cursor types we support */
  10115. crtc_w = drm_rect_width(&state->orig_dst);
  10116. crtc_h = drm_rect_height(&state->orig_dst);
  10117. if (!cursor_size_ok(dev, crtc_w, crtc_h)) {
  10118. DRM_DEBUG("Cursor dimension not supported\n");
  10119. return -EINVAL;
  10120. }
  10121. stride = roundup_pow_of_two(crtc_w) * 4;
  10122. if (obj->base.size < stride * crtc_h) {
  10123. DRM_DEBUG_KMS("buffer is too small\n");
  10124. return -ENOMEM;
  10125. }
  10126. if (fb == crtc->cursor->fb)
  10127. return 0;
  10128. /* we only need to pin inside GTT if cursor is non-phy */
  10129. mutex_lock(&dev->struct_mutex);
  10130. if (!INTEL_INFO(dev)->cursor_needs_physical && obj->tiling_mode) {
  10131. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  10132. ret = -EINVAL;
  10133. }
  10134. mutex_unlock(&dev->struct_mutex);
  10135. return ret;
  10136. }
  10137. static int
  10138. intel_commit_cursor_plane(struct drm_plane *plane,
  10139. struct intel_plane_state *state)
  10140. {
  10141. struct drm_crtc *crtc = state->crtc;
  10142. struct drm_framebuffer *fb = state->fb;
  10143. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10144. struct intel_plane *intel_plane = to_intel_plane(plane);
  10145. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10146. struct drm_i915_gem_object *obj = intel_fb->obj;
  10147. int crtc_w, crtc_h;
  10148. crtc->cursor_x = state->orig_dst.x1;
  10149. crtc->cursor_y = state->orig_dst.y1;
  10150. intel_plane->crtc_x = state->orig_dst.x1;
  10151. intel_plane->crtc_y = state->orig_dst.y1;
  10152. intel_plane->crtc_w = drm_rect_width(&state->orig_dst);
  10153. intel_plane->crtc_h = drm_rect_height(&state->orig_dst);
  10154. intel_plane->src_x = state->orig_src.x1;
  10155. intel_plane->src_y = state->orig_src.y1;
  10156. intel_plane->src_w = drm_rect_width(&state->orig_src);
  10157. intel_plane->src_h = drm_rect_height(&state->orig_src);
  10158. intel_plane->obj = obj;
  10159. if (fb != crtc->cursor->fb) {
  10160. crtc_w = drm_rect_width(&state->orig_dst);
  10161. crtc_h = drm_rect_height(&state->orig_dst);
  10162. return intel_crtc_cursor_set_obj(crtc, obj, crtc_w, crtc_h);
  10163. } else {
  10164. intel_crtc_update_cursor(crtc, state->visible);
  10165. intel_frontbuffer_flip(crtc->dev,
  10166. INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe));
  10167. return 0;
  10168. }
  10169. }
  10170. static int
  10171. intel_cursor_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
  10172. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  10173. unsigned int crtc_w, unsigned int crtc_h,
  10174. uint32_t src_x, uint32_t src_y,
  10175. uint32_t src_w, uint32_t src_h)
  10176. {
  10177. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10178. struct intel_plane_state state;
  10179. int ret;
  10180. state.crtc = crtc;
  10181. state.fb = fb;
  10182. /* sample coordinates in 16.16 fixed point */
  10183. state.src.x1 = src_x;
  10184. state.src.x2 = src_x + src_w;
  10185. state.src.y1 = src_y;
  10186. state.src.y2 = src_y + src_h;
  10187. /* integer pixels */
  10188. state.dst.x1 = crtc_x;
  10189. state.dst.x2 = crtc_x + crtc_w;
  10190. state.dst.y1 = crtc_y;
  10191. state.dst.y2 = crtc_y + crtc_h;
  10192. state.clip.x1 = 0;
  10193. state.clip.y1 = 0;
  10194. state.clip.x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0;
  10195. state.clip.y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0;
  10196. state.orig_src = state.src;
  10197. state.orig_dst = state.dst;
  10198. ret = intel_check_cursor_plane(plane, &state);
  10199. if (ret)
  10200. return ret;
  10201. return intel_commit_cursor_plane(plane, &state);
  10202. }
  10203. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  10204. .update_plane = intel_cursor_plane_update,
  10205. .disable_plane = intel_cursor_plane_disable,
  10206. .destroy = intel_plane_destroy,
  10207. .set_property = intel_plane_set_property,
  10208. };
  10209. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  10210. int pipe)
  10211. {
  10212. struct intel_plane *cursor;
  10213. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  10214. if (cursor == NULL)
  10215. return NULL;
  10216. cursor->can_scale = false;
  10217. cursor->max_downscale = 1;
  10218. cursor->pipe = pipe;
  10219. cursor->plane = pipe;
  10220. cursor->rotation = BIT(DRM_ROTATE_0);
  10221. drm_universal_plane_init(dev, &cursor->base, 0,
  10222. &intel_cursor_plane_funcs,
  10223. intel_cursor_formats,
  10224. ARRAY_SIZE(intel_cursor_formats),
  10225. DRM_PLANE_TYPE_CURSOR);
  10226. if (INTEL_INFO(dev)->gen >= 4) {
  10227. if (!dev->mode_config.rotation_property)
  10228. dev->mode_config.rotation_property =
  10229. drm_mode_create_rotation_property(dev,
  10230. BIT(DRM_ROTATE_0) |
  10231. BIT(DRM_ROTATE_180));
  10232. if (dev->mode_config.rotation_property)
  10233. drm_object_attach_property(&cursor->base.base,
  10234. dev->mode_config.rotation_property,
  10235. cursor->rotation);
  10236. }
  10237. return &cursor->base;
  10238. }
  10239. static void intel_crtc_init(struct drm_device *dev, int pipe)
  10240. {
  10241. struct drm_i915_private *dev_priv = dev->dev_private;
  10242. struct intel_crtc *intel_crtc;
  10243. struct drm_plane *primary = NULL;
  10244. struct drm_plane *cursor = NULL;
  10245. int i, ret;
  10246. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  10247. if (intel_crtc == NULL)
  10248. return;
  10249. primary = intel_primary_plane_create(dev, pipe);
  10250. if (!primary)
  10251. goto fail;
  10252. cursor = intel_cursor_plane_create(dev, pipe);
  10253. if (!cursor)
  10254. goto fail;
  10255. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  10256. cursor, &intel_crtc_funcs);
  10257. if (ret)
  10258. goto fail;
  10259. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  10260. for (i = 0; i < 256; i++) {
  10261. intel_crtc->lut_r[i] = i;
  10262. intel_crtc->lut_g[i] = i;
  10263. intel_crtc->lut_b[i] = i;
  10264. }
  10265. /*
  10266. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  10267. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  10268. */
  10269. intel_crtc->pipe = pipe;
  10270. intel_crtc->plane = pipe;
  10271. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  10272. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  10273. intel_crtc->plane = !pipe;
  10274. }
  10275. intel_crtc->cursor_base = ~0;
  10276. intel_crtc->cursor_cntl = ~0;
  10277. intel_crtc->cursor_size = ~0;
  10278. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  10279. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  10280. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  10281. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  10282. INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
  10283. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  10284. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  10285. return;
  10286. fail:
  10287. if (primary)
  10288. drm_plane_cleanup(primary);
  10289. if (cursor)
  10290. drm_plane_cleanup(cursor);
  10291. kfree(intel_crtc);
  10292. }
  10293. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  10294. {
  10295. struct drm_encoder *encoder = connector->base.encoder;
  10296. struct drm_device *dev = connector->base.dev;
  10297. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  10298. if (!encoder || WARN_ON(!encoder->crtc))
  10299. return INVALID_PIPE;
  10300. return to_intel_crtc(encoder->crtc)->pipe;
  10301. }
  10302. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  10303. struct drm_file *file)
  10304. {
  10305. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  10306. struct drm_crtc *drmmode_crtc;
  10307. struct intel_crtc *crtc;
  10308. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  10309. return -ENODEV;
  10310. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  10311. if (!drmmode_crtc) {
  10312. DRM_ERROR("no such CRTC id\n");
  10313. return -ENOENT;
  10314. }
  10315. crtc = to_intel_crtc(drmmode_crtc);
  10316. pipe_from_crtc_id->pipe = crtc->pipe;
  10317. return 0;
  10318. }
  10319. static int intel_encoder_clones(struct intel_encoder *encoder)
  10320. {
  10321. struct drm_device *dev = encoder->base.dev;
  10322. struct intel_encoder *source_encoder;
  10323. int index_mask = 0;
  10324. int entry = 0;
  10325. for_each_intel_encoder(dev, source_encoder) {
  10326. if (encoders_cloneable(encoder, source_encoder))
  10327. index_mask |= (1 << entry);
  10328. entry++;
  10329. }
  10330. return index_mask;
  10331. }
  10332. static bool has_edp_a(struct drm_device *dev)
  10333. {
  10334. struct drm_i915_private *dev_priv = dev->dev_private;
  10335. if (!IS_MOBILE(dev))
  10336. return false;
  10337. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  10338. return false;
  10339. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  10340. return false;
  10341. return true;
  10342. }
  10343. const char *intel_output_name(int output)
  10344. {
  10345. static const char *names[] = {
  10346. [INTEL_OUTPUT_UNUSED] = "Unused",
  10347. [INTEL_OUTPUT_ANALOG] = "Analog",
  10348. [INTEL_OUTPUT_DVO] = "DVO",
  10349. [INTEL_OUTPUT_SDVO] = "SDVO",
  10350. [INTEL_OUTPUT_LVDS] = "LVDS",
  10351. [INTEL_OUTPUT_TVOUT] = "TV",
  10352. [INTEL_OUTPUT_HDMI] = "HDMI",
  10353. [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
  10354. [INTEL_OUTPUT_EDP] = "eDP",
  10355. [INTEL_OUTPUT_DSI] = "DSI",
  10356. [INTEL_OUTPUT_UNKNOWN] = "Unknown",
  10357. };
  10358. if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
  10359. return "Invalid";
  10360. return names[output];
  10361. }
  10362. static bool intel_crt_present(struct drm_device *dev)
  10363. {
  10364. struct drm_i915_private *dev_priv = dev->dev_private;
  10365. if (INTEL_INFO(dev)->gen >= 9)
  10366. return false;
  10367. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  10368. return false;
  10369. if (IS_CHERRYVIEW(dev))
  10370. return false;
  10371. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  10372. return false;
  10373. return true;
  10374. }
  10375. static void intel_setup_outputs(struct drm_device *dev)
  10376. {
  10377. struct drm_i915_private *dev_priv = dev->dev_private;
  10378. struct intel_encoder *encoder;
  10379. bool dpd_is_edp = false;
  10380. intel_lvds_init(dev);
  10381. if (intel_crt_present(dev))
  10382. intel_crt_init(dev);
  10383. if (HAS_DDI(dev)) {
  10384. int found;
  10385. /* Haswell uses DDI functions to detect digital outputs */
  10386. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  10387. /* DDI A only supports eDP */
  10388. if (found)
  10389. intel_ddi_init(dev, PORT_A);
  10390. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  10391. * register */
  10392. found = I915_READ(SFUSE_STRAP);
  10393. if (found & SFUSE_STRAP_DDIB_DETECTED)
  10394. intel_ddi_init(dev, PORT_B);
  10395. if (found & SFUSE_STRAP_DDIC_DETECTED)
  10396. intel_ddi_init(dev, PORT_C);
  10397. if (found & SFUSE_STRAP_DDID_DETECTED)
  10398. intel_ddi_init(dev, PORT_D);
  10399. } else if (HAS_PCH_SPLIT(dev)) {
  10400. int found;
  10401. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  10402. if (has_edp_a(dev))
  10403. intel_dp_init(dev, DP_A, PORT_A);
  10404. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  10405. /* PCH SDVOB multiplex with HDMIB */
  10406. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  10407. if (!found)
  10408. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  10409. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  10410. intel_dp_init(dev, PCH_DP_B, PORT_B);
  10411. }
  10412. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  10413. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  10414. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  10415. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  10416. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  10417. intel_dp_init(dev, PCH_DP_C, PORT_C);
  10418. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  10419. intel_dp_init(dev, PCH_DP_D, PORT_D);
  10420. } else if (IS_VALLEYVIEW(dev)) {
  10421. /*
  10422. * The DP_DETECTED bit is the latched state of the DDC
  10423. * SDA pin at boot. However since eDP doesn't require DDC
  10424. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  10425. * eDP ports may have been muxed to an alternate function.
  10426. * Thus we can't rely on the DP_DETECTED bit alone to detect
  10427. * eDP ports. Consult the VBT as well as DP_DETECTED to
  10428. * detect eDP ports.
  10429. */
  10430. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED)
  10431. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  10432. PORT_B);
  10433. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  10434. intel_dp_is_edp(dev, PORT_B))
  10435. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  10436. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED)
  10437. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  10438. PORT_C);
  10439. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  10440. intel_dp_is_edp(dev, PORT_C))
  10441. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  10442. if (IS_CHERRYVIEW(dev)) {
  10443. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  10444. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  10445. PORT_D);
  10446. /* eDP not supported on port D, so don't check VBT */
  10447. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  10448. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  10449. }
  10450. intel_dsi_init(dev);
  10451. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  10452. bool found = false;
  10453. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10454. DRM_DEBUG_KMS("probing SDVOB\n");
  10455. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  10456. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  10457. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  10458. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  10459. }
  10460. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  10461. intel_dp_init(dev, DP_B, PORT_B);
  10462. }
  10463. /* Before G4X SDVOC doesn't have its own detect register */
  10464. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10465. DRM_DEBUG_KMS("probing SDVOC\n");
  10466. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  10467. }
  10468. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  10469. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  10470. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  10471. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  10472. }
  10473. if (SUPPORTS_INTEGRATED_DP(dev))
  10474. intel_dp_init(dev, DP_C, PORT_C);
  10475. }
  10476. if (SUPPORTS_INTEGRATED_DP(dev) &&
  10477. (I915_READ(DP_D) & DP_DETECTED))
  10478. intel_dp_init(dev, DP_D, PORT_D);
  10479. } else if (IS_GEN2(dev))
  10480. intel_dvo_init(dev);
  10481. if (SUPPORTS_TV(dev))
  10482. intel_tv_init(dev);
  10483. intel_psr_init(dev);
  10484. for_each_intel_encoder(dev, encoder) {
  10485. encoder->base.possible_crtcs = encoder->crtc_mask;
  10486. encoder->base.possible_clones =
  10487. intel_encoder_clones(encoder);
  10488. }
  10489. intel_init_pch_refclk(dev);
  10490. drm_helper_move_panel_connectors_to_head(dev);
  10491. }
  10492. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  10493. {
  10494. struct drm_device *dev = fb->dev;
  10495. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10496. drm_framebuffer_cleanup(fb);
  10497. mutex_lock(&dev->struct_mutex);
  10498. WARN_ON(!intel_fb->obj->framebuffer_references--);
  10499. drm_gem_object_unreference(&intel_fb->obj->base);
  10500. mutex_unlock(&dev->struct_mutex);
  10501. kfree(intel_fb);
  10502. }
  10503. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  10504. struct drm_file *file,
  10505. unsigned int *handle)
  10506. {
  10507. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10508. struct drm_i915_gem_object *obj = intel_fb->obj;
  10509. return drm_gem_handle_create(file, &obj->base, handle);
  10510. }
  10511. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  10512. .destroy = intel_user_framebuffer_destroy,
  10513. .create_handle = intel_user_framebuffer_create_handle,
  10514. };
  10515. static int intel_framebuffer_init(struct drm_device *dev,
  10516. struct intel_framebuffer *intel_fb,
  10517. struct drm_mode_fb_cmd2 *mode_cmd,
  10518. struct drm_i915_gem_object *obj)
  10519. {
  10520. int aligned_height;
  10521. int pitch_limit;
  10522. int ret;
  10523. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  10524. if (obj->tiling_mode == I915_TILING_Y) {
  10525. DRM_DEBUG("hardware does not support tiling Y\n");
  10526. return -EINVAL;
  10527. }
  10528. if (mode_cmd->pitches[0] & 63) {
  10529. DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
  10530. mode_cmd->pitches[0]);
  10531. return -EINVAL;
  10532. }
  10533. if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
  10534. pitch_limit = 32*1024;
  10535. } else if (INTEL_INFO(dev)->gen >= 4) {
  10536. if (obj->tiling_mode)
  10537. pitch_limit = 16*1024;
  10538. else
  10539. pitch_limit = 32*1024;
  10540. } else if (INTEL_INFO(dev)->gen >= 3) {
  10541. if (obj->tiling_mode)
  10542. pitch_limit = 8*1024;
  10543. else
  10544. pitch_limit = 16*1024;
  10545. } else
  10546. /* XXX DSPC is limited to 4k tiled */
  10547. pitch_limit = 8*1024;
  10548. if (mode_cmd->pitches[0] > pitch_limit) {
  10549. DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
  10550. obj->tiling_mode ? "tiled" : "linear",
  10551. mode_cmd->pitches[0], pitch_limit);
  10552. return -EINVAL;
  10553. }
  10554. if (obj->tiling_mode != I915_TILING_NONE &&
  10555. mode_cmd->pitches[0] != obj->stride) {
  10556. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  10557. mode_cmd->pitches[0], obj->stride);
  10558. return -EINVAL;
  10559. }
  10560. /* Reject formats not supported by any plane early. */
  10561. switch (mode_cmd->pixel_format) {
  10562. case DRM_FORMAT_C8:
  10563. case DRM_FORMAT_RGB565:
  10564. case DRM_FORMAT_XRGB8888:
  10565. case DRM_FORMAT_ARGB8888:
  10566. break;
  10567. case DRM_FORMAT_XRGB1555:
  10568. case DRM_FORMAT_ARGB1555:
  10569. if (INTEL_INFO(dev)->gen > 3) {
  10570. DRM_DEBUG("unsupported pixel format: %s\n",
  10571. drm_get_format_name(mode_cmd->pixel_format));
  10572. return -EINVAL;
  10573. }
  10574. break;
  10575. case DRM_FORMAT_XBGR8888:
  10576. case DRM_FORMAT_ABGR8888:
  10577. case DRM_FORMAT_XRGB2101010:
  10578. case DRM_FORMAT_ARGB2101010:
  10579. case DRM_FORMAT_XBGR2101010:
  10580. case DRM_FORMAT_ABGR2101010:
  10581. if (INTEL_INFO(dev)->gen < 4) {
  10582. DRM_DEBUG("unsupported pixel format: %s\n",
  10583. drm_get_format_name(mode_cmd->pixel_format));
  10584. return -EINVAL;
  10585. }
  10586. break;
  10587. case DRM_FORMAT_YUYV:
  10588. case DRM_FORMAT_UYVY:
  10589. case DRM_FORMAT_YVYU:
  10590. case DRM_FORMAT_VYUY:
  10591. if (INTEL_INFO(dev)->gen < 5) {
  10592. DRM_DEBUG("unsupported pixel format: %s\n",
  10593. drm_get_format_name(mode_cmd->pixel_format));
  10594. return -EINVAL;
  10595. }
  10596. break;
  10597. default:
  10598. DRM_DEBUG("unsupported pixel format: %s\n",
  10599. drm_get_format_name(mode_cmd->pixel_format));
  10600. return -EINVAL;
  10601. }
  10602. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  10603. if (mode_cmd->offsets[0] != 0)
  10604. return -EINVAL;
  10605. aligned_height = intel_align_height(dev, mode_cmd->height,
  10606. obj->tiling_mode);
  10607. /* FIXME drm helper for size checks (especially planar formats)? */
  10608. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  10609. return -EINVAL;
  10610. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  10611. intel_fb->obj = obj;
  10612. intel_fb->obj->framebuffer_references++;
  10613. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  10614. if (ret) {
  10615. DRM_ERROR("framebuffer init failed %d\n", ret);
  10616. return ret;
  10617. }
  10618. return 0;
  10619. }
  10620. static struct drm_framebuffer *
  10621. intel_user_framebuffer_create(struct drm_device *dev,
  10622. struct drm_file *filp,
  10623. struct drm_mode_fb_cmd2 *mode_cmd)
  10624. {
  10625. struct drm_i915_gem_object *obj;
  10626. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  10627. mode_cmd->handles[0]));
  10628. if (&obj->base == NULL)
  10629. return ERR_PTR(-ENOENT);
  10630. return intel_framebuffer_create(dev, mode_cmd, obj);
  10631. }
  10632. #ifndef CONFIG_DRM_I915_FBDEV
  10633. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  10634. {
  10635. }
  10636. #endif
  10637. static const struct drm_mode_config_funcs intel_mode_funcs = {
  10638. .fb_create = intel_user_framebuffer_create,
  10639. .output_poll_changed = intel_fbdev_output_poll_changed,
  10640. };
  10641. /* Set up chip specific display functions */
  10642. static void intel_init_display(struct drm_device *dev)
  10643. {
  10644. struct drm_i915_private *dev_priv = dev->dev_private;
  10645. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  10646. dev_priv->display.find_dpll = g4x_find_best_dpll;
  10647. else if (IS_CHERRYVIEW(dev))
  10648. dev_priv->display.find_dpll = chv_find_best_dpll;
  10649. else if (IS_VALLEYVIEW(dev))
  10650. dev_priv->display.find_dpll = vlv_find_best_dpll;
  10651. else if (IS_PINEVIEW(dev))
  10652. dev_priv->display.find_dpll = pnv_find_best_dpll;
  10653. else
  10654. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  10655. if (HAS_DDI(dev)) {
  10656. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  10657. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  10658. dev_priv->display.crtc_compute_clock =
  10659. haswell_crtc_compute_clock;
  10660. dev_priv->display.crtc_enable = haswell_crtc_enable;
  10661. dev_priv->display.crtc_disable = haswell_crtc_disable;
  10662. dev_priv->display.off = ironlake_crtc_off;
  10663. if (INTEL_INFO(dev)->gen >= 9)
  10664. dev_priv->display.update_primary_plane =
  10665. skylake_update_primary_plane;
  10666. else
  10667. dev_priv->display.update_primary_plane =
  10668. ironlake_update_primary_plane;
  10669. } else if (HAS_PCH_SPLIT(dev)) {
  10670. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  10671. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  10672. dev_priv->display.crtc_compute_clock =
  10673. ironlake_crtc_compute_clock;
  10674. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  10675. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  10676. dev_priv->display.off = ironlake_crtc_off;
  10677. dev_priv->display.update_primary_plane =
  10678. ironlake_update_primary_plane;
  10679. } else if (IS_VALLEYVIEW(dev)) {
  10680. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10681. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  10682. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  10683. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  10684. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10685. dev_priv->display.off = i9xx_crtc_off;
  10686. dev_priv->display.update_primary_plane =
  10687. i9xx_update_primary_plane;
  10688. } else {
  10689. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10690. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  10691. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  10692. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  10693. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10694. dev_priv->display.off = i9xx_crtc_off;
  10695. dev_priv->display.update_primary_plane =
  10696. i9xx_update_primary_plane;
  10697. }
  10698. /* Returns the core display clock speed */
  10699. if (IS_VALLEYVIEW(dev))
  10700. dev_priv->display.get_display_clock_speed =
  10701. valleyview_get_display_clock_speed;
  10702. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  10703. dev_priv->display.get_display_clock_speed =
  10704. i945_get_display_clock_speed;
  10705. else if (IS_I915G(dev))
  10706. dev_priv->display.get_display_clock_speed =
  10707. i915_get_display_clock_speed;
  10708. else if (IS_I945GM(dev) || IS_845G(dev))
  10709. dev_priv->display.get_display_clock_speed =
  10710. i9xx_misc_get_display_clock_speed;
  10711. else if (IS_PINEVIEW(dev))
  10712. dev_priv->display.get_display_clock_speed =
  10713. pnv_get_display_clock_speed;
  10714. else if (IS_I915GM(dev))
  10715. dev_priv->display.get_display_clock_speed =
  10716. i915gm_get_display_clock_speed;
  10717. else if (IS_I865G(dev))
  10718. dev_priv->display.get_display_clock_speed =
  10719. i865_get_display_clock_speed;
  10720. else if (IS_I85X(dev))
  10721. dev_priv->display.get_display_clock_speed =
  10722. i855_get_display_clock_speed;
  10723. else /* 852, 830 */
  10724. dev_priv->display.get_display_clock_speed =
  10725. i830_get_display_clock_speed;
  10726. if (IS_GEN5(dev)) {
  10727. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  10728. } else if (IS_GEN6(dev)) {
  10729. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  10730. } else if (IS_IVYBRIDGE(dev)) {
  10731. /* FIXME: detect B0+ stepping and use auto training */
  10732. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  10733. dev_priv->display.modeset_global_resources =
  10734. ivb_modeset_global_resources;
  10735. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  10736. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  10737. } else if (IS_VALLEYVIEW(dev)) {
  10738. dev_priv->display.modeset_global_resources =
  10739. valleyview_modeset_global_resources;
  10740. }
  10741. /* Default just returns -ENODEV to indicate unsupported */
  10742. dev_priv->display.queue_flip = intel_default_queue_flip;
  10743. switch (INTEL_INFO(dev)->gen) {
  10744. case 2:
  10745. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  10746. break;
  10747. case 3:
  10748. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  10749. break;
  10750. case 4:
  10751. case 5:
  10752. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  10753. break;
  10754. case 6:
  10755. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  10756. break;
  10757. case 7:
  10758. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  10759. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  10760. break;
  10761. case 9:
  10762. dev_priv->display.queue_flip = intel_gen9_queue_flip;
  10763. break;
  10764. }
  10765. intel_panel_init_backlight_funcs(dev);
  10766. mutex_init(&dev_priv->pps_mutex);
  10767. }
  10768. /*
  10769. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  10770. * resume, or other times. This quirk makes sure that's the case for
  10771. * affected systems.
  10772. */
  10773. static void quirk_pipea_force(struct drm_device *dev)
  10774. {
  10775. struct drm_i915_private *dev_priv = dev->dev_private;
  10776. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  10777. DRM_INFO("applying pipe a force quirk\n");
  10778. }
  10779. static void quirk_pipeb_force(struct drm_device *dev)
  10780. {
  10781. struct drm_i915_private *dev_priv = dev->dev_private;
  10782. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  10783. DRM_INFO("applying pipe b force quirk\n");
  10784. }
  10785. /*
  10786. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  10787. */
  10788. static void quirk_ssc_force_disable(struct drm_device *dev)
  10789. {
  10790. struct drm_i915_private *dev_priv = dev->dev_private;
  10791. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  10792. DRM_INFO("applying lvds SSC disable quirk\n");
  10793. }
  10794. /*
  10795. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  10796. * brightness value
  10797. */
  10798. static void quirk_invert_brightness(struct drm_device *dev)
  10799. {
  10800. struct drm_i915_private *dev_priv = dev->dev_private;
  10801. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  10802. DRM_INFO("applying inverted panel brightness quirk\n");
  10803. }
  10804. /* Some VBT's incorrectly indicate no backlight is present */
  10805. static void quirk_backlight_present(struct drm_device *dev)
  10806. {
  10807. struct drm_i915_private *dev_priv = dev->dev_private;
  10808. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  10809. DRM_INFO("applying backlight present quirk\n");
  10810. }
  10811. struct intel_quirk {
  10812. int device;
  10813. int subsystem_vendor;
  10814. int subsystem_device;
  10815. void (*hook)(struct drm_device *dev);
  10816. };
  10817. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  10818. struct intel_dmi_quirk {
  10819. void (*hook)(struct drm_device *dev);
  10820. const struct dmi_system_id (*dmi_id_list)[];
  10821. };
  10822. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  10823. {
  10824. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  10825. return 1;
  10826. }
  10827. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  10828. {
  10829. .dmi_id_list = &(const struct dmi_system_id[]) {
  10830. {
  10831. .callback = intel_dmi_reverse_brightness,
  10832. .ident = "NCR Corporation",
  10833. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  10834. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  10835. },
  10836. },
  10837. { } /* terminating entry */
  10838. },
  10839. .hook = quirk_invert_brightness,
  10840. },
  10841. };
  10842. static struct intel_quirk intel_quirks[] = {
  10843. /* HP Mini needs pipe A force quirk (LP: #322104) */
  10844. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  10845. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  10846. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  10847. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  10848. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  10849. /* 830 needs to leave pipe A & dpll A up */
  10850. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  10851. /* 830 needs to leave pipe B & dpll B up */
  10852. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  10853. /* Lenovo U160 cannot use SSC on LVDS */
  10854. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  10855. /* Sony Vaio Y cannot use SSC on LVDS */
  10856. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  10857. /* Acer Aspire 5734Z must invert backlight brightness */
  10858. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  10859. /* Acer/eMachines G725 */
  10860. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  10861. /* Acer/eMachines e725 */
  10862. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  10863. /* Acer/Packard Bell NCL20 */
  10864. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  10865. /* Acer Aspire 4736Z */
  10866. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  10867. /* Acer Aspire 5336 */
  10868. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  10869. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  10870. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  10871. /* Acer C720 Chromebook (Core i3 4005U) */
  10872. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  10873. /* Apple Macbook 2,1 (Core 2 T7400) */
  10874. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  10875. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  10876. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  10877. /* HP Chromebook 14 (Celeron 2955U) */
  10878. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  10879. };
  10880. static void intel_init_quirks(struct drm_device *dev)
  10881. {
  10882. struct pci_dev *d = dev->pdev;
  10883. int i;
  10884. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  10885. struct intel_quirk *q = &intel_quirks[i];
  10886. if (d->device == q->device &&
  10887. (d->subsystem_vendor == q->subsystem_vendor ||
  10888. q->subsystem_vendor == PCI_ANY_ID) &&
  10889. (d->subsystem_device == q->subsystem_device ||
  10890. q->subsystem_device == PCI_ANY_ID))
  10891. q->hook(dev);
  10892. }
  10893. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  10894. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  10895. intel_dmi_quirks[i].hook(dev);
  10896. }
  10897. }
  10898. /* Disable the VGA plane that we never use */
  10899. static void i915_disable_vga(struct drm_device *dev)
  10900. {
  10901. struct drm_i915_private *dev_priv = dev->dev_private;
  10902. u8 sr1;
  10903. u32 vga_reg = i915_vgacntrl_reg(dev);
  10904. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  10905. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  10906. outb(SR01, VGA_SR_INDEX);
  10907. sr1 = inb(VGA_SR_DATA);
  10908. outb(sr1 | 1<<5, VGA_SR_DATA);
  10909. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  10910. udelay(300);
  10911. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  10912. POSTING_READ(vga_reg);
  10913. }
  10914. void intel_modeset_init_hw(struct drm_device *dev)
  10915. {
  10916. intel_prepare_ddi(dev);
  10917. if (IS_VALLEYVIEW(dev))
  10918. vlv_update_cdclk(dev);
  10919. intel_init_clock_gating(dev);
  10920. intel_enable_gt_powersave(dev);
  10921. }
  10922. void intel_modeset_init(struct drm_device *dev)
  10923. {
  10924. struct drm_i915_private *dev_priv = dev->dev_private;
  10925. int sprite, ret;
  10926. enum pipe pipe;
  10927. struct intel_crtc *crtc;
  10928. drm_mode_config_init(dev);
  10929. dev->mode_config.min_width = 0;
  10930. dev->mode_config.min_height = 0;
  10931. dev->mode_config.preferred_depth = 24;
  10932. dev->mode_config.prefer_shadow = 1;
  10933. dev->mode_config.funcs = &intel_mode_funcs;
  10934. intel_init_quirks(dev);
  10935. intel_init_pm(dev);
  10936. if (INTEL_INFO(dev)->num_pipes == 0)
  10937. return;
  10938. intel_init_display(dev);
  10939. intel_init_audio(dev);
  10940. if (IS_GEN2(dev)) {
  10941. dev->mode_config.max_width = 2048;
  10942. dev->mode_config.max_height = 2048;
  10943. } else if (IS_GEN3(dev)) {
  10944. dev->mode_config.max_width = 4096;
  10945. dev->mode_config.max_height = 4096;
  10946. } else {
  10947. dev->mode_config.max_width = 8192;
  10948. dev->mode_config.max_height = 8192;
  10949. }
  10950. if (IS_845G(dev) || IS_I865G(dev)) {
  10951. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  10952. dev->mode_config.cursor_height = 1023;
  10953. } else if (IS_GEN2(dev)) {
  10954. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  10955. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  10956. } else {
  10957. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  10958. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  10959. }
  10960. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  10961. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  10962. INTEL_INFO(dev)->num_pipes,
  10963. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  10964. for_each_pipe(dev_priv, pipe) {
  10965. intel_crtc_init(dev, pipe);
  10966. for_each_sprite(pipe, sprite) {
  10967. ret = intel_plane_init(dev, pipe, sprite);
  10968. if (ret)
  10969. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  10970. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  10971. }
  10972. }
  10973. intel_init_dpio(dev);
  10974. intel_shared_dpll_init(dev);
  10975. /* Just disable it once at startup */
  10976. i915_disable_vga(dev);
  10977. intel_setup_outputs(dev);
  10978. /* Just in case the BIOS is doing something questionable. */
  10979. intel_disable_fbc(dev);
  10980. drm_modeset_lock_all(dev);
  10981. intel_modeset_setup_hw_state(dev, false);
  10982. drm_modeset_unlock_all(dev);
  10983. for_each_intel_crtc(dev, crtc) {
  10984. if (!crtc->active)
  10985. continue;
  10986. /*
  10987. * Note that reserving the BIOS fb up front prevents us
  10988. * from stuffing other stolen allocations like the ring
  10989. * on top. This prevents some ugliness at boot time, and
  10990. * can even allow for smooth boot transitions if the BIOS
  10991. * fb is large enough for the active pipe configuration.
  10992. */
  10993. if (dev_priv->display.get_plane_config) {
  10994. dev_priv->display.get_plane_config(crtc,
  10995. &crtc->plane_config);
  10996. /*
  10997. * If the fb is shared between multiple heads, we'll
  10998. * just get the first one.
  10999. */
  11000. intel_find_plane_obj(crtc, &crtc->plane_config);
  11001. }
  11002. }
  11003. }
  11004. static void intel_enable_pipe_a(struct drm_device *dev)
  11005. {
  11006. struct intel_connector *connector;
  11007. struct drm_connector *crt = NULL;
  11008. struct intel_load_detect_pipe load_detect_temp;
  11009. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  11010. /* We can't just switch on the pipe A, we need to set things up with a
  11011. * proper mode and output configuration. As a gross hack, enable pipe A
  11012. * by enabling the load detect pipe once. */
  11013. list_for_each_entry(connector,
  11014. &dev->mode_config.connector_list,
  11015. base.head) {
  11016. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  11017. crt = &connector->base;
  11018. break;
  11019. }
  11020. }
  11021. if (!crt)
  11022. return;
  11023. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  11024. intel_release_load_detect_pipe(crt, &load_detect_temp);
  11025. }
  11026. static bool
  11027. intel_check_plane_mapping(struct intel_crtc *crtc)
  11028. {
  11029. struct drm_device *dev = crtc->base.dev;
  11030. struct drm_i915_private *dev_priv = dev->dev_private;
  11031. u32 reg, val;
  11032. if (INTEL_INFO(dev)->num_pipes == 1)
  11033. return true;
  11034. reg = DSPCNTR(!crtc->plane);
  11035. val = I915_READ(reg);
  11036. if ((val & DISPLAY_PLANE_ENABLE) &&
  11037. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  11038. return false;
  11039. return true;
  11040. }
  11041. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  11042. {
  11043. struct drm_device *dev = crtc->base.dev;
  11044. struct drm_i915_private *dev_priv = dev->dev_private;
  11045. u32 reg;
  11046. /* Clear any frame start delays used for debugging left by the BIOS */
  11047. reg = PIPECONF(crtc->config.cpu_transcoder);
  11048. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  11049. /* restore vblank interrupts to correct state */
  11050. if (crtc->active) {
  11051. update_scanline_offset(crtc);
  11052. drm_vblank_on(dev, crtc->pipe);
  11053. } else
  11054. drm_vblank_off(dev, crtc->pipe);
  11055. /* We need to sanitize the plane -> pipe mapping first because this will
  11056. * disable the crtc (and hence change the state) if it is wrong. Note
  11057. * that gen4+ has a fixed plane -> pipe mapping. */
  11058. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  11059. struct intel_connector *connector;
  11060. bool plane;
  11061. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  11062. crtc->base.base.id);
  11063. /* Pipe has the wrong plane attached and the plane is active.
  11064. * Temporarily change the plane mapping and disable everything
  11065. * ... */
  11066. plane = crtc->plane;
  11067. crtc->plane = !plane;
  11068. crtc->primary_enabled = true;
  11069. dev_priv->display.crtc_disable(&crtc->base);
  11070. crtc->plane = plane;
  11071. /* ... and break all links. */
  11072. list_for_each_entry(connector, &dev->mode_config.connector_list,
  11073. base.head) {
  11074. if (connector->encoder->base.crtc != &crtc->base)
  11075. continue;
  11076. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11077. connector->base.encoder = NULL;
  11078. }
  11079. /* multiple connectors may have the same encoder:
  11080. * handle them and break crtc link separately */
  11081. list_for_each_entry(connector, &dev->mode_config.connector_list,
  11082. base.head)
  11083. if (connector->encoder->base.crtc == &crtc->base) {
  11084. connector->encoder->base.crtc = NULL;
  11085. connector->encoder->connectors_active = false;
  11086. }
  11087. WARN_ON(crtc->active);
  11088. crtc->base.enabled = false;
  11089. }
  11090. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  11091. crtc->pipe == PIPE_A && !crtc->active) {
  11092. /* BIOS forgot to enable pipe A, this mostly happens after
  11093. * resume. Force-enable the pipe to fix this, the update_dpms
  11094. * call below we restore the pipe to the right state, but leave
  11095. * the required bits on. */
  11096. intel_enable_pipe_a(dev);
  11097. }
  11098. /* Adjust the state of the output pipe according to whether we
  11099. * have active connectors/encoders. */
  11100. intel_crtc_update_dpms(&crtc->base);
  11101. if (crtc->active != crtc->base.enabled) {
  11102. struct intel_encoder *encoder;
  11103. /* This can happen either due to bugs in the get_hw_state
  11104. * functions or because the pipe is force-enabled due to the
  11105. * pipe A quirk. */
  11106. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  11107. crtc->base.base.id,
  11108. crtc->base.enabled ? "enabled" : "disabled",
  11109. crtc->active ? "enabled" : "disabled");
  11110. crtc->base.enabled = crtc->active;
  11111. /* Because we only establish the connector -> encoder ->
  11112. * crtc links if something is active, this means the
  11113. * crtc is now deactivated. Break the links. connector
  11114. * -> encoder links are only establish when things are
  11115. * actually up, hence no need to break them. */
  11116. WARN_ON(crtc->active);
  11117. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  11118. WARN_ON(encoder->connectors_active);
  11119. encoder->base.crtc = NULL;
  11120. }
  11121. }
  11122. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  11123. /*
  11124. * We start out with underrun reporting disabled to avoid races.
  11125. * For correct bookkeeping mark this on active crtcs.
  11126. *
  11127. * Also on gmch platforms we dont have any hardware bits to
  11128. * disable the underrun reporting. Which means we need to start
  11129. * out with underrun reporting disabled also on inactive pipes,
  11130. * since otherwise we'll complain about the garbage we read when
  11131. * e.g. coming up after runtime pm.
  11132. *
  11133. * No protection against concurrent access is required - at
  11134. * worst a fifo underrun happens which also sets this to false.
  11135. */
  11136. crtc->cpu_fifo_underrun_disabled = true;
  11137. crtc->pch_fifo_underrun_disabled = true;
  11138. }
  11139. }
  11140. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  11141. {
  11142. struct intel_connector *connector;
  11143. struct drm_device *dev = encoder->base.dev;
  11144. /* We need to check both for a crtc link (meaning that the
  11145. * encoder is active and trying to read from a pipe) and the
  11146. * pipe itself being active. */
  11147. bool has_active_crtc = encoder->base.crtc &&
  11148. to_intel_crtc(encoder->base.crtc)->active;
  11149. if (encoder->connectors_active && !has_active_crtc) {
  11150. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  11151. encoder->base.base.id,
  11152. encoder->base.name);
  11153. /* Connector is active, but has no active pipe. This is
  11154. * fallout from our resume register restoring. Disable
  11155. * the encoder manually again. */
  11156. if (encoder->base.crtc) {
  11157. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  11158. encoder->base.base.id,
  11159. encoder->base.name);
  11160. encoder->disable(encoder);
  11161. if (encoder->post_disable)
  11162. encoder->post_disable(encoder);
  11163. }
  11164. encoder->base.crtc = NULL;
  11165. encoder->connectors_active = false;
  11166. /* Inconsistent output/port/pipe state happens presumably due to
  11167. * a bug in one of the get_hw_state functions. Or someplace else
  11168. * in our code, like the register restore mess on resume. Clamp
  11169. * things to off as a safer default. */
  11170. list_for_each_entry(connector,
  11171. &dev->mode_config.connector_list,
  11172. base.head) {
  11173. if (connector->encoder != encoder)
  11174. continue;
  11175. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11176. connector->base.encoder = NULL;
  11177. }
  11178. }
  11179. /* Enabled encoders without active connectors will be fixed in
  11180. * the crtc fixup. */
  11181. }
  11182. void i915_redisable_vga_power_on(struct drm_device *dev)
  11183. {
  11184. struct drm_i915_private *dev_priv = dev->dev_private;
  11185. u32 vga_reg = i915_vgacntrl_reg(dev);
  11186. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  11187. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  11188. i915_disable_vga(dev);
  11189. }
  11190. }
  11191. void i915_redisable_vga(struct drm_device *dev)
  11192. {
  11193. struct drm_i915_private *dev_priv = dev->dev_private;
  11194. /* This function can be called both from intel_modeset_setup_hw_state or
  11195. * at a very early point in our resume sequence, where the power well
  11196. * structures are not yet restored. Since this function is at a very
  11197. * paranoid "someone might have enabled VGA while we were not looking"
  11198. * level, just check if the power well is enabled instead of trying to
  11199. * follow the "don't touch the power well if we don't need it" policy
  11200. * the rest of the driver uses. */
  11201. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  11202. return;
  11203. i915_redisable_vga_power_on(dev);
  11204. }
  11205. static bool primary_get_hw_state(struct intel_crtc *crtc)
  11206. {
  11207. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  11208. if (!crtc->active)
  11209. return false;
  11210. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  11211. }
  11212. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  11213. {
  11214. struct drm_i915_private *dev_priv = dev->dev_private;
  11215. enum pipe pipe;
  11216. struct intel_crtc *crtc;
  11217. struct intel_encoder *encoder;
  11218. struct intel_connector *connector;
  11219. int i;
  11220. for_each_intel_crtc(dev, crtc) {
  11221. memset(&crtc->config, 0, sizeof(crtc->config));
  11222. crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  11223. crtc->active = dev_priv->display.get_pipe_config(crtc,
  11224. &crtc->config);
  11225. crtc->base.enabled = crtc->active;
  11226. crtc->primary_enabled = primary_get_hw_state(crtc);
  11227. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  11228. crtc->base.base.id,
  11229. crtc->active ? "enabled" : "disabled");
  11230. }
  11231. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11232. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11233. pll->on = pll->get_hw_state(dev_priv, pll,
  11234. &pll->config.hw_state);
  11235. pll->active = 0;
  11236. pll->config.crtc_mask = 0;
  11237. for_each_intel_crtc(dev, crtc) {
  11238. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  11239. pll->active++;
  11240. pll->config.crtc_mask |= 1 << crtc->pipe;
  11241. }
  11242. }
  11243. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  11244. pll->name, pll->config.crtc_mask, pll->on);
  11245. if (pll->config.crtc_mask)
  11246. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  11247. }
  11248. for_each_intel_encoder(dev, encoder) {
  11249. pipe = 0;
  11250. if (encoder->get_hw_state(encoder, &pipe)) {
  11251. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11252. encoder->base.crtc = &crtc->base;
  11253. encoder->get_config(encoder, &crtc->config);
  11254. } else {
  11255. encoder->base.crtc = NULL;
  11256. }
  11257. encoder->connectors_active = false;
  11258. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  11259. encoder->base.base.id,
  11260. encoder->base.name,
  11261. encoder->base.crtc ? "enabled" : "disabled",
  11262. pipe_name(pipe));
  11263. }
  11264. list_for_each_entry(connector, &dev->mode_config.connector_list,
  11265. base.head) {
  11266. if (connector->get_hw_state(connector)) {
  11267. connector->base.dpms = DRM_MODE_DPMS_ON;
  11268. connector->encoder->connectors_active = true;
  11269. connector->base.encoder = &connector->encoder->base;
  11270. } else {
  11271. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11272. connector->base.encoder = NULL;
  11273. }
  11274. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  11275. connector->base.base.id,
  11276. connector->base.name,
  11277. connector->base.encoder ? "enabled" : "disabled");
  11278. }
  11279. }
  11280. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  11281. * and i915 state tracking structures. */
  11282. void intel_modeset_setup_hw_state(struct drm_device *dev,
  11283. bool force_restore)
  11284. {
  11285. struct drm_i915_private *dev_priv = dev->dev_private;
  11286. enum pipe pipe;
  11287. struct intel_crtc *crtc;
  11288. struct intel_encoder *encoder;
  11289. int i;
  11290. intel_modeset_readout_hw_state(dev);
  11291. /*
  11292. * Now that we have the config, copy it to each CRTC struct
  11293. * Note that this could go away if we move to using crtc_config
  11294. * checking everywhere.
  11295. */
  11296. for_each_intel_crtc(dev, crtc) {
  11297. if (crtc->active && i915.fastboot) {
  11298. intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
  11299. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  11300. crtc->base.base.id);
  11301. drm_mode_debug_printmodeline(&crtc->base.mode);
  11302. }
  11303. }
  11304. /* HW state is read out, now we need to sanitize this mess. */
  11305. for_each_intel_encoder(dev, encoder) {
  11306. intel_sanitize_encoder(encoder);
  11307. }
  11308. for_each_pipe(dev_priv, pipe) {
  11309. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11310. intel_sanitize_crtc(crtc);
  11311. intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
  11312. }
  11313. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11314. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11315. if (!pll->on || pll->active)
  11316. continue;
  11317. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  11318. pll->disable(dev_priv, pll);
  11319. pll->on = false;
  11320. }
  11321. if (IS_GEN9(dev))
  11322. skl_wm_get_hw_state(dev);
  11323. else if (HAS_PCH_SPLIT(dev))
  11324. ilk_wm_get_hw_state(dev);
  11325. if (force_restore) {
  11326. i915_redisable_vga(dev);
  11327. /*
  11328. * We need to use raw interfaces for restoring state to avoid
  11329. * checking (bogus) intermediate states.
  11330. */
  11331. for_each_pipe(dev_priv, pipe) {
  11332. struct drm_crtc *crtc =
  11333. dev_priv->pipe_to_crtc_mapping[pipe];
  11334. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
  11335. crtc->primary->fb);
  11336. }
  11337. } else {
  11338. intel_modeset_update_staged_output_state(dev);
  11339. }
  11340. intel_modeset_check_state(dev);
  11341. }
  11342. void intel_modeset_gem_init(struct drm_device *dev)
  11343. {
  11344. struct drm_i915_private *dev_priv = dev->dev_private;
  11345. struct drm_crtc *c;
  11346. struct drm_i915_gem_object *obj;
  11347. mutex_lock(&dev->struct_mutex);
  11348. intel_init_gt_powersave(dev);
  11349. mutex_unlock(&dev->struct_mutex);
  11350. /*
  11351. * There may be no VBT; and if the BIOS enabled SSC we can
  11352. * just keep using it to avoid unnecessary flicker. Whereas if the
  11353. * BIOS isn't using it, don't assume it will work even if the VBT
  11354. * indicates as much.
  11355. */
  11356. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11357. dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  11358. DREF_SSC1_ENABLE);
  11359. intel_modeset_init_hw(dev);
  11360. intel_setup_overlay(dev);
  11361. /*
  11362. * Make sure any fbs we allocated at startup are properly
  11363. * pinned & fenced. When we do the allocation it's too early
  11364. * for this.
  11365. */
  11366. mutex_lock(&dev->struct_mutex);
  11367. for_each_crtc(dev, c) {
  11368. obj = intel_fb_obj(c->primary->fb);
  11369. if (obj == NULL)
  11370. continue;
  11371. if (intel_pin_and_fence_fb_obj(c->primary,
  11372. c->primary->fb,
  11373. NULL)) {
  11374. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  11375. to_intel_crtc(c)->pipe);
  11376. drm_framebuffer_unreference(c->primary->fb);
  11377. c->primary->fb = NULL;
  11378. }
  11379. }
  11380. mutex_unlock(&dev->struct_mutex);
  11381. intel_backlight_register(dev);
  11382. }
  11383. void intel_connector_unregister(struct intel_connector *intel_connector)
  11384. {
  11385. struct drm_connector *connector = &intel_connector->base;
  11386. intel_panel_destroy_backlight(connector);
  11387. drm_connector_unregister(connector);
  11388. }
  11389. void intel_modeset_cleanup(struct drm_device *dev)
  11390. {
  11391. struct drm_i915_private *dev_priv = dev->dev_private;
  11392. struct drm_connector *connector;
  11393. intel_disable_gt_powersave(dev);
  11394. intel_backlight_unregister(dev);
  11395. /*
  11396. * Interrupts and polling as the first thing to avoid creating havoc.
  11397. * Too much stuff here (turning of connectors, ...) would
  11398. * experience fancy races otherwise.
  11399. */
  11400. intel_irq_uninstall(dev_priv);
  11401. /*
  11402. * Due to the hpd irq storm handling the hotplug work can re-arm the
  11403. * poll handlers. Hence disable polling after hpd handling is shut down.
  11404. */
  11405. drm_kms_helper_poll_fini(dev);
  11406. mutex_lock(&dev->struct_mutex);
  11407. intel_unregister_dsm_handler();
  11408. intel_disable_fbc(dev);
  11409. ironlake_teardown_rc6(dev);
  11410. mutex_unlock(&dev->struct_mutex);
  11411. /* flush any delayed tasks or pending work */
  11412. flush_scheduled_work();
  11413. /* destroy the backlight and sysfs files before encoders/connectors */
  11414. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  11415. struct intel_connector *intel_connector;
  11416. intel_connector = to_intel_connector(connector);
  11417. intel_connector->unregister(intel_connector);
  11418. }
  11419. drm_mode_config_cleanup(dev);
  11420. intel_cleanup_overlay(dev);
  11421. mutex_lock(&dev->struct_mutex);
  11422. intel_cleanup_gt_powersave(dev);
  11423. mutex_unlock(&dev->struct_mutex);
  11424. }
  11425. /*
  11426. * Return which encoder is currently attached for connector.
  11427. */
  11428. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  11429. {
  11430. return &intel_attached_encoder(connector)->base;
  11431. }
  11432. void intel_connector_attach_encoder(struct intel_connector *connector,
  11433. struct intel_encoder *encoder)
  11434. {
  11435. connector->encoder = encoder;
  11436. drm_mode_connector_attach_encoder(&connector->base,
  11437. &encoder->base);
  11438. }
  11439. /*
  11440. * set vga decode state - true == enable VGA decode
  11441. */
  11442. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  11443. {
  11444. struct drm_i915_private *dev_priv = dev->dev_private;
  11445. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  11446. u16 gmch_ctrl;
  11447. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  11448. DRM_ERROR("failed to read control word\n");
  11449. return -EIO;
  11450. }
  11451. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  11452. return 0;
  11453. if (state)
  11454. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  11455. else
  11456. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  11457. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  11458. DRM_ERROR("failed to write control word\n");
  11459. return -EIO;
  11460. }
  11461. return 0;
  11462. }
  11463. struct intel_display_error_state {
  11464. u32 power_well_driver;
  11465. int num_transcoders;
  11466. struct intel_cursor_error_state {
  11467. u32 control;
  11468. u32 position;
  11469. u32 base;
  11470. u32 size;
  11471. } cursor[I915_MAX_PIPES];
  11472. struct intel_pipe_error_state {
  11473. bool power_domain_on;
  11474. u32 source;
  11475. u32 stat;
  11476. } pipe[I915_MAX_PIPES];
  11477. struct intel_plane_error_state {
  11478. u32 control;
  11479. u32 stride;
  11480. u32 size;
  11481. u32 pos;
  11482. u32 addr;
  11483. u32 surface;
  11484. u32 tile_offset;
  11485. } plane[I915_MAX_PIPES];
  11486. struct intel_transcoder_error_state {
  11487. bool power_domain_on;
  11488. enum transcoder cpu_transcoder;
  11489. u32 conf;
  11490. u32 htotal;
  11491. u32 hblank;
  11492. u32 hsync;
  11493. u32 vtotal;
  11494. u32 vblank;
  11495. u32 vsync;
  11496. } transcoder[4];
  11497. };
  11498. struct intel_display_error_state *
  11499. intel_display_capture_error_state(struct drm_device *dev)
  11500. {
  11501. struct drm_i915_private *dev_priv = dev->dev_private;
  11502. struct intel_display_error_state *error;
  11503. int transcoders[] = {
  11504. TRANSCODER_A,
  11505. TRANSCODER_B,
  11506. TRANSCODER_C,
  11507. TRANSCODER_EDP,
  11508. };
  11509. int i;
  11510. if (INTEL_INFO(dev)->num_pipes == 0)
  11511. return NULL;
  11512. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  11513. if (error == NULL)
  11514. return NULL;
  11515. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11516. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  11517. for_each_pipe(dev_priv, i) {
  11518. error->pipe[i].power_domain_on =
  11519. __intel_display_power_is_enabled(dev_priv,
  11520. POWER_DOMAIN_PIPE(i));
  11521. if (!error->pipe[i].power_domain_on)
  11522. continue;
  11523. error->cursor[i].control = I915_READ(CURCNTR(i));
  11524. error->cursor[i].position = I915_READ(CURPOS(i));
  11525. error->cursor[i].base = I915_READ(CURBASE(i));
  11526. error->plane[i].control = I915_READ(DSPCNTR(i));
  11527. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  11528. if (INTEL_INFO(dev)->gen <= 3) {
  11529. error->plane[i].size = I915_READ(DSPSIZE(i));
  11530. error->plane[i].pos = I915_READ(DSPPOS(i));
  11531. }
  11532. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11533. error->plane[i].addr = I915_READ(DSPADDR(i));
  11534. if (INTEL_INFO(dev)->gen >= 4) {
  11535. error->plane[i].surface = I915_READ(DSPSURF(i));
  11536. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  11537. }
  11538. error->pipe[i].source = I915_READ(PIPESRC(i));
  11539. if (HAS_GMCH_DISPLAY(dev))
  11540. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  11541. }
  11542. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  11543. if (HAS_DDI(dev_priv->dev))
  11544. error->num_transcoders++; /* Account for eDP. */
  11545. for (i = 0; i < error->num_transcoders; i++) {
  11546. enum transcoder cpu_transcoder = transcoders[i];
  11547. error->transcoder[i].power_domain_on =
  11548. __intel_display_power_is_enabled(dev_priv,
  11549. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  11550. if (!error->transcoder[i].power_domain_on)
  11551. continue;
  11552. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  11553. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  11554. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  11555. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  11556. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  11557. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  11558. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  11559. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  11560. }
  11561. return error;
  11562. }
  11563. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  11564. void
  11565. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  11566. struct drm_device *dev,
  11567. struct intel_display_error_state *error)
  11568. {
  11569. struct drm_i915_private *dev_priv = dev->dev_private;
  11570. int i;
  11571. if (!error)
  11572. return;
  11573. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  11574. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11575. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  11576. error->power_well_driver);
  11577. for_each_pipe(dev_priv, i) {
  11578. err_printf(m, "Pipe [%d]:\n", i);
  11579. err_printf(m, " Power: %s\n",
  11580. error->pipe[i].power_domain_on ? "on" : "off");
  11581. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  11582. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  11583. err_printf(m, "Plane [%d]:\n", i);
  11584. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  11585. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  11586. if (INTEL_INFO(dev)->gen <= 3) {
  11587. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  11588. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  11589. }
  11590. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11591. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  11592. if (INTEL_INFO(dev)->gen >= 4) {
  11593. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  11594. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  11595. }
  11596. err_printf(m, "Cursor [%d]:\n", i);
  11597. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  11598. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  11599. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  11600. }
  11601. for (i = 0; i < error->num_transcoders; i++) {
  11602. err_printf(m, "CPU transcoder: %c\n",
  11603. transcoder_name(error->transcoder[i].cpu_transcoder));
  11604. err_printf(m, " Power: %s\n",
  11605. error->transcoder[i].power_domain_on ? "on" : "off");
  11606. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  11607. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  11608. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  11609. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  11610. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  11611. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  11612. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  11613. }
  11614. }
  11615. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  11616. {
  11617. struct intel_crtc *crtc;
  11618. for_each_intel_crtc(dev, crtc) {
  11619. struct intel_unpin_work *work;
  11620. spin_lock_irq(&dev->event_lock);
  11621. work = crtc->unpin_work;
  11622. if (work && work->event &&
  11623. work->event->base.file_priv == file) {
  11624. kfree(work->event);
  11625. work->event = NULL;
  11626. }
  11627. spin_unlock_irq(&dev->event_lock);
  11628. }
  11629. }