intel_display.c 384 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911
  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. I915_STATE_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. I915_STATE_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. I915_STATE_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. I915_STATE_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. I915_STATE_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. I915_STATE_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. I915_STATE_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. I915_STATE_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. I915_STATE_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. I915_STATE_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. I915_STATE_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. I915_STATE_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. I915_STATE_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. I915_STATE_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. I915_STATE_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. I915_STATE_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. I915_STATE_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 (I915_STATE_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. I915_STATE_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. I915_STATE_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. I915_STATE_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. I915_STATE_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. I915_STATE_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. I915_STATE_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. I915_STATE_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. I915_STATE_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. I915_STATE_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. if (WARN_ON(!intel_crtc->active))
  1871. return;
  1872. if (!intel_crtc->primary_enabled)
  1873. return;
  1874. intel_crtc->primary_enabled = false;
  1875. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1876. crtc->x, crtc->y);
  1877. }
  1878. static bool need_vtd_wa(struct drm_device *dev)
  1879. {
  1880. #ifdef CONFIG_INTEL_IOMMU
  1881. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1882. return true;
  1883. #endif
  1884. return false;
  1885. }
  1886. static int intel_align_height(struct drm_device *dev, int height, bool tiled)
  1887. {
  1888. int tile_height;
  1889. tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
  1890. return ALIGN(height, tile_height);
  1891. }
  1892. int
  1893. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  1894. struct drm_framebuffer *fb,
  1895. struct intel_engine_cs *pipelined)
  1896. {
  1897. struct drm_device *dev = fb->dev;
  1898. struct drm_i915_private *dev_priv = dev->dev_private;
  1899. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1900. u32 alignment;
  1901. int ret;
  1902. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1903. switch (obj->tiling_mode) {
  1904. case I915_TILING_NONE:
  1905. if (INTEL_INFO(dev)->gen >= 9)
  1906. alignment = 256 * 1024;
  1907. else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1908. alignment = 128 * 1024;
  1909. else if (INTEL_INFO(dev)->gen >= 4)
  1910. alignment = 4 * 1024;
  1911. else
  1912. alignment = 64 * 1024;
  1913. break;
  1914. case I915_TILING_X:
  1915. if (INTEL_INFO(dev)->gen >= 9)
  1916. alignment = 256 * 1024;
  1917. else {
  1918. /* pin() will align the object as required by fence */
  1919. alignment = 0;
  1920. }
  1921. break;
  1922. case I915_TILING_Y:
  1923. WARN(1, "Y tiled bo slipped through, driver bug!\n");
  1924. return -EINVAL;
  1925. default:
  1926. BUG();
  1927. }
  1928. /* Note that the w/a also requires 64 PTE of padding following the
  1929. * bo. We currently fill all unused PTE with the shadow page and so
  1930. * we should always have valid PTE following the scanout preventing
  1931. * the VT-d warning.
  1932. */
  1933. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1934. alignment = 256 * 1024;
  1935. /*
  1936. * Global gtt pte registers are special registers which actually forward
  1937. * writes to a chunk of system memory. Which means that there is no risk
  1938. * that the register values disappear as soon as we call
  1939. * intel_runtime_pm_put(), so it is correct to wrap only the
  1940. * pin/unpin/fence and not more.
  1941. */
  1942. intel_runtime_pm_get(dev_priv);
  1943. dev_priv->mm.interruptible = false;
  1944. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1945. if (ret)
  1946. goto err_interruptible;
  1947. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1948. * fence, whereas 965+ only requires a fence if using
  1949. * framebuffer compression. For simplicity, we always install
  1950. * a fence as the cost is not that onerous.
  1951. */
  1952. ret = i915_gem_object_get_fence(obj);
  1953. if (ret)
  1954. goto err_unpin;
  1955. i915_gem_object_pin_fence(obj);
  1956. dev_priv->mm.interruptible = true;
  1957. intel_runtime_pm_put(dev_priv);
  1958. return 0;
  1959. err_unpin:
  1960. i915_gem_object_unpin_from_display_plane(obj);
  1961. err_interruptible:
  1962. dev_priv->mm.interruptible = true;
  1963. intel_runtime_pm_put(dev_priv);
  1964. return ret;
  1965. }
  1966. void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
  1967. {
  1968. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  1969. i915_gem_object_unpin_fence(obj);
  1970. i915_gem_object_unpin_from_display_plane(obj);
  1971. }
  1972. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  1973. * is assumed to be a power-of-two. */
  1974. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  1975. unsigned int tiling_mode,
  1976. unsigned int cpp,
  1977. unsigned int pitch)
  1978. {
  1979. if (tiling_mode != I915_TILING_NONE) {
  1980. unsigned int tile_rows, tiles;
  1981. tile_rows = *y / 8;
  1982. *y %= 8;
  1983. tiles = *x / (512/cpp);
  1984. *x %= 512/cpp;
  1985. return tile_rows * pitch * 8 + tiles * 4096;
  1986. } else {
  1987. unsigned int offset;
  1988. offset = *y * pitch + *x * cpp;
  1989. *y = 0;
  1990. *x = (offset & 4095) / cpp;
  1991. return offset & -4096;
  1992. }
  1993. }
  1994. int intel_format_to_fourcc(int format)
  1995. {
  1996. switch (format) {
  1997. case DISPPLANE_8BPP:
  1998. return DRM_FORMAT_C8;
  1999. case DISPPLANE_BGRX555:
  2000. return DRM_FORMAT_XRGB1555;
  2001. case DISPPLANE_BGRX565:
  2002. return DRM_FORMAT_RGB565;
  2003. default:
  2004. case DISPPLANE_BGRX888:
  2005. return DRM_FORMAT_XRGB8888;
  2006. case DISPPLANE_RGBX888:
  2007. return DRM_FORMAT_XBGR8888;
  2008. case DISPPLANE_BGRX101010:
  2009. return DRM_FORMAT_XRGB2101010;
  2010. case DISPPLANE_RGBX101010:
  2011. return DRM_FORMAT_XBGR2101010;
  2012. }
  2013. }
  2014. static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
  2015. struct intel_plane_config *plane_config)
  2016. {
  2017. struct drm_device *dev = crtc->base.dev;
  2018. struct drm_i915_gem_object *obj = NULL;
  2019. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2020. u32 base = plane_config->base;
  2021. if (plane_config->size == 0)
  2022. return false;
  2023. obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
  2024. plane_config->size);
  2025. if (!obj)
  2026. return false;
  2027. if (plane_config->tiled) {
  2028. obj->tiling_mode = I915_TILING_X;
  2029. obj->stride = crtc->base.primary->fb->pitches[0];
  2030. }
  2031. mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
  2032. mode_cmd.width = crtc->base.primary->fb->width;
  2033. mode_cmd.height = crtc->base.primary->fb->height;
  2034. mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
  2035. mutex_lock(&dev->struct_mutex);
  2036. if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
  2037. &mode_cmd, obj)) {
  2038. DRM_DEBUG_KMS("intel fb init failed\n");
  2039. goto out_unref_obj;
  2040. }
  2041. obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
  2042. mutex_unlock(&dev->struct_mutex);
  2043. DRM_DEBUG_KMS("plane fb obj %p\n", obj);
  2044. return true;
  2045. out_unref_obj:
  2046. drm_gem_object_unreference(&obj->base);
  2047. mutex_unlock(&dev->struct_mutex);
  2048. return false;
  2049. }
  2050. static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
  2051. struct intel_plane_config *plane_config)
  2052. {
  2053. struct drm_device *dev = intel_crtc->base.dev;
  2054. struct drm_i915_private *dev_priv = dev->dev_private;
  2055. struct drm_crtc *c;
  2056. struct intel_crtc *i;
  2057. struct drm_i915_gem_object *obj;
  2058. if (!intel_crtc->base.primary->fb)
  2059. return;
  2060. if (intel_alloc_plane_obj(intel_crtc, plane_config))
  2061. return;
  2062. kfree(intel_crtc->base.primary->fb);
  2063. intel_crtc->base.primary->fb = NULL;
  2064. /*
  2065. * Failed to alloc the obj, check to see if we should share
  2066. * an fb with another CRTC instead
  2067. */
  2068. for_each_crtc(dev, c) {
  2069. i = to_intel_crtc(c);
  2070. if (c == &intel_crtc->base)
  2071. continue;
  2072. if (!i->active)
  2073. continue;
  2074. obj = intel_fb_obj(c->primary->fb);
  2075. if (obj == NULL)
  2076. continue;
  2077. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2078. if (obj->tiling_mode != I915_TILING_NONE)
  2079. dev_priv->preserve_bios_swizzle = true;
  2080. drm_framebuffer_reference(c->primary->fb);
  2081. intel_crtc->base.primary->fb = c->primary->fb;
  2082. obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  2083. break;
  2084. }
  2085. }
  2086. }
  2087. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2088. struct drm_framebuffer *fb,
  2089. int x, int y)
  2090. {
  2091. struct drm_device *dev = crtc->dev;
  2092. struct drm_i915_private *dev_priv = dev->dev_private;
  2093. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2094. struct drm_i915_gem_object *obj;
  2095. int plane = intel_crtc->plane;
  2096. unsigned long linear_offset;
  2097. u32 dspcntr;
  2098. u32 reg = DSPCNTR(plane);
  2099. int pixel_size;
  2100. if (!intel_crtc->primary_enabled) {
  2101. I915_WRITE(reg, 0);
  2102. if (INTEL_INFO(dev)->gen >= 4)
  2103. I915_WRITE(DSPSURF(plane), 0);
  2104. else
  2105. I915_WRITE(DSPADDR(plane), 0);
  2106. POSTING_READ(reg);
  2107. return;
  2108. }
  2109. obj = intel_fb_obj(fb);
  2110. if (WARN_ON(obj == NULL))
  2111. return;
  2112. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2113. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2114. dspcntr |= DISPLAY_PLANE_ENABLE;
  2115. if (INTEL_INFO(dev)->gen < 4) {
  2116. if (intel_crtc->pipe == PIPE_B)
  2117. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2118. /* pipesrc and dspsize control the size that is scaled from,
  2119. * which should always be the user's requested size.
  2120. */
  2121. I915_WRITE(DSPSIZE(plane),
  2122. ((intel_crtc->config.pipe_src_h - 1) << 16) |
  2123. (intel_crtc->config.pipe_src_w - 1));
  2124. I915_WRITE(DSPPOS(plane), 0);
  2125. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2126. I915_WRITE(PRIMSIZE(plane),
  2127. ((intel_crtc->config.pipe_src_h - 1) << 16) |
  2128. (intel_crtc->config.pipe_src_w - 1));
  2129. I915_WRITE(PRIMPOS(plane), 0);
  2130. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2131. }
  2132. switch (fb->pixel_format) {
  2133. case DRM_FORMAT_C8:
  2134. dspcntr |= DISPPLANE_8BPP;
  2135. break;
  2136. case DRM_FORMAT_XRGB1555:
  2137. case DRM_FORMAT_ARGB1555:
  2138. dspcntr |= DISPPLANE_BGRX555;
  2139. break;
  2140. case DRM_FORMAT_RGB565:
  2141. dspcntr |= DISPPLANE_BGRX565;
  2142. break;
  2143. case DRM_FORMAT_XRGB8888:
  2144. case DRM_FORMAT_ARGB8888:
  2145. dspcntr |= DISPPLANE_BGRX888;
  2146. break;
  2147. case DRM_FORMAT_XBGR8888:
  2148. case DRM_FORMAT_ABGR8888:
  2149. dspcntr |= DISPPLANE_RGBX888;
  2150. break;
  2151. case DRM_FORMAT_XRGB2101010:
  2152. case DRM_FORMAT_ARGB2101010:
  2153. dspcntr |= DISPPLANE_BGRX101010;
  2154. break;
  2155. case DRM_FORMAT_XBGR2101010:
  2156. case DRM_FORMAT_ABGR2101010:
  2157. dspcntr |= DISPPLANE_RGBX101010;
  2158. break;
  2159. default:
  2160. BUG();
  2161. }
  2162. if (INTEL_INFO(dev)->gen >= 4 &&
  2163. obj->tiling_mode != I915_TILING_NONE)
  2164. dspcntr |= DISPPLANE_TILED;
  2165. if (IS_G4X(dev))
  2166. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2167. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2168. if (INTEL_INFO(dev)->gen >= 4) {
  2169. intel_crtc->dspaddr_offset =
  2170. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2171. pixel_size,
  2172. fb->pitches[0]);
  2173. linear_offset -= intel_crtc->dspaddr_offset;
  2174. } else {
  2175. intel_crtc->dspaddr_offset = linear_offset;
  2176. }
  2177. if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
  2178. dspcntr |= DISPPLANE_ROTATE_180;
  2179. x += (intel_crtc->config.pipe_src_w - 1);
  2180. y += (intel_crtc->config.pipe_src_h - 1);
  2181. /* Finding the last pixel of the last line of the display
  2182. data and adding to linear_offset*/
  2183. linear_offset +=
  2184. (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
  2185. (intel_crtc->config.pipe_src_w - 1) * pixel_size;
  2186. }
  2187. I915_WRITE(reg, dspcntr);
  2188. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2189. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2190. fb->pitches[0]);
  2191. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2192. if (INTEL_INFO(dev)->gen >= 4) {
  2193. I915_WRITE(DSPSURF(plane),
  2194. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2195. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2196. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2197. } else
  2198. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2199. POSTING_READ(reg);
  2200. }
  2201. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2202. struct drm_framebuffer *fb,
  2203. int x, int y)
  2204. {
  2205. struct drm_device *dev = crtc->dev;
  2206. struct drm_i915_private *dev_priv = dev->dev_private;
  2207. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2208. struct drm_i915_gem_object *obj;
  2209. int plane = intel_crtc->plane;
  2210. unsigned long linear_offset;
  2211. u32 dspcntr;
  2212. u32 reg = DSPCNTR(plane);
  2213. int pixel_size;
  2214. if (!intel_crtc->primary_enabled) {
  2215. I915_WRITE(reg, 0);
  2216. I915_WRITE(DSPSURF(plane), 0);
  2217. POSTING_READ(reg);
  2218. return;
  2219. }
  2220. obj = intel_fb_obj(fb);
  2221. if (WARN_ON(obj == NULL))
  2222. return;
  2223. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2224. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2225. dspcntr |= DISPLAY_PLANE_ENABLE;
  2226. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2227. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2228. switch (fb->pixel_format) {
  2229. case DRM_FORMAT_C8:
  2230. dspcntr |= DISPPLANE_8BPP;
  2231. break;
  2232. case DRM_FORMAT_RGB565:
  2233. dspcntr |= DISPPLANE_BGRX565;
  2234. break;
  2235. case DRM_FORMAT_XRGB8888:
  2236. case DRM_FORMAT_ARGB8888:
  2237. dspcntr |= DISPPLANE_BGRX888;
  2238. break;
  2239. case DRM_FORMAT_XBGR8888:
  2240. case DRM_FORMAT_ABGR8888:
  2241. dspcntr |= DISPPLANE_RGBX888;
  2242. break;
  2243. case DRM_FORMAT_XRGB2101010:
  2244. case DRM_FORMAT_ARGB2101010:
  2245. dspcntr |= DISPPLANE_BGRX101010;
  2246. break;
  2247. case DRM_FORMAT_XBGR2101010:
  2248. case DRM_FORMAT_ABGR2101010:
  2249. dspcntr |= DISPPLANE_RGBX101010;
  2250. break;
  2251. default:
  2252. BUG();
  2253. }
  2254. if (obj->tiling_mode != I915_TILING_NONE)
  2255. dspcntr |= DISPPLANE_TILED;
  2256. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2257. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2258. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2259. intel_crtc->dspaddr_offset =
  2260. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2261. pixel_size,
  2262. fb->pitches[0]);
  2263. linear_offset -= intel_crtc->dspaddr_offset;
  2264. if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
  2265. dspcntr |= DISPPLANE_ROTATE_180;
  2266. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2267. x += (intel_crtc->config.pipe_src_w - 1);
  2268. y += (intel_crtc->config.pipe_src_h - 1);
  2269. /* Finding the last pixel of the last line of the display
  2270. data and adding to linear_offset*/
  2271. linear_offset +=
  2272. (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
  2273. (intel_crtc->config.pipe_src_w - 1) * pixel_size;
  2274. }
  2275. }
  2276. I915_WRITE(reg, dspcntr);
  2277. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2278. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2279. fb->pitches[0]);
  2280. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2281. I915_WRITE(DSPSURF(plane),
  2282. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2283. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2284. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2285. } else {
  2286. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2287. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2288. }
  2289. POSTING_READ(reg);
  2290. }
  2291. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2292. struct drm_framebuffer *fb,
  2293. int x, int y)
  2294. {
  2295. struct drm_device *dev = crtc->dev;
  2296. struct drm_i915_private *dev_priv = dev->dev_private;
  2297. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2298. struct intel_framebuffer *intel_fb;
  2299. struct drm_i915_gem_object *obj;
  2300. int pipe = intel_crtc->pipe;
  2301. u32 plane_ctl, stride;
  2302. if (!intel_crtc->primary_enabled) {
  2303. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2304. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2305. POSTING_READ(PLANE_CTL(pipe, 0));
  2306. return;
  2307. }
  2308. plane_ctl = PLANE_CTL_ENABLE |
  2309. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2310. PLANE_CTL_PIPE_CSC_ENABLE;
  2311. switch (fb->pixel_format) {
  2312. case DRM_FORMAT_RGB565:
  2313. plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
  2314. break;
  2315. case DRM_FORMAT_XRGB8888:
  2316. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2317. break;
  2318. case DRM_FORMAT_XBGR8888:
  2319. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2320. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2321. break;
  2322. case DRM_FORMAT_XRGB2101010:
  2323. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2324. break;
  2325. case DRM_FORMAT_XBGR2101010:
  2326. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2327. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2328. break;
  2329. default:
  2330. BUG();
  2331. }
  2332. intel_fb = to_intel_framebuffer(fb);
  2333. obj = intel_fb->obj;
  2334. /*
  2335. * The stride is either expressed as a multiple of 64 bytes chunks for
  2336. * linear buffers or in number of tiles for tiled buffers.
  2337. */
  2338. switch (obj->tiling_mode) {
  2339. case I915_TILING_NONE:
  2340. stride = fb->pitches[0] >> 6;
  2341. break;
  2342. case I915_TILING_X:
  2343. plane_ctl |= PLANE_CTL_TILED_X;
  2344. stride = fb->pitches[0] >> 9;
  2345. break;
  2346. default:
  2347. BUG();
  2348. }
  2349. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2350. if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180))
  2351. plane_ctl |= PLANE_CTL_ROTATE_180;
  2352. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2353. DRM_DEBUG_KMS("Writing base %08lX %d,%d,%d,%d pitch=%d\n",
  2354. i915_gem_obj_ggtt_offset(obj),
  2355. x, y, fb->width, fb->height,
  2356. fb->pitches[0]);
  2357. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2358. I915_WRITE(PLANE_OFFSET(pipe, 0), (y << 16) | x);
  2359. I915_WRITE(PLANE_SIZE(pipe, 0),
  2360. (intel_crtc->config.pipe_src_h - 1) << 16 |
  2361. (intel_crtc->config.pipe_src_w - 1));
  2362. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2363. I915_WRITE(PLANE_SURF(pipe, 0), i915_gem_obj_ggtt_offset(obj));
  2364. POSTING_READ(PLANE_SURF(pipe, 0));
  2365. }
  2366. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2367. static int
  2368. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2369. int x, int y, enum mode_set_atomic state)
  2370. {
  2371. struct drm_device *dev = crtc->dev;
  2372. struct drm_i915_private *dev_priv = dev->dev_private;
  2373. if (dev_priv->display.disable_fbc)
  2374. dev_priv->display.disable_fbc(dev);
  2375. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2376. return 0;
  2377. }
  2378. static void intel_complete_page_flips(struct drm_device *dev)
  2379. {
  2380. struct drm_crtc *crtc;
  2381. for_each_crtc(dev, crtc) {
  2382. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2383. enum plane plane = intel_crtc->plane;
  2384. intel_prepare_page_flip(dev, plane);
  2385. intel_finish_page_flip_plane(dev, plane);
  2386. }
  2387. }
  2388. static void intel_update_primary_planes(struct drm_device *dev)
  2389. {
  2390. struct drm_i915_private *dev_priv = dev->dev_private;
  2391. struct drm_crtc *crtc;
  2392. for_each_crtc(dev, crtc) {
  2393. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2394. drm_modeset_lock(&crtc->mutex, NULL);
  2395. /*
  2396. * FIXME: Once we have proper support for primary planes (and
  2397. * disabling them without disabling the entire crtc) allow again
  2398. * a NULL crtc->primary->fb.
  2399. */
  2400. if (intel_crtc->active && crtc->primary->fb)
  2401. dev_priv->display.update_primary_plane(crtc,
  2402. crtc->primary->fb,
  2403. crtc->x,
  2404. crtc->y);
  2405. drm_modeset_unlock(&crtc->mutex);
  2406. }
  2407. }
  2408. void intel_prepare_reset(struct drm_device *dev)
  2409. {
  2410. struct drm_i915_private *dev_priv = to_i915(dev);
  2411. struct intel_crtc *crtc;
  2412. /* no reset support for gen2 */
  2413. if (IS_GEN2(dev))
  2414. return;
  2415. /* reset doesn't touch the display */
  2416. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2417. return;
  2418. drm_modeset_lock_all(dev);
  2419. /*
  2420. * Disabling the crtcs gracefully seems nicer. Also the
  2421. * g33 docs say we should at least disable all the planes.
  2422. */
  2423. for_each_intel_crtc(dev, crtc) {
  2424. if (crtc->active)
  2425. dev_priv->display.crtc_disable(&crtc->base);
  2426. }
  2427. }
  2428. void intel_finish_reset(struct drm_device *dev)
  2429. {
  2430. struct drm_i915_private *dev_priv = to_i915(dev);
  2431. /*
  2432. * Flips in the rings will be nuked by the reset,
  2433. * so complete all pending flips so that user space
  2434. * will get its events and not get stuck.
  2435. */
  2436. intel_complete_page_flips(dev);
  2437. /* no reset support for gen2 */
  2438. if (IS_GEN2(dev))
  2439. return;
  2440. /* reset doesn't touch the display */
  2441. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2442. /*
  2443. * Flips in the rings have been nuked by the reset,
  2444. * so update the base address of all primary
  2445. * planes to the the last fb to make sure we're
  2446. * showing the correct fb after a reset.
  2447. */
  2448. intel_update_primary_planes(dev);
  2449. return;
  2450. }
  2451. /*
  2452. * The display has been reset as well,
  2453. * so need a full re-initialization.
  2454. */
  2455. intel_runtime_pm_disable_interrupts(dev_priv);
  2456. intel_runtime_pm_enable_interrupts(dev_priv);
  2457. intel_modeset_init_hw(dev);
  2458. spin_lock_irq(&dev_priv->irq_lock);
  2459. if (dev_priv->display.hpd_irq_setup)
  2460. dev_priv->display.hpd_irq_setup(dev);
  2461. spin_unlock_irq(&dev_priv->irq_lock);
  2462. intel_modeset_setup_hw_state(dev, true);
  2463. intel_hpd_init(dev_priv);
  2464. drm_modeset_unlock_all(dev);
  2465. }
  2466. static int
  2467. intel_finish_fb(struct drm_framebuffer *old_fb)
  2468. {
  2469. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2470. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2471. bool was_interruptible = dev_priv->mm.interruptible;
  2472. int ret;
  2473. /* Big Hammer, we also need to ensure that any pending
  2474. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2475. * current scanout is retired before unpinning the old
  2476. * framebuffer.
  2477. *
  2478. * This should only fail upon a hung GPU, in which case we
  2479. * can safely continue.
  2480. */
  2481. dev_priv->mm.interruptible = false;
  2482. ret = i915_gem_object_finish_gpu(obj);
  2483. dev_priv->mm.interruptible = was_interruptible;
  2484. return ret;
  2485. }
  2486. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2487. {
  2488. struct drm_device *dev = crtc->dev;
  2489. struct drm_i915_private *dev_priv = dev->dev_private;
  2490. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2491. bool pending;
  2492. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2493. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2494. return false;
  2495. spin_lock_irq(&dev->event_lock);
  2496. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2497. spin_unlock_irq(&dev->event_lock);
  2498. return pending;
  2499. }
  2500. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2501. {
  2502. struct drm_device *dev = crtc->base.dev;
  2503. struct drm_i915_private *dev_priv = dev->dev_private;
  2504. const struct drm_display_mode *adjusted_mode;
  2505. if (!i915.fastboot)
  2506. return;
  2507. /*
  2508. * Update pipe size and adjust fitter if needed: the reason for this is
  2509. * that in compute_mode_changes we check the native mode (not the pfit
  2510. * mode) to see if we can flip rather than do a full mode set. In the
  2511. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2512. * pfit state, we'll end up with a big fb scanned out into the wrong
  2513. * sized surface.
  2514. *
  2515. * To fix this properly, we need to hoist the checks up into
  2516. * compute_mode_changes (or above), check the actual pfit state and
  2517. * whether the platform allows pfit disable with pipe active, and only
  2518. * then update the pipesrc and pfit state, even on the flip path.
  2519. */
  2520. adjusted_mode = &crtc->config.adjusted_mode;
  2521. I915_WRITE(PIPESRC(crtc->pipe),
  2522. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2523. (adjusted_mode->crtc_vdisplay - 1));
  2524. if (!crtc->config.pch_pfit.enabled &&
  2525. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2526. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2527. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2528. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2529. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2530. }
  2531. crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
  2532. crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
  2533. }
  2534. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2535. {
  2536. struct drm_device *dev = crtc->dev;
  2537. struct drm_i915_private *dev_priv = dev->dev_private;
  2538. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2539. int pipe = intel_crtc->pipe;
  2540. u32 reg, temp;
  2541. /* enable normal train */
  2542. reg = FDI_TX_CTL(pipe);
  2543. temp = I915_READ(reg);
  2544. if (IS_IVYBRIDGE(dev)) {
  2545. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2546. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2547. } else {
  2548. temp &= ~FDI_LINK_TRAIN_NONE;
  2549. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2550. }
  2551. I915_WRITE(reg, temp);
  2552. reg = FDI_RX_CTL(pipe);
  2553. temp = I915_READ(reg);
  2554. if (HAS_PCH_CPT(dev)) {
  2555. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2556. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2557. } else {
  2558. temp &= ~FDI_LINK_TRAIN_NONE;
  2559. temp |= FDI_LINK_TRAIN_NONE;
  2560. }
  2561. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2562. /* wait one idle pattern time */
  2563. POSTING_READ(reg);
  2564. udelay(1000);
  2565. /* IVB wants error correction enabled */
  2566. if (IS_IVYBRIDGE(dev))
  2567. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2568. FDI_FE_ERRC_ENABLE);
  2569. }
  2570. static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
  2571. {
  2572. return crtc->base.enabled && crtc->active &&
  2573. crtc->config.has_pch_encoder;
  2574. }
  2575. static void ivb_modeset_global_resources(struct drm_device *dev)
  2576. {
  2577. struct drm_i915_private *dev_priv = dev->dev_private;
  2578. struct intel_crtc *pipe_B_crtc =
  2579. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  2580. struct intel_crtc *pipe_C_crtc =
  2581. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
  2582. uint32_t temp;
  2583. /*
  2584. * When everything is off disable fdi C so that we could enable fdi B
  2585. * with all lanes. Note that we don't care about enabled pipes without
  2586. * an enabled pch encoder.
  2587. */
  2588. if (!pipe_has_enabled_pch(pipe_B_crtc) &&
  2589. !pipe_has_enabled_pch(pipe_C_crtc)) {
  2590. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2591. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2592. temp = I915_READ(SOUTH_CHICKEN1);
  2593. temp &= ~FDI_BC_BIFURCATION_SELECT;
  2594. DRM_DEBUG_KMS("disabling fdi C rx\n");
  2595. I915_WRITE(SOUTH_CHICKEN1, temp);
  2596. }
  2597. }
  2598. /* The FDI link training functions for ILK/Ibexpeak. */
  2599. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2600. {
  2601. struct drm_device *dev = crtc->dev;
  2602. struct drm_i915_private *dev_priv = dev->dev_private;
  2603. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2604. int pipe = intel_crtc->pipe;
  2605. u32 reg, temp, tries;
  2606. /* FDI needs bits from pipe first */
  2607. assert_pipe_enabled(dev_priv, pipe);
  2608. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2609. for train result */
  2610. reg = FDI_RX_IMR(pipe);
  2611. temp = I915_READ(reg);
  2612. temp &= ~FDI_RX_SYMBOL_LOCK;
  2613. temp &= ~FDI_RX_BIT_LOCK;
  2614. I915_WRITE(reg, temp);
  2615. I915_READ(reg);
  2616. udelay(150);
  2617. /* enable CPU FDI TX and PCH FDI RX */
  2618. reg = FDI_TX_CTL(pipe);
  2619. temp = I915_READ(reg);
  2620. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2621. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2622. temp &= ~FDI_LINK_TRAIN_NONE;
  2623. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2624. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2625. reg = FDI_RX_CTL(pipe);
  2626. temp = I915_READ(reg);
  2627. temp &= ~FDI_LINK_TRAIN_NONE;
  2628. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2629. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2630. POSTING_READ(reg);
  2631. udelay(150);
  2632. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2633. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2634. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2635. FDI_RX_PHASE_SYNC_POINTER_EN);
  2636. reg = FDI_RX_IIR(pipe);
  2637. for (tries = 0; tries < 5; tries++) {
  2638. temp = I915_READ(reg);
  2639. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2640. if ((temp & FDI_RX_BIT_LOCK)) {
  2641. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2642. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2643. break;
  2644. }
  2645. }
  2646. if (tries == 5)
  2647. DRM_ERROR("FDI train 1 fail!\n");
  2648. /* Train 2 */
  2649. reg = FDI_TX_CTL(pipe);
  2650. temp = I915_READ(reg);
  2651. temp &= ~FDI_LINK_TRAIN_NONE;
  2652. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2653. I915_WRITE(reg, temp);
  2654. reg = FDI_RX_CTL(pipe);
  2655. temp = I915_READ(reg);
  2656. temp &= ~FDI_LINK_TRAIN_NONE;
  2657. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2658. I915_WRITE(reg, temp);
  2659. POSTING_READ(reg);
  2660. udelay(150);
  2661. reg = FDI_RX_IIR(pipe);
  2662. for (tries = 0; tries < 5; tries++) {
  2663. temp = I915_READ(reg);
  2664. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2665. if (temp & FDI_RX_SYMBOL_LOCK) {
  2666. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2667. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2668. break;
  2669. }
  2670. }
  2671. if (tries == 5)
  2672. DRM_ERROR("FDI train 2 fail!\n");
  2673. DRM_DEBUG_KMS("FDI train done\n");
  2674. }
  2675. static const int snb_b_fdi_train_param[] = {
  2676. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2677. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2678. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2679. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2680. };
  2681. /* The FDI link training functions for SNB/Cougarpoint. */
  2682. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2683. {
  2684. struct drm_device *dev = crtc->dev;
  2685. struct drm_i915_private *dev_priv = dev->dev_private;
  2686. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2687. int pipe = intel_crtc->pipe;
  2688. u32 reg, temp, i, retry;
  2689. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2690. for train result */
  2691. reg = FDI_RX_IMR(pipe);
  2692. temp = I915_READ(reg);
  2693. temp &= ~FDI_RX_SYMBOL_LOCK;
  2694. temp &= ~FDI_RX_BIT_LOCK;
  2695. I915_WRITE(reg, temp);
  2696. POSTING_READ(reg);
  2697. udelay(150);
  2698. /* enable CPU FDI TX and PCH FDI RX */
  2699. reg = FDI_TX_CTL(pipe);
  2700. temp = I915_READ(reg);
  2701. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2702. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2703. temp &= ~FDI_LINK_TRAIN_NONE;
  2704. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2705. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2706. /* SNB-B */
  2707. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2708. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2709. I915_WRITE(FDI_RX_MISC(pipe),
  2710. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2711. reg = FDI_RX_CTL(pipe);
  2712. temp = I915_READ(reg);
  2713. if (HAS_PCH_CPT(dev)) {
  2714. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2715. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2716. } else {
  2717. temp &= ~FDI_LINK_TRAIN_NONE;
  2718. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2719. }
  2720. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2721. POSTING_READ(reg);
  2722. udelay(150);
  2723. for (i = 0; i < 4; i++) {
  2724. reg = FDI_TX_CTL(pipe);
  2725. temp = I915_READ(reg);
  2726. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2727. temp |= snb_b_fdi_train_param[i];
  2728. I915_WRITE(reg, temp);
  2729. POSTING_READ(reg);
  2730. udelay(500);
  2731. for (retry = 0; retry < 5; retry++) {
  2732. reg = FDI_RX_IIR(pipe);
  2733. temp = I915_READ(reg);
  2734. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2735. if (temp & FDI_RX_BIT_LOCK) {
  2736. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2737. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2738. break;
  2739. }
  2740. udelay(50);
  2741. }
  2742. if (retry < 5)
  2743. break;
  2744. }
  2745. if (i == 4)
  2746. DRM_ERROR("FDI train 1 fail!\n");
  2747. /* Train 2 */
  2748. reg = FDI_TX_CTL(pipe);
  2749. temp = I915_READ(reg);
  2750. temp &= ~FDI_LINK_TRAIN_NONE;
  2751. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2752. if (IS_GEN6(dev)) {
  2753. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2754. /* SNB-B */
  2755. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2756. }
  2757. I915_WRITE(reg, temp);
  2758. reg = FDI_RX_CTL(pipe);
  2759. temp = I915_READ(reg);
  2760. if (HAS_PCH_CPT(dev)) {
  2761. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2762. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2763. } else {
  2764. temp &= ~FDI_LINK_TRAIN_NONE;
  2765. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2766. }
  2767. I915_WRITE(reg, temp);
  2768. POSTING_READ(reg);
  2769. udelay(150);
  2770. for (i = 0; i < 4; i++) {
  2771. reg = FDI_TX_CTL(pipe);
  2772. temp = I915_READ(reg);
  2773. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2774. temp |= snb_b_fdi_train_param[i];
  2775. I915_WRITE(reg, temp);
  2776. POSTING_READ(reg);
  2777. udelay(500);
  2778. for (retry = 0; retry < 5; retry++) {
  2779. reg = FDI_RX_IIR(pipe);
  2780. temp = I915_READ(reg);
  2781. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2782. if (temp & FDI_RX_SYMBOL_LOCK) {
  2783. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2784. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2785. break;
  2786. }
  2787. udelay(50);
  2788. }
  2789. if (retry < 5)
  2790. break;
  2791. }
  2792. if (i == 4)
  2793. DRM_ERROR("FDI train 2 fail!\n");
  2794. DRM_DEBUG_KMS("FDI train done.\n");
  2795. }
  2796. /* Manual link training for Ivy Bridge A0 parts */
  2797. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2798. {
  2799. struct drm_device *dev = crtc->dev;
  2800. struct drm_i915_private *dev_priv = dev->dev_private;
  2801. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2802. int pipe = intel_crtc->pipe;
  2803. u32 reg, temp, i, j;
  2804. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2805. for train result */
  2806. reg = FDI_RX_IMR(pipe);
  2807. temp = I915_READ(reg);
  2808. temp &= ~FDI_RX_SYMBOL_LOCK;
  2809. temp &= ~FDI_RX_BIT_LOCK;
  2810. I915_WRITE(reg, temp);
  2811. POSTING_READ(reg);
  2812. udelay(150);
  2813. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  2814. I915_READ(FDI_RX_IIR(pipe)));
  2815. /* Try each vswing and preemphasis setting twice before moving on */
  2816. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  2817. /* disable first in case we need to retry */
  2818. reg = FDI_TX_CTL(pipe);
  2819. temp = I915_READ(reg);
  2820. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2821. temp &= ~FDI_TX_ENABLE;
  2822. I915_WRITE(reg, temp);
  2823. reg = FDI_RX_CTL(pipe);
  2824. temp = I915_READ(reg);
  2825. temp &= ~FDI_LINK_TRAIN_AUTO;
  2826. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2827. temp &= ~FDI_RX_ENABLE;
  2828. I915_WRITE(reg, temp);
  2829. /* enable CPU FDI TX and PCH FDI RX */
  2830. reg = FDI_TX_CTL(pipe);
  2831. temp = I915_READ(reg);
  2832. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2833. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2834. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2835. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2836. temp |= snb_b_fdi_train_param[j/2];
  2837. temp |= FDI_COMPOSITE_SYNC;
  2838. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2839. I915_WRITE(FDI_RX_MISC(pipe),
  2840. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2841. reg = FDI_RX_CTL(pipe);
  2842. temp = I915_READ(reg);
  2843. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2844. temp |= FDI_COMPOSITE_SYNC;
  2845. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2846. POSTING_READ(reg);
  2847. udelay(1); /* should be 0.5us */
  2848. for (i = 0; i < 4; i++) {
  2849. reg = FDI_RX_IIR(pipe);
  2850. temp = I915_READ(reg);
  2851. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2852. if (temp & FDI_RX_BIT_LOCK ||
  2853. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2854. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2855. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  2856. i);
  2857. break;
  2858. }
  2859. udelay(1); /* should be 0.5us */
  2860. }
  2861. if (i == 4) {
  2862. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  2863. continue;
  2864. }
  2865. /* Train 2 */
  2866. reg = FDI_TX_CTL(pipe);
  2867. temp = I915_READ(reg);
  2868. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2869. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2870. I915_WRITE(reg, temp);
  2871. reg = FDI_RX_CTL(pipe);
  2872. temp = I915_READ(reg);
  2873. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2874. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2875. I915_WRITE(reg, temp);
  2876. POSTING_READ(reg);
  2877. udelay(2); /* should be 1.5us */
  2878. for (i = 0; i < 4; i++) {
  2879. reg = FDI_RX_IIR(pipe);
  2880. temp = I915_READ(reg);
  2881. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2882. if (temp & FDI_RX_SYMBOL_LOCK ||
  2883. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  2884. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2885. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  2886. i);
  2887. goto train_done;
  2888. }
  2889. udelay(2); /* should be 1.5us */
  2890. }
  2891. if (i == 4)
  2892. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  2893. }
  2894. train_done:
  2895. DRM_DEBUG_KMS("FDI train done.\n");
  2896. }
  2897. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  2898. {
  2899. struct drm_device *dev = intel_crtc->base.dev;
  2900. struct drm_i915_private *dev_priv = dev->dev_private;
  2901. int pipe = intel_crtc->pipe;
  2902. u32 reg, temp;
  2903. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2904. reg = FDI_RX_CTL(pipe);
  2905. temp = I915_READ(reg);
  2906. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  2907. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2908. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2909. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2910. POSTING_READ(reg);
  2911. udelay(200);
  2912. /* Switch from Rawclk to PCDclk */
  2913. temp = I915_READ(reg);
  2914. I915_WRITE(reg, temp | FDI_PCDCLK);
  2915. POSTING_READ(reg);
  2916. udelay(200);
  2917. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2918. reg = FDI_TX_CTL(pipe);
  2919. temp = I915_READ(reg);
  2920. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2921. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2922. POSTING_READ(reg);
  2923. udelay(100);
  2924. }
  2925. }
  2926. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  2927. {
  2928. struct drm_device *dev = intel_crtc->base.dev;
  2929. struct drm_i915_private *dev_priv = dev->dev_private;
  2930. int pipe = intel_crtc->pipe;
  2931. u32 reg, temp;
  2932. /* Switch from PCDclk to Rawclk */
  2933. reg = FDI_RX_CTL(pipe);
  2934. temp = I915_READ(reg);
  2935. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2936. /* Disable CPU FDI TX PLL */
  2937. reg = FDI_TX_CTL(pipe);
  2938. temp = I915_READ(reg);
  2939. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2940. POSTING_READ(reg);
  2941. udelay(100);
  2942. reg = FDI_RX_CTL(pipe);
  2943. temp = I915_READ(reg);
  2944. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2945. /* Wait for the clocks to turn off. */
  2946. POSTING_READ(reg);
  2947. udelay(100);
  2948. }
  2949. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2950. {
  2951. struct drm_device *dev = crtc->dev;
  2952. struct drm_i915_private *dev_priv = dev->dev_private;
  2953. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2954. int pipe = intel_crtc->pipe;
  2955. u32 reg, temp;
  2956. /* disable CPU FDI tx and PCH FDI rx */
  2957. reg = FDI_TX_CTL(pipe);
  2958. temp = I915_READ(reg);
  2959. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2960. POSTING_READ(reg);
  2961. reg = FDI_RX_CTL(pipe);
  2962. temp = I915_READ(reg);
  2963. temp &= ~(0x7 << 16);
  2964. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2965. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2966. POSTING_READ(reg);
  2967. udelay(100);
  2968. /* Ironlake workaround, disable clock pointer after downing FDI */
  2969. if (HAS_PCH_IBX(dev))
  2970. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2971. /* still set train pattern 1 */
  2972. reg = FDI_TX_CTL(pipe);
  2973. temp = I915_READ(reg);
  2974. temp &= ~FDI_LINK_TRAIN_NONE;
  2975. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2976. I915_WRITE(reg, temp);
  2977. reg = FDI_RX_CTL(pipe);
  2978. temp = I915_READ(reg);
  2979. if (HAS_PCH_CPT(dev)) {
  2980. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2981. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2982. } else {
  2983. temp &= ~FDI_LINK_TRAIN_NONE;
  2984. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2985. }
  2986. /* BPC in FDI rx is consistent with that in PIPECONF */
  2987. temp &= ~(0x07 << 16);
  2988. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2989. I915_WRITE(reg, temp);
  2990. POSTING_READ(reg);
  2991. udelay(100);
  2992. }
  2993. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  2994. {
  2995. struct intel_crtc *crtc;
  2996. /* Note that we don't need to be called with mode_config.lock here
  2997. * as our list of CRTC objects is static for the lifetime of the
  2998. * device and so cannot disappear as we iterate. Similarly, we can
  2999. * happily treat the predicates as racy, atomic checks as userspace
  3000. * cannot claim and pin a new fb without at least acquring the
  3001. * struct_mutex and so serialising with us.
  3002. */
  3003. for_each_intel_crtc(dev, crtc) {
  3004. if (atomic_read(&crtc->unpin_work_count) == 0)
  3005. continue;
  3006. if (crtc->unpin_work)
  3007. intel_wait_for_vblank(dev, crtc->pipe);
  3008. return true;
  3009. }
  3010. return false;
  3011. }
  3012. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3013. {
  3014. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3015. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3016. /* ensure that the unpin work is consistent wrt ->pending. */
  3017. smp_rmb();
  3018. intel_crtc->unpin_work = NULL;
  3019. if (work->event)
  3020. drm_send_vblank_event(intel_crtc->base.dev,
  3021. intel_crtc->pipe,
  3022. work->event);
  3023. drm_crtc_vblank_put(&intel_crtc->base);
  3024. wake_up_all(&dev_priv->pending_flip_queue);
  3025. queue_work(dev_priv->wq, &work->work);
  3026. trace_i915_flip_complete(intel_crtc->plane,
  3027. work->pending_flip_obj);
  3028. }
  3029. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3030. {
  3031. struct drm_device *dev = crtc->dev;
  3032. struct drm_i915_private *dev_priv = dev->dev_private;
  3033. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3034. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3035. !intel_crtc_has_pending_flip(crtc),
  3036. 60*HZ) == 0)) {
  3037. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3038. spin_lock_irq(&dev->event_lock);
  3039. if (intel_crtc->unpin_work) {
  3040. WARN_ONCE(1, "Removing stuck page flip\n");
  3041. page_flip_completed(intel_crtc);
  3042. }
  3043. spin_unlock_irq(&dev->event_lock);
  3044. }
  3045. if (crtc->primary->fb) {
  3046. mutex_lock(&dev->struct_mutex);
  3047. intel_finish_fb(crtc->primary->fb);
  3048. mutex_unlock(&dev->struct_mutex);
  3049. }
  3050. }
  3051. /* Program iCLKIP clock to the desired frequency */
  3052. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3053. {
  3054. struct drm_device *dev = crtc->dev;
  3055. struct drm_i915_private *dev_priv = dev->dev_private;
  3056. int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
  3057. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3058. u32 temp;
  3059. mutex_lock(&dev_priv->dpio_lock);
  3060. /* It is necessary to ungate the pixclk gate prior to programming
  3061. * the divisors, and gate it back when it is done.
  3062. */
  3063. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3064. /* Disable SSCCTL */
  3065. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3066. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3067. SBI_SSCCTL_DISABLE,
  3068. SBI_ICLK);
  3069. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3070. if (clock == 20000) {
  3071. auxdiv = 1;
  3072. divsel = 0x41;
  3073. phaseinc = 0x20;
  3074. } else {
  3075. /* The iCLK virtual clock root frequency is in MHz,
  3076. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3077. * divisors, it is necessary to divide one by another, so we
  3078. * convert the virtual clock precision to KHz here for higher
  3079. * precision.
  3080. */
  3081. u32 iclk_virtual_root_freq = 172800 * 1000;
  3082. u32 iclk_pi_range = 64;
  3083. u32 desired_divisor, msb_divisor_value, pi_value;
  3084. desired_divisor = (iclk_virtual_root_freq / clock);
  3085. msb_divisor_value = desired_divisor / iclk_pi_range;
  3086. pi_value = desired_divisor % iclk_pi_range;
  3087. auxdiv = 0;
  3088. divsel = msb_divisor_value - 2;
  3089. phaseinc = pi_value;
  3090. }
  3091. /* This should not happen with any sane values */
  3092. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3093. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3094. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3095. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3096. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3097. clock,
  3098. auxdiv,
  3099. divsel,
  3100. phasedir,
  3101. phaseinc);
  3102. /* Program SSCDIVINTPHASE6 */
  3103. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3104. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3105. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3106. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3107. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3108. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3109. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3110. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3111. /* Program SSCAUXDIV */
  3112. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3113. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3114. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3115. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3116. /* Enable modulator and associated divider */
  3117. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3118. temp &= ~SBI_SSCCTL_DISABLE;
  3119. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3120. /* Wait for initialization time */
  3121. udelay(24);
  3122. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3123. mutex_unlock(&dev_priv->dpio_lock);
  3124. }
  3125. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3126. enum pipe pch_transcoder)
  3127. {
  3128. struct drm_device *dev = crtc->base.dev;
  3129. struct drm_i915_private *dev_priv = dev->dev_private;
  3130. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  3131. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3132. I915_READ(HTOTAL(cpu_transcoder)));
  3133. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3134. I915_READ(HBLANK(cpu_transcoder)));
  3135. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3136. I915_READ(HSYNC(cpu_transcoder)));
  3137. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3138. I915_READ(VTOTAL(cpu_transcoder)));
  3139. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3140. I915_READ(VBLANK(cpu_transcoder)));
  3141. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3142. I915_READ(VSYNC(cpu_transcoder)));
  3143. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3144. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3145. }
  3146. static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
  3147. {
  3148. struct drm_i915_private *dev_priv = dev->dev_private;
  3149. uint32_t temp;
  3150. temp = I915_READ(SOUTH_CHICKEN1);
  3151. if (temp & FDI_BC_BIFURCATION_SELECT)
  3152. return;
  3153. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3154. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3155. temp |= FDI_BC_BIFURCATION_SELECT;
  3156. DRM_DEBUG_KMS("enabling fdi C rx\n");
  3157. I915_WRITE(SOUTH_CHICKEN1, temp);
  3158. POSTING_READ(SOUTH_CHICKEN1);
  3159. }
  3160. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3161. {
  3162. struct drm_device *dev = intel_crtc->base.dev;
  3163. struct drm_i915_private *dev_priv = dev->dev_private;
  3164. switch (intel_crtc->pipe) {
  3165. case PIPE_A:
  3166. break;
  3167. case PIPE_B:
  3168. if (intel_crtc->config.fdi_lanes > 2)
  3169. WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
  3170. else
  3171. cpt_enable_fdi_bc_bifurcation(dev);
  3172. break;
  3173. case PIPE_C:
  3174. cpt_enable_fdi_bc_bifurcation(dev);
  3175. break;
  3176. default:
  3177. BUG();
  3178. }
  3179. }
  3180. /*
  3181. * Enable PCH resources required for PCH ports:
  3182. * - PCH PLLs
  3183. * - FDI training & RX/TX
  3184. * - update transcoder timings
  3185. * - DP transcoding bits
  3186. * - transcoder
  3187. */
  3188. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3189. {
  3190. struct drm_device *dev = crtc->dev;
  3191. struct drm_i915_private *dev_priv = dev->dev_private;
  3192. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3193. int pipe = intel_crtc->pipe;
  3194. u32 reg, temp;
  3195. assert_pch_transcoder_disabled(dev_priv, pipe);
  3196. if (IS_IVYBRIDGE(dev))
  3197. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3198. /* Write the TU size bits before fdi link training, so that error
  3199. * detection works. */
  3200. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3201. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3202. /* For PCH output, training FDI link */
  3203. dev_priv->display.fdi_link_train(crtc);
  3204. /* We need to program the right clock selection before writing the pixel
  3205. * mutliplier into the DPLL. */
  3206. if (HAS_PCH_CPT(dev)) {
  3207. u32 sel;
  3208. temp = I915_READ(PCH_DPLL_SEL);
  3209. temp |= TRANS_DPLL_ENABLE(pipe);
  3210. sel = TRANS_DPLLB_SEL(pipe);
  3211. if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
  3212. temp |= sel;
  3213. else
  3214. temp &= ~sel;
  3215. I915_WRITE(PCH_DPLL_SEL, temp);
  3216. }
  3217. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3218. * transcoder, and we actually should do this to not upset any PCH
  3219. * transcoder that already use the clock when we share it.
  3220. *
  3221. * Note that enable_shared_dpll tries to do the right thing, but
  3222. * get_shared_dpll unconditionally resets the pll - we need that to have
  3223. * the right LVDS enable sequence. */
  3224. intel_enable_shared_dpll(intel_crtc);
  3225. /* set transcoder timing, panel must allow it */
  3226. assert_panel_unlocked(dev_priv, pipe);
  3227. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3228. intel_fdi_normal_train(crtc);
  3229. /* For PCH DP, enable TRANS_DP_CTL */
  3230. if (HAS_PCH_CPT(dev) && intel_crtc->config.has_dp_encoder) {
  3231. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3232. reg = TRANS_DP_CTL(pipe);
  3233. temp = I915_READ(reg);
  3234. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3235. TRANS_DP_SYNC_MASK |
  3236. TRANS_DP_BPC_MASK);
  3237. temp |= (TRANS_DP_OUTPUT_ENABLE |
  3238. TRANS_DP_ENH_FRAMING);
  3239. temp |= bpc << 9; /* same format but at 11:9 */
  3240. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3241. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3242. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3243. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3244. switch (intel_trans_dp_port_sel(crtc)) {
  3245. case PCH_DP_B:
  3246. temp |= TRANS_DP_PORT_SEL_B;
  3247. break;
  3248. case PCH_DP_C:
  3249. temp |= TRANS_DP_PORT_SEL_C;
  3250. break;
  3251. case PCH_DP_D:
  3252. temp |= TRANS_DP_PORT_SEL_D;
  3253. break;
  3254. default:
  3255. BUG();
  3256. }
  3257. I915_WRITE(reg, temp);
  3258. }
  3259. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3260. }
  3261. static void lpt_pch_enable(struct drm_crtc *crtc)
  3262. {
  3263. struct drm_device *dev = crtc->dev;
  3264. struct drm_i915_private *dev_priv = dev->dev_private;
  3265. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3266. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3267. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3268. lpt_program_iclkip(crtc);
  3269. /* Set transcoder timing. */
  3270. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3271. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3272. }
  3273. void intel_put_shared_dpll(struct intel_crtc *crtc)
  3274. {
  3275. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3276. if (pll == NULL)
  3277. return;
  3278. if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
  3279. WARN(1, "bad %s crtc mask\n", pll->name);
  3280. return;
  3281. }
  3282. pll->config.crtc_mask &= ~(1 << crtc->pipe);
  3283. if (pll->config.crtc_mask == 0) {
  3284. WARN_ON(pll->on);
  3285. WARN_ON(pll->active);
  3286. }
  3287. crtc->config.shared_dpll = DPLL_ID_PRIVATE;
  3288. }
  3289. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
  3290. {
  3291. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3292. struct intel_shared_dpll *pll;
  3293. enum intel_dpll_id i;
  3294. if (HAS_PCH_IBX(dev_priv->dev)) {
  3295. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3296. i = (enum intel_dpll_id) crtc->pipe;
  3297. pll = &dev_priv->shared_dplls[i];
  3298. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3299. crtc->base.base.id, pll->name);
  3300. WARN_ON(pll->new_config->crtc_mask);
  3301. goto found;
  3302. }
  3303. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3304. pll = &dev_priv->shared_dplls[i];
  3305. /* Only want to check enabled timings first */
  3306. if (pll->new_config->crtc_mask == 0)
  3307. continue;
  3308. if (memcmp(&crtc->new_config->dpll_hw_state,
  3309. &pll->new_config->hw_state,
  3310. sizeof(pll->new_config->hw_state)) == 0) {
  3311. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3312. crtc->base.base.id, pll->name,
  3313. pll->new_config->crtc_mask,
  3314. pll->active);
  3315. goto found;
  3316. }
  3317. }
  3318. /* Ok no matching timings, maybe there's a free one? */
  3319. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3320. pll = &dev_priv->shared_dplls[i];
  3321. if (pll->new_config->crtc_mask == 0) {
  3322. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3323. crtc->base.base.id, pll->name);
  3324. goto found;
  3325. }
  3326. }
  3327. return NULL;
  3328. found:
  3329. if (pll->new_config->crtc_mask == 0)
  3330. pll->new_config->hw_state = crtc->new_config->dpll_hw_state;
  3331. crtc->new_config->shared_dpll = i;
  3332. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3333. pipe_name(crtc->pipe));
  3334. pll->new_config->crtc_mask |= 1 << crtc->pipe;
  3335. return pll;
  3336. }
  3337. /**
  3338. * intel_shared_dpll_start_config - start a new PLL staged config
  3339. * @dev_priv: DRM device
  3340. * @clear_pipes: mask of pipes that will have their PLLs freed
  3341. *
  3342. * Starts a new PLL staged config, copying the current config but
  3343. * releasing the references of pipes specified in clear_pipes.
  3344. */
  3345. static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
  3346. unsigned clear_pipes)
  3347. {
  3348. struct intel_shared_dpll *pll;
  3349. enum intel_dpll_id i;
  3350. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3351. pll = &dev_priv->shared_dplls[i];
  3352. pll->new_config = kmemdup(&pll->config, sizeof pll->config,
  3353. GFP_KERNEL);
  3354. if (!pll->new_config)
  3355. goto cleanup;
  3356. pll->new_config->crtc_mask &= ~clear_pipes;
  3357. }
  3358. return 0;
  3359. cleanup:
  3360. while (--i >= 0) {
  3361. pll = &dev_priv->shared_dplls[i];
  3362. kfree(pll->new_config);
  3363. pll->new_config = NULL;
  3364. }
  3365. return -ENOMEM;
  3366. }
  3367. static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
  3368. {
  3369. struct intel_shared_dpll *pll;
  3370. enum intel_dpll_id i;
  3371. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3372. pll = &dev_priv->shared_dplls[i];
  3373. WARN_ON(pll->new_config == &pll->config);
  3374. pll->config = *pll->new_config;
  3375. kfree(pll->new_config);
  3376. pll->new_config = NULL;
  3377. }
  3378. }
  3379. static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
  3380. {
  3381. struct intel_shared_dpll *pll;
  3382. enum intel_dpll_id i;
  3383. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3384. pll = &dev_priv->shared_dplls[i];
  3385. WARN_ON(pll->new_config == &pll->config);
  3386. kfree(pll->new_config);
  3387. pll->new_config = NULL;
  3388. }
  3389. }
  3390. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3391. {
  3392. struct drm_i915_private *dev_priv = dev->dev_private;
  3393. int dslreg = PIPEDSL(pipe);
  3394. u32 temp;
  3395. temp = I915_READ(dslreg);
  3396. udelay(500);
  3397. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3398. if (wait_for(I915_READ(dslreg) != temp, 5))
  3399. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3400. }
  3401. }
  3402. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3403. {
  3404. struct drm_device *dev = crtc->base.dev;
  3405. struct drm_i915_private *dev_priv = dev->dev_private;
  3406. int pipe = crtc->pipe;
  3407. if (crtc->config.pch_pfit.enabled) {
  3408. I915_WRITE(PS_CTL(pipe), PS_ENABLE);
  3409. I915_WRITE(PS_WIN_POS(pipe), crtc->config.pch_pfit.pos);
  3410. I915_WRITE(PS_WIN_SZ(pipe), crtc->config.pch_pfit.size);
  3411. }
  3412. }
  3413. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3414. {
  3415. struct drm_device *dev = crtc->base.dev;
  3416. struct drm_i915_private *dev_priv = dev->dev_private;
  3417. int pipe = crtc->pipe;
  3418. if (crtc->config.pch_pfit.enabled) {
  3419. /* Force use of hard-coded filter coefficients
  3420. * as some pre-programmed values are broken,
  3421. * e.g. x201.
  3422. */
  3423. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3424. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3425. PF_PIPE_SEL_IVB(pipe));
  3426. else
  3427. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3428. I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
  3429. I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
  3430. }
  3431. }
  3432. static void intel_enable_sprite_planes(struct drm_crtc *crtc)
  3433. {
  3434. struct drm_device *dev = crtc->dev;
  3435. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3436. struct drm_plane *plane;
  3437. struct intel_plane *intel_plane;
  3438. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3439. intel_plane = to_intel_plane(plane);
  3440. if (intel_plane->pipe == pipe)
  3441. intel_plane_restore(&intel_plane->base);
  3442. }
  3443. }
  3444. static void intel_disable_sprite_planes(struct drm_crtc *crtc)
  3445. {
  3446. struct drm_device *dev = crtc->dev;
  3447. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3448. struct drm_plane *plane;
  3449. struct intel_plane *intel_plane;
  3450. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3451. intel_plane = to_intel_plane(plane);
  3452. if (intel_plane->pipe == pipe)
  3453. plane->funcs->disable_plane(plane);
  3454. }
  3455. }
  3456. void hsw_enable_ips(struct intel_crtc *crtc)
  3457. {
  3458. struct drm_device *dev = crtc->base.dev;
  3459. struct drm_i915_private *dev_priv = dev->dev_private;
  3460. if (!crtc->config.ips_enabled)
  3461. return;
  3462. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3463. intel_wait_for_vblank(dev, crtc->pipe);
  3464. assert_plane_enabled(dev_priv, crtc->plane);
  3465. if (IS_BROADWELL(dev)) {
  3466. mutex_lock(&dev_priv->rps.hw_lock);
  3467. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3468. mutex_unlock(&dev_priv->rps.hw_lock);
  3469. /* Quoting Art Runyan: "its not safe to expect any particular
  3470. * value in IPS_CTL bit 31 after enabling IPS through the
  3471. * mailbox." Moreover, the mailbox may return a bogus state,
  3472. * so we need to just enable it and continue on.
  3473. */
  3474. } else {
  3475. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3476. /* The bit only becomes 1 in the next vblank, so this wait here
  3477. * is essentially intel_wait_for_vblank. If we don't have this
  3478. * and don't wait for vblanks until the end of crtc_enable, then
  3479. * the HW state readout code will complain that the expected
  3480. * IPS_CTL value is not the one we read. */
  3481. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3482. DRM_ERROR("Timed out waiting for IPS enable\n");
  3483. }
  3484. }
  3485. void hsw_disable_ips(struct intel_crtc *crtc)
  3486. {
  3487. struct drm_device *dev = crtc->base.dev;
  3488. struct drm_i915_private *dev_priv = dev->dev_private;
  3489. if (!crtc->config.ips_enabled)
  3490. return;
  3491. assert_plane_enabled(dev_priv, crtc->plane);
  3492. if (IS_BROADWELL(dev)) {
  3493. mutex_lock(&dev_priv->rps.hw_lock);
  3494. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3495. mutex_unlock(&dev_priv->rps.hw_lock);
  3496. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3497. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3498. DRM_ERROR("Timed out waiting for IPS disable\n");
  3499. } else {
  3500. I915_WRITE(IPS_CTL, 0);
  3501. POSTING_READ(IPS_CTL);
  3502. }
  3503. /* We need to wait for a vblank before we can disable the plane. */
  3504. intel_wait_for_vblank(dev, crtc->pipe);
  3505. }
  3506. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3507. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3508. {
  3509. struct drm_device *dev = crtc->dev;
  3510. struct drm_i915_private *dev_priv = dev->dev_private;
  3511. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3512. enum pipe pipe = intel_crtc->pipe;
  3513. int palreg = PALETTE(pipe);
  3514. int i;
  3515. bool reenable_ips = false;
  3516. /* The clocks have to be on to load the palette. */
  3517. if (!crtc->enabled || !intel_crtc->active)
  3518. return;
  3519. if (!HAS_PCH_SPLIT(dev_priv->dev)) {
  3520. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3521. assert_dsi_pll_enabled(dev_priv);
  3522. else
  3523. assert_pll_enabled(dev_priv, pipe);
  3524. }
  3525. /* use legacy palette for Ironlake */
  3526. if (!HAS_GMCH_DISPLAY(dev))
  3527. palreg = LGC_PALETTE(pipe);
  3528. /* Workaround : Do not read or write the pipe palette/gamma data while
  3529. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3530. */
  3531. if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
  3532. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3533. GAMMA_MODE_MODE_SPLIT)) {
  3534. hsw_disable_ips(intel_crtc);
  3535. reenable_ips = true;
  3536. }
  3537. for (i = 0; i < 256; i++) {
  3538. I915_WRITE(palreg + 4 * i,
  3539. (intel_crtc->lut_r[i] << 16) |
  3540. (intel_crtc->lut_g[i] << 8) |
  3541. intel_crtc->lut_b[i]);
  3542. }
  3543. if (reenable_ips)
  3544. hsw_enable_ips(intel_crtc);
  3545. }
  3546. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  3547. {
  3548. if (!enable && intel_crtc->overlay) {
  3549. struct drm_device *dev = intel_crtc->base.dev;
  3550. struct drm_i915_private *dev_priv = dev->dev_private;
  3551. mutex_lock(&dev->struct_mutex);
  3552. dev_priv->mm.interruptible = false;
  3553. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3554. dev_priv->mm.interruptible = true;
  3555. mutex_unlock(&dev->struct_mutex);
  3556. }
  3557. /* Let userspace switch the overlay on again. In most cases userspace
  3558. * has to recompute where to put it anyway.
  3559. */
  3560. }
  3561. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  3562. {
  3563. struct drm_device *dev = crtc->dev;
  3564. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3565. int pipe = intel_crtc->pipe;
  3566. intel_enable_primary_hw_plane(crtc->primary, crtc);
  3567. intel_enable_sprite_planes(crtc);
  3568. intel_crtc_update_cursor(crtc, true);
  3569. intel_crtc_dpms_overlay(intel_crtc, true);
  3570. hsw_enable_ips(intel_crtc);
  3571. mutex_lock(&dev->struct_mutex);
  3572. intel_fbc_update(dev);
  3573. mutex_unlock(&dev->struct_mutex);
  3574. /*
  3575. * FIXME: Once we grow proper nuclear flip support out of this we need
  3576. * to compute the mask of flip planes precisely. For the time being
  3577. * consider this a flip from a NULL plane.
  3578. */
  3579. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3580. }
  3581. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  3582. {
  3583. struct drm_device *dev = crtc->dev;
  3584. struct drm_i915_private *dev_priv = dev->dev_private;
  3585. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3586. int pipe = intel_crtc->pipe;
  3587. int plane = intel_crtc->plane;
  3588. intel_crtc_wait_for_pending_flips(crtc);
  3589. if (dev_priv->fbc.plane == plane)
  3590. intel_fbc_disable(dev);
  3591. hsw_disable_ips(intel_crtc);
  3592. intel_crtc_dpms_overlay(intel_crtc, false);
  3593. intel_crtc_update_cursor(crtc, false);
  3594. intel_disable_sprite_planes(crtc);
  3595. intel_disable_primary_hw_plane(crtc->primary, crtc);
  3596. /*
  3597. * FIXME: Once we grow proper nuclear flip support out of this we need
  3598. * to compute the mask of flip planes precisely. For the time being
  3599. * consider this a flip to a NULL plane.
  3600. */
  3601. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3602. }
  3603. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  3604. {
  3605. struct drm_device *dev = crtc->dev;
  3606. struct drm_i915_private *dev_priv = dev->dev_private;
  3607. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3608. struct intel_encoder *encoder;
  3609. int pipe = intel_crtc->pipe;
  3610. WARN_ON(!crtc->enabled);
  3611. if (intel_crtc->active)
  3612. return;
  3613. if (intel_crtc->config.has_pch_encoder)
  3614. intel_prepare_shared_dpll(intel_crtc);
  3615. if (intel_crtc->config.has_dp_encoder)
  3616. intel_dp_set_m_n(intel_crtc);
  3617. intel_set_pipe_timings(intel_crtc);
  3618. if (intel_crtc->config.has_pch_encoder) {
  3619. intel_cpu_transcoder_set_m_n(intel_crtc,
  3620. &intel_crtc->config.fdi_m_n, NULL);
  3621. }
  3622. ironlake_set_pipeconf(crtc);
  3623. intel_crtc->active = true;
  3624. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3625. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  3626. for_each_encoder_on_crtc(dev, crtc, encoder)
  3627. if (encoder->pre_enable)
  3628. encoder->pre_enable(encoder);
  3629. if (intel_crtc->config.has_pch_encoder) {
  3630. /* Note: FDI PLL enabling _must_ be done before we enable the
  3631. * cpu pipes, hence this is separate from all the other fdi/pch
  3632. * enabling. */
  3633. ironlake_fdi_pll_enable(intel_crtc);
  3634. } else {
  3635. assert_fdi_tx_disabled(dev_priv, pipe);
  3636. assert_fdi_rx_disabled(dev_priv, pipe);
  3637. }
  3638. ironlake_pfit_enable(intel_crtc);
  3639. /*
  3640. * On ILK+ LUT must be loaded before the pipe is running but with
  3641. * clocks enabled
  3642. */
  3643. intel_crtc_load_lut(crtc);
  3644. intel_update_watermarks(crtc);
  3645. intel_enable_pipe(intel_crtc);
  3646. if (intel_crtc->config.has_pch_encoder)
  3647. ironlake_pch_enable(crtc);
  3648. assert_vblank_disabled(crtc);
  3649. drm_crtc_vblank_on(crtc);
  3650. for_each_encoder_on_crtc(dev, crtc, encoder)
  3651. encoder->enable(encoder);
  3652. if (HAS_PCH_CPT(dev))
  3653. cpt_verify_modeset(dev, intel_crtc->pipe);
  3654. intel_crtc_enable_planes(crtc);
  3655. }
  3656. /* IPS only exists on ULT machines and is tied to pipe A. */
  3657. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  3658. {
  3659. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  3660. }
  3661. /*
  3662. * This implements the workaround described in the "notes" section of the mode
  3663. * set sequence documentation. When going from no pipes or single pipe to
  3664. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  3665. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  3666. */
  3667. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  3668. {
  3669. struct drm_device *dev = crtc->base.dev;
  3670. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  3671. /* We want to get the other_active_crtc only if there's only 1 other
  3672. * active crtc. */
  3673. for_each_intel_crtc(dev, crtc_it) {
  3674. if (!crtc_it->active || crtc_it == crtc)
  3675. continue;
  3676. if (other_active_crtc)
  3677. return;
  3678. other_active_crtc = crtc_it;
  3679. }
  3680. if (!other_active_crtc)
  3681. return;
  3682. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3683. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3684. }
  3685. static void haswell_crtc_enable(struct drm_crtc *crtc)
  3686. {
  3687. struct drm_device *dev = crtc->dev;
  3688. struct drm_i915_private *dev_priv = dev->dev_private;
  3689. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3690. struct intel_encoder *encoder;
  3691. int pipe = intel_crtc->pipe;
  3692. WARN_ON(!crtc->enabled);
  3693. if (intel_crtc->active)
  3694. return;
  3695. if (intel_crtc_to_shared_dpll(intel_crtc))
  3696. intel_enable_shared_dpll(intel_crtc);
  3697. if (intel_crtc->config.has_dp_encoder)
  3698. intel_dp_set_m_n(intel_crtc);
  3699. intel_set_pipe_timings(intel_crtc);
  3700. if (intel_crtc->config.cpu_transcoder != TRANSCODER_EDP) {
  3701. I915_WRITE(PIPE_MULT(intel_crtc->config.cpu_transcoder),
  3702. intel_crtc->config.pixel_multiplier - 1);
  3703. }
  3704. if (intel_crtc->config.has_pch_encoder) {
  3705. intel_cpu_transcoder_set_m_n(intel_crtc,
  3706. &intel_crtc->config.fdi_m_n, NULL);
  3707. }
  3708. haswell_set_pipeconf(crtc);
  3709. intel_set_pipe_csc(crtc);
  3710. intel_crtc->active = true;
  3711. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3712. for_each_encoder_on_crtc(dev, crtc, encoder)
  3713. if (encoder->pre_enable)
  3714. encoder->pre_enable(encoder);
  3715. if (intel_crtc->config.has_pch_encoder) {
  3716. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  3717. true);
  3718. dev_priv->display.fdi_link_train(crtc);
  3719. }
  3720. intel_ddi_enable_pipe_clock(intel_crtc);
  3721. if (IS_SKYLAKE(dev))
  3722. skylake_pfit_enable(intel_crtc);
  3723. else
  3724. ironlake_pfit_enable(intel_crtc);
  3725. /*
  3726. * On ILK+ LUT must be loaded before the pipe is running but with
  3727. * clocks enabled
  3728. */
  3729. intel_crtc_load_lut(crtc);
  3730. intel_ddi_set_pipe_settings(crtc);
  3731. intel_ddi_enable_transcoder_func(crtc);
  3732. intel_update_watermarks(crtc);
  3733. intel_enable_pipe(intel_crtc);
  3734. if (intel_crtc->config.has_pch_encoder)
  3735. lpt_pch_enable(crtc);
  3736. if (intel_crtc->config.dp_encoder_is_mst)
  3737. intel_ddi_set_vc_payload_alloc(crtc, true);
  3738. assert_vblank_disabled(crtc);
  3739. drm_crtc_vblank_on(crtc);
  3740. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3741. encoder->enable(encoder);
  3742. intel_opregion_notify_encoder(encoder, true);
  3743. }
  3744. /* If we change the relative order between pipe/planes enabling, we need
  3745. * to change the workaround. */
  3746. haswell_mode_set_planes_workaround(intel_crtc);
  3747. intel_crtc_enable_planes(crtc);
  3748. }
  3749. static void skylake_pfit_disable(struct intel_crtc *crtc)
  3750. {
  3751. struct drm_device *dev = crtc->base.dev;
  3752. struct drm_i915_private *dev_priv = dev->dev_private;
  3753. int pipe = crtc->pipe;
  3754. /* To avoid upsetting the power well on haswell only disable the pfit if
  3755. * it's in use. The hw state code will make sure we get this right. */
  3756. if (crtc->config.pch_pfit.enabled) {
  3757. I915_WRITE(PS_CTL(pipe), 0);
  3758. I915_WRITE(PS_WIN_POS(pipe), 0);
  3759. I915_WRITE(PS_WIN_SZ(pipe), 0);
  3760. }
  3761. }
  3762. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  3763. {
  3764. struct drm_device *dev = crtc->base.dev;
  3765. struct drm_i915_private *dev_priv = dev->dev_private;
  3766. int pipe = crtc->pipe;
  3767. /* To avoid upsetting the power well on haswell only disable the pfit if
  3768. * it's in use. The hw state code will make sure we get this right. */
  3769. if (crtc->config.pch_pfit.enabled) {
  3770. I915_WRITE(PF_CTL(pipe), 0);
  3771. I915_WRITE(PF_WIN_POS(pipe), 0);
  3772. I915_WRITE(PF_WIN_SZ(pipe), 0);
  3773. }
  3774. }
  3775. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  3776. {
  3777. struct drm_device *dev = crtc->dev;
  3778. struct drm_i915_private *dev_priv = dev->dev_private;
  3779. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3780. struct intel_encoder *encoder;
  3781. int pipe = intel_crtc->pipe;
  3782. u32 reg, temp;
  3783. if (!intel_crtc->active)
  3784. return;
  3785. intel_crtc_disable_planes(crtc);
  3786. for_each_encoder_on_crtc(dev, crtc, encoder)
  3787. encoder->disable(encoder);
  3788. drm_crtc_vblank_off(crtc);
  3789. assert_vblank_disabled(crtc);
  3790. if (intel_crtc->config.has_pch_encoder)
  3791. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  3792. intel_disable_pipe(intel_crtc);
  3793. ironlake_pfit_disable(intel_crtc);
  3794. for_each_encoder_on_crtc(dev, crtc, encoder)
  3795. if (encoder->post_disable)
  3796. encoder->post_disable(encoder);
  3797. if (intel_crtc->config.has_pch_encoder) {
  3798. ironlake_fdi_disable(crtc);
  3799. ironlake_disable_pch_transcoder(dev_priv, pipe);
  3800. if (HAS_PCH_CPT(dev)) {
  3801. /* disable TRANS_DP_CTL */
  3802. reg = TRANS_DP_CTL(pipe);
  3803. temp = I915_READ(reg);
  3804. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  3805. TRANS_DP_PORT_SEL_MASK);
  3806. temp |= TRANS_DP_PORT_SEL_NONE;
  3807. I915_WRITE(reg, temp);
  3808. /* disable DPLL_SEL */
  3809. temp = I915_READ(PCH_DPLL_SEL);
  3810. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  3811. I915_WRITE(PCH_DPLL_SEL, temp);
  3812. }
  3813. /* disable PCH DPLL */
  3814. intel_disable_shared_dpll(intel_crtc);
  3815. ironlake_fdi_pll_disable(intel_crtc);
  3816. }
  3817. intel_crtc->active = false;
  3818. intel_update_watermarks(crtc);
  3819. mutex_lock(&dev->struct_mutex);
  3820. intel_fbc_update(dev);
  3821. mutex_unlock(&dev->struct_mutex);
  3822. }
  3823. static void haswell_crtc_disable(struct drm_crtc *crtc)
  3824. {
  3825. struct drm_device *dev = crtc->dev;
  3826. struct drm_i915_private *dev_priv = dev->dev_private;
  3827. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3828. struct intel_encoder *encoder;
  3829. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3830. if (!intel_crtc->active)
  3831. return;
  3832. intel_crtc_disable_planes(crtc);
  3833. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3834. intel_opregion_notify_encoder(encoder, false);
  3835. encoder->disable(encoder);
  3836. }
  3837. drm_crtc_vblank_off(crtc);
  3838. assert_vblank_disabled(crtc);
  3839. if (intel_crtc->config.has_pch_encoder)
  3840. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  3841. false);
  3842. intel_disable_pipe(intel_crtc);
  3843. if (intel_crtc->config.dp_encoder_is_mst)
  3844. intel_ddi_set_vc_payload_alloc(crtc, false);
  3845. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  3846. if (IS_SKYLAKE(dev))
  3847. skylake_pfit_disable(intel_crtc);
  3848. else
  3849. ironlake_pfit_disable(intel_crtc);
  3850. intel_ddi_disable_pipe_clock(intel_crtc);
  3851. if (intel_crtc->config.has_pch_encoder) {
  3852. lpt_disable_pch_transcoder(dev_priv);
  3853. intel_ddi_fdi_disable(crtc);
  3854. }
  3855. for_each_encoder_on_crtc(dev, crtc, encoder)
  3856. if (encoder->post_disable)
  3857. encoder->post_disable(encoder);
  3858. intel_crtc->active = false;
  3859. intel_update_watermarks(crtc);
  3860. mutex_lock(&dev->struct_mutex);
  3861. intel_fbc_update(dev);
  3862. mutex_unlock(&dev->struct_mutex);
  3863. if (intel_crtc_to_shared_dpll(intel_crtc))
  3864. intel_disable_shared_dpll(intel_crtc);
  3865. }
  3866. static void ironlake_crtc_off(struct drm_crtc *crtc)
  3867. {
  3868. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3869. intel_put_shared_dpll(intel_crtc);
  3870. }
  3871. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  3872. {
  3873. struct drm_device *dev = crtc->base.dev;
  3874. struct drm_i915_private *dev_priv = dev->dev_private;
  3875. struct intel_crtc_config *pipe_config = &crtc->config;
  3876. if (!crtc->config.gmch_pfit.control)
  3877. return;
  3878. /*
  3879. * The panel fitter should only be adjusted whilst the pipe is disabled,
  3880. * according to register description and PRM.
  3881. */
  3882. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  3883. assert_pipe_disabled(dev_priv, crtc->pipe);
  3884. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  3885. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  3886. /* Border color in case we don't scale up to the full screen. Black by
  3887. * default, change to something else for debugging. */
  3888. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  3889. }
  3890. static enum intel_display_power_domain port_to_power_domain(enum port port)
  3891. {
  3892. switch (port) {
  3893. case PORT_A:
  3894. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  3895. case PORT_B:
  3896. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  3897. case PORT_C:
  3898. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  3899. case PORT_D:
  3900. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  3901. default:
  3902. WARN_ON_ONCE(1);
  3903. return POWER_DOMAIN_PORT_OTHER;
  3904. }
  3905. }
  3906. #define for_each_power_domain(domain, mask) \
  3907. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  3908. if ((1 << (domain)) & (mask))
  3909. enum intel_display_power_domain
  3910. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  3911. {
  3912. struct drm_device *dev = intel_encoder->base.dev;
  3913. struct intel_digital_port *intel_dig_port;
  3914. switch (intel_encoder->type) {
  3915. case INTEL_OUTPUT_UNKNOWN:
  3916. /* Only DDI platforms should ever use this output type */
  3917. WARN_ON_ONCE(!HAS_DDI(dev));
  3918. case INTEL_OUTPUT_DISPLAYPORT:
  3919. case INTEL_OUTPUT_HDMI:
  3920. case INTEL_OUTPUT_EDP:
  3921. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  3922. return port_to_power_domain(intel_dig_port->port);
  3923. case INTEL_OUTPUT_DP_MST:
  3924. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  3925. return port_to_power_domain(intel_dig_port->port);
  3926. case INTEL_OUTPUT_ANALOG:
  3927. return POWER_DOMAIN_PORT_CRT;
  3928. case INTEL_OUTPUT_DSI:
  3929. return POWER_DOMAIN_PORT_DSI;
  3930. default:
  3931. return POWER_DOMAIN_PORT_OTHER;
  3932. }
  3933. }
  3934. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  3935. {
  3936. struct drm_device *dev = crtc->dev;
  3937. struct intel_encoder *intel_encoder;
  3938. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3939. enum pipe pipe = intel_crtc->pipe;
  3940. unsigned long mask;
  3941. enum transcoder transcoder;
  3942. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  3943. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  3944. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  3945. if (intel_crtc->config.pch_pfit.enabled ||
  3946. intel_crtc->config.pch_pfit.force_thru)
  3947. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  3948. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  3949. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  3950. return mask;
  3951. }
  3952. static void modeset_update_crtc_power_domains(struct drm_device *dev)
  3953. {
  3954. struct drm_i915_private *dev_priv = dev->dev_private;
  3955. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  3956. struct intel_crtc *crtc;
  3957. /*
  3958. * First get all needed power domains, then put all unneeded, to avoid
  3959. * any unnecessary toggling of the power wells.
  3960. */
  3961. for_each_intel_crtc(dev, crtc) {
  3962. enum intel_display_power_domain domain;
  3963. if (!crtc->base.enabled)
  3964. continue;
  3965. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  3966. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  3967. intel_display_power_get(dev_priv, domain);
  3968. }
  3969. if (dev_priv->display.modeset_global_resources)
  3970. dev_priv->display.modeset_global_resources(dev);
  3971. for_each_intel_crtc(dev, crtc) {
  3972. enum intel_display_power_domain domain;
  3973. for_each_power_domain(domain, crtc->enabled_power_domains)
  3974. intel_display_power_put(dev_priv, domain);
  3975. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  3976. }
  3977. intel_display_set_init_power(dev_priv, false);
  3978. }
  3979. /* returns HPLL frequency in kHz */
  3980. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  3981. {
  3982. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  3983. /* Obtain SKU information */
  3984. mutex_lock(&dev_priv->dpio_lock);
  3985. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  3986. CCK_FUSE_HPLL_FREQ_MASK;
  3987. mutex_unlock(&dev_priv->dpio_lock);
  3988. return vco_freq[hpll_freq] * 1000;
  3989. }
  3990. static void vlv_update_cdclk(struct drm_device *dev)
  3991. {
  3992. struct drm_i915_private *dev_priv = dev->dev_private;
  3993. dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  3994. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  3995. dev_priv->vlv_cdclk_freq);
  3996. /*
  3997. * Program the gmbus_freq based on the cdclk frequency.
  3998. * BSpec erroneously claims we should aim for 4MHz, but
  3999. * in fact 1MHz is the correct frequency.
  4000. */
  4001. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->vlv_cdclk_freq, 1000));
  4002. }
  4003. /* Adjust CDclk dividers to allow high res or save power if possible */
  4004. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4005. {
  4006. struct drm_i915_private *dev_priv = dev->dev_private;
  4007. u32 val, cmd;
  4008. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  4009. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4010. cmd = 2;
  4011. else if (cdclk == 266667)
  4012. cmd = 1;
  4013. else
  4014. cmd = 0;
  4015. mutex_lock(&dev_priv->rps.hw_lock);
  4016. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4017. val &= ~DSPFREQGUAR_MASK;
  4018. val |= (cmd << DSPFREQGUAR_SHIFT);
  4019. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4020. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4021. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4022. 50)) {
  4023. DRM_ERROR("timed out waiting for CDclk change\n");
  4024. }
  4025. mutex_unlock(&dev_priv->rps.hw_lock);
  4026. if (cdclk == 400000) {
  4027. u32 divider;
  4028. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4029. mutex_lock(&dev_priv->dpio_lock);
  4030. /* adjust cdclk divider */
  4031. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4032. val &= ~DISPLAY_FREQUENCY_VALUES;
  4033. val |= divider;
  4034. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4035. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4036. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4037. 50))
  4038. DRM_ERROR("timed out waiting for CDclk change\n");
  4039. mutex_unlock(&dev_priv->dpio_lock);
  4040. }
  4041. mutex_lock(&dev_priv->dpio_lock);
  4042. /* adjust self-refresh exit latency value */
  4043. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4044. val &= ~0x7f;
  4045. /*
  4046. * For high bandwidth configs, we set a higher latency in the bunit
  4047. * so that the core display fetch happens in time to avoid underruns.
  4048. */
  4049. if (cdclk == 400000)
  4050. val |= 4500 / 250; /* 4.5 usec */
  4051. else
  4052. val |= 3000 / 250; /* 3.0 usec */
  4053. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4054. mutex_unlock(&dev_priv->dpio_lock);
  4055. vlv_update_cdclk(dev);
  4056. }
  4057. static void cherryview_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. switch (cdclk) {
  4063. case 400000:
  4064. cmd = 3;
  4065. break;
  4066. case 333333:
  4067. case 320000:
  4068. cmd = 2;
  4069. break;
  4070. case 266667:
  4071. cmd = 1;
  4072. break;
  4073. case 200000:
  4074. cmd = 0;
  4075. break;
  4076. default:
  4077. MISSING_CASE(cdclk);
  4078. return;
  4079. }
  4080. mutex_lock(&dev_priv->rps.hw_lock);
  4081. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4082. val &= ~DSPFREQGUAR_MASK_CHV;
  4083. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4084. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4085. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4086. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4087. 50)) {
  4088. DRM_ERROR("timed out waiting for CDclk change\n");
  4089. }
  4090. mutex_unlock(&dev_priv->rps.hw_lock);
  4091. vlv_update_cdclk(dev);
  4092. }
  4093. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4094. int max_pixclk)
  4095. {
  4096. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4097. /* FIXME: Punit isn't quite ready yet */
  4098. if (IS_CHERRYVIEW(dev_priv->dev))
  4099. return 400000;
  4100. /*
  4101. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4102. * 200MHz
  4103. * 267MHz
  4104. * 320/333MHz (depends on HPLL freq)
  4105. * 400MHz
  4106. * So we check to see whether we're above 90% of the lower bin and
  4107. * adjust if needed.
  4108. *
  4109. * We seem to get an unstable or solid color picture at 200MHz.
  4110. * Not sure what's wrong. For now use 200MHz only when all pipes
  4111. * are off.
  4112. */
  4113. if (max_pixclk > freq_320*9/10)
  4114. return 400000;
  4115. else if (max_pixclk > 266667*9/10)
  4116. return freq_320;
  4117. else if (max_pixclk > 0)
  4118. return 266667;
  4119. else
  4120. return 200000;
  4121. }
  4122. /* compute the max pixel clock for new configuration */
  4123. static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
  4124. {
  4125. struct drm_device *dev = dev_priv->dev;
  4126. struct intel_crtc *intel_crtc;
  4127. int max_pixclk = 0;
  4128. for_each_intel_crtc(dev, intel_crtc) {
  4129. if (intel_crtc->new_enabled)
  4130. max_pixclk = max(max_pixclk,
  4131. intel_crtc->new_config->adjusted_mode.crtc_clock);
  4132. }
  4133. return max_pixclk;
  4134. }
  4135. static void valleyview_modeset_global_pipes(struct drm_device *dev,
  4136. unsigned *prepare_pipes)
  4137. {
  4138. struct drm_i915_private *dev_priv = dev->dev_private;
  4139. struct intel_crtc *intel_crtc;
  4140. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4141. if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
  4142. dev_priv->vlv_cdclk_freq)
  4143. return;
  4144. /* disable/enable all currently active pipes while we change cdclk */
  4145. for_each_intel_crtc(dev, intel_crtc)
  4146. if (intel_crtc->base.enabled)
  4147. *prepare_pipes |= (1 << intel_crtc->pipe);
  4148. }
  4149. static void valleyview_modeset_global_resources(struct drm_device *dev)
  4150. {
  4151. struct drm_i915_private *dev_priv = dev->dev_private;
  4152. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4153. int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  4154. if (req_cdclk != dev_priv->vlv_cdclk_freq) {
  4155. /*
  4156. * FIXME: We can end up here with all power domains off, yet
  4157. * with a CDCLK frequency other than the minimum. To account
  4158. * for this take the PIPE-A power domain, which covers the HW
  4159. * blocks needed for the following programming. This can be
  4160. * removed once it's guaranteed that we get here either with
  4161. * the minimum CDCLK set, or the required power domains
  4162. * enabled.
  4163. */
  4164. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  4165. if (IS_CHERRYVIEW(dev))
  4166. cherryview_set_cdclk(dev, req_cdclk);
  4167. else
  4168. valleyview_set_cdclk(dev, req_cdclk);
  4169. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  4170. }
  4171. }
  4172. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  4173. {
  4174. struct drm_device *dev = crtc->dev;
  4175. struct drm_i915_private *dev_priv = to_i915(dev);
  4176. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4177. struct intel_encoder *encoder;
  4178. int pipe = intel_crtc->pipe;
  4179. bool is_dsi;
  4180. WARN_ON(!crtc->enabled);
  4181. if (intel_crtc->active)
  4182. return;
  4183. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  4184. if (!is_dsi) {
  4185. if (IS_CHERRYVIEW(dev))
  4186. chv_prepare_pll(intel_crtc, &intel_crtc->config);
  4187. else
  4188. vlv_prepare_pll(intel_crtc, &intel_crtc->config);
  4189. }
  4190. if (intel_crtc->config.has_dp_encoder)
  4191. intel_dp_set_m_n(intel_crtc);
  4192. intel_set_pipe_timings(intel_crtc);
  4193. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  4194. struct drm_i915_private *dev_priv = dev->dev_private;
  4195. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  4196. I915_WRITE(CHV_CANVAS(pipe), 0);
  4197. }
  4198. i9xx_set_pipeconf(intel_crtc);
  4199. intel_crtc->active = true;
  4200. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4201. for_each_encoder_on_crtc(dev, crtc, encoder)
  4202. if (encoder->pre_pll_enable)
  4203. encoder->pre_pll_enable(encoder);
  4204. if (!is_dsi) {
  4205. if (IS_CHERRYVIEW(dev))
  4206. chv_enable_pll(intel_crtc, &intel_crtc->config);
  4207. else
  4208. vlv_enable_pll(intel_crtc, &intel_crtc->config);
  4209. }
  4210. for_each_encoder_on_crtc(dev, crtc, encoder)
  4211. if (encoder->pre_enable)
  4212. encoder->pre_enable(encoder);
  4213. i9xx_pfit_enable(intel_crtc);
  4214. intel_crtc_load_lut(crtc);
  4215. intel_update_watermarks(crtc);
  4216. intel_enable_pipe(intel_crtc);
  4217. assert_vblank_disabled(crtc);
  4218. drm_crtc_vblank_on(crtc);
  4219. for_each_encoder_on_crtc(dev, crtc, encoder)
  4220. encoder->enable(encoder);
  4221. intel_crtc_enable_planes(crtc);
  4222. /* Underruns don't raise interrupts, so check manually. */
  4223. i9xx_check_fifo_underruns(dev_priv);
  4224. }
  4225. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  4226. {
  4227. struct drm_device *dev = crtc->base.dev;
  4228. struct drm_i915_private *dev_priv = dev->dev_private;
  4229. I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
  4230. I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
  4231. }
  4232. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  4233. {
  4234. struct drm_device *dev = crtc->dev;
  4235. struct drm_i915_private *dev_priv = to_i915(dev);
  4236. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4237. struct intel_encoder *encoder;
  4238. int pipe = intel_crtc->pipe;
  4239. WARN_ON(!crtc->enabled);
  4240. if (intel_crtc->active)
  4241. return;
  4242. i9xx_set_pll_dividers(intel_crtc);
  4243. if (intel_crtc->config.has_dp_encoder)
  4244. intel_dp_set_m_n(intel_crtc);
  4245. intel_set_pipe_timings(intel_crtc);
  4246. i9xx_set_pipeconf(intel_crtc);
  4247. intel_crtc->active = true;
  4248. if (!IS_GEN2(dev))
  4249. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4250. for_each_encoder_on_crtc(dev, crtc, encoder)
  4251. if (encoder->pre_enable)
  4252. encoder->pre_enable(encoder);
  4253. i9xx_enable_pll(intel_crtc);
  4254. i9xx_pfit_enable(intel_crtc);
  4255. intel_crtc_load_lut(crtc);
  4256. intel_update_watermarks(crtc);
  4257. intel_enable_pipe(intel_crtc);
  4258. assert_vblank_disabled(crtc);
  4259. drm_crtc_vblank_on(crtc);
  4260. for_each_encoder_on_crtc(dev, crtc, encoder)
  4261. encoder->enable(encoder);
  4262. intel_crtc_enable_planes(crtc);
  4263. /*
  4264. * Gen2 reports pipe underruns whenever all planes are disabled.
  4265. * So don't enable underrun reporting before at least some planes
  4266. * are enabled.
  4267. * FIXME: Need to fix the logic to work when we turn off all planes
  4268. * but leave the pipe running.
  4269. */
  4270. if (IS_GEN2(dev))
  4271. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4272. /* Underruns don't raise interrupts, so check manually. */
  4273. i9xx_check_fifo_underruns(dev_priv);
  4274. }
  4275. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4276. {
  4277. struct drm_device *dev = crtc->base.dev;
  4278. struct drm_i915_private *dev_priv = dev->dev_private;
  4279. if (!crtc->config.gmch_pfit.control)
  4280. return;
  4281. assert_pipe_disabled(dev_priv, crtc->pipe);
  4282. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4283. I915_READ(PFIT_CONTROL));
  4284. I915_WRITE(PFIT_CONTROL, 0);
  4285. }
  4286. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  4287. {
  4288. struct drm_device *dev = crtc->dev;
  4289. struct drm_i915_private *dev_priv = dev->dev_private;
  4290. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4291. struct intel_encoder *encoder;
  4292. int pipe = intel_crtc->pipe;
  4293. if (!intel_crtc->active)
  4294. return;
  4295. /*
  4296. * Gen2 reports pipe underruns whenever all planes are disabled.
  4297. * So diasble underrun reporting before all the planes get disabled.
  4298. * FIXME: Need to fix the logic to work when we turn off all planes
  4299. * but leave the pipe running.
  4300. */
  4301. if (IS_GEN2(dev))
  4302. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4303. /*
  4304. * Vblank time updates from the shadow to live plane control register
  4305. * are blocked if the memory self-refresh mode is active at that
  4306. * moment. So to make sure the plane gets truly disabled, disable
  4307. * first the self-refresh mode. The self-refresh enable bit in turn
  4308. * will be checked/applied by the HW only at the next frame start
  4309. * event which is after the vblank start event, so we need to have a
  4310. * wait-for-vblank between disabling the plane and the pipe.
  4311. */
  4312. intel_set_memory_cxsr(dev_priv, false);
  4313. intel_crtc_disable_planes(crtc);
  4314. /*
  4315. * On gen2 planes are double buffered but the pipe isn't, so we must
  4316. * wait for planes to fully turn off before disabling the pipe.
  4317. * We also need to wait on all gmch platforms because of the
  4318. * self-refresh mode constraint explained above.
  4319. */
  4320. intel_wait_for_vblank(dev, pipe);
  4321. for_each_encoder_on_crtc(dev, crtc, encoder)
  4322. encoder->disable(encoder);
  4323. drm_crtc_vblank_off(crtc);
  4324. assert_vblank_disabled(crtc);
  4325. intel_disable_pipe(intel_crtc);
  4326. i9xx_pfit_disable(intel_crtc);
  4327. for_each_encoder_on_crtc(dev, crtc, encoder)
  4328. if (encoder->post_disable)
  4329. encoder->post_disable(encoder);
  4330. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  4331. if (IS_CHERRYVIEW(dev))
  4332. chv_disable_pll(dev_priv, pipe);
  4333. else if (IS_VALLEYVIEW(dev))
  4334. vlv_disable_pll(dev_priv, pipe);
  4335. else
  4336. i9xx_disable_pll(intel_crtc);
  4337. }
  4338. if (!IS_GEN2(dev))
  4339. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4340. intel_crtc->active = false;
  4341. intel_update_watermarks(crtc);
  4342. mutex_lock(&dev->struct_mutex);
  4343. intel_fbc_update(dev);
  4344. mutex_unlock(&dev->struct_mutex);
  4345. }
  4346. static void i9xx_crtc_off(struct drm_crtc *crtc)
  4347. {
  4348. }
  4349. /* Master function to enable/disable CRTC and corresponding power wells */
  4350. void intel_crtc_control(struct drm_crtc *crtc, bool enable)
  4351. {
  4352. struct drm_device *dev = crtc->dev;
  4353. struct drm_i915_private *dev_priv = dev->dev_private;
  4354. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4355. enum intel_display_power_domain domain;
  4356. unsigned long domains;
  4357. if (enable) {
  4358. if (!intel_crtc->active) {
  4359. domains = get_crtc_power_domains(crtc);
  4360. for_each_power_domain(domain, domains)
  4361. intel_display_power_get(dev_priv, domain);
  4362. intel_crtc->enabled_power_domains = domains;
  4363. dev_priv->display.crtc_enable(crtc);
  4364. }
  4365. } else {
  4366. if (intel_crtc->active) {
  4367. dev_priv->display.crtc_disable(crtc);
  4368. domains = intel_crtc->enabled_power_domains;
  4369. for_each_power_domain(domain, domains)
  4370. intel_display_power_put(dev_priv, domain);
  4371. intel_crtc->enabled_power_domains = 0;
  4372. }
  4373. }
  4374. }
  4375. /**
  4376. * Sets the power management mode of the pipe and plane.
  4377. */
  4378. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  4379. {
  4380. struct drm_device *dev = crtc->dev;
  4381. struct intel_encoder *intel_encoder;
  4382. bool enable = false;
  4383. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4384. enable |= intel_encoder->connectors_active;
  4385. intel_crtc_control(crtc, enable);
  4386. }
  4387. static void intel_crtc_disable(struct drm_crtc *crtc)
  4388. {
  4389. struct drm_device *dev = crtc->dev;
  4390. struct drm_connector *connector;
  4391. struct drm_i915_private *dev_priv = dev->dev_private;
  4392. /* crtc should still be enabled when we disable it. */
  4393. WARN_ON(!crtc->enabled);
  4394. dev_priv->display.crtc_disable(crtc);
  4395. dev_priv->display.off(crtc);
  4396. crtc->primary->funcs->disable_plane(crtc->primary);
  4397. /* Update computed state. */
  4398. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  4399. if (!connector->encoder || !connector->encoder->crtc)
  4400. continue;
  4401. if (connector->encoder->crtc != crtc)
  4402. continue;
  4403. connector->dpms = DRM_MODE_DPMS_OFF;
  4404. to_intel_encoder(connector->encoder)->connectors_active = false;
  4405. }
  4406. }
  4407. void intel_encoder_destroy(struct drm_encoder *encoder)
  4408. {
  4409. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4410. drm_encoder_cleanup(encoder);
  4411. kfree(intel_encoder);
  4412. }
  4413. /* Simple dpms helper for encoders with just one connector, no cloning and only
  4414. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  4415. * state of the entire output pipe. */
  4416. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  4417. {
  4418. if (mode == DRM_MODE_DPMS_ON) {
  4419. encoder->connectors_active = true;
  4420. intel_crtc_update_dpms(encoder->base.crtc);
  4421. } else {
  4422. encoder->connectors_active = false;
  4423. intel_crtc_update_dpms(encoder->base.crtc);
  4424. }
  4425. }
  4426. /* Cross check the actual hw state with our own modeset state tracking (and it's
  4427. * internal consistency). */
  4428. static void intel_connector_check_state(struct intel_connector *connector)
  4429. {
  4430. if (connector->get_hw_state(connector)) {
  4431. struct intel_encoder *encoder = connector->encoder;
  4432. struct drm_crtc *crtc;
  4433. bool encoder_enabled;
  4434. enum pipe pipe;
  4435. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4436. connector->base.base.id,
  4437. connector->base.name);
  4438. /* there is no real hw state for MST connectors */
  4439. if (connector->mst_port)
  4440. return;
  4441. I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  4442. "wrong connector dpms state\n");
  4443. I915_STATE_WARN(connector->base.encoder != &encoder->base,
  4444. "active connector not linked to encoder\n");
  4445. if (encoder) {
  4446. I915_STATE_WARN(!encoder->connectors_active,
  4447. "encoder->connectors_active not set\n");
  4448. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  4449. I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
  4450. if (I915_STATE_WARN_ON(!encoder->base.crtc))
  4451. return;
  4452. crtc = encoder->base.crtc;
  4453. I915_STATE_WARN(!crtc->enabled, "crtc not enabled\n");
  4454. I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  4455. I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
  4456. "encoder active on the wrong pipe\n");
  4457. }
  4458. }
  4459. }
  4460. /* Even simpler default implementation, if there's really no special case to
  4461. * consider. */
  4462. void intel_connector_dpms(struct drm_connector *connector, int mode)
  4463. {
  4464. /* All the simple cases only support two dpms states. */
  4465. if (mode != DRM_MODE_DPMS_ON)
  4466. mode = DRM_MODE_DPMS_OFF;
  4467. if (mode == connector->dpms)
  4468. return;
  4469. connector->dpms = mode;
  4470. /* Only need to change hw state when actually enabled */
  4471. if (connector->encoder)
  4472. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  4473. intel_modeset_check_state(connector->dev);
  4474. }
  4475. /* Simple connector->get_hw_state implementation for encoders that support only
  4476. * one connector and no cloning and hence the encoder state determines the state
  4477. * of the connector. */
  4478. bool intel_connector_get_hw_state(struct intel_connector *connector)
  4479. {
  4480. enum pipe pipe = 0;
  4481. struct intel_encoder *encoder = connector->encoder;
  4482. return encoder->get_hw_state(encoder, &pipe);
  4483. }
  4484. static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  4485. struct intel_crtc_config *pipe_config)
  4486. {
  4487. struct drm_i915_private *dev_priv = dev->dev_private;
  4488. struct intel_crtc *pipe_B_crtc =
  4489. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  4490. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  4491. pipe_name(pipe), pipe_config->fdi_lanes);
  4492. if (pipe_config->fdi_lanes > 4) {
  4493. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  4494. pipe_name(pipe), pipe_config->fdi_lanes);
  4495. return false;
  4496. }
  4497. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  4498. if (pipe_config->fdi_lanes > 2) {
  4499. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  4500. pipe_config->fdi_lanes);
  4501. return false;
  4502. } else {
  4503. return true;
  4504. }
  4505. }
  4506. if (INTEL_INFO(dev)->num_pipes == 2)
  4507. return true;
  4508. /* Ivybridge 3 pipe is really complicated */
  4509. switch (pipe) {
  4510. case PIPE_A:
  4511. return true;
  4512. case PIPE_B:
  4513. if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
  4514. pipe_config->fdi_lanes > 2) {
  4515. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4516. pipe_name(pipe), pipe_config->fdi_lanes);
  4517. return false;
  4518. }
  4519. return true;
  4520. case PIPE_C:
  4521. if (!pipe_has_enabled_pch(pipe_B_crtc) ||
  4522. pipe_B_crtc->config.fdi_lanes <= 2) {
  4523. if (pipe_config->fdi_lanes > 2) {
  4524. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4525. pipe_name(pipe), pipe_config->fdi_lanes);
  4526. return false;
  4527. }
  4528. } else {
  4529. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  4530. return false;
  4531. }
  4532. return true;
  4533. default:
  4534. BUG();
  4535. }
  4536. }
  4537. #define RETRY 1
  4538. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  4539. struct intel_crtc_config *pipe_config)
  4540. {
  4541. struct drm_device *dev = intel_crtc->base.dev;
  4542. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4543. int lane, link_bw, fdi_dotclock;
  4544. bool setup_ok, needs_recompute = false;
  4545. retry:
  4546. /* FDI is a binary signal running at ~2.7GHz, encoding
  4547. * each output octet as 10 bits. The actual frequency
  4548. * is stored as a divider into a 100MHz clock, and the
  4549. * mode pixel clock is stored in units of 1KHz.
  4550. * Hence the bw of each lane in terms of the mode signal
  4551. * is:
  4552. */
  4553. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4554. fdi_dotclock = adjusted_mode->crtc_clock;
  4555. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  4556. pipe_config->pipe_bpp);
  4557. pipe_config->fdi_lanes = lane;
  4558. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  4559. link_bw, &pipe_config->fdi_m_n);
  4560. setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  4561. intel_crtc->pipe, pipe_config);
  4562. if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
  4563. pipe_config->pipe_bpp -= 2*3;
  4564. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  4565. pipe_config->pipe_bpp);
  4566. needs_recompute = true;
  4567. pipe_config->bw_constrained = true;
  4568. goto retry;
  4569. }
  4570. if (needs_recompute)
  4571. return RETRY;
  4572. return setup_ok ? 0 : -EINVAL;
  4573. }
  4574. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  4575. struct intel_crtc_config *pipe_config)
  4576. {
  4577. pipe_config->ips_enabled = i915.enable_ips &&
  4578. hsw_crtc_supports_ips(crtc) &&
  4579. pipe_config->pipe_bpp <= 24;
  4580. }
  4581. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  4582. struct intel_crtc_config *pipe_config)
  4583. {
  4584. struct drm_device *dev = crtc->base.dev;
  4585. struct drm_i915_private *dev_priv = dev->dev_private;
  4586. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4587. /* FIXME should check pixel clock limits on all platforms */
  4588. if (INTEL_INFO(dev)->gen < 4) {
  4589. int clock_limit =
  4590. dev_priv->display.get_display_clock_speed(dev);
  4591. /*
  4592. * Enable pixel doubling when the dot clock
  4593. * is > 90% of the (display) core speed.
  4594. *
  4595. * GDG double wide on either pipe,
  4596. * otherwise pipe A only.
  4597. */
  4598. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  4599. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  4600. clock_limit *= 2;
  4601. pipe_config->double_wide = true;
  4602. }
  4603. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  4604. return -EINVAL;
  4605. }
  4606. /*
  4607. * Pipe horizontal size must be even in:
  4608. * - DVO ganged mode
  4609. * - LVDS dual channel mode
  4610. * - Double wide pipe
  4611. */
  4612. if ((intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  4613. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  4614. pipe_config->pipe_src_w &= ~1;
  4615. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  4616. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  4617. */
  4618. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  4619. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  4620. return -EINVAL;
  4621. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
  4622. pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
  4623. } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
  4624. /* only a 8bpc pipe, with 6bpc dither through the panel fitter
  4625. * for lvds. */
  4626. pipe_config->pipe_bpp = 8*3;
  4627. }
  4628. if (HAS_IPS(dev))
  4629. hsw_compute_ips_config(crtc, pipe_config);
  4630. if (pipe_config->has_pch_encoder)
  4631. return ironlake_fdi_compute_config(crtc, pipe_config);
  4632. return 0;
  4633. }
  4634. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  4635. {
  4636. struct drm_i915_private *dev_priv = dev->dev_private;
  4637. u32 val;
  4638. int divider;
  4639. /* FIXME: Punit isn't quite ready yet */
  4640. if (IS_CHERRYVIEW(dev))
  4641. return 400000;
  4642. if (dev_priv->hpll_freq == 0)
  4643. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  4644. mutex_lock(&dev_priv->dpio_lock);
  4645. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4646. mutex_unlock(&dev_priv->dpio_lock);
  4647. divider = val & DISPLAY_FREQUENCY_VALUES;
  4648. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  4649. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4650. "cdclk change in progress\n");
  4651. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  4652. }
  4653. static int i945_get_display_clock_speed(struct drm_device *dev)
  4654. {
  4655. return 400000;
  4656. }
  4657. static int i915_get_display_clock_speed(struct drm_device *dev)
  4658. {
  4659. return 333000;
  4660. }
  4661. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  4662. {
  4663. return 200000;
  4664. }
  4665. static int pnv_get_display_clock_speed(struct drm_device *dev)
  4666. {
  4667. u16 gcfgc = 0;
  4668. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4669. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4670. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  4671. return 267000;
  4672. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  4673. return 333000;
  4674. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  4675. return 444000;
  4676. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  4677. return 200000;
  4678. default:
  4679. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  4680. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  4681. return 133000;
  4682. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  4683. return 167000;
  4684. }
  4685. }
  4686. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  4687. {
  4688. u16 gcfgc = 0;
  4689. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4690. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  4691. return 133000;
  4692. else {
  4693. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4694. case GC_DISPLAY_CLOCK_333_MHZ:
  4695. return 333000;
  4696. default:
  4697. case GC_DISPLAY_CLOCK_190_200_MHZ:
  4698. return 190000;
  4699. }
  4700. }
  4701. }
  4702. static int i865_get_display_clock_speed(struct drm_device *dev)
  4703. {
  4704. return 266000;
  4705. }
  4706. static int i855_get_display_clock_speed(struct drm_device *dev)
  4707. {
  4708. u16 hpllcc = 0;
  4709. /* Assume that the hardware is in the high speed state. This
  4710. * should be the default.
  4711. */
  4712. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  4713. case GC_CLOCK_133_200:
  4714. case GC_CLOCK_100_200:
  4715. return 200000;
  4716. case GC_CLOCK_166_250:
  4717. return 250000;
  4718. case GC_CLOCK_100_133:
  4719. return 133000;
  4720. }
  4721. /* Shouldn't happen */
  4722. return 0;
  4723. }
  4724. static int i830_get_display_clock_speed(struct drm_device *dev)
  4725. {
  4726. return 133000;
  4727. }
  4728. static void
  4729. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  4730. {
  4731. while (*num > DATA_LINK_M_N_MASK ||
  4732. *den > DATA_LINK_M_N_MASK) {
  4733. *num >>= 1;
  4734. *den >>= 1;
  4735. }
  4736. }
  4737. static void compute_m_n(unsigned int m, unsigned int n,
  4738. uint32_t *ret_m, uint32_t *ret_n)
  4739. {
  4740. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  4741. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  4742. intel_reduce_m_n_ratio(ret_m, ret_n);
  4743. }
  4744. void
  4745. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  4746. int pixel_clock, int link_clock,
  4747. struct intel_link_m_n *m_n)
  4748. {
  4749. m_n->tu = 64;
  4750. compute_m_n(bits_per_pixel * pixel_clock,
  4751. link_clock * nlanes * 8,
  4752. &m_n->gmch_m, &m_n->gmch_n);
  4753. compute_m_n(pixel_clock, link_clock,
  4754. &m_n->link_m, &m_n->link_n);
  4755. }
  4756. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  4757. {
  4758. if (i915.panel_use_ssc >= 0)
  4759. return i915.panel_use_ssc != 0;
  4760. return dev_priv->vbt.lvds_use_ssc
  4761. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  4762. }
  4763. static int i9xx_get_refclk(struct intel_crtc *crtc, int num_connectors)
  4764. {
  4765. struct drm_device *dev = crtc->base.dev;
  4766. struct drm_i915_private *dev_priv = dev->dev_private;
  4767. int refclk;
  4768. if (IS_VALLEYVIEW(dev)) {
  4769. refclk = 100000;
  4770. } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  4771. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4772. refclk = dev_priv->vbt.lvds_ssc_freq;
  4773. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  4774. } else if (!IS_GEN2(dev)) {
  4775. refclk = 96000;
  4776. } else {
  4777. refclk = 48000;
  4778. }
  4779. return refclk;
  4780. }
  4781. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  4782. {
  4783. return (1 << dpll->n) << 16 | dpll->m2;
  4784. }
  4785. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  4786. {
  4787. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  4788. }
  4789. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  4790. intel_clock_t *reduced_clock)
  4791. {
  4792. struct drm_device *dev = crtc->base.dev;
  4793. u32 fp, fp2 = 0;
  4794. if (IS_PINEVIEW(dev)) {
  4795. fp = pnv_dpll_compute_fp(&crtc->new_config->dpll);
  4796. if (reduced_clock)
  4797. fp2 = pnv_dpll_compute_fp(reduced_clock);
  4798. } else {
  4799. fp = i9xx_dpll_compute_fp(&crtc->new_config->dpll);
  4800. if (reduced_clock)
  4801. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  4802. }
  4803. crtc->new_config->dpll_hw_state.fp0 = fp;
  4804. crtc->lowfreq_avail = false;
  4805. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  4806. reduced_clock && i915.powersave) {
  4807. crtc->new_config->dpll_hw_state.fp1 = fp2;
  4808. crtc->lowfreq_avail = true;
  4809. } else {
  4810. crtc->new_config->dpll_hw_state.fp1 = fp;
  4811. }
  4812. }
  4813. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  4814. pipe)
  4815. {
  4816. u32 reg_val;
  4817. /*
  4818. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  4819. * and set it to a reasonable value instead.
  4820. */
  4821. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4822. reg_val &= 0xffffff00;
  4823. reg_val |= 0x00000030;
  4824. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4825. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4826. reg_val &= 0x8cffffff;
  4827. reg_val = 0x8c000000;
  4828. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4829. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4830. reg_val &= 0xffffff00;
  4831. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4832. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4833. reg_val &= 0x00ffffff;
  4834. reg_val |= 0xb0000000;
  4835. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4836. }
  4837. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  4838. struct intel_link_m_n *m_n)
  4839. {
  4840. struct drm_device *dev = crtc->base.dev;
  4841. struct drm_i915_private *dev_priv = dev->dev_private;
  4842. int pipe = crtc->pipe;
  4843. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4844. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  4845. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  4846. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  4847. }
  4848. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  4849. struct intel_link_m_n *m_n,
  4850. struct intel_link_m_n *m2_n2)
  4851. {
  4852. struct drm_device *dev = crtc->base.dev;
  4853. struct drm_i915_private *dev_priv = dev->dev_private;
  4854. int pipe = crtc->pipe;
  4855. enum transcoder transcoder = crtc->config.cpu_transcoder;
  4856. if (INTEL_INFO(dev)->gen >= 5) {
  4857. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4858. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  4859. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  4860. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  4861. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  4862. * for gen < 8) and if DRRS is supported (to make sure the
  4863. * registers are not unnecessarily accessed).
  4864. */
  4865. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  4866. crtc->config.has_drrs) {
  4867. I915_WRITE(PIPE_DATA_M2(transcoder),
  4868. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  4869. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  4870. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  4871. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  4872. }
  4873. } else {
  4874. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4875. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  4876. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  4877. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  4878. }
  4879. }
  4880. void intel_dp_set_m_n(struct intel_crtc *crtc)
  4881. {
  4882. if (crtc->config.has_pch_encoder)
  4883. intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
  4884. else
  4885. intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n,
  4886. &crtc->config.dp_m2_n2);
  4887. }
  4888. static void vlv_update_pll(struct intel_crtc *crtc,
  4889. struct intel_crtc_config *pipe_config)
  4890. {
  4891. u32 dpll, dpll_md;
  4892. /*
  4893. * Enable DPIO clock input. We should never disable the reference
  4894. * clock for pipe B, since VGA hotplug / manual detection depends
  4895. * on it.
  4896. */
  4897. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  4898. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  4899. /* We should never disable this, set it here for state tracking */
  4900. if (crtc->pipe == PIPE_B)
  4901. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4902. dpll |= DPLL_VCO_ENABLE;
  4903. pipe_config->dpll_hw_state.dpll = dpll;
  4904. dpll_md = (pipe_config->pixel_multiplier - 1)
  4905. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4906. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  4907. }
  4908. static void vlv_prepare_pll(struct intel_crtc *crtc,
  4909. const struct intel_crtc_config *pipe_config)
  4910. {
  4911. struct drm_device *dev = crtc->base.dev;
  4912. struct drm_i915_private *dev_priv = dev->dev_private;
  4913. int pipe = crtc->pipe;
  4914. u32 mdiv;
  4915. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  4916. u32 coreclk, reg_val;
  4917. mutex_lock(&dev_priv->dpio_lock);
  4918. bestn = pipe_config->dpll.n;
  4919. bestm1 = pipe_config->dpll.m1;
  4920. bestm2 = pipe_config->dpll.m2;
  4921. bestp1 = pipe_config->dpll.p1;
  4922. bestp2 = pipe_config->dpll.p2;
  4923. /* See eDP HDMI DPIO driver vbios notes doc */
  4924. /* PLL B needs special handling */
  4925. if (pipe == PIPE_B)
  4926. vlv_pllb_recal_opamp(dev_priv, pipe);
  4927. /* Set up Tx target for periodic Rcomp update */
  4928. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  4929. /* Disable target IRef on PLL */
  4930. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  4931. reg_val &= 0x00ffffff;
  4932. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  4933. /* Disable fast lock */
  4934. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  4935. /* Set idtafcrecal before PLL is enabled */
  4936. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  4937. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  4938. mdiv |= ((bestn << DPIO_N_SHIFT));
  4939. mdiv |= (1 << DPIO_K_SHIFT);
  4940. /*
  4941. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  4942. * but we don't support that).
  4943. * Note: don't use the DAC post divider as it seems unstable.
  4944. */
  4945. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  4946. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4947. mdiv |= DPIO_ENABLE_CALIBRATION;
  4948. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4949. /* Set HBR and RBR LPF coefficients */
  4950. if (pipe_config->port_clock == 162000 ||
  4951. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  4952. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  4953. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4954. 0x009f0003);
  4955. else
  4956. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4957. 0x00d0000f);
  4958. if (crtc->config.has_dp_encoder) {
  4959. /* Use SSC source */
  4960. if (pipe == PIPE_A)
  4961. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4962. 0x0df40000);
  4963. else
  4964. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4965. 0x0df70000);
  4966. } else { /* HDMI or VGA */
  4967. /* Use bend source */
  4968. if (pipe == PIPE_A)
  4969. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4970. 0x0df70000);
  4971. else
  4972. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4973. 0x0df40000);
  4974. }
  4975. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  4976. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  4977. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  4978. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  4979. coreclk |= 0x01000000;
  4980. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  4981. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  4982. mutex_unlock(&dev_priv->dpio_lock);
  4983. }
  4984. static void chv_update_pll(struct intel_crtc *crtc,
  4985. struct intel_crtc_config *pipe_config)
  4986. {
  4987. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  4988. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  4989. DPLL_VCO_ENABLE;
  4990. if (crtc->pipe != PIPE_A)
  4991. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4992. pipe_config->dpll_hw_state.dpll_md =
  4993. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4994. }
  4995. static void chv_prepare_pll(struct intel_crtc *crtc,
  4996. const struct intel_crtc_config *pipe_config)
  4997. {
  4998. struct drm_device *dev = crtc->base.dev;
  4999. struct drm_i915_private *dev_priv = dev->dev_private;
  5000. int pipe = crtc->pipe;
  5001. int dpll_reg = DPLL(crtc->pipe);
  5002. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5003. u32 loopfilter, intcoeff;
  5004. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5005. int refclk;
  5006. bestn = pipe_config->dpll.n;
  5007. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5008. bestm1 = pipe_config->dpll.m1;
  5009. bestm2 = pipe_config->dpll.m2 >> 22;
  5010. bestp1 = pipe_config->dpll.p1;
  5011. bestp2 = pipe_config->dpll.p2;
  5012. /*
  5013. * Enable Refclk and SSC
  5014. */
  5015. I915_WRITE(dpll_reg,
  5016. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5017. mutex_lock(&dev_priv->dpio_lock);
  5018. /* p1 and p2 divider */
  5019. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5020. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5021. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5022. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5023. 1 << DPIO_CHV_K_DIV_SHIFT);
  5024. /* Feedback post-divider - m2 */
  5025. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5026. /* Feedback refclk divider - n and m1 */
  5027. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5028. DPIO_CHV_M1_DIV_BY_2 |
  5029. 1 << DPIO_CHV_N_DIV_SHIFT);
  5030. /* M2 fraction division */
  5031. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5032. /* M2 fraction division enable */
  5033. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
  5034. DPIO_CHV_FRAC_DIV_EN |
  5035. (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
  5036. /* Loop filter */
  5037. refclk = i9xx_get_refclk(crtc, 0);
  5038. loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
  5039. 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
  5040. if (refclk == 100000)
  5041. intcoeff = 11;
  5042. else if (refclk == 38400)
  5043. intcoeff = 10;
  5044. else
  5045. intcoeff = 9;
  5046. loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
  5047. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5048. /* AFC Recal */
  5049. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5050. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5051. DPIO_AFC_RECAL);
  5052. mutex_unlock(&dev_priv->dpio_lock);
  5053. }
  5054. /**
  5055. * vlv_force_pll_on - forcibly enable just the PLL
  5056. * @dev_priv: i915 private structure
  5057. * @pipe: pipe PLL to enable
  5058. * @dpll: PLL configuration
  5059. *
  5060. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5061. * in cases where we need the PLL enabled even when @pipe is not going to
  5062. * be enabled.
  5063. */
  5064. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  5065. const struct dpll *dpll)
  5066. {
  5067. struct intel_crtc *crtc =
  5068. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  5069. struct intel_crtc_config pipe_config = {
  5070. .pixel_multiplier = 1,
  5071. .dpll = *dpll,
  5072. };
  5073. if (IS_CHERRYVIEW(dev)) {
  5074. chv_update_pll(crtc, &pipe_config);
  5075. chv_prepare_pll(crtc, &pipe_config);
  5076. chv_enable_pll(crtc, &pipe_config);
  5077. } else {
  5078. vlv_update_pll(crtc, &pipe_config);
  5079. vlv_prepare_pll(crtc, &pipe_config);
  5080. vlv_enable_pll(crtc, &pipe_config);
  5081. }
  5082. }
  5083. /**
  5084. * vlv_force_pll_off - forcibly disable just the PLL
  5085. * @dev_priv: i915 private structure
  5086. * @pipe: pipe PLL to disable
  5087. *
  5088. * Disable the PLL for @pipe. To be used in cases where we need
  5089. * the PLL enabled even when @pipe is not going to be enabled.
  5090. */
  5091. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  5092. {
  5093. if (IS_CHERRYVIEW(dev))
  5094. chv_disable_pll(to_i915(dev), pipe);
  5095. else
  5096. vlv_disable_pll(to_i915(dev), pipe);
  5097. }
  5098. static void i9xx_update_pll(struct intel_crtc *crtc,
  5099. intel_clock_t *reduced_clock,
  5100. int num_connectors)
  5101. {
  5102. struct drm_device *dev = crtc->base.dev;
  5103. struct drm_i915_private *dev_priv = dev->dev_private;
  5104. u32 dpll;
  5105. bool is_sdvo;
  5106. struct dpll *clock = &crtc->new_config->dpll;
  5107. i9xx_update_pll_dividers(crtc, reduced_clock);
  5108. is_sdvo = intel_pipe_will_have_type(crtc, INTEL_OUTPUT_SDVO) ||
  5109. intel_pipe_will_have_type(crtc, INTEL_OUTPUT_HDMI);
  5110. dpll = DPLL_VGA_MODE_DIS;
  5111. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  5112. dpll |= DPLLB_MODE_LVDS;
  5113. else
  5114. dpll |= DPLLB_MODE_DAC_SERIAL;
  5115. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5116. dpll |= (crtc->new_config->pixel_multiplier - 1)
  5117. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5118. }
  5119. if (is_sdvo)
  5120. dpll |= DPLL_SDVO_HIGH_SPEED;
  5121. if (crtc->new_config->has_dp_encoder)
  5122. dpll |= DPLL_SDVO_HIGH_SPEED;
  5123. /* compute bitmask from p1 value */
  5124. if (IS_PINEVIEW(dev))
  5125. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5126. else {
  5127. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5128. if (IS_G4X(dev) && reduced_clock)
  5129. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5130. }
  5131. switch (clock->p2) {
  5132. case 5:
  5133. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5134. break;
  5135. case 7:
  5136. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5137. break;
  5138. case 10:
  5139. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5140. break;
  5141. case 14:
  5142. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5143. break;
  5144. }
  5145. if (INTEL_INFO(dev)->gen >= 4)
  5146. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5147. if (crtc->new_config->sdvo_tv_clock)
  5148. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5149. else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  5150. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5151. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5152. else
  5153. dpll |= PLL_REF_INPUT_DREFCLK;
  5154. dpll |= DPLL_VCO_ENABLE;
  5155. crtc->new_config->dpll_hw_state.dpll = dpll;
  5156. if (INTEL_INFO(dev)->gen >= 4) {
  5157. u32 dpll_md = (crtc->new_config->pixel_multiplier - 1)
  5158. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5159. crtc->new_config->dpll_hw_state.dpll_md = dpll_md;
  5160. }
  5161. }
  5162. static void i8xx_update_pll(struct intel_crtc *crtc,
  5163. intel_clock_t *reduced_clock,
  5164. int num_connectors)
  5165. {
  5166. struct drm_device *dev = crtc->base.dev;
  5167. struct drm_i915_private *dev_priv = dev->dev_private;
  5168. u32 dpll;
  5169. struct dpll *clock = &crtc->new_config->dpll;
  5170. i9xx_update_pll_dividers(crtc, reduced_clock);
  5171. dpll = DPLL_VGA_MODE_DIS;
  5172. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  5173. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5174. } else {
  5175. if (clock->p1 == 2)
  5176. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5177. else
  5178. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5179. if (clock->p2 == 4)
  5180. dpll |= PLL_P2_DIVIDE_BY_4;
  5181. }
  5182. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc, INTEL_OUTPUT_DVO))
  5183. dpll |= DPLL_DVO_2X_MODE;
  5184. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  5185. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5186. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5187. else
  5188. dpll |= PLL_REF_INPUT_DREFCLK;
  5189. dpll |= DPLL_VCO_ENABLE;
  5190. crtc->new_config->dpll_hw_state.dpll = dpll;
  5191. }
  5192. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5193. {
  5194. struct drm_device *dev = intel_crtc->base.dev;
  5195. struct drm_i915_private *dev_priv = dev->dev_private;
  5196. enum pipe pipe = intel_crtc->pipe;
  5197. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  5198. struct drm_display_mode *adjusted_mode =
  5199. &intel_crtc->config.adjusted_mode;
  5200. uint32_t crtc_vtotal, crtc_vblank_end;
  5201. int vsyncshift = 0;
  5202. /* We need to be careful not to changed the adjusted mode, for otherwise
  5203. * the hw state checker will get angry at the mismatch. */
  5204. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5205. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5206. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5207. /* the chip adds 2 halflines automatically */
  5208. crtc_vtotal -= 1;
  5209. crtc_vblank_end -= 1;
  5210. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  5211. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5212. else
  5213. vsyncshift = adjusted_mode->crtc_hsync_start -
  5214. adjusted_mode->crtc_htotal / 2;
  5215. if (vsyncshift < 0)
  5216. vsyncshift += adjusted_mode->crtc_htotal;
  5217. }
  5218. if (INTEL_INFO(dev)->gen > 3)
  5219. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5220. I915_WRITE(HTOTAL(cpu_transcoder),
  5221. (adjusted_mode->crtc_hdisplay - 1) |
  5222. ((adjusted_mode->crtc_htotal - 1) << 16));
  5223. I915_WRITE(HBLANK(cpu_transcoder),
  5224. (adjusted_mode->crtc_hblank_start - 1) |
  5225. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5226. I915_WRITE(HSYNC(cpu_transcoder),
  5227. (adjusted_mode->crtc_hsync_start - 1) |
  5228. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5229. I915_WRITE(VTOTAL(cpu_transcoder),
  5230. (adjusted_mode->crtc_vdisplay - 1) |
  5231. ((crtc_vtotal - 1) << 16));
  5232. I915_WRITE(VBLANK(cpu_transcoder),
  5233. (adjusted_mode->crtc_vblank_start - 1) |
  5234. ((crtc_vblank_end - 1) << 16));
  5235. I915_WRITE(VSYNC(cpu_transcoder),
  5236. (adjusted_mode->crtc_vsync_start - 1) |
  5237. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5238. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5239. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5240. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5241. * bits. */
  5242. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  5243. (pipe == PIPE_B || pipe == PIPE_C))
  5244. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5245. /* pipesrc controls the size that is scaled from, which should
  5246. * always be the user's requested size.
  5247. */
  5248. I915_WRITE(PIPESRC(pipe),
  5249. ((intel_crtc->config.pipe_src_w - 1) << 16) |
  5250. (intel_crtc->config.pipe_src_h - 1));
  5251. }
  5252. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5253. struct intel_crtc_config *pipe_config)
  5254. {
  5255. struct drm_device *dev = crtc->base.dev;
  5256. struct drm_i915_private *dev_priv = dev->dev_private;
  5257. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5258. uint32_t tmp;
  5259. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5260. pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5261. pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5262. tmp = I915_READ(HBLANK(cpu_transcoder));
  5263. pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5264. pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5265. tmp = I915_READ(HSYNC(cpu_transcoder));
  5266. pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5267. pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5268. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5269. pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5270. pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  5271. tmp = I915_READ(VBLANK(cpu_transcoder));
  5272. pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  5273. pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  5274. tmp = I915_READ(VSYNC(cpu_transcoder));
  5275. pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  5276. pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  5277. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  5278. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  5279. pipe_config->adjusted_mode.crtc_vtotal += 1;
  5280. pipe_config->adjusted_mode.crtc_vblank_end += 1;
  5281. }
  5282. tmp = I915_READ(PIPESRC(crtc->pipe));
  5283. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  5284. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  5285. pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
  5286. pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
  5287. }
  5288. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  5289. struct intel_crtc_config *pipe_config)
  5290. {
  5291. mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
  5292. mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
  5293. mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
  5294. mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
  5295. mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
  5296. mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
  5297. mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
  5298. mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
  5299. mode->flags = pipe_config->adjusted_mode.flags;
  5300. mode->clock = pipe_config->adjusted_mode.crtc_clock;
  5301. mode->flags |= pipe_config->adjusted_mode.flags;
  5302. }
  5303. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  5304. {
  5305. struct drm_device *dev = intel_crtc->base.dev;
  5306. struct drm_i915_private *dev_priv = dev->dev_private;
  5307. uint32_t pipeconf;
  5308. pipeconf = 0;
  5309. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  5310. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  5311. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  5312. if (intel_crtc->config.double_wide)
  5313. pipeconf |= PIPECONF_DOUBLE_WIDE;
  5314. /* only g4x and later have fancy bpc/dither controls */
  5315. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5316. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  5317. if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
  5318. pipeconf |= PIPECONF_DITHER_EN |
  5319. PIPECONF_DITHER_TYPE_SP;
  5320. switch (intel_crtc->config.pipe_bpp) {
  5321. case 18:
  5322. pipeconf |= PIPECONF_6BPC;
  5323. break;
  5324. case 24:
  5325. pipeconf |= PIPECONF_8BPC;
  5326. break;
  5327. case 30:
  5328. pipeconf |= PIPECONF_10BPC;
  5329. break;
  5330. default:
  5331. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5332. BUG();
  5333. }
  5334. }
  5335. if (HAS_PIPE_CXSR(dev)) {
  5336. if (intel_crtc->lowfreq_avail) {
  5337. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  5338. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  5339. } else {
  5340. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  5341. }
  5342. }
  5343. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  5344. if (INTEL_INFO(dev)->gen < 4 ||
  5345. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  5346. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  5347. else
  5348. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  5349. } else
  5350. pipeconf |= PIPECONF_PROGRESSIVE;
  5351. if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
  5352. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  5353. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  5354. POSTING_READ(PIPECONF(intel_crtc->pipe));
  5355. }
  5356. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc)
  5357. {
  5358. struct drm_device *dev = crtc->base.dev;
  5359. struct drm_i915_private *dev_priv = dev->dev_private;
  5360. int refclk, num_connectors = 0;
  5361. intel_clock_t clock, reduced_clock;
  5362. bool ok, has_reduced_clock = false;
  5363. bool is_lvds = false, is_dsi = false;
  5364. struct intel_encoder *encoder;
  5365. const intel_limit_t *limit;
  5366. for_each_intel_encoder(dev, encoder) {
  5367. if (encoder->new_crtc != crtc)
  5368. continue;
  5369. switch (encoder->type) {
  5370. case INTEL_OUTPUT_LVDS:
  5371. is_lvds = true;
  5372. break;
  5373. case INTEL_OUTPUT_DSI:
  5374. is_dsi = true;
  5375. break;
  5376. default:
  5377. break;
  5378. }
  5379. num_connectors++;
  5380. }
  5381. if (is_dsi)
  5382. return 0;
  5383. if (!crtc->new_config->clock_set) {
  5384. refclk = i9xx_get_refclk(crtc, num_connectors);
  5385. /*
  5386. * Returns a set of divisors for the desired target clock with
  5387. * the given refclk, or FALSE. The returned values represent
  5388. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  5389. * 2) / p1 / p2.
  5390. */
  5391. limit = intel_limit(crtc, refclk);
  5392. ok = dev_priv->display.find_dpll(limit, crtc,
  5393. crtc->new_config->port_clock,
  5394. refclk, NULL, &clock);
  5395. if (!ok) {
  5396. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5397. return -EINVAL;
  5398. }
  5399. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5400. /*
  5401. * Ensure we match the reduced clock's P to the target
  5402. * clock. If the clocks don't match, we can't switch
  5403. * the display clock by using the FP0/FP1. In such case
  5404. * we will disable the LVDS downclock feature.
  5405. */
  5406. has_reduced_clock =
  5407. dev_priv->display.find_dpll(limit, crtc,
  5408. dev_priv->lvds_downclock,
  5409. refclk, &clock,
  5410. &reduced_clock);
  5411. }
  5412. /* Compat-code for transition, will disappear. */
  5413. crtc->new_config->dpll.n = clock.n;
  5414. crtc->new_config->dpll.m1 = clock.m1;
  5415. crtc->new_config->dpll.m2 = clock.m2;
  5416. crtc->new_config->dpll.p1 = clock.p1;
  5417. crtc->new_config->dpll.p2 = clock.p2;
  5418. }
  5419. if (IS_GEN2(dev)) {
  5420. i8xx_update_pll(crtc,
  5421. has_reduced_clock ? &reduced_clock : NULL,
  5422. num_connectors);
  5423. } else if (IS_CHERRYVIEW(dev)) {
  5424. chv_update_pll(crtc, crtc->new_config);
  5425. } else if (IS_VALLEYVIEW(dev)) {
  5426. vlv_update_pll(crtc, crtc->new_config);
  5427. } else {
  5428. i9xx_update_pll(crtc,
  5429. has_reduced_clock ? &reduced_clock : NULL,
  5430. num_connectors);
  5431. }
  5432. return 0;
  5433. }
  5434. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  5435. struct intel_crtc_config *pipe_config)
  5436. {
  5437. struct drm_device *dev = crtc->base.dev;
  5438. struct drm_i915_private *dev_priv = dev->dev_private;
  5439. uint32_t tmp;
  5440. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  5441. return;
  5442. tmp = I915_READ(PFIT_CONTROL);
  5443. if (!(tmp & PFIT_ENABLE))
  5444. return;
  5445. /* Check whether the pfit is attached to our pipe. */
  5446. if (INTEL_INFO(dev)->gen < 4) {
  5447. if (crtc->pipe != PIPE_B)
  5448. return;
  5449. } else {
  5450. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  5451. return;
  5452. }
  5453. pipe_config->gmch_pfit.control = tmp;
  5454. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  5455. if (INTEL_INFO(dev)->gen < 5)
  5456. pipe_config->gmch_pfit.lvds_border_bits =
  5457. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  5458. }
  5459. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  5460. struct intel_crtc_config *pipe_config)
  5461. {
  5462. struct drm_device *dev = crtc->base.dev;
  5463. struct drm_i915_private *dev_priv = dev->dev_private;
  5464. int pipe = pipe_config->cpu_transcoder;
  5465. intel_clock_t clock;
  5466. u32 mdiv;
  5467. int refclk = 100000;
  5468. /* In case of MIPI DPLL will not even be used */
  5469. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  5470. return;
  5471. mutex_lock(&dev_priv->dpio_lock);
  5472. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  5473. mutex_unlock(&dev_priv->dpio_lock);
  5474. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  5475. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  5476. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  5477. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  5478. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  5479. vlv_clock(refclk, &clock);
  5480. /* clock.dot is the fast clock */
  5481. pipe_config->port_clock = clock.dot / 5;
  5482. }
  5483. static void i9xx_get_plane_config(struct intel_crtc *crtc,
  5484. struct intel_plane_config *plane_config)
  5485. {
  5486. struct drm_device *dev = crtc->base.dev;
  5487. struct drm_i915_private *dev_priv = dev->dev_private;
  5488. u32 val, base, offset;
  5489. int pipe = crtc->pipe, plane = crtc->plane;
  5490. int fourcc, pixel_format;
  5491. int aligned_height;
  5492. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  5493. if (!crtc->base.primary->fb) {
  5494. DRM_DEBUG_KMS("failed to alloc fb\n");
  5495. return;
  5496. }
  5497. val = I915_READ(DSPCNTR(plane));
  5498. if (INTEL_INFO(dev)->gen >= 4)
  5499. if (val & DISPPLANE_TILED)
  5500. plane_config->tiled = true;
  5501. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  5502. fourcc = intel_format_to_fourcc(pixel_format);
  5503. crtc->base.primary->fb->pixel_format = fourcc;
  5504. crtc->base.primary->fb->bits_per_pixel =
  5505. drm_format_plane_cpp(fourcc, 0) * 8;
  5506. if (INTEL_INFO(dev)->gen >= 4) {
  5507. if (plane_config->tiled)
  5508. offset = I915_READ(DSPTILEOFF(plane));
  5509. else
  5510. offset = I915_READ(DSPLINOFF(plane));
  5511. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  5512. } else {
  5513. base = I915_READ(DSPADDR(plane));
  5514. }
  5515. plane_config->base = base;
  5516. val = I915_READ(PIPESRC(pipe));
  5517. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  5518. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  5519. val = I915_READ(DSPSTRIDE(pipe));
  5520. crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
  5521. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  5522. plane_config->tiled);
  5523. plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
  5524. aligned_height);
  5525. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  5526. pipe, plane, crtc->base.primary->fb->width,
  5527. crtc->base.primary->fb->height,
  5528. crtc->base.primary->fb->bits_per_pixel, base,
  5529. crtc->base.primary->fb->pitches[0],
  5530. plane_config->size);
  5531. }
  5532. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  5533. struct intel_crtc_config *pipe_config)
  5534. {
  5535. struct drm_device *dev = crtc->base.dev;
  5536. struct drm_i915_private *dev_priv = dev->dev_private;
  5537. int pipe = pipe_config->cpu_transcoder;
  5538. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5539. intel_clock_t clock;
  5540. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  5541. int refclk = 100000;
  5542. mutex_lock(&dev_priv->dpio_lock);
  5543. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  5544. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  5545. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  5546. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  5547. mutex_unlock(&dev_priv->dpio_lock);
  5548. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  5549. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  5550. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  5551. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  5552. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  5553. chv_clock(refclk, &clock);
  5554. /* clock.dot is the fast clock */
  5555. pipe_config->port_clock = clock.dot / 5;
  5556. }
  5557. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  5558. struct intel_crtc_config *pipe_config)
  5559. {
  5560. struct drm_device *dev = crtc->base.dev;
  5561. struct drm_i915_private *dev_priv = dev->dev_private;
  5562. uint32_t tmp;
  5563. if (!intel_display_power_is_enabled(dev_priv,
  5564. POWER_DOMAIN_PIPE(crtc->pipe)))
  5565. return false;
  5566. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5567. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5568. tmp = I915_READ(PIPECONF(crtc->pipe));
  5569. if (!(tmp & PIPECONF_ENABLE))
  5570. return false;
  5571. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5572. switch (tmp & PIPECONF_BPC_MASK) {
  5573. case PIPECONF_6BPC:
  5574. pipe_config->pipe_bpp = 18;
  5575. break;
  5576. case PIPECONF_8BPC:
  5577. pipe_config->pipe_bpp = 24;
  5578. break;
  5579. case PIPECONF_10BPC:
  5580. pipe_config->pipe_bpp = 30;
  5581. break;
  5582. default:
  5583. break;
  5584. }
  5585. }
  5586. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  5587. pipe_config->limited_color_range = true;
  5588. if (INTEL_INFO(dev)->gen < 4)
  5589. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  5590. intel_get_pipe_timings(crtc, pipe_config);
  5591. i9xx_get_pfit_config(crtc, pipe_config);
  5592. if (INTEL_INFO(dev)->gen >= 4) {
  5593. tmp = I915_READ(DPLL_MD(crtc->pipe));
  5594. pipe_config->pixel_multiplier =
  5595. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  5596. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  5597. pipe_config->dpll_hw_state.dpll_md = tmp;
  5598. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5599. tmp = I915_READ(DPLL(crtc->pipe));
  5600. pipe_config->pixel_multiplier =
  5601. ((tmp & SDVO_MULTIPLIER_MASK)
  5602. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  5603. } else {
  5604. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  5605. * port and will be fixed up in the encoder->get_config
  5606. * function. */
  5607. pipe_config->pixel_multiplier = 1;
  5608. }
  5609. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  5610. if (!IS_VALLEYVIEW(dev)) {
  5611. /*
  5612. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  5613. * on 830. Filter it out here so that we don't
  5614. * report errors due to that.
  5615. */
  5616. if (IS_I830(dev))
  5617. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  5618. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  5619. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  5620. } else {
  5621. /* Mask out read-only status bits. */
  5622. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  5623. DPLL_PORTC_READY_MASK |
  5624. DPLL_PORTB_READY_MASK);
  5625. }
  5626. if (IS_CHERRYVIEW(dev))
  5627. chv_crtc_clock_get(crtc, pipe_config);
  5628. else if (IS_VALLEYVIEW(dev))
  5629. vlv_crtc_clock_get(crtc, pipe_config);
  5630. else
  5631. i9xx_crtc_clock_get(crtc, pipe_config);
  5632. return true;
  5633. }
  5634. static void ironlake_init_pch_refclk(struct drm_device *dev)
  5635. {
  5636. struct drm_i915_private *dev_priv = dev->dev_private;
  5637. struct intel_encoder *encoder;
  5638. u32 val, final;
  5639. bool has_lvds = false;
  5640. bool has_cpu_edp = false;
  5641. bool has_panel = false;
  5642. bool has_ck505 = false;
  5643. bool can_ssc = false;
  5644. /* We need to take the global config into account */
  5645. for_each_intel_encoder(dev, encoder) {
  5646. switch (encoder->type) {
  5647. case INTEL_OUTPUT_LVDS:
  5648. has_panel = true;
  5649. has_lvds = true;
  5650. break;
  5651. case INTEL_OUTPUT_EDP:
  5652. has_panel = true;
  5653. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  5654. has_cpu_edp = true;
  5655. break;
  5656. default:
  5657. break;
  5658. }
  5659. }
  5660. if (HAS_PCH_IBX(dev)) {
  5661. has_ck505 = dev_priv->vbt.display_clock_mode;
  5662. can_ssc = has_ck505;
  5663. } else {
  5664. has_ck505 = false;
  5665. can_ssc = true;
  5666. }
  5667. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  5668. has_panel, has_lvds, has_ck505);
  5669. /* Ironlake: try to setup display ref clock before DPLL
  5670. * enabling. This is only under driver's control after
  5671. * PCH B stepping, previous chipset stepping should be
  5672. * ignoring this setting.
  5673. */
  5674. val = I915_READ(PCH_DREF_CONTROL);
  5675. /* As we must carefully and slowly disable/enable each source in turn,
  5676. * compute the final state we want first and check if we need to
  5677. * make any changes at all.
  5678. */
  5679. final = val;
  5680. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  5681. if (has_ck505)
  5682. final |= DREF_NONSPREAD_CK505_ENABLE;
  5683. else
  5684. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  5685. final &= ~DREF_SSC_SOURCE_MASK;
  5686. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5687. final &= ~DREF_SSC1_ENABLE;
  5688. if (has_panel) {
  5689. final |= DREF_SSC_SOURCE_ENABLE;
  5690. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5691. final |= DREF_SSC1_ENABLE;
  5692. if (has_cpu_edp) {
  5693. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5694. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5695. else
  5696. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5697. } else
  5698. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5699. } else {
  5700. final |= DREF_SSC_SOURCE_DISABLE;
  5701. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5702. }
  5703. if (final == val)
  5704. return;
  5705. /* Always enable nonspread source */
  5706. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  5707. if (has_ck505)
  5708. val |= DREF_NONSPREAD_CK505_ENABLE;
  5709. else
  5710. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  5711. if (has_panel) {
  5712. val &= ~DREF_SSC_SOURCE_MASK;
  5713. val |= DREF_SSC_SOURCE_ENABLE;
  5714. /* SSC must be turned on before enabling the CPU output */
  5715. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5716. DRM_DEBUG_KMS("Using SSC on panel\n");
  5717. val |= DREF_SSC1_ENABLE;
  5718. } else
  5719. val &= ~DREF_SSC1_ENABLE;
  5720. /* Get SSC going before enabling the outputs */
  5721. I915_WRITE(PCH_DREF_CONTROL, val);
  5722. POSTING_READ(PCH_DREF_CONTROL);
  5723. udelay(200);
  5724. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5725. /* Enable CPU source on CPU attached eDP */
  5726. if (has_cpu_edp) {
  5727. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5728. DRM_DEBUG_KMS("Using SSC on eDP\n");
  5729. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5730. } else
  5731. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5732. } else
  5733. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5734. I915_WRITE(PCH_DREF_CONTROL, val);
  5735. POSTING_READ(PCH_DREF_CONTROL);
  5736. udelay(200);
  5737. } else {
  5738. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  5739. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5740. /* Turn off CPU output */
  5741. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5742. I915_WRITE(PCH_DREF_CONTROL, val);
  5743. POSTING_READ(PCH_DREF_CONTROL);
  5744. udelay(200);
  5745. /* Turn off the SSC source */
  5746. val &= ~DREF_SSC_SOURCE_MASK;
  5747. val |= DREF_SSC_SOURCE_DISABLE;
  5748. /* Turn off SSC1 */
  5749. val &= ~DREF_SSC1_ENABLE;
  5750. I915_WRITE(PCH_DREF_CONTROL, val);
  5751. POSTING_READ(PCH_DREF_CONTROL);
  5752. udelay(200);
  5753. }
  5754. BUG_ON(val != final);
  5755. }
  5756. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  5757. {
  5758. uint32_t tmp;
  5759. tmp = I915_READ(SOUTH_CHICKEN2);
  5760. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  5761. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5762. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  5763. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  5764. DRM_ERROR("FDI mPHY reset assert timeout\n");
  5765. tmp = I915_READ(SOUTH_CHICKEN2);
  5766. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  5767. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5768. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  5769. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  5770. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  5771. }
  5772. /* WaMPhyProgramming:hsw */
  5773. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  5774. {
  5775. uint32_t tmp;
  5776. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  5777. tmp &= ~(0xFF << 24);
  5778. tmp |= (0x12 << 24);
  5779. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  5780. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  5781. tmp |= (1 << 11);
  5782. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  5783. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  5784. tmp |= (1 << 11);
  5785. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  5786. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  5787. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5788. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  5789. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  5790. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5791. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  5792. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  5793. tmp &= ~(7 << 13);
  5794. tmp |= (5 << 13);
  5795. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  5796. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  5797. tmp &= ~(7 << 13);
  5798. tmp |= (5 << 13);
  5799. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  5800. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  5801. tmp &= ~0xFF;
  5802. tmp |= 0x1C;
  5803. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  5804. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  5805. tmp &= ~0xFF;
  5806. tmp |= 0x1C;
  5807. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  5808. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  5809. tmp &= ~(0xFF << 16);
  5810. tmp |= (0x1C << 16);
  5811. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  5812. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  5813. tmp &= ~(0xFF << 16);
  5814. tmp |= (0x1C << 16);
  5815. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  5816. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  5817. tmp |= (1 << 27);
  5818. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  5819. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  5820. tmp |= (1 << 27);
  5821. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  5822. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  5823. tmp &= ~(0xF << 28);
  5824. tmp |= (4 << 28);
  5825. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  5826. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  5827. tmp &= ~(0xF << 28);
  5828. tmp |= (4 << 28);
  5829. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  5830. }
  5831. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  5832. * Programming" based on the parameters passed:
  5833. * - Sequence to enable CLKOUT_DP
  5834. * - Sequence to enable CLKOUT_DP without spread
  5835. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  5836. */
  5837. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  5838. bool with_fdi)
  5839. {
  5840. struct drm_i915_private *dev_priv = dev->dev_private;
  5841. uint32_t reg, tmp;
  5842. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  5843. with_spread = true;
  5844. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  5845. with_fdi, "LP PCH doesn't have FDI\n"))
  5846. with_fdi = false;
  5847. mutex_lock(&dev_priv->dpio_lock);
  5848. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5849. tmp &= ~SBI_SSCCTL_DISABLE;
  5850. tmp |= SBI_SSCCTL_PATHALT;
  5851. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5852. udelay(24);
  5853. if (with_spread) {
  5854. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5855. tmp &= ~SBI_SSCCTL_PATHALT;
  5856. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5857. if (with_fdi) {
  5858. lpt_reset_fdi_mphy(dev_priv);
  5859. lpt_program_fdi_mphy(dev_priv);
  5860. }
  5861. }
  5862. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5863. SBI_GEN0 : SBI_DBUFF0;
  5864. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5865. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5866. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5867. mutex_unlock(&dev_priv->dpio_lock);
  5868. }
  5869. /* Sequence to disable CLKOUT_DP */
  5870. static void lpt_disable_clkout_dp(struct drm_device *dev)
  5871. {
  5872. struct drm_i915_private *dev_priv = dev->dev_private;
  5873. uint32_t reg, tmp;
  5874. mutex_lock(&dev_priv->dpio_lock);
  5875. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5876. SBI_GEN0 : SBI_DBUFF0;
  5877. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5878. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5879. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5880. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5881. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  5882. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  5883. tmp |= SBI_SSCCTL_PATHALT;
  5884. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5885. udelay(32);
  5886. }
  5887. tmp |= SBI_SSCCTL_DISABLE;
  5888. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5889. }
  5890. mutex_unlock(&dev_priv->dpio_lock);
  5891. }
  5892. static void lpt_init_pch_refclk(struct drm_device *dev)
  5893. {
  5894. struct intel_encoder *encoder;
  5895. bool has_vga = false;
  5896. for_each_intel_encoder(dev, encoder) {
  5897. switch (encoder->type) {
  5898. case INTEL_OUTPUT_ANALOG:
  5899. has_vga = true;
  5900. break;
  5901. default:
  5902. break;
  5903. }
  5904. }
  5905. if (has_vga)
  5906. lpt_enable_clkout_dp(dev, true, true);
  5907. else
  5908. lpt_disable_clkout_dp(dev);
  5909. }
  5910. /*
  5911. * Initialize reference clocks when the driver loads
  5912. */
  5913. void intel_init_pch_refclk(struct drm_device *dev)
  5914. {
  5915. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  5916. ironlake_init_pch_refclk(dev);
  5917. else if (HAS_PCH_LPT(dev))
  5918. lpt_init_pch_refclk(dev);
  5919. }
  5920. static int ironlake_get_refclk(struct drm_crtc *crtc)
  5921. {
  5922. struct drm_device *dev = crtc->dev;
  5923. struct drm_i915_private *dev_priv = dev->dev_private;
  5924. struct intel_encoder *encoder;
  5925. int num_connectors = 0;
  5926. bool is_lvds = false;
  5927. for_each_intel_encoder(dev, encoder) {
  5928. if (encoder->new_crtc != to_intel_crtc(crtc))
  5929. continue;
  5930. switch (encoder->type) {
  5931. case INTEL_OUTPUT_LVDS:
  5932. is_lvds = true;
  5933. break;
  5934. default:
  5935. break;
  5936. }
  5937. num_connectors++;
  5938. }
  5939. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5940. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  5941. dev_priv->vbt.lvds_ssc_freq);
  5942. return dev_priv->vbt.lvds_ssc_freq;
  5943. }
  5944. return 120000;
  5945. }
  5946. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  5947. {
  5948. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  5949. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5950. int pipe = intel_crtc->pipe;
  5951. uint32_t val;
  5952. val = 0;
  5953. switch (intel_crtc->config.pipe_bpp) {
  5954. case 18:
  5955. val |= PIPECONF_6BPC;
  5956. break;
  5957. case 24:
  5958. val |= PIPECONF_8BPC;
  5959. break;
  5960. case 30:
  5961. val |= PIPECONF_10BPC;
  5962. break;
  5963. case 36:
  5964. val |= PIPECONF_12BPC;
  5965. break;
  5966. default:
  5967. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5968. BUG();
  5969. }
  5970. if (intel_crtc->config.dither)
  5971. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  5972. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  5973. val |= PIPECONF_INTERLACED_ILK;
  5974. else
  5975. val |= PIPECONF_PROGRESSIVE;
  5976. if (intel_crtc->config.limited_color_range)
  5977. val |= PIPECONF_COLOR_RANGE_SELECT;
  5978. I915_WRITE(PIPECONF(pipe), val);
  5979. POSTING_READ(PIPECONF(pipe));
  5980. }
  5981. /*
  5982. * Set up the pipe CSC unit.
  5983. *
  5984. * Currently only full range RGB to limited range RGB conversion
  5985. * is supported, but eventually this should handle various
  5986. * RGB<->YCbCr scenarios as well.
  5987. */
  5988. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  5989. {
  5990. struct drm_device *dev = crtc->dev;
  5991. struct drm_i915_private *dev_priv = dev->dev_private;
  5992. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5993. int pipe = intel_crtc->pipe;
  5994. uint16_t coeff = 0x7800; /* 1.0 */
  5995. /*
  5996. * TODO: Check what kind of values actually come out of the pipe
  5997. * with these coeff/postoff values and adjust to get the best
  5998. * accuracy. Perhaps we even need to take the bpc value into
  5999. * consideration.
  6000. */
  6001. if (intel_crtc->config.limited_color_range)
  6002. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  6003. /*
  6004. * GY/GU and RY/RU should be the other way around according
  6005. * to BSpec, but reality doesn't agree. Just set them up in
  6006. * a way that results in the correct picture.
  6007. */
  6008. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  6009. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  6010. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  6011. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  6012. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  6013. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  6014. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  6015. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  6016. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  6017. if (INTEL_INFO(dev)->gen > 6) {
  6018. uint16_t postoff = 0;
  6019. if (intel_crtc->config.limited_color_range)
  6020. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  6021. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  6022. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  6023. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  6024. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  6025. } else {
  6026. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  6027. if (intel_crtc->config.limited_color_range)
  6028. mode |= CSC_BLACK_SCREEN_OFFSET;
  6029. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  6030. }
  6031. }
  6032. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6033. {
  6034. struct drm_device *dev = crtc->dev;
  6035. struct drm_i915_private *dev_priv = dev->dev_private;
  6036. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6037. enum pipe pipe = intel_crtc->pipe;
  6038. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  6039. uint32_t val;
  6040. val = 0;
  6041. if (IS_HASWELL(dev) && intel_crtc->config.dither)
  6042. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6043. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6044. val |= PIPECONF_INTERLACED_ILK;
  6045. else
  6046. val |= PIPECONF_PROGRESSIVE;
  6047. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6048. POSTING_READ(PIPECONF(cpu_transcoder));
  6049. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  6050. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  6051. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  6052. val = 0;
  6053. switch (intel_crtc->config.pipe_bpp) {
  6054. case 18:
  6055. val |= PIPEMISC_DITHER_6_BPC;
  6056. break;
  6057. case 24:
  6058. val |= PIPEMISC_DITHER_8_BPC;
  6059. break;
  6060. case 30:
  6061. val |= PIPEMISC_DITHER_10_BPC;
  6062. break;
  6063. case 36:
  6064. val |= PIPEMISC_DITHER_12_BPC;
  6065. break;
  6066. default:
  6067. /* Case prevented by pipe_config_set_bpp. */
  6068. BUG();
  6069. }
  6070. if (intel_crtc->config.dither)
  6071. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  6072. I915_WRITE(PIPEMISC(pipe), val);
  6073. }
  6074. }
  6075. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  6076. intel_clock_t *clock,
  6077. bool *has_reduced_clock,
  6078. intel_clock_t *reduced_clock)
  6079. {
  6080. struct drm_device *dev = crtc->dev;
  6081. struct drm_i915_private *dev_priv = dev->dev_private;
  6082. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6083. int refclk;
  6084. const intel_limit_t *limit;
  6085. bool ret, is_lvds = false;
  6086. is_lvds = intel_pipe_will_have_type(intel_crtc, INTEL_OUTPUT_LVDS);
  6087. refclk = ironlake_get_refclk(crtc);
  6088. /*
  6089. * Returns a set of divisors for the desired target clock with the given
  6090. * refclk, or FALSE. The returned values represent the clock equation:
  6091. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  6092. */
  6093. limit = intel_limit(intel_crtc, refclk);
  6094. ret = dev_priv->display.find_dpll(limit, intel_crtc,
  6095. intel_crtc->new_config->port_clock,
  6096. refclk, NULL, clock);
  6097. if (!ret)
  6098. return false;
  6099. if (is_lvds && dev_priv->lvds_downclock_avail) {
  6100. /*
  6101. * Ensure we match the reduced clock's P to the target clock.
  6102. * If the clocks don't match, we can't switch the display clock
  6103. * by using the FP0/FP1. In such case we will disable the LVDS
  6104. * downclock feature.
  6105. */
  6106. *has_reduced_clock =
  6107. dev_priv->display.find_dpll(limit, intel_crtc,
  6108. dev_priv->lvds_downclock,
  6109. refclk, clock,
  6110. reduced_clock);
  6111. }
  6112. return true;
  6113. }
  6114. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  6115. {
  6116. /*
  6117. * Account for spread spectrum to avoid
  6118. * oversubscribing the link. Max center spread
  6119. * is 2.5%; use 5% for safety's sake.
  6120. */
  6121. u32 bps = target_clock * bpp * 21 / 20;
  6122. return DIV_ROUND_UP(bps, link_bw * 8);
  6123. }
  6124. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  6125. {
  6126. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  6127. }
  6128. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  6129. u32 *fp,
  6130. intel_clock_t *reduced_clock, u32 *fp2)
  6131. {
  6132. struct drm_crtc *crtc = &intel_crtc->base;
  6133. struct drm_device *dev = crtc->dev;
  6134. struct drm_i915_private *dev_priv = dev->dev_private;
  6135. struct intel_encoder *intel_encoder;
  6136. uint32_t dpll;
  6137. int factor, num_connectors = 0;
  6138. bool is_lvds = false, is_sdvo = false;
  6139. for_each_intel_encoder(dev, intel_encoder) {
  6140. if (intel_encoder->new_crtc != to_intel_crtc(crtc))
  6141. continue;
  6142. switch (intel_encoder->type) {
  6143. case INTEL_OUTPUT_LVDS:
  6144. is_lvds = true;
  6145. break;
  6146. case INTEL_OUTPUT_SDVO:
  6147. case INTEL_OUTPUT_HDMI:
  6148. is_sdvo = true;
  6149. break;
  6150. default:
  6151. break;
  6152. }
  6153. num_connectors++;
  6154. }
  6155. /* Enable autotuning of the PLL clock (if permissible) */
  6156. factor = 21;
  6157. if (is_lvds) {
  6158. if ((intel_panel_use_ssc(dev_priv) &&
  6159. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6160. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  6161. factor = 25;
  6162. } else if (intel_crtc->new_config->sdvo_tv_clock)
  6163. factor = 20;
  6164. if (ironlake_needs_fb_cb_tune(&intel_crtc->new_config->dpll, factor))
  6165. *fp |= FP_CB_TUNE;
  6166. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  6167. *fp2 |= FP_CB_TUNE;
  6168. dpll = 0;
  6169. if (is_lvds)
  6170. dpll |= DPLLB_MODE_LVDS;
  6171. else
  6172. dpll |= DPLLB_MODE_DAC_SERIAL;
  6173. dpll |= (intel_crtc->new_config->pixel_multiplier - 1)
  6174. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6175. if (is_sdvo)
  6176. dpll |= DPLL_SDVO_HIGH_SPEED;
  6177. if (intel_crtc->new_config->has_dp_encoder)
  6178. dpll |= DPLL_SDVO_HIGH_SPEED;
  6179. /* compute bitmask from p1 value */
  6180. dpll |= (1 << (intel_crtc->new_config->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6181. /* also FPA1 */
  6182. dpll |= (1 << (intel_crtc->new_config->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6183. switch (intel_crtc->new_config->dpll.p2) {
  6184. case 5:
  6185. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6186. break;
  6187. case 7:
  6188. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6189. break;
  6190. case 10:
  6191. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6192. break;
  6193. case 14:
  6194. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6195. break;
  6196. }
  6197. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6198. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6199. else
  6200. dpll |= PLL_REF_INPUT_DREFCLK;
  6201. return dpll | DPLL_VCO_ENABLE;
  6202. }
  6203. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc)
  6204. {
  6205. struct drm_device *dev = crtc->base.dev;
  6206. intel_clock_t clock, reduced_clock;
  6207. u32 dpll = 0, fp = 0, fp2 = 0;
  6208. bool ok, has_reduced_clock = false;
  6209. bool is_lvds = false;
  6210. struct intel_shared_dpll *pll;
  6211. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  6212. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  6213. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  6214. ok = ironlake_compute_clocks(&crtc->base, &clock,
  6215. &has_reduced_clock, &reduced_clock);
  6216. if (!ok && !crtc->new_config->clock_set) {
  6217. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6218. return -EINVAL;
  6219. }
  6220. /* Compat-code for transition, will disappear. */
  6221. if (!crtc->new_config->clock_set) {
  6222. crtc->new_config->dpll.n = clock.n;
  6223. crtc->new_config->dpll.m1 = clock.m1;
  6224. crtc->new_config->dpll.m2 = clock.m2;
  6225. crtc->new_config->dpll.p1 = clock.p1;
  6226. crtc->new_config->dpll.p2 = clock.p2;
  6227. }
  6228. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  6229. if (crtc->new_config->has_pch_encoder) {
  6230. fp = i9xx_dpll_compute_fp(&crtc->new_config->dpll);
  6231. if (has_reduced_clock)
  6232. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  6233. dpll = ironlake_compute_dpll(crtc,
  6234. &fp, &reduced_clock,
  6235. has_reduced_clock ? &fp2 : NULL);
  6236. crtc->new_config->dpll_hw_state.dpll = dpll;
  6237. crtc->new_config->dpll_hw_state.fp0 = fp;
  6238. if (has_reduced_clock)
  6239. crtc->new_config->dpll_hw_state.fp1 = fp2;
  6240. else
  6241. crtc->new_config->dpll_hw_state.fp1 = fp;
  6242. pll = intel_get_shared_dpll(crtc);
  6243. if (pll == NULL) {
  6244. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  6245. pipe_name(crtc->pipe));
  6246. return -EINVAL;
  6247. }
  6248. }
  6249. if (is_lvds && has_reduced_clock && i915.powersave)
  6250. crtc->lowfreq_avail = true;
  6251. else
  6252. crtc->lowfreq_avail = false;
  6253. return 0;
  6254. }
  6255. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  6256. struct intel_link_m_n *m_n)
  6257. {
  6258. struct drm_device *dev = crtc->base.dev;
  6259. struct drm_i915_private *dev_priv = dev->dev_private;
  6260. enum pipe pipe = crtc->pipe;
  6261. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  6262. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  6263. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  6264. & ~TU_SIZE_MASK;
  6265. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  6266. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  6267. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6268. }
  6269. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  6270. enum transcoder transcoder,
  6271. struct intel_link_m_n *m_n,
  6272. struct intel_link_m_n *m2_n2)
  6273. {
  6274. struct drm_device *dev = crtc->base.dev;
  6275. struct drm_i915_private *dev_priv = dev->dev_private;
  6276. enum pipe pipe = crtc->pipe;
  6277. if (INTEL_INFO(dev)->gen >= 5) {
  6278. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  6279. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  6280. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  6281. & ~TU_SIZE_MASK;
  6282. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  6283. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  6284. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6285. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  6286. * gen < 8) and if DRRS is supported (to make sure the
  6287. * registers are not unnecessarily read).
  6288. */
  6289. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  6290. crtc->config.has_drrs) {
  6291. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  6292. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  6293. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  6294. & ~TU_SIZE_MASK;
  6295. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  6296. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  6297. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6298. }
  6299. } else {
  6300. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  6301. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  6302. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  6303. & ~TU_SIZE_MASK;
  6304. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  6305. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  6306. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6307. }
  6308. }
  6309. void intel_dp_get_m_n(struct intel_crtc *crtc,
  6310. struct intel_crtc_config *pipe_config)
  6311. {
  6312. if (crtc->config.has_pch_encoder)
  6313. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  6314. else
  6315. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6316. &pipe_config->dp_m_n,
  6317. &pipe_config->dp_m2_n2);
  6318. }
  6319. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  6320. struct intel_crtc_config *pipe_config)
  6321. {
  6322. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6323. &pipe_config->fdi_m_n, NULL);
  6324. }
  6325. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  6326. struct intel_crtc_config *pipe_config)
  6327. {
  6328. struct drm_device *dev = crtc->base.dev;
  6329. struct drm_i915_private *dev_priv = dev->dev_private;
  6330. uint32_t tmp;
  6331. tmp = I915_READ(PS_CTL(crtc->pipe));
  6332. if (tmp & PS_ENABLE) {
  6333. pipe_config->pch_pfit.enabled = true;
  6334. pipe_config->pch_pfit.pos = I915_READ(PS_WIN_POS(crtc->pipe));
  6335. pipe_config->pch_pfit.size = I915_READ(PS_WIN_SZ(crtc->pipe));
  6336. }
  6337. }
  6338. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  6339. struct intel_crtc_config *pipe_config)
  6340. {
  6341. struct drm_device *dev = crtc->base.dev;
  6342. struct drm_i915_private *dev_priv = dev->dev_private;
  6343. uint32_t tmp;
  6344. tmp = I915_READ(PF_CTL(crtc->pipe));
  6345. if (tmp & PF_ENABLE) {
  6346. pipe_config->pch_pfit.enabled = true;
  6347. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  6348. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  6349. /* We currently do not free assignements of panel fitters on
  6350. * ivb/hsw (since we don't use the higher upscaling modes which
  6351. * differentiates them) so just WARN about this case for now. */
  6352. if (IS_GEN7(dev)) {
  6353. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  6354. PF_PIPE_SEL_IVB(crtc->pipe));
  6355. }
  6356. }
  6357. }
  6358. static void ironlake_get_plane_config(struct intel_crtc *crtc,
  6359. struct intel_plane_config *plane_config)
  6360. {
  6361. struct drm_device *dev = crtc->base.dev;
  6362. struct drm_i915_private *dev_priv = dev->dev_private;
  6363. u32 val, base, offset;
  6364. int pipe = crtc->pipe, plane = crtc->plane;
  6365. int fourcc, pixel_format;
  6366. int aligned_height;
  6367. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  6368. if (!crtc->base.primary->fb) {
  6369. DRM_DEBUG_KMS("failed to alloc fb\n");
  6370. return;
  6371. }
  6372. val = I915_READ(DSPCNTR(plane));
  6373. if (INTEL_INFO(dev)->gen >= 4)
  6374. if (val & DISPPLANE_TILED)
  6375. plane_config->tiled = true;
  6376. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6377. fourcc = intel_format_to_fourcc(pixel_format);
  6378. crtc->base.primary->fb->pixel_format = fourcc;
  6379. crtc->base.primary->fb->bits_per_pixel =
  6380. drm_format_plane_cpp(fourcc, 0) * 8;
  6381. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6382. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  6383. offset = I915_READ(DSPOFFSET(plane));
  6384. } else {
  6385. if (plane_config->tiled)
  6386. offset = I915_READ(DSPTILEOFF(plane));
  6387. else
  6388. offset = I915_READ(DSPLINOFF(plane));
  6389. }
  6390. plane_config->base = base;
  6391. val = I915_READ(PIPESRC(pipe));
  6392. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  6393. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  6394. val = I915_READ(DSPSTRIDE(pipe));
  6395. crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
  6396. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  6397. plane_config->tiled);
  6398. plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
  6399. aligned_height);
  6400. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6401. pipe, plane, crtc->base.primary->fb->width,
  6402. crtc->base.primary->fb->height,
  6403. crtc->base.primary->fb->bits_per_pixel, base,
  6404. crtc->base.primary->fb->pitches[0],
  6405. plane_config->size);
  6406. }
  6407. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  6408. struct intel_crtc_config *pipe_config)
  6409. {
  6410. struct drm_device *dev = crtc->base.dev;
  6411. struct drm_i915_private *dev_priv = dev->dev_private;
  6412. uint32_t tmp;
  6413. if (!intel_display_power_is_enabled(dev_priv,
  6414. POWER_DOMAIN_PIPE(crtc->pipe)))
  6415. return false;
  6416. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6417. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6418. tmp = I915_READ(PIPECONF(crtc->pipe));
  6419. if (!(tmp & PIPECONF_ENABLE))
  6420. return false;
  6421. switch (tmp & PIPECONF_BPC_MASK) {
  6422. case PIPECONF_6BPC:
  6423. pipe_config->pipe_bpp = 18;
  6424. break;
  6425. case PIPECONF_8BPC:
  6426. pipe_config->pipe_bpp = 24;
  6427. break;
  6428. case PIPECONF_10BPC:
  6429. pipe_config->pipe_bpp = 30;
  6430. break;
  6431. case PIPECONF_12BPC:
  6432. pipe_config->pipe_bpp = 36;
  6433. break;
  6434. default:
  6435. break;
  6436. }
  6437. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  6438. pipe_config->limited_color_range = true;
  6439. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  6440. struct intel_shared_dpll *pll;
  6441. pipe_config->has_pch_encoder = true;
  6442. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  6443. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6444. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6445. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6446. if (HAS_PCH_IBX(dev_priv->dev)) {
  6447. pipe_config->shared_dpll =
  6448. (enum intel_dpll_id) crtc->pipe;
  6449. } else {
  6450. tmp = I915_READ(PCH_DPLL_SEL);
  6451. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  6452. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  6453. else
  6454. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  6455. }
  6456. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6457. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6458. &pipe_config->dpll_hw_state));
  6459. tmp = pipe_config->dpll_hw_state.dpll;
  6460. pipe_config->pixel_multiplier =
  6461. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  6462. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  6463. ironlake_pch_clock_get(crtc, pipe_config);
  6464. } else {
  6465. pipe_config->pixel_multiplier = 1;
  6466. }
  6467. intel_get_pipe_timings(crtc, pipe_config);
  6468. ironlake_get_pfit_config(crtc, pipe_config);
  6469. return true;
  6470. }
  6471. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  6472. {
  6473. struct drm_device *dev = dev_priv->dev;
  6474. struct intel_crtc *crtc;
  6475. for_each_intel_crtc(dev, crtc)
  6476. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  6477. pipe_name(crtc->pipe));
  6478. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  6479. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  6480. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  6481. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  6482. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  6483. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  6484. "CPU PWM1 enabled\n");
  6485. if (IS_HASWELL(dev))
  6486. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  6487. "CPU PWM2 enabled\n");
  6488. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  6489. "PCH PWM1 enabled\n");
  6490. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  6491. "Utility pin enabled\n");
  6492. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  6493. /*
  6494. * In theory we can still leave IRQs enabled, as long as only the HPD
  6495. * interrupts remain enabled. We used to check for that, but since it's
  6496. * gen-specific and since we only disable LCPLL after we fully disable
  6497. * the interrupts, the check below should be enough.
  6498. */
  6499. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  6500. }
  6501. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  6502. {
  6503. struct drm_device *dev = dev_priv->dev;
  6504. if (IS_HASWELL(dev))
  6505. return I915_READ(D_COMP_HSW);
  6506. else
  6507. return I915_READ(D_COMP_BDW);
  6508. }
  6509. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  6510. {
  6511. struct drm_device *dev = dev_priv->dev;
  6512. if (IS_HASWELL(dev)) {
  6513. mutex_lock(&dev_priv->rps.hw_lock);
  6514. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  6515. val))
  6516. DRM_ERROR("Failed to write to D_COMP\n");
  6517. mutex_unlock(&dev_priv->rps.hw_lock);
  6518. } else {
  6519. I915_WRITE(D_COMP_BDW, val);
  6520. POSTING_READ(D_COMP_BDW);
  6521. }
  6522. }
  6523. /*
  6524. * This function implements pieces of two sequences from BSpec:
  6525. * - Sequence for display software to disable LCPLL
  6526. * - Sequence for display software to allow package C8+
  6527. * The steps implemented here are just the steps that actually touch the LCPLL
  6528. * register. Callers should take care of disabling all the display engine
  6529. * functions, doing the mode unset, fixing interrupts, etc.
  6530. */
  6531. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  6532. bool switch_to_fclk, bool allow_power_down)
  6533. {
  6534. uint32_t val;
  6535. assert_can_disable_lcpll(dev_priv);
  6536. val = I915_READ(LCPLL_CTL);
  6537. if (switch_to_fclk) {
  6538. val |= LCPLL_CD_SOURCE_FCLK;
  6539. I915_WRITE(LCPLL_CTL, val);
  6540. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  6541. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  6542. DRM_ERROR("Switching to FCLK failed\n");
  6543. val = I915_READ(LCPLL_CTL);
  6544. }
  6545. val |= LCPLL_PLL_DISABLE;
  6546. I915_WRITE(LCPLL_CTL, val);
  6547. POSTING_READ(LCPLL_CTL);
  6548. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  6549. DRM_ERROR("LCPLL still locked\n");
  6550. val = hsw_read_dcomp(dev_priv);
  6551. val |= D_COMP_COMP_DISABLE;
  6552. hsw_write_dcomp(dev_priv, val);
  6553. ndelay(100);
  6554. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  6555. 1))
  6556. DRM_ERROR("D_COMP RCOMP still in progress\n");
  6557. if (allow_power_down) {
  6558. val = I915_READ(LCPLL_CTL);
  6559. val |= LCPLL_POWER_DOWN_ALLOW;
  6560. I915_WRITE(LCPLL_CTL, val);
  6561. POSTING_READ(LCPLL_CTL);
  6562. }
  6563. }
  6564. /*
  6565. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  6566. * source.
  6567. */
  6568. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  6569. {
  6570. uint32_t val;
  6571. val = I915_READ(LCPLL_CTL);
  6572. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  6573. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  6574. return;
  6575. /*
  6576. * Make sure we're not on PC8 state before disabling PC8, otherwise
  6577. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  6578. *
  6579. * The other problem is that hsw_restore_lcpll() is called as part of
  6580. * the runtime PM resume sequence, so we can't just call
  6581. * gen6_gt_force_wake_get() because that function calls
  6582. * intel_runtime_pm_get(), and we can't change the runtime PM refcount
  6583. * while we are on the resume sequence. So to solve this problem we have
  6584. * to call special forcewake code that doesn't touch runtime PM and
  6585. * doesn't enable the forcewake delayed work.
  6586. */
  6587. spin_lock_irq(&dev_priv->uncore.lock);
  6588. if (dev_priv->uncore.forcewake_count++ == 0)
  6589. dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
  6590. spin_unlock_irq(&dev_priv->uncore.lock);
  6591. if (val & LCPLL_POWER_DOWN_ALLOW) {
  6592. val &= ~LCPLL_POWER_DOWN_ALLOW;
  6593. I915_WRITE(LCPLL_CTL, val);
  6594. POSTING_READ(LCPLL_CTL);
  6595. }
  6596. val = hsw_read_dcomp(dev_priv);
  6597. val |= D_COMP_COMP_FORCE;
  6598. val &= ~D_COMP_COMP_DISABLE;
  6599. hsw_write_dcomp(dev_priv, val);
  6600. val = I915_READ(LCPLL_CTL);
  6601. val &= ~LCPLL_PLL_DISABLE;
  6602. I915_WRITE(LCPLL_CTL, val);
  6603. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  6604. DRM_ERROR("LCPLL not locked yet\n");
  6605. if (val & LCPLL_CD_SOURCE_FCLK) {
  6606. val = I915_READ(LCPLL_CTL);
  6607. val &= ~LCPLL_CD_SOURCE_FCLK;
  6608. I915_WRITE(LCPLL_CTL, val);
  6609. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  6610. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  6611. DRM_ERROR("Switching back to LCPLL failed\n");
  6612. }
  6613. /* See the big comment above. */
  6614. spin_lock_irq(&dev_priv->uncore.lock);
  6615. if (--dev_priv->uncore.forcewake_count == 0)
  6616. dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
  6617. spin_unlock_irq(&dev_priv->uncore.lock);
  6618. }
  6619. /*
  6620. * Package states C8 and deeper are really deep PC states that can only be
  6621. * reached when all the devices on the system allow it, so even if the graphics
  6622. * device allows PC8+, it doesn't mean the system will actually get to these
  6623. * states. Our driver only allows PC8+ when going into runtime PM.
  6624. *
  6625. * The requirements for PC8+ are that all the outputs are disabled, the power
  6626. * well is disabled and most interrupts are disabled, and these are also
  6627. * requirements for runtime PM. When these conditions are met, we manually do
  6628. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  6629. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  6630. * hang the machine.
  6631. *
  6632. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  6633. * the state of some registers, so when we come back from PC8+ we need to
  6634. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  6635. * need to take care of the registers kept by RC6. Notice that this happens even
  6636. * if we don't put the device in PCI D3 state (which is what currently happens
  6637. * because of the runtime PM support).
  6638. *
  6639. * For more, read "Display Sequences for Package C8" on the hardware
  6640. * documentation.
  6641. */
  6642. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  6643. {
  6644. struct drm_device *dev = dev_priv->dev;
  6645. uint32_t val;
  6646. DRM_DEBUG_KMS("Enabling package C8+\n");
  6647. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6648. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6649. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  6650. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6651. }
  6652. lpt_disable_clkout_dp(dev);
  6653. hsw_disable_lcpll(dev_priv, true, true);
  6654. }
  6655. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  6656. {
  6657. struct drm_device *dev = dev_priv->dev;
  6658. uint32_t val;
  6659. DRM_DEBUG_KMS("Disabling package C8+\n");
  6660. hsw_restore_lcpll(dev_priv);
  6661. lpt_init_pch_refclk(dev);
  6662. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6663. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6664. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  6665. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6666. }
  6667. intel_prepare_ddi(dev);
  6668. }
  6669. static int haswell_crtc_compute_clock(struct intel_crtc *crtc)
  6670. {
  6671. if (!intel_ddi_pll_select(crtc))
  6672. return -EINVAL;
  6673. crtc->lowfreq_avail = false;
  6674. return 0;
  6675. }
  6676. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  6677. enum port port,
  6678. struct intel_crtc_config *pipe_config)
  6679. {
  6680. u32 temp, dpll_ctl1;
  6681. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  6682. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  6683. switch (pipe_config->ddi_pll_sel) {
  6684. case SKL_DPLL0:
  6685. /*
  6686. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  6687. * of the shared DPLL framework and thus needs to be read out
  6688. * separately
  6689. */
  6690. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  6691. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  6692. break;
  6693. case SKL_DPLL1:
  6694. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  6695. break;
  6696. case SKL_DPLL2:
  6697. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  6698. break;
  6699. case SKL_DPLL3:
  6700. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  6701. break;
  6702. }
  6703. }
  6704. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  6705. enum port port,
  6706. struct intel_crtc_config *pipe_config)
  6707. {
  6708. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  6709. switch (pipe_config->ddi_pll_sel) {
  6710. case PORT_CLK_SEL_WRPLL1:
  6711. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  6712. break;
  6713. case PORT_CLK_SEL_WRPLL2:
  6714. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  6715. break;
  6716. }
  6717. }
  6718. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  6719. struct intel_crtc_config *pipe_config)
  6720. {
  6721. struct drm_device *dev = crtc->base.dev;
  6722. struct drm_i915_private *dev_priv = dev->dev_private;
  6723. struct intel_shared_dpll *pll;
  6724. enum port port;
  6725. uint32_t tmp;
  6726. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  6727. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  6728. if (IS_SKYLAKE(dev))
  6729. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  6730. else
  6731. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  6732. if (pipe_config->shared_dpll >= 0) {
  6733. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6734. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6735. &pipe_config->dpll_hw_state));
  6736. }
  6737. /*
  6738. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  6739. * DDI E. So just check whether this pipe is wired to DDI E and whether
  6740. * the PCH transcoder is on.
  6741. */
  6742. if (INTEL_INFO(dev)->gen < 9 &&
  6743. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  6744. pipe_config->has_pch_encoder = true;
  6745. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  6746. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6747. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6748. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6749. }
  6750. }
  6751. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  6752. struct intel_crtc_config *pipe_config)
  6753. {
  6754. struct drm_device *dev = crtc->base.dev;
  6755. struct drm_i915_private *dev_priv = dev->dev_private;
  6756. enum intel_display_power_domain pfit_domain;
  6757. uint32_t tmp;
  6758. if (!intel_display_power_is_enabled(dev_priv,
  6759. POWER_DOMAIN_PIPE(crtc->pipe)))
  6760. return false;
  6761. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6762. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6763. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  6764. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  6765. enum pipe trans_edp_pipe;
  6766. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  6767. default:
  6768. WARN(1, "unknown pipe linked to edp transcoder\n");
  6769. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  6770. case TRANS_DDI_EDP_INPUT_A_ON:
  6771. trans_edp_pipe = PIPE_A;
  6772. break;
  6773. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  6774. trans_edp_pipe = PIPE_B;
  6775. break;
  6776. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  6777. trans_edp_pipe = PIPE_C;
  6778. break;
  6779. }
  6780. if (trans_edp_pipe == crtc->pipe)
  6781. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  6782. }
  6783. if (!intel_display_power_is_enabled(dev_priv,
  6784. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  6785. return false;
  6786. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  6787. if (!(tmp & PIPECONF_ENABLE))
  6788. return false;
  6789. haswell_get_ddi_port_state(crtc, pipe_config);
  6790. intel_get_pipe_timings(crtc, pipe_config);
  6791. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  6792. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  6793. if (IS_SKYLAKE(dev))
  6794. skylake_get_pfit_config(crtc, pipe_config);
  6795. else
  6796. ironlake_get_pfit_config(crtc, pipe_config);
  6797. }
  6798. if (IS_HASWELL(dev))
  6799. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  6800. (I915_READ(IPS_CTL) & IPS_ENABLE);
  6801. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  6802. pipe_config->pixel_multiplier =
  6803. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  6804. } else {
  6805. pipe_config->pixel_multiplier = 1;
  6806. }
  6807. return true;
  6808. }
  6809. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  6810. {
  6811. struct drm_device *dev = crtc->dev;
  6812. struct drm_i915_private *dev_priv = dev->dev_private;
  6813. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6814. uint32_t cntl = 0, size = 0;
  6815. if (base) {
  6816. unsigned int width = intel_crtc->cursor_width;
  6817. unsigned int height = intel_crtc->cursor_height;
  6818. unsigned int stride = roundup_pow_of_two(width) * 4;
  6819. switch (stride) {
  6820. default:
  6821. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  6822. width, stride);
  6823. stride = 256;
  6824. /* fallthrough */
  6825. case 256:
  6826. case 512:
  6827. case 1024:
  6828. case 2048:
  6829. break;
  6830. }
  6831. cntl |= CURSOR_ENABLE |
  6832. CURSOR_GAMMA_ENABLE |
  6833. CURSOR_FORMAT_ARGB |
  6834. CURSOR_STRIDE(stride);
  6835. size = (height << 12) | width;
  6836. }
  6837. if (intel_crtc->cursor_cntl != 0 &&
  6838. (intel_crtc->cursor_base != base ||
  6839. intel_crtc->cursor_size != size ||
  6840. intel_crtc->cursor_cntl != cntl)) {
  6841. /* On these chipsets we can only modify the base/size/stride
  6842. * whilst the cursor is disabled.
  6843. */
  6844. I915_WRITE(_CURACNTR, 0);
  6845. POSTING_READ(_CURACNTR);
  6846. intel_crtc->cursor_cntl = 0;
  6847. }
  6848. if (intel_crtc->cursor_base != base) {
  6849. I915_WRITE(_CURABASE, base);
  6850. intel_crtc->cursor_base = base;
  6851. }
  6852. if (intel_crtc->cursor_size != size) {
  6853. I915_WRITE(CURSIZE, size);
  6854. intel_crtc->cursor_size = size;
  6855. }
  6856. if (intel_crtc->cursor_cntl != cntl) {
  6857. I915_WRITE(_CURACNTR, cntl);
  6858. POSTING_READ(_CURACNTR);
  6859. intel_crtc->cursor_cntl = cntl;
  6860. }
  6861. }
  6862. static void i9xx_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. int pipe = intel_crtc->pipe;
  6868. uint32_t cntl;
  6869. cntl = 0;
  6870. if (base) {
  6871. cntl = MCURSOR_GAMMA_ENABLE;
  6872. switch (intel_crtc->cursor_width) {
  6873. case 64:
  6874. cntl |= CURSOR_MODE_64_ARGB_AX;
  6875. break;
  6876. case 128:
  6877. cntl |= CURSOR_MODE_128_ARGB_AX;
  6878. break;
  6879. case 256:
  6880. cntl |= CURSOR_MODE_256_ARGB_AX;
  6881. break;
  6882. default:
  6883. MISSING_CASE(intel_crtc->cursor_width);
  6884. return;
  6885. }
  6886. cntl |= pipe << 28; /* Connect to correct pipe */
  6887. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  6888. cntl |= CURSOR_PIPE_CSC_ENABLE;
  6889. }
  6890. if (to_intel_plane(crtc->cursor)->rotation == BIT(DRM_ROTATE_180))
  6891. cntl |= CURSOR_ROTATE_180;
  6892. if (intel_crtc->cursor_cntl != cntl) {
  6893. I915_WRITE(CURCNTR(pipe), cntl);
  6894. POSTING_READ(CURCNTR(pipe));
  6895. intel_crtc->cursor_cntl = cntl;
  6896. }
  6897. /* and commit changes on next vblank */
  6898. I915_WRITE(CURBASE(pipe), base);
  6899. POSTING_READ(CURBASE(pipe));
  6900. intel_crtc->cursor_base = base;
  6901. }
  6902. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  6903. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  6904. bool on)
  6905. {
  6906. struct drm_device *dev = crtc->dev;
  6907. struct drm_i915_private *dev_priv = dev->dev_private;
  6908. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6909. int pipe = intel_crtc->pipe;
  6910. int x = crtc->cursor_x;
  6911. int y = crtc->cursor_y;
  6912. u32 base = 0, pos = 0;
  6913. if (on)
  6914. base = intel_crtc->cursor_addr;
  6915. if (x >= intel_crtc->config.pipe_src_w)
  6916. base = 0;
  6917. if (y >= intel_crtc->config.pipe_src_h)
  6918. base = 0;
  6919. if (x < 0) {
  6920. if (x + intel_crtc->cursor_width <= 0)
  6921. base = 0;
  6922. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  6923. x = -x;
  6924. }
  6925. pos |= x << CURSOR_X_SHIFT;
  6926. if (y < 0) {
  6927. if (y + intel_crtc->cursor_height <= 0)
  6928. base = 0;
  6929. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  6930. y = -y;
  6931. }
  6932. pos |= y << CURSOR_Y_SHIFT;
  6933. if (base == 0 && intel_crtc->cursor_base == 0)
  6934. return;
  6935. I915_WRITE(CURPOS(pipe), pos);
  6936. /* ILK+ do this automagically */
  6937. if (HAS_GMCH_DISPLAY(dev) &&
  6938. to_intel_plane(crtc->cursor)->rotation == BIT(DRM_ROTATE_180)) {
  6939. base += (intel_crtc->cursor_height *
  6940. intel_crtc->cursor_width - 1) * 4;
  6941. }
  6942. if (IS_845G(dev) || IS_I865G(dev))
  6943. i845_update_cursor(crtc, base);
  6944. else
  6945. i9xx_update_cursor(crtc, base);
  6946. }
  6947. static bool cursor_size_ok(struct drm_device *dev,
  6948. uint32_t width, uint32_t height)
  6949. {
  6950. if (width == 0 || height == 0)
  6951. return false;
  6952. /*
  6953. * 845g/865g are special in that they are only limited by
  6954. * the width of their cursors, the height is arbitrary up to
  6955. * the precision of the register. Everything else requires
  6956. * square cursors, limited to a few power-of-two sizes.
  6957. */
  6958. if (IS_845G(dev) || IS_I865G(dev)) {
  6959. if ((width & 63) != 0)
  6960. return false;
  6961. if (width > (IS_845G(dev) ? 64 : 512))
  6962. return false;
  6963. if (height > 1023)
  6964. return false;
  6965. } else {
  6966. switch (width | height) {
  6967. case 256:
  6968. case 128:
  6969. if (IS_GEN2(dev))
  6970. return false;
  6971. case 64:
  6972. break;
  6973. default:
  6974. return false;
  6975. }
  6976. }
  6977. return true;
  6978. }
  6979. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  6980. u16 *blue, uint32_t start, uint32_t size)
  6981. {
  6982. int end = (start + size > 256) ? 256 : start + size, i;
  6983. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6984. for (i = start; i < end; i++) {
  6985. intel_crtc->lut_r[i] = red[i] >> 8;
  6986. intel_crtc->lut_g[i] = green[i] >> 8;
  6987. intel_crtc->lut_b[i] = blue[i] >> 8;
  6988. }
  6989. intel_crtc_load_lut(crtc);
  6990. }
  6991. /* VESA 640x480x72Hz mode to set on the pipe */
  6992. static struct drm_display_mode load_detect_mode = {
  6993. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  6994. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  6995. };
  6996. struct drm_framebuffer *
  6997. __intel_framebuffer_create(struct drm_device *dev,
  6998. struct drm_mode_fb_cmd2 *mode_cmd,
  6999. struct drm_i915_gem_object *obj)
  7000. {
  7001. struct intel_framebuffer *intel_fb;
  7002. int ret;
  7003. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7004. if (!intel_fb) {
  7005. drm_gem_object_unreference(&obj->base);
  7006. return ERR_PTR(-ENOMEM);
  7007. }
  7008. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  7009. if (ret)
  7010. goto err;
  7011. return &intel_fb->base;
  7012. err:
  7013. drm_gem_object_unreference(&obj->base);
  7014. kfree(intel_fb);
  7015. return ERR_PTR(ret);
  7016. }
  7017. static struct drm_framebuffer *
  7018. intel_framebuffer_create(struct drm_device *dev,
  7019. struct drm_mode_fb_cmd2 *mode_cmd,
  7020. struct drm_i915_gem_object *obj)
  7021. {
  7022. struct drm_framebuffer *fb;
  7023. int ret;
  7024. ret = i915_mutex_lock_interruptible(dev);
  7025. if (ret)
  7026. return ERR_PTR(ret);
  7027. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  7028. mutex_unlock(&dev->struct_mutex);
  7029. return fb;
  7030. }
  7031. static u32
  7032. intel_framebuffer_pitch_for_width(int width, int bpp)
  7033. {
  7034. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  7035. return ALIGN(pitch, 64);
  7036. }
  7037. static u32
  7038. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  7039. {
  7040. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  7041. return PAGE_ALIGN(pitch * mode->vdisplay);
  7042. }
  7043. static struct drm_framebuffer *
  7044. intel_framebuffer_create_for_mode(struct drm_device *dev,
  7045. struct drm_display_mode *mode,
  7046. int depth, int bpp)
  7047. {
  7048. struct drm_i915_gem_object *obj;
  7049. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  7050. obj = i915_gem_alloc_object(dev,
  7051. intel_framebuffer_size_for_mode(mode, bpp));
  7052. if (obj == NULL)
  7053. return ERR_PTR(-ENOMEM);
  7054. mode_cmd.width = mode->hdisplay;
  7055. mode_cmd.height = mode->vdisplay;
  7056. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  7057. bpp);
  7058. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  7059. return intel_framebuffer_create(dev, &mode_cmd, obj);
  7060. }
  7061. static struct drm_framebuffer *
  7062. mode_fits_in_fbdev(struct drm_device *dev,
  7063. struct drm_display_mode *mode)
  7064. {
  7065. #ifdef CONFIG_DRM_I915_FBDEV
  7066. struct drm_i915_private *dev_priv = dev->dev_private;
  7067. struct drm_i915_gem_object *obj;
  7068. struct drm_framebuffer *fb;
  7069. if (!dev_priv->fbdev)
  7070. return NULL;
  7071. if (!dev_priv->fbdev->fb)
  7072. return NULL;
  7073. obj = dev_priv->fbdev->fb->obj;
  7074. BUG_ON(!obj);
  7075. fb = &dev_priv->fbdev->fb->base;
  7076. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  7077. fb->bits_per_pixel))
  7078. return NULL;
  7079. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  7080. return NULL;
  7081. return fb;
  7082. #else
  7083. return NULL;
  7084. #endif
  7085. }
  7086. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  7087. struct drm_display_mode *mode,
  7088. struct intel_load_detect_pipe *old,
  7089. struct drm_modeset_acquire_ctx *ctx)
  7090. {
  7091. struct intel_crtc *intel_crtc;
  7092. struct intel_encoder *intel_encoder =
  7093. intel_attached_encoder(connector);
  7094. struct drm_crtc *possible_crtc;
  7095. struct drm_encoder *encoder = &intel_encoder->base;
  7096. struct drm_crtc *crtc = NULL;
  7097. struct drm_device *dev = encoder->dev;
  7098. struct drm_framebuffer *fb;
  7099. struct drm_mode_config *config = &dev->mode_config;
  7100. int ret, i = -1;
  7101. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7102. connector->base.id, connector->name,
  7103. encoder->base.id, encoder->name);
  7104. retry:
  7105. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  7106. if (ret)
  7107. goto fail_unlock;
  7108. /*
  7109. * Algorithm gets a little messy:
  7110. *
  7111. * - if the connector already has an assigned crtc, use it (but make
  7112. * sure it's on first)
  7113. *
  7114. * - try to find the first unused crtc that can drive this connector,
  7115. * and use that if we find one
  7116. */
  7117. /* See if we already have a CRTC for this connector */
  7118. if (encoder->crtc) {
  7119. crtc = encoder->crtc;
  7120. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7121. if (ret)
  7122. goto fail_unlock;
  7123. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  7124. if (ret)
  7125. goto fail_unlock;
  7126. old->dpms_mode = connector->dpms;
  7127. old->load_detect_temp = false;
  7128. /* Make sure the crtc and connector are running */
  7129. if (connector->dpms != DRM_MODE_DPMS_ON)
  7130. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  7131. return true;
  7132. }
  7133. /* Find an unused one (if possible) */
  7134. for_each_crtc(dev, possible_crtc) {
  7135. i++;
  7136. if (!(encoder->possible_crtcs & (1 << i)))
  7137. continue;
  7138. if (possible_crtc->enabled)
  7139. continue;
  7140. /* This can occur when applying the pipe A quirk on resume. */
  7141. if (to_intel_crtc(possible_crtc)->new_enabled)
  7142. continue;
  7143. crtc = possible_crtc;
  7144. break;
  7145. }
  7146. /*
  7147. * If we didn't find an unused CRTC, don't use any.
  7148. */
  7149. if (!crtc) {
  7150. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  7151. goto fail_unlock;
  7152. }
  7153. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7154. if (ret)
  7155. goto fail_unlock;
  7156. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  7157. if (ret)
  7158. goto fail_unlock;
  7159. intel_encoder->new_crtc = to_intel_crtc(crtc);
  7160. to_intel_connector(connector)->new_encoder = intel_encoder;
  7161. intel_crtc = to_intel_crtc(crtc);
  7162. intel_crtc->new_enabled = true;
  7163. intel_crtc->new_config = &intel_crtc->config;
  7164. old->dpms_mode = connector->dpms;
  7165. old->load_detect_temp = true;
  7166. old->release_fb = NULL;
  7167. if (!mode)
  7168. mode = &load_detect_mode;
  7169. /* We need a framebuffer large enough to accommodate all accesses
  7170. * that the plane may generate whilst we perform load detection.
  7171. * We can not rely on the fbcon either being present (we get called
  7172. * during its initialisation to detect all boot displays, or it may
  7173. * not even exist) or that it is large enough to satisfy the
  7174. * requested mode.
  7175. */
  7176. fb = mode_fits_in_fbdev(dev, mode);
  7177. if (fb == NULL) {
  7178. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  7179. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  7180. old->release_fb = fb;
  7181. } else
  7182. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  7183. if (IS_ERR(fb)) {
  7184. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  7185. goto fail;
  7186. }
  7187. if (intel_set_mode(crtc, mode, 0, 0, fb)) {
  7188. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  7189. if (old->release_fb)
  7190. old->release_fb->funcs->destroy(old->release_fb);
  7191. goto fail;
  7192. }
  7193. /* let the connector get through one full cycle before testing */
  7194. intel_wait_for_vblank(dev, intel_crtc->pipe);
  7195. return true;
  7196. fail:
  7197. intel_crtc->new_enabled = crtc->enabled;
  7198. if (intel_crtc->new_enabled)
  7199. intel_crtc->new_config = &intel_crtc->config;
  7200. else
  7201. intel_crtc->new_config = NULL;
  7202. fail_unlock:
  7203. if (ret == -EDEADLK) {
  7204. drm_modeset_backoff(ctx);
  7205. goto retry;
  7206. }
  7207. return false;
  7208. }
  7209. void intel_release_load_detect_pipe(struct drm_connector *connector,
  7210. struct intel_load_detect_pipe *old)
  7211. {
  7212. struct intel_encoder *intel_encoder =
  7213. intel_attached_encoder(connector);
  7214. struct drm_encoder *encoder = &intel_encoder->base;
  7215. struct drm_crtc *crtc = encoder->crtc;
  7216. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7217. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7218. connector->base.id, connector->name,
  7219. encoder->base.id, encoder->name);
  7220. if (old->load_detect_temp) {
  7221. to_intel_connector(connector)->new_encoder = NULL;
  7222. intel_encoder->new_crtc = NULL;
  7223. intel_crtc->new_enabled = false;
  7224. intel_crtc->new_config = NULL;
  7225. intel_set_mode(crtc, NULL, 0, 0, NULL);
  7226. if (old->release_fb) {
  7227. drm_framebuffer_unregister_private(old->release_fb);
  7228. drm_framebuffer_unreference(old->release_fb);
  7229. }
  7230. return;
  7231. }
  7232. /* Switch crtc and encoder back off if necessary */
  7233. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  7234. connector->funcs->dpms(connector, old->dpms_mode);
  7235. }
  7236. static int i9xx_pll_refclk(struct drm_device *dev,
  7237. const struct intel_crtc_config *pipe_config)
  7238. {
  7239. struct drm_i915_private *dev_priv = dev->dev_private;
  7240. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7241. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  7242. return dev_priv->vbt.lvds_ssc_freq;
  7243. else if (HAS_PCH_SPLIT(dev))
  7244. return 120000;
  7245. else if (!IS_GEN2(dev))
  7246. return 96000;
  7247. else
  7248. return 48000;
  7249. }
  7250. /* Returns the clock of the currently programmed mode of the given pipe. */
  7251. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  7252. struct intel_crtc_config *pipe_config)
  7253. {
  7254. struct drm_device *dev = crtc->base.dev;
  7255. struct drm_i915_private *dev_priv = dev->dev_private;
  7256. int pipe = pipe_config->cpu_transcoder;
  7257. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7258. u32 fp;
  7259. intel_clock_t clock;
  7260. int refclk = i9xx_pll_refclk(dev, pipe_config);
  7261. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  7262. fp = pipe_config->dpll_hw_state.fp0;
  7263. else
  7264. fp = pipe_config->dpll_hw_state.fp1;
  7265. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  7266. if (IS_PINEVIEW(dev)) {
  7267. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  7268. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7269. } else {
  7270. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  7271. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7272. }
  7273. if (!IS_GEN2(dev)) {
  7274. if (IS_PINEVIEW(dev))
  7275. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  7276. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  7277. else
  7278. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  7279. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7280. switch (dpll & DPLL_MODE_MASK) {
  7281. case DPLLB_MODE_DAC_SERIAL:
  7282. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  7283. 5 : 10;
  7284. break;
  7285. case DPLLB_MODE_LVDS:
  7286. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  7287. 7 : 14;
  7288. break;
  7289. default:
  7290. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  7291. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  7292. return;
  7293. }
  7294. if (IS_PINEVIEW(dev))
  7295. pineview_clock(refclk, &clock);
  7296. else
  7297. i9xx_clock(refclk, &clock);
  7298. } else {
  7299. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  7300. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  7301. if (is_lvds) {
  7302. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  7303. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7304. if (lvds & LVDS_CLKB_POWER_UP)
  7305. clock.p2 = 7;
  7306. else
  7307. clock.p2 = 14;
  7308. } else {
  7309. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  7310. clock.p1 = 2;
  7311. else {
  7312. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  7313. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  7314. }
  7315. if (dpll & PLL_P2_DIVIDE_BY_4)
  7316. clock.p2 = 4;
  7317. else
  7318. clock.p2 = 2;
  7319. }
  7320. i9xx_clock(refclk, &clock);
  7321. }
  7322. /*
  7323. * This value includes pixel_multiplier. We will use
  7324. * port_clock to compute adjusted_mode.crtc_clock in the
  7325. * encoder's get_config() function.
  7326. */
  7327. pipe_config->port_clock = clock.dot;
  7328. }
  7329. int intel_dotclock_calculate(int link_freq,
  7330. const struct intel_link_m_n *m_n)
  7331. {
  7332. /*
  7333. * The calculation for the data clock is:
  7334. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  7335. * But we want to avoid losing precison if possible, so:
  7336. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  7337. *
  7338. * and the link clock is simpler:
  7339. * link_clock = (m * link_clock) / n
  7340. */
  7341. if (!m_n->link_n)
  7342. return 0;
  7343. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  7344. }
  7345. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  7346. struct intel_crtc_config *pipe_config)
  7347. {
  7348. struct drm_device *dev = crtc->base.dev;
  7349. /* read out port_clock from the DPLL */
  7350. i9xx_crtc_clock_get(crtc, pipe_config);
  7351. /*
  7352. * This value does not include pixel_multiplier.
  7353. * We will check that port_clock and adjusted_mode.crtc_clock
  7354. * agree once we know their relationship in the encoder's
  7355. * get_config() function.
  7356. */
  7357. pipe_config->adjusted_mode.crtc_clock =
  7358. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  7359. &pipe_config->fdi_m_n);
  7360. }
  7361. /** Returns the currently programmed mode of the given pipe. */
  7362. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  7363. struct drm_crtc *crtc)
  7364. {
  7365. struct drm_i915_private *dev_priv = dev->dev_private;
  7366. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7367. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  7368. struct drm_display_mode *mode;
  7369. struct intel_crtc_config pipe_config;
  7370. int htot = I915_READ(HTOTAL(cpu_transcoder));
  7371. int hsync = I915_READ(HSYNC(cpu_transcoder));
  7372. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  7373. int vsync = I915_READ(VSYNC(cpu_transcoder));
  7374. enum pipe pipe = intel_crtc->pipe;
  7375. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  7376. if (!mode)
  7377. return NULL;
  7378. /*
  7379. * Construct a pipe_config sufficient for getting the clock info
  7380. * back out of crtc_clock_get.
  7381. *
  7382. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  7383. * to use a real value here instead.
  7384. */
  7385. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  7386. pipe_config.pixel_multiplier = 1;
  7387. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  7388. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  7389. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  7390. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  7391. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  7392. mode->hdisplay = (htot & 0xffff) + 1;
  7393. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  7394. mode->hsync_start = (hsync & 0xffff) + 1;
  7395. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  7396. mode->vdisplay = (vtot & 0xffff) + 1;
  7397. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  7398. mode->vsync_start = (vsync & 0xffff) + 1;
  7399. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  7400. drm_mode_set_name(mode);
  7401. return mode;
  7402. }
  7403. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  7404. {
  7405. struct drm_device *dev = crtc->dev;
  7406. struct drm_i915_private *dev_priv = dev->dev_private;
  7407. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7408. if (!HAS_GMCH_DISPLAY(dev))
  7409. return;
  7410. if (!dev_priv->lvds_downclock_avail)
  7411. return;
  7412. /*
  7413. * Since this is called by a timer, we should never get here in
  7414. * the manual case.
  7415. */
  7416. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  7417. int pipe = intel_crtc->pipe;
  7418. int dpll_reg = DPLL(pipe);
  7419. int dpll;
  7420. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  7421. assert_panel_unlocked(dev_priv, pipe);
  7422. dpll = I915_READ(dpll_reg);
  7423. dpll |= DISPLAY_RATE_SELECT_FPA1;
  7424. I915_WRITE(dpll_reg, dpll);
  7425. intel_wait_for_vblank(dev, pipe);
  7426. dpll = I915_READ(dpll_reg);
  7427. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  7428. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  7429. }
  7430. }
  7431. void intel_mark_busy(struct drm_device *dev)
  7432. {
  7433. struct drm_i915_private *dev_priv = dev->dev_private;
  7434. if (dev_priv->mm.busy)
  7435. return;
  7436. intel_runtime_pm_get(dev_priv);
  7437. i915_update_gfx_val(dev_priv);
  7438. dev_priv->mm.busy = true;
  7439. }
  7440. void intel_mark_idle(struct drm_device *dev)
  7441. {
  7442. struct drm_i915_private *dev_priv = dev->dev_private;
  7443. struct drm_crtc *crtc;
  7444. if (!dev_priv->mm.busy)
  7445. return;
  7446. dev_priv->mm.busy = false;
  7447. if (!i915.powersave)
  7448. goto out;
  7449. for_each_crtc(dev, crtc) {
  7450. if (!crtc->primary->fb)
  7451. continue;
  7452. intel_decrease_pllclock(crtc);
  7453. }
  7454. if (INTEL_INFO(dev)->gen >= 6)
  7455. gen6_rps_idle(dev->dev_private);
  7456. out:
  7457. intel_runtime_pm_put(dev_priv);
  7458. }
  7459. static void intel_crtc_destroy(struct drm_crtc *crtc)
  7460. {
  7461. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7462. struct drm_device *dev = crtc->dev;
  7463. struct intel_unpin_work *work;
  7464. spin_lock_irq(&dev->event_lock);
  7465. work = intel_crtc->unpin_work;
  7466. intel_crtc->unpin_work = NULL;
  7467. spin_unlock_irq(&dev->event_lock);
  7468. if (work) {
  7469. cancel_work_sync(&work->work);
  7470. kfree(work);
  7471. }
  7472. drm_crtc_cleanup(crtc);
  7473. kfree(intel_crtc);
  7474. }
  7475. static void intel_unpin_work_fn(struct work_struct *__work)
  7476. {
  7477. struct intel_unpin_work *work =
  7478. container_of(__work, struct intel_unpin_work, work);
  7479. struct drm_device *dev = work->crtc->dev;
  7480. enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
  7481. mutex_lock(&dev->struct_mutex);
  7482. intel_unpin_fb_obj(work->old_fb_obj);
  7483. drm_gem_object_unreference(&work->pending_flip_obj->base);
  7484. drm_gem_object_unreference(&work->old_fb_obj->base);
  7485. intel_fbc_update(dev);
  7486. if (work->flip_queued_req)
  7487. i915_gem_request_assign(&work->flip_queued_req, NULL);
  7488. mutex_unlock(&dev->struct_mutex);
  7489. intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  7490. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  7491. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  7492. kfree(work);
  7493. }
  7494. static void do_intel_finish_page_flip(struct drm_device *dev,
  7495. struct drm_crtc *crtc)
  7496. {
  7497. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7498. struct intel_unpin_work *work;
  7499. unsigned long flags;
  7500. /* Ignore early vblank irqs */
  7501. if (intel_crtc == NULL)
  7502. return;
  7503. /*
  7504. * This is called both by irq handlers and the reset code (to complete
  7505. * lost pageflips) so needs the full irqsave spinlocks.
  7506. */
  7507. spin_lock_irqsave(&dev->event_lock, flags);
  7508. work = intel_crtc->unpin_work;
  7509. /* Ensure we don't miss a work->pending update ... */
  7510. smp_rmb();
  7511. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  7512. spin_unlock_irqrestore(&dev->event_lock, flags);
  7513. return;
  7514. }
  7515. page_flip_completed(intel_crtc);
  7516. spin_unlock_irqrestore(&dev->event_lock, flags);
  7517. }
  7518. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  7519. {
  7520. struct drm_i915_private *dev_priv = dev->dev_private;
  7521. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  7522. do_intel_finish_page_flip(dev, crtc);
  7523. }
  7524. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  7525. {
  7526. struct drm_i915_private *dev_priv = dev->dev_private;
  7527. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  7528. do_intel_finish_page_flip(dev, crtc);
  7529. }
  7530. /* Is 'a' after or equal to 'b'? */
  7531. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  7532. {
  7533. return !((a - b) & 0x80000000);
  7534. }
  7535. static bool page_flip_finished(struct intel_crtc *crtc)
  7536. {
  7537. struct drm_device *dev = crtc->base.dev;
  7538. struct drm_i915_private *dev_priv = dev->dev_private;
  7539. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  7540. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  7541. return true;
  7542. /*
  7543. * The relevant registers doen't exist on pre-ctg.
  7544. * As the flip done interrupt doesn't trigger for mmio
  7545. * flips on gmch platforms, a flip count check isn't
  7546. * really needed there. But since ctg has the registers,
  7547. * include it in the check anyway.
  7548. */
  7549. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  7550. return true;
  7551. /*
  7552. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  7553. * used the same base address. In that case the mmio flip might
  7554. * have completed, but the CS hasn't even executed the flip yet.
  7555. *
  7556. * A flip count check isn't enough as the CS might have updated
  7557. * the base address just after start of vblank, but before we
  7558. * managed to process the interrupt. This means we'd complete the
  7559. * CS flip too soon.
  7560. *
  7561. * Combining both checks should get us a good enough result. It may
  7562. * still happen that the CS flip has been executed, but has not
  7563. * yet actually completed. But in case the base address is the same
  7564. * anyway, we don't really care.
  7565. */
  7566. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  7567. crtc->unpin_work->gtt_offset &&
  7568. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  7569. crtc->unpin_work->flip_count);
  7570. }
  7571. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  7572. {
  7573. struct drm_i915_private *dev_priv = dev->dev_private;
  7574. struct intel_crtc *intel_crtc =
  7575. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  7576. unsigned long flags;
  7577. /*
  7578. * This is called both by irq handlers and the reset code (to complete
  7579. * lost pageflips) so needs the full irqsave spinlocks.
  7580. *
  7581. * NB: An MMIO update of the plane base pointer will also
  7582. * generate a page-flip completion irq, i.e. every modeset
  7583. * is also accompanied by a spurious intel_prepare_page_flip().
  7584. */
  7585. spin_lock_irqsave(&dev->event_lock, flags);
  7586. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  7587. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  7588. spin_unlock_irqrestore(&dev->event_lock, flags);
  7589. }
  7590. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  7591. {
  7592. /* Ensure that the work item is consistent when activating it ... */
  7593. smp_wmb();
  7594. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  7595. /* and that it is marked active as soon as the irq could fire. */
  7596. smp_wmb();
  7597. }
  7598. static int intel_gen2_queue_flip(struct drm_device *dev,
  7599. struct drm_crtc *crtc,
  7600. struct drm_framebuffer *fb,
  7601. struct drm_i915_gem_object *obj,
  7602. struct intel_engine_cs *ring,
  7603. uint32_t flags)
  7604. {
  7605. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7606. u32 flip_mask;
  7607. int ret;
  7608. ret = intel_ring_begin(ring, 6);
  7609. if (ret)
  7610. return ret;
  7611. /* Can't queue multiple flips, so wait for the previous
  7612. * one to finish before executing the next.
  7613. */
  7614. if (intel_crtc->plane)
  7615. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7616. else
  7617. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7618. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7619. intel_ring_emit(ring, MI_NOOP);
  7620. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7621. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7622. intel_ring_emit(ring, fb->pitches[0]);
  7623. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7624. intel_ring_emit(ring, 0); /* aux display base address, unused */
  7625. intel_mark_page_flip_active(intel_crtc);
  7626. __intel_ring_advance(ring);
  7627. return 0;
  7628. }
  7629. static int intel_gen3_queue_flip(struct drm_device *dev,
  7630. struct drm_crtc *crtc,
  7631. struct drm_framebuffer *fb,
  7632. struct drm_i915_gem_object *obj,
  7633. struct intel_engine_cs *ring,
  7634. uint32_t flags)
  7635. {
  7636. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7637. u32 flip_mask;
  7638. int ret;
  7639. ret = intel_ring_begin(ring, 6);
  7640. if (ret)
  7641. return ret;
  7642. if (intel_crtc->plane)
  7643. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7644. else
  7645. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7646. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7647. intel_ring_emit(ring, MI_NOOP);
  7648. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  7649. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7650. intel_ring_emit(ring, fb->pitches[0]);
  7651. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7652. intel_ring_emit(ring, MI_NOOP);
  7653. intel_mark_page_flip_active(intel_crtc);
  7654. __intel_ring_advance(ring);
  7655. return 0;
  7656. }
  7657. static int intel_gen4_queue_flip(struct drm_device *dev,
  7658. struct drm_crtc *crtc,
  7659. struct drm_framebuffer *fb,
  7660. struct drm_i915_gem_object *obj,
  7661. struct intel_engine_cs *ring,
  7662. uint32_t flags)
  7663. {
  7664. struct drm_i915_private *dev_priv = dev->dev_private;
  7665. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7666. uint32_t pf, pipesrc;
  7667. int ret;
  7668. ret = intel_ring_begin(ring, 4);
  7669. if (ret)
  7670. return ret;
  7671. /* i965+ uses the linear or tiled offsets from the
  7672. * Display Registers (which do not change across a page-flip)
  7673. * so we need only reprogram the base address.
  7674. */
  7675. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7676. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7677. intel_ring_emit(ring, fb->pitches[0]);
  7678. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  7679. obj->tiling_mode);
  7680. /* XXX Enabling the panel-fitter across page-flip is so far
  7681. * untested on non-native modes, so ignore it for now.
  7682. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  7683. */
  7684. pf = 0;
  7685. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7686. intel_ring_emit(ring, pf | pipesrc);
  7687. intel_mark_page_flip_active(intel_crtc);
  7688. __intel_ring_advance(ring);
  7689. return 0;
  7690. }
  7691. static int intel_gen6_queue_flip(struct drm_device *dev,
  7692. struct drm_crtc *crtc,
  7693. struct drm_framebuffer *fb,
  7694. struct drm_i915_gem_object *obj,
  7695. struct intel_engine_cs *ring,
  7696. uint32_t flags)
  7697. {
  7698. struct drm_i915_private *dev_priv = dev->dev_private;
  7699. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7700. uint32_t pf, pipesrc;
  7701. int ret;
  7702. ret = intel_ring_begin(ring, 4);
  7703. if (ret)
  7704. return ret;
  7705. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7706. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7707. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  7708. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7709. /* Contrary to the suggestions in the documentation,
  7710. * "Enable Panel Fitter" does not seem to be required when page
  7711. * flipping with a non-native mode, and worse causes a normal
  7712. * modeset to fail.
  7713. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  7714. */
  7715. pf = 0;
  7716. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7717. intel_ring_emit(ring, pf | pipesrc);
  7718. intel_mark_page_flip_active(intel_crtc);
  7719. __intel_ring_advance(ring);
  7720. return 0;
  7721. }
  7722. static int intel_gen7_queue_flip(struct drm_device *dev,
  7723. struct drm_crtc *crtc,
  7724. struct drm_framebuffer *fb,
  7725. struct drm_i915_gem_object *obj,
  7726. struct intel_engine_cs *ring,
  7727. uint32_t flags)
  7728. {
  7729. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7730. uint32_t plane_bit = 0;
  7731. int len, ret;
  7732. switch (intel_crtc->plane) {
  7733. case PLANE_A:
  7734. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  7735. break;
  7736. case PLANE_B:
  7737. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  7738. break;
  7739. case PLANE_C:
  7740. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  7741. break;
  7742. default:
  7743. WARN_ONCE(1, "unknown plane in flip command\n");
  7744. return -ENODEV;
  7745. }
  7746. len = 4;
  7747. if (ring->id == RCS) {
  7748. len += 6;
  7749. /*
  7750. * On Gen 8, SRM is now taking an extra dword to accommodate
  7751. * 48bits addresses, and we need a NOOP for the batch size to
  7752. * stay even.
  7753. */
  7754. if (IS_GEN8(dev))
  7755. len += 2;
  7756. }
  7757. /*
  7758. * BSpec MI_DISPLAY_FLIP for IVB:
  7759. * "The full packet must be contained within the same cache line."
  7760. *
  7761. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  7762. * cacheline, if we ever start emitting more commands before
  7763. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  7764. * then do the cacheline alignment, and finally emit the
  7765. * MI_DISPLAY_FLIP.
  7766. */
  7767. ret = intel_ring_cacheline_align(ring);
  7768. if (ret)
  7769. return ret;
  7770. ret = intel_ring_begin(ring, len);
  7771. if (ret)
  7772. return ret;
  7773. /* Unmask the flip-done completion message. Note that the bspec says that
  7774. * we should do this for both the BCS and RCS, and that we must not unmask
  7775. * more than one flip event at any time (or ensure that one flip message
  7776. * can be sent by waiting for flip-done prior to queueing new flips).
  7777. * Experimentation says that BCS works despite DERRMR masking all
  7778. * flip-done completion events and that unmasking all planes at once
  7779. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  7780. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  7781. */
  7782. if (ring->id == RCS) {
  7783. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  7784. intel_ring_emit(ring, DERRMR);
  7785. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  7786. DERRMR_PIPEB_PRI_FLIP_DONE |
  7787. DERRMR_PIPEC_PRI_FLIP_DONE));
  7788. if (IS_GEN8(dev))
  7789. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  7790. MI_SRM_LRM_GLOBAL_GTT);
  7791. else
  7792. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  7793. MI_SRM_LRM_GLOBAL_GTT);
  7794. intel_ring_emit(ring, DERRMR);
  7795. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  7796. if (IS_GEN8(dev)) {
  7797. intel_ring_emit(ring, 0);
  7798. intel_ring_emit(ring, MI_NOOP);
  7799. }
  7800. }
  7801. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  7802. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  7803. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7804. intel_ring_emit(ring, (MI_NOOP));
  7805. intel_mark_page_flip_active(intel_crtc);
  7806. __intel_ring_advance(ring);
  7807. return 0;
  7808. }
  7809. static bool use_mmio_flip(struct intel_engine_cs *ring,
  7810. struct drm_i915_gem_object *obj)
  7811. {
  7812. /*
  7813. * This is not being used for older platforms, because
  7814. * non-availability of flip done interrupt forces us to use
  7815. * CS flips. Older platforms derive flip done using some clever
  7816. * tricks involving the flip_pending status bits and vblank irqs.
  7817. * So using MMIO flips there would disrupt this mechanism.
  7818. */
  7819. if (ring == NULL)
  7820. return true;
  7821. if (INTEL_INFO(ring->dev)->gen < 5)
  7822. return false;
  7823. if (i915.use_mmio_flip < 0)
  7824. return false;
  7825. else if (i915.use_mmio_flip > 0)
  7826. return true;
  7827. else if (i915.enable_execlists)
  7828. return true;
  7829. else
  7830. return ring != i915_gem_request_get_ring(obj->last_read_req);
  7831. }
  7832. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
  7833. {
  7834. struct drm_device *dev = intel_crtc->base.dev;
  7835. struct drm_i915_private *dev_priv = dev->dev_private;
  7836. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  7837. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  7838. struct drm_i915_gem_object *obj = intel_fb->obj;
  7839. const enum pipe pipe = intel_crtc->pipe;
  7840. u32 ctl, stride;
  7841. ctl = I915_READ(PLANE_CTL(pipe, 0));
  7842. ctl &= ~PLANE_CTL_TILED_MASK;
  7843. if (obj->tiling_mode == I915_TILING_X)
  7844. ctl |= PLANE_CTL_TILED_X;
  7845. /*
  7846. * The stride is either expressed as a multiple of 64 bytes chunks for
  7847. * linear buffers or in number of tiles for tiled buffers.
  7848. */
  7849. stride = fb->pitches[0] >> 6;
  7850. if (obj->tiling_mode == I915_TILING_X)
  7851. stride = fb->pitches[0] >> 9; /* X tiles are 512 bytes wide */
  7852. /*
  7853. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  7854. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  7855. */
  7856. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  7857. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  7858. I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
  7859. POSTING_READ(PLANE_SURF(pipe, 0));
  7860. }
  7861. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
  7862. {
  7863. struct drm_device *dev = intel_crtc->base.dev;
  7864. struct drm_i915_private *dev_priv = dev->dev_private;
  7865. struct intel_framebuffer *intel_fb =
  7866. to_intel_framebuffer(intel_crtc->base.primary->fb);
  7867. struct drm_i915_gem_object *obj = intel_fb->obj;
  7868. u32 dspcntr;
  7869. u32 reg;
  7870. reg = DSPCNTR(intel_crtc->plane);
  7871. dspcntr = I915_READ(reg);
  7872. if (obj->tiling_mode != I915_TILING_NONE)
  7873. dspcntr |= DISPPLANE_TILED;
  7874. else
  7875. dspcntr &= ~DISPPLANE_TILED;
  7876. I915_WRITE(reg, dspcntr);
  7877. I915_WRITE(DSPSURF(intel_crtc->plane),
  7878. intel_crtc->unpin_work->gtt_offset);
  7879. POSTING_READ(DSPSURF(intel_crtc->plane));
  7880. }
  7881. /*
  7882. * XXX: This is the temporary way to update the plane registers until we get
  7883. * around to using the usual plane update functions for MMIO flips
  7884. */
  7885. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  7886. {
  7887. struct drm_device *dev = intel_crtc->base.dev;
  7888. bool atomic_update;
  7889. u32 start_vbl_count;
  7890. intel_mark_page_flip_active(intel_crtc);
  7891. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  7892. if (INTEL_INFO(dev)->gen >= 9)
  7893. skl_do_mmio_flip(intel_crtc);
  7894. else
  7895. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  7896. ilk_do_mmio_flip(intel_crtc);
  7897. if (atomic_update)
  7898. intel_pipe_update_end(intel_crtc, start_vbl_count);
  7899. }
  7900. static void intel_mmio_flip_work_func(struct work_struct *work)
  7901. {
  7902. struct intel_crtc *crtc =
  7903. container_of(work, struct intel_crtc, mmio_flip.work);
  7904. struct intel_mmio_flip *mmio_flip;
  7905. mmio_flip = &crtc->mmio_flip;
  7906. if (mmio_flip->req)
  7907. WARN_ON(__i915_wait_request(mmio_flip->req,
  7908. crtc->reset_counter,
  7909. false, NULL, NULL) != 0);
  7910. intel_do_mmio_flip(crtc);
  7911. if (mmio_flip->req) {
  7912. mutex_lock(&crtc->base.dev->struct_mutex);
  7913. i915_gem_request_assign(&mmio_flip->req, NULL);
  7914. mutex_unlock(&crtc->base.dev->struct_mutex);
  7915. }
  7916. }
  7917. static int intel_queue_mmio_flip(struct drm_device *dev,
  7918. struct drm_crtc *crtc,
  7919. struct drm_framebuffer *fb,
  7920. struct drm_i915_gem_object *obj,
  7921. struct intel_engine_cs *ring,
  7922. uint32_t flags)
  7923. {
  7924. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7925. i915_gem_request_assign(&intel_crtc->mmio_flip.req,
  7926. obj->last_write_req);
  7927. schedule_work(&intel_crtc->mmio_flip.work);
  7928. return 0;
  7929. }
  7930. static int intel_gen9_queue_flip(struct drm_device *dev,
  7931. struct drm_crtc *crtc,
  7932. struct drm_framebuffer *fb,
  7933. struct drm_i915_gem_object *obj,
  7934. struct intel_engine_cs *ring,
  7935. uint32_t flags)
  7936. {
  7937. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7938. uint32_t plane = 0, stride;
  7939. int ret;
  7940. switch(intel_crtc->pipe) {
  7941. case PIPE_A:
  7942. plane = MI_DISPLAY_FLIP_SKL_PLANE_1_A;
  7943. break;
  7944. case PIPE_B:
  7945. plane = MI_DISPLAY_FLIP_SKL_PLANE_1_B;
  7946. break;
  7947. case PIPE_C:
  7948. plane = MI_DISPLAY_FLIP_SKL_PLANE_1_C;
  7949. break;
  7950. default:
  7951. WARN_ONCE(1, "unknown plane in flip command\n");
  7952. return -ENODEV;
  7953. }
  7954. switch (obj->tiling_mode) {
  7955. case I915_TILING_NONE:
  7956. stride = fb->pitches[0] >> 6;
  7957. break;
  7958. case I915_TILING_X:
  7959. stride = fb->pitches[0] >> 9;
  7960. break;
  7961. default:
  7962. WARN_ONCE(1, "unknown tiling in flip command\n");
  7963. return -ENODEV;
  7964. }
  7965. ret = intel_ring_begin(ring, 10);
  7966. if (ret)
  7967. return ret;
  7968. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  7969. intel_ring_emit(ring, DERRMR);
  7970. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  7971. DERRMR_PIPEB_PRI_FLIP_DONE |
  7972. DERRMR_PIPEC_PRI_FLIP_DONE));
  7973. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  7974. MI_SRM_LRM_GLOBAL_GTT);
  7975. intel_ring_emit(ring, DERRMR);
  7976. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  7977. intel_ring_emit(ring, 0);
  7978. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane);
  7979. intel_ring_emit(ring, stride << 6 | obj->tiling_mode);
  7980. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7981. intel_mark_page_flip_active(intel_crtc);
  7982. __intel_ring_advance(ring);
  7983. return 0;
  7984. }
  7985. static int intel_default_queue_flip(struct drm_device *dev,
  7986. struct drm_crtc *crtc,
  7987. struct drm_framebuffer *fb,
  7988. struct drm_i915_gem_object *obj,
  7989. struct intel_engine_cs *ring,
  7990. uint32_t flags)
  7991. {
  7992. return -ENODEV;
  7993. }
  7994. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  7995. struct drm_crtc *crtc)
  7996. {
  7997. struct drm_i915_private *dev_priv = dev->dev_private;
  7998. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7999. struct intel_unpin_work *work = intel_crtc->unpin_work;
  8000. u32 addr;
  8001. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  8002. return true;
  8003. if (!work->enable_stall_check)
  8004. return false;
  8005. if (work->flip_ready_vblank == 0) {
  8006. if (work->flip_queued_req &&
  8007. !i915_gem_request_completed(work->flip_queued_req, true))
  8008. return false;
  8009. work->flip_ready_vblank = drm_vblank_count(dev, intel_crtc->pipe);
  8010. }
  8011. if (drm_vblank_count(dev, intel_crtc->pipe) - work->flip_ready_vblank < 3)
  8012. return false;
  8013. /* Potential stall - if we see that the flip has happened,
  8014. * assume a missed interrupt. */
  8015. if (INTEL_INFO(dev)->gen >= 4)
  8016. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  8017. else
  8018. addr = I915_READ(DSPADDR(intel_crtc->plane));
  8019. /* There is a potential issue here with a false positive after a flip
  8020. * to the same address. We could address this by checking for a
  8021. * non-incrementing frame counter.
  8022. */
  8023. return addr == work->gtt_offset;
  8024. }
  8025. void intel_check_page_flip(struct drm_device *dev, int pipe)
  8026. {
  8027. struct drm_i915_private *dev_priv = dev->dev_private;
  8028. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  8029. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8030. WARN_ON(!in_irq());
  8031. if (crtc == NULL)
  8032. return;
  8033. spin_lock(&dev->event_lock);
  8034. if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
  8035. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  8036. intel_crtc->unpin_work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  8037. page_flip_completed(intel_crtc);
  8038. }
  8039. spin_unlock(&dev->event_lock);
  8040. }
  8041. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  8042. struct drm_framebuffer *fb,
  8043. struct drm_pending_vblank_event *event,
  8044. uint32_t page_flip_flags)
  8045. {
  8046. struct drm_device *dev = crtc->dev;
  8047. struct drm_i915_private *dev_priv = dev->dev_private;
  8048. struct drm_framebuffer *old_fb = crtc->primary->fb;
  8049. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  8050. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8051. struct drm_plane *primary = crtc->primary;
  8052. struct intel_plane *intel_plane = to_intel_plane(primary);
  8053. enum pipe pipe = intel_crtc->pipe;
  8054. struct intel_unpin_work *work;
  8055. struct intel_engine_cs *ring;
  8056. int ret;
  8057. /*
  8058. * drm_mode_page_flip_ioctl() should already catch this, but double
  8059. * check to be safe. In the future we may enable pageflipping from
  8060. * a disabled primary plane.
  8061. */
  8062. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  8063. return -EBUSY;
  8064. /* Can't change pixel format via MI display flips. */
  8065. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  8066. return -EINVAL;
  8067. /*
  8068. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  8069. * Note that pitch changes could also affect these register.
  8070. */
  8071. if (INTEL_INFO(dev)->gen > 3 &&
  8072. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  8073. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  8074. return -EINVAL;
  8075. if (i915_terminally_wedged(&dev_priv->gpu_error))
  8076. goto out_hang;
  8077. work = kzalloc(sizeof(*work), GFP_KERNEL);
  8078. if (work == NULL)
  8079. return -ENOMEM;
  8080. work->event = event;
  8081. work->crtc = crtc;
  8082. work->old_fb_obj = intel_fb_obj(old_fb);
  8083. INIT_WORK(&work->work, intel_unpin_work_fn);
  8084. ret = drm_crtc_vblank_get(crtc);
  8085. if (ret)
  8086. goto free_work;
  8087. /* We borrow the event spin lock for protecting unpin_work */
  8088. spin_lock_irq(&dev->event_lock);
  8089. if (intel_crtc->unpin_work) {
  8090. /* Before declaring the flip queue wedged, check if
  8091. * the hardware completed the operation behind our backs.
  8092. */
  8093. if (__intel_pageflip_stall_check(dev, crtc)) {
  8094. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  8095. page_flip_completed(intel_crtc);
  8096. } else {
  8097. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  8098. spin_unlock_irq(&dev->event_lock);
  8099. drm_crtc_vblank_put(crtc);
  8100. kfree(work);
  8101. return -EBUSY;
  8102. }
  8103. }
  8104. intel_crtc->unpin_work = work;
  8105. spin_unlock_irq(&dev->event_lock);
  8106. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  8107. flush_workqueue(dev_priv->wq);
  8108. ret = i915_mutex_lock_interruptible(dev);
  8109. if (ret)
  8110. goto cleanup;
  8111. /* Reference the objects for the scheduled work. */
  8112. drm_gem_object_reference(&work->old_fb_obj->base);
  8113. drm_gem_object_reference(&obj->base);
  8114. crtc->primary->fb = fb;
  8115. work->pending_flip_obj = obj;
  8116. atomic_inc(&intel_crtc->unpin_work_count);
  8117. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  8118. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  8119. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  8120. if (IS_VALLEYVIEW(dev)) {
  8121. ring = &dev_priv->ring[BCS];
  8122. if (obj->tiling_mode != work->old_fb_obj->tiling_mode)
  8123. /* vlv: DISPLAY_FLIP fails to change tiling */
  8124. ring = NULL;
  8125. } else if (IS_IVYBRIDGE(dev)) {
  8126. ring = &dev_priv->ring[BCS];
  8127. } else if (INTEL_INFO(dev)->gen >= 7) {
  8128. ring = i915_gem_request_get_ring(obj->last_read_req);
  8129. if (ring == NULL || ring->id != RCS)
  8130. ring = &dev_priv->ring[BCS];
  8131. } else {
  8132. ring = &dev_priv->ring[RCS];
  8133. }
  8134. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb, ring);
  8135. if (ret)
  8136. goto cleanup_pending;
  8137. work->gtt_offset =
  8138. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
  8139. if (use_mmio_flip(ring, obj)) {
  8140. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  8141. page_flip_flags);
  8142. if (ret)
  8143. goto cleanup_unpin;
  8144. i915_gem_request_assign(&work->flip_queued_req,
  8145. obj->last_write_req);
  8146. } else {
  8147. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
  8148. page_flip_flags);
  8149. if (ret)
  8150. goto cleanup_unpin;
  8151. i915_gem_request_assign(&work->flip_queued_req,
  8152. intel_ring_get_request(ring));
  8153. }
  8154. work->flip_queued_vblank = drm_vblank_count(dev, intel_crtc->pipe);
  8155. work->enable_stall_check = true;
  8156. i915_gem_track_fb(work->old_fb_obj, obj,
  8157. INTEL_FRONTBUFFER_PRIMARY(pipe));
  8158. intel_fbc_disable(dev);
  8159. intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  8160. mutex_unlock(&dev->struct_mutex);
  8161. trace_i915_flip_request(intel_crtc->plane, obj);
  8162. return 0;
  8163. cleanup_unpin:
  8164. intel_unpin_fb_obj(obj);
  8165. cleanup_pending:
  8166. atomic_dec(&intel_crtc->unpin_work_count);
  8167. crtc->primary->fb = old_fb;
  8168. drm_gem_object_unreference(&work->old_fb_obj->base);
  8169. drm_gem_object_unreference(&obj->base);
  8170. mutex_unlock(&dev->struct_mutex);
  8171. cleanup:
  8172. spin_lock_irq(&dev->event_lock);
  8173. intel_crtc->unpin_work = NULL;
  8174. spin_unlock_irq(&dev->event_lock);
  8175. drm_crtc_vblank_put(crtc);
  8176. free_work:
  8177. kfree(work);
  8178. if (ret == -EIO) {
  8179. out_hang:
  8180. ret = primary->funcs->update_plane(primary, crtc, fb,
  8181. intel_plane->crtc_x,
  8182. intel_plane->crtc_y,
  8183. intel_plane->crtc_h,
  8184. intel_plane->crtc_w,
  8185. intel_plane->src_x,
  8186. intel_plane->src_y,
  8187. intel_plane->src_h,
  8188. intel_plane->src_w);
  8189. if (ret == 0 && event) {
  8190. spin_lock_irq(&dev->event_lock);
  8191. drm_send_vblank_event(dev, pipe, event);
  8192. spin_unlock_irq(&dev->event_lock);
  8193. }
  8194. }
  8195. return ret;
  8196. }
  8197. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  8198. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  8199. .load_lut = intel_crtc_load_lut,
  8200. };
  8201. /**
  8202. * intel_modeset_update_staged_output_state
  8203. *
  8204. * Updates the staged output configuration state, e.g. after we've read out the
  8205. * current hw state.
  8206. */
  8207. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  8208. {
  8209. struct intel_crtc *crtc;
  8210. struct intel_encoder *encoder;
  8211. struct intel_connector *connector;
  8212. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8213. base.head) {
  8214. connector->new_encoder =
  8215. to_intel_encoder(connector->base.encoder);
  8216. }
  8217. for_each_intel_encoder(dev, encoder) {
  8218. encoder->new_crtc =
  8219. to_intel_crtc(encoder->base.crtc);
  8220. }
  8221. for_each_intel_crtc(dev, crtc) {
  8222. crtc->new_enabled = crtc->base.enabled;
  8223. if (crtc->new_enabled)
  8224. crtc->new_config = &crtc->config;
  8225. else
  8226. crtc->new_config = NULL;
  8227. }
  8228. }
  8229. /**
  8230. * intel_modeset_commit_output_state
  8231. *
  8232. * This function copies the stage display pipe configuration to the real one.
  8233. */
  8234. static void intel_modeset_commit_output_state(struct drm_device *dev)
  8235. {
  8236. struct intel_crtc *crtc;
  8237. struct intel_encoder *encoder;
  8238. struct intel_connector *connector;
  8239. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8240. base.head) {
  8241. connector->base.encoder = &connector->new_encoder->base;
  8242. }
  8243. for_each_intel_encoder(dev, encoder) {
  8244. encoder->base.crtc = &encoder->new_crtc->base;
  8245. }
  8246. for_each_intel_crtc(dev, crtc) {
  8247. crtc->base.enabled = crtc->new_enabled;
  8248. }
  8249. }
  8250. static void
  8251. connected_sink_compute_bpp(struct intel_connector *connector,
  8252. struct intel_crtc_config *pipe_config)
  8253. {
  8254. int bpp = pipe_config->pipe_bpp;
  8255. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8256. connector->base.base.id,
  8257. connector->base.name);
  8258. /* Don't use an invalid EDID bpc value */
  8259. if (connector->base.display_info.bpc &&
  8260. connector->base.display_info.bpc * 3 < bpp) {
  8261. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8262. bpp, connector->base.display_info.bpc*3);
  8263. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  8264. }
  8265. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8266. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  8267. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8268. bpp);
  8269. pipe_config->pipe_bpp = 24;
  8270. }
  8271. }
  8272. static int
  8273. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8274. struct drm_framebuffer *fb,
  8275. struct intel_crtc_config *pipe_config)
  8276. {
  8277. struct drm_device *dev = crtc->base.dev;
  8278. struct intel_connector *connector;
  8279. int bpp;
  8280. switch (fb->pixel_format) {
  8281. case DRM_FORMAT_C8:
  8282. bpp = 8*3; /* since we go through a colormap */
  8283. break;
  8284. case DRM_FORMAT_XRGB1555:
  8285. case DRM_FORMAT_ARGB1555:
  8286. /* checked in intel_framebuffer_init already */
  8287. if (WARN_ON(INTEL_INFO(dev)->gen > 3))
  8288. return -EINVAL;
  8289. case DRM_FORMAT_RGB565:
  8290. bpp = 6*3; /* min is 18bpp */
  8291. break;
  8292. case DRM_FORMAT_XBGR8888:
  8293. case DRM_FORMAT_ABGR8888:
  8294. /* checked in intel_framebuffer_init already */
  8295. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8296. return -EINVAL;
  8297. case DRM_FORMAT_XRGB8888:
  8298. case DRM_FORMAT_ARGB8888:
  8299. bpp = 8*3;
  8300. break;
  8301. case DRM_FORMAT_XRGB2101010:
  8302. case DRM_FORMAT_ARGB2101010:
  8303. case DRM_FORMAT_XBGR2101010:
  8304. case DRM_FORMAT_ABGR2101010:
  8305. /* checked in intel_framebuffer_init already */
  8306. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8307. return -EINVAL;
  8308. bpp = 10*3;
  8309. break;
  8310. /* TODO: gen4+ supports 16 bpc floating point, too. */
  8311. default:
  8312. DRM_DEBUG_KMS("unsupported depth\n");
  8313. return -EINVAL;
  8314. }
  8315. pipe_config->pipe_bpp = bpp;
  8316. /* Clamp display bpp to EDID value */
  8317. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8318. base.head) {
  8319. if (!connector->new_encoder ||
  8320. connector->new_encoder->new_crtc != crtc)
  8321. continue;
  8322. connected_sink_compute_bpp(connector, pipe_config);
  8323. }
  8324. return bpp;
  8325. }
  8326. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  8327. {
  8328. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  8329. "type: 0x%x flags: 0x%x\n",
  8330. mode->crtc_clock,
  8331. mode->crtc_hdisplay, mode->crtc_hsync_start,
  8332. mode->crtc_hsync_end, mode->crtc_htotal,
  8333. mode->crtc_vdisplay, mode->crtc_vsync_start,
  8334. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  8335. }
  8336. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  8337. struct intel_crtc_config *pipe_config,
  8338. const char *context)
  8339. {
  8340. DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
  8341. context, pipe_name(crtc->pipe));
  8342. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  8343. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  8344. pipe_config->pipe_bpp, pipe_config->dither);
  8345. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8346. pipe_config->has_pch_encoder,
  8347. pipe_config->fdi_lanes,
  8348. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  8349. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  8350. pipe_config->fdi_m_n.tu);
  8351. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8352. pipe_config->has_dp_encoder,
  8353. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  8354. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  8355. pipe_config->dp_m_n.tu);
  8356. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  8357. pipe_config->has_dp_encoder,
  8358. pipe_config->dp_m2_n2.gmch_m,
  8359. pipe_config->dp_m2_n2.gmch_n,
  8360. pipe_config->dp_m2_n2.link_m,
  8361. pipe_config->dp_m2_n2.link_n,
  8362. pipe_config->dp_m2_n2.tu);
  8363. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  8364. pipe_config->has_audio,
  8365. pipe_config->has_infoframe);
  8366. DRM_DEBUG_KMS("requested mode:\n");
  8367. drm_mode_debug_printmodeline(&pipe_config->requested_mode);
  8368. DRM_DEBUG_KMS("adjusted mode:\n");
  8369. drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
  8370. intel_dump_crtc_timings(&pipe_config->adjusted_mode);
  8371. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  8372. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  8373. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  8374. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  8375. pipe_config->gmch_pfit.control,
  8376. pipe_config->gmch_pfit.pgm_ratios,
  8377. pipe_config->gmch_pfit.lvds_border_bits);
  8378. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  8379. pipe_config->pch_pfit.pos,
  8380. pipe_config->pch_pfit.size,
  8381. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  8382. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  8383. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  8384. }
  8385. static bool encoders_cloneable(const struct intel_encoder *a,
  8386. const struct intel_encoder *b)
  8387. {
  8388. /* masks could be asymmetric, so check both ways */
  8389. return a == b || (a->cloneable & (1 << b->type) &&
  8390. b->cloneable & (1 << a->type));
  8391. }
  8392. static bool check_single_encoder_cloning(struct intel_crtc *crtc,
  8393. struct intel_encoder *encoder)
  8394. {
  8395. struct drm_device *dev = crtc->base.dev;
  8396. struct intel_encoder *source_encoder;
  8397. for_each_intel_encoder(dev, source_encoder) {
  8398. if (source_encoder->new_crtc != crtc)
  8399. continue;
  8400. if (!encoders_cloneable(encoder, source_encoder))
  8401. return false;
  8402. }
  8403. return true;
  8404. }
  8405. static bool check_encoder_cloning(struct intel_crtc *crtc)
  8406. {
  8407. struct drm_device *dev = crtc->base.dev;
  8408. struct intel_encoder *encoder;
  8409. for_each_intel_encoder(dev, encoder) {
  8410. if (encoder->new_crtc != crtc)
  8411. continue;
  8412. if (!check_single_encoder_cloning(crtc, encoder))
  8413. return false;
  8414. }
  8415. return true;
  8416. }
  8417. static bool check_digital_port_conflicts(struct drm_device *dev)
  8418. {
  8419. struct intel_connector *connector;
  8420. unsigned int used_ports = 0;
  8421. /*
  8422. * Walk the connector list instead of the encoder
  8423. * list to detect the problem on ddi platforms
  8424. * where there's just one encoder per digital port.
  8425. */
  8426. list_for_each_entry(connector,
  8427. &dev->mode_config.connector_list, base.head) {
  8428. struct intel_encoder *encoder = connector->new_encoder;
  8429. if (!encoder)
  8430. continue;
  8431. WARN_ON(!encoder->new_crtc);
  8432. switch (encoder->type) {
  8433. unsigned int port_mask;
  8434. case INTEL_OUTPUT_UNKNOWN:
  8435. if (WARN_ON(!HAS_DDI(dev)))
  8436. break;
  8437. case INTEL_OUTPUT_DISPLAYPORT:
  8438. case INTEL_OUTPUT_HDMI:
  8439. case INTEL_OUTPUT_EDP:
  8440. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  8441. /* the same port mustn't appear more than once */
  8442. if (used_ports & port_mask)
  8443. return false;
  8444. used_ports |= port_mask;
  8445. default:
  8446. break;
  8447. }
  8448. }
  8449. return true;
  8450. }
  8451. static struct intel_crtc_config *
  8452. intel_modeset_pipe_config(struct drm_crtc *crtc,
  8453. struct drm_framebuffer *fb,
  8454. struct drm_display_mode *mode)
  8455. {
  8456. struct drm_device *dev = crtc->dev;
  8457. struct intel_encoder *encoder;
  8458. struct intel_crtc_config *pipe_config;
  8459. int plane_bpp, ret = -EINVAL;
  8460. bool retry = true;
  8461. if (!check_encoder_cloning(to_intel_crtc(crtc))) {
  8462. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  8463. return ERR_PTR(-EINVAL);
  8464. }
  8465. if (!check_digital_port_conflicts(dev)) {
  8466. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  8467. return ERR_PTR(-EINVAL);
  8468. }
  8469. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8470. if (!pipe_config)
  8471. return ERR_PTR(-ENOMEM);
  8472. drm_mode_copy(&pipe_config->adjusted_mode, mode);
  8473. drm_mode_copy(&pipe_config->requested_mode, mode);
  8474. pipe_config->cpu_transcoder =
  8475. (enum transcoder) to_intel_crtc(crtc)->pipe;
  8476. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8477. /*
  8478. * Sanitize sync polarity flags based on requested ones. If neither
  8479. * positive or negative polarity is requested, treat this as meaning
  8480. * negative polarity.
  8481. */
  8482. if (!(pipe_config->adjusted_mode.flags &
  8483. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  8484. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  8485. if (!(pipe_config->adjusted_mode.flags &
  8486. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  8487. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  8488. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  8489. * plane pixel format and any sink constraints into account. Returns the
  8490. * source plane bpp so that dithering can be selected on mismatches
  8491. * after encoders and crtc also have had their say. */
  8492. plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  8493. fb, pipe_config);
  8494. if (plane_bpp < 0)
  8495. goto fail;
  8496. /*
  8497. * Determine the real pipe dimensions. Note that stereo modes can
  8498. * increase the actual pipe size due to the frame doubling and
  8499. * insertion of additional space for blanks between the frame. This
  8500. * is stored in the crtc timings. We use the requested mode to do this
  8501. * computation to clearly distinguish it from the adjusted mode, which
  8502. * can be changed by the connectors in the below retry loop.
  8503. */
  8504. drm_crtc_get_hv_timing(&pipe_config->requested_mode,
  8505. &pipe_config->pipe_src_w,
  8506. &pipe_config->pipe_src_h);
  8507. encoder_retry:
  8508. /* Ensure the port clock defaults are reset when retrying. */
  8509. pipe_config->port_clock = 0;
  8510. pipe_config->pixel_multiplier = 1;
  8511. /* Fill in default crtc timings, allow encoders to overwrite them. */
  8512. drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
  8513. /* Pass our mode to the connectors and the CRTC to give them a chance to
  8514. * adjust it according to limitations or connector properties, and also
  8515. * a chance to reject the mode entirely.
  8516. */
  8517. for_each_intel_encoder(dev, encoder) {
  8518. if (&encoder->new_crtc->base != crtc)
  8519. continue;
  8520. if (!(encoder->compute_config(encoder, pipe_config))) {
  8521. DRM_DEBUG_KMS("Encoder config failure\n");
  8522. goto fail;
  8523. }
  8524. }
  8525. /* Set default port clock if not overwritten by the encoder. Needs to be
  8526. * done afterwards in case the encoder adjusts the mode. */
  8527. if (!pipe_config->port_clock)
  8528. pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
  8529. * pipe_config->pixel_multiplier;
  8530. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  8531. if (ret < 0) {
  8532. DRM_DEBUG_KMS("CRTC fixup failed\n");
  8533. goto fail;
  8534. }
  8535. if (ret == RETRY) {
  8536. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  8537. ret = -EINVAL;
  8538. goto fail;
  8539. }
  8540. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  8541. retry = false;
  8542. goto encoder_retry;
  8543. }
  8544. pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
  8545. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  8546. plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  8547. return pipe_config;
  8548. fail:
  8549. kfree(pipe_config);
  8550. return ERR_PTR(ret);
  8551. }
  8552. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  8553. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  8554. static void
  8555. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  8556. unsigned *prepare_pipes, unsigned *disable_pipes)
  8557. {
  8558. struct intel_crtc *intel_crtc;
  8559. struct drm_device *dev = crtc->dev;
  8560. struct intel_encoder *encoder;
  8561. struct intel_connector *connector;
  8562. struct drm_crtc *tmp_crtc;
  8563. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  8564. /* Check which crtcs have changed outputs connected to them, these need
  8565. * to be part of the prepare_pipes mask. We don't (yet) support global
  8566. * modeset across multiple crtcs, so modeset_pipes will only have one
  8567. * bit set at most. */
  8568. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8569. base.head) {
  8570. if (connector->base.encoder == &connector->new_encoder->base)
  8571. continue;
  8572. if (connector->base.encoder) {
  8573. tmp_crtc = connector->base.encoder->crtc;
  8574. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8575. }
  8576. if (connector->new_encoder)
  8577. *prepare_pipes |=
  8578. 1 << connector->new_encoder->new_crtc->pipe;
  8579. }
  8580. for_each_intel_encoder(dev, encoder) {
  8581. if (encoder->base.crtc == &encoder->new_crtc->base)
  8582. continue;
  8583. if (encoder->base.crtc) {
  8584. tmp_crtc = encoder->base.crtc;
  8585. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8586. }
  8587. if (encoder->new_crtc)
  8588. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  8589. }
  8590. /* Check for pipes that will be enabled/disabled ... */
  8591. for_each_intel_crtc(dev, intel_crtc) {
  8592. if (intel_crtc->base.enabled == intel_crtc->new_enabled)
  8593. continue;
  8594. if (!intel_crtc->new_enabled)
  8595. *disable_pipes |= 1 << intel_crtc->pipe;
  8596. else
  8597. *prepare_pipes |= 1 << intel_crtc->pipe;
  8598. }
  8599. /* set_mode is also used to update properties on life display pipes. */
  8600. intel_crtc = to_intel_crtc(crtc);
  8601. if (intel_crtc->new_enabled)
  8602. *prepare_pipes |= 1 << intel_crtc->pipe;
  8603. /*
  8604. * For simplicity do a full modeset on any pipe where the output routing
  8605. * changed. We could be more clever, but that would require us to be
  8606. * more careful with calling the relevant encoder->mode_set functions.
  8607. */
  8608. if (*prepare_pipes)
  8609. *modeset_pipes = *prepare_pipes;
  8610. /* ... and mask these out. */
  8611. *modeset_pipes &= ~(*disable_pipes);
  8612. *prepare_pipes &= ~(*disable_pipes);
  8613. /*
  8614. * HACK: We don't (yet) fully support global modesets. intel_set_config
  8615. * obies this rule, but the modeset restore mode of
  8616. * intel_modeset_setup_hw_state does not.
  8617. */
  8618. *modeset_pipes &= 1 << intel_crtc->pipe;
  8619. *prepare_pipes &= 1 << intel_crtc->pipe;
  8620. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  8621. *modeset_pipes, *prepare_pipes, *disable_pipes);
  8622. }
  8623. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  8624. {
  8625. struct drm_encoder *encoder;
  8626. struct drm_device *dev = crtc->dev;
  8627. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  8628. if (encoder->crtc == crtc)
  8629. return true;
  8630. return false;
  8631. }
  8632. static void
  8633. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  8634. {
  8635. struct drm_i915_private *dev_priv = dev->dev_private;
  8636. struct intel_encoder *intel_encoder;
  8637. struct intel_crtc *intel_crtc;
  8638. struct drm_connector *connector;
  8639. intel_shared_dpll_commit(dev_priv);
  8640. for_each_intel_encoder(dev, intel_encoder) {
  8641. if (!intel_encoder->base.crtc)
  8642. continue;
  8643. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  8644. if (prepare_pipes & (1 << intel_crtc->pipe))
  8645. intel_encoder->connectors_active = false;
  8646. }
  8647. intel_modeset_commit_output_state(dev);
  8648. /* Double check state. */
  8649. for_each_intel_crtc(dev, intel_crtc) {
  8650. WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
  8651. WARN_ON(intel_crtc->new_config &&
  8652. intel_crtc->new_config != &intel_crtc->config);
  8653. WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
  8654. }
  8655. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  8656. if (!connector->encoder || !connector->encoder->crtc)
  8657. continue;
  8658. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  8659. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  8660. struct drm_property *dpms_property =
  8661. dev->mode_config.dpms_property;
  8662. connector->dpms = DRM_MODE_DPMS_ON;
  8663. drm_object_property_set_value(&connector->base,
  8664. dpms_property,
  8665. DRM_MODE_DPMS_ON);
  8666. intel_encoder = to_intel_encoder(connector->encoder);
  8667. intel_encoder->connectors_active = true;
  8668. }
  8669. }
  8670. }
  8671. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  8672. {
  8673. int diff;
  8674. if (clock1 == clock2)
  8675. return true;
  8676. if (!clock1 || !clock2)
  8677. return false;
  8678. diff = abs(clock1 - clock2);
  8679. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  8680. return true;
  8681. return false;
  8682. }
  8683. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  8684. list_for_each_entry((intel_crtc), \
  8685. &(dev)->mode_config.crtc_list, \
  8686. base.head) \
  8687. if (mask & (1 <<(intel_crtc)->pipe))
  8688. static bool
  8689. intel_pipe_config_compare(struct drm_device *dev,
  8690. struct intel_crtc_config *current_config,
  8691. struct intel_crtc_config *pipe_config)
  8692. {
  8693. #define PIPE_CONF_CHECK_X(name) \
  8694. if (current_config->name != pipe_config->name) { \
  8695. DRM_ERROR("mismatch in " #name " " \
  8696. "(expected 0x%08x, found 0x%08x)\n", \
  8697. current_config->name, \
  8698. pipe_config->name); \
  8699. return false; \
  8700. }
  8701. #define PIPE_CONF_CHECK_I(name) \
  8702. if (current_config->name != pipe_config->name) { \
  8703. DRM_ERROR("mismatch in " #name " " \
  8704. "(expected %i, found %i)\n", \
  8705. current_config->name, \
  8706. pipe_config->name); \
  8707. return false; \
  8708. }
  8709. /* This is required for BDW+ where there is only one set of registers for
  8710. * switching between high and low RR.
  8711. * This macro can be used whenever a comparison has to be made between one
  8712. * hw state and multiple sw state variables.
  8713. */
  8714. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  8715. if ((current_config->name != pipe_config->name) && \
  8716. (current_config->alt_name != pipe_config->name)) { \
  8717. DRM_ERROR("mismatch in " #name " " \
  8718. "(expected %i or %i, found %i)\n", \
  8719. current_config->name, \
  8720. current_config->alt_name, \
  8721. pipe_config->name); \
  8722. return false; \
  8723. }
  8724. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  8725. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  8726. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  8727. "(expected %i, found %i)\n", \
  8728. current_config->name & (mask), \
  8729. pipe_config->name & (mask)); \
  8730. return false; \
  8731. }
  8732. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  8733. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  8734. DRM_ERROR("mismatch in " #name " " \
  8735. "(expected %i, found %i)\n", \
  8736. current_config->name, \
  8737. pipe_config->name); \
  8738. return false; \
  8739. }
  8740. #define PIPE_CONF_QUIRK(quirk) \
  8741. ((current_config->quirks | pipe_config->quirks) & (quirk))
  8742. PIPE_CONF_CHECK_I(cpu_transcoder);
  8743. PIPE_CONF_CHECK_I(has_pch_encoder);
  8744. PIPE_CONF_CHECK_I(fdi_lanes);
  8745. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  8746. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  8747. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  8748. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  8749. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  8750. PIPE_CONF_CHECK_I(has_dp_encoder);
  8751. if (INTEL_INFO(dev)->gen < 8) {
  8752. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  8753. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  8754. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  8755. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  8756. PIPE_CONF_CHECK_I(dp_m_n.tu);
  8757. if (current_config->has_drrs) {
  8758. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
  8759. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
  8760. PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
  8761. PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
  8762. PIPE_CONF_CHECK_I(dp_m2_n2.tu);
  8763. }
  8764. } else {
  8765. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
  8766. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
  8767. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
  8768. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
  8769. PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
  8770. }
  8771. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
  8772. PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
  8773. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
  8774. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
  8775. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
  8776. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
  8777. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
  8778. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
  8779. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
  8780. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
  8781. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
  8782. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
  8783. PIPE_CONF_CHECK_I(pixel_multiplier);
  8784. PIPE_CONF_CHECK_I(has_hdmi_sink);
  8785. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  8786. IS_VALLEYVIEW(dev))
  8787. PIPE_CONF_CHECK_I(limited_color_range);
  8788. PIPE_CONF_CHECK_I(has_infoframe);
  8789. PIPE_CONF_CHECK_I(has_audio);
  8790. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8791. DRM_MODE_FLAG_INTERLACE);
  8792. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  8793. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8794. DRM_MODE_FLAG_PHSYNC);
  8795. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8796. DRM_MODE_FLAG_NHSYNC);
  8797. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8798. DRM_MODE_FLAG_PVSYNC);
  8799. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8800. DRM_MODE_FLAG_NVSYNC);
  8801. }
  8802. PIPE_CONF_CHECK_I(pipe_src_w);
  8803. PIPE_CONF_CHECK_I(pipe_src_h);
  8804. /*
  8805. * FIXME: BIOS likes to set up a cloned config with lvds+external
  8806. * screen. Since we don't yet re-compute the pipe config when moving
  8807. * just the lvds port away to another pipe the sw tracking won't match.
  8808. *
  8809. * Proper atomic modesets with recomputed global state will fix this.
  8810. * Until then just don't check gmch state for inherited modes.
  8811. */
  8812. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  8813. PIPE_CONF_CHECK_I(gmch_pfit.control);
  8814. /* pfit ratios are autocomputed by the hw on gen4+ */
  8815. if (INTEL_INFO(dev)->gen < 4)
  8816. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  8817. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  8818. }
  8819. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  8820. if (current_config->pch_pfit.enabled) {
  8821. PIPE_CONF_CHECK_I(pch_pfit.pos);
  8822. PIPE_CONF_CHECK_I(pch_pfit.size);
  8823. }
  8824. /* BDW+ don't expose a synchronous way to read the state */
  8825. if (IS_HASWELL(dev))
  8826. PIPE_CONF_CHECK_I(ips_enabled);
  8827. PIPE_CONF_CHECK_I(double_wide);
  8828. PIPE_CONF_CHECK_X(ddi_pll_sel);
  8829. PIPE_CONF_CHECK_I(shared_dpll);
  8830. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  8831. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  8832. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  8833. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  8834. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  8835. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  8836. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  8837. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  8838. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  8839. PIPE_CONF_CHECK_I(pipe_bpp);
  8840. PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
  8841. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  8842. #undef PIPE_CONF_CHECK_X
  8843. #undef PIPE_CONF_CHECK_I
  8844. #undef PIPE_CONF_CHECK_I_ALT
  8845. #undef PIPE_CONF_CHECK_FLAGS
  8846. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  8847. #undef PIPE_CONF_QUIRK
  8848. return true;
  8849. }
  8850. static void check_wm_state(struct drm_device *dev)
  8851. {
  8852. struct drm_i915_private *dev_priv = dev->dev_private;
  8853. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  8854. struct intel_crtc *intel_crtc;
  8855. int plane;
  8856. if (INTEL_INFO(dev)->gen < 9)
  8857. return;
  8858. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  8859. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  8860. for_each_intel_crtc(dev, intel_crtc) {
  8861. struct skl_ddb_entry *hw_entry, *sw_entry;
  8862. const enum pipe pipe = intel_crtc->pipe;
  8863. if (!intel_crtc->active)
  8864. continue;
  8865. /* planes */
  8866. for_each_plane(pipe, plane) {
  8867. hw_entry = &hw_ddb.plane[pipe][plane];
  8868. sw_entry = &sw_ddb->plane[pipe][plane];
  8869. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  8870. continue;
  8871. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  8872. "(expected (%u,%u), found (%u,%u))\n",
  8873. pipe_name(pipe), plane + 1,
  8874. sw_entry->start, sw_entry->end,
  8875. hw_entry->start, hw_entry->end);
  8876. }
  8877. /* cursor */
  8878. hw_entry = &hw_ddb.cursor[pipe];
  8879. sw_entry = &sw_ddb->cursor[pipe];
  8880. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  8881. continue;
  8882. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  8883. "(expected (%u,%u), found (%u,%u))\n",
  8884. pipe_name(pipe),
  8885. sw_entry->start, sw_entry->end,
  8886. hw_entry->start, hw_entry->end);
  8887. }
  8888. }
  8889. static void
  8890. check_connector_state(struct drm_device *dev)
  8891. {
  8892. struct intel_connector *connector;
  8893. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8894. base.head) {
  8895. /* This also checks the encoder/connector hw state with the
  8896. * ->get_hw_state callbacks. */
  8897. intel_connector_check_state(connector);
  8898. I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
  8899. "connector's staged encoder doesn't match current encoder\n");
  8900. }
  8901. }
  8902. static void
  8903. check_encoder_state(struct drm_device *dev)
  8904. {
  8905. struct intel_encoder *encoder;
  8906. struct intel_connector *connector;
  8907. for_each_intel_encoder(dev, encoder) {
  8908. bool enabled = false;
  8909. bool active = false;
  8910. enum pipe pipe, tracked_pipe;
  8911. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  8912. encoder->base.base.id,
  8913. encoder->base.name);
  8914. I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
  8915. "encoder's stage crtc doesn't match current crtc\n");
  8916. I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
  8917. "encoder's active_connectors set, but no crtc\n");
  8918. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8919. base.head) {
  8920. if (connector->base.encoder != &encoder->base)
  8921. continue;
  8922. enabled = true;
  8923. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  8924. active = true;
  8925. }
  8926. /*
  8927. * for MST connectors if we unplug the connector is gone
  8928. * away but the encoder is still connected to a crtc
  8929. * until a modeset happens in response to the hotplug.
  8930. */
  8931. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  8932. continue;
  8933. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  8934. "encoder's enabled state mismatch "
  8935. "(expected %i, found %i)\n",
  8936. !!encoder->base.crtc, enabled);
  8937. I915_STATE_WARN(active && !encoder->base.crtc,
  8938. "active encoder with no crtc\n");
  8939. I915_STATE_WARN(encoder->connectors_active != active,
  8940. "encoder's computed active state doesn't match tracked active state "
  8941. "(expected %i, found %i)\n", active, encoder->connectors_active);
  8942. active = encoder->get_hw_state(encoder, &pipe);
  8943. I915_STATE_WARN(active != encoder->connectors_active,
  8944. "encoder's hw state doesn't match sw tracking "
  8945. "(expected %i, found %i)\n",
  8946. encoder->connectors_active, active);
  8947. if (!encoder->base.crtc)
  8948. continue;
  8949. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  8950. I915_STATE_WARN(active && pipe != tracked_pipe,
  8951. "active encoder's pipe doesn't match"
  8952. "(expected %i, found %i)\n",
  8953. tracked_pipe, pipe);
  8954. }
  8955. }
  8956. static void
  8957. check_crtc_state(struct drm_device *dev)
  8958. {
  8959. struct drm_i915_private *dev_priv = dev->dev_private;
  8960. struct intel_crtc *crtc;
  8961. struct intel_encoder *encoder;
  8962. struct intel_crtc_config pipe_config;
  8963. for_each_intel_crtc(dev, crtc) {
  8964. bool enabled = false;
  8965. bool active = false;
  8966. memset(&pipe_config, 0, sizeof(pipe_config));
  8967. DRM_DEBUG_KMS("[CRTC:%d]\n",
  8968. crtc->base.base.id);
  8969. I915_STATE_WARN(crtc->active && !crtc->base.enabled,
  8970. "active crtc, but not enabled in sw tracking\n");
  8971. for_each_intel_encoder(dev, encoder) {
  8972. if (encoder->base.crtc != &crtc->base)
  8973. continue;
  8974. enabled = true;
  8975. if (encoder->connectors_active)
  8976. active = true;
  8977. }
  8978. I915_STATE_WARN(active != crtc->active,
  8979. "crtc's computed active state doesn't match tracked active state "
  8980. "(expected %i, found %i)\n", active, crtc->active);
  8981. I915_STATE_WARN(enabled != crtc->base.enabled,
  8982. "crtc's computed enabled state doesn't match tracked enabled state "
  8983. "(expected %i, found %i)\n", enabled, crtc->base.enabled);
  8984. active = dev_priv->display.get_pipe_config(crtc,
  8985. &pipe_config);
  8986. /* hw state is inconsistent with the pipe quirk */
  8987. if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  8988. (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  8989. active = crtc->active;
  8990. for_each_intel_encoder(dev, encoder) {
  8991. enum pipe pipe;
  8992. if (encoder->base.crtc != &crtc->base)
  8993. continue;
  8994. if (encoder->get_hw_state(encoder, &pipe))
  8995. encoder->get_config(encoder, &pipe_config);
  8996. }
  8997. I915_STATE_WARN(crtc->active != active,
  8998. "crtc active state doesn't match with hw state "
  8999. "(expected %i, found %i)\n", crtc->active, active);
  9000. if (active &&
  9001. !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
  9002. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  9003. intel_dump_pipe_config(crtc, &pipe_config,
  9004. "[hw state]");
  9005. intel_dump_pipe_config(crtc, &crtc->config,
  9006. "[sw state]");
  9007. }
  9008. }
  9009. }
  9010. static void
  9011. check_shared_dpll_state(struct drm_device *dev)
  9012. {
  9013. struct drm_i915_private *dev_priv = dev->dev_private;
  9014. struct intel_crtc *crtc;
  9015. struct intel_dpll_hw_state dpll_hw_state;
  9016. int i;
  9017. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9018. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  9019. int enabled_crtcs = 0, active_crtcs = 0;
  9020. bool active;
  9021. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  9022. DRM_DEBUG_KMS("%s\n", pll->name);
  9023. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  9024. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  9025. "more active pll users than references: %i vs %i\n",
  9026. pll->active, hweight32(pll->config.crtc_mask));
  9027. I915_STATE_WARN(pll->active && !pll->on,
  9028. "pll in active use but not on in sw tracking\n");
  9029. I915_STATE_WARN(pll->on && !pll->active,
  9030. "pll in on but not on in use in sw tracking\n");
  9031. I915_STATE_WARN(pll->on != active,
  9032. "pll on state mismatch (expected %i, found %i)\n",
  9033. pll->on, active);
  9034. for_each_intel_crtc(dev, crtc) {
  9035. if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
  9036. enabled_crtcs++;
  9037. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  9038. active_crtcs++;
  9039. }
  9040. I915_STATE_WARN(pll->active != active_crtcs,
  9041. "pll active crtcs mismatch (expected %i, found %i)\n",
  9042. pll->active, active_crtcs);
  9043. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  9044. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  9045. hweight32(pll->config.crtc_mask), enabled_crtcs);
  9046. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  9047. sizeof(dpll_hw_state)),
  9048. "pll hw state mismatch\n");
  9049. }
  9050. }
  9051. void
  9052. intel_modeset_check_state(struct drm_device *dev)
  9053. {
  9054. check_wm_state(dev);
  9055. check_connector_state(dev);
  9056. check_encoder_state(dev);
  9057. check_crtc_state(dev);
  9058. check_shared_dpll_state(dev);
  9059. }
  9060. void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
  9061. int dotclock)
  9062. {
  9063. /*
  9064. * FDI already provided one idea for the dotclock.
  9065. * Yell if the encoder disagrees.
  9066. */
  9067. WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
  9068. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9069. pipe_config->adjusted_mode.crtc_clock, dotclock);
  9070. }
  9071. static void update_scanline_offset(struct intel_crtc *crtc)
  9072. {
  9073. struct drm_device *dev = crtc->base.dev;
  9074. /*
  9075. * The scanline counter increments at the leading edge of hsync.
  9076. *
  9077. * On most platforms it starts counting from vtotal-1 on the
  9078. * first active line. That means the scanline counter value is
  9079. * always one less than what we would expect. Ie. just after
  9080. * start of vblank, which also occurs at start of hsync (on the
  9081. * last active line), the scanline counter will read vblank_start-1.
  9082. *
  9083. * On gen2 the scanline counter starts counting from 1 instead
  9084. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  9085. * to keep the value positive), instead of adding one.
  9086. *
  9087. * On HSW+ the behaviour of the scanline counter depends on the output
  9088. * type. For DP ports it behaves like most other platforms, but on HDMI
  9089. * there's an extra 1 line difference. So we need to add two instead of
  9090. * one to the value.
  9091. */
  9092. if (IS_GEN2(dev)) {
  9093. const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
  9094. int vtotal;
  9095. vtotal = mode->crtc_vtotal;
  9096. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  9097. vtotal /= 2;
  9098. crtc->scanline_offset = vtotal - 1;
  9099. } else if (HAS_DDI(dev) &&
  9100. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  9101. crtc->scanline_offset = 2;
  9102. } else
  9103. crtc->scanline_offset = 1;
  9104. }
  9105. static struct intel_crtc_config *
  9106. intel_modeset_compute_config(struct drm_crtc *crtc,
  9107. struct drm_display_mode *mode,
  9108. struct drm_framebuffer *fb,
  9109. unsigned *modeset_pipes,
  9110. unsigned *prepare_pipes,
  9111. unsigned *disable_pipes)
  9112. {
  9113. struct intel_crtc_config *pipe_config = NULL;
  9114. intel_modeset_affected_pipes(crtc, modeset_pipes,
  9115. prepare_pipes, disable_pipes);
  9116. if ((*modeset_pipes) == 0)
  9117. goto out;
  9118. /*
  9119. * Note this needs changes when we start tracking multiple modes
  9120. * and crtcs. At that point we'll need to compute the whole config
  9121. * (i.e. one pipe_config for each crtc) rather than just the one
  9122. * for this crtc.
  9123. */
  9124. pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
  9125. if (IS_ERR(pipe_config)) {
  9126. goto out;
  9127. }
  9128. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  9129. "[modeset]");
  9130. out:
  9131. return pipe_config;
  9132. }
  9133. static int __intel_set_mode(struct drm_crtc *crtc,
  9134. struct drm_display_mode *mode,
  9135. int x, int y, struct drm_framebuffer *fb,
  9136. struct intel_crtc_config *pipe_config,
  9137. unsigned modeset_pipes,
  9138. unsigned prepare_pipes,
  9139. unsigned disable_pipes)
  9140. {
  9141. struct drm_device *dev = crtc->dev;
  9142. struct drm_i915_private *dev_priv = dev->dev_private;
  9143. struct drm_display_mode *saved_mode;
  9144. struct intel_crtc *intel_crtc;
  9145. int ret = 0;
  9146. saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
  9147. if (!saved_mode)
  9148. return -ENOMEM;
  9149. *saved_mode = crtc->mode;
  9150. if (modeset_pipes)
  9151. to_intel_crtc(crtc)->new_config = pipe_config;
  9152. /*
  9153. * See if the config requires any additional preparation, e.g.
  9154. * to adjust global state with pipes off. We need to do this
  9155. * here so we can get the modeset_pipe updated config for the new
  9156. * mode set on this crtc. For other crtcs we need to use the
  9157. * adjusted_mode bits in the crtc directly.
  9158. */
  9159. if (IS_VALLEYVIEW(dev)) {
  9160. valleyview_modeset_global_pipes(dev, &prepare_pipes);
  9161. /* may have added more to prepare_pipes than we should */
  9162. prepare_pipes &= ~disable_pipes;
  9163. }
  9164. if (dev_priv->display.crtc_compute_clock) {
  9165. unsigned clear_pipes = modeset_pipes | disable_pipes;
  9166. ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
  9167. if (ret)
  9168. goto done;
  9169. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9170. ret = dev_priv->display.crtc_compute_clock(intel_crtc);
  9171. if (ret) {
  9172. intel_shared_dpll_abort_config(dev_priv);
  9173. goto done;
  9174. }
  9175. }
  9176. }
  9177. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  9178. intel_crtc_disable(&intel_crtc->base);
  9179. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9180. if (intel_crtc->base.enabled)
  9181. dev_priv->display.crtc_disable(&intel_crtc->base);
  9182. }
  9183. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  9184. * to set it here already despite that we pass it down the callchain.
  9185. *
  9186. * Note we'll need to fix this up when we start tracking multiple
  9187. * pipes; here we assume a single modeset_pipe and only track the
  9188. * single crtc and mode.
  9189. */
  9190. if (modeset_pipes) {
  9191. crtc->mode = *mode;
  9192. /* mode_set/enable/disable functions rely on a correct pipe
  9193. * config. */
  9194. to_intel_crtc(crtc)->config = *pipe_config;
  9195. to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
  9196. /*
  9197. * Calculate and store various constants which
  9198. * are later needed by vblank and swap-completion
  9199. * timestamping. They are derived from true hwmode.
  9200. */
  9201. drm_calc_timestamping_constants(crtc,
  9202. &pipe_config->adjusted_mode);
  9203. }
  9204. /* Only after disabling all output pipelines that will be changed can we
  9205. * update the the output configuration. */
  9206. intel_modeset_update_state(dev, prepare_pipes);
  9207. modeset_update_crtc_power_domains(dev);
  9208. /* Set up the DPLL and any encoders state that needs to adjust or depend
  9209. * on the DPLL.
  9210. */
  9211. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9212. struct drm_plane *primary = intel_crtc->base.primary;
  9213. int vdisplay, hdisplay;
  9214. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  9215. ret = primary->funcs->update_plane(primary, &intel_crtc->base,
  9216. fb, 0, 0,
  9217. hdisplay, vdisplay,
  9218. x << 16, y << 16,
  9219. hdisplay << 16, vdisplay << 16);
  9220. }
  9221. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  9222. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9223. update_scanline_offset(intel_crtc);
  9224. dev_priv->display.crtc_enable(&intel_crtc->base);
  9225. }
  9226. /* FIXME: add subpixel order */
  9227. done:
  9228. if (ret && crtc->enabled)
  9229. crtc->mode = *saved_mode;
  9230. kfree(pipe_config);
  9231. kfree(saved_mode);
  9232. return ret;
  9233. }
  9234. static int intel_set_mode_pipes(struct drm_crtc *crtc,
  9235. struct drm_display_mode *mode,
  9236. int x, int y, struct drm_framebuffer *fb,
  9237. struct intel_crtc_config *pipe_config,
  9238. unsigned modeset_pipes,
  9239. unsigned prepare_pipes,
  9240. unsigned disable_pipes)
  9241. {
  9242. int ret;
  9243. ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
  9244. prepare_pipes, disable_pipes);
  9245. if (ret == 0)
  9246. intel_modeset_check_state(crtc->dev);
  9247. return ret;
  9248. }
  9249. static int intel_set_mode(struct drm_crtc *crtc,
  9250. struct drm_display_mode *mode,
  9251. int x, int y, struct drm_framebuffer *fb)
  9252. {
  9253. struct intel_crtc_config *pipe_config;
  9254. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  9255. pipe_config = intel_modeset_compute_config(crtc, mode, fb,
  9256. &modeset_pipes,
  9257. &prepare_pipes,
  9258. &disable_pipes);
  9259. if (IS_ERR(pipe_config))
  9260. return PTR_ERR(pipe_config);
  9261. return intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
  9262. modeset_pipes, prepare_pipes,
  9263. disable_pipes);
  9264. }
  9265. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  9266. {
  9267. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
  9268. }
  9269. #undef for_each_intel_crtc_masked
  9270. static void intel_set_config_free(struct intel_set_config *config)
  9271. {
  9272. if (!config)
  9273. return;
  9274. kfree(config->save_connector_encoders);
  9275. kfree(config->save_encoder_crtcs);
  9276. kfree(config->save_crtc_enabled);
  9277. kfree(config);
  9278. }
  9279. static int intel_set_config_save_state(struct drm_device *dev,
  9280. struct intel_set_config *config)
  9281. {
  9282. struct drm_crtc *crtc;
  9283. struct drm_encoder *encoder;
  9284. struct drm_connector *connector;
  9285. int count;
  9286. config->save_crtc_enabled =
  9287. kcalloc(dev->mode_config.num_crtc,
  9288. sizeof(bool), GFP_KERNEL);
  9289. if (!config->save_crtc_enabled)
  9290. return -ENOMEM;
  9291. config->save_encoder_crtcs =
  9292. kcalloc(dev->mode_config.num_encoder,
  9293. sizeof(struct drm_crtc *), GFP_KERNEL);
  9294. if (!config->save_encoder_crtcs)
  9295. return -ENOMEM;
  9296. config->save_connector_encoders =
  9297. kcalloc(dev->mode_config.num_connector,
  9298. sizeof(struct drm_encoder *), GFP_KERNEL);
  9299. if (!config->save_connector_encoders)
  9300. return -ENOMEM;
  9301. /* Copy data. Note that driver private data is not affected.
  9302. * Should anything bad happen only the expected state is
  9303. * restored, not the drivers personal bookkeeping.
  9304. */
  9305. count = 0;
  9306. for_each_crtc(dev, crtc) {
  9307. config->save_crtc_enabled[count++] = crtc->enabled;
  9308. }
  9309. count = 0;
  9310. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  9311. config->save_encoder_crtcs[count++] = encoder->crtc;
  9312. }
  9313. count = 0;
  9314. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  9315. config->save_connector_encoders[count++] = connector->encoder;
  9316. }
  9317. return 0;
  9318. }
  9319. static void intel_set_config_restore_state(struct drm_device *dev,
  9320. struct intel_set_config *config)
  9321. {
  9322. struct intel_crtc *crtc;
  9323. struct intel_encoder *encoder;
  9324. struct intel_connector *connector;
  9325. int count;
  9326. count = 0;
  9327. for_each_intel_crtc(dev, crtc) {
  9328. crtc->new_enabled = config->save_crtc_enabled[count++];
  9329. if (crtc->new_enabled)
  9330. crtc->new_config = &crtc->config;
  9331. else
  9332. crtc->new_config = NULL;
  9333. }
  9334. count = 0;
  9335. for_each_intel_encoder(dev, encoder) {
  9336. encoder->new_crtc =
  9337. to_intel_crtc(config->save_encoder_crtcs[count++]);
  9338. }
  9339. count = 0;
  9340. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  9341. connector->new_encoder =
  9342. to_intel_encoder(config->save_connector_encoders[count++]);
  9343. }
  9344. }
  9345. static bool
  9346. is_crtc_connector_off(struct drm_mode_set *set)
  9347. {
  9348. int i;
  9349. if (set->num_connectors == 0)
  9350. return false;
  9351. if (WARN_ON(set->connectors == NULL))
  9352. return false;
  9353. for (i = 0; i < set->num_connectors; i++)
  9354. if (set->connectors[i]->encoder &&
  9355. set->connectors[i]->encoder->crtc == set->crtc &&
  9356. set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
  9357. return true;
  9358. return false;
  9359. }
  9360. static void
  9361. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  9362. struct intel_set_config *config)
  9363. {
  9364. /* We should be able to check here if the fb has the same properties
  9365. * and then just flip_or_move it */
  9366. if (is_crtc_connector_off(set)) {
  9367. config->mode_changed = true;
  9368. } else if (set->crtc->primary->fb != set->fb) {
  9369. /*
  9370. * If we have no fb, we can only flip as long as the crtc is
  9371. * active, otherwise we need a full mode set. The crtc may
  9372. * be active if we've only disabled the primary plane, or
  9373. * in fastboot situations.
  9374. */
  9375. if (set->crtc->primary->fb == NULL) {
  9376. struct intel_crtc *intel_crtc =
  9377. to_intel_crtc(set->crtc);
  9378. if (intel_crtc->active) {
  9379. DRM_DEBUG_KMS("crtc has no fb, will flip\n");
  9380. config->fb_changed = true;
  9381. } else {
  9382. DRM_DEBUG_KMS("inactive crtc, full mode set\n");
  9383. config->mode_changed = true;
  9384. }
  9385. } else if (set->fb == NULL) {
  9386. config->mode_changed = true;
  9387. } else if (set->fb->pixel_format !=
  9388. set->crtc->primary->fb->pixel_format) {
  9389. config->mode_changed = true;
  9390. } else {
  9391. config->fb_changed = true;
  9392. }
  9393. }
  9394. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  9395. config->fb_changed = true;
  9396. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  9397. DRM_DEBUG_KMS("modes are different, full mode set\n");
  9398. drm_mode_debug_printmodeline(&set->crtc->mode);
  9399. drm_mode_debug_printmodeline(set->mode);
  9400. config->mode_changed = true;
  9401. }
  9402. DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
  9403. set->crtc->base.id, config->mode_changed, config->fb_changed);
  9404. }
  9405. static int
  9406. intel_modeset_stage_output_state(struct drm_device *dev,
  9407. struct drm_mode_set *set,
  9408. struct intel_set_config *config)
  9409. {
  9410. struct intel_connector *connector;
  9411. struct intel_encoder *encoder;
  9412. struct intel_crtc *crtc;
  9413. int ro;
  9414. /* The upper layers ensure that we either disable a crtc or have a list
  9415. * of connectors. For paranoia, double-check this. */
  9416. WARN_ON(!set->fb && (set->num_connectors != 0));
  9417. WARN_ON(set->fb && (set->num_connectors == 0));
  9418. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9419. base.head) {
  9420. /* Otherwise traverse passed in connector list and get encoders
  9421. * for them. */
  9422. for (ro = 0; ro < set->num_connectors; ro++) {
  9423. if (set->connectors[ro] == &connector->base) {
  9424. connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
  9425. break;
  9426. }
  9427. }
  9428. /* If we disable the crtc, disable all its connectors. Also, if
  9429. * the connector is on the changing crtc but not on the new
  9430. * connector list, disable it. */
  9431. if ((!set->fb || ro == set->num_connectors) &&
  9432. connector->base.encoder &&
  9433. connector->base.encoder->crtc == set->crtc) {
  9434. connector->new_encoder = NULL;
  9435. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  9436. connector->base.base.id,
  9437. connector->base.name);
  9438. }
  9439. if (&connector->new_encoder->base != connector->base.encoder) {
  9440. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  9441. config->mode_changed = true;
  9442. }
  9443. }
  9444. /* connector->new_encoder is now updated for all connectors. */
  9445. /* Update crtc of enabled connectors. */
  9446. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9447. base.head) {
  9448. struct drm_crtc *new_crtc;
  9449. if (!connector->new_encoder)
  9450. continue;
  9451. new_crtc = connector->new_encoder->base.crtc;
  9452. for (ro = 0; ro < set->num_connectors; ro++) {
  9453. if (set->connectors[ro] == &connector->base)
  9454. new_crtc = set->crtc;
  9455. }
  9456. /* Make sure the new CRTC will work with the encoder */
  9457. if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
  9458. new_crtc)) {
  9459. return -EINVAL;
  9460. }
  9461. connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
  9462. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  9463. connector->base.base.id,
  9464. connector->base.name,
  9465. new_crtc->base.id);
  9466. }
  9467. /* Check for any encoders that needs to be disabled. */
  9468. for_each_intel_encoder(dev, encoder) {
  9469. int num_connectors = 0;
  9470. list_for_each_entry(connector,
  9471. &dev->mode_config.connector_list,
  9472. base.head) {
  9473. if (connector->new_encoder == encoder) {
  9474. WARN_ON(!connector->new_encoder->new_crtc);
  9475. num_connectors++;
  9476. }
  9477. }
  9478. if (num_connectors == 0)
  9479. encoder->new_crtc = NULL;
  9480. else if (num_connectors > 1)
  9481. return -EINVAL;
  9482. /* Only now check for crtc changes so we don't miss encoders
  9483. * that will be disabled. */
  9484. if (&encoder->new_crtc->base != encoder->base.crtc) {
  9485. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  9486. config->mode_changed = true;
  9487. }
  9488. }
  9489. /* Now we've also updated encoder->new_crtc for all encoders. */
  9490. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9491. base.head) {
  9492. if (connector->new_encoder)
  9493. if (connector->new_encoder != connector->encoder)
  9494. connector->encoder = connector->new_encoder;
  9495. }
  9496. for_each_intel_crtc(dev, crtc) {
  9497. crtc->new_enabled = false;
  9498. for_each_intel_encoder(dev, encoder) {
  9499. if (encoder->new_crtc == crtc) {
  9500. crtc->new_enabled = true;
  9501. break;
  9502. }
  9503. }
  9504. if (crtc->new_enabled != crtc->base.enabled) {
  9505. DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
  9506. crtc->new_enabled ? "en" : "dis");
  9507. config->mode_changed = true;
  9508. }
  9509. if (crtc->new_enabled)
  9510. crtc->new_config = &crtc->config;
  9511. else
  9512. crtc->new_config = NULL;
  9513. }
  9514. return 0;
  9515. }
  9516. static void disable_crtc_nofb(struct intel_crtc *crtc)
  9517. {
  9518. struct drm_device *dev = crtc->base.dev;
  9519. struct intel_encoder *encoder;
  9520. struct intel_connector *connector;
  9521. DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
  9522. pipe_name(crtc->pipe));
  9523. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  9524. if (connector->new_encoder &&
  9525. connector->new_encoder->new_crtc == crtc)
  9526. connector->new_encoder = NULL;
  9527. }
  9528. for_each_intel_encoder(dev, encoder) {
  9529. if (encoder->new_crtc == crtc)
  9530. encoder->new_crtc = NULL;
  9531. }
  9532. crtc->new_enabled = false;
  9533. crtc->new_config = NULL;
  9534. }
  9535. static int intel_crtc_set_config(struct drm_mode_set *set)
  9536. {
  9537. struct drm_device *dev;
  9538. struct drm_mode_set save_set;
  9539. struct intel_set_config *config;
  9540. struct intel_crtc_config *pipe_config;
  9541. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  9542. int ret;
  9543. BUG_ON(!set);
  9544. BUG_ON(!set->crtc);
  9545. BUG_ON(!set->crtc->helper_private);
  9546. /* Enforce sane interface api - has been abused by the fb helper. */
  9547. BUG_ON(!set->mode && set->fb);
  9548. BUG_ON(set->fb && set->num_connectors == 0);
  9549. if (set->fb) {
  9550. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  9551. set->crtc->base.id, set->fb->base.id,
  9552. (int)set->num_connectors, set->x, set->y);
  9553. } else {
  9554. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  9555. }
  9556. dev = set->crtc->dev;
  9557. ret = -ENOMEM;
  9558. config = kzalloc(sizeof(*config), GFP_KERNEL);
  9559. if (!config)
  9560. goto out_config;
  9561. ret = intel_set_config_save_state(dev, config);
  9562. if (ret)
  9563. goto out_config;
  9564. save_set.crtc = set->crtc;
  9565. save_set.mode = &set->crtc->mode;
  9566. save_set.x = set->crtc->x;
  9567. save_set.y = set->crtc->y;
  9568. save_set.fb = set->crtc->primary->fb;
  9569. /* Compute whether we need a full modeset, only an fb base update or no
  9570. * change at all. In the future we might also check whether only the
  9571. * mode changed, e.g. for LVDS where we only change the panel fitter in
  9572. * such cases. */
  9573. intel_set_config_compute_mode_changes(set, config);
  9574. ret = intel_modeset_stage_output_state(dev, set, config);
  9575. if (ret)
  9576. goto fail;
  9577. pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
  9578. set->fb,
  9579. &modeset_pipes,
  9580. &prepare_pipes,
  9581. &disable_pipes);
  9582. if (IS_ERR(pipe_config)) {
  9583. ret = PTR_ERR(pipe_config);
  9584. goto fail;
  9585. } else if (pipe_config) {
  9586. if (pipe_config->has_audio !=
  9587. to_intel_crtc(set->crtc)->config.has_audio)
  9588. config->mode_changed = true;
  9589. /*
  9590. * Note we have an issue here with infoframes: current code
  9591. * only updates them on the full mode set path per hw
  9592. * requirements. So here we should be checking for any
  9593. * required changes and forcing a mode set.
  9594. */
  9595. }
  9596. /* set_mode will free it in the mode_changed case */
  9597. if (!config->mode_changed)
  9598. kfree(pipe_config);
  9599. intel_update_pipe_size(to_intel_crtc(set->crtc));
  9600. if (config->mode_changed) {
  9601. ret = intel_set_mode_pipes(set->crtc, set->mode,
  9602. set->x, set->y, set->fb, pipe_config,
  9603. modeset_pipes, prepare_pipes,
  9604. disable_pipes);
  9605. } else if (config->fb_changed) {
  9606. struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
  9607. struct drm_plane *primary = set->crtc->primary;
  9608. int vdisplay, hdisplay;
  9609. drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
  9610. ret = primary->funcs->update_plane(primary, set->crtc, set->fb,
  9611. 0, 0, hdisplay, vdisplay,
  9612. set->x << 16, set->y << 16,
  9613. hdisplay << 16, vdisplay << 16);
  9614. /*
  9615. * We need to make sure the primary plane is re-enabled if it
  9616. * has previously been turned off.
  9617. */
  9618. if (!intel_crtc->primary_enabled && ret == 0) {
  9619. WARN_ON(!intel_crtc->active);
  9620. intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
  9621. }
  9622. /*
  9623. * In the fastboot case this may be our only check of the
  9624. * state after boot. It would be better to only do it on
  9625. * the first update, but we don't have a nice way of doing that
  9626. * (and really, set_config isn't used much for high freq page
  9627. * flipping, so increasing its cost here shouldn't be a big
  9628. * deal).
  9629. */
  9630. if (i915.fastboot && ret == 0)
  9631. intel_modeset_check_state(set->crtc->dev);
  9632. }
  9633. if (ret) {
  9634. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  9635. set->crtc->base.id, ret);
  9636. fail:
  9637. intel_set_config_restore_state(dev, config);
  9638. /*
  9639. * HACK: if the pipe was on, but we didn't have a framebuffer,
  9640. * force the pipe off to avoid oopsing in the modeset code
  9641. * due to fb==NULL. This should only happen during boot since
  9642. * we don't yet reconstruct the FB from the hardware state.
  9643. */
  9644. if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
  9645. disable_crtc_nofb(to_intel_crtc(save_set.crtc));
  9646. /* Try to restore the config */
  9647. if (config->mode_changed &&
  9648. intel_set_mode(save_set.crtc, save_set.mode,
  9649. save_set.x, save_set.y, save_set.fb))
  9650. DRM_ERROR("failed to restore config after modeset failure\n");
  9651. }
  9652. out_config:
  9653. intel_set_config_free(config);
  9654. return ret;
  9655. }
  9656. static const struct drm_crtc_funcs intel_crtc_funcs = {
  9657. .gamma_set = intel_crtc_gamma_set,
  9658. .set_config = intel_crtc_set_config,
  9659. .destroy = intel_crtc_destroy,
  9660. .page_flip = intel_crtc_page_flip,
  9661. };
  9662. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  9663. struct intel_shared_dpll *pll,
  9664. struct intel_dpll_hw_state *hw_state)
  9665. {
  9666. uint32_t val;
  9667. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  9668. return false;
  9669. val = I915_READ(PCH_DPLL(pll->id));
  9670. hw_state->dpll = val;
  9671. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  9672. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  9673. return val & DPLL_VCO_ENABLE;
  9674. }
  9675. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  9676. struct intel_shared_dpll *pll)
  9677. {
  9678. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  9679. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  9680. }
  9681. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  9682. struct intel_shared_dpll *pll)
  9683. {
  9684. /* PCH refclock must be enabled first */
  9685. ibx_assert_pch_refclk_enabled(dev_priv);
  9686. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  9687. /* Wait for the clocks to stabilize. */
  9688. POSTING_READ(PCH_DPLL(pll->id));
  9689. udelay(150);
  9690. /* The pixel multiplier can only be updated once the
  9691. * DPLL is enabled and the clocks are stable.
  9692. *
  9693. * So write it again.
  9694. */
  9695. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  9696. POSTING_READ(PCH_DPLL(pll->id));
  9697. udelay(200);
  9698. }
  9699. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  9700. struct intel_shared_dpll *pll)
  9701. {
  9702. struct drm_device *dev = dev_priv->dev;
  9703. struct intel_crtc *crtc;
  9704. /* Make sure no transcoder isn't still depending on us. */
  9705. for_each_intel_crtc(dev, crtc) {
  9706. if (intel_crtc_to_shared_dpll(crtc) == pll)
  9707. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  9708. }
  9709. I915_WRITE(PCH_DPLL(pll->id), 0);
  9710. POSTING_READ(PCH_DPLL(pll->id));
  9711. udelay(200);
  9712. }
  9713. static char *ibx_pch_dpll_names[] = {
  9714. "PCH DPLL A",
  9715. "PCH DPLL B",
  9716. };
  9717. static void ibx_pch_dpll_init(struct drm_device *dev)
  9718. {
  9719. struct drm_i915_private *dev_priv = dev->dev_private;
  9720. int i;
  9721. dev_priv->num_shared_dpll = 2;
  9722. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9723. dev_priv->shared_dplls[i].id = i;
  9724. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  9725. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  9726. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  9727. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  9728. dev_priv->shared_dplls[i].get_hw_state =
  9729. ibx_pch_dpll_get_hw_state;
  9730. }
  9731. }
  9732. static void intel_shared_dpll_init(struct drm_device *dev)
  9733. {
  9734. struct drm_i915_private *dev_priv = dev->dev_private;
  9735. if (HAS_DDI(dev))
  9736. intel_ddi_pll_init(dev);
  9737. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  9738. ibx_pch_dpll_init(dev);
  9739. else
  9740. dev_priv->num_shared_dpll = 0;
  9741. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  9742. }
  9743. /**
  9744. * intel_prepare_plane_fb - Prepare fb for usage on plane
  9745. * @plane: drm plane to prepare for
  9746. * @fb: framebuffer to prepare for presentation
  9747. *
  9748. * Prepares a framebuffer for usage on a display plane. Generally this
  9749. * involves pinning the underlying object and updating the frontbuffer tracking
  9750. * bits. Some older platforms need special physical address handling for
  9751. * cursor planes.
  9752. *
  9753. * Returns 0 on success, negative error code on failure.
  9754. */
  9755. int
  9756. intel_prepare_plane_fb(struct drm_plane *plane,
  9757. struct drm_framebuffer *fb)
  9758. {
  9759. struct drm_device *dev = plane->dev;
  9760. struct intel_plane *intel_plane = to_intel_plane(plane);
  9761. enum pipe pipe = intel_plane->pipe;
  9762. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9763. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  9764. unsigned frontbuffer_bits = 0;
  9765. int ret = 0;
  9766. if (WARN_ON(fb == plane->fb || !obj))
  9767. return 0;
  9768. switch (plane->type) {
  9769. case DRM_PLANE_TYPE_PRIMARY:
  9770. frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
  9771. break;
  9772. case DRM_PLANE_TYPE_CURSOR:
  9773. frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
  9774. break;
  9775. case DRM_PLANE_TYPE_OVERLAY:
  9776. frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
  9777. break;
  9778. }
  9779. mutex_lock(&dev->struct_mutex);
  9780. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  9781. INTEL_INFO(dev)->cursor_needs_physical) {
  9782. int align = IS_I830(dev) ? 16 * 1024 : 256;
  9783. ret = i915_gem_object_attach_phys(obj, align);
  9784. if (ret)
  9785. DRM_DEBUG_KMS("failed to attach phys object\n");
  9786. } else {
  9787. ret = intel_pin_and_fence_fb_obj(plane, fb, NULL);
  9788. }
  9789. if (ret == 0)
  9790. i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
  9791. mutex_unlock(&dev->struct_mutex);
  9792. return ret;
  9793. }
  9794. /**
  9795. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  9796. * @plane: drm plane to clean up for
  9797. * @fb: old framebuffer that was on plane
  9798. *
  9799. * Cleans up a framebuffer that has just been removed from a plane.
  9800. */
  9801. void
  9802. intel_cleanup_plane_fb(struct drm_plane *plane,
  9803. struct drm_framebuffer *fb)
  9804. {
  9805. struct drm_device *dev = plane->dev;
  9806. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9807. if (WARN_ON(!obj))
  9808. return;
  9809. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  9810. !INTEL_INFO(dev)->cursor_needs_physical) {
  9811. mutex_lock(&dev->struct_mutex);
  9812. intel_unpin_fb_obj(obj);
  9813. mutex_unlock(&dev->struct_mutex);
  9814. }
  9815. }
  9816. static int
  9817. intel_check_primary_plane(struct drm_plane *plane,
  9818. struct intel_plane_state *state)
  9819. {
  9820. struct drm_device *dev = plane->dev;
  9821. struct drm_i915_private *dev_priv = dev->dev_private;
  9822. struct drm_crtc *crtc = state->base.crtc;
  9823. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9824. struct intel_plane *intel_plane = to_intel_plane(plane);
  9825. struct drm_framebuffer *fb = state->base.fb;
  9826. struct drm_rect *dest = &state->dst;
  9827. struct drm_rect *src = &state->src;
  9828. const struct drm_rect *clip = &state->clip;
  9829. int ret;
  9830. ret = drm_plane_helper_check_update(plane, crtc, fb,
  9831. src, dest, clip,
  9832. DRM_PLANE_HELPER_NO_SCALING,
  9833. DRM_PLANE_HELPER_NO_SCALING,
  9834. false, true, &state->visible);
  9835. if (ret)
  9836. return ret;
  9837. if (intel_crtc->active) {
  9838. intel_crtc->atomic.wait_for_flips = true;
  9839. /*
  9840. * FBC does not work on some platforms for rotated
  9841. * planes, so disable it when rotation is not 0 and
  9842. * update it when rotation is set back to 0.
  9843. *
  9844. * FIXME: This is redundant with the fbc update done in
  9845. * the primary plane enable function except that that
  9846. * one is done too late. We eventually need to unify
  9847. * this.
  9848. */
  9849. if (intel_crtc->primary_enabled &&
  9850. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  9851. dev_priv->fbc.plane == intel_crtc->plane &&
  9852. intel_plane->rotation != BIT(DRM_ROTATE_0)) {
  9853. intel_crtc->atomic.disable_fbc = true;
  9854. }
  9855. if (state->visible) {
  9856. /*
  9857. * BDW signals flip done immediately if the plane
  9858. * is disabled, even if the plane enable is already
  9859. * armed to occur at the next vblank :(
  9860. */
  9861. if (IS_BROADWELL(dev) && !intel_crtc->primary_enabled)
  9862. intel_crtc->atomic.wait_vblank = true;
  9863. }
  9864. intel_crtc->atomic.fb_bits |=
  9865. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  9866. intel_crtc->atomic.update_fbc = true;
  9867. }
  9868. return 0;
  9869. }
  9870. static void
  9871. intel_commit_primary_plane(struct drm_plane *plane,
  9872. struct intel_plane_state *state)
  9873. {
  9874. struct drm_crtc *crtc = state->base.crtc;
  9875. struct drm_framebuffer *fb = state->base.fb;
  9876. struct drm_device *dev = plane->dev;
  9877. struct drm_i915_private *dev_priv = dev->dev_private;
  9878. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9879. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9880. struct intel_plane *intel_plane = to_intel_plane(plane);
  9881. struct drm_rect *src = &state->src;
  9882. plane->fb = fb;
  9883. crtc->x = src->x1 >> 16;
  9884. crtc->y = src->y1 >> 16;
  9885. intel_plane->crtc_x = state->orig_dst.x1;
  9886. intel_plane->crtc_y = state->orig_dst.y1;
  9887. intel_plane->crtc_w = drm_rect_width(&state->orig_dst);
  9888. intel_plane->crtc_h = drm_rect_height(&state->orig_dst);
  9889. intel_plane->src_x = state->orig_src.x1;
  9890. intel_plane->src_y = state->orig_src.y1;
  9891. intel_plane->src_w = drm_rect_width(&state->orig_src);
  9892. intel_plane->src_h = drm_rect_height(&state->orig_src);
  9893. intel_plane->obj = obj;
  9894. if (intel_crtc->active) {
  9895. if (state->visible) {
  9896. /* FIXME: kill this fastboot hack */
  9897. intel_update_pipe_size(intel_crtc);
  9898. intel_crtc->primary_enabled = true;
  9899. dev_priv->display.update_primary_plane(crtc, plane->fb,
  9900. crtc->x, crtc->y);
  9901. } else {
  9902. /*
  9903. * If clipping results in a non-visible primary plane,
  9904. * we'll disable the primary plane. Note that this is
  9905. * a bit different than what happens if userspace
  9906. * explicitly disables the plane by passing fb=0
  9907. * because plane->fb still gets set and pinned.
  9908. */
  9909. intel_disable_primary_hw_plane(plane, crtc);
  9910. }
  9911. }
  9912. }
  9913. static void intel_begin_crtc_commit(struct drm_crtc *crtc)
  9914. {
  9915. struct drm_device *dev = crtc->dev;
  9916. struct drm_i915_private *dev_priv = dev->dev_private;
  9917. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9918. if (intel_crtc->atomic.wait_for_flips)
  9919. intel_crtc_wait_for_pending_flips(crtc);
  9920. if (intel_crtc->atomic.disable_fbc)
  9921. intel_fbc_disable(dev);
  9922. if (intel_crtc->atomic.pre_disable_primary)
  9923. intel_pre_disable_primary(crtc);
  9924. if (intel_crtc->atomic.update_wm)
  9925. intel_update_watermarks(crtc);
  9926. intel_runtime_pm_get(dev_priv);
  9927. /* Perform vblank evasion around commit operation */
  9928. if (intel_crtc->active)
  9929. intel_crtc->atomic.evade =
  9930. intel_pipe_update_start(intel_crtc,
  9931. &intel_crtc->atomic.start_vbl_count);
  9932. }
  9933. static void intel_finish_crtc_commit(struct drm_crtc *crtc)
  9934. {
  9935. struct drm_device *dev = crtc->dev;
  9936. struct drm_i915_private *dev_priv = dev->dev_private;
  9937. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9938. struct drm_plane *p;
  9939. if (intel_crtc->atomic.evade)
  9940. intel_pipe_update_end(intel_crtc,
  9941. intel_crtc->atomic.start_vbl_count);
  9942. intel_runtime_pm_put(dev_priv);
  9943. if (intel_crtc->atomic.wait_vblank)
  9944. intel_wait_for_vblank(dev, intel_crtc->pipe);
  9945. intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
  9946. if (intel_crtc->atomic.update_fbc) {
  9947. mutex_lock(&dev->struct_mutex);
  9948. intel_fbc_update(dev);
  9949. mutex_unlock(&dev->struct_mutex);
  9950. }
  9951. if (intel_crtc->atomic.post_enable_primary)
  9952. intel_post_enable_primary(crtc);
  9953. drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
  9954. if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
  9955. intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
  9956. false, false);
  9957. memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
  9958. }
  9959. int
  9960. intel_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
  9961. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  9962. unsigned int crtc_w, unsigned int crtc_h,
  9963. uint32_t src_x, uint32_t src_y,
  9964. uint32_t src_w, uint32_t src_h)
  9965. {
  9966. struct drm_device *dev = plane->dev;
  9967. struct drm_framebuffer *old_fb = plane->fb;
  9968. struct intel_plane_state state = {{ 0 }};
  9969. struct intel_plane *intel_plane = to_intel_plane(plane);
  9970. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9971. int ret;
  9972. state.base.crtc = crtc ? crtc : plane->crtc;
  9973. state.base.fb = fb;
  9974. /* sample coordinates in 16.16 fixed point */
  9975. state.src.x1 = src_x;
  9976. state.src.x2 = src_x + src_w;
  9977. state.src.y1 = src_y;
  9978. state.src.y2 = src_y + src_h;
  9979. /* integer pixels */
  9980. state.dst.x1 = crtc_x;
  9981. state.dst.x2 = crtc_x + crtc_w;
  9982. state.dst.y1 = crtc_y;
  9983. state.dst.y2 = crtc_y + crtc_h;
  9984. state.clip.x1 = 0;
  9985. state.clip.y1 = 0;
  9986. state.clip.x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0;
  9987. state.clip.y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0;
  9988. state.orig_src = state.src;
  9989. state.orig_dst = state.dst;
  9990. ret = intel_plane->check_plane(plane, &state);
  9991. if (ret)
  9992. return ret;
  9993. if (fb != old_fb && fb) {
  9994. ret = intel_prepare_plane_fb(plane, fb);
  9995. if (ret)
  9996. return ret;
  9997. }
  9998. if (!state.base.fb) {
  9999. unsigned fb_bits = 0;
  10000. switch (plane->type) {
  10001. case DRM_PLANE_TYPE_PRIMARY:
  10002. fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
  10003. break;
  10004. case DRM_PLANE_TYPE_CURSOR:
  10005. fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
  10006. break;
  10007. case DRM_PLANE_TYPE_OVERLAY:
  10008. fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
  10009. break;
  10010. }
  10011. /*
  10012. * 'prepare' is never called when plane is being disabled, so
  10013. * we need to handle frontbuffer tracking here
  10014. */
  10015. mutex_lock(&dev->struct_mutex);
  10016. i915_gem_track_fb(intel_fb_obj(plane->fb), NULL, fb_bits);
  10017. mutex_unlock(&dev->struct_mutex);
  10018. }
  10019. intel_begin_crtc_commit(crtc);
  10020. intel_plane->commit_plane(plane, &state);
  10021. intel_finish_crtc_commit(crtc);
  10022. if (fb != old_fb && old_fb) {
  10023. if (intel_crtc->active)
  10024. intel_wait_for_vblank(dev, intel_crtc->pipe);
  10025. intel_cleanup_plane_fb(plane, old_fb);
  10026. }
  10027. plane->fb = fb;
  10028. return 0;
  10029. }
  10030. /**
  10031. * intel_disable_plane - disable a plane
  10032. * @plane: plane to disable
  10033. *
  10034. * General disable handler for all plane types.
  10035. */
  10036. int
  10037. intel_disable_plane(struct drm_plane *plane)
  10038. {
  10039. if (!plane->fb)
  10040. return 0;
  10041. if (WARN_ON(!plane->crtc))
  10042. return -EINVAL;
  10043. return plane->funcs->update_plane(plane, plane->crtc, NULL,
  10044. 0, 0, 0, 0, 0, 0, 0, 0);
  10045. }
  10046. /**
  10047. * intel_plane_destroy - destroy a plane
  10048. * @plane: plane to destroy
  10049. *
  10050. * Common destruction function for all types of planes (primary, cursor,
  10051. * sprite).
  10052. */
  10053. void intel_plane_destroy(struct drm_plane *plane)
  10054. {
  10055. struct intel_plane *intel_plane = to_intel_plane(plane);
  10056. drm_plane_cleanup(plane);
  10057. kfree(intel_plane);
  10058. }
  10059. static const struct drm_plane_funcs intel_primary_plane_funcs = {
  10060. .update_plane = intel_update_plane,
  10061. .disable_plane = intel_disable_plane,
  10062. .destroy = intel_plane_destroy,
  10063. .set_property = intel_plane_set_property
  10064. };
  10065. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  10066. int pipe)
  10067. {
  10068. struct intel_plane *primary;
  10069. const uint32_t *intel_primary_formats;
  10070. int num_formats;
  10071. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  10072. if (primary == NULL)
  10073. return NULL;
  10074. primary->can_scale = false;
  10075. primary->max_downscale = 1;
  10076. primary->pipe = pipe;
  10077. primary->plane = pipe;
  10078. primary->rotation = BIT(DRM_ROTATE_0);
  10079. primary->check_plane = intel_check_primary_plane;
  10080. primary->commit_plane = intel_commit_primary_plane;
  10081. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  10082. primary->plane = !pipe;
  10083. if (INTEL_INFO(dev)->gen <= 3) {
  10084. intel_primary_formats = intel_primary_formats_gen2;
  10085. num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
  10086. } else {
  10087. intel_primary_formats = intel_primary_formats_gen4;
  10088. num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
  10089. }
  10090. drm_universal_plane_init(dev, &primary->base, 0,
  10091. &intel_primary_plane_funcs,
  10092. intel_primary_formats, num_formats,
  10093. DRM_PLANE_TYPE_PRIMARY);
  10094. if (INTEL_INFO(dev)->gen >= 4) {
  10095. if (!dev->mode_config.rotation_property)
  10096. dev->mode_config.rotation_property =
  10097. drm_mode_create_rotation_property(dev,
  10098. BIT(DRM_ROTATE_0) |
  10099. BIT(DRM_ROTATE_180));
  10100. if (dev->mode_config.rotation_property)
  10101. drm_object_attach_property(&primary->base.base,
  10102. dev->mode_config.rotation_property,
  10103. primary->rotation);
  10104. }
  10105. return &primary->base;
  10106. }
  10107. static int
  10108. intel_check_cursor_plane(struct drm_plane *plane,
  10109. struct intel_plane_state *state)
  10110. {
  10111. struct drm_crtc *crtc = state->base.crtc;
  10112. struct drm_device *dev = crtc->dev;
  10113. struct drm_framebuffer *fb = state->base.fb;
  10114. struct drm_rect *dest = &state->dst;
  10115. struct drm_rect *src = &state->src;
  10116. const struct drm_rect *clip = &state->clip;
  10117. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10118. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10119. int crtc_w, crtc_h;
  10120. unsigned stride;
  10121. int ret;
  10122. ret = drm_plane_helper_check_update(plane, crtc, fb,
  10123. src, dest, clip,
  10124. DRM_PLANE_HELPER_NO_SCALING,
  10125. DRM_PLANE_HELPER_NO_SCALING,
  10126. true, true, &state->visible);
  10127. if (ret)
  10128. return ret;
  10129. /* if we want to turn off the cursor ignore width and height */
  10130. if (!obj)
  10131. goto finish;
  10132. /* Check for which cursor types we support */
  10133. crtc_w = drm_rect_width(&state->orig_dst);
  10134. crtc_h = drm_rect_height(&state->orig_dst);
  10135. if (!cursor_size_ok(dev, crtc_w, crtc_h)) {
  10136. DRM_DEBUG("Cursor dimension not supported\n");
  10137. return -EINVAL;
  10138. }
  10139. stride = roundup_pow_of_two(crtc_w) * 4;
  10140. if (obj->base.size < stride * crtc_h) {
  10141. DRM_DEBUG_KMS("buffer is too small\n");
  10142. return -ENOMEM;
  10143. }
  10144. if (fb == crtc->cursor->fb)
  10145. return 0;
  10146. /* we only need to pin inside GTT if cursor is non-phy */
  10147. mutex_lock(&dev->struct_mutex);
  10148. if (!INTEL_INFO(dev)->cursor_needs_physical && obj->tiling_mode) {
  10149. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  10150. ret = -EINVAL;
  10151. }
  10152. mutex_unlock(&dev->struct_mutex);
  10153. finish:
  10154. if (intel_crtc->active) {
  10155. if (intel_crtc->cursor_width !=
  10156. drm_rect_width(&state->orig_dst))
  10157. intel_crtc->atomic.update_wm = true;
  10158. intel_crtc->atomic.fb_bits |=
  10159. INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
  10160. }
  10161. return ret;
  10162. }
  10163. static void
  10164. intel_commit_cursor_plane(struct drm_plane *plane,
  10165. struct intel_plane_state *state)
  10166. {
  10167. struct drm_crtc *crtc = state->base.crtc;
  10168. struct drm_device *dev = crtc->dev;
  10169. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10170. struct intel_plane *intel_plane = to_intel_plane(plane);
  10171. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  10172. uint32_t addr;
  10173. plane->fb = state->base.fb;
  10174. crtc->cursor_x = state->orig_dst.x1;
  10175. crtc->cursor_y = state->orig_dst.y1;
  10176. intel_plane->crtc_x = state->orig_dst.x1;
  10177. intel_plane->crtc_y = state->orig_dst.y1;
  10178. intel_plane->crtc_w = drm_rect_width(&state->orig_dst);
  10179. intel_plane->crtc_h = drm_rect_height(&state->orig_dst);
  10180. intel_plane->src_x = state->orig_src.x1;
  10181. intel_plane->src_y = state->orig_src.y1;
  10182. intel_plane->src_w = drm_rect_width(&state->orig_src);
  10183. intel_plane->src_h = drm_rect_height(&state->orig_src);
  10184. intel_plane->obj = obj;
  10185. if (intel_crtc->cursor_bo == obj)
  10186. goto update;
  10187. if (!obj)
  10188. addr = 0;
  10189. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  10190. addr = i915_gem_obj_ggtt_offset(obj);
  10191. else
  10192. addr = obj->phys_handle->busaddr;
  10193. intel_crtc->cursor_addr = addr;
  10194. intel_crtc->cursor_bo = obj;
  10195. update:
  10196. intel_crtc->cursor_width = drm_rect_width(&state->orig_dst);
  10197. intel_crtc->cursor_height = drm_rect_height(&state->orig_dst);
  10198. if (intel_crtc->active)
  10199. intel_crtc_update_cursor(crtc, state->visible);
  10200. }
  10201. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  10202. .update_plane = intel_update_plane,
  10203. .disable_plane = intel_disable_plane,
  10204. .destroy = intel_plane_destroy,
  10205. .set_property = intel_plane_set_property,
  10206. };
  10207. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  10208. int pipe)
  10209. {
  10210. struct intel_plane *cursor;
  10211. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  10212. if (cursor == NULL)
  10213. return NULL;
  10214. cursor->can_scale = false;
  10215. cursor->max_downscale = 1;
  10216. cursor->pipe = pipe;
  10217. cursor->plane = pipe;
  10218. cursor->rotation = BIT(DRM_ROTATE_0);
  10219. cursor->check_plane = intel_check_cursor_plane;
  10220. cursor->commit_plane = intel_commit_cursor_plane;
  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. static bool intel_crt_present(struct drm_device *dev)
  10344. {
  10345. struct drm_i915_private *dev_priv = dev->dev_private;
  10346. if (INTEL_INFO(dev)->gen >= 9)
  10347. return false;
  10348. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  10349. return false;
  10350. if (IS_CHERRYVIEW(dev))
  10351. return false;
  10352. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  10353. return false;
  10354. return true;
  10355. }
  10356. static void intel_setup_outputs(struct drm_device *dev)
  10357. {
  10358. struct drm_i915_private *dev_priv = dev->dev_private;
  10359. struct intel_encoder *encoder;
  10360. bool dpd_is_edp = false;
  10361. intel_lvds_init(dev);
  10362. if (intel_crt_present(dev))
  10363. intel_crt_init(dev);
  10364. if (HAS_DDI(dev)) {
  10365. int found;
  10366. /* Haswell uses DDI functions to detect digital outputs */
  10367. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  10368. /* DDI A only supports eDP */
  10369. if (found)
  10370. intel_ddi_init(dev, PORT_A);
  10371. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  10372. * register */
  10373. found = I915_READ(SFUSE_STRAP);
  10374. if (found & SFUSE_STRAP_DDIB_DETECTED)
  10375. intel_ddi_init(dev, PORT_B);
  10376. if (found & SFUSE_STRAP_DDIC_DETECTED)
  10377. intel_ddi_init(dev, PORT_C);
  10378. if (found & SFUSE_STRAP_DDID_DETECTED)
  10379. intel_ddi_init(dev, PORT_D);
  10380. } else if (HAS_PCH_SPLIT(dev)) {
  10381. int found;
  10382. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  10383. if (has_edp_a(dev))
  10384. intel_dp_init(dev, DP_A, PORT_A);
  10385. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  10386. /* PCH SDVOB multiplex with HDMIB */
  10387. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  10388. if (!found)
  10389. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  10390. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  10391. intel_dp_init(dev, PCH_DP_B, PORT_B);
  10392. }
  10393. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  10394. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  10395. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  10396. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  10397. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  10398. intel_dp_init(dev, PCH_DP_C, PORT_C);
  10399. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  10400. intel_dp_init(dev, PCH_DP_D, PORT_D);
  10401. } else if (IS_VALLEYVIEW(dev)) {
  10402. /*
  10403. * The DP_DETECTED bit is the latched state of the DDC
  10404. * SDA pin at boot. However since eDP doesn't require DDC
  10405. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  10406. * eDP ports may have been muxed to an alternate function.
  10407. * Thus we can't rely on the DP_DETECTED bit alone to detect
  10408. * eDP ports. Consult the VBT as well as DP_DETECTED to
  10409. * detect eDP ports.
  10410. */
  10411. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED)
  10412. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  10413. PORT_B);
  10414. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  10415. intel_dp_is_edp(dev, PORT_B))
  10416. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  10417. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED)
  10418. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  10419. PORT_C);
  10420. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  10421. intel_dp_is_edp(dev, PORT_C))
  10422. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  10423. if (IS_CHERRYVIEW(dev)) {
  10424. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  10425. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  10426. PORT_D);
  10427. /* eDP not supported on port D, so don't check VBT */
  10428. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  10429. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  10430. }
  10431. intel_dsi_init(dev);
  10432. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  10433. bool found = false;
  10434. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10435. DRM_DEBUG_KMS("probing SDVOB\n");
  10436. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  10437. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  10438. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  10439. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  10440. }
  10441. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  10442. intel_dp_init(dev, DP_B, PORT_B);
  10443. }
  10444. /* Before G4X SDVOC doesn't have its own detect register */
  10445. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10446. DRM_DEBUG_KMS("probing SDVOC\n");
  10447. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  10448. }
  10449. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  10450. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  10451. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  10452. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  10453. }
  10454. if (SUPPORTS_INTEGRATED_DP(dev))
  10455. intel_dp_init(dev, DP_C, PORT_C);
  10456. }
  10457. if (SUPPORTS_INTEGRATED_DP(dev) &&
  10458. (I915_READ(DP_D) & DP_DETECTED))
  10459. intel_dp_init(dev, DP_D, PORT_D);
  10460. } else if (IS_GEN2(dev))
  10461. intel_dvo_init(dev);
  10462. if (SUPPORTS_TV(dev))
  10463. intel_tv_init(dev);
  10464. intel_psr_init(dev);
  10465. for_each_intel_encoder(dev, encoder) {
  10466. encoder->base.possible_crtcs = encoder->crtc_mask;
  10467. encoder->base.possible_clones =
  10468. intel_encoder_clones(encoder);
  10469. }
  10470. intel_init_pch_refclk(dev);
  10471. drm_helper_move_panel_connectors_to_head(dev);
  10472. }
  10473. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  10474. {
  10475. struct drm_device *dev = fb->dev;
  10476. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10477. drm_framebuffer_cleanup(fb);
  10478. mutex_lock(&dev->struct_mutex);
  10479. WARN_ON(!intel_fb->obj->framebuffer_references--);
  10480. drm_gem_object_unreference(&intel_fb->obj->base);
  10481. mutex_unlock(&dev->struct_mutex);
  10482. kfree(intel_fb);
  10483. }
  10484. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  10485. struct drm_file *file,
  10486. unsigned int *handle)
  10487. {
  10488. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10489. struct drm_i915_gem_object *obj = intel_fb->obj;
  10490. return drm_gem_handle_create(file, &obj->base, handle);
  10491. }
  10492. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  10493. .destroy = intel_user_framebuffer_destroy,
  10494. .create_handle = intel_user_framebuffer_create_handle,
  10495. };
  10496. static int intel_framebuffer_init(struct drm_device *dev,
  10497. struct intel_framebuffer *intel_fb,
  10498. struct drm_mode_fb_cmd2 *mode_cmd,
  10499. struct drm_i915_gem_object *obj)
  10500. {
  10501. int aligned_height;
  10502. int pitch_limit;
  10503. int ret;
  10504. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  10505. if (obj->tiling_mode == I915_TILING_Y) {
  10506. DRM_DEBUG("hardware does not support tiling Y\n");
  10507. return -EINVAL;
  10508. }
  10509. if (mode_cmd->pitches[0] & 63) {
  10510. DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
  10511. mode_cmd->pitches[0]);
  10512. return -EINVAL;
  10513. }
  10514. if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
  10515. pitch_limit = 32*1024;
  10516. } else if (INTEL_INFO(dev)->gen >= 4) {
  10517. if (obj->tiling_mode)
  10518. pitch_limit = 16*1024;
  10519. else
  10520. pitch_limit = 32*1024;
  10521. } else if (INTEL_INFO(dev)->gen >= 3) {
  10522. if (obj->tiling_mode)
  10523. pitch_limit = 8*1024;
  10524. else
  10525. pitch_limit = 16*1024;
  10526. } else
  10527. /* XXX DSPC is limited to 4k tiled */
  10528. pitch_limit = 8*1024;
  10529. if (mode_cmd->pitches[0] > pitch_limit) {
  10530. DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
  10531. obj->tiling_mode ? "tiled" : "linear",
  10532. mode_cmd->pitches[0], pitch_limit);
  10533. return -EINVAL;
  10534. }
  10535. if (obj->tiling_mode != I915_TILING_NONE &&
  10536. mode_cmd->pitches[0] != obj->stride) {
  10537. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  10538. mode_cmd->pitches[0], obj->stride);
  10539. return -EINVAL;
  10540. }
  10541. /* Reject formats not supported by any plane early. */
  10542. switch (mode_cmd->pixel_format) {
  10543. case DRM_FORMAT_C8:
  10544. case DRM_FORMAT_RGB565:
  10545. case DRM_FORMAT_XRGB8888:
  10546. case DRM_FORMAT_ARGB8888:
  10547. break;
  10548. case DRM_FORMAT_XRGB1555:
  10549. case DRM_FORMAT_ARGB1555:
  10550. if (INTEL_INFO(dev)->gen > 3) {
  10551. DRM_DEBUG("unsupported pixel format: %s\n",
  10552. drm_get_format_name(mode_cmd->pixel_format));
  10553. return -EINVAL;
  10554. }
  10555. break;
  10556. case DRM_FORMAT_XBGR8888:
  10557. case DRM_FORMAT_ABGR8888:
  10558. case DRM_FORMAT_XRGB2101010:
  10559. case DRM_FORMAT_ARGB2101010:
  10560. case DRM_FORMAT_XBGR2101010:
  10561. case DRM_FORMAT_ABGR2101010:
  10562. if (INTEL_INFO(dev)->gen < 4) {
  10563. DRM_DEBUG("unsupported pixel format: %s\n",
  10564. drm_get_format_name(mode_cmd->pixel_format));
  10565. return -EINVAL;
  10566. }
  10567. break;
  10568. case DRM_FORMAT_YUYV:
  10569. case DRM_FORMAT_UYVY:
  10570. case DRM_FORMAT_YVYU:
  10571. case DRM_FORMAT_VYUY:
  10572. if (INTEL_INFO(dev)->gen < 5) {
  10573. DRM_DEBUG("unsupported pixel format: %s\n",
  10574. drm_get_format_name(mode_cmd->pixel_format));
  10575. return -EINVAL;
  10576. }
  10577. break;
  10578. default:
  10579. DRM_DEBUG("unsupported pixel format: %s\n",
  10580. drm_get_format_name(mode_cmd->pixel_format));
  10581. return -EINVAL;
  10582. }
  10583. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  10584. if (mode_cmd->offsets[0] != 0)
  10585. return -EINVAL;
  10586. aligned_height = intel_align_height(dev, mode_cmd->height,
  10587. obj->tiling_mode);
  10588. /* FIXME drm helper for size checks (especially planar formats)? */
  10589. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  10590. return -EINVAL;
  10591. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  10592. intel_fb->obj = obj;
  10593. intel_fb->obj->framebuffer_references++;
  10594. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  10595. if (ret) {
  10596. DRM_ERROR("framebuffer init failed %d\n", ret);
  10597. return ret;
  10598. }
  10599. return 0;
  10600. }
  10601. static struct drm_framebuffer *
  10602. intel_user_framebuffer_create(struct drm_device *dev,
  10603. struct drm_file *filp,
  10604. struct drm_mode_fb_cmd2 *mode_cmd)
  10605. {
  10606. struct drm_i915_gem_object *obj;
  10607. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  10608. mode_cmd->handles[0]));
  10609. if (&obj->base == NULL)
  10610. return ERR_PTR(-ENOENT);
  10611. return intel_framebuffer_create(dev, mode_cmd, obj);
  10612. }
  10613. #ifndef CONFIG_DRM_I915_FBDEV
  10614. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  10615. {
  10616. }
  10617. #endif
  10618. static const struct drm_mode_config_funcs intel_mode_funcs = {
  10619. .fb_create = intel_user_framebuffer_create,
  10620. .output_poll_changed = intel_fbdev_output_poll_changed,
  10621. };
  10622. /* Set up chip specific display functions */
  10623. static void intel_init_display(struct drm_device *dev)
  10624. {
  10625. struct drm_i915_private *dev_priv = dev->dev_private;
  10626. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  10627. dev_priv->display.find_dpll = g4x_find_best_dpll;
  10628. else if (IS_CHERRYVIEW(dev))
  10629. dev_priv->display.find_dpll = chv_find_best_dpll;
  10630. else if (IS_VALLEYVIEW(dev))
  10631. dev_priv->display.find_dpll = vlv_find_best_dpll;
  10632. else if (IS_PINEVIEW(dev))
  10633. dev_priv->display.find_dpll = pnv_find_best_dpll;
  10634. else
  10635. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  10636. if (HAS_DDI(dev)) {
  10637. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  10638. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  10639. dev_priv->display.crtc_compute_clock =
  10640. haswell_crtc_compute_clock;
  10641. dev_priv->display.crtc_enable = haswell_crtc_enable;
  10642. dev_priv->display.crtc_disable = haswell_crtc_disable;
  10643. dev_priv->display.off = ironlake_crtc_off;
  10644. if (INTEL_INFO(dev)->gen >= 9)
  10645. dev_priv->display.update_primary_plane =
  10646. skylake_update_primary_plane;
  10647. else
  10648. dev_priv->display.update_primary_plane =
  10649. ironlake_update_primary_plane;
  10650. } else if (HAS_PCH_SPLIT(dev)) {
  10651. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  10652. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  10653. dev_priv->display.crtc_compute_clock =
  10654. ironlake_crtc_compute_clock;
  10655. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  10656. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  10657. dev_priv->display.off = ironlake_crtc_off;
  10658. dev_priv->display.update_primary_plane =
  10659. ironlake_update_primary_plane;
  10660. } else if (IS_VALLEYVIEW(dev)) {
  10661. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10662. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  10663. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  10664. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  10665. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10666. dev_priv->display.off = i9xx_crtc_off;
  10667. dev_priv->display.update_primary_plane =
  10668. i9xx_update_primary_plane;
  10669. } else {
  10670. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10671. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  10672. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  10673. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  10674. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10675. dev_priv->display.off = i9xx_crtc_off;
  10676. dev_priv->display.update_primary_plane =
  10677. i9xx_update_primary_plane;
  10678. }
  10679. /* Returns the core display clock speed */
  10680. if (IS_VALLEYVIEW(dev))
  10681. dev_priv->display.get_display_clock_speed =
  10682. valleyview_get_display_clock_speed;
  10683. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  10684. dev_priv->display.get_display_clock_speed =
  10685. i945_get_display_clock_speed;
  10686. else if (IS_I915G(dev))
  10687. dev_priv->display.get_display_clock_speed =
  10688. i915_get_display_clock_speed;
  10689. else if (IS_I945GM(dev) || IS_845G(dev))
  10690. dev_priv->display.get_display_clock_speed =
  10691. i9xx_misc_get_display_clock_speed;
  10692. else if (IS_PINEVIEW(dev))
  10693. dev_priv->display.get_display_clock_speed =
  10694. pnv_get_display_clock_speed;
  10695. else if (IS_I915GM(dev))
  10696. dev_priv->display.get_display_clock_speed =
  10697. i915gm_get_display_clock_speed;
  10698. else if (IS_I865G(dev))
  10699. dev_priv->display.get_display_clock_speed =
  10700. i865_get_display_clock_speed;
  10701. else if (IS_I85X(dev))
  10702. dev_priv->display.get_display_clock_speed =
  10703. i855_get_display_clock_speed;
  10704. else /* 852, 830 */
  10705. dev_priv->display.get_display_clock_speed =
  10706. i830_get_display_clock_speed;
  10707. if (IS_GEN5(dev)) {
  10708. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  10709. } else if (IS_GEN6(dev)) {
  10710. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  10711. } else if (IS_IVYBRIDGE(dev)) {
  10712. /* FIXME: detect B0+ stepping and use auto training */
  10713. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  10714. dev_priv->display.modeset_global_resources =
  10715. ivb_modeset_global_resources;
  10716. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  10717. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  10718. } else if (IS_VALLEYVIEW(dev)) {
  10719. dev_priv->display.modeset_global_resources =
  10720. valleyview_modeset_global_resources;
  10721. }
  10722. /* Default just returns -ENODEV to indicate unsupported */
  10723. dev_priv->display.queue_flip = intel_default_queue_flip;
  10724. switch (INTEL_INFO(dev)->gen) {
  10725. case 2:
  10726. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  10727. break;
  10728. case 3:
  10729. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  10730. break;
  10731. case 4:
  10732. case 5:
  10733. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  10734. break;
  10735. case 6:
  10736. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  10737. break;
  10738. case 7:
  10739. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  10740. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  10741. break;
  10742. case 9:
  10743. dev_priv->display.queue_flip = intel_gen9_queue_flip;
  10744. break;
  10745. }
  10746. intel_panel_init_backlight_funcs(dev);
  10747. mutex_init(&dev_priv->pps_mutex);
  10748. }
  10749. /*
  10750. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  10751. * resume, or other times. This quirk makes sure that's the case for
  10752. * affected systems.
  10753. */
  10754. static void quirk_pipea_force(struct drm_device *dev)
  10755. {
  10756. struct drm_i915_private *dev_priv = dev->dev_private;
  10757. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  10758. DRM_INFO("applying pipe a force quirk\n");
  10759. }
  10760. static void quirk_pipeb_force(struct drm_device *dev)
  10761. {
  10762. struct drm_i915_private *dev_priv = dev->dev_private;
  10763. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  10764. DRM_INFO("applying pipe b force quirk\n");
  10765. }
  10766. /*
  10767. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  10768. */
  10769. static void quirk_ssc_force_disable(struct drm_device *dev)
  10770. {
  10771. struct drm_i915_private *dev_priv = dev->dev_private;
  10772. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  10773. DRM_INFO("applying lvds SSC disable quirk\n");
  10774. }
  10775. /*
  10776. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  10777. * brightness value
  10778. */
  10779. static void quirk_invert_brightness(struct drm_device *dev)
  10780. {
  10781. struct drm_i915_private *dev_priv = dev->dev_private;
  10782. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  10783. DRM_INFO("applying inverted panel brightness quirk\n");
  10784. }
  10785. /* Some VBT's incorrectly indicate no backlight is present */
  10786. static void quirk_backlight_present(struct drm_device *dev)
  10787. {
  10788. struct drm_i915_private *dev_priv = dev->dev_private;
  10789. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  10790. DRM_INFO("applying backlight present quirk\n");
  10791. }
  10792. struct intel_quirk {
  10793. int device;
  10794. int subsystem_vendor;
  10795. int subsystem_device;
  10796. void (*hook)(struct drm_device *dev);
  10797. };
  10798. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  10799. struct intel_dmi_quirk {
  10800. void (*hook)(struct drm_device *dev);
  10801. const struct dmi_system_id (*dmi_id_list)[];
  10802. };
  10803. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  10804. {
  10805. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  10806. return 1;
  10807. }
  10808. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  10809. {
  10810. .dmi_id_list = &(const struct dmi_system_id[]) {
  10811. {
  10812. .callback = intel_dmi_reverse_brightness,
  10813. .ident = "NCR Corporation",
  10814. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  10815. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  10816. },
  10817. },
  10818. { } /* terminating entry */
  10819. },
  10820. .hook = quirk_invert_brightness,
  10821. },
  10822. };
  10823. static struct intel_quirk intel_quirks[] = {
  10824. /* HP Mini needs pipe A force quirk (LP: #322104) */
  10825. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  10826. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  10827. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  10828. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  10829. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  10830. /* 830 needs to leave pipe A & dpll A up */
  10831. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  10832. /* 830 needs to leave pipe B & dpll B up */
  10833. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  10834. /* Lenovo U160 cannot use SSC on LVDS */
  10835. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  10836. /* Sony Vaio Y cannot use SSC on LVDS */
  10837. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  10838. /* Acer Aspire 5734Z must invert backlight brightness */
  10839. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  10840. /* Acer/eMachines G725 */
  10841. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  10842. /* Acer/eMachines e725 */
  10843. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  10844. /* Acer/Packard Bell NCL20 */
  10845. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  10846. /* Acer Aspire 4736Z */
  10847. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  10848. /* Acer Aspire 5336 */
  10849. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  10850. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  10851. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  10852. /* Acer C720 Chromebook (Core i3 4005U) */
  10853. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  10854. /* Apple Macbook 2,1 (Core 2 T7400) */
  10855. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  10856. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  10857. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  10858. /* HP Chromebook 14 (Celeron 2955U) */
  10859. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  10860. };
  10861. static void intel_init_quirks(struct drm_device *dev)
  10862. {
  10863. struct pci_dev *d = dev->pdev;
  10864. int i;
  10865. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  10866. struct intel_quirk *q = &intel_quirks[i];
  10867. if (d->device == q->device &&
  10868. (d->subsystem_vendor == q->subsystem_vendor ||
  10869. q->subsystem_vendor == PCI_ANY_ID) &&
  10870. (d->subsystem_device == q->subsystem_device ||
  10871. q->subsystem_device == PCI_ANY_ID))
  10872. q->hook(dev);
  10873. }
  10874. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  10875. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  10876. intel_dmi_quirks[i].hook(dev);
  10877. }
  10878. }
  10879. /* Disable the VGA plane that we never use */
  10880. static void i915_disable_vga(struct drm_device *dev)
  10881. {
  10882. struct drm_i915_private *dev_priv = dev->dev_private;
  10883. u8 sr1;
  10884. u32 vga_reg = i915_vgacntrl_reg(dev);
  10885. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  10886. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  10887. outb(SR01, VGA_SR_INDEX);
  10888. sr1 = inb(VGA_SR_DATA);
  10889. outb(sr1 | 1<<5, VGA_SR_DATA);
  10890. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  10891. udelay(300);
  10892. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  10893. POSTING_READ(vga_reg);
  10894. }
  10895. void intel_modeset_init_hw(struct drm_device *dev)
  10896. {
  10897. intel_prepare_ddi(dev);
  10898. if (IS_VALLEYVIEW(dev))
  10899. vlv_update_cdclk(dev);
  10900. intel_init_clock_gating(dev);
  10901. intel_enable_gt_powersave(dev);
  10902. }
  10903. void intel_modeset_init(struct drm_device *dev)
  10904. {
  10905. struct drm_i915_private *dev_priv = dev->dev_private;
  10906. int sprite, ret;
  10907. enum pipe pipe;
  10908. struct intel_crtc *crtc;
  10909. drm_mode_config_init(dev);
  10910. dev->mode_config.min_width = 0;
  10911. dev->mode_config.min_height = 0;
  10912. dev->mode_config.preferred_depth = 24;
  10913. dev->mode_config.prefer_shadow = 1;
  10914. dev->mode_config.funcs = &intel_mode_funcs;
  10915. intel_init_quirks(dev);
  10916. intel_init_pm(dev);
  10917. if (INTEL_INFO(dev)->num_pipes == 0)
  10918. return;
  10919. intel_init_display(dev);
  10920. intel_init_audio(dev);
  10921. if (IS_GEN2(dev)) {
  10922. dev->mode_config.max_width = 2048;
  10923. dev->mode_config.max_height = 2048;
  10924. } else if (IS_GEN3(dev)) {
  10925. dev->mode_config.max_width = 4096;
  10926. dev->mode_config.max_height = 4096;
  10927. } else {
  10928. dev->mode_config.max_width = 8192;
  10929. dev->mode_config.max_height = 8192;
  10930. }
  10931. if (IS_845G(dev) || IS_I865G(dev)) {
  10932. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  10933. dev->mode_config.cursor_height = 1023;
  10934. } else if (IS_GEN2(dev)) {
  10935. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  10936. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  10937. } else {
  10938. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  10939. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  10940. }
  10941. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  10942. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  10943. INTEL_INFO(dev)->num_pipes,
  10944. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  10945. for_each_pipe(dev_priv, pipe) {
  10946. intel_crtc_init(dev, pipe);
  10947. for_each_sprite(pipe, sprite) {
  10948. ret = intel_plane_init(dev, pipe, sprite);
  10949. if (ret)
  10950. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  10951. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  10952. }
  10953. }
  10954. intel_init_dpio(dev);
  10955. intel_shared_dpll_init(dev);
  10956. /* Just disable it once at startup */
  10957. i915_disable_vga(dev);
  10958. intel_setup_outputs(dev);
  10959. /* Just in case the BIOS is doing something questionable. */
  10960. intel_fbc_disable(dev);
  10961. drm_modeset_lock_all(dev);
  10962. intel_modeset_setup_hw_state(dev, false);
  10963. drm_modeset_unlock_all(dev);
  10964. for_each_intel_crtc(dev, crtc) {
  10965. if (!crtc->active)
  10966. continue;
  10967. /*
  10968. * Note that reserving the BIOS fb up front prevents us
  10969. * from stuffing other stolen allocations like the ring
  10970. * on top. This prevents some ugliness at boot time, and
  10971. * can even allow for smooth boot transitions if the BIOS
  10972. * fb is large enough for the active pipe configuration.
  10973. */
  10974. if (dev_priv->display.get_plane_config) {
  10975. dev_priv->display.get_plane_config(crtc,
  10976. &crtc->plane_config);
  10977. /*
  10978. * If the fb is shared between multiple heads, we'll
  10979. * just get the first one.
  10980. */
  10981. intel_find_plane_obj(crtc, &crtc->plane_config);
  10982. }
  10983. }
  10984. }
  10985. static void intel_enable_pipe_a(struct drm_device *dev)
  10986. {
  10987. struct intel_connector *connector;
  10988. struct drm_connector *crt = NULL;
  10989. struct intel_load_detect_pipe load_detect_temp;
  10990. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  10991. /* We can't just switch on the pipe A, we need to set things up with a
  10992. * proper mode and output configuration. As a gross hack, enable pipe A
  10993. * by enabling the load detect pipe once. */
  10994. list_for_each_entry(connector,
  10995. &dev->mode_config.connector_list,
  10996. base.head) {
  10997. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  10998. crt = &connector->base;
  10999. break;
  11000. }
  11001. }
  11002. if (!crt)
  11003. return;
  11004. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  11005. intel_release_load_detect_pipe(crt, &load_detect_temp);
  11006. }
  11007. static bool
  11008. intel_check_plane_mapping(struct intel_crtc *crtc)
  11009. {
  11010. struct drm_device *dev = crtc->base.dev;
  11011. struct drm_i915_private *dev_priv = dev->dev_private;
  11012. u32 reg, val;
  11013. if (INTEL_INFO(dev)->num_pipes == 1)
  11014. return true;
  11015. reg = DSPCNTR(!crtc->plane);
  11016. val = I915_READ(reg);
  11017. if ((val & DISPLAY_PLANE_ENABLE) &&
  11018. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  11019. return false;
  11020. return true;
  11021. }
  11022. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  11023. {
  11024. struct drm_device *dev = crtc->base.dev;
  11025. struct drm_i915_private *dev_priv = dev->dev_private;
  11026. u32 reg;
  11027. /* Clear any frame start delays used for debugging left by the BIOS */
  11028. reg = PIPECONF(crtc->config.cpu_transcoder);
  11029. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  11030. /* restore vblank interrupts to correct state */
  11031. if (crtc->active) {
  11032. update_scanline_offset(crtc);
  11033. drm_vblank_on(dev, crtc->pipe);
  11034. } else
  11035. drm_vblank_off(dev, crtc->pipe);
  11036. /* We need to sanitize the plane -> pipe mapping first because this will
  11037. * disable the crtc (and hence change the state) if it is wrong. Note
  11038. * that gen4+ has a fixed plane -> pipe mapping. */
  11039. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  11040. struct intel_connector *connector;
  11041. bool plane;
  11042. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  11043. crtc->base.base.id);
  11044. /* Pipe has the wrong plane attached and the plane is active.
  11045. * Temporarily change the plane mapping and disable everything
  11046. * ... */
  11047. plane = crtc->plane;
  11048. crtc->plane = !plane;
  11049. crtc->primary_enabled = true;
  11050. dev_priv->display.crtc_disable(&crtc->base);
  11051. crtc->plane = plane;
  11052. /* ... and break all links. */
  11053. list_for_each_entry(connector, &dev->mode_config.connector_list,
  11054. base.head) {
  11055. if (connector->encoder->base.crtc != &crtc->base)
  11056. continue;
  11057. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11058. connector->base.encoder = NULL;
  11059. }
  11060. /* multiple connectors may have the same encoder:
  11061. * handle them and break crtc link separately */
  11062. list_for_each_entry(connector, &dev->mode_config.connector_list,
  11063. base.head)
  11064. if (connector->encoder->base.crtc == &crtc->base) {
  11065. connector->encoder->base.crtc = NULL;
  11066. connector->encoder->connectors_active = false;
  11067. }
  11068. WARN_ON(crtc->active);
  11069. crtc->base.enabled = false;
  11070. }
  11071. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  11072. crtc->pipe == PIPE_A && !crtc->active) {
  11073. /* BIOS forgot to enable pipe A, this mostly happens after
  11074. * resume. Force-enable the pipe to fix this, the update_dpms
  11075. * call below we restore the pipe to the right state, but leave
  11076. * the required bits on. */
  11077. intel_enable_pipe_a(dev);
  11078. }
  11079. /* Adjust the state of the output pipe according to whether we
  11080. * have active connectors/encoders. */
  11081. intel_crtc_update_dpms(&crtc->base);
  11082. if (crtc->active != crtc->base.enabled) {
  11083. struct intel_encoder *encoder;
  11084. /* This can happen either due to bugs in the get_hw_state
  11085. * functions or because the pipe is force-enabled due to the
  11086. * pipe A quirk. */
  11087. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  11088. crtc->base.base.id,
  11089. crtc->base.enabled ? "enabled" : "disabled",
  11090. crtc->active ? "enabled" : "disabled");
  11091. crtc->base.enabled = crtc->active;
  11092. /* Because we only establish the connector -> encoder ->
  11093. * crtc links if something is active, this means the
  11094. * crtc is now deactivated. Break the links. connector
  11095. * -> encoder links are only establish when things are
  11096. * actually up, hence no need to break them. */
  11097. WARN_ON(crtc->active);
  11098. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  11099. WARN_ON(encoder->connectors_active);
  11100. encoder->base.crtc = NULL;
  11101. }
  11102. }
  11103. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  11104. /*
  11105. * We start out with underrun reporting disabled to avoid races.
  11106. * For correct bookkeeping mark this on active crtcs.
  11107. *
  11108. * Also on gmch platforms we dont have any hardware bits to
  11109. * disable the underrun reporting. Which means we need to start
  11110. * out with underrun reporting disabled also on inactive pipes,
  11111. * since otherwise we'll complain about the garbage we read when
  11112. * e.g. coming up after runtime pm.
  11113. *
  11114. * No protection against concurrent access is required - at
  11115. * worst a fifo underrun happens which also sets this to false.
  11116. */
  11117. crtc->cpu_fifo_underrun_disabled = true;
  11118. crtc->pch_fifo_underrun_disabled = true;
  11119. }
  11120. }
  11121. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  11122. {
  11123. struct intel_connector *connector;
  11124. struct drm_device *dev = encoder->base.dev;
  11125. /* We need to check both for a crtc link (meaning that the
  11126. * encoder is active and trying to read from a pipe) and the
  11127. * pipe itself being active. */
  11128. bool has_active_crtc = encoder->base.crtc &&
  11129. to_intel_crtc(encoder->base.crtc)->active;
  11130. if (encoder->connectors_active && !has_active_crtc) {
  11131. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  11132. encoder->base.base.id,
  11133. encoder->base.name);
  11134. /* Connector is active, but has no active pipe. This is
  11135. * fallout from our resume register restoring. Disable
  11136. * the encoder manually again. */
  11137. if (encoder->base.crtc) {
  11138. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  11139. encoder->base.base.id,
  11140. encoder->base.name);
  11141. encoder->disable(encoder);
  11142. if (encoder->post_disable)
  11143. encoder->post_disable(encoder);
  11144. }
  11145. encoder->base.crtc = NULL;
  11146. encoder->connectors_active = false;
  11147. /* Inconsistent output/port/pipe state happens presumably due to
  11148. * a bug in one of the get_hw_state functions. Or someplace else
  11149. * in our code, like the register restore mess on resume. Clamp
  11150. * things to off as a safer default. */
  11151. list_for_each_entry(connector,
  11152. &dev->mode_config.connector_list,
  11153. base.head) {
  11154. if (connector->encoder != encoder)
  11155. continue;
  11156. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11157. connector->base.encoder = NULL;
  11158. }
  11159. }
  11160. /* Enabled encoders without active connectors will be fixed in
  11161. * the crtc fixup. */
  11162. }
  11163. void i915_redisable_vga_power_on(struct drm_device *dev)
  11164. {
  11165. struct drm_i915_private *dev_priv = dev->dev_private;
  11166. u32 vga_reg = i915_vgacntrl_reg(dev);
  11167. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  11168. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  11169. i915_disable_vga(dev);
  11170. }
  11171. }
  11172. void i915_redisable_vga(struct drm_device *dev)
  11173. {
  11174. struct drm_i915_private *dev_priv = dev->dev_private;
  11175. /* This function can be called both from intel_modeset_setup_hw_state or
  11176. * at a very early point in our resume sequence, where the power well
  11177. * structures are not yet restored. Since this function is at a very
  11178. * paranoid "someone might have enabled VGA while we were not looking"
  11179. * level, just check if the power well is enabled instead of trying to
  11180. * follow the "don't touch the power well if we don't need it" policy
  11181. * the rest of the driver uses. */
  11182. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  11183. return;
  11184. i915_redisable_vga_power_on(dev);
  11185. }
  11186. static bool primary_get_hw_state(struct intel_crtc *crtc)
  11187. {
  11188. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  11189. if (!crtc->active)
  11190. return false;
  11191. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  11192. }
  11193. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  11194. {
  11195. struct drm_i915_private *dev_priv = dev->dev_private;
  11196. enum pipe pipe;
  11197. struct intel_crtc *crtc;
  11198. struct intel_encoder *encoder;
  11199. struct intel_connector *connector;
  11200. int i;
  11201. for_each_intel_crtc(dev, crtc) {
  11202. memset(&crtc->config, 0, sizeof(crtc->config));
  11203. crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  11204. crtc->active = dev_priv->display.get_pipe_config(crtc,
  11205. &crtc->config);
  11206. crtc->base.enabled = crtc->active;
  11207. crtc->primary_enabled = primary_get_hw_state(crtc);
  11208. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  11209. crtc->base.base.id,
  11210. crtc->active ? "enabled" : "disabled");
  11211. }
  11212. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11213. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11214. pll->on = pll->get_hw_state(dev_priv, pll,
  11215. &pll->config.hw_state);
  11216. pll->active = 0;
  11217. pll->config.crtc_mask = 0;
  11218. for_each_intel_crtc(dev, crtc) {
  11219. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  11220. pll->active++;
  11221. pll->config.crtc_mask |= 1 << crtc->pipe;
  11222. }
  11223. }
  11224. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  11225. pll->name, pll->config.crtc_mask, pll->on);
  11226. if (pll->config.crtc_mask)
  11227. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  11228. }
  11229. for_each_intel_encoder(dev, encoder) {
  11230. pipe = 0;
  11231. if (encoder->get_hw_state(encoder, &pipe)) {
  11232. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11233. encoder->base.crtc = &crtc->base;
  11234. encoder->get_config(encoder, &crtc->config);
  11235. } else {
  11236. encoder->base.crtc = NULL;
  11237. }
  11238. encoder->connectors_active = false;
  11239. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  11240. encoder->base.base.id,
  11241. encoder->base.name,
  11242. encoder->base.crtc ? "enabled" : "disabled",
  11243. pipe_name(pipe));
  11244. }
  11245. list_for_each_entry(connector, &dev->mode_config.connector_list,
  11246. base.head) {
  11247. if (connector->get_hw_state(connector)) {
  11248. connector->base.dpms = DRM_MODE_DPMS_ON;
  11249. connector->encoder->connectors_active = true;
  11250. connector->base.encoder = &connector->encoder->base;
  11251. } else {
  11252. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11253. connector->base.encoder = NULL;
  11254. }
  11255. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  11256. connector->base.base.id,
  11257. connector->base.name,
  11258. connector->base.encoder ? "enabled" : "disabled");
  11259. }
  11260. }
  11261. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  11262. * and i915 state tracking structures. */
  11263. void intel_modeset_setup_hw_state(struct drm_device *dev,
  11264. bool force_restore)
  11265. {
  11266. struct drm_i915_private *dev_priv = dev->dev_private;
  11267. enum pipe pipe;
  11268. struct intel_crtc *crtc;
  11269. struct intel_encoder *encoder;
  11270. int i;
  11271. intel_modeset_readout_hw_state(dev);
  11272. /*
  11273. * Now that we have the config, copy it to each CRTC struct
  11274. * Note that this could go away if we move to using crtc_config
  11275. * checking everywhere.
  11276. */
  11277. for_each_intel_crtc(dev, crtc) {
  11278. if (crtc->active && i915.fastboot) {
  11279. intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
  11280. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  11281. crtc->base.base.id);
  11282. drm_mode_debug_printmodeline(&crtc->base.mode);
  11283. }
  11284. }
  11285. /* HW state is read out, now we need to sanitize this mess. */
  11286. for_each_intel_encoder(dev, encoder) {
  11287. intel_sanitize_encoder(encoder);
  11288. }
  11289. for_each_pipe(dev_priv, pipe) {
  11290. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11291. intel_sanitize_crtc(crtc);
  11292. intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
  11293. }
  11294. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11295. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11296. if (!pll->on || pll->active)
  11297. continue;
  11298. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  11299. pll->disable(dev_priv, pll);
  11300. pll->on = false;
  11301. }
  11302. if (IS_GEN9(dev))
  11303. skl_wm_get_hw_state(dev);
  11304. else if (HAS_PCH_SPLIT(dev))
  11305. ilk_wm_get_hw_state(dev);
  11306. if (force_restore) {
  11307. i915_redisable_vga(dev);
  11308. /*
  11309. * We need to use raw interfaces for restoring state to avoid
  11310. * checking (bogus) intermediate states.
  11311. */
  11312. for_each_pipe(dev_priv, pipe) {
  11313. struct drm_crtc *crtc =
  11314. dev_priv->pipe_to_crtc_mapping[pipe];
  11315. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
  11316. crtc->primary->fb);
  11317. }
  11318. } else {
  11319. intel_modeset_update_staged_output_state(dev);
  11320. }
  11321. intel_modeset_check_state(dev);
  11322. }
  11323. void intel_modeset_gem_init(struct drm_device *dev)
  11324. {
  11325. struct drm_i915_private *dev_priv = dev->dev_private;
  11326. struct drm_crtc *c;
  11327. struct drm_i915_gem_object *obj;
  11328. mutex_lock(&dev->struct_mutex);
  11329. intel_init_gt_powersave(dev);
  11330. mutex_unlock(&dev->struct_mutex);
  11331. /*
  11332. * There may be no VBT; and if the BIOS enabled SSC we can
  11333. * just keep using it to avoid unnecessary flicker. Whereas if the
  11334. * BIOS isn't using it, don't assume it will work even if the VBT
  11335. * indicates as much.
  11336. */
  11337. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11338. dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  11339. DREF_SSC1_ENABLE);
  11340. intel_modeset_init_hw(dev);
  11341. intel_setup_overlay(dev);
  11342. /*
  11343. * Make sure any fbs we allocated at startup are properly
  11344. * pinned & fenced. When we do the allocation it's too early
  11345. * for this.
  11346. */
  11347. mutex_lock(&dev->struct_mutex);
  11348. for_each_crtc(dev, c) {
  11349. obj = intel_fb_obj(c->primary->fb);
  11350. if (obj == NULL)
  11351. continue;
  11352. if (intel_pin_and_fence_fb_obj(c->primary,
  11353. c->primary->fb,
  11354. NULL)) {
  11355. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  11356. to_intel_crtc(c)->pipe);
  11357. drm_framebuffer_unreference(c->primary->fb);
  11358. c->primary->fb = NULL;
  11359. }
  11360. }
  11361. mutex_unlock(&dev->struct_mutex);
  11362. intel_backlight_register(dev);
  11363. }
  11364. void intel_connector_unregister(struct intel_connector *intel_connector)
  11365. {
  11366. struct drm_connector *connector = &intel_connector->base;
  11367. intel_panel_destroy_backlight(connector);
  11368. drm_connector_unregister(connector);
  11369. }
  11370. void intel_modeset_cleanup(struct drm_device *dev)
  11371. {
  11372. struct drm_i915_private *dev_priv = dev->dev_private;
  11373. struct drm_connector *connector;
  11374. intel_disable_gt_powersave(dev);
  11375. intel_backlight_unregister(dev);
  11376. /*
  11377. * Interrupts and polling as the first thing to avoid creating havoc.
  11378. * Too much stuff here (turning of connectors, ...) would
  11379. * experience fancy races otherwise.
  11380. */
  11381. intel_irq_uninstall(dev_priv);
  11382. /*
  11383. * Due to the hpd irq storm handling the hotplug work can re-arm the
  11384. * poll handlers. Hence disable polling after hpd handling is shut down.
  11385. */
  11386. drm_kms_helper_poll_fini(dev);
  11387. mutex_lock(&dev->struct_mutex);
  11388. intel_unregister_dsm_handler();
  11389. intel_fbc_disable(dev);
  11390. ironlake_teardown_rc6(dev);
  11391. mutex_unlock(&dev->struct_mutex);
  11392. /* flush any delayed tasks or pending work */
  11393. flush_scheduled_work();
  11394. /* destroy the backlight and sysfs files before encoders/connectors */
  11395. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  11396. struct intel_connector *intel_connector;
  11397. intel_connector = to_intel_connector(connector);
  11398. intel_connector->unregister(intel_connector);
  11399. }
  11400. drm_mode_config_cleanup(dev);
  11401. intel_cleanup_overlay(dev);
  11402. mutex_lock(&dev->struct_mutex);
  11403. intel_cleanup_gt_powersave(dev);
  11404. mutex_unlock(&dev->struct_mutex);
  11405. }
  11406. /*
  11407. * Return which encoder is currently attached for connector.
  11408. */
  11409. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  11410. {
  11411. return &intel_attached_encoder(connector)->base;
  11412. }
  11413. void intel_connector_attach_encoder(struct intel_connector *connector,
  11414. struct intel_encoder *encoder)
  11415. {
  11416. connector->encoder = encoder;
  11417. drm_mode_connector_attach_encoder(&connector->base,
  11418. &encoder->base);
  11419. }
  11420. /*
  11421. * set vga decode state - true == enable VGA decode
  11422. */
  11423. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  11424. {
  11425. struct drm_i915_private *dev_priv = dev->dev_private;
  11426. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  11427. u16 gmch_ctrl;
  11428. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  11429. DRM_ERROR("failed to read control word\n");
  11430. return -EIO;
  11431. }
  11432. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  11433. return 0;
  11434. if (state)
  11435. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  11436. else
  11437. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  11438. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  11439. DRM_ERROR("failed to write control word\n");
  11440. return -EIO;
  11441. }
  11442. return 0;
  11443. }
  11444. struct intel_display_error_state {
  11445. u32 power_well_driver;
  11446. int num_transcoders;
  11447. struct intel_cursor_error_state {
  11448. u32 control;
  11449. u32 position;
  11450. u32 base;
  11451. u32 size;
  11452. } cursor[I915_MAX_PIPES];
  11453. struct intel_pipe_error_state {
  11454. bool power_domain_on;
  11455. u32 source;
  11456. u32 stat;
  11457. } pipe[I915_MAX_PIPES];
  11458. struct intel_plane_error_state {
  11459. u32 control;
  11460. u32 stride;
  11461. u32 size;
  11462. u32 pos;
  11463. u32 addr;
  11464. u32 surface;
  11465. u32 tile_offset;
  11466. } plane[I915_MAX_PIPES];
  11467. struct intel_transcoder_error_state {
  11468. bool power_domain_on;
  11469. enum transcoder cpu_transcoder;
  11470. u32 conf;
  11471. u32 htotal;
  11472. u32 hblank;
  11473. u32 hsync;
  11474. u32 vtotal;
  11475. u32 vblank;
  11476. u32 vsync;
  11477. } transcoder[4];
  11478. };
  11479. struct intel_display_error_state *
  11480. intel_display_capture_error_state(struct drm_device *dev)
  11481. {
  11482. struct drm_i915_private *dev_priv = dev->dev_private;
  11483. struct intel_display_error_state *error;
  11484. int transcoders[] = {
  11485. TRANSCODER_A,
  11486. TRANSCODER_B,
  11487. TRANSCODER_C,
  11488. TRANSCODER_EDP,
  11489. };
  11490. int i;
  11491. if (INTEL_INFO(dev)->num_pipes == 0)
  11492. return NULL;
  11493. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  11494. if (error == NULL)
  11495. return NULL;
  11496. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11497. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  11498. for_each_pipe(dev_priv, i) {
  11499. error->pipe[i].power_domain_on =
  11500. __intel_display_power_is_enabled(dev_priv,
  11501. POWER_DOMAIN_PIPE(i));
  11502. if (!error->pipe[i].power_domain_on)
  11503. continue;
  11504. error->cursor[i].control = I915_READ(CURCNTR(i));
  11505. error->cursor[i].position = I915_READ(CURPOS(i));
  11506. error->cursor[i].base = I915_READ(CURBASE(i));
  11507. error->plane[i].control = I915_READ(DSPCNTR(i));
  11508. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  11509. if (INTEL_INFO(dev)->gen <= 3) {
  11510. error->plane[i].size = I915_READ(DSPSIZE(i));
  11511. error->plane[i].pos = I915_READ(DSPPOS(i));
  11512. }
  11513. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11514. error->plane[i].addr = I915_READ(DSPADDR(i));
  11515. if (INTEL_INFO(dev)->gen >= 4) {
  11516. error->plane[i].surface = I915_READ(DSPSURF(i));
  11517. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  11518. }
  11519. error->pipe[i].source = I915_READ(PIPESRC(i));
  11520. if (HAS_GMCH_DISPLAY(dev))
  11521. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  11522. }
  11523. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  11524. if (HAS_DDI(dev_priv->dev))
  11525. error->num_transcoders++; /* Account for eDP. */
  11526. for (i = 0; i < error->num_transcoders; i++) {
  11527. enum transcoder cpu_transcoder = transcoders[i];
  11528. error->transcoder[i].power_domain_on =
  11529. __intel_display_power_is_enabled(dev_priv,
  11530. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  11531. if (!error->transcoder[i].power_domain_on)
  11532. continue;
  11533. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  11534. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  11535. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  11536. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  11537. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  11538. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  11539. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  11540. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  11541. }
  11542. return error;
  11543. }
  11544. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  11545. void
  11546. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  11547. struct drm_device *dev,
  11548. struct intel_display_error_state *error)
  11549. {
  11550. struct drm_i915_private *dev_priv = dev->dev_private;
  11551. int i;
  11552. if (!error)
  11553. return;
  11554. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  11555. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11556. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  11557. error->power_well_driver);
  11558. for_each_pipe(dev_priv, i) {
  11559. err_printf(m, "Pipe [%d]:\n", i);
  11560. err_printf(m, " Power: %s\n",
  11561. error->pipe[i].power_domain_on ? "on" : "off");
  11562. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  11563. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  11564. err_printf(m, "Plane [%d]:\n", i);
  11565. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  11566. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  11567. if (INTEL_INFO(dev)->gen <= 3) {
  11568. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  11569. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  11570. }
  11571. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11572. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  11573. if (INTEL_INFO(dev)->gen >= 4) {
  11574. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  11575. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  11576. }
  11577. err_printf(m, "Cursor [%d]:\n", i);
  11578. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  11579. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  11580. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  11581. }
  11582. for (i = 0; i < error->num_transcoders; i++) {
  11583. err_printf(m, "CPU transcoder: %c\n",
  11584. transcoder_name(error->transcoder[i].cpu_transcoder));
  11585. err_printf(m, " Power: %s\n",
  11586. error->transcoder[i].power_domain_on ? "on" : "off");
  11587. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  11588. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  11589. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  11590. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  11591. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  11592. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  11593. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  11594. }
  11595. }
  11596. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  11597. {
  11598. struct intel_crtc *crtc;
  11599. for_each_intel_crtc(dev, crtc) {
  11600. struct intel_unpin_work *work;
  11601. spin_lock_irq(&dev->event_lock);
  11602. work = crtc->unpin_work;
  11603. if (work && work->event &&
  11604. work->event->base.file_priv == file) {
  11605. kfree(work->event);
  11606. work->event = NULL;
  11607. }
  11608. spin_unlock_irq(&dev->event_lock);
  11609. }
  11610. }