intel_display.c 383 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include <drm/i915_drm.h>
  37. #include "i915_drv.h"
  38. #include "i915_trace.h"
  39. #include <drm/drm_dp_helper.h>
  40. #include <drm/drm_crtc_helper.h>
  41. #include <drm/drm_plane_helper.h>
  42. #include <drm/drm_rect.h>
  43. #include <linux/dma_remapping.h>
  44. /* Primary plane formats supported by all gen */
  45. #define COMMON_PRIMARY_FORMATS \
  46. DRM_FORMAT_C8, \
  47. DRM_FORMAT_RGB565, \
  48. DRM_FORMAT_XRGB8888, \
  49. DRM_FORMAT_ARGB8888
  50. /* Primary plane formats for gen <= 3 */
  51. static const uint32_t intel_primary_formats_gen2[] = {
  52. COMMON_PRIMARY_FORMATS,
  53. DRM_FORMAT_XRGB1555,
  54. DRM_FORMAT_ARGB1555,
  55. };
  56. /* Primary plane formats for gen >= 4 */
  57. static const uint32_t intel_primary_formats_gen4[] = {
  58. COMMON_PRIMARY_FORMATS, \
  59. DRM_FORMAT_XBGR8888,
  60. DRM_FORMAT_ABGR8888,
  61. DRM_FORMAT_XRGB2101010,
  62. DRM_FORMAT_ARGB2101010,
  63. DRM_FORMAT_XBGR2101010,
  64. DRM_FORMAT_ABGR2101010,
  65. };
  66. /* Cursor formats */
  67. static const uint32_t intel_cursor_formats[] = {
  68. DRM_FORMAT_ARGB8888,
  69. };
  70. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  71. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  72. struct intel_crtc_config *pipe_config);
  73. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  74. struct intel_crtc_config *pipe_config);
  75. static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
  76. int x, int y, struct drm_framebuffer *old_fb);
  77. static int intel_framebuffer_init(struct drm_device *dev,
  78. struct intel_framebuffer *ifb,
  79. struct drm_mode_fb_cmd2 *mode_cmd,
  80. struct drm_i915_gem_object *obj);
  81. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  82. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  83. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  84. struct intel_link_m_n *m_n,
  85. struct intel_link_m_n *m2_n2);
  86. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  87. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  88. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  89. static void vlv_prepare_pll(struct intel_crtc *crtc,
  90. const struct intel_crtc_config *pipe_config);
  91. static void chv_prepare_pll(struct intel_crtc *crtc,
  92. const struct intel_crtc_config *pipe_config);
  93. static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
  94. {
  95. if (!connector->mst_port)
  96. return connector->encoder;
  97. else
  98. return &connector->mst_port->mst_encoders[pipe]->base;
  99. }
  100. typedef struct {
  101. int min, max;
  102. } intel_range_t;
  103. typedef struct {
  104. int dot_limit;
  105. int p2_slow, p2_fast;
  106. } intel_p2_t;
  107. typedef struct intel_limit intel_limit_t;
  108. struct intel_limit {
  109. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  110. intel_p2_t p2;
  111. };
  112. int
  113. intel_pch_rawclk(struct drm_device *dev)
  114. {
  115. struct drm_i915_private *dev_priv = dev->dev_private;
  116. WARN_ON(!HAS_PCH_SPLIT(dev));
  117. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  118. }
  119. static inline u32 /* units of 100MHz */
  120. intel_fdi_link_freq(struct drm_device *dev)
  121. {
  122. if (IS_GEN5(dev)) {
  123. struct drm_i915_private *dev_priv = dev->dev_private;
  124. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  125. } else
  126. return 27;
  127. }
  128. static const intel_limit_t intel_limits_i8xx_dac = {
  129. .dot = { .min = 25000, .max = 350000 },
  130. .vco = { .min = 908000, .max = 1512000 },
  131. .n = { .min = 2, .max = 16 },
  132. .m = { .min = 96, .max = 140 },
  133. .m1 = { .min = 18, .max = 26 },
  134. .m2 = { .min = 6, .max = 16 },
  135. .p = { .min = 4, .max = 128 },
  136. .p1 = { .min = 2, .max = 33 },
  137. .p2 = { .dot_limit = 165000,
  138. .p2_slow = 4, .p2_fast = 2 },
  139. };
  140. static const intel_limit_t intel_limits_i8xx_dvo = {
  141. .dot = { .min = 25000, .max = 350000 },
  142. .vco = { .min = 908000, .max = 1512000 },
  143. .n = { .min = 2, .max = 16 },
  144. .m = { .min = 96, .max = 140 },
  145. .m1 = { .min = 18, .max = 26 },
  146. .m2 = { .min = 6, .max = 16 },
  147. .p = { .min = 4, .max = 128 },
  148. .p1 = { .min = 2, .max = 33 },
  149. .p2 = { .dot_limit = 165000,
  150. .p2_slow = 4, .p2_fast = 4 },
  151. };
  152. static const intel_limit_t intel_limits_i8xx_lvds = {
  153. .dot = { .min = 25000, .max = 350000 },
  154. .vco = { .min = 908000, .max = 1512000 },
  155. .n = { .min = 2, .max = 16 },
  156. .m = { .min = 96, .max = 140 },
  157. .m1 = { .min = 18, .max = 26 },
  158. .m2 = { .min = 6, .max = 16 },
  159. .p = { .min = 4, .max = 128 },
  160. .p1 = { .min = 1, .max = 6 },
  161. .p2 = { .dot_limit = 165000,
  162. .p2_slow = 14, .p2_fast = 7 },
  163. };
  164. static const intel_limit_t intel_limits_i9xx_sdvo = {
  165. .dot = { .min = 20000, .max = 400000 },
  166. .vco = { .min = 1400000, .max = 2800000 },
  167. .n = { .min = 1, .max = 6 },
  168. .m = { .min = 70, .max = 120 },
  169. .m1 = { .min = 8, .max = 18 },
  170. .m2 = { .min = 3, .max = 7 },
  171. .p = { .min = 5, .max = 80 },
  172. .p1 = { .min = 1, .max = 8 },
  173. .p2 = { .dot_limit = 200000,
  174. .p2_slow = 10, .p2_fast = 5 },
  175. };
  176. static const intel_limit_t intel_limits_i9xx_lvds = {
  177. .dot = { .min = 20000, .max = 400000 },
  178. .vco = { .min = 1400000, .max = 2800000 },
  179. .n = { .min = 1, .max = 6 },
  180. .m = { .min = 70, .max = 120 },
  181. .m1 = { .min = 8, .max = 18 },
  182. .m2 = { .min = 3, .max = 7 },
  183. .p = { .min = 7, .max = 98 },
  184. .p1 = { .min = 1, .max = 8 },
  185. .p2 = { .dot_limit = 112000,
  186. .p2_slow = 14, .p2_fast = 7 },
  187. };
  188. static const intel_limit_t intel_limits_g4x_sdvo = {
  189. .dot = { .min = 25000, .max = 270000 },
  190. .vco = { .min = 1750000, .max = 3500000},
  191. .n = { .min = 1, .max = 4 },
  192. .m = { .min = 104, .max = 138 },
  193. .m1 = { .min = 17, .max = 23 },
  194. .m2 = { .min = 5, .max = 11 },
  195. .p = { .min = 10, .max = 30 },
  196. .p1 = { .min = 1, .max = 3},
  197. .p2 = { .dot_limit = 270000,
  198. .p2_slow = 10,
  199. .p2_fast = 10
  200. },
  201. };
  202. static const intel_limit_t intel_limits_g4x_hdmi = {
  203. .dot = { .min = 22000, .max = 400000 },
  204. .vco = { .min = 1750000, .max = 3500000},
  205. .n = { .min = 1, .max = 4 },
  206. .m = { .min = 104, .max = 138 },
  207. .m1 = { .min = 16, .max = 23 },
  208. .m2 = { .min = 5, .max = 11 },
  209. .p = { .min = 5, .max = 80 },
  210. .p1 = { .min = 1, .max = 8},
  211. .p2 = { .dot_limit = 165000,
  212. .p2_slow = 10, .p2_fast = 5 },
  213. };
  214. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  215. .dot = { .min = 20000, .max = 115000 },
  216. .vco = { .min = 1750000, .max = 3500000 },
  217. .n = { .min = 1, .max = 3 },
  218. .m = { .min = 104, .max = 138 },
  219. .m1 = { .min = 17, .max = 23 },
  220. .m2 = { .min = 5, .max = 11 },
  221. .p = { .min = 28, .max = 112 },
  222. .p1 = { .min = 2, .max = 8 },
  223. .p2 = { .dot_limit = 0,
  224. .p2_slow = 14, .p2_fast = 14
  225. },
  226. };
  227. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  228. .dot = { .min = 80000, .max = 224000 },
  229. .vco = { .min = 1750000, .max = 3500000 },
  230. .n = { .min = 1, .max = 3 },
  231. .m = { .min = 104, .max = 138 },
  232. .m1 = { .min = 17, .max = 23 },
  233. .m2 = { .min = 5, .max = 11 },
  234. .p = { .min = 14, .max = 42 },
  235. .p1 = { .min = 2, .max = 6 },
  236. .p2 = { .dot_limit = 0,
  237. .p2_slow = 7, .p2_fast = 7
  238. },
  239. };
  240. static const intel_limit_t intel_limits_pineview_sdvo = {
  241. .dot = { .min = 20000, .max = 400000},
  242. .vco = { .min = 1700000, .max = 3500000 },
  243. /* Pineview's Ncounter is a ring counter */
  244. .n = { .min = 3, .max = 6 },
  245. .m = { .min = 2, .max = 256 },
  246. /* Pineview only has one combined m divider, which we treat as m2. */
  247. .m1 = { .min = 0, .max = 0 },
  248. .m2 = { .min = 0, .max = 254 },
  249. .p = { .min = 5, .max = 80 },
  250. .p1 = { .min = 1, .max = 8 },
  251. .p2 = { .dot_limit = 200000,
  252. .p2_slow = 10, .p2_fast = 5 },
  253. };
  254. static const intel_limit_t intel_limits_pineview_lvds = {
  255. .dot = { .min = 20000, .max = 400000 },
  256. .vco = { .min = 1700000, .max = 3500000 },
  257. .n = { .min = 3, .max = 6 },
  258. .m = { .min = 2, .max = 256 },
  259. .m1 = { .min = 0, .max = 0 },
  260. .m2 = { .min = 0, .max = 254 },
  261. .p = { .min = 7, .max = 112 },
  262. .p1 = { .min = 1, .max = 8 },
  263. .p2 = { .dot_limit = 112000,
  264. .p2_slow = 14, .p2_fast = 14 },
  265. };
  266. /* Ironlake / Sandybridge
  267. *
  268. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  269. * the range value for them is (actual_value - 2).
  270. */
  271. static const intel_limit_t intel_limits_ironlake_dac = {
  272. .dot = { .min = 25000, .max = 350000 },
  273. .vco = { .min = 1760000, .max = 3510000 },
  274. .n = { .min = 1, .max = 5 },
  275. .m = { .min = 79, .max = 127 },
  276. .m1 = { .min = 12, .max = 22 },
  277. .m2 = { .min = 5, .max = 9 },
  278. .p = { .min = 5, .max = 80 },
  279. .p1 = { .min = 1, .max = 8 },
  280. .p2 = { .dot_limit = 225000,
  281. .p2_slow = 10, .p2_fast = 5 },
  282. };
  283. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  284. .dot = { .min = 25000, .max = 350000 },
  285. .vco = { .min = 1760000, .max = 3510000 },
  286. .n = { .min = 1, .max = 3 },
  287. .m = { .min = 79, .max = 118 },
  288. .m1 = { .min = 12, .max = 22 },
  289. .m2 = { .min = 5, .max = 9 },
  290. .p = { .min = 28, .max = 112 },
  291. .p1 = { .min = 2, .max = 8 },
  292. .p2 = { .dot_limit = 225000,
  293. .p2_slow = 14, .p2_fast = 14 },
  294. };
  295. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  296. .dot = { .min = 25000, .max = 350000 },
  297. .vco = { .min = 1760000, .max = 3510000 },
  298. .n = { .min = 1, .max = 3 },
  299. .m = { .min = 79, .max = 127 },
  300. .m1 = { .min = 12, .max = 22 },
  301. .m2 = { .min = 5, .max = 9 },
  302. .p = { .min = 14, .max = 56 },
  303. .p1 = { .min = 2, .max = 8 },
  304. .p2 = { .dot_limit = 225000,
  305. .p2_slow = 7, .p2_fast = 7 },
  306. };
  307. /* LVDS 100mhz refclk limits. */
  308. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  309. .dot = { .min = 25000, .max = 350000 },
  310. .vco = { .min = 1760000, .max = 3510000 },
  311. .n = { .min = 1, .max = 2 },
  312. .m = { .min = 79, .max = 126 },
  313. .m1 = { .min = 12, .max = 22 },
  314. .m2 = { .min = 5, .max = 9 },
  315. .p = { .min = 28, .max = 112 },
  316. .p1 = { .min = 2, .max = 8 },
  317. .p2 = { .dot_limit = 225000,
  318. .p2_slow = 14, .p2_fast = 14 },
  319. };
  320. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  321. .dot = { .min = 25000, .max = 350000 },
  322. .vco = { .min = 1760000, .max = 3510000 },
  323. .n = { .min = 1, .max = 3 },
  324. .m = { .min = 79, .max = 126 },
  325. .m1 = { .min = 12, .max = 22 },
  326. .m2 = { .min = 5, .max = 9 },
  327. .p = { .min = 14, .max = 42 },
  328. .p1 = { .min = 2, .max = 6 },
  329. .p2 = { .dot_limit = 225000,
  330. .p2_slow = 7, .p2_fast = 7 },
  331. };
  332. static const intel_limit_t intel_limits_vlv = {
  333. /*
  334. * These are the data rate limits (measured in fast clocks)
  335. * since those are the strictest limits we have. The fast
  336. * clock and actual rate limits are more relaxed, so checking
  337. * them would make no difference.
  338. */
  339. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  340. .vco = { .min = 4000000, .max = 6000000 },
  341. .n = { .min = 1, .max = 7 },
  342. .m1 = { .min = 2, .max = 3 },
  343. .m2 = { .min = 11, .max = 156 },
  344. .p1 = { .min = 2, .max = 3 },
  345. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  346. };
  347. static const intel_limit_t intel_limits_chv = {
  348. /*
  349. * These are the data rate limits (measured in fast clocks)
  350. * since those are the strictest limits we have. The fast
  351. * clock and actual rate limits are more relaxed, so checking
  352. * them would make no difference.
  353. */
  354. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  355. .vco = { .min = 4860000, .max = 6700000 },
  356. .n = { .min = 1, .max = 1 },
  357. .m1 = { .min = 2, .max = 2 },
  358. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  359. .p1 = { .min = 2, .max = 4 },
  360. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  361. };
  362. static void vlv_clock(int refclk, intel_clock_t *clock)
  363. {
  364. clock->m = clock->m1 * clock->m2;
  365. clock->p = clock->p1 * clock->p2;
  366. if (WARN_ON(clock->n == 0 || clock->p == 0))
  367. return;
  368. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  369. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  370. }
  371. /**
  372. * Returns whether any output on the specified pipe is of the specified type
  373. */
  374. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  375. {
  376. struct drm_device *dev = crtc->base.dev;
  377. struct intel_encoder *encoder;
  378. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  379. if (encoder->type == type)
  380. return true;
  381. return false;
  382. }
  383. /**
  384. * Returns whether any output on the specified pipe will have the specified
  385. * type after a staged modeset is complete, i.e., the same as
  386. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  387. * encoder->crtc.
  388. */
  389. static bool intel_pipe_will_have_type(struct intel_crtc *crtc, int type)
  390. {
  391. struct drm_device *dev = crtc->base.dev;
  392. struct intel_encoder *encoder;
  393. for_each_intel_encoder(dev, encoder)
  394. if (encoder->new_crtc == crtc && encoder->type == type)
  395. return true;
  396. return false;
  397. }
  398. static const intel_limit_t *intel_ironlake_limit(struct intel_crtc *crtc,
  399. int refclk)
  400. {
  401. struct drm_device *dev = crtc->base.dev;
  402. const intel_limit_t *limit;
  403. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  404. if (intel_is_dual_link_lvds(dev)) {
  405. if (refclk == 100000)
  406. limit = &intel_limits_ironlake_dual_lvds_100m;
  407. else
  408. limit = &intel_limits_ironlake_dual_lvds;
  409. } else {
  410. if (refclk == 100000)
  411. limit = &intel_limits_ironlake_single_lvds_100m;
  412. else
  413. limit = &intel_limits_ironlake_single_lvds;
  414. }
  415. } else
  416. limit = &intel_limits_ironlake_dac;
  417. return limit;
  418. }
  419. static const intel_limit_t *intel_g4x_limit(struct intel_crtc *crtc)
  420. {
  421. struct drm_device *dev = crtc->base.dev;
  422. const intel_limit_t *limit;
  423. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  424. if (intel_is_dual_link_lvds(dev))
  425. limit = &intel_limits_g4x_dual_channel_lvds;
  426. else
  427. limit = &intel_limits_g4x_single_channel_lvds;
  428. } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_HDMI) ||
  429. intel_pipe_will_have_type(crtc, INTEL_OUTPUT_ANALOG)) {
  430. limit = &intel_limits_g4x_hdmi;
  431. } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_SDVO)) {
  432. limit = &intel_limits_g4x_sdvo;
  433. } else /* The option is for other outputs */
  434. limit = &intel_limits_i9xx_sdvo;
  435. return limit;
  436. }
  437. static const intel_limit_t *intel_limit(struct intel_crtc *crtc, int refclk)
  438. {
  439. struct drm_device *dev = crtc->base.dev;
  440. const intel_limit_t *limit;
  441. if (HAS_PCH_SPLIT(dev))
  442. limit = intel_ironlake_limit(crtc, refclk);
  443. else if (IS_G4X(dev)) {
  444. limit = intel_g4x_limit(crtc);
  445. } else if (IS_PINEVIEW(dev)) {
  446. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  447. limit = &intel_limits_pineview_lvds;
  448. else
  449. limit = &intel_limits_pineview_sdvo;
  450. } else if (IS_CHERRYVIEW(dev)) {
  451. limit = &intel_limits_chv;
  452. } else if (IS_VALLEYVIEW(dev)) {
  453. limit = &intel_limits_vlv;
  454. } else if (!IS_GEN2(dev)) {
  455. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  456. limit = &intel_limits_i9xx_lvds;
  457. else
  458. limit = &intel_limits_i9xx_sdvo;
  459. } else {
  460. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  461. limit = &intel_limits_i8xx_lvds;
  462. else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_DVO))
  463. limit = &intel_limits_i8xx_dvo;
  464. else
  465. limit = &intel_limits_i8xx_dac;
  466. }
  467. return limit;
  468. }
  469. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  470. static void pineview_clock(int refclk, intel_clock_t *clock)
  471. {
  472. clock->m = clock->m2 + 2;
  473. clock->p = clock->p1 * clock->p2;
  474. if (WARN_ON(clock->n == 0 || clock->p == 0))
  475. return;
  476. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  477. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  478. }
  479. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  480. {
  481. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  482. }
  483. static void i9xx_clock(int refclk, intel_clock_t *clock)
  484. {
  485. clock->m = i9xx_dpll_compute_m(clock);
  486. clock->p = clock->p1 * clock->p2;
  487. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  488. return;
  489. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  490. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  491. }
  492. static void chv_clock(int refclk, intel_clock_t *clock)
  493. {
  494. clock->m = clock->m1 * clock->m2;
  495. clock->p = clock->p1 * clock->p2;
  496. if (WARN_ON(clock->n == 0 || clock->p == 0))
  497. return;
  498. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  499. clock->n << 22);
  500. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  501. }
  502. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  503. /**
  504. * Returns whether the given set of divisors are valid for a given refclk with
  505. * the given connectors.
  506. */
  507. static bool intel_PLL_is_valid(struct drm_device *dev,
  508. const intel_limit_t *limit,
  509. const intel_clock_t *clock)
  510. {
  511. if (clock->n < limit->n.min || limit->n.max < clock->n)
  512. INTELPllInvalid("n out of range\n");
  513. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  514. INTELPllInvalid("p1 out of range\n");
  515. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  516. INTELPllInvalid("m2 out of range\n");
  517. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  518. INTELPllInvalid("m1 out of range\n");
  519. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
  520. if (clock->m1 <= clock->m2)
  521. INTELPllInvalid("m1 <= m2\n");
  522. if (!IS_VALLEYVIEW(dev)) {
  523. if (clock->p < limit->p.min || limit->p.max < clock->p)
  524. INTELPllInvalid("p out of range\n");
  525. if (clock->m < limit->m.min || limit->m.max < clock->m)
  526. INTELPllInvalid("m out of range\n");
  527. }
  528. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  529. INTELPllInvalid("vco out of range\n");
  530. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  531. * connector, etc., rather than just a single range.
  532. */
  533. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  534. INTELPllInvalid("dot out of range\n");
  535. return true;
  536. }
  537. static bool
  538. i9xx_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  539. int target, int refclk, intel_clock_t *match_clock,
  540. intel_clock_t *best_clock)
  541. {
  542. struct drm_device *dev = crtc->base.dev;
  543. intel_clock_t clock;
  544. int err = target;
  545. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  546. /*
  547. * For LVDS just rely on its current settings for dual-channel.
  548. * We haven't figured out how to reliably set up different
  549. * single/dual channel state, if we even can.
  550. */
  551. if (intel_is_dual_link_lvds(dev))
  552. clock.p2 = limit->p2.p2_fast;
  553. else
  554. clock.p2 = limit->p2.p2_slow;
  555. } else {
  556. if (target < limit->p2.dot_limit)
  557. clock.p2 = limit->p2.p2_slow;
  558. else
  559. clock.p2 = limit->p2.p2_fast;
  560. }
  561. memset(best_clock, 0, sizeof(*best_clock));
  562. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  563. clock.m1++) {
  564. for (clock.m2 = limit->m2.min;
  565. clock.m2 <= limit->m2.max; clock.m2++) {
  566. if (clock.m2 >= clock.m1)
  567. break;
  568. for (clock.n = limit->n.min;
  569. clock.n <= limit->n.max; clock.n++) {
  570. for (clock.p1 = limit->p1.min;
  571. clock.p1 <= limit->p1.max; clock.p1++) {
  572. int this_err;
  573. i9xx_clock(refclk, &clock);
  574. if (!intel_PLL_is_valid(dev, limit,
  575. &clock))
  576. continue;
  577. if (match_clock &&
  578. clock.p != match_clock->p)
  579. continue;
  580. this_err = abs(clock.dot - target);
  581. if (this_err < err) {
  582. *best_clock = clock;
  583. err = this_err;
  584. }
  585. }
  586. }
  587. }
  588. }
  589. return (err != target);
  590. }
  591. static bool
  592. pnv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  593. int target, int refclk, intel_clock_t *match_clock,
  594. intel_clock_t *best_clock)
  595. {
  596. struct drm_device *dev = crtc->base.dev;
  597. intel_clock_t clock;
  598. int err = target;
  599. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  600. /*
  601. * For LVDS just rely on its current settings for dual-channel.
  602. * We haven't figured out how to reliably set up different
  603. * single/dual channel state, if we even can.
  604. */
  605. if (intel_is_dual_link_lvds(dev))
  606. clock.p2 = limit->p2.p2_fast;
  607. else
  608. clock.p2 = limit->p2.p2_slow;
  609. } else {
  610. if (target < limit->p2.dot_limit)
  611. clock.p2 = limit->p2.p2_slow;
  612. else
  613. clock.p2 = limit->p2.p2_fast;
  614. }
  615. memset(best_clock, 0, sizeof(*best_clock));
  616. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  617. clock.m1++) {
  618. for (clock.m2 = limit->m2.min;
  619. clock.m2 <= limit->m2.max; clock.m2++) {
  620. for (clock.n = limit->n.min;
  621. clock.n <= limit->n.max; clock.n++) {
  622. for (clock.p1 = limit->p1.min;
  623. clock.p1 <= limit->p1.max; clock.p1++) {
  624. int this_err;
  625. pineview_clock(refclk, &clock);
  626. if (!intel_PLL_is_valid(dev, limit,
  627. &clock))
  628. continue;
  629. if (match_clock &&
  630. clock.p != match_clock->p)
  631. continue;
  632. this_err = abs(clock.dot - target);
  633. if (this_err < err) {
  634. *best_clock = clock;
  635. err = this_err;
  636. }
  637. }
  638. }
  639. }
  640. }
  641. return (err != target);
  642. }
  643. static bool
  644. g4x_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  645. int target, int refclk, intel_clock_t *match_clock,
  646. intel_clock_t *best_clock)
  647. {
  648. struct drm_device *dev = crtc->base.dev;
  649. intel_clock_t clock;
  650. int max_n;
  651. bool found;
  652. /* approximately equals target * 0.00585 */
  653. int err_most = (target >> 8) + (target >> 9);
  654. found = false;
  655. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  656. if (intel_is_dual_link_lvds(dev))
  657. clock.p2 = limit->p2.p2_fast;
  658. else
  659. clock.p2 = limit->p2.p2_slow;
  660. } else {
  661. if (target < limit->p2.dot_limit)
  662. clock.p2 = limit->p2.p2_slow;
  663. else
  664. clock.p2 = limit->p2.p2_fast;
  665. }
  666. memset(best_clock, 0, sizeof(*best_clock));
  667. max_n = limit->n.max;
  668. /* based on hardware requirement, prefer smaller n to precision */
  669. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  670. /* based on hardware requirement, prefere larger m1,m2 */
  671. for (clock.m1 = limit->m1.max;
  672. clock.m1 >= limit->m1.min; clock.m1--) {
  673. for (clock.m2 = limit->m2.max;
  674. clock.m2 >= limit->m2.min; clock.m2--) {
  675. for (clock.p1 = limit->p1.max;
  676. clock.p1 >= limit->p1.min; clock.p1--) {
  677. int this_err;
  678. i9xx_clock(refclk, &clock);
  679. if (!intel_PLL_is_valid(dev, limit,
  680. &clock))
  681. continue;
  682. this_err = abs(clock.dot - target);
  683. if (this_err < err_most) {
  684. *best_clock = clock;
  685. err_most = this_err;
  686. max_n = clock.n;
  687. found = true;
  688. }
  689. }
  690. }
  691. }
  692. }
  693. return found;
  694. }
  695. static bool
  696. vlv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  697. int target, int refclk, intel_clock_t *match_clock,
  698. intel_clock_t *best_clock)
  699. {
  700. struct drm_device *dev = crtc->base.dev;
  701. intel_clock_t clock;
  702. unsigned int bestppm = 1000000;
  703. /* min update 19.2 MHz */
  704. int max_n = min(limit->n.max, refclk / 19200);
  705. bool found = false;
  706. target *= 5; /* fast clock */
  707. memset(best_clock, 0, sizeof(*best_clock));
  708. /* based on hardware requirement, prefer smaller n to precision */
  709. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  710. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  711. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  712. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  713. clock.p = clock.p1 * clock.p2;
  714. /* based on hardware requirement, prefer bigger m1,m2 values */
  715. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  716. unsigned int ppm, diff;
  717. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  718. refclk * clock.m1);
  719. vlv_clock(refclk, &clock);
  720. if (!intel_PLL_is_valid(dev, limit,
  721. &clock))
  722. continue;
  723. diff = abs(clock.dot - target);
  724. ppm = div_u64(1000000ULL * diff, target);
  725. if (ppm < 100 && clock.p > best_clock->p) {
  726. bestppm = 0;
  727. *best_clock = clock;
  728. found = true;
  729. }
  730. if (bestppm >= 10 && ppm < bestppm - 10) {
  731. bestppm = ppm;
  732. *best_clock = clock;
  733. found = true;
  734. }
  735. }
  736. }
  737. }
  738. }
  739. return found;
  740. }
  741. static bool
  742. chv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  743. int target, int refclk, intel_clock_t *match_clock,
  744. intel_clock_t *best_clock)
  745. {
  746. struct drm_device *dev = crtc->base.dev;
  747. intel_clock_t clock;
  748. uint64_t m2;
  749. int found = false;
  750. memset(best_clock, 0, sizeof(*best_clock));
  751. /*
  752. * Based on hardware doc, the n always set to 1, and m1 always
  753. * set to 2. If requires to support 200Mhz refclk, we need to
  754. * revisit this because n may not 1 anymore.
  755. */
  756. clock.n = 1, clock.m1 = 2;
  757. target *= 5; /* fast clock */
  758. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  759. for (clock.p2 = limit->p2.p2_fast;
  760. clock.p2 >= limit->p2.p2_slow;
  761. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  762. clock.p = clock.p1 * clock.p2;
  763. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  764. clock.n) << 22, refclk * clock.m1);
  765. if (m2 > INT_MAX/clock.m1)
  766. continue;
  767. clock.m2 = m2;
  768. chv_clock(refclk, &clock);
  769. if (!intel_PLL_is_valid(dev, limit, &clock))
  770. continue;
  771. /* based on hardware requirement, prefer bigger p
  772. */
  773. if (clock.p > best_clock->p) {
  774. *best_clock = clock;
  775. found = true;
  776. }
  777. }
  778. }
  779. return found;
  780. }
  781. bool intel_crtc_active(struct drm_crtc *crtc)
  782. {
  783. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  784. /* Be paranoid as we can arrive here with only partial
  785. * state retrieved from the hardware during setup.
  786. *
  787. * We can ditch the adjusted_mode.crtc_clock check as soon
  788. * as Haswell has gained clock readout/fastboot support.
  789. *
  790. * We can ditch the crtc->primary->fb check as soon as we can
  791. * properly reconstruct framebuffers.
  792. */
  793. return intel_crtc->active && crtc->primary->fb &&
  794. intel_crtc->config.adjusted_mode.crtc_clock;
  795. }
  796. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  797. enum pipe pipe)
  798. {
  799. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  800. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  801. return intel_crtc->config.cpu_transcoder;
  802. }
  803. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  804. {
  805. struct drm_i915_private *dev_priv = dev->dev_private;
  806. u32 reg = PIPEDSL(pipe);
  807. u32 line1, line2;
  808. u32 line_mask;
  809. if (IS_GEN2(dev))
  810. line_mask = DSL_LINEMASK_GEN2;
  811. else
  812. line_mask = DSL_LINEMASK_GEN3;
  813. line1 = I915_READ(reg) & line_mask;
  814. mdelay(5);
  815. line2 = I915_READ(reg) & line_mask;
  816. return line1 == line2;
  817. }
  818. /*
  819. * intel_wait_for_pipe_off - wait for pipe to turn off
  820. * @crtc: crtc whose pipe to wait for
  821. *
  822. * After disabling a pipe, we can't wait for vblank in the usual way,
  823. * spinning on the vblank interrupt status bit, since we won't actually
  824. * see an interrupt when the pipe is disabled.
  825. *
  826. * On Gen4 and above:
  827. * wait for the pipe register state bit to turn off
  828. *
  829. * Otherwise:
  830. * wait for the display line value to settle (it usually
  831. * ends up stopping at the start of the next frame).
  832. *
  833. */
  834. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  835. {
  836. struct drm_device *dev = crtc->base.dev;
  837. struct drm_i915_private *dev_priv = dev->dev_private;
  838. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  839. enum pipe pipe = crtc->pipe;
  840. if (INTEL_INFO(dev)->gen >= 4) {
  841. int reg = PIPECONF(cpu_transcoder);
  842. /* Wait for the Pipe State to go off */
  843. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  844. 100))
  845. WARN(1, "pipe_off wait timed out\n");
  846. } else {
  847. /* Wait for the display line to settle */
  848. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  849. WARN(1, "pipe_off wait timed out\n");
  850. }
  851. }
  852. /*
  853. * ibx_digital_port_connected - is the specified port connected?
  854. * @dev_priv: i915 private structure
  855. * @port: the port to test
  856. *
  857. * Returns true if @port is connected, false otherwise.
  858. */
  859. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  860. struct intel_digital_port *port)
  861. {
  862. u32 bit;
  863. if (HAS_PCH_IBX(dev_priv->dev)) {
  864. switch (port->port) {
  865. case PORT_B:
  866. bit = SDE_PORTB_HOTPLUG;
  867. break;
  868. case PORT_C:
  869. bit = SDE_PORTC_HOTPLUG;
  870. break;
  871. case PORT_D:
  872. bit = SDE_PORTD_HOTPLUG;
  873. break;
  874. default:
  875. return true;
  876. }
  877. } else {
  878. switch (port->port) {
  879. case PORT_B:
  880. bit = SDE_PORTB_HOTPLUG_CPT;
  881. break;
  882. case PORT_C:
  883. bit = SDE_PORTC_HOTPLUG_CPT;
  884. break;
  885. case PORT_D:
  886. bit = SDE_PORTD_HOTPLUG_CPT;
  887. break;
  888. default:
  889. return true;
  890. }
  891. }
  892. return I915_READ(SDEISR) & bit;
  893. }
  894. static const char *state_string(bool enabled)
  895. {
  896. return enabled ? "on" : "off";
  897. }
  898. /* Only for pre-ILK configs */
  899. void assert_pll(struct drm_i915_private *dev_priv,
  900. enum pipe pipe, bool state)
  901. {
  902. int reg;
  903. u32 val;
  904. bool cur_state;
  905. reg = DPLL(pipe);
  906. val = I915_READ(reg);
  907. cur_state = !!(val & DPLL_VCO_ENABLE);
  908. WARN(cur_state != state,
  909. "PLL state assertion failure (expected %s, current %s)\n",
  910. state_string(state), state_string(cur_state));
  911. }
  912. /* XXX: the dsi pll is shared between MIPI DSI ports */
  913. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  914. {
  915. u32 val;
  916. bool cur_state;
  917. mutex_lock(&dev_priv->dpio_lock);
  918. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  919. mutex_unlock(&dev_priv->dpio_lock);
  920. cur_state = val & DSI_PLL_VCO_EN;
  921. WARN(cur_state != state,
  922. "DSI PLL state assertion failure (expected %s, current %s)\n",
  923. state_string(state), state_string(cur_state));
  924. }
  925. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  926. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  927. struct intel_shared_dpll *
  928. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  929. {
  930. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  931. if (crtc->config.shared_dpll < 0)
  932. return NULL;
  933. return &dev_priv->shared_dplls[crtc->config.shared_dpll];
  934. }
  935. /* For ILK+ */
  936. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  937. struct intel_shared_dpll *pll,
  938. bool state)
  939. {
  940. bool cur_state;
  941. struct intel_dpll_hw_state hw_state;
  942. if (WARN (!pll,
  943. "asserting DPLL %s with no DPLL\n", state_string(state)))
  944. return;
  945. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  946. WARN(cur_state != state,
  947. "%s assertion failure (expected %s, current %s)\n",
  948. pll->name, state_string(state), state_string(cur_state));
  949. }
  950. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  951. enum pipe pipe, bool state)
  952. {
  953. int reg;
  954. u32 val;
  955. bool cur_state;
  956. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  957. pipe);
  958. if (HAS_DDI(dev_priv->dev)) {
  959. /* DDI does not have a specific FDI_TX register */
  960. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  961. val = I915_READ(reg);
  962. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  963. } else {
  964. reg = FDI_TX_CTL(pipe);
  965. val = I915_READ(reg);
  966. cur_state = !!(val & FDI_TX_ENABLE);
  967. }
  968. WARN(cur_state != state,
  969. "FDI TX state assertion failure (expected %s, current %s)\n",
  970. state_string(state), state_string(cur_state));
  971. }
  972. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  973. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  974. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  975. enum pipe pipe, bool state)
  976. {
  977. int reg;
  978. u32 val;
  979. bool cur_state;
  980. reg = FDI_RX_CTL(pipe);
  981. val = I915_READ(reg);
  982. cur_state = !!(val & FDI_RX_ENABLE);
  983. WARN(cur_state != state,
  984. "FDI RX state assertion failure (expected %s, current %s)\n",
  985. state_string(state), state_string(cur_state));
  986. }
  987. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  988. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  989. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  990. enum pipe pipe)
  991. {
  992. int reg;
  993. u32 val;
  994. /* ILK FDI PLL is always enabled */
  995. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  996. return;
  997. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  998. if (HAS_DDI(dev_priv->dev))
  999. return;
  1000. reg = FDI_TX_CTL(pipe);
  1001. val = I915_READ(reg);
  1002. WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1003. }
  1004. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1005. enum pipe pipe, bool state)
  1006. {
  1007. int reg;
  1008. u32 val;
  1009. bool cur_state;
  1010. reg = FDI_RX_CTL(pipe);
  1011. val = I915_READ(reg);
  1012. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1013. WARN(cur_state != state,
  1014. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1015. state_string(state), state_string(cur_state));
  1016. }
  1017. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1018. enum pipe pipe)
  1019. {
  1020. struct drm_device *dev = dev_priv->dev;
  1021. int pp_reg;
  1022. u32 val;
  1023. enum pipe panel_pipe = PIPE_A;
  1024. bool locked = true;
  1025. if (WARN_ON(HAS_DDI(dev)))
  1026. return;
  1027. if (HAS_PCH_SPLIT(dev)) {
  1028. u32 port_sel;
  1029. pp_reg = PCH_PP_CONTROL;
  1030. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1031. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1032. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1033. panel_pipe = PIPE_B;
  1034. /* XXX: else fix for eDP */
  1035. } else if (IS_VALLEYVIEW(dev)) {
  1036. /* presumably write lock depends on pipe, not port select */
  1037. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1038. panel_pipe = pipe;
  1039. } else {
  1040. pp_reg = PP_CONTROL;
  1041. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1042. panel_pipe = PIPE_B;
  1043. }
  1044. val = I915_READ(pp_reg);
  1045. if (!(val & PANEL_POWER_ON) ||
  1046. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1047. locked = false;
  1048. WARN(panel_pipe == pipe && locked,
  1049. "panel assertion failure, pipe %c regs locked\n",
  1050. pipe_name(pipe));
  1051. }
  1052. static void assert_cursor(struct drm_i915_private *dev_priv,
  1053. enum pipe pipe, bool state)
  1054. {
  1055. struct drm_device *dev = dev_priv->dev;
  1056. bool cur_state;
  1057. if (IS_845G(dev) || IS_I865G(dev))
  1058. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1059. else
  1060. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1061. WARN(cur_state != state,
  1062. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1063. pipe_name(pipe), state_string(state), state_string(cur_state));
  1064. }
  1065. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1066. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1067. void assert_pipe(struct drm_i915_private *dev_priv,
  1068. enum pipe pipe, bool state)
  1069. {
  1070. int reg;
  1071. u32 val;
  1072. bool cur_state;
  1073. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1074. pipe);
  1075. /* if we need the pipe quirk it must be always on */
  1076. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1077. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1078. state = true;
  1079. if (!intel_display_power_is_enabled(dev_priv,
  1080. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1081. cur_state = false;
  1082. } else {
  1083. reg = PIPECONF(cpu_transcoder);
  1084. val = I915_READ(reg);
  1085. cur_state = !!(val & PIPECONF_ENABLE);
  1086. }
  1087. WARN(cur_state != state,
  1088. "pipe %c assertion failure (expected %s, current %s)\n",
  1089. pipe_name(pipe), state_string(state), state_string(cur_state));
  1090. }
  1091. static void assert_plane(struct drm_i915_private *dev_priv,
  1092. enum plane plane, bool state)
  1093. {
  1094. int reg;
  1095. u32 val;
  1096. bool cur_state;
  1097. reg = DSPCNTR(plane);
  1098. val = I915_READ(reg);
  1099. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1100. WARN(cur_state != state,
  1101. "plane %c assertion failure (expected %s, current %s)\n",
  1102. plane_name(plane), state_string(state), state_string(cur_state));
  1103. }
  1104. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1105. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1106. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1107. enum pipe pipe)
  1108. {
  1109. struct drm_device *dev = dev_priv->dev;
  1110. int reg, i;
  1111. u32 val;
  1112. int cur_pipe;
  1113. /* Primary planes are fixed to pipes on gen4+ */
  1114. if (INTEL_INFO(dev)->gen >= 4) {
  1115. reg = DSPCNTR(pipe);
  1116. val = I915_READ(reg);
  1117. WARN(val & DISPLAY_PLANE_ENABLE,
  1118. "plane %c assertion failure, should be disabled but not\n",
  1119. plane_name(pipe));
  1120. return;
  1121. }
  1122. /* Need to check both planes against the pipe */
  1123. for_each_pipe(dev_priv, i) {
  1124. reg = DSPCNTR(i);
  1125. val = I915_READ(reg);
  1126. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1127. DISPPLANE_SEL_PIPE_SHIFT;
  1128. WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1129. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1130. plane_name(i), pipe_name(pipe));
  1131. }
  1132. }
  1133. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1134. enum pipe pipe)
  1135. {
  1136. struct drm_device *dev = dev_priv->dev;
  1137. int reg, sprite;
  1138. u32 val;
  1139. if (INTEL_INFO(dev)->gen >= 9) {
  1140. for_each_sprite(pipe, sprite) {
  1141. val = I915_READ(PLANE_CTL(pipe, sprite));
  1142. WARN(val & PLANE_CTL_ENABLE,
  1143. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1144. sprite, pipe_name(pipe));
  1145. }
  1146. } else if (IS_VALLEYVIEW(dev)) {
  1147. for_each_sprite(pipe, sprite) {
  1148. reg = SPCNTR(pipe, sprite);
  1149. val = I915_READ(reg);
  1150. WARN(val & SP_ENABLE,
  1151. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1152. sprite_name(pipe, sprite), pipe_name(pipe));
  1153. }
  1154. } else if (INTEL_INFO(dev)->gen >= 7) {
  1155. reg = SPRCTL(pipe);
  1156. val = I915_READ(reg);
  1157. WARN(val & SPRITE_ENABLE,
  1158. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1159. plane_name(pipe), pipe_name(pipe));
  1160. } else if (INTEL_INFO(dev)->gen >= 5) {
  1161. reg = DVSCNTR(pipe);
  1162. val = I915_READ(reg);
  1163. WARN(val & DVS_ENABLE,
  1164. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1165. plane_name(pipe), pipe_name(pipe));
  1166. }
  1167. }
  1168. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1169. {
  1170. if (WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1171. drm_crtc_vblank_put(crtc);
  1172. }
  1173. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1174. {
  1175. u32 val;
  1176. bool enabled;
  1177. WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1178. val = I915_READ(PCH_DREF_CONTROL);
  1179. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1180. DREF_SUPERSPREAD_SOURCE_MASK));
  1181. WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1182. }
  1183. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1184. enum pipe pipe)
  1185. {
  1186. int reg;
  1187. u32 val;
  1188. bool enabled;
  1189. reg = PCH_TRANSCONF(pipe);
  1190. val = I915_READ(reg);
  1191. enabled = !!(val & TRANS_ENABLE);
  1192. WARN(enabled,
  1193. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1194. pipe_name(pipe));
  1195. }
  1196. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1197. enum pipe pipe, u32 port_sel, u32 val)
  1198. {
  1199. if ((val & DP_PORT_EN) == 0)
  1200. return false;
  1201. if (HAS_PCH_CPT(dev_priv->dev)) {
  1202. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1203. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1204. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1205. return false;
  1206. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1207. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1208. return false;
  1209. } else {
  1210. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1211. return false;
  1212. }
  1213. return true;
  1214. }
  1215. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1216. enum pipe pipe, u32 val)
  1217. {
  1218. if ((val & SDVO_ENABLE) == 0)
  1219. return false;
  1220. if (HAS_PCH_CPT(dev_priv->dev)) {
  1221. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1222. return false;
  1223. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1224. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1225. return false;
  1226. } else {
  1227. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1228. return false;
  1229. }
  1230. return true;
  1231. }
  1232. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1233. enum pipe pipe, u32 val)
  1234. {
  1235. if ((val & LVDS_PORT_EN) == 0)
  1236. return false;
  1237. if (HAS_PCH_CPT(dev_priv->dev)) {
  1238. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1239. return false;
  1240. } else {
  1241. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1242. return false;
  1243. }
  1244. return true;
  1245. }
  1246. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1247. enum pipe pipe, u32 val)
  1248. {
  1249. if ((val & ADPA_DAC_ENABLE) == 0)
  1250. return false;
  1251. if (HAS_PCH_CPT(dev_priv->dev)) {
  1252. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1253. return false;
  1254. } else {
  1255. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1256. return false;
  1257. }
  1258. return true;
  1259. }
  1260. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1261. enum pipe pipe, int reg, u32 port_sel)
  1262. {
  1263. u32 val = I915_READ(reg);
  1264. WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1265. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1266. reg, pipe_name(pipe));
  1267. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1268. && (val & DP_PIPEB_SELECT),
  1269. "IBX PCH dp port still using transcoder B\n");
  1270. }
  1271. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1272. enum pipe pipe, int reg)
  1273. {
  1274. u32 val = I915_READ(reg);
  1275. WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1276. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1277. reg, pipe_name(pipe));
  1278. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1279. && (val & SDVO_PIPE_B_SELECT),
  1280. "IBX PCH hdmi port still using transcoder B\n");
  1281. }
  1282. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1283. enum pipe pipe)
  1284. {
  1285. int reg;
  1286. u32 val;
  1287. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1288. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1289. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1290. reg = PCH_ADPA;
  1291. val = I915_READ(reg);
  1292. WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1293. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1294. pipe_name(pipe));
  1295. reg = PCH_LVDS;
  1296. val = I915_READ(reg);
  1297. WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1298. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1299. pipe_name(pipe));
  1300. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1301. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1302. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1303. }
  1304. static void intel_init_dpio(struct drm_device *dev)
  1305. {
  1306. struct drm_i915_private *dev_priv = dev->dev_private;
  1307. if (!IS_VALLEYVIEW(dev))
  1308. return;
  1309. /*
  1310. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1311. * CHV x1 PHY (DP/HDMI D)
  1312. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1313. */
  1314. if (IS_CHERRYVIEW(dev)) {
  1315. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1316. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1317. } else {
  1318. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1319. }
  1320. }
  1321. static void vlv_enable_pll(struct intel_crtc *crtc,
  1322. const struct intel_crtc_config *pipe_config)
  1323. {
  1324. struct drm_device *dev = crtc->base.dev;
  1325. struct drm_i915_private *dev_priv = dev->dev_private;
  1326. int reg = DPLL(crtc->pipe);
  1327. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1328. assert_pipe_disabled(dev_priv, crtc->pipe);
  1329. /* No really, not for ILK+ */
  1330. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1331. /* PLL is protected by panel, make sure we can write it */
  1332. if (IS_MOBILE(dev_priv->dev))
  1333. assert_panel_unlocked(dev_priv, crtc->pipe);
  1334. I915_WRITE(reg, dpll);
  1335. POSTING_READ(reg);
  1336. udelay(150);
  1337. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1338. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1339. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1340. POSTING_READ(DPLL_MD(crtc->pipe));
  1341. /* We do this three times for luck */
  1342. I915_WRITE(reg, dpll);
  1343. POSTING_READ(reg);
  1344. udelay(150); /* wait for warmup */
  1345. I915_WRITE(reg, dpll);
  1346. POSTING_READ(reg);
  1347. udelay(150); /* wait for warmup */
  1348. I915_WRITE(reg, dpll);
  1349. POSTING_READ(reg);
  1350. udelay(150); /* wait for warmup */
  1351. }
  1352. static void chv_enable_pll(struct intel_crtc *crtc,
  1353. const struct intel_crtc_config *pipe_config)
  1354. {
  1355. struct drm_device *dev = crtc->base.dev;
  1356. struct drm_i915_private *dev_priv = dev->dev_private;
  1357. int pipe = crtc->pipe;
  1358. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1359. u32 tmp;
  1360. assert_pipe_disabled(dev_priv, crtc->pipe);
  1361. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1362. mutex_lock(&dev_priv->dpio_lock);
  1363. /* Enable back the 10bit clock to display controller */
  1364. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1365. tmp |= DPIO_DCLKP_EN;
  1366. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1367. /*
  1368. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1369. */
  1370. udelay(1);
  1371. /* Enable PLL */
  1372. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1373. /* Check PLL is locked */
  1374. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1375. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1376. /* not sure when this should be written */
  1377. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1378. POSTING_READ(DPLL_MD(pipe));
  1379. mutex_unlock(&dev_priv->dpio_lock);
  1380. }
  1381. static int intel_num_dvo_pipes(struct drm_device *dev)
  1382. {
  1383. struct intel_crtc *crtc;
  1384. int count = 0;
  1385. for_each_intel_crtc(dev, crtc)
  1386. count += crtc->active &&
  1387. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1388. return count;
  1389. }
  1390. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1391. {
  1392. struct drm_device *dev = crtc->base.dev;
  1393. struct drm_i915_private *dev_priv = dev->dev_private;
  1394. int reg = DPLL(crtc->pipe);
  1395. u32 dpll = crtc->config.dpll_hw_state.dpll;
  1396. assert_pipe_disabled(dev_priv, crtc->pipe);
  1397. /* No really, not for ILK+ */
  1398. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1399. /* PLL is protected by panel, make sure we can write it */
  1400. if (IS_MOBILE(dev) && !IS_I830(dev))
  1401. assert_panel_unlocked(dev_priv, crtc->pipe);
  1402. /* Enable DVO 2x clock on both PLLs if necessary */
  1403. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1404. /*
  1405. * It appears to be important that we don't enable this
  1406. * for the current pipe before otherwise configuring the
  1407. * PLL. No idea how this should be handled if multiple
  1408. * DVO outputs are enabled simultaneosly.
  1409. */
  1410. dpll |= DPLL_DVO_2X_MODE;
  1411. I915_WRITE(DPLL(!crtc->pipe),
  1412. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1413. }
  1414. /* Wait for the clocks to stabilize. */
  1415. POSTING_READ(reg);
  1416. udelay(150);
  1417. if (INTEL_INFO(dev)->gen >= 4) {
  1418. I915_WRITE(DPLL_MD(crtc->pipe),
  1419. crtc->config.dpll_hw_state.dpll_md);
  1420. } else {
  1421. /* The pixel multiplier can only be updated once the
  1422. * DPLL is enabled and the clocks are stable.
  1423. *
  1424. * So write it again.
  1425. */
  1426. I915_WRITE(reg, dpll);
  1427. }
  1428. /* We do this three times for luck */
  1429. I915_WRITE(reg, dpll);
  1430. POSTING_READ(reg);
  1431. udelay(150); /* wait for warmup */
  1432. I915_WRITE(reg, dpll);
  1433. POSTING_READ(reg);
  1434. udelay(150); /* wait for warmup */
  1435. I915_WRITE(reg, dpll);
  1436. POSTING_READ(reg);
  1437. udelay(150); /* wait for warmup */
  1438. }
  1439. /**
  1440. * i9xx_disable_pll - disable a PLL
  1441. * @dev_priv: i915 private structure
  1442. * @pipe: pipe PLL to disable
  1443. *
  1444. * Disable the PLL for @pipe, making sure the pipe is off first.
  1445. *
  1446. * Note! This is for pre-ILK only.
  1447. */
  1448. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1449. {
  1450. struct drm_device *dev = crtc->base.dev;
  1451. struct drm_i915_private *dev_priv = dev->dev_private;
  1452. enum pipe pipe = crtc->pipe;
  1453. /* Disable DVO 2x clock on both PLLs if necessary */
  1454. if (IS_I830(dev) &&
  1455. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1456. intel_num_dvo_pipes(dev) == 1) {
  1457. I915_WRITE(DPLL(PIPE_B),
  1458. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1459. I915_WRITE(DPLL(PIPE_A),
  1460. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1461. }
  1462. /* Don't disable pipe or pipe PLLs if needed */
  1463. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1464. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1465. return;
  1466. /* Make sure the pipe isn't still relying on us */
  1467. assert_pipe_disabled(dev_priv, pipe);
  1468. I915_WRITE(DPLL(pipe), 0);
  1469. POSTING_READ(DPLL(pipe));
  1470. }
  1471. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1472. {
  1473. u32 val = 0;
  1474. /* Make sure the pipe isn't still relying on us */
  1475. assert_pipe_disabled(dev_priv, pipe);
  1476. /*
  1477. * Leave integrated clock source and reference clock enabled for pipe B.
  1478. * The latter is needed for VGA hotplug / manual detection.
  1479. */
  1480. if (pipe == PIPE_B)
  1481. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1482. I915_WRITE(DPLL(pipe), val);
  1483. POSTING_READ(DPLL(pipe));
  1484. }
  1485. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1486. {
  1487. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1488. u32 val;
  1489. /* Make sure the pipe isn't still relying on us */
  1490. assert_pipe_disabled(dev_priv, pipe);
  1491. /* Set PLL en = 0 */
  1492. val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
  1493. if (pipe != PIPE_A)
  1494. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1495. I915_WRITE(DPLL(pipe), val);
  1496. POSTING_READ(DPLL(pipe));
  1497. mutex_lock(&dev_priv->dpio_lock);
  1498. /* Disable 10bit clock to display controller */
  1499. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1500. val &= ~DPIO_DCLKP_EN;
  1501. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1502. /* disable left/right clock distribution */
  1503. if (pipe != PIPE_B) {
  1504. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1505. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1506. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1507. } else {
  1508. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1509. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1510. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1511. }
  1512. mutex_unlock(&dev_priv->dpio_lock);
  1513. }
  1514. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1515. struct intel_digital_port *dport)
  1516. {
  1517. u32 port_mask;
  1518. int dpll_reg;
  1519. switch (dport->port) {
  1520. case PORT_B:
  1521. port_mask = DPLL_PORTB_READY_MASK;
  1522. dpll_reg = DPLL(0);
  1523. break;
  1524. case PORT_C:
  1525. port_mask = DPLL_PORTC_READY_MASK;
  1526. dpll_reg = DPLL(0);
  1527. break;
  1528. case PORT_D:
  1529. port_mask = DPLL_PORTD_READY_MASK;
  1530. dpll_reg = DPIO_PHY_STATUS;
  1531. break;
  1532. default:
  1533. BUG();
  1534. }
  1535. if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
  1536. WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
  1537. port_name(dport->port), I915_READ(dpll_reg));
  1538. }
  1539. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1540. {
  1541. struct drm_device *dev = crtc->base.dev;
  1542. struct drm_i915_private *dev_priv = dev->dev_private;
  1543. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1544. if (WARN_ON(pll == NULL))
  1545. return;
  1546. WARN_ON(!pll->config.crtc_mask);
  1547. if (pll->active == 0) {
  1548. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1549. WARN_ON(pll->on);
  1550. assert_shared_dpll_disabled(dev_priv, pll);
  1551. pll->mode_set(dev_priv, pll);
  1552. }
  1553. }
  1554. /**
  1555. * intel_enable_shared_dpll - enable PCH PLL
  1556. * @dev_priv: i915 private structure
  1557. * @pipe: pipe PLL to enable
  1558. *
  1559. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1560. * drives the transcoder clock.
  1561. */
  1562. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1563. {
  1564. struct drm_device *dev = crtc->base.dev;
  1565. struct drm_i915_private *dev_priv = dev->dev_private;
  1566. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1567. if (WARN_ON(pll == NULL))
  1568. return;
  1569. if (WARN_ON(pll->config.crtc_mask == 0))
  1570. return;
  1571. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1572. pll->name, pll->active, pll->on,
  1573. crtc->base.base.id);
  1574. if (pll->active++) {
  1575. WARN_ON(!pll->on);
  1576. assert_shared_dpll_enabled(dev_priv, pll);
  1577. return;
  1578. }
  1579. WARN_ON(pll->on);
  1580. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1581. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1582. pll->enable(dev_priv, pll);
  1583. pll->on = true;
  1584. }
  1585. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1586. {
  1587. struct drm_device *dev = crtc->base.dev;
  1588. struct drm_i915_private *dev_priv = dev->dev_private;
  1589. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1590. /* PCH only available on ILK+ */
  1591. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1592. if (WARN_ON(pll == NULL))
  1593. return;
  1594. if (WARN_ON(pll->config.crtc_mask == 0))
  1595. return;
  1596. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1597. pll->name, pll->active, pll->on,
  1598. crtc->base.base.id);
  1599. if (WARN_ON(pll->active == 0)) {
  1600. assert_shared_dpll_disabled(dev_priv, pll);
  1601. return;
  1602. }
  1603. assert_shared_dpll_enabled(dev_priv, pll);
  1604. WARN_ON(!pll->on);
  1605. if (--pll->active)
  1606. return;
  1607. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1608. pll->disable(dev_priv, pll);
  1609. pll->on = false;
  1610. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1611. }
  1612. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1613. enum pipe pipe)
  1614. {
  1615. struct drm_device *dev = dev_priv->dev;
  1616. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1617. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1618. uint32_t reg, val, pipeconf_val;
  1619. /* PCH only available on ILK+ */
  1620. BUG_ON(!HAS_PCH_SPLIT(dev));
  1621. /* Make sure PCH DPLL is enabled */
  1622. assert_shared_dpll_enabled(dev_priv,
  1623. intel_crtc_to_shared_dpll(intel_crtc));
  1624. /* FDI must be feeding us bits for PCH ports */
  1625. assert_fdi_tx_enabled(dev_priv, pipe);
  1626. assert_fdi_rx_enabled(dev_priv, pipe);
  1627. if (HAS_PCH_CPT(dev)) {
  1628. /* Workaround: Set the timing override bit before enabling the
  1629. * pch transcoder. */
  1630. reg = TRANS_CHICKEN2(pipe);
  1631. val = I915_READ(reg);
  1632. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1633. I915_WRITE(reg, val);
  1634. }
  1635. reg = PCH_TRANSCONF(pipe);
  1636. val = I915_READ(reg);
  1637. pipeconf_val = I915_READ(PIPECONF(pipe));
  1638. if (HAS_PCH_IBX(dev_priv->dev)) {
  1639. /*
  1640. * make the BPC in transcoder be consistent with
  1641. * that in pipeconf reg.
  1642. */
  1643. val &= ~PIPECONF_BPC_MASK;
  1644. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1645. }
  1646. val &= ~TRANS_INTERLACE_MASK;
  1647. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1648. if (HAS_PCH_IBX(dev_priv->dev) &&
  1649. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1650. val |= TRANS_LEGACY_INTERLACED_ILK;
  1651. else
  1652. val |= TRANS_INTERLACED;
  1653. else
  1654. val |= TRANS_PROGRESSIVE;
  1655. I915_WRITE(reg, val | TRANS_ENABLE);
  1656. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1657. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1658. }
  1659. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1660. enum transcoder cpu_transcoder)
  1661. {
  1662. u32 val, pipeconf_val;
  1663. /* PCH only available on ILK+ */
  1664. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1665. /* FDI must be feeding us bits for PCH ports */
  1666. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1667. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1668. /* Workaround: set timing override bit. */
  1669. val = I915_READ(_TRANSA_CHICKEN2);
  1670. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1671. I915_WRITE(_TRANSA_CHICKEN2, val);
  1672. val = TRANS_ENABLE;
  1673. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1674. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1675. PIPECONF_INTERLACED_ILK)
  1676. val |= TRANS_INTERLACED;
  1677. else
  1678. val |= TRANS_PROGRESSIVE;
  1679. I915_WRITE(LPT_TRANSCONF, val);
  1680. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1681. DRM_ERROR("Failed to enable PCH transcoder\n");
  1682. }
  1683. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1684. enum pipe pipe)
  1685. {
  1686. struct drm_device *dev = dev_priv->dev;
  1687. uint32_t reg, val;
  1688. /* FDI relies on the transcoder */
  1689. assert_fdi_tx_disabled(dev_priv, pipe);
  1690. assert_fdi_rx_disabled(dev_priv, pipe);
  1691. /* Ports must be off as well */
  1692. assert_pch_ports_disabled(dev_priv, pipe);
  1693. reg = PCH_TRANSCONF(pipe);
  1694. val = I915_READ(reg);
  1695. val &= ~TRANS_ENABLE;
  1696. I915_WRITE(reg, val);
  1697. /* wait for PCH transcoder off, transcoder state */
  1698. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1699. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1700. if (!HAS_PCH_IBX(dev)) {
  1701. /* Workaround: Clear the timing override chicken bit again. */
  1702. reg = TRANS_CHICKEN2(pipe);
  1703. val = I915_READ(reg);
  1704. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1705. I915_WRITE(reg, val);
  1706. }
  1707. }
  1708. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1709. {
  1710. u32 val;
  1711. val = I915_READ(LPT_TRANSCONF);
  1712. val &= ~TRANS_ENABLE;
  1713. I915_WRITE(LPT_TRANSCONF, val);
  1714. /* wait for PCH transcoder off, transcoder state */
  1715. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1716. DRM_ERROR("Failed to disable PCH transcoder\n");
  1717. /* Workaround: clear timing override bit. */
  1718. val = I915_READ(_TRANSA_CHICKEN2);
  1719. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1720. I915_WRITE(_TRANSA_CHICKEN2, val);
  1721. }
  1722. /**
  1723. * intel_enable_pipe - enable a pipe, asserting requirements
  1724. * @crtc: crtc responsible for the pipe
  1725. *
  1726. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1727. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1728. */
  1729. static void intel_enable_pipe(struct intel_crtc *crtc)
  1730. {
  1731. struct drm_device *dev = crtc->base.dev;
  1732. struct drm_i915_private *dev_priv = dev->dev_private;
  1733. enum pipe pipe = crtc->pipe;
  1734. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1735. pipe);
  1736. enum pipe pch_transcoder;
  1737. int reg;
  1738. u32 val;
  1739. assert_planes_disabled(dev_priv, pipe);
  1740. assert_cursor_disabled(dev_priv, pipe);
  1741. assert_sprites_disabled(dev_priv, pipe);
  1742. if (HAS_PCH_LPT(dev_priv->dev))
  1743. pch_transcoder = TRANSCODER_A;
  1744. else
  1745. pch_transcoder = pipe;
  1746. /*
  1747. * A pipe without a PLL won't actually be able to drive bits from
  1748. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1749. * need the check.
  1750. */
  1751. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1752. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1753. assert_dsi_pll_enabled(dev_priv);
  1754. else
  1755. assert_pll_enabled(dev_priv, pipe);
  1756. else {
  1757. if (crtc->config.has_pch_encoder) {
  1758. /* if driving the PCH, we need FDI enabled */
  1759. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1760. assert_fdi_tx_pll_enabled(dev_priv,
  1761. (enum pipe) cpu_transcoder);
  1762. }
  1763. /* FIXME: assert CPU port conditions for SNB+ */
  1764. }
  1765. reg = PIPECONF(cpu_transcoder);
  1766. val = I915_READ(reg);
  1767. if (val & PIPECONF_ENABLE) {
  1768. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1769. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1770. return;
  1771. }
  1772. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1773. POSTING_READ(reg);
  1774. }
  1775. /**
  1776. * intel_disable_pipe - disable a pipe, asserting requirements
  1777. * @crtc: crtc whose pipes is to be disabled
  1778. *
  1779. * Disable the pipe of @crtc, making sure that various hardware
  1780. * specific requirements are met, if applicable, e.g. plane
  1781. * disabled, panel fitter off, etc.
  1782. *
  1783. * Will wait until the pipe has shut down before returning.
  1784. */
  1785. static void intel_disable_pipe(struct intel_crtc *crtc)
  1786. {
  1787. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1788. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  1789. enum pipe pipe = crtc->pipe;
  1790. int reg;
  1791. u32 val;
  1792. /*
  1793. * Make sure planes won't keep trying to pump pixels to us,
  1794. * or we might hang the display.
  1795. */
  1796. assert_planes_disabled(dev_priv, pipe);
  1797. assert_cursor_disabled(dev_priv, pipe);
  1798. assert_sprites_disabled(dev_priv, pipe);
  1799. reg = PIPECONF(cpu_transcoder);
  1800. val = I915_READ(reg);
  1801. if ((val & PIPECONF_ENABLE) == 0)
  1802. return;
  1803. /*
  1804. * Double wide has implications for planes
  1805. * so best keep it disabled when not needed.
  1806. */
  1807. if (crtc->config.double_wide)
  1808. val &= ~PIPECONF_DOUBLE_WIDE;
  1809. /* Don't disable pipe or pipe PLLs if needed */
  1810. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1811. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1812. val &= ~PIPECONF_ENABLE;
  1813. I915_WRITE(reg, val);
  1814. if ((val & PIPECONF_ENABLE) == 0)
  1815. intel_wait_for_pipe_off(crtc);
  1816. }
  1817. /*
  1818. * Plane regs are double buffered, going from enabled->disabled needs a
  1819. * trigger in order to latch. The display address reg provides this.
  1820. */
  1821. void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
  1822. enum plane plane)
  1823. {
  1824. struct drm_device *dev = dev_priv->dev;
  1825. u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
  1826. I915_WRITE(reg, I915_READ(reg));
  1827. POSTING_READ(reg);
  1828. }
  1829. /**
  1830. * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  1831. * @plane: plane to be enabled
  1832. * @crtc: crtc for the plane
  1833. *
  1834. * Enable @plane on @crtc, making sure that the pipe is running first.
  1835. */
  1836. static void intel_enable_primary_hw_plane(struct drm_plane *plane,
  1837. struct drm_crtc *crtc)
  1838. {
  1839. struct drm_device *dev = plane->dev;
  1840. struct drm_i915_private *dev_priv = dev->dev_private;
  1841. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1842. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1843. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1844. if (intel_crtc->primary_enabled)
  1845. return;
  1846. intel_crtc->primary_enabled = true;
  1847. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1848. crtc->x, crtc->y);
  1849. /*
  1850. * BDW signals flip done immediately if the plane
  1851. * is disabled, even if the plane enable is already
  1852. * armed to occur at the next vblank :(
  1853. */
  1854. if (IS_BROADWELL(dev))
  1855. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1856. }
  1857. /**
  1858. * intel_disable_primary_hw_plane - disable the primary hardware plane
  1859. * @plane: plane to be disabled
  1860. * @crtc: crtc for the plane
  1861. *
  1862. * Disable @plane on @crtc, making sure that the pipe is running first.
  1863. */
  1864. static void intel_disable_primary_hw_plane(struct drm_plane *plane,
  1865. struct drm_crtc *crtc)
  1866. {
  1867. struct drm_device *dev = plane->dev;
  1868. struct drm_i915_private *dev_priv = dev->dev_private;
  1869. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1870. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1871. if (!intel_crtc->primary_enabled)
  1872. return;
  1873. intel_crtc->primary_enabled = false;
  1874. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1875. crtc->x, crtc->y);
  1876. }
  1877. static bool need_vtd_wa(struct drm_device *dev)
  1878. {
  1879. #ifdef CONFIG_INTEL_IOMMU
  1880. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1881. return true;
  1882. #endif
  1883. return false;
  1884. }
  1885. static int intel_align_height(struct drm_device *dev, int height, bool tiled)
  1886. {
  1887. int tile_height;
  1888. tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
  1889. return ALIGN(height, tile_height);
  1890. }
  1891. int
  1892. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  1893. struct drm_framebuffer *fb,
  1894. struct intel_engine_cs *pipelined)
  1895. {
  1896. struct drm_device *dev = fb->dev;
  1897. struct drm_i915_private *dev_priv = dev->dev_private;
  1898. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1899. u32 alignment;
  1900. int ret;
  1901. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1902. switch (obj->tiling_mode) {
  1903. case I915_TILING_NONE:
  1904. if (INTEL_INFO(dev)->gen >= 9)
  1905. alignment = 256 * 1024;
  1906. else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1907. alignment = 128 * 1024;
  1908. else if (INTEL_INFO(dev)->gen >= 4)
  1909. alignment = 4 * 1024;
  1910. else
  1911. alignment = 64 * 1024;
  1912. break;
  1913. case I915_TILING_X:
  1914. if (INTEL_INFO(dev)->gen >= 9)
  1915. alignment = 256 * 1024;
  1916. else {
  1917. /* pin() will align the object as required by fence */
  1918. alignment = 0;
  1919. }
  1920. break;
  1921. case I915_TILING_Y:
  1922. WARN(1, "Y tiled bo slipped through, driver bug!\n");
  1923. return -EINVAL;
  1924. default:
  1925. BUG();
  1926. }
  1927. /* Note that the w/a also requires 64 PTE of padding following the
  1928. * bo. We currently fill all unused PTE with the shadow page and so
  1929. * we should always have valid PTE following the scanout preventing
  1930. * the VT-d warning.
  1931. */
  1932. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1933. alignment = 256 * 1024;
  1934. /*
  1935. * Global gtt pte registers are special registers which actually forward
  1936. * writes to a chunk of system memory. Which means that there is no risk
  1937. * that the register values disappear as soon as we call
  1938. * intel_runtime_pm_put(), so it is correct to wrap only the
  1939. * pin/unpin/fence and not more.
  1940. */
  1941. intel_runtime_pm_get(dev_priv);
  1942. dev_priv->mm.interruptible = false;
  1943. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1944. if (ret)
  1945. goto err_interruptible;
  1946. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1947. * fence, whereas 965+ only requires a fence if using
  1948. * framebuffer compression. For simplicity, we always install
  1949. * a fence as the cost is not that onerous.
  1950. */
  1951. ret = i915_gem_object_get_fence(obj);
  1952. if (ret)
  1953. goto err_unpin;
  1954. i915_gem_object_pin_fence(obj);
  1955. dev_priv->mm.interruptible = true;
  1956. intel_runtime_pm_put(dev_priv);
  1957. return 0;
  1958. err_unpin:
  1959. i915_gem_object_unpin_from_display_plane(obj);
  1960. err_interruptible:
  1961. dev_priv->mm.interruptible = true;
  1962. intel_runtime_pm_put(dev_priv);
  1963. return ret;
  1964. }
  1965. void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
  1966. {
  1967. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  1968. i915_gem_object_unpin_fence(obj);
  1969. i915_gem_object_unpin_from_display_plane(obj);
  1970. }
  1971. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  1972. * is assumed to be a power-of-two. */
  1973. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  1974. unsigned int tiling_mode,
  1975. unsigned int cpp,
  1976. unsigned int pitch)
  1977. {
  1978. if (tiling_mode != I915_TILING_NONE) {
  1979. unsigned int tile_rows, tiles;
  1980. tile_rows = *y / 8;
  1981. *y %= 8;
  1982. tiles = *x / (512/cpp);
  1983. *x %= 512/cpp;
  1984. return tile_rows * pitch * 8 + tiles * 4096;
  1985. } else {
  1986. unsigned int offset;
  1987. offset = *y * pitch + *x * cpp;
  1988. *y = 0;
  1989. *x = (offset & 4095) / cpp;
  1990. return offset & -4096;
  1991. }
  1992. }
  1993. int intel_format_to_fourcc(int format)
  1994. {
  1995. switch (format) {
  1996. case DISPPLANE_8BPP:
  1997. return DRM_FORMAT_C8;
  1998. case DISPPLANE_BGRX555:
  1999. return DRM_FORMAT_XRGB1555;
  2000. case DISPPLANE_BGRX565:
  2001. return DRM_FORMAT_RGB565;
  2002. default:
  2003. case DISPPLANE_BGRX888:
  2004. return DRM_FORMAT_XRGB8888;
  2005. case DISPPLANE_RGBX888:
  2006. return DRM_FORMAT_XBGR8888;
  2007. case DISPPLANE_BGRX101010:
  2008. return DRM_FORMAT_XRGB2101010;
  2009. case DISPPLANE_RGBX101010:
  2010. return DRM_FORMAT_XBGR2101010;
  2011. }
  2012. }
  2013. static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
  2014. struct intel_plane_config *plane_config)
  2015. {
  2016. struct drm_device *dev = crtc->base.dev;
  2017. struct drm_i915_gem_object *obj = NULL;
  2018. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2019. u32 base = plane_config->base;
  2020. if (plane_config->size == 0)
  2021. return false;
  2022. obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
  2023. plane_config->size);
  2024. if (!obj)
  2025. return false;
  2026. if (plane_config->tiled) {
  2027. obj->tiling_mode = I915_TILING_X;
  2028. obj->stride = crtc->base.primary->fb->pitches[0];
  2029. }
  2030. mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
  2031. mode_cmd.width = crtc->base.primary->fb->width;
  2032. mode_cmd.height = crtc->base.primary->fb->height;
  2033. mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
  2034. mutex_lock(&dev->struct_mutex);
  2035. if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
  2036. &mode_cmd, obj)) {
  2037. DRM_DEBUG_KMS("intel fb init failed\n");
  2038. goto out_unref_obj;
  2039. }
  2040. obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
  2041. mutex_unlock(&dev->struct_mutex);
  2042. DRM_DEBUG_KMS("plane fb obj %p\n", obj);
  2043. return true;
  2044. out_unref_obj:
  2045. drm_gem_object_unreference(&obj->base);
  2046. mutex_unlock(&dev->struct_mutex);
  2047. return false;
  2048. }
  2049. static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
  2050. struct intel_plane_config *plane_config)
  2051. {
  2052. struct drm_device *dev = intel_crtc->base.dev;
  2053. struct drm_i915_private *dev_priv = dev->dev_private;
  2054. struct drm_crtc *c;
  2055. struct intel_crtc *i;
  2056. struct drm_i915_gem_object *obj;
  2057. if (!intel_crtc->base.primary->fb)
  2058. return;
  2059. if (intel_alloc_plane_obj(intel_crtc, plane_config))
  2060. return;
  2061. kfree(intel_crtc->base.primary->fb);
  2062. intel_crtc->base.primary->fb = NULL;
  2063. /*
  2064. * Failed to alloc the obj, check to see if we should share
  2065. * an fb with another CRTC instead
  2066. */
  2067. for_each_crtc(dev, c) {
  2068. i = to_intel_crtc(c);
  2069. if (c == &intel_crtc->base)
  2070. continue;
  2071. if (!i->active)
  2072. continue;
  2073. obj = intel_fb_obj(c->primary->fb);
  2074. if (obj == NULL)
  2075. continue;
  2076. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2077. if (obj->tiling_mode != I915_TILING_NONE)
  2078. dev_priv->preserve_bios_swizzle = true;
  2079. drm_framebuffer_reference(c->primary->fb);
  2080. intel_crtc->base.primary->fb = c->primary->fb;
  2081. obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  2082. break;
  2083. }
  2084. }
  2085. }
  2086. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2087. struct drm_framebuffer *fb,
  2088. int x, int y)
  2089. {
  2090. struct drm_device *dev = crtc->dev;
  2091. struct drm_i915_private *dev_priv = dev->dev_private;
  2092. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2093. struct drm_i915_gem_object *obj;
  2094. int plane = intel_crtc->plane;
  2095. unsigned long linear_offset;
  2096. u32 dspcntr;
  2097. u32 reg = DSPCNTR(plane);
  2098. int pixel_size;
  2099. if (!intel_crtc->primary_enabled) {
  2100. I915_WRITE(reg, 0);
  2101. if (INTEL_INFO(dev)->gen >= 4)
  2102. I915_WRITE(DSPSURF(plane), 0);
  2103. else
  2104. I915_WRITE(DSPADDR(plane), 0);
  2105. POSTING_READ(reg);
  2106. return;
  2107. }
  2108. obj = intel_fb_obj(fb);
  2109. if (WARN_ON(obj == NULL))
  2110. return;
  2111. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2112. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2113. dspcntr |= DISPLAY_PLANE_ENABLE;
  2114. if (INTEL_INFO(dev)->gen < 4) {
  2115. if (intel_crtc->pipe == PIPE_B)
  2116. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2117. /* pipesrc and dspsize control the size that is scaled from,
  2118. * which should always be the user's requested size.
  2119. */
  2120. I915_WRITE(DSPSIZE(plane),
  2121. ((intel_crtc->config.pipe_src_h - 1) << 16) |
  2122. (intel_crtc->config.pipe_src_w - 1));
  2123. I915_WRITE(DSPPOS(plane), 0);
  2124. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2125. I915_WRITE(PRIMSIZE(plane),
  2126. ((intel_crtc->config.pipe_src_h - 1) << 16) |
  2127. (intel_crtc->config.pipe_src_w - 1));
  2128. I915_WRITE(PRIMPOS(plane), 0);
  2129. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2130. }
  2131. switch (fb->pixel_format) {
  2132. case DRM_FORMAT_C8:
  2133. dspcntr |= DISPPLANE_8BPP;
  2134. break;
  2135. case DRM_FORMAT_XRGB1555:
  2136. case DRM_FORMAT_ARGB1555:
  2137. dspcntr |= DISPPLANE_BGRX555;
  2138. break;
  2139. case DRM_FORMAT_RGB565:
  2140. dspcntr |= DISPPLANE_BGRX565;
  2141. break;
  2142. case DRM_FORMAT_XRGB8888:
  2143. case DRM_FORMAT_ARGB8888:
  2144. dspcntr |= DISPPLANE_BGRX888;
  2145. break;
  2146. case DRM_FORMAT_XBGR8888:
  2147. case DRM_FORMAT_ABGR8888:
  2148. dspcntr |= DISPPLANE_RGBX888;
  2149. break;
  2150. case DRM_FORMAT_XRGB2101010:
  2151. case DRM_FORMAT_ARGB2101010:
  2152. dspcntr |= DISPPLANE_BGRX101010;
  2153. break;
  2154. case DRM_FORMAT_XBGR2101010:
  2155. case DRM_FORMAT_ABGR2101010:
  2156. dspcntr |= DISPPLANE_RGBX101010;
  2157. break;
  2158. default:
  2159. BUG();
  2160. }
  2161. if (INTEL_INFO(dev)->gen >= 4 &&
  2162. obj->tiling_mode != I915_TILING_NONE)
  2163. dspcntr |= DISPPLANE_TILED;
  2164. if (IS_G4X(dev))
  2165. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2166. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2167. if (INTEL_INFO(dev)->gen >= 4) {
  2168. intel_crtc->dspaddr_offset =
  2169. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2170. pixel_size,
  2171. fb->pitches[0]);
  2172. linear_offset -= intel_crtc->dspaddr_offset;
  2173. } else {
  2174. intel_crtc->dspaddr_offset = linear_offset;
  2175. }
  2176. if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
  2177. dspcntr |= DISPPLANE_ROTATE_180;
  2178. x += (intel_crtc->config.pipe_src_w - 1);
  2179. y += (intel_crtc->config.pipe_src_h - 1);
  2180. /* Finding the last pixel of the last line of the display
  2181. data and adding to linear_offset*/
  2182. linear_offset +=
  2183. (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
  2184. (intel_crtc->config.pipe_src_w - 1) * pixel_size;
  2185. }
  2186. I915_WRITE(reg, dspcntr);
  2187. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2188. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2189. fb->pitches[0]);
  2190. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2191. if (INTEL_INFO(dev)->gen >= 4) {
  2192. I915_WRITE(DSPSURF(plane),
  2193. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2194. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2195. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2196. } else
  2197. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2198. POSTING_READ(reg);
  2199. }
  2200. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2201. struct drm_framebuffer *fb,
  2202. int x, int y)
  2203. {
  2204. struct drm_device *dev = crtc->dev;
  2205. struct drm_i915_private *dev_priv = dev->dev_private;
  2206. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2207. struct drm_i915_gem_object *obj;
  2208. int plane = intel_crtc->plane;
  2209. unsigned long linear_offset;
  2210. u32 dspcntr;
  2211. u32 reg = DSPCNTR(plane);
  2212. int pixel_size;
  2213. if (!intel_crtc->primary_enabled) {
  2214. I915_WRITE(reg, 0);
  2215. I915_WRITE(DSPSURF(plane), 0);
  2216. POSTING_READ(reg);
  2217. return;
  2218. }
  2219. obj = intel_fb_obj(fb);
  2220. if (WARN_ON(obj == NULL))
  2221. return;
  2222. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2223. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2224. dspcntr |= DISPLAY_PLANE_ENABLE;
  2225. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2226. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2227. switch (fb->pixel_format) {
  2228. case DRM_FORMAT_C8:
  2229. dspcntr |= DISPPLANE_8BPP;
  2230. break;
  2231. case DRM_FORMAT_RGB565:
  2232. dspcntr |= DISPPLANE_BGRX565;
  2233. break;
  2234. case DRM_FORMAT_XRGB8888:
  2235. case DRM_FORMAT_ARGB8888:
  2236. dspcntr |= DISPPLANE_BGRX888;
  2237. break;
  2238. case DRM_FORMAT_XBGR8888:
  2239. case DRM_FORMAT_ABGR8888:
  2240. dspcntr |= DISPPLANE_RGBX888;
  2241. break;
  2242. case DRM_FORMAT_XRGB2101010:
  2243. case DRM_FORMAT_ARGB2101010:
  2244. dspcntr |= DISPPLANE_BGRX101010;
  2245. break;
  2246. case DRM_FORMAT_XBGR2101010:
  2247. case DRM_FORMAT_ABGR2101010:
  2248. dspcntr |= DISPPLANE_RGBX101010;
  2249. break;
  2250. default:
  2251. BUG();
  2252. }
  2253. if (obj->tiling_mode != I915_TILING_NONE)
  2254. dspcntr |= DISPPLANE_TILED;
  2255. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2256. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2257. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2258. intel_crtc->dspaddr_offset =
  2259. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2260. pixel_size,
  2261. fb->pitches[0]);
  2262. linear_offset -= intel_crtc->dspaddr_offset;
  2263. if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
  2264. dspcntr |= DISPPLANE_ROTATE_180;
  2265. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2266. x += (intel_crtc->config.pipe_src_w - 1);
  2267. y += (intel_crtc->config.pipe_src_h - 1);
  2268. /* Finding the last pixel of the last line of the display
  2269. data and adding to linear_offset*/
  2270. linear_offset +=
  2271. (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
  2272. (intel_crtc->config.pipe_src_w - 1) * pixel_size;
  2273. }
  2274. }
  2275. I915_WRITE(reg, dspcntr);
  2276. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2277. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2278. fb->pitches[0]);
  2279. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2280. I915_WRITE(DSPSURF(plane),
  2281. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2282. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2283. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2284. } else {
  2285. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2286. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2287. }
  2288. POSTING_READ(reg);
  2289. }
  2290. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2291. struct drm_framebuffer *fb,
  2292. int x, int y)
  2293. {
  2294. struct drm_device *dev = crtc->dev;
  2295. struct drm_i915_private *dev_priv = dev->dev_private;
  2296. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2297. struct intel_framebuffer *intel_fb;
  2298. struct drm_i915_gem_object *obj;
  2299. int pipe = intel_crtc->pipe;
  2300. u32 plane_ctl, stride;
  2301. if (!intel_crtc->primary_enabled) {
  2302. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2303. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2304. POSTING_READ(PLANE_CTL(pipe, 0));
  2305. return;
  2306. }
  2307. plane_ctl = PLANE_CTL_ENABLE |
  2308. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2309. PLANE_CTL_PIPE_CSC_ENABLE;
  2310. switch (fb->pixel_format) {
  2311. case DRM_FORMAT_RGB565:
  2312. plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
  2313. break;
  2314. case DRM_FORMAT_XRGB8888:
  2315. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2316. break;
  2317. case DRM_FORMAT_XBGR8888:
  2318. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2319. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2320. break;
  2321. case DRM_FORMAT_XRGB2101010:
  2322. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2323. break;
  2324. case DRM_FORMAT_XBGR2101010:
  2325. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2326. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2327. break;
  2328. default:
  2329. BUG();
  2330. }
  2331. intel_fb = to_intel_framebuffer(fb);
  2332. obj = intel_fb->obj;
  2333. /*
  2334. * The stride is either expressed as a multiple of 64 bytes chunks for
  2335. * linear buffers or in number of tiles for tiled buffers.
  2336. */
  2337. switch (obj->tiling_mode) {
  2338. case I915_TILING_NONE:
  2339. stride = fb->pitches[0] >> 6;
  2340. break;
  2341. case I915_TILING_X:
  2342. plane_ctl |= PLANE_CTL_TILED_X;
  2343. stride = fb->pitches[0] >> 9;
  2344. break;
  2345. default:
  2346. BUG();
  2347. }
  2348. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2349. if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180))
  2350. plane_ctl |= PLANE_CTL_ROTATE_180;
  2351. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2352. DRM_DEBUG_KMS("Writing base %08lX %d,%d,%d,%d pitch=%d\n",
  2353. i915_gem_obj_ggtt_offset(obj),
  2354. x, y, fb->width, fb->height,
  2355. fb->pitches[0]);
  2356. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2357. I915_WRITE(PLANE_OFFSET(pipe, 0), (y << 16) | x);
  2358. I915_WRITE(PLANE_SIZE(pipe, 0),
  2359. (intel_crtc->config.pipe_src_h - 1) << 16 |
  2360. (intel_crtc->config.pipe_src_w - 1));
  2361. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2362. I915_WRITE(PLANE_SURF(pipe, 0), i915_gem_obj_ggtt_offset(obj));
  2363. POSTING_READ(PLANE_SURF(pipe, 0));
  2364. }
  2365. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2366. static int
  2367. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2368. int x, int y, enum mode_set_atomic state)
  2369. {
  2370. struct drm_device *dev = crtc->dev;
  2371. struct drm_i915_private *dev_priv = dev->dev_private;
  2372. if (dev_priv->display.disable_fbc)
  2373. dev_priv->display.disable_fbc(dev);
  2374. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2375. return 0;
  2376. }
  2377. static void intel_complete_page_flips(struct drm_device *dev)
  2378. {
  2379. struct drm_crtc *crtc;
  2380. for_each_crtc(dev, crtc) {
  2381. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2382. enum plane plane = intel_crtc->plane;
  2383. intel_prepare_page_flip(dev, plane);
  2384. intel_finish_page_flip_plane(dev, plane);
  2385. }
  2386. }
  2387. static void intel_update_primary_planes(struct drm_device *dev)
  2388. {
  2389. struct drm_i915_private *dev_priv = dev->dev_private;
  2390. struct drm_crtc *crtc;
  2391. for_each_crtc(dev, crtc) {
  2392. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2393. drm_modeset_lock(&crtc->mutex, NULL);
  2394. /*
  2395. * FIXME: Once we have proper support for primary planes (and
  2396. * disabling them without disabling the entire crtc) allow again
  2397. * a NULL crtc->primary->fb.
  2398. */
  2399. if (intel_crtc->active && crtc->primary->fb)
  2400. dev_priv->display.update_primary_plane(crtc,
  2401. crtc->primary->fb,
  2402. crtc->x,
  2403. crtc->y);
  2404. drm_modeset_unlock(&crtc->mutex);
  2405. }
  2406. }
  2407. void intel_prepare_reset(struct drm_device *dev)
  2408. {
  2409. struct drm_i915_private *dev_priv = to_i915(dev);
  2410. struct intel_crtc *crtc;
  2411. /* no reset support for gen2 */
  2412. if (IS_GEN2(dev))
  2413. return;
  2414. /* reset doesn't touch the display */
  2415. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2416. return;
  2417. drm_modeset_lock_all(dev);
  2418. /*
  2419. * Disabling the crtcs gracefully seems nicer. Also the
  2420. * g33 docs say we should at least disable all the planes.
  2421. */
  2422. for_each_intel_crtc(dev, crtc) {
  2423. if (crtc->active)
  2424. dev_priv->display.crtc_disable(&crtc->base);
  2425. }
  2426. }
  2427. void intel_finish_reset(struct drm_device *dev)
  2428. {
  2429. struct drm_i915_private *dev_priv = to_i915(dev);
  2430. /*
  2431. * Flips in the rings will be nuked by the reset,
  2432. * so complete all pending flips so that user space
  2433. * will get its events and not get stuck.
  2434. */
  2435. intel_complete_page_flips(dev);
  2436. /* no reset support for gen2 */
  2437. if (IS_GEN2(dev))
  2438. return;
  2439. /* reset doesn't touch the display */
  2440. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2441. /*
  2442. * Flips in the rings have been nuked by the reset,
  2443. * so update the base address of all primary
  2444. * planes to the the last fb to make sure we're
  2445. * showing the correct fb after a reset.
  2446. */
  2447. intel_update_primary_planes(dev);
  2448. return;
  2449. }
  2450. /*
  2451. * The display has been reset as well,
  2452. * so need a full re-initialization.
  2453. */
  2454. intel_runtime_pm_disable_interrupts(dev_priv);
  2455. intel_runtime_pm_enable_interrupts(dev_priv);
  2456. intel_modeset_init_hw(dev);
  2457. spin_lock_irq(&dev_priv->irq_lock);
  2458. if (dev_priv->display.hpd_irq_setup)
  2459. dev_priv->display.hpd_irq_setup(dev);
  2460. spin_unlock_irq(&dev_priv->irq_lock);
  2461. intel_modeset_setup_hw_state(dev, true);
  2462. intel_hpd_init(dev_priv);
  2463. drm_modeset_unlock_all(dev);
  2464. }
  2465. static int
  2466. intel_finish_fb(struct drm_framebuffer *old_fb)
  2467. {
  2468. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2469. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2470. bool was_interruptible = dev_priv->mm.interruptible;
  2471. int ret;
  2472. /* Big Hammer, we also need to ensure that any pending
  2473. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2474. * current scanout is retired before unpinning the old
  2475. * framebuffer.
  2476. *
  2477. * This should only fail upon a hung GPU, in which case we
  2478. * can safely continue.
  2479. */
  2480. dev_priv->mm.interruptible = false;
  2481. ret = i915_gem_object_finish_gpu(obj);
  2482. dev_priv->mm.interruptible = was_interruptible;
  2483. return ret;
  2484. }
  2485. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2486. {
  2487. struct drm_device *dev = crtc->dev;
  2488. struct drm_i915_private *dev_priv = dev->dev_private;
  2489. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2490. bool pending;
  2491. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2492. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2493. return false;
  2494. spin_lock_irq(&dev->event_lock);
  2495. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2496. spin_unlock_irq(&dev->event_lock);
  2497. return pending;
  2498. }
  2499. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2500. {
  2501. struct drm_device *dev = crtc->base.dev;
  2502. struct drm_i915_private *dev_priv = dev->dev_private;
  2503. const struct drm_display_mode *adjusted_mode;
  2504. if (!i915.fastboot)
  2505. return;
  2506. /*
  2507. * Update pipe size and adjust fitter if needed: the reason for this is
  2508. * that in compute_mode_changes we check the native mode (not the pfit
  2509. * mode) to see if we can flip rather than do a full mode set. In the
  2510. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2511. * pfit state, we'll end up with a big fb scanned out into the wrong
  2512. * sized surface.
  2513. *
  2514. * To fix this properly, we need to hoist the checks up into
  2515. * compute_mode_changes (or above), check the actual pfit state and
  2516. * whether the platform allows pfit disable with pipe active, and only
  2517. * then update the pipesrc and pfit state, even on the flip path.
  2518. */
  2519. adjusted_mode = &crtc->config.adjusted_mode;
  2520. I915_WRITE(PIPESRC(crtc->pipe),
  2521. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2522. (adjusted_mode->crtc_vdisplay - 1));
  2523. if (!crtc->config.pch_pfit.enabled &&
  2524. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2525. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2526. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2527. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2528. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2529. }
  2530. crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
  2531. crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
  2532. }
  2533. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2534. {
  2535. struct drm_device *dev = crtc->dev;
  2536. struct drm_i915_private *dev_priv = dev->dev_private;
  2537. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2538. int pipe = intel_crtc->pipe;
  2539. u32 reg, temp;
  2540. /* enable normal train */
  2541. reg = FDI_TX_CTL(pipe);
  2542. temp = I915_READ(reg);
  2543. if (IS_IVYBRIDGE(dev)) {
  2544. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2545. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2546. } else {
  2547. temp &= ~FDI_LINK_TRAIN_NONE;
  2548. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2549. }
  2550. I915_WRITE(reg, temp);
  2551. reg = FDI_RX_CTL(pipe);
  2552. temp = I915_READ(reg);
  2553. if (HAS_PCH_CPT(dev)) {
  2554. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2555. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2556. } else {
  2557. temp &= ~FDI_LINK_TRAIN_NONE;
  2558. temp |= FDI_LINK_TRAIN_NONE;
  2559. }
  2560. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2561. /* wait one idle pattern time */
  2562. POSTING_READ(reg);
  2563. udelay(1000);
  2564. /* IVB wants error correction enabled */
  2565. if (IS_IVYBRIDGE(dev))
  2566. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2567. FDI_FE_ERRC_ENABLE);
  2568. }
  2569. static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
  2570. {
  2571. return crtc->base.enabled && crtc->active &&
  2572. crtc->config.has_pch_encoder;
  2573. }
  2574. static void ivb_modeset_global_resources(struct drm_device *dev)
  2575. {
  2576. struct drm_i915_private *dev_priv = dev->dev_private;
  2577. struct intel_crtc *pipe_B_crtc =
  2578. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  2579. struct intel_crtc *pipe_C_crtc =
  2580. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
  2581. uint32_t temp;
  2582. /*
  2583. * When everything is off disable fdi C so that we could enable fdi B
  2584. * with all lanes. Note that we don't care about enabled pipes without
  2585. * an enabled pch encoder.
  2586. */
  2587. if (!pipe_has_enabled_pch(pipe_B_crtc) &&
  2588. !pipe_has_enabled_pch(pipe_C_crtc)) {
  2589. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2590. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2591. temp = I915_READ(SOUTH_CHICKEN1);
  2592. temp &= ~FDI_BC_BIFURCATION_SELECT;
  2593. DRM_DEBUG_KMS("disabling fdi C rx\n");
  2594. I915_WRITE(SOUTH_CHICKEN1, temp);
  2595. }
  2596. }
  2597. /* The FDI link training functions for ILK/Ibexpeak. */
  2598. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2599. {
  2600. struct drm_device *dev = crtc->dev;
  2601. struct drm_i915_private *dev_priv = dev->dev_private;
  2602. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2603. int pipe = intel_crtc->pipe;
  2604. u32 reg, temp, tries;
  2605. /* FDI needs bits from pipe first */
  2606. assert_pipe_enabled(dev_priv, pipe);
  2607. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2608. for train result */
  2609. reg = FDI_RX_IMR(pipe);
  2610. temp = I915_READ(reg);
  2611. temp &= ~FDI_RX_SYMBOL_LOCK;
  2612. temp &= ~FDI_RX_BIT_LOCK;
  2613. I915_WRITE(reg, temp);
  2614. I915_READ(reg);
  2615. udelay(150);
  2616. /* enable CPU FDI TX and PCH FDI RX */
  2617. reg = FDI_TX_CTL(pipe);
  2618. temp = I915_READ(reg);
  2619. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2620. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2621. temp &= ~FDI_LINK_TRAIN_NONE;
  2622. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2623. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2624. reg = FDI_RX_CTL(pipe);
  2625. temp = I915_READ(reg);
  2626. temp &= ~FDI_LINK_TRAIN_NONE;
  2627. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2628. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2629. POSTING_READ(reg);
  2630. udelay(150);
  2631. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2632. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2633. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2634. FDI_RX_PHASE_SYNC_POINTER_EN);
  2635. reg = FDI_RX_IIR(pipe);
  2636. for (tries = 0; tries < 5; tries++) {
  2637. temp = I915_READ(reg);
  2638. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2639. if ((temp & FDI_RX_BIT_LOCK)) {
  2640. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2641. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2642. break;
  2643. }
  2644. }
  2645. if (tries == 5)
  2646. DRM_ERROR("FDI train 1 fail!\n");
  2647. /* Train 2 */
  2648. reg = FDI_TX_CTL(pipe);
  2649. temp = I915_READ(reg);
  2650. temp &= ~FDI_LINK_TRAIN_NONE;
  2651. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2652. I915_WRITE(reg, temp);
  2653. reg = FDI_RX_CTL(pipe);
  2654. temp = I915_READ(reg);
  2655. temp &= ~FDI_LINK_TRAIN_NONE;
  2656. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2657. I915_WRITE(reg, temp);
  2658. POSTING_READ(reg);
  2659. udelay(150);
  2660. reg = FDI_RX_IIR(pipe);
  2661. for (tries = 0; tries < 5; tries++) {
  2662. temp = I915_READ(reg);
  2663. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2664. if (temp & FDI_RX_SYMBOL_LOCK) {
  2665. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2666. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2667. break;
  2668. }
  2669. }
  2670. if (tries == 5)
  2671. DRM_ERROR("FDI train 2 fail!\n");
  2672. DRM_DEBUG_KMS("FDI train done\n");
  2673. }
  2674. static const int snb_b_fdi_train_param[] = {
  2675. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2676. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2677. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2678. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2679. };
  2680. /* The FDI link training functions for SNB/Cougarpoint. */
  2681. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2682. {
  2683. struct drm_device *dev = crtc->dev;
  2684. struct drm_i915_private *dev_priv = dev->dev_private;
  2685. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2686. int pipe = intel_crtc->pipe;
  2687. u32 reg, temp, i, retry;
  2688. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2689. for train result */
  2690. reg = FDI_RX_IMR(pipe);
  2691. temp = I915_READ(reg);
  2692. temp &= ~FDI_RX_SYMBOL_LOCK;
  2693. temp &= ~FDI_RX_BIT_LOCK;
  2694. I915_WRITE(reg, temp);
  2695. POSTING_READ(reg);
  2696. udelay(150);
  2697. /* enable CPU FDI TX and PCH FDI RX */
  2698. reg = FDI_TX_CTL(pipe);
  2699. temp = I915_READ(reg);
  2700. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2701. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2702. temp &= ~FDI_LINK_TRAIN_NONE;
  2703. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2704. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2705. /* SNB-B */
  2706. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2707. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2708. I915_WRITE(FDI_RX_MISC(pipe),
  2709. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2710. reg = FDI_RX_CTL(pipe);
  2711. temp = I915_READ(reg);
  2712. if (HAS_PCH_CPT(dev)) {
  2713. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2714. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2715. } else {
  2716. temp &= ~FDI_LINK_TRAIN_NONE;
  2717. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2718. }
  2719. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2720. POSTING_READ(reg);
  2721. udelay(150);
  2722. for (i = 0; i < 4; i++) {
  2723. reg = FDI_TX_CTL(pipe);
  2724. temp = I915_READ(reg);
  2725. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2726. temp |= snb_b_fdi_train_param[i];
  2727. I915_WRITE(reg, temp);
  2728. POSTING_READ(reg);
  2729. udelay(500);
  2730. for (retry = 0; retry < 5; retry++) {
  2731. reg = FDI_RX_IIR(pipe);
  2732. temp = I915_READ(reg);
  2733. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2734. if (temp & FDI_RX_BIT_LOCK) {
  2735. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2736. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2737. break;
  2738. }
  2739. udelay(50);
  2740. }
  2741. if (retry < 5)
  2742. break;
  2743. }
  2744. if (i == 4)
  2745. DRM_ERROR("FDI train 1 fail!\n");
  2746. /* Train 2 */
  2747. reg = FDI_TX_CTL(pipe);
  2748. temp = I915_READ(reg);
  2749. temp &= ~FDI_LINK_TRAIN_NONE;
  2750. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2751. if (IS_GEN6(dev)) {
  2752. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2753. /* SNB-B */
  2754. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2755. }
  2756. I915_WRITE(reg, temp);
  2757. reg = FDI_RX_CTL(pipe);
  2758. temp = I915_READ(reg);
  2759. if (HAS_PCH_CPT(dev)) {
  2760. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2761. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2762. } else {
  2763. temp &= ~FDI_LINK_TRAIN_NONE;
  2764. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2765. }
  2766. I915_WRITE(reg, temp);
  2767. POSTING_READ(reg);
  2768. udelay(150);
  2769. for (i = 0; i < 4; i++) {
  2770. reg = FDI_TX_CTL(pipe);
  2771. temp = I915_READ(reg);
  2772. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2773. temp |= snb_b_fdi_train_param[i];
  2774. I915_WRITE(reg, temp);
  2775. POSTING_READ(reg);
  2776. udelay(500);
  2777. for (retry = 0; retry < 5; retry++) {
  2778. reg = FDI_RX_IIR(pipe);
  2779. temp = I915_READ(reg);
  2780. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2781. if (temp & FDI_RX_SYMBOL_LOCK) {
  2782. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2783. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2784. break;
  2785. }
  2786. udelay(50);
  2787. }
  2788. if (retry < 5)
  2789. break;
  2790. }
  2791. if (i == 4)
  2792. DRM_ERROR("FDI train 2 fail!\n");
  2793. DRM_DEBUG_KMS("FDI train done.\n");
  2794. }
  2795. /* Manual link training for Ivy Bridge A0 parts */
  2796. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2797. {
  2798. struct drm_device *dev = crtc->dev;
  2799. struct drm_i915_private *dev_priv = dev->dev_private;
  2800. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2801. int pipe = intel_crtc->pipe;
  2802. u32 reg, temp, i, j;
  2803. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2804. for train result */
  2805. reg = FDI_RX_IMR(pipe);
  2806. temp = I915_READ(reg);
  2807. temp &= ~FDI_RX_SYMBOL_LOCK;
  2808. temp &= ~FDI_RX_BIT_LOCK;
  2809. I915_WRITE(reg, temp);
  2810. POSTING_READ(reg);
  2811. udelay(150);
  2812. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  2813. I915_READ(FDI_RX_IIR(pipe)));
  2814. /* Try each vswing and preemphasis setting twice before moving on */
  2815. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  2816. /* disable first in case we need to retry */
  2817. reg = FDI_TX_CTL(pipe);
  2818. temp = I915_READ(reg);
  2819. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2820. temp &= ~FDI_TX_ENABLE;
  2821. I915_WRITE(reg, temp);
  2822. reg = FDI_RX_CTL(pipe);
  2823. temp = I915_READ(reg);
  2824. temp &= ~FDI_LINK_TRAIN_AUTO;
  2825. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2826. temp &= ~FDI_RX_ENABLE;
  2827. I915_WRITE(reg, temp);
  2828. /* enable CPU FDI TX and PCH FDI RX */
  2829. reg = FDI_TX_CTL(pipe);
  2830. temp = I915_READ(reg);
  2831. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2832. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2833. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2834. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2835. temp |= snb_b_fdi_train_param[j/2];
  2836. temp |= FDI_COMPOSITE_SYNC;
  2837. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2838. I915_WRITE(FDI_RX_MISC(pipe),
  2839. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2840. reg = FDI_RX_CTL(pipe);
  2841. temp = I915_READ(reg);
  2842. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2843. temp |= FDI_COMPOSITE_SYNC;
  2844. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2845. POSTING_READ(reg);
  2846. udelay(1); /* should be 0.5us */
  2847. for (i = 0; i < 4; i++) {
  2848. reg = FDI_RX_IIR(pipe);
  2849. temp = I915_READ(reg);
  2850. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2851. if (temp & FDI_RX_BIT_LOCK ||
  2852. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2853. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2854. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  2855. i);
  2856. break;
  2857. }
  2858. udelay(1); /* should be 0.5us */
  2859. }
  2860. if (i == 4) {
  2861. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  2862. continue;
  2863. }
  2864. /* Train 2 */
  2865. reg = FDI_TX_CTL(pipe);
  2866. temp = I915_READ(reg);
  2867. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2868. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2869. I915_WRITE(reg, temp);
  2870. reg = FDI_RX_CTL(pipe);
  2871. temp = I915_READ(reg);
  2872. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2873. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2874. I915_WRITE(reg, temp);
  2875. POSTING_READ(reg);
  2876. udelay(2); /* should be 1.5us */
  2877. for (i = 0; i < 4; i++) {
  2878. reg = FDI_RX_IIR(pipe);
  2879. temp = I915_READ(reg);
  2880. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2881. if (temp & FDI_RX_SYMBOL_LOCK ||
  2882. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  2883. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2884. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  2885. i);
  2886. goto train_done;
  2887. }
  2888. udelay(2); /* should be 1.5us */
  2889. }
  2890. if (i == 4)
  2891. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  2892. }
  2893. train_done:
  2894. DRM_DEBUG_KMS("FDI train done.\n");
  2895. }
  2896. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  2897. {
  2898. struct drm_device *dev = intel_crtc->base.dev;
  2899. struct drm_i915_private *dev_priv = dev->dev_private;
  2900. int pipe = intel_crtc->pipe;
  2901. u32 reg, temp;
  2902. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2903. reg = FDI_RX_CTL(pipe);
  2904. temp = I915_READ(reg);
  2905. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  2906. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2907. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2908. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2909. POSTING_READ(reg);
  2910. udelay(200);
  2911. /* Switch from Rawclk to PCDclk */
  2912. temp = I915_READ(reg);
  2913. I915_WRITE(reg, temp | FDI_PCDCLK);
  2914. POSTING_READ(reg);
  2915. udelay(200);
  2916. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2917. reg = FDI_TX_CTL(pipe);
  2918. temp = I915_READ(reg);
  2919. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2920. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2921. POSTING_READ(reg);
  2922. udelay(100);
  2923. }
  2924. }
  2925. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  2926. {
  2927. struct drm_device *dev = intel_crtc->base.dev;
  2928. struct drm_i915_private *dev_priv = dev->dev_private;
  2929. int pipe = intel_crtc->pipe;
  2930. u32 reg, temp;
  2931. /* Switch from PCDclk to Rawclk */
  2932. reg = FDI_RX_CTL(pipe);
  2933. temp = I915_READ(reg);
  2934. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2935. /* Disable CPU FDI TX PLL */
  2936. reg = FDI_TX_CTL(pipe);
  2937. temp = I915_READ(reg);
  2938. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2939. POSTING_READ(reg);
  2940. udelay(100);
  2941. reg = FDI_RX_CTL(pipe);
  2942. temp = I915_READ(reg);
  2943. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2944. /* Wait for the clocks to turn off. */
  2945. POSTING_READ(reg);
  2946. udelay(100);
  2947. }
  2948. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2949. {
  2950. struct drm_device *dev = crtc->dev;
  2951. struct drm_i915_private *dev_priv = dev->dev_private;
  2952. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2953. int pipe = intel_crtc->pipe;
  2954. u32 reg, temp;
  2955. /* disable CPU FDI tx and PCH FDI rx */
  2956. reg = FDI_TX_CTL(pipe);
  2957. temp = I915_READ(reg);
  2958. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2959. POSTING_READ(reg);
  2960. reg = FDI_RX_CTL(pipe);
  2961. temp = I915_READ(reg);
  2962. temp &= ~(0x7 << 16);
  2963. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2964. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2965. POSTING_READ(reg);
  2966. udelay(100);
  2967. /* Ironlake workaround, disable clock pointer after downing FDI */
  2968. if (HAS_PCH_IBX(dev))
  2969. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2970. /* still set train pattern 1 */
  2971. reg = FDI_TX_CTL(pipe);
  2972. temp = I915_READ(reg);
  2973. temp &= ~FDI_LINK_TRAIN_NONE;
  2974. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2975. I915_WRITE(reg, temp);
  2976. reg = FDI_RX_CTL(pipe);
  2977. temp = I915_READ(reg);
  2978. if (HAS_PCH_CPT(dev)) {
  2979. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2980. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2981. } else {
  2982. temp &= ~FDI_LINK_TRAIN_NONE;
  2983. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2984. }
  2985. /* BPC in FDI rx is consistent with that in PIPECONF */
  2986. temp &= ~(0x07 << 16);
  2987. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2988. I915_WRITE(reg, temp);
  2989. POSTING_READ(reg);
  2990. udelay(100);
  2991. }
  2992. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  2993. {
  2994. struct intel_crtc *crtc;
  2995. /* Note that we don't need to be called with mode_config.lock here
  2996. * as our list of CRTC objects is static for the lifetime of the
  2997. * device and so cannot disappear as we iterate. Similarly, we can
  2998. * happily treat the predicates as racy, atomic checks as userspace
  2999. * cannot claim and pin a new fb without at least acquring the
  3000. * struct_mutex and so serialising with us.
  3001. */
  3002. for_each_intel_crtc(dev, crtc) {
  3003. if (atomic_read(&crtc->unpin_work_count) == 0)
  3004. continue;
  3005. if (crtc->unpin_work)
  3006. intel_wait_for_vblank(dev, crtc->pipe);
  3007. return true;
  3008. }
  3009. return false;
  3010. }
  3011. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3012. {
  3013. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3014. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3015. /* ensure that the unpin work is consistent wrt ->pending. */
  3016. smp_rmb();
  3017. intel_crtc->unpin_work = NULL;
  3018. if (work->event)
  3019. drm_send_vblank_event(intel_crtc->base.dev,
  3020. intel_crtc->pipe,
  3021. work->event);
  3022. drm_crtc_vblank_put(&intel_crtc->base);
  3023. wake_up_all(&dev_priv->pending_flip_queue);
  3024. queue_work(dev_priv->wq, &work->work);
  3025. trace_i915_flip_complete(intel_crtc->plane,
  3026. work->pending_flip_obj);
  3027. }
  3028. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3029. {
  3030. struct drm_device *dev = crtc->dev;
  3031. struct drm_i915_private *dev_priv = dev->dev_private;
  3032. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3033. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3034. !intel_crtc_has_pending_flip(crtc),
  3035. 60*HZ) == 0)) {
  3036. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3037. spin_lock_irq(&dev->event_lock);
  3038. if (intel_crtc->unpin_work) {
  3039. WARN_ONCE(1, "Removing stuck page flip\n");
  3040. page_flip_completed(intel_crtc);
  3041. }
  3042. spin_unlock_irq(&dev->event_lock);
  3043. }
  3044. if (crtc->primary->fb) {
  3045. mutex_lock(&dev->struct_mutex);
  3046. intel_finish_fb(crtc->primary->fb);
  3047. mutex_unlock(&dev->struct_mutex);
  3048. }
  3049. }
  3050. /* Program iCLKIP clock to the desired frequency */
  3051. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3052. {
  3053. struct drm_device *dev = crtc->dev;
  3054. struct drm_i915_private *dev_priv = dev->dev_private;
  3055. int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
  3056. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3057. u32 temp;
  3058. mutex_lock(&dev_priv->dpio_lock);
  3059. /* It is necessary to ungate the pixclk gate prior to programming
  3060. * the divisors, and gate it back when it is done.
  3061. */
  3062. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3063. /* Disable SSCCTL */
  3064. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3065. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3066. SBI_SSCCTL_DISABLE,
  3067. SBI_ICLK);
  3068. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3069. if (clock == 20000) {
  3070. auxdiv = 1;
  3071. divsel = 0x41;
  3072. phaseinc = 0x20;
  3073. } else {
  3074. /* The iCLK virtual clock root frequency is in MHz,
  3075. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3076. * divisors, it is necessary to divide one by another, so we
  3077. * convert the virtual clock precision to KHz here for higher
  3078. * precision.
  3079. */
  3080. u32 iclk_virtual_root_freq = 172800 * 1000;
  3081. u32 iclk_pi_range = 64;
  3082. u32 desired_divisor, msb_divisor_value, pi_value;
  3083. desired_divisor = (iclk_virtual_root_freq / clock);
  3084. msb_divisor_value = desired_divisor / iclk_pi_range;
  3085. pi_value = desired_divisor % iclk_pi_range;
  3086. auxdiv = 0;
  3087. divsel = msb_divisor_value - 2;
  3088. phaseinc = pi_value;
  3089. }
  3090. /* This should not happen with any sane values */
  3091. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3092. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3093. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3094. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3095. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3096. clock,
  3097. auxdiv,
  3098. divsel,
  3099. phasedir,
  3100. phaseinc);
  3101. /* Program SSCDIVINTPHASE6 */
  3102. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3103. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3104. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3105. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3106. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3107. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3108. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3109. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3110. /* Program SSCAUXDIV */
  3111. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3112. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3113. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3114. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3115. /* Enable modulator and associated divider */
  3116. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3117. temp &= ~SBI_SSCCTL_DISABLE;
  3118. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3119. /* Wait for initialization time */
  3120. udelay(24);
  3121. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3122. mutex_unlock(&dev_priv->dpio_lock);
  3123. }
  3124. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3125. enum pipe pch_transcoder)
  3126. {
  3127. struct drm_device *dev = crtc->base.dev;
  3128. struct drm_i915_private *dev_priv = dev->dev_private;
  3129. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  3130. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3131. I915_READ(HTOTAL(cpu_transcoder)));
  3132. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3133. I915_READ(HBLANK(cpu_transcoder)));
  3134. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3135. I915_READ(HSYNC(cpu_transcoder)));
  3136. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3137. I915_READ(VTOTAL(cpu_transcoder)));
  3138. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3139. I915_READ(VBLANK(cpu_transcoder)));
  3140. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3141. I915_READ(VSYNC(cpu_transcoder)));
  3142. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3143. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3144. }
  3145. static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
  3146. {
  3147. struct drm_i915_private *dev_priv = dev->dev_private;
  3148. uint32_t temp;
  3149. temp = I915_READ(SOUTH_CHICKEN1);
  3150. if (temp & FDI_BC_BIFURCATION_SELECT)
  3151. return;
  3152. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3153. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3154. temp |= FDI_BC_BIFURCATION_SELECT;
  3155. DRM_DEBUG_KMS("enabling fdi C rx\n");
  3156. I915_WRITE(SOUTH_CHICKEN1, temp);
  3157. POSTING_READ(SOUTH_CHICKEN1);
  3158. }
  3159. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3160. {
  3161. struct drm_device *dev = intel_crtc->base.dev;
  3162. struct drm_i915_private *dev_priv = dev->dev_private;
  3163. switch (intel_crtc->pipe) {
  3164. case PIPE_A:
  3165. break;
  3166. case PIPE_B:
  3167. if (intel_crtc->config.fdi_lanes > 2)
  3168. WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
  3169. else
  3170. cpt_enable_fdi_bc_bifurcation(dev);
  3171. break;
  3172. case PIPE_C:
  3173. cpt_enable_fdi_bc_bifurcation(dev);
  3174. break;
  3175. default:
  3176. BUG();
  3177. }
  3178. }
  3179. /*
  3180. * Enable PCH resources required for PCH ports:
  3181. * - PCH PLLs
  3182. * - FDI training & RX/TX
  3183. * - update transcoder timings
  3184. * - DP transcoding bits
  3185. * - transcoder
  3186. */
  3187. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3188. {
  3189. struct drm_device *dev = crtc->dev;
  3190. struct drm_i915_private *dev_priv = dev->dev_private;
  3191. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3192. int pipe = intel_crtc->pipe;
  3193. u32 reg, temp;
  3194. assert_pch_transcoder_disabled(dev_priv, pipe);
  3195. if (IS_IVYBRIDGE(dev))
  3196. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3197. /* Write the TU size bits before fdi link training, so that error
  3198. * detection works. */
  3199. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3200. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3201. /* For PCH output, training FDI link */
  3202. dev_priv->display.fdi_link_train(crtc);
  3203. /* We need to program the right clock selection before writing the pixel
  3204. * mutliplier into the DPLL. */
  3205. if (HAS_PCH_CPT(dev)) {
  3206. u32 sel;
  3207. temp = I915_READ(PCH_DPLL_SEL);
  3208. temp |= TRANS_DPLL_ENABLE(pipe);
  3209. sel = TRANS_DPLLB_SEL(pipe);
  3210. if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
  3211. temp |= sel;
  3212. else
  3213. temp &= ~sel;
  3214. I915_WRITE(PCH_DPLL_SEL, temp);
  3215. }
  3216. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3217. * transcoder, and we actually should do this to not upset any PCH
  3218. * transcoder that already use the clock when we share it.
  3219. *
  3220. * Note that enable_shared_dpll tries to do the right thing, but
  3221. * get_shared_dpll unconditionally resets the pll - we need that to have
  3222. * the right LVDS enable sequence. */
  3223. intel_enable_shared_dpll(intel_crtc);
  3224. /* set transcoder timing, panel must allow it */
  3225. assert_panel_unlocked(dev_priv, pipe);
  3226. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3227. intel_fdi_normal_train(crtc);
  3228. /* For PCH DP, enable TRANS_DP_CTL */
  3229. if (HAS_PCH_CPT(dev) && intel_crtc->config.has_dp_encoder) {
  3230. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3231. reg = TRANS_DP_CTL(pipe);
  3232. temp = I915_READ(reg);
  3233. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3234. TRANS_DP_SYNC_MASK |
  3235. TRANS_DP_BPC_MASK);
  3236. temp |= (TRANS_DP_OUTPUT_ENABLE |
  3237. TRANS_DP_ENH_FRAMING);
  3238. temp |= bpc << 9; /* same format but at 11:9 */
  3239. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3240. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3241. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3242. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3243. switch (intel_trans_dp_port_sel(crtc)) {
  3244. case PCH_DP_B:
  3245. temp |= TRANS_DP_PORT_SEL_B;
  3246. break;
  3247. case PCH_DP_C:
  3248. temp |= TRANS_DP_PORT_SEL_C;
  3249. break;
  3250. case PCH_DP_D:
  3251. temp |= TRANS_DP_PORT_SEL_D;
  3252. break;
  3253. default:
  3254. BUG();
  3255. }
  3256. I915_WRITE(reg, temp);
  3257. }
  3258. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3259. }
  3260. static void lpt_pch_enable(struct drm_crtc *crtc)
  3261. {
  3262. struct drm_device *dev = crtc->dev;
  3263. struct drm_i915_private *dev_priv = dev->dev_private;
  3264. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3265. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3266. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3267. lpt_program_iclkip(crtc);
  3268. /* Set transcoder timing. */
  3269. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3270. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3271. }
  3272. void intel_put_shared_dpll(struct intel_crtc *crtc)
  3273. {
  3274. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3275. if (pll == NULL)
  3276. return;
  3277. if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
  3278. WARN(1, "bad %s crtc mask\n", pll->name);
  3279. return;
  3280. }
  3281. pll->config.crtc_mask &= ~(1 << crtc->pipe);
  3282. if (pll->config.crtc_mask == 0) {
  3283. WARN_ON(pll->on);
  3284. WARN_ON(pll->active);
  3285. }
  3286. crtc->config.shared_dpll = DPLL_ID_PRIVATE;
  3287. }
  3288. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
  3289. {
  3290. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3291. struct intel_shared_dpll *pll;
  3292. enum intel_dpll_id i;
  3293. if (HAS_PCH_IBX(dev_priv->dev)) {
  3294. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3295. i = (enum intel_dpll_id) crtc->pipe;
  3296. pll = &dev_priv->shared_dplls[i];
  3297. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3298. crtc->base.base.id, pll->name);
  3299. WARN_ON(pll->new_config->crtc_mask);
  3300. goto found;
  3301. }
  3302. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3303. pll = &dev_priv->shared_dplls[i];
  3304. /* Only want to check enabled timings first */
  3305. if (pll->new_config->crtc_mask == 0)
  3306. continue;
  3307. if (memcmp(&crtc->new_config->dpll_hw_state,
  3308. &pll->new_config->hw_state,
  3309. sizeof(pll->new_config->hw_state)) == 0) {
  3310. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3311. crtc->base.base.id, pll->name,
  3312. pll->new_config->crtc_mask,
  3313. pll->active);
  3314. goto found;
  3315. }
  3316. }
  3317. /* Ok no matching timings, maybe there's a free one? */
  3318. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3319. pll = &dev_priv->shared_dplls[i];
  3320. if (pll->new_config->crtc_mask == 0) {
  3321. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3322. crtc->base.base.id, pll->name);
  3323. goto found;
  3324. }
  3325. }
  3326. return NULL;
  3327. found:
  3328. if (pll->new_config->crtc_mask == 0)
  3329. pll->new_config->hw_state = crtc->new_config->dpll_hw_state;
  3330. crtc->new_config->shared_dpll = i;
  3331. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3332. pipe_name(crtc->pipe));
  3333. pll->new_config->crtc_mask |= 1 << crtc->pipe;
  3334. return pll;
  3335. }
  3336. /**
  3337. * intel_shared_dpll_start_config - start a new PLL staged config
  3338. * @dev_priv: DRM device
  3339. * @clear_pipes: mask of pipes that will have their PLLs freed
  3340. *
  3341. * Starts a new PLL staged config, copying the current config but
  3342. * releasing the references of pipes specified in clear_pipes.
  3343. */
  3344. static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
  3345. unsigned clear_pipes)
  3346. {
  3347. struct intel_shared_dpll *pll;
  3348. enum intel_dpll_id i;
  3349. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3350. pll = &dev_priv->shared_dplls[i];
  3351. pll->new_config = kmemdup(&pll->config, sizeof pll->config,
  3352. GFP_KERNEL);
  3353. if (!pll->new_config)
  3354. goto cleanup;
  3355. pll->new_config->crtc_mask &= ~clear_pipes;
  3356. }
  3357. return 0;
  3358. cleanup:
  3359. while (--i >= 0) {
  3360. pll = &dev_priv->shared_dplls[i];
  3361. kfree(pll->new_config);
  3362. pll->new_config = NULL;
  3363. }
  3364. return -ENOMEM;
  3365. }
  3366. static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
  3367. {
  3368. struct intel_shared_dpll *pll;
  3369. enum intel_dpll_id i;
  3370. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3371. pll = &dev_priv->shared_dplls[i];
  3372. WARN_ON(pll->new_config == &pll->config);
  3373. pll->config = *pll->new_config;
  3374. kfree(pll->new_config);
  3375. pll->new_config = NULL;
  3376. }
  3377. }
  3378. static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
  3379. {
  3380. struct intel_shared_dpll *pll;
  3381. enum intel_dpll_id i;
  3382. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3383. pll = &dev_priv->shared_dplls[i];
  3384. WARN_ON(pll->new_config == &pll->config);
  3385. kfree(pll->new_config);
  3386. pll->new_config = NULL;
  3387. }
  3388. }
  3389. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3390. {
  3391. struct drm_i915_private *dev_priv = dev->dev_private;
  3392. int dslreg = PIPEDSL(pipe);
  3393. u32 temp;
  3394. temp = I915_READ(dslreg);
  3395. udelay(500);
  3396. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3397. if (wait_for(I915_READ(dslreg) != temp, 5))
  3398. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3399. }
  3400. }
  3401. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3402. {
  3403. struct drm_device *dev = crtc->base.dev;
  3404. struct drm_i915_private *dev_priv = dev->dev_private;
  3405. int pipe = crtc->pipe;
  3406. if (crtc->config.pch_pfit.enabled) {
  3407. I915_WRITE(PS_CTL(pipe), PS_ENABLE);
  3408. I915_WRITE(PS_WIN_POS(pipe), crtc->config.pch_pfit.pos);
  3409. I915_WRITE(PS_WIN_SZ(pipe), crtc->config.pch_pfit.size);
  3410. }
  3411. }
  3412. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3413. {
  3414. struct drm_device *dev = crtc->base.dev;
  3415. struct drm_i915_private *dev_priv = dev->dev_private;
  3416. int pipe = crtc->pipe;
  3417. if (crtc->config.pch_pfit.enabled) {
  3418. /* Force use of hard-coded filter coefficients
  3419. * as some pre-programmed values are broken,
  3420. * e.g. x201.
  3421. */
  3422. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3423. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3424. PF_PIPE_SEL_IVB(pipe));
  3425. else
  3426. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3427. I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
  3428. I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
  3429. }
  3430. }
  3431. static void intel_enable_planes(struct drm_crtc *crtc)
  3432. {
  3433. struct drm_device *dev = crtc->dev;
  3434. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3435. struct drm_plane *plane;
  3436. struct intel_plane *intel_plane;
  3437. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3438. intel_plane = to_intel_plane(plane);
  3439. if (intel_plane->pipe == pipe)
  3440. intel_plane_restore(&intel_plane->base);
  3441. }
  3442. }
  3443. static void intel_disable_planes(struct drm_crtc *crtc)
  3444. {
  3445. struct drm_device *dev = crtc->dev;
  3446. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3447. struct drm_plane *plane;
  3448. struct intel_plane *intel_plane;
  3449. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3450. intel_plane = to_intel_plane(plane);
  3451. if (intel_plane->pipe == pipe)
  3452. intel_plane_disable(&intel_plane->base);
  3453. }
  3454. }
  3455. void hsw_enable_ips(struct intel_crtc *crtc)
  3456. {
  3457. struct drm_device *dev = crtc->base.dev;
  3458. struct drm_i915_private *dev_priv = dev->dev_private;
  3459. if (!crtc->config.ips_enabled)
  3460. return;
  3461. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3462. intel_wait_for_vblank(dev, crtc->pipe);
  3463. assert_plane_enabled(dev_priv, crtc->plane);
  3464. if (IS_BROADWELL(dev)) {
  3465. mutex_lock(&dev_priv->rps.hw_lock);
  3466. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3467. mutex_unlock(&dev_priv->rps.hw_lock);
  3468. /* Quoting Art Runyan: "its not safe to expect any particular
  3469. * value in IPS_CTL bit 31 after enabling IPS through the
  3470. * mailbox." Moreover, the mailbox may return a bogus state,
  3471. * so we need to just enable it and continue on.
  3472. */
  3473. } else {
  3474. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3475. /* The bit only becomes 1 in the next vblank, so this wait here
  3476. * is essentially intel_wait_for_vblank. If we don't have this
  3477. * and don't wait for vblanks until the end of crtc_enable, then
  3478. * the HW state readout code will complain that the expected
  3479. * IPS_CTL value is not the one we read. */
  3480. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3481. DRM_ERROR("Timed out waiting for IPS enable\n");
  3482. }
  3483. }
  3484. void hsw_disable_ips(struct intel_crtc *crtc)
  3485. {
  3486. struct drm_device *dev = crtc->base.dev;
  3487. struct drm_i915_private *dev_priv = dev->dev_private;
  3488. if (!crtc->config.ips_enabled)
  3489. return;
  3490. assert_plane_enabled(dev_priv, crtc->plane);
  3491. if (IS_BROADWELL(dev)) {
  3492. mutex_lock(&dev_priv->rps.hw_lock);
  3493. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3494. mutex_unlock(&dev_priv->rps.hw_lock);
  3495. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3496. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3497. DRM_ERROR("Timed out waiting for IPS disable\n");
  3498. } else {
  3499. I915_WRITE(IPS_CTL, 0);
  3500. POSTING_READ(IPS_CTL);
  3501. }
  3502. /* We need to wait for a vblank before we can disable the plane. */
  3503. intel_wait_for_vblank(dev, crtc->pipe);
  3504. }
  3505. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3506. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3507. {
  3508. struct drm_device *dev = crtc->dev;
  3509. struct drm_i915_private *dev_priv = dev->dev_private;
  3510. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3511. enum pipe pipe = intel_crtc->pipe;
  3512. int palreg = PALETTE(pipe);
  3513. int i;
  3514. bool reenable_ips = false;
  3515. /* The clocks have to be on to load the palette. */
  3516. if (!crtc->enabled || !intel_crtc->active)
  3517. return;
  3518. if (!HAS_PCH_SPLIT(dev_priv->dev)) {
  3519. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3520. assert_dsi_pll_enabled(dev_priv);
  3521. else
  3522. assert_pll_enabled(dev_priv, pipe);
  3523. }
  3524. /* use legacy palette for Ironlake */
  3525. if (!HAS_GMCH_DISPLAY(dev))
  3526. palreg = LGC_PALETTE(pipe);
  3527. /* Workaround : Do not read or write the pipe palette/gamma data while
  3528. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3529. */
  3530. if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
  3531. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3532. GAMMA_MODE_MODE_SPLIT)) {
  3533. hsw_disable_ips(intel_crtc);
  3534. reenable_ips = true;
  3535. }
  3536. for (i = 0; i < 256; i++) {
  3537. I915_WRITE(palreg + 4 * i,
  3538. (intel_crtc->lut_r[i] << 16) |
  3539. (intel_crtc->lut_g[i] << 8) |
  3540. intel_crtc->lut_b[i]);
  3541. }
  3542. if (reenable_ips)
  3543. hsw_enable_ips(intel_crtc);
  3544. }
  3545. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  3546. {
  3547. if (!enable && intel_crtc->overlay) {
  3548. struct drm_device *dev = intel_crtc->base.dev;
  3549. struct drm_i915_private *dev_priv = dev->dev_private;
  3550. mutex_lock(&dev->struct_mutex);
  3551. dev_priv->mm.interruptible = false;
  3552. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3553. dev_priv->mm.interruptible = true;
  3554. mutex_unlock(&dev->struct_mutex);
  3555. }
  3556. /* Let userspace switch the overlay on again. In most cases userspace
  3557. * has to recompute where to put it anyway.
  3558. */
  3559. }
  3560. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  3561. {
  3562. struct drm_device *dev = crtc->dev;
  3563. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3564. int pipe = intel_crtc->pipe;
  3565. intel_enable_primary_hw_plane(crtc->primary, crtc);
  3566. intel_enable_planes(crtc);
  3567. intel_crtc_update_cursor(crtc, true);
  3568. intel_crtc_dpms_overlay(intel_crtc, true);
  3569. hsw_enable_ips(intel_crtc);
  3570. mutex_lock(&dev->struct_mutex);
  3571. intel_update_fbc(dev);
  3572. mutex_unlock(&dev->struct_mutex);
  3573. /*
  3574. * FIXME: Once we grow proper nuclear flip support out of this we need
  3575. * to compute the mask of flip planes precisely. For the time being
  3576. * consider this a flip from a NULL plane.
  3577. */
  3578. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3579. }
  3580. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  3581. {
  3582. struct drm_device *dev = crtc->dev;
  3583. struct drm_i915_private *dev_priv = dev->dev_private;
  3584. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3585. int pipe = intel_crtc->pipe;
  3586. int plane = intel_crtc->plane;
  3587. intel_crtc_wait_for_pending_flips(crtc);
  3588. if (dev_priv->fbc.plane == plane)
  3589. intel_disable_fbc(dev);
  3590. hsw_disable_ips(intel_crtc);
  3591. intel_crtc_dpms_overlay(intel_crtc, false);
  3592. intel_crtc_update_cursor(crtc, false);
  3593. intel_disable_planes(crtc);
  3594. intel_disable_primary_hw_plane(crtc->primary, crtc);
  3595. /*
  3596. * FIXME: Once we grow proper nuclear flip support out of this we need
  3597. * to compute the mask of flip planes precisely. For the time being
  3598. * consider this a flip to a NULL plane.
  3599. */
  3600. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3601. }
  3602. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  3603. {
  3604. struct drm_device *dev = crtc->dev;
  3605. struct drm_i915_private *dev_priv = dev->dev_private;
  3606. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3607. struct intel_encoder *encoder;
  3608. int pipe = intel_crtc->pipe;
  3609. WARN_ON(!crtc->enabled);
  3610. if (intel_crtc->active)
  3611. return;
  3612. if (intel_crtc->config.has_pch_encoder)
  3613. intel_prepare_shared_dpll(intel_crtc);
  3614. if (intel_crtc->config.has_dp_encoder)
  3615. intel_dp_set_m_n(intel_crtc);
  3616. intel_set_pipe_timings(intel_crtc);
  3617. if (intel_crtc->config.has_pch_encoder) {
  3618. intel_cpu_transcoder_set_m_n(intel_crtc,
  3619. &intel_crtc->config.fdi_m_n, NULL);
  3620. }
  3621. ironlake_set_pipeconf(crtc);
  3622. intel_crtc->active = true;
  3623. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3624. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  3625. for_each_encoder_on_crtc(dev, crtc, encoder)
  3626. if (encoder->pre_enable)
  3627. encoder->pre_enable(encoder);
  3628. if (intel_crtc->config.has_pch_encoder) {
  3629. /* Note: FDI PLL enabling _must_ be done before we enable the
  3630. * cpu pipes, hence this is separate from all the other fdi/pch
  3631. * enabling. */
  3632. ironlake_fdi_pll_enable(intel_crtc);
  3633. } else {
  3634. assert_fdi_tx_disabled(dev_priv, pipe);
  3635. assert_fdi_rx_disabled(dev_priv, pipe);
  3636. }
  3637. ironlake_pfit_enable(intel_crtc);
  3638. /*
  3639. * On ILK+ LUT must be loaded before the pipe is running but with
  3640. * clocks enabled
  3641. */
  3642. intel_crtc_load_lut(crtc);
  3643. intel_update_watermarks(crtc);
  3644. intel_enable_pipe(intel_crtc);
  3645. if (intel_crtc->config.has_pch_encoder)
  3646. ironlake_pch_enable(crtc);
  3647. for_each_encoder_on_crtc(dev, crtc, encoder)
  3648. encoder->enable(encoder);
  3649. if (HAS_PCH_CPT(dev))
  3650. cpt_verify_modeset(dev, intel_crtc->pipe);
  3651. assert_vblank_disabled(crtc);
  3652. drm_crtc_vblank_on(crtc);
  3653. intel_crtc_enable_planes(crtc);
  3654. }
  3655. /* IPS only exists on ULT machines and is tied to pipe A. */
  3656. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  3657. {
  3658. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  3659. }
  3660. /*
  3661. * This implements the workaround described in the "notes" section of the mode
  3662. * set sequence documentation. When going from no pipes or single pipe to
  3663. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  3664. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  3665. */
  3666. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  3667. {
  3668. struct drm_device *dev = crtc->base.dev;
  3669. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  3670. /* We want to get the other_active_crtc only if there's only 1 other
  3671. * active crtc. */
  3672. for_each_intel_crtc(dev, crtc_it) {
  3673. if (!crtc_it->active || crtc_it == crtc)
  3674. continue;
  3675. if (other_active_crtc)
  3676. return;
  3677. other_active_crtc = crtc_it;
  3678. }
  3679. if (!other_active_crtc)
  3680. return;
  3681. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3682. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3683. }
  3684. static void haswell_crtc_enable(struct drm_crtc *crtc)
  3685. {
  3686. struct drm_device *dev = crtc->dev;
  3687. struct drm_i915_private *dev_priv = dev->dev_private;
  3688. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3689. struct intel_encoder *encoder;
  3690. int pipe = intel_crtc->pipe;
  3691. WARN_ON(!crtc->enabled);
  3692. if (intel_crtc->active)
  3693. return;
  3694. if (intel_crtc_to_shared_dpll(intel_crtc))
  3695. intel_enable_shared_dpll(intel_crtc);
  3696. if (intel_crtc->config.has_dp_encoder)
  3697. intel_dp_set_m_n(intel_crtc);
  3698. intel_set_pipe_timings(intel_crtc);
  3699. if (intel_crtc->config.cpu_transcoder != TRANSCODER_EDP) {
  3700. I915_WRITE(PIPE_MULT(intel_crtc->config.cpu_transcoder),
  3701. intel_crtc->config.pixel_multiplier - 1);
  3702. }
  3703. if (intel_crtc->config.has_pch_encoder) {
  3704. intel_cpu_transcoder_set_m_n(intel_crtc,
  3705. &intel_crtc->config.fdi_m_n, NULL);
  3706. }
  3707. haswell_set_pipeconf(crtc);
  3708. intel_set_pipe_csc(crtc);
  3709. intel_crtc->active = true;
  3710. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3711. for_each_encoder_on_crtc(dev, crtc, encoder)
  3712. if (encoder->pre_enable)
  3713. encoder->pre_enable(encoder);
  3714. if (intel_crtc->config.has_pch_encoder) {
  3715. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  3716. true);
  3717. dev_priv->display.fdi_link_train(crtc);
  3718. }
  3719. intel_ddi_enable_pipe_clock(intel_crtc);
  3720. if (IS_SKYLAKE(dev))
  3721. skylake_pfit_enable(intel_crtc);
  3722. else
  3723. ironlake_pfit_enable(intel_crtc);
  3724. /*
  3725. * On ILK+ LUT must be loaded before the pipe is running but with
  3726. * clocks enabled
  3727. */
  3728. intel_crtc_load_lut(crtc);
  3729. intel_ddi_set_pipe_settings(crtc);
  3730. intel_ddi_enable_transcoder_func(crtc);
  3731. intel_update_watermarks(crtc);
  3732. intel_enable_pipe(intel_crtc);
  3733. if (intel_crtc->config.has_pch_encoder)
  3734. lpt_pch_enable(crtc);
  3735. if (intel_crtc->config.dp_encoder_is_mst)
  3736. intel_ddi_set_vc_payload_alloc(crtc, true);
  3737. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3738. encoder->enable(encoder);
  3739. intel_opregion_notify_encoder(encoder, true);
  3740. }
  3741. assert_vblank_disabled(crtc);
  3742. drm_crtc_vblank_on(crtc);
  3743. /* If we change the relative order between pipe/planes enabling, we need
  3744. * to change the workaround. */
  3745. haswell_mode_set_planes_workaround(intel_crtc);
  3746. intel_crtc_enable_planes(crtc);
  3747. }
  3748. static void skylake_pfit_disable(struct intel_crtc *crtc)
  3749. {
  3750. struct drm_device *dev = crtc->base.dev;
  3751. struct drm_i915_private *dev_priv = dev->dev_private;
  3752. int pipe = crtc->pipe;
  3753. /* To avoid upsetting the power well on haswell only disable the pfit if
  3754. * it's in use. The hw state code will make sure we get this right. */
  3755. if (crtc->config.pch_pfit.enabled) {
  3756. I915_WRITE(PS_CTL(pipe), 0);
  3757. I915_WRITE(PS_WIN_POS(pipe), 0);
  3758. I915_WRITE(PS_WIN_SZ(pipe), 0);
  3759. }
  3760. }
  3761. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  3762. {
  3763. struct drm_device *dev = crtc->base.dev;
  3764. struct drm_i915_private *dev_priv = dev->dev_private;
  3765. int pipe = crtc->pipe;
  3766. /* To avoid upsetting the power well on haswell only disable the pfit if
  3767. * it's in use. The hw state code will make sure we get this right. */
  3768. if (crtc->config.pch_pfit.enabled) {
  3769. I915_WRITE(PF_CTL(pipe), 0);
  3770. I915_WRITE(PF_WIN_POS(pipe), 0);
  3771. I915_WRITE(PF_WIN_SZ(pipe), 0);
  3772. }
  3773. }
  3774. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  3775. {
  3776. struct drm_device *dev = crtc->dev;
  3777. struct drm_i915_private *dev_priv = dev->dev_private;
  3778. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3779. struct intel_encoder *encoder;
  3780. int pipe = intel_crtc->pipe;
  3781. u32 reg, temp;
  3782. if (!intel_crtc->active)
  3783. return;
  3784. intel_crtc_disable_planes(crtc);
  3785. drm_crtc_vblank_off(crtc);
  3786. assert_vblank_disabled(crtc);
  3787. for_each_encoder_on_crtc(dev, crtc, encoder)
  3788. encoder->disable(encoder);
  3789. if (intel_crtc->config.has_pch_encoder)
  3790. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  3791. intel_disable_pipe(intel_crtc);
  3792. ironlake_pfit_disable(intel_crtc);
  3793. for_each_encoder_on_crtc(dev, crtc, encoder)
  3794. if (encoder->post_disable)
  3795. encoder->post_disable(encoder);
  3796. if (intel_crtc->config.has_pch_encoder) {
  3797. ironlake_fdi_disable(crtc);
  3798. ironlake_disable_pch_transcoder(dev_priv, pipe);
  3799. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  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_update_fbc(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. drm_crtc_vblank_off(crtc);
  3834. assert_vblank_disabled(crtc);
  3835. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3836. intel_opregion_notify_encoder(encoder, false);
  3837. encoder->disable(encoder);
  3838. }
  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_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  3854. true);
  3855. intel_ddi_fdi_disable(crtc);
  3856. }
  3857. for_each_encoder_on_crtc(dev, crtc, encoder)
  3858. if (encoder->post_disable)
  3859. encoder->post_disable(encoder);
  3860. intel_crtc->active = false;
  3861. intel_update_watermarks(crtc);
  3862. mutex_lock(&dev->struct_mutex);
  3863. intel_update_fbc(dev);
  3864. mutex_unlock(&dev->struct_mutex);
  3865. if (intel_crtc_to_shared_dpll(intel_crtc))
  3866. intel_disable_shared_dpll(intel_crtc);
  3867. }
  3868. static void ironlake_crtc_off(struct drm_crtc *crtc)
  3869. {
  3870. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3871. intel_put_shared_dpll(intel_crtc);
  3872. }
  3873. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  3874. {
  3875. struct drm_device *dev = crtc->base.dev;
  3876. struct drm_i915_private *dev_priv = dev->dev_private;
  3877. struct intel_crtc_config *pipe_config = &crtc->config;
  3878. if (!crtc->config.gmch_pfit.control)
  3879. return;
  3880. /*
  3881. * The panel fitter should only be adjusted whilst the pipe is disabled,
  3882. * according to register description and PRM.
  3883. */
  3884. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  3885. assert_pipe_disabled(dev_priv, crtc->pipe);
  3886. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  3887. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  3888. /* Border color in case we don't scale up to the full screen. Black by
  3889. * default, change to something else for debugging. */
  3890. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  3891. }
  3892. static enum intel_display_power_domain port_to_power_domain(enum port port)
  3893. {
  3894. switch (port) {
  3895. case PORT_A:
  3896. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  3897. case PORT_B:
  3898. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  3899. case PORT_C:
  3900. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  3901. case PORT_D:
  3902. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  3903. default:
  3904. WARN_ON_ONCE(1);
  3905. return POWER_DOMAIN_PORT_OTHER;
  3906. }
  3907. }
  3908. #define for_each_power_domain(domain, mask) \
  3909. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  3910. if ((1 << (domain)) & (mask))
  3911. enum intel_display_power_domain
  3912. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  3913. {
  3914. struct drm_device *dev = intel_encoder->base.dev;
  3915. struct intel_digital_port *intel_dig_port;
  3916. switch (intel_encoder->type) {
  3917. case INTEL_OUTPUT_UNKNOWN:
  3918. /* Only DDI platforms should ever use this output type */
  3919. WARN_ON_ONCE(!HAS_DDI(dev));
  3920. case INTEL_OUTPUT_DISPLAYPORT:
  3921. case INTEL_OUTPUT_HDMI:
  3922. case INTEL_OUTPUT_EDP:
  3923. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  3924. return port_to_power_domain(intel_dig_port->port);
  3925. case INTEL_OUTPUT_DP_MST:
  3926. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  3927. return port_to_power_domain(intel_dig_port->port);
  3928. case INTEL_OUTPUT_ANALOG:
  3929. return POWER_DOMAIN_PORT_CRT;
  3930. case INTEL_OUTPUT_DSI:
  3931. return POWER_DOMAIN_PORT_DSI;
  3932. default:
  3933. return POWER_DOMAIN_PORT_OTHER;
  3934. }
  3935. }
  3936. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  3937. {
  3938. struct drm_device *dev = crtc->dev;
  3939. struct intel_encoder *intel_encoder;
  3940. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3941. enum pipe pipe = intel_crtc->pipe;
  3942. unsigned long mask;
  3943. enum transcoder transcoder;
  3944. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  3945. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  3946. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  3947. if (intel_crtc->config.pch_pfit.enabled ||
  3948. intel_crtc->config.pch_pfit.force_thru)
  3949. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  3950. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  3951. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  3952. return mask;
  3953. }
  3954. static void modeset_update_crtc_power_domains(struct drm_device *dev)
  3955. {
  3956. struct drm_i915_private *dev_priv = dev->dev_private;
  3957. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  3958. struct intel_crtc *crtc;
  3959. /*
  3960. * First get all needed power domains, then put all unneeded, to avoid
  3961. * any unnecessary toggling of the power wells.
  3962. */
  3963. for_each_intel_crtc(dev, crtc) {
  3964. enum intel_display_power_domain domain;
  3965. if (!crtc->base.enabled)
  3966. continue;
  3967. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  3968. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  3969. intel_display_power_get(dev_priv, domain);
  3970. }
  3971. if (dev_priv->display.modeset_global_resources)
  3972. dev_priv->display.modeset_global_resources(dev);
  3973. for_each_intel_crtc(dev, crtc) {
  3974. enum intel_display_power_domain domain;
  3975. for_each_power_domain(domain, crtc->enabled_power_domains)
  3976. intel_display_power_put(dev_priv, domain);
  3977. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  3978. }
  3979. intel_display_set_init_power(dev_priv, false);
  3980. }
  3981. /* returns HPLL frequency in kHz */
  3982. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  3983. {
  3984. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  3985. /* Obtain SKU information */
  3986. mutex_lock(&dev_priv->dpio_lock);
  3987. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  3988. CCK_FUSE_HPLL_FREQ_MASK;
  3989. mutex_unlock(&dev_priv->dpio_lock);
  3990. return vco_freq[hpll_freq] * 1000;
  3991. }
  3992. static void vlv_update_cdclk(struct drm_device *dev)
  3993. {
  3994. struct drm_i915_private *dev_priv = dev->dev_private;
  3995. dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  3996. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  3997. dev_priv->vlv_cdclk_freq);
  3998. /*
  3999. * Program the gmbus_freq based on the cdclk frequency.
  4000. * BSpec erroneously claims we should aim for 4MHz, but
  4001. * in fact 1MHz is the correct frequency.
  4002. */
  4003. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->vlv_cdclk_freq, 1000));
  4004. }
  4005. /* Adjust CDclk dividers to allow high res or save power if possible */
  4006. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4007. {
  4008. struct drm_i915_private *dev_priv = dev->dev_private;
  4009. u32 val, cmd;
  4010. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  4011. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4012. cmd = 2;
  4013. else if (cdclk == 266667)
  4014. cmd = 1;
  4015. else
  4016. cmd = 0;
  4017. mutex_lock(&dev_priv->rps.hw_lock);
  4018. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4019. val &= ~DSPFREQGUAR_MASK;
  4020. val |= (cmd << DSPFREQGUAR_SHIFT);
  4021. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4022. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4023. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4024. 50)) {
  4025. DRM_ERROR("timed out waiting for CDclk change\n");
  4026. }
  4027. mutex_unlock(&dev_priv->rps.hw_lock);
  4028. if (cdclk == 400000) {
  4029. u32 divider;
  4030. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4031. mutex_lock(&dev_priv->dpio_lock);
  4032. /* adjust cdclk divider */
  4033. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4034. val &= ~DISPLAY_FREQUENCY_VALUES;
  4035. val |= divider;
  4036. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4037. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4038. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4039. 50))
  4040. DRM_ERROR("timed out waiting for CDclk change\n");
  4041. mutex_unlock(&dev_priv->dpio_lock);
  4042. }
  4043. mutex_lock(&dev_priv->dpio_lock);
  4044. /* adjust self-refresh exit latency value */
  4045. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4046. val &= ~0x7f;
  4047. /*
  4048. * For high bandwidth configs, we set a higher latency in the bunit
  4049. * so that the core display fetch happens in time to avoid underruns.
  4050. */
  4051. if (cdclk == 400000)
  4052. val |= 4500 / 250; /* 4.5 usec */
  4053. else
  4054. val |= 3000 / 250; /* 3.0 usec */
  4055. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4056. mutex_unlock(&dev_priv->dpio_lock);
  4057. vlv_update_cdclk(dev);
  4058. }
  4059. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4060. {
  4061. struct drm_i915_private *dev_priv = dev->dev_private;
  4062. u32 val, cmd;
  4063. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  4064. switch (cdclk) {
  4065. case 400000:
  4066. cmd = 3;
  4067. break;
  4068. case 333333:
  4069. case 320000:
  4070. cmd = 2;
  4071. break;
  4072. case 266667:
  4073. cmd = 1;
  4074. break;
  4075. case 200000:
  4076. cmd = 0;
  4077. break;
  4078. default:
  4079. WARN_ON(1);
  4080. return;
  4081. }
  4082. mutex_lock(&dev_priv->rps.hw_lock);
  4083. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4084. val &= ~DSPFREQGUAR_MASK_CHV;
  4085. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4086. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4087. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4088. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4089. 50)) {
  4090. DRM_ERROR("timed out waiting for CDclk change\n");
  4091. }
  4092. mutex_unlock(&dev_priv->rps.hw_lock);
  4093. vlv_update_cdclk(dev);
  4094. }
  4095. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4096. int max_pixclk)
  4097. {
  4098. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4099. /* FIXME: Punit isn't quite ready yet */
  4100. if (IS_CHERRYVIEW(dev_priv->dev))
  4101. return 400000;
  4102. /*
  4103. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4104. * 200MHz
  4105. * 267MHz
  4106. * 320/333MHz (depends on HPLL freq)
  4107. * 400MHz
  4108. * So we check to see whether we're above 90% of the lower bin and
  4109. * adjust if needed.
  4110. *
  4111. * We seem to get an unstable or solid color picture at 200MHz.
  4112. * Not sure what's wrong. For now use 200MHz only when all pipes
  4113. * are off.
  4114. */
  4115. if (max_pixclk > freq_320*9/10)
  4116. return 400000;
  4117. else if (max_pixclk > 266667*9/10)
  4118. return freq_320;
  4119. else if (max_pixclk > 0)
  4120. return 266667;
  4121. else
  4122. return 200000;
  4123. }
  4124. /* compute the max pixel clock for new configuration */
  4125. static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
  4126. {
  4127. struct drm_device *dev = dev_priv->dev;
  4128. struct intel_crtc *intel_crtc;
  4129. int max_pixclk = 0;
  4130. for_each_intel_crtc(dev, intel_crtc) {
  4131. if (intel_crtc->new_enabled)
  4132. max_pixclk = max(max_pixclk,
  4133. intel_crtc->new_config->adjusted_mode.crtc_clock);
  4134. }
  4135. return max_pixclk;
  4136. }
  4137. static void valleyview_modeset_global_pipes(struct drm_device *dev,
  4138. unsigned *prepare_pipes)
  4139. {
  4140. struct drm_i915_private *dev_priv = dev->dev_private;
  4141. struct intel_crtc *intel_crtc;
  4142. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4143. if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
  4144. dev_priv->vlv_cdclk_freq)
  4145. return;
  4146. /* disable/enable all currently active pipes while we change cdclk */
  4147. for_each_intel_crtc(dev, intel_crtc)
  4148. if (intel_crtc->base.enabled)
  4149. *prepare_pipes |= (1 << intel_crtc->pipe);
  4150. }
  4151. static void valleyview_modeset_global_resources(struct drm_device *dev)
  4152. {
  4153. struct drm_i915_private *dev_priv = dev->dev_private;
  4154. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4155. int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  4156. if (req_cdclk != dev_priv->vlv_cdclk_freq) {
  4157. /*
  4158. * FIXME: We can end up here with all power domains off, yet
  4159. * with a CDCLK frequency other than the minimum. To account
  4160. * for this take the PIPE-A power domain, which covers the HW
  4161. * blocks needed for the following programming. This can be
  4162. * removed once it's guaranteed that we get here either with
  4163. * the minimum CDCLK set, or the required power domains
  4164. * enabled.
  4165. */
  4166. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  4167. if (IS_CHERRYVIEW(dev))
  4168. cherryview_set_cdclk(dev, req_cdclk);
  4169. else
  4170. valleyview_set_cdclk(dev, req_cdclk);
  4171. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  4172. }
  4173. }
  4174. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  4175. {
  4176. struct drm_device *dev = crtc->dev;
  4177. struct drm_i915_private *dev_priv = to_i915(dev);
  4178. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4179. struct intel_encoder *encoder;
  4180. int pipe = intel_crtc->pipe;
  4181. bool is_dsi;
  4182. WARN_ON(!crtc->enabled);
  4183. if (intel_crtc->active)
  4184. return;
  4185. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  4186. if (!is_dsi) {
  4187. if (IS_CHERRYVIEW(dev))
  4188. chv_prepare_pll(intel_crtc, &intel_crtc->config);
  4189. else
  4190. vlv_prepare_pll(intel_crtc, &intel_crtc->config);
  4191. }
  4192. if (intel_crtc->config.has_dp_encoder)
  4193. intel_dp_set_m_n(intel_crtc);
  4194. intel_set_pipe_timings(intel_crtc);
  4195. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  4196. struct drm_i915_private *dev_priv = dev->dev_private;
  4197. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  4198. I915_WRITE(CHV_CANVAS(pipe), 0);
  4199. }
  4200. i9xx_set_pipeconf(intel_crtc);
  4201. intel_crtc->active = true;
  4202. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4203. for_each_encoder_on_crtc(dev, crtc, encoder)
  4204. if (encoder->pre_pll_enable)
  4205. encoder->pre_pll_enable(encoder);
  4206. if (!is_dsi) {
  4207. if (IS_CHERRYVIEW(dev))
  4208. chv_enable_pll(intel_crtc, &intel_crtc->config);
  4209. else
  4210. vlv_enable_pll(intel_crtc, &intel_crtc->config);
  4211. }
  4212. for_each_encoder_on_crtc(dev, crtc, encoder)
  4213. if (encoder->pre_enable)
  4214. encoder->pre_enable(encoder);
  4215. i9xx_pfit_enable(intel_crtc);
  4216. intel_crtc_load_lut(crtc);
  4217. intel_update_watermarks(crtc);
  4218. intel_enable_pipe(intel_crtc);
  4219. for_each_encoder_on_crtc(dev, crtc, encoder)
  4220. encoder->enable(encoder);
  4221. assert_vblank_disabled(crtc);
  4222. drm_crtc_vblank_on(crtc);
  4223. intel_crtc_enable_planes(crtc);
  4224. /* Underruns don't raise interrupts, so check manually. */
  4225. i9xx_check_fifo_underruns(dev_priv);
  4226. }
  4227. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  4228. {
  4229. struct drm_device *dev = crtc->base.dev;
  4230. struct drm_i915_private *dev_priv = dev->dev_private;
  4231. I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
  4232. I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
  4233. }
  4234. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  4235. {
  4236. struct drm_device *dev = crtc->dev;
  4237. struct drm_i915_private *dev_priv = to_i915(dev);
  4238. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4239. struct intel_encoder *encoder;
  4240. int pipe = intel_crtc->pipe;
  4241. WARN_ON(!crtc->enabled);
  4242. if (intel_crtc->active)
  4243. return;
  4244. i9xx_set_pll_dividers(intel_crtc);
  4245. if (intel_crtc->config.has_dp_encoder)
  4246. intel_dp_set_m_n(intel_crtc);
  4247. intel_set_pipe_timings(intel_crtc);
  4248. i9xx_set_pipeconf(intel_crtc);
  4249. intel_crtc->active = true;
  4250. if (!IS_GEN2(dev))
  4251. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4252. for_each_encoder_on_crtc(dev, crtc, encoder)
  4253. if (encoder->pre_enable)
  4254. encoder->pre_enable(encoder);
  4255. i9xx_enable_pll(intel_crtc);
  4256. i9xx_pfit_enable(intel_crtc);
  4257. intel_crtc_load_lut(crtc);
  4258. intel_update_watermarks(crtc);
  4259. intel_enable_pipe(intel_crtc);
  4260. for_each_encoder_on_crtc(dev, crtc, encoder)
  4261. encoder->enable(encoder);
  4262. assert_vblank_disabled(crtc);
  4263. drm_crtc_vblank_on(crtc);
  4264. intel_crtc_enable_planes(crtc);
  4265. /*
  4266. * Gen2 reports pipe underruns whenever all planes are disabled.
  4267. * So don't enable underrun reporting before at least some planes
  4268. * are enabled.
  4269. * FIXME: Need to fix the logic to work when we turn off all planes
  4270. * but leave the pipe running.
  4271. */
  4272. if (IS_GEN2(dev))
  4273. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4274. /* Underruns don't raise interrupts, so check manually. */
  4275. i9xx_check_fifo_underruns(dev_priv);
  4276. }
  4277. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4278. {
  4279. struct drm_device *dev = crtc->base.dev;
  4280. struct drm_i915_private *dev_priv = dev->dev_private;
  4281. if (!crtc->config.gmch_pfit.control)
  4282. return;
  4283. assert_pipe_disabled(dev_priv, crtc->pipe);
  4284. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4285. I915_READ(PFIT_CONTROL));
  4286. I915_WRITE(PFIT_CONTROL, 0);
  4287. }
  4288. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  4289. {
  4290. struct drm_device *dev = crtc->dev;
  4291. struct drm_i915_private *dev_priv = dev->dev_private;
  4292. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4293. struct intel_encoder *encoder;
  4294. int pipe = intel_crtc->pipe;
  4295. if (!intel_crtc->active)
  4296. return;
  4297. /*
  4298. * Gen2 reports pipe underruns whenever all planes are disabled.
  4299. * So diasble underrun reporting before all the planes get disabled.
  4300. * FIXME: Need to fix the logic to work when we turn off all planes
  4301. * but leave the pipe running.
  4302. */
  4303. if (IS_GEN2(dev))
  4304. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4305. /*
  4306. * Vblank time updates from the shadow to live plane control register
  4307. * are blocked if the memory self-refresh mode is active at that
  4308. * moment. So to make sure the plane gets truly disabled, disable
  4309. * first the self-refresh mode. The self-refresh enable bit in turn
  4310. * will be checked/applied by the HW only at the next frame start
  4311. * event which is after the vblank start event, so we need to have a
  4312. * wait-for-vblank between disabling the plane and the pipe.
  4313. */
  4314. intel_set_memory_cxsr(dev_priv, false);
  4315. intel_crtc_disable_planes(crtc);
  4316. /*
  4317. * On gen2 planes are double buffered but the pipe isn't, so we must
  4318. * wait for planes to fully turn off before disabling the pipe.
  4319. * We also need to wait on all gmch platforms because of the
  4320. * self-refresh mode constraint explained above.
  4321. */
  4322. intel_wait_for_vblank(dev, pipe);
  4323. drm_crtc_vblank_off(crtc);
  4324. assert_vblank_disabled(crtc);
  4325. for_each_encoder_on_crtc(dev, crtc, encoder)
  4326. encoder->disable(encoder);
  4327. intel_disable_pipe(intel_crtc);
  4328. i9xx_pfit_disable(intel_crtc);
  4329. for_each_encoder_on_crtc(dev, crtc, encoder)
  4330. if (encoder->post_disable)
  4331. encoder->post_disable(encoder);
  4332. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  4333. if (IS_CHERRYVIEW(dev))
  4334. chv_disable_pll(dev_priv, pipe);
  4335. else if (IS_VALLEYVIEW(dev))
  4336. vlv_disable_pll(dev_priv, pipe);
  4337. else
  4338. i9xx_disable_pll(intel_crtc);
  4339. }
  4340. if (!IS_GEN2(dev))
  4341. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4342. intel_crtc->active = false;
  4343. intel_update_watermarks(crtc);
  4344. mutex_lock(&dev->struct_mutex);
  4345. intel_update_fbc(dev);
  4346. mutex_unlock(&dev->struct_mutex);
  4347. }
  4348. static void i9xx_crtc_off(struct drm_crtc *crtc)
  4349. {
  4350. }
  4351. /* Master function to enable/disable CRTC and corresponding power wells */
  4352. void intel_crtc_control(struct drm_crtc *crtc, bool enable)
  4353. {
  4354. struct drm_device *dev = crtc->dev;
  4355. struct drm_i915_private *dev_priv = dev->dev_private;
  4356. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4357. enum intel_display_power_domain domain;
  4358. unsigned long domains;
  4359. if (enable) {
  4360. if (!intel_crtc->active) {
  4361. domains = get_crtc_power_domains(crtc);
  4362. for_each_power_domain(domain, domains)
  4363. intel_display_power_get(dev_priv, domain);
  4364. intel_crtc->enabled_power_domains = domains;
  4365. dev_priv->display.crtc_enable(crtc);
  4366. }
  4367. } else {
  4368. if (intel_crtc->active) {
  4369. dev_priv->display.crtc_disable(crtc);
  4370. domains = intel_crtc->enabled_power_domains;
  4371. for_each_power_domain(domain, domains)
  4372. intel_display_power_put(dev_priv, domain);
  4373. intel_crtc->enabled_power_domains = 0;
  4374. }
  4375. }
  4376. }
  4377. /**
  4378. * Sets the power management mode of the pipe and plane.
  4379. */
  4380. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  4381. {
  4382. struct drm_device *dev = crtc->dev;
  4383. struct intel_encoder *intel_encoder;
  4384. bool enable = false;
  4385. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4386. enable |= intel_encoder->connectors_active;
  4387. intel_crtc_control(crtc, enable);
  4388. }
  4389. static void intel_crtc_disable(struct drm_crtc *crtc)
  4390. {
  4391. struct drm_device *dev = crtc->dev;
  4392. struct drm_connector *connector;
  4393. struct drm_i915_private *dev_priv = dev->dev_private;
  4394. /* crtc should still be enabled when we disable it. */
  4395. WARN_ON(!crtc->enabled);
  4396. dev_priv->display.crtc_disable(crtc);
  4397. dev_priv->display.off(crtc);
  4398. crtc->primary->funcs->disable_plane(crtc->primary);
  4399. /* Update computed state. */
  4400. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  4401. if (!connector->encoder || !connector->encoder->crtc)
  4402. continue;
  4403. if (connector->encoder->crtc != crtc)
  4404. continue;
  4405. connector->dpms = DRM_MODE_DPMS_OFF;
  4406. to_intel_encoder(connector->encoder)->connectors_active = false;
  4407. }
  4408. }
  4409. void intel_encoder_destroy(struct drm_encoder *encoder)
  4410. {
  4411. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4412. drm_encoder_cleanup(encoder);
  4413. kfree(intel_encoder);
  4414. }
  4415. /* Simple dpms helper for encoders with just one connector, no cloning and only
  4416. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  4417. * state of the entire output pipe. */
  4418. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  4419. {
  4420. if (mode == DRM_MODE_DPMS_ON) {
  4421. encoder->connectors_active = true;
  4422. intel_crtc_update_dpms(encoder->base.crtc);
  4423. } else {
  4424. encoder->connectors_active = false;
  4425. intel_crtc_update_dpms(encoder->base.crtc);
  4426. }
  4427. }
  4428. /* Cross check the actual hw state with our own modeset state tracking (and it's
  4429. * internal consistency). */
  4430. static void intel_connector_check_state(struct intel_connector *connector)
  4431. {
  4432. if (connector->get_hw_state(connector)) {
  4433. struct intel_encoder *encoder = connector->encoder;
  4434. struct drm_crtc *crtc;
  4435. bool encoder_enabled;
  4436. enum pipe pipe;
  4437. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4438. connector->base.base.id,
  4439. connector->base.name);
  4440. /* there is no real hw state for MST connectors */
  4441. if (connector->mst_port)
  4442. return;
  4443. WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  4444. "wrong connector dpms state\n");
  4445. WARN(connector->base.encoder != &encoder->base,
  4446. "active connector not linked to encoder\n");
  4447. if (encoder) {
  4448. WARN(!encoder->connectors_active,
  4449. "encoder->connectors_active not set\n");
  4450. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  4451. WARN(!encoder_enabled, "encoder not enabled\n");
  4452. if (WARN_ON(!encoder->base.crtc))
  4453. return;
  4454. crtc = encoder->base.crtc;
  4455. WARN(!crtc->enabled, "crtc not enabled\n");
  4456. WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  4457. WARN(pipe != to_intel_crtc(crtc)->pipe,
  4458. "encoder active on the wrong pipe\n");
  4459. }
  4460. }
  4461. }
  4462. /* Even simpler default implementation, if there's really no special case to
  4463. * consider. */
  4464. void intel_connector_dpms(struct drm_connector *connector, int mode)
  4465. {
  4466. /* All the simple cases only support two dpms states. */
  4467. if (mode != DRM_MODE_DPMS_ON)
  4468. mode = DRM_MODE_DPMS_OFF;
  4469. if (mode == connector->dpms)
  4470. return;
  4471. connector->dpms = mode;
  4472. /* Only need to change hw state when actually enabled */
  4473. if (connector->encoder)
  4474. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  4475. intel_modeset_check_state(connector->dev);
  4476. }
  4477. /* Simple connector->get_hw_state implementation for encoders that support only
  4478. * one connector and no cloning and hence the encoder state determines the state
  4479. * of the connector. */
  4480. bool intel_connector_get_hw_state(struct intel_connector *connector)
  4481. {
  4482. enum pipe pipe = 0;
  4483. struct intel_encoder *encoder = connector->encoder;
  4484. return encoder->get_hw_state(encoder, &pipe);
  4485. }
  4486. static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  4487. struct intel_crtc_config *pipe_config)
  4488. {
  4489. struct drm_i915_private *dev_priv = dev->dev_private;
  4490. struct intel_crtc *pipe_B_crtc =
  4491. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  4492. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  4493. pipe_name(pipe), pipe_config->fdi_lanes);
  4494. if (pipe_config->fdi_lanes > 4) {
  4495. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  4496. pipe_name(pipe), pipe_config->fdi_lanes);
  4497. return false;
  4498. }
  4499. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  4500. if (pipe_config->fdi_lanes > 2) {
  4501. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  4502. pipe_config->fdi_lanes);
  4503. return false;
  4504. } else {
  4505. return true;
  4506. }
  4507. }
  4508. if (INTEL_INFO(dev)->num_pipes == 2)
  4509. return true;
  4510. /* Ivybridge 3 pipe is really complicated */
  4511. switch (pipe) {
  4512. case PIPE_A:
  4513. return true;
  4514. case PIPE_B:
  4515. if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
  4516. pipe_config->fdi_lanes > 2) {
  4517. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4518. pipe_name(pipe), pipe_config->fdi_lanes);
  4519. return false;
  4520. }
  4521. return true;
  4522. case PIPE_C:
  4523. if (!pipe_has_enabled_pch(pipe_B_crtc) ||
  4524. pipe_B_crtc->config.fdi_lanes <= 2) {
  4525. if (pipe_config->fdi_lanes > 2) {
  4526. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4527. pipe_name(pipe), pipe_config->fdi_lanes);
  4528. return false;
  4529. }
  4530. } else {
  4531. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  4532. return false;
  4533. }
  4534. return true;
  4535. default:
  4536. BUG();
  4537. }
  4538. }
  4539. #define RETRY 1
  4540. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  4541. struct intel_crtc_config *pipe_config)
  4542. {
  4543. struct drm_device *dev = intel_crtc->base.dev;
  4544. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4545. int lane, link_bw, fdi_dotclock;
  4546. bool setup_ok, needs_recompute = false;
  4547. retry:
  4548. /* FDI is a binary signal running at ~2.7GHz, encoding
  4549. * each output octet as 10 bits. The actual frequency
  4550. * is stored as a divider into a 100MHz clock, and the
  4551. * mode pixel clock is stored in units of 1KHz.
  4552. * Hence the bw of each lane in terms of the mode signal
  4553. * is:
  4554. */
  4555. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4556. fdi_dotclock = adjusted_mode->crtc_clock;
  4557. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  4558. pipe_config->pipe_bpp);
  4559. pipe_config->fdi_lanes = lane;
  4560. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  4561. link_bw, &pipe_config->fdi_m_n);
  4562. setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  4563. intel_crtc->pipe, pipe_config);
  4564. if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
  4565. pipe_config->pipe_bpp -= 2*3;
  4566. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  4567. pipe_config->pipe_bpp);
  4568. needs_recompute = true;
  4569. pipe_config->bw_constrained = true;
  4570. goto retry;
  4571. }
  4572. if (needs_recompute)
  4573. return RETRY;
  4574. return setup_ok ? 0 : -EINVAL;
  4575. }
  4576. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  4577. struct intel_crtc_config *pipe_config)
  4578. {
  4579. pipe_config->ips_enabled = i915.enable_ips &&
  4580. hsw_crtc_supports_ips(crtc) &&
  4581. pipe_config->pipe_bpp <= 24;
  4582. }
  4583. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  4584. struct intel_crtc_config *pipe_config)
  4585. {
  4586. struct drm_device *dev = crtc->base.dev;
  4587. struct drm_i915_private *dev_priv = dev->dev_private;
  4588. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4589. /* FIXME should check pixel clock limits on all platforms */
  4590. if (INTEL_INFO(dev)->gen < 4) {
  4591. int clock_limit =
  4592. dev_priv->display.get_display_clock_speed(dev);
  4593. /*
  4594. * Enable pixel doubling when the dot clock
  4595. * is > 90% of the (display) core speed.
  4596. *
  4597. * GDG double wide on either pipe,
  4598. * otherwise pipe A only.
  4599. */
  4600. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  4601. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  4602. clock_limit *= 2;
  4603. pipe_config->double_wide = true;
  4604. }
  4605. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  4606. return -EINVAL;
  4607. }
  4608. /*
  4609. * Pipe horizontal size must be even in:
  4610. * - DVO ganged mode
  4611. * - LVDS dual channel mode
  4612. * - Double wide pipe
  4613. */
  4614. if ((intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  4615. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  4616. pipe_config->pipe_src_w &= ~1;
  4617. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  4618. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  4619. */
  4620. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  4621. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  4622. return -EINVAL;
  4623. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
  4624. pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
  4625. } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
  4626. /* only a 8bpc pipe, with 6bpc dither through the panel fitter
  4627. * for lvds. */
  4628. pipe_config->pipe_bpp = 8*3;
  4629. }
  4630. if (HAS_IPS(dev))
  4631. hsw_compute_ips_config(crtc, pipe_config);
  4632. if (pipe_config->has_pch_encoder)
  4633. return ironlake_fdi_compute_config(crtc, pipe_config);
  4634. return 0;
  4635. }
  4636. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  4637. {
  4638. struct drm_i915_private *dev_priv = dev->dev_private;
  4639. u32 val;
  4640. int divider;
  4641. /* FIXME: Punit isn't quite ready yet */
  4642. if (IS_CHERRYVIEW(dev))
  4643. return 400000;
  4644. if (dev_priv->hpll_freq == 0)
  4645. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  4646. mutex_lock(&dev_priv->dpio_lock);
  4647. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4648. mutex_unlock(&dev_priv->dpio_lock);
  4649. divider = val & DISPLAY_FREQUENCY_VALUES;
  4650. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  4651. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4652. "cdclk change in progress\n");
  4653. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  4654. }
  4655. static int i945_get_display_clock_speed(struct drm_device *dev)
  4656. {
  4657. return 400000;
  4658. }
  4659. static int i915_get_display_clock_speed(struct drm_device *dev)
  4660. {
  4661. return 333000;
  4662. }
  4663. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  4664. {
  4665. return 200000;
  4666. }
  4667. static int pnv_get_display_clock_speed(struct drm_device *dev)
  4668. {
  4669. u16 gcfgc = 0;
  4670. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4671. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4672. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  4673. return 267000;
  4674. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  4675. return 333000;
  4676. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  4677. return 444000;
  4678. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  4679. return 200000;
  4680. default:
  4681. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  4682. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  4683. return 133000;
  4684. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  4685. return 167000;
  4686. }
  4687. }
  4688. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  4689. {
  4690. u16 gcfgc = 0;
  4691. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4692. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  4693. return 133000;
  4694. else {
  4695. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4696. case GC_DISPLAY_CLOCK_333_MHZ:
  4697. return 333000;
  4698. default:
  4699. case GC_DISPLAY_CLOCK_190_200_MHZ:
  4700. return 190000;
  4701. }
  4702. }
  4703. }
  4704. static int i865_get_display_clock_speed(struct drm_device *dev)
  4705. {
  4706. return 266000;
  4707. }
  4708. static int i855_get_display_clock_speed(struct drm_device *dev)
  4709. {
  4710. u16 hpllcc = 0;
  4711. /* Assume that the hardware is in the high speed state. This
  4712. * should be the default.
  4713. */
  4714. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  4715. case GC_CLOCK_133_200:
  4716. case GC_CLOCK_100_200:
  4717. return 200000;
  4718. case GC_CLOCK_166_250:
  4719. return 250000;
  4720. case GC_CLOCK_100_133:
  4721. return 133000;
  4722. }
  4723. /* Shouldn't happen */
  4724. return 0;
  4725. }
  4726. static int i830_get_display_clock_speed(struct drm_device *dev)
  4727. {
  4728. return 133000;
  4729. }
  4730. static void
  4731. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  4732. {
  4733. while (*num > DATA_LINK_M_N_MASK ||
  4734. *den > DATA_LINK_M_N_MASK) {
  4735. *num >>= 1;
  4736. *den >>= 1;
  4737. }
  4738. }
  4739. static void compute_m_n(unsigned int m, unsigned int n,
  4740. uint32_t *ret_m, uint32_t *ret_n)
  4741. {
  4742. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  4743. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  4744. intel_reduce_m_n_ratio(ret_m, ret_n);
  4745. }
  4746. void
  4747. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  4748. int pixel_clock, int link_clock,
  4749. struct intel_link_m_n *m_n)
  4750. {
  4751. m_n->tu = 64;
  4752. compute_m_n(bits_per_pixel * pixel_clock,
  4753. link_clock * nlanes * 8,
  4754. &m_n->gmch_m, &m_n->gmch_n);
  4755. compute_m_n(pixel_clock, link_clock,
  4756. &m_n->link_m, &m_n->link_n);
  4757. }
  4758. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  4759. {
  4760. if (i915.panel_use_ssc >= 0)
  4761. return i915.panel_use_ssc != 0;
  4762. return dev_priv->vbt.lvds_use_ssc
  4763. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  4764. }
  4765. static int i9xx_get_refclk(struct intel_crtc *crtc, int num_connectors)
  4766. {
  4767. struct drm_device *dev = crtc->base.dev;
  4768. struct drm_i915_private *dev_priv = dev->dev_private;
  4769. int refclk;
  4770. if (IS_VALLEYVIEW(dev)) {
  4771. refclk = 100000;
  4772. } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  4773. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4774. refclk = dev_priv->vbt.lvds_ssc_freq;
  4775. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  4776. } else if (!IS_GEN2(dev)) {
  4777. refclk = 96000;
  4778. } else {
  4779. refclk = 48000;
  4780. }
  4781. return refclk;
  4782. }
  4783. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  4784. {
  4785. return (1 << dpll->n) << 16 | dpll->m2;
  4786. }
  4787. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  4788. {
  4789. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  4790. }
  4791. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  4792. intel_clock_t *reduced_clock)
  4793. {
  4794. struct drm_device *dev = crtc->base.dev;
  4795. u32 fp, fp2 = 0;
  4796. if (IS_PINEVIEW(dev)) {
  4797. fp = pnv_dpll_compute_fp(&crtc->new_config->dpll);
  4798. if (reduced_clock)
  4799. fp2 = pnv_dpll_compute_fp(reduced_clock);
  4800. } else {
  4801. fp = i9xx_dpll_compute_fp(&crtc->new_config->dpll);
  4802. if (reduced_clock)
  4803. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  4804. }
  4805. crtc->new_config->dpll_hw_state.fp0 = fp;
  4806. crtc->lowfreq_avail = false;
  4807. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  4808. reduced_clock && i915.powersave) {
  4809. crtc->new_config->dpll_hw_state.fp1 = fp2;
  4810. crtc->lowfreq_avail = true;
  4811. } else {
  4812. crtc->new_config->dpll_hw_state.fp1 = fp;
  4813. }
  4814. }
  4815. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  4816. pipe)
  4817. {
  4818. u32 reg_val;
  4819. /*
  4820. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  4821. * and set it to a reasonable value instead.
  4822. */
  4823. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4824. reg_val &= 0xffffff00;
  4825. reg_val |= 0x00000030;
  4826. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4827. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4828. reg_val &= 0x8cffffff;
  4829. reg_val = 0x8c000000;
  4830. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4831. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4832. reg_val &= 0xffffff00;
  4833. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4834. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4835. reg_val &= 0x00ffffff;
  4836. reg_val |= 0xb0000000;
  4837. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4838. }
  4839. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  4840. struct intel_link_m_n *m_n)
  4841. {
  4842. struct drm_device *dev = crtc->base.dev;
  4843. struct drm_i915_private *dev_priv = dev->dev_private;
  4844. int pipe = crtc->pipe;
  4845. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4846. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  4847. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  4848. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  4849. }
  4850. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  4851. struct intel_link_m_n *m_n,
  4852. struct intel_link_m_n *m2_n2)
  4853. {
  4854. struct drm_device *dev = crtc->base.dev;
  4855. struct drm_i915_private *dev_priv = dev->dev_private;
  4856. int pipe = crtc->pipe;
  4857. enum transcoder transcoder = crtc->config.cpu_transcoder;
  4858. if (INTEL_INFO(dev)->gen >= 5) {
  4859. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4860. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  4861. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  4862. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  4863. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  4864. * for gen < 8) and if DRRS is supported (to make sure the
  4865. * registers are not unnecessarily accessed).
  4866. */
  4867. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  4868. crtc->config.has_drrs) {
  4869. I915_WRITE(PIPE_DATA_M2(transcoder),
  4870. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  4871. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  4872. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  4873. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  4874. }
  4875. } else {
  4876. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4877. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  4878. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  4879. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  4880. }
  4881. }
  4882. void intel_dp_set_m_n(struct intel_crtc *crtc)
  4883. {
  4884. if (crtc->config.has_pch_encoder)
  4885. intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
  4886. else
  4887. intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n,
  4888. &crtc->config.dp_m2_n2);
  4889. }
  4890. static void vlv_update_pll(struct intel_crtc *crtc,
  4891. struct intel_crtc_config *pipe_config)
  4892. {
  4893. u32 dpll, dpll_md;
  4894. /*
  4895. * Enable DPIO clock input. We should never disable the reference
  4896. * clock for pipe B, since VGA hotplug / manual detection depends
  4897. * on it.
  4898. */
  4899. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  4900. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  4901. /* We should never disable this, set it here for state tracking */
  4902. if (crtc->pipe == PIPE_B)
  4903. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4904. dpll |= DPLL_VCO_ENABLE;
  4905. pipe_config->dpll_hw_state.dpll = dpll;
  4906. dpll_md = (pipe_config->pixel_multiplier - 1)
  4907. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4908. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  4909. }
  4910. static void vlv_prepare_pll(struct intel_crtc *crtc,
  4911. const struct intel_crtc_config *pipe_config)
  4912. {
  4913. struct drm_device *dev = crtc->base.dev;
  4914. struct drm_i915_private *dev_priv = dev->dev_private;
  4915. int pipe = crtc->pipe;
  4916. u32 mdiv;
  4917. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  4918. u32 coreclk, reg_val;
  4919. mutex_lock(&dev_priv->dpio_lock);
  4920. bestn = pipe_config->dpll.n;
  4921. bestm1 = pipe_config->dpll.m1;
  4922. bestm2 = pipe_config->dpll.m2;
  4923. bestp1 = pipe_config->dpll.p1;
  4924. bestp2 = pipe_config->dpll.p2;
  4925. /* See eDP HDMI DPIO driver vbios notes doc */
  4926. /* PLL B needs special handling */
  4927. if (pipe == PIPE_B)
  4928. vlv_pllb_recal_opamp(dev_priv, pipe);
  4929. /* Set up Tx target for periodic Rcomp update */
  4930. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  4931. /* Disable target IRef on PLL */
  4932. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  4933. reg_val &= 0x00ffffff;
  4934. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  4935. /* Disable fast lock */
  4936. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  4937. /* Set idtafcrecal before PLL is enabled */
  4938. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  4939. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  4940. mdiv |= ((bestn << DPIO_N_SHIFT));
  4941. mdiv |= (1 << DPIO_K_SHIFT);
  4942. /*
  4943. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  4944. * but we don't support that).
  4945. * Note: don't use the DAC post divider as it seems unstable.
  4946. */
  4947. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  4948. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4949. mdiv |= DPIO_ENABLE_CALIBRATION;
  4950. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4951. /* Set HBR and RBR LPF coefficients */
  4952. if (pipe_config->port_clock == 162000 ||
  4953. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  4954. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  4955. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4956. 0x009f0003);
  4957. else
  4958. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4959. 0x00d0000f);
  4960. if (crtc->config.has_dp_encoder) {
  4961. /* Use SSC source */
  4962. if (pipe == PIPE_A)
  4963. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4964. 0x0df40000);
  4965. else
  4966. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4967. 0x0df70000);
  4968. } else { /* HDMI or VGA */
  4969. /* Use bend source */
  4970. if (pipe == PIPE_A)
  4971. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4972. 0x0df70000);
  4973. else
  4974. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4975. 0x0df40000);
  4976. }
  4977. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  4978. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  4979. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  4980. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  4981. coreclk |= 0x01000000;
  4982. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  4983. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  4984. mutex_unlock(&dev_priv->dpio_lock);
  4985. }
  4986. static void chv_update_pll(struct intel_crtc *crtc,
  4987. struct intel_crtc_config *pipe_config)
  4988. {
  4989. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  4990. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  4991. DPLL_VCO_ENABLE;
  4992. if (crtc->pipe != PIPE_A)
  4993. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4994. pipe_config->dpll_hw_state.dpll_md =
  4995. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4996. }
  4997. static void chv_prepare_pll(struct intel_crtc *crtc,
  4998. const struct intel_crtc_config *pipe_config)
  4999. {
  5000. struct drm_device *dev = crtc->base.dev;
  5001. struct drm_i915_private *dev_priv = dev->dev_private;
  5002. int pipe = crtc->pipe;
  5003. int dpll_reg = DPLL(crtc->pipe);
  5004. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5005. u32 loopfilter, intcoeff;
  5006. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5007. int refclk;
  5008. bestn = pipe_config->dpll.n;
  5009. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5010. bestm1 = pipe_config->dpll.m1;
  5011. bestm2 = pipe_config->dpll.m2 >> 22;
  5012. bestp1 = pipe_config->dpll.p1;
  5013. bestp2 = pipe_config->dpll.p2;
  5014. /*
  5015. * Enable Refclk and SSC
  5016. */
  5017. I915_WRITE(dpll_reg,
  5018. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5019. mutex_lock(&dev_priv->dpio_lock);
  5020. /* p1 and p2 divider */
  5021. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5022. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5023. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5024. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5025. 1 << DPIO_CHV_K_DIV_SHIFT);
  5026. /* Feedback post-divider - m2 */
  5027. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5028. /* Feedback refclk divider - n and m1 */
  5029. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5030. DPIO_CHV_M1_DIV_BY_2 |
  5031. 1 << DPIO_CHV_N_DIV_SHIFT);
  5032. /* M2 fraction division */
  5033. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5034. /* M2 fraction division enable */
  5035. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
  5036. DPIO_CHV_FRAC_DIV_EN |
  5037. (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
  5038. /* Loop filter */
  5039. refclk = i9xx_get_refclk(crtc, 0);
  5040. loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
  5041. 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
  5042. if (refclk == 100000)
  5043. intcoeff = 11;
  5044. else if (refclk == 38400)
  5045. intcoeff = 10;
  5046. else
  5047. intcoeff = 9;
  5048. loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
  5049. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5050. /* AFC Recal */
  5051. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5052. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5053. DPIO_AFC_RECAL);
  5054. mutex_unlock(&dev_priv->dpio_lock);
  5055. }
  5056. /**
  5057. * vlv_force_pll_on - forcibly enable just the PLL
  5058. * @dev_priv: i915 private structure
  5059. * @pipe: pipe PLL to enable
  5060. * @dpll: PLL configuration
  5061. *
  5062. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5063. * in cases where we need the PLL enabled even when @pipe is not going to
  5064. * be enabled.
  5065. */
  5066. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  5067. const struct dpll *dpll)
  5068. {
  5069. struct intel_crtc *crtc =
  5070. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  5071. struct intel_crtc_config pipe_config = {
  5072. .pixel_multiplier = 1,
  5073. .dpll = *dpll,
  5074. };
  5075. if (IS_CHERRYVIEW(dev)) {
  5076. chv_update_pll(crtc, &pipe_config);
  5077. chv_prepare_pll(crtc, &pipe_config);
  5078. chv_enable_pll(crtc, &pipe_config);
  5079. } else {
  5080. vlv_update_pll(crtc, &pipe_config);
  5081. vlv_prepare_pll(crtc, &pipe_config);
  5082. vlv_enable_pll(crtc, &pipe_config);
  5083. }
  5084. }
  5085. /**
  5086. * vlv_force_pll_off - forcibly disable just the PLL
  5087. * @dev_priv: i915 private structure
  5088. * @pipe: pipe PLL to disable
  5089. *
  5090. * Disable the PLL for @pipe. To be used in cases where we need
  5091. * the PLL enabled even when @pipe is not going to be enabled.
  5092. */
  5093. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  5094. {
  5095. if (IS_CHERRYVIEW(dev))
  5096. chv_disable_pll(to_i915(dev), pipe);
  5097. else
  5098. vlv_disable_pll(to_i915(dev), pipe);
  5099. }
  5100. static void i9xx_update_pll(struct intel_crtc *crtc,
  5101. intel_clock_t *reduced_clock,
  5102. int num_connectors)
  5103. {
  5104. struct drm_device *dev = crtc->base.dev;
  5105. struct drm_i915_private *dev_priv = dev->dev_private;
  5106. u32 dpll;
  5107. bool is_sdvo;
  5108. struct dpll *clock = &crtc->new_config->dpll;
  5109. i9xx_update_pll_dividers(crtc, reduced_clock);
  5110. is_sdvo = intel_pipe_will_have_type(crtc, INTEL_OUTPUT_SDVO) ||
  5111. intel_pipe_will_have_type(crtc, INTEL_OUTPUT_HDMI);
  5112. dpll = DPLL_VGA_MODE_DIS;
  5113. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  5114. dpll |= DPLLB_MODE_LVDS;
  5115. else
  5116. dpll |= DPLLB_MODE_DAC_SERIAL;
  5117. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5118. dpll |= (crtc->new_config->pixel_multiplier - 1)
  5119. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5120. }
  5121. if (is_sdvo)
  5122. dpll |= DPLL_SDVO_HIGH_SPEED;
  5123. if (crtc->new_config->has_dp_encoder)
  5124. dpll |= DPLL_SDVO_HIGH_SPEED;
  5125. /* compute bitmask from p1 value */
  5126. if (IS_PINEVIEW(dev))
  5127. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5128. else {
  5129. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5130. if (IS_G4X(dev) && reduced_clock)
  5131. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5132. }
  5133. switch (clock->p2) {
  5134. case 5:
  5135. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5136. break;
  5137. case 7:
  5138. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5139. break;
  5140. case 10:
  5141. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5142. break;
  5143. case 14:
  5144. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5145. break;
  5146. }
  5147. if (INTEL_INFO(dev)->gen >= 4)
  5148. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5149. if (crtc->new_config->sdvo_tv_clock)
  5150. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5151. else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  5152. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5153. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5154. else
  5155. dpll |= PLL_REF_INPUT_DREFCLK;
  5156. dpll |= DPLL_VCO_ENABLE;
  5157. crtc->new_config->dpll_hw_state.dpll = dpll;
  5158. if (INTEL_INFO(dev)->gen >= 4) {
  5159. u32 dpll_md = (crtc->new_config->pixel_multiplier - 1)
  5160. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5161. crtc->new_config->dpll_hw_state.dpll_md = dpll_md;
  5162. }
  5163. }
  5164. static void i8xx_update_pll(struct intel_crtc *crtc,
  5165. intel_clock_t *reduced_clock,
  5166. int num_connectors)
  5167. {
  5168. struct drm_device *dev = crtc->base.dev;
  5169. struct drm_i915_private *dev_priv = dev->dev_private;
  5170. u32 dpll;
  5171. struct dpll *clock = &crtc->new_config->dpll;
  5172. i9xx_update_pll_dividers(crtc, reduced_clock);
  5173. dpll = DPLL_VGA_MODE_DIS;
  5174. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  5175. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5176. } else {
  5177. if (clock->p1 == 2)
  5178. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5179. else
  5180. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5181. if (clock->p2 == 4)
  5182. dpll |= PLL_P2_DIVIDE_BY_4;
  5183. }
  5184. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc, INTEL_OUTPUT_DVO))
  5185. dpll |= DPLL_DVO_2X_MODE;
  5186. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  5187. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5188. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5189. else
  5190. dpll |= PLL_REF_INPUT_DREFCLK;
  5191. dpll |= DPLL_VCO_ENABLE;
  5192. crtc->new_config->dpll_hw_state.dpll = dpll;
  5193. }
  5194. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5195. {
  5196. struct drm_device *dev = intel_crtc->base.dev;
  5197. struct drm_i915_private *dev_priv = dev->dev_private;
  5198. enum pipe pipe = intel_crtc->pipe;
  5199. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  5200. struct drm_display_mode *adjusted_mode =
  5201. &intel_crtc->config.adjusted_mode;
  5202. uint32_t crtc_vtotal, crtc_vblank_end;
  5203. int vsyncshift = 0;
  5204. /* We need to be careful not to changed the adjusted mode, for otherwise
  5205. * the hw state checker will get angry at the mismatch. */
  5206. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5207. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5208. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5209. /* the chip adds 2 halflines automatically */
  5210. crtc_vtotal -= 1;
  5211. crtc_vblank_end -= 1;
  5212. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  5213. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5214. else
  5215. vsyncshift = adjusted_mode->crtc_hsync_start -
  5216. adjusted_mode->crtc_htotal / 2;
  5217. if (vsyncshift < 0)
  5218. vsyncshift += adjusted_mode->crtc_htotal;
  5219. }
  5220. if (INTEL_INFO(dev)->gen > 3)
  5221. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5222. I915_WRITE(HTOTAL(cpu_transcoder),
  5223. (adjusted_mode->crtc_hdisplay - 1) |
  5224. ((adjusted_mode->crtc_htotal - 1) << 16));
  5225. I915_WRITE(HBLANK(cpu_transcoder),
  5226. (adjusted_mode->crtc_hblank_start - 1) |
  5227. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5228. I915_WRITE(HSYNC(cpu_transcoder),
  5229. (adjusted_mode->crtc_hsync_start - 1) |
  5230. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5231. I915_WRITE(VTOTAL(cpu_transcoder),
  5232. (adjusted_mode->crtc_vdisplay - 1) |
  5233. ((crtc_vtotal - 1) << 16));
  5234. I915_WRITE(VBLANK(cpu_transcoder),
  5235. (adjusted_mode->crtc_vblank_start - 1) |
  5236. ((crtc_vblank_end - 1) << 16));
  5237. I915_WRITE(VSYNC(cpu_transcoder),
  5238. (adjusted_mode->crtc_vsync_start - 1) |
  5239. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5240. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5241. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5242. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5243. * bits. */
  5244. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  5245. (pipe == PIPE_B || pipe == PIPE_C))
  5246. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5247. /* pipesrc controls the size that is scaled from, which should
  5248. * always be the user's requested size.
  5249. */
  5250. I915_WRITE(PIPESRC(pipe),
  5251. ((intel_crtc->config.pipe_src_w - 1) << 16) |
  5252. (intel_crtc->config.pipe_src_h - 1));
  5253. }
  5254. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5255. struct intel_crtc_config *pipe_config)
  5256. {
  5257. struct drm_device *dev = crtc->base.dev;
  5258. struct drm_i915_private *dev_priv = dev->dev_private;
  5259. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5260. uint32_t tmp;
  5261. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5262. pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5263. pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5264. tmp = I915_READ(HBLANK(cpu_transcoder));
  5265. pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5266. pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5267. tmp = I915_READ(HSYNC(cpu_transcoder));
  5268. pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5269. pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5270. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5271. pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5272. pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  5273. tmp = I915_READ(VBLANK(cpu_transcoder));
  5274. pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  5275. pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  5276. tmp = I915_READ(VSYNC(cpu_transcoder));
  5277. pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  5278. pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  5279. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  5280. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  5281. pipe_config->adjusted_mode.crtc_vtotal += 1;
  5282. pipe_config->adjusted_mode.crtc_vblank_end += 1;
  5283. }
  5284. tmp = I915_READ(PIPESRC(crtc->pipe));
  5285. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  5286. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  5287. pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
  5288. pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
  5289. }
  5290. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  5291. struct intel_crtc_config *pipe_config)
  5292. {
  5293. mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
  5294. mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
  5295. mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
  5296. mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
  5297. mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
  5298. mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
  5299. mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
  5300. mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
  5301. mode->flags = pipe_config->adjusted_mode.flags;
  5302. mode->clock = pipe_config->adjusted_mode.crtc_clock;
  5303. mode->flags |= pipe_config->adjusted_mode.flags;
  5304. }
  5305. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  5306. {
  5307. struct drm_device *dev = intel_crtc->base.dev;
  5308. struct drm_i915_private *dev_priv = dev->dev_private;
  5309. uint32_t pipeconf;
  5310. pipeconf = 0;
  5311. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  5312. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  5313. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  5314. if (intel_crtc->config.double_wide)
  5315. pipeconf |= PIPECONF_DOUBLE_WIDE;
  5316. /* only g4x and later have fancy bpc/dither controls */
  5317. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5318. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  5319. if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
  5320. pipeconf |= PIPECONF_DITHER_EN |
  5321. PIPECONF_DITHER_TYPE_SP;
  5322. switch (intel_crtc->config.pipe_bpp) {
  5323. case 18:
  5324. pipeconf |= PIPECONF_6BPC;
  5325. break;
  5326. case 24:
  5327. pipeconf |= PIPECONF_8BPC;
  5328. break;
  5329. case 30:
  5330. pipeconf |= PIPECONF_10BPC;
  5331. break;
  5332. default:
  5333. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5334. BUG();
  5335. }
  5336. }
  5337. if (HAS_PIPE_CXSR(dev)) {
  5338. if (intel_crtc->lowfreq_avail) {
  5339. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  5340. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  5341. } else {
  5342. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  5343. }
  5344. }
  5345. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  5346. if (INTEL_INFO(dev)->gen < 4 ||
  5347. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  5348. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  5349. else
  5350. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  5351. } else
  5352. pipeconf |= PIPECONF_PROGRESSIVE;
  5353. if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
  5354. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  5355. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  5356. POSTING_READ(PIPECONF(intel_crtc->pipe));
  5357. }
  5358. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc)
  5359. {
  5360. struct drm_device *dev = crtc->base.dev;
  5361. struct drm_i915_private *dev_priv = dev->dev_private;
  5362. int refclk, num_connectors = 0;
  5363. intel_clock_t clock, reduced_clock;
  5364. bool ok, has_reduced_clock = false;
  5365. bool is_lvds = false, is_dsi = false;
  5366. struct intel_encoder *encoder;
  5367. const intel_limit_t *limit;
  5368. for_each_intel_encoder(dev, encoder) {
  5369. if (encoder->new_crtc != crtc)
  5370. continue;
  5371. switch (encoder->type) {
  5372. case INTEL_OUTPUT_LVDS:
  5373. is_lvds = true;
  5374. break;
  5375. case INTEL_OUTPUT_DSI:
  5376. is_dsi = true;
  5377. break;
  5378. default:
  5379. break;
  5380. }
  5381. num_connectors++;
  5382. }
  5383. if (is_dsi)
  5384. return 0;
  5385. if (!crtc->new_config->clock_set) {
  5386. refclk = i9xx_get_refclk(crtc, num_connectors);
  5387. /*
  5388. * Returns a set of divisors for the desired target clock with
  5389. * the given refclk, or FALSE. The returned values represent
  5390. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  5391. * 2) / p1 / p2.
  5392. */
  5393. limit = intel_limit(crtc, refclk);
  5394. ok = dev_priv->display.find_dpll(limit, crtc,
  5395. crtc->new_config->port_clock,
  5396. refclk, NULL, &clock);
  5397. if (!ok) {
  5398. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5399. return -EINVAL;
  5400. }
  5401. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5402. /*
  5403. * Ensure we match the reduced clock's P to the target
  5404. * clock. If the clocks don't match, we can't switch
  5405. * the display clock by using the FP0/FP1. In such case
  5406. * we will disable the LVDS downclock feature.
  5407. */
  5408. has_reduced_clock =
  5409. dev_priv->display.find_dpll(limit, crtc,
  5410. dev_priv->lvds_downclock,
  5411. refclk, &clock,
  5412. &reduced_clock);
  5413. }
  5414. /* Compat-code for transition, will disappear. */
  5415. crtc->new_config->dpll.n = clock.n;
  5416. crtc->new_config->dpll.m1 = clock.m1;
  5417. crtc->new_config->dpll.m2 = clock.m2;
  5418. crtc->new_config->dpll.p1 = clock.p1;
  5419. crtc->new_config->dpll.p2 = clock.p2;
  5420. }
  5421. if (IS_GEN2(dev)) {
  5422. i8xx_update_pll(crtc,
  5423. has_reduced_clock ? &reduced_clock : NULL,
  5424. num_connectors);
  5425. } else if (IS_CHERRYVIEW(dev)) {
  5426. chv_update_pll(crtc, crtc->new_config);
  5427. } else if (IS_VALLEYVIEW(dev)) {
  5428. vlv_update_pll(crtc, crtc->new_config);
  5429. } else {
  5430. i9xx_update_pll(crtc,
  5431. has_reduced_clock ? &reduced_clock : NULL,
  5432. num_connectors);
  5433. }
  5434. return 0;
  5435. }
  5436. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  5437. struct intel_crtc_config *pipe_config)
  5438. {
  5439. struct drm_device *dev = crtc->base.dev;
  5440. struct drm_i915_private *dev_priv = dev->dev_private;
  5441. uint32_t tmp;
  5442. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  5443. return;
  5444. tmp = I915_READ(PFIT_CONTROL);
  5445. if (!(tmp & PFIT_ENABLE))
  5446. return;
  5447. /* Check whether the pfit is attached to our pipe. */
  5448. if (INTEL_INFO(dev)->gen < 4) {
  5449. if (crtc->pipe != PIPE_B)
  5450. return;
  5451. } else {
  5452. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  5453. return;
  5454. }
  5455. pipe_config->gmch_pfit.control = tmp;
  5456. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  5457. if (INTEL_INFO(dev)->gen < 5)
  5458. pipe_config->gmch_pfit.lvds_border_bits =
  5459. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  5460. }
  5461. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  5462. struct intel_crtc_config *pipe_config)
  5463. {
  5464. struct drm_device *dev = crtc->base.dev;
  5465. struct drm_i915_private *dev_priv = dev->dev_private;
  5466. int pipe = pipe_config->cpu_transcoder;
  5467. intel_clock_t clock;
  5468. u32 mdiv;
  5469. int refclk = 100000;
  5470. /* In case of MIPI DPLL will not even be used */
  5471. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  5472. return;
  5473. mutex_lock(&dev_priv->dpio_lock);
  5474. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  5475. mutex_unlock(&dev_priv->dpio_lock);
  5476. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  5477. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  5478. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  5479. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  5480. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  5481. vlv_clock(refclk, &clock);
  5482. /* clock.dot is the fast clock */
  5483. pipe_config->port_clock = clock.dot / 5;
  5484. }
  5485. static void i9xx_get_plane_config(struct intel_crtc *crtc,
  5486. struct intel_plane_config *plane_config)
  5487. {
  5488. struct drm_device *dev = crtc->base.dev;
  5489. struct drm_i915_private *dev_priv = dev->dev_private;
  5490. u32 val, base, offset;
  5491. int pipe = crtc->pipe, plane = crtc->plane;
  5492. int fourcc, pixel_format;
  5493. int aligned_height;
  5494. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  5495. if (!crtc->base.primary->fb) {
  5496. DRM_DEBUG_KMS("failed to alloc fb\n");
  5497. return;
  5498. }
  5499. val = I915_READ(DSPCNTR(plane));
  5500. if (INTEL_INFO(dev)->gen >= 4)
  5501. if (val & DISPPLANE_TILED)
  5502. plane_config->tiled = true;
  5503. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  5504. fourcc = intel_format_to_fourcc(pixel_format);
  5505. crtc->base.primary->fb->pixel_format = fourcc;
  5506. crtc->base.primary->fb->bits_per_pixel =
  5507. drm_format_plane_cpp(fourcc, 0) * 8;
  5508. if (INTEL_INFO(dev)->gen >= 4) {
  5509. if (plane_config->tiled)
  5510. offset = I915_READ(DSPTILEOFF(plane));
  5511. else
  5512. offset = I915_READ(DSPLINOFF(plane));
  5513. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  5514. } else {
  5515. base = I915_READ(DSPADDR(plane));
  5516. }
  5517. plane_config->base = base;
  5518. val = I915_READ(PIPESRC(pipe));
  5519. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  5520. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  5521. val = I915_READ(DSPSTRIDE(pipe));
  5522. crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
  5523. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  5524. plane_config->tiled);
  5525. plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
  5526. aligned_height);
  5527. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  5528. pipe, plane, crtc->base.primary->fb->width,
  5529. crtc->base.primary->fb->height,
  5530. crtc->base.primary->fb->bits_per_pixel, base,
  5531. crtc->base.primary->fb->pitches[0],
  5532. plane_config->size);
  5533. }
  5534. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  5535. struct intel_crtc_config *pipe_config)
  5536. {
  5537. struct drm_device *dev = crtc->base.dev;
  5538. struct drm_i915_private *dev_priv = dev->dev_private;
  5539. int pipe = pipe_config->cpu_transcoder;
  5540. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5541. intel_clock_t clock;
  5542. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  5543. int refclk = 100000;
  5544. mutex_lock(&dev_priv->dpio_lock);
  5545. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  5546. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  5547. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  5548. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  5549. mutex_unlock(&dev_priv->dpio_lock);
  5550. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  5551. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  5552. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  5553. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  5554. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  5555. chv_clock(refclk, &clock);
  5556. /* clock.dot is the fast clock */
  5557. pipe_config->port_clock = clock.dot / 5;
  5558. }
  5559. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  5560. struct intel_crtc_config *pipe_config)
  5561. {
  5562. struct drm_device *dev = crtc->base.dev;
  5563. struct drm_i915_private *dev_priv = dev->dev_private;
  5564. uint32_t tmp;
  5565. if (!intel_display_power_is_enabled(dev_priv,
  5566. POWER_DOMAIN_PIPE(crtc->pipe)))
  5567. return false;
  5568. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5569. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5570. tmp = I915_READ(PIPECONF(crtc->pipe));
  5571. if (!(tmp & PIPECONF_ENABLE))
  5572. return false;
  5573. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5574. switch (tmp & PIPECONF_BPC_MASK) {
  5575. case PIPECONF_6BPC:
  5576. pipe_config->pipe_bpp = 18;
  5577. break;
  5578. case PIPECONF_8BPC:
  5579. pipe_config->pipe_bpp = 24;
  5580. break;
  5581. case PIPECONF_10BPC:
  5582. pipe_config->pipe_bpp = 30;
  5583. break;
  5584. default:
  5585. break;
  5586. }
  5587. }
  5588. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  5589. pipe_config->limited_color_range = true;
  5590. if (INTEL_INFO(dev)->gen < 4)
  5591. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  5592. intel_get_pipe_timings(crtc, pipe_config);
  5593. i9xx_get_pfit_config(crtc, pipe_config);
  5594. if (INTEL_INFO(dev)->gen >= 4) {
  5595. tmp = I915_READ(DPLL_MD(crtc->pipe));
  5596. pipe_config->pixel_multiplier =
  5597. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  5598. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  5599. pipe_config->dpll_hw_state.dpll_md = tmp;
  5600. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5601. tmp = I915_READ(DPLL(crtc->pipe));
  5602. pipe_config->pixel_multiplier =
  5603. ((tmp & SDVO_MULTIPLIER_MASK)
  5604. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  5605. } else {
  5606. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  5607. * port and will be fixed up in the encoder->get_config
  5608. * function. */
  5609. pipe_config->pixel_multiplier = 1;
  5610. }
  5611. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  5612. if (!IS_VALLEYVIEW(dev)) {
  5613. /*
  5614. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  5615. * on 830. Filter it out here so that we don't
  5616. * report errors due to that.
  5617. */
  5618. if (IS_I830(dev))
  5619. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  5620. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  5621. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  5622. } else {
  5623. /* Mask out read-only status bits. */
  5624. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  5625. DPLL_PORTC_READY_MASK |
  5626. DPLL_PORTB_READY_MASK);
  5627. }
  5628. if (IS_CHERRYVIEW(dev))
  5629. chv_crtc_clock_get(crtc, pipe_config);
  5630. else if (IS_VALLEYVIEW(dev))
  5631. vlv_crtc_clock_get(crtc, pipe_config);
  5632. else
  5633. i9xx_crtc_clock_get(crtc, pipe_config);
  5634. return true;
  5635. }
  5636. static void ironlake_init_pch_refclk(struct drm_device *dev)
  5637. {
  5638. struct drm_i915_private *dev_priv = dev->dev_private;
  5639. struct intel_encoder *encoder;
  5640. u32 val, final;
  5641. bool has_lvds = false;
  5642. bool has_cpu_edp = false;
  5643. bool has_panel = false;
  5644. bool has_ck505 = false;
  5645. bool can_ssc = false;
  5646. /* We need to take the global config into account */
  5647. for_each_intel_encoder(dev, encoder) {
  5648. switch (encoder->type) {
  5649. case INTEL_OUTPUT_LVDS:
  5650. has_panel = true;
  5651. has_lvds = true;
  5652. break;
  5653. case INTEL_OUTPUT_EDP:
  5654. has_panel = true;
  5655. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  5656. has_cpu_edp = true;
  5657. break;
  5658. default:
  5659. break;
  5660. }
  5661. }
  5662. if (HAS_PCH_IBX(dev)) {
  5663. has_ck505 = dev_priv->vbt.display_clock_mode;
  5664. can_ssc = has_ck505;
  5665. } else {
  5666. has_ck505 = false;
  5667. can_ssc = true;
  5668. }
  5669. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  5670. has_panel, has_lvds, has_ck505);
  5671. /* Ironlake: try to setup display ref clock before DPLL
  5672. * enabling. This is only under driver's control after
  5673. * PCH B stepping, previous chipset stepping should be
  5674. * ignoring this setting.
  5675. */
  5676. val = I915_READ(PCH_DREF_CONTROL);
  5677. /* As we must carefully and slowly disable/enable each source in turn,
  5678. * compute the final state we want first and check if we need to
  5679. * make any changes at all.
  5680. */
  5681. final = val;
  5682. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  5683. if (has_ck505)
  5684. final |= DREF_NONSPREAD_CK505_ENABLE;
  5685. else
  5686. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  5687. final &= ~DREF_SSC_SOURCE_MASK;
  5688. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5689. final &= ~DREF_SSC1_ENABLE;
  5690. if (has_panel) {
  5691. final |= DREF_SSC_SOURCE_ENABLE;
  5692. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5693. final |= DREF_SSC1_ENABLE;
  5694. if (has_cpu_edp) {
  5695. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5696. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5697. else
  5698. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5699. } else
  5700. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5701. } else {
  5702. final |= DREF_SSC_SOURCE_DISABLE;
  5703. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5704. }
  5705. if (final == val)
  5706. return;
  5707. /* Always enable nonspread source */
  5708. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  5709. if (has_ck505)
  5710. val |= DREF_NONSPREAD_CK505_ENABLE;
  5711. else
  5712. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  5713. if (has_panel) {
  5714. val &= ~DREF_SSC_SOURCE_MASK;
  5715. val |= DREF_SSC_SOURCE_ENABLE;
  5716. /* SSC must be turned on before enabling the CPU output */
  5717. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5718. DRM_DEBUG_KMS("Using SSC on panel\n");
  5719. val |= DREF_SSC1_ENABLE;
  5720. } else
  5721. val &= ~DREF_SSC1_ENABLE;
  5722. /* Get SSC going before enabling the outputs */
  5723. I915_WRITE(PCH_DREF_CONTROL, val);
  5724. POSTING_READ(PCH_DREF_CONTROL);
  5725. udelay(200);
  5726. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5727. /* Enable CPU source on CPU attached eDP */
  5728. if (has_cpu_edp) {
  5729. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5730. DRM_DEBUG_KMS("Using SSC on eDP\n");
  5731. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5732. } else
  5733. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5734. } else
  5735. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5736. I915_WRITE(PCH_DREF_CONTROL, val);
  5737. POSTING_READ(PCH_DREF_CONTROL);
  5738. udelay(200);
  5739. } else {
  5740. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  5741. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5742. /* Turn off CPU output */
  5743. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5744. I915_WRITE(PCH_DREF_CONTROL, val);
  5745. POSTING_READ(PCH_DREF_CONTROL);
  5746. udelay(200);
  5747. /* Turn off the SSC source */
  5748. val &= ~DREF_SSC_SOURCE_MASK;
  5749. val |= DREF_SSC_SOURCE_DISABLE;
  5750. /* Turn off SSC1 */
  5751. val &= ~DREF_SSC1_ENABLE;
  5752. I915_WRITE(PCH_DREF_CONTROL, val);
  5753. POSTING_READ(PCH_DREF_CONTROL);
  5754. udelay(200);
  5755. }
  5756. BUG_ON(val != final);
  5757. }
  5758. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  5759. {
  5760. uint32_t tmp;
  5761. tmp = I915_READ(SOUTH_CHICKEN2);
  5762. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  5763. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5764. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  5765. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  5766. DRM_ERROR("FDI mPHY reset assert timeout\n");
  5767. tmp = I915_READ(SOUTH_CHICKEN2);
  5768. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  5769. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5770. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  5771. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  5772. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  5773. }
  5774. /* WaMPhyProgramming:hsw */
  5775. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  5776. {
  5777. uint32_t tmp;
  5778. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  5779. tmp &= ~(0xFF << 24);
  5780. tmp |= (0x12 << 24);
  5781. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  5782. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  5783. tmp |= (1 << 11);
  5784. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  5785. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  5786. tmp |= (1 << 11);
  5787. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  5788. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  5789. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5790. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  5791. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  5792. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5793. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  5794. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  5795. tmp &= ~(7 << 13);
  5796. tmp |= (5 << 13);
  5797. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  5798. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  5799. tmp &= ~(7 << 13);
  5800. tmp |= (5 << 13);
  5801. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  5802. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  5803. tmp &= ~0xFF;
  5804. tmp |= 0x1C;
  5805. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  5806. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  5807. tmp &= ~0xFF;
  5808. tmp |= 0x1C;
  5809. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  5810. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  5811. tmp &= ~(0xFF << 16);
  5812. tmp |= (0x1C << 16);
  5813. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  5814. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  5815. tmp &= ~(0xFF << 16);
  5816. tmp |= (0x1C << 16);
  5817. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  5818. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  5819. tmp |= (1 << 27);
  5820. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  5821. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  5822. tmp |= (1 << 27);
  5823. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  5824. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  5825. tmp &= ~(0xF << 28);
  5826. tmp |= (4 << 28);
  5827. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  5828. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  5829. tmp &= ~(0xF << 28);
  5830. tmp |= (4 << 28);
  5831. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  5832. }
  5833. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  5834. * Programming" based on the parameters passed:
  5835. * - Sequence to enable CLKOUT_DP
  5836. * - Sequence to enable CLKOUT_DP without spread
  5837. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  5838. */
  5839. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  5840. bool with_fdi)
  5841. {
  5842. struct drm_i915_private *dev_priv = dev->dev_private;
  5843. uint32_t reg, tmp;
  5844. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  5845. with_spread = true;
  5846. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  5847. with_fdi, "LP PCH doesn't have FDI\n"))
  5848. with_fdi = false;
  5849. mutex_lock(&dev_priv->dpio_lock);
  5850. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5851. tmp &= ~SBI_SSCCTL_DISABLE;
  5852. tmp |= SBI_SSCCTL_PATHALT;
  5853. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5854. udelay(24);
  5855. if (with_spread) {
  5856. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5857. tmp &= ~SBI_SSCCTL_PATHALT;
  5858. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5859. if (with_fdi) {
  5860. lpt_reset_fdi_mphy(dev_priv);
  5861. lpt_program_fdi_mphy(dev_priv);
  5862. }
  5863. }
  5864. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5865. SBI_GEN0 : SBI_DBUFF0;
  5866. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5867. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5868. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5869. mutex_unlock(&dev_priv->dpio_lock);
  5870. }
  5871. /* Sequence to disable CLKOUT_DP */
  5872. static void lpt_disable_clkout_dp(struct drm_device *dev)
  5873. {
  5874. struct drm_i915_private *dev_priv = dev->dev_private;
  5875. uint32_t reg, tmp;
  5876. mutex_lock(&dev_priv->dpio_lock);
  5877. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5878. SBI_GEN0 : SBI_DBUFF0;
  5879. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5880. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5881. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5882. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5883. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  5884. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  5885. tmp |= SBI_SSCCTL_PATHALT;
  5886. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5887. udelay(32);
  5888. }
  5889. tmp |= SBI_SSCCTL_DISABLE;
  5890. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5891. }
  5892. mutex_unlock(&dev_priv->dpio_lock);
  5893. }
  5894. static void lpt_init_pch_refclk(struct drm_device *dev)
  5895. {
  5896. struct intel_encoder *encoder;
  5897. bool has_vga = false;
  5898. for_each_intel_encoder(dev, encoder) {
  5899. switch (encoder->type) {
  5900. case INTEL_OUTPUT_ANALOG:
  5901. has_vga = true;
  5902. break;
  5903. default:
  5904. break;
  5905. }
  5906. }
  5907. if (has_vga)
  5908. lpt_enable_clkout_dp(dev, true, true);
  5909. else
  5910. lpt_disable_clkout_dp(dev);
  5911. }
  5912. /*
  5913. * Initialize reference clocks when the driver loads
  5914. */
  5915. void intel_init_pch_refclk(struct drm_device *dev)
  5916. {
  5917. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  5918. ironlake_init_pch_refclk(dev);
  5919. else if (HAS_PCH_LPT(dev))
  5920. lpt_init_pch_refclk(dev);
  5921. }
  5922. static int ironlake_get_refclk(struct drm_crtc *crtc)
  5923. {
  5924. struct drm_device *dev = crtc->dev;
  5925. struct drm_i915_private *dev_priv = dev->dev_private;
  5926. struct intel_encoder *encoder;
  5927. int num_connectors = 0;
  5928. bool is_lvds = false;
  5929. for_each_intel_encoder(dev, encoder) {
  5930. if (encoder->new_crtc != to_intel_crtc(crtc))
  5931. continue;
  5932. switch (encoder->type) {
  5933. case INTEL_OUTPUT_LVDS:
  5934. is_lvds = true;
  5935. break;
  5936. default:
  5937. break;
  5938. }
  5939. num_connectors++;
  5940. }
  5941. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5942. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  5943. dev_priv->vbt.lvds_ssc_freq);
  5944. return dev_priv->vbt.lvds_ssc_freq;
  5945. }
  5946. return 120000;
  5947. }
  5948. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  5949. {
  5950. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  5951. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5952. int pipe = intel_crtc->pipe;
  5953. uint32_t val;
  5954. val = 0;
  5955. switch (intel_crtc->config.pipe_bpp) {
  5956. case 18:
  5957. val |= PIPECONF_6BPC;
  5958. break;
  5959. case 24:
  5960. val |= PIPECONF_8BPC;
  5961. break;
  5962. case 30:
  5963. val |= PIPECONF_10BPC;
  5964. break;
  5965. case 36:
  5966. val |= PIPECONF_12BPC;
  5967. break;
  5968. default:
  5969. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5970. BUG();
  5971. }
  5972. if (intel_crtc->config.dither)
  5973. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  5974. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  5975. val |= PIPECONF_INTERLACED_ILK;
  5976. else
  5977. val |= PIPECONF_PROGRESSIVE;
  5978. if (intel_crtc->config.limited_color_range)
  5979. val |= PIPECONF_COLOR_RANGE_SELECT;
  5980. I915_WRITE(PIPECONF(pipe), val);
  5981. POSTING_READ(PIPECONF(pipe));
  5982. }
  5983. /*
  5984. * Set up the pipe CSC unit.
  5985. *
  5986. * Currently only full range RGB to limited range RGB conversion
  5987. * is supported, but eventually this should handle various
  5988. * RGB<->YCbCr scenarios as well.
  5989. */
  5990. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  5991. {
  5992. struct drm_device *dev = crtc->dev;
  5993. struct drm_i915_private *dev_priv = dev->dev_private;
  5994. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5995. int pipe = intel_crtc->pipe;
  5996. uint16_t coeff = 0x7800; /* 1.0 */
  5997. /*
  5998. * TODO: Check what kind of values actually come out of the pipe
  5999. * with these coeff/postoff values and adjust to get the best
  6000. * accuracy. Perhaps we even need to take the bpc value into
  6001. * consideration.
  6002. */
  6003. if (intel_crtc->config.limited_color_range)
  6004. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  6005. /*
  6006. * GY/GU and RY/RU should be the other way around according
  6007. * to BSpec, but reality doesn't agree. Just set them up in
  6008. * a way that results in the correct picture.
  6009. */
  6010. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  6011. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  6012. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  6013. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  6014. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  6015. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  6016. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  6017. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  6018. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  6019. if (INTEL_INFO(dev)->gen > 6) {
  6020. uint16_t postoff = 0;
  6021. if (intel_crtc->config.limited_color_range)
  6022. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  6023. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  6024. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  6025. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  6026. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  6027. } else {
  6028. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  6029. if (intel_crtc->config.limited_color_range)
  6030. mode |= CSC_BLACK_SCREEN_OFFSET;
  6031. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  6032. }
  6033. }
  6034. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6035. {
  6036. struct drm_device *dev = crtc->dev;
  6037. struct drm_i915_private *dev_priv = dev->dev_private;
  6038. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6039. enum pipe pipe = intel_crtc->pipe;
  6040. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  6041. uint32_t val;
  6042. val = 0;
  6043. if (IS_HASWELL(dev) && intel_crtc->config.dither)
  6044. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6045. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6046. val |= PIPECONF_INTERLACED_ILK;
  6047. else
  6048. val |= PIPECONF_PROGRESSIVE;
  6049. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6050. POSTING_READ(PIPECONF(cpu_transcoder));
  6051. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  6052. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  6053. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  6054. val = 0;
  6055. switch (intel_crtc->config.pipe_bpp) {
  6056. case 18:
  6057. val |= PIPEMISC_DITHER_6_BPC;
  6058. break;
  6059. case 24:
  6060. val |= PIPEMISC_DITHER_8_BPC;
  6061. break;
  6062. case 30:
  6063. val |= PIPEMISC_DITHER_10_BPC;
  6064. break;
  6065. case 36:
  6066. val |= PIPEMISC_DITHER_12_BPC;
  6067. break;
  6068. default:
  6069. /* Case prevented by pipe_config_set_bpp. */
  6070. BUG();
  6071. }
  6072. if (intel_crtc->config.dither)
  6073. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  6074. I915_WRITE(PIPEMISC(pipe), val);
  6075. }
  6076. }
  6077. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  6078. intel_clock_t *clock,
  6079. bool *has_reduced_clock,
  6080. intel_clock_t *reduced_clock)
  6081. {
  6082. struct drm_device *dev = crtc->dev;
  6083. struct drm_i915_private *dev_priv = dev->dev_private;
  6084. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6085. int refclk;
  6086. const intel_limit_t *limit;
  6087. bool ret, is_lvds = false;
  6088. is_lvds = intel_pipe_will_have_type(intel_crtc, INTEL_OUTPUT_LVDS);
  6089. refclk = ironlake_get_refclk(crtc);
  6090. /*
  6091. * Returns a set of divisors for the desired target clock with the given
  6092. * refclk, or FALSE. The returned values represent the clock equation:
  6093. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  6094. */
  6095. limit = intel_limit(intel_crtc, refclk);
  6096. ret = dev_priv->display.find_dpll(limit, intel_crtc,
  6097. intel_crtc->new_config->port_clock,
  6098. refclk, NULL, clock);
  6099. if (!ret)
  6100. return false;
  6101. if (is_lvds && dev_priv->lvds_downclock_avail) {
  6102. /*
  6103. * Ensure we match the reduced clock's P to the target clock.
  6104. * If the clocks don't match, we can't switch the display clock
  6105. * by using the FP0/FP1. In such case we will disable the LVDS
  6106. * downclock feature.
  6107. */
  6108. *has_reduced_clock =
  6109. dev_priv->display.find_dpll(limit, intel_crtc,
  6110. dev_priv->lvds_downclock,
  6111. refclk, clock,
  6112. reduced_clock);
  6113. }
  6114. return true;
  6115. }
  6116. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  6117. {
  6118. /*
  6119. * Account for spread spectrum to avoid
  6120. * oversubscribing the link. Max center spread
  6121. * is 2.5%; use 5% for safety's sake.
  6122. */
  6123. u32 bps = target_clock * bpp * 21 / 20;
  6124. return DIV_ROUND_UP(bps, link_bw * 8);
  6125. }
  6126. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  6127. {
  6128. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  6129. }
  6130. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  6131. u32 *fp,
  6132. intel_clock_t *reduced_clock, u32 *fp2)
  6133. {
  6134. struct drm_crtc *crtc = &intel_crtc->base;
  6135. struct drm_device *dev = crtc->dev;
  6136. struct drm_i915_private *dev_priv = dev->dev_private;
  6137. struct intel_encoder *intel_encoder;
  6138. uint32_t dpll;
  6139. int factor, num_connectors = 0;
  6140. bool is_lvds = false, is_sdvo = false;
  6141. for_each_intel_encoder(dev, intel_encoder) {
  6142. if (intel_encoder->new_crtc != to_intel_crtc(crtc))
  6143. continue;
  6144. switch (intel_encoder->type) {
  6145. case INTEL_OUTPUT_LVDS:
  6146. is_lvds = true;
  6147. break;
  6148. case INTEL_OUTPUT_SDVO:
  6149. case INTEL_OUTPUT_HDMI:
  6150. is_sdvo = true;
  6151. break;
  6152. default:
  6153. break;
  6154. }
  6155. num_connectors++;
  6156. }
  6157. /* Enable autotuning of the PLL clock (if permissible) */
  6158. factor = 21;
  6159. if (is_lvds) {
  6160. if ((intel_panel_use_ssc(dev_priv) &&
  6161. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6162. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  6163. factor = 25;
  6164. } else if (intel_crtc->new_config->sdvo_tv_clock)
  6165. factor = 20;
  6166. if (ironlake_needs_fb_cb_tune(&intel_crtc->new_config->dpll, factor))
  6167. *fp |= FP_CB_TUNE;
  6168. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  6169. *fp2 |= FP_CB_TUNE;
  6170. dpll = 0;
  6171. if (is_lvds)
  6172. dpll |= DPLLB_MODE_LVDS;
  6173. else
  6174. dpll |= DPLLB_MODE_DAC_SERIAL;
  6175. dpll |= (intel_crtc->new_config->pixel_multiplier - 1)
  6176. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6177. if (is_sdvo)
  6178. dpll |= DPLL_SDVO_HIGH_SPEED;
  6179. if (intel_crtc->new_config->has_dp_encoder)
  6180. dpll |= DPLL_SDVO_HIGH_SPEED;
  6181. /* compute bitmask from p1 value */
  6182. dpll |= (1 << (intel_crtc->new_config->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6183. /* also FPA1 */
  6184. dpll |= (1 << (intel_crtc->new_config->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6185. switch (intel_crtc->new_config->dpll.p2) {
  6186. case 5:
  6187. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6188. break;
  6189. case 7:
  6190. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6191. break;
  6192. case 10:
  6193. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6194. break;
  6195. case 14:
  6196. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6197. break;
  6198. }
  6199. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6200. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6201. else
  6202. dpll |= PLL_REF_INPUT_DREFCLK;
  6203. return dpll | DPLL_VCO_ENABLE;
  6204. }
  6205. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc)
  6206. {
  6207. struct drm_device *dev = crtc->base.dev;
  6208. intel_clock_t clock, reduced_clock;
  6209. u32 dpll = 0, fp = 0, fp2 = 0;
  6210. bool ok, has_reduced_clock = false;
  6211. bool is_lvds = false;
  6212. struct intel_shared_dpll *pll;
  6213. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  6214. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  6215. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  6216. ok = ironlake_compute_clocks(&crtc->base, &clock,
  6217. &has_reduced_clock, &reduced_clock);
  6218. if (!ok && !crtc->new_config->clock_set) {
  6219. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6220. return -EINVAL;
  6221. }
  6222. /* Compat-code for transition, will disappear. */
  6223. if (!crtc->new_config->clock_set) {
  6224. crtc->new_config->dpll.n = clock.n;
  6225. crtc->new_config->dpll.m1 = clock.m1;
  6226. crtc->new_config->dpll.m2 = clock.m2;
  6227. crtc->new_config->dpll.p1 = clock.p1;
  6228. crtc->new_config->dpll.p2 = clock.p2;
  6229. }
  6230. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  6231. if (crtc->new_config->has_pch_encoder) {
  6232. fp = i9xx_dpll_compute_fp(&crtc->new_config->dpll);
  6233. if (has_reduced_clock)
  6234. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  6235. dpll = ironlake_compute_dpll(crtc,
  6236. &fp, &reduced_clock,
  6237. has_reduced_clock ? &fp2 : NULL);
  6238. crtc->new_config->dpll_hw_state.dpll = dpll;
  6239. crtc->new_config->dpll_hw_state.fp0 = fp;
  6240. if (has_reduced_clock)
  6241. crtc->new_config->dpll_hw_state.fp1 = fp2;
  6242. else
  6243. crtc->new_config->dpll_hw_state.fp1 = fp;
  6244. pll = intel_get_shared_dpll(crtc);
  6245. if (pll == NULL) {
  6246. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  6247. pipe_name(crtc->pipe));
  6248. return -EINVAL;
  6249. }
  6250. }
  6251. if (is_lvds && has_reduced_clock && i915.powersave)
  6252. crtc->lowfreq_avail = true;
  6253. else
  6254. crtc->lowfreq_avail = false;
  6255. return 0;
  6256. }
  6257. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  6258. struct intel_link_m_n *m_n)
  6259. {
  6260. struct drm_device *dev = crtc->base.dev;
  6261. struct drm_i915_private *dev_priv = dev->dev_private;
  6262. enum pipe pipe = crtc->pipe;
  6263. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  6264. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  6265. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  6266. & ~TU_SIZE_MASK;
  6267. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  6268. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  6269. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6270. }
  6271. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  6272. enum transcoder transcoder,
  6273. struct intel_link_m_n *m_n,
  6274. struct intel_link_m_n *m2_n2)
  6275. {
  6276. struct drm_device *dev = crtc->base.dev;
  6277. struct drm_i915_private *dev_priv = dev->dev_private;
  6278. enum pipe pipe = crtc->pipe;
  6279. if (INTEL_INFO(dev)->gen >= 5) {
  6280. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  6281. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  6282. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  6283. & ~TU_SIZE_MASK;
  6284. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  6285. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  6286. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6287. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  6288. * gen < 8) and if DRRS is supported (to make sure the
  6289. * registers are not unnecessarily read).
  6290. */
  6291. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  6292. crtc->config.has_drrs) {
  6293. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  6294. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  6295. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  6296. & ~TU_SIZE_MASK;
  6297. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  6298. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  6299. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6300. }
  6301. } else {
  6302. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  6303. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  6304. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  6305. & ~TU_SIZE_MASK;
  6306. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  6307. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  6308. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6309. }
  6310. }
  6311. void intel_dp_get_m_n(struct intel_crtc *crtc,
  6312. struct intel_crtc_config *pipe_config)
  6313. {
  6314. if (crtc->config.has_pch_encoder)
  6315. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  6316. else
  6317. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6318. &pipe_config->dp_m_n,
  6319. &pipe_config->dp_m2_n2);
  6320. }
  6321. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  6322. struct intel_crtc_config *pipe_config)
  6323. {
  6324. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6325. &pipe_config->fdi_m_n, NULL);
  6326. }
  6327. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  6328. struct intel_crtc_config *pipe_config)
  6329. {
  6330. struct drm_device *dev = crtc->base.dev;
  6331. struct drm_i915_private *dev_priv = dev->dev_private;
  6332. uint32_t tmp;
  6333. tmp = I915_READ(PS_CTL(crtc->pipe));
  6334. if (tmp & PS_ENABLE) {
  6335. pipe_config->pch_pfit.enabled = true;
  6336. pipe_config->pch_pfit.pos = I915_READ(PS_WIN_POS(crtc->pipe));
  6337. pipe_config->pch_pfit.size = I915_READ(PS_WIN_SZ(crtc->pipe));
  6338. }
  6339. }
  6340. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  6341. struct intel_crtc_config *pipe_config)
  6342. {
  6343. struct drm_device *dev = crtc->base.dev;
  6344. struct drm_i915_private *dev_priv = dev->dev_private;
  6345. uint32_t tmp;
  6346. tmp = I915_READ(PF_CTL(crtc->pipe));
  6347. if (tmp & PF_ENABLE) {
  6348. pipe_config->pch_pfit.enabled = true;
  6349. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  6350. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  6351. /* We currently do not free assignements of panel fitters on
  6352. * ivb/hsw (since we don't use the higher upscaling modes which
  6353. * differentiates them) so just WARN about this case for now. */
  6354. if (IS_GEN7(dev)) {
  6355. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  6356. PF_PIPE_SEL_IVB(crtc->pipe));
  6357. }
  6358. }
  6359. }
  6360. static void ironlake_get_plane_config(struct intel_crtc *crtc,
  6361. struct intel_plane_config *plane_config)
  6362. {
  6363. struct drm_device *dev = crtc->base.dev;
  6364. struct drm_i915_private *dev_priv = dev->dev_private;
  6365. u32 val, base, offset;
  6366. int pipe = crtc->pipe, plane = crtc->plane;
  6367. int fourcc, pixel_format;
  6368. int aligned_height;
  6369. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  6370. if (!crtc->base.primary->fb) {
  6371. DRM_DEBUG_KMS("failed to alloc fb\n");
  6372. return;
  6373. }
  6374. val = I915_READ(DSPCNTR(plane));
  6375. if (INTEL_INFO(dev)->gen >= 4)
  6376. if (val & DISPPLANE_TILED)
  6377. plane_config->tiled = true;
  6378. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6379. fourcc = intel_format_to_fourcc(pixel_format);
  6380. crtc->base.primary->fb->pixel_format = fourcc;
  6381. crtc->base.primary->fb->bits_per_pixel =
  6382. drm_format_plane_cpp(fourcc, 0) * 8;
  6383. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6384. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  6385. offset = I915_READ(DSPOFFSET(plane));
  6386. } else {
  6387. if (plane_config->tiled)
  6388. offset = I915_READ(DSPTILEOFF(plane));
  6389. else
  6390. offset = I915_READ(DSPLINOFF(plane));
  6391. }
  6392. plane_config->base = base;
  6393. val = I915_READ(PIPESRC(pipe));
  6394. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  6395. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  6396. val = I915_READ(DSPSTRIDE(pipe));
  6397. crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
  6398. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  6399. plane_config->tiled);
  6400. plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
  6401. aligned_height);
  6402. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6403. pipe, plane, crtc->base.primary->fb->width,
  6404. crtc->base.primary->fb->height,
  6405. crtc->base.primary->fb->bits_per_pixel, base,
  6406. crtc->base.primary->fb->pitches[0],
  6407. plane_config->size);
  6408. }
  6409. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  6410. struct intel_crtc_config *pipe_config)
  6411. {
  6412. struct drm_device *dev = crtc->base.dev;
  6413. struct drm_i915_private *dev_priv = dev->dev_private;
  6414. uint32_t tmp;
  6415. if (!intel_display_power_is_enabled(dev_priv,
  6416. POWER_DOMAIN_PIPE(crtc->pipe)))
  6417. return false;
  6418. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6419. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6420. tmp = I915_READ(PIPECONF(crtc->pipe));
  6421. if (!(tmp & PIPECONF_ENABLE))
  6422. return false;
  6423. switch (tmp & PIPECONF_BPC_MASK) {
  6424. case PIPECONF_6BPC:
  6425. pipe_config->pipe_bpp = 18;
  6426. break;
  6427. case PIPECONF_8BPC:
  6428. pipe_config->pipe_bpp = 24;
  6429. break;
  6430. case PIPECONF_10BPC:
  6431. pipe_config->pipe_bpp = 30;
  6432. break;
  6433. case PIPECONF_12BPC:
  6434. pipe_config->pipe_bpp = 36;
  6435. break;
  6436. default:
  6437. break;
  6438. }
  6439. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  6440. pipe_config->limited_color_range = true;
  6441. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  6442. struct intel_shared_dpll *pll;
  6443. pipe_config->has_pch_encoder = true;
  6444. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  6445. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6446. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6447. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6448. if (HAS_PCH_IBX(dev_priv->dev)) {
  6449. pipe_config->shared_dpll =
  6450. (enum intel_dpll_id) crtc->pipe;
  6451. } else {
  6452. tmp = I915_READ(PCH_DPLL_SEL);
  6453. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  6454. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  6455. else
  6456. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  6457. }
  6458. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6459. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6460. &pipe_config->dpll_hw_state));
  6461. tmp = pipe_config->dpll_hw_state.dpll;
  6462. pipe_config->pixel_multiplier =
  6463. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  6464. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  6465. ironlake_pch_clock_get(crtc, pipe_config);
  6466. } else {
  6467. pipe_config->pixel_multiplier = 1;
  6468. }
  6469. intel_get_pipe_timings(crtc, pipe_config);
  6470. ironlake_get_pfit_config(crtc, pipe_config);
  6471. return true;
  6472. }
  6473. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  6474. {
  6475. struct drm_device *dev = dev_priv->dev;
  6476. struct intel_crtc *crtc;
  6477. for_each_intel_crtc(dev, crtc)
  6478. WARN(crtc->active, "CRTC for pipe %c enabled\n",
  6479. pipe_name(crtc->pipe));
  6480. WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  6481. WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  6482. WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  6483. WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  6484. WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  6485. WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  6486. "CPU PWM1 enabled\n");
  6487. if (IS_HASWELL(dev))
  6488. WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  6489. "CPU PWM2 enabled\n");
  6490. WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  6491. "PCH PWM1 enabled\n");
  6492. WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  6493. "Utility pin enabled\n");
  6494. WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  6495. /*
  6496. * In theory we can still leave IRQs enabled, as long as only the HPD
  6497. * interrupts remain enabled. We used to check for that, but since it's
  6498. * gen-specific and since we only disable LCPLL after we fully disable
  6499. * the interrupts, the check below should be enough.
  6500. */
  6501. WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  6502. }
  6503. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  6504. {
  6505. struct drm_device *dev = dev_priv->dev;
  6506. if (IS_HASWELL(dev))
  6507. return I915_READ(D_COMP_HSW);
  6508. else
  6509. return I915_READ(D_COMP_BDW);
  6510. }
  6511. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  6512. {
  6513. struct drm_device *dev = dev_priv->dev;
  6514. if (IS_HASWELL(dev)) {
  6515. mutex_lock(&dev_priv->rps.hw_lock);
  6516. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  6517. val))
  6518. DRM_ERROR("Failed to write to D_COMP\n");
  6519. mutex_unlock(&dev_priv->rps.hw_lock);
  6520. } else {
  6521. I915_WRITE(D_COMP_BDW, val);
  6522. POSTING_READ(D_COMP_BDW);
  6523. }
  6524. }
  6525. /*
  6526. * This function implements pieces of two sequences from BSpec:
  6527. * - Sequence for display software to disable LCPLL
  6528. * - Sequence for display software to allow package C8+
  6529. * The steps implemented here are just the steps that actually touch the LCPLL
  6530. * register. Callers should take care of disabling all the display engine
  6531. * functions, doing the mode unset, fixing interrupts, etc.
  6532. */
  6533. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  6534. bool switch_to_fclk, bool allow_power_down)
  6535. {
  6536. uint32_t val;
  6537. assert_can_disable_lcpll(dev_priv);
  6538. val = I915_READ(LCPLL_CTL);
  6539. if (switch_to_fclk) {
  6540. val |= LCPLL_CD_SOURCE_FCLK;
  6541. I915_WRITE(LCPLL_CTL, val);
  6542. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  6543. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  6544. DRM_ERROR("Switching to FCLK failed\n");
  6545. val = I915_READ(LCPLL_CTL);
  6546. }
  6547. val |= LCPLL_PLL_DISABLE;
  6548. I915_WRITE(LCPLL_CTL, val);
  6549. POSTING_READ(LCPLL_CTL);
  6550. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  6551. DRM_ERROR("LCPLL still locked\n");
  6552. val = hsw_read_dcomp(dev_priv);
  6553. val |= D_COMP_COMP_DISABLE;
  6554. hsw_write_dcomp(dev_priv, val);
  6555. ndelay(100);
  6556. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  6557. 1))
  6558. DRM_ERROR("D_COMP RCOMP still in progress\n");
  6559. if (allow_power_down) {
  6560. val = I915_READ(LCPLL_CTL);
  6561. val |= LCPLL_POWER_DOWN_ALLOW;
  6562. I915_WRITE(LCPLL_CTL, val);
  6563. POSTING_READ(LCPLL_CTL);
  6564. }
  6565. }
  6566. /*
  6567. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  6568. * source.
  6569. */
  6570. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  6571. {
  6572. uint32_t val;
  6573. val = I915_READ(LCPLL_CTL);
  6574. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  6575. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  6576. return;
  6577. /*
  6578. * Make sure we're not on PC8 state before disabling PC8, otherwise
  6579. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  6580. *
  6581. * The other problem is that hsw_restore_lcpll() is called as part of
  6582. * the runtime PM resume sequence, so we can't just call
  6583. * gen6_gt_force_wake_get() because that function calls
  6584. * intel_runtime_pm_get(), and we can't change the runtime PM refcount
  6585. * while we are on the resume sequence. So to solve this problem we have
  6586. * to call special forcewake code that doesn't touch runtime PM and
  6587. * doesn't enable the forcewake delayed work.
  6588. */
  6589. spin_lock_irq(&dev_priv->uncore.lock);
  6590. if (dev_priv->uncore.forcewake_count++ == 0)
  6591. dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
  6592. spin_unlock_irq(&dev_priv->uncore.lock);
  6593. if (val & LCPLL_POWER_DOWN_ALLOW) {
  6594. val &= ~LCPLL_POWER_DOWN_ALLOW;
  6595. I915_WRITE(LCPLL_CTL, val);
  6596. POSTING_READ(LCPLL_CTL);
  6597. }
  6598. val = hsw_read_dcomp(dev_priv);
  6599. val |= D_COMP_COMP_FORCE;
  6600. val &= ~D_COMP_COMP_DISABLE;
  6601. hsw_write_dcomp(dev_priv, val);
  6602. val = I915_READ(LCPLL_CTL);
  6603. val &= ~LCPLL_PLL_DISABLE;
  6604. I915_WRITE(LCPLL_CTL, val);
  6605. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  6606. DRM_ERROR("LCPLL not locked yet\n");
  6607. if (val & LCPLL_CD_SOURCE_FCLK) {
  6608. val = I915_READ(LCPLL_CTL);
  6609. val &= ~LCPLL_CD_SOURCE_FCLK;
  6610. I915_WRITE(LCPLL_CTL, val);
  6611. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  6612. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  6613. DRM_ERROR("Switching back to LCPLL failed\n");
  6614. }
  6615. /* See the big comment above. */
  6616. spin_lock_irq(&dev_priv->uncore.lock);
  6617. if (--dev_priv->uncore.forcewake_count == 0)
  6618. dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
  6619. spin_unlock_irq(&dev_priv->uncore.lock);
  6620. }
  6621. /*
  6622. * Package states C8 and deeper are really deep PC states that can only be
  6623. * reached when all the devices on the system allow it, so even if the graphics
  6624. * device allows PC8+, it doesn't mean the system will actually get to these
  6625. * states. Our driver only allows PC8+ when going into runtime PM.
  6626. *
  6627. * The requirements for PC8+ are that all the outputs are disabled, the power
  6628. * well is disabled and most interrupts are disabled, and these are also
  6629. * requirements for runtime PM. When these conditions are met, we manually do
  6630. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  6631. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  6632. * hang the machine.
  6633. *
  6634. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  6635. * the state of some registers, so when we come back from PC8+ we need to
  6636. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  6637. * need to take care of the registers kept by RC6. Notice that this happens even
  6638. * if we don't put the device in PCI D3 state (which is what currently happens
  6639. * because of the runtime PM support).
  6640. *
  6641. * For more, read "Display Sequences for Package C8" on the hardware
  6642. * documentation.
  6643. */
  6644. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  6645. {
  6646. struct drm_device *dev = dev_priv->dev;
  6647. uint32_t val;
  6648. DRM_DEBUG_KMS("Enabling package C8+\n");
  6649. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6650. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6651. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  6652. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6653. }
  6654. lpt_disable_clkout_dp(dev);
  6655. hsw_disable_lcpll(dev_priv, true, true);
  6656. }
  6657. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  6658. {
  6659. struct drm_device *dev = dev_priv->dev;
  6660. uint32_t val;
  6661. DRM_DEBUG_KMS("Disabling package C8+\n");
  6662. hsw_restore_lcpll(dev_priv);
  6663. lpt_init_pch_refclk(dev);
  6664. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6665. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6666. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  6667. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6668. }
  6669. intel_prepare_ddi(dev);
  6670. }
  6671. static int haswell_crtc_compute_clock(struct intel_crtc *crtc)
  6672. {
  6673. if (!intel_ddi_pll_select(crtc))
  6674. return -EINVAL;
  6675. crtc->lowfreq_avail = false;
  6676. return 0;
  6677. }
  6678. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  6679. enum port port,
  6680. struct intel_crtc_config *pipe_config)
  6681. {
  6682. u32 temp, dpll_ctl1;
  6683. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  6684. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  6685. switch (pipe_config->ddi_pll_sel) {
  6686. case SKL_DPLL0:
  6687. /*
  6688. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  6689. * of the shared DPLL framework and thus needs to be read out
  6690. * separately
  6691. */
  6692. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  6693. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  6694. break;
  6695. case SKL_DPLL1:
  6696. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  6697. break;
  6698. case SKL_DPLL2:
  6699. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  6700. break;
  6701. case SKL_DPLL3:
  6702. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  6703. break;
  6704. }
  6705. }
  6706. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  6707. enum port port,
  6708. struct intel_crtc_config *pipe_config)
  6709. {
  6710. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  6711. switch (pipe_config->ddi_pll_sel) {
  6712. case PORT_CLK_SEL_WRPLL1:
  6713. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  6714. break;
  6715. case PORT_CLK_SEL_WRPLL2:
  6716. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  6717. break;
  6718. }
  6719. }
  6720. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  6721. struct intel_crtc_config *pipe_config)
  6722. {
  6723. struct drm_device *dev = crtc->base.dev;
  6724. struct drm_i915_private *dev_priv = dev->dev_private;
  6725. struct intel_shared_dpll *pll;
  6726. enum port port;
  6727. uint32_t tmp;
  6728. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  6729. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  6730. if (IS_SKYLAKE(dev))
  6731. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  6732. else
  6733. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  6734. if (pipe_config->shared_dpll >= 0) {
  6735. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6736. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6737. &pipe_config->dpll_hw_state));
  6738. }
  6739. /*
  6740. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  6741. * DDI E. So just check whether this pipe is wired to DDI E and whether
  6742. * the PCH transcoder is on.
  6743. */
  6744. if (INTEL_INFO(dev)->gen < 9 &&
  6745. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  6746. pipe_config->has_pch_encoder = true;
  6747. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  6748. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6749. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6750. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6751. }
  6752. }
  6753. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  6754. struct intel_crtc_config *pipe_config)
  6755. {
  6756. struct drm_device *dev = crtc->base.dev;
  6757. struct drm_i915_private *dev_priv = dev->dev_private;
  6758. enum intel_display_power_domain pfit_domain;
  6759. uint32_t tmp;
  6760. if (!intel_display_power_is_enabled(dev_priv,
  6761. POWER_DOMAIN_PIPE(crtc->pipe)))
  6762. return false;
  6763. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6764. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6765. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  6766. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  6767. enum pipe trans_edp_pipe;
  6768. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  6769. default:
  6770. WARN(1, "unknown pipe linked to edp transcoder\n");
  6771. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  6772. case TRANS_DDI_EDP_INPUT_A_ON:
  6773. trans_edp_pipe = PIPE_A;
  6774. break;
  6775. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  6776. trans_edp_pipe = PIPE_B;
  6777. break;
  6778. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  6779. trans_edp_pipe = PIPE_C;
  6780. break;
  6781. }
  6782. if (trans_edp_pipe == crtc->pipe)
  6783. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  6784. }
  6785. if (!intel_display_power_is_enabled(dev_priv,
  6786. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  6787. return false;
  6788. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  6789. if (!(tmp & PIPECONF_ENABLE))
  6790. return false;
  6791. haswell_get_ddi_port_state(crtc, pipe_config);
  6792. intel_get_pipe_timings(crtc, pipe_config);
  6793. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  6794. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  6795. if (IS_SKYLAKE(dev))
  6796. skylake_get_pfit_config(crtc, pipe_config);
  6797. else
  6798. ironlake_get_pfit_config(crtc, pipe_config);
  6799. }
  6800. if (IS_HASWELL(dev))
  6801. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  6802. (I915_READ(IPS_CTL) & IPS_ENABLE);
  6803. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  6804. pipe_config->pixel_multiplier =
  6805. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  6806. } else {
  6807. pipe_config->pixel_multiplier = 1;
  6808. }
  6809. return true;
  6810. }
  6811. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  6812. {
  6813. struct drm_device *dev = crtc->dev;
  6814. struct drm_i915_private *dev_priv = dev->dev_private;
  6815. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6816. uint32_t cntl = 0, size = 0;
  6817. if (base) {
  6818. unsigned int width = intel_crtc->cursor_width;
  6819. unsigned int height = intel_crtc->cursor_height;
  6820. unsigned int stride = roundup_pow_of_two(width) * 4;
  6821. switch (stride) {
  6822. default:
  6823. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  6824. width, stride);
  6825. stride = 256;
  6826. /* fallthrough */
  6827. case 256:
  6828. case 512:
  6829. case 1024:
  6830. case 2048:
  6831. break;
  6832. }
  6833. cntl |= CURSOR_ENABLE |
  6834. CURSOR_GAMMA_ENABLE |
  6835. CURSOR_FORMAT_ARGB |
  6836. CURSOR_STRIDE(stride);
  6837. size = (height << 12) | width;
  6838. }
  6839. if (intel_crtc->cursor_cntl != 0 &&
  6840. (intel_crtc->cursor_base != base ||
  6841. intel_crtc->cursor_size != size ||
  6842. intel_crtc->cursor_cntl != cntl)) {
  6843. /* On these chipsets we can only modify the base/size/stride
  6844. * whilst the cursor is disabled.
  6845. */
  6846. I915_WRITE(_CURACNTR, 0);
  6847. POSTING_READ(_CURACNTR);
  6848. intel_crtc->cursor_cntl = 0;
  6849. }
  6850. if (intel_crtc->cursor_base != base) {
  6851. I915_WRITE(_CURABASE, base);
  6852. intel_crtc->cursor_base = base;
  6853. }
  6854. if (intel_crtc->cursor_size != size) {
  6855. I915_WRITE(CURSIZE, size);
  6856. intel_crtc->cursor_size = size;
  6857. }
  6858. if (intel_crtc->cursor_cntl != cntl) {
  6859. I915_WRITE(_CURACNTR, cntl);
  6860. POSTING_READ(_CURACNTR);
  6861. intel_crtc->cursor_cntl = cntl;
  6862. }
  6863. }
  6864. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  6865. {
  6866. struct drm_device *dev = crtc->dev;
  6867. struct drm_i915_private *dev_priv = dev->dev_private;
  6868. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6869. int pipe = intel_crtc->pipe;
  6870. uint32_t cntl;
  6871. cntl = 0;
  6872. if (base) {
  6873. cntl = MCURSOR_GAMMA_ENABLE;
  6874. switch (intel_crtc->cursor_width) {
  6875. case 64:
  6876. cntl |= CURSOR_MODE_64_ARGB_AX;
  6877. break;
  6878. case 128:
  6879. cntl |= CURSOR_MODE_128_ARGB_AX;
  6880. break;
  6881. case 256:
  6882. cntl |= CURSOR_MODE_256_ARGB_AX;
  6883. break;
  6884. default:
  6885. WARN_ON(1);
  6886. return;
  6887. }
  6888. cntl |= pipe << 28; /* Connect to correct pipe */
  6889. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  6890. cntl |= CURSOR_PIPE_CSC_ENABLE;
  6891. }
  6892. if (to_intel_plane(crtc->cursor)->rotation == BIT(DRM_ROTATE_180))
  6893. cntl |= CURSOR_ROTATE_180;
  6894. if (intel_crtc->cursor_cntl != cntl) {
  6895. I915_WRITE(CURCNTR(pipe), cntl);
  6896. POSTING_READ(CURCNTR(pipe));
  6897. intel_crtc->cursor_cntl = cntl;
  6898. }
  6899. /* and commit changes on next vblank */
  6900. I915_WRITE(CURBASE(pipe), base);
  6901. POSTING_READ(CURBASE(pipe));
  6902. intel_crtc->cursor_base = base;
  6903. }
  6904. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  6905. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  6906. bool on)
  6907. {
  6908. struct drm_device *dev = crtc->dev;
  6909. struct drm_i915_private *dev_priv = dev->dev_private;
  6910. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6911. int pipe = intel_crtc->pipe;
  6912. int x = crtc->cursor_x;
  6913. int y = crtc->cursor_y;
  6914. u32 base = 0, pos = 0;
  6915. if (on)
  6916. base = intel_crtc->cursor_addr;
  6917. if (x >= intel_crtc->config.pipe_src_w)
  6918. base = 0;
  6919. if (y >= intel_crtc->config.pipe_src_h)
  6920. base = 0;
  6921. if (x < 0) {
  6922. if (x + intel_crtc->cursor_width <= 0)
  6923. base = 0;
  6924. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  6925. x = -x;
  6926. }
  6927. pos |= x << CURSOR_X_SHIFT;
  6928. if (y < 0) {
  6929. if (y + intel_crtc->cursor_height <= 0)
  6930. base = 0;
  6931. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  6932. y = -y;
  6933. }
  6934. pos |= y << CURSOR_Y_SHIFT;
  6935. if (base == 0 && intel_crtc->cursor_base == 0)
  6936. return;
  6937. I915_WRITE(CURPOS(pipe), pos);
  6938. /* ILK+ do this automagically */
  6939. if (HAS_GMCH_DISPLAY(dev) &&
  6940. to_intel_plane(crtc->cursor)->rotation == BIT(DRM_ROTATE_180)) {
  6941. base += (intel_crtc->cursor_height *
  6942. intel_crtc->cursor_width - 1) * 4;
  6943. }
  6944. if (IS_845G(dev) || IS_I865G(dev))
  6945. i845_update_cursor(crtc, base);
  6946. else
  6947. i9xx_update_cursor(crtc, base);
  6948. }
  6949. static bool cursor_size_ok(struct drm_device *dev,
  6950. uint32_t width, uint32_t height)
  6951. {
  6952. if (width == 0 || height == 0)
  6953. return false;
  6954. /*
  6955. * 845g/865g are special in that they are only limited by
  6956. * the width of their cursors, the height is arbitrary up to
  6957. * the precision of the register. Everything else requires
  6958. * square cursors, limited to a few power-of-two sizes.
  6959. */
  6960. if (IS_845G(dev) || IS_I865G(dev)) {
  6961. if ((width & 63) != 0)
  6962. return false;
  6963. if (width > (IS_845G(dev) ? 64 : 512))
  6964. return false;
  6965. if (height > 1023)
  6966. return false;
  6967. } else {
  6968. switch (width | height) {
  6969. case 256:
  6970. case 128:
  6971. if (IS_GEN2(dev))
  6972. return false;
  6973. case 64:
  6974. break;
  6975. default:
  6976. return false;
  6977. }
  6978. }
  6979. return true;
  6980. }
  6981. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  6982. u16 *blue, uint32_t start, uint32_t size)
  6983. {
  6984. int end = (start + size > 256) ? 256 : start + size, i;
  6985. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6986. for (i = start; i < end; i++) {
  6987. intel_crtc->lut_r[i] = red[i] >> 8;
  6988. intel_crtc->lut_g[i] = green[i] >> 8;
  6989. intel_crtc->lut_b[i] = blue[i] >> 8;
  6990. }
  6991. intel_crtc_load_lut(crtc);
  6992. }
  6993. /* VESA 640x480x72Hz mode to set on the pipe */
  6994. static struct drm_display_mode load_detect_mode = {
  6995. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  6996. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  6997. };
  6998. struct drm_framebuffer *
  6999. __intel_framebuffer_create(struct drm_device *dev,
  7000. struct drm_mode_fb_cmd2 *mode_cmd,
  7001. struct drm_i915_gem_object *obj)
  7002. {
  7003. struct intel_framebuffer *intel_fb;
  7004. int ret;
  7005. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7006. if (!intel_fb) {
  7007. drm_gem_object_unreference(&obj->base);
  7008. return ERR_PTR(-ENOMEM);
  7009. }
  7010. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  7011. if (ret)
  7012. goto err;
  7013. return &intel_fb->base;
  7014. err:
  7015. drm_gem_object_unreference(&obj->base);
  7016. kfree(intel_fb);
  7017. return ERR_PTR(ret);
  7018. }
  7019. static struct drm_framebuffer *
  7020. intel_framebuffer_create(struct drm_device *dev,
  7021. struct drm_mode_fb_cmd2 *mode_cmd,
  7022. struct drm_i915_gem_object *obj)
  7023. {
  7024. struct drm_framebuffer *fb;
  7025. int ret;
  7026. ret = i915_mutex_lock_interruptible(dev);
  7027. if (ret)
  7028. return ERR_PTR(ret);
  7029. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  7030. mutex_unlock(&dev->struct_mutex);
  7031. return fb;
  7032. }
  7033. static u32
  7034. intel_framebuffer_pitch_for_width(int width, int bpp)
  7035. {
  7036. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  7037. return ALIGN(pitch, 64);
  7038. }
  7039. static u32
  7040. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  7041. {
  7042. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  7043. return PAGE_ALIGN(pitch * mode->vdisplay);
  7044. }
  7045. static struct drm_framebuffer *
  7046. intel_framebuffer_create_for_mode(struct drm_device *dev,
  7047. struct drm_display_mode *mode,
  7048. int depth, int bpp)
  7049. {
  7050. struct drm_i915_gem_object *obj;
  7051. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  7052. obj = i915_gem_alloc_object(dev,
  7053. intel_framebuffer_size_for_mode(mode, bpp));
  7054. if (obj == NULL)
  7055. return ERR_PTR(-ENOMEM);
  7056. mode_cmd.width = mode->hdisplay;
  7057. mode_cmd.height = mode->vdisplay;
  7058. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  7059. bpp);
  7060. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  7061. return intel_framebuffer_create(dev, &mode_cmd, obj);
  7062. }
  7063. static struct drm_framebuffer *
  7064. mode_fits_in_fbdev(struct drm_device *dev,
  7065. struct drm_display_mode *mode)
  7066. {
  7067. #ifdef CONFIG_DRM_I915_FBDEV
  7068. struct drm_i915_private *dev_priv = dev->dev_private;
  7069. struct drm_i915_gem_object *obj;
  7070. struct drm_framebuffer *fb;
  7071. if (!dev_priv->fbdev)
  7072. return NULL;
  7073. if (!dev_priv->fbdev->fb)
  7074. return NULL;
  7075. obj = dev_priv->fbdev->fb->obj;
  7076. BUG_ON(!obj);
  7077. fb = &dev_priv->fbdev->fb->base;
  7078. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  7079. fb->bits_per_pixel))
  7080. return NULL;
  7081. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  7082. return NULL;
  7083. return fb;
  7084. #else
  7085. return NULL;
  7086. #endif
  7087. }
  7088. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  7089. struct drm_display_mode *mode,
  7090. struct intel_load_detect_pipe *old,
  7091. struct drm_modeset_acquire_ctx *ctx)
  7092. {
  7093. struct intel_crtc *intel_crtc;
  7094. struct intel_encoder *intel_encoder =
  7095. intel_attached_encoder(connector);
  7096. struct drm_crtc *possible_crtc;
  7097. struct drm_encoder *encoder = &intel_encoder->base;
  7098. struct drm_crtc *crtc = NULL;
  7099. struct drm_device *dev = encoder->dev;
  7100. struct drm_framebuffer *fb;
  7101. struct drm_mode_config *config = &dev->mode_config;
  7102. int ret, i = -1;
  7103. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7104. connector->base.id, connector->name,
  7105. encoder->base.id, encoder->name);
  7106. retry:
  7107. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  7108. if (ret)
  7109. goto fail_unlock;
  7110. /*
  7111. * Algorithm gets a little messy:
  7112. *
  7113. * - if the connector already has an assigned crtc, use it (but make
  7114. * sure it's on first)
  7115. *
  7116. * - try to find the first unused crtc that can drive this connector,
  7117. * and use that if we find one
  7118. */
  7119. /* See if we already have a CRTC for this connector */
  7120. if (encoder->crtc) {
  7121. crtc = encoder->crtc;
  7122. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7123. if (ret)
  7124. goto fail_unlock;
  7125. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  7126. if (ret)
  7127. goto fail_unlock;
  7128. old->dpms_mode = connector->dpms;
  7129. old->load_detect_temp = false;
  7130. /* Make sure the crtc and connector are running */
  7131. if (connector->dpms != DRM_MODE_DPMS_ON)
  7132. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  7133. return true;
  7134. }
  7135. /* Find an unused one (if possible) */
  7136. for_each_crtc(dev, possible_crtc) {
  7137. i++;
  7138. if (!(encoder->possible_crtcs & (1 << i)))
  7139. continue;
  7140. if (possible_crtc->enabled)
  7141. continue;
  7142. /* This can occur when applying the pipe A quirk on resume. */
  7143. if (to_intel_crtc(possible_crtc)->new_enabled)
  7144. continue;
  7145. crtc = possible_crtc;
  7146. break;
  7147. }
  7148. /*
  7149. * If we didn't find an unused CRTC, don't use any.
  7150. */
  7151. if (!crtc) {
  7152. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  7153. goto fail_unlock;
  7154. }
  7155. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7156. if (ret)
  7157. goto fail_unlock;
  7158. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  7159. if (ret)
  7160. goto fail_unlock;
  7161. intel_encoder->new_crtc = to_intel_crtc(crtc);
  7162. to_intel_connector(connector)->new_encoder = intel_encoder;
  7163. intel_crtc = to_intel_crtc(crtc);
  7164. intel_crtc->new_enabled = true;
  7165. intel_crtc->new_config = &intel_crtc->config;
  7166. old->dpms_mode = connector->dpms;
  7167. old->load_detect_temp = true;
  7168. old->release_fb = NULL;
  7169. if (!mode)
  7170. mode = &load_detect_mode;
  7171. /* We need a framebuffer large enough to accommodate all accesses
  7172. * that the plane may generate whilst we perform load detection.
  7173. * We can not rely on the fbcon either being present (we get called
  7174. * during its initialisation to detect all boot displays, or it may
  7175. * not even exist) or that it is large enough to satisfy the
  7176. * requested mode.
  7177. */
  7178. fb = mode_fits_in_fbdev(dev, mode);
  7179. if (fb == NULL) {
  7180. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  7181. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  7182. old->release_fb = fb;
  7183. } else
  7184. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  7185. if (IS_ERR(fb)) {
  7186. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  7187. goto fail;
  7188. }
  7189. if (intel_set_mode(crtc, mode, 0, 0, fb)) {
  7190. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  7191. if (old->release_fb)
  7192. old->release_fb->funcs->destroy(old->release_fb);
  7193. goto fail;
  7194. }
  7195. /* let the connector get through one full cycle before testing */
  7196. intel_wait_for_vblank(dev, intel_crtc->pipe);
  7197. return true;
  7198. fail:
  7199. intel_crtc->new_enabled = crtc->enabled;
  7200. if (intel_crtc->new_enabled)
  7201. intel_crtc->new_config = &intel_crtc->config;
  7202. else
  7203. intel_crtc->new_config = NULL;
  7204. fail_unlock:
  7205. if (ret == -EDEADLK) {
  7206. drm_modeset_backoff(ctx);
  7207. goto retry;
  7208. }
  7209. return false;
  7210. }
  7211. void intel_release_load_detect_pipe(struct drm_connector *connector,
  7212. struct intel_load_detect_pipe *old)
  7213. {
  7214. struct intel_encoder *intel_encoder =
  7215. intel_attached_encoder(connector);
  7216. struct drm_encoder *encoder = &intel_encoder->base;
  7217. struct drm_crtc *crtc = encoder->crtc;
  7218. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7219. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7220. connector->base.id, connector->name,
  7221. encoder->base.id, encoder->name);
  7222. if (old->load_detect_temp) {
  7223. to_intel_connector(connector)->new_encoder = NULL;
  7224. intel_encoder->new_crtc = NULL;
  7225. intel_crtc->new_enabled = false;
  7226. intel_crtc->new_config = NULL;
  7227. intel_set_mode(crtc, NULL, 0, 0, NULL);
  7228. if (old->release_fb) {
  7229. drm_framebuffer_unregister_private(old->release_fb);
  7230. drm_framebuffer_unreference(old->release_fb);
  7231. }
  7232. return;
  7233. }
  7234. /* Switch crtc and encoder back off if necessary */
  7235. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  7236. connector->funcs->dpms(connector, old->dpms_mode);
  7237. }
  7238. static int i9xx_pll_refclk(struct drm_device *dev,
  7239. const struct intel_crtc_config *pipe_config)
  7240. {
  7241. struct drm_i915_private *dev_priv = dev->dev_private;
  7242. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7243. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  7244. return dev_priv->vbt.lvds_ssc_freq;
  7245. else if (HAS_PCH_SPLIT(dev))
  7246. return 120000;
  7247. else if (!IS_GEN2(dev))
  7248. return 96000;
  7249. else
  7250. return 48000;
  7251. }
  7252. /* Returns the clock of the currently programmed mode of the given pipe. */
  7253. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  7254. struct intel_crtc_config *pipe_config)
  7255. {
  7256. struct drm_device *dev = crtc->base.dev;
  7257. struct drm_i915_private *dev_priv = dev->dev_private;
  7258. int pipe = pipe_config->cpu_transcoder;
  7259. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7260. u32 fp;
  7261. intel_clock_t clock;
  7262. int refclk = i9xx_pll_refclk(dev, pipe_config);
  7263. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  7264. fp = pipe_config->dpll_hw_state.fp0;
  7265. else
  7266. fp = pipe_config->dpll_hw_state.fp1;
  7267. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  7268. if (IS_PINEVIEW(dev)) {
  7269. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  7270. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7271. } else {
  7272. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  7273. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7274. }
  7275. if (!IS_GEN2(dev)) {
  7276. if (IS_PINEVIEW(dev))
  7277. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  7278. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  7279. else
  7280. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  7281. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7282. switch (dpll & DPLL_MODE_MASK) {
  7283. case DPLLB_MODE_DAC_SERIAL:
  7284. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  7285. 5 : 10;
  7286. break;
  7287. case DPLLB_MODE_LVDS:
  7288. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  7289. 7 : 14;
  7290. break;
  7291. default:
  7292. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  7293. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  7294. return;
  7295. }
  7296. if (IS_PINEVIEW(dev))
  7297. pineview_clock(refclk, &clock);
  7298. else
  7299. i9xx_clock(refclk, &clock);
  7300. } else {
  7301. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  7302. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  7303. if (is_lvds) {
  7304. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  7305. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7306. if (lvds & LVDS_CLKB_POWER_UP)
  7307. clock.p2 = 7;
  7308. else
  7309. clock.p2 = 14;
  7310. } else {
  7311. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  7312. clock.p1 = 2;
  7313. else {
  7314. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  7315. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  7316. }
  7317. if (dpll & PLL_P2_DIVIDE_BY_4)
  7318. clock.p2 = 4;
  7319. else
  7320. clock.p2 = 2;
  7321. }
  7322. i9xx_clock(refclk, &clock);
  7323. }
  7324. /*
  7325. * This value includes pixel_multiplier. We will use
  7326. * port_clock to compute adjusted_mode.crtc_clock in the
  7327. * encoder's get_config() function.
  7328. */
  7329. pipe_config->port_clock = clock.dot;
  7330. }
  7331. int intel_dotclock_calculate(int link_freq,
  7332. const struct intel_link_m_n *m_n)
  7333. {
  7334. /*
  7335. * The calculation for the data clock is:
  7336. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  7337. * But we want to avoid losing precison if possible, so:
  7338. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  7339. *
  7340. * and the link clock is simpler:
  7341. * link_clock = (m * link_clock) / n
  7342. */
  7343. if (!m_n->link_n)
  7344. return 0;
  7345. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  7346. }
  7347. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  7348. struct intel_crtc_config *pipe_config)
  7349. {
  7350. struct drm_device *dev = crtc->base.dev;
  7351. /* read out port_clock from the DPLL */
  7352. i9xx_crtc_clock_get(crtc, pipe_config);
  7353. /*
  7354. * This value does not include pixel_multiplier.
  7355. * We will check that port_clock and adjusted_mode.crtc_clock
  7356. * agree once we know their relationship in the encoder's
  7357. * get_config() function.
  7358. */
  7359. pipe_config->adjusted_mode.crtc_clock =
  7360. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  7361. &pipe_config->fdi_m_n);
  7362. }
  7363. /** Returns the currently programmed mode of the given pipe. */
  7364. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  7365. struct drm_crtc *crtc)
  7366. {
  7367. struct drm_i915_private *dev_priv = dev->dev_private;
  7368. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7369. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  7370. struct drm_display_mode *mode;
  7371. struct intel_crtc_config pipe_config;
  7372. int htot = I915_READ(HTOTAL(cpu_transcoder));
  7373. int hsync = I915_READ(HSYNC(cpu_transcoder));
  7374. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  7375. int vsync = I915_READ(VSYNC(cpu_transcoder));
  7376. enum pipe pipe = intel_crtc->pipe;
  7377. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  7378. if (!mode)
  7379. return NULL;
  7380. /*
  7381. * Construct a pipe_config sufficient for getting the clock info
  7382. * back out of crtc_clock_get.
  7383. *
  7384. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  7385. * to use a real value here instead.
  7386. */
  7387. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  7388. pipe_config.pixel_multiplier = 1;
  7389. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  7390. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  7391. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  7392. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  7393. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  7394. mode->hdisplay = (htot & 0xffff) + 1;
  7395. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  7396. mode->hsync_start = (hsync & 0xffff) + 1;
  7397. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  7398. mode->vdisplay = (vtot & 0xffff) + 1;
  7399. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  7400. mode->vsync_start = (vsync & 0xffff) + 1;
  7401. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  7402. drm_mode_set_name(mode);
  7403. return mode;
  7404. }
  7405. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  7406. {
  7407. struct drm_device *dev = crtc->dev;
  7408. struct drm_i915_private *dev_priv = dev->dev_private;
  7409. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7410. if (!HAS_GMCH_DISPLAY(dev))
  7411. return;
  7412. if (!dev_priv->lvds_downclock_avail)
  7413. return;
  7414. /*
  7415. * Since this is called by a timer, we should never get here in
  7416. * the manual case.
  7417. */
  7418. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  7419. int pipe = intel_crtc->pipe;
  7420. int dpll_reg = DPLL(pipe);
  7421. int dpll;
  7422. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  7423. assert_panel_unlocked(dev_priv, pipe);
  7424. dpll = I915_READ(dpll_reg);
  7425. dpll |= DISPLAY_RATE_SELECT_FPA1;
  7426. I915_WRITE(dpll_reg, dpll);
  7427. intel_wait_for_vblank(dev, pipe);
  7428. dpll = I915_READ(dpll_reg);
  7429. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  7430. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  7431. }
  7432. }
  7433. void intel_mark_busy(struct drm_device *dev)
  7434. {
  7435. struct drm_i915_private *dev_priv = dev->dev_private;
  7436. if (dev_priv->mm.busy)
  7437. return;
  7438. intel_runtime_pm_get(dev_priv);
  7439. i915_update_gfx_val(dev_priv);
  7440. dev_priv->mm.busy = true;
  7441. }
  7442. void intel_mark_idle(struct drm_device *dev)
  7443. {
  7444. struct drm_i915_private *dev_priv = dev->dev_private;
  7445. struct drm_crtc *crtc;
  7446. if (!dev_priv->mm.busy)
  7447. return;
  7448. dev_priv->mm.busy = false;
  7449. if (!i915.powersave)
  7450. goto out;
  7451. for_each_crtc(dev, crtc) {
  7452. if (!crtc->primary->fb)
  7453. continue;
  7454. intel_decrease_pllclock(crtc);
  7455. }
  7456. if (INTEL_INFO(dev)->gen >= 6)
  7457. gen6_rps_idle(dev->dev_private);
  7458. out:
  7459. intel_runtime_pm_put(dev_priv);
  7460. }
  7461. static void intel_crtc_destroy(struct drm_crtc *crtc)
  7462. {
  7463. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7464. struct drm_device *dev = crtc->dev;
  7465. struct intel_unpin_work *work;
  7466. spin_lock_irq(&dev->event_lock);
  7467. work = intel_crtc->unpin_work;
  7468. intel_crtc->unpin_work = NULL;
  7469. spin_unlock_irq(&dev->event_lock);
  7470. if (work) {
  7471. cancel_work_sync(&work->work);
  7472. kfree(work);
  7473. }
  7474. drm_crtc_cleanup(crtc);
  7475. kfree(intel_crtc);
  7476. }
  7477. static void intel_unpin_work_fn(struct work_struct *__work)
  7478. {
  7479. struct intel_unpin_work *work =
  7480. container_of(__work, struct intel_unpin_work, work);
  7481. struct drm_device *dev = work->crtc->dev;
  7482. enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
  7483. mutex_lock(&dev->struct_mutex);
  7484. intel_unpin_fb_obj(work->old_fb_obj);
  7485. drm_gem_object_unreference(&work->pending_flip_obj->base);
  7486. drm_gem_object_unreference(&work->old_fb_obj->base);
  7487. intel_update_fbc(dev);
  7488. if (work->flip_queued_req)
  7489. i915_gem_request_unreference(work->flip_queued_req);
  7490. work->flip_queued_req = NULL;
  7491. mutex_unlock(&dev->struct_mutex);
  7492. intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  7493. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  7494. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  7495. kfree(work);
  7496. }
  7497. static void do_intel_finish_page_flip(struct drm_device *dev,
  7498. struct drm_crtc *crtc)
  7499. {
  7500. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7501. struct intel_unpin_work *work;
  7502. unsigned long flags;
  7503. /* Ignore early vblank irqs */
  7504. if (intel_crtc == NULL)
  7505. return;
  7506. /*
  7507. * This is called both by irq handlers and the reset code (to complete
  7508. * lost pageflips) so needs the full irqsave spinlocks.
  7509. */
  7510. spin_lock_irqsave(&dev->event_lock, flags);
  7511. work = intel_crtc->unpin_work;
  7512. /* Ensure we don't miss a work->pending update ... */
  7513. smp_rmb();
  7514. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  7515. spin_unlock_irqrestore(&dev->event_lock, flags);
  7516. return;
  7517. }
  7518. page_flip_completed(intel_crtc);
  7519. spin_unlock_irqrestore(&dev->event_lock, flags);
  7520. }
  7521. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  7522. {
  7523. struct drm_i915_private *dev_priv = dev->dev_private;
  7524. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  7525. do_intel_finish_page_flip(dev, crtc);
  7526. }
  7527. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  7528. {
  7529. struct drm_i915_private *dev_priv = dev->dev_private;
  7530. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  7531. do_intel_finish_page_flip(dev, crtc);
  7532. }
  7533. /* Is 'a' after or equal to 'b'? */
  7534. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  7535. {
  7536. return !((a - b) & 0x80000000);
  7537. }
  7538. static bool page_flip_finished(struct intel_crtc *crtc)
  7539. {
  7540. struct drm_device *dev = crtc->base.dev;
  7541. struct drm_i915_private *dev_priv = dev->dev_private;
  7542. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  7543. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  7544. return true;
  7545. /*
  7546. * The relevant registers doen't exist on pre-ctg.
  7547. * As the flip done interrupt doesn't trigger for mmio
  7548. * flips on gmch platforms, a flip count check isn't
  7549. * really needed there. But since ctg has the registers,
  7550. * include it in the check anyway.
  7551. */
  7552. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  7553. return true;
  7554. /*
  7555. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  7556. * used the same base address. In that case the mmio flip might
  7557. * have completed, but the CS hasn't even executed the flip yet.
  7558. *
  7559. * A flip count check isn't enough as the CS might have updated
  7560. * the base address just after start of vblank, but before we
  7561. * managed to process the interrupt. This means we'd complete the
  7562. * CS flip too soon.
  7563. *
  7564. * Combining both checks should get us a good enough result. It may
  7565. * still happen that the CS flip has been executed, but has not
  7566. * yet actually completed. But in case the base address is the same
  7567. * anyway, we don't really care.
  7568. */
  7569. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  7570. crtc->unpin_work->gtt_offset &&
  7571. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  7572. crtc->unpin_work->flip_count);
  7573. }
  7574. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  7575. {
  7576. struct drm_i915_private *dev_priv = dev->dev_private;
  7577. struct intel_crtc *intel_crtc =
  7578. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  7579. unsigned long flags;
  7580. /*
  7581. * This is called both by irq handlers and the reset code (to complete
  7582. * lost pageflips) so needs the full irqsave spinlocks.
  7583. *
  7584. * NB: An MMIO update of the plane base pointer will also
  7585. * generate a page-flip completion irq, i.e. every modeset
  7586. * is also accompanied by a spurious intel_prepare_page_flip().
  7587. */
  7588. spin_lock_irqsave(&dev->event_lock, flags);
  7589. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  7590. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  7591. spin_unlock_irqrestore(&dev->event_lock, flags);
  7592. }
  7593. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  7594. {
  7595. /* Ensure that the work item is consistent when activating it ... */
  7596. smp_wmb();
  7597. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  7598. /* and that it is marked active as soon as the irq could fire. */
  7599. smp_wmb();
  7600. }
  7601. static int intel_gen2_queue_flip(struct drm_device *dev,
  7602. struct drm_crtc *crtc,
  7603. struct drm_framebuffer *fb,
  7604. struct drm_i915_gem_object *obj,
  7605. struct intel_engine_cs *ring,
  7606. uint32_t flags)
  7607. {
  7608. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7609. u32 flip_mask;
  7610. int ret;
  7611. ret = intel_ring_begin(ring, 6);
  7612. if (ret)
  7613. return ret;
  7614. /* Can't queue multiple flips, so wait for the previous
  7615. * one to finish before executing the next.
  7616. */
  7617. if (intel_crtc->plane)
  7618. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7619. else
  7620. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7621. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7622. intel_ring_emit(ring, MI_NOOP);
  7623. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7624. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7625. intel_ring_emit(ring, fb->pitches[0]);
  7626. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7627. intel_ring_emit(ring, 0); /* aux display base address, unused */
  7628. intel_mark_page_flip_active(intel_crtc);
  7629. __intel_ring_advance(ring);
  7630. return 0;
  7631. }
  7632. static int intel_gen3_queue_flip(struct drm_device *dev,
  7633. struct drm_crtc *crtc,
  7634. struct drm_framebuffer *fb,
  7635. struct drm_i915_gem_object *obj,
  7636. struct intel_engine_cs *ring,
  7637. uint32_t flags)
  7638. {
  7639. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7640. u32 flip_mask;
  7641. int ret;
  7642. ret = intel_ring_begin(ring, 6);
  7643. if (ret)
  7644. return ret;
  7645. if (intel_crtc->plane)
  7646. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7647. else
  7648. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7649. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7650. intel_ring_emit(ring, MI_NOOP);
  7651. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  7652. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7653. intel_ring_emit(ring, fb->pitches[0]);
  7654. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7655. intel_ring_emit(ring, MI_NOOP);
  7656. intel_mark_page_flip_active(intel_crtc);
  7657. __intel_ring_advance(ring);
  7658. return 0;
  7659. }
  7660. static int intel_gen4_queue_flip(struct drm_device *dev,
  7661. struct drm_crtc *crtc,
  7662. struct drm_framebuffer *fb,
  7663. struct drm_i915_gem_object *obj,
  7664. struct intel_engine_cs *ring,
  7665. uint32_t flags)
  7666. {
  7667. struct drm_i915_private *dev_priv = dev->dev_private;
  7668. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7669. uint32_t pf, pipesrc;
  7670. int ret;
  7671. ret = intel_ring_begin(ring, 4);
  7672. if (ret)
  7673. return ret;
  7674. /* i965+ uses the linear or tiled offsets from the
  7675. * Display Registers (which do not change across a page-flip)
  7676. * so we need only reprogram the base address.
  7677. */
  7678. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7679. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7680. intel_ring_emit(ring, fb->pitches[0]);
  7681. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  7682. obj->tiling_mode);
  7683. /* XXX Enabling the panel-fitter across page-flip is so far
  7684. * untested on non-native modes, so ignore it for now.
  7685. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  7686. */
  7687. pf = 0;
  7688. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7689. intel_ring_emit(ring, pf | pipesrc);
  7690. intel_mark_page_flip_active(intel_crtc);
  7691. __intel_ring_advance(ring);
  7692. return 0;
  7693. }
  7694. static int intel_gen6_queue_flip(struct drm_device *dev,
  7695. struct drm_crtc *crtc,
  7696. struct drm_framebuffer *fb,
  7697. struct drm_i915_gem_object *obj,
  7698. struct intel_engine_cs *ring,
  7699. uint32_t flags)
  7700. {
  7701. struct drm_i915_private *dev_priv = dev->dev_private;
  7702. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7703. uint32_t pf, pipesrc;
  7704. int ret;
  7705. ret = intel_ring_begin(ring, 4);
  7706. if (ret)
  7707. return ret;
  7708. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7709. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7710. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  7711. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7712. /* Contrary to the suggestions in the documentation,
  7713. * "Enable Panel Fitter" does not seem to be required when page
  7714. * flipping with a non-native mode, and worse causes a normal
  7715. * modeset to fail.
  7716. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  7717. */
  7718. pf = 0;
  7719. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7720. intel_ring_emit(ring, pf | pipesrc);
  7721. intel_mark_page_flip_active(intel_crtc);
  7722. __intel_ring_advance(ring);
  7723. return 0;
  7724. }
  7725. static int intel_gen7_queue_flip(struct drm_device *dev,
  7726. struct drm_crtc *crtc,
  7727. struct drm_framebuffer *fb,
  7728. struct drm_i915_gem_object *obj,
  7729. struct intel_engine_cs *ring,
  7730. uint32_t flags)
  7731. {
  7732. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7733. uint32_t plane_bit = 0;
  7734. int len, ret;
  7735. switch (intel_crtc->plane) {
  7736. case PLANE_A:
  7737. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  7738. break;
  7739. case PLANE_B:
  7740. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  7741. break;
  7742. case PLANE_C:
  7743. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  7744. break;
  7745. default:
  7746. WARN_ONCE(1, "unknown plane in flip command\n");
  7747. return -ENODEV;
  7748. }
  7749. len = 4;
  7750. if (ring->id == RCS) {
  7751. len += 6;
  7752. /*
  7753. * On Gen 8, SRM is now taking an extra dword to accommodate
  7754. * 48bits addresses, and we need a NOOP for the batch size to
  7755. * stay even.
  7756. */
  7757. if (IS_GEN8(dev))
  7758. len += 2;
  7759. }
  7760. /*
  7761. * BSpec MI_DISPLAY_FLIP for IVB:
  7762. * "The full packet must be contained within the same cache line."
  7763. *
  7764. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  7765. * cacheline, if we ever start emitting more commands before
  7766. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  7767. * then do the cacheline alignment, and finally emit the
  7768. * MI_DISPLAY_FLIP.
  7769. */
  7770. ret = intel_ring_cacheline_align(ring);
  7771. if (ret)
  7772. return ret;
  7773. ret = intel_ring_begin(ring, len);
  7774. if (ret)
  7775. return ret;
  7776. /* Unmask the flip-done completion message. Note that the bspec says that
  7777. * we should do this for both the BCS and RCS, and that we must not unmask
  7778. * more than one flip event at any time (or ensure that one flip message
  7779. * can be sent by waiting for flip-done prior to queueing new flips).
  7780. * Experimentation says that BCS works despite DERRMR masking all
  7781. * flip-done completion events and that unmasking all planes at once
  7782. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  7783. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  7784. */
  7785. if (ring->id == RCS) {
  7786. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  7787. intel_ring_emit(ring, DERRMR);
  7788. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  7789. DERRMR_PIPEB_PRI_FLIP_DONE |
  7790. DERRMR_PIPEC_PRI_FLIP_DONE));
  7791. if (IS_GEN8(dev))
  7792. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  7793. MI_SRM_LRM_GLOBAL_GTT);
  7794. else
  7795. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  7796. MI_SRM_LRM_GLOBAL_GTT);
  7797. intel_ring_emit(ring, DERRMR);
  7798. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  7799. if (IS_GEN8(dev)) {
  7800. intel_ring_emit(ring, 0);
  7801. intel_ring_emit(ring, MI_NOOP);
  7802. }
  7803. }
  7804. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  7805. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  7806. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7807. intel_ring_emit(ring, (MI_NOOP));
  7808. intel_mark_page_flip_active(intel_crtc);
  7809. __intel_ring_advance(ring);
  7810. return 0;
  7811. }
  7812. static bool use_mmio_flip(struct intel_engine_cs *ring,
  7813. struct drm_i915_gem_object *obj)
  7814. {
  7815. /*
  7816. * This is not being used for older platforms, because
  7817. * non-availability of flip done interrupt forces us to use
  7818. * CS flips. Older platforms derive flip done using some clever
  7819. * tricks involving the flip_pending status bits and vblank irqs.
  7820. * So using MMIO flips there would disrupt this mechanism.
  7821. */
  7822. if (ring == NULL)
  7823. return true;
  7824. if (INTEL_INFO(ring->dev)->gen < 5)
  7825. return false;
  7826. if (i915.use_mmio_flip < 0)
  7827. return false;
  7828. else if (i915.use_mmio_flip > 0)
  7829. return true;
  7830. else if (i915.enable_execlists)
  7831. return true;
  7832. else
  7833. return ring != i915_gem_request_get_ring(obj->last_read_req);
  7834. }
  7835. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
  7836. {
  7837. struct drm_device *dev = intel_crtc->base.dev;
  7838. struct drm_i915_private *dev_priv = dev->dev_private;
  7839. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  7840. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  7841. struct drm_i915_gem_object *obj = intel_fb->obj;
  7842. const enum pipe pipe = intel_crtc->pipe;
  7843. u32 ctl, stride;
  7844. ctl = I915_READ(PLANE_CTL(pipe, 0));
  7845. ctl &= ~PLANE_CTL_TILED_MASK;
  7846. if (obj->tiling_mode == I915_TILING_X)
  7847. ctl |= PLANE_CTL_TILED_X;
  7848. /*
  7849. * The stride is either expressed as a multiple of 64 bytes chunks for
  7850. * linear buffers or in number of tiles for tiled buffers.
  7851. */
  7852. stride = fb->pitches[0] >> 6;
  7853. if (obj->tiling_mode == I915_TILING_X)
  7854. stride = fb->pitches[0] >> 9; /* X tiles are 512 bytes wide */
  7855. /*
  7856. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  7857. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  7858. */
  7859. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  7860. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  7861. I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
  7862. POSTING_READ(PLANE_SURF(pipe, 0));
  7863. }
  7864. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
  7865. {
  7866. struct drm_device *dev = intel_crtc->base.dev;
  7867. struct drm_i915_private *dev_priv = dev->dev_private;
  7868. struct intel_framebuffer *intel_fb =
  7869. to_intel_framebuffer(intel_crtc->base.primary->fb);
  7870. struct drm_i915_gem_object *obj = intel_fb->obj;
  7871. u32 dspcntr;
  7872. u32 reg;
  7873. reg = DSPCNTR(intel_crtc->plane);
  7874. dspcntr = I915_READ(reg);
  7875. if (obj->tiling_mode != I915_TILING_NONE)
  7876. dspcntr |= DISPPLANE_TILED;
  7877. else
  7878. dspcntr &= ~DISPPLANE_TILED;
  7879. I915_WRITE(reg, dspcntr);
  7880. I915_WRITE(DSPSURF(intel_crtc->plane),
  7881. intel_crtc->unpin_work->gtt_offset);
  7882. POSTING_READ(DSPSURF(intel_crtc->plane));
  7883. }
  7884. /*
  7885. * XXX: This is the temporary way to update the plane registers until we get
  7886. * around to using the usual plane update functions for MMIO flips
  7887. */
  7888. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  7889. {
  7890. struct drm_device *dev = intel_crtc->base.dev;
  7891. bool atomic_update;
  7892. u32 start_vbl_count;
  7893. intel_mark_page_flip_active(intel_crtc);
  7894. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  7895. if (INTEL_INFO(dev)->gen >= 9)
  7896. skl_do_mmio_flip(intel_crtc);
  7897. else
  7898. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  7899. ilk_do_mmio_flip(intel_crtc);
  7900. if (atomic_update)
  7901. intel_pipe_update_end(intel_crtc, start_vbl_count);
  7902. }
  7903. static void intel_mmio_flip_work_func(struct work_struct *work)
  7904. {
  7905. struct intel_crtc *crtc =
  7906. container_of(work, struct intel_crtc, mmio_flip.work);
  7907. struct intel_mmio_flip *mmio_flip;
  7908. mmio_flip = &crtc->mmio_flip;
  7909. if (mmio_flip->req)
  7910. WARN_ON(__i915_wait_request(mmio_flip->req,
  7911. crtc->reset_counter,
  7912. false, NULL, NULL) != 0);
  7913. intel_do_mmio_flip(crtc);
  7914. if (mmio_flip->req) {
  7915. mutex_lock(&crtc->base.dev->struct_mutex);
  7916. i915_gem_request_unreference(mmio_flip->req);
  7917. mutex_unlock(&crtc->base.dev->struct_mutex);
  7918. }
  7919. mmio_flip->req = NULL;
  7920. }
  7921. static int intel_queue_mmio_flip(struct drm_device *dev,
  7922. struct drm_crtc *crtc,
  7923. struct drm_framebuffer *fb,
  7924. struct drm_i915_gem_object *obj,
  7925. struct intel_engine_cs *ring,
  7926. uint32_t flags)
  7927. {
  7928. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7929. i915_gem_request_assign(&intel_crtc->mmio_flip.req,
  7930. obj->last_write_req);
  7931. schedule_work(&intel_crtc->mmio_flip.work);
  7932. return 0;
  7933. }
  7934. static int intel_gen9_queue_flip(struct drm_device *dev,
  7935. struct drm_crtc *crtc,
  7936. struct drm_framebuffer *fb,
  7937. struct drm_i915_gem_object *obj,
  7938. struct intel_engine_cs *ring,
  7939. uint32_t flags)
  7940. {
  7941. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7942. uint32_t plane = 0, stride;
  7943. int ret;
  7944. switch(intel_crtc->pipe) {
  7945. case PIPE_A:
  7946. plane = MI_DISPLAY_FLIP_SKL_PLANE_1_A;
  7947. break;
  7948. case PIPE_B:
  7949. plane = MI_DISPLAY_FLIP_SKL_PLANE_1_B;
  7950. break;
  7951. case PIPE_C:
  7952. plane = MI_DISPLAY_FLIP_SKL_PLANE_1_C;
  7953. break;
  7954. default:
  7955. WARN_ONCE(1, "unknown plane in flip command\n");
  7956. return -ENODEV;
  7957. }
  7958. switch (obj->tiling_mode) {
  7959. case I915_TILING_NONE:
  7960. stride = fb->pitches[0] >> 6;
  7961. break;
  7962. case I915_TILING_X:
  7963. stride = fb->pitches[0] >> 9;
  7964. break;
  7965. default:
  7966. WARN_ONCE(1, "unknown tiling in flip command\n");
  7967. return -ENODEV;
  7968. }
  7969. ret = intel_ring_begin(ring, 10);
  7970. if (ret)
  7971. return ret;
  7972. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  7973. intel_ring_emit(ring, DERRMR);
  7974. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  7975. DERRMR_PIPEB_PRI_FLIP_DONE |
  7976. DERRMR_PIPEC_PRI_FLIP_DONE));
  7977. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  7978. MI_SRM_LRM_GLOBAL_GTT);
  7979. intel_ring_emit(ring, DERRMR);
  7980. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  7981. intel_ring_emit(ring, 0);
  7982. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane);
  7983. intel_ring_emit(ring, stride << 6 | obj->tiling_mode);
  7984. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7985. intel_mark_page_flip_active(intel_crtc);
  7986. __intel_ring_advance(ring);
  7987. return 0;
  7988. }
  7989. static int intel_default_queue_flip(struct drm_device *dev,
  7990. struct drm_crtc *crtc,
  7991. struct drm_framebuffer *fb,
  7992. struct drm_i915_gem_object *obj,
  7993. struct intel_engine_cs *ring,
  7994. uint32_t flags)
  7995. {
  7996. return -ENODEV;
  7997. }
  7998. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  7999. struct drm_crtc *crtc)
  8000. {
  8001. struct drm_i915_private *dev_priv = dev->dev_private;
  8002. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8003. struct intel_unpin_work *work = intel_crtc->unpin_work;
  8004. u32 addr;
  8005. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  8006. return true;
  8007. if (!work->enable_stall_check)
  8008. return false;
  8009. if (work->flip_ready_vblank == 0) {
  8010. if (work->flip_queued_req &&
  8011. !i915_gem_request_completed(work->flip_queued_req, true))
  8012. return false;
  8013. work->flip_ready_vblank = drm_vblank_count(dev, intel_crtc->pipe);
  8014. }
  8015. if (drm_vblank_count(dev, intel_crtc->pipe) - work->flip_ready_vblank < 3)
  8016. return false;
  8017. /* Potential stall - if we see that the flip has happened,
  8018. * assume a missed interrupt. */
  8019. if (INTEL_INFO(dev)->gen >= 4)
  8020. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  8021. else
  8022. addr = I915_READ(DSPADDR(intel_crtc->plane));
  8023. /* There is a potential issue here with a false positive after a flip
  8024. * to the same address. We could address this by checking for a
  8025. * non-incrementing frame counter.
  8026. */
  8027. return addr == work->gtt_offset;
  8028. }
  8029. void intel_check_page_flip(struct drm_device *dev, int pipe)
  8030. {
  8031. struct drm_i915_private *dev_priv = dev->dev_private;
  8032. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  8033. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8034. WARN_ON(!in_irq());
  8035. if (crtc == NULL)
  8036. return;
  8037. spin_lock(&dev->event_lock);
  8038. if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
  8039. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  8040. intel_crtc->unpin_work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  8041. page_flip_completed(intel_crtc);
  8042. }
  8043. spin_unlock(&dev->event_lock);
  8044. }
  8045. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  8046. struct drm_framebuffer *fb,
  8047. struct drm_pending_vblank_event *event,
  8048. uint32_t page_flip_flags)
  8049. {
  8050. struct drm_device *dev = crtc->dev;
  8051. struct drm_i915_private *dev_priv = dev->dev_private;
  8052. struct drm_framebuffer *old_fb = crtc->primary->fb;
  8053. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  8054. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8055. struct drm_plane *primary = crtc->primary;
  8056. struct intel_plane *intel_plane = to_intel_plane(primary);
  8057. enum pipe pipe = intel_crtc->pipe;
  8058. struct intel_unpin_work *work;
  8059. struct intel_engine_cs *ring;
  8060. int ret;
  8061. /*
  8062. * drm_mode_page_flip_ioctl() should already catch this, but double
  8063. * check to be safe. In the future we may enable pageflipping from
  8064. * a disabled primary plane.
  8065. */
  8066. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  8067. return -EBUSY;
  8068. /* Can't change pixel format via MI display flips. */
  8069. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  8070. return -EINVAL;
  8071. /*
  8072. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  8073. * Note that pitch changes could also affect these register.
  8074. */
  8075. if (INTEL_INFO(dev)->gen > 3 &&
  8076. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  8077. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  8078. return -EINVAL;
  8079. if (i915_terminally_wedged(&dev_priv->gpu_error))
  8080. goto out_hang;
  8081. work = kzalloc(sizeof(*work), GFP_KERNEL);
  8082. if (work == NULL)
  8083. return -ENOMEM;
  8084. work->event = event;
  8085. work->crtc = crtc;
  8086. work->old_fb_obj = intel_fb_obj(old_fb);
  8087. INIT_WORK(&work->work, intel_unpin_work_fn);
  8088. ret = drm_crtc_vblank_get(crtc);
  8089. if (ret)
  8090. goto free_work;
  8091. /* We borrow the event spin lock for protecting unpin_work */
  8092. spin_lock_irq(&dev->event_lock);
  8093. if (intel_crtc->unpin_work) {
  8094. /* Before declaring the flip queue wedged, check if
  8095. * the hardware completed the operation behind our backs.
  8096. */
  8097. if (__intel_pageflip_stall_check(dev, crtc)) {
  8098. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  8099. page_flip_completed(intel_crtc);
  8100. } else {
  8101. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  8102. spin_unlock_irq(&dev->event_lock);
  8103. drm_crtc_vblank_put(crtc);
  8104. kfree(work);
  8105. return -EBUSY;
  8106. }
  8107. }
  8108. intel_crtc->unpin_work = work;
  8109. spin_unlock_irq(&dev->event_lock);
  8110. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  8111. flush_workqueue(dev_priv->wq);
  8112. ret = i915_mutex_lock_interruptible(dev);
  8113. if (ret)
  8114. goto cleanup;
  8115. /* Reference the objects for the scheduled work. */
  8116. drm_gem_object_reference(&work->old_fb_obj->base);
  8117. drm_gem_object_reference(&obj->base);
  8118. crtc->primary->fb = fb;
  8119. work->pending_flip_obj = obj;
  8120. atomic_inc(&intel_crtc->unpin_work_count);
  8121. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  8122. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  8123. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  8124. if (IS_VALLEYVIEW(dev)) {
  8125. ring = &dev_priv->ring[BCS];
  8126. if (obj->tiling_mode != work->old_fb_obj->tiling_mode)
  8127. /* vlv: DISPLAY_FLIP fails to change tiling */
  8128. ring = NULL;
  8129. } else if (IS_IVYBRIDGE(dev)) {
  8130. ring = &dev_priv->ring[BCS];
  8131. } else if (INTEL_INFO(dev)->gen >= 7) {
  8132. ring = i915_gem_request_get_ring(obj->last_read_req);
  8133. if (ring == NULL || ring->id != RCS)
  8134. ring = &dev_priv->ring[BCS];
  8135. } else {
  8136. ring = &dev_priv->ring[RCS];
  8137. }
  8138. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb, ring);
  8139. if (ret)
  8140. goto cleanup_pending;
  8141. work->gtt_offset =
  8142. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
  8143. if (use_mmio_flip(ring, obj)) {
  8144. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  8145. page_flip_flags);
  8146. if (ret)
  8147. goto cleanup_unpin;
  8148. i915_gem_request_assign(&work->flip_queued_req,
  8149. obj->last_write_req);
  8150. } else {
  8151. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
  8152. page_flip_flags);
  8153. if (ret)
  8154. goto cleanup_unpin;
  8155. i915_gem_request_assign(&work->flip_queued_req,
  8156. intel_ring_get_request(ring));
  8157. }
  8158. work->flip_queued_vblank = drm_vblank_count(dev, intel_crtc->pipe);
  8159. work->enable_stall_check = true;
  8160. i915_gem_track_fb(work->old_fb_obj, obj,
  8161. INTEL_FRONTBUFFER_PRIMARY(pipe));
  8162. intel_disable_fbc(dev);
  8163. intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  8164. mutex_unlock(&dev->struct_mutex);
  8165. trace_i915_flip_request(intel_crtc->plane, obj);
  8166. return 0;
  8167. cleanup_unpin:
  8168. intel_unpin_fb_obj(obj);
  8169. cleanup_pending:
  8170. atomic_dec(&intel_crtc->unpin_work_count);
  8171. crtc->primary->fb = old_fb;
  8172. drm_gem_object_unreference(&work->old_fb_obj->base);
  8173. drm_gem_object_unreference(&obj->base);
  8174. mutex_unlock(&dev->struct_mutex);
  8175. cleanup:
  8176. spin_lock_irq(&dev->event_lock);
  8177. intel_crtc->unpin_work = NULL;
  8178. spin_unlock_irq(&dev->event_lock);
  8179. drm_crtc_vblank_put(crtc);
  8180. free_work:
  8181. kfree(work);
  8182. if (ret == -EIO) {
  8183. out_hang:
  8184. ret = primary->funcs->update_plane(primary, crtc, fb,
  8185. intel_plane->crtc_x,
  8186. intel_plane->crtc_y,
  8187. intel_plane->crtc_h,
  8188. intel_plane->crtc_w,
  8189. intel_plane->src_x,
  8190. intel_plane->src_y,
  8191. intel_plane->src_h,
  8192. intel_plane->src_w);
  8193. if (ret == 0 && event) {
  8194. spin_lock_irq(&dev->event_lock);
  8195. drm_send_vblank_event(dev, pipe, event);
  8196. spin_unlock_irq(&dev->event_lock);
  8197. }
  8198. }
  8199. return ret;
  8200. }
  8201. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  8202. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  8203. .load_lut = intel_crtc_load_lut,
  8204. };
  8205. /**
  8206. * intel_modeset_update_staged_output_state
  8207. *
  8208. * Updates the staged output configuration state, e.g. after we've read out the
  8209. * current hw state.
  8210. */
  8211. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  8212. {
  8213. struct intel_crtc *crtc;
  8214. struct intel_encoder *encoder;
  8215. struct intel_connector *connector;
  8216. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8217. base.head) {
  8218. connector->new_encoder =
  8219. to_intel_encoder(connector->base.encoder);
  8220. }
  8221. for_each_intel_encoder(dev, encoder) {
  8222. encoder->new_crtc =
  8223. to_intel_crtc(encoder->base.crtc);
  8224. }
  8225. for_each_intel_crtc(dev, crtc) {
  8226. crtc->new_enabled = crtc->base.enabled;
  8227. if (crtc->new_enabled)
  8228. crtc->new_config = &crtc->config;
  8229. else
  8230. crtc->new_config = NULL;
  8231. }
  8232. }
  8233. /**
  8234. * intel_modeset_commit_output_state
  8235. *
  8236. * This function copies the stage display pipe configuration to the real one.
  8237. */
  8238. static void intel_modeset_commit_output_state(struct drm_device *dev)
  8239. {
  8240. struct intel_crtc *crtc;
  8241. struct intel_encoder *encoder;
  8242. struct intel_connector *connector;
  8243. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8244. base.head) {
  8245. connector->base.encoder = &connector->new_encoder->base;
  8246. }
  8247. for_each_intel_encoder(dev, encoder) {
  8248. encoder->base.crtc = &encoder->new_crtc->base;
  8249. }
  8250. for_each_intel_crtc(dev, crtc) {
  8251. crtc->base.enabled = crtc->new_enabled;
  8252. }
  8253. }
  8254. static void
  8255. connected_sink_compute_bpp(struct intel_connector *connector,
  8256. struct intel_crtc_config *pipe_config)
  8257. {
  8258. int bpp = pipe_config->pipe_bpp;
  8259. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8260. connector->base.base.id,
  8261. connector->base.name);
  8262. /* Don't use an invalid EDID bpc value */
  8263. if (connector->base.display_info.bpc &&
  8264. connector->base.display_info.bpc * 3 < bpp) {
  8265. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8266. bpp, connector->base.display_info.bpc*3);
  8267. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  8268. }
  8269. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8270. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  8271. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8272. bpp);
  8273. pipe_config->pipe_bpp = 24;
  8274. }
  8275. }
  8276. static int
  8277. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8278. struct drm_framebuffer *fb,
  8279. struct intel_crtc_config *pipe_config)
  8280. {
  8281. struct drm_device *dev = crtc->base.dev;
  8282. struct intel_connector *connector;
  8283. int bpp;
  8284. switch (fb->pixel_format) {
  8285. case DRM_FORMAT_C8:
  8286. bpp = 8*3; /* since we go through a colormap */
  8287. break;
  8288. case DRM_FORMAT_XRGB1555:
  8289. case DRM_FORMAT_ARGB1555:
  8290. /* checked in intel_framebuffer_init already */
  8291. if (WARN_ON(INTEL_INFO(dev)->gen > 3))
  8292. return -EINVAL;
  8293. case DRM_FORMAT_RGB565:
  8294. bpp = 6*3; /* min is 18bpp */
  8295. break;
  8296. case DRM_FORMAT_XBGR8888:
  8297. case DRM_FORMAT_ABGR8888:
  8298. /* checked in intel_framebuffer_init already */
  8299. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8300. return -EINVAL;
  8301. case DRM_FORMAT_XRGB8888:
  8302. case DRM_FORMAT_ARGB8888:
  8303. bpp = 8*3;
  8304. break;
  8305. case DRM_FORMAT_XRGB2101010:
  8306. case DRM_FORMAT_ARGB2101010:
  8307. case DRM_FORMAT_XBGR2101010:
  8308. case DRM_FORMAT_ABGR2101010:
  8309. /* checked in intel_framebuffer_init already */
  8310. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8311. return -EINVAL;
  8312. bpp = 10*3;
  8313. break;
  8314. /* TODO: gen4+ supports 16 bpc floating point, too. */
  8315. default:
  8316. DRM_DEBUG_KMS("unsupported depth\n");
  8317. return -EINVAL;
  8318. }
  8319. pipe_config->pipe_bpp = bpp;
  8320. /* Clamp display bpp to EDID value */
  8321. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8322. base.head) {
  8323. if (!connector->new_encoder ||
  8324. connector->new_encoder->new_crtc != crtc)
  8325. continue;
  8326. connected_sink_compute_bpp(connector, pipe_config);
  8327. }
  8328. return bpp;
  8329. }
  8330. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  8331. {
  8332. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  8333. "type: 0x%x flags: 0x%x\n",
  8334. mode->crtc_clock,
  8335. mode->crtc_hdisplay, mode->crtc_hsync_start,
  8336. mode->crtc_hsync_end, mode->crtc_htotal,
  8337. mode->crtc_vdisplay, mode->crtc_vsync_start,
  8338. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  8339. }
  8340. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  8341. struct intel_crtc_config *pipe_config,
  8342. const char *context)
  8343. {
  8344. DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
  8345. context, pipe_name(crtc->pipe));
  8346. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  8347. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  8348. pipe_config->pipe_bpp, pipe_config->dither);
  8349. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8350. pipe_config->has_pch_encoder,
  8351. pipe_config->fdi_lanes,
  8352. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  8353. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  8354. pipe_config->fdi_m_n.tu);
  8355. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8356. pipe_config->has_dp_encoder,
  8357. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  8358. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  8359. pipe_config->dp_m_n.tu);
  8360. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  8361. pipe_config->has_dp_encoder,
  8362. pipe_config->dp_m2_n2.gmch_m,
  8363. pipe_config->dp_m2_n2.gmch_n,
  8364. pipe_config->dp_m2_n2.link_m,
  8365. pipe_config->dp_m2_n2.link_n,
  8366. pipe_config->dp_m2_n2.tu);
  8367. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  8368. pipe_config->has_audio,
  8369. pipe_config->has_infoframe);
  8370. DRM_DEBUG_KMS("requested mode:\n");
  8371. drm_mode_debug_printmodeline(&pipe_config->requested_mode);
  8372. DRM_DEBUG_KMS("adjusted mode:\n");
  8373. drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
  8374. intel_dump_crtc_timings(&pipe_config->adjusted_mode);
  8375. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  8376. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  8377. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  8378. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  8379. pipe_config->gmch_pfit.control,
  8380. pipe_config->gmch_pfit.pgm_ratios,
  8381. pipe_config->gmch_pfit.lvds_border_bits);
  8382. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  8383. pipe_config->pch_pfit.pos,
  8384. pipe_config->pch_pfit.size,
  8385. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  8386. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  8387. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  8388. }
  8389. static bool encoders_cloneable(const struct intel_encoder *a,
  8390. const struct intel_encoder *b)
  8391. {
  8392. /* masks could be asymmetric, so check both ways */
  8393. return a == b || (a->cloneable & (1 << b->type) &&
  8394. b->cloneable & (1 << a->type));
  8395. }
  8396. static bool check_single_encoder_cloning(struct intel_crtc *crtc,
  8397. struct intel_encoder *encoder)
  8398. {
  8399. struct drm_device *dev = crtc->base.dev;
  8400. struct intel_encoder *source_encoder;
  8401. for_each_intel_encoder(dev, source_encoder) {
  8402. if (source_encoder->new_crtc != crtc)
  8403. continue;
  8404. if (!encoders_cloneable(encoder, source_encoder))
  8405. return false;
  8406. }
  8407. return true;
  8408. }
  8409. static bool check_encoder_cloning(struct intel_crtc *crtc)
  8410. {
  8411. struct drm_device *dev = crtc->base.dev;
  8412. struct intel_encoder *encoder;
  8413. for_each_intel_encoder(dev, encoder) {
  8414. if (encoder->new_crtc != crtc)
  8415. continue;
  8416. if (!check_single_encoder_cloning(crtc, encoder))
  8417. return false;
  8418. }
  8419. return true;
  8420. }
  8421. static bool check_digital_port_conflicts(struct drm_device *dev)
  8422. {
  8423. struct intel_connector *connector;
  8424. unsigned int used_ports = 0;
  8425. /*
  8426. * Walk the connector list instead of the encoder
  8427. * list to detect the problem on ddi platforms
  8428. * where there's just one encoder per digital port.
  8429. */
  8430. list_for_each_entry(connector,
  8431. &dev->mode_config.connector_list, base.head) {
  8432. struct intel_encoder *encoder = connector->new_encoder;
  8433. if (!encoder)
  8434. continue;
  8435. WARN_ON(!encoder->new_crtc);
  8436. switch (encoder->type) {
  8437. unsigned int port_mask;
  8438. case INTEL_OUTPUT_UNKNOWN:
  8439. if (WARN_ON(!HAS_DDI(dev)))
  8440. break;
  8441. case INTEL_OUTPUT_DISPLAYPORT:
  8442. case INTEL_OUTPUT_HDMI:
  8443. case INTEL_OUTPUT_EDP:
  8444. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  8445. /* the same port mustn't appear more than once */
  8446. if (used_ports & port_mask)
  8447. return false;
  8448. used_ports |= port_mask;
  8449. default:
  8450. break;
  8451. }
  8452. }
  8453. return true;
  8454. }
  8455. static struct intel_crtc_config *
  8456. intel_modeset_pipe_config(struct drm_crtc *crtc,
  8457. struct drm_framebuffer *fb,
  8458. struct drm_display_mode *mode)
  8459. {
  8460. struct drm_device *dev = crtc->dev;
  8461. struct intel_encoder *encoder;
  8462. struct intel_crtc_config *pipe_config;
  8463. int plane_bpp, ret = -EINVAL;
  8464. bool retry = true;
  8465. if (!check_encoder_cloning(to_intel_crtc(crtc))) {
  8466. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  8467. return ERR_PTR(-EINVAL);
  8468. }
  8469. if (!check_digital_port_conflicts(dev)) {
  8470. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  8471. return ERR_PTR(-EINVAL);
  8472. }
  8473. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8474. if (!pipe_config)
  8475. return ERR_PTR(-ENOMEM);
  8476. drm_mode_copy(&pipe_config->adjusted_mode, mode);
  8477. drm_mode_copy(&pipe_config->requested_mode, mode);
  8478. pipe_config->cpu_transcoder =
  8479. (enum transcoder) to_intel_crtc(crtc)->pipe;
  8480. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8481. /*
  8482. * Sanitize sync polarity flags based on requested ones. If neither
  8483. * positive or negative polarity is requested, treat this as meaning
  8484. * negative polarity.
  8485. */
  8486. if (!(pipe_config->adjusted_mode.flags &
  8487. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  8488. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  8489. if (!(pipe_config->adjusted_mode.flags &
  8490. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  8491. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  8492. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  8493. * plane pixel format and any sink constraints into account. Returns the
  8494. * source plane bpp so that dithering can be selected on mismatches
  8495. * after encoders and crtc also have had their say. */
  8496. plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  8497. fb, pipe_config);
  8498. if (plane_bpp < 0)
  8499. goto fail;
  8500. /*
  8501. * Determine the real pipe dimensions. Note that stereo modes can
  8502. * increase the actual pipe size due to the frame doubling and
  8503. * insertion of additional space for blanks between the frame. This
  8504. * is stored in the crtc timings. We use the requested mode to do this
  8505. * computation to clearly distinguish it from the adjusted mode, which
  8506. * can be changed by the connectors in the below retry loop.
  8507. */
  8508. drm_crtc_get_hv_timing(&pipe_config->requested_mode,
  8509. &pipe_config->pipe_src_w,
  8510. &pipe_config->pipe_src_h);
  8511. encoder_retry:
  8512. /* Ensure the port clock defaults are reset when retrying. */
  8513. pipe_config->port_clock = 0;
  8514. pipe_config->pixel_multiplier = 1;
  8515. /* Fill in default crtc timings, allow encoders to overwrite them. */
  8516. drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
  8517. /* Pass our mode to the connectors and the CRTC to give them a chance to
  8518. * adjust it according to limitations or connector properties, and also
  8519. * a chance to reject the mode entirely.
  8520. */
  8521. for_each_intel_encoder(dev, encoder) {
  8522. if (&encoder->new_crtc->base != crtc)
  8523. continue;
  8524. if (!(encoder->compute_config(encoder, pipe_config))) {
  8525. DRM_DEBUG_KMS("Encoder config failure\n");
  8526. goto fail;
  8527. }
  8528. }
  8529. /* Set default port clock if not overwritten by the encoder. Needs to be
  8530. * done afterwards in case the encoder adjusts the mode. */
  8531. if (!pipe_config->port_clock)
  8532. pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
  8533. * pipe_config->pixel_multiplier;
  8534. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  8535. if (ret < 0) {
  8536. DRM_DEBUG_KMS("CRTC fixup failed\n");
  8537. goto fail;
  8538. }
  8539. if (ret == RETRY) {
  8540. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  8541. ret = -EINVAL;
  8542. goto fail;
  8543. }
  8544. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  8545. retry = false;
  8546. goto encoder_retry;
  8547. }
  8548. pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
  8549. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  8550. plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  8551. return pipe_config;
  8552. fail:
  8553. kfree(pipe_config);
  8554. return ERR_PTR(ret);
  8555. }
  8556. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  8557. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  8558. static void
  8559. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  8560. unsigned *prepare_pipes, unsigned *disable_pipes)
  8561. {
  8562. struct intel_crtc *intel_crtc;
  8563. struct drm_device *dev = crtc->dev;
  8564. struct intel_encoder *encoder;
  8565. struct intel_connector *connector;
  8566. struct drm_crtc *tmp_crtc;
  8567. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  8568. /* Check which crtcs have changed outputs connected to them, these need
  8569. * to be part of the prepare_pipes mask. We don't (yet) support global
  8570. * modeset across multiple crtcs, so modeset_pipes will only have one
  8571. * bit set at most. */
  8572. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8573. base.head) {
  8574. if (connector->base.encoder == &connector->new_encoder->base)
  8575. continue;
  8576. if (connector->base.encoder) {
  8577. tmp_crtc = connector->base.encoder->crtc;
  8578. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8579. }
  8580. if (connector->new_encoder)
  8581. *prepare_pipes |=
  8582. 1 << connector->new_encoder->new_crtc->pipe;
  8583. }
  8584. for_each_intel_encoder(dev, encoder) {
  8585. if (encoder->base.crtc == &encoder->new_crtc->base)
  8586. continue;
  8587. if (encoder->base.crtc) {
  8588. tmp_crtc = encoder->base.crtc;
  8589. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8590. }
  8591. if (encoder->new_crtc)
  8592. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  8593. }
  8594. /* Check for pipes that will be enabled/disabled ... */
  8595. for_each_intel_crtc(dev, intel_crtc) {
  8596. if (intel_crtc->base.enabled == intel_crtc->new_enabled)
  8597. continue;
  8598. if (!intel_crtc->new_enabled)
  8599. *disable_pipes |= 1 << intel_crtc->pipe;
  8600. else
  8601. *prepare_pipes |= 1 << intel_crtc->pipe;
  8602. }
  8603. /* set_mode is also used to update properties on life display pipes. */
  8604. intel_crtc = to_intel_crtc(crtc);
  8605. if (intel_crtc->new_enabled)
  8606. *prepare_pipes |= 1 << intel_crtc->pipe;
  8607. /*
  8608. * For simplicity do a full modeset on any pipe where the output routing
  8609. * changed. We could be more clever, but that would require us to be
  8610. * more careful with calling the relevant encoder->mode_set functions.
  8611. */
  8612. if (*prepare_pipes)
  8613. *modeset_pipes = *prepare_pipes;
  8614. /* ... and mask these out. */
  8615. *modeset_pipes &= ~(*disable_pipes);
  8616. *prepare_pipes &= ~(*disable_pipes);
  8617. /*
  8618. * HACK: We don't (yet) fully support global modesets. intel_set_config
  8619. * obies this rule, but the modeset restore mode of
  8620. * intel_modeset_setup_hw_state does not.
  8621. */
  8622. *modeset_pipes &= 1 << intel_crtc->pipe;
  8623. *prepare_pipes &= 1 << intel_crtc->pipe;
  8624. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  8625. *modeset_pipes, *prepare_pipes, *disable_pipes);
  8626. }
  8627. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  8628. {
  8629. struct drm_encoder *encoder;
  8630. struct drm_device *dev = crtc->dev;
  8631. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  8632. if (encoder->crtc == crtc)
  8633. return true;
  8634. return false;
  8635. }
  8636. static void
  8637. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  8638. {
  8639. struct drm_i915_private *dev_priv = dev->dev_private;
  8640. struct intel_encoder *intel_encoder;
  8641. struct intel_crtc *intel_crtc;
  8642. struct drm_connector *connector;
  8643. intel_shared_dpll_commit(dev_priv);
  8644. for_each_intel_encoder(dev, intel_encoder) {
  8645. if (!intel_encoder->base.crtc)
  8646. continue;
  8647. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  8648. if (prepare_pipes & (1 << intel_crtc->pipe))
  8649. intel_encoder->connectors_active = false;
  8650. }
  8651. intel_modeset_commit_output_state(dev);
  8652. /* Double check state. */
  8653. for_each_intel_crtc(dev, intel_crtc) {
  8654. WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
  8655. WARN_ON(intel_crtc->new_config &&
  8656. intel_crtc->new_config != &intel_crtc->config);
  8657. WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
  8658. }
  8659. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  8660. if (!connector->encoder || !connector->encoder->crtc)
  8661. continue;
  8662. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  8663. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  8664. struct drm_property *dpms_property =
  8665. dev->mode_config.dpms_property;
  8666. connector->dpms = DRM_MODE_DPMS_ON;
  8667. drm_object_property_set_value(&connector->base,
  8668. dpms_property,
  8669. DRM_MODE_DPMS_ON);
  8670. intel_encoder = to_intel_encoder(connector->encoder);
  8671. intel_encoder->connectors_active = true;
  8672. }
  8673. }
  8674. }
  8675. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  8676. {
  8677. int diff;
  8678. if (clock1 == clock2)
  8679. return true;
  8680. if (!clock1 || !clock2)
  8681. return false;
  8682. diff = abs(clock1 - clock2);
  8683. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  8684. return true;
  8685. return false;
  8686. }
  8687. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  8688. list_for_each_entry((intel_crtc), \
  8689. &(dev)->mode_config.crtc_list, \
  8690. base.head) \
  8691. if (mask & (1 <<(intel_crtc)->pipe))
  8692. static bool
  8693. intel_pipe_config_compare(struct drm_device *dev,
  8694. struct intel_crtc_config *current_config,
  8695. struct intel_crtc_config *pipe_config)
  8696. {
  8697. #define PIPE_CONF_CHECK_X(name) \
  8698. if (current_config->name != pipe_config->name) { \
  8699. DRM_ERROR("mismatch in " #name " " \
  8700. "(expected 0x%08x, found 0x%08x)\n", \
  8701. current_config->name, \
  8702. pipe_config->name); \
  8703. return false; \
  8704. }
  8705. #define PIPE_CONF_CHECK_I(name) \
  8706. if (current_config->name != pipe_config->name) { \
  8707. DRM_ERROR("mismatch in " #name " " \
  8708. "(expected %i, found %i)\n", \
  8709. current_config->name, \
  8710. pipe_config->name); \
  8711. return false; \
  8712. }
  8713. /* This is required for BDW+ where there is only one set of registers for
  8714. * switching between high and low RR.
  8715. * This macro can be used whenever a comparison has to be made between one
  8716. * hw state and multiple sw state variables.
  8717. */
  8718. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  8719. if ((current_config->name != pipe_config->name) && \
  8720. (current_config->alt_name != pipe_config->name)) { \
  8721. DRM_ERROR("mismatch in " #name " " \
  8722. "(expected %i or %i, found %i)\n", \
  8723. current_config->name, \
  8724. current_config->alt_name, \
  8725. pipe_config->name); \
  8726. return false; \
  8727. }
  8728. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  8729. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  8730. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  8731. "(expected %i, found %i)\n", \
  8732. current_config->name & (mask), \
  8733. pipe_config->name & (mask)); \
  8734. return false; \
  8735. }
  8736. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  8737. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  8738. DRM_ERROR("mismatch in " #name " " \
  8739. "(expected %i, found %i)\n", \
  8740. current_config->name, \
  8741. pipe_config->name); \
  8742. return false; \
  8743. }
  8744. #define PIPE_CONF_QUIRK(quirk) \
  8745. ((current_config->quirks | pipe_config->quirks) & (quirk))
  8746. PIPE_CONF_CHECK_I(cpu_transcoder);
  8747. PIPE_CONF_CHECK_I(has_pch_encoder);
  8748. PIPE_CONF_CHECK_I(fdi_lanes);
  8749. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  8750. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  8751. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  8752. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  8753. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  8754. PIPE_CONF_CHECK_I(has_dp_encoder);
  8755. if (INTEL_INFO(dev)->gen < 8) {
  8756. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  8757. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  8758. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  8759. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  8760. PIPE_CONF_CHECK_I(dp_m_n.tu);
  8761. if (current_config->has_drrs) {
  8762. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
  8763. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
  8764. PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
  8765. PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
  8766. PIPE_CONF_CHECK_I(dp_m2_n2.tu);
  8767. }
  8768. } else {
  8769. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
  8770. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
  8771. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
  8772. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
  8773. PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
  8774. }
  8775. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
  8776. PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
  8777. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
  8778. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
  8779. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
  8780. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
  8781. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
  8782. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
  8783. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
  8784. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
  8785. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
  8786. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
  8787. PIPE_CONF_CHECK_I(pixel_multiplier);
  8788. PIPE_CONF_CHECK_I(has_hdmi_sink);
  8789. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  8790. IS_VALLEYVIEW(dev))
  8791. PIPE_CONF_CHECK_I(limited_color_range);
  8792. PIPE_CONF_CHECK_I(has_infoframe);
  8793. PIPE_CONF_CHECK_I(has_audio);
  8794. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8795. DRM_MODE_FLAG_INTERLACE);
  8796. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  8797. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8798. DRM_MODE_FLAG_PHSYNC);
  8799. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8800. DRM_MODE_FLAG_NHSYNC);
  8801. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8802. DRM_MODE_FLAG_PVSYNC);
  8803. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8804. DRM_MODE_FLAG_NVSYNC);
  8805. }
  8806. PIPE_CONF_CHECK_I(pipe_src_w);
  8807. PIPE_CONF_CHECK_I(pipe_src_h);
  8808. /*
  8809. * FIXME: BIOS likes to set up a cloned config with lvds+external
  8810. * screen. Since we don't yet re-compute the pipe config when moving
  8811. * just the lvds port away to another pipe the sw tracking won't match.
  8812. *
  8813. * Proper atomic modesets with recomputed global state will fix this.
  8814. * Until then just don't check gmch state for inherited modes.
  8815. */
  8816. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  8817. PIPE_CONF_CHECK_I(gmch_pfit.control);
  8818. /* pfit ratios are autocomputed by the hw on gen4+ */
  8819. if (INTEL_INFO(dev)->gen < 4)
  8820. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  8821. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  8822. }
  8823. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  8824. if (current_config->pch_pfit.enabled) {
  8825. PIPE_CONF_CHECK_I(pch_pfit.pos);
  8826. PIPE_CONF_CHECK_I(pch_pfit.size);
  8827. }
  8828. /* BDW+ don't expose a synchronous way to read the state */
  8829. if (IS_HASWELL(dev))
  8830. PIPE_CONF_CHECK_I(ips_enabled);
  8831. PIPE_CONF_CHECK_I(double_wide);
  8832. PIPE_CONF_CHECK_X(ddi_pll_sel);
  8833. PIPE_CONF_CHECK_I(shared_dpll);
  8834. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  8835. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  8836. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  8837. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  8838. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  8839. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  8840. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  8841. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  8842. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  8843. PIPE_CONF_CHECK_I(pipe_bpp);
  8844. PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
  8845. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  8846. #undef PIPE_CONF_CHECK_X
  8847. #undef PIPE_CONF_CHECK_I
  8848. #undef PIPE_CONF_CHECK_I_ALT
  8849. #undef PIPE_CONF_CHECK_FLAGS
  8850. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  8851. #undef PIPE_CONF_QUIRK
  8852. return true;
  8853. }
  8854. static void check_wm_state(struct drm_device *dev)
  8855. {
  8856. struct drm_i915_private *dev_priv = dev->dev_private;
  8857. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  8858. struct intel_crtc *intel_crtc;
  8859. int plane;
  8860. if (INTEL_INFO(dev)->gen < 9)
  8861. return;
  8862. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  8863. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  8864. for_each_intel_crtc(dev, intel_crtc) {
  8865. struct skl_ddb_entry *hw_entry, *sw_entry;
  8866. const enum pipe pipe = intel_crtc->pipe;
  8867. if (!intel_crtc->active)
  8868. continue;
  8869. /* planes */
  8870. for_each_plane(pipe, plane) {
  8871. hw_entry = &hw_ddb.plane[pipe][plane];
  8872. sw_entry = &sw_ddb->plane[pipe][plane];
  8873. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  8874. continue;
  8875. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  8876. "(expected (%u,%u), found (%u,%u))\n",
  8877. pipe_name(pipe), plane + 1,
  8878. sw_entry->start, sw_entry->end,
  8879. hw_entry->start, hw_entry->end);
  8880. }
  8881. /* cursor */
  8882. hw_entry = &hw_ddb.cursor[pipe];
  8883. sw_entry = &sw_ddb->cursor[pipe];
  8884. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  8885. continue;
  8886. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  8887. "(expected (%u,%u), found (%u,%u))\n",
  8888. pipe_name(pipe),
  8889. sw_entry->start, sw_entry->end,
  8890. hw_entry->start, hw_entry->end);
  8891. }
  8892. }
  8893. static void
  8894. check_connector_state(struct drm_device *dev)
  8895. {
  8896. struct intel_connector *connector;
  8897. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8898. base.head) {
  8899. /* This also checks the encoder/connector hw state with the
  8900. * ->get_hw_state callbacks. */
  8901. intel_connector_check_state(connector);
  8902. WARN(&connector->new_encoder->base != connector->base.encoder,
  8903. "connector's staged encoder doesn't match current encoder\n");
  8904. }
  8905. }
  8906. static void
  8907. check_encoder_state(struct drm_device *dev)
  8908. {
  8909. struct intel_encoder *encoder;
  8910. struct intel_connector *connector;
  8911. for_each_intel_encoder(dev, encoder) {
  8912. bool enabled = false;
  8913. bool active = false;
  8914. enum pipe pipe, tracked_pipe;
  8915. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  8916. encoder->base.base.id,
  8917. encoder->base.name);
  8918. WARN(&encoder->new_crtc->base != encoder->base.crtc,
  8919. "encoder's stage crtc doesn't match current crtc\n");
  8920. WARN(encoder->connectors_active && !encoder->base.crtc,
  8921. "encoder's active_connectors set, but no crtc\n");
  8922. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8923. base.head) {
  8924. if (connector->base.encoder != &encoder->base)
  8925. continue;
  8926. enabled = true;
  8927. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  8928. active = true;
  8929. }
  8930. /*
  8931. * for MST connectors if we unplug the connector is gone
  8932. * away but the encoder is still connected to a crtc
  8933. * until a modeset happens in response to the hotplug.
  8934. */
  8935. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  8936. continue;
  8937. WARN(!!encoder->base.crtc != enabled,
  8938. "encoder's enabled state mismatch "
  8939. "(expected %i, found %i)\n",
  8940. !!encoder->base.crtc, enabled);
  8941. WARN(active && !encoder->base.crtc,
  8942. "active encoder with no crtc\n");
  8943. WARN(encoder->connectors_active != active,
  8944. "encoder's computed active state doesn't match tracked active state "
  8945. "(expected %i, found %i)\n", active, encoder->connectors_active);
  8946. active = encoder->get_hw_state(encoder, &pipe);
  8947. WARN(active != encoder->connectors_active,
  8948. "encoder's hw state doesn't match sw tracking "
  8949. "(expected %i, found %i)\n",
  8950. encoder->connectors_active, active);
  8951. if (!encoder->base.crtc)
  8952. continue;
  8953. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  8954. WARN(active && pipe != tracked_pipe,
  8955. "active encoder's pipe doesn't match"
  8956. "(expected %i, found %i)\n",
  8957. tracked_pipe, pipe);
  8958. }
  8959. }
  8960. static void
  8961. check_crtc_state(struct drm_device *dev)
  8962. {
  8963. struct drm_i915_private *dev_priv = dev->dev_private;
  8964. struct intel_crtc *crtc;
  8965. struct intel_encoder *encoder;
  8966. struct intel_crtc_config pipe_config;
  8967. for_each_intel_crtc(dev, crtc) {
  8968. bool enabled = false;
  8969. bool active = false;
  8970. memset(&pipe_config, 0, sizeof(pipe_config));
  8971. DRM_DEBUG_KMS("[CRTC:%d]\n",
  8972. crtc->base.base.id);
  8973. WARN(crtc->active && !crtc->base.enabled,
  8974. "active crtc, but not enabled in sw tracking\n");
  8975. for_each_intel_encoder(dev, encoder) {
  8976. if (encoder->base.crtc != &crtc->base)
  8977. continue;
  8978. enabled = true;
  8979. if (encoder->connectors_active)
  8980. active = true;
  8981. }
  8982. WARN(active != crtc->active,
  8983. "crtc's computed active state doesn't match tracked active state "
  8984. "(expected %i, found %i)\n", active, crtc->active);
  8985. WARN(enabled != crtc->base.enabled,
  8986. "crtc's computed enabled state doesn't match tracked enabled state "
  8987. "(expected %i, found %i)\n", enabled, crtc->base.enabled);
  8988. active = dev_priv->display.get_pipe_config(crtc,
  8989. &pipe_config);
  8990. /* hw state is inconsistent with the pipe quirk */
  8991. if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  8992. (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  8993. active = crtc->active;
  8994. for_each_intel_encoder(dev, encoder) {
  8995. enum pipe pipe;
  8996. if (encoder->base.crtc != &crtc->base)
  8997. continue;
  8998. if (encoder->get_hw_state(encoder, &pipe))
  8999. encoder->get_config(encoder, &pipe_config);
  9000. }
  9001. WARN(crtc->active != active,
  9002. "crtc active state doesn't match with hw state "
  9003. "(expected %i, found %i)\n", crtc->active, active);
  9004. if (active &&
  9005. !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
  9006. WARN(1, "pipe state doesn't match!\n");
  9007. intel_dump_pipe_config(crtc, &pipe_config,
  9008. "[hw state]");
  9009. intel_dump_pipe_config(crtc, &crtc->config,
  9010. "[sw state]");
  9011. }
  9012. }
  9013. }
  9014. static void
  9015. check_shared_dpll_state(struct drm_device *dev)
  9016. {
  9017. struct drm_i915_private *dev_priv = dev->dev_private;
  9018. struct intel_crtc *crtc;
  9019. struct intel_dpll_hw_state dpll_hw_state;
  9020. int i;
  9021. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9022. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  9023. int enabled_crtcs = 0, active_crtcs = 0;
  9024. bool active;
  9025. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  9026. DRM_DEBUG_KMS("%s\n", pll->name);
  9027. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  9028. WARN(pll->active > hweight32(pll->config.crtc_mask),
  9029. "more active pll users than references: %i vs %i\n",
  9030. pll->active, hweight32(pll->config.crtc_mask));
  9031. WARN(pll->active && !pll->on,
  9032. "pll in active use but not on in sw tracking\n");
  9033. WARN(pll->on && !pll->active,
  9034. "pll in on but not on in use in sw tracking\n");
  9035. WARN(pll->on != active,
  9036. "pll on state mismatch (expected %i, found %i)\n",
  9037. pll->on, active);
  9038. for_each_intel_crtc(dev, crtc) {
  9039. if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
  9040. enabled_crtcs++;
  9041. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  9042. active_crtcs++;
  9043. }
  9044. WARN(pll->active != active_crtcs,
  9045. "pll active crtcs mismatch (expected %i, found %i)\n",
  9046. pll->active, active_crtcs);
  9047. WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  9048. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  9049. hweight32(pll->config.crtc_mask), enabled_crtcs);
  9050. WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  9051. sizeof(dpll_hw_state)),
  9052. "pll hw state mismatch\n");
  9053. }
  9054. }
  9055. void
  9056. intel_modeset_check_state(struct drm_device *dev)
  9057. {
  9058. check_wm_state(dev);
  9059. check_connector_state(dev);
  9060. check_encoder_state(dev);
  9061. check_crtc_state(dev);
  9062. check_shared_dpll_state(dev);
  9063. }
  9064. void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
  9065. int dotclock)
  9066. {
  9067. /*
  9068. * FDI already provided one idea for the dotclock.
  9069. * Yell if the encoder disagrees.
  9070. */
  9071. WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
  9072. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9073. pipe_config->adjusted_mode.crtc_clock, dotclock);
  9074. }
  9075. static void update_scanline_offset(struct intel_crtc *crtc)
  9076. {
  9077. struct drm_device *dev = crtc->base.dev;
  9078. /*
  9079. * The scanline counter increments at the leading edge of hsync.
  9080. *
  9081. * On most platforms it starts counting from vtotal-1 on the
  9082. * first active line. That means the scanline counter value is
  9083. * always one less than what we would expect. Ie. just after
  9084. * start of vblank, which also occurs at start of hsync (on the
  9085. * last active line), the scanline counter will read vblank_start-1.
  9086. *
  9087. * On gen2 the scanline counter starts counting from 1 instead
  9088. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  9089. * to keep the value positive), instead of adding one.
  9090. *
  9091. * On HSW+ the behaviour of the scanline counter depends on the output
  9092. * type. For DP ports it behaves like most other platforms, but on HDMI
  9093. * there's an extra 1 line difference. So we need to add two instead of
  9094. * one to the value.
  9095. */
  9096. if (IS_GEN2(dev)) {
  9097. const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
  9098. int vtotal;
  9099. vtotal = mode->crtc_vtotal;
  9100. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  9101. vtotal /= 2;
  9102. crtc->scanline_offset = vtotal - 1;
  9103. } else if (HAS_DDI(dev) &&
  9104. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  9105. crtc->scanline_offset = 2;
  9106. } else
  9107. crtc->scanline_offset = 1;
  9108. }
  9109. static struct intel_crtc_config *
  9110. intel_modeset_compute_config(struct drm_crtc *crtc,
  9111. struct drm_display_mode *mode,
  9112. struct drm_framebuffer *fb,
  9113. unsigned *modeset_pipes,
  9114. unsigned *prepare_pipes,
  9115. unsigned *disable_pipes)
  9116. {
  9117. struct intel_crtc_config *pipe_config = NULL;
  9118. intel_modeset_affected_pipes(crtc, modeset_pipes,
  9119. prepare_pipes, disable_pipes);
  9120. if ((*modeset_pipes) == 0)
  9121. goto out;
  9122. /*
  9123. * Note this needs changes when we start tracking multiple modes
  9124. * and crtcs. At that point we'll need to compute the whole config
  9125. * (i.e. one pipe_config for each crtc) rather than just the one
  9126. * for this crtc.
  9127. */
  9128. pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
  9129. if (IS_ERR(pipe_config)) {
  9130. goto out;
  9131. }
  9132. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  9133. "[modeset]");
  9134. out:
  9135. return pipe_config;
  9136. }
  9137. static int __intel_set_mode(struct drm_crtc *crtc,
  9138. struct drm_display_mode *mode,
  9139. int x, int y, struct drm_framebuffer *fb,
  9140. struct intel_crtc_config *pipe_config,
  9141. unsigned modeset_pipes,
  9142. unsigned prepare_pipes,
  9143. unsigned disable_pipes)
  9144. {
  9145. struct drm_device *dev = crtc->dev;
  9146. struct drm_i915_private *dev_priv = dev->dev_private;
  9147. struct drm_display_mode *saved_mode;
  9148. struct intel_crtc *intel_crtc;
  9149. int ret = 0;
  9150. saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
  9151. if (!saved_mode)
  9152. return -ENOMEM;
  9153. *saved_mode = crtc->mode;
  9154. if (modeset_pipes)
  9155. to_intel_crtc(crtc)->new_config = pipe_config;
  9156. /*
  9157. * See if the config requires any additional preparation, e.g.
  9158. * to adjust global state with pipes off. We need to do this
  9159. * here so we can get the modeset_pipe updated config for the new
  9160. * mode set on this crtc. For other crtcs we need to use the
  9161. * adjusted_mode bits in the crtc directly.
  9162. */
  9163. if (IS_VALLEYVIEW(dev)) {
  9164. valleyview_modeset_global_pipes(dev, &prepare_pipes);
  9165. /* may have added more to prepare_pipes than we should */
  9166. prepare_pipes &= ~disable_pipes;
  9167. }
  9168. if (dev_priv->display.crtc_compute_clock) {
  9169. unsigned clear_pipes = modeset_pipes | disable_pipes;
  9170. ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
  9171. if (ret)
  9172. goto done;
  9173. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9174. ret = dev_priv->display.crtc_compute_clock(intel_crtc);
  9175. if (ret) {
  9176. intel_shared_dpll_abort_config(dev_priv);
  9177. goto done;
  9178. }
  9179. }
  9180. }
  9181. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  9182. intel_crtc_disable(&intel_crtc->base);
  9183. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9184. if (intel_crtc->base.enabled)
  9185. dev_priv->display.crtc_disable(&intel_crtc->base);
  9186. }
  9187. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  9188. * to set it here already despite that we pass it down the callchain.
  9189. *
  9190. * Note we'll need to fix this up when we start tracking multiple
  9191. * pipes; here we assume a single modeset_pipe and only track the
  9192. * single crtc and mode.
  9193. */
  9194. if (modeset_pipes) {
  9195. crtc->mode = *mode;
  9196. /* mode_set/enable/disable functions rely on a correct pipe
  9197. * config. */
  9198. to_intel_crtc(crtc)->config = *pipe_config;
  9199. to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
  9200. /*
  9201. * Calculate and store various constants which
  9202. * are later needed by vblank and swap-completion
  9203. * timestamping. They are derived from true hwmode.
  9204. */
  9205. drm_calc_timestamping_constants(crtc,
  9206. &pipe_config->adjusted_mode);
  9207. }
  9208. /* Only after disabling all output pipelines that will be changed can we
  9209. * update the the output configuration. */
  9210. intel_modeset_update_state(dev, prepare_pipes);
  9211. modeset_update_crtc_power_domains(dev);
  9212. /* Set up the DPLL and any encoders state that needs to adjust or depend
  9213. * on the DPLL.
  9214. */
  9215. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9216. struct drm_plane *primary = intel_crtc->base.primary;
  9217. int vdisplay, hdisplay;
  9218. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  9219. ret = primary->funcs->update_plane(primary, &intel_crtc->base,
  9220. fb, 0, 0,
  9221. hdisplay, vdisplay,
  9222. x << 16, y << 16,
  9223. hdisplay << 16, vdisplay << 16);
  9224. }
  9225. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  9226. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9227. update_scanline_offset(intel_crtc);
  9228. dev_priv->display.crtc_enable(&intel_crtc->base);
  9229. }
  9230. /* FIXME: add subpixel order */
  9231. done:
  9232. if (ret && crtc->enabled)
  9233. crtc->mode = *saved_mode;
  9234. kfree(pipe_config);
  9235. kfree(saved_mode);
  9236. return ret;
  9237. }
  9238. static int intel_set_mode_pipes(struct drm_crtc *crtc,
  9239. struct drm_display_mode *mode,
  9240. int x, int y, struct drm_framebuffer *fb,
  9241. struct intel_crtc_config *pipe_config,
  9242. unsigned modeset_pipes,
  9243. unsigned prepare_pipes,
  9244. unsigned disable_pipes)
  9245. {
  9246. int ret;
  9247. ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
  9248. prepare_pipes, disable_pipes);
  9249. if (ret == 0)
  9250. intel_modeset_check_state(crtc->dev);
  9251. return ret;
  9252. }
  9253. static int intel_set_mode(struct drm_crtc *crtc,
  9254. struct drm_display_mode *mode,
  9255. int x, int y, struct drm_framebuffer *fb)
  9256. {
  9257. struct intel_crtc_config *pipe_config;
  9258. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  9259. pipe_config = intel_modeset_compute_config(crtc, mode, fb,
  9260. &modeset_pipes,
  9261. &prepare_pipes,
  9262. &disable_pipes);
  9263. if (IS_ERR(pipe_config))
  9264. return PTR_ERR(pipe_config);
  9265. return intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
  9266. modeset_pipes, prepare_pipes,
  9267. disable_pipes);
  9268. }
  9269. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  9270. {
  9271. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
  9272. }
  9273. #undef for_each_intel_crtc_masked
  9274. static void intel_set_config_free(struct intel_set_config *config)
  9275. {
  9276. if (!config)
  9277. return;
  9278. kfree(config->save_connector_encoders);
  9279. kfree(config->save_encoder_crtcs);
  9280. kfree(config->save_crtc_enabled);
  9281. kfree(config);
  9282. }
  9283. static int intel_set_config_save_state(struct drm_device *dev,
  9284. struct intel_set_config *config)
  9285. {
  9286. struct drm_crtc *crtc;
  9287. struct drm_encoder *encoder;
  9288. struct drm_connector *connector;
  9289. int count;
  9290. config->save_crtc_enabled =
  9291. kcalloc(dev->mode_config.num_crtc,
  9292. sizeof(bool), GFP_KERNEL);
  9293. if (!config->save_crtc_enabled)
  9294. return -ENOMEM;
  9295. config->save_encoder_crtcs =
  9296. kcalloc(dev->mode_config.num_encoder,
  9297. sizeof(struct drm_crtc *), GFP_KERNEL);
  9298. if (!config->save_encoder_crtcs)
  9299. return -ENOMEM;
  9300. config->save_connector_encoders =
  9301. kcalloc(dev->mode_config.num_connector,
  9302. sizeof(struct drm_encoder *), GFP_KERNEL);
  9303. if (!config->save_connector_encoders)
  9304. return -ENOMEM;
  9305. /* Copy data. Note that driver private data is not affected.
  9306. * Should anything bad happen only the expected state is
  9307. * restored, not the drivers personal bookkeeping.
  9308. */
  9309. count = 0;
  9310. for_each_crtc(dev, crtc) {
  9311. config->save_crtc_enabled[count++] = crtc->enabled;
  9312. }
  9313. count = 0;
  9314. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  9315. config->save_encoder_crtcs[count++] = encoder->crtc;
  9316. }
  9317. count = 0;
  9318. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  9319. config->save_connector_encoders[count++] = connector->encoder;
  9320. }
  9321. return 0;
  9322. }
  9323. static void intel_set_config_restore_state(struct drm_device *dev,
  9324. struct intel_set_config *config)
  9325. {
  9326. struct intel_crtc *crtc;
  9327. struct intel_encoder *encoder;
  9328. struct intel_connector *connector;
  9329. int count;
  9330. count = 0;
  9331. for_each_intel_crtc(dev, crtc) {
  9332. crtc->new_enabled = config->save_crtc_enabled[count++];
  9333. if (crtc->new_enabled)
  9334. crtc->new_config = &crtc->config;
  9335. else
  9336. crtc->new_config = NULL;
  9337. }
  9338. count = 0;
  9339. for_each_intel_encoder(dev, encoder) {
  9340. encoder->new_crtc =
  9341. to_intel_crtc(config->save_encoder_crtcs[count++]);
  9342. }
  9343. count = 0;
  9344. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  9345. connector->new_encoder =
  9346. to_intel_encoder(config->save_connector_encoders[count++]);
  9347. }
  9348. }
  9349. static bool
  9350. is_crtc_connector_off(struct drm_mode_set *set)
  9351. {
  9352. int i;
  9353. if (set->num_connectors == 0)
  9354. return false;
  9355. if (WARN_ON(set->connectors == NULL))
  9356. return false;
  9357. for (i = 0; i < set->num_connectors; i++)
  9358. if (set->connectors[i]->encoder &&
  9359. set->connectors[i]->encoder->crtc == set->crtc &&
  9360. set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
  9361. return true;
  9362. return false;
  9363. }
  9364. static void
  9365. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  9366. struct intel_set_config *config)
  9367. {
  9368. /* We should be able to check here if the fb has the same properties
  9369. * and then just flip_or_move it */
  9370. if (is_crtc_connector_off(set)) {
  9371. config->mode_changed = true;
  9372. } else if (set->crtc->primary->fb != set->fb) {
  9373. /*
  9374. * If we have no fb, we can only flip as long as the crtc is
  9375. * active, otherwise we need a full mode set. The crtc may
  9376. * be active if we've only disabled the primary plane, or
  9377. * in fastboot situations.
  9378. */
  9379. if (set->crtc->primary->fb == NULL) {
  9380. struct intel_crtc *intel_crtc =
  9381. to_intel_crtc(set->crtc);
  9382. if (intel_crtc->active) {
  9383. DRM_DEBUG_KMS("crtc has no fb, will flip\n");
  9384. config->fb_changed = true;
  9385. } else {
  9386. DRM_DEBUG_KMS("inactive crtc, full mode set\n");
  9387. config->mode_changed = true;
  9388. }
  9389. } else if (set->fb == NULL) {
  9390. config->mode_changed = true;
  9391. } else if (set->fb->pixel_format !=
  9392. set->crtc->primary->fb->pixel_format) {
  9393. config->mode_changed = true;
  9394. } else {
  9395. config->fb_changed = true;
  9396. }
  9397. }
  9398. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  9399. config->fb_changed = true;
  9400. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  9401. DRM_DEBUG_KMS("modes are different, full mode set\n");
  9402. drm_mode_debug_printmodeline(&set->crtc->mode);
  9403. drm_mode_debug_printmodeline(set->mode);
  9404. config->mode_changed = true;
  9405. }
  9406. DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
  9407. set->crtc->base.id, config->mode_changed, config->fb_changed);
  9408. }
  9409. static int
  9410. intel_modeset_stage_output_state(struct drm_device *dev,
  9411. struct drm_mode_set *set,
  9412. struct intel_set_config *config)
  9413. {
  9414. struct intel_connector *connector;
  9415. struct intel_encoder *encoder;
  9416. struct intel_crtc *crtc;
  9417. int ro;
  9418. /* The upper layers ensure that we either disable a crtc or have a list
  9419. * of connectors. For paranoia, double-check this. */
  9420. WARN_ON(!set->fb && (set->num_connectors != 0));
  9421. WARN_ON(set->fb && (set->num_connectors == 0));
  9422. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9423. base.head) {
  9424. /* Otherwise traverse passed in connector list and get encoders
  9425. * for them. */
  9426. for (ro = 0; ro < set->num_connectors; ro++) {
  9427. if (set->connectors[ro] == &connector->base) {
  9428. connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
  9429. break;
  9430. }
  9431. }
  9432. /* If we disable the crtc, disable all its connectors. Also, if
  9433. * the connector is on the changing crtc but not on the new
  9434. * connector list, disable it. */
  9435. if ((!set->fb || ro == set->num_connectors) &&
  9436. connector->base.encoder &&
  9437. connector->base.encoder->crtc == set->crtc) {
  9438. connector->new_encoder = NULL;
  9439. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  9440. connector->base.base.id,
  9441. connector->base.name);
  9442. }
  9443. if (&connector->new_encoder->base != connector->base.encoder) {
  9444. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  9445. config->mode_changed = true;
  9446. }
  9447. }
  9448. /* connector->new_encoder is now updated for all connectors. */
  9449. /* Update crtc of enabled connectors. */
  9450. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9451. base.head) {
  9452. struct drm_crtc *new_crtc;
  9453. if (!connector->new_encoder)
  9454. continue;
  9455. new_crtc = connector->new_encoder->base.crtc;
  9456. for (ro = 0; ro < set->num_connectors; ro++) {
  9457. if (set->connectors[ro] == &connector->base)
  9458. new_crtc = set->crtc;
  9459. }
  9460. /* Make sure the new CRTC will work with the encoder */
  9461. if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
  9462. new_crtc)) {
  9463. return -EINVAL;
  9464. }
  9465. connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
  9466. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  9467. connector->base.base.id,
  9468. connector->base.name,
  9469. new_crtc->base.id);
  9470. }
  9471. /* Check for any encoders that needs to be disabled. */
  9472. for_each_intel_encoder(dev, encoder) {
  9473. int num_connectors = 0;
  9474. list_for_each_entry(connector,
  9475. &dev->mode_config.connector_list,
  9476. base.head) {
  9477. if (connector->new_encoder == encoder) {
  9478. WARN_ON(!connector->new_encoder->new_crtc);
  9479. num_connectors++;
  9480. }
  9481. }
  9482. if (num_connectors == 0)
  9483. encoder->new_crtc = NULL;
  9484. else if (num_connectors > 1)
  9485. return -EINVAL;
  9486. /* Only now check for crtc changes so we don't miss encoders
  9487. * that will be disabled. */
  9488. if (&encoder->new_crtc->base != encoder->base.crtc) {
  9489. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  9490. config->mode_changed = true;
  9491. }
  9492. }
  9493. /* Now we've also updated encoder->new_crtc for all encoders. */
  9494. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9495. base.head) {
  9496. if (connector->new_encoder)
  9497. if (connector->new_encoder != connector->encoder)
  9498. connector->encoder = connector->new_encoder;
  9499. }
  9500. for_each_intel_crtc(dev, crtc) {
  9501. crtc->new_enabled = false;
  9502. for_each_intel_encoder(dev, encoder) {
  9503. if (encoder->new_crtc == crtc) {
  9504. crtc->new_enabled = true;
  9505. break;
  9506. }
  9507. }
  9508. if (crtc->new_enabled != crtc->base.enabled) {
  9509. DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
  9510. crtc->new_enabled ? "en" : "dis");
  9511. config->mode_changed = true;
  9512. }
  9513. if (crtc->new_enabled)
  9514. crtc->new_config = &crtc->config;
  9515. else
  9516. crtc->new_config = NULL;
  9517. }
  9518. return 0;
  9519. }
  9520. static void disable_crtc_nofb(struct intel_crtc *crtc)
  9521. {
  9522. struct drm_device *dev = crtc->base.dev;
  9523. struct intel_encoder *encoder;
  9524. struct intel_connector *connector;
  9525. DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
  9526. pipe_name(crtc->pipe));
  9527. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  9528. if (connector->new_encoder &&
  9529. connector->new_encoder->new_crtc == crtc)
  9530. connector->new_encoder = NULL;
  9531. }
  9532. for_each_intel_encoder(dev, encoder) {
  9533. if (encoder->new_crtc == crtc)
  9534. encoder->new_crtc = NULL;
  9535. }
  9536. crtc->new_enabled = false;
  9537. crtc->new_config = NULL;
  9538. }
  9539. static int intel_crtc_set_config(struct drm_mode_set *set)
  9540. {
  9541. struct drm_device *dev;
  9542. struct drm_mode_set save_set;
  9543. struct intel_set_config *config;
  9544. struct intel_crtc_config *pipe_config;
  9545. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  9546. int ret;
  9547. BUG_ON(!set);
  9548. BUG_ON(!set->crtc);
  9549. BUG_ON(!set->crtc->helper_private);
  9550. /* Enforce sane interface api - has been abused by the fb helper. */
  9551. BUG_ON(!set->mode && set->fb);
  9552. BUG_ON(set->fb && set->num_connectors == 0);
  9553. if (set->fb) {
  9554. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  9555. set->crtc->base.id, set->fb->base.id,
  9556. (int)set->num_connectors, set->x, set->y);
  9557. } else {
  9558. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  9559. }
  9560. dev = set->crtc->dev;
  9561. ret = -ENOMEM;
  9562. config = kzalloc(sizeof(*config), GFP_KERNEL);
  9563. if (!config)
  9564. goto out_config;
  9565. ret = intel_set_config_save_state(dev, config);
  9566. if (ret)
  9567. goto out_config;
  9568. save_set.crtc = set->crtc;
  9569. save_set.mode = &set->crtc->mode;
  9570. save_set.x = set->crtc->x;
  9571. save_set.y = set->crtc->y;
  9572. save_set.fb = set->crtc->primary->fb;
  9573. /* Compute whether we need a full modeset, only an fb base update or no
  9574. * change at all. In the future we might also check whether only the
  9575. * mode changed, e.g. for LVDS where we only change the panel fitter in
  9576. * such cases. */
  9577. intel_set_config_compute_mode_changes(set, config);
  9578. ret = intel_modeset_stage_output_state(dev, set, config);
  9579. if (ret)
  9580. goto fail;
  9581. pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
  9582. set->fb,
  9583. &modeset_pipes,
  9584. &prepare_pipes,
  9585. &disable_pipes);
  9586. if (IS_ERR(pipe_config)) {
  9587. ret = PTR_ERR(pipe_config);
  9588. goto fail;
  9589. } else if (pipe_config) {
  9590. if (pipe_config->has_audio !=
  9591. to_intel_crtc(set->crtc)->config.has_audio)
  9592. config->mode_changed = true;
  9593. /* Force mode sets for any infoframe stuff */
  9594. if (pipe_config->has_infoframe ||
  9595. to_intel_crtc(set->crtc)->config.has_infoframe)
  9596. config->mode_changed = true;
  9597. }
  9598. /* set_mode will free it in the mode_changed case */
  9599. if (!config->mode_changed)
  9600. kfree(pipe_config);
  9601. intel_update_pipe_size(to_intel_crtc(set->crtc));
  9602. if (config->mode_changed) {
  9603. ret = intel_set_mode_pipes(set->crtc, set->mode,
  9604. set->x, set->y, set->fb, pipe_config,
  9605. modeset_pipes, prepare_pipes,
  9606. disable_pipes);
  9607. } else if (config->fb_changed) {
  9608. struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
  9609. struct drm_plane *primary = set->crtc->primary;
  9610. int vdisplay, hdisplay;
  9611. drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
  9612. ret = primary->funcs->update_plane(primary, set->crtc, set->fb,
  9613. 0, 0, hdisplay, vdisplay,
  9614. set->x << 16, set->y << 16,
  9615. hdisplay << 16, vdisplay << 16);
  9616. /*
  9617. * We need to make sure the primary plane is re-enabled if it
  9618. * has previously been turned off.
  9619. */
  9620. if (!intel_crtc->primary_enabled && ret == 0) {
  9621. WARN_ON(!intel_crtc->active);
  9622. intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
  9623. }
  9624. /*
  9625. * In the fastboot case this may be our only check of the
  9626. * state after boot. It would be better to only do it on
  9627. * the first update, but we don't have a nice way of doing that
  9628. * (and really, set_config isn't used much for high freq page
  9629. * flipping, so increasing its cost here shouldn't be a big
  9630. * deal).
  9631. */
  9632. if (i915.fastboot && ret == 0)
  9633. intel_modeset_check_state(set->crtc->dev);
  9634. }
  9635. if (ret) {
  9636. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  9637. set->crtc->base.id, ret);
  9638. fail:
  9639. intel_set_config_restore_state(dev, config);
  9640. /*
  9641. * HACK: if the pipe was on, but we didn't have a framebuffer,
  9642. * force the pipe off to avoid oopsing in the modeset code
  9643. * due to fb==NULL. This should only happen during boot since
  9644. * we don't yet reconstruct the FB from the hardware state.
  9645. */
  9646. if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
  9647. disable_crtc_nofb(to_intel_crtc(save_set.crtc));
  9648. /* Try to restore the config */
  9649. if (config->mode_changed &&
  9650. intel_set_mode(save_set.crtc, save_set.mode,
  9651. save_set.x, save_set.y, save_set.fb))
  9652. DRM_ERROR("failed to restore config after modeset failure\n");
  9653. }
  9654. out_config:
  9655. intel_set_config_free(config);
  9656. return ret;
  9657. }
  9658. static const struct drm_crtc_funcs intel_crtc_funcs = {
  9659. .gamma_set = intel_crtc_gamma_set,
  9660. .set_config = intel_crtc_set_config,
  9661. .destroy = intel_crtc_destroy,
  9662. .page_flip = intel_crtc_page_flip,
  9663. };
  9664. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  9665. struct intel_shared_dpll *pll,
  9666. struct intel_dpll_hw_state *hw_state)
  9667. {
  9668. uint32_t val;
  9669. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  9670. return false;
  9671. val = I915_READ(PCH_DPLL(pll->id));
  9672. hw_state->dpll = val;
  9673. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  9674. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  9675. return val & DPLL_VCO_ENABLE;
  9676. }
  9677. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  9678. struct intel_shared_dpll *pll)
  9679. {
  9680. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  9681. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  9682. }
  9683. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  9684. struct intel_shared_dpll *pll)
  9685. {
  9686. /* PCH refclock must be enabled first */
  9687. ibx_assert_pch_refclk_enabled(dev_priv);
  9688. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  9689. /* Wait for the clocks to stabilize. */
  9690. POSTING_READ(PCH_DPLL(pll->id));
  9691. udelay(150);
  9692. /* The pixel multiplier can only be updated once the
  9693. * DPLL is enabled and the clocks are stable.
  9694. *
  9695. * So write it again.
  9696. */
  9697. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  9698. POSTING_READ(PCH_DPLL(pll->id));
  9699. udelay(200);
  9700. }
  9701. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  9702. struct intel_shared_dpll *pll)
  9703. {
  9704. struct drm_device *dev = dev_priv->dev;
  9705. struct intel_crtc *crtc;
  9706. /* Make sure no transcoder isn't still depending on us. */
  9707. for_each_intel_crtc(dev, crtc) {
  9708. if (intel_crtc_to_shared_dpll(crtc) == pll)
  9709. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  9710. }
  9711. I915_WRITE(PCH_DPLL(pll->id), 0);
  9712. POSTING_READ(PCH_DPLL(pll->id));
  9713. udelay(200);
  9714. }
  9715. static char *ibx_pch_dpll_names[] = {
  9716. "PCH DPLL A",
  9717. "PCH DPLL B",
  9718. };
  9719. static void ibx_pch_dpll_init(struct drm_device *dev)
  9720. {
  9721. struct drm_i915_private *dev_priv = dev->dev_private;
  9722. int i;
  9723. dev_priv->num_shared_dpll = 2;
  9724. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9725. dev_priv->shared_dplls[i].id = i;
  9726. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  9727. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  9728. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  9729. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  9730. dev_priv->shared_dplls[i].get_hw_state =
  9731. ibx_pch_dpll_get_hw_state;
  9732. }
  9733. }
  9734. static void intel_shared_dpll_init(struct drm_device *dev)
  9735. {
  9736. struct drm_i915_private *dev_priv = dev->dev_private;
  9737. if (HAS_DDI(dev))
  9738. intel_ddi_pll_init(dev);
  9739. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  9740. ibx_pch_dpll_init(dev);
  9741. else
  9742. dev_priv->num_shared_dpll = 0;
  9743. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  9744. }
  9745. static int
  9746. intel_primary_plane_disable(struct drm_plane *plane)
  9747. {
  9748. struct drm_device *dev = plane->dev;
  9749. struct intel_crtc *intel_crtc;
  9750. if (!plane->fb)
  9751. return 0;
  9752. BUG_ON(!plane->crtc);
  9753. intel_crtc = to_intel_crtc(plane->crtc);
  9754. /*
  9755. * Even though we checked plane->fb above, it's still possible that
  9756. * the primary plane has been implicitly disabled because the crtc
  9757. * coordinates given weren't visible, or because we detected
  9758. * that it was 100% covered by a sprite plane. Or, the CRTC may be
  9759. * off and we've set a fb, but haven't actually turned on the CRTC yet.
  9760. * In either case, we need to unpin the FB and let the fb pointer get
  9761. * updated, but otherwise we don't need to touch the hardware.
  9762. */
  9763. if (intel_crtc->primary_enabled) {
  9764. intel_crtc_wait_for_pending_flips(plane->crtc);
  9765. intel_disable_primary_hw_plane(plane, plane->crtc);
  9766. }
  9767. mutex_lock(&dev->struct_mutex);
  9768. i915_gem_track_fb(intel_fb_obj(plane->fb), NULL,
  9769. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
  9770. intel_unpin_fb_obj(intel_fb_obj(plane->fb));
  9771. mutex_unlock(&dev->struct_mutex);
  9772. plane->fb = NULL;
  9773. return 0;
  9774. }
  9775. /**
  9776. * intel_prepare_plane_fb - Prepare fb for usage on plane
  9777. * @plane: drm plane to prepare for
  9778. * @fb: framebuffer to prepare for presentation
  9779. *
  9780. * Prepares a framebuffer for usage on a display plane. Generally this
  9781. * involves pinning the underlying object and updating the frontbuffer tracking
  9782. * bits. Some older platforms need special physical address handling for
  9783. * cursor planes.
  9784. *
  9785. * Returns 0 on success, negative error code on failure.
  9786. */
  9787. int
  9788. intel_prepare_plane_fb(struct drm_plane *plane,
  9789. struct drm_framebuffer *fb)
  9790. {
  9791. struct drm_device *dev = plane->dev;
  9792. struct intel_plane *intel_plane = to_intel_plane(plane);
  9793. enum pipe pipe = intel_plane->pipe;
  9794. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9795. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  9796. unsigned frontbuffer_bits = 0;
  9797. int ret = 0;
  9798. if (WARN_ON(fb == plane->fb || !obj))
  9799. return 0;
  9800. switch (plane->type) {
  9801. case DRM_PLANE_TYPE_PRIMARY:
  9802. frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
  9803. break;
  9804. case DRM_PLANE_TYPE_CURSOR:
  9805. frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
  9806. break;
  9807. case DRM_PLANE_TYPE_OVERLAY:
  9808. frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
  9809. break;
  9810. }
  9811. mutex_lock(&dev->struct_mutex);
  9812. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  9813. INTEL_INFO(dev)->cursor_needs_physical) {
  9814. int align = IS_I830(dev) ? 16 * 1024 : 256;
  9815. ret = i915_gem_object_attach_phys(obj, align);
  9816. if (ret)
  9817. DRM_DEBUG_KMS("failed to attach phys object\n");
  9818. } else {
  9819. ret = intel_pin_and_fence_fb_obj(plane, fb, NULL);
  9820. }
  9821. if (ret == 0)
  9822. i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
  9823. mutex_unlock(&dev->struct_mutex);
  9824. return ret;
  9825. }
  9826. /**
  9827. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  9828. * @plane: drm plane to clean up for
  9829. * @fb: old framebuffer that was on plane
  9830. *
  9831. * Cleans up a framebuffer that has just been removed from a plane.
  9832. */
  9833. void
  9834. intel_cleanup_plane_fb(struct drm_plane *plane,
  9835. struct drm_framebuffer *fb)
  9836. {
  9837. struct drm_device *dev = plane->dev;
  9838. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9839. if (WARN_ON(!obj))
  9840. return;
  9841. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  9842. !INTEL_INFO(dev)->cursor_needs_physical) {
  9843. mutex_lock(&dev->struct_mutex);
  9844. intel_unpin_fb_obj(obj);
  9845. mutex_unlock(&dev->struct_mutex);
  9846. }
  9847. }
  9848. static int
  9849. intel_check_primary_plane(struct drm_plane *plane,
  9850. struct intel_plane_state *state)
  9851. {
  9852. struct drm_crtc *crtc = state->base.crtc;
  9853. struct drm_framebuffer *fb = state->base.fb;
  9854. struct drm_rect *dest = &state->dst;
  9855. struct drm_rect *src = &state->src;
  9856. const struct drm_rect *clip = &state->clip;
  9857. int ret;
  9858. ret = drm_plane_helper_check_update(plane, crtc, fb,
  9859. src, dest, clip,
  9860. DRM_PLANE_HELPER_NO_SCALING,
  9861. DRM_PLANE_HELPER_NO_SCALING,
  9862. false, true, &state->visible);
  9863. if (ret)
  9864. return ret;
  9865. intel_crtc_wait_for_pending_flips(crtc);
  9866. if (intel_crtc_has_pending_flip(crtc)) {
  9867. DRM_ERROR("pipe is still busy with an old pageflip\n");
  9868. return -EBUSY;
  9869. }
  9870. return 0;
  9871. }
  9872. static void
  9873. intel_commit_primary_plane(struct drm_plane *plane,
  9874. struct intel_plane_state *state)
  9875. {
  9876. struct drm_crtc *crtc = state->base.crtc;
  9877. struct drm_framebuffer *fb = state->base.fb;
  9878. struct drm_device *dev = plane->dev;
  9879. struct drm_i915_private *dev_priv = dev->dev_private;
  9880. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9881. enum pipe pipe = intel_crtc->pipe;
  9882. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9883. struct intel_plane *intel_plane = to_intel_plane(plane);
  9884. struct drm_rect *src = &state->src;
  9885. crtc->primary->fb = fb;
  9886. crtc->x = src->x1 >> 16;
  9887. crtc->y = src->y1 >> 16;
  9888. intel_plane->crtc_x = state->orig_dst.x1;
  9889. intel_plane->crtc_y = state->orig_dst.y1;
  9890. intel_plane->crtc_w = drm_rect_width(&state->orig_dst);
  9891. intel_plane->crtc_h = drm_rect_height(&state->orig_dst);
  9892. intel_plane->src_x = state->orig_src.x1;
  9893. intel_plane->src_y = state->orig_src.y1;
  9894. intel_plane->src_w = drm_rect_width(&state->orig_src);
  9895. intel_plane->src_h = drm_rect_height(&state->orig_src);
  9896. intel_plane->obj = obj;
  9897. if (intel_crtc->active) {
  9898. /*
  9899. * FBC does not work on some platforms for rotated
  9900. * planes, so disable it when rotation is not 0 and
  9901. * update it when rotation is set back to 0.
  9902. *
  9903. * FIXME: This is redundant with the fbc update done in
  9904. * the primary plane enable function except that that
  9905. * one is done too late. We eventually need to unify
  9906. * this.
  9907. */
  9908. if (intel_crtc->primary_enabled &&
  9909. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  9910. dev_priv->fbc.plane == intel_crtc->plane &&
  9911. intel_plane->rotation != BIT(DRM_ROTATE_0)) {
  9912. intel_disable_fbc(dev);
  9913. }
  9914. if (state->visible) {
  9915. bool was_enabled = intel_crtc->primary_enabled;
  9916. /* FIXME: kill this fastboot hack */
  9917. intel_update_pipe_size(intel_crtc);
  9918. intel_crtc->primary_enabled = true;
  9919. dev_priv->display.update_primary_plane(crtc, plane->fb,
  9920. crtc->x, crtc->y);
  9921. /*
  9922. * BDW signals flip done immediately if the plane
  9923. * is disabled, even if the plane enable is already
  9924. * armed to occur at the next vblank :(
  9925. */
  9926. if (IS_BROADWELL(dev) && !was_enabled)
  9927. intel_wait_for_vblank(dev, intel_crtc->pipe);
  9928. } else {
  9929. /*
  9930. * If clipping results in a non-visible primary plane,
  9931. * we'll disable the primary plane. Note that this is
  9932. * a bit different than what happens if userspace
  9933. * explicitly disables the plane by passing fb=0
  9934. * because plane->fb still gets set and pinned.
  9935. */
  9936. intel_disable_primary_hw_plane(plane, crtc);
  9937. }
  9938. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  9939. mutex_lock(&dev->struct_mutex);
  9940. intel_update_fbc(dev);
  9941. mutex_unlock(&dev->struct_mutex);
  9942. }
  9943. }
  9944. int
  9945. intel_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
  9946. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  9947. unsigned int crtc_w, unsigned int crtc_h,
  9948. uint32_t src_x, uint32_t src_y,
  9949. uint32_t src_w, uint32_t src_h)
  9950. {
  9951. struct drm_device *dev = plane->dev;
  9952. struct drm_framebuffer *old_fb = plane->fb;
  9953. struct intel_plane_state state;
  9954. struct intel_plane *intel_plane = to_intel_plane(plane);
  9955. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9956. int ret;
  9957. state.base.crtc = crtc;
  9958. state.base.fb = fb;
  9959. /* sample coordinates in 16.16 fixed point */
  9960. state.src.x1 = src_x;
  9961. state.src.x2 = src_x + src_w;
  9962. state.src.y1 = src_y;
  9963. state.src.y2 = src_y + src_h;
  9964. /* integer pixels */
  9965. state.dst.x1 = crtc_x;
  9966. state.dst.x2 = crtc_x + crtc_w;
  9967. state.dst.y1 = crtc_y;
  9968. state.dst.y2 = crtc_y + crtc_h;
  9969. state.clip.x1 = 0;
  9970. state.clip.y1 = 0;
  9971. state.clip.x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0;
  9972. state.clip.y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0;
  9973. state.orig_src = state.src;
  9974. state.orig_dst = state.dst;
  9975. ret = intel_plane->check_plane(plane, &state);
  9976. if (ret)
  9977. return ret;
  9978. if (fb != old_fb && fb) {
  9979. ret = intel_prepare_plane_fb(plane, fb);
  9980. if (ret)
  9981. return ret;
  9982. }
  9983. intel_plane->commit_plane(plane, &state);
  9984. if (fb != old_fb && old_fb) {
  9985. if (intel_crtc->active)
  9986. intel_wait_for_vblank(dev, intel_crtc->pipe);
  9987. intel_cleanup_plane_fb(plane, old_fb);
  9988. }
  9989. plane->fb = fb;
  9990. return 0;
  9991. }
  9992. /* Common destruction function for both primary and cursor planes */
  9993. static void intel_plane_destroy(struct drm_plane *plane)
  9994. {
  9995. struct intel_plane *intel_plane = to_intel_plane(plane);
  9996. drm_plane_cleanup(plane);
  9997. kfree(intel_plane);
  9998. }
  9999. static const struct drm_plane_funcs intel_primary_plane_funcs = {
  10000. .update_plane = intel_update_plane,
  10001. .disable_plane = intel_primary_plane_disable,
  10002. .destroy = intel_plane_destroy,
  10003. .set_property = intel_plane_set_property
  10004. };
  10005. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  10006. int pipe)
  10007. {
  10008. struct intel_plane *primary;
  10009. const uint32_t *intel_primary_formats;
  10010. int num_formats;
  10011. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  10012. if (primary == NULL)
  10013. return NULL;
  10014. primary->can_scale = false;
  10015. primary->max_downscale = 1;
  10016. primary->pipe = pipe;
  10017. primary->plane = pipe;
  10018. primary->rotation = BIT(DRM_ROTATE_0);
  10019. primary->check_plane = intel_check_primary_plane;
  10020. primary->commit_plane = intel_commit_primary_plane;
  10021. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  10022. primary->plane = !pipe;
  10023. if (INTEL_INFO(dev)->gen <= 3) {
  10024. intel_primary_formats = intel_primary_formats_gen2;
  10025. num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
  10026. } else {
  10027. intel_primary_formats = intel_primary_formats_gen4;
  10028. num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
  10029. }
  10030. drm_universal_plane_init(dev, &primary->base, 0,
  10031. &intel_primary_plane_funcs,
  10032. intel_primary_formats, num_formats,
  10033. DRM_PLANE_TYPE_PRIMARY);
  10034. if (INTEL_INFO(dev)->gen >= 4) {
  10035. if (!dev->mode_config.rotation_property)
  10036. dev->mode_config.rotation_property =
  10037. drm_mode_create_rotation_property(dev,
  10038. BIT(DRM_ROTATE_0) |
  10039. BIT(DRM_ROTATE_180));
  10040. if (dev->mode_config.rotation_property)
  10041. drm_object_attach_property(&primary->base.base,
  10042. dev->mode_config.rotation_property,
  10043. primary->rotation);
  10044. }
  10045. return &primary->base;
  10046. }
  10047. static int
  10048. intel_cursor_plane_disable(struct drm_plane *plane)
  10049. {
  10050. if (!plane->fb)
  10051. return 0;
  10052. BUG_ON(!plane->crtc);
  10053. return plane->funcs->update_plane(plane, plane->crtc, NULL,
  10054. 0, 0, 0, 0, 0, 0, 0, 0);
  10055. }
  10056. static int
  10057. intel_check_cursor_plane(struct drm_plane *plane,
  10058. struct intel_plane_state *state)
  10059. {
  10060. struct drm_crtc *crtc = state->base.crtc;
  10061. struct drm_device *dev = crtc->dev;
  10062. struct drm_framebuffer *fb = state->base.fb;
  10063. struct drm_rect *dest = &state->dst;
  10064. struct drm_rect *src = &state->src;
  10065. const struct drm_rect *clip = &state->clip;
  10066. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10067. int crtc_w, crtc_h;
  10068. unsigned stride;
  10069. int ret;
  10070. ret = drm_plane_helper_check_update(plane, crtc, fb,
  10071. src, dest, clip,
  10072. DRM_PLANE_HELPER_NO_SCALING,
  10073. DRM_PLANE_HELPER_NO_SCALING,
  10074. true, true, &state->visible);
  10075. if (ret)
  10076. return ret;
  10077. /* if we want to turn off the cursor ignore width and height */
  10078. if (!obj)
  10079. return 0;
  10080. /* Check for which cursor types we support */
  10081. crtc_w = drm_rect_width(&state->orig_dst);
  10082. crtc_h = drm_rect_height(&state->orig_dst);
  10083. if (!cursor_size_ok(dev, crtc_w, crtc_h)) {
  10084. DRM_DEBUG("Cursor dimension not supported\n");
  10085. return -EINVAL;
  10086. }
  10087. stride = roundup_pow_of_two(crtc_w) * 4;
  10088. if (obj->base.size < stride * crtc_h) {
  10089. DRM_DEBUG_KMS("buffer is too small\n");
  10090. return -ENOMEM;
  10091. }
  10092. if (fb == crtc->cursor->fb)
  10093. return 0;
  10094. /* we only need to pin inside GTT if cursor is non-phy */
  10095. mutex_lock(&dev->struct_mutex);
  10096. if (!INTEL_INFO(dev)->cursor_needs_physical && obj->tiling_mode) {
  10097. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  10098. ret = -EINVAL;
  10099. }
  10100. mutex_unlock(&dev->struct_mutex);
  10101. return ret;
  10102. }
  10103. static void
  10104. intel_commit_cursor_plane(struct drm_plane *plane,
  10105. struct intel_plane_state *state)
  10106. {
  10107. struct drm_crtc *crtc = state->base.crtc;
  10108. struct drm_device *dev = crtc->dev;
  10109. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10110. struct intel_plane *intel_plane = to_intel_plane(plane);
  10111. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  10112. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  10113. enum pipe pipe = intel_crtc->pipe;
  10114. unsigned old_width;
  10115. uint32_t addr;
  10116. plane->fb = state->base.fb;
  10117. crtc->cursor_x = state->orig_dst.x1;
  10118. crtc->cursor_y = state->orig_dst.y1;
  10119. intel_plane->crtc_x = state->orig_dst.x1;
  10120. intel_plane->crtc_y = state->orig_dst.y1;
  10121. intel_plane->crtc_w = drm_rect_width(&state->orig_dst);
  10122. intel_plane->crtc_h = drm_rect_height(&state->orig_dst);
  10123. intel_plane->src_x = state->orig_src.x1;
  10124. intel_plane->src_y = state->orig_src.y1;
  10125. intel_plane->src_w = drm_rect_width(&state->orig_src);
  10126. intel_plane->src_h = drm_rect_height(&state->orig_src);
  10127. intel_plane->obj = obj;
  10128. if (intel_crtc->cursor_bo == obj)
  10129. goto update;
  10130. /*
  10131. * 'prepare' is only called when fb != NULL; we still need to update
  10132. * frontbuffer tracking for the 'disable' case here.
  10133. */
  10134. if (!obj) {
  10135. mutex_lock(&dev->struct_mutex);
  10136. i915_gem_track_fb(old_obj, NULL,
  10137. INTEL_FRONTBUFFER_CURSOR(pipe));
  10138. mutex_unlock(&dev->struct_mutex);
  10139. }
  10140. if (!obj)
  10141. addr = 0;
  10142. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  10143. addr = i915_gem_obj_ggtt_offset(obj);
  10144. else
  10145. addr = obj->phys_handle->busaddr;
  10146. intel_crtc->cursor_addr = addr;
  10147. intel_crtc->cursor_bo = obj;
  10148. update:
  10149. old_width = intel_crtc->cursor_width;
  10150. intel_crtc->cursor_width = drm_rect_width(&state->orig_dst);
  10151. intel_crtc->cursor_height = drm_rect_height(&state->orig_dst);
  10152. if (intel_crtc->active) {
  10153. if (old_width != intel_crtc->cursor_width)
  10154. intel_update_watermarks(crtc);
  10155. intel_crtc_update_cursor(crtc, state->visible);
  10156. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_CURSOR(pipe));
  10157. }
  10158. }
  10159. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  10160. .update_plane = intel_update_plane,
  10161. .disable_plane = intel_cursor_plane_disable,
  10162. .destroy = intel_plane_destroy,
  10163. .set_property = intel_plane_set_property,
  10164. };
  10165. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  10166. int pipe)
  10167. {
  10168. struct intel_plane *cursor;
  10169. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  10170. if (cursor == NULL)
  10171. return NULL;
  10172. cursor->can_scale = false;
  10173. cursor->max_downscale = 1;
  10174. cursor->pipe = pipe;
  10175. cursor->plane = pipe;
  10176. cursor->rotation = BIT(DRM_ROTATE_0);
  10177. cursor->check_plane = intel_check_cursor_plane;
  10178. cursor->commit_plane = intel_commit_cursor_plane;
  10179. drm_universal_plane_init(dev, &cursor->base, 0,
  10180. &intel_cursor_plane_funcs,
  10181. intel_cursor_formats,
  10182. ARRAY_SIZE(intel_cursor_formats),
  10183. DRM_PLANE_TYPE_CURSOR);
  10184. if (INTEL_INFO(dev)->gen >= 4) {
  10185. if (!dev->mode_config.rotation_property)
  10186. dev->mode_config.rotation_property =
  10187. drm_mode_create_rotation_property(dev,
  10188. BIT(DRM_ROTATE_0) |
  10189. BIT(DRM_ROTATE_180));
  10190. if (dev->mode_config.rotation_property)
  10191. drm_object_attach_property(&cursor->base.base,
  10192. dev->mode_config.rotation_property,
  10193. cursor->rotation);
  10194. }
  10195. return &cursor->base;
  10196. }
  10197. static void intel_crtc_init(struct drm_device *dev, int pipe)
  10198. {
  10199. struct drm_i915_private *dev_priv = dev->dev_private;
  10200. struct intel_crtc *intel_crtc;
  10201. struct drm_plane *primary = NULL;
  10202. struct drm_plane *cursor = NULL;
  10203. int i, ret;
  10204. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  10205. if (intel_crtc == NULL)
  10206. return;
  10207. primary = intel_primary_plane_create(dev, pipe);
  10208. if (!primary)
  10209. goto fail;
  10210. cursor = intel_cursor_plane_create(dev, pipe);
  10211. if (!cursor)
  10212. goto fail;
  10213. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  10214. cursor, &intel_crtc_funcs);
  10215. if (ret)
  10216. goto fail;
  10217. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  10218. for (i = 0; i < 256; i++) {
  10219. intel_crtc->lut_r[i] = i;
  10220. intel_crtc->lut_g[i] = i;
  10221. intel_crtc->lut_b[i] = i;
  10222. }
  10223. /*
  10224. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  10225. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  10226. */
  10227. intel_crtc->pipe = pipe;
  10228. intel_crtc->plane = pipe;
  10229. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  10230. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  10231. intel_crtc->plane = !pipe;
  10232. }
  10233. intel_crtc->cursor_base = ~0;
  10234. intel_crtc->cursor_cntl = ~0;
  10235. intel_crtc->cursor_size = ~0;
  10236. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  10237. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  10238. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  10239. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  10240. INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
  10241. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  10242. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  10243. return;
  10244. fail:
  10245. if (primary)
  10246. drm_plane_cleanup(primary);
  10247. if (cursor)
  10248. drm_plane_cleanup(cursor);
  10249. kfree(intel_crtc);
  10250. }
  10251. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  10252. {
  10253. struct drm_encoder *encoder = connector->base.encoder;
  10254. struct drm_device *dev = connector->base.dev;
  10255. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  10256. if (!encoder || WARN_ON(!encoder->crtc))
  10257. return INVALID_PIPE;
  10258. return to_intel_crtc(encoder->crtc)->pipe;
  10259. }
  10260. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  10261. struct drm_file *file)
  10262. {
  10263. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  10264. struct drm_crtc *drmmode_crtc;
  10265. struct intel_crtc *crtc;
  10266. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  10267. return -ENODEV;
  10268. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  10269. if (!drmmode_crtc) {
  10270. DRM_ERROR("no such CRTC id\n");
  10271. return -ENOENT;
  10272. }
  10273. crtc = to_intel_crtc(drmmode_crtc);
  10274. pipe_from_crtc_id->pipe = crtc->pipe;
  10275. return 0;
  10276. }
  10277. static int intel_encoder_clones(struct intel_encoder *encoder)
  10278. {
  10279. struct drm_device *dev = encoder->base.dev;
  10280. struct intel_encoder *source_encoder;
  10281. int index_mask = 0;
  10282. int entry = 0;
  10283. for_each_intel_encoder(dev, source_encoder) {
  10284. if (encoders_cloneable(encoder, source_encoder))
  10285. index_mask |= (1 << entry);
  10286. entry++;
  10287. }
  10288. return index_mask;
  10289. }
  10290. static bool has_edp_a(struct drm_device *dev)
  10291. {
  10292. struct drm_i915_private *dev_priv = dev->dev_private;
  10293. if (!IS_MOBILE(dev))
  10294. return false;
  10295. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  10296. return false;
  10297. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  10298. return false;
  10299. return true;
  10300. }
  10301. const char *intel_output_name(int output)
  10302. {
  10303. static const char *names[] = {
  10304. [INTEL_OUTPUT_UNUSED] = "Unused",
  10305. [INTEL_OUTPUT_ANALOG] = "Analog",
  10306. [INTEL_OUTPUT_DVO] = "DVO",
  10307. [INTEL_OUTPUT_SDVO] = "SDVO",
  10308. [INTEL_OUTPUT_LVDS] = "LVDS",
  10309. [INTEL_OUTPUT_TVOUT] = "TV",
  10310. [INTEL_OUTPUT_HDMI] = "HDMI",
  10311. [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
  10312. [INTEL_OUTPUT_EDP] = "eDP",
  10313. [INTEL_OUTPUT_DSI] = "DSI",
  10314. [INTEL_OUTPUT_UNKNOWN] = "Unknown",
  10315. };
  10316. if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
  10317. return "Invalid";
  10318. return names[output];
  10319. }
  10320. static bool intel_crt_present(struct drm_device *dev)
  10321. {
  10322. struct drm_i915_private *dev_priv = dev->dev_private;
  10323. if (INTEL_INFO(dev)->gen >= 9)
  10324. return false;
  10325. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  10326. return false;
  10327. if (IS_CHERRYVIEW(dev))
  10328. return false;
  10329. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  10330. return false;
  10331. return true;
  10332. }
  10333. static void intel_setup_outputs(struct drm_device *dev)
  10334. {
  10335. struct drm_i915_private *dev_priv = dev->dev_private;
  10336. struct intel_encoder *encoder;
  10337. bool dpd_is_edp = false;
  10338. intel_lvds_init(dev);
  10339. if (intel_crt_present(dev))
  10340. intel_crt_init(dev);
  10341. if (HAS_DDI(dev)) {
  10342. int found;
  10343. /* Haswell uses DDI functions to detect digital outputs */
  10344. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  10345. /* DDI A only supports eDP */
  10346. if (found)
  10347. intel_ddi_init(dev, PORT_A);
  10348. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  10349. * register */
  10350. found = I915_READ(SFUSE_STRAP);
  10351. if (found & SFUSE_STRAP_DDIB_DETECTED)
  10352. intel_ddi_init(dev, PORT_B);
  10353. if (found & SFUSE_STRAP_DDIC_DETECTED)
  10354. intel_ddi_init(dev, PORT_C);
  10355. if (found & SFUSE_STRAP_DDID_DETECTED)
  10356. intel_ddi_init(dev, PORT_D);
  10357. } else if (HAS_PCH_SPLIT(dev)) {
  10358. int found;
  10359. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  10360. if (has_edp_a(dev))
  10361. intel_dp_init(dev, DP_A, PORT_A);
  10362. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  10363. /* PCH SDVOB multiplex with HDMIB */
  10364. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  10365. if (!found)
  10366. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  10367. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  10368. intel_dp_init(dev, PCH_DP_B, PORT_B);
  10369. }
  10370. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  10371. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  10372. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  10373. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  10374. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  10375. intel_dp_init(dev, PCH_DP_C, PORT_C);
  10376. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  10377. intel_dp_init(dev, PCH_DP_D, PORT_D);
  10378. } else if (IS_VALLEYVIEW(dev)) {
  10379. /*
  10380. * The DP_DETECTED bit is the latched state of the DDC
  10381. * SDA pin at boot. However since eDP doesn't require DDC
  10382. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  10383. * eDP ports may have been muxed to an alternate function.
  10384. * Thus we can't rely on the DP_DETECTED bit alone to detect
  10385. * eDP ports. Consult the VBT as well as DP_DETECTED to
  10386. * detect eDP ports.
  10387. */
  10388. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED)
  10389. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  10390. PORT_B);
  10391. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  10392. intel_dp_is_edp(dev, PORT_B))
  10393. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  10394. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED)
  10395. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  10396. PORT_C);
  10397. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  10398. intel_dp_is_edp(dev, PORT_C))
  10399. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  10400. if (IS_CHERRYVIEW(dev)) {
  10401. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  10402. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  10403. PORT_D);
  10404. /* eDP not supported on port D, so don't check VBT */
  10405. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  10406. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  10407. }
  10408. intel_dsi_init(dev);
  10409. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  10410. bool found = false;
  10411. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10412. DRM_DEBUG_KMS("probing SDVOB\n");
  10413. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  10414. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  10415. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  10416. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  10417. }
  10418. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  10419. intel_dp_init(dev, DP_B, PORT_B);
  10420. }
  10421. /* Before G4X SDVOC doesn't have its own detect register */
  10422. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10423. DRM_DEBUG_KMS("probing SDVOC\n");
  10424. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  10425. }
  10426. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  10427. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  10428. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  10429. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  10430. }
  10431. if (SUPPORTS_INTEGRATED_DP(dev))
  10432. intel_dp_init(dev, DP_C, PORT_C);
  10433. }
  10434. if (SUPPORTS_INTEGRATED_DP(dev) &&
  10435. (I915_READ(DP_D) & DP_DETECTED))
  10436. intel_dp_init(dev, DP_D, PORT_D);
  10437. } else if (IS_GEN2(dev))
  10438. intel_dvo_init(dev);
  10439. if (SUPPORTS_TV(dev))
  10440. intel_tv_init(dev);
  10441. intel_psr_init(dev);
  10442. for_each_intel_encoder(dev, encoder) {
  10443. encoder->base.possible_crtcs = encoder->crtc_mask;
  10444. encoder->base.possible_clones =
  10445. intel_encoder_clones(encoder);
  10446. }
  10447. intel_init_pch_refclk(dev);
  10448. drm_helper_move_panel_connectors_to_head(dev);
  10449. }
  10450. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  10451. {
  10452. struct drm_device *dev = fb->dev;
  10453. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10454. drm_framebuffer_cleanup(fb);
  10455. mutex_lock(&dev->struct_mutex);
  10456. WARN_ON(!intel_fb->obj->framebuffer_references--);
  10457. drm_gem_object_unreference(&intel_fb->obj->base);
  10458. mutex_unlock(&dev->struct_mutex);
  10459. kfree(intel_fb);
  10460. }
  10461. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  10462. struct drm_file *file,
  10463. unsigned int *handle)
  10464. {
  10465. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10466. struct drm_i915_gem_object *obj = intel_fb->obj;
  10467. return drm_gem_handle_create(file, &obj->base, handle);
  10468. }
  10469. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  10470. .destroy = intel_user_framebuffer_destroy,
  10471. .create_handle = intel_user_framebuffer_create_handle,
  10472. };
  10473. static int intel_framebuffer_init(struct drm_device *dev,
  10474. struct intel_framebuffer *intel_fb,
  10475. struct drm_mode_fb_cmd2 *mode_cmd,
  10476. struct drm_i915_gem_object *obj)
  10477. {
  10478. int aligned_height;
  10479. int pitch_limit;
  10480. int ret;
  10481. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  10482. if (obj->tiling_mode == I915_TILING_Y) {
  10483. DRM_DEBUG("hardware does not support tiling Y\n");
  10484. return -EINVAL;
  10485. }
  10486. if (mode_cmd->pitches[0] & 63) {
  10487. DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
  10488. mode_cmd->pitches[0]);
  10489. return -EINVAL;
  10490. }
  10491. if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
  10492. pitch_limit = 32*1024;
  10493. } else if (INTEL_INFO(dev)->gen >= 4) {
  10494. if (obj->tiling_mode)
  10495. pitch_limit = 16*1024;
  10496. else
  10497. pitch_limit = 32*1024;
  10498. } else if (INTEL_INFO(dev)->gen >= 3) {
  10499. if (obj->tiling_mode)
  10500. pitch_limit = 8*1024;
  10501. else
  10502. pitch_limit = 16*1024;
  10503. } else
  10504. /* XXX DSPC is limited to 4k tiled */
  10505. pitch_limit = 8*1024;
  10506. if (mode_cmd->pitches[0] > pitch_limit) {
  10507. DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
  10508. obj->tiling_mode ? "tiled" : "linear",
  10509. mode_cmd->pitches[0], pitch_limit);
  10510. return -EINVAL;
  10511. }
  10512. if (obj->tiling_mode != I915_TILING_NONE &&
  10513. mode_cmd->pitches[0] != obj->stride) {
  10514. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  10515. mode_cmd->pitches[0], obj->stride);
  10516. return -EINVAL;
  10517. }
  10518. /* Reject formats not supported by any plane early. */
  10519. switch (mode_cmd->pixel_format) {
  10520. case DRM_FORMAT_C8:
  10521. case DRM_FORMAT_RGB565:
  10522. case DRM_FORMAT_XRGB8888:
  10523. case DRM_FORMAT_ARGB8888:
  10524. break;
  10525. case DRM_FORMAT_XRGB1555:
  10526. case DRM_FORMAT_ARGB1555:
  10527. if (INTEL_INFO(dev)->gen > 3) {
  10528. DRM_DEBUG("unsupported pixel format: %s\n",
  10529. drm_get_format_name(mode_cmd->pixel_format));
  10530. return -EINVAL;
  10531. }
  10532. break;
  10533. case DRM_FORMAT_XBGR8888:
  10534. case DRM_FORMAT_ABGR8888:
  10535. case DRM_FORMAT_XRGB2101010:
  10536. case DRM_FORMAT_ARGB2101010:
  10537. case DRM_FORMAT_XBGR2101010:
  10538. case DRM_FORMAT_ABGR2101010:
  10539. if (INTEL_INFO(dev)->gen < 4) {
  10540. DRM_DEBUG("unsupported pixel format: %s\n",
  10541. drm_get_format_name(mode_cmd->pixel_format));
  10542. return -EINVAL;
  10543. }
  10544. break;
  10545. case DRM_FORMAT_YUYV:
  10546. case DRM_FORMAT_UYVY:
  10547. case DRM_FORMAT_YVYU:
  10548. case DRM_FORMAT_VYUY:
  10549. if (INTEL_INFO(dev)->gen < 5) {
  10550. DRM_DEBUG("unsupported pixel format: %s\n",
  10551. drm_get_format_name(mode_cmd->pixel_format));
  10552. return -EINVAL;
  10553. }
  10554. break;
  10555. default:
  10556. DRM_DEBUG("unsupported pixel format: %s\n",
  10557. drm_get_format_name(mode_cmd->pixel_format));
  10558. return -EINVAL;
  10559. }
  10560. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  10561. if (mode_cmd->offsets[0] != 0)
  10562. return -EINVAL;
  10563. aligned_height = intel_align_height(dev, mode_cmd->height,
  10564. obj->tiling_mode);
  10565. /* FIXME drm helper for size checks (especially planar formats)? */
  10566. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  10567. return -EINVAL;
  10568. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  10569. intel_fb->obj = obj;
  10570. intel_fb->obj->framebuffer_references++;
  10571. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  10572. if (ret) {
  10573. DRM_ERROR("framebuffer init failed %d\n", ret);
  10574. return ret;
  10575. }
  10576. return 0;
  10577. }
  10578. static struct drm_framebuffer *
  10579. intel_user_framebuffer_create(struct drm_device *dev,
  10580. struct drm_file *filp,
  10581. struct drm_mode_fb_cmd2 *mode_cmd)
  10582. {
  10583. struct drm_i915_gem_object *obj;
  10584. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  10585. mode_cmd->handles[0]));
  10586. if (&obj->base == NULL)
  10587. return ERR_PTR(-ENOENT);
  10588. return intel_framebuffer_create(dev, mode_cmd, obj);
  10589. }
  10590. #ifndef CONFIG_DRM_I915_FBDEV
  10591. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  10592. {
  10593. }
  10594. #endif
  10595. static const struct drm_mode_config_funcs intel_mode_funcs = {
  10596. .fb_create = intel_user_framebuffer_create,
  10597. .output_poll_changed = intel_fbdev_output_poll_changed,
  10598. };
  10599. /* Set up chip specific display functions */
  10600. static void intel_init_display(struct drm_device *dev)
  10601. {
  10602. struct drm_i915_private *dev_priv = dev->dev_private;
  10603. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  10604. dev_priv->display.find_dpll = g4x_find_best_dpll;
  10605. else if (IS_CHERRYVIEW(dev))
  10606. dev_priv->display.find_dpll = chv_find_best_dpll;
  10607. else if (IS_VALLEYVIEW(dev))
  10608. dev_priv->display.find_dpll = vlv_find_best_dpll;
  10609. else if (IS_PINEVIEW(dev))
  10610. dev_priv->display.find_dpll = pnv_find_best_dpll;
  10611. else
  10612. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  10613. if (HAS_DDI(dev)) {
  10614. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  10615. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  10616. dev_priv->display.crtc_compute_clock =
  10617. haswell_crtc_compute_clock;
  10618. dev_priv->display.crtc_enable = haswell_crtc_enable;
  10619. dev_priv->display.crtc_disable = haswell_crtc_disable;
  10620. dev_priv->display.off = ironlake_crtc_off;
  10621. if (INTEL_INFO(dev)->gen >= 9)
  10622. dev_priv->display.update_primary_plane =
  10623. skylake_update_primary_plane;
  10624. else
  10625. dev_priv->display.update_primary_plane =
  10626. ironlake_update_primary_plane;
  10627. } else if (HAS_PCH_SPLIT(dev)) {
  10628. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  10629. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  10630. dev_priv->display.crtc_compute_clock =
  10631. ironlake_crtc_compute_clock;
  10632. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  10633. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  10634. dev_priv->display.off = ironlake_crtc_off;
  10635. dev_priv->display.update_primary_plane =
  10636. ironlake_update_primary_plane;
  10637. } else if (IS_VALLEYVIEW(dev)) {
  10638. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10639. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  10640. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  10641. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  10642. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10643. dev_priv->display.off = i9xx_crtc_off;
  10644. dev_priv->display.update_primary_plane =
  10645. i9xx_update_primary_plane;
  10646. } else {
  10647. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10648. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  10649. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  10650. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  10651. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10652. dev_priv->display.off = i9xx_crtc_off;
  10653. dev_priv->display.update_primary_plane =
  10654. i9xx_update_primary_plane;
  10655. }
  10656. /* Returns the core display clock speed */
  10657. if (IS_VALLEYVIEW(dev))
  10658. dev_priv->display.get_display_clock_speed =
  10659. valleyview_get_display_clock_speed;
  10660. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  10661. dev_priv->display.get_display_clock_speed =
  10662. i945_get_display_clock_speed;
  10663. else if (IS_I915G(dev))
  10664. dev_priv->display.get_display_clock_speed =
  10665. i915_get_display_clock_speed;
  10666. else if (IS_I945GM(dev) || IS_845G(dev))
  10667. dev_priv->display.get_display_clock_speed =
  10668. i9xx_misc_get_display_clock_speed;
  10669. else if (IS_PINEVIEW(dev))
  10670. dev_priv->display.get_display_clock_speed =
  10671. pnv_get_display_clock_speed;
  10672. else if (IS_I915GM(dev))
  10673. dev_priv->display.get_display_clock_speed =
  10674. i915gm_get_display_clock_speed;
  10675. else if (IS_I865G(dev))
  10676. dev_priv->display.get_display_clock_speed =
  10677. i865_get_display_clock_speed;
  10678. else if (IS_I85X(dev))
  10679. dev_priv->display.get_display_clock_speed =
  10680. i855_get_display_clock_speed;
  10681. else /* 852, 830 */
  10682. dev_priv->display.get_display_clock_speed =
  10683. i830_get_display_clock_speed;
  10684. if (IS_GEN5(dev)) {
  10685. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  10686. } else if (IS_GEN6(dev)) {
  10687. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  10688. } else if (IS_IVYBRIDGE(dev)) {
  10689. /* FIXME: detect B0+ stepping and use auto training */
  10690. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  10691. dev_priv->display.modeset_global_resources =
  10692. ivb_modeset_global_resources;
  10693. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  10694. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  10695. } else if (IS_VALLEYVIEW(dev)) {
  10696. dev_priv->display.modeset_global_resources =
  10697. valleyview_modeset_global_resources;
  10698. }
  10699. /* Default just returns -ENODEV to indicate unsupported */
  10700. dev_priv->display.queue_flip = intel_default_queue_flip;
  10701. switch (INTEL_INFO(dev)->gen) {
  10702. case 2:
  10703. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  10704. break;
  10705. case 3:
  10706. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  10707. break;
  10708. case 4:
  10709. case 5:
  10710. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  10711. break;
  10712. case 6:
  10713. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  10714. break;
  10715. case 7:
  10716. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  10717. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  10718. break;
  10719. case 9:
  10720. dev_priv->display.queue_flip = intel_gen9_queue_flip;
  10721. break;
  10722. }
  10723. intel_panel_init_backlight_funcs(dev);
  10724. mutex_init(&dev_priv->pps_mutex);
  10725. }
  10726. /*
  10727. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  10728. * resume, or other times. This quirk makes sure that's the case for
  10729. * affected systems.
  10730. */
  10731. static void quirk_pipea_force(struct drm_device *dev)
  10732. {
  10733. struct drm_i915_private *dev_priv = dev->dev_private;
  10734. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  10735. DRM_INFO("applying pipe a force quirk\n");
  10736. }
  10737. static void quirk_pipeb_force(struct drm_device *dev)
  10738. {
  10739. struct drm_i915_private *dev_priv = dev->dev_private;
  10740. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  10741. DRM_INFO("applying pipe b force quirk\n");
  10742. }
  10743. /*
  10744. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  10745. */
  10746. static void quirk_ssc_force_disable(struct drm_device *dev)
  10747. {
  10748. struct drm_i915_private *dev_priv = dev->dev_private;
  10749. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  10750. DRM_INFO("applying lvds SSC disable quirk\n");
  10751. }
  10752. /*
  10753. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  10754. * brightness value
  10755. */
  10756. static void quirk_invert_brightness(struct drm_device *dev)
  10757. {
  10758. struct drm_i915_private *dev_priv = dev->dev_private;
  10759. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  10760. DRM_INFO("applying inverted panel brightness quirk\n");
  10761. }
  10762. /* Some VBT's incorrectly indicate no backlight is present */
  10763. static void quirk_backlight_present(struct drm_device *dev)
  10764. {
  10765. struct drm_i915_private *dev_priv = dev->dev_private;
  10766. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  10767. DRM_INFO("applying backlight present quirk\n");
  10768. }
  10769. struct intel_quirk {
  10770. int device;
  10771. int subsystem_vendor;
  10772. int subsystem_device;
  10773. void (*hook)(struct drm_device *dev);
  10774. };
  10775. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  10776. struct intel_dmi_quirk {
  10777. void (*hook)(struct drm_device *dev);
  10778. const struct dmi_system_id (*dmi_id_list)[];
  10779. };
  10780. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  10781. {
  10782. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  10783. return 1;
  10784. }
  10785. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  10786. {
  10787. .dmi_id_list = &(const struct dmi_system_id[]) {
  10788. {
  10789. .callback = intel_dmi_reverse_brightness,
  10790. .ident = "NCR Corporation",
  10791. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  10792. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  10793. },
  10794. },
  10795. { } /* terminating entry */
  10796. },
  10797. .hook = quirk_invert_brightness,
  10798. },
  10799. };
  10800. static struct intel_quirk intel_quirks[] = {
  10801. /* HP Mini needs pipe A force quirk (LP: #322104) */
  10802. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  10803. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  10804. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  10805. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  10806. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  10807. /* 830 needs to leave pipe A & dpll A up */
  10808. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  10809. /* 830 needs to leave pipe B & dpll B up */
  10810. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  10811. /* Lenovo U160 cannot use SSC on LVDS */
  10812. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  10813. /* Sony Vaio Y cannot use SSC on LVDS */
  10814. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  10815. /* Acer Aspire 5734Z must invert backlight brightness */
  10816. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  10817. /* Acer/eMachines G725 */
  10818. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  10819. /* Acer/eMachines e725 */
  10820. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  10821. /* Acer/Packard Bell NCL20 */
  10822. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  10823. /* Acer Aspire 4736Z */
  10824. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  10825. /* Acer Aspire 5336 */
  10826. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  10827. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  10828. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  10829. /* Acer C720 Chromebook (Core i3 4005U) */
  10830. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  10831. /* Apple Macbook 2,1 (Core 2 T7400) */
  10832. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  10833. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  10834. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  10835. /* HP Chromebook 14 (Celeron 2955U) */
  10836. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  10837. };
  10838. static void intel_init_quirks(struct drm_device *dev)
  10839. {
  10840. struct pci_dev *d = dev->pdev;
  10841. int i;
  10842. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  10843. struct intel_quirk *q = &intel_quirks[i];
  10844. if (d->device == q->device &&
  10845. (d->subsystem_vendor == q->subsystem_vendor ||
  10846. q->subsystem_vendor == PCI_ANY_ID) &&
  10847. (d->subsystem_device == q->subsystem_device ||
  10848. q->subsystem_device == PCI_ANY_ID))
  10849. q->hook(dev);
  10850. }
  10851. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  10852. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  10853. intel_dmi_quirks[i].hook(dev);
  10854. }
  10855. }
  10856. /* Disable the VGA plane that we never use */
  10857. static void i915_disable_vga(struct drm_device *dev)
  10858. {
  10859. struct drm_i915_private *dev_priv = dev->dev_private;
  10860. u8 sr1;
  10861. u32 vga_reg = i915_vgacntrl_reg(dev);
  10862. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  10863. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  10864. outb(SR01, VGA_SR_INDEX);
  10865. sr1 = inb(VGA_SR_DATA);
  10866. outb(sr1 | 1<<5, VGA_SR_DATA);
  10867. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  10868. udelay(300);
  10869. /*
  10870. * Fujitsu-Siemens Lifebook S6010 (830) has problems resuming
  10871. * from S3 without preserving (some of?) the other bits.
  10872. */
  10873. I915_WRITE(vga_reg, dev_priv->bios_vgacntr | VGA_DISP_DISABLE);
  10874. POSTING_READ(vga_reg);
  10875. }
  10876. void intel_modeset_init_hw(struct drm_device *dev)
  10877. {
  10878. intel_prepare_ddi(dev);
  10879. if (IS_VALLEYVIEW(dev))
  10880. vlv_update_cdclk(dev);
  10881. intel_init_clock_gating(dev);
  10882. intel_enable_gt_powersave(dev);
  10883. }
  10884. void intel_modeset_init(struct drm_device *dev)
  10885. {
  10886. struct drm_i915_private *dev_priv = dev->dev_private;
  10887. int sprite, ret;
  10888. enum pipe pipe;
  10889. struct intel_crtc *crtc;
  10890. drm_mode_config_init(dev);
  10891. dev->mode_config.min_width = 0;
  10892. dev->mode_config.min_height = 0;
  10893. dev->mode_config.preferred_depth = 24;
  10894. dev->mode_config.prefer_shadow = 1;
  10895. dev->mode_config.funcs = &intel_mode_funcs;
  10896. intel_init_quirks(dev);
  10897. intel_init_pm(dev);
  10898. if (INTEL_INFO(dev)->num_pipes == 0)
  10899. return;
  10900. intel_init_display(dev);
  10901. intel_init_audio(dev);
  10902. if (IS_GEN2(dev)) {
  10903. dev->mode_config.max_width = 2048;
  10904. dev->mode_config.max_height = 2048;
  10905. } else if (IS_GEN3(dev)) {
  10906. dev->mode_config.max_width = 4096;
  10907. dev->mode_config.max_height = 4096;
  10908. } else {
  10909. dev->mode_config.max_width = 8192;
  10910. dev->mode_config.max_height = 8192;
  10911. }
  10912. if (IS_845G(dev) || IS_I865G(dev)) {
  10913. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  10914. dev->mode_config.cursor_height = 1023;
  10915. } else if (IS_GEN2(dev)) {
  10916. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  10917. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  10918. } else {
  10919. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  10920. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  10921. }
  10922. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  10923. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  10924. INTEL_INFO(dev)->num_pipes,
  10925. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  10926. for_each_pipe(dev_priv, pipe) {
  10927. intel_crtc_init(dev, pipe);
  10928. for_each_sprite(pipe, sprite) {
  10929. ret = intel_plane_init(dev, pipe, sprite);
  10930. if (ret)
  10931. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  10932. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  10933. }
  10934. }
  10935. intel_init_dpio(dev);
  10936. intel_shared_dpll_init(dev);
  10937. /* save the BIOS value before clobbering it */
  10938. dev_priv->bios_vgacntr = I915_READ(i915_vgacntrl_reg(dev));
  10939. /* Just disable it once at startup */
  10940. i915_disable_vga(dev);
  10941. intel_setup_outputs(dev);
  10942. /* Just in case the BIOS is doing something questionable. */
  10943. intel_disable_fbc(dev);
  10944. drm_modeset_lock_all(dev);
  10945. intel_modeset_setup_hw_state(dev, false);
  10946. drm_modeset_unlock_all(dev);
  10947. for_each_intel_crtc(dev, crtc) {
  10948. if (!crtc->active)
  10949. continue;
  10950. /*
  10951. * Note that reserving the BIOS fb up front prevents us
  10952. * from stuffing other stolen allocations like the ring
  10953. * on top. This prevents some ugliness at boot time, and
  10954. * can even allow for smooth boot transitions if the BIOS
  10955. * fb is large enough for the active pipe configuration.
  10956. */
  10957. if (dev_priv->display.get_plane_config) {
  10958. dev_priv->display.get_plane_config(crtc,
  10959. &crtc->plane_config);
  10960. /*
  10961. * If the fb is shared between multiple heads, we'll
  10962. * just get the first one.
  10963. */
  10964. intel_find_plane_obj(crtc, &crtc->plane_config);
  10965. }
  10966. }
  10967. }
  10968. static void intel_enable_pipe_a(struct drm_device *dev)
  10969. {
  10970. struct intel_connector *connector;
  10971. struct drm_connector *crt = NULL;
  10972. struct intel_load_detect_pipe load_detect_temp;
  10973. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  10974. /* We can't just switch on the pipe A, we need to set things up with a
  10975. * proper mode and output configuration. As a gross hack, enable pipe A
  10976. * by enabling the load detect pipe once. */
  10977. list_for_each_entry(connector,
  10978. &dev->mode_config.connector_list,
  10979. base.head) {
  10980. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  10981. crt = &connector->base;
  10982. break;
  10983. }
  10984. }
  10985. if (!crt)
  10986. return;
  10987. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  10988. intel_release_load_detect_pipe(crt, &load_detect_temp);
  10989. }
  10990. static bool
  10991. intel_check_plane_mapping(struct intel_crtc *crtc)
  10992. {
  10993. struct drm_device *dev = crtc->base.dev;
  10994. struct drm_i915_private *dev_priv = dev->dev_private;
  10995. u32 reg, val;
  10996. if (INTEL_INFO(dev)->num_pipes == 1)
  10997. return true;
  10998. reg = DSPCNTR(!crtc->plane);
  10999. val = I915_READ(reg);
  11000. if ((val & DISPLAY_PLANE_ENABLE) &&
  11001. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  11002. return false;
  11003. return true;
  11004. }
  11005. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  11006. {
  11007. struct drm_device *dev = crtc->base.dev;
  11008. struct drm_i915_private *dev_priv = dev->dev_private;
  11009. u32 reg;
  11010. /* Clear any frame start delays used for debugging left by the BIOS */
  11011. reg = PIPECONF(crtc->config.cpu_transcoder);
  11012. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  11013. /* restore vblank interrupts to correct state */
  11014. if (crtc->active) {
  11015. update_scanline_offset(crtc);
  11016. drm_vblank_on(dev, crtc->pipe);
  11017. } else
  11018. drm_vblank_off(dev, crtc->pipe);
  11019. /* We need to sanitize the plane -> pipe mapping first because this will
  11020. * disable the crtc (and hence change the state) if it is wrong. Note
  11021. * that gen4+ has a fixed plane -> pipe mapping. */
  11022. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  11023. struct intel_connector *connector;
  11024. bool plane;
  11025. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  11026. crtc->base.base.id);
  11027. /* Pipe has the wrong plane attached and the plane is active.
  11028. * Temporarily change the plane mapping and disable everything
  11029. * ... */
  11030. plane = crtc->plane;
  11031. crtc->plane = !plane;
  11032. crtc->primary_enabled = true;
  11033. dev_priv->display.crtc_disable(&crtc->base);
  11034. crtc->plane = plane;
  11035. /* ... and break all links. */
  11036. list_for_each_entry(connector, &dev->mode_config.connector_list,
  11037. base.head) {
  11038. if (connector->encoder->base.crtc != &crtc->base)
  11039. continue;
  11040. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11041. connector->base.encoder = NULL;
  11042. }
  11043. /* multiple connectors may have the same encoder:
  11044. * handle them and break crtc link separately */
  11045. list_for_each_entry(connector, &dev->mode_config.connector_list,
  11046. base.head)
  11047. if (connector->encoder->base.crtc == &crtc->base) {
  11048. connector->encoder->base.crtc = NULL;
  11049. connector->encoder->connectors_active = false;
  11050. }
  11051. WARN_ON(crtc->active);
  11052. crtc->base.enabled = false;
  11053. }
  11054. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  11055. crtc->pipe == PIPE_A && !crtc->active) {
  11056. /* BIOS forgot to enable pipe A, this mostly happens after
  11057. * resume. Force-enable the pipe to fix this, the update_dpms
  11058. * call below we restore the pipe to the right state, but leave
  11059. * the required bits on. */
  11060. intel_enable_pipe_a(dev);
  11061. }
  11062. /* Adjust the state of the output pipe according to whether we
  11063. * have active connectors/encoders. */
  11064. intel_crtc_update_dpms(&crtc->base);
  11065. if (crtc->active != crtc->base.enabled) {
  11066. struct intel_encoder *encoder;
  11067. /* This can happen either due to bugs in the get_hw_state
  11068. * functions or because the pipe is force-enabled due to the
  11069. * pipe A quirk. */
  11070. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  11071. crtc->base.base.id,
  11072. crtc->base.enabled ? "enabled" : "disabled",
  11073. crtc->active ? "enabled" : "disabled");
  11074. crtc->base.enabled = crtc->active;
  11075. /* Because we only establish the connector -> encoder ->
  11076. * crtc links if something is active, this means the
  11077. * crtc is now deactivated. Break the links. connector
  11078. * -> encoder links are only establish when things are
  11079. * actually up, hence no need to break them. */
  11080. WARN_ON(crtc->active);
  11081. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  11082. WARN_ON(encoder->connectors_active);
  11083. encoder->base.crtc = NULL;
  11084. }
  11085. }
  11086. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  11087. /*
  11088. * We start out with underrun reporting disabled to avoid races.
  11089. * For correct bookkeeping mark this on active crtcs.
  11090. *
  11091. * Also on gmch platforms we dont have any hardware bits to
  11092. * disable the underrun reporting. Which means we need to start
  11093. * out with underrun reporting disabled also on inactive pipes,
  11094. * since otherwise we'll complain about the garbage we read when
  11095. * e.g. coming up after runtime pm.
  11096. *
  11097. * No protection against concurrent access is required - at
  11098. * worst a fifo underrun happens which also sets this to false.
  11099. */
  11100. crtc->cpu_fifo_underrun_disabled = true;
  11101. crtc->pch_fifo_underrun_disabled = true;
  11102. }
  11103. }
  11104. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  11105. {
  11106. struct intel_connector *connector;
  11107. struct drm_device *dev = encoder->base.dev;
  11108. /* We need to check both for a crtc link (meaning that the
  11109. * encoder is active and trying to read from a pipe) and the
  11110. * pipe itself being active. */
  11111. bool has_active_crtc = encoder->base.crtc &&
  11112. to_intel_crtc(encoder->base.crtc)->active;
  11113. if (encoder->connectors_active && !has_active_crtc) {
  11114. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  11115. encoder->base.base.id,
  11116. encoder->base.name);
  11117. /* Connector is active, but has no active pipe. This is
  11118. * fallout from our resume register restoring. Disable
  11119. * the encoder manually again. */
  11120. if (encoder->base.crtc) {
  11121. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  11122. encoder->base.base.id,
  11123. encoder->base.name);
  11124. encoder->disable(encoder);
  11125. if (encoder->post_disable)
  11126. encoder->post_disable(encoder);
  11127. }
  11128. encoder->base.crtc = NULL;
  11129. encoder->connectors_active = false;
  11130. /* Inconsistent output/port/pipe state happens presumably due to
  11131. * a bug in one of the get_hw_state functions. Or someplace else
  11132. * in our code, like the register restore mess on resume. Clamp
  11133. * things to off as a safer default. */
  11134. list_for_each_entry(connector,
  11135. &dev->mode_config.connector_list,
  11136. base.head) {
  11137. if (connector->encoder != encoder)
  11138. continue;
  11139. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11140. connector->base.encoder = NULL;
  11141. }
  11142. }
  11143. /* Enabled encoders without active connectors will be fixed in
  11144. * the crtc fixup. */
  11145. }
  11146. void i915_redisable_vga_power_on(struct drm_device *dev)
  11147. {
  11148. struct drm_i915_private *dev_priv = dev->dev_private;
  11149. u32 vga_reg = i915_vgacntrl_reg(dev);
  11150. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  11151. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  11152. i915_disable_vga(dev);
  11153. }
  11154. }
  11155. void i915_redisable_vga(struct drm_device *dev)
  11156. {
  11157. struct drm_i915_private *dev_priv = dev->dev_private;
  11158. /* This function can be called both from intel_modeset_setup_hw_state or
  11159. * at a very early point in our resume sequence, where the power well
  11160. * structures are not yet restored. Since this function is at a very
  11161. * paranoid "someone might have enabled VGA while we were not looking"
  11162. * level, just check if the power well is enabled instead of trying to
  11163. * follow the "don't touch the power well if we don't need it" policy
  11164. * the rest of the driver uses. */
  11165. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  11166. return;
  11167. i915_redisable_vga_power_on(dev);
  11168. }
  11169. static bool primary_get_hw_state(struct intel_crtc *crtc)
  11170. {
  11171. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  11172. if (!crtc->active)
  11173. return false;
  11174. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  11175. }
  11176. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  11177. {
  11178. struct drm_i915_private *dev_priv = dev->dev_private;
  11179. enum pipe pipe;
  11180. struct intel_crtc *crtc;
  11181. struct intel_encoder *encoder;
  11182. struct intel_connector *connector;
  11183. int i;
  11184. for_each_intel_crtc(dev, crtc) {
  11185. memset(&crtc->config, 0, sizeof(crtc->config));
  11186. crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  11187. crtc->active = dev_priv->display.get_pipe_config(crtc,
  11188. &crtc->config);
  11189. crtc->base.enabled = crtc->active;
  11190. crtc->primary_enabled = primary_get_hw_state(crtc);
  11191. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  11192. crtc->base.base.id,
  11193. crtc->active ? "enabled" : "disabled");
  11194. }
  11195. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11196. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11197. pll->on = pll->get_hw_state(dev_priv, pll,
  11198. &pll->config.hw_state);
  11199. pll->active = 0;
  11200. pll->config.crtc_mask = 0;
  11201. for_each_intel_crtc(dev, crtc) {
  11202. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  11203. pll->active++;
  11204. pll->config.crtc_mask |= 1 << crtc->pipe;
  11205. }
  11206. }
  11207. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  11208. pll->name, pll->config.crtc_mask, pll->on);
  11209. if (pll->config.crtc_mask)
  11210. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  11211. }
  11212. for_each_intel_encoder(dev, encoder) {
  11213. pipe = 0;
  11214. if (encoder->get_hw_state(encoder, &pipe)) {
  11215. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11216. encoder->base.crtc = &crtc->base;
  11217. encoder->get_config(encoder, &crtc->config);
  11218. } else {
  11219. encoder->base.crtc = NULL;
  11220. }
  11221. encoder->connectors_active = false;
  11222. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  11223. encoder->base.base.id,
  11224. encoder->base.name,
  11225. encoder->base.crtc ? "enabled" : "disabled",
  11226. pipe_name(pipe));
  11227. }
  11228. list_for_each_entry(connector, &dev->mode_config.connector_list,
  11229. base.head) {
  11230. if (connector->get_hw_state(connector)) {
  11231. connector->base.dpms = DRM_MODE_DPMS_ON;
  11232. connector->encoder->connectors_active = true;
  11233. connector->base.encoder = &connector->encoder->base;
  11234. } else {
  11235. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11236. connector->base.encoder = NULL;
  11237. }
  11238. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  11239. connector->base.base.id,
  11240. connector->base.name,
  11241. connector->base.encoder ? "enabled" : "disabled");
  11242. }
  11243. }
  11244. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  11245. * and i915 state tracking structures. */
  11246. void intel_modeset_setup_hw_state(struct drm_device *dev,
  11247. bool force_restore)
  11248. {
  11249. struct drm_i915_private *dev_priv = dev->dev_private;
  11250. enum pipe pipe;
  11251. struct intel_crtc *crtc;
  11252. struct intel_encoder *encoder;
  11253. int i;
  11254. intel_modeset_readout_hw_state(dev);
  11255. /*
  11256. * Now that we have the config, copy it to each CRTC struct
  11257. * Note that this could go away if we move to using crtc_config
  11258. * checking everywhere.
  11259. */
  11260. for_each_intel_crtc(dev, crtc) {
  11261. if (crtc->active && i915.fastboot) {
  11262. intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
  11263. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  11264. crtc->base.base.id);
  11265. drm_mode_debug_printmodeline(&crtc->base.mode);
  11266. }
  11267. }
  11268. /* HW state is read out, now we need to sanitize this mess. */
  11269. for_each_intel_encoder(dev, encoder) {
  11270. intel_sanitize_encoder(encoder);
  11271. }
  11272. for_each_pipe(dev_priv, pipe) {
  11273. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11274. intel_sanitize_crtc(crtc);
  11275. intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
  11276. }
  11277. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11278. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11279. if (!pll->on || pll->active)
  11280. continue;
  11281. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  11282. pll->disable(dev_priv, pll);
  11283. pll->on = false;
  11284. }
  11285. if (IS_GEN9(dev))
  11286. skl_wm_get_hw_state(dev);
  11287. else if (HAS_PCH_SPLIT(dev))
  11288. ilk_wm_get_hw_state(dev);
  11289. if (force_restore) {
  11290. i915_redisable_vga(dev);
  11291. /*
  11292. * We need to use raw interfaces for restoring state to avoid
  11293. * checking (bogus) intermediate states.
  11294. */
  11295. for_each_pipe(dev_priv, pipe) {
  11296. struct drm_crtc *crtc =
  11297. dev_priv->pipe_to_crtc_mapping[pipe];
  11298. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
  11299. crtc->primary->fb);
  11300. }
  11301. } else {
  11302. intel_modeset_update_staged_output_state(dev);
  11303. }
  11304. intel_modeset_check_state(dev);
  11305. }
  11306. void intel_modeset_gem_init(struct drm_device *dev)
  11307. {
  11308. struct drm_i915_private *dev_priv = dev->dev_private;
  11309. struct drm_crtc *c;
  11310. struct drm_i915_gem_object *obj;
  11311. mutex_lock(&dev->struct_mutex);
  11312. intel_init_gt_powersave(dev);
  11313. mutex_unlock(&dev->struct_mutex);
  11314. /*
  11315. * There may be no VBT; and if the BIOS enabled SSC we can
  11316. * just keep using it to avoid unnecessary flicker. Whereas if the
  11317. * BIOS isn't using it, don't assume it will work even if the VBT
  11318. * indicates as much.
  11319. */
  11320. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11321. dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  11322. DREF_SSC1_ENABLE);
  11323. intel_modeset_init_hw(dev);
  11324. intel_setup_overlay(dev);
  11325. /*
  11326. * Make sure any fbs we allocated at startup are properly
  11327. * pinned & fenced. When we do the allocation it's too early
  11328. * for this.
  11329. */
  11330. mutex_lock(&dev->struct_mutex);
  11331. for_each_crtc(dev, c) {
  11332. obj = intel_fb_obj(c->primary->fb);
  11333. if (obj == NULL)
  11334. continue;
  11335. if (intel_pin_and_fence_fb_obj(c->primary,
  11336. c->primary->fb,
  11337. NULL)) {
  11338. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  11339. to_intel_crtc(c)->pipe);
  11340. drm_framebuffer_unreference(c->primary->fb);
  11341. c->primary->fb = NULL;
  11342. }
  11343. }
  11344. mutex_unlock(&dev->struct_mutex);
  11345. intel_backlight_register(dev);
  11346. }
  11347. void intel_connector_unregister(struct intel_connector *intel_connector)
  11348. {
  11349. struct drm_connector *connector = &intel_connector->base;
  11350. intel_panel_destroy_backlight(connector);
  11351. drm_connector_unregister(connector);
  11352. }
  11353. void intel_modeset_cleanup(struct drm_device *dev)
  11354. {
  11355. struct drm_i915_private *dev_priv = dev->dev_private;
  11356. struct drm_connector *connector;
  11357. intel_disable_gt_powersave(dev);
  11358. intel_backlight_unregister(dev);
  11359. /*
  11360. * Interrupts and polling as the first thing to avoid creating havoc.
  11361. * Too much stuff here (turning of connectors, ...) would
  11362. * experience fancy races otherwise.
  11363. */
  11364. intel_irq_uninstall(dev_priv);
  11365. /*
  11366. * Due to the hpd irq storm handling the hotplug work can re-arm the
  11367. * poll handlers. Hence disable polling after hpd handling is shut down.
  11368. */
  11369. drm_kms_helper_poll_fini(dev);
  11370. mutex_lock(&dev->struct_mutex);
  11371. intel_unregister_dsm_handler();
  11372. intel_disable_fbc(dev);
  11373. ironlake_teardown_rc6(dev);
  11374. mutex_unlock(&dev->struct_mutex);
  11375. /* flush any delayed tasks or pending work */
  11376. flush_scheduled_work();
  11377. /* destroy the backlight and sysfs files before encoders/connectors */
  11378. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  11379. struct intel_connector *intel_connector;
  11380. intel_connector = to_intel_connector(connector);
  11381. intel_connector->unregister(intel_connector);
  11382. }
  11383. drm_mode_config_cleanup(dev);
  11384. intel_cleanup_overlay(dev);
  11385. mutex_lock(&dev->struct_mutex);
  11386. intel_cleanup_gt_powersave(dev);
  11387. mutex_unlock(&dev->struct_mutex);
  11388. }
  11389. /*
  11390. * Return which encoder is currently attached for connector.
  11391. */
  11392. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  11393. {
  11394. return &intel_attached_encoder(connector)->base;
  11395. }
  11396. void intel_connector_attach_encoder(struct intel_connector *connector,
  11397. struct intel_encoder *encoder)
  11398. {
  11399. connector->encoder = encoder;
  11400. drm_mode_connector_attach_encoder(&connector->base,
  11401. &encoder->base);
  11402. }
  11403. /*
  11404. * set vga decode state - true == enable VGA decode
  11405. */
  11406. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  11407. {
  11408. struct drm_i915_private *dev_priv = dev->dev_private;
  11409. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  11410. u16 gmch_ctrl;
  11411. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  11412. DRM_ERROR("failed to read control word\n");
  11413. return -EIO;
  11414. }
  11415. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  11416. return 0;
  11417. if (state)
  11418. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  11419. else
  11420. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  11421. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  11422. DRM_ERROR("failed to write control word\n");
  11423. return -EIO;
  11424. }
  11425. return 0;
  11426. }
  11427. struct intel_display_error_state {
  11428. u32 power_well_driver;
  11429. int num_transcoders;
  11430. struct intel_cursor_error_state {
  11431. u32 control;
  11432. u32 position;
  11433. u32 base;
  11434. u32 size;
  11435. } cursor[I915_MAX_PIPES];
  11436. struct intel_pipe_error_state {
  11437. bool power_domain_on;
  11438. u32 source;
  11439. u32 stat;
  11440. } pipe[I915_MAX_PIPES];
  11441. struct intel_plane_error_state {
  11442. u32 control;
  11443. u32 stride;
  11444. u32 size;
  11445. u32 pos;
  11446. u32 addr;
  11447. u32 surface;
  11448. u32 tile_offset;
  11449. } plane[I915_MAX_PIPES];
  11450. struct intel_transcoder_error_state {
  11451. bool power_domain_on;
  11452. enum transcoder cpu_transcoder;
  11453. u32 conf;
  11454. u32 htotal;
  11455. u32 hblank;
  11456. u32 hsync;
  11457. u32 vtotal;
  11458. u32 vblank;
  11459. u32 vsync;
  11460. } transcoder[4];
  11461. };
  11462. struct intel_display_error_state *
  11463. intel_display_capture_error_state(struct drm_device *dev)
  11464. {
  11465. struct drm_i915_private *dev_priv = dev->dev_private;
  11466. struct intel_display_error_state *error;
  11467. int transcoders[] = {
  11468. TRANSCODER_A,
  11469. TRANSCODER_B,
  11470. TRANSCODER_C,
  11471. TRANSCODER_EDP,
  11472. };
  11473. int i;
  11474. if (INTEL_INFO(dev)->num_pipes == 0)
  11475. return NULL;
  11476. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  11477. if (error == NULL)
  11478. return NULL;
  11479. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11480. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  11481. for_each_pipe(dev_priv, i) {
  11482. error->pipe[i].power_domain_on =
  11483. __intel_display_power_is_enabled(dev_priv,
  11484. POWER_DOMAIN_PIPE(i));
  11485. if (!error->pipe[i].power_domain_on)
  11486. continue;
  11487. error->cursor[i].control = I915_READ(CURCNTR(i));
  11488. error->cursor[i].position = I915_READ(CURPOS(i));
  11489. error->cursor[i].base = I915_READ(CURBASE(i));
  11490. error->plane[i].control = I915_READ(DSPCNTR(i));
  11491. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  11492. if (INTEL_INFO(dev)->gen <= 3) {
  11493. error->plane[i].size = I915_READ(DSPSIZE(i));
  11494. error->plane[i].pos = I915_READ(DSPPOS(i));
  11495. }
  11496. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11497. error->plane[i].addr = I915_READ(DSPADDR(i));
  11498. if (INTEL_INFO(dev)->gen >= 4) {
  11499. error->plane[i].surface = I915_READ(DSPSURF(i));
  11500. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  11501. }
  11502. error->pipe[i].source = I915_READ(PIPESRC(i));
  11503. if (HAS_GMCH_DISPLAY(dev))
  11504. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  11505. }
  11506. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  11507. if (HAS_DDI(dev_priv->dev))
  11508. error->num_transcoders++; /* Account for eDP. */
  11509. for (i = 0; i < error->num_transcoders; i++) {
  11510. enum transcoder cpu_transcoder = transcoders[i];
  11511. error->transcoder[i].power_domain_on =
  11512. __intel_display_power_is_enabled(dev_priv,
  11513. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  11514. if (!error->transcoder[i].power_domain_on)
  11515. continue;
  11516. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  11517. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  11518. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  11519. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  11520. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  11521. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  11522. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  11523. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  11524. }
  11525. return error;
  11526. }
  11527. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  11528. void
  11529. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  11530. struct drm_device *dev,
  11531. struct intel_display_error_state *error)
  11532. {
  11533. struct drm_i915_private *dev_priv = dev->dev_private;
  11534. int i;
  11535. if (!error)
  11536. return;
  11537. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  11538. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11539. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  11540. error->power_well_driver);
  11541. for_each_pipe(dev_priv, i) {
  11542. err_printf(m, "Pipe [%d]:\n", i);
  11543. err_printf(m, " Power: %s\n",
  11544. error->pipe[i].power_domain_on ? "on" : "off");
  11545. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  11546. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  11547. err_printf(m, "Plane [%d]:\n", i);
  11548. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  11549. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  11550. if (INTEL_INFO(dev)->gen <= 3) {
  11551. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  11552. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  11553. }
  11554. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11555. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  11556. if (INTEL_INFO(dev)->gen >= 4) {
  11557. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  11558. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  11559. }
  11560. err_printf(m, "Cursor [%d]:\n", i);
  11561. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  11562. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  11563. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  11564. }
  11565. for (i = 0; i < error->num_transcoders; i++) {
  11566. err_printf(m, "CPU transcoder: %c\n",
  11567. transcoder_name(error->transcoder[i].cpu_transcoder));
  11568. err_printf(m, " Power: %s\n",
  11569. error->transcoder[i].power_domain_on ? "on" : "off");
  11570. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  11571. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  11572. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  11573. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  11574. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  11575. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  11576. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  11577. }
  11578. }
  11579. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  11580. {
  11581. struct intel_crtc *crtc;
  11582. for_each_intel_crtc(dev, crtc) {
  11583. struct intel_unpin_work *work;
  11584. spin_lock_irq(&dev->event_lock);
  11585. work = crtc->unpin_work;
  11586. if (work && work->event &&
  11587. work->event->base.file_priv == file) {
  11588. kfree(work->event);
  11589. work->event = NULL;
  11590. }
  11591. spin_unlock_irq(&dev->event_lock);
  11592. }
  11593. }