intel_display.c 381 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836
  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. #define DIV_ROUND_CLOSEST_ULL(ll, d) \
  71. ({ unsigned long long _tmp = (ll)+(d)/2; do_div(_tmp, d); _tmp; })
  72. static void intel_increase_pllclock(struct drm_device *dev,
  73. enum pipe pipe);
  74. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  75. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  76. struct intel_crtc_config *pipe_config);
  77. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  78. struct intel_crtc_config *pipe_config);
  79. static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
  80. int x, int y, struct drm_framebuffer *old_fb);
  81. static int intel_framebuffer_init(struct drm_device *dev,
  82. struct intel_framebuffer *ifb,
  83. struct drm_mode_fb_cmd2 *mode_cmd,
  84. struct drm_i915_gem_object *obj);
  85. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  86. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  87. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  88. struct intel_link_m_n *m_n,
  89. struct intel_link_m_n *m2_n2);
  90. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  91. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  92. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  93. static void vlv_prepare_pll(struct intel_crtc *crtc);
  94. static void chv_prepare_pll(struct intel_crtc *crtc);
  95. static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
  96. {
  97. if (!connector->mst_port)
  98. return connector->encoder;
  99. else
  100. return &connector->mst_port->mst_encoders[pipe]->base;
  101. }
  102. typedef struct {
  103. int min, max;
  104. } intel_range_t;
  105. typedef struct {
  106. int dot_limit;
  107. int p2_slow, p2_fast;
  108. } intel_p2_t;
  109. typedef struct intel_limit intel_limit_t;
  110. struct intel_limit {
  111. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  112. intel_p2_t p2;
  113. };
  114. int
  115. intel_pch_rawclk(struct drm_device *dev)
  116. {
  117. struct drm_i915_private *dev_priv = dev->dev_private;
  118. WARN_ON(!HAS_PCH_SPLIT(dev));
  119. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  120. }
  121. static inline u32 /* units of 100MHz */
  122. intel_fdi_link_freq(struct drm_device *dev)
  123. {
  124. if (IS_GEN5(dev)) {
  125. struct drm_i915_private *dev_priv = dev->dev_private;
  126. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  127. } else
  128. return 27;
  129. }
  130. static const intel_limit_t intel_limits_i8xx_dac = {
  131. .dot = { .min = 25000, .max = 350000 },
  132. .vco = { .min = 908000, .max = 1512000 },
  133. .n = { .min = 2, .max = 16 },
  134. .m = { .min = 96, .max = 140 },
  135. .m1 = { .min = 18, .max = 26 },
  136. .m2 = { .min = 6, .max = 16 },
  137. .p = { .min = 4, .max = 128 },
  138. .p1 = { .min = 2, .max = 33 },
  139. .p2 = { .dot_limit = 165000,
  140. .p2_slow = 4, .p2_fast = 2 },
  141. };
  142. static const intel_limit_t intel_limits_i8xx_dvo = {
  143. .dot = { .min = 25000, .max = 350000 },
  144. .vco = { .min = 908000, .max = 1512000 },
  145. .n = { .min = 2, .max = 16 },
  146. .m = { .min = 96, .max = 140 },
  147. .m1 = { .min = 18, .max = 26 },
  148. .m2 = { .min = 6, .max = 16 },
  149. .p = { .min = 4, .max = 128 },
  150. .p1 = { .min = 2, .max = 33 },
  151. .p2 = { .dot_limit = 165000,
  152. .p2_slow = 4, .p2_fast = 4 },
  153. };
  154. static const intel_limit_t intel_limits_i8xx_lvds = {
  155. .dot = { .min = 25000, .max = 350000 },
  156. .vco = { .min = 908000, .max = 1512000 },
  157. .n = { .min = 2, .max = 16 },
  158. .m = { .min = 96, .max = 140 },
  159. .m1 = { .min = 18, .max = 26 },
  160. .m2 = { .min = 6, .max = 16 },
  161. .p = { .min = 4, .max = 128 },
  162. .p1 = { .min = 1, .max = 6 },
  163. .p2 = { .dot_limit = 165000,
  164. .p2_slow = 14, .p2_fast = 7 },
  165. };
  166. static const intel_limit_t intel_limits_i9xx_sdvo = {
  167. .dot = { .min = 20000, .max = 400000 },
  168. .vco = { .min = 1400000, .max = 2800000 },
  169. .n = { .min = 1, .max = 6 },
  170. .m = { .min = 70, .max = 120 },
  171. .m1 = { .min = 8, .max = 18 },
  172. .m2 = { .min = 3, .max = 7 },
  173. .p = { .min = 5, .max = 80 },
  174. .p1 = { .min = 1, .max = 8 },
  175. .p2 = { .dot_limit = 200000,
  176. .p2_slow = 10, .p2_fast = 5 },
  177. };
  178. static const intel_limit_t intel_limits_i9xx_lvds = {
  179. .dot = { .min = 20000, .max = 400000 },
  180. .vco = { .min = 1400000, .max = 2800000 },
  181. .n = { .min = 1, .max = 6 },
  182. .m = { .min = 70, .max = 120 },
  183. .m1 = { .min = 8, .max = 18 },
  184. .m2 = { .min = 3, .max = 7 },
  185. .p = { .min = 7, .max = 98 },
  186. .p1 = { .min = 1, .max = 8 },
  187. .p2 = { .dot_limit = 112000,
  188. .p2_slow = 14, .p2_fast = 7 },
  189. };
  190. static const intel_limit_t intel_limits_g4x_sdvo = {
  191. .dot = { .min = 25000, .max = 270000 },
  192. .vco = { .min = 1750000, .max = 3500000},
  193. .n = { .min = 1, .max = 4 },
  194. .m = { .min = 104, .max = 138 },
  195. .m1 = { .min = 17, .max = 23 },
  196. .m2 = { .min = 5, .max = 11 },
  197. .p = { .min = 10, .max = 30 },
  198. .p1 = { .min = 1, .max = 3},
  199. .p2 = { .dot_limit = 270000,
  200. .p2_slow = 10,
  201. .p2_fast = 10
  202. },
  203. };
  204. static const intel_limit_t intel_limits_g4x_hdmi = {
  205. .dot = { .min = 22000, .max = 400000 },
  206. .vco = { .min = 1750000, .max = 3500000},
  207. .n = { .min = 1, .max = 4 },
  208. .m = { .min = 104, .max = 138 },
  209. .m1 = { .min = 16, .max = 23 },
  210. .m2 = { .min = 5, .max = 11 },
  211. .p = { .min = 5, .max = 80 },
  212. .p1 = { .min = 1, .max = 8},
  213. .p2 = { .dot_limit = 165000,
  214. .p2_slow = 10, .p2_fast = 5 },
  215. };
  216. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  217. .dot = { .min = 20000, .max = 115000 },
  218. .vco = { .min = 1750000, .max = 3500000 },
  219. .n = { .min = 1, .max = 3 },
  220. .m = { .min = 104, .max = 138 },
  221. .m1 = { .min = 17, .max = 23 },
  222. .m2 = { .min = 5, .max = 11 },
  223. .p = { .min = 28, .max = 112 },
  224. .p1 = { .min = 2, .max = 8 },
  225. .p2 = { .dot_limit = 0,
  226. .p2_slow = 14, .p2_fast = 14
  227. },
  228. };
  229. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  230. .dot = { .min = 80000, .max = 224000 },
  231. .vco = { .min = 1750000, .max = 3500000 },
  232. .n = { .min = 1, .max = 3 },
  233. .m = { .min = 104, .max = 138 },
  234. .m1 = { .min = 17, .max = 23 },
  235. .m2 = { .min = 5, .max = 11 },
  236. .p = { .min = 14, .max = 42 },
  237. .p1 = { .min = 2, .max = 6 },
  238. .p2 = { .dot_limit = 0,
  239. .p2_slow = 7, .p2_fast = 7
  240. },
  241. };
  242. static const intel_limit_t intel_limits_pineview_sdvo = {
  243. .dot = { .min = 20000, .max = 400000},
  244. .vco = { .min = 1700000, .max = 3500000 },
  245. /* Pineview's Ncounter is a ring counter */
  246. .n = { .min = 3, .max = 6 },
  247. .m = { .min = 2, .max = 256 },
  248. /* Pineview only has one combined m divider, which we treat as m2. */
  249. .m1 = { .min = 0, .max = 0 },
  250. .m2 = { .min = 0, .max = 254 },
  251. .p = { .min = 5, .max = 80 },
  252. .p1 = { .min = 1, .max = 8 },
  253. .p2 = { .dot_limit = 200000,
  254. .p2_slow = 10, .p2_fast = 5 },
  255. };
  256. static const intel_limit_t intel_limits_pineview_lvds = {
  257. .dot = { .min = 20000, .max = 400000 },
  258. .vco = { .min = 1700000, .max = 3500000 },
  259. .n = { .min = 3, .max = 6 },
  260. .m = { .min = 2, .max = 256 },
  261. .m1 = { .min = 0, .max = 0 },
  262. .m2 = { .min = 0, .max = 254 },
  263. .p = { .min = 7, .max = 112 },
  264. .p1 = { .min = 1, .max = 8 },
  265. .p2 = { .dot_limit = 112000,
  266. .p2_slow = 14, .p2_fast = 14 },
  267. };
  268. /* Ironlake / Sandybridge
  269. *
  270. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  271. * the range value for them is (actual_value - 2).
  272. */
  273. static const intel_limit_t intel_limits_ironlake_dac = {
  274. .dot = { .min = 25000, .max = 350000 },
  275. .vco = { .min = 1760000, .max = 3510000 },
  276. .n = { .min = 1, .max = 5 },
  277. .m = { .min = 79, .max = 127 },
  278. .m1 = { .min = 12, .max = 22 },
  279. .m2 = { .min = 5, .max = 9 },
  280. .p = { .min = 5, .max = 80 },
  281. .p1 = { .min = 1, .max = 8 },
  282. .p2 = { .dot_limit = 225000,
  283. .p2_slow = 10, .p2_fast = 5 },
  284. };
  285. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  286. .dot = { .min = 25000, .max = 350000 },
  287. .vco = { .min = 1760000, .max = 3510000 },
  288. .n = { .min = 1, .max = 3 },
  289. .m = { .min = 79, .max = 118 },
  290. .m1 = { .min = 12, .max = 22 },
  291. .m2 = { .min = 5, .max = 9 },
  292. .p = { .min = 28, .max = 112 },
  293. .p1 = { .min = 2, .max = 8 },
  294. .p2 = { .dot_limit = 225000,
  295. .p2_slow = 14, .p2_fast = 14 },
  296. };
  297. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  298. .dot = { .min = 25000, .max = 350000 },
  299. .vco = { .min = 1760000, .max = 3510000 },
  300. .n = { .min = 1, .max = 3 },
  301. .m = { .min = 79, .max = 127 },
  302. .m1 = { .min = 12, .max = 22 },
  303. .m2 = { .min = 5, .max = 9 },
  304. .p = { .min = 14, .max = 56 },
  305. .p1 = { .min = 2, .max = 8 },
  306. .p2 = { .dot_limit = 225000,
  307. .p2_slow = 7, .p2_fast = 7 },
  308. };
  309. /* LVDS 100mhz refclk limits. */
  310. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  311. .dot = { .min = 25000, .max = 350000 },
  312. .vco = { .min = 1760000, .max = 3510000 },
  313. .n = { .min = 1, .max = 2 },
  314. .m = { .min = 79, .max = 126 },
  315. .m1 = { .min = 12, .max = 22 },
  316. .m2 = { .min = 5, .max = 9 },
  317. .p = { .min = 28, .max = 112 },
  318. .p1 = { .min = 2, .max = 8 },
  319. .p2 = { .dot_limit = 225000,
  320. .p2_slow = 14, .p2_fast = 14 },
  321. };
  322. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  323. .dot = { .min = 25000, .max = 350000 },
  324. .vco = { .min = 1760000, .max = 3510000 },
  325. .n = { .min = 1, .max = 3 },
  326. .m = { .min = 79, .max = 126 },
  327. .m1 = { .min = 12, .max = 22 },
  328. .m2 = { .min = 5, .max = 9 },
  329. .p = { .min = 14, .max = 42 },
  330. .p1 = { .min = 2, .max = 6 },
  331. .p2 = { .dot_limit = 225000,
  332. .p2_slow = 7, .p2_fast = 7 },
  333. };
  334. static const intel_limit_t intel_limits_vlv = {
  335. /*
  336. * These are the data rate limits (measured in fast clocks)
  337. * since those are the strictest limits we have. The fast
  338. * clock and actual rate limits are more relaxed, so checking
  339. * them would make no difference.
  340. */
  341. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  342. .vco = { .min = 4000000, .max = 6000000 },
  343. .n = { .min = 1, .max = 7 },
  344. .m1 = { .min = 2, .max = 3 },
  345. .m2 = { .min = 11, .max = 156 },
  346. .p1 = { .min = 2, .max = 3 },
  347. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  348. };
  349. static const intel_limit_t intel_limits_chv = {
  350. /*
  351. * These are the data rate limits (measured in fast clocks)
  352. * since those are the strictest limits we have. The fast
  353. * clock and actual rate limits are more relaxed, so checking
  354. * them would make no difference.
  355. */
  356. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  357. .vco = { .min = 4860000, .max = 6700000 },
  358. .n = { .min = 1, .max = 1 },
  359. .m1 = { .min = 2, .max = 2 },
  360. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  361. .p1 = { .min = 2, .max = 4 },
  362. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  363. };
  364. static void vlv_clock(int refclk, intel_clock_t *clock)
  365. {
  366. clock->m = clock->m1 * clock->m2;
  367. clock->p = clock->p1 * clock->p2;
  368. if (WARN_ON(clock->n == 0 || clock->p == 0))
  369. return;
  370. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  371. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  372. }
  373. /**
  374. * Returns whether any output on the specified pipe is of the specified type
  375. */
  376. static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
  377. {
  378. struct drm_device *dev = crtc->dev;
  379. struct intel_encoder *encoder;
  380. for_each_encoder_on_crtc(dev, crtc, encoder)
  381. if (encoder->type == type)
  382. return true;
  383. return false;
  384. }
  385. static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
  386. int refclk)
  387. {
  388. struct drm_device *dev = crtc->dev;
  389. const intel_limit_t *limit;
  390. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  391. if (intel_is_dual_link_lvds(dev)) {
  392. if (refclk == 100000)
  393. limit = &intel_limits_ironlake_dual_lvds_100m;
  394. else
  395. limit = &intel_limits_ironlake_dual_lvds;
  396. } else {
  397. if (refclk == 100000)
  398. limit = &intel_limits_ironlake_single_lvds_100m;
  399. else
  400. limit = &intel_limits_ironlake_single_lvds;
  401. }
  402. } else
  403. limit = &intel_limits_ironlake_dac;
  404. return limit;
  405. }
  406. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  407. {
  408. struct drm_device *dev = crtc->dev;
  409. const intel_limit_t *limit;
  410. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  411. if (intel_is_dual_link_lvds(dev))
  412. limit = &intel_limits_g4x_dual_channel_lvds;
  413. else
  414. limit = &intel_limits_g4x_single_channel_lvds;
  415. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  416. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  417. limit = &intel_limits_g4x_hdmi;
  418. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  419. limit = &intel_limits_g4x_sdvo;
  420. } else /* The option is for other outputs */
  421. limit = &intel_limits_i9xx_sdvo;
  422. return limit;
  423. }
  424. static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
  425. {
  426. struct drm_device *dev = crtc->dev;
  427. const intel_limit_t *limit;
  428. if (HAS_PCH_SPLIT(dev))
  429. limit = intel_ironlake_limit(crtc, refclk);
  430. else if (IS_G4X(dev)) {
  431. limit = intel_g4x_limit(crtc);
  432. } else if (IS_PINEVIEW(dev)) {
  433. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  434. limit = &intel_limits_pineview_lvds;
  435. else
  436. limit = &intel_limits_pineview_sdvo;
  437. } else if (IS_CHERRYVIEW(dev)) {
  438. limit = &intel_limits_chv;
  439. } else if (IS_VALLEYVIEW(dev)) {
  440. limit = &intel_limits_vlv;
  441. } else if (!IS_GEN2(dev)) {
  442. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  443. limit = &intel_limits_i9xx_lvds;
  444. else
  445. limit = &intel_limits_i9xx_sdvo;
  446. } else {
  447. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  448. limit = &intel_limits_i8xx_lvds;
  449. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
  450. limit = &intel_limits_i8xx_dvo;
  451. else
  452. limit = &intel_limits_i8xx_dac;
  453. }
  454. return limit;
  455. }
  456. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  457. static void pineview_clock(int refclk, intel_clock_t *clock)
  458. {
  459. clock->m = clock->m2 + 2;
  460. clock->p = clock->p1 * clock->p2;
  461. if (WARN_ON(clock->n == 0 || clock->p == 0))
  462. return;
  463. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  464. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  465. }
  466. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  467. {
  468. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  469. }
  470. static void i9xx_clock(int refclk, intel_clock_t *clock)
  471. {
  472. clock->m = i9xx_dpll_compute_m(clock);
  473. clock->p = clock->p1 * clock->p2;
  474. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  475. return;
  476. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  477. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  478. }
  479. static void chv_clock(int refclk, intel_clock_t *clock)
  480. {
  481. clock->m = clock->m1 * clock->m2;
  482. clock->p = clock->p1 * clock->p2;
  483. if (WARN_ON(clock->n == 0 || clock->p == 0))
  484. return;
  485. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  486. clock->n << 22);
  487. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  488. }
  489. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  490. /**
  491. * Returns whether the given set of divisors are valid for a given refclk with
  492. * the given connectors.
  493. */
  494. static bool intel_PLL_is_valid(struct drm_device *dev,
  495. const intel_limit_t *limit,
  496. const intel_clock_t *clock)
  497. {
  498. if (clock->n < limit->n.min || limit->n.max < clock->n)
  499. INTELPllInvalid("n out of range\n");
  500. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  501. INTELPllInvalid("p1 out of range\n");
  502. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  503. INTELPllInvalid("m2 out of range\n");
  504. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  505. INTELPllInvalid("m1 out of range\n");
  506. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
  507. if (clock->m1 <= clock->m2)
  508. INTELPllInvalid("m1 <= m2\n");
  509. if (!IS_VALLEYVIEW(dev)) {
  510. if (clock->p < limit->p.min || limit->p.max < clock->p)
  511. INTELPllInvalid("p out of range\n");
  512. if (clock->m < limit->m.min || limit->m.max < clock->m)
  513. INTELPllInvalid("m out of range\n");
  514. }
  515. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  516. INTELPllInvalid("vco out of range\n");
  517. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  518. * connector, etc., rather than just a single range.
  519. */
  520. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  521. INTELPllInvalid("dot out of range\n");
  522. return true;
  523. }
  524. static bool
  525. i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  526. int target, int refclk, intel_clock_t *match_clock,
  527. intel_clock_t *best_clock)
  528. {
  529. struct drm_device *dev = crtc->dev;
  530. intel_clock_t clock;
  531. int err = target;
  532. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  533. /*
  534. * For LVDS just rely on its current settings for dual-channel.
  535. * We haven't figured out how to reliably set up different
  536. * single/dual channel state, if we even can.
  537. */
  538. if (intel_is_dual_link_lvds(dev))
  539. clock.p2 = limit->p2.p2_fast;
  540. else
  541. clock.p2 = limit->p2.p2_slow;
  542. } else {
  543. if (target < limit->p2.dot_limit)
  544. clock.p2 = limit->p2.p2_slow;
  545. else
  546. clock.p2 = limit->p2.p2_fast;
  547. }
  548. memset(best_clock, 0, sizeof(*best_clock));
  549. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  550. clock.m1++) {
  551. for (clock.m2 = limit->m2.min;
  552. clock.m2 <= limit->m2.max; clock.m2++) {
  553. if (clock.m2 >= clock.m1)
  554. break;
  555. for (clock.n = limit->n.min;
  556. clock.n <= limit->n.max; clock.n++) {
  557. for (clock.p1 = limit->p1.min;
  558. clock.p1 <= limit->p1.max; clock.p1++) {
  559. int this_err;
  560. i9xx_clock(refclk, &clock);
  561. if (!intel_PLL_is_valid(dev, limit,
  562. &clock))
  563. continue;
  564. if (match_clock &&
  565. clock.p != match_clock->p)
  566. continue;
  567. this_err = abs(clock.dot - target);
  568. if (this_err < err) {
  569. *best_clock = clock;
  570. err = this_err;
  571. }
  572. }
  573. }
  574. }
  575. }
  576. return (err != target);
  577. }
  578. static bool
  579. pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  580. int target, int refclk, intel_clock_t *match_clock,
  581. intel_clock_t *best_clock)
  582. {
  583. struct drm_device *dev = crtc->dev;
  584. intel_clock_t clock;
  585. int err = target;
  586. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  587. /*
  588. * For LVDS just rely on its current settings for dual-channel.
  589. * We haven't figured out how to reliably set up different
  590. * single/dual channel state, if we even can.
  591. */
  592. if (intel_is_dual_link_lvds(dev))
  593. clock.p2 = limit->p2.p2_fast;
  594. else
  595. clock.p2 = limit->p2.p2_slow;
  596. } else {
  597. if (target < limit->p2.dot_limit)
  598. clock.p2 = limit->p2.p2_slow;
  599. else
  600. clock.p2 = limit->p2.p2_fast;
  601. }
  602. memset(best_clock, 0, sizeof(*best_clock));
  603. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  604. clock.m1++) {
  605. for (clock.m2 = limit->m2.min;
  606. clock.m2 <= limit->m2.max; clock.m2++) {
  607. for (clock.n = limit->n.min;
  608. clock.n <= limit->n.max; clock.n++) {
  609. for (clock.p1 = limit->p1.min;
  610. clock.p1 <= limit->p1.max; clock.p1++) {
  611. int this_err;
  612. pineview_clock(refclk, &clock);
  613. if (!intel_PLL_is_valid(dev, limit,
  614. &clock))
  615. continue;
  616. if (match_clock &&
  617. clock.p != match_clock->p)
  618. continue;
  619. this_err = abs(clock.dot - target);
  620. if (this_err < err) {
  621. *best_clock = clock;
  622. err = this_err;
  623. }
  624. }
  625. }
  626. }
  627. }
  628. return (err != target);
  629. }
  630. static bool
  631. g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  632. int target, int refclk, intel_clock_t *match_clock,
  633. intel_clock_t *best_clock)
  634. {
  635. struct drm_device *dev = crtc->dev;
  636. intel_clock_t clock;
  637. int max_n;
  638. bool found;
  639. /* approximately equals target * 0.00585 */
  640. int err_most = (target >> 8) + (target >> 9);
  641. found = false;
  642. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  643. if (intel_is_dual_link_lvds(dev))
  644. clock.p2 = limit->p2.p2_fast;
  645. else
  646. clock.p2 = limit->p2.p2_slow;
  647. } else {
  648. if (target < limit->p2.dot_limit)
  649. clock.p2 = limit->p2.p2_slow;
  650. else
  651. clock.p2 = limit->p2.p2_fast;
  652. }
  653. memset(best_clock, 0, sizeof(*best_clock));
  654. max_n = limit->n.max;
  655. /* based on hardware requirement, prefer smaller n to precision */
  656. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  657. /* based on hardware requirement, prefere larger m1,m2 */
  658. for (clock.m1 = limit->m1.max;
  659. clock.m1 >= limit->m1.min; clock.m1--) {
  660. for (clock.m2 = limit->m2.max;
  661. clock.m2 >= limit->m2.min; clock.m2--) {
  662. for (clock.p1 = limit->p1.max;
  663. clock.p1 >= limit->p1.min; clock.p1--) {
  664. int this_err;
  665. i9xx_clock(refclk, &clock);
  666. if (!intel_PLL_is_valid(dev, limit,
  667. &clock))
  668. continue;
  669. this_err = abs(clock.dot - target);
  670. if (this_err < err_most) {
  671. *best_clock = clock;
  672. err_most = this_err;
  673. max_n = clock.n;
  674. found = true;
  675. }
  676. }
  677. }
  678. }
  679. }
  680. return found;
  681. }
  682. static bool
  683. vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  684. int target, int refclk, intel_clock_t *match_clock,
  685. intel_clock_t *best_clock)
  686. {
  687. struct drm_device *dev = crtc->dev;
  688. intel_clock_t clock;
  689. unsigned int bestppm = 1000000;
  690. /* min update 19.2 MHz */
  691. int max_n = min(limit->n.max, refclk / 19200);
  692. bool found = false;
  693. target *= 5; /* fast clock */
  694. memset(best_clock, 0, sizeof(*best_clock));
  695. /* based on hardware requirement, prefer smaller n to precision */
  696. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  697. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  698. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  699. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  700. clock.p = clock.p1 * clock.p2;
  701. /* based on hardware requirement, prefer bigger m1,m2 values */
  702. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  703. unsigned int ppm, diff;
  704. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  705. refclk * clock.m1);
  706. vlv_clock(refclk, &clock);
  707. if (!intel_PLL_is_valid(dev, limit,
  708. &clock))
  709. continue;
  710. diff = abs(clock.dot - target);
  711. ppm = div_u64(1000000ULL * diff, target);
  712. if (ppm < 100 && clock.p > best_clock->p) {
  713. bestppm = 0;
  714. *best_clock = clock;
  715. found = true;
  716. }
  717. if (bestppm >= 10 && ppm < bestppm - 10) {
  718. bestppm = ppm;
  719. *best_clock = clock;
  720. found = true;
  721. }
  722. }
  723. }
  724. }
  725. }
  726. return found;
  727. }
  728. static bool
  729. chv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  730. int target, int refclk, intel_clock_t *match_clock,
  731. intel_clock_t *best_clock)
  732. {
  733. struct drm_device *dev = crtc->dev;
  734. intel_clock_t clock;
  735. uint64_t m2;
  736. int found = false;
  737. memset(best_clock, 0, sizeof(*best_clock));
  738. /*
  739. * Based on hardware doc, the n always set to 1, and m1 always
  740. * set to 2. If requires to support 200Mhz refclk, we need to
  741. * revisit this because n may not 1 anymore.
  742. */
  743. clock.n = 1, clock.m1 = 2;
  744. target *= 5; /* fast clock */
  745. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  746. for (clock.p2 = limit->p2.p2_fast;
  747. clock.p2 >= limit->p2.p2_slow;
  748. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  749. clock.p = clock.p1 * clock.p2;
  750. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  751. clock.n) << 22, refclk * clock.m1);
  752. if (m2 > INT_MAX/clock.m1)
  753. continue;
  754. clock.m2 = m2;
  755. chv_clock(refclk, &clock);
  756. if (!intel_PLL_is_valid(dev, limit, &clock))
  757. continue;
  758. /* based on hardware requirement, prefer bigger p
  759. */
  760. if (clock.p > best_clock->p) {
  761. *best_clock = clock;
  762. found = true;
  763. }
  764. }
  765. }
  766. return found;
  767. }
  768. bool intel_crtc_active(struct drm_crtc *crtc)
  769. {
  770. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  771. /* Be paranoid as we can arrive here with only partial
  772. * state retrieved from the hardware during setup.
  773. *
  774. * We can ditch the adjusted_mode.crtc_clock check as soon
  775. * as Haswell has gained clock readout/fastboot support.
  776. *
  777. * We can ditch the crtc->primary->fb check as soon as we can
  778. * properly reconstruct framebuffers.
  779. */
  780. return intel_crtc->active && crtc->primary->fb &&
  781. intel_crtc->config.adjusted_mode.crtc_clock;
  782. }
  783. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  784. enum pipe pipe)
  785. {
  786. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  787. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  788. return intel_crtc->config.cpu_transcoder;
  789. }
  790. static void g4x_wait_for_vblank(struct drm_device *dev, int pipe)
  791. {
  792. struct drm_i915_private *dev_priv = dev->dev_private;
  793. u32 frame, frame_reg = PIPE_FRMCOUNT_GM45(pipe);
  794. frame = I915_READ(frame_reg);
  795. if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
  796. WARN(1, "vblank wait on pipe %c timed out\n",
  797. pipe_name(pipe));
  798. }
  799. /**
  800. * intel_wait_for_vblank - wait for vblank on a given pipe
  801. * @dev: drm device
  802. * @pipe: pipe to wait for
  803. *
  804. * Wait for vblank to occur on a given pipe. Needed for various bits of
  805. * mode setting code.
  806. */
  807. void intel_wait_for_vblank(struct drm_device *dev, int pipe)
  808. {
  809. struct drm_i915_private *dev_priv = dev->dev_private;
  810. int pipestat_reg = PIPESTAT(pipe);
  811. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
  812. g4x_wait_for_vblank(dev, pipe);
  813. return;
  814. }
  815. /* Clear existing vblank status. Note this will clear any other
  816. * sticky status fields as well.
  817. *
  818. * This races with i915_driver_irq_handler() with the result
  819. * that either function could miss a vblank event. Here it is not
  820. * fatal, as we will either wait upon the next vblank interrupt or
  821. * timeout. Generally speaking intel_wait_for_vblank() is only
  822. * called during modeset at which time the GPU should be idle and
  823. * should *not* be performing page flips and thus not waiting on
  824. * vblanks...
  825. * Currently, the result of us stealing a vblank from the irq
  826. * handler is that a single frame will be skipped during swapbuffers.
  827. */
  828. I915_WRITE(pipestat_reg,
  829. I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
  830. /* Wait for vblank interrupt bit to set */
  831. if (wait_for(I915_READ(pipestat_reg) &
  832. PIPE_VBLANK_INTERRUPT_STATUS,
  833. 50))
  834. DRM_DEBUG_KMS("vblank wait on pipe %c timed out\n",
  835. pipe_name(pipe));
  836. }
  837. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  838. {
  839. struct drm_i915_private *dev_priv = dev->dev_private;
  840. u32 reg = PIPEDSL(pipe);
  841. u32 line1, line2;
  842. u32 line_mask;
  843. if (IS_GEN2(dev))
  844. line_mask = DSL_LINEMASK_GEN2;
  845. else
  846. line_mask = DSL_LINEMASK_GEN3;
  847. line1 = I915_READ(reg) & line_mask;
  848. mdelay(5);
  849. line2 = I915_READ(reg) & line_mask;
  850. return line1 == line2;
  851. }
  852. /*
  853. * intel_wait_for_pipe_off - wait for pipe to turn off
  854. * @crtc: crtc whose pipe to wait for
  855. *
  856. * After disabling a pipe, we can't wait for vblank in the usual way,
  857. * spinning on the vblank interrupt status bit, since we won't actually
  858. * see an interrupt when the pipe is disabled.
  859. *
  860. * On Gen4 and above:
  861. * wait for the pipe register state bit to turn off
  862. *
  863. * Otherwise:
  864. * wait for the display line value to settle (it usually
  865. * ends up stopping at the start of the next frame).
  866. *
  867. */
  868. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  869. {
  870. struct drm_device *dev = crtc->base.dev;
  871. struct drm_i915_private *dev_priv = dev->dev_private;
  872. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  873. enum pipe pipe = crtc->pipe;
  874. if (INTEL_INFO(dev)->gen >= 4) {
  875. int reg = PIPECONF(cpu_transcoder);
  876. /* Wait for the Pipe State to go off */
  877. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  878. 100))
  879. WARN(1, "pipe_off wait timed out\n");
  880. } else {
  881. /* Wait for the display line to settle */
  882. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  883. WARN(1, "pipe_off wait timed out\n");
  884. }
  885. }
  886. /*
  887. * ibx_digital_port_connected - is the specified port connected?
  888. * @dev_priv: i915 private structure
  889. * @port: the port to test
  890. *
  891. * Returns true if @port is connected, false otherwise.
  892. */
  893. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  894. struct intel_digital_port *port)
  895. {
  896. u32 bit;
  897. if (HAS_PCH_IBX(dev_priv->dev)) {
  898. switch (port->port) {
  899. case PORT_B:
  900. bit = SDE_PORTB_HOTPLUG;
  901. break;
  902. case PORT_C:
  903. bit = SDE_PORTC_HOTPLUG;
  904. break;
  905. case PORT_D:
  906. bit = SDE_PORTD_HOTPLUG;
  907. break;
  908. default:
  909. return true;
  910. }
  911. } else {
  912. switch (port->port) {
  913. case PORT_B:
  914. bit = SDE_PORTB_HOTPLUG_CPT;
  915. break;
  916. case PORT_C:
  917. bit = SDE_PORTC_HOTPLUG_CPT;
  918. break;
  919. case PORT_D:
  920. bit = SDE_PORTD_HOTPLUG_CPT;
  921. break;
  922. default:
  923. return true;
  924. }
  925. }
  926. return I915_READ(SDEISR) & bit;
  927. }
  928. static const char *state_string(bool enabled)
  929. {
  930. return enabled ? "on" : "off";
  931. }
  932. /* Only for pre-ILK configs */
  933. void assert_pll(struct drm_i915_private *dev_priv,
  934. enum pipe pipe, bool state)
  935. {
  936. int reg;
  937. u32 val;
  938. bool cur_state;
  939. reg = DPLL(pipe);
  940. val = I915_READ(reg);
  941. cur_state = !!(val & DPLL_VCO_ENABLE);
  942. WARN(cur_state != state,
  943. "PLL state assertion failure (expected %s, current %s)\n",
  944. state_string(state), state_string(cur_state));
  945. }
  946. /* XXX: the dsi pll is shared between MIPI DSI ports */
  947. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  948. {
  949. u32 val;
  950. bool cur_state;
  951. mutex_lock(&dev_priv->dpio_lock);
  952. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  953. mutex_unlock(&dev_priv->dpio_lock);
  954. cur_state = val & DSI_PLL_VCO_EN;
  955. WARN(cur_state != state,
  956. "DSI PLL state assertion failure (expected %s, current %s)\n",
  957. state_string(state), state_string(cur_state));
  958. }
  959. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  960. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  961. struct intel_shared_dpll *
  962. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  963. {
  964. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  965. if (crtc->config.shared_dpll < 0)
  966. return NULL;
  967. return &dev_priv->shared_dplls[crtc->config.shared_dpll];
  968. }
  969. /* For ILK+ */
  970. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  971. struct intel_shared_dpll *pll,
  972. bool state)
  973. {
  974. bool cur_state;
  975. struct intel_dpll_hw_state hw_state;
  976. if (WARN (!pll,
  977. "asserting DPLL %s with no DPLL\n", state_string(state)))
  978. return;
  979. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  980. WARN(cur_state != state,
  981. "%s assertion failure (expected %s, current %s)\n",
  982. pll->name, state_string(state), state_string(cur_state));
  983. }
  984. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  985. enum pipe pipe, bool state)
  986. {
  987. int reg;
  988. u32 val;
  989. bool cur_state;
  990. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  991. pipe);
  992. if (HAS_DDI(dev_priv->dev)) {
  993. /* DDI does not have a specific FDI_TX register */
  994. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  995. val = I915_READ(reg);
  996. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  997. } else {
  998. reg = FDI_TX_CTL(pipe);
  999. val = I915_READ(reg);
  1000. cur_state = !!(val & FDI_TX_ENABLE);
  1001. }
  1002. WARN(cur_state != state,
  1003. "FDI TX state assertion failure (expected %s, current %s)\n",
  1004. state_string(state), state_string(cur_state));
  1005. }
  1006. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1007. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1008. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1009. enum pipe pipe, bool state)
  1010. {
  1011. int reg;
  1012. u32 val;
  1013. bool cur_state;
  1014. reg = FDI_RX_CTL(pipe);
  1015. val = I915_READ(reg);
  1016. cur_state = !!(val & FDI_RX_ENABLE);
  1017. WARN(cur_state != state,
  1018. "FDI RX state assertion failure (expected %s, current %s)\n",
  1019. state_string(state), state_string(cur_state));
  1020. }
  1021. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1022. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1023. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1024. enum pipe pipe)
  1025. {
  1026. int reg;
  1027. u32 val;
  1028. /* ILK FDI PLL is always enabled */
  1029. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  1030. return;
  1031. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1032. if (HAS_DDI(dev_priv->dev))
  1033. return;
  1034. reg = FDI_TX_CTL(pipe);
  1035. val = I915_READ(reg);
  1036. WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1037. }
  1038. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1039. enum pipe pipe, bool state)
  1040. {
  1041. int reg;
  1042. u32 val;
  1043. bool cur_state;
  1044. reg = FDI_RX_CTL(pipe);
  1045. val = I915_READ(reg);
  1046. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1047. WARN(cur_state != state,
  1048. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1049. state_string(state), state_string(cur_state));
  1050. }
  1051. static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1052. enum pipe pipe)
  1053. {
  1054. struct drm_device *dev = dev_priv->dev;
  1055. int pp_reg;
  1056. u32 val;
  1057. enum pipe panel_pipe = PIPE_A;
  1058. bool locked = true;
  1059. if (WARN_ON(HAS_DDI(dev)))
  1060. return;
  1061. if (HAS_PCH_SPLIT(dev)) {
  1062. u32 port_sel;
  1063. pp_reg = PCH_PP_CONTROL;
  1064. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1065. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1066. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1067. panel_pipe = PIPE_B;
  1068. /* XXX: else fix for eDP */
  1069. } else if (IS_VALLEYVIEW(dev)) {
  1070. /* presumably write lock depends on pipe, not port select */
  1071. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1072. panel_pipe = pipe;
  1073. } else {
  1074. pp_reg = PP_CONTROL;
  1075. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1076. panel_pipe = PIPE_B;
  1077. }
  1078. val = I915_READ(pp_reg);
  1079. if (!(val & PANEL_POWER_ON) ||
  1080. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1081. locked = false;
  1082. WARN(panel_pipe == pipe && locked,
  1083. "panel assertion failure, pipe %c regs locked\n",
  1084. pipe_name(pipe));
  1085. }
  1086. static void assert_cursor(struct drm_i915_private *dev_priv,
  1087. enum pipe pipe, bool state)
  1088. {
  1089. struct drm_device *dev = dev_priv->dev;
  1090. bool cur_state;
  1091. if (IS_845G(dev) || IS_I865G(dev))
  1092. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1093. else
  1094. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1095. WARN(cur_state != state,
  1096. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1097. pipe_name(pipe), state_string(state), state_string(cur_state));
  1098. }
  1099. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1100. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1101. void assert_pipe(struct drm_i915_private *dev_priv,
  1102. enum pipe pipe, bool state)
  1103. {
  1104. int reg;
  1105. u32 val;
  1106. bool cur_state;
  1107. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1108. pipe);
  1109. /* if we need the pipe quirk it must be always on */
  1110. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1111. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1112. state = true;
  1113. if (!intel_display_power_enabled(dev_priv,
  1114. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1115. cur_state = false;
  1116. } else {
  1117. reg = PIPECONF(cpu_transcoder);
  1118. val = I915_READ(reg);
  1119. cur_state = !!(val & PIPECONF_ENABLE);
  1120. }
  1121. WARN(cur_state != state,
  1122. "pipe %c assertion failure (expected %s, current %s)\n",
  1123. pipe_name(pipe), state_string(state), state_string(cur_state));
  1124. }
  1125. static void assert_plane(struct drm_i915_private *dev_priv,
  1126. enum plane plane, bool state)
  1127. {
  1128. int reg;
  1129. u32 val;
  1130. bool cur_state;
  1131. reg = DSPCNTR(plane);
  1132. val = I915_READ(reg);
  1133. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1134. WARN(cur_state != state,
  1135. "plane %c assertion failure (expected %s, current %s)\n",
  1136. plane_name(plane), state_string(state), state_string(cur_state));
  1137. }
  1138. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1139. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1140. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1141. enum pipe pipe)
  1142. {
  1143. struct drm_device *dev = dev_priv->dev;
  1144. int reg, i;
  1145. u32 val;
  1146. int cur_pipe;
  1147. /* Primary planes are fixed to pipes on gen4+ */
  1148. if (INTEL_INFO(dev)->gen >= 4) {
  1149. reg = DSPCNTR(pipe);
  1150. val = I915_READ(reg);
  1151. WARN(val & DISPLAY_PLANE_ENABLE,
  1152. "plane %c assertion failure, should be disabled but not\n",
  1153. plane_name(pipe));
  1154. return;
  1155. }
  1156. /* Need to check both planes against the pipe */
  1157. for_each_pipe(dev_priv, i) {
  1158. reg = DSPCNTR(i);
  1159. val = I915_READ(reg);
  1160. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1161. DISPPLANE_SEL_PIPE_SHIFT;
  1162. WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1163. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1164. plane_name(i), pipe_name(pipe));
  1165. }
  1166. }
  1167. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1168. enum pipe pipe)
  1169. {
  1170. struct drm_device *dev = dev_priv->dev;
  1171. int reg, sprite;
  1172. u32 val;
  1173. if (IS_VALLEYVIEW(dev)) {
  1174. for_each_sprite(pipe, sprite) {
  1175. reg = SPCNTR(pipe, sprite);
  1176. val = I915_READ(reg);
  1177. WARN(val & SP_ENABLE,
  1178. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1179. sprite_name(pipe, sprite), pipe_name(pipe));
  1180. }
  1181. } else if (INTEL_INFO(dev)->gen >= 7) {
  1182. reg = SPRCTL(pipe);
  1183. val = I915_READ(reg);
  1184. WARN(val & SPRITE_ENABLE,
  1185. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1186. plane_name(pipe), pipe_name(pipe));
  1187. } else if (INTEL_INFO(dev)->gen >= 5) {
  1188. reg = DVSCNTR(pipe);
  1189. val = I915_READ(reg);
  1190. WARN(val & DVS_ENABLE,
  1191. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1192. plane_name(pipe), pipe_name(pipe));
  1193. }
  1194. }
  1195. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1196. {
  1197. if (WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1198. drm_crtc_vblank_put(crtc);
  1199. }
  1200. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1201. {
  1202. u32 val;
  1203. bool enabled;
  1204. WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1205. val = I915_READ(PCH_DREF_CONTROL);
  1206. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1207. DREF_SUPERSPREAD_SOURCE_MASK));
  1208. WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1209. }
  1210. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1211. enum pipe pipe)
  1212. {
  1213. int reg;
  1214. u32 val;
  1215. bool enabled;
  1216. reg = PCH_TRANSCONF(pipe);
  1217. val = I915_READ(reg);
  1218. enabled = !!(val & TRANS_ENABLE);
  1219. WARN(enabled,
  1220. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1221. pipe_name(pipe));
  1222. }
  1223. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1224. enum pipe pipe, u32 port_sel, u32 val)
  1225. {
  1226. if ((val & DP_PORT_EN) == 0)
  1227. return false;
  1228. if (HAS_PCH_CPT(dev_priv->dev)) {
  1229. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1230. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1231. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1232. return false;
  1233. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1234. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1235. return false;
  1236. } else {
  1237. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1238. return false;
  1239. }
  1240. return true;
  1241. }
  1242. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1243. enum pipe pipe, u32 val)
  1244. {
  1245. if ((val & SDVO_ENABLE) == 0)
  1246. return false;
  1247. if (HAS_PCH_CPT(dev_priv->dev)) {
  1248. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1249. return false;
  1250. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1251. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1252. return false;
  1253. } else {
  1254. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1255. return false;
  1256. }
  1257. return true;
  1258. }
  1259. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1260. enum pipe pipe, u32 val)
  1261. {
  1262. if ((val & LVDS_PORT_EN) == 0)
  1263. return false;
  1264. if (HAS_PCH_CPT(dev_priv->dev)) {
  1265. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1266. return false;
  1267. } else {
  1268. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1269. return false;
  1270. }
  1271. return true;
  1272. }
  1273. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1274. enum pipe pipe, u32 val)
  1275. {
  1276. if ((val & ADPA_DAC_ENABLE) == 0)
  1277. return false;
  1278. if (HAS_PCH_CPT(dev_priv->dev)) {
  1279. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1280. return false;
  1281. } else {
  1282. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1283. return false;
  1284. }
  1285. return true;
  1286. }
  1287. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1288. enum pipe pipe, int reg, u32 port_sel)
  1289. {
  1290. u32 val = I915_READ(reg);
  1291. WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1292. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1293. reg, pipe_name(pipe));
  1294. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1295. && (val & DP_PIPEB_SELECT),
  1296. "IBX PCH dp port still using transcoder B\n");
  1297. }
  1298. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1299. enum pipe pipe, int reg)
  1300. {
  1301. u32 val = I915_READ(reg);
  1302. WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1303. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1304. reg, pipe_name(pipe));
  1305. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1306. && (val & SDVO_PIPE_B_SELECT),
  1307. "IBX PCH hdmi port still using transcoder B\n");
  1308. }
  1309. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1310. enum pipe pipe)
  1311. {
  1312. int reg;
  1313. u32 val;
  1314. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1315. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1316. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1317. reg = PCH_ADPA;
  1318. val = I915_READ(reg);
  1319. WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1320. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1321. pipe_name(pipe));
  1322. reg = PCH_LVDS;
  1323. val = I915_READ(reg);
  1324. WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1325. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1326. pipe_name(pipe));
  1327. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1328. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1329. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1330. }
  1331. static void intel_init_dpio(struct drm_device *dev)
  1332. {
  1333. struct drm_i915_private *dev_priv = dev->dev_private;
  1334. if (!IS_VALLEYVIEW(dev))
  1335. return;
  1336. /*
  1337. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1338. * CHV x1 PHY (DP/HDMI D)
  1339. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1340. */
  1341. if (IS_CHERRYVIEW(dev)) {
  1342. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1343. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1344. } else {
  1345. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1346. }
  1347. }
  1348. static void vlv_enable_pll(struct intel_crtc *crtc)
  1349. {
  1350. struct drm_device *dev = crtc->base.dev;
  1351. struct drm_i915_private *dev_priv = dev->dev_private;
  1352. int reg = DPLL(crtc->pipe);
  1353. u32 dpll = crtc->config.dpll_hw_state.dpll;
  1354. assert_pipe_disabled(dev_priv, crtc->pipe);
  1355. /* No really, not for ILK+ */
  1356. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1357. /* PLL is protected by panel, make sure we can write it */
  1358. if (IS_MOBILE(dev_priv->dev))
  1359. assert_panel_unlocked(dev_priv, crtc->pipe);
  1360. I915_WRITE(reg, dpll);
  1361. POSTING_READ(reg);
  1362. udelay(150);
  1363. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1364. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1365. I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
  1366. POSTING_READ(DPLL_MD(crtc->pipe));
  1367. /* We do this three times for luck */
  1368. I915_WRITE(reg, dpll);
  1369. POSTING_READ(reg);
  1370. udelay(150); /* wait for warmup */
  1371. I915_WRITE(reg, dpll);
  1372. POSTING_READ(reg);
  1373. udelay(150); /* wait for warmup */
  1374. I915_WRITE(reg, dpll);
  1375. POSTING_READ(reg);
  1376. udelay(150); /* wait for warmup */
  1377. }
  1378. static void chv_enable_pll(struct intel_crtc *crtc)
  1379. {
  1380. struct drm_device *dev = crtc->base.dev;
  1381. struct drm_i915_private *dev_priv = dev->dev_private;
  1382. int pipe = crtc->pipe;
  1383. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1384. u32 tmp;
  1385. assert_pipe_disabled(dev_priv, crtc->pipe);
  1386. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1387. mutex_lock(&dev_priv->dpio_lock);
  1388. /* Enable back the 10bit clock to display controller */
  1389. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1390. tmp |= DPIO_DCLKP_EN;
  1391. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1392. /*
  1393. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1394. */
  1395. udelay(1);
  1396. /* Enable PLL */
  1397. I915_WRITE(DPLL(pipe), crtc->config.dpll_hw_state.dpll);
  1398. /* Check PLL is locked */
  1399. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1400. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1401. /* not sure when this should be written */
  1402. I915_WRITE(DPLL_MD(pipe), crtc->config.dpll_hw_state.dpll_md);
  1403. POSTING_READ(DPLL_MD(pipe));
  1404. mutex_unlock(&dev_priv->dpio_lock);
  1405. }
  1406. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1407. {
  1408. struct drm_device *dev = crtc->base.dev;
  1409. struct drm_i915_private *dev_priv = dev->dev_private;
  1410. int reg = DPLL(crtc->pipe);
  1411. u32 dpll = crtc->config.dpll_hw_state.dpll;
  1412. assert_pipe_disabled(dev_priv, crtc->pipe);
  1413. /* No really, not for ILK+ */
  1414. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1415. /* PLL is protected by panel, make sure we can write it */
  1416. if (IS_MOBILE(dev) && !IS_I830(dev))
  1417. assert_panel_unlocked(dev_priv, crtc->pipe);
  1418. I915_WRITE(reg, dpll);
  1419. /* Wait for the clocks to stabilize. */
  1420. POSTING_READ(reg);
  1421. udelay(150);
  1422. if (INTEL_INFO(dev)->gen >= 4) {
  1423. I915_WRITE(DPLL_MD(crtc->pipe),
  1424. crtc->config.dpll_hw_state.dpll_md);
  1425. } else {
  1426. /* The pixel multiplier can only be updated once the
  1427. * DPLL is enabled and the clocks are stable.
  1428. *
  1429. * So write it again.
  1430. */
  1431. I915_WRITE(reg, dpll);
  1432. }
  1433. /* We do this three times for luck */
  1434. I915_WRITE(reg, dpll);
  1435. POSTING_READ(reg);
  1436. udelay(150); /* wait for warmup */
  1437. I915_WRITE(reg, dpll);
  1438. POSTING_READ(reg);
  1439. udelay(150); /* wait for warmup */
  1440. I915_WRITE(reg, dpll);
  1441. POSTING_READ(reg);
  1442. udelay(150); /* wait for warmup */
  1443. }
  1444. /**
  1445. * i9xx_disable_pll - disable a PLL
  1446. * @dev_priv: i915 private structure
  1447. * @pipe: pipe PLL to disable
  1448. *
  1449. * Disable the PLL for @pipe, making sure the pipe is off first.
  1450. *
  1451. * Note! This is for pre-ILK only.
  1452. */
  1453. static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1454. {
  1455. /* Don't disable pipe or pipe PLLs if needed */
  1456. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1457. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1458. return;
  1459. /* Make sure the pipe isn't still relying on us */
  1460. assert_pipe_disabled(dev_priv, pipe);
  1461. I915_WRITE(DPLL(pipe), 0);
  1462. POSTING_READ(DPLL(pipe));
  1463. }
  1464. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1465. {
  1466. u32 val = 0;
  1467. /* Make sure the pipe isn't still relying on us */
  1468. assert_pipe_disabled(dev_priv, pipe);
  1469. /*
  1470. * Leave integrated clock source and reference clock enabled for pipe B.
  1471. * The latter is needed for VGA hotplug / manual detection.
  1472. */
  1473. if (pipe == PIPE_B)
  1474. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1475. I915_WRITE(DPLL(pipe), val);
  1476. POSTING_READ(DPLL(pipe));
  1477. }
  1478. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1479. {
  1480. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1481. u32 val;
  1482. /* Make sure the pipe isn't still relying on us */
  1483. assert_pipe_disabled(dev_priv, pipe);
  1484. /* Set PLL en = 0 */
  1485. val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
  1486. if (pipe != PIPE_A)
  1487. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1488. I915_WRITE(DPLL(pipe), val);
  1489. POSTING_READ(DPLL(pipe));
  1490. mutex_lock(&dev_priv->dpio_lock);
  1491. /* Disable 10bit clock to display controller */
  1492. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1493. val &= ~DPIO_DCLKP_EN;
  1494. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1495. /* disable left/right clock distribution */
  1496. if (pipe != PIPE_B) {
  1497. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1498. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1499. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1500. } else {
  1501. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1502. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1503. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1504. }
  1505. mutex_unlock(&dev_priv->dpio_lock);
  1506. }
  1507. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1508. struct intel_digital_port *dport)
  1509. {
  1510. u32 port_mask;
  1511. int dpll_reg;
  1512. switch (dport->port) {
  1513. case PORT_B:
  1514. port_mask = DPLL_PORTB_READY_MASK;
  1515. dpll_reg = DPLL(0);
  1516. break;
  1517. case PORT_C:
  1518. port_mask = DPLL_PORTC_READY_MASK;
  1519. dpll_reg = DPLL(0);
  1520. break;
  1521. case PORT_D:
  1522. port_mask = DPLL_PORTD_READY_MASK;
  1523. dpll_reg = DPIO_PHY_STATUS;
  1524. break;
  1525. default:
  1526. BUG();
  1527. }
  1528. if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
  1529. WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
  1530. port_name(dport->port), I915_READ(dpll_reg));
  1531. }
  1532. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1533. {
  1534. struct drm_device *dev = crtc->base.dev;
  1535. struct drm_i915_private *dev_priv = dev->dev_private;
  1536. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1537. if (WARN_ON(pll == NULL))
  1538. return;
  1539. WARN_ON(!pll->refcount);
  1540. if (pll->active == 0) {
  1541. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1542. WARN_ON(pll->on);
  1543. assert_shared_dpll_disabled(dev_priv, pll);
  1544. pll->mode_set(dev_priv, pll);
  1545. }
  1546. }
  1547. /**
  1548. * intel_enable_shared_dpll - enable PCH PLL
  1549. * @dev_priv: i915 private structure
  1550. * @pipe: pipe PLL to enable
  1551. *
  1552. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1553. * drives the transcoder clock.
  1554. */
  1555. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1556. {
  1557. struct drm_device *dev = crtc->base.dev;
  1558. struct drm_i915_private *dev_priv = dev->dev_private;
  1559. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1560. if (WARN_ON(pll == NULL))
  1561. return;
  1562. if (WARN_ON(pll->refcount == 0))
  1563. return;
  1564. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1565. pll->name, pll->active, pll->on,
  1566. crtc->base.base.id);
  1567. if (pll->active++) {
  1568. WARN_ON(!pll->on);
  1569. assert_shared_dpll_enabled(dev_priv, pll);
  1570. return;
  1571. }
  1572. WARN_ON(pll->on);
  1573. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1574. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1575. pll->enable(dev_priv, pll);
  1576. pll->on = true;
  1577. }
  1578. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1579. {
  1580. struct drm_device *dev = crtc->base.dev;
  1581. struct drm_i915_private *dev_priv = dev->dev_private;
  1582. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1583. /* PCH only available on ILK+ */
  1584. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1585. if (WARN_ON(pll == NULL))
  1586. return;
  1587. if (WARN_ON(pll->refcount == 0))
  1588. return;
  1589. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1590. pll->name, pll->active, pll->on,
  1591. crtc->base.base.id);
  1592. if (WARN_ON(pll->active == 0)) {
  1593. assert_shared_dpll_disabled(dev_priv, pll);
  1594. return;
  1595. }
  1596. assert_shared_dpll_enabled(dev_priv, pll);
  1597. WARN_ON(!pll->on);
  1598. if (--pll->active)
  1599. return;
  1600. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1601. pll->disable(dev_priv, pll);
  1602. pll->on = false;
  1603. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1604. }
  1605. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1606. enum pipe pipe)
  1607. {
  1608. struct drm_device *dev = dev_priv->dev;
  1609. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1610. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1611. uint32_t reg, val, pipeconf_val;
  1612. /* PCH only available on ILK+ */
  1613. BUG_ON(!HAS_PCH_SPLIT(dev));
  1614. /* Make sure PCH DPLL is enabled */
  1615. assert_shared_dpll_enabled(dev_priv,
  1616. intel_crtc_to_shared_dpll(intel_crtc));
  1617. /* FDI must be feeding us bits for PCH ports */
  1618. assert_fdi_tx_enabled(dev_priv, pipe);
  1619. assert_fdi_rx_enabled(dev_priv, pipe);
  1620. if (HAS_PCH_CPT(dev)) {
  1621. /* Workaround: Set the timing override bit before enabling the
  1622. * pch transcoder. */
  1623. reg = TRANS_CHICKEN2(pipe);
  1624. val = I915_READ(reg);
  1625. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1626. I915_WRITE(reg, val);
  1627. }
  1628. reg = PCH_TRANSCONF(pipe);
  1629. val = I915_READ(reg);
  1630. pipeconf_val = I915_READ(PIPECONF(pipe));
  1631. if (HAS_PCH_IBX(dev_priv->dev)) {
  1632. /*
  1633. * make the BPC in transcoder be consistent with
  1634. * that in pipeconf reg.
  1635. */
  1636. val &= ~PIPECONF_BPC_MASK;
  1637. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1638. }
  1639. val &= ~TRANS_INTERLACE_MASK;
  1640. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1641. if (HAS_PCH_IBX(dev_priv->dev) &&
  1642. intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
  1643. val |= TRANS_LEGACY_INTERLACED_ILK;
  1644. else
  1645. val |= TRANS_INTERLACED;
  1646. else
  1647. val |= TRANS_PROGRESSIVE;
  1648. I915_WRITE(reg, val | TRANS_ENABLE);
  1649. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1650. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1651. }
  1652. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1653. enum transcoder cpu_transcoder)
  1654. {
  1655. u32 val, pipeconf_val;
  1656. /* PCH only available on ILK+ */
  1657. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1658. /* FDI must be feeding us bits for PCH ports */
  1659. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1660. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1661. /* Workaround: set timing override bit. */
  1662. val = I915_READ(_TRANSA_CHICKEN2);
  1663. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1664. I915_WRITE(_TRANSA_CHICKEN2, val);
  1665. val = TRANS_ENABLE;
  1666. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1667. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1668. PIPECONF_INTERLACED_ILK)
  1669. val |= TRANS_INTERLACED;
  1670. else
  1671. val |= TRANS_PROGRESSIVE;
  1672. I915_WRITE(LPT_TRANSCONF, val);
  1673. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1674. DRM_ERROR("Failed to enable PCH transcoder\n");
  1675. }
  1676. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1677. enum pipe pipe)
  1678. {
  1679. struct drm_device *dev = dev_priv->dev;
  1680. uint32_t reg, val;
  1681. /* FDI relies on the transcoder */
  1682. assert_fdi_tx_disabled(dev_priv, pipe);
  1683. assert_fdi_rx_disabled(dev_priv, pipe);
  1684. /* Ports must be off as well */
  1685. assert_pch_ports_disabled(dev_priv, pipe);
  1686. reg = PCH_TRANSCONF(pipe);
  1687. val = I915_READ(reg);
  1688. val &= ~TRANS_ENABLE;
  1689. I915_WRITE(reg, val);
  1690. /* wait for PCH transcoder off, transcoder state */
  1691. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1692. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1693. if (!HAS_PCH_IBX(dev)) {
  1694. /* Workaround: Clear the timing override chicken bit again. */
  1695. reg = TRANS_CHICKEN2(pipe);
  1696. val = I915_READ(reg);
  1697. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1698. I915_WRITE(reg, val);
  1699. }
  1700. }
  1701. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1702. {
  1703. u32 val;
  1704. val = I915_READ(LPT_TRANSCONF);
  1705. val &= ~TRANS_ENABLE;
  1706. I915_WRITE(LPT_TRANSCONF, val);
  1707. /* wait for PCH transcoder off, transcoder state */
  1708. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1709. DRM_ERROR("Failed to disable PCH transcoder\n");
  1710. /* Workaround: clear timing override bit. */
  1711. val = I915_READ(_TRANSA_CHICKEN2);
  1712. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1713. I915_WRITE(_TRANSA_CHICKEN2, val);
  1714. }
  1715. /**
  1716. * intel_enable_pipe - enable a pipe, asserting requirements
  1717. * @crtc: crtc responsible for the pipe
  1718. *
  1719. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1720. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1721. */
  1722. static void intel_enable_pipe(struct intel_crtc *crtc)
  1723. {
  1724. struct drm_device *dev = crtc->base.dev;
  1725. struct drm_i915_private *dev_priv = dev->dev_private;
  1726. enum pipe pipe = crtc->pipe;
  1727. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1728. pipe);
  1729. enum pipe pch_transcoder;
  1730. int reg;
  1731. u32 val;
  1732. assert_planes_disabled(dev_priv, pipe);
  1733. assert_cursor_disabled(dev_priv, pipe);
  1734. assert_sprites_disabled(dev_priv, pipe);
  1735. if (HAS_PCH_LPT(dev_priv->dev))
  1736. pch_transcoder = TRANSCODER_A;
  1737. else
  1738. pch_transcoder = pipe;
  1739. /*
  1740. * A pipe without a PLL won't actually be able to drive bits from
  1741. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1742. * need the check.
  1743. */
  1744. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1745. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DSI))
  1746. assert_dsi_pll_enabled(dev_priv);
  1747. else
  1748. assert_pll_enabled(dev_priv, pipe);
  1749. else {
  1750. if (crtc->config.has_pch_encoder) {
  1751. /* if driving the PCH, we need FDI enabled */
  1752. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1753. assert_fdi_tx_pll_enabled(dev_priv,
  1754. (enum pipe) cpu_transcoder);
  1755. }
  1756. /* FIXME: assert CPU port conditions for SNB+ */
  1757. }
  1758. reg = PIPECONF(cpu_transcoder);
  1759. val = I915_READ(reg);
  1760. if (val & PIPECONF_ENABLE) {
  1761. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1762. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1763. return;
  1764. }
  1765. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1766. POSTING_READ(reg);
  1767. }
  1768. /**
  1769. * intel_disable_pipe - disable a pipe, asserting requirements
  1770. * @crtc: crtc whose pipes is to be disabled
  1771. *
  1772. * Disable the pipe of @crtc, making sure that various hardware
  1773. * specific requirements are met, if applicable, e.g. plane
  1774. * disabled, panel fitter off, etc.
  1775. *
  1776. * Will wait until the pipe has shut down before returning.
  1777. */
  1778. static void intel_disable_pipe(struct intel_crtc *crtc)
  1779. {
  1780. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1781. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  1782. enum pipe pipe = crtc->pipe;
  1783. int reg;
  1784. u32 val;
  1785. /*
  1786. * Make sure planes won't keep trying to pump pixels to us,
  1787. * or we might hang the display.
  1788. */
  1789. assert_planes_disabled(dev_priv, pipe);
  1790. assert_cursor_disabled(dev_priv, pipe);
  1791. assert_sprites_disabled(dev_priv, pipe);
  1792. reg = PIPECONF(cpu_transcoder);
  1793. val = I915_READ(reg);
  1794. if ((val & PIPECONF_ENABLE) == 0)
  1795. return;
  1796. /*
  1797. * Double wide has implications for planes
  1798. * so best keep it disabled when not needed.
  1799. */
  1800. if (crtc->config.double_wide)
  1801. val &= ~PIPECONF_DOUBLE_WIDE;
  1802. /* Don't disable pipe or pipe PLLs if needed */
  1803. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1804. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1805. val &= ~PIPECONF_ENABLE;
  1806. I915_WRITE(reg, val);
  1807. if ((val & PIPECONF_ENABLE) == 0)
  1808. intel_wait_for_pipe_off(crtc);
  1809. }
  1810. /*
  1811. * Plane regs are double buffered, going from enabled->disabled needs a
  1812. * trigger in order to latch. The display address reg provides this.
  1813. */
  1814. void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
  1815. enum plane plane)
  1816. {
  1817. struct drm_device *dev = dev_priv->dev;
  1818. u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
  1819. I915_WRITE(reg, I915_READ(reg));
  1820. POSTING_READ(reg);
  1821. }
  1822. /**
  1823. * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  1824. * @plane: plane to be enabled
  1825. * @crtc: crtc for the plane
  1826. *
  1827. * Enable @plane on @crtc, making sure that the pipe is running first.
  1828. */
  1829. static void intel_enable_primary_hw_plane(struct drm_plane *plane,
  1830. struct drm_crtc *crtc)
  1831. {
  1832. struct drm_device *dev = plane->dev;
  1833. struct drm_i915_private *dev_priv = dev->dev_private;
  1834. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1835. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1836. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1837. if (intel_crtc->primary_enabled)
  1838. return;
  1839. intel_crtc->primary_enabled = true;
  1840. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1841. crtc->x, crtc->y);
  1842. /*
  1843. * BDW signals flip done immediately if the plane
  1844. * is disabled, even if the plane enable is already
  1845. * armed to occur at the next vblank :(
  1846. */
  1847. if (IS_BROADWELL(dev))
  1848. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1849. }
  1850. /**
  1851. * intel_disable_primary_hw_plane - disable the primary hardware plane
  1852. * @plane: plane to be disabled
  1853. * @crtc: crtc for the plane
  1854. *
  1855. * Disable @plane on @crtc, making sure that the pipe is running first.
  1856. */
  1857. static void intel_disable_primary_hw_plane(struct drm_plane *plane,
  1858. struct drm_crtc *crtc)
  1859. {
  1860. struct drm_device *dev = plane->dev;
  1861. struct drm_i915_private *dev_priv = dev->dev_private;
  1862. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1863. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1864. if (!intel_crtc->primary_enabled)
  1865. return;
  1866. intel_crtc->primary_enabled = false;
  1867. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1868. crtc->x, crtc->y);
  1869. }
  1870. static bool need_vtd_wa(struct drm_device *dev)
  1871. {
  1872. #ifdef CONFIG_INTEL_IOMMU
  1873. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1874. return true;
  1875. #endif
  1876. return false;
  1877. }
  1878. static int intel_align_height(struct drm_device *dev, int height, bool tiled)
  1879. {
  1880. int tile_height;
  1881. tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
  1882. return ALIGN(height, tile_height);
  1883. }
  1884. int
  1885. intel_pin_and_fence_fb_obj(struct drm_device *dev,
  1886. struct drm_i915_gem_object *obj,
  1887. struct intel_engine_cs *pipelined)
  1888. {
  1889. struct drm_i915_private *dev_priv = dev->dev_private;
  1890. u32 alignment;
  1891. int ret;
  1892. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1893. switch (obj->tiling_mode) {
  1894. case I915_TILING_NONE:
  1895. if (INTEL_INFO(dev)->gen >= 9)
  1896. alignment = 256 * 1024;
  1897. else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1898. alignment = 128 * 1024;
  1899. else if (INTEL_INFO(dev)->gen >= 4)
  1900. alignment = 4 * 1024;
  1901. else
  1902. alignment = 64 * 1024;
  1903. break;
  1904. case I915_TILING_X:
  1905. if (INTEL_INFO(dev)->gen >= 9)
  1906. alignment = 256 * 1024;
  1907. else {
  1908. /* pin() will align the object as required by fence */
  1909. alignment = 0;
  1910. }
  1911. break;
  1912. case I915_TILING_Y:
  1913. WARN(1, "Y tiled bo slipped through, driver bug!\n");
  1914. return -EINVAL;
  1915. default:
  1916. BUG();
  1917. }
  1918. /* Note that the w/a also requires 64 PTE of padding following the
  1919. * bo. We currently fill all unused PTE with the shadow page and so
  1920. * we should always have valid PTE following the scanout preventing
  1921. * the VT-d warning.
  1922. */
  1923. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1924. alignment = 256 * 1024;
  1925. /*
  1926. * Global gtt pte registers are special registers which actually forward
  1927. * writes to a chunk of system memory. Which means that there is no risk
  1928. * that the register values disappear as soon as we call
  1929. * intel_runtime_pm_put(), so it is correct to wrap only the
  1930. * pin/unpin/fence and not more.
  1931. */
  1932. intel_runtime_pm_get(dev_priv);
  1933. dev_priv->mm.interruptible = false;
  1934. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1935. if (ret)
  1936. goto err_interruptible;
  1937. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1938. * fence, whereas 965+ only requires a fence if using
  1939. * framebuffer compression. For simplicity, we always install
  1940. * a fence as the cost is not that onerous.
  1941. */
  1942. ret = i915_gem_object_get_fence(obj);
  1943. if (ret)
  1944. goto err_unpin;
  1945. i915_gem_object_pin_fence(obj);
  1946. dev_priv->mm.interruptible = true;
  1947. intel_runtime_pm_put(dev_priv);
  1948. return 0;
  1949. err_unpin:
  1950. i915_gem_object_unpin_from_display_plane(obj);
  1951. err_interruptible:
  1952. dev_priv->mm.interruptible = true;
  1953. intel_runtime_pm_put(dev_priv);
  1954. return ret;
  1955. }
  1956. void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
  1957. {
  1958. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  1959. i915_gem_object_unpin_fence(obj);
  1960. i915_gem_object_unpin_from_display_plane(obj);
  1961. }
  1962. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  1963. * is assumed to be a power-of-two. */
  1964. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  1965. unsigned int tiling_mode,
  1966. unsigned int cpp,
  1967. unsigned int pitch)
  1968. {
  1969. if (tiling_mode != I915_TILING_NONE) {
  1970. unsigned int tile_rows, tiles;
  1971. tile_rows = *y / 8;
  1972. *y %= 8;
  1973. tiles = *x / (512/cpp);
  1974. *x %= 512/cpp;
  1975. return tile_rows * pitch * 8 + tiles * 4096;
  1976. } else {
  1977. unsigned int offset;
  1978. offset = *y * pitch + *x * cpp;
  1979. *y = 0;
  1980. *x = (offset & 4095) / cpp;
  1981. return offset & -4096;
  1982. }
  1983. }
  1984. int intel_format_to_fourcc(int format)
  1985. {
  1986. switch (format) {
  1987. case DISPPLANE_8BPP:
  1988. return DRM_FORMAT_C8;
  1989. case DISPPLANE_BGRX555:
  1990. return DRM_FORMAT_XRGB1555;
  1991. case DISPPLANE_BGRX565:
  1992. return DRM_FORMAT_RGB565;
  1993. default:
  1994. case DISPPLANE_BGRX888:
  1995. return DRM_FORMAT_XRGB8888;
  1996. case DISPPLANE_RGBX888:
  1997. return DRM_FORMAT_XBGR8888;
  1998. case DISPPLANE_BGRX101010:
  1999. return DRM_FORMAT_XRGB2101010;
  2000. case DISPPLANE_RGBX101010:
  2001. return DRM_FORMAT_XBGR2101010;
  2002. }
  2003. }
  2004. static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
  2005. struct intel_plane_config *plane_config)
  2006. {
  2007. struct drm_device *dev = crtc->base.dev;
  2008. struct drm_i915_gem_object *obj = NULL;
  2009. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2010. u32 base = plane_config->base;
  2011. if (plane_config->size == 0)
  2012. return false;
  2013. obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
  2014. plane_config->size);
  2015. if (!obj)
  2016. return false;
  2017. if (plane_config->tiled) {
  2018. obj->tiling_mode = I915_TILING_X;
  2019. obj->stride = crtc->base.primary->fb->pitches[0];
  2020. }
  2021. mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
  2022. mode_cmd.width = crtc->base.primary->fb->width;
  2023. mode_cmd.height = crtc->base.primary->fb->height;
  2024. mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
  2025. mutex_lock(&dev->struct_mutex);
  2026. if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
  2027. &mode_cmd, obj)) {
  2028. DRM_DEBUG_KMS("intel fb init failed\n");
  2029. goto out_unref_obj;
  2030. }
  2031. obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
  2032. mutex_unlock(&dev->struct_mutex);
  2033. DRM_DEBUG_KMS("plane fb obj %p\n", obj);
  2034. return true;
  2035. out_unref_obj:
  2036. drm_gem_object_unreference(&obj->base);
  2037. mutex_unlock(&dev->struct_mutex);
  2038. return false;
  2039. }
  2040. static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
  2041. struct intel_plane_config *plane_config)
  2042. {
  2043. struct drm_device *dev = intel_crtc->base.dev;
  2044. struct drm_crtc *c;
  2045. struct intel_crtc *i;
  2046. struct drm_i915_gem_object *obj;
  2047. if (!intel_crtc->base.primary->fb)
  2048. return;
  2049. if (intel_alloc_plane_obj(intel_crtc, plane_config))
  2050. return;
  2051. kfree(intel_crtc->base.primary->fb);
  2052. intel_crtc->base.primary->fb = NULL;
  2053. /*
  2054. * Failed to alloc the obj, check to see if we should share
  2055. * an fb with another CRTC instead
  2056. */
  2057. for_each_crtc(dev, c) {
  2058. i = to_intel_crtc(c);
  2059. if (c == &intel_crtc->base)
  2060. continue;
  2061. if (!i->active)
  2062. continue;
  2063. obj = intel_fb_obj(c->primary->fb);
  2064. if (obj == NULL)
  2065. continue;
  2066. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2067. drm_framebuffer_reference(c->primary->fb);
  2068. intel_crtc->base.primary->fb = c->primary->fb;
  2069. obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  2070. break;
  2071. }
  2072. }
  2073. }
  2074. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2075. struct drm_framebuffer *fb,
  2076. int x, int y)
  2077. {
  2078. struct drm_device *dev = crtc->dev;
  2079. struct drm_i915_private *dev_priv = dev->dev_private;
  2080. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2081. struct drm_i915_gem_object *obj;
  2082. int plane = intel_crtc->plane;
  2083. unsigned long linear_offset;
  2084. u32 dspcntr;
  2085. u32 reg = DSPCNTR(plane);
  2086. int pixel_size;
  2087. if (!intel_crtc->primary_enabled) {
  2088. I915_WRITE(reg, 0);
  2089. if (INTEL_INFO(dev)->gen >= 4)
  2090. I915_WRITE(DSPSURF(plane), 0);
  2091. else
  2092. I915_WRITE(DSPADDR(plane), 0);
  2093. POSTING_READ(reg);
  2094. return;
  2095. }
  2096. obj = intel_fb_obj(fb);
  2097. if (WARN_ON(obj == NULL))
  2098. return;
  2099. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2100. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2101. dspcntr |= DISPLAY_PLANE_ENABLE;
  2102. if (INTEL_INFO(dev)->gen < 4) {
  2103. if (intel_crtc->pipe == PIPE_B)
  2104. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2105. /* pipesrc and dspsize control the size that is scaled from,
  2106. * which should always be the user's requested size.
  2107. */
  2108. I915_WRITE(DSPSIZE(plane),
  2109. ((intel_crtc->config.pipe_src_h - 1) << 16) |
  2110. (intel_crtc->config.pipe_src_w - 1));
  2111. I915_WRITE(DSPPOS(plane), 0);
  2112. }
  2113. switch (fb->pixel_format) {
  2114. case DRM_FORMAT_C8:
  2115. dspcntr |= DISPPLANE_8BPP;
  2116. break;
  2117. case DRM_FORMAT_XRGB1555:
  2118. case DRM_FORMAT_ARGB1555:
  2119. dspcntr |= DISPPLANE_BGRX555;
  2120. break;
  2121. case DRM_FORMAT_RGB565:
  2122. dspcntr |= DISPPLANE_BGRX565;
  2123. break;
  2124. case DRM_FORMAT_XRGB8888:
  2125. case DRM_FORMAT_ARGB8888:
  2126. dspcntr |= DISPPLANE_BGRX888;
  2127. break;
  2128. case DRM_FORMAT_XBGR8888:
  2129. case DRM_FORMAT_ABGR8888:
  2130. dspcntr |= DISPPLANE_RGBX888;
  2131. break;
  2132. case DRM_FORMAT_XRGB2101010:
  2133. case DRM_FORMAT_ARGB2101010:
  2134. dspcntr |= DISPPLANE_BGRX101010;
  2135. break;
  2136. case DRM_FORMAT_XBGR2101010:
  2137. case DRM_FORMAT_ABGR2101010:
  2138. dspcntr |= DISPPLANE_RGBX101010;
  2139. break;
  2140. default:
  2141. BUG();
  2142. }
  2143. if (INTEL_INFO(dev)->gen >= 4 &&
  2144. obj->tiling_mode != I915_TILING_NONE)
  2145. dspcntr |= DISPPLANE_TILED;
  2146. if (IS_G4X(dev))
  2147. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2148. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2149. if (INTEL_INFO(dev)->gen >= 4) {
  2150. intel_crtc->dspaddr_offset =
  2151. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2152. pixel_size,
  2153. fb->pitches[0]);
  2154. linear_offset -= intel_crtc->dspaddr_offset;
  2155. } else {
  2156. intel_crtc->dspaddr_offset = linear_offset;
  2157. }
  2158. if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
  2159. dspcntr |= DISPPLANE_ROTATE_180;
  2160. x += (intel_crtc->config.pipe_src_w - 1);
  2161. y += (intel_crtc->config.pipe_src_h - 1);
  2162. /* Finding the last pixel of the last line of the display
  2163. data and adding to linear_offset*/
  2164. linear_offset +=
  2165. (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
  2166. (intel_crtc->config.pipe_src_w - 1) * pixel_size;
  2167. }
  2168. I915_WRITE(reg, dspcntr);
  2169. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2170. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2171. fb->pitches[0]);
  2172. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2173. if (INTEL_INFO(dev)->gen >= 4) {
  2174. I915_WRITE(DSPSURF(plane),
  2175. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2176. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2177. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2178. } else
  2179. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2180. POSTING_READ(reg);
  2181. }
  2182. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2183. struct drm_framebuffer *fb,
  2184. int x, int y)
  2185. {
  2186. struct drm_device *dev = crtc->dev;
  2187. struct drm_i915_private *dev_priv = dev->dev_private;
  2188. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2189. struct drm_i915_gem_object *obj;
  2190. int plane = intel_crtc->plane;
  2191. unsigned long linear_offset;
  2192. u32 dspcntr;
  2193. u32 reg = DSPCNTR(plane);
  2194. int pixel_size;
  2195. if (!intel_crtc->primary_enabled) {
  2196. I915_WRITE(reg, 0);
  2197. I915_WRITE(DSPSURF(plane), 0);
  2198. POSTING_READ(reg);
  2199. return;
  2200. }
  2201. obj = intel_fb_obj(fb);
  2202. if (WARN_ON(obj == NULL))
  2203. return;
  2204. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2205. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2206. dspcntr |= DISPLAY_PLANE_ENABLE;
  2207. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2208. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2209. switch (fb->pixel_format) {
  2210. case DRM_FORMAT_C8:
  2211. dspcntr |= DISPPLANE_8BPP;
  2212. break;
  2213. case DRM_FORMAT_RGB565:
  2214. dspcntr |= DISPPLANE_BGRX565;
  2215. break;
  2216. case DRM_FORMAT_XRGB8888:
  2217. case DRM_FORMAT_ARGB8888:
  2218. dspcntr |= DISPPLANE_BGRX888;
  2219. break;
  2220. case DRM_FORMAT_XBGR8888:
  2221. case DRM_FORMAT_ABGR8888:
  2222. dspcntr |= DISPPLANE_RGBX888;
  2223. break;
  2224. case DRM_FORMAT_XRGB2101010:
  2225. case DRM_FORMAT_ARGB2101010:
  2226. dspcntr |= DISPPLANE_BGRX101010;
  2227. break;
  2228. case DRM_FORMAT_XBGR2101010:
  2229. case DRM_FORMAT_ABGR2101010:
  2230. dspcntr |= DISPPLANE_RGBX101010;
  2231. break;
  2232. default:
  2233. BUG();
  2234. }
  2235. if (obj->tiling_mode != I915_TILING_NONE)
  2236. dspcntr |= DISPPLANE_TILED;
  2237. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2238. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2239. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2240. intel_crtc->dspaddr_offset =
  2241. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2242. pixel_size,
  2243. fb->pitches[0]);
  2244. linear_offset -= intel_crtc->dspaddr_offset;
  2245. if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
  2246. dspcntr |= DISPPLANE_ROTATE_180;
  2247. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2248. x += (intel_crtc->config.pipe_src_w - 1);
  2249. y += (intel_crtc->config.pipe_src_h - 1);
  2250. /* Finding the last pixel of the last line of the display
  2251. data and adding to linear_offset*/
  2252. linear_offset +=
  2253. (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
  2254. (intel_crtc->config.pipe_src_w - 1) * pixel_size;
  2255. }
  2256. }
  2257. I915_WRITE(reg, dspcntr);
  2258. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2259. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2260. fb->pitches[0]);
  2261. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2262. I915_WRITE(DSPSURF(plane),
  2263. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2264. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2265. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2266. } else {
  2267. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2268. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2269. }
  2270. POSTING_READ(reg);
  2271. }
  2272. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2273. struct drm_framebuffer *fb,
  2274. int x, int y)
  2275. {
  2276. struct drm_device *dev = crtc->dev;
  2277. struct drm_i915_private *dev_priv = dev->dev_private;
  2278. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2279. struct intel_framebuffer *intel_fb;
  2280. struct drm_i915_gem_object *obj;
  2281. int pipe = intel_crtc->pipe;
  2282. u32 plane_ctl, stride;
  2283. if (!intel_crtc->primary_enabled) {
  2284. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2285. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2286. POSTING_READ(PLANE_CTL(pipe, 0));
  2287. return;
  2288. }
  2289. plane_ctl = PLANE_CTL_ENABLE |
  2290. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2291. PLANE_CTL_PIPE_CSC_ENABLE;
  2292. switch (fb->pixel_format) {
  2293. case DRM_FORMAT_RGB565:
  2294. plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
  2295. break;
  2296. case DRM_FORMAT_XRGB8888:
  2297. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2298. break;
  2299. case DRM_FORMAT_XBGR8888:
  2300. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2301. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2302. break;
  2303. case DRM_FORMAT_XRGB2101010:
  2304. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2305. break;
  2306. case DRM_FORMAT_XBGR2101010:
  2307. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2308. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2309. break;
  2310. default:
  2311. BUG();
  2312. }
  2313. intel_fb = to_intel_framebuffer(fb);
  2314. obj = intel_fb->obj;
  2315. /*
  2316. * The stride is either expressed as a multiple of 64 bytes chunks for
  2317. * linear buffers or in number of tiles for tiled buffers.
  2318. */
  2319. switch (obj->tiling_mode) {
  2320. case I915_TILING_NONE:
  2321. stride = fb->pitches[0] >> 6;
  2322. break;
  2323. case I915_TILING_X:
  2324. plane_ctl |= PLANE_CTL_TILED_X;
  2325. stride = fb->pitches[0] >> 9;
  2326. break;
  2327. default:
  2328. BUG();
  2329. }
  2330. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2331. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2332. DRM_DEBUG_KMS("Writing base %08lX %d,%d,%d,%d pitch=%d\n",
  2333. i915_gem_obj_ggtt_offset(obj),
  2334. x, y, fb->width, fb->height,
  2335. fb->pitches[0]);
  2336. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2337. I915_WRITE(PLANE_OFFSET(pipe, 0), (y << 16) | x);
  2338. I915_WRITE(PLANE_SIZE(pipe, 0),
  2339. (intel_crtc->config.pipe_src_h - 1) << 16 |
  2340. (intel_crtc->config.pipe_src_w - 1));
  2341. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2342. I915_WRITE(PLANE_SURF(pipe, 0), i915_gem_obj_ggtt_offset(obj));
  2343. POSTING_READ(PLANE_SURF(pipe, 0));
  2344. }
  2345. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2346. static int
  2347. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2348. int x, int y, enum mode_set_atomic state)
  2349. {
  2350. struct drm_device *dev = crtc->dev;
  2351. struct drm_i915_private *dev_priv = dev->dev_private;
  2352. if (dev_priv->display.disable_fbc)
  2353. dev_priv->display.disable_fbc(dev);
  2354. intel_increase_pllclock(dev, to_intel_crtc(crtc)->pipe);
  2355. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2356. return 0;
  2357. }
  2358. void intel_display_handle_reset(struct drm_device *dev)
  2359. {
  2360. struct drm_i915_private *dev_priv = dev->dev_private;
  2361. struct drm_crtc *crtc;
  2362. /*
  2363. * Flips in the rings have been nuked by the reset,
  2364. * so complete all pending flips so that user space
  2365. * will get its events and not get stuck.
  2366. *
  2367. * Also update the base address of all primary
  2368. * planes to the the last fb to make sure we're
  2369. * showing the correct fb after a reset.
  2370. *
  2371. * Need to make two loops over the crtcs so that we
  2372. * don't try to grab a crtc mutex before the
  2373. * pending_flip_queue really got woken up.
  2374. */
  2375. for_each_crtc(dev, crtc) {
  2376. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2377. enum plane plane = intel_crtc->plane;
  2378. intel_prepare_page_flip(dev, plane);
  2379. intel_finish_page_flip_plane(dev, plane);
  2380. }
  2381. for_each_crtc(dev, crtc) {
  2382. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2383. drm_modeset_lock(&crtc->mutex, NULL);
  2384. /*
  2385. * FIXME: Once we have proper support for primary planes (and
  2386. * disabling them without disabling the entire crtc) allow again
  2387. * a NULL crtc->primary->fb.
  2388. */
  2389. if (intel_crtc->active && crtc->primary->fb)
  2390. dev_priv->display.update_primary_plane(crtc,
  2391. crtc->primary->fb,
  2392. crtc->x,
  2393. crtc->y);
  2394. drm_modeset_unlock(&crtc->mutex);
  2395. }
  2396. }
  2397. static int
  2398. intel_finish_fb(struct drm_framebuffer *old_fb)
  2399. {
  2400. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2401. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2402. bool was_interruptible = dev_priv->mm.interruptible;
  2403. int ret;
  2404. /* Big Hammer, we also need to ensure that any pending
  2405. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2406. * current scanout is retired before unpinning the old
  2407. * framebuffer.
  2408. *
  2409. * This should only fail upon a hung GPU, in which case we
  2410. * can safely continue.
  2411. */
  2412. dev_priv->mm.interruptible = false;
  2413. ret = i915_gem_object_finish_gpu(obj);
  2414. dev_priv->mm.interruptible = was_interruptible;
  2415. return ret;
  2416. }
  2417. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2418. {
  2419. struct drm_device *dev = crtc->dev;
  2420. struct drm_i915_private *dev_priv = dev->dev_private;
  2421. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2422. unsigned long flags;
  2423. bool pending;
  2424. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2425. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2426. return false;
  2427. spin_lock_irqsave(&dev->event_lock, flags);
  2428. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2429. spin_unlock_irqrestore(&dev->event_lock, flags);
  2430. return pending;
  2431. }
  2432. static int
  2433. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  2434. struct drm_framebuffer *fb)
  2435. {
  2436. struct drm_device *dev = crtc->dev;
  2437. struct drm_i915_private *dev_priv = dev->dev_private;
  2438. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2439. enum pipe pipe = intel_crtc->pipe;
  2440. struct drm_framebuffer *old_fb = crtc->primary->fb;
  2441. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2442. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
  2443. int ret;
  2444. if (intel_crtc_has_pending_flip(crtc)) {
  2445. DRM_ERROR("pipe is still busy with an old pageflip\n");
  2446. return -EBUSY;
  2447. }
  2448. /* no fb bound */
  2449. if (!fb) {
  2450. DRM_ERROR("No FB bound\n");
  2451. return 0;
  2452. }
  2453. if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
  2454. DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
  2455. plane_name(intel_crtc->plane),
  2456. INTEL_INFO(dev)->num_pipes);
  2457. return -EINVAL;
  2458. }
  2459. mutex_lock(&dev->struct_mutex);
  2460. ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
  2461. if (ret == 0)
  2462. i915_gem_track_fb(old_obj, obj,
  2463. INTEL_FRONTBUFFER_PRIMARY(pipe));
  2464. mutex_unlock(&dev->struct_mutex);
  2465. if (ret != 0) {
  2466. DRM_ERROR("pin & fence failed\n");
  2467. return ret;
  2468. }
  2469. /*
  2470. * Update pipe size and adjust fitter if needed: the reason for this is
  2471. * that in compute_mode_changes we check the native mode (not the pfit
  2472. * mode) to see if we can flip rather than do a full mode set. In the
  2473. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2474. * pfit state, we'll end up with a big fb scanned out into the wrong
  2475. * sized surface.
  2476. *
  2477. * To fix this properly, we need to hoist the checks up into
  2478. * compute_mode_changes (or above), check the actual pfit state and
  2479. * whether the platform allows pfit disable with pipe active, and only
  2480. * then update the pipesrc and pfit state, even on the flip path.
  2481. */
  2482. if (i915.fastboot) {
  2483. const struct drm_display_mode *adjusted_mode =
  2484. &intel_crtc->config.adjusted_mode;
  2485. I915_WRITE(PIPESRC(intel_crtc->pipe),
  2486. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2487. (adjusted_mode->crtc_vdisplay - 1));
  2488. if (!intel_crtc->config.pch_pfit.enabled &&
  2489. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2490. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2491. I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
  2492. I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
  2493. I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
  2494. }
  2495. intel_crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
  2496. intel_crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
  2497. }
  2498. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2499. if (intel_crtc->active)
  2500. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  2501. crtc->primary->fb = fb;
  2502. crtc->x = x;
  2503. crtc->y = y;
  2504. if (old_fb) {
  2505. if (intel_crtc->active && old_fb != fb)
  2506. intel_wait_for_vblank(dev, intel_crtc->pipe);
  2507. mutex_lock(&dev->struct_mutex);
  2508. intel_unpin_fb_obj(old_obj);
  2509. mutex_unlock(&dev->struct_mutex);
  2510. }
  2511. mutex_lock(&dev->struct_mutex);
  2512. intel_update_fbc(dev);
  2513. mutex_unlock(&dev->struct_mutex);
  2514. return 0;
  2515. }
  2516. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2517. {
  2518. struct drm_device *dev = crtc->dev;
  2519. struct drm_i915_private *dev_priv = dev->dev_private;
  2520. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2521. int pipe = intel_crtc->pipe;
  2522. u32 reg, temp;
  2523. /* enable normal train */
  2524. reg = FDI_TX_CTL(pipe);
  2525. temp = I915_READ(reg);
  2526. if (IS_IVYBRIDGE(dev)) {
  2527. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2528. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2529. } else {
  2530. temp &= ~FDI_LINK_TRAIN_NONE;
  2531. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2532. }
  2533. I915_WRITE(reg, temp);
  2534. reg = FDI_RX_CTL(pipe);
  2535. temp = I915_READ(reg);
  2536. if (HAS_PCH_CPT(dev)) {
  2537. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2538. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2539. } else {
  2540. temp &= ~FDI_LINK_TRAIN_NONE;
  2541. temp |= FDI_LINK_TRAIN_NONE;
  2542. }
  2543. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2544. /* wait one idle pattern time */
  2545. POSTING_READ(reg);
  2546. udelay(1000);
  2547. /* IVB wants error correction enabled */
  2548. if (IS_IVYBRIDGE(dev))
  2549. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2550. FDI_FE_ERRC_ENABLE);
  2551. }
  2552. static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
  2553. {
  2554. return crtc->base.enabled && crtc->active &&
  2555. crtc->config.has_pch_encoder;
  2556. }
  2557. static void ivb_modeset_global_resources(struct drm_device *dev)
  2558. {
  2559. struct drm_i915_private *dev_priv = dev->dev_private;
  2560. struct intel_crtc *pipe_B_crtc =
  2561. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  2562. struct intel_crtc *pipe_C_crtc =
  2563. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
  2564. uint32_t temp;
  2565. /*
  2566. * When everything is off disable fdi C so that we could enable fdi B
  2567. * with all lanes. Note that we don't care about enabled pipes without
  2568. * an enabled pch encoder.
  2569. */
  2570. if (!pipe_has_enabled_pch(pipe_B_crtc) &&
  2571. !pipe_has_enabled_pch(pipe_C_crtc)) {
  2572. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2573. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2574. temp = I915_READ(SOUTH_CHICKEN1);
  2575. temp &= ~FDI_BC_BIFURCATION_SELECT;
  2576. DRM_DEBUG_KMS("disabling fdi C rx\n");
  2577. I915_WRITE(SOUTH_CHICKEN1, temp);
  2578. }
  2579. }
  2580. /* The FDI link training functions for ILK/Ibexpeak. */
  2581. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2582. {
  2583. struct drm_device *dev = crtc->dev;
  2584. struct drm_i915_private *dev_priv = dev->dev_private;
  2585. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2586. int pipe = intel_crtc->pipe;
  2587. u32 reg, temp, tries;
  2588. /* FDI needs bits from pipe first */
  2589. assert_pipe_enabled(dev_priv, pipe);
  2590. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2591. for train result */
  2592. reg = FDI_RX_IMR(pipe);
  2593. temp = I915_READ(reg);
  2594. temp &= ~FDI_RX_SYMBOL_LOCK;
  2595. temp &= ~FDI_RX_BIT_LOCK;
  2596. I915_WRITE(reg, temp);
  2597. I915_READ(reg);
  2598. udelay(150);
  2599. /* enable CPU FDI TX and PCH FDI RX */
  2600. reg = FDI_TX_CTL(pipe);
  2601. temp = I915_READ(reg);
  2602. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2603. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2604. temp &= ~FDI_LINK_TRAIN_NONE;
  2605. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2606. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2607. reg = FDI_RX_CTL(pipe);
  2608. temp = I915_READ(reg);
  2609. temp &= ~FDI_LINK_TRAIN_NONE;
  2610. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2611. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2612. POSTING_READ(reg);
  2613. udelay(150);
  2614. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2615. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2616. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2617. FDI_RX_PHASE_SYNC_POINTER_EN);
  2618. reg = FDI_RX_IIR(pipe);
  2619. for (tries = 0; tries < 5; tries++) {
  2620. temp = I915_READ(reg);
  2621. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2622. if ((temp & FDI_RX_BIT_LOCK)) {
  2623. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2624. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2625. break;
  2626. }
  2627. }
  2628. if (tries == 5)
  2629. DRM_ERROR("FDI train 1 fail!\n");
  2630. /* Train 2 */
  2631. reg = FDI_TX_CTL(pipe);
  2632. temp = I915_READ(reg);
  2633. temp &= ~FDI_LINK_TRAIN_NONE;
  2634. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2635. I915_WRITE(reg, temp);
  2636. reg = FDI_RX_CTL(pipe);
  2637. temp = I915_READ(reg);
  2638. temp &= ~FDI_LINK_TRAIN_NONE;
  2639. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2640. I915_WRITE(reg, temp);
  2641. POSTING_READ(reg);
  2642. udelay(150);
  2643. reg = FDI_RX_IIR(pipe);
  2644. for (tries = 0; tries < 5; tries++) {
  2645. temp = I915_READ(reg);
  2646. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2647. if (temp & FDI_RX_SYMBOL_LOCK) {
  2648. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2649. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2650. break;
  2651. }
  2652. }
  2653. if (tries == 5)
  2654. DRM_ERROR("FDI train 2 fail!\n");
  2655. DRM_DEBUG_KMS("FDI train done\n");
  2656. }
  2657. static const int snb_b_fdi_train_param[] = {
  2658. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2659. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2660. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2661. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2662. };
  2663. /* The FDI link training functions for SNB/Cougarpoint. */
  2664. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2665. {
  2666. struct drm_device *dev = crtc->dev;
  2667. struct drm_i915_private *dev_priv = dev->dev_private;
  2668. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2669. int pipe = intel_crtc->pipe;
  2670. u32 reg, temp, i, retry;
  2671. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2672. for train result */
  2673. reg = FDI_RX_IMR(pipe);
  2674. temp = I915_READ(reg);
  2675. temp &= ~FDI_RX_SYMBOL_LOCK;
  2676. temp &= ~FDI_RX_BIT_LOCK;
  2677. I915_WRITE(reg, temp);
  2678. POSTING_READ(reg);
  2679. udelay(150);
  2680. /* enable CPU FDI TX and PCH FDI RX */
  2681. reg = FDI_TX_CTL(pipe);
  2682. temp = I915_READ(reg);
  2683. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2684. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2685. temp &= ~FDI_LINK_TRAIN_NONE;
  2686. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2687. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2688. /* SNB-B */
  2689. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2690. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2691. I915_WRITE(FDI_RX_MISC(pipe),
  2692. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2693. reg = FDI_RX_CTL(pipe);
  2694. temp = I915_READ(reg);
  2695. if (HAS_PCH_CPT(dev)) {
  2696. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2697. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2698. } else {
  2699. temp &= ~FDI_LINK_TRAIN_NONE;
  2700. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2701. }
  2702. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2703. POSTING_READ(reg);
  2704. udelay(150);
  2705. for (i = 0; i < 4; i++) {
  2706. reg = FDI_TX_CTL(pipe);
  2707. temp = I915_READ(reg);
  2708. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2709. temp |= snb_b_fdi_train_param[i];
  2710. I915_WRITE(reg, temp);
  2711. POSTING_READ(reg);
  2712. udelay(500);
  2713. for (retry = 0; retry < 5; retry++) {
  2714. reg = FDI_RX_IIR(pipe);
  2715. temp = I915_READ(reg);
  2716. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2717. if (temp & FDI_RX_BIT_LOCK) {
  2718. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2719. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2720. break;
  2721. }
  2722. udelay(50);
  2723. }
  2724. if (retry < 5)
  2725. break;
  2726. }
  2727. if (i == 4)
  2728. DRM_ERROR("FDI train 1 fail!\n");
  2729. /* Train 2 */
  2730. reg = FDI_TX_CTL(pipe);
  2731. temp = I915_READ(reg);
  2732. temp &= ~FDI_LINK_TRAIN_NONE;
  2733. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2734. if (IS_GEN6(dev)) {
  2735. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2736. /* SNB-B */
  2737. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2738. }
  2739. I915_WRITE(reg, temp);
  2740. reg = FDI_RX_CTL(pipe);
  2741. temp = I915_READ(reg);
  2742. if (HAS_PCH_CPT(dev)) {
  2743. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2744. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2745. } else {
  2746. temp &= ~FDI_LINK_TRAIN_NONE;
  2747. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2748. }
  2749. I915_WRITE(reg, temp);
  2750. POSTING_READ(reg);
  2751. udelay(150);
  2752. for (i = 0; i < 4; i++) {
  2753. reg = FDI_TX_CTL(pipe);
  2754. temp = I915_READ(reg);
  2755. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2756. temp |= snb_b_fdi_train_param[i];
  2757. I915_WRITE(reg, temp);
  2758. POSTING_READ(reg);
  2759. udelay(500);
  2760. for (retry = 0; retry < 5; retry++) {
  2761. reg = FDI_RX_IIR(pipe);
  2762. temp = I915_READ(reg);
  2763. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2764. if (temp & FDI_RX_SYMBOL_LOCK) {
  2765. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2766. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2767. break;
  2768. }
  2769. udelay(50);
  2770. }
  2771. if (retry < 5)
  2772. break;
  2773. }
  2774. if (i == 4)
  2775. DRM_ERROR("FDI train 2 fail!\n");
  2776. DRM_DEBUG_KMS("FDI train done.\n");
  2777. }
  2778. /* Manual link training for Ivy Bridge A0 parts */
  2779. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2780. {
  2781. struct drm_device *dev = crtc->dev;
  2782. struct drm_i915_private *dev_priv = dev->dev_private;
  2783. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2784. int pipe = intel_crtc->pipe;
  2785. u32 reg, temp, i, j;
  2786. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2787. for train result */
  2788. reg = FDI_RX_IMR(pipe);
  2789. temp = I915_READ(reg);
  2790. temp &= ~FDI_RX_SYMBOL_LOCK;
  2791. temp &= ~FDI_RX_BIT_LOCK;
  2792. I915_WRITE(reg, temp);
  2793. POSTING_READ(reg);
  2794. udelay(150);
  2795. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  2796. I915_READ(FDI_RX_IIR(pipe)));
  2797. /* Try each vswing and preemphasis setting twice before moving on */
  2798. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  2799. /* disable first in case we need to retry */
  2800. reg = FDI_TX_CTL(pipe);
  2801. temp = I915_READ(reg);
  2802. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2803. temp &= ~FDI_TX_ENABLE;
  2804. I915_WRITE(reg, temp);
  2805. reg = FDI_RX_CTL(pipe);
  2806. temp = I915_READ(reg);
  2807. temp &= ~FDI_LINK_TRAIN_AUTO;
  2808. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2809. temp &= ~FDI_RX_ENABLE;
  2810. I915_WRITE(reg, temp);
  2811. /* enable CPU FDI TX and PCH FDI RX */
  2812. reg = FDI_TX_CTL(pipe);
  2813. temp = I915_READ(reg);
  2814. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2815. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2816. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2817. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2818. temp |= snb_b_fdi_train_param[j/2];
  2819. temp |= FDI_COMPOSITE_SYNC;
  2820. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2821. I915_WRITE(FDI_RX_MISC(pipe),
  2822. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2823. reg = FDI_RX_CTL(pipe);
  2824. temp = I915_READ(reg);
  2825. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2826. temp |= FDI_COMPOSITE_SYNC;
  2827. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2828. POSTING_READ(reg);
  2829. udelay(1); /* should be 0.5us */
  2830. for (i = 0; i < 4; i++) {
  2831. reg = FDI_RX_IIR(pipe);
  2832. temp = I915_READ(reg);
  2833. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2834. if (temp & FDI_RX_BIT_LOCK ||
  2835. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2836. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2837. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  2838. i);
  2839. break;
  2840. }
  2841. udelay(1); /* should be 0.5us */
  2842. }
  2843. if (i == 4) {
  2844. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  2845. continue;
  2846. }
  2847. /* Train 2 */
  2848. reg = FDI_TX_CTL(pipe);
  2849. temp = I915_READ(reg);
  2850. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2851. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2852. I915_WRITE(reg, temp);
  2853. reg = FDI_RX_CTL(pipe);
  2854. temp = I915_READ(reg);
  2855. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2856. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2857. I915_WRITE(reg, temp);
  2858. POSTING_READ(reg);
  2859. udelay(2); /* should be 1.5us */
  2860. for (i = 0; i < 4; i++) {
  2861. reg = FDI_RX_IIR(pipe);
  2862. temp = I915_READ(reg);
  2863. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2864. if (temp & FDI_RX_SYMBOL_LOCK ||
  2865. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  2866. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2867. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  2868. i);
  2869. goto train_done;
  2870. }
  2871. udelay(2); /* should be 1.5us */
  2872. }
  2873. if (i == 4)
  2874. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  2875. }
  2876. train_done:
  2877. DRM_DEBUG_KMS("FDI train done.\n");
  2878. }
  2879. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  2880. {
  2881. struct drm_device *dev = intel_crtc->base.dev;
  2882. struct drm_i915_private *dev_priv = dev->dev_private;
  2883. int pipe = intel_crtc->pipe;
  2884. u32 reg, temp;
  2885. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2886. reg = FDI_RX_CTL(pipe);
  2887. temp = I915_READ(reg);
  2888. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  2889. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2890. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2891. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2892. POSTING_READ(reg);
  2893. udelay(200);
  2894. /* Switch from Rawclk to PCDclk */
  2895. temp = I915_READ(reg);
  2896. I915_WRITE(reg, temp | FDI_PCDCLK);
  2897. POSTING_READ(reg);
  2898. udelay(200);
  2899. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2900. reg = FDI_TX_CTL(pipe);
  2901. temp = I915_READ(reg);
  2902. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2903. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2904. POSTING_READ(reg);
  2905. udelay(100);
  2906. }
  2907. }
  2908. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  2909. {
  2910. struct drm_device *dev = intel_crtc->base.dev;
  2911. struct drm_i915_private *dev_priv = dev->dev_private;
  2912. int pipe = intel_crtc->pipe;
  2913. u32 reg, temp;
  2914. /* Switch from PCDclk to Rawclk */
  2915. reg = FDI_RX_CTL(pipe);
  2916. temp = I915_READ(reg);
  2917. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2918. /* Disable CPU FDI TX PLL */
  2919. reg = FDI_TX_CTL(pipe);
  2920. temp = I915_READ(reg);
  2921. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2922. POSTING_READ(reg);
  2923. udelay(100);
  2924. reg = FDI_RX_CTL(pipe);
  2925. temp = I915_READ(reg);
  2926. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2927. /* Wait for the clocks to turn off. */
  2928. POSTING_READ(reg);
  2929. udelay(100);
  2930. }
  2931. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2932. {
  2933. struct drm_device *dev = crtc->dev;
  2934. struct drm_i915_private *dev_priv = dev->dev_private;
  2935. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2936. int pipe = intel_crtc->pipe;
  2937. u32 reg, temp;
  2938. /* disable CPU FDI tx and PCH FDI rx */
  2939. reg = FDI_TX_CTL(pipe);
  2940. temp = I915_READ(reg);
  2941. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2942. POSTING_READ(reg);
  2943. reg = FDI_RX_CTL(pipe);
  2944. temp = I915_READ(reg);
  2945. temp &= ~(0x7 << 16);
  2946. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2947. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2948. POSTING_READ(reg);
  2949. udelay(100);
  2950. /* Ironlake workaround, disable clock pointer after downing FDI */
  2951. if (HAS_PCH_IBX(dev))
  2952. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2953. /* still set train pattern 1 */
  2954. reg = FDI_TX_CTL(pipe);
  2955. temp = I915_READ(reg);
  2956. temp &= ~FDI_LINK_TRAIN_NONE;
  2957. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2958. I915_WRITE(reg, temp);
  2959. reg = FDI_RX_CTL(pipe);
  2960. temp = I915_READ(reg);
  2961. if (HAS_PCH_CPT(dev)) {
  2962. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2963. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2964. } else {
  2965. temp &= ~FDI_LINK_TRAIN_NONE;
  2966. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2967. }
  2968. /* BPC in FDI rx is consistent with that in PIPECONF */
  2969. temp &= ~(0x07 << 16);
  2970. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2971. I915_WRITE(reg, temp);
  2972. POSTING_READ(reg);
  2973. udelay(100);
  2974. }
  2975. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  2976. {
  2977. struct intel_crtc *crtc;
  2978. /* Note that we don't need to be called with mode_config.lock here
  2979. * as our list of CRTC objects is static for the lifetime of the
  2980. * device and so cannot disappear as we iterate. Similarly, we can
  2981. * happily treat the predicates as racy, atomic checks as userspace
  2982. * cannot claim and pin a new fb without at least acquring the
  2983. * struct_mutex and so serialising with us.
  2984. */
  2985. for_each_intel_crtc(dev, crtc) {
  2986. if (atomic_read(&crtc->unpin_work_count) == 0)
  2987. continue;
  2988. if (crtc->unpin_work)
  2989. intel_wait_for_vblank(dev, crtc->pipe);
  2990. return true;
  2991. }
  2992. return false;
  2993. }
  2994. static void page_flip_completed(struct intel_crtc *intel_crtc)
  2995. {
  2996. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  2997. struct intel_unpin_work *work = intel_crtc->unpin_work;
  2998. /* ensure that the unpin work is consistent wrt ->pending. */
  2999. smp_rmb();
  3000. intel_crtc->unpin_work = NULL;
  3001. if (work->event)
  3002. drm_send_vblank_event(intel_crtc->base.dev,
  3003. intel_crtc->pipe,
  3004. work->event);
  3005. drm_crtc_vblank_put(&intel_crtc->base);
  3006. wake_up_all(&dev_priv->pending_flip_queue);
  3007. queue_work(dev_priv->wq, &work->work);
  3008. trace_i915_flip_complete(intel_crtc->plane,
  3009. work->pending_flip_obj);
  3010. }
  3011. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3012. {
  3013. struct drm_device *dev = crtc->dev;
  3014. struct drm_i915_private *dev_priv = dev->dev_private;
  3015. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3016. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3017. !intel_crtc_has_pending_flip(crtc),
  3018. 60*HZ) == 0)) {
  3019. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3020. unsigned long flags;
  3021. spin_lock_irqsave(&dev->event_lock, flags);
  3022. if (intel_crtc->unpin_work) {
  3023. WARN_ONCE(1, "Removing stuck page flip\n");
  3024. page_flip_completed(intel_crtc);
  3025. }
  3026. spin_unlock_irqrestore(&dev->event_lock, flags);
  3027. }
  3028. if (crtc->primary->fb) {
  3029. mutex_lock(&dev->struct_mutex);
  3030. intel_finish_fb(crtc->primary->fb);
  3031. mutex_unlock(&dev->struct_mutex);
  3032. }
  3033. }
  3034. /* Program iCLKIP clock to the desired frequency */
  3035. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3036. {
  3037. struct drm_device *dev = crtc->dev;
  3038. struct drm_i915_private *dev_priv = dev->dev_private;
  3039. int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
  3040. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3041. u32 temp;
  3042. mutex_lock(&dev_priv->dpio_lock);
  3043. /* It is necessary to ungate the pixclk gate prior to programming
  3044. * the divisors, and gate it back when it is done.
  3045. */
  3046. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3047. /* Disable SSCCTL */
  3048. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3049. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3050. SBI_SSCCTL_DISABLE,
  3051. SBI_ICLK);
  3052. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3053. if (clock == 20000) {
  3054. auxdiv = 1;
  3055. divsel = 0x41;
  3056. phaseinc = 0x20;
  3057. } else {
  3058. /* The iCLK virtual clock root frequency is in MHz,
  3059. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3060. * divisors, it is necessary to divide one by another, so we
  3061. * convert the virtual clock precision to KHz here for higher
  3062. * precision.
  3063. */
  3064. u32 iclk_virtual_root_freq = 172800 * 1000;
  3065. u32 iclk_pi_range = 64;
  3066. u32 desired_divisor, msb_divisor_value, pi_value;
  3067. desired_divisor = (iclk_virtual_root_freq / clock);
  3068. msb_divisor_value = desired_divisor / iclk_pi_range;
  3069. pi_value = desired_divisor % iclk_pi_range;
  3070. auxdiv = 0;
  3071. divsel = msb_divisor_value - 2;
  3072. phaseinc = pi_value;
  3073. }
  3074. /* This should not happen with any sane values */
  3075. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3076. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3077. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3078. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3079. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3080. clock,
  3081. auxdiv,
  3082. divsel,
  3083. phasedir,
  3084. phaseinc);
  3085. /* Program SSCDIVINTPHASE6 */
  3086. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3087. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3088. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3089. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3090. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3091. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3092. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3093. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3094. /* Program SSCAUXDIV */
  3095. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3096. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3097. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3098. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3099. /* Enable modulator and associated divider */
  3100. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3101. temp &= ~SBI_SSCCTL_DISABLE;
  3102. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3103. /* Wait for initialization time */
  3104. udelay(24);
  3105. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3106. mutex_unlock(&dev_priv->dpio_lock);
  3107. }
  3108. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3109. enum pipe pch_transcoder)
  3110. {
  3111. struct drm_device *dev = crtc->base.dev;
  3112. struct drm_i915_private *dev_priv = dev->dev_private;
  3113. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  3114. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3115. I915_READ(HTOTAL(cpu_transcoder)));
  3116. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3117. I915_READ(HBLANK(cpu_transcoder)));
  3118. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3119. I915_READ(HSYNC(cpu_transcoder)));
  3120. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3121. I915_READ(VTOTAL(cpu_transcoder)));
  3122. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3123. I915_READ(VBLANK(cpu_transcoder)));
  3124. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3125. I915_READ(VSYNC(cpu_transcoder)));
  3126. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3127. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3128. }
  3129. static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
  3130. {
  3131. struct drm_i915_private *dev_priv = dev->dev_private;
  3132. uint32_t temp;
  3133. temp = I915_READ(SOUTH_CHICKEN1);
  3134. if (temp & FDI_BC_BIFURCATION_SELECT)
  3135. return;
  3136. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3137. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3138. temp |= FDI_BC_BIFURCATION_SELECT;
  3139. DRM_DEBUG_KMS("enabling fdi C rx\n");
  3140. I915_WRITE(SOUTH_CHICKEN1, temp);
  3141. POSTING_READ(SOUTH_CHICKEN1);
  3142. }
  3143. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3144. {
  3145. struct drm_device *dev = intel_crtc->base.dev;
  3146. struct drm_i915_private *dev_priv = dev->dev_private;
  3147. switch (intel_crtc->pipe) {
  3148. case PIPE_A:
  3149. break;
  3150. case PIPE_B:
  3151. if (intel_crtc->config.fdi_lanes > 2)
  3152. WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
  3153. else
  3154. cpt_enable_fdi_bc_bifurcation(dev);
  3155. break;
  3156. case PIPE_C:
  3157. cpt_enable_fdi_bc_bifurcation(dev);
  3158. break;
  3159. default:
  3160. BUG();
  3161. }
  3162. }
  3163. /*
  3164. * Enable PCH resources required for PCH ports:
  3165. * - PCH PLLs
  3166. * - FDI training & RX/TX
  3167. * - update transcoder timings
  3168. * - DP transcoding bits
  3169. * - transcoder
  3170. */
  3171. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3172. {
  3173. struct drm_device *dev = crtc->dev;
  3174. struct drm_i915_private *dev_priv = dev->dev_private;
  3175. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3176. int pipe = intel_crtc->pipe;
  3177. u32 reg, temp;
  3178. assert_pch_transcoder_disabled(dev_priv, pipe);
  3179. if (IS_IVYBRIDGE(dev))
  3180. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3181. /* Write the TU size bits before fdi link training, so that error
  3182. * detection works. */
  3183. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3184. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3185. /* For PCH output, training FDI link */
  3186. dev_priv->display.fdi_link_train(crtc);
  3187. /* We need to program the right clock selection before writing the pixel
  3188. * mutliplier into the DPLL. */
  3189. if (HAS_PCH_CPT(dev)) {
  3190. u32 sel;
  3191. temp = I915_READ(PCH_DPLL_SEL);
  3192. temp |= TRANS_DPLL_ENABLE(pipe);
  3193. sel = TRANS_DPLLB_SEL(pipe);
  3194. if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
  3195. temp |= sel;
  3196. else
  3197. temp &= ~sel;
  3198. I915_WRITE(PCH_DPLL_SEL, temp);
  3199. }
  3200. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3201. * transcoder, and we actually should do this to not upset any PCH
  3202. * transcoder that already use the clock when we share it.
  3203. *
  3204. * Note that enable_shared_dpll tries to do the right thing, but
  3205. * get_shared_dpll unconditionally resets the pll - we need that to have
  3206. * the right LVDS enable sequence. */
  3207. intel_enable_shared_dpll(intel_crtc);
  3208. /* set transcoder timing, panel must allow it */
  3209. assert_panel_unlocked(dev_priv, pipe);
  3210. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3211. intel_fdi_normal_train(crtc);
  3212. /* For PCH DP, enable TRANS_DP_CTL */
  3213. if (HAS_PCH_CPT(dev) &&
  3214. (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  3215. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  3216. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3217. reg = TRANS_DP_CTL(pipe);
  3218. temp = I915_READ(reg);
  3219. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3220. TRANS_DP_SYNC_MASK |
  3221. TRANS_DP_BPC_MASK);
  3222. temp |= (TRANS_DP_OUTPUT_ENABLE |
  3223. TRANS_DP_ENH_FRAMING);
  3224. temp |= bpc << 9; /* same format but at 11:9 */
  3225. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3226. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3227. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3228. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3229. switch (intel_trans_dp_port_sel(crtc)) {
  3230. case PCH_DP_B:
  3231. temp |= TRANS_DP_PORT_SEL_B;
  3232. break;
  3233. case PCH_DP_C:
  3234. temp |= TRANS_DP_PORT_SEL_C;
  3235. break;
  3236. case PCH_DP_D:
  3237. temp |= TRANS_DP_PORT_SEL_D;
  3238. break;
  3239. default:
  3240. BUG();
  3241. }
  3242. I915_WRITE(reg, temp);
  3243. }
  3244. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3245. }
  3246. static void lpt_pch_enable(struct drm_crtc *crtc)
  3247. {
  3248. struct drm_device *dev = crtc->dev;
  3249. struct drm_i915_private *dev_priv = dev->dev_private;
  3250. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3251. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3252. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3253. lpt_program_iclkip(crtc);
  3254. /* Set transcoder timing. */
  3255. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3256. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3257. }
  3258. void intel_put_shared_dpll(struct intel_crtc *crtc)
  3259. {
  3260. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3261. if (pll == NULL)
  3262. return;
  3263. if (pll->refcount == 0) {
  3264. WARN(1, "bad %s refcount\n", pll->name);
  3265. return;
  3266. }
  3267. if (--pll->refcount == 0) {
  3268. WARN_ON(pll->on);
  3269. WARN_ON(pll->active);
  3270. }
  3271. crtc->config.shared_dpll = DPLL_ID_PRIVATE;
  3272. }
  3273. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
  3274. {
  3275. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3276. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3277. enum intel_dpll_id i;
  3278. if (pll) {
  3279. DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
  3280. crtc->base.base.id, pll->name);
  3281. intel_put_shared_dpll(crtc);
  3282. }
  3283. if (HAS_PCH_IBX(dev_priv->dev)) {
  3284. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3285. i = (enum intel_dpll_id) crtc->pipe;
  3286. pll = &dev_priv->shared_dplls[i];
  3287. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3288. crtc->base.base.id, pll->name);
  3289. WARN_ON(pll->refcount);
  3290. goto found;
  3291. }
  3292. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3293. pll = &dev_priv->shared_dplls[i];
  3294. /* Only want to check enabled timings first */
  3295. if (pll->refcount == 0)
  3296. continue;
  3297. if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
  3298. sizeof(pll->hw_state)) == 0) {
  3299. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
  3300. crtc->base.base.id,
  3301. pll->name, pll->refcount, pll->active);
  3302. goto found;
  3303. }
  3304. }
  3305. /* Ok no matching timings, maybe there's a free one? */
  3306. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3307. pll = &dev_priv->shared_dplls[i];
  3308. if (pll->refcount == 0) {
  3309. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3310. crtc->base.base.id, pll->name);
  3311. goto found;
  3312. }
  3313. }
  3314. return NULL;
  3315. found:
  3316. if (pll->refcount == 0)
  3317. pll->hw_state = crtc->config.dpll_hw_state;
  3318. crtc->config.shared_dpll = i;
  3319. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3320. pipe_name(crtc->pipe));
  3321. pll->refcount++;
  3322. return pll;
  3323. }
  3324. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3325. {
  3326. struct drm_i915_private *dev_priv = dev->dev_private;
  3327. int dslreg = PIPEDSL(pipe);
  3328. u32 temp;
  3329. temp = I915_READ(dslreg);
  3330. udelay(500);
  3331. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3332. if (wait_for(I915_READ(dslreg) != temp, 5))
  3333. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3334. }
  3335. }
  3336. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3337. {
  3338. struct drm_device *dev = crtc->base.dev;
  3339. struct drm_i915_private *dev_priv = dev->dev_private;
  3340. int pipe = crtc->pipe;
  3341. if (crtc->config.pch_pfit.enabled) {
  3342. /* Force use of hard-coded filter coefficients
  3343. * as some pre-programmed values are broken,
  3344. * e.g. x201.
  3345. */
  3346. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3347. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3348. PF_PIPE_SEL_IVB(pipe));
  3349. else
  3350. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3351. I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
  3352. I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
  3353. }
  3354. }
  3355. static void intel_enable_planes(struct drm_crtc *crtc)
  3356. {
  3357. struct drm_device *dev = crtc->dev;
  3358. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3359. struct drm_plane *plane;
  3360. struct intel_plane *intel_plane;
  3361. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3362. intel_plane = to_intel_plane(plane);
  3363. if (intel_plane->pipe == pipe)
  3364. intel_plane_restore(&intel_plane->base);
  3365. }
  3366. }
  3367. static void intel_disable_planes(struct drm_crtc *crtc)
  3368. {
  3369. struct drm_device *dev = crtc->dev;
  3370. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3371. struct drm_plane *plane;
  3372. struct intel_plane *intel_plane;
  3373. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3374. intel_plane = to_intel_plane(plane);
  3375. if (intel_plane->pipe == pipe)
  3376. intel_plane_disable(&intel_plane->base);
  3377. }
  3378. }
  3379. void hsw_enable_ips(struct intel_crtc *crtc)
  3380. {
  3381. struct drm_device *dev = crtc->base.dev;
  3382. struct drm_i915_private *dev_priv = dev->dev_private;
  3383. if (!crtc->config.ips_enabled)
  3384. return;
  3385. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3386. intel_wait_for_vblank(dev, crtc->pipe);
  3387. assert_plane_enabled(dev_priv, crtc->plane);
  3388. if (IS_BROADWELL(dev)) {
  3389. mutex_lock(&dev_priv->rps.hw_lock);
  3390. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3391. mutex_unlock(&dev_priv->rps.hw_lock);
  3392. /* Quoting Art Runyan: "its not safe to expect any particular
  3393. * value in IPS_CTL bit 31 after enabling IPS through the
  3394. * mailbox." Moreover, the mailbox may return a bogus state,
  3395. * so we need to just enable it and continue on.
  3396. */
  3397. } else {
  3398. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3399. /* The bit only becomes 1 in the next vblank, so this wait here
  3400. * is essentially intel_wait_for_vblank. If we don't have this
  3401. * and don't wait for vblanks until the end of crtc_enable, then
  3402. * the HW state readout code will complain that the expected
  3403. * IPS_CTL value is not the one we read. */
  3404. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3405. DRM_ERROR("Timed out waiting for IPS enable\n");
  3406. }
  3407. }
  3408. void hsw_disable_ips(struct intel_crtc *crtc)
  3409. {
  3410. struct drm_device *dev = crtc->base.dev;
  3411. struct drm_i915_private *dev_priv = dev->dev_private;
  3412. if (!crtc->config.ips_enabled)
  3413. return;
  3414. assert_plane_enabled(dev_priv, crtc->plane);
  3415. if (IS_BROADWELL(dev)) {
  3416. mutex_lock(&dev_priv->rps.hw_lock);
  3417. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3418. mutex_unlock(&dev_priv->rps.hw_lock);
  3419. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3420. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3421. DRM_ERROR("Timed out waiting for IPS disable\n");
  3422. } else {
  3423. I915_WRITE(IPS_CTL, 0);
  3424. POSTING_READ(IPS_CTL);
  3425. }
  3426. /* We need to wait for a vblank before we can disable the plane. */
  3427. intel_wait_for_vblank(dev, crtc->pipe);
  3428. }
  3429. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3430. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3431. {
  3432. struct drm_device *dev = crtc->dev;
  3433. struct drm_i915_private *dev_priv = dev->dev_private;
  3434. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3435. enum pipe pipe = intel_crtc->pipe;
  3436. int palreg = PALETTE(pipe);
  3437. int i;
  3438. bool reenable_ips = false;
  3439. /* The clocks have to be on to load the palette. */
  3440. if (!crtc->enabled || !intel_crtc->active)
  3441. return;
  3442. if (!HAS_PCH_SPLIT(dev_priv->dev)) {
  3443. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  3444. assert_dsi_pll_enabled(dev_priv);
  3445. else
  3446. assert_pll_enabled(dev_priv, pipe);
  3447. }
  3448. /* use legacy palette for Ironlake */
  3449. if (!HAS_GMCH_DISPLAY(dev))
  3450. palreg = LGC_PALETTE(pipe);
  3451. /* Workaround : Do not read or write the pipe palette/gamma data while
  3452. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3453. */
  3454. if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
  3455. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3456. GAMMA_MODE_MODE_SPLIT)) {
  3457. hsw_disable_ips(intel_crtc);
  3458. reenable_ips = true;
  3459. }
  3460. for (i = 0; i < 256; i++) {
  3461. I915_WRITE(palreg + 4 * i,
  3462. (intel_crtc->lut_r[i] << 16) |
  3463. (intel_crtc->lut_g[i] << 8) |
  3464. intel_crtc->lut_b[i]);
  3465. }
  3466. if (reenable_ips)
  3467. hsw_enable_ips(intel_crtc);
  3468. }
  3469. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  3470. {
  3471. if (!enable && intel_crtc->overlay) {
  3472. struct drm_device *dev = intel_crtc->base.dev;
  3473. struct drm_i915_private *dev_priv = dev->dev_private;
  3474. mutex_lock(&dev->struct_mutex);
  3475. dev_priv->mm.interruptible = false;
  3476. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3477. dev_priv->mm.interruptible = true;
  3478. mutex_unlock(&dev->struct_mutex);
  3479. }
  3480. /* Let userspace switch the overlay on again. In most cases userspace
  3481. * has to recompute where to put it anyway.
  3482. */
  3483. }
  3484. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  3485. {
  3486. struct drm_device *dev = crtc->dev;
  3487. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3488. int pipe = intel_crtc->pipe;
  3489. assert_vblank_disabled(crtc);
  3490. drm_vblank_on(dev, pipe);
  3491. intel_enable_primary_hw_plane(crtc->primary, crtc);
  3492. intel_enable_planes(crtc);
  3493. intel_crtc_update_cursor(crtc, true);
  3494. intel_crtc_dpms_overlay(intel_crtc, true);
  3495. hsw_enable_ips(intel_crtc);
  3496. mutex_lock(&dev->struct_mutex);
  3497. intel_update_fbc(dev);
  3498. mutex_unlock(&dev->struct_mutex);
  3499. /*
  3500. * FIXME: Once we grow proper nuclear flip support out of this we need
  3501. * to compute the mask of flip planes precisely. For the time being
  3502. * consider this a flip from a NULL plane.
  3503. */
  3504. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3505. }
  3506. static void intel_crtc_disable_planes(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. int pipe = intel_crtc->pipe;
  3512. int plane = intel_crtc->plane;
  3513. intel_crtc_wait_for_pending_flips(crtc);
  3514. if (dev_priv->fbc.plane == plane)
  3515. intel_disable_fbc(dev);
  3516. hsw_disable_ips(intel_crtc);
  3517. intel_crtc_dpms_overlay(intel_crtc, false);
  3518. intel_crtc_update_cursor(crtc, false);
  3519. intel_disable_planes(crtc);
  3520. intel_disable_primary_hw_plane(crtc->primary, crtc);
  3521. /*
  3522. * FIXME: Once we grow proper nuclear flip support out of this we need
  3523. * to compute the mask of flip planes precisely. For the time being
  3524. * consider this a flip to a NULL plane.
  3525. */
  3526. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3527. drm_vblank_off(dev, pipe);
  3528. assert_vblank_disabled(crtc);
  3529. }
  3530. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  3531. {
  3532. struct drm_device *dev = crtc->dev;
  3533. struct drm_i915_private *dev_priv = dev->dev_private;
  3534. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3535. struct intel_encoder *encoder;
  3536. int pipe = intel_crtc->pipe;
  3537. WARN_ON(!crtc->enabled);
  3538. if (intel_crtc->active)
  3539. return;
  3540. if (intel_crtc->config.has_pch_encoder)
  3541. intel_prepare_shared_dpll(intel_crtc);
  3542. if (intel_crtc->config.has_dp_encoder)
  3543. intel_dp_set_m_n(intel_crtc);
  3544. intel_set_pipe_timings(intel_crtc);
  3545. if (intel_crtc->config.has_pch_encoder) {
  3546. intel_cpu_transcoder_set_m_n(intel_crtc,
  3547. &intel_crtc->config.fdi_m_n, NULL);
  3548. }
  3549. ironlake_set_pipeconf(crtc);
  3550. intel_crtc->active = true;
  3551. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3552. intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
  3553. for_each_encoder_on_crtc(dev, crtc, encoder)
  3554. if (encoder->pre_enable)
  3555. encoder->pre_enable(encoder);
  3556. if (intel_crtc->config.has_pch_encoder) {
  3557. /* Note: FDI PLL enabling _must_ be done before we enable the
  3558. * cpu pipes, hence this is separate from all the other fdi/pch
  3559. * enabling. */
  3560. ironlake_fdi_pll_enable(intel_crtc);
  3561. } else {
  3562. assert_fdi_tx_disabled(dev_priv, pipe);
  3563. assert_fdi_rx_disabled(dev_priv, pipe);
  3564. }
  3565. ironlake_pfit_enable(intel_crtc);
  3566. /*
  3567. * On ILK+ LUT must be loaded before the pipe is running but with
  3568. * clocks enabled
  3569. */
  3570. intel_crtc_load_lut(crtc);
  3571. intel_update_watermarks(crtc);
  3572. intel_enable_pipe(intel_crtc);
  3573. if (intel_crtc->config.has_pch_encoder)
  3574. ironlake_pch_enable(crtc);
  3575. for_each_encoder_on_crtc(dev, crtc, encoder)
  3576. encoder->enable(encoder);
  3577. if (HAS_PCH_CPT(dev))
  3578. cpt_verify_modeset(dev, intel_crtc->pipe);
  3579. intel_crtc_enable_planes(crtc);
  3580. }
  3581. /* IPS only exists on ULT machines and is tied to pipe A. */
  3582. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  3583. {
  3584. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  3585. }
  3586. /*
  3587. * This implements the workaround described in the "notes" section of the mode
  3588. * set sequence documentation. When going from no pipes or single pipe to
  3589. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  3590. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  3591. */
  3592. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  3593. {
  3594. struct drm_device *dev = crtc->base.dev;
  3595. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  3596. /* We want to get the other_active_crtc only if there's only 1 other
  3597. * active crtc. */
  3598. for_each_intel_crtc(dev, crtc_it) {
  3599. if (!crtc_it->active || crtc_it == crtc)
  3600. continue;
  3601. if (other_active_crtc)
  3602. return;
  3603. other_active_crtc = crtc_it;
  3604. }
  3605. if (!other_active_crtc)
  3606. return;
  3607. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3608. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3609. }
  3610. static void haswell_crtc_enable(struct drm_crtc *crtc)
  3611. {
  3612. struct drm_device *dev = crtc->dev;
  3613. struct drm_i915_private *dev_priv = dev->dev_private;
  3614. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3615. struct intel_encoder *encoder;
  3616. int pipe = intel_crtc->pipe;
  3617. WARN_ON(!crtc->enabled);
  3618. if (intel_crtc->active)
  3619. return;
  3620. if (intel_crtc_to_shared_dpll(intel_crtc))
  3621. intel_enable_shared_dpll(intel_crtc);
  3622. if (intel_crtc->config.has_dp_encoder)
  3623. intel_dp_set_m_n(intel_crtc);
  3624. intel_set_pipe_timings(intel_crtc);
  3625. if (intel_crtc->config.has_pch_encoder) {
  3626. intel_cpu_transcoder_set_m_n(intel_crtc,
  3627. &intel_crtc->config.fdi_m_n, NULL);
  3628. }
  3629. haswell_set_pipeconf(crtc);
  3630. intel_set_pipe_csc(crtc);
  3631. intel_crtc->active = true;
  3632. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3633. for_each_encoder_on_crtc(dev, crtc, encoder)
  3634. if (encoder->pre_enable)
  3635. encoder->pre_enable(encoder);
  3636. if (intel_crtc->config.has_pch_encoder) {
  3637. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
  3638. dev_priv->display.fdi_link_train(crtc);
  3639. }
  3640. intel_ddi_enable_pipe_clock(intel_crtc);
  3641. ironlake_pfit_enable(intel_crtc);
  3642. /*
  3643. * On ILK+ LUT must be loaded before the pipe is running but with
  3644. * clocks enabled
  3645. */
  3646. intel_crtc_load_lut(crtc);
  3647. intel_ddi_set_pipe_settings(crtc);
  3648. intel_ddi_enable_transcoder_func(crtc);
  3649. intel_update_watermarks(crtc);
  3650. intel_enable_pipe(intel_crtc);
  3651. if (intel_crtc->config.has_pch_encoder)
  3652. lpt_pch_enable(crtc);
  3653. if (intel_crtc->config.dp_encoder_is_mst)
  3654. intel_ddi_set_vc_payload_alloc(crtc, true);
  3655. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3656. encoder->enable(encoder);
  3657. intel_opregion_notify_encoder(encoder, true);
  3658. }
  3659. /* If we change the relative order between pipe/planes enabling, we need
  3660. * to change the workaround. */
  3661. haswell_mode_set_planes_workaround(intel_crtc);
  3662. intel_crtc_enable_planes(crtc);
  3663. }
  3664. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  3665. {
  3666. struct drm_device *dev = crtc->base.dev;
  3667. struct drm_i915_private *dev_priv = dev->dev_private;
  3668. int pipe = crtc->pipe;
  3669. /* To avoid upsetting the power well on haswell only disable the pfit if
  3670. * it's in use. The hw state code will make sure we get this right. */
  3671. if (crtc->config.pch_pfit.enabled) {
  3672. I915_WRITE(PF_CTL(pipe), 0);
  3673. I915_WRITE(PF_WIN_POS(pipe), 0);
  3674. I915_WRITE(PF_WIN_SZ(pipe), 0);
  3675. }
  3676. }
  3677. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  3678. {
  3679. struct drm_device *dev = crtc->dev;
  3680. struct drm_i915_private *dev_priv = dev->dev_private;
  3681. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3682. struct intel_encoder *encoder;
  3683. int pipe = intel_crtc->pipe;
  3684. u32 reg, temp;
  3685. if (!intel_crtc->active)
  3686. return;
  3687. intel_crtc_disable_planes(crtc);
  3688. for_each_encoder_on_crtc(dev, crtc, encoder)
  3689. encoder->disable(encoder);
  3690. if (intel_crtc->config.has_pch_encoder)
  3691. intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
  3692. intel_disable_pipe(intel_crtc);
  3693. ironlake_pfit_disable(intel_crtc);
  3694. for_each_encoder_on_crtc(dev, crtc, encoder)
  3695. if (encoder->post_disable)
  3696. encoder->post_disable(encoder);
  3697. if (intel_crtc->config.has_pch_encoder) {
  3698. ironlake_fdi_disable(crtc);
  3699. ironlake_disable_pch_transcoder(dev_priv, pipe);
  3700. intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
  3701. if (HAS_PCH_CPT(dev)) {
  3702. /* disable TRANS_DP_CTL */
  3703. reg = TRANS_DP_CTL(pipe);
  3704. temp = I915_READ(reg);
  3705. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  3706. TRANS_DP_PORT_SEL_MASK);
  3707. temp |= TRANS_DP_PORT_SEL_NONE;
  3708. I915_WRITE(reg, temp);
  3709. /* disable DPLL_SEL */
  3710. temp = I915_READ(PCH_DPLL_SEL);
  3711. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  3712. I915_WRITE(PCH_DPLL_SEL, temp);
  3713. }
  3714. /* disable PCH DPLL */
  3715. intel_disable_shared_dpll(intel_crtc);
  3716. ironlake_fdi_pll_disable(intel_crtc);
  3717. }
  3718. intel_crtc->active = false;
  3719. intel_update_watermarks(crtc);
  3720. mutex_lock(&dev->struct_mutex);
  3721. intel_update_fbc(dev);
  3722. mutex_unlock(&dev->struct_mutex);
  3723. }
  3724. static void haswell_crtc_disable(struct drm_crtc *crtc)
  3725. {
  3726. struct drm_device *dev = crtc->dev;
  3727. struct drm_i915_private *dev_priv = dev->dev_private;
  3728. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3729. struct intel_encoder *encoder;
  3730. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3731. if (!intel_crtc->active)
  3732. return;
  3733. intel_crtc_disable_planes(crtc);
  3734. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3735. intel_opregion_notify_encoder(encoder, false);
  3736. encoder->disable(encoder);
  3737. }
  3738. if (intel_crtc->config.has_pch_encoder)
  3739. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
  3740. intel_disable_pipe(intel_crtc);
  3741. if (intel_crtc->config.dp_encoder_is_mst)
  3742. intel_ddi_set_vc_payload_alloc(crtc, false);
  3743. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  3744. ironlake_pfit_disable(intel_crtc);
  3745. intel_ddi_disable_pipe_clock(intel_crtc);
  3746. if (intel_crtc->config.has_pch_encoder) {
  3747. lpt_disable_pch_transcoder(dev_priv);
  3748. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
  3749. intel_ddi_fdi_disable(crtc);
  3750. }
  3751. for_each_encoder_on_crtc(dev, crtc, encoder)
  3752. if (encoder->post_disable)
  3753. encoder->post_disable(encoder);
  3754. intel_crtc->active = false;
  3755. intel_update_watermarks(crtc);
  3756. mutex_lock(&dev->struct_mutex);
  3757. intel_update_fbc(dev);
  3758. mutex_unlock(&dev->struct_mutex);
  3759. if (intel_crtc_to_shared_dpll(intel_crtc))
  3760. intel_disable_shared_dpll(intel_crtc);
  3761. }
  3762. static void ironlake_crtc_off(struct drm_crtc *crtc)
  3763. {
  3764. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3765. intel_put_shared_dpll(intel_crtc);
  3766. }
  3767. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  3768. {
  3769. struct drm_device *dev = crtc->base.dev;
  3770. struct drm_i915_private *dev_priv = dev->dev_private;
  3771. struct intel_crtc_config *pipe_config = &crtc->config;
  3772. if (!crtc->config.gmch_pfit.control)
  3773. return;
  3774. /*
  3775. * The panel fitter should only be adjusted whilst the pipe is disabled,
  3776. * according to register description and PRM.
  3777. */
  3778. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  3779. assert_pipe_disabled(dev_priv, crtc->pipe);
  3780. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  3781. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  3782. /* Border color in case we don't scale up to the full screen. Black by
  3783. * default, change to something else for debugging. */
  3784. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  3785. }
  3786. static enum intel_display_power_domain port_to_power_domain(enum port port)
  3787. {
  3788. switch (port) {
  3789. case PORT_A:
  3790. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  3791. case PORT_B:
  3792. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  3793. case PORT_C:
  3794. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  3795. case PORT_D:
  3796. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  3797. default:
  3798. WARN_ON_ONCE(1);
  3799. return POWER_DOMAIN_PORT_OTHER;
  3800. }
  3801. }
  3802. #define for_each_power_domain(domain, mask) \
  3803. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  3804. if ((1 << (domain)) & (mask))
  3805. enum intel_display_power_domain
  3806. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  3807. {
  3808. struct drm_device *dev = intel_encoder->base.dev;
  3809. struct intel_digital_port *intel_dig_port;
  3810. switch (intel_encoder->type) {
  3811. case INTEL_OUTPUT_UNKNOWN:
  3812. /* Only DDI platforms should ever use this output type */
  3813. WARN_ON_ONCE(!HAS_DDI(dev));
  3814. case INTEL_OUTPUT_DISPLAYPORT:
  3815. case INTEL_OUTPUT_HDMI:
  3816. case INTEL_OUTPUT_EDP:
  3817. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  3818. return port_to_power_domain(intel_dig_port->port);
  3819. case INTEL_OUTPUT_DP_MST:
  3820. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  3821. return port_to_power_domain(intel_dig_port->port);
  3822. case INTEL_OUTPUT_ANALOG:
  3823. return POWER_DOMAIN_PORT_CRT;
  3824. case INTEL_OUTPUT_DSI:
  3825. return POWER_DOMAIN_PORT_DSI;
  3826. default:
  3827. return POWER_DOMAIN_PORT_OTHER;
  3828. }
  3829. }
  3830. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  3831. {
  3832. struct drm_device *dev = crtc->dev;
  3833. struct intel_encoder *intel_encoder;
  3834. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3835. enum pipe pipe = intel_crtc->pipe;
  3836. unsigned long mask;
  3837. enum transcoder transcoder;
  3838. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  3839. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  3840. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  3841. if (intel_crtc->config.pch_pfit.enabled ||
  3842. intel_crtc->config.pch_pfit.force_thru)
  3843. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  3844. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  3845. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  3846. return mask;
  3847. }
  3848. void intel_display_set_init_power(struct drm_i915_private *dev_priv,
  3849. bool enable)
  3850. {
  3851. if (dev_priv->power_domains.init_power_on == enable)
  3852. return;
  3853. if (enable)
  3854. intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
  3855. else
  3856. intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
  3857. dev_priv->power_domains.init_power_on = enable;
  3858. }
  3859. static void modeset_update_crtc_power_domains(struct drm_device *dev)
  3860. {
  3861. struct drm_i915_private *dev_priv = dev->dev_private;
  3862. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  3863. struct intel_crtc *crtc;
  3864. /*
  3865. * First get all needed power domains, then put all unneeded, to avoid
  3866. * any unnecessary toggling of the power wells.
  3867. */
  3868. for_each_intel_crtc(dev, crtc) {
  3869. enum intel_display_power_domain domain;
  3870. if (!crtc->base.enabled)
  3871. continue;
  3872. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  3873. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  3874. intel_display_power_get(dev_priv, domain);
  3875. }
  3876. for_each_intel_crtc(dev, crtc) {
  3877. enum intel_display_power_domain domain;
  3878. for_each_power_domain(domain, crtc->enabled_power_domains)
  3879. intel_display_power_put(dev_priv, domain);
  3880. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  3881. }
  3882. intel_display_set_init_power(dev_priv, false);
  3883. }
  3884. /* returns HPLL frequency in kHz */
  3885. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  3886. {
  3887. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  3888. /* Obtain SKU information */
  3889. mutex_lock(&dev_priv->dpio_lock);
  3890. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  3891. CCK_FUSE_HPLL_FREQ_MASK;
  3892. mutex_unlock(&dev_priv->dpio_lock);
  3893. return vco_freq[hpll_freq] * 1000;
  3894. }
  3895. static void vlv_update_cdclk(struct drm_device *dev)
  3896. {
  3897. struct drm_i915_private *dev_priv = dev->dev_private;
  3898. dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  3899. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz",
  3900. dev_priv->vlv_cdclk_freq);
  3901. /*
  3902. * Program the gmbus_freq based on the cdclk frequency.
  3903. * BSpec erroneously claims we should aim for 4MHz, but
  3904. * in fact 1MHz is the correct frequency.
  3905. */
  3906. I915_WRITE(GMBUSFREQ_VLV, dev_priv->vlv_cdclk_freq);
  3907. }
  3908. /* Adjust CDclk dividers to allow high res or save power if possible */
  3909. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  3910. {
  3911. struct drm_i915_private *dev_priv = dev->dev_private;
  3912. u32 val, cmd;
  3913. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  3914. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  3915. cmd = 2;
  3916. else if (cdclk == 266667)
  3917. cmd = 1;
  3918. else
  3919. cmd = 0;
  3920. mutex_lock(&dev_priv->rps.hw_lock);
  3921. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  3922. val &= ~DSPFREQGUAR_MASK;
  3923. val |= (cmd << DSPFREQGUAR_SHIFT);
  3924. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  3925. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  3926. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  3927. 50)) {
  3928. DRM_ERROR("timed out waiting for CDclk change\n");
  3929. }
  3930. mutex_unlock(&dev_priv->rps.hw_lock);
  3931. if (cdclk == 400000) {
  3932. u32 divider, vco;
  3933. vco = valleyview_get_vco(dev_priv);
  3934. divider = DIV_ROUND_CLOSEST(vco << 1, cdclk) - 1;
  3935. mutex_lock(&dev_priv->dpio_lock);
  3936. /* adjust cdclk divider */
  3937. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  3938. val &= ~DISPLAY_FREQUENCY_VALUES;
  3939. val |= divider;
  3940. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  3941. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  3942. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  3943. 50))
  3944. DRM_ERROR("timed out waiting for CDclk change\n");
  3945. mutex_unlock(&dev_priv->dpio_lock);
  3946. }
  3947. mutex_lock(&dev_priv->dpio_lock);
  3948. /* adjust self-refresh exit latency value */
  3949. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  3950. val &= ~0x7f;
  3951. /*
  3952. * For high bandwidth configs, we set a higher latency in the bunit
  3953. * so that the core display fetch happens in time to avoid underruns.
  3954. */
  3955. if (cdclk == 400000)
  3956. val |= 4500 / 250; /* 4.5 usec */
  3957. else
  3958. val |= 3000 / 250; /* 3.0 usec */
  3959. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  3960. mutex_unlock(&dev_priv->dpio_lock);
  3961. vlv_update_cdclk(dev);
  3962. }
  3963. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  3964. {
  3965. struct drm_i915_private *dev_priv = dev->dev_private;
  3966. u32 val, cmd;
  3967. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  3968. switch (cdclk) {
  3969. case 400000:
  3970. cmd = 3;
  3971. break;
  3972. case 333333:
  3973. case 320000:
  3974. cmd = 2;
  3975. break;
  3976. case 266667:
  3977. cmd = 1;
  3978. break;
  3979. case 200000:
  3980. cmd = 0;
  3981. break;
  3982. default:
  3983. WARN_ON(1);
  3984. return;
  3985. }
  3986. mutex_lock(&dev_priv->rps.hw_lock);
  3987. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  3988. val &= ~DSPFREQGUAR_MASK_CHV;
  3989. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  3990. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  3991. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  3992. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  3993. 50)) {
  3994. DRM_ERROR("timed out waiting for CDclk change\n");
  3995. }
  3996. mutex_unlock(&dev_priv->rps.hw_lock);
  3997. vlv_update_cdclk(dev);
  3998. }
  3999. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4000. int max_pixclk)
  4001. {
  4002. int vco = valleyview_get_vco(dev_priv);
  4003. int freq_320 = (vco << 1) % 320000 != 0 ? 333333 : 320000;
  4004. /* FIXME: Punit isn't quite ready yet */
  4005. if (IS_CHERRYVIEW(dev_priv->dev))
  4006. return 400000;
  4007. /*
  4008. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4009. * 200MHz
  4010. * 267MHz
  4011. * 320/333MHz (depends on HPLL freq)
  4012. * 400MHz
  4013. * So we check to see whether we're above 90% of the lower bin and
  4014. * adjust if needed.
  4015. *
  4016. * We seem to get an unstable or solid color picture at 200MHz.
  4017. * Not sure what's wrong. For now use 200MHz only when all pipes
  4018. * are off.
  4019. */
  4020. if (max_pixclk > freq_320*9/10)
  4021. return 400000;
  4022. else if (max_pixclk > 266667*9/10)
  4023. return freq_320;
  4024. else if (max_pixclk > 0)
  4025. return 266667;
  4026. else
  4027. return 200000;
  4028. }
  4029. /* compute the max pixel clock for new configuration */
  4030. static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
  4031. {
  4032. struct drm_device *dev = dev_priv->dev;
  4033. struct intel_crtc *intel_crtc;
  4034. int max_pixclk = 0;
  4035. for_each_intel_crtc(dev, intel_crtc) {
  4036. if (intel_crtc->new_enabled)
  4037. max_pixclk = max(max_pixclk,
  4038. intel_crtc->new_config->adjusted_mode.crtc_clock);
  4039. }
  4040. return max_pixclk;
  4041. }
  4042. static void valleyview_modeset_global_pipes(struct drm_device *dev,
  4043. unsigned *prepare_pipes)
  4044. {
  4045. struct drm_i915_private *dev_priv = dev->dev_private;
  4046. struct intel_crtc *intel_crtc;
  4047. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4048. if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
  4049. dev_priv->vlv_cdclk_freq)
  4050. return;
  4051. /* disable/enable all currently active pipes while we change cdclk */
  4052. for_each_intel_crtc(dev, intel_crtc)
  4053. if (intel_crtc->base.enabled)
  4054. *prepare_pipes |= (1 << intel_crtc->pipe);
  4055. }
  4056. static void valleyview_modeset_global_resources(struct drm_device *dev)
  4057. {
  4058. struct drm_i915_private *dev_priv = dev->dev_private;
  4059. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4060. int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  4061. if (req_cdclk != dev_priv->vlv_cdclk_freq) {
  4062. if (IS_CHERRYVIEW(dev))
  4063. cherryview_set_cdclk(dev, req_cdclk);
  4064. else
  4065. valleyview_set_cdclk(dev, req_cdclk);
  4066. }
  4067. modeset_update_crtc_power_domains(dev);
  4068. }
  4069. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  4070. {
  4071. struct drm_device *dev = crtc->dev;
  4072. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4073. struct intel_encoder *encoder;
  4074. int pipe = intel_crtc->pipe;
  4075. bool is_dsi;
  4076. WARN_ON(!crtc->enabled);
  4077. if (intel_crtc->active)
  4078. return;
  4079. is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
  4080. if (!is_dsi) {
  4081. if (IS_CHERRYVIEW(dev))
  4082. chv_prepare_pll(intel_crtc);
  4083. else
  4084. vlv_prepare_pll(intel_crtc);
  4085. }
  4086. if (intel_crtc->config.has_dp_encoder)
  4087. intel_dp_set_m_n(intel_crtc);
  4088. intel_set_pipe_timings(intel_crtc);
  4089. i9xx_set_pipeconf(intel_crtc);
  4090. intel_crtc->active = true;
  4091. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  4092. for_each_encoder_on_crtc(dev, crtc, encoder)
  4093. if (encoder->pre_pll_enable)
  4094. encoder->pre_pll_enable(encoder);
  4095. if (!is_dsi) {
  4096. if (IS_CHERRYVIEW(dev))
  4097. chv_enable_pll(intel_crtc);
  4098. else
  4099. vlv_enable_pll(intel_crtc);
  4100. }
  4101. for_each_encoder_on_crtc(dev, crtc, encoder)
  4102. if (encoder->pre_enable)
  4103. encoder->pre_enable(encoder);
  4104. i9xx_pfit_enable(intel_crtc);
  4105. intel_crtc_load_lut(crtc);
  4106. intel_update_watermarks(crtc);
  4107. intel_enable_pipe(intel_crtc);
  4108. for_each_encoder_on_crtc(dev, crtc, encoder)
  4109. encoder->enable(encoder);
  4110. intel_crtc_enable_planes(crtc);
  4111. /* Underruns don't raise interrupts, so check manually. */
  4112. i9xx_check_fifo_underruns(dev);
  4113. }
  4114. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  4115. {
  4116. struct drm_device *dev = crtc->base.dev;
  4117. struct drm_i915_private *dev_priv = dev->dev_private;
  4118. I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
  4119. I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
  4120. }
  4121. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  4122. {
  4123. struct drm_device *dev = crtc->dev;
  4124. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4125. struct intel_encoder *encoder;
  4126. int pipe = intel_crtc->pipe;
  4127. WARN_ON(!crtc->enabled);
  4128. if (intel_crtc->active)
  4129. return;
  4130. i9xx_set_pll_dividers(intel_crtc);
  4131. if (intel_crtc->config.has_dp_encoder)
  4132. intel_dp_set_m_n(intel_crtc);
  4133. intel_set_pipe_timings(intel_crtc);
  4134. i9xx_set_pipeconf(intel_crtc);
  4135. intel_crtc->active = true;
  4136. if (!IS_GEN2(dev))
  4137. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  4138. for_each_encoder_on_crtc(dev, crtc, encoder)
  4139. if (encoder->pre_enable)
  4140. encoder->pre_enable(encoder);
  4141. i9xx_enable_pll(intel_crtc);
  4142. i9xx_pfit_enable(intel_crtc);
  4143. intel_crtc_load_lut(crtc);
  4144. intel_update_watermarks(crtc);
  4145. intel_enable_pipe(intel_crtc);
  4146. for_each_encoder_on_crtc(dev, crtc, encoder)
  4147. encoder->enable(encoder);
  4148. intel_crtc_enable_planes(crtc);
  4149. /*
  4150. * Gen2 reports pipe underruns whenever all planes are disabled.
  4151. * So don't enable underrun reporting before at least some planes
  4152. * are enabled.
  4153. * FIXME: Need to fix the logic to work when we turn off all planes
  4154. * but leave the pipe running.
  4155. */
  4156. if (IS_GEN2(dev))
  4157. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  4158. /* Underruns don't raise interrupts, so check manually. */
  4159. i9xx_check_fifo_underruns(dev);
  4160. }
  4161. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4162. {
  4163. struct drm_device *dev = crtc->base.dev;
  4164. struct drm_i915_private *dev_priv = dev->dev_private;
  4165. if (!crtc->config.gmch_pfit.control)
  4166. return;
  4167. assert_pipe_disabled(dev_priv, crtc->pipe);
  4168. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4169. I915_READ(PFIT_CONTROL));
  4170. I915_WRITE(PFIT_CONTROL, 0);
  4171. }
  4172. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  4173. {
  4174. struct drm_device *dev = crtc->dev;
  4175. struct drm_i915_private *dev_priv = dev->dev_private;
  4176. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4177. struct intel_encoder *encoder;
  4178. int pipe = intel_crtc->pipe;
  4179. if (!intel_crtc->active)
  4180. return;
  4181. /*
  4182. * Gen2 reports pipe underruns whenever all planes are disabled.
  4183. * So diasble underrun reporting before all the planes get disabled.
  4184. * FIXME: Need to fix the logic to work when we turn off all planes
  4185. * but leave the pipe running.
  4186. */
  4187. if (IS_GEN2(dev))
  4188. intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
  4189. /*
  4190. * Vblank time updates from the shadow to live plane control register
  4191. * are blocked if the memory self-refresh mode is active at that
  4192. * moment. So to make sure the plane gets truly disabled, disable
  4193. * first the self-refresh mode. The self-refresh enable bit in turn
  4194. * will be checked/applied by the HW only at the next frame start
  4195. * event which is after the vblank start event, so we need to have a
  4196. * wait-for-vblank between disabling the plane and the pipe.
  4197. */
  4198. intel_set_memory_cxsr(dev_priv, false);
  4199. intel_crtc_disable_planes(crtc);
  4200. for_each_encoder_on_crtc(dev, crtc, encoder)
  4201. encoder->disable(encoder);
  4202. /*
  4203. * On gen2 planes are double buffered but the pipe isn't, so we must
  4204. * wait for planes to fully turn off before disabling the pipe.
  4205. * We also need to wait on all gmch platforms because of the
  4206. * self-refresh mode constraint explained above.
  4207. */
  4208. intel_wait_for_vblank(dev, pipe);
  4209. intel_disable_pipe(intel_crtc);
  4210. i9xx_pfit_disable(intel_crtc);
  4211. for_each_encoder_on_crtc(dev, crtc, encoder)
  4212. if (encoder->post_disable)
  4213. encoder->post_disable(encoder);
  4214. if (!intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI)) {
  4215. if (IS_CHERRYVIEW(dev))
  4216. chv_disable_pll(dev_priv, pipe);
  4217. else if (IS_VALLEYVIEW(dev))
  4218. vlv_disable_pll(dev_priv, pipe);
  4219. else
  4220. i9xx_disable_pll(dev_priv, pipe);
  4221. }
  4222. if (!IS_GEN2(dev))
  4223. intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
  4224. intel_crtc->active = false;
  4225. intel_update_watermarks(crtc);
  4226. mutex_lock(&dev->struct_mutex);
  4227. intel_update_fbc(dev);
  4228. mutex_unlock(&dev->struct_mutex);
  4229. }
  4230. static void i9xx_crtc_off(struct drm_crtc *crtc)
  4231. {
  4232. }
  4233. static void intel_crtc_update_sarea(struct drm_crtc *crtc,
  4234. bool enabled)
  4235. {
  4236. struct drm_device *dev = crtc->dev;
  4237. struct drm_i915_master_private *master_priv;
  4238. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4239. int pipe = intel_crtc->pipe;
  4240. if (!dev->primary->master)
  4241. return;
  4242. master_priv = dev->primary->master->driver_priv;
  4243. if (!master_priv->sarea_priv)
  4244. return;
  4245. switch (pipe) {
  4246. case 0:
  4247. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  4248. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  4249. break;
  4250. case 1:
  4251. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  4252. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  4253. break;
  4254. default:
  4255. DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
  4256. break;
  4257. }
  4258. }
  4259. /* Master function to enable/disable CRTC and corresponding power wells */
  4260. void intel_crtc_control(struct drm_crtc *crtc, bool enable)
  4261. {
  4262. struct drm_device *dev = crtc->dev;
  4263. struct drm_i915_private *dev_priv = dev->dev_private;
  4264. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4265. enum intel_display_power_domain domain;
  4266. unsigned long domains;
  4267. if (enable) {
  4268. if (!intel_crtc->active) {
  4269. domains = get_crtc_power_domains(crtc);
  4270. for_each_power_domain(domain, domains)
  4271. intel_display_power_get(dev_priv, domain);
  4272. intel_crtc->enabled_power_domains = domains;
  4273. dev_priv->display.crtc_enable(crtc);
  4274. }
  4275. } else {
  4276. if (intel_crtc->active) {
  4277. dev_priv->display.crtc_disable(crtc);
  4278. domains = intel_crtc->enabled_power_domains;
  4279. for_each_power_domain(domain, domains)
  4280. intel_display_power_put(dev_priv, domain);
  4281. intel_crtc->enabled_power_domains = 0;
  4282. }
  4283. }
  4284. }
  4285. /**
  4286. * Sets the power management mode of the pipe and plane.
  4287. */
  4288. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  4289. {
  4290. struct drm_device *dev = crtc->dev;
  4291. struct intel_encoder *intel_encoder;
  4292. bool enable = false;
  4293. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4294. enable |= intel_encoder->connectors_active;
  4295. intel_crtc_control(crtc, enable);
  4296. intel_crtc_update_sarea(crtc, enable);
  4297. }
  4298. static void intel_crtc_disable(struct drm_crtc *crtc)
  4299. {
  4300. struct drm_device *dev = crtc->dev;
  4301. struct drm_connector *connector;
  4302. struct drm_i915_private *dev_priv = dev->dev_private;
  4303. struct drm_i915_gem_object *old_obj = intel_fb_obj(crtc->primary->fb);
  4304. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  4305. /* crtc should still be enabled when we disable it. */
  4306. WARN_ON(!crtc->enabled);
  4307. dev_priv->display.crtc_disable(crtc);
  4308. intel_crtc_update_sarea(crtc, false);
  4309. dev_priv->display.off(crtc);
  4310. if (crtc->primary->fb) {
  4311. mutex_lock(&dev->struct_mutex);
  4312. intel_unpin_fb_obj(old_obj);
  4313. i915_gem_track_fb(old_obj, NULL,
  4314. INTEL_FRONTBUFFER_PRIMARY(pipe));
  4315. mutex_unlock(&dev->struct_mutex);
  4316. crtc->primary->fb = NULL;
  4317. }
  4318. /* Update computed state. */
  4319. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  4320. if (!connector->encoder || !connector->encoder->crtc)
  4321. continue;
  4322. if (connector->encoder->crtc != crtc)
  4323. continue;
  4324. connector->dpms = DRM_MODE_DPMS_OFF;
  4325. to_intel_encoder(connector->encoder)->connectors_active = false;
  4326. }
  4327. }
  4328. void intel_encoder_destroy(struct drm_encoder *encoder)
  4329. {
  4330. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4331. drm_encoder_cleanup(encoder);
  4332. kfree(intel_encoder);
  4333. }
  4334. /* Simple dpms helper for encoders with just one connector, no cloning and only
  4335. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  4336. * state of the entire output pipe. */
  4337. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  4338. {
  4339. if (mode == DRM_MODE_DPMS_ON) {
  4340. encoder->connectors_active = true;
  4341. intel_crtc_update_dpms(encoder->base.crtc);
  4342. } else {
  4343. encoder->connectors_active = false;
  4344. intel_crtc_update_dpms(encoder->base.crtc);
  4345. }
  4346. }
  4347. /* Cross check the actual hw state with our own modeset state tracking (and it's
  4348. * internal consistency). */
  4349. static void intel_connector_check_state(struct intel_connector *connector)
  4350. {
  4351. if (connector->get_hw_state(connector)) {
  4352. struct intel_encoder *encoder = connector->encoder;
  4353. struct drm_crtc *crtc;
  4354. bool encoder_enabled;
  4355. enum pipe pipe;
  4356. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4357. connector->base.base.id,
  4358. connector->base.name);
  4359. /* there is no real hw state for MST connectors */
  4360. if (connector->mst_port)
  4361. return;
  4362. WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  4363. "wrong connector dpms state\n");
  4364. WARN(connector->base.encoder != &encoder->base,
  4365. "active connector not linked to encoder\n");
  4366. if (encoder) {
  4367. WARN(!encoder->connectors_active,
  4368. "encoder->connectors_active not set\n");
  4369. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  4370. WARN(!encoder_enabled, "encoder not enabled\n");
  4371. if (WARN_ON(!encoder->base.crtc))
  4372. return;
  4373. crtc = encoder->base.crtc;
  4374. WARN(!crtc->enabled, "crtc not enabled\n");
  4375. WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  4376. WARN(pipe != to_intel_crtc(crtc)->pipe,
  4377. "encoder active on the wrong pipe\n");
  4378. }
  4379. }
  4380. }
  4381. /* Even simpler default implementation, if there's really no special case to
  4382. * consider. */
  4383. void intel_connector_dpms(struct drm_connector *connector, int mode)
  4384. {
  4385. /* All the simple cases only support two dpms states. */
  4386. if (mode != DRM_MODE_DPMS_ON)
  4387. mode = DRM_MODE_DPMS_OFF;
  4388. if (mode == connector->dpms)
  4389. return;
  4390. connector->dpms = mode;
  4391. /* Only need to change hw state when actually enabled */
  4392. if (connector->encoder)
  4393. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  4394. intel_modeset_check_state(connector->dev);
  4395. }
  4396. /* Simple connector->get_hw_state implementation for encoders that support only
  4397. * one connector and no cloning and hence the encoder state determines the state
  4398. * of the connector. */
  4399. bool intel_connector_get_hw_state(struct intel_connector *connector)
  4400. {
  4401. enum pipe pipe = 0;
  4402. struct intel_encoder *encoder = connector->encoder;
  4403. return encoder->get_hw_state(encoder, &pipe);
  4404. }
  4405. static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  4406. struct intel_crtc_config *pipe_config)
  4407. {
  4408. struct drm_i915_private *dev_priv = dev->dev_private;
  4409. struct intel_crtc *pipe_B_crtc =
  4410. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  4411. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  4412. pipe_name(pipe), pipe_config->fdi_lanes);
  4413. if (pipe_config->fdi_lanes > 4) {
  4414. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  4415. pipe_name(pipe), pipe_config->fdi_lanes);
  4416. return false;
  4417. }
  4418. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  4419. if (pipe_config->fdi_lanes > 2) {
  4420. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  4421. pipe_config->fdi_lanes);
  4422. return false;
  4423. } else {
  4424. return true;
  4425. }
  4426. }
  4427. if (INTEL_INFO(dev)->num_pipes == 2)
  4428. return true;
  4429. /* Ivybridge 3 pipe is really complicated */
  4430. switch (pipe) {
  4431. case PIPE_A:
  4432. return true;
  4433. case PIPE_B:
  4434. if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
  4435. pipe_config->fdi_lanes > 2) {
  4436. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4437. pipe_name(pipe), pipe_config->fdi_lanes);
  4438. return false;
  4439. }
  4440. return true;
  4441. case PIPE_C:
  4442. if (!pipe_has_enabled_pch(pipe_B_crtc) ||
  4443. pipe_B_crtc->config.fdi_lanes <= 2) {
  4444. if (pipe_config->fdi_lanes > 2) {
  4445. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4446. pipe_name(pipe), pipe_config->fdi_lanes);
  4447. return false;
  4448. }
  4449. } else {
  4450. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  4451. return false;
  4452. }
  4453. return true;
  4454. default:
  4455. BUG();
  4456. }
  4457. }
  4458. #define RETRY 1
  4459. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  4460. struct intel_crtc_config *pipe_config)
  4461. {
  4462. struct drm_device *dev = intel_crtc->base.dev;
  4463. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4464. int lane, link_bw, fdi_dotclock;
  4465. bool setup_ok, needs_recompute = false;
  4466. retry:
  4467. /* FDI is a binary signal running at ~2.7GHz, encoding
  4468. * each output octet as 10 bits. The actual frequency
  4469. * is stored as a divider into a 100MHz clock, and the
  4470. * mode pixel clock is stored in units of 1KHz.
  4471. * Hence the bw of each lane in terms of the mode signal
  4472. * is:
  4473. */
  4474. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4475. fdi_dotclock = adjusted_mode->crtc_clock;
  4476. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  4477. pipe_config->pipe_bpp);
  4478. pipe_config->fdi_lanes = lane;
  4479. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  4480. link_bw, &pipe_config->fdi_m_n);
  4481. setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  4482. intel_crtc->pipe, pipe_config);
  4483. if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
  4484. pipe_config->pipe_bpp -= 2*3;
  4485. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  4486. pipe_config->pipe_bpp);
  4487. needs_recompute = true;
  4488. pipe_config->bw_constrained = true;
  4489. goto retry;
  4490. }
  4491. if (needs_recompute)
  4492. return RETRY;
  4493. return setup_ok ? 0 : -EINVAL;
  4494. }
  4495. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  4496. struct intel_crtc_config *pipe_config)
  4497. {
  4498. pipe_config->ips_enabled = i915.enable_ips &&
  4499. hsw_crtc_supports_ips(crtc) &&
  4500. pipe_config->pipe_bpp <= 24;
  4501. }
  4502. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  4503. struct intel_crtc_config *pipe_config)
  4504. {
  4505. struct drm_device *dev = crtc->base.dev;
  4506. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4507. /* FIXME should check pixel clock limits on all platforms */
  4508. if (INTEL_INFO(dev)->gen < 4) {
  4509. struct drm_i915_private *dev_priv = dev->dev_private;
  4510. int clock_limit =
  4511. dev_priv->display.get_display_clock_speed(dev);
  4512. /*
  4513. * Enable pixel doubling when the dot clock
  4514. * is > 90% of the (display) core speed.
  4515. *
  4516. * GDG double wide on either pipe,
  4517. * otherwise pipe A only.
  4518. */
  4519. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  4520. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  4521. clock_limit *= 2;
  4522. pipe_config->double_wide = true;
  4523. }
  4524. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  4525. return -EINVAL;
  4526. }
  4527. /*
  4528. * Pipe horizontal size must be even in:
  4529. * - DVO ganged mode
  4530. * - LVDS dual channel mode
  4531. * - Double wide pipe
  4532. */
  4533. if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4534. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  4535. pipe_config->pipe_src_w &= ~1;
  4536. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  4537. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  4538. */
  4539. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  4540. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  4541. return -EINVAL;
  4542. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
  4543. pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
  4544. } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
  4545. /* only a 8bpc pipe, with 6bpc dither through the panel fitter
  4546. * for lvds. */
  4547. pipe_config->pipe_bpp = 8*3;
  4548. }
  4549. if (HAS_IPS(dev))
  4550. hsw_compute_ips_config(crtc, pipe_config);
  4551. /*
  4552. * XXX: PCH/WRPLL clock sharing is done in ->mode_set, so make sure the
  4553. * old clock survives for now.
  4554. */
  4555. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev) || HAS_DDI(dev))
  4556. pipe_config->shared_dpll = crtc->config.shared_dpll;
  4557. if (pipe_config->has_pch_encoder)
  4558. return ironlake_fdi_compute_config(crtc, pipe_config);
  4559. return 0;
  4560. }
  4561. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  4562. {
  4563. struct drm_i915_private *dev_priv = dev->dev_private;
  4564. int vco = valleyview_get_vco(dev_priv);
  4565. u32 val;
  4566. int divider;
  4567. /* FIXME: Punit isn't quite ready yet */
  4568. if (IS_CHERRYVIEW(dev))
  4569. return 400000;
  4570. mutex_lock(&dev_priv->dpio_lock);
  4571. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4572. mutex_unlock(&dev_priv->dpio_lock);
  4573. divider = val & DISPLAY_FREQUENCY_VALUES;
  4574. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  4575. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4576. "cdclk change in progress\n");
  4577. return DIV_ROUND_CLOSEST(vco << 1, divider + 1);
  4578. }
  4579. static int i945_get_display_clock_speed(struct drm_device *dev)
  4580. {
  4581. return 400000;
  4582. }
  4583. static int i915_get_display_clock_speed(struct drm_device *dev)
  4584. {
  4585. return 333000;
  4586. }
  4587. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  4588. {
  4589. return 200000;
  4590. }
  4591. static int pnv_get_display_clock_speed(struct drm_device *dev)
  4592. {
  4593. u16 gcfgc = 0;
  4594. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4595. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4596. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  4597. return 267000;
  4598. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  4599. return 333000;
  4600. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  4601. return 444000;
  4602. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  4603. return 200000;
  4604. default:
  4605. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  4606. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  4607. return 133000;
  4608. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  4609. return 167000;
  4610. }
  4611. }
  4612. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  4613. {
  4614. u16 gcfgc = 0;
  4615. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4616. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  4617. return 133000;
  4618. else {
  4619. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4620. case GC_DISPLAY_CLOCK_333_MHZ:
  4621. return 333000;
  4622. default:
  4623. case GC_DISPLAY_CLOCK_190_200_MHZ:
  4624. return 190000;
  4625. }
  4626. }
  4627. }
  4628. static int i865_get_display_clock_speed(struct drm_device *dev)
  4629. {
  4630. return 266000;
  4631. }
  4632. static int i855_get_display_clock_speed(struct drm_device *dev)
  4633. {
  4634. u16 hpllcc = 0;
  4635. /* Assume that the hardware is in the high speed state. This
  4636. * should be the default.
  4637. */
  4638. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  4639. case GC_CLOCK_133_200:
  4640. case GC_CLOCK_100_200:
  4641. return 200000;
  4642. case GC_CLOCK_166_250:
  4643. return 250000;
  4644. case GC_CLOCK_100_133:
  4645. return 133000;
  4646. }
  4647. /* Shouldn't happen */
  4648. return 0;
  4649. }
  4650. static int i830_get_display_clock_speed(struct drm_device *dev)
  4651. {
  4652. return 133000;
  4653. }
  4654. static void
  4655. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  4656. {
  4657. while (*num > DATA_LINK_M_N_MASK ||
  4658. *den > DATA_LINK_M_N_MASK) {
  4659. *num >>= 1;
  4660. *den >>= 1;
  4661. }
  4662. }
  4663. static void compute_m_n(unsigned int m, unsigned int n,
  4664. uint32_t *ret_m, uint32_t *ret_n)
  4665. {
  4666. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  4667. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  4668. intel_reduce_m_n_ratio(ret_m, ret_n);
  4669. }
  4670. void
  4671. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  4672. int pixel_clock, int link_clock,
  4673. struct intel_link_m_n *m_n)
  4674. {
  4675. m_n->tu = 64;
  4676. compute_m_n(bits_per_pixel * pixel_clock,
  4677. link_clock * nlanes * 8,
  4678. &m_n->gmch_m, &m_n->gmch_n);
  4679. compute_m_n(pixel_clock, link_clock,
  4680. &m_n->link_m, &m_n->link_n);
  4681. }
  4682. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  4683. {
  4684. if (i915.panel_use_ssc >= 0)
  4685. return i915.panel_use_ssc != 0;
  4686. return dev_priv->vbt.lvds_use_ssc
  4687. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  4688. }
  4689. static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
  4690. {
  4691. struct drm_device *dev = crtc->dev;
  4692. struct drm_i915_private *dev_priv = dev->dev_private;
  4693. int refclk;
  4694. if (IS_VALLEYVIEW(dev)) {
  4695. refclk = 100000;
  4696. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  4697. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4698. refclk = dev_priv->vbt.lvds_ssc_freq;
  4699. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  4700. } else if (!IS_GEN2(dev)) {
  4701. refclk = 96000;
  4702. } else {
  4703. refclk = 48000;
  4704. }
  4705. return refclk;
  4706. }
  4707. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  4708. {
  4709. return (1 << dpll->n) << 16 | dpll->m2;
  4710. }
  4711. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  4712. {
  4713. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  4714. }
  4715. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  4716. intel_clock_t *reduced_clock)
  4717. {
  4718. struct drm_device *dev = crtc->base.dev;
  4719. u32 fp, fp2 = 0;
  4720. if (IS_PINEVIEW(dev)) {
  4721. fp = pnv_dpll_compute_fp(&crtc->config.dpll);
  4722. if (reduced_clock)
  4723. fp2 = pnv_dpll_compute_fp(reduced_clock);
  4724. } else {
  4725. fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
  4726. if (reduced_clock)
  4727. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  4728. }
  4729. crtc->config.dpll_hw_state.fp0 = fp;
  4730. crtc->lowfreq_avail = false;
  4731. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4732. reduced_clock && i915.powersave) {
  4733. crtc->config.dpll_hw_state.fp1 = fp2;
  4734. crtc->lowfreq_avail = true;
  4735. } else {
  4736. crtc->config.dpll_hw_state.fp1 = fp;
  4737. }
  4738. }
  4739. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  4740. pipe)
  4741. {
  4742. u32 reg_val;
  4743. /*
  4744. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  4745. * and set it to a reasonable value instead.
  4746. */
  4747. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4748. reg_val &= 0xffffff00;
  4749. reg_val |= 0x00000030;
  4750. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4751. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4752. reg_val &= 0x8cffffff;
  4753. reg_val = 0x8c000000;
  4754. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4755. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4756. reg_val &= 0xffffff00;
  4757. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4758. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4759. reg_val &= 0x00ffffff;
  4760. reg_val |= 0xb0000000;
  4761. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4762. }
  4763. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  4764. struct intel_link_m_n *m_n)
  4765. {
  4766. struct drm_device *dev = crtc->base.dev;
  4767. struct drm_i915_private *dev_priv = dev->dev_private;
  4768. int pipe = crtc->pipe;
  4769. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4770. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  4771. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  4772. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  4773. }
  4774. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  4775. struct intel_link_m_n *m_n,
  4776. struct intel_link_m_n *m2_n2)
  4777. {
  4778. struct drm_device *dev = crtc->base.dev;
  4779. struct drm_i915_private *dev_priv = dev->dev_private;
  4780. int pipe = crtc->pipe;
  4781. enum transcoder transcoder = crtc->config.cpu_transcoder;
  4782. if (INTEL_INFO(dev)->gen >= 5) {
  4783. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4784. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  4785. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  4786. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  4787. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  4788. * for gen < 8) and if DRRS is supported (to make sure the
  4789. * registers are not unnecessarily accessed).
  4790. */
  4791. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  4792. crtc->config.has_drrs) {
  4793. I915_WRITE(PIPE_DATA_M2(transcoder),
  4794. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  4795. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  4796. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  4797. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  4798. }
  4799. } else {
  4800. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4801. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  4802. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  4803. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  4804. }
  4805. }
  4806. void intel_dp_set_m_n(struct intel_crtc *crtc)
  4807. {
  4808. if (crtc->config.has_pch_encoder)
  4809. intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
  4810. else
  4811. intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n,
  4812. &crtc->config.dp_m2_n2);
  4813. }
  4814. static void vlv_update_pll(struct intel_crtc *crtc)
  4815. {
  4816. u32 dpll, dpll_md;
  4817. /*
  4818. * Enable DPIO clock input. We should never disable the reference
  4819. * clock for pipe B, since VGA hotplug / manual detection depends
  4820. * on it.
  4821. */
  4822. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  4823. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  4824. /* We should never disable this, set it here for state tracking */
  4825. if (crtc->pipe == PIPE_B)
  4826. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4827. dpll |= DPLL_VCO_ENABLE;
  4828. crtc->config.dpll_hw_state.dpll = dpll;
  4829. dpll_md = (crtc->config.pixel_multiplier - 1)
  4830. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4831. crtc->config.dpll_hw_state.dpll_md = dpll_md;
  4832. }
  4833. static void vlv_prepare_pll(struct intel_crtc *crtc)
  4834. {
  4835. struct drm_device *dev = crtc->base.dev;
  4836. struct drm_i915_private *dev_priv = dev->dev_private;
  4837. int pipe = crtc->pipe;
  4838. u32 mdiv;
  4839. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  4840. u32 coreclk, reg_val;
  4841. mutex_lock(&dev_priv->dpio_lock);
  4842. bestn = crtc->config.dpll.n;
  4843. bestm1 = crtc->config.dpll.m1;
  4844. bestm2 = crtc->config.dpll.m2;
  4845. bestp1 = crtc->config.dpll.p1;
  4846. bestp2 = crtc->config.dpll.p2;
  4847. /* See eDP HDMI DPIO driver vbios notes doc */
  4848. /* PLL B needs special handling */
  4849. if (pipe == PIPE_B)
  4850. vlv_pllb_recal_opamp(dev_priv, pipe);
  4851. /* Set up Tx target for periodic Rcomp update */
  4852. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  4853. /* Disable target IRef on PLL */
  4854. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  4855. reg_val &= 0x00ffffff;
  4856. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  4857. /* Disable fast lock */
  4858. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  4859. /* Set idtafcrecal before PLL is enabled */
  4860. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  4861. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  4862. mdiv |= ((bestn << DPIO_N_SHIFT));
  4863. mdiv |= (1 << DPIO_K_SHIFT);
  4864. /*
  4865. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  4866. * but we don't support that).
  4867. * Note: don't use the DAC post divider as it seems unstable.
  4868. */
  4869. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  4870. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4871. mdiv |= DPIO_ENABLE_CALIBRATION;
  4872. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4873. /* Set HBR and RBR LPF coefficients */
  4874. if (crtc->config.port_clock == 162000 ||
  4875. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
  4876. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
  4877. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4878. 0x009f0003);
  4879. else
  4880. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4881. 0x00d0000f);
  4882. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
  4883. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
  4884. /* Use SSC source */
  4885. if (pipe == PIPE_A)
  4886. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4887. 0x0df40000);
  4888. else
  4889. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4890. 0x0df70000);
  4891. } else { /* HDMI or VGA */
  4892. /* Use bend source */
  4893. if (pipe == PIPE_A)
  4894. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4895. 0x0df70000);
  4896. else
  4897. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4898. 0x0df40000);
  4899. }
  4900. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  4901. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  4902. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
  4903. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
  4904. coreclk |= 0x01000000;
  4905. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  4906. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  4907. mutex_unlock(&dev_priv->dpio_lock);
  4908. }
  4909. static void chv_update_pll(struct intel_crtc *crtc)
  4910. {
  4911. crtc->config.dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  4912. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  4913. DPLL_VCO_ENABLE;
  4914. if (crtc->pipe != PIPE_A)
  4915. crtc->config.dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4916. crtc->config.dpll_hw_state.dpll_md =
  4917. (crtc->config.pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4918. }
  4919. static void chv_prepare_pll(struct intel_crtc *crtc)
  4920. {
  4921. struct drm_device *dev = crtc->base.dev;
  4922. struct drm_i915_private *dev_priv = dev->dev_private;
  4923. int pipe = crtc->pipe;
  4924. int dpll_reg = DPLL(crtc->pipe);
  4925. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  4926. u32 loopfilter, intcoeff;
  4927. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  4928. int refclk;
  4929. bestn = crtc->config.dpll.n;
  4930. bestm2_frac = crtc->config.dpll.m2 & 0x3fffff;
  4931. bestm1 = crtc->config.dpll.m1;
  4932. bestm2 = crtc->config.dpll.m2 >> 22;
  4933. bestp1 = crtc->config.dpll.p1;
  4934. bestp2 = crtc->config.dpll.p2;
  4935. /*
  4936. * Enable Refclk and SSC
  4937. */
  4938. I915_WRITE(dpll_reg,
  4939. crtc->config.dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  4940. mutex_lock(&dev_priv->dpio_lock);
  4941. /* p1 and p2 divider */
  4942. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  4943. 5 << DPIO_CHV_S1_DIV_SHIFT |
  4944. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  4945. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  4946. 1 << DPIO_CHV_K_DIV_SHIFT);
  4947. /* Feedback post-divider - m2 */
  4948. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  4949. /* Feedback refclk divider - n and m1 */
  4950. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  4951. DPIO_CHV_M1_DIV_BY_2 |
  4952. 1 << DPIO_CHV_N_DIV_SHIFT);
  4953. /* M2 fraction division */
  4954. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  4955. /* M2 fraction division enable */
  4956. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
  4957. DPIO_CHV_FRAC_DIV_EN |
  4958. (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
  4959. /* Loop filter */
  4960. refclk = i9xx_get_refclk(&crtc->base, 0);
  4961. loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
  4962. 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
  4963. if (refclk == 100000)
  4964. intcoeff = 11;
  4965. else if (refclk == 38400)
  4966. intcoeff = 10;
  4967. else
  4968. intcoeff = 9;
  4969. loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
  4970. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  4971. /* AFC Recal */
  4972. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  4973. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  4974. DPIO_AFC_RECAL);
  4975. mutex_unlock(&dev_priv->dpio_lock);
  4976. }
  4977. static void i9xx_update_pll(struct intel_crtc *crtc,
  4978. intel_clock_t *reduced_clock,
  4979. int num_connectors)
  4980. {
  4981. struct drm_device *dev = crtc->base.dev;
  4982. struct drm_i915_private *dev_priv = dev->dev_private;
  4983. u32 dpll;
  4984. bool is_sdvo;
  4985. struct dpll *clock = &crtc->config.dpll;
  4986. i9xx_update_pll_dividers(crtc, reduced_clock);
  4987. is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
  4988. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
  4989. dpll = DPLL_VGA_MODE_DIS;
  4990. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
  4991. dpll |= DPLLB_MODE_LVDS;
  4992. else
  4993. dpll |= DPLLB_MODE_DAC_SERIAL;
  4994. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  4995. dpll |= (crtc->config.pixel_multiplier - 1)
  4996. << SDVO_MULTIPLIER_SHIFT_HIRES;
  4997. }
  4998. if (is_sdvo)
  4999. dpll |= DPLL_SDVO_HIGH_SPEED;
  5000. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
  5001. dpll |= DPLL_SDVO_HIGH_SPEED;
  5002. /* compute bitmask from p1 value */
  5003. if (IS_PINEVIEW(dev))
  5004. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5005. else {
  5006. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5007. if (IS_G4X(dev) && reduced_clock)
  5008. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5009. }
  5010. switch (clock->p2) {
  5011. case 5:
  5012. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5013. break;
  5014. case 7:
  5015. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5016. break;
  5017. case 10:
  5018. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5019. break;
  5020. case 14:
  5021. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5022. break;
  5023. }
  5024. if (INTEL_INFO(dev)->gen >= 4)
  5025. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5026. if (crtc->config.sdvo_tv_clock)
  5027. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5028. else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  5029. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5030. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5031. else
  5032. dpll |= PLL_REF_INPUT_DREFCLK;
  5033. dpll |= DPLL_VCO_ENABLE;
  5034. crtc->config.dpll_hw_state.dpll = dpll;
  5035. if (INTEL_INFO(dev)->gen >= 4) {
  5036. u32 dpll_md = (crtc->config.pixel_multiplier - 1)
  5037. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5038. crtc->config.dpll_hw_state.dpll_md = dpll_md;
  5039. }
  5040. }
  5041. static void i8xx_update_pll(struct intel_crtc *crtc,
  5042. intel_clock_t *reduced_clock,
  5043. int num_connectors)
  5044. {
  5045. struct drm_device *dev = crtc->base.dev;
  5046. struct drm_i915_private *dev_priv = dev->dev_private;
  5047. u32 dpll;
  5048. struct dpll *clock = &crtc->config.dpll;
  5049. i9xx_update_pll_dividers(crtc, reduced_clock);
  5050. dpll = DPLL_VGA_MODE_DIS;
  5051. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
  5052. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5053. } else {
  5054. if (clock->p1 == 2)
  5055. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5056. else
  5057. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5058. if (clock->p2 == 4)
  5059. dpll |= PLL_P2_DIVIDE_BY_4;
  5060. }
  5061. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
  5062. dpll |= DPLL_DVO_2X_MODE;
  5063. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  5064. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5065. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5066. else
  5067. dpll |= PLL_REF_INPUT_DREFCLK;
  5068. dpll |= DPLL_VCO_ENABLE;
  5069. crtc->config.dpll_hw_state.dpll = dpll;
  5070. }
  5071. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5072. {
  5073. struct drm_device *dev = intel_crtc->base.dev;
  5074. struct drm_i915_private *dev_priv = dev->dev_private;
  5075. enum pipe pipe = intel_crtc->pipe;
  5076. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  5077. struct drm_display_mode *adjusted_mode =
  5078. &intel_crtc->config.adjusted_mode;
  5079. uint32_t crtc_vtotal, crtc_vblank_end;
  5080. int vsyncshift = 0;
  5081. /* We need to be careful not to changed the adjusted mode, for otherwise
  5082. * the hw state checker will get angry at the mismatch. */
  5083. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5084. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5085. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5086. /* the chip adds 2 halflines automatically */
  5087. crtc_vtotal -= 1;
  5088. crtc_vblank_end -= 1;
  5089. if (intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
  5090. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5091. else
  5092. vsyncshift = adjusted_mode->crtc_hsync_start -
  5093. adjusted_mode->crtc_htotal / 2;
  5094. if (vsyncshift < 0)
  5095. vsyncshift += adjusted_mode->crtc_htotal;
  5096. }
  5097. if (INTEL_INFO(dev)->gen > 3)
  5098. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5099. I915_WRITE(HTOTAL(cpu_transcoder),
  5100. (adjusted_mode->crtc_hdisplay - 1) |
  5101. ((adjusted_mode->crtc_htotal - 1) << 16));
  5102. I915_WRITE(HBLANK(cpu_transcoder),
  5103. (adjusted_mode->crtc_hblank_start - 1) |
  5104. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5105. I915_WRITE(HSYNC(cpu_transcoder),
  5106. (adjusted_mode->crtc_hsync_start - 1) |
  5107. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5108. I915_WRITE(VTOTAL(cpu_transcoder),
  5109. (adjusted_mode->crtc_vdisplay - 1) |
  5110. ((crtc_vtotal - 1) << 16));
  5111. I915_WRITE(VBLANK(cpu_transcoder),
  5112. (adjusted_mode->crtc_vblank_start - 1) |
  5113. ((crtc_vblank_end - 1) << 16));
  5114. I915_WRITE(VSYNC(cpu_transcoder),
  5115. (adjusted_mode->crtc_vsync_start - 1) |
  5116. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5117. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5118. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5119. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5120. * bits. */
  5121. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  5122. (pipe == PIPE_B || pipe == PIPE_C))
  5123. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5124. /* pipesrc controls the size that is scaled from, which should
  5125. * always be the user's requested size.
  5126. */
  5127. I915_WRITE(PIPESRC(pipe),
  5128. ((intel_crtc->config.pipe_src_w - 1) << 16) |
  5129. (intel_crtc->config.pipe_src_h - 1));
  5130. }
  5131. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5132. struct intel_crtc_config *pipe_config)
  5133. {
  5134. struct drm_device *dev = crtc->base.dev;
  5135. struct drm_i915_private *dev_priv = dev->dev_private;
  5136. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5137. uint32_t tmp;
  5138. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5139. pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5140. pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5141. tmp = I915_READ(HBLANK(cpu_transcoder));
  5142. pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5143. pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5144. tmp = I915_READ(HSYNC(cpu_transcoder));
  5145. pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5146. pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5147. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5148. pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5149. pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  5150. tmp = I915_READ(VBLANK(cpu_transcoder));
  5151. pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  5152. pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  5153. tmp = I915_READ(VSYNC(cpu_transcoder));
  5154. pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  5155. pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  5156. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  5157. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  5158. pipe_config->adjusted_mode.crtc_vtotal += 1;
  5159. pipe_config->adjusted_mode.crtc_vblank_end += 1;
  5160. }
  5161. tmp = I915_READ(PIPESRC(crtc->pipe));
  5162. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  5163. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  5164. pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
  5165. pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
  5166. }
  5167. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  5168. struct intel_crtc_config *pipe_config)
  5169. {
  5170. mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
  5171. mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
  5172. mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
  5173. mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
  5174. mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
  5175. mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
  5176. mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
  5177. mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
  5178. mode->flags = pipe_config->adjusted_mode.flags;
  5179. mode->clock = pipe_config->adjusted_mode.crtc_clock;
  5180. mode->flags |= pipe_config->adjusted_mode.flags;
  5181. }
  5182. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  5183. {
  5184. struct drm_device *dev = intel_crtc->base.dev;
  5185. struct drm_i915_private *dev_priv = dev->dev_private;
  5186. uint32_t pipeconf;
  5187. pipeconf = 0;
  5188. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  5189. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  5190. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  5191. if (intel_crtc->config.double_wide)
  5192. pipeconf |= PIPECONF_DOUBLE_WIDE;
  5193. /* only g4x and later have fancy bpc/dither controls */
  5194. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5195. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  5196. if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
  5197. pipeconf |= PIPECONF_DITHER_EN |
  5198. PIPECONF_DITHER_TYPE_SP;
  5199. switch (intel_crtc->config.pipe_bpp) {
  5200. case 18:
  5201. pipeconf |= PIPECONF_6BPC;
  5202. break;
  5203. case 24:
  5204. pipeconf |= PIPECONF_8BPC;
  5205. break;
  5206. case 30:
  5207. pipeconf |= PIPECONF_10BPC;
  5208. break;
  5209. default:
  5210. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5211. BUG();
  5212. }
  5213. }
  5214. if (HAS_PIPE_CXSR(dev)) {
  5215. if (intel_crtc->lowfreq_avail) {
  5216. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  5217. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  5218. } else {
  5219. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  5220. }
  5221. }
  5222. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  5223. if (INTEL_INFO(dev)->gen < 4 ||
  5224. intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
  5225. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  5226. else
  5227. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  5228. } else
  5229. pipeconf |= PIPECONF_PROGRESSIVE;
  5230. if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
  5231. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  5232. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  5233. POSTING_READ(PIPECONF(intel_crtc->pipe));
  5234. }
  5235. static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
  5236. int x, int y,
  5237. struct drm_framebuffer *fb)
  5238. {
  5239. struct drm_device *dev = crtc->dev;
  5240. struct drm_i915_private *dev_priv = dev->dev_private;
  5241. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5242. int refclk, num_connectors = 0;
  5243. intel_clock_t clock, reduced_clock;
  5244. bool ok, has_reduced_clock = false;
  5245. bool is_lvds = false, is_dsi = false;
  5246. struct intel_encoder *encoder;
  5247. const intel_limit_t *limit;
  5248. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5249. switch (encoder->type) {
  5250. case INTEL_OUTPUT_LVDS:
  5251. is_lvds = true;
  5252. break;
  5253. case INTEL_OUTPUT_DSI:
  5254. is_dsi = true;
  5255. break;
  5256. }
  5257. num_connectors++;
  5258. }
  5259. if (is_dsi)
  5260. return 0;
  5261. if (!intel_crtc->config.clock_set) {
  5262. refclk = i9xx_get_refclk(crtc, num_connectors);
  5263. /*
  5264. * Returns a set of divisors for the desired target clock with
  5265. * the given refclk, or FALSE. The returned values represent
  5266. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  5267. * 2) / p1 / p2.
  5268. */
  5269. limit = intel_limit(crtc, refclk);
  5270. ok = dev_priv->display.find_dpll(limit, crtc,
  5271. intel_crtc->config.port_clock,
  5272. refclk, NULL, &clock);
  5273. if (!ok) {
  5274. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5275. return -EINVAL;
  5276. }
  5277. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5278. /*
  5279. * Ensure we match the reduced clock's P to the target
  5280. * clock. If the clocks don't match, we can't switch
  5281. * the display clock by using the FP0/FP1. In such case
  5282. * we will disable the LVDS downclock feature.
  5283. */
  5284. has_reduced_clock =
  5285. dev_priv->display.find_dpll(limit, crtc,
  5286. dev_priv->lvds_downclock,
  5287. refclk, &clock,
  5288. &reduced_clock);
  5289. }
  5290. /* Compat-code for transition, will disappear. */
  5291. intel_crtc->config.dpll.n = clock.n;
  5292. intel_crtc->config.dpll.m1 = clock.m1;
  5293. intel_crtc->config.dpll.m2 = clock.m2;
  5294. intel_crtc->config.dpll.p1 = clock.p1;
  5295. intel_crtc->config.dpll.p2 = clock.p2;
  5296. }
  5297. if (IS_GEN2(dev)) {
  5298. i8xx_update_pll(intel_crtc,
  5299. has_reduced_clock ? &reduced_clock : NULL,
  5300. num_connectors);
  5301. } else if (IS_CHERRYVIEW(dev)) {
  5302. chv_update_pll(intel_crtc);
  5303. } else if (IS_VALLEYVIEW(dev)) {
  5304. vlv_update_pll(intel_crtc);
  5305. } else {
  5306. i9xx_update_pll(intel_crtc,
  5307. has_reduced_clock ? &reduced_clock : NULL,
  5308. num_connectors);
  5309. }
  5310. return 0;
  5311. }
  5312. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  5313. struct intel_crtc_config *pipe_config)
  5314. {
  5315. struct drm_device *dev = crtc->base.dev;
  5316. struct drm_i915_private *dev_priv = dev->dev_private;
  5317. uint32_t tmp;
  5318. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  5319. return;
  5320. tmp = I915_READ(PFIT_CONTROL);
  5321. if (!(tmp & PFIT_ENABLE))
  5322. return;
  5323. /* Check whether the pfit is attached to our pipe. */
  5324. if (INTEL_INFO(dev)->gen < 4) {
  5325. if (crtc->pipe != PIPE_B)
  5326. return;
  5327. } else {
  5328. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  5329. return;
  5330. }
  5331. pipe_config->gmch_pfit.control = tmp;
  5332. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  5333. if (INTEL_INFO(dev)->gen < 5)
  5334. pipe_config->gmch_pfit.lvds_border_bits =
  5335. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  5336. }
  5337. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  5338. struct intel_crtc_config *pipe_config)
  5339. {
  5340. struct drm_device *dev = crtc->base.dev;
  5341. struct drm_i915_private *dev_priv = dev->dev_private;
  5342. int pipe = pipe_config->cpu_transcoder;
  5343. intel_clock_t clock;
  5344. u32 mdiv;
  5345. int refclk = 100000;
  5346. /* In case of MIPI DPLL will not even be used */
  5347. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  5348. return;
  5349. mutex_lock(&dev_priv->dpio_lock);
  5350. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  5351. mutex_unlock(&dev_priv->dpio_lock);
  5352. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  5353. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  5354. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  5355. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  5356. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  5357. vlv_clock(refclk, &clock);
  5358. /* clock.dot is the fast clock */
  5359. pipe_config->port_clock = clock.dot / 5;
  5360. }
  5361. static void i9xx_get_plane_config(struct intel_crtc *crtc,
  5362. struct intel_plane_config *plane_config)
  5363. {
  5364. struct drm_device *dev = crtc->base.dev;
  5365. struct drm_i915_private *dev_priv = dev->dev_private;
  5366. u32 val, base, offset;
  5367. int pipe = crtc->pipe, plane = crtc->plane;
  5368. int fourcc, pixel_format;
  5369. int aligned_height;
  5370. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  5371. if (!crtc->base.primary->fb) {
  5372. DRM_DEBUG_KMS("failed to alloc fb\n");
  5373. return;
  5374. }
  5375. val = I915_READ(DSPCNTR(plane));
  5376. if (INTEL_INFO(dev)->gen >= 4)
  5377. if (val & DISPPLANE_TILED)
  5378. plane_config->tiled = true;
  5379. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  5380. fourcc = intel_format_to_fourcc(pixel_format);
  5381. crtc->base.primary->fb->pixel_format = fourcc;
  5382. crtc->base.primary->fb->bits_per_pixel =
  5383. drm_format_plane_cpp(fourcc, 0) * 8;
  5384. if (INTEL_INFO(dev)->gen >= 4) {
  5385. if (plane_config->tiled)
  5386. offset = I915_READ(DSPTILEOFF(plane));
  5387. else
  5388. offset = I915_READ(DSPLINOFF(plane));
  5389. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  5390. } else {
  5391. base = I915_READ(DSPADDR(plane));
  5392. }
  5393. plane_config->base = base;
  5394. val = I915_READ(PIPESRC(pipe));
  5395. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  5396. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  5397. val = I915_READ(DSPSTRIDE(pipe));
  5398. crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
  5399. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  5400. plane_config->tiled);
  5401. plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
  5402. aligned_height);
  5403. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  5404. pipe, plane, crtc->base.primary->fb->width,
  5405. crtc->base.primary->fb->height,
  5406. crtc->base.primary->fb->bits_per_pixel, base,
  5407. crtc->base.primary->fb->pitches[0],
  5408. plane_config->size);
  5409. }
  5410. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  5411. struct intel_crtc_config *pipe_config)
  5412. {
  5413. struct drm_device *dev = crtc->base.dev;
  5414. struct drm_i915_private *dev_priv = dev->dev_private;
  5415. int pipe = pipe_config->cpu_transcoder;
  5416. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5417. intel_clock_t clock;
  5418. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  5419. int refclk = 100000;
  5420. mutex_lock(&dev_priv->dpio_lock);
  5421. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  5422. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  5423. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  5424. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  5425. mutex_unlock(&dev_priv->dpio_lock);
  5426. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  5427. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  5428. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  5429. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  5430. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  5431. chv_clock(refclk, &clock);
  5432. /* clock.dot is the fast clock */
  5433. pipe_config->port_clock = clock.dot / 5;
  5434. }
  5435. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  5436. struct intel_crtc_config *pipe_config)
  5437. {
  5438. struct drm_device *dev = crtc->base.dev;
  5439. struct drm_i915_private *dev_priv = dev->dev_private;
  5440. uint32_t tmp;
  5441. if (!intel_display_power_enabled(dev_priv,
  5442. POWER_DOMAIN_PIPE(crtc->pipe)))
  5443. return false;
  5444. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5445. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5446. tmp = I915_READ(PIPECONF(crtc->pipe));
  5447. if (!(tmp & PIPECONF_ENABLE))
  5448. return false;
  5449. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5450. switch (tmp & PIPECONF_BPC_MASK) {
  5451. case PIPECONF_6BPC:
  5452. pipe_config->pipe_bpp = 18;
  5453. break;
  5454. case PIPECONF_8BPC:
  5455. pipe_config->pipe_bpp = 24;
  5456. break;
  5457. case PIPECONF_10BPC:
  5458. pipe_config->pipe_bpp = 30;
  5459. break;
  5460. default:
  5461. break;
  5462. }
  5463. }
  5464. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  5465. pipe_config->limited_color_range = true;
  5466. if (INTEL_INFO(dev)->gen < 4)
  5467. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  5468. intel_get_pipe_timings(crtc, pipe_config);
  5469. i9xx_get_pfit_config(crtc, pipe_config);
  5470. if (INTEL_INFO(dev)->gen >= 4) {
  5471. tmp = I915_READ(DPLL_MD(crtc->pipe));
  5472. pipe_config->pixel_multiplier =
  5473. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  5474. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  5475. pipe_config->dpll_hw_state.dpll_md = tmp;
  5476. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5477. tmp = I915_READ(DPLL(crtc->pipe));
  5478. pipe_config->pixel_multiplier =
  5479. ((tmp & SDVO_MULTIPLIER_MASK)
  5480. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  5481. } else {
  5482. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  5483. * port and will be fixed up in the encoder->get_config
  5484. * function. */
  5485. pipe_config->pixel_multiplier = 1;
  5486. }
  5487. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  5488. if (!IS_VALLEYVIEW(dev)) {
  5489. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  5490. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  5491. } else {
  5492. /* Mask out read-only status bits. */
  5493. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  5494. DPLL_PORTC_READY_MASK |
  5495. DPLL_PORTB_READY_MASK);
  5496. }
  5497. if (IS_CHERRYVIEW(dev))
  5498. chv_crtc_clock_get(crtc, pipe_config);
  5499. else if (IS_VALLEYVIEW(dev))
  5500. vlv_crtc_clock_get(crtc, pipe_config);
  5501. else
  5502. i9xx_crtc_clock_get(crtc, pipe_config);
  5503. return true;
  5504. }
  5505. static void ironlake_init_pch_refclk(struct drm_device *dev)
  5506. {
  5507. struct drm_i915_private *dev_priv = dev->dev_private;
  5508. struct intel_encoder *encoder;
  5509. u32 val, final;
  5510. bool has_lvds = false;
  5511. bool has_cpu_edp = false;
  5512. bool has_panel = false;
  5513. bool has_ck505 = false;
  5514. bool can_ssc = false;
  5515. /* We need to take the global config into account */
  5516. for_each_intel_encoder(dev, encoder) {
  5517. switch (encoder->type) {
  5518. case INTEL_OUTPUT_LVDS:
  5519. has_panel = true;
  5520. has_lvds = true;
  5521. break;
  5522. case INTEL_OUTPUT_EDP:
  5523. has_panel = true;
  5524. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  5525. has_cpu_edp = true;
  5526. break;
  5527. }
  5528. }
  5529. if (HAS_PCH_IBX(dev)) {
  5530. has_ck505 = dev_priv->vbt.display_clock_mode;
  5531. can_ssc = has_ck505;
  5532. } else {
  5533. has_ck505 = false;
  5534. can_ssc = true;
  5535. }
  5536. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  5537. has_panel, has_lvds, has_ck505);
  5538. /* Ironlake: try to setup display ref clock before DPLL
  5539. * enabling. This is only under driver's control after
  5540. * PCH B stepping, previous chipset stepping should be
  5541. * ignoring this setting.
  5542. */
  5543. val = I915_READ(PCH_DREF_CONTROL);
  5544. /* As we must carefully and slowly disable/enable each source in turn,
  5545. * compute the final state we want first and check if we need to
  5546. * make any changes at all.
  5547. */
  5548. final = val;
  5549. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  5550. if (has_ck505)
  5551. final |= DREF_NONSPREAD_CK505_ENABLE;
  5552. else
  5553. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  5554. final &= ~DREF_SSC_SOURCE_MASK;
  5555. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5556. final &= ~DREF_SSC1_ENABLE;
  5557. if (has_panel) {
  5558. final |= DREF_SSC_SOURCE_ENABLE;
  5559. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5560. final |= DREF_SSC1_ENABLE;
  5561. if (has_cpu_edp) {
  5562. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5563. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5564. else
  5565. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5566. } else
  5567. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5568. } else {
  5569. final |= DREF_SSC_SOURCE_DISABLE;
  5570. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5571. }
  5572. if (final == val)
  5573. return;
  5574. /* Always enable nonspread source */
  5575. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  5576. if (has_ck505)
  5577. val |= DREF_NONSPREAD_CK505_ENABLE;
  5578. else
  5579. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  5580. if (has_panel) {
  5581. val &= ~DREF_SSC_SOURCE_MASK;
  5582. val |= DREF_SSC_SOURCE_ENABLE;
  5583. /* SSC must be turned on before enabling the CPU output */
  5584. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5585. DRM_DEBUG_KMS("Using SSC on panel\n");
  5586. val |= DREF_SSC1_ENABLE;
  5587. } else
  5588. val &= ~DREF_SSC1_ENABLE;
  5589. /* Get SSC going before enabling the outputs */
  5590. I915_WRITE(PCH_DREF_CONTROL, val);
  5591. POSTING_READ(PCH_DREF_CONTROL);
  5592. udelay(200);
  5593. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5594. /* Enable CPU source on CPU attached eDP */
  5595. if (has_cpu_edp) {
  5596. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5597. DRM_DEBUG_KMS("Using SSC on eDP\n");
  5598. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5599. } else
  5600. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5601. } else
  5602. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5603. I915_WRITE(PCH_DREF_CONTROL, val);
  5604. POSTING_READ(PCH_DREF_CONTROL);
  5605. udelay(200);
  5606. } else {
  5607. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  5608. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5609. /* Turn off CPU output */
  5610. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5611. I915_WRITE(PCH_DREF_CONTROL, val);
  5612. POSTING_READ(PCH_DREF_CONTROL);
  5613. udelay(200);
  5614. /* Turn off the SSC source */
  5615. val &= ~DREF_SSC_SOURCE_MASK;
  5616. val |= DREF_SSC_SOURCE_DISABLE;
  5617. /* Turn off SSC1 */
  5618. val &= ~DREF_SSC1_ENABLE;
  5619. I915_WRITE(PCH_DREF_CONTROL, val);
  5620. POSTING_READ(PCH_DREF_CONTROL);
  5621. udelay(200);
  5622. }
  5623. BUG_ON(val != final);
  5624. }
  5625. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  5626. {
  5627. uint32_t tmp;
  5628. tmp = I915_READ(SOUTH_CHICKEN2);
  5629. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  5630. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5631. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  5632. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  5633. DRM_ERROR("FDI mPHY reset assert timeout\n");
  5634. tmp = I915_READ(SOUTH_CHICKEN2);
  5635. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  5636. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5637. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  5638. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  5639. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  5640. }
  5641. /* WaMPhyProgramming:hsw */
  5642. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  5643. {
  5644. uint32_t tmp;
  5645. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  5646. tmp &= ~(0xFF << 24);
  5647. tmp |= (0x12 << 24);
  5648. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  5649. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  5650. tmp |= (1 << 11);
  5651. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  5652. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  5653. tmp |= (1 << 11);
  5654. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  5655. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  5656. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5657. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  5658. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  5659. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5660. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  5661. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  5662. tmp &= ~(7 << 13);
  5663. tmp |= (5 << 13);
  5664. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  5665. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  5666. tmp &= ~(7 << 13);
  5667. tmp |= (5 << 13);
  5668. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  5669. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  5670. tmp &= ~0xFF;
  5671. tmp |= 0x1C;
  5672. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  5673. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  5674. tmp &= ~0xFF;
  5675. tmp |= 0x1C;
  5676. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  5677. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  5678. tmp &= ~(0xFF << 16);
  5679. tmp |= (0x1C << 16);
  5680. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  5681. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  5682. tmp &= ~(0xFF << 16);
  5683. tmp |= (0x1C << 16);
  5684. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  5685. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  5686. tmp |= (1 << 27);
  5687. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  5688. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  5689. tmp |= (1 << 27);
  5690. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  5691. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  5692. tmp &= ~(0xF << 28);
  5693. tmp |= (4 << 28);
  5694. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  5695. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  5696. tmp &= ~(0xF << 28);
  5697. tmp |= (4 << 28);
  5698. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  5699. }
  5700. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  5701. * Programming" based on the parameters passed:
  5702. * - Sequence to enable CLKOUT_DP
  5703. * - Sequence to enable CLKOUT_DP without spread
  5704. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  5705. */
  5706. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  5707. bool with_fdi)
  5708. {
  5709. struct drm_i915_private *dev_priv = dev->dev_private;
  5710. uint32_t reg, tmp;
  5711. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  5712. with_spread = true;
  5713. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  5714. with_fdi, "LP PCH doesn't have FDI\n"))
  5715. with_fdi = false;
  5716. mutex_lock(&dev_priv->dpio_lock);
  5717. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5718. tmp &= ~SBI_SSCCTL_DISABLE;
  5719. tmp |= SBI_SSCCTL_PATHALT;
  5720. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5721. udelay(24);
  5722. if (with_spread) {
  5723. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5724. tmp &= ~SBI_SSCCTL_PATHALT;
  5725. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5726. if (with_fdi) {
  5727. lpt_reset_fdi_mphy(dev_priv);
  5728. lpt_program_fdi_mphy(dev_priv);
  5729. }
  5730. }
  5731. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5732. SBI_GEN0 : SBI_DBUFF0;
  5733. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5734. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5735. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5736. mutex_unlock(&dev_priv->dpio_lock);
  5737. }
  5738. /* Sequence to disable CLKOUT_DP */
  5739. static void lpt_disable_clkout_dp(struct drm_device *dev)
  5740. {
  5741. struct drm_i915_private *dev_priv = dev->dev_private;
  5742. uint32_t reg, tmp;
  5743. mutex_lock(&dev_priv->dpio_lock);
  5744. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5745. SBI_GEN0 : SBI_DBUFF0;
  5746. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5747. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5748. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5749. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5750. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  5751. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  5752. tmp |= SBI_SSCCTL_PATHALT;
  5753. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5754. udelay(32);
  5755. }
  5756. tmp |= SBI_SSCCTL_DISABLE;
  5757. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5758. }
  5759. mutex_unlock(&dev_priv->dpio_lock);
  5760. }
  5761. static void lpt_init_pch_refclk(struct drm_device *dev)
  5762. {
  5763. struct intel_encoder *encoder;
  5764. bool has_vga = false;
  5765. for_each_intel_encoder(dev, encoder) {
  5766. switch (encoder->type) {
  5767. case INTEL_OUTPUT_ANALOG:
  5768. has_vga = true;
  5769. break;
  5770. }
  5771. }
  5772. if (has_vga)
  5773. lpt_enable_clkout_dp(dev, true, true);
  5774. else
  5775. lpt_disable_clkout_dp(dev);
  5776. }
  5777. /*
  5778. * Initialize reference clocks when the driver loads
  5779. */
  5780. void intel_init_pch_refclk(struct drm_device *dev)
  5781. {
  5782. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  5783. ironlake_init_pch_refclk(dev);
  5784. else if (HAS_PCH_LPT(dev))
  5785. lpt_init_pch_refclk(dev);
  5786. }
  5787. static int ironlake_get_refclk(struct drm_crtc *crtc)
  5788. {
  5789. struct drm_device *dev = crtc->dev;
  5790. struct drm_i915_private *dev_priv = dev->dev_private;
  5791. struct intel_encoder *encoder;
  5792. int num_connectors = 0;
  5793. bool is_lvds = false;
  5794. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5795. switch (encoder->type) {
  5796. case INTEL_OUTPUT_LVDS:
  5797. is_lvds = true;
  5798. break;
  5799. }
  5800. num_connectors++;
  5801. }
  5802. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5803. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  5804. dev_priv->vbt.lvds_ssc_freq);
  5805. return dev_priv->vbt.lvds_ssc_freq;
  5806. }
  5807. return 120000;
  5808. }
  5809. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  5810. {
  5811. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  5812. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5813. int pipe = intel_crtc->pipe;
  5814. uint32_t val;
  5815. val = 0;
  5816. switch (intel_crtc->config.pipe_bpp) {
  5817. case 18:
  5818. val |= PIPECONF_6BPC;
  5819. break;
  5820. case 24:
  5821. val |= PIPECONF_8BPC;
  5822. break;
  5823. case 30:
  5824. val |= PIPECONF_10BPC;
  5825. break;
  5826. case 36:
  5827. val |= PIPECONF_12BPC;
  5828. break;
  5829. default:
  5830. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5831. BUG();
  5832. }
  5833. if (intel_crtc->config.dither)
  5834. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  5835. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  5836. val |= PIPECONF_INTERLACED_ILK;
  5837. else
  5838. val |= PIPECONF_PROGRESSIVE;
  5839. if (intel_crtc->config.limited_color_range)
  5840. val |= PIPECONF_COLOR_RANGE_SELECT;
  5841. I915_WRITE(PIPECONF(pipe), val);
  5842. POSTING_READ(PIPECONF(pipe));
  5843. }
  5844. /*
  5845. * Set up the pipe CSC unit.
  5846. *
  5847. * Currently only full range RGB to limited range RGB conversion
  5848. * is supported, but eventually this should handle various
  5849. * RGB<->YCbCr scenarios as well.
  5850. */
  5851. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  5852. {
  5853. struct drm_device *dev = crtc->dev;
  5854. struct drm_i915_private *dev_priv = dev->dev_private;
  5855. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5856. int pipe = intel_crtc->pipe;
  5857. uint16_t coeff = 0x7800; /* 1.0 */
  5858. /*
  5859. * TODO: Check what kind of values actually come out of the pipe
  5860. * with these coeff/postoff values and adjust to get the best
  5861. * accuracy. Perhaps we even need to take the bpc value into
  5862. * consideration.
  5863. */
  5864. if (intel_crtc->config.limited_color_range)
  5865. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  5866. /*
  5867. * GY/GU and RY/RU should be the other way around according
  5868. * to BSpec, but reality doesn't agree. Just set them up in
  5869. * a way that results in the correct picture.
  5870. */
  5871. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  5872. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  5873. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  5874. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  5875. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  5876. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  5877. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  5878. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  5879. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  5880. if (INTEL_INFO(dev)->gen > 6) {
  5881. uint16_t postoff = 0;
  5882. if (intel_crtc->config.limited_color_range)
  5883. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  5884. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  5885. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  5886. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  5887. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  5888. } else {
  5889. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  5890. if (intel_crtc->config.limited_color_range)
  5891. mode |= CSC_BLACK_SCREEN_OFFSET;
  5892. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  5893. }
  5894. }
  5895. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  5896. {
  5897. struct drm_device *dev = crtc->dev;
  5898. struct drm_i915_private *dev_priv = dev->dev_private;
  5899. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5900. enum pipe pipe = intel_crtc->pipe;
  5901. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  5902. uint32_t val;
  5903. val = 0;
  5904. if (IS_HASWELL(dev) && intel_crtc->config.dither)
  5905. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  5906. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  5907. val |= PIPECONF_INTERLACED_ILK;
  5908. else
  5909. val |= PIPECONF_PROGRESSIVE;
  5910. I915_WRITE(PIPECONF(cpu_transcoder), val);
  5911. POSTING_READ(PIPECONF(cpu_transcoder));
  5912. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  5913. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  5914. if (IS_BROADWELL(dev)) {
  5915. val = 0;
  5916. switch (intel_crtc->config.pipe_bpp) {
  5917. case 18:
  5918. val |= PIPEMISC_DITHER_6_BPC;
  5919. break;
  5920. case 24:
  5921. val |= PIPEMISC_DITHER_8_BPC;
  5922. break;
  5923. case 30:
  5924. val |= PIPEMISC_DITHER_10_BPC;
  5925. break;
  5926. case 36:
  5927. val |= PIPEMISC_DITHER_12_BPC;
  5928. break;
  5929. default:
  5930. /* Case prevented by pipe_config_set_bpp. */
  5931. BUG();
  5932. }
  5933. if (intel_crtc->config.dither)
  5934. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  5935. I915_WRITE(PIPEMISC(pipe), val);
  5936. }
  5937. }
  5938. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  5939. intel_clock_t *clock,
  5940. bool *has_reduced_clock,
  5941. intel_clock_t *reduced_clock)
  5942. {
  5943. struct drm_device *dev = crtc->dev;
  5944. struct drm_i915_private *dev_priv = dev->dev_private;
  5945. struct intel_encoder *intel_encoder;
  5946. int refclk;
  5947. const intel_limit_t *limit;
  5948. bool ret, is_lvds = false;
  5949. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  5950. switch (intel_encoder->type) {
  5951. case INTEL_OUTPUT_LVDS:
  5952. is_lvds = true;
  5953. break;
  5954. }
  5955. }
  5956. refclk = ironlake_get_refclk(crtc);
  5957. /*
  5958. * Returns a set of divisors for the desired target clock with the given
  5959. * refclk, or FALSE. The returned values represent the clock equation:
  5960. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  5961. */
  5962. limit = intel_limit(crtc, refclk);
  5963. ret = dev_priv->display.find_dpll(limit, crtc,
  5964. to_intel_crtc(crtc)->config.port_clock,
  5965. refclk, NULL, clock);
  5966. if (!ret)
  5967. return false;
  5968. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5969. /*
  5970. * Ensure we match the reduced clock's P to the target clock.
  5971. * If the clocks don't match, we can't switch the display clock
  5972. * by using the FP0/FP1. In such case we will disable the LVDS
  5973. * downclock feature.
  5974. */
  5975. *has_reduced_clock =
  5976. dev_priv->display.find_dpll(limit, crtc,
  5977. dev_priv->lvds_downclock,
  5978. refclk, clock,
  5979. reduced_clock);
  5980. }
  5981. return true;
  5982. }
  5983. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  5984. {
  5985. /*
  5986. * Account for spread spectrum to avoid
  5987. * oversubscribing the link. Max center spread
  5988. * is 2.5%; use 5% for safety's sake.
  5989. */
  5990. u32 bps = target_clock * bpp * 21 / 20;
  5991. return DIV_ROUND_UP(bps, link_bw * 8);
  5992. }
  5993. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  5994. {
  5995. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  5996. }
  5997. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  5998. u32 *fp,
  5999. intel_clock_t *reduced_clock, u32 *fp2)
  6000. {
  6001. struct drm_crtc *crtc = &intel_crtc->base;
  6002. struct drm_device *dev = crtc->dev;
  6003. struct drm_i915_private *dev_priv = dev->dev_private;
  6004. struct intel_encoder *intel_encoder;
  6005. uint32_t dpll;
  6006. int factor, num_connectors = 0;
  6007. bool is_lvds = false, is_sdvo = false;
  6008. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  6009. switch (intel_encoder->type) {
  6010. case INTEL_OUTPUT_LVDS:
  6011. is_lvds = true;
  6012. break;
  6013. case INTEL_OUTPUT_SDVO:
  6014. case INTEL_OUTPUT_HDMI:
  6015. is_sdvo = true;
  6016. break;
  6017. }
  6018. num_connectors++;
  6019. }
  6020. /* Enable autotuning of the PLL clock (if permissible) */
  6021. factor = 21;
  6022. if (is_lvds) {
  6023. if ((intel_panel_use_ssc(dev_priv) &&
  6024. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6025. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  6026. factor = 25;
  6027. } else if (intel_crtc->config.sdvo_tv_clock)
  6028. factor = 20;
  6029. if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
  6030. *fp |= FP_CB_TUNE;
  6031. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  6032. *fp2 |= FP_CB_TUNE;
  6033. dpll = 0;
  6034. if (is_lvds)
  6035. dpll |= DPLLB_MODE_LVDS;
  6036. else
  6037. dpll |= DPLLB_MODE_DAC_SERIAL;
  6038. dpll |= (intel_crtc->config.pixel_multiplier - 1)
  6039. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6040. if (is_sdvo)
  6041. dpll |= DPLL_SDVO_HIGH_SPEED;
  6042. if (intel_crtc->config.has_dp_encoder)
  6043. dpll |= DPLL_SDVO_HIGH_SPEED;
  6044. /* compute bitmask from p1 value */
  6045. dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6046. /* also FPA1 */
  6047. dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6048. switch (intel_crtc->config.dpll.p2) {
  6049. case 5:
  6050. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6051. break;
  6052. case 7:
  6053. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6054. break;
  6055. case 10:
  6056. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6057. break;
  6058. case 14:
  6059. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6060. break;
  6061. }
  6062. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6063. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6064. else
  6065. dpll |= PLL_REF_INPUT_DREFCLK;
  6066. return dpll | DPLL_VCO_ENABLE;
  6067. }
  6068. static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
  6069. int x, int y,
  6070. struct drm_framebuffer *fb)
  6071. {
  6072. struct drm_device *dev = crtc->dev;
  6073. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6074. int num_connectors = 0;
  6075. intel_clock_t clock, reduced_clock;
  6076. u32 dpll = 0, fp = 0, fp2 = 0;
  6077. bool ok, has_reduced_clock = false;
  6078. bool is_lvds = false;
  6079. struct intel_encoder *encoder;
  6080. struct intel_shared_dpll *pll;
  6081. for_each_encoder_on_crtc(dev, crtc, encoder) {
  6082. switch (encoder->type) {
  6083. case INTEL_OUTPUT_LVDS:
  6084. is_lvds = true;
  6085. break;
  6086. }
  6087. num_connectors++;
  6088. }
  6089. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  6090. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  6091. ok = ironlake_compute_clocks(crtc, &clock,
  6092. &has_reduced_clock, &reduced_clock);
  6093. if (!ok && !intel_crtc->config.clock_set) {
  6094. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6095. return -EINVAL;
  6096. }
  6097. /* Compat-code for transition, will disappear. */
  6098. if (!intel_crtc->config.clock_set) {
  6099. intel_crtc->config.dpll.n = clock.n;
  6100. intel_crtc->config.dpll.m1 = clock.m1;
  6101. intel_crtc->config.dpll.m2 = clock.m2;
  6102. intel_crtc->config.dpll.p1 = clock.p1;
  6103. intel_crtc->config.dpll.p2 = clock.p2;
  6104. }
  6105. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  6106. if (intel_crtc->config.has_pch_encoder) {
  6107. fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
  6108. if (has_reduced_clock)
  6109. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  6110. dpll = ironlake_compute_dpll(intel_crtc,
  6111. &fp, &reduced_clock,
  6112. has_reduced_clock ? &fp2 : NULL);
  6113. intel_crtc->config.dpll_hw_state.dpll = dpll;
  6114. intel_crtc->config.dpll_hw_state.fp0 = fp;
  6115. if (has_reduced_clock)
  6116. intel_crtc->config.dpll_hw_state.fp1 = fp2;
  6117. else
  6118. intel_crtc->config.dpll_hw_state.fp1 = fp;
  6119. pll = intel_get_shared_dpll(intel_crtc);
  6120. if (pll == NULL) {
  6121. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  6122. pipe_name(intel_crtc->pipe));
  6123. return -EINVAL;
  6124. }
  6125. } else
  6126. intel_put_shared_dpll(intel_crtc);
  6127. if (is_lvds && has_reduced_clock && i915.powersave)
  6128. intel_crtc->lowfreq_avail = true;
  6129. else
  6130. intel_crtc->lowfreq_avail = false;
  6131. return 0;
  6132. }
  6133. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  6134. struct intel_link_m_n *m_n)
  6135. {
  6136. struct drm_device *dev = crtc->base.dev;
  6137. struct drm_i915_private *dev_priv = dev->dev_private;
  6138. enum pipe pipe = crtc->pipe;
  6139. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  6140. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  6141. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  6142. & ~TU_SIZE_MASK;
  6143. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  6144. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  6145. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6146. }
  6147. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  6148. enum transcoder transcoder,
  6149. struct intel_link_m_n *m_n,
  6150. struct intel_link_m_n *m2_n2)
  6151. {
  6152. struct drm_device *dev = crtc->base.dev;
  6153. struct drm_i915_private *dev_priv = dev->dev_private;
  6154. enum pipe pipe = crtc->pipe;
  6155. if (INTEL_INFO(dev)->gen >= 5) {
  6156. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  6157. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  6158. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  6159. & ~TU_SIZE_MASK;
  6160. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  6161. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  6162. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6163. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  6164. * gen < 8) and if DRRS is supported (to make sure the
  6165. * registers are not unnecessarily read).
  6166. */
  6167. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  6168. crtc->config.has_drrs) {
  6169. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  6170. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  6171. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  6172. & ~TU_SIZE_MASK;
  6173. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  6174. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  6175. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6176. }
  6177. } else {
  6178. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  6179. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  6180. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  6181. & ~TU_SIZE_MASK;
  6182. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  6183. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  6184. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6185. }
  6186. }
  6187. void intel_dp_get_m_n(struct intel_crtc *crtc,
  6188. struct intel_crtc_config *pipe_config)
  6189. {
  6190. if (crtc->config.has_pch_encoder)
  6191. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  6192. else
  6193. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6194. &pipe_config->dp_m_n,
  6195. &pipe_config->dp_m2_n2);
  6196. }
  6197. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  6198. struct intel_crtc_config *pipe_config)
  6199. {
  6200. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6201. &pipe_config->fdi_m_n, NULL);
  6202. }
  6203. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  6204. struct intel_crtc_config *pipe_config)
  6205. {
  6206. struct drm_device *dev = crtc->base.dev;
  6207. struct drm_i915_private *dev_priv = dev->dev_private;
  6208. uint32_t tmp;
  6209. tmp = I915_READ(PF_CTL(crtc->pipe));
  6210. if (tmp & PF_ENABLE) {
  6211. pipe_config->pch_pfit.enabled = true;
  6212. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  6213. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  6214. /* We currently do not free assignements of panel fitters on
  6215. * ivb/hsw (since we don't use the higher upscaling modes which
  6216. * differentiates them) so just WARN about this case for now. */
  6217. if (IS_GEN7(dev)) {
  6218. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  6219. PF_PIPE_SEL_IVB(crtc->pipe));
  6220. }
  6221. }
  6222. }
  6223. static void ironlake_get_plane_config(struct intel_crtc *crtc,
  6224. struct intel_plane_config *plane_config)
  6225. {
  6226. struct drm_device *dev = crtc->base.dev;
  6227. struct drm_i915_private *dev_priv = dev->dev_private;
  6228. u32 val, base, offset;
  6229. int pipe = crtc->pipe, plane = crtc->plane;
  6230. int fourcc, pixel_format;
  6231. int aligned_height;
  6232. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  6233. if (!crtc->base.primary->fb) {
  6234. DRM_DEBUG_KMS("failed to alloc fb\n");
  6235. return;
  6236. }
  6237. val = I915_READ(DSPCNTR(plane));
  6238. if (INTEL_INFO(dev)->gen >= 4)
  6239. if (val & DISPPLANE_TILED)
  6240. plane_config->tiled = true;
  6241. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6242. fourcc = intel_format_to_fourcc(pixel_format);
  6243. crtc->base.primary->fb->pixel_format = fourcc;
  6244. crtc->base.primary->fb->bits_per_pixel =
  6245. drm_format_plane_cpp(fourcc, 0) * 8;
  6246. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6247. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  6248. offset = I915_READ(DSPOFFSET(plane));
  6249. } else {
  6250. if (plane_config->tiled)
  6251. offset = I915_READ(DSPTILEOFF(plane));
  6252. else
  6253. offset = I915_READ(DSPLINOFF(plane));
  6254. }
  6255. plane_config->base = base;
  6256. val = I915_READ(PIPESRC(pipe));
  6257. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  6258. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  6259. val = I915_READ(DSPSTRIDE(pipe));
  6260. crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
  6261. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  6262. plane_config->tiled);
  6263. plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
  6264. aligned_height);
  6265. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6266. pipe, plane, crtc->base.primary->fb->width,
  6267. crtc->base.primary->fb->height,
  6268. crtc->base.primary->fb->bits_per_pixel, base,
  6269. crtc->base.primary->fb->pitches[0],
  6270. plane_config->size);
  6271. }
  6272. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  6273. struct intel_crtc_config *pipe_config)
  6274. {
  6275. struct drm_device *dev = crtc->base.dev;
  6276. struct drm_i915_private *dev_priv = dev->dev_private;
  6277. uint32_t tmp;
  6278. if (!intel_display_power_enabled(dev_priv,
  6279. POWER_DOMAIN_PIPE(crtc->pipe)))
  6280. return false;
  6281. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6282. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6283. tmp = I915_READ(PIPECONF(crtc->pipe));
  6284. if (!(tmp & PIPECONF_ENABLE))
  6285. return false;
  6286. switch (tmp & PIPECONF_BPC_MASK) {
  6287. case PIPECONF_6BPC:
  6288. pipe_config->pipe_bpp = 18;
  6289. break;
  6290. case PIPECONF_8BPC:
  6291. pipe_config->pipe_bpp = 24;
  6292. break;
  6293. case PIPECONF_10BPC:
  6294. pipe_config->pipe_bpp = 30;
  6295. break;
  6296. case PIPECONF_12BPC:
  6297. pipe_config->pipe_bpp = 36;
  6298. break;
  6299. default:
  6300. break;
  6301. }
  6302. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  6303. pipe_config->limited_color_range = true;
  6304. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  6305. struct intel_shared_dpll *pll;
  6306. pipe_config->has_pch_encoder = true;
  6307. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  6308. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6309. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6310. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6311. if (HAS_PCH_IBX(dev_priv->dev)) {
  6312. pipe_config->shared_dpll =
  6313. (enum intel_dpll_id) crtc->pipe;
  6314. } else {
  6315. tmp = I915_READ(PCH_DPLL_SEL);
  6316. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  6317. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  6318. else
  6319. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  6320. }
  6321. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6322. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6323. &pipe_config->dpll_hw_state));
  6324. tmp = pipe_config->dpll_hw_state.dpll;
  6325. pipe_config->pixel_multiplier =
  6326. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  6327. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  6328. ironlake_pch_clock_get(crtc, pipe_config);
  6329. } else {
  6330. pipe_config->pixel_multiplier = 1;
  6331. }
  6332. intel_get_pipe_timings(crtc, pipe_config);
  6333. ironlake_get_pfit_config(crtc, pipe_config);
  6334. return true;
  6335. }
  6336. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  6337. {
  6338. struct drm_device *dev = dev_priv->dev;
  6339. struct intel_crtc *crtc;
  6340. for_each_intel_crtc(dev, crtc)
  6341. WARN(crtc->active, "CRTC for pipe %c enabled\n",
  6342. pipe_name(crtc->pipe));
  6343. WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  6344. WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  6345. WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  6346. WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  6347. WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  6348. WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  6349. "CPU PWM1 enabled\n");
  6350. if (IS_HASWELL(dev))
  6351. WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  6352. "CPU PWM2 enabled\n");
  6353. WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  6354. "PCH PWM1 enabled\n");
  6355. WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  6356. "Utility pin enabled\n");
  6357. WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  6358. /*
  6359. * In theory we can still leave IRQs enabled, as long as only the HPD
  6360. * interrupts remain enabled. We used to check for that, but since it's
  6361. * gen-specific and since we only disable LCPLL after we fully disable
  6362. * the interrupts, the check below should be enough.
  6363. */
  6364. WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  6365. }
  6366. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  6367. {
  6368. struct drm_device *dev = dev_priv->dev;
  6369. if (IS_HASWELL(dev))
  6370. return I915_READ(D_COMP_HSW);
  6371. else
  6372. return I915_READ(D_COMP_BDW);
  6373. }
  6374. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  6375. {
  6376. struct drm_device *dev = dev_priv->dev;
  6377. if (IS_HASWELL(dev)) {
  6378. mutex_lock(&dev_priv->rps.hw_lock);
  6379. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  6380. val))
  6381. DRM_ERROR("Failed to write to D_COMP\n");
  6382. mutex_unlock(&dev_priv->rps.hw_lock);
  6383. } else {
  6384. I915_WRITE(D_COMP_BDW, val);
  6385. POSTING_READ(D_COMP_BDW);
  6386. }
  6387. }
  6388. /*
  6389. * This function implements pieces of two sequences from BSpec:
  6390. * - Sequence for display software to disable LCPLL
  6391. * - Sequence for display software to allow package C8+
  6392. * The steps implemented here are just the steps that actually touch the LCPLL
  6393. * register. Callers should take care of disabling all the display engine
  6394. * functions, doing the mode unset, fixing interrupts, etc.
  6395. */
  6396. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  6397. bool switch_to_fclk, bool allow_power_down)
  6398. {
  6399. uint32_t val;
  6400. assert_can_disable_lcpll(dev_priv);
  6401. val = I915_READ(LCPLL_CTL);
  6402. if (switch_to_fclk) {
  6403. val |= LCPLL_CD_SOURCE_FCLK;
  6404. I915_WRITE(LCPLL_CTL, val);
  6405. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  6406. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  6407. DRM_ERROR("Switching to FCLK failed\n");
  6408. val = I915_READ(LCPLL_CTL);
  6409. }
  6410. val |= LCPLL_PLL_DISABLE;
  6411. I915_WRITE(LCPLL_CTL, val);
  6412. POSTING_READ(LCPLL_CTL);
  6413. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  6414. DRM_ERROR("LCPLL still locked\n");
  6415. val = hsw_read_dcomp(dev_priv);
  6416. val |= D_COMP_COMP_DISABLE;
  6417. hsw_write_dcomp(dev_priv, val);
  6418. ndelay(100);
  6419. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  6420. 1))
  6421. DRM_ERROR("D_COMP RCOMP still in progress\n");
  6422. if (allow_power_down) {
  6423. val = I915_READ(LCPLL_CTL);
  6424. val |= LCPLL_POWER_DOWN_ALLOW;
  6425. I915_WRITE(LCPLL_CTL, val);
  6426. POSTING_READ(LCPLL_CTL);
  6427. }
  6428. }
  6429. /*
  6430. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  6431. * source.
  6432. */
  6433. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  6434. {
  6435. uint32_t val;
  6436. unsigned long irqflags;
  6437. val = I915_READ(LCPLL_CTL);
  6438. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  6439. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  6440. return;
  6441. /*
  6442. * Make sure we're not on PC8 state before disabling PC8, otherwise
  6443. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  6444. *
  6445. * The other problem is that hsw_restore_lcpll() is called as part of
  6446. * the runtime PM resume sequence, so we can't just call
  6447. * gen6_gt_force_wake_get() because that function calls
  6448. * intel_runtime_pm_get(), and we can't change the runtime PM refcount
  6449. * while we are on the resume sequence. So to solve this problem we have
  6450. * to call special forcewake code that doesn't touch runtime PM and
  6451. * doesn't enable the forcewake delayed work.
  6452. */
  6453. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  6454. if (dev_priv->uncore.forcewake_count++ == 0)
  6455. dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
  6456. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  6457. if (val & LCPLL_POWER_DOWN_ALLOW) {
  6458. val &= ~LCPLL_POWER_DOWN_ALLOW;
  6459. I915_WRITE(LCPLL_CTL, val);
  6460. POSTING_READ(LCPLL_CTL);
  6461. }
  6462. val = hsw_read_dcomp(dev_priv);
  6463. val |= D_COMP_COMP_FORCE;
  6464. val &= ~D_COMP_COMP_DISABLE;
  6465. hsw_write_dcomp(dev_priv, val);
  6466. val = I915_READ(LCPLL_CTL);
  6467. val &= ~LCPLL_PLL_DISABLE;
  6468. I915_WRITE(LCPLL_CTL, val);
  6469. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  6470. DRM_ERROR("LCPLL not locked yet\n");
  6471. if (val & LCPLL_CD_SOURCE_FCLK) {
  6472. val = I915_READ(LCPLL_CTL);
  6473. val &= ~LCPLL_CD_SOURCE_FCLK;
  6474. I915_WRITE(LCPLL_CTL, val);
  6475. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  6476. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  6477. DRM_ERROR("Switching back to LCPLL failed\n");
  6478. }
  6479. /* See the big comment above. */
  6480. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  6481. if (--dev_priv->uncore.forcewake_count == 0)
  6482. dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
  6483. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  6484. }
  6485. /*
  6486. * Package states C8 and deeper are really deep PC states that can only be
  6487. * reached when all the devices on the system allow it, so even if the graphics
  6488. * device allows PC8+, it doesn't mean the system will actually get to these
  6489. * states. Our driver only allows PC8+ when going into runtime PM.
  6490. *
  6491. * The requirements for PC8+ are that all the outputs are disabled, the power
  6492. * well is disabled and most interrupts are disabled, and these are also
  6493. * requirements for runtime PM. When these conditions are met, we manually do
  6494. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  6495. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  6496. * hang the machine.
  6497. *
  6498. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  6499. * the state of some registers, so when we come back from PC8+ we need to
  6500. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  6501. * need to take care of the registers kept by RC6. Notice that this happens even
  6502. * if we don't put the device in PCI D3 state (which is what currently happens
  6503. * because of the runtime PM support).
  6504. *
  6505. * For more, read "Display Sequences for Package C8" on the hardware
  6506. * documentation.
  6507. */
  6508. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  6509. {
  6510. struct drm_device *dev = dev_priv->dev;
  6511. uint32_t val;
  6512. DRM_DEBUG_KMS("Enabling package C8+\n");
  6513. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6514. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6515. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  6516. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6517. }
  6518. lpt_disable_clkout_dp(dev);
  6519. hsw_disable_lcpll(dev_priv, true, true);
  6520. }
  6521. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  6522. {
  6523. struct drm_device *dev = dev_priv->dev;
  6524. uint32_t val;
  6525. DRM_DEBUG_KMS("Disabling package C8+\n");
  6526. hsw_restore_lcpll(dev_priv);
  6527. lpt_init_pch_refclk(dev);
  6528. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6529. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6530. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  6531. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6532. }
  6533. intel_prepare_ddi(dev);
  6534. }
  6535. static void snb_modeset_global_resources(struct drm_device *dev)
  6536. {
  6537. modeset_update_crtc_power_domains(dev);
  6538. }
  6539. static void haswell_modeset_global_resources(struct drm_device *dev)
  6540. {
  6541. modeset_update_crtc_power_domains(dev);
  6542. }
  6543. static int haswell_crtc_mode_set(struct drm_crtc *crtc,
  6544. int x, int y,
  6545. struct drm_framebuffer *fb)
  6546. {
  6547. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6548. if (!intel_ddi_pll_select(intel_crtc))
  6549. return -EINVAL;
  6550. intel_crtc->lowfreq_avail = false;
  6551. return 0;
  6552. }
  6553. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  6554. enum port port,
  6555. struct intel_crtc_config *pipe_config)
  6556. {
  6557. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  6558. switch (pipe_config->ddi_pll_sel) {
  6559. case PORT_CLK_SEL_WRPLL1:
  6560. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  6561. break;
  6562. case PORT_CLK_SEL_WRPLL2:
  6563. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  6564. break;
  6565. }
  6566. }
  6567. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  6568. struct intel_crtc_config *pipe_config)
  6569. {
  6570. struct drm_device *dev = crtc->base.dev;
  6571. struct drm_i915_private *dev_priv = dev->dev_private;
  6572. struct intel_shared_dpll *pll;
  6573. enum port port;
  6574. uint32_t tmp;
  6575. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  6576. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  6577. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  6578. if (pipe_config->shared_dpll >= 0) {
  6579. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6580. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6581. &pipe_config->dpll_hw_state));
  6582. }
  6583. /*
  6584. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  6585. * DDI E. So just check whether this pipe is wired to DDI E and whether
  6586. * the PCH transcoder is on.
  6587. */
  6588. if ((port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  6589. pipe_config->has_pch_encoder = true;
  6590. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  6591. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6592. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6593. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6594. }
  6595. }
  6596. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  6597. struct intel_crtc_config *pipe_config)
  6598. {
  6599. struct drm_device *dev = crtc->base.dev;
  6600. struct drm_i915_private *dev_priv = dev->dev_private;
  6601. enum intel_display_power_domain pfit_domain;
  6602. uint32_t tmp;
  6603. if (!intel_display_power_enabled(dev_priv,
  6604. POWER_DOMAIN_PIPE(crtc->pipe)))
  6605. return false;
  6606. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6607. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6608. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  6609. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  6610. enum pipe trans_edp_pipe;
  6611. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  6612. default:
  6613. WARN(1, "unknown pipe linked to edp transcoder\n");
  6614. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  6615. case TRANS_DDI_EDP_INPUT_A_ON:
  6616. trans_edp_pipe = PIPE_A;
  6617. break;
  6618. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  6619. trans_edp_pipe = PIPE_B;
  6620. break;
  6621. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  6622. trans_edp_pipe = PIPE_C;
  6623. break;
  6624. }
  6625. if (trans_edp_pipe == crtc->pipe)
  6626. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  6627. }
  6628. if (!intel_display_power_enabled(dev_priv,
  6629. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  6630. return false;
  6631. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  6632. if (!(tmp & PIPECONF_ENABLE))
  6633. return false;
  6634. haswell_get_ddi_port_state(crtc, pipe_config);
  6635. intel_get_pipe_timings(crtc, pipe_config);
  6636. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  6637. if (intel_display_power_enabled(dev_priv, pfit_domain))
  6638. ironlake_get_pfit_config(crtc, pipe_config);
  6639. if (IS_HASWELL(dev))
  6640. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  6641. (I915_READ(IPS_CTL) & IPS_ENABLE);
  6642. pipe_config->pixel_multiplier = 1;
  6643. return true;
  6644. }
  6645. static struct {
  6646. int clock;
  6647. u32 config;
  6648. } hdmi_audio_clock[] = {
  6649. { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
  6650. { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
  6651. { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
  6652. { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
  6653. { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
  6654. { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
  6655. { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
  6656. { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
  6657. { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
  6658. { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
  6659. };
  6660. /* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
  6661. static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
  6662. {
  6663. int i;
  6664. for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
  6665. if (mode->clock == hdmi_audio_clock[i].clock)
  6666. break;
  6667. }
  6668. if (i == ARRAY_SIZE(hdmi_audio_clock)) {
  6669. DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
  6670. i = 1;
  6671. }
  6672. DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
  6673. hdmi_audio_clock[i].clock,
  6674. hdmi_audio_clock[i].config);
  6675. return hdmi_audio_clock[i].config;
  6676. }
  6677. static bool intel_eld_uptodate(struct drm_connector *connector,
  6678. int reg_eldv, uint32_t bits_eldv,
  6679. int reg_elda, uint32_t bits_elda,
  6680. int reg_edid)
  6681. {
  6682. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6683. uint8_t *eld = connector->eld;
  6684. uint32_t i;
  6685. i = I915_READ(reg_eldv);
  6686. i &= bits_eldv;
  6687. if (!eld[0])
  6688. return !i;
  6689. if (!i)
  6690. return false;
  6691. i = I915_READ(reg_elda);
  6692. i &= ~bits_elda;
  6693. I915_WRITE(reg_elda, i);
  6694. for (i = 0; i < eld[2]; i++)
  6695. if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
  6696. return false;
  6697. return true;
  6698. }
  6699. static void g4x_write_eld(struct drm_connector *connector,
  6700. struct drm_crtc *crtc,
  6701. struct drm_display_mode *mode)
  6702. {
  6703. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6704. uint8_t *eld = connector->eld;
  6705. uint32_t eldv;
  6706. uint32_t len;
  6707. uint32_t i;
  6708. i = I915_READ(G4X_AUD_VID_DID);
  6709. if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
  6710. eldv = G4X_ELDV_DEVCL_DEVBLC;
  6711. else
  6712. eldv = G4X_ELDV_DEVCTG;
  6713. if (intel_eld_uptodate(connector,
  6714. G4X_AUD_CNTL_ST, eldv,
  6715. G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
  6716. G4X_HDMIW_HDMIEDID))
  6717. return;
  6718. i = I915_READ(G4X_AUD_CNTL_ST);
  6719. i &= ~(eldv | G4X_ELD_ADDR);
  6720. len = (i >> 9) & 0x1f; /* ELD buffer size */
  6721. I915_WRITE(G4X_AUD_CNTL_ST, i);
  6722. if (!eld[0])
  6723. return;
  6724. len = min_t(uint8_t, eld[2], len);
  6725. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6726. for (i = 0; i < len; i++)
  6727. I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
  6728. i = I915_READ(G4X_AUD_CNTL_ST);
  6729. i |= eldv;
  6730. I915_WRITE(G4X_AUD_CNTL_ST, i);
  6731. }
  6732. static void haswell_write_eld(struct drm_connector *connector,
  6733. struct drm_crtc *crtc,
  6734. struct drm_display_mode *mode)
  6735. {
  6736. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6737. uint8_t *eld = connector->eld;
  6738. uint32_t eldv;
  6739. uint32_t i;
  6740. int len;
  6741. int pipe = to_intel_crtc(crtc)->pipe;
  6742. int tmp;
  6743. int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
  6744. int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
  6745. int aud_config = HSW_AUD_CFG(pipe);
  6746. int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
  6747. /* Audio output enable */
  6748. DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
  6749. tmp = I915_READ(aud_cntrl_st2);
  6750. tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
  6751. I915_WRITE(aud_cntrl_st2, tmp);
  6752. POSTING_READ(aud_cntrl_st2);
  6753. assert_pipe_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
  6754. /* Set ELD valid state */
  6755. tmp = I915_READ(aud_cntrl_st2);
  6756. DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
  6757. tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
  6758. I915_WRITE(aud_cntrl_st2, tmp);
  6759. tmp = I915_READ(aud_cntrl_st2);
  6760. DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
  6761. /* Enable HDMI mode */
  6762. tmp = I915_READ(aud_config);
  6763. DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
  6764. /* clear N_programing_enable and N_value_index */
  6765. tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
  6766. I915_WRITE(aud_config, tmp);
  6767. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  6768. eldv = AUDIO_ELD_VALID_A << (pipe * 4);
  6769. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  6770. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  6771. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  6772. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  6773. } else {
  6774. I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
  6775. }
  6776. if (intel_eld_uptodate(connector,
  6777. aud_cntrl_st2, eldv,
  6778. aud_cntl_st, IBX_ELD_ADDRESS,
  6779. hdmiw_hdmiedid))
  6780. return;
  6781. i = I915_READ(aud_cntrl_st2);
  6782. i &= ~eldv;
  6783. I915_WRITE(aud_cntrl_st2, i);
  6784. if (!eld[0])
  6785. return;
  6786. i = I915_READ(aud_cntl_st);
  6787. i &= ~IBX_ELD_ADDRESS;
  6788. I915_WRITE(aud_cntl_st, i);
  6789. i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
  6790. DRM_DEBUG_DRIVER("port num:%d\n", i);
  6791. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  6792. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6793. for (i = 0; i < len; i++)
  6794. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  6795. i = I915_READ(aud_cntrl_st2);
  6796. i |= eldv;
  6797. I915_WRITE(aud_cntrl_st2, i);
  6798. }
  6799. static void ironlake_write_eld(struct drm_connector *connector,
  6800. struct drm_crtc *crtc,
  6801. struct drm_display_mode *mode)
  6802. {
  6803. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6804. uint8_t *eld = connector->eld;
  6805. uint32_t eldv;
  6806. uint32_t i;
  6807. int len;
  6808. int hdmiw_hdmiedid;
  6809. int aud_config;
  6810. int aud_cntl_st;
  6811. int aud_cntrl_st2;
  6812. int pipe = to_intel_crtc(crtc)->pipe;
  6813. if (HAS_PCH_IBX(connector->dev)) {
  6814. hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
  6815. aud_config = IBX_AUD_CFG(pipe);
  6816. aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
  6817. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  6818. } else if (IS_VALLEYVIEW(connector->dev)) {
  6819. hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
  6820. aud_config = VLV_AUD_CFG(pipe);
  6821. aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
  6822. aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
  6823. } else {
  6824. hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
  6825. aud_config = CPT_AUD_CFG(pipe);
  6826. aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
  6827. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  6828. }
  6829. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  6830. if (IS_VALLEYVIEW(connector->dev)) {
  6831. struct intel_encoder *intel_encoder;
  6832. struct intel_digital_port *intel_dig_port;
  6833. intel_encoder = intel_attached_encoder(connector);
  6834. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  6835. i = intel_dig_port->port;
  6836. } else {
  6837. i = I915_READ(aud_cntl_st);
  6838. i = (i >> 29) & DIP_PORT_SEL_MASK;
  6839. /* DIP_Port_Select, 0x1 = PortB */
  6840. }
  6841. if (!i) {
  6842. DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
  6843. /* operate blindly on all ports */
  6844. eldv = IBX_ELD_VALIDB;
  6845. eldv |= IBX_ELD_VALIDB << 4;
  6846. eldv |= IBX_ELD_VALIDB << 8;
  6847. } else {
  6848. DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
  6849. eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
  6850. }
  6851. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  6852. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  6853. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  6854. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  6855. } else {
  6856. I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
  6857. }
  6858. if (intel_eld_uptodate(connector,
  6859. aud_cntrl_st2, eldv,
  6860. aud_cntl_st, IBX_ELD_ADDRESS,
  6861. hdmiw_hdmiedid))
  6862. return;
  6863. i = I915_READ(aud_cntrl_st2);
  6864. i &= ~eldv;
  6865. I915_WRITE(aud_cntrl_st2, i);
  6866. if (!eld[0])
  6867. return;
  6868. i = I915_READ(aud_cntl_st);
  6869. i &= ~IBX_ELD_ADDRESS;
  6870. I915_WRITE(aud_cntl_st, i);
  6871. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  6872. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6873. for (i = 0; i < len; i++)
  6874. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  6875. i = I915_READ(aud_cntrl_st2);
  6876. i |= eldv;
  6877. I915_WRITE(aud_cntrl_st2, i);
  6878. }
  6879. void intel_write_eld(struct drm_encoder *encoder,
  6880. struct drm_display_mode *mode)
  6881. {
  6882. struct drm_crtc *crtc = encoder->crtc;
  6883. struct drm_connector *connector;
  6884. struct drm_device *dev = encoder->dev;
  6885. struct drm_i915_private *dev_priv = dev->dev_private;
  6886. connector = drm_select_eld(encoder, mode);
  6887. if (!connector)
  6888. return;
  6889. DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  6890. connector->base.id,
  6891. connector->name,
  6892. connector->encoder->base.id,
  6893. connector->encoder->name);
  6894. connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
  6895. if (dev_priv->display.write_eld)
  6896. dev_priv->display.write_eld(connector, crtc, mode);
  6897. }
  6898. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  6899. {
  6900. struct drm_device *dev = crtc->dev;
  6901. struct drm_i915_private *dev_priv = dev->dev_private;
  6902. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6903. uint32_t cntl = 0, size = 0;
  6904. if (base) {
  6905. unsigned int width = intel_crtc->cursor_width;
  6906. unsigned int height = intel_crtc->cursor_height;
  6907. unsigned int stride = roundup_pow_of_two(width) * 4;
  6908. switch (stride) {
  6909. default:
  6910. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  6911. width, stride);
  6912. stride = 256;
  6913. /* fallthrough */
  6914. case 256:
  6915. case 512:
  6916. case 1024:
  6917. case 2048:
  6918. break;
  6919. }
  6920. cntl |= CURSOR_ENABLE |
  6921. CURSOR_GAMMA_ENABLE |
  6922. CURSOR_FORMAT_ARGB |
  6923. CURSOR_STRIDE(stride);
  6924. size = (height << 12) | width;
  6925. }
  6926. if (intel_crtc->cursor_cntl != 0 &&
  6927. (intel_crtc->cursor_base != base ||
  6928. intel_crtc->cursor_size != size ||
  6929. intel_crtc->cursor_cntl != cntl)) {
  6930. /* On these chipsets we can only modify the base/size/stride
  6931. * whilst the cursor is disabled.
  6932. */
  6933. I915_WRITE(_CURACNTR, 0);
  6934. POSTING_READ(_CURACNTR);
  6935. intel_crtc->cursor_cntl = 0;
  6936. }
  6937. if (intel_crtc->cursor_base != base)
  6938. I915_WRITE(_CURABASE, base);
  6939. if (intel_crtc->cursor_size != size) {
  6940. I915_WRITE(CURSIZE, size);
  6941. intel_crtc->cursor_size = size;
  6942. }
  6943. if (intel_crtc->cursor_cntl != cntl) {
  6944. I915_WRITE(_CURACNTR, cntl);
  6945. POSTING_READ(_CURACNTR);
  6946. intel_crtc->cursor_cntl = cntl;
  6947. }
  6948. }
  6949. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  6950. {
  6951. struct drm_device *dev = crtc->dev;
  6952. struct drm_i915_private *dev_priv = dev->dev_private;
  6953. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6954. int pipe = intel_crtc->pipe;
  6955. uint32_t cntl;
  6956. cntl = 0;
  6957. if (base) {
  6958. cntl = MCURSOR_GAMMA_ENABLE;
  6959. switch (intel_crtc->cursor_width) {
  6960. case 64:
  6961. cntl |= CURSOR_MODE_64_ARGB_AX;
  6962. break;
  6963. case 128:
  6964. cntl |= CURSOR_MODE_128_ARGB_AX;
  6965. break;
  6966. case 256:
  6967. cntl |= CURSOR_MODE_256_ARGB_AX;
  6968. break;
  6969. default:
  6970. WARN_ON(1);
  6971. return;
  6972. }
  6973. cntl |= pipe << 28; /* Connect to correct pipe */
  6974. }
  6975. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  6976. cntl |= CURSOR_PIPE_CSC_ENABLE;
  6977. if (intel_crtc->cursor_cntl != cntl) {
  6978. I915_WRITE(CURCNTR(pipe), cntl);
  6979. POSTING_READ(CURCNTR(pipe));
  6980. intel_crtc->cursor_cntl = cntl;
  6981. }
  6982. /* and commit changes on next vblank */
  6983. I915_WRITE(CURBASE(pipe), base);
  6984. POSTING_READ(CURBASE(pipe));
  6985. }
  6986. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  6987. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  6988. bool on)
  6989. {
  6990. struct drm_device *dev = crtc->dev;
  6991. struct drm_i915_private *dev_priv = dev->dev_private;
  6992. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6993. int pipe = intel_crtc->pipe;
  6994. int x = crtc->cursor_x;
  6995. int y = crtc->cursor_y;
  6996. u32 base = 0, pos = 0;
  6997. if (on)
  6998. base = intel_crtc->cursor_addr;
  6999. if (x >= intel_crtc->config.pipe_src_w)
  7000. base = 0;
  7001. if (y >= intel_crtc->config.pipe_src_h)
  7002. base = 0;
  7003. if (x < 0) {
  7004. if (x + intel_crtc->cursor_width <= 0)
  7005. base = 0;
  7006. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  7007. x = -x;
  7008. }
  7009. pos |= x << CURSOR_X_SHIFT;
  7010. if (y < 0) {
  7011. if (y + intel_crtc->cursor_height <= 0)
  7012. base = 0;
  7013. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  7014. y = -y;
  7015. }
  7016. pos |= y << CURSOR_Y_SHIFT;
  7017. if (base == 0 && intel_crtc->cursor_base == 0)
  7018. return;
  7019. I915_WRITE(CURPOS(pipe), pos);
  7020. if (IS_845G(dev) || IS_I865G(dev))
  7021. i845_update_cursor(crtc, base);
  7022. else
  7023. i9xx_update_cursor(crtc, base);
  7024. intel_crtc->cursor_base = base;
  7025. }
  7026. static bool cursor_size_ok(struct drm_device *dev,
  7027. uint32_t width, uint32_t height)
  7028. {
  7029. if (width == 0 || height == 0)
  7030. return false;
  7031. /*
  7032. * 845g/865g are special in that they are only limited by
  7033. * the width of their cursors, the height is arbitrary up to
  7034. * the precision of the register. Everything else requires
  7035. * square cursors, limited to a few power-of-two sizes.
  7036. */
  7037. if (IS_845G(dev) || IS_I865G(dev)) {
  7038. if ((width & 63) != 0)
  7039. return false;
  7040. if (width > (IS_845G(dev) ? 64 : 512))
  7041. return false;
  7042. if (height > 1023)
  7043. return false;
  7044. } else {
  7045. switch (width | height) {
  7046. case 256:
  7047. case 128:
  7048. if (IS_GEN2(dev))
  7049. return false;
  7050. case 64:
  7051. break;
  7052. default:
  7053. return false;
  7054. }
  7055. }
  7056. return true;
  7057. }
  7058. /*
  7059. * intel_crtc_cursor_set_obj - Set cursor to specified GEM object
  7060. *
  7061. * Note that the object's reference will be consumed if the update fails. If
  7062. * the update succeeds, the reference of the old object (if any) will be
  7063. * consumed.
  7064. */
  7065. static int intel_crtc_cursor_set_obj(struct drm_crtc *crtc,
  7066. struct drm_i915_gem_object *obj,
  7067. uint32_t width, uint32_t height)
  7068. {
  7069. struct drm_device *dev = crtc->dev;
  7070. struct drm_i915_private *dev_priv = dev->dev_private;
  7071. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7072. enum pipe pipe = intel_crtc->pipe;
  7073. unsigned old_width, stride;
  7074. uint32_t addr;
  7075. int ret;
  7076. /* if we want to turn off the cursor ignore width and height */
  7077. if (!obj) {
  7078. DRM_DEBUG_KMS("cursor off\n");
  7079. addr = 0;
  7080. mutex_lock(&dev->struct_mutex);
  7081. goto finish;
  7082. }
  7083. /* Check for which cursor types we support */
  7084. if (!cursor_size_ok(dev, width, height)) {
  7085. DRM_DEBUG("Cursor dimension not supported\n");
  7086. return -EINVAL;
  7087. }
  7088. stride = roundup_pow_of_two(width) * 4;
  7089. if (obj->base.size < stride * height) {
  7090. DRM_DEBUG_KMS("buffer is too small\n");
  7091. ret = -ENOMEM;
  7092. goto fail;
  7093. }
  7094. /* we only need to pin inside GTT if cursor is non-phy */
  7095. mutex_lock(&dev->struct_mutex);
  7096. if (!INTEL_INFO(dev)->cursor_needs_physical) {
  7097. unsigned alignment;
  7098. if (obj->tiling_mode) {
  7099. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  7100. ret = -EINVAL;
  7101. goto fail_locked;
  7102. }
  7103. /*
  7104. * Global gtt pte registers are special registers which actually
  7105. * forward writes to a chunk of system memory. Which means that
  7106. * there is no risk that the register values disappear as soon
  7107. * as we call intel_runtime_pm_put(), so it is correct to wrap
  7108. * only the pin/unpin/fence and not more.
  7109. */
  7110. intel_runtime_pm_get(dev_priv);
  7111. /* Note that the w/a also requires 2 PTE of padding following
  7112. * the bo. We currently fill all unused PTE with the shadow
  7113. * page and so we should always have valid PTE following the
  7114. * cursor preventing the VT-d warning.
  7115. */
  7116. alignment = 0;
  7117. if (need_vtd_wa(dev))
  7118. alignment = 64*1024;
  7119. ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
  7120. if (ret) {
  7121. DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
  7122. intel_runtime_pm_put(dev_priv);
  7123. goto fail_locked;
  7124. }
  7125. ret = i915_gem_object_put_fence(obj);
  7126. if (ret) {
  7127. DRM_DEBUG_KMS("failed to release fence for cursor");
  7128. intel_runtime_pm_put(dev_priv);
  7129. goto fail_unpin;
  7130. }
  7131. addr = i915_gem_obj_ggtt_offset(obj);
  7132. intel_runtime_pm_put(dev_priv);
  7133. } else {
  7134. int align = IS_I830(dev) ? 16 * 1024 : 256;
  7135. ret = i915_gem_object_attach_phys(obj, align);
  7136. if (ret) {
  7137. DRM_DEBUG_KMS("failed to attach phys object\n");
  7138. goto fail_locked;
  7139. }
  7140. addr = obj->phys_handle->busaddr;
  7141. }
  7142. finish:
  7143. if (intel_crtc->cursor_bo) {
  7144. if (!INTEL_INFO(dev)->cursor_needs_physical)
  7145. i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
  7146. }
  7147. i915_gem_track_fb(intel_crtc->cursor_bo, obj,
  7148. INTEL_FRONTBUFFER_CURSOR(pipe));
  7149. mutex_unlock(&dev->struct_mutex);
  7150. old_width = intel_crtc->cursor_width;
  7151. intel_crtc->cursor_addr = addr;
  7152. intel_crtc->cursor_bo = obj;
  7153. intel_crtc->cursor_width = width;
  7154. intel_crtc->cursor_height = height;
  7155. if (intel_crtc->active) {
  7156. if (old_width != width)
  7157. intel_update_watermarks(crtc);
  7158. intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
  7159. }
  7160. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_CURSOR(pipe));
  7161. return 0;
  7162. fail_unpin:
  7163. i915_gem_object_unpin_from_display_plane(obj);
  7164. fail_locked:
  7165. mutex_unlock(&dev->struct_mutex);
  7166. fail:
  7167. drm_gem_object_unreference_unlocked(&obj->base);
  7168. return ret;
  7169. }
  7170. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  7171. u16 *blue, uint32_t start, uint32_t size)
  7172. {
  7173. int end = (start + size > 256) ? 256 : start + size, i;
  7174. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7175. for (i = start; i < end; i++) {
  7176. intel_crtc->lut_r[i] = red[i] >> 8;
  7177. intel_crtc->lut_g[i] = green[i] >> 8;
  7178. intel_crtc->lut_b[i] = blue[i] >> 8;
  7179. }
  7180. intel_crtc_load_lut(crtc);
  7181. }
  7182. /* VESA 640x480x72Hz mode to set on the pipe */
  7183. static struct drm_display_mode load_detect_mode = {
  7184. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  7185. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  7186. };
  7187. struct drm_framebuffer *
  7188. __intel_framebuffer_create(struct drm_device *dev,
  7189. struct drm_mode_fb_cmd2 *mode_cmd,
  7190. struct drm_i915_gem_object *obj)
  7191. {
  7192. struct intel_framebuffer *intel_fb;
  7193. int ret;
  7194. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7195. if (!intel_fb) {
  7196. drm_gem_object_unreference_unlocked(&obj->base);
  7197. return ERR_PTR(-ENOMEM);
  7198. }
  7199. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  7200. if (ret)
  7201. goto err;
  7202. return &intel_fb->base;
  7203. err:
  7204. drm_gem_object_unreference_unlocked(&obj->base);
  7205. kfree(intel_fb);
  7206. return ERR_PTR(ret);
  7207. }
  7208. static struct drm_framebuffer *
  7209. intel_framebuffer_create(struct drm_device *dev,
  7210. struct drm_mode_fb_cmd2 *mode_cmd,
  7211. struct drm_i915_gem_object *obj)
  7212. {
  7213. struct drm_framebuffer *fb;
  7214. int ret;
  7215. ret = i915_mutex_lock_interruptible(dev);
  7216. if (ret)
  7217. return ERR_PTR(ret);
  7218. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  7219. mutex_unlock(&dev->struct_mutex);
  7220. return fb;
  7221. }
  7222. static u32
  7223. intel_framebuffer_pitch_for_width(int width, int bpp)
  7224. {
  7225. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  7226. return ALIGN(pitch, 64);
  7227. }
  7228. static u32
  7229. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  7230. {
  7231. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  7232. return PAGE_ALIGN(pitch * mode->vdisplay);
  7233. }
  7234. static struct drm_framebuffer *
  7235. intel_framebuffer_create_for_mode(struct drm_device *dev,
  7236. struct drm_display_mode *mode,
  7237. int depth, int bpp)
  7238. {
  7239. struct drm_i915_gem_object *obj;
  7240. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  7241. obj = i915_gem_alloc_object(dev,
  7242. intel_framebuffer_size_for_mode(mode, bpp));
  7243. if (obj == NULL)
  7244. return ERR_PTR(-ENOMEM);
  7245. mode_cmd.width = mode->hdisplay;
  7246. mode_cmd.height = mode->vdisplay;
  7247. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  7248. bpp);
  7249. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  7250. return intel_framebuffer_create(dev, &mode_cmd, obj);
  7251. }
  7252. static struct drm_framebuffer *
  7253. mode_fits_in_fbdev(struct drm_device *dev,
  7254. struct drm_display_mode *mode)
  7255. {
  7256. #ifdef CONFIG_DRM_I915_FBDEV
  7257. struct drm_i915_private *dev_priv = dev->dev_private;
  7258. struct drm_i915_gem_object *obj;
  7259. struct drm_framebuffer *fb;
  7260. if (!dev_priv->fbdev)
  7261. return NULL;
  7262. if (!dev_priv->fbdev->fb)
  7263. return NULL;
  7264. obj = dev_priv->fbdev->fb->obj;
  7265. BUG_ON(!obj);
  7266. fb = &dev_priv->fbdev->fb->base;
  7267. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  7268. fb->bits_per_pixel))
  7269. return NULL;
  7270. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  7271. return NULL;
  7272. return fb;
  7273. #else
  7274. return NULL;
  7275. #endif
  7276. }
  7277. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  7278. struct drm_display_mode *mode,
  7279. struct intel_load_detect_pipe *old,
  7280. struct drm_modeset_acquire_ctx *ctx)
  7281. {
  7282. struct intel_crtc *intel_crtc;
  7283. struct intel_encoder *intel_encoder =
  7284. intel_attached_encoder(connector);
  7285. struct drm_crtc *possible_crtc;
  7286. struct drm_encoder *encoder = &intel_encoder->base;
  7287. struct drm_crtc *crtc = NULL;
  7288. struct drm_device *dev = encoder->dev;
  7289. struct drm_framebuffer *fb;
  7290. struct drm_mode_config *config = &dev->mode_config;
  7291. int ret, i = -1;
  7292. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7293. connector->base.id, connector->name,
  7294. encoder->base.id, encoder->name);
  7295. retry:
  7296. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  7297. if (ret)
  7298. goto fail_unlock;
  7299. /*
  7300. * Algorithm gets a little messy:
  7301. *
  7302. * - if the connector already has an assigned crtc, use it (but make
  7303. * sure it's on first)
  7304. *
  7305. * - try to find the first unused crtc that can drive this connector,
  7306. * and use that if we find one
  7307. */
  7308. /* See if we already have a CRTC for this connector */
  7309. if (encoder->crtc) {
  7310. crtc = encoder->crtc;
  7311. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7312. if (ret)
  7313. goto fail_unlock;
  7314. old->dpms_mode = connector->dpms;
  7315. old->load_detect_temp = false;
  7316. /* Make sure the crtc and connector are running */
  7317. if (connector->dpms != DRM_MODE_DPMS_ON)
  7318. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  7319. return true;
  7320. }
  7321. /* Find an unused one (if possible) */
  7322. for_each_crtc(dev, possible_crtc) {
  7323. i++;
  7324. if (!(encoder->possible_crtcs & (1 << i)))
  7325. continue;
  7326. if (possible_crtc->enabled)
  7327. continue;
  7328. /* This can occur when applying the pipe A quirk on resume. */
  7329. if (to_intel_crtc(possible_crtc)->new_enabled)
  7330. continue;
  7331. crtc = possible_crtc;
  7332. break;
  7333. }
  7334. /*
  7335. * If we didn't find an unused CRTC, don't use any.
  7336. */
  7337. if (!crtc) {
  7338. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  7339. goto fail_unlock;
  7340. }
  7341. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7342. if (ret)
  7343. goto fail_unlock;
  7344. intel_encoder->new_crtc = to_intel_crtc(crtc);
  7345. to_intel_connector(connector)->new_encoder = intel_encoder;
  7346. intel_crtc = to_intel_crtc(crtc);
  7347. intel_crtc->new_enabled = true;
  7348. intel_crtc->new_config = &intel_crtc->config;
  7349. old->dpms_mode = connector->dpms;
  7350. old->load_detect_temp = true;
  7351. old->release_fb = NULL;
  7352. if (!mode)
  7353. mode = &load_detect_mode;
  7354. /* We need a framebuffer large enough to accommodate all accesses
  7355. * that the plane may generate whilst we perform load detection.
  7356. * We can not rely on the fbcon either being present (we get called
  7357. * during its initialisation to detect all boot displays, or it may
  7358. * not even exist) or that it is large enough to satisfy the
  7359. * requested mode.
  7360. */
  7361. fb = mode_fits_in_fbdev(dev, mode);
  7362. if (fb == NULL) {
  7363. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  7364. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  7365. old->release_fb = fb;
  7366. } else
  7367. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  7368. if (IS_ERR(fb)) {
  7369. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  7370. goto fail;
  7371. }
  7372. if (intel_set_mode(crtc, mode, 0, 0, fb)) {
  7373. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  7374. if (old->release_fb)
  7375. old->release_fb->funcs->destroy(old->release_fb);
  7376. goto fail;
  7377. }
  7378. /* let the connector get through one full cycle before testing */
  7379. intel_wait_for_vblank(dev, intel_crtc->pipe);
  7380. return true;
  7381. fail:
  7382. intel_crtc->new_enabled = crtc->enabled;
  7383. if (intel_crtc->new_enabled)
  7384. intel_crtc->new_config = &intel_crtc->config;
  7385. else
  7386. intel_crtc->new_config = NULL;
  7387. fail_unlock:
  7388. if (ret == -EDEADLK) {
  7389. drm_modeset_backoff(ctx);
  7390. goto retry;
  7391. }
  7392. return false;
  7393. }
  7394. void intel_release_load_detect_pipe(struct drm_connector *connector,
  7395. struct intel_load_detect_pipe *old)
  7396. {
  7397. struct intel_encoder *intel_encoder =
  7398. intel_attached_encoder(connector);
  7399. struct drm_encoder *encoder = &intel_encoder->base;
  7400. struct drm_crtc *crtc = encoder->crtc;
  7401. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7402. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7403. connector->base.id, connector->name,
  7404. encoder->base.id, encoder->name);
  7405. if (old->load_detect_temp) {
  7406. to_intel_connector(connector)->new_encoder = NULL;
  7407. intel_encoder->new_crtc = NULL;
  7408. intel_crtc->new_enabled = false;
  7409. intel_crtc->new_config = NULL;
  7410. intel_set_mode(crtc, NULL, 0, 0, NULL);
  7411. if (old->release_fb) {
  7412. drm_framebuffer_unregister_private(old->release_fb);
  7413. drm_framebuffer_unreference(old->release_fb);
  7414. }
  7415. return;
  7416. }
  7417. /* Switch crtc and encoder back off if necessary */
  7418. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  7419. connector->funcs->dpms(connector, old->dpms_mode);
  7420. }
  7421. static int i9xx_pll_refclk(struct drm_device *dev,
  7422. const struct intel_crtc_config *pipe_config)
  7423. {
  7424. struct drm_i915_private *dev_priv = dev->dev_private;
  7425. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7426. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  7427. return dev_priv->vbt.lvds_ssc_freq;
  7428. else if (HAS_PCH_SPLIT(dev))
  7429. return 120000;
  7430. else if (!IS_GEN2(dev))
  7431. return 96000;
  7432. else
  7433. return 48000;
  7434. }
  7435. /* Returns the clock of the currently programmed mode of the given pipe. */
  7436. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  7437. struct intel_crtc_config *pipe_config)
  7438. {
  7439. struct drm_device *dev = crtc->base.dev;
  7440. struct drm_i915_private *dev_priv = dev->dev_private;
  7441. int pipe = pipe_config->cpu_transcoder;
  7442. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7443. u32 fp;
  7444. intel_clock_t clock;
  7445. int refclk = i9xx_pll_refclk(dev, pipe_config);
  7446. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  7447. fp = pipe_config->dpll_hw_state.fp0;
  7448. else
  7449. fp = pipe_config->dpll_hw_state.fp1;
  7450. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  7451. if (IS_PINEVIEW(dev)) {
  7452. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  7453. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7454. } else {
  7455. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  7456. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7457. }
  7458. if (!IS_GEN2(dev)) {
  7459. if (IS_PINEVIEW(dev))
  7460. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  7461. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  7462. else
  7463. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  7464. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7465. switch (dpll & DPLL_MODE_MASK) {
  7466. case DPLLB_MODE_DAC_SERIAL:
  7467. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  7468. 5 : 10;
  7469. break;
  7470. case DPLLB_MODE_LVDS:
  7471. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  7472. 7 : 14;
  7473. break;
  7474. default:
  7475. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  7476. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  7477. return;
  7478. }
  7479. if (IS_PINEVIEW(dev))
  7480. pineview_clock(refclk, &clock);
  7481. else
  7482. i9xx_clock(refclk, &clock);
  7483. } else {
  7484. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  7485. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  7486. if (is_lvds) {
  7487. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  7488. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7489. if (lvds & LVDS_CLKB_POWER_UP)
  7490. clock.p2 = 7;
  7491. else
  7492. clock.p2 = 14;
  7493. } else {
  7494. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  7495. clock.p1 = 2;
  7496. else {
  7497. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  7498. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  7499. }
  7500. if (dpll & PLL_P2_DIVIDE_BY_4)
  7501. clock.p2 = 4;
  7502. else
  7503. clock.p2 = 2;
  7504. }
  7505. i9xx_clock(refclk, &clock);
  7506. }
  7507. /*
  7508. * This value includes pixel_multiplier. We will use
  7509. * port_clock to compute adjusted_mode.crtc_clock in the
  7510. * encoder's get_config() function.
  7511. */
  7512. pipe_config->port_clock = clock.dot;
  7513. }
  7514. int intel_dotclock_calculate(int link_freq,
  7515. const struct intel_link_m_n *m_n)
  7516. {
  7517. /*
  7518. * The calculation for the data clock is:
  7519. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  7520. * But we want to avoid losing precison if possible, so:
  7521. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  7522. *
  7523. * and the link clock is simpler:
  7524. * link_clock = (m * link_clock) / n
  7525. */
  7526. if (!m_n->link_n)
  7527. return 0;
  7528. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  7529. }
  7530. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  7531. struct intel_crtc_config *pipe_config)
  7532. {
  7533. struct drm_device *dev = crtc->base.dev;
  7534. /* read out port_clock from the DPLL */
  7535. i9xx_crtc_clock_get(crtc, pipe_config);
  7536. /*
  7537. * This value does not include pixel_multiplier.
  7538. * We will check that port_clock and adjusted_mode.crtc_clock
  7539. * agree once we know their relationship in the encoder's
  7540. * get_config() function.
  7541. */
  7542. pipe_config->adjusted_mode.crtc_clock =
  7543. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  7544. &pipe_config->fdi_m_n);
  7545. }
  7546. /** Returns the currently programmed mode of the given pipe. */
  7547. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  7548. struct drm_crtc *crtc)
  7549. {
  7550. struct drm_i915_private *dev_priv = dev->dev_private;
  7551. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7552. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  7553. struct drm_display_mode *mode;
  7554. struct intel_crtc_config pipe_config;
  7555. int htot = I915_READ(HTOTAL(cpu_transcoder));
  7556. int hsync = I915_READ(HSYNC(cpu_transcoder));
  7557. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  7558. int vsync = I915_READ(VSYNC(cpu_transcoder));
  7559. enum pipe pipe = intel_crtc->pipe;
  7560. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  7561. if (!mode)
  7562. return NULL;
  7563. /*
  7564. * Construct a pipe_config sufficient for getting the clock info
  7565. * back out of crtc_clock_get.
  7566. *
  7567. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  7568. * to use a real value here instead.
  7569. */
  7570. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  7571. pipe_config.pixel_multiplier = 1;
  7572. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  7573. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  7574. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  7575. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  7576. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  7577. mode->hdisplay = (htot & 0xffff) + 1;
  7578. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  7579. mode->hsync_start = (hsync & 0xffff) + 1;
  7580. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  7581. mode->vdisplay = (vtot & 0xffff) + 1;
  7582. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  7583. mode->vsync_start = (vsync & 0xffff) + 1;
  7584. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  7585. drm_mode_set_name(mode);
  7586. return mode;
  7587. }
  7588. static void intel_increase_pllclock(struct drm_device *dev,
  7589. enum pipe pipe)
  7590. {
  7591. struct drm_i915_private *dev_priv = dev->dev_private;
  7592. int dpll_reg = DPLL(pipe);
  7593. int dpll;
  7594. if (!HAS_GMCH_DISPLAY(dev))
  7595. return;
  7596. if (!dev_priv->lvds_downclock_avail)
  7597. return;
  7598. dpll = I915_READ(dpll_reg);
  7599. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  7600. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  7601. assert_panel_unlocked(dev_priv, pipe);
  7602. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  7603. I915_WRITE(dpll_reg, dpll);
  7604. intel_wait_for_vblank(dev, pipe);
  7605. dpll = I915_READ(dpll_reg);
  7606. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  7607. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  7608. }
  7609. }
  7610. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  7611. {
  7612. struct drm_device *dev = crtc->dev;
  7613. struct drm_i915_private *dev_priv = dev->dev_private;
  7614. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7615. if (!HAS_GMCH_DISPLAY(dev))
  7616. return;
  7617. if (!dev_priv->lvds_downclock_avail)
  7618. return;
  7619. /*
  7620. * Since this is called by a timer, we should never get here in
  7621. * the manual case.
  7622. */
  7623. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  7624. int pipe = intel_crtc->pipe;
  7625. int dpll_reg = DPLL(pipe);
  7626. int dpll;
  7627. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  7628. assert_panel_unlocked(dev_priv, pipe);
  7629. dpll = I915_READ(dpll_reg);
  7630. dpll |= DISPLAY_RATE_SELECT_FPA1;
  7631. I915_WRITE(dpll_reg, dpll);
  7632. intel_wait_for_vblank(dev, pipe);
  7633. dpll = I915_READ(dpll_reg);
  7634. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  7635. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  7636. }
  7637. }
  7638. void intel_mark_busy(struct drm_device *dev)
  7639. {
  7640. struct drm_i915_private *dev_priv = dev->dev_private;
  7641. if (dev_priv->mm.busy)
  7642. return;
  7643. intel_runtime_pm_get(dev_priv);
  7644. i915_update_gfx_val(dev_priv);
  7645. dev_priv->mm.busy = true;
  7646. }
  7647. void intel_mark_idle(struct drm_device *dev)
  7648. {
  7649. struct drm_i915_private *dev_priv = dev->dev_private;
  7650. struct drm_crtc *crtc;
  7651. if (!dev_priv->mm.busy)
  7652. return;
  7653. dev_priv->mm.busy = false;
  7654. if (!i915.powersave)
  7655. goto out;
  7656. for_each_crtc(dev, crtc) {
  7657. if (!crtc->primary->fb)
  7658. continue;
  7659. intel_decrease_pllclock(crtc);
  7660. }
  7661. if (INTEL_INFO(dev)->gen >= 6)
  7662. gen6_rps_idle(dev->dev_private);
  7663. out:
  7664. intel_runtime_pm_put(dev_priv);
  7665. }
  7666. /**
  7667. * intel_mark_fb_busy - mark given planes as busy
  7668. * @dev: DRM device
  7669. * @frontbuffer_bits: bits for the affected planes
  7670. * @ring: optional ring for asynchronous commands
  7671. *
  7672. * This function gets called every time the screen contents change. It can be
  7673. * used to keep e.g. the update rate at the nominal refresh rate with DRRS.
  7674. */
  7675. static void intel_mark_fb_busy(struct drm_device *dev,
  7676. unsigned frontbuffer_bits,
  7677. struct intel_engine_cs *ring)
  7678. {
  7679. struct drm_i915_private *dev_priv = dev->dev_private;
  7680. enum pipe pipe;
  7681. if (!i915.powersave)
  7682. return;
  7683. for_each_pipe(dev_priv, pipe) {
  7684. if (!(frontbuffer_bits & INTEL_FRONTBUFFER_ALL_MASK(pipe)))
  7685. continue;
  7686. intel_increase_pllclock(dev, pipe);
  7687. if (ring && intel_fbc_enabled(dev))
  7688. ring->fbc_dirty = true;
  7689. }
  7690. }
  7691. /**
  7692. * intel_fb_obj_invalidate - invalidate frontbuffer object
  7693. * @obj: GEM object to invalidate
  7694. * @ring: set for asynchronous rendering
  7695. *
  7696. * This function gets called every time rendering on the given object starts and
  7697. * frontbuffer caching (fbc, low refresh rate for DRRS, panel self refresh) must
  7698. * be invalidated. If @ring is non-NULL any subsequent invalidation will be delayed
  7699. * until the rendering completes or a flip on this frontbuffer plane is
  7700. * scheduled.
  7701. */
  7702. void intel_fb_obj_invalidate(struct drm_i915_gem_object *obj,
  7703. struct intel_engine_cs *ring)
  7704. {
  7705. struct drm_device *dev = obj->base.dev;
  7706. struct drm_i915_private *dev_priv = dev->dev_private;
  7707. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  7708. if (!obj->frontbuffer_bits)
  7709. return;
  7710. if (ring) {
  7711. mutex_lock(&dev_priv->fb_tracking.lock);
  7712. dev_priv->fb_tracking.busy_bits
  7713. |= obj->frontbuffer_bits;
  7714. dev_priv->fb_tracking.flip_bits
  7715. &= ~obj->frontbuffer_bits;
  7716. mutex_unlock(&dev_priv->fb_tracking.lock);
  7717. }
  7718. intel_mark_fb_busy(dev, obj->frontbuffer_bits, ring);
  7719. intel_edp_psr_invalidate(dev, obj->frontbuffer_bits);
  7720. }
  7721. /**
  7722. * intel_frontbuffer_flush - flush frontbuffer
  7723. * @dev: DRM device
  7724. * @frontbuffer_bits: frontbuffer plane tracking bits
  7725. *
  7726. * This function gets called every time rendering on the given planes has
  7727. * completed and frontbuffer caching can be started again. Flushes will get
  7728. * delayed if they're blocked by some oustanding asynchronous rendering.
  7729. *
  7730. * Can be called without any locks held.
  7731. */
  7732. void intel_frontbuffer_flush(struct drm_device *dev,
  7733. unsigned frontbuffer_bits)
  7734. {
  7735. struct drm_i915_private *dev_priv = dev->dev_private;
  7736. /* Delay flushing when rings are still busy.*/
  7737. mutex_lock(&dev_priv->fb_tracking.lock);
  7738. frontbuffer_bits &= ~dev_priv->fb_tracking.busy_bits;
  7739. mutex_unlock(&dev_priv->fb_tracking.lock);
  7740. intel_mark_fb_busy(dev, frontbuffer_bits, NULL);
  7741. intel_edp_psr_flush(dev, frontbuffer_bits);
  7742. /*
  7743. * FIXME: Unconditional fbc flushing here is a rather gross hack and
  7744. * needs to be reworked into a proper frontbuffer tracking scheme like
  7745. * psr employs.
  7746. */
  7747. if (IS_BROADWELL(dev))
  7748. gen8_fbc_sw_flush(dev, FBC_REND_CACHE_CLEAN);
  7749. }
  7750. /**
  7751. * intel_fb_obj_flush - flush frontbuffer object
  7752. * @obj: GEM object to flush
  7753. * @retire: set when retiring asynchronous rendering
  7754. *
  7755. * This function gets called every time rendering on the given object has
  7756. * completed and frontbuffer caching can be started again. If @retire is true
  7757. * then any delayed flushes will be unblocked.
  7758. */
  7759. void intel_fb_obj_flush(struct drm_i915_gem_object *obj,
  7760. bool retire)
  7761. {
  7762. struct drm_device *dev = obj->base.dev;
  7763. struct drm_i915_private *dev_priv = dev->dev_private;
  7764. unsigned frontbuffer_bits;
  7765. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  7766. if (!obj->frontbuffer_bits)
  7767. return;
  7768. frontbuffer_bits = obj->frontbuffer_bits;
  7769. if (retire) {
  7770. mutex_lock(&dev_priv->fb_tracking.lock);
  7771. /* Filter out new bits since rendering started. */
  7772. frontbuffer_bits &= dev_priv->fb_tracking.busy_bits;
  7773. dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits;
  7774. mutex_unlock(&dev_priv->fb_tracking.lock);
  7775. }
  7776. intel_frontbuffer_flush(dev, frontbuffer_bits);
  7777. }
  7778. /**
  7779. * intel_frontbuffer_flip_prepare - prepare asnychronous frontbuffer flip
  7780. * @dev: DRM device
  7781. * @frontbuffer_bits: frontbuffer plane tracking bits
  7782. *
  7783. * This function gets called after scheduling a flip on @obj. The actual
  7784. * frontbuffer flushing will be delayed until completion is signalled with
  7785. * intel_frontbuffer_flip_complete. If an invalidate happens in between this
  7786. * flush will be cancelled.
  7787. *
  7788. * Can be called without any locks held.
  7789. */
  7790. void intel_frontbuffer_flip_prepare(struct drm_device *dev,
  7791. unsigned frontbuffer_bits)
  7792. {
  7793. struct drm_i915_private *dev_priv = dev->dev_private;
  7794. mutex_lock(&dev_priv->fb_tracking.lock);
  7795. dev_priv->fb_tracking.flip_bits
  7796. |= frontbuffer_bits;
  7797. mutex_unlock(&dev_priv->fb_tracking.lock);
  7798. }
  7799. /**
  7800. * intel_frontbuffer_flip_complete - complete asynchronous frontbuffer flush
  7801. * @dev: DRM device
  7802. * @frontbuffer_bits: frontbuffer plane tracking bits
  7803. *
  7804. * This function gets called after the flip has been latched and will complete
  7805. * on the next vblank. It will execute the fush if it hasn't been cancalled yet.
  7806. *
  7807. * Can be called without any locks held.
  7808. */
  7809. void intel_frontbuffer_flip_complete(struct drm_device *dev,
  7810. unsigned frontbuffer_bits)
  7811. {
  7812. struct drm_i915_private *dev_priv = dev->dev_private;
  7813. mutex_lock(&dev_priv->fb_tracking.lock);
  7814. /* Mask any cancelled flips. */
  7815. frontbuffer_bits &= dev_priv->fb_tracking.flip_bits;
  7816. dev_priv->fb_tracking.flip_bits &= ~frontbuffer_bits;
  7817. mutex_unlock(&dev_priv->fb_tracking.lock);
  7818. intel_frontbuffer_flush(dev, frontbuffer_bits);
  7819. }
  7820. static void intel_crtc_destroy(struct drm_crtc *crtc)
  7821. {
  7822. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7823. struct drm_device *dev = crtc->dev;
  7824. struct intel_unpin_work *work;
  7825. unsigned long flags;
  7826. spin_lock_irqsave(&dev->event_lock, flags);
  7827. work = intel_crtc->unpin_work;
  7828. intel_crtc->unpin_work = NULL;
  7829. spin_unlock_irqrestore(&dev->event_lock, flags);
  7830. if (work) {
  7831. cancel_work_sync(&work->work);
  7832. kfree(work);
  7833. }
  7834. drm_crtc_cleanup(crtc);
  7835. kfree(intel_crtc);
  7836. }
  7837. static void intel_unpin_work_fn(struct work_struct *__work)
  7838. {
  7839. struct intel_unpin_work *work =
  7840. container_of(__work, struct intel_unpin_work, work);
  7841. struct drm_device *dev = work->crtc->dev;
  7842. enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
  7843. mutex_lock(&dev->struct_mutex);
  7844. intel_unpin_fb_obj(work->old_fb_obj);
  7845. drm_gem_object_unreference(&work->pending_flip_obj->base);
  7846. drm_gem_object_unreference(&work->old_fb_obj->base);
  7847. intel_update_fbc(dev);
  7848. mutex_unlock(&dev->struct_mutex);
  7849. intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  7850. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  7851. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  7852. kfree(work);
  7853. }
  7854. static void do_intel_finish_page_flip(struct drm_device *dev,
  7855. struct drm_crtc *crtc)
  7856. {
  7857. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7858. struct intel_unpin_work *work;
  7859. unsigned long flags;
  7860. /* Ignore early vblank irqs */
  7861. if (intel_crtc == NULL)
  7862. return;
  7863. spin_lock_irqsave(&dev->event_lock, flags);
  7864. work = intel_crtc->unpin_work;
  7865. /* Ensure we don't miss a work->pending update ... */
  7866. smp_rmb();
  7867. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  7868. spin_unlock_irqrestore(&dev->event_lock, flags);
  7869. return;
  7870. }
  7871. page_flip_completed(intel_crtc);
  7872. spin_unlock_irqrestore(&dev->event_lock, flags);
  7873. }
  7874. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  7875. {
  7876. struct drm_i915_private *dev_priv = dev->dev_private;
  7877. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  7878. do_intel_finish_page_flip(dev, crtc);
  7879. }
  7880. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  7881. {
  7882. struct drm_i915_private *dev_priv = dev->dev_private;
  7883. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  7884. do_intel_finish_page_flip(dev, crtc);
  7885. }
  7886. /* Is 'a' after or equal to 'b'? */
  7887. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  7888. {
  7889. return !((a - b) & 0x80000000);
  7890. }
  7891. static bool page_flip_finished(struct intel_crtc *crtc)
  7892. {
  7893. struct drm_device *dev = crtc->base.dev;
  7894. struct drm_i915_private *dev_priv = dev->dev_private;
  7895. /*
  7896. * The relevant registers doen't exist on pre-ctg.
  7897. * As the flip done interrupt doesn't trigger for mmio
  7898. * flips on gmch platforms, a flip count check isn't
  7899. * really needed there. But since ctg has the registers,
  7900. * include it in the check anyway.
  7901. */
  7902. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  7903. return true;
  7904. /*
  7905. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  7906. * used the same base address. In that case the mmio flip might
  7907. * have completed, but the CS hasn't even executed the flip yet.
  7908. *
  7909. * A flip count check isn't enough as the CS might have updated
  7910. * the base address just after start of vblank, but before we
  7911. * managed to process the interrupt. This means we'd complete the
  7912. * CS flip too soon.
  7913. *
  7914. * Combining both checks should get us a good enough result. It may
  7915. * still happen that the CS flip has been executed, but has not
  7916. * yet actually completed. But in case the base address is the same
  7917. * anyway, we don't really care.
  7918. */
  7919. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  7920. crtc->unpin_work->gtt_offset &&
  7921. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  7922. crtc->unpin_work->flip_count);
  7923. }
  7924. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  7925. {
  7926. struct drm_i915_private *dev_priv = dev->dev_private;
  7927. struct intel_crtc *intel_crtc =
  7928. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  7929. unsigned long flags;
  7930. /* NB: An MMIO update of the plane base pointer will also
  7931. * generate a page-flip completion irq, i.e. every modeset
  7932. * is also accompanied by a spurious intel_prepare_page_flip().
  7933. */
  7934. spin_lock_irqsave(&dev->event_lock, flags);
  7935. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  7936. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  7937. spin_unlock_irqrestore(&dev->event_lock, flags);
  7938. }
  7939. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  7940. {
  7941. /* Ensure that the work item is consistent when activating it ... */
  7942. smp_wmb();
  7943. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  7944. /* and that it is marked active as soon as the irq could fire. */
  7945. smp_wmb();
  7946. }
  7947. static int intel_gen2_queue_flip(struct drm_device *dev,
  7948. struct drm_crtc *crtc,
  7949. struct drm_framebuffer *fb,
  7950. struct drm_i915_gem_object *obj,
  7951. struct intel_engine_cs *ring,
  7952. uint32_t flags)
  7953. {
  7954. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7955. u32 flip_mask;
  7956. int ret;
  7957. ret = intel_ring_begin(ring, 6);
  7958. if (ret)
  7959. return ret;
  7960. /* Can't queue multiple flips, so wait for the previous
  7961. * one to finish before executing the next.
  7962. */
  7963. if (intel_crtc->plane)
  7964. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7965. else
  7966. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7967. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7968. intel_ring_emit(ring, MI_NOOP);
  7969. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7970. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7971. intel_ring_emit(ring, fb->pitches[0]);
  7972. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7973. intel_ring_emit(ring, 0); /* aux display base address, unused */
  7974. intel_mark_page_flip_active(intel_crtc);
  7975. __intel_ring_advance(ring);
  7976. return 0;
  7977. }
  7978. static int intel_gen3_queue_flip(struct drm_device *dev,
  7979. struct drm_crtc *crtc,
  7980. struct drm_framebuffer *fb,
  7981. struct drm_i915_gem_object *obj,
  7982. struct intel_engine_cs *ring,
  7983. uint32_t flags)
  7984. {
  7985. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7986. u32 flip_mask;
  7987. int ret;
  7988. ret = intel_ring_begin(ring, 6);
  7989. if (ret)
  7990. return ret;
  7991. if (intel_crtc->plane)
  7992. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7993. else
  7994. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7995. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7996. intel_ring_emit(ring, MI_NOOP);
  7997. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  7998. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7999. intel_ring_emit(ring, fb->pitches[0]);
  8000. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8001. intel_ring_emit(ring, MI_NOOP);
  8002. intel_mark_page_flip_active(intel_crtc);
  8003. __intel_ring_advance(ring);
  8004. return 0;
  8005. }
  8006. static int intel_gen4_queue_flip(struct drm_device *dev,
  8007. struct drm_crtc *crtc,
  8008. struct drm_framebuffer *fb,
  8009. struct drm_i915_gem_object *obj,
  8010. struct intel_engine_cs *ring,
  8011. uint32_t flags)
  8012. {
  8013. struct drm_i915_private *dev_priv = dev->dev_private;
  8014. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8015. uint32_t pf, pipesrc;
  8016. int ret;
  8017. ret = intel_ring_begin(ring, 4);
  8018. if (ret)
  8019. return ret;
  8020. /* i965+ uses the linear or tiled offsets from the
  8021. * Display Registers (which do not change across a page-flip)
  8022. * so we need only reprogram the base address.
  8023. */
  8024. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  8025. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  8026. intel_ring_emit(ring, fb->pitches[0]);
  8027. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  8028. obj->tiling_mode);
  8029. /* XXX Enabling the panel-fitter across page-flip is so far
  8030. * untested on non-native modes, so ignore it for now.
  8031. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  8032. */
  8033. pf = 0;
  8034. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  8035. intel_ring_emit(ring, pf | pipesrc);
  8036. intel_mark_page_flip_active(intel_crtc);
  8037. __intel_ring_advance(ring);
  8038. return 0;
  8039. }
  8040. static int intel_gen6_queue_flip(struct drm_device *dev,
  8041. struct drm_crtc *crtc,
  8042. struct drm_framebuffer *fb,
  8043. struct drm_i915_gem_object *obj,
  8044. struct intel_engine_cs *ring,
  8045. uint32_t flags)
  8046. {
  8047. struct drm_i915_private *dev_priv = dev->dev_private;
  8048. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8049. uint32_t pf, pipesrc;
  8050. int ret;
  8051. ret = intel_ring_begin(ring, 4);
  8052. if (ret)
  8053. return ret;
  8054. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  8055. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  8056. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  8057. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8058. /* Contrary to the suggestions in the documentation,
  8059. * "Enable Panel Fitter" does not seem to be required when page
  8060. * flipping with a non-native mode, and worse causes a normal
  8061. * modeset to fail.
  8062. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  8063. */
  8064. pf = 0;
  8065. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  8066. intel_ring_emit(ring, pf | pipesrc);
  8067. intel_mark_page_flip_active(intel_crtc);
  8068. __intel_ring_advance(ring);
  8069. return 0;
  8070. }
  8071. static int intel_gen7_queue_flip(struct drm_device *dev,
  8072. struct drm_crtc *crtc,
  8073. struct drm_framebuffer *fb,
  8074. struct drm_i915_gem_object *obj,
  8075. struct intel_engine_cs *ring,
  8076. uint32_t flags)
  8077. {
  8078. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8079. uint32_t plane_bit = 0;
  8080. int len, ret;
  8081. switch (intel_crtc->plane) {
  8082. case PLANE_A:
  8083. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  8084. break;
  8085. case PLANE_B:
  8086. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  8087. break;
  8088. case PLANE_C:
  8089. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  8090. break;
  8091. default:
  8092. WARN_ONCE(1, "unknown plane in flip command\n");
  8093. return -ENODEV;
  8094. }
  8095. len = 4;
  8096. if (ring->id == RCS) {
  8097. len += 6;
  8098. /*
  8099. * On Gen 8, SRM is now taking an extra dword to accommodate
  8100. * 48bits addresses, and we need a NOOP for the batch size to
  8101. * stay even.
  8102. */
  8103. if (IS_GEN8(dev))
  8104. len += 2;
  8105. }
  8106. /*
  8107. * BSpec MI_DISPLAY_FLIP for IVB:
  8108. * "The full packet must be contained within the same cache line."
  8109. *
  8110. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  8111. * cacheline, if we ever start emitting more commands before
  8112. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  8113. * then do the cacheline alignment, and finally emit the
  8114. * MI_DISPLAY_FLIP.
  8115. */
  8116. ret = intel_ring_cacheline_align(ring);
  8117. if (ret)
  8118. return ret;
  8119. ret = intel_ring_begin(ring, len);
  8120. if (ret)
  8121. return ret;
  8122. /* Unmask the flip-done completion message. Note that the bspec says that
  8123. * we should do this for both the BCS and RCS, and that we must not unmask
  8124. * more than one flip event at any time (or ensure that one flip message
  8125. * can be sent by waiting for flip-done prior to queueing new flips).
  8126. * Experimentation says that BCS works despite DERRMR masking all
  8127. * flip-done completion events and that unmasking all planes at once
  8128. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  8129. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  8130. */
  8131. if (ring->id == RCS) {
  8132. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  8133. intel_ring_emit(ring, DERRMR);
  8134. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  8135. DERRMR_PIPEB_PRI_FLIP_DONE |
  8136. DERRMR_PIPEC_PRI_FLIP_DONE));
  8137. if (IS_GEN8(dev))
  8138. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  8139. MI_SRM_LRM_GLOBAL_GTT);
  8140. else
  8141. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  8142. MI_SRM_LRM_GLOBAL_GTT);
  8143. intel_ring_emit(ring, DERRMR);
  8144. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  8145. if (IS_GEN8(dev)) {
  8146. intel_ring_emit(ring, 0);
  8147. intel_ring_emit(ring, MI_NOOP);
  8148. }
  8149. }
  8150. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  8151. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  8152. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8153. intel_ring_emit(ring, (MI_NOOP));
  8154. intel_mark_page_flip_active(intel_crtc);
  8155. __intel_ring_advance(ring);
  8156. return 0;
  8157. }
  8158. static bool use_mmio_flip(struct intel_engine_cs *ring,
  8159. struct drm_i915_gem_object *obj)
  8160. {
  8161. /*
  8162. * This is not being used for older platforms, because
  8163. * non-availability of flip done interrupt forces us to use
  8164. * CS flips. Older platforms derive flip done using some clever
  8165. * tricks involving the flip_pending status bits and vblank irqs.
  8166. * So using MMIO flips there would disrupt this mechanism.
  8167. */
  8168. if (ring == NULL)
  8169. return true;
  8170. if (INTEL_INFO(ring->dev)->gen < 5)
  8171. return false;
  8172. if (i915.use_mmio_flip < 0)
  8173. return false;
  8174. else if (i915.use_mmio_flip > 0)
  8175. return true;
  8176. else if (i915.enable_execlists)
  8177. return true;
  8178. else
  8179. return ring != obj->ring;
  8180. }
  8181. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  8182. {
  8183. struct drm_device *dev = intel_crtc->base.dev;
  8184. struct drm_i915_private *dev_priv = dev->dev_private;
  8185. struct intel_framebuffer *intel_fb =
  8186. to_intel_framebuffer(intel_crtc->base.primary->fb);
  8187. struct drm_i915_gem_object *obj = intel_fb->obj;
  8188. u32 dspcntr;
  8189. u32 reg;
  8190. intel_mark_page_flip_active(intel_crtc);
  8191. reg = DSPCNTR(intel_crtc->plane);
  8192. dspcntr = I915_READ(reg);
  8193. if (INTEL_INFO(dev)->gen >= 4) {
  8194. if (obj->tiling_mode != I915_TILING_NONE)
  8195. dspcntr |= DISPPLANE_TILED;
  8196. else
  8197. dspcntr &= ~DISPPLANE_TILED;
  8198. }
  8199. I915_WRITE(reg, dspcntr);
  8200. I915_WRITE(DSPSURF(intel_crtc->plane),
  8201. intel_crtc->unpin_work->gtt_offset);
  8202. POSTING_READ(DSPSURF(intel_crtc->plane));
  8203. }
  8204. static int intel_postpone_flip(struct drm_i915_gem_object *obj)
  8205. {
  8206. struct intel_engine_cs *ring;
  8207. int ret;
  8208. lockdep_assert_held(&obj->base.dev->struct_mutex);
  8209. if (!obj->last_write_seqno)
  8210. return 0;
  8211. ring = obj->ring;
  8212. if (i915_seqno_passed(ring->get_seqno(ring, true),
  8213. obj->last_write_seqno))
  8214. return 0;
  8215. ret = i915_gem_check_olr(ring, obj->last_write_seqno);
  8216. if (ret)
  8217. return ret;
  8218. if (WARN_ON(!ring->irq_get(ring)))
  8219. return 0;
  8220. return 1;
  8221. }
  8222. void intel_notify_mmio_flip(struct intel_engine_cs *ring)
  8223. {
  8224. struct drm_i915_private *dev_priv = to_i915(ring->dev);
  8225. struct intel_crtc *intel_crtc;
  8226. unsigned long irq_flags;
  8227. u32 seqno;
  8228. seqno = ring->get_seqno(ring, false);
  8229. spin_lock_irqsave(&dev_priv->mmio_flip_lock, irq_flags);
  8230. for_each_intel_crtc(ring->dev, intel_crtc) {
  8231. struct intel_mmio_flip *mmio_flip;
  8232. mmio_flip = &intel_crtc->mmio_flip;
  8233. if (mmio_flip->seqno == 0)
  8234. continue;
  8235. if (ring->id != mmio_flip->ring_id)
  8236. continue;
  8237. if (i915_seqno_passed(seqno, mmio_flip->seqno)) {
  8238. intel_do_mmio_flip(intel_crtc);
  8239. mmio_flip->seqno = 0;
  8240. ring->irq_put(ring);
  8241. }
  8242. }
  8243. spin_unlock_irqrestore(&dev_priv->mmio_flip_lock, irq_flags);
  8244. }
  8245. static int intel_queue_mmio_flip(struct drm_device *dev,
  8246. struct drm_crtc *crtc,
  8247. struct drm_framebuffer *fb,
  8248. struct drm_i915_gem_object *obj,
  8249. struct intel_engine_cs *ring,
  8250. uint32_t flags)
  8251. {
  8252. struct drm_i915_private *dev_priv = dev->dev_private;
  8253. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8254. unsigned long irq_flags;
  8255. int ret;
  8256. if (WARN_ON(intel_crtc->mmio_flip.seqno))
  8257. return -EBUSY;
  8258. ret = intel_postpone_flip(obj);
  8259. if (ret < 0)
  8260. return ret;
  8261. if (ret == 0) {
  8262. intel_do_mmio_flip(intel_crtc);
  8263. return 0;
  8264. }
  8265. spin_lock_irqsave(&dev_priv->mmio_flip_lock, irq_flags);
  8266. intel_crtc->mmio_flip.seqno = obj->last_write_seqno;
  8267. intel_crtc->mmio_flip.ring_id = obj->ring->id;
  8268. spin_unlock_irqrestore(&dev_priv->mmio_flip_lock, irq_flags);
  8269. /*
  8270. * Double check to catch cases where irq fired before
  8271. * mmio flip data was ready
  8272. */
  8273. intel_notify_mmio_flip(obj->ring);
  8274. return 0;
  8275. }
  8276. static int intel_default_queue_flip(struct drm_device *dev,
  8277. struct drm_crtc *crtc,
  8278. struct drm_framebuffer *fb,
  8279. struct drm_i915_gem_object *obj,
  8280. struct intel_engine_cs *ring,
  8281. uint32_t flags)
  8282. {
  8283. return -ENODEV;
  8284. }
  8285. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  8286. struct drm_crtc *crtc)
  8287. {
  8288. struct drm_i915_private *dev_priv = dev->dev_private;
  8289. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8290. struct intel_unpin_work *work = intel_crtc->unpin_work;
  8291. u32 addr;
  8292. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  8293. return true;
  8294. if (!work->enable_stall_check)
  8295. return false;
  8296. if (work->flip_ready_vblank == 0) {
  8297. if (work->flip_queued_ring &&
  8298. !i915_seqno_passed(work->flip_queued_ring->get_seqno(work->flip_queued_ring, true),
  8299. work->flip_queued_seqno))
  8300. return false;
  8301. work->flip_ready_vblank = drm_vblank_count(dev, intel_crtc->pipe);
  8302. }
  8303. if (drm_vblank_count(dev, intel_crtc->pipe) - work->flip_ready_vblank < 3)
  8304. return false;
  8305. /* Potential stall - if we see that the flip has happened,
  8306. * assume a missed interrupt. */
  8307. if (INTEL_INFO(dev)->gen >= 4)
  8308. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  8309. else
  8310. addr = I915_READ(DSPADDR(intel_crtc->plane));
  8311. /* There is a potential issue here with a false positive after a flip
  8312. * to the same address. We could address this by checking for a
  8313. * non-incrementing frame counter.
  8314. */
  8315. return addr == work->gtt_offset;
  8316. }
  8317. void intel_check_page_flip(struct drm_device *dev, int pipe)
  8318. {
  8319. struct drm_i915_private *dev_priv = dev->dev_private;
  8320. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  8321. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8322. unsigned long flags;
  8323. if (crtc == NULL)
  8324. return;
  8325. spin_lock_irqsave(&dev->event_lock, flags);
  8326. if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
  8327. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  8328. intel_crtc->unpin_work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  8329. page_flip_completed(intel_crtc);
  8330. }
  8331. spin_unlock_irqrestore(&dev->event_lock, flags);
  8332. }
  8333. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  8334. struct drm_framebuffer *fb,
  8335. struct drm_pending_vblank_event *event,
  8336. uint32_t page_flip_flags)
  8337. {
  8338. struct drm_device *dev = crtc->dev;
  8339. struct drm_i915_private *dev_priv = dev->dev_private;
  8340. struct drm_framebuffer *old_fb = crtc->primary->fb;
  8341. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  8342. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8343. enum pipe pipe = intel_crtc->pipe;
  8344. struct intel_unpin_work *work;
  8345. struct intel_engine_cs *ring;
  8346. unsigned long flags;
  8347. int ret;
  8348. //trigger software GT busyness calculation
  8349. gen8_flip_interrupt(dev);
  8350. /*
  8351. * drm_mode_page_flip_ioctl() should already catch this, but double
  8352. * check to be safe. In the future we may enable pageflipping from
  8353. * a disabled primary plane.
  8354. */
  8355. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  8356. return -EBUSY;
  8357. /* Can't change pixel format via MI display flips. */
  8358. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  8359. return -EINVAL;
  8360. /*
  8361. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  8362. * Note that pitch changes could also affect these register.
  8363. */
  8364. if (INTEL_INFO(dev)->gen > 3 &&
  8365. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  8366. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  8367. return -EINVAL;
  8368. if (i915_terminally_wedged(&dev_priv->gpu_error))
  8369. goto out_hang;
  8370. work = kzalloc(sizeof(*work), GFP_KERNEL);
  8371. if (work == NULL)
  8372. return -ENOMEM;
  8373. work->event = event;
  8374. work->crtc = crtc;
  8375. work->old_fb_obj = intel_fb_obj(old_fb);
  8376. INIT_WORK(&work->work, intel_unpin_work_fn);
  8377. ret = drm_crtc_vblank_get(crtc);
  8378. if (ret)
  8379. goto free_work;
  8380. /* We borrow the event spin lock for protecting unpin_work */
  8381. spin_lock_irqsave(&dev->event_lock, flags);
  8382. if (intel_crtc->unpin_work) {
  8383. /* Before declaring the flip queue wedged, check if
  8384. * the hardware completed the operation behind our backs.
  8385. */
  8386. if (__intel_pageflip_stall_check(dev, crtc)) {
  8387. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  8388. page_flip_completed(intel_crtc);
  8389. } else {
  8390. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  8391. spin_unlock_irqrestore(&dev->event_lock, flags);
  8392. drm_crtc_vblank_put(crtc);
  8393. kfree(work);
  8394. return -EBUSY;
  8395. }
  8396. }
  8397. intel_crtc->unpin_work = work;
  8398. spin_unlock_irqrestore(&dev->event_lock, flags);
  8399. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  8400. flush_workqueue(dev_priv->wq);
  8401. ret = i915_mutex_lock_interruptible(dev);
  8402. if (ret)
  8403. goto cleanup;
  8404. /* Reference the objects for the scheduled work. */
  8405. drm_gem_object_reference(&work->old_fb_obj->base);
  8406. drm_gem_object_reference(&obj->base);
  8407. crtc->primary->fb = fb;
  8408. work->pending_flip_obj = obj;
  8409. atomic_inc(&intel_crtc->unpin_work_count);
  8410. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  8411. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  8412. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  8413. if (IS_VALLEYVIEW(dev)) {
  8414. ring = &dev_priv->ring[BCS];
  8415. if (obj->tiling_mode != work->old_fb_obj->tiling_mode)
  8416. /* vlv: DISPLAY_FLIP fails to change tiling */
  8417. ring = NULL;
  8418. } else if (IS_IVYBRIDGE(dev)) {
  8419. ring = &dev_priv->ring[BCS];
  8420. } else if (INTEL_INFO(dev)->gen >= 7) {
  8421. ring = obj->ring;
  8422. if (ring == NULL || ring->id != RCS)
  8423. ring = &dev_priv->ring[BCS];
  8424. } else {
  8425. ring = &dev_priv->ring[RCS];
  8426. }
  8427. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  8428. if (ret)
  8429. goto cleanup_pending;
  8430. work->gtt_offset =
  8431. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
  8432. if (use_mmio_flip(ring, obj)) {
  8433. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  8434. page_flip_flags);
  8435. if (ret)
  8436. goto cleanup_unpin;
  8437. work->flip_queued_seqno = obj->last_write_seqno;
  8438. work->flip_queued_ring = obj->ring;
  8439. } else {
  8440. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
  8441. page_flip_flags);
  8442. if (ret)
  8443. goto cleanup_unpin;
  8444. work->flip_queued_seqno = intel_ring_get_seqno(ring);
  8445. work->flip_queued_ring = ring;
  8446. }
  8447. work->flip_queued_vblank = drm_vblank_count(dev, intel_crtc->pipe);
  8448. work->enable_stall_check = true;
  8449. i915_gem_track_fb(work->old_fb_obj, obj,
  8450. INTEL_FRONTBUFFER_PRIMARY(pipe));
  8451. intel_disable_fbc(dev);
  8452. intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  8453. mutex_unlock(&dev->struct_mutex);
  8454. trace_i915_flip_request(intel_crtc->plane, obj);
  8455. return 0;
  8456. cleanup_unpin:
  8457. intel_unpin_fb_obj(obj);
  8458. cleanup_pending:
  8459. atomic_dec(&intel_crtc->unpin_work_count);
  8460. crtc->primary->fb = old_fb;
  8461. drm_gem_object_unreference(&work->old_fb_obj->base);
  8462. drm_gem_object_unreference(&obj->base);
  8463. mutex_unlock(&dev->struct_mutex);
  8464. cleanup:
  8465. spin_lock_irqsave(&dev->event_lock, flags);
  8466. intel_crtc->unpin_work = NULL;
  8467. spin_unlock_irqrestore(&dev->event_lock, flags);
  8468. drm_crtc_vblank_put(crtc);
  8469. free_work:
  8470. kfree(work);
  8471. if (ret == -EIO) {
  8472. out_hang:
  8473. intel_crtc_wait_for_pending_flips(crtc);
  8474. ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
  8475. if (ret == 0 && event)
  8476. drm_send_vblank_event(dev, pipe, event);
  8477. }
  8478. return ret;
  8479. }
  8480. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  8481. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  8482. .load_lut = intel_crtc_load_lut,
  8483. };
  8484. /**
  8485. * intel_modeset_update_staged_output_state
  8486. *
  8487. * Updates the staged output configuration state, e.g. after we've read out the
  8488. * current hw state.
  8489. */
  8490. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  8491. {
  8492. struct intel_crtc *crtc;
  8493. struct intel_encoder *encoder;
  8494. struct intel_connector *connector;
  8495. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8496. base.head) {
  8497. connector->new_encoder =
  8498. to_intel_encoder(connector->base.encoder);
  8499. }
  8500. for_each_intel_encoder(dev, encoder) {
  8501. encoder->new_crtc =
  8502. to_intel_crtc(encoder->base.crtc);
  8503. }
  8504. for_each_intel_crtc(dev, crtc) {
  8505. crtc->new_enabled = crtc->base.enabled;
  8506. if (crtc->new_enabled)
  8507. crtc->new_config = &crtc->config;
  8508. else
  8509. crtc->new_config = NULL;
  8510. }
  8511. }
  8512. /**
  8513. * intel_modeset_commit_output_state
  8514. *
  8515. * This function copies the stage display pipe configuration to the real one.
  8516. */
  8517. static void intel_modeset_commit_output_state(struct drm_device *dev)
  8518. {
  8519. struct intel_crtc *crtc;
  8520. struct intel_encoder *encoder;
  8521. struct intel_connector *connector;
  8522. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8523. base.head) {
  8524. connector->base.encoder = &connector->new_encoder->base;
  8525. }
  8526. for_each_intel_encoder(dev, encoder) {
  8527. encoder->base.crtc = &encoder->new_crtc->base;
  8528. }
  8529. for_each_intel_crtc(dev, crtc) {
  8530. crtc->base.enabled = crtc->new_enabled;
  8531. }
  8532. }
  8533. static void
  8534. connected_sink_compute_bpp(struct intel_connector *connector,
  8535. struct intel_crtc_config *pipe_config)
  8536. {
  8537. int bpp = pipe_config->pipe_bpp;
  8538. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8539. connector->base.base.id,
  8540. connector->base.name);
  8541. /* Don't use an invalid EDID bpc value */
  8542. if (connector->base.display_info.bpc &&
  8543. connector->base.display_info.bpc * 3 < bpp) {
  8544. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8545. bpp, connector->base.display_info.bpc*3);
  8546. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  8547. }
  8548. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8549. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  8550. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8551. bpp);
  8552. pipe_config->pipe_bpp = 24;
  8553. }
  8554. }
  8555. static int
  8556. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8557. struct drm_framebuffer *fb,
  8558. struct intel_crtc_config *pipe_config)
  8559. {
  8560. struct drm_device *dev = crtc->base.dev;
  8561. struct intel_connector *connector;
  8562. int bpp;
  8563. switch (fb->pixel_format) {
  8564. case DRM_FORMAT_C8:
  8565. bpp = 8*3; /* since we go through a colormap */
  8566. break;
  8567. case DRM_FORMAT_XRGB1555:
  8568. case DRM_FORMAT_ARGB1555:
  8569. /* checked in intel_framebuffer_init already */
  8570. if (WARN_ON(INTEL_INFO(dev)->gen > 3))
  8571. return -EINVAL;
  8572. case DRM_FORMAT_RGB565:
  8573. bpp = 6*3; /* min is 18bpp */
  8574. break;
  8575. case DRM_FORMAT_XBGR8888:
  8576. case DRM_FORMAT_ABGR8888:
  8577. /* checked in intel_framebuffer_init already */
  8578. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8579. return -EINVAL;
  8580. case DRM_FORMAT_XRGB8888:
  8581. case DRM_FORMAT_ARGB8888:
  8582. bpp = 8*3;
  8583. break;
  8584. case DRM_FORMAT_XRGB2101010:
  8585. case DRM_FORMAT_ARGB2101010:
  8586. case DRM_FORMAT_XBGR2101010:
  8587. case DRM_FORMAT_ABGR2101010:
  8588. /* checked in intel_framebuffer_init already */
  8589. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8590. return -EINVAL;
  8591. bpp = 10*3;
  8592. break;
  8593. /* TODO: gen4+ supports 16 bpc floating point, too. */
  8594. default:
  8595. DRM_DEBUG_KMS("unsupported depth\n");
  8596. return -EINVAL;
  8597. }
  8598. pipe_config->pipe_bpp = bpp;
  8599. /* Clamp display bpp to EDID value */
  8600. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8601. base.head) {
  8602. if (!connector->new_encoder ||
  8603. connector->new_encoder->new_crtc != crtc)
  8604. continue;
  8605. connected_sink_compute_bpp(connector, pipe_config);
  8606. }
  8607. return bpp;
  8608. }
  8609. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  8610. {
  8611. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  8612. "type: 0x%x flags: 0x%x\n",
  8613. mode->crtc_clock,
  8614. mode->crtc_hdisplay, mode->crtc_hsync_start,
  8615. mode->crtc_hsync_end, mode->crtc_htotal,
  8616. mode->crtc_vdisplay, mode->crtc_vsync_start,
  8617. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  8618. }
  8619. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  8620. struct intel_crtc_config *pipe_config,
  8621. const char *context)
  8622. {
  8623. DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
  8624. context, pipe_name(crtc->pipe));
  8625. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  8626. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  8627. pipe_config->pipe_bpp, pipe_config->dither);
  8628. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8629. pipe_config->has_pch_encoder,
  8630. pipe_config->fdi_lanes,
  8631. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  8632. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  8633. pipe_config->fdi_m_n.tu);
  8634. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8635. pipe_config->has_dp_encoder,
  8636. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  8637. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  8638. pipe_config->dp_m_n.tu);
  8639. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  8640. pipe_config->has_dp_encoder,
  8641. pipe_config->dp_m2_n2.gmch_m,
  8642. pipe_config->dp_m2_n2.gmch_n,
  8643. pipe_config->dp_m2_n2.link_m,
  8644. pipe_config->dp_m2_n2.link_n,
  8645. pipe_config->dp_m2_n2.tu);
  8646. DRM_DEBUG_KMS("requested mode:\n");
  8647. drm_mode_debug_printmodeline(&pipe_config->requested_mode);
  8648. DRM_DEBUG_KMS("adjusted mode:\n");
  8649. drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
  8650. intel_dump_crtc_timings(&pipe_config->adjusted_mode);
  8651. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  8652. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  8653. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  8654. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  8655. pipe_config->gmch_pfit.control,
  8656. pipe_config->gmch_pfit.pgm_ratios,
  8657. pipe_config->gmch_pfit.lvds_border_bits);
  8658. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  8659. pipe_config->pch_pfit.pos,
  8660. pipe_config->pch_pfit.size,
  8661. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  8662. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  8663. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  8664. }
  8665. static bool encoders_cloneable(const struct intel_encoder *a,
  8666. const struct intel_encoder *b)
  8667. {
  8668. /* masks could be asymmetric, so check both ways */
  8669. return a == b || (a->cloneable & (1 << b->type) &&
  8670. b->cloneable & (1 << a->type));
  8671. }
  8672. static bool check_single_encoder_cloning(struct intel_crtc *crtc,
  8673. struct intel_encoder *encoder)
  8674. {
  8675. struct drm_device *dev = crtc->base.dev;
  8676. struct intel_encoder *source_encoder;
  8677. for_each_intel_encoder(dev, source_encoder) {
  8678. if (source_encoder->new_crtc != crtc)
  8679. continue;
  8680. if (!encoders_cloneable(encoder, source_encoder))
  8681. return false;
  8682. }
  8683. return true;
  8684. }
  8685. static bool check_encoder_cloning(struct intel_crtc *crtc)
  8686. {
  8687. struct drm_device *dev = crtc->base.dev;
  8688. struct intel_encoder *encoder;
  8689. for_each_intel_encoder(dev, encoder) {
  8690. if (encoder->new_crtc != crtc)
  8691. continue;
  8692. if (!check_single_encoder_cloning(crtc, encoder))
  8693. return false;
  8694. }
  8695. return true;
  8696. }
  8697. static struct intel_crtc_config *
  8698. intel_modeset_pipe_config(struct drm_crtc *crtc,
  8699. struct drm_framebuffer *fb,
  8700. struct drm_display_mode *mode)
  8701. {
  8702. struct drm_device *dev = crtc->dev;
  8703. struct intel_encoder *encoder;
  8704. struct intel_crtc_config *pipe_config;
  8705. int plane_bpp, ret = -EINVAL;
  8706. bool retry = true;
  8707. if (!check_encoder_cloning(to_intel_crtc(crtc))) {
  8708. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  8709. return ERR_PTR(-EINVAL);
  8710. }
  8711. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8712. if (!pipe_config)
  8713. return ERR_PTR(-ENOMEM);
  8714. drm_mode_copy(&pipe_config->adjusted_mode, mode);
  8715. drm_mode_copy(&pipe_config->requested_mode, mode);
  8716. pipe_config->cpu_transcoder =
  8717. (enum transcoder) to_intel_crtc(crtc)->pipe;
  8718. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8719. /*
  8720. * Sanitize sync polarity flags based on requested ones. If neither
  8721. * positive or negative polarity is requested, treat this as meaning
  8722. * negative polarity.
  8723. */
  8724. if (!(pipe_config->adjusted_mode.flags &
  8725. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  8726. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  8727. if (!(pipe_config->adjusted_mode.flags &
  8728. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  8729. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  8730. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  8731. * plane pixel format and any sink constraints into account. Returns the
  8732. * source plane bpp so that dithering can be selected on mismatches
  8733. * after encoders and crtc also have had their say. */
  8734. plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  8735. fb, pipe_config);
  8736. if (plane_bpp < 0)
  8737. goto fail;
  8738. /*
  8739. * Determine the real pipe dimensions. Note that stereo modes can
  8740. * increase the actual pipe size due to the frame doubling and
  8741. * insertion of additional space for blanks between the frame. This
  8742. * is stored in the crtc timings. We use the requested mode to do this
  8743. * computation to clearly distinguish it from the adjusted mode, which
  8744. * can be changed by the connectors in the below retry loop.
  8745. */
  8746. drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
  8747. pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
  8748. pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
  8749. encoder_retry:
  8750. /* Ensure the port clock defaults are reset when retrying. */
  8751. pipe_config->port_clock = 0;
  8752. pipe_config->pixel_multiplier = 1;
  8753. /* Fill in default crtc timings, allow encoders to overwrite them. */
  8754. drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
  8755. /* Pass our mode to the connectors and the CRTC to give them a chance to
  8756. * adjust it according to limitations or connector properties, and also
  8757. * a chance to reject the mode entirely.
  8758. */
  8759. for_each_intel_encoder(dev, encoder) {
  8760. if (&encoder->new_crtc->base != crtc)
  8761. continue;
  8762. if (!(encoder->compute_config(encoder, pipe_config))) {
  8763. DRM_DEBUG_KMS("Encoder config failure\n");
  8764. goto fail;
  8765. }
  8766. }
  8767. /* Set default port clock if not overwritten by the encoder. Needs to be
  8768. * done afterwards in case the encoder adjusts the mode. */
  8769. if (!pipe_config->port_clock)
  8770. pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
  8771. * pipe_config->pixel_multiplier;
  8772. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  8773. if (ret < 0) {
  8774. DRM_DEBUG_KMS("CRTC fixup failed\n");
  8775. goto fail;
  8776. }
  8777. if (ret == RETRY) {
  8778. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  8779. ret = -EINVAL;
  8780. goto fail;
  8781. }
  8782. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  8783. retry = false;
  8784. goto encoder_retry;
  8785. }
  8786. pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
  8787. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  8788. plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  8789. return pipe_config;
  8790. fail:
  8791. kfree(pipe_config);
  8792. return ERR_PTR(ret);
  8793. }
  8794. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  8795. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  8796. static void
  8797. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  8798. unsigned *prepare_pipes, unsigned *disable_pipes)
  8799. {
  8800. struct intel_crtc *intel_crtc;
  8801. struct drm_device *dev = crtc->dev;
  8802. struct intel_encoder *encoder;
  8803. struct intel_connector *connector;
  8804. struct drm_crtc *tmp_crtc;
  8805. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  8806. /* Check which crtcs have changed outputs connected to them, these need
  8807. * to be part of the prepare_pipes mask. We don't (yet) support global
  8808. * modeset across multiple crtcs, so modeset_pipes will only have one
  8809. * bit set at most. */
  8810. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8811. base.head) {
  8812. if (connector->base.encoder == &connector->new_encoder->base)
  8813. continue;
  8814. if (connector->base.encoder) {
  8815. tmp_crtc = connector->base.encoder->crtc;
  8816. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8817. }
  8818. if (connector->new_encoder)
  8819. *prepare_pipes |=
  8820. 1 << connector->new_encoder->new_crtc->pipe;
  8821. }
  8822. for_each_intel_encoder(dev, encoder) {
  8823. if (encoder->base.crtc == &encoder->new_crtc->base)
  8824. continue;
  8825. if (encoder->base.crtc) {
  8826. tmp_crtc = encoder->base.crtc;
  8827. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8828. }
  8829. if (encoder->new_crtc)
  8830. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  8831. }
  8832. /* Check for pipes that will be enabled/disabled ... */
  8833. for_each_intel_crtc(dev, intel_crtc) {
  8834. if (intel_crtc->base.enabled == intel_crtc->new_enabled)
  8835. continue;
  8836. if (!intel_crtc->new_enabled)
  8837. *disable_pipes |= 1 << intel_crtc->pipe;
  8838. else
  8839. *prepare_pipes |= 1 << intel_crtc->pipe;
  8840. }
  8841. /* set_mode is also used to update properties on life display pipes. */
  8842. intel_crtc = to_intel_crtc(crtc);
  8843. if (intel_crtc->new_enabled)
  8844. *prepare_pipes |= 1 << intel_crtc->pipe;
  8845. /*
  8846. * For simplicity do a full modeset on any pipe where the output routing
  8847. * changed. We could be more clever, but that would require us to be
  8848. * more careful with calling the relevant encoder->mode_set functions.
  8849. */
  8850. if (*prepare_pipes)
  8851. *modeset_pipes = *prepare_pipes;
  8852. /* ... and mask these out. */
  8853. *modeset_pipes &= ~(*disable_pipes);
  8854. *prepare_pipes &= ~(*disable_pipes);
  8855. /*
  8856. * HACK: We don't (yet) fully support global modesets. intel_set_config
  8857. * obies this rule, but the modeset restore mode of
  8858. * intel_modeset_setup_hw_state does not.
  8859. */
  8860. *modeset_pipes &= 1 << intel_crtc->pipe;
  8861. *prepare_pipes &= 1 << intel_crtc->pipe;
  8862. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  8863. *modeset_pipes, *prepare_pipes, *disable_pipes);
  8864. }
  8865. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  8866. {
  8867. struct drm_encoder *encoder;
  8868. struct drm_device *dev = crtc->dev;
  8869. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  8870. if (encoder->crtc == crtc)
  8871. return true;
  8872. return false;
  8873. }
  8874. static void
  8875. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  8876. {
  8877. struct intel_encoder *intel_encoder;
  8878. struct intel_crtc *intel_crtc;
  8879. struct drm_connector *connector;
  8880. for_each_intel_encoder(dev, intel_encoder) {
  8881. if (!intel_encoder->base.crtc)
  8882. continue;
  8883. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  8884. if (prepare_pipes & (1 << intel_crtc->pipe))
  8885. intel_encoder->connectors_active = false;
  8886. }
  8887. intel_modeset_commit_output_state(dev);
  8888. /* Double check state. */
  8889. for_each_intel_crtc(dev, intel_crtc) {
  8890. WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
  8891. WARN_ON(intel_crtc->new_config &&
  8892. intel_crtc->new_config != &intel_crtc->config);
  8893. WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
  8894. }
  8895. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  8896. if (!connector->encoder || !connector->encoder->crtc)
  8897. continue;
  8898. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  8899. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  8900. struct drm_property *dpms_property =
  8901. dev->mode_config.dpms_property;
  8902. connector->dpms = DRM_MODE_DPMS_ON;
  8903. drm_object_property_set_value(&connector->base,
  8904. dpms_property,
  8905. DRM_MODE_DPMS_ON);
  8906. intel_encoder = to_intel_encoder(connector->encoder);
  8907. intel_encoder->connectors_active = true;
  8908. }
  8909. }
  8910. }
  8911. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  8912. {
  8913. int diff;
  8914. if (clock1 == clock2)
  8915. return true;
  8916. if (!clock1 || !clock2)
  8917. return false;
  8918. diff = abs(clock1 - clock2);
  8919. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  8920. return true;
  8921. return false;
  8922. }
  8923. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  8924. list_for_each_entry((intel_crtc), \
  8925. &(dev)->mode_config.crtc_list, \
  8926. base.head) \
  8927. if (mask & (1 <<(intel_crtc)->pipe))
  8928. static bool
  8929. intel_pipe_config_compare(struct drm_device *dev,
  8930. struct intel_crtc_config *current_config,
  8931. struct intel_crtc_config *pipe_config)
  8932. {
  8933. #define PIPE_CONF_CHECK_X(name) \
  8934. if (current_config->name != pipe_config->name) { \
  8935. DRM_ERROR("mismatch in " #name " " \
  8936. "(expected 0x%08x, found 0x%08x)\n", \
  8937. current_config->name, \
  8938. pipe_config->name); \
  8939. return false; \
  8940. }
  8941. #define PIPE_CONF_CHECK_I(name) \
  8942. if (current_config->name != pipe_config->name) { \
  8943. DRM_ERROR("mismatch in " #name " " \
  8944. "(expected %i, found %i)\n", \
  8945. current_config->name, \
  8946. pipe_config->name); \
  8947. return false; \
  8948. }
  8949. /* This is required for BDW+ where there is only one set of registers for
  8950. * switching between high and low RR.
  8951. * This macro can be used whenever a comparison has to be made between one
  8952. * hw state and multiple sw state variables.
  8953. */
  8954. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  8955. if ((current_config->name != pipe_config->name) && \
  8956. (current_config->alt_name != pipe_config->name)) { \
  8957. DRM_ERROR("mismatch in " #name " " \
  8958. "(expected %i or %i, found %i)\n", \
  8959. current_config->name, \
  8960. current_config->alt_name, \
  8961. pipe_config->name); \
  8962. return false; \
  8963. }
  8964. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  8965. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  8966. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  8967. "(expected %i, found %i)\n", \
  8968. current_config->name & (mask), \
  8969. pipe_config->name & (mask)); \
  8970. return false; \
  8971. }
  8972. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  8973. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  8974. DRM_ERROR("mismatch in " #name " " \
  8975. "(expected %i, found %i)\n", \
  8976. current_config->name, \
  8977. pipe_config->name); \
  8978. return false; \
  8979. }
  8980. #define PIPE_CONF_QUIRK(quirk) \
  8981. ((current_config->quirks | pipe_config->quirks) & (quirk))
  8982. PIPE_CONF_CHECK_I(cpu_transcoder);
  8983. PIPE_CONF_CHECK_I(has_pch_encoder);
  8984. PIPE_CONF_CHECK_I(fdi_lanes);
  8985. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  8986. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  8987. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  8988. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  8989. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  8990. PIPE_CONF_CHECK_I(has_dp_encoder);
  8991. if (INTEL_INFO(dev)->gen < 8) {
  8992. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  8993. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  8994. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  8995. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  8996. PIPE_CONF_CHECK_I(dp_m_n.tu);
  8997. if (current_config->has_drrs) {
  8998. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
  8999. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
  9000. PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
  9001. PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
  9002. PIPE_CONF_CHECK_I(dp_m2_n2.tu);
  9003. }
  9004. } else {
  9005. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
  9006. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
  9007. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
  9008. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
  9009. PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
  9010. }
  9011. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
  9012. PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
  9013. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
  9014. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
  9015. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
  9016. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
  9017. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
  9018. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
  9019. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
  9020. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
  9021. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
  9022. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
  9023. PIPE_CONF_CHECK_I(pixel_multiplier);
  9024. PIPE_CONF_CHECK_I(has_hdmi_sink);
  9025. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  9026. IS_VALLEYVIEW(dev))
  9027. PIPE_CONF_CHECK_I(limited_color_range);
  9028. PIPE_CONF_CHECK_I(has_audio);
  9029. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  9030. DRM_MODE_FLAG_INTERLACE);
  9031. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  9032. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  9033. DRM_MODE_FLAG_PHSYNC);
  9034. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  9035. DRM_MODE_FLAG_NHSYNC);
  9036. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  9037. DRM_MODE_FLAG_PVSYNC);
  9038. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  9039. DRM_MODE_FLAG_NVSYNC);
  9040. }
  9041. PIPE_CONF_CHECK_I(pipe_src_w);
  9042. PIPE_CONF_CHECK_I(pipe_src_h);
  9043. /*
  9044. * FIXME: BIOS likes to set up a cloned config with lvds+external
  9045. * screen. Since we don't yet re-compute the pipe config when moving
  9046. * just the lvds port away to another pipe the sw tracking won't match.
  9047. *
  9048. * Proper atomic modesets with recomputed global state will fix this.
  9049. * Until then just don't check gmch state for inherited modes.
  9050. */
  9051. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  9052. PIPE_CONF_CHECK_I(gmch_pfit.control);
  9053. /* pfit ratios are autocomputed by the hw on gen4+ */
  9054. if (INTEL_INFO(dev)->gen < 4)
  9055. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  9056. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  9057. }
  9058. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  9059. if (current_config->pch_pfit.enabled) {
  9060. PIPE_CONF_CHECK_I(pch_pfit.pos);
  9061. PIPE_CONF_CHECK_I(pch_pfit.size);
  9062. }
  9063. /* BDW+ don't expose a synchronous way to read the state */
  9064. if (IS_HASWELL(dev))
  9065. PIPE_CONF_CHECK_I(ips_enabled);
  9066. PIPE_CONF_CHECK_I(double_wide);
  9067. PIPE_CONF_CHECK_X(ddi_pll_sel);
  9068. PIPE_CONF_CHECK_I(shared_dpll);
  9069. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  9070. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  9071. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  9072. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  9073. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  9074. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  9075. PIPE_CONF_CHECK_I(pipe_bpp);
  9076. PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
  9077. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  9078. #undef PIPE_CONF_CHECK_X
  9079. #undef PIPE_CONF_CHECK_I
  9080. #undef PIPE_CONF_CHECK_I_ALT
  9081. #undef PIPE_CONF_CHECK_FLAGS
  9082. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  9083. #undef PIPE_CONF_QUIRK
  9084. return true;
  9085. }
  9086. static void
  9087. check_connector_state(struct drm_device *dev)
  9088. {
  9089. struct intel_connector *connector;
  9090. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9091. base.head) {
  9092. /* This also checks the encoder/connector hw state with the
  9093. * ->get_hw_state callbacks. */
  9094. intel_connector_check_state(connector);
  9095. WARN(&connector->new_encoder->base != connector->base.encoder,
  9096. "connector's staged encoder doesn't match current encoder\n");
  9097. }
  9098. }
  9099. static void
  9100. check_encoder_state(struct drm_device *dev)
  9101. {
  9102. struct intel_encoder *encoder;
  9103. struct intel_connector *connector;
  9104. for_each_intel_encoder(dev, encoder) {
  9105. bool enabled = false;
  9106. bool active = false;
  9107. enum pipe pipe, tracked_pipe;
  9108. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  9109. encoder->base.base.id,
  9110. encoder->base.name);
  9111. WARN(&encoder->new_crtc->base != encoder->base.crtc,
  9112. "encoder's stage crtc doesn't match current crtc\n");
  9113. WARN(encoder->connectors_active && !encoder->base.crtc,
  9114. "encoder's active_connectors set, but no crtc\n");
  9115. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9116. base.head) {
  9117. if (connector->base.encoder != &encoder->base)
  9118. continue;
  9119. enabled = true;
  9120. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  9121. active = true;
  9122. }
  9123. /*
  9124. * for MST connectors if we unplug the connector is gone
  9125. * away but the encoder is still connected to a crtc
  9126. * until a modeset happens in response to the hotplug.
  9127. */
  9128. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  9129. continue;
  9130. WARN(!!encoder->base.crtc != enabled,
  9131. "encoder's enabled state mismatch "
  9132. "(expected %i, found %i)\n",
  9133. !!encoder->base.crtc, enabled);
  9134. WARN(active && !encoder->base.crtc,
  9135. "active encoder with no crtc\n");
  9136. WARN(encoder->connectors_active != active,
  9137. "encoder's computed active state doesn't match tracked active state "
  9138. "(expected %i, found %i)\n", active, encoder->connectors_active);
  9139. active = encoder->get_hw_state(encoder, &pipe);
  9140. WARN(active != encoder->connectors_active,
  9141. "encoder's hw state doesn't match sw tracking "
  9142. "(expected %i, found %i)\n",
  9143. encoder->connectors_active, active);
  9144. if (!encoder->base.crtc)
  9145. continue;
  9146. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  9147. WARN(active && pipe != tracked_pipe,
  9148. "active encoder's pipe doesn't match"
  9149. "(expected %i, found %i)\n",
  9150. tracked_pipe, pipe);
  9151. }
  9152. }
  9153. static void
  9154. check_crtc_state(struct drm_device *dev)
  9155. {
  9156. struct drm_i915_private *dev_priv = dev->dev_private;
  9157. struct intel_crtc *crtc;
  9158. struct intel_encoder *encoder;
  9159. struct intel_crtc_config pipe_config;
  9160. for_each_intel_crtc(dev, crtc) {
  9161. bool enabled = false;
  9162. bool active = false;
  9163. memset(&pipe_config, 0, sizeof(pipe_config));
  9164. DRM_DEBUG_KMS("[CRTC:%d]\n",
  9165. crtc->base.base.id);
  9166. WARN(crtc->active && !crtc->base.enabled,
  9167. "active crtc, but not enabled in sw tracking\n");
  9168. for_each_intel_encoder(dev, encoder) {
  9169. if (encoder->base.crtc != &crtc->base)
  9170. continue;
  9171. enabled = true;
  9172. if (encoder->connectors_active)
  9173. active = true;
  9174. }
  9175. WARN(active != crtc->active,
  9176. "crtc's computed active state doesn't match tracked active state "
  9177. "(expected %i, found %i)\n", active, crtc->active);
  9178. WARN(enabled != crtc->base.enabled,
  9179. "crtc's computed enabled state doesn't match tracked enabled state "
  9180. "(expected %i, found %i)\n", enabled, crtc->base.enabled);
  9181. active = dev_priv->display.get_pipe_config(crtc,
  9182. &pipe_config);
  9183. /* hw state is inconsistent with the pipe quirk */
  9184. if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  9185. (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  9186. active = crtc->active;
  9187. for_each_intel_encoder(dev, encoder) {
  9188. enum pipe pipe;
  9189. if (encoder->base.crtc != &crtc->base)
  9190. continue;
  9191. if (encoder->get_hw_state(encoder, &pipe))
  9192. encoder->get_config(encoder, &pipe_config);
  9193. }
  9194. WARN(crtc->active != active,
  9195. "crtc active state doesn't match with hw state "
  9196. "(expected %i, found %i)\n", crtc->active, active);
  9197. if (active &&
  9198. !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
  9199. WARN(1, "pipe state doesn't match!\n");
  9200. intel_dump_pipe_config(crtc, &pipe_config,
  9201. "[hw state]");
  9202. intel_dump_pipe_config(crtc, &crtc->config,
  9203. "[sw state]");
  9204. }
  9205. }
  9206. }
  9207. static void
  9208. check_shared_dpll_state(struct drm_device *dev)
  9209. {
  9210. struct drm_i915_private *dev_priv = dev->dev_private;
  9211. struct intel_crtc *crtc;
  9212. struct intel_dpll_hw_state dpll_hw_state;
  9213. int i;
  9214. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9215. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  9216. int enabled_crtcs = 0, active_crtcs = 0;
  9217. bool active;
  9218. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  9219. DRM_DEBUG_KMS("%s\n", pll->name);
  9220. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  9221. WARN(pll->active > pll->refcount,
  9222. "more active pll users than references: %i vs %i\n",
  9223. pll->active, pll->refcount);
  9224. WARN(pll->active && !pll->on,
  9225. "pll in active use but not on in sw tracking\n");
  9226. WARN(pll->on && !pll->active,
  9227. "pll in on but not on in use in sw tracking\n");
  9228. WARN(pll->on != active,
  9229. "pll on state mismatch (expected %i, found %i)\n",
  9230. pll->on, active);
  9231. for_each_intel_crtc(dev, crtc) {
  9232. if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
  9233. enabled_crtcs++;
  9234. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  9235. active_crtcs++;
  9236. }
  9237. WARN(pll->active != active_crtcs,
  9238. "pll active crtcs mismatch (expected %i, found %i)\n",
  9239. pll->active, active_crtcs);
  9240. WARN(pll->refcount != enabled_crtcs,
  9241. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  9242. pll->refcount, enabled_crtcs);
  9243. WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
  9244. sizeof(dpll_hw_state)),
  9245. "pll hw state mismatch\n");
  9246. }
  9247. }
  9248. void
  9249. intel_modeset_check_state(struct drm_device *dev)
  9250. {
  9251. check_connector_state(dev);
  9252. check_encoder_state(dev);
  9253. check_crtc_state(dev);
  9254. check_shared_dpll_state(dev);
  9255. }
  9256. void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
  9257. int dotclock)
  9258. {
  9259. /*
  9260. * FDI already provided one idea for the dotclock.
  9261. * Yell if the encoder disagrees.
  9262. */
  9263. WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
  9264. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9265. pipe_config->adjusted_mode.crtc_clock, dotclock);
  9266. }
  9267. static void update_scanline_offset(struct intel_crtc *crtc)
  9268. {
  9269. struct drm_device *dev = crtc->base.dev;
  9270. /*
  9271. * The scanline counter increments at the leading edge of hsync.
  9272. *
  9273. * On most platforms it starts counting from vtotal-1 on the
  9274. * first active line. That means the scanline counter value is
  9275. * always one less than what we would expect. Ie. just after
  9276. * start of vblank, which also occurs at start of hsync (on the
  9277. * last active line), the scanline counter will read vblank_start-1.
  9278. *
  9279. * On gen2 the scanline counter starts counting from 1 instead
  9280. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  9281. * to keep the value positive), instead of adding one.
  9282. *
  9283. * On HSW+ the behaviour of the scanline counter depends on the output
  9284. * type. For DP ports it behaves like most other platforms, but on HDMI
  9285. * there's an extra 1 line difference. So we need to add two instead of
  9286. * one to the value.
  9287. */
  9288. if (IS_GEN2(dev)) {
  9289. const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
  9290. int vtotal;
  9291. vtotal = mode->crtc_vtotal;
  9292. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  9293. vtotal /= 2;
  9294. crtc->scanline_offset = vtotal - 1;
  9295. } else if (HAS_DDI(dev) &&
  9296. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI)) {
  9297. crtc->scanline_offset = 2;
  9298. } else
  9299. crtc->scanline_offset = 1;
  9300. }
  9301. static int __intel_set_mode(struct drm_crtc *crtc,
  9302. struct drm_display_mode *mode,
  9303. int x, int y, struct drm_framebuffer *fb)
  9304. {
  9305. struct drm_device *dev = crtc->dev;
  9306. struct drm_i915_private *dev_priv = dev->dev_private;
  9307. struct drm_display_mode *saved_mode;
  9308. struct intel_crtc_config *pipe_config = NULL;
  9309. struct intel_crtc *intel_crtc;
  9310. unsigned disable_pipes, prepare_pipes, modeset_pipes;
  9311. int ret = 0;
  9312. saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
  9313. if (!saved_mode)
  9314. return -ENOMEM;
  9315. intel_modeset_affected_pipes(crtc, &modeset_pipes,
  9316. &prepare_pipes, &disable_pipes);
  9317. *saved_mode = crtc->mode;
  9318. /* Hack: Because we don't (yet) support global modeset on multiple
  9319. * crtcs, we don't keep track of the new mode for more than one crtc.
  9320. * Hence simply check whether any bit is set in modeset_pipes in all the
  9321. * pieces of code that are not yet converted to deal with mutliple crtcs
  9322. * changing their mode at the same time. */
  9323. if (modeset_pipes) {
  9324. pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
  9325. if (IS_ERR(pipe_config)) {
  9326. ret = PTR_ERR(pipe_config);
  9327. pipe_config = NULL;
  9328. goto out;
  9329. }
  9330. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  9331. "[modeset]");
  9332. to_intel_crtc(crtc)->new_config = pipe_config;
  9333. }
  9334. /*
  9335. * See if the config requires any additional preparation, e.g.
  9336. * to adjust global state with pipes off. We need to do this
  9337. * here so we can get the modeset_pipe updated config for the new
  9338. * mode set on this crtc. For other crtcs we need to use the
  9339. * adjusted_mode bits in the crtc directly.
  9340. */
  9341. if (IS_VALLEYVIEW(dev)) {
  9342. valleyview_modeset_global_pipes(dev, &prepare_pipes);
  9343. /* may have added more to prepare_pipes than we should */
  9344. prepare_pipes &= ~disable_pipes;
  9345. }
  9346. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  9347. intel_crtc_disable(&intel_crtc->base);
  9348. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9349. if (intel_crtc->base.enabled)
  9350. dev_priv->display.crtc_disable(&intel_crtc->base);
  9351. }
  9352. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  9353. * to set it here already despite that we pass it down the callchain.
  9354. */
  9355. if (modeset_pipes) {
  9356. crtc->mode = *mode;
  9357. /* mode_set/enable/disable functions rely on a correct pipe
  9358. * config. */
  9359. to_intel_crtc(crtc)->config = *pipe_config;
  9360. to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
  9361. /*
  9362. * Calculate and store various constants which
  9363. * are later needed by vblank and swap-completion
  9364. * timestamping. They are derived from true hwmode.
  9365. */
  9366. drm_calc_timestamping_constants(crtc,
  9367. &pipe_config->adjusted_mode);
  9368. }
  9369. /* Only after disabling all output pipelines that will be changed can we
  9370. * update the the output configuration. */
  9371. intel_modeset_update_state(dev, prepare_pipes);
  9372. if (dev_priv->display.modeset_global_resources)
  9373. dev_priv->display.modeset_global_resources(dev);
  9374. /* Set up the DPLL and any encoders state that needs to adjust or depend
  9375. * on the DPLL.
  9376. */
  9377. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9378. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9379. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
  9380. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9381. mutex_lock(&dev->struct_mutex);
  9382. ret = intel_pin_and_fence_fb_obj(dev,
  9383. obj,
  9384. NULL);
  9385. if (ret != 0) {
  9386. DRM_ERROR("pin & fence failed\n");
  9387. mutex_unlock(&dev->struct_mutex);
  9388. goto done;
  9389. }
  9390. if (old_fb)
  9391. intel_unpin_fb_obj(old_obj);
  9392. i915_gem_track_fb(old_obj, obj,
  9393. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
  9394. mutex_unlock(&dev->struct_mutex);
  9395. crtc->primary->fb = fb;
  9396. crtc->x = x;
  9397. crtc->y = y;
  9398. ret = dev_priv->display.crtc_mode_set(&intel_crtc->base,
  9399. x, y, fb);
  9400. if (ret)
  9401. goto done;
  9402. }
  9403. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  9404. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9405. update_scanline_offset(intel_crtc);
  9406. dev_priv->display.crtc_enable(&intel_crtc->base);
  9407. }
  9408. /* FIXME: add subpixel order */
  9409. done:
  9410. if (ret && crtc->enabled)
  9411. crtc->mode = *saved_mode;
  9412. out:
  9413. kfree(pipe_config);
  9414. kfree(saved_mode);
  9415. return ret;
  9416. }
  9417. static int intel_set_mode(struct drm_crtc *crtc,
  9418. struct drm_display_mode *mode,
  9419. int x, int y, struct drm_framebuffer *fb)
  9420. {
  9421. int ret;
  9422. ret = __intel_set_mode(crtc, mode, x, y, fb);
  9423. if (ret == 0)
  9424. intel_modeset_check_state(crtc->dev);
  9425. return ret;
  9426. }
  9427. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  9428. {
  9429. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
  9430. }
  9431. #undef for_each_intel_crtc_masked
  9432. static void intel_set_config_free(struct intel_set_config *config)
  9433. {
  9434. if (!config)
  9435. return;
  9436. kfree(config->save_connector_encoders);
  9437. kfree(config->save_encoder_crtcs);
  9438. kfree(config->save_crtc_enabled);
  9439. kfree(config);
  9440. }
  9441. static int intel_set_config_save_state(struct drm_device *dev,
  9442. struct intel_set_config *config)
  9443. {
  9444. struct drm_crtc *crtc;
  9445. struct drm_encoder *encoder;
  9446. struct drm_connector *connector;
  9447. int count;
  9448. config->save_crtc_enabled =
  9449. kcalloc(dev->mode_config.num_crtc,
  9450. sizeof(bool), GFP_KERNEL);
  9451. if (!config->save_crtc_enabled)
  9452. return -ENOMEM;
  9453. config->save_encoder_crtcs =
  9454. kcalloc(dev->mode_config.num_encoder,
  9455. sizeof(struct drm_crtc *), GFP_KERNEL);
  9456. if (!config->save_encoder_crtcs)
  9457. return -ENOMEM;
  9458. config->save_connector_encoders =
  9459. kcalloc(dev->mode_config.num_connector,
  9460. sizeof(struct drm_encoder *), GFP_KERNEL);
  9461. if (!config->save_connector_encoders)
  9462. return -ENOMEM;
  9463. /* Copy data. Note that driver private data is not affected.
  9464. * Should anything bad happen only the expected state is
  9465. * restored, not the drivers personal bookkeeping.
  9466. */
  9467. count = 0;
  9468. for_each_crtc(dev, crtc) {
  9469. config->save_crtc_enabled[count++] = crtc->enabled;
  9470. }
  9471. count = 0;
  9472. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  9473. config->save_encoder_crtcs[count++] = encoder->crtc;
  9474. }
  9475. count = 0;
  9476. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  9477. config->save_connector_encoders[count++] = connector->encoder;
  9478. }
  9479. return 0;
  9480. }
  9481. static void intel_set_config_restore_state(struct drm_device *dev,
  9482. struct intel_set_config *config)
  9483. {
  9484. struct intel_crtc *crtc;
  9485. struct intel_encoder *encoder;
  9486. struct intel_connector *connector;
  9487. int count;
  9488. count = 0;
  9489. for_each_intel_crtc(dev, crtc) {
  9490. crtc->new_enabled = config->save_crtc_enabled[count++];
  9491. if (crtc->new_enabled)
  9492. crtc->new_config = &crtc->config;
  9493. else
  9494. crtc->new_config = NULL;
  9495. }
  9496. count = 0;
  9497. for_each_intel_encoder(dev, encoder) {
  9498. encoder->new_crtc =
  9499. to_intel_crtc(config->save_encoder_crtcs[count++]);
  9500. }
  9501. count = 0;
  9502. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  9503. connector->new_encoder =
  9504. to_intel_encoder(config->save_connector_encoders[count++]);
  9505. }
  9506. }
  9507. static bool
  9508. is_crtc_connector_off(struct drm_mode_set *set)
  9509. {
  9510. int i;
  9511. if (set->num_connectors == 0)
  9512. return false;
  9513. if (WARN_ON(set->connectors == NULL))
  9514. return false;
  9515. for (i = 0; i < set->num_connectors; i++)
  9516. if (set->connectors[i]->encoder &&
  9517. set->connectors[i]->encoder->crtc == set->crtc &&
  9518. set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
  9519. return true;
  9520. return false;
  9521. }
  9522. static void
  9523. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  9524. struct intel_set_config *config)
  9525. {
  9526. /* We should be able to check here if the fb has the same properties
  9527. * and then just flip_or_move it */
  9528. if (is_crtc_connector_off(set)) {
  9529. config->mode_changed = true;
  9530. } else if (set->crtc->primary->fb != set->fb) {
  9531. /*
  9532. * If we have no fb, we can only flip as long as the crtc is
  9533. * active, otherwise we need a full mode set. The crtc may
  9534. * be active if we've only disabled the primary plane, or
  9535. * in fastboot situations.
  9536. */
  9537. if (set->crtc->primary->fb == NULL) {
  9538. struct intel_crtc *intel_crtc =
  9539. to_intel_crtc(set->crtc);
  9540. if (intel_crtc->active) {
  9541. DRM_DEBUG_KMS("crtc has no fb, will flip\n");
  9542. config->fb_changed = true;
  9543. } else {
  9544. DRM_DEBUG_KMS("inactive crtc, full mode set\n");
  9545. config->mode_changed = true;
  9546. }
  9547. } else if (set->fb == NULL) {
  9548. config->mode_changed = true;
  9549. } else if (set->fb->pixel_format !=
  9550. set->crtc->primary->fb->pixel_format) {
  9551. config->mode_changed = true;
  9552. } else {
  9553. config->fb_changed = true;
  9554. }
  9555. }
  9556. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  9557. config->fb_changed = true;
  9558. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  9559. DRM_DEBUG_KMS("modes are different, full mode set\n");
  9560. drm_mode_debug_printmodeline(&set->crtc->mode);
  9561. drm_mode_debug_printmodeline(set->mode);
  9562. config->mode_changed = true;
  9563. }
  9564. DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
  9565. set->crtc->base.id, config->mode_changed, config->fb_changed);
  9566. }
  9567. static int
  9568. intel_modeset_stage_output_state(struct drm_device *dev,
  9569. struct drm_mode_set *set,
  9570. struct intel_set_config *config)
  9571. {
  9572. struct intel_connector *connector;
  9573. struct intel_encoder *encoder;
  9574. struct intel_crtc *crtc;
  9575. int ro;
  9576. /* The upper layers ensure that we either disable a crtc or have a list
  9577. * of connectors. For paranoia, double-check this. */
  9578. WARN_ON(!set->fb && (set->num_connectors != 0));
  9579. WARN_ON(set->fb && (set->num_connectors == 0));
  9580. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9581. base.head) {
  9582. /* Otherwise traverse passed in connector list and get encoders
  9583. * for them. */
  9584. for (ro = 0; ro < set->num_connectors; ro++) {
  9585. if (set->connectors[ro] == &connector->base) {
  9586. connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
  9587. break;
  9588. }
  9589. }
  9590. /* If we disable the crtc, disable all its connectors. Also, if
  9591. * the connector is on the changing crtc but not on the new
  9592. * connector list, disable it. */
  9593. if ((!set->fb || ro == set->num_connectors) &&
  9594. connector->base.encoder &&
  9595. connector->base.encoder->crtc == set->crtc) {
  9596. connector->new_encoder = NULL;
  9597. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  9598. connector->base.base.id,
  9599. connector->base.name);
  9600. }
  9601. if (&connector->new_encoder->base != connector->base.encoder) {
  9602. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  9603. config->mode_changed = true;
  9604. }
  9605. }
  9606. /* connector->new_encoder is now updated for all connectors. */
  9607. /* Update crtc of enabled connectors. */
  9608. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9609. base.head) {
  9610. struct drm_crtc *new_crtc;
  9611. if (!connector->new_encoder)
  9612. continue;
  9613. new_crtc = connector->new_encoder->base.crtc;
  9614. for (ro = 0; ro < set->num_connectors; ro++) {
  9615. if (set->connectors[ro] == &connector->base)
  9616. new_crtc = set->crtc;
  9617. }
  9618. /* Make sure the new CRTC will work with the encoder */
  9619. if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
  9620. new_crtc)) {
  9621. return -EINVAL;
  9622. }
  9623. connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
  9624. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  9625. connector->base.base.id,
  9626. connector->base.name,
  9627. new_crtc->base.id);
  9628. }
  9629. /* Check for any encoders that needs to be disabled. */
  9630. for_each_intel_encoder(dev, encoder) {
  9631. int num_connectors = 0;
  9632. list_for_each_entry(connector,
  9633. &dev->mode_config.connector_list,
  9634. base.head) {
  9635. if (connector->new_encoder == encoder) {
  9636. WARN_ON(!connector->new_encoder->new_crtc);
  9637. num_connectors++;
  9638. }
  9639. }
  9640. if (num_connectors == 0)
  9641. encoder->new_crtc = NULL;
  9642. else if (num_connectors > 1)
  9643. return -EINVAL;
  9644. /* Only now check for crtc changes so we don't miss encoders
  9645. * that will be disabled. */
  9646. if (&encoder->new_crtc->base != encoder->base.crtc) {
  9647. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  9648. config->mode_changed = true;
  9649. }
  9650. }
  9651. /* Now we've also updated encoder->new_crtc for all encoders. */
  9652. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9653. base.head) {
  9654. if (connector->new_encoder)
  9655. if (connector->new_encoder != connector->encoder)
  9656. connector->encoder = connector->new_encoder;
  9657. }
  9658. for_each_intel_crtc(dev, crtc) {
  9659. crtc->new_enabled = false;
  9660. for_each_intel_encoder(dev, encoder) {
  9661. if (encoder->new_crtc == crtc) {
  9662. crtc->new_enabled = true;
  9663. break;
  9664. }
  9665. }
  9666. if (crtc->new_enabled != crtc->base.enabled) {
  9667. DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
  9668. crtc->new_enabled ? "en" : "dis");
  9669. config->mode_changed = true;
  9670. }
  9671. if (crtc->new_enabled)
  9672. crtc->new_config = &crtc->config;
  9673. else
  9674. crtc->new_config = NULL;
  9675. }
  9676. return 0;
  9677. }
  9678. static void disable_crtc_nofb(struct intel_crtc *crtc)
  9679. {
  9680. struct drm_device *dev = crtc->base.dev;
  9681. struct intel_encoder *encoder;
  9682. struct intel_connector *connector;
  9683. DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
  9684. pipe_name(crtc->pipe));
  9685. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  9686. if (connector->new_encoder &&
  9687. connector->new_encoder->new_crtc == crtc)
  9688. connector->new_encoder = NULL;
  9689. }
  9690. for_each_intel_encoder(dev, encoder) {
  9691. if (encoder->new_crtc == crtc)
  9692. encoder->new_crtc = NULL;
  9693. }
  9694. crtc->new_enabled = false;
  9695. crtc->new_config = NULL;
  9696. }
  9697. static int intel_crtc_set_config(struct drm_mode_set *set)
  9698. {
  9699. struct drm_device *dev;
  9700. struct drm_mode_set save_set;
  9701. struct intel_set_config *config;
  9702. int ret;
  9703. BUG_ON(!set);
  9704. BUG_ON(!set->crtc);
  9705. BUG_ON(!set->crtc->helper_private);
  9706. /* Enforce sane interface api - has been abused by the fb helper. */
  9707. BUG_ON(!set->mode && set->fb);
  9708. BUG_ON(set->fb && set->num_connectors == 0);
  9709. if (set->fb) {
  9710. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  9711. set->crtc->base.id, set->fb->base.id,
  9712. (int)set->num_connectors, set->x, set->y);
  9713. } else {
  9714. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  9715. }
  9716. dev = set->crtc->dev;
  9717. ret = -ENOMEM;
  9718. config = kzalloc(sizeof(*config), GFP_KERNEL);
  9719. if (!config)
  9720. goto out_config;
  9721. ret = intel_set_config_save_state(dev, config);
  9722. if (ret)
  9723. goto out_config;
  9724. save_set.crtc = set->crtc;
  9725. save_set.mode = &set->crtc->mode;
  9726. save_set.x = set->crtc->x;
  9727. save_set.y = set->crtc->y;
  9728. save_set.fb = set->crtc->primary->fb;
  9729. /* Compute whether we need a full modeset, only an fb base update or no
  9730. * change at all. In the future we might also check whether only the
  9731. * mode changed, e.g. for LVDS where we only change the panel fitter in
  9732. * such cases. */
  9733. intel_set_config_compute_mode_changes(set, config);
  9734. ret = intel_modeset_stage_output_state(dev, set, config);
  9735. if (ret)
  9736. goto fail;
  9737. if (config->mode_changed) {
  9738. ret = intel_set_mode(set->crtc, set->mode,
  9739. set->x, set->y, set->fb);
  9740. } else if (config->fb_changed) {
  9741. struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
  9742. intel_crtc_wait_for_pending_flips(set->crtc);
  9743. ret = intel_pipe_set_base(set->crtc,
  9744. set->x, set->y, set->fb);
  9745. /*
  9746. * We need to make sure the primary plane is re-enabled if it
  9747. * has previously been turned off.
  9748. */
  9749. if (!intel_crtc->primary_enabled && ret == 0) {
  9750. WARN_ON(!intel_crtc->active);
  9751. intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
  9752. }
  9753. /*
  9754. * In the fastboot case this may be our only check of the
  9755. * state after boot. It would be better to only do it on
  9756. * the first update, but we don't have a nice way of doing that
  9757. * (and really, set_config isn't used much for high freq page
  9758. * flipping, so increasing its cost here shouldn't be a big
  9759. * deal).
  9760. */
  9761. if (i915.fastboot && ret == 0)
  9762. intel_modeset_check_state(set->crtc->dev);
  9763. }
  9764. if (ret) {
  9765. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  9766. set->crtc->base.id, ret);
  9767. fail:
  9768. intel_set_config_restore_state(dev, config);
  9769. /*
  9770. * HACK: if the pipe was on, but we didn't have a framebuffer,
  9771. * force the pipe off to avoid oopsing in the modeset code
  9772. * due to fb==NULL. This should only happen during boot since
  9773. * we don't yet reconstruct the FB from the hardware state.
  9774. */
  9775. if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
  9776. disable_crtc_nofb(to_intel_crtc(save_set.crtc));
  9777. /* Try to restore the config */
  9778. if (config->mode_changed &&
  9779. intel_set_mode(save_set.crtc, save_set.mode,
  9780. save_set.x, save_set.y, save_set.fb))
  9781. DRM_ERROR("failed to restore config after modeset failure\n");
  9782. }
  9783. out_config:
  9784. intel_set_config_free(config);
  9785. return ret;
  9786. }
  9787. static const struct drm_crtc_funcs intel_crtc_funcs = {
  9788. .gamma_set = intel_crtc_gamma_set,
  9789. .set_config = intel_crtc_set_config,
  9790. .destroy = intel_crtc_destroy,
  9791. .page_flip = intel_crtc_page_flip,
  9792. };
  9793. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  9794. struct intel_shared_dpll *pll,
  9795. struct intel_dpll_hw_state *hw_state)
  9796. {
  9797. uint32_t val;
  9798. if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_PLLS))
  9799. return false;
  9800. val = I915_READ(PCH_DPLL(pll->id));
  9801. hw_state->dpll = val;
  9802. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  9803. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  9804. return val & DPLL_VCO_ENABLE;
  9805. }
  9806. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  9807. struct intel_shared_dpll *pll)
  9808. {
  9809. I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
  9810. I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
  9811. }
  9812. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  9813. struct intel_shared_dpll *pll)
  9814. {
  9815. /* PCH refclock must be enabled first */
  9816. ibx_assert_pch_refclk_enabled(dev_priv);
  9817. I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
  9818. /* Wait for the clocks to stabilize. */
  9819. POSTING_READ(PCH_DPLL(pll->id));
  9820. udelay(150);
  9821. /* The pixel multiplier can only be updated once the
  9822. * DPLL is enabled and the clocks are stable.
  9823. *
  9824. * So write it again.
  9825. */
  9826. I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
  9827. POSTING_READ(PCH_DPLL(pll->id));
  9828. udelay(200);
  9829. }
  9830. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  9831. struct intel_shared_dpll *pll)
  9832. {
  9833. struct drm_device *dev = dev_priv->dev;
  9834. struct intel_crtc *crtc;
  9835. /* Make sure no transcoder isn't still depending on us. */
  9836. for_each_intel_crtc(dev, crtc) {
  9837. if (intel_crtc_to_shared_dpll(crtc) == pll)
  9838. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  9839. }
  9840. I915_WRITE(PCH_DPLL(pll->id), 0);
  9841. POSTING_READ(PCH_DPLL(pll->id));
  9842. udelay(200);
  9843. }
  9844. static char *ibx_pch_dpll_names[] = {
  9845. "PCH DPLL A",
  9846. "PCH DPLL B",
  9847. };
  9848. static void ibx_pch_dpll_init(struct drm_device *dev)
  9849. {
  9850. struct drm_i915_private *dev_priv = dev->dev_private;
  9851. int i;
  9852. dev_priv->num_shared_dpll = 2;
  9853. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9854. dev_priv->shared_dplls[i].id = i;
  9855. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  9856. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  9857. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  9858. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  9859. dev_priv->shared_dplls[i].get_hw_state =
  9860. ibx_pch_dpll_get_hw_state;
  9861. }
  9862. }
  9863. static void intel_shared_dpll_init(struct drm_device *dev)
  9864. {
  9865. struct drm_i915_private *dev_priv = dev->dev_private;
  9866. if (HAS_DDI(dev))
  9867. intel_ddi_pll_init(dev);
  9868. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  9869. ibx_pch_dpll_init(dev);
  9870. else
  9871. dev_priv->num_shared_dpll = 0;
  9872. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  9873. }
  9874. static int
  9875. intel_primary_plane_disable(struct drm_plane *plane)
  9876. {
  9877. struct drm_device *dev = plane->dev;
  9878. struct intel_crtc *intel_crtc;
  9879. if (!plane->fb)
  9880. return 0;
  9881. BUG_ON(!plane->crtc);
  9882. intel_crtc = to_intel_crtc(plane->crtc);
  9883. /*
  9884. * Even though we checked plane->fb above, it's still possible that
  9885. * the primary plane has been implicitly disabled because the crtc
  9886. * coordinates given weren't visible, or because we detected
  9887. * that it was 100% covered by a sprite plane. Or, the CRTC may be
  9888. * off and we've set a fb, but haven't actually turned on the CRTC yet.
  9889. * In either case, we need to unpin the FB and let the fb pointer get
  9890. * updated, but otherwise we don't need to touch the hardware.
  9891. */
  9892. if (!intel_crtc->primary_enabled)
  9893. goto disable_unpin;
  9894. intel_crtc_wait_for_pending_flips(plane->crtc);
  9895. intel_disable_primary_hw_plane(plane, plane->crtc);
  9896. disable_unpin:
  9897. mutex_lock(&dev->struct_mutex);
  9898. i915_gem_track_fb(intel_fb_obj(plane->fb), NULL,
  9899. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
  9900. intel_unpin_fb_obj(intel_fb_obj(plane->fb));
  9901. mutex_unlock(&dev->struct_mutex);
  9902. plane->fb = NULL;
  9903. return 0;
  9904. }
  9905. static int
  9906. intel_primary_plane_setplane(struct drm_plane *plane, struct drm_crtc *crtc,
  9907. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  9908. unsigned int crtc_w, unsigned int crtc_h,
  9909. uint32_t src_x, uint32_t src_y,
  9910. uint32_t src_w, uint32_t src_h)
  9911. {
  9912. struct drm_device *dev = crtc->dev;
  9913. struct drm_i915_private *dev_priv = dev->dev_private;
  9914. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9915. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9916. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  9917. struct drm_rect dest = {
  9918. /* integer pixels */
  9919. .x1 = crtc_x,
  9920. .y1 = crtc_y,
  9921. .x2 = crtc_x + crtc_w,
  9922. .y2 = crtc_y + crtc_h,
  9923. };
  9924. struct drm_rect src = {
  9925. /* 16.16 fixed point */
  9926. .x1 = src_x,
  9927. .y1 = src_y,
  9928. .x2 = src_x + src_w,
  9929. .y2 = src_y + src_h,
  9930. };
  9931. const struct drm_rect clip = {
  9932. /* integer pixels */
  9933. .x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0,
  9934. .y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0,
  9935. };
  9936. const struct {
  9937. int crtc_x, crtc_y;
  9938. unsigned int crtc_w, crtc_h;
  9939. uint32_t src_x, src_y, src_w, src_h;
  9940. } orig = {
  9941. .crtc_x = crtc_x,
  9942. .crtc_y = crtc_y,
  9943. .crtc_w = crtc_w,
  9944. .crtc_h = crtc_h,
  9945. .src_x = src_x,
  9946. .src_y = src_y,
  9947. .src_w = src_w,
  9948. .src_h = src_h,
  9949. };
  9950. struct intel_plane *intel_plane = to_intel_plane(plane);
  9951. bool visible;
  9952. int ret;
  9953. ret = drm_plane_helper_check_update(plane, crtc, fb,
  9954. &src, &dest, &clip,
  9955. DRM_PLANE_HELPER_NO_SCALING,
  9956. DRM_PLANE_HELPER_NO_SCALING,
  9957. false, true, &visible);
  9958. if (ret)
  9959. return ret;
  9960. /*
  9961. * If the CRTC isn't enabled, we're just pinning the framebuffer,
  9962. * updating the fb pointer, and returning without touching the
  9963. * hardware. This allows us to later do a drmModeSetCrtc with fb=-1 to
  9964. * turn on the display with all planes setup as desired.
  9965. */
  9966. if (!crtc->enabled) {
  9967. mutex_lock(&dev->struct_mutex);
  9968. /*
  9969. * If we already called setplane while the crtc was disabled,
  9970. * we may have an fb pinned; unpin it.
  9971. */
  9972. if (plane->fb)
  9973. intel_unpin_fb_obj(old_obj);
  9974. i915_gem_track_fb(old_obj, obj,
  9975. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
  9976. /* Pin and return without programming hardware */
  9977. ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
  9978. mutex_unlock(&dev->struct_mutex);
  9979. return ret;
  9980. }
  9981. intel_crtc_wait_for_pending_flips(crtc);
  9982. /*
  9983. * If clipping results in a non-visible primary plane, we'll disable
  9984. * the primary plane. Note that this is a bit different than what
  9985. * happens if userspace explicitly disables the plane by passing fb=0
  9986. * because plane->fb still gets set and pinned.
  9987. */
  9988. if (!visible) {
  9989. mutex_lock(&dev->struct_mutex);
  9990. /*
  9991. * Try to pin the new fb first so that we can bail out if we
  9992. * fail.
  9993. */
  9994. if (plane->fb != fb) {
  9995. ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
  9996. if (ret) {
  9997. mutex_unlock(&dev->struct_mutex);
  9998. return ret;
  9999. }
  10000. }
  10001. i915_gem_track_fb(old_obj, obj,
  10002. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
  10003. if (intel_crtc->primary_enabled)
  10004. intel_disable_primary_hw_plane(plane, crtc);
  10005. if (plane->fb != fb)
  10006. if (plane->fb)
  10007. intel_unpin_fb_obj(old_obj);
  10008. mutex_unlock(&dev->struct_mutex);
  10009. } else {
  10010. if (intel_crtc && intel_crtc->active &&
  10011. intel_crtc->primary_enabled) {
  10012. /*
  10013. * FBC does not work on some platforms for rotated
  10014. * planes, so disable it when rotation is not 0 and
  10015. * update it when rotation is set back to 0.
  10016. *
  10017. * FIXME: This is redundant with the fbc update done in
  10018. * the primary plane enable function except that that
  10019. * one is done too late. We eventually need to unify
  10020. * this.
  10021. */
  10022. if (INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  10023. dev_priv->fbc.plane == intel_crtc->plane &&
  10024. intel_plane->rotation != BIT(DRM_ROTATE_0)) {
  10025. intel_disable_fbc(dev);
  10026. }
  10027. }
  10028. ret = intel_pipe_set_base(crtc, src.x1, src.y1, fb);
  10029. if (ret)
  10030. return ret;
  10031. if (!intel_crtc->primary_enabled)
  10032. intel_enable_primary_hw_plane(plane, crtc);
  10033. }
  10034. intel_plane->crtc_x = orig.crtc_x;
  10035. intel_plane->crtc_y = orig.crtc_y;
  10036. intel_plane->crtc_w = orig.crtc_w;
  10037. intel_plane->crtc_h = orig.crtc_h;
  10038. intel_plane->src_x = orig.src_x;
  10039. intel_plane->src_y = orig.src_y;
  10040. intel_plane->src_w = orig.src_w;
  10041. intel_plane->src_h = orig.src_h;
  10042. intel_plane->obj = obj;
  10043. return 0;
  10044. }
  10045. /* Common destruction function for both primary and cursor planes */
  10046. static void intel_plane_destroy(struct drm_plane *plane)
  10047. {
  10048. struct intel_plane *intel_plane = to_intel_plane(plane);
  10049. drm_plane_cleanup(plane);
  10050. kfree(intel_plane);
  10051. }
  10052. static const struct drm_plane_funcs intel_primary_plane_funcs = {
  10053. .update_plane = intel_primary_plane_setplane,
  10054. .disable_plane = intel_primary_plane_disable,
  10055. .destroy = intel_plane_destroy,
  10056. .set_property = intel_plane_set_property
  10057. };
  10058. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  10059. int pipe)
  10060. {
  10061. struct intel_plane *primary;
  10062. const uint32_t *intel_primary_formats;
  10063. int num_formats;
  10064. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  10065. if (primary == NULL)
  10066. return NULL;
  10067. primary->can_scale = false;
  10068. primary->max_downscale = 1;
  10069. primary->pipe = pipe;
  10070. primary->plane = pipe;
  10071. primary->rotation = BIT(DRM_ROTATE_0);
  10072. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  10073. primary->plane = !pipe;
  10074. if (INTEL_INFO(dev)->gen <= 3) {
  10075. intel_primary_formats = intel_primary_formats_gen2;
  10076. num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
  10077. } else {
  10078. intel_primary_formats = intel_primary_formats_gen4;
  10079. num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
  10080. }
  10081. drm_universal_plane_init(dev, &primary->base, 0,
  10082. &intel_primary_plane_funcs,
  10083. intel_primary_formats, num_formats,
  10084. DRM_PLANE_TYPE_PRIMARY);
  10085. if (INTEL_INFO(dev)->gen >= 4) {
  10086. if (!dev->mode_config.rotation_property)
  10087. dev->mode_config.rotation_property =
  10088. drm_mode_create_rotation_property(dev,
  10089. BIT(DRM_ROTATE_0) |
  10090. BIT(DRM_ROTATE_180));
  10091. if (dev->mode_config.rotation_property)
  10092. drm_object_attach_property(&primary->base.base,
  10093. dev->mode_config.rotation_property,
  10094. primary->rotation);
  10095. }
  10096. return &primary->base;
  10097. }
  10098. static int
  10099. intel_cursor_plane_disable(struct drm_plane *plane)
  10100. {
  10101. if (!plane->fb)
  10102. return 0;
  10103. BUG_ON(!plane->crtc);
  10104. return intel_crtc_cursor_set_obj(plane->crtc, NULL, 0, 0);
  10105. }
  10106. static int
  10107. intel_cursor_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
  10108. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  10109. unsigned int crtc_w, unsigned int crtc_h,
  10110. uint32_t src_x, uint32_t src_y,
  10111. uint32_t src_w, uint32_t src_h)
  10112. {
  10113. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10114. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10115. struct drm_i915_gem_object *obj = intel_fb->obj;
  10116. struct drm_rect dest = {
  10117. /* integer pixels */
  10118. .x1 = crtc_x,
  10119. .y1 = crtc_y,
  10120. .x2 = crtc_x + crtc_w,
  10121. .y2 = crtc_y + crtc_h,
  10122. };
  10123. struct drm_rect src = {
  10124. /* 16.16 fixed point */
  10125. .x1 = src_x,
  10126. .y1 = src_y,
  10127. .x2 = src_x + src_w,
  10128. .y2 = src_y + src_h,
  10129. };
  10130. const struct drm_rect clip = {
  10131. /* integer pixels */
  10132. .x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0,
  10133. .y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0,
  10134. };
  10135. bool visible;
  10136. int ret;
  10137. ret = drm_plane_helper_check_update(plane, crtc, fb,
  10138. &src, &dest, &clip,
  10139. DRM_PLANE_HELPER_NO_SCALING,
  10140. DRM_PLANE_HELPER_NO_SCALING,
  10141. true, true, &visible);
  10142. if (ret)
  10143. return ret;
  10144. crtc->cursor_x = crtc_x;
  10145. crtc->cursor_y = crtc_y;
  10146. if (fb != crtc->cursor->fb) {
  10147. return intel_crtc_cursor_set_obj(crtc, obj, crtc_w, crtc_h);
  10148. } else {
  10149. intel_crtc_update_cursor(crtc, visible);
  10150. intel_frontbuffer_flip(crtc->dev,
  10151. INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe));
  10152. return 0;
  10153. }
  10154. }
  10155. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  10156. .update_plane = intel_cursor_plane_update,
  10157. .disable_plane = intel_cursor_plane_disable,
  10158. .destroy = intel_plane_destroy,
  10159. };
  10160. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  10161. int pipe)
  10162. {
  10163. struct intel_plane *cursor;
  10164. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  10165. if (cursor == NULL)
  10166. return NULL;
  10167. cursor->can_scale = false;
  10168. cursor->max_downscale = 1;
  10169. cursor->pipe = pipe;
  10170. cursor->plane = pipe;
  10171. drm_universal_plane_init(dev, &cursor->base, 0,
  10172. &intel_cursor_plane_funcs,
  10173. intel_cursor_formats,
  10174. ARRAY_SIZE(intel_cursor_formats),
  10175. DRM_PLANE_TYPE_CURSOR);
  10176. return &cursor->base;
  10177. }
  10178. static void intel_crtc_init(struct drm_device *dev, int pipe)
  10179. {
  10180. struct drm_i915_private *dev_priv = dev->dev_private;
  10181. struct intel_crtc *intel_crtc;
  10182. struct drm_plane *primary = NULL;
  10183. struct drm_plane *cursor = NULL;
  10184. int i, ret;
  10185. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  10186. if (intel_crtc == NULL)
  10187. return;
  10188. primary = intel_primary_plane_create(dev, pipe);
  10189. if (!primary)
  10190. goto fail;
  10191. cursor = intel_cursor_plane_create(dev, pipe);
  10192. if (!cursor)
  10193. goto fail;
  10194. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  10195. cursor, &intel_crtc_funcs);
  10196. if (ret)
  10197. goto fail;
  10198. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  10199. for (i = 0; i < 256; i++) {
  10200. intel_crtc->lut_r[i] = i;
  10201. intel_crtc->lut_g[i] = i;
  10202. intel_crtc->lut_b[i] = i;
  10203. }
  10204. /*
  10205. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  10206. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  10207. */
  10208. intel_crtc->pipe = pipe;
  10209. intel_crtc->plane = pipe;
  10210. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  10211. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  10212. intel_crtc->plane = !pipe;
  10213. }
  10214. intel_crtc->cursor_base = ~0;
  10215. intel_crtc->cursor_cntl = ~0;
  10216. intel_crtc->cursor_size = ~0;
  10217. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  10218. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  10219. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  10220. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  10221. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  10222. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  10223. return;
  10224. fail:
  10225. if (primary)
  10226. drm_plane_cleanup(primary);
  10227. if (cursor)
  10228. drm_plane_cleanup(cursor);
  10229. kfree(intel_crtc);
  10230. }
  10231. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  10232. {
  10233. struct drm_encoder *encoder = connector->base.encoder;
  10234. struct drm_device *dev = connector->base.dev;
  10235. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  10236. if (!encoder)
  10237. return INVALID_PIPE;
  10238. return to_intel_crtc(encoder->crtc)->pipe;
  10239. }
  10240. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  10241. struct drm_file *file)
  10242. {
  10243. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  10244. struct drm_crtc *drmmode_crtc;
  10245. struct intel_crtc *crtc;
  10246. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  10247. return -ENODEV;
  10248. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  10249. if (!drmmode_crtc) {
  10250. DRM_ERROR("no such CRTC id\n");
  10251. return -ENOENT;
  10252. }
  10253. crtc = to_intel_crtc(drmmode_crtc);
  10254. pipe_from_crtc_id->pipe = crtc->pipe;
  10255. return 0;
  10256. }
  10257. static int intel_encoder_clones(struct intel_encoder *encoder)
  10258. {
  10259. struct drm_device *dev = encoder->base.dev;
  10260. struct intel_encoder *source_encoder;
  10261. int index_mask = 0;
  10262. int entry = 0;
  10263. for_each_intel_encoder(dev, source_encoder) {
  10264. if (encoders_cloneable(encoder, source_encoder))
  10265. index_mask |= (1 << entry);
  10266. entry++;
  10267. }
  10268. return index_mask;
  10269. }
  10270. static bool has_edp_a(struct drm_device *dev)
  10271. {
  10272. struct drm_i915_private *dev_priv = dev->dev_private;
  10273. if (!IS_MOBILE(dev))
  10274. return false;
  10275. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  10276. return false;
  10277. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  10278. return false;
  10279. return true;
  10280. }
  10281. const char *intel_output_name(int output)
  10282. {
  10283. static const char *names[] = {
  10284. [INTEL_OUTPUT_UNUSED] = "Unused",
  10285. [INTEL_OUTPUT_ANALOG] = "Analog",
  10286. [INTEL_OUTPUT_DVO] = "DVO",
  10287. [INTEL_OUTPUT_SDVO] = "SDVO",
  10288. [INTEL_OUTPUT_LVDS] = "LVDS",
  10289. [INTEL_OUTPUT_TVOUT] = "TV",
  10290. [INTEL_OUTPUT_HDMI] = "HDMI",
  10291. [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
  10292. [INTEL_OUTPUT_EDP] = "eDP",
  10293. [INTEL_OUTPUT_DSI] = "DSI",
  10294. [INTEL_OUTPUT_UNKNOWN] = "Unknown",
  10295. };
  10296. if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
  10297. return "Invalid";
  10298. return names[output];
  10299. }
  10300. static bool intel_crt_present(struct drm_device *dev)
  10301. {
  10302. struct drm_i915_private *dev_priv = dev->dev_private;
  10303. if (IS_ULT(dev))
  10304. return false;
  10305. if (IS_CHERRYVIEW(dev))
  10306. return false;
  10307. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  10308. return false;
  10309. return true;
  10310. }
  10311. static void intel_setup_outputs(struct drm_device *dev)
  10312. {
  10313. struct drm_i915_private *dev_priv = dev->dev_private;
  10314. struct intel_encoder *encoder;
  10315. bool dpd_is_edp = false;
  10316. intel_lvds_init(dev);
  10317. if (intel_crt_present(dev))
  10318. intel_crt_init(dev);
  10319. if (HAS_DDI(dev)) {
  10320. int found;
  10321. /* Haswell uses DDI functions to detect digital outputs */
  10322. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  10323. /* DDI A only supports eDP */
  10324. if (found)
  10325. intel_ddi_init(dev, PORT_A);
  10326. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  10327. * register */
  10328. found = I915_READ(SFUSE_STRAP);
  10329. if (found & SFUSE_STRAP_DDIB_DETECTED)
  10330. intel_ddi_init(dev, PORT_B);
  10331. if (found & SFUSE_STRAP_DDIC_DETECTED)
  10332. intel_ddi_init(dev, PORT_C);
  10333. if (found & SFUSE_STRAP_DDID_DETECTED)
  10334. intel_ddi_init(dev, PORT_D);
  10335. } else if (HAS_PCH_SPLIT(dev)) {
  10336. int found;
  10337. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  10338. if (has_edp_a(dev))
  10339. intel_dp_init(dev, DP_A, PORT_A);
  10340. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  10341. /* PCH SDVOB multiplex with HDMIB */
  10342. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  10343. if (!found)
  10344. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  10345. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  10346. intel_dp_init(dev, PCH_DP_B, PORT_B);
  10347. }
  10348. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  10349. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  10350. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  10351. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  10352. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  10353. intel_dp_init(dev, PCH_DP_C, PORT_C);
  10354. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  10355. intel_dp_init(dev, PCH_DP_D, PORT_D);
  10356. } else if (IS_VALLEYVIEW(dev)) {
  10357. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
  10358. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  10359. PORT_B);
  10360. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
  10361. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  10362. }
  10363. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
  10364. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  10365. PORT_C);
  10366. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
  10367. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  10368. }
  10369. if (IS_CHERRYVIEW(dev)) {
  10370. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED) {
  10371. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  10372. PORT_D);
  10373. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  10374. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  10375. }
  10376. }
  10377. intel_dsi_init(dev);
  10378. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  10379. bool found = false;
  10380. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10381. DRM_DEBUG_KMS("probing SDVOB\n");
  10382. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  10383. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  10384. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  10385. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  10386. }
  10387. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  10388. intel_dp_init(dev, DP_B, PORT_B);
  10389. }
  10390. /* Before G4X SDVOC doesn't have its own detect register */
  10391. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10392. DRM_DEBUG_KMS("probing SDVOC\n");
  10393. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  10394. }
  10395. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  10396. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  10397. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  10398. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  10399. }
  10400. if (SUPPORTS_INTEGRATED_DP(dev))
  10401. intel_dp_init(dev, DP_C, PORT_C);
  10402. }
  10403. if (SUPPORTS_INTEGRATED_DP(dev) &&
  10404. (I915_READ(DP_D) & DP_DETECTED))
  10405. intel_dp_init(dev, DP_D, PORT_D);
  10406. } else if (IS_GEN2(dev))
  10407. intel_dvo_init(dev);
  10408. if (SUPPORTS_TV(dev))
  10409. intel_tv_init(dev);
  10410. intel_edp_psr_init(dev);
  10411. for_each_intel_encoder(dev, encoder) {
  10412. encoder->base.possible_crtcs = encoder->crtc_mask;
  10413. encoder->base.possible_clones =
  10414. intel_encoder_clones(encoder);
  10415. }
  10416. intel_init_pch_refclk(dev);
  10417. drm_helper_move_panel_connectors_to_head(dev);
  10418. }
  10419. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  10420. {
  10421. struct drm_device *dev = fb->dev;
  10422. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10423. drm_framebuffer_cleanup(fb);
  10424. mutex_lock(&dev->struct_mutex);
  10425. WARN_ON(!intel_fb->obj->framebuffer_references--);
  10426. drm_gem_object_unreference(&intel_fb->obj->base);
  10427. mutex_unlock(&dev->struct_mutex);
  10428. kfree(intel_fb);
  10429. }
  10430. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  10431. struct drm_file *file,
  10432. unsigned int *handle)
  10433. {
  10434. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10435. struct drm_i915_gem_object *obj = intel_fb->obj;
  10436. return drm_gem_handle_create(file, &obj->base, handle);
  10437. }
  10438. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  10439. .destroy = intel_user_framebuffer_destroy,
  10440. .create_handle = intel_user_framebuffer_create_handle,
  10441. };
  10442. static int intel_framebuffer_init(struct drm_device *dev,
  10443. struct intel_framebuffer *intel_fb,
  10444. struct drm_mode_fb_cmd2 *mode_cmd,
  10445. struct drm_i915_gem_object *obj)
  10446. {
  10447. int aligned_height;
  10448. int pitch_limit;
  10449. int ret;
  10450. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  10451. if (obj->tiling_mode == I915_TILING_Y) {
  10452. DRM_DEBUG("hardware does not support tiling Y\n");
  10453. return -EINVAL;
  10454. }
  10455. if (mode_cmd->pitches[0] & 63) {
  10456. DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
  10457. mode_cmd->pitches[0]);
  10458. return -EINVAL;
  10459. }
  10460. if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
  10461. pitch_limit = 32*1024;
  10462. } else if (INTEL_INFO(dev)->gen >= 4) {
  10463. if (obj->tiling_mode)
  10464. pitch_limit = 16*1024;
  10465. else
  10466. pitch_limit = 32*1024;
  10467. } else if (INTEL_INFO(dev)->gen >= 3) {
  10468. if (obj->tiling_mode)
  10469. pitch_limit = 8*1024;
  10470. else
  10471. pitch_limit = 16*1024;
  10472. } else
  10473. /* XXX DSPC is limited to 4k tiled */
  10474. pitch_limit = 8*1024;
  10475. if (mode_cmd->pitches[0] > pitch_limit) {
  10476. DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
  10477. obj->tiling_mode ? "tiled" : "linear",
  10478. mode_cmd->pitches[0], pitch_limit);
  10479. return -EINVAL;
  10480. }
  10481. if (obj->tiling_mode != I915_TILING_NONE &&
  10482. mode_cmd->pitches[0] != obj->stride) {
  10483. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  10484. mode_cmd->pitches[0], obj->stride);
  10485. return -EINVAL;
  10486. }
  10487. /* Reject formats not supported by any plane early. */
  10488. switch (mode_cmd->pixel_format) {
  10489. case DRM_FORMAT_C8:
  10490. case DRM_FORMAT_RGB565:
  10491. case DRM_FORMAT_XRGB8888:
  10492. case DRM_FORMAT_ARGB8888:
  10493. break;
  10494. case DRM_FORMAT_XRGB1555:
  10495. case DRM_FORMAT_ARGB1555:
  10496. if (INTEL_INFO(dev)->gen > 3) {
  10497. DRM_DEBUG("unsupported pixel format: %s\n",
  10498. drm_get_format_name(mode_cmd->pixel_format));
  10499. return -EINVAL;
  10500. }
  10501. break;
  10502. case DRM_FORMAT_XBGR8888:
  10503. case DRM_FORMAT_ABGR8888:
  10504. case DRM_FORMAT_XRGB2101010:
  10505. case DRM_FORMAT_ARGB2101010:
  10506. case DRM_FORMAT_XBGR2101010:
  10507. case DRM_FORMAT_ABGR2101010:
  10508. if (INTEL_INFO(dev)->gen < 4) {
  10509. DRM_DEBUG("unsupported pixel format: %s\n",
  10510. drm_get_format_name(mode_cmd->pixel_format));
  10511. return -EINVAL;
  10512. }
  10513. break;
  10514. case DRM_FORMAT_YUYV:
  10515. case DRM_FORMAT_UYVY:
  10516. case DRM_FORMAT_YVYU:
  10517. case DRM_FORMAT_VYUY:
  10518. if (INTEL_INFO(dev)->gen < 5) {
  10519. DRM_DEBUG("unsupported pixel format: %s\n",
  10520. drm_get_format_name(mode_cmd->pixel_format));
  10521. return -EINVAL;
  10522. }
  10523. break;
  10524. default:
  10525. DRM_DEBUG("unsupported pixel format: %s\n",
  10526. drm_get_format_name(mode_cmd->pixel_format));
  10527. return -EINVAL;
  10528. }
  10529. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  10530. if (mode_cmd->offsets[0] != 0)
  10531. return -EINVAL;
  10532. aligned_height = intel_align_height(dev, mode_cmd->height,
  10533. obj->tiling_mode);
  10534. /* FIXME drm helper for size checks (especially planar formats)? */
  10535. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  10536. return -EINVAL;
  10537. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  10538. intel_fb->obj = obj;
  10539. intel_fb->obj->framebuffer_references++;
  10540. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  10541. if (ret) {
  10542. DRM_ERROR("framebuffer init failed %d\n", ret);
  10543. return ret;
  10544. }
  10545. return 0;
  10546. }
  10547. static struct drm_framebuffer *
  10548. intel_user_framebuffer_create(struct drm_device *dev,
  10549. struct drm_file *filp,
  10550. struct drm_mode_fb_cmd2 *mode_cmd)
  10551. {
  10552. struct drm_i915_gem_object *obj;
  10553. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  10554. mode_cmd->handles[0]));
  10555. if (&obj->base == NULL)
  10556. return ERR_PTR(-ENOENT);
  10557. return intel_framebuffer_create(dev, mode_cmd, obj);
  10558. }
  10559. #ifndef CONFIG_DRM_I915_FBDEV
  10560. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  10561. {
  10562. }
  10563. #endif
  10564. static const struct drm_mode_config_funcs intel_mode_funcs = {
  10565. .fb_create = intel_user_framebuffer_create,
  10566. .output_poll_changed = intel_fbdev_output_poll_changed,
  10567. };
  10568. /* Set up chip specific display functions */
  10569. static void intel_init_display(struct drm_device *dev)
  10570. {
  10571. struct drm_i915_private *dev_priv = dev->dev_private;
  10572. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  10573. dev_priv->display.find_dpll = g4x_find_best_dpll;
  10574. else if (IS_CHERRYVIEW(dev))
  10575. dev_priv->display.find_dpll = chv_find_best_dpll;
  10576. else if (IS_VALLEYVIEW(dev))
  10577. dev_priv->display.find_dpll = vlv_find_best_dpll;
  10578. else if (IS_PINEVIEW(dev))
  10579. dev_priv->display.find_dpll = pnv_find_best_dpll;
  10580. else
  10581. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  10582. if (HAS_DDI(dev)) {
  10583. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  10584. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  10585. dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
  10586. dev_priv->display.crtc_enable = haswell_crtc_enable;
  10587. dev_priv->display.crtc_disable = haswell_crtc_disable;
  10588. dev_priv->display.off = ironlake_crtc_off;
  10589. if (INTEL_INFO(dev)->gen >= 9)
  10590. dev_priv->display.update_primary_plane =
  10591. skylake_update_primary_plane;
  10592. else
  10593. dev_priv->display.update_primary_plane =
  10594. ironlake_update_primary_plane;
  10595. } else if (HAS_PCH_SPLIT(dev)) {
  10596. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  10597. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  10598. dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
  10599. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  10600. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  10601. dev_priv->display.off = ironlake_crtc_off;
  10602. dev_priv->display.update_primary_plane =
  10603. ironlake_update_primary_plane;
  10604. } else if (IS_VALLEYVIEW(dev)) {
  10605. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10606. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  10607. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  10608. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  10609. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10610. dev_priv->display.off = i9xx_crtc_off;
  10611. dev_priv->display.update_primary_plane =
  10612. i9xx_update_primary_plane;
  10613. } else {
  10614. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10615. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  10616. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  10617. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  10618. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10619. dev_priv->display.off = i9xx_crtc_off;
  10620. dev_priv->display.update_primary_plane =
  10621. i9xx_update_primary_plane;
  10622. }
  10623. /* Returns the core display clock speed */
  10624. if (IS_VALLEYVIEW(dev))
  10625. dev_priv->display.get_display_clock_speed =
  10626. valleyview_get_display_clock_speed;
  10627. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  10628. dev_priv->display.get_display_clock_speed =
  10629. i945_get_display_clock_speed;
  10630. else if (IS_I915G(dev))
  10631. dev_priv->display.get_display_clock_speed =
  10632. i915_get_display_clock_speed;
  10633. else if (IS_I945GM(dev) || IS_845G(dev))
  10634. dev_priv->display.get_display_clock_speed =
  10635. i9xx_misc_get_display_clock_speed;
  10636. else if (IS_PINEVIEW(dev))
  10637. dev_priv->display.get_display_clock_speed =
  10638. pnv_get_display_clock_speed;
  10639. else if (IS_I915GM(dev))
  10640. dev_priv->display.get_display_clock_speed =
  10641. i915gm_get_display_clock_speed;
  10642. else if (IS_I865G(dev))
  10643. dev_priv->display.get_display_clock_speed =
  10644. i865_get_display_clock_speed;
  10645. else if (IS_I85X(dev))
  10646. dev_priv->display.get_display_clock_speed =
  10647. i855_get_display_clock_speed;
  10648. else /* 852, 830 */
  10649. dev_priv->display.get_display_clock_speed =
  10650. i830_get_display_clock_speed;
  10651. if (IS_G4X(dev)) {
  10652. dev_priv->display.write_eld = g4x_write_eld;
  10653. } else if (IS_GEN5(dev)) {
  10654. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  10655. dev_priv->display.write_eld = ironlake_write_eld;
  10656. } else if (IS_GEN6(dev)) {
  10657. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  10658. dev_priv->display.write_eld = ironlake_write_eld;
  10659. dev_priv->display.modeset_global_resources =
  10660. snb_modeset_global_resources;
  10661. } else if (IS_IVYBRIDGE(dev)) {
  10662. /* FIXME: detect B0+ stepping and use auto training */
  10663. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  10664. dev_priv->display.write_eld = ironlake_write_eld;
  10665. dev_priv->display.modeset_global_resources =
  10666. ivb_modeset_global_resources;
  10667. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  10668. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  10669. dev_priv->display.write_eld = haswell_write_eld;
  10670. dev_priv->display.modeset_global_resources =
  10671. haswell_modeset_global_resources;
  10672. } else if (IS_VALLEYVIEW(dev)) {
  10673. dev_priv->display.modeset_global_resources =
  10674. valleyview_modeset_global_resources;
  10675. dev_priv->display.write_eld = ironlake_write_eld;
  10676. }
  10677. /* Default just returns -ENODEV to indicate unsupported */
  10678. dev_priv->display.queue_flip = intel_default_queue_flip;
  10679. switch (INTEL_INFO(dev)->gen) {
  10680. case 2:
  10681. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  10682. break;
  10683. case 3:
  10684. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  10685. break;
  10686. case 4:
  10687. case 5:
  10688. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  10689. break;
  10690. case 6:
  10691. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  10692. break;
  10693. case 7:
  10694. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  10695. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  10696. break;
  10697. }
  10698. intel_panel_init_backlight_funcs(dev);
  10699. mutex_init(&dev_priv->pps_mutex);
  10700. }
  10701. /*
  10702. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  10703. * resume, or other times. This quirk makes sure that's the case for
  10704. * affected systems.
  10705. */
  10706. static void quirk_pipea_force(struct drm_device *dev)
  10707. {
  10708. struct drm_i915_private *dev_priv = dev->dev_private;
  10709. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  10710. DRM_INFO("applying pipe a force quirk\n");
  10711. }
  10712. static void quirk_pipeb_force(struct drm_device *dev)
  10713. {
  10714. struct drm_i915_private *dev_priv = dev->dev_private;
  10715. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  10716. DRM_INFO("applying pipe b force quirk\n");
  10717. }
  10718. /*
  10719. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  10720. */
  10721. static void quirk_ssc_force_disable(struct drm_device *dev)
  10722. {
  10723. struct drm_i915_private *dev_priv = dev->dev_private;
  10724. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  10725. DRM_INFO("applying lvds SSC disable quirk\n");
  10726. }
  10727. /*
  10728. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  10729. * brightness value
  10730. */
  10731. static void quirk_invert_brightness(struct drm_device *dev)
  10732. {
  10733. struct drm_i915_private *dev_priv = dev->dev_private;
  10734. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  10735. DRM_INFO("applying inverted panel brightness quirk\n");
  10736. }
  10737. /* Some VBT's incorrectly indicate no backlight is present */
  10738. static void quirk_backlight_present(struct drm_device *dev)
  10739. {
  10740. struct drm_i915_private *dev_priv = dev->dev_private;
  10741. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  10742. DRM_INFO("applying backlight present quirk\n");
  10743. }
  10744. struct intel_quirk {
  10745. int device;
  10746. int subsystem_vendor;
  10747. int subsystem_device;
  10748. void (*hook)(struct drm_device *dev);
  10749. };
  10750. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  10751. struct intel_dmi_quirk {
  10752. void (*hook)(struct drm_device *dev);
  10753. const struct dmi_system_id (*dmi_id_list)[];
  10754. };
  10755. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  10756. {
  10757. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  10758. return 1;
  10759. }
  10760. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  10761. {
  10762. .dmi_id_list = &(const struct dmi_system_id[]) {
  10763. {
  10764. .callback = intel_dmi_reverse_brightness,
  10765. .ident = "NCR Corporation",
  10766. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  10767. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  10768. },
  10769. },
  10770. { } /* terminating entry */
  10771. },
  10772. .hook = quirk_invert_brightness,
  10773. },
  10774. };
  10775. static struct intel_quirk intel_quirks[] = {
  10776. /* HP Mini needs pipe A force quirk (LP: #322104) */
  10777. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  10778. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  10779. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  10780. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  10781. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  10782. /* 830 needs to leave pipe A & dpll A up */
  10783. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  10784. /* 830 needs to leave pipe B & dpll B up */
  10785. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  10786. /* Lenovo U160 cannot use SSC on LVDS */
  10787. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  10788. /* Sony Vaio Y cannot use SSC on LVDS */
  10789. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  10790. /* Acer Aspire 5734Z must invert backlight brightness */
  10791. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  10792. /* Acer/eMachines G725 */
  10793. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  10794. /* Acer/eMachines e725 */
  10795. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  10796. /* Acer/Packard Bell NCL20 */
  10797. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  10798. /* Acer Aspire 4736Z */
  10799. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  10800. /* Acer Aspire 5336 */
  10801. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  10802. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  10803. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  10804. /* Acer C720 Chromebook (Core i3 4005U) */
  10805. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  10806. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  10807. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  10808. /* HP Chromebook 14 (Celeron 2955U) */
  10809. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  10810. };
  10811. static void intel_init_quirks(struct drm_device *dev)
  10812. {
  10813. struct pci_dev *d = dev->pdev;
  10814. int i;
  10815. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  10816. struct intel_quirk *q = &intel_quirks[i];
  10817. if (d->device == q->device &&
  10818. (d->subsystem_vendor == q->subsystem_vendor ||
  10819. q->subsystem_vendor == PCI_ANY_ID) &&
  10820. (d->subsystem_device == q->subsystem_device ||
  10821. q->subsystem_device == PCI_ANY_ID))
  10822. q->hook(dev);
  10823. }
  10824. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  10825. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  10826. intel_dmi_quirks[i].hook(dev);
  10827. }
  10828. }
  10829. /* Disable the VGA plane that we never use */
  10830. static void i915_disable_vga(struct drm_device *dev)
  10831. {
  10832. struct drm_i915_private *dev_priv = dev->dev_private;
  10833. u8 sr1;
  10834. u32 vga_reg = i915_vgacntrl_reg(dev);
  10835. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  10836. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  10837. outb(SR01, VGA_SR_INDEX);
  10838. sr1 = inb(VGA_SR_DATA);
  10839. outb(sr1 | 1<<5, VGA_SR_DATA);
  10840. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  10841. udelay(300);
  10842. /*
  10843. * Fujitsu-Siemens Lifebook S6010 (830) has problems resuming
  10844. * from S3 without preserving (some of?) the other bits.
  10845. */
  10846. I915_WRITE(vga_reg, dev_priv->bios_vgacntr | VGA_DISP_DISABLE);
  10847. POSTING_READ(vga_reg);
  10848. }
  10849. void intel_modeset_init_hw(struct drm_device *dev)
  10850. {
  10851. intel_prepare_ddi(dev);
  10852. if (IS_VALLEYVIEW(dev))
  10853. vlv_update_cdclk(dev);
  10854. intel_init_clock_gating(dev);
  10855. intel_enable_gt_powersave(dev);
  10856. }
  10857. void intel_modeset_suspend_hw(struct drm_device *dev)
  10858. {
  10859. intel_suspend_hw(dev);
  10860. }
  10861. void intel_modeset_init(struct drm_device *dev)
  10862. {
  10863. struct drm_i915_private *dev_priv = dev->dev_private;
  10864. int sprite, ret;
  10865. enum pipe pipe;
  10866. struct intel_crtc *crtc;
  10867. drm_mode_config_init(dev);
  10868. dev->mode_config.min_width = 0;
  10869. dev->mode_config.min_height = 0;
  10870. dev->mode_config.preferred_depth = 24;
  10871. dev->mode_config.prefer_shadow = 1;
  10872. dev->mode_config.funcs = &intel_mode_funcs;
  10873. intel_init_quirks(dev);
  10874. intel_init_pm(dev);
  10875. if (INTEL_INFO(dev)->num_pipes == 0)
  10876. return;
  10877. intel_init_display(dev);
  10878. if (IS_GEN2(dev)) {
  10879. dev->mode_config.max_width = 2048;
  10880. dev->mode_config.max_height = 2048;
  10881. } else if (IS_GEN3(dev)) {
  10882. dev->mode_config.max_width = 4096;
  10883. dev->mode_config.max_height = 4096;
  10884. } else {
  10885. dev->mode_config.max_width = 8192;
  10886. dev->mode_config.max_height = 8192;
  10887. }
  10888. if (IS_845G(dev) || IS_I865G(dev)) {
  10889. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  10890. dev->mode_config.cursor_height = 1023;
  10891. } else if (IS_GEN2(dev)) {
  10892. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  10893. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  10894. } else {
  10895. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  10896. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  10897. }
  10898. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  10899. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  10900. INTEL_INFO(dev)->num_pipes,
  10901. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  10902. for_each_pipe(dev_priv, pipe) {
  10903. intel_crtc_init(dev, pipe);
  10904. for_each_sprite(pipe, sprite) {
  10905. ret = intel_plane_init(dev, pipe, sprite);
  10906. if (ret)
  10907. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  10908. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  10909. }
  10910. }
  10911. intel_init_dpio(dev);
  10912. intel_shared_dpll_init(dev);
  10913. /* save the BIOS value before clobbering it */
  10914. dev_priv->bios_vgacntr = I915_READ(i915_vgacntrl_reg(dev));
  10915. /* Just disable it once at startup */
  10916. i915_disable_vga(dev);
  10917. intel_setup_outputs(dev);
  10918. /* Just in case the BIOS is doing something questionable. */
  10919. intel_disable_fbc(dev);
  10920. drm_modeset_lock_all(dev);
  10921. intel_modeset_setup_hw_state(dev, false);
  10922. drm_modeset_unlock_all(dev);
  10923. for_each_intel_crtc(dev, crtc) {
  10924. if (!crtc->active)
  10925. continue;
  10926. /*
  10927. * Note that reserving the BIOS fb up front prevents us
  10928. * from stuffing other stolen allocations like the ring
  10929. * on top. This prevents some ugliness at boot time, and
  10930. * can even allow for smooth boot transitions if the BIOS
  10931. * fb is large enough for the active pipe configuration.
  10932. */
  10933. if (dev_priv->display.get_plane_config) {
  10934. dev_priv->display.get_plane_config(crtc,
  10935. &crtc->plane_config);
  10936. /*
  10937. * If the fb is shared between multiple heads, we'll
  10938. * just get the first one.
  10939. */
  10940. intel_find_plane_obj(crtc, &crtc->plane_config);
  10941. }
  10942. }
  10943. }
  10944. static void intel_enable_pipe_a(struct drm_device *dev)
  10945. {
  10946. struct intel_connector *connector;
  10947. struct drm_connector *crt = NULL;
  10948. struct intel_load_detect_pipe load_detect_temp;
  10949. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  10950. /* We can't just switch on the pipe A, we need to set things up with a
  10951. * proper mode and output configuration. As a gross hack, enable pipe A
  10952. * by enabling the load detect pipe once. */
  10953. list_for_each_entry(connector,
  10954. &dev->mode_config.connector_list,
  10955. base.head) {
  10956. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  10957. crt = &connector->base;
  10958. break;
  10959. }
  10960. }
  10961. if (!crt)
  10962. return;
  10963. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  10964. intel_release_load_detect_pipe(crt, &load_detect_temp);
  10965. }
  10966. static bool
  10967. intel_check_plane_mapping(struct intel_crtc *crtc)
  10968. {
  10969. struct drm_device *dev = crtc->base.dev;
  10970. struct drm_i915_private *dev_priv = dev->dev_private;
  10971. u32 reg, val;
  10972. if (INTEL_INFO(dev)->num_pipes == 1)
  10973. return true;
  10974. reg = DSPCNTR(!crtc->plane);
  10975. val = I915_READ(reg);
  10976. if ((val & DISPLAY_PLANE_ENABLE) &&
  10977. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  10978. return false;
  10979. return true;
  10980. }
  10981. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  10982. {
  10983. struct drm_device *dev = crtc->base.dev;
  10984. struct drm_i915_private *dev_priv = dev->dev_private;
  10985. u32 reg;
  10986. /* Clear any frame start delays used for debugging left by the BIOS */
  10987. reg = PIPECONF(crtc->config.cpu_transcoder);
  10988. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  10989. /* restore vblank interrupts to correct state */
  10990. if (crtc->active) {
  10991. update_scanline_offset(crtc);
  10992. drm_vblank_on(dev, crtc->pipe);
  10993. } else
  10994. drm_vblank_off(dev, crtc->pipe);
  10995. /* We need to sanitize the plane -> pipe mapping first because this will
  10996. * disable the crtc (and hence change the state) if it is wrong. Note
  10997. * that gen4+ has a fixed plane -> pipe mapping. */
  10998. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  10999. struct intel_connector *connector;
  11000. bool plane;
  11001. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  11002. crtc->base.base.id);
  11003. /* Pipe has the wrong plane attached and the plane is active.
  11004. * Temporarily change the plane mapping and disable everything
  11005. * ... */
  11006. plane = crtc->plane;
  11007. crtc->plane = !plane;
  11008. crtc->primary_enabled = true;
  11009. dev_priv->display.crtc_disable(&crtc->base);
  11010. crtc->plane = plane;
  11011. /* ... and break all links. */
  11012. list_for_each_entry(connector, &dev->mode_config.connector_list,
  11013. base.head) {
  11014. if (connector->encoder->base.crtc != &crtc->base)
  11015. continue;
  11016. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11017. connector->base.encoder = NULL;
  11018. }
  11019. /* multiple connectors may have the same encoder:
  11020. * handle them and break crtc link separately */
  11021. list_for_each_entry(connector, &dev->mode_config.connector_list,
  11022. base.head)
  11023. if (connector->encoder->base.crtc == &crtc->base) {
  11024. connector->encoder->base.crtc = NULL;
  11025. connector->encoder->connectors_active = false;
  11026. }
  11027. WARN_ON(crtc->active);
  11028. crtc->base.enabled = false;
  11029. }
  11030. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  11031. crtc->pipe == PIPE_A && !crtc->active) {
  11032. /* BIOS forgot to enable pipe A, this mostly happens after
  11033. * resume. Force-enable the pipe to fix this, the update_dpms
  11034. * call below we restore the pipe to the right state, but leave
  11035. * the required bits on. */
  11036. intel_enable_pipe_a(dev);
  11037. }
  11038. /* Adjust the state of the output pipe according to whether we
  11039. * have active connectors/encoders. */
  11040. intel_crtc_update_dpms(&crtc->base);
  11041. if (crtc->active != crtc->base.enabled) {
  11042. struct intel_encoder *encoder;
  11043. /* This can happen either due to bugs in the get_hw_state
  11044. * functions or because the pipe is force-enabled due to the
  11045. * pipe A quirk. */
  11046. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  11047. crtc->base.base.id,
  11048. crtc->base.enabled ? "enabled" : "disabled",
  11049. crtc->active ? "enabled" : "disabled");
  11050. crtc->base.enabled = crtc->active;
  11051. /* Because we only establish the connector -> encoder ->
  11052. * crtc links if something is active, this means the
  11053. * crtc is now deactivated. Break the links. connector
  11054. * -> encoder links are only establish when things are
  11055. * actually up, hence no need to break them. */
  11056. WARN_ON(crtc->active);
  11057. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  11058. WARN_ON(encoder->connectors_active);
  11059. encoder->base.crtc = NULL;
  11060. }
  11061. }
  11062. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  11063. /*
  11064. * We start out with underrun reporting disabled to avoid races.
  11065. * For correct bookkeeping mark this on active crtcs.
  11066. *
  11067. * Also on gmch platforms we dont have any hardware bits to
  11068. * disable the underrun reporting. Which means we need to start
  11069. * out with underrun reporting disabled also on inactive pipes,
  11070. * since otherwise we'll complain about the garbage we read when
  11071. * e.g. coming up after runtime pm.
  11072. *
  11073. * No protection against concurrent access is required - at
  11074. * worst a fifo underrun happens which also sets this to false.
  11075. */
  11076. crtc->cpu_fifo_underrun_disabled = true;
  11077. crtc->pch_fifo_underrun_disabled = true;
  11078. }
  11079. }
  11080. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  11081. {
  11082. struct intel_connector *connector;
  11083. struct drm_device *dev = encoder->base.dev;
  11084. /* We need to check both for a crtc link (meaning that the
  11085. * encoder is active and trying to read from a pipe) and the
  11086. * pipe itself being active. */
  11087. bool has_active_crtc = encoder->base.crtc &&
  11088. to_intel_crtc(encoder->base.crtc)->active;
  11089. if (encoder->connectors_active && !has_active_crtc) {
  11090. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  11091. encoder->base.base.id,
  11092. encoder->base.name);
  11093. /* Connector is active, but has no active pipe. This is
  11094. * fallout from our resume register restoring. Disable
  11095. * the encoder manually again. */
  11096. if (encoder->base.crtc) {
  11097. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  11098. encoder->base.base.id,
  11099. encoder->base.name);
  11100. encoder->disable(encoder);
  11101. if (encoder->post_disable)
  11102. encoder->post_disable(encoder);
  11103. }
  11104. encoder->base.crtc = NULL;
  11105. encoder->connectors_active = false;
  11106. /* Inconsistent output/port/pipe state happens presumably due to
  11107. * a bug in one of the get_hw_state functions. Or someplace else
  11108. * in our code, like the register restore mess on resume. Clamp
  11109. * things to off as a safer default. */
  11110. list_for_each_entry(connector,
  11111. &dev->mode_config.connector_list,
  11112. base.head) {
  11113. if (connector->encoder != encoder)
  11114. continue;
  11115. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11116. connector->base.encoder = NULL;
  11117. }
  11118. }
  11119. /* Enabled encoders without active connectors will be fixed in
  11120. * the crtc fixup. */
  11121. }
  11122. void i915_redisable_vga_power_on(struct drm_device *dev)
  11123. {
  11124. struct drm_i915_private *dev_priv = dev->dev_private;
  11125. u32 vga_reg = i915_vgacntrl_reg(dev);
  11126. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  11127. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  11128. i915_disable_vga(dev);
  11129. }
  11130. }
  11131. void i915_redisable_vga(struct drm_device *dev)
  11132. {
  11133. struct drm_i915_private *dev_priv = dev->dev_private;
  11134. /* This function can be called both from intel_modeset_setup_hw_state or
  11135. * at a very early point in our resume sequence, where the power well
  11136. * structures are not yet restored. Since this function is at a very
  11137. * paranoid "someone might have enabled VGA while we were not looking"
  11138. * level, just check if the power well is enabled instead of trying to
  11139. * follow the "don't touch the power well if we don't need it" policy
  11140. * the rest of the driver uses. */
  11141. if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_VGA))
  11142. return;
  11143. i915_redisable_vga_power_on(dev);
  11144. }
  11145. static bool primary_get_hw_state(struct intel_crtc *crtc)
  11146. {
  11147. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  11148. if (!crtc->active)
  11149. return false;
  11150. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  11151. }
  11152. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  11153. {
  11154. struct drm_i915_private *dev_priv = dev->dev_private;
  11155. enum pipe pipe;
  11156. struct intel_crtc *crtc;
  11157. struct intel_encoder *encoder;
  11158. struct intel_connector *connector;
  11159. int i;
  11160. for_each_intel_crtc(dev, crtc) {
  11161. memset(&crtc->config, 0, sizeof(crtc->config));
  11162. crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  11163. crtc->active = dev_priv->display.get_pipe_config(crtc,
  11164. &crtc->config);
  11165. crtc->base.enabled = crtc->active;
  11166. crtc->primary_enabled = primary_get_hw_state(crtc);
  11167. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  11168. crtc->base.base.id,
  11169. crtc->active ? "enabled" : "disabled");
  11170. }
  11171. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11172. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11173. pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
  11174. pll->active = 0;
  11175. for_each_intel_crtc(dev, crtc) {
  11176. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  11177. pll->active++;
  11178. }
  11179. pll->refcount = pll->active;
  11180. DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
  11181. pll->name, pll->refcount, pll->on);
  11182. if (pll->refcount)
  11183. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  11184. }
  11185. for_each_intel_encoder(dev, encoder) {
  11186. pipe = 0;
  11187. if (encoder->get_hw_state(encoder, &pipe)) {
  11188. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11189. encoder->base.crtc = &crtc->base;
  11190. encoder->get_config(encoder, &crtc->config);
  11191. } else {
  11192. encoder->base.crtc = NULL;
  11193. }
  11194. encoder->connectors_active = false;
  11195. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  11196. encoder->base.base.id,
  11197. encoder->base.name,
  11198. encoder->base.crtc ? "enabled" : "disabled",
  11199. pipe_name(pipe));
  11200. }
  11201. list_for_each_entry(connector, &dev->mode_config.connector_list,
  11202. base.head) {
  11203. if (connector->get_hw_state(connector)) {
  11204. connector->base.dpms = DRM_MODE_DPMS_ON;
  11205. connector->encoder->connectors_active = true;
  11206. connector->base.encoder = &connector->encoder->base;
  11207. } else {
  11208. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11209. connector->base.encoder = NULL;
  11210. }
  11211. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  11212. connector->base.base.id,
  11213. connector->base.name,
  11214. connector->base.encoder ? "enabled" : "disabled");
  11215. }
  11216. }
  11217. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  11218. * and i915 state tracking structures. */
  11219. void intel_modeset_setup_hw_state(struct drm_device *dev,
  11220. bool force_restore)
  11221. {
  11222. struct drm_i915_private *dev_priv = dev->dev_private;
  11223. enum pipe pipe;
  11224. struct intel_crtc *crtc;
  11225. struct intel_encoder *encoder;
  11226. int i;
  11227. intel_modeset_readout_hw_state(dev);
  11228. /*
  11229. * Now that we have the config, copy it to each CRTC struct
  11230. * Note that this could go away if we move to using crtc_config
  11231. * checking everywhere.
  11232. */
  11233. for_each_intel_crtc(dev, crtc) {
  11234. if (crtc->active && i915.fastboot) {
  11235. intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
  11236. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  11237. crtc->base.base.id);
  11238. drm_mode_debug_printmodeline(&crtc->base.mode);
  11239. }
  11240. }
  11241. /* HW state is read out, now we need to sanitize this mess. */
  11242. for_each_intel_encoder(dev, encoder) {
  11243. intel_sanitize_encoder(encoder);
  11244. }
  11245. for_each_pipe(dev_priv, pipe) {
  11246. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11247. intel_sanitize_crtc(crtc);
  11248. intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
  11249. }
  11250. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11251. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11252. if (!pll->on || pll->active)
  11253. continue;
  11254. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  11255. pll->disable(dev_priv, pll);
  11256. pll->on = false;
  11257. }
  11258. if (HAS_PCH_SPLIT(dev))
  11259. ilk_wm_get_hw_state(dev);
  11260. if (force_restore) {
  11261. i915_redisable_vga(dev);
  11262. /*
  11263. * We need to use raw interfaces for restoring state to avoid
  11264. * checking (bogus) intermediate states.
  11265. */
  11266. for_each_pipe(dev_priv, pipe) {
  11267. struct drm_crtc *crtc =
  11268. dev_priv->pipe_to_crtc_mapping[pipe];
  11269. __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
  11270. crtc->primary->fb);
  11271. }
  11272. } else {
  11273. intel_modeset_update_staged_output_state(dev);
  11274. }
  11275. intel_modeset_check_state(dev);
  11276. }
  11277. void intel_modeset_gem_init(struct drm_device *dev)
  11278. {
  11279. struct drm_crtc *c;
  11280. struct drm_i915_gem_object *obj;
  11281. mutex_lock(&dev->struct_mutex);
  11282. intel_init_gt_powersave(dev);
  11283. mutex_unlock(&dev->struct_mutex);
  11284. intel_modeset_init_hw(dev);
  11285. intel_setup_overlay(dev);
  11286. /*
  11287. * Make sure any fbs we allocated at startup are properly
  11288. * pinned & fenced. When we do the allocation it's too early
  11289. * for this.
  11290. */
  11291. mutex_lock(&dev->struct_mutex);
  11292. for_each_crtc(dev, c) {
  11293. obj = intel_fb_obj(c->primary->fb);
  11294. if (obj == NULL)
  11295. continue;
  11296. if (intel_pin_and_fence_fb_obj(dev, obj, NULL)) {
  11297. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  11298. to_intel_crtc(c)->pipe);
  11299. drm_framebuffer_unreference(c->primary->fb);
  11300. c->primary->fb = NULL;
  11301. }
  11302. }
  11303. mutex_unlock(&dev->struct_mutex);
  11304. }
  11305. void intel_connector_unregister(struct intel_connector *intel_connector)
  11306. {
  11307. struct drm_connector *connector = &intel_connector->base;
  11308. intel_panel_destroy_backlight(connector);
  11309. drm_connector_unregister(connector);
  11310. }
  11311. void intel_modeset_cleanup(struct drm_device *dev)
  11312. {
  11313. struct drm_i915_private *dev_priv = dev->dev_private;
  11314. struct drm_connector *connector;
  11315. /*
  11316. * Interrupts and polling as the first thing to avoid creating havoc.
  11317. * Too much stuff here (turning of rps, connectors, ...) would
  11318. * experience fancy races otherwise.
  11319. */
  11320. drm_irq_uninstall(dev);
  11321. intel_hpd_cancel_work(dev_priv);
  11322. dev_priv->pm._irqs_disabled = true;
  11323. /*
  11324. * Due to the hpd irq storm handling the hotplug work can re-arm the
  11325. * poll handlers. Hence disable polling after hpd handling is shut down.
  11326. */
  11327. drm_kms_helper_poll_fini(dev);
  11328. mutex_lock(&dev->struct_mutex);
  11329. intel_unregister_dsm_handler();
  11330. intel_disable_fbc(dev);
  11331. intel_disable_gt_powersave(dev);
  11332. ironlake_teardown_rc6(dev);
  11333. mutex_unlock(&dev->struct_mutex);
  11334. /* flush any delayed tasks or pending work */
  11335. flush_scheduled_work();
  11336. /* destroy the backlight and sysfs files before encoders/connectors */
  11337. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  11338. struct intel_connector *intel_connector;
  11339. intel_connector = to_intel_connector(connector);
  11340. intel_connector->unregister(intel_connector);
  11341. }
  11342. drm_mode_config_cleanup(dev);
  11343. intel_cleanup_overlay(dev);
  11344. mutex_lock(&dev->struct_mutex);
  11345. intel_cleanup_gt_powersave(dev);
  11346. mutex_unlock(&dev->struct_mutex);
  11347. }
  11348. /*
  11349. * Return which encoder is currently attached for connector.
  11350. */
  11351. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  11352. {
  11353. return &intel_attached_encoder(connector)->base;
  11354. }
  11355. void intel_connector_attach_encoder(struct intel_connector *connector,
  11356. struct intel_encoder *encoder)
  11357. {
  11358. connector->encoder = encoder;
  11359. drm_mode_connector_attach_encoder(&connector->base,
  11360. &encoder->base);
  11361. }
  11362. /*
  11363. * set vga decode state - true == enable VGA decode
  11364. */
  11365. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  11366. {
  11367. struct drm_i915_private *dev_priv = dev->dev_private;
  11368. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  11369. u16 gmch_ctrl;
  11370. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  11371. DRM_ERROR("failed to read control word\n");
  11372. return -EIO;
  11373. }
  11374. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  11375. return 0;
  11376. if (state)
  11377. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  11378. else
  11379. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  11380. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  11381. DRM_ERROR("failed to write control word\n");
  11382. return -EIO;
  11383. }
  11384. return 0;
  11385. }
  11386. struct intel_display_error_state {
  11387. u32 power_well_driver;
  11388. int num_transcoders;
  11389. struct intel_cursor_error_state {
  11390. u32 control;
  11391. u32 position;
  11392. u32 base;
  11393. u32 size;
  11394. } cursor[I915_MAX_PIPES];
  11395. struct intel_pipe_error_state {
  11396. bool power_domain_on;
  11397. u32 source;
  11398. u32 stat;
  11399. } pipe[I915_MAX_PIPES];
  11400. struct intel_plane_error_state {
  11401. u32 control;
  11402. u32 stride;
  11403. u32 size;
  11404. u32 pos;
  11405. u32 addr;
  11406. u32 surface;
  11407. u32 tile_offset;
  11408. } plane[I915_MAX_PIPES];
  11409. struct intel_transcoder_error_state {
  11410. bool power_domain_on;
  11411. enum transcoder cpu_transcoder;
  11412. u32 conf;
  11413. u32 htotal;
  11414. u32 hblank;
  11415. u32 hsync;
  11416. u32 vtotal;
  11417. u32 vblank;
  11418. u32 vsync;
  11419. } transcoder[4];
  11420. };
  11421. struct intel_display_error_state *
  11422. intel_display_capture_error_state(struct drm_device *dev)
  11423. {
  11424. struct drm_i915_private *dev_priv = dev->dev_private;
  11425. struct intel_display_error_state *error;
  11426. int transcoders[] = {
  11427. TRANSCODER_A,
  11428. TRANSCODER_B,
  11429. TRANSCODER_C,
  11430. TRANSCODER_EDP,
  11431. };
  11432. int i;
  11433. if (INTEL_INFO(dev)->num_pipes == 0)
  11434. return NULL;
  11435. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  11436. if (error == NULL)
  11437. return NULL;
  11438. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11439. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  11440. for_each_pipe(dev_priv, i) {
  11441. error->pipe[i].power_domain_on =
  11442. intel_display_power_enabled_unlocked(dev_priv,
  11443. POWER_DOMAIN_PIPE(i));
  11444. if (!error->pipe[i].power_domain_on)
  11445. continue;
  11446. error->cursor[i].control = I915_READ(CURCNTR(i));
  11447. error->cursor[i].position = I915_READ(CURPOS(i));
  11448. error->cursor[i].base = I915_READ(CURBASE(i));
  11449. error->plane[i].control = I915_READ(DSPCNTR(i));
  11450. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  11451. if (INTEL_INFO(dev)->gen <= 3) {
  11452. error->plane[i].size = I915_READ(DSPSIZE(i));
  11453. error->plane[i].pos = I915_READ(DSPPOS(i));
  11454. }
  11455. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11456. error->plane[i].addr = I915_READ(DSPADDR(i));
  11457. if (INTEL_INFO(dev)->gen >= 4) {
  11458. error->plane[i].surface = I915_READ(DSPSURF(i));
  11459. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  11460. }
  11461. error->pipe[i].source = I915_READ(PIPESRC(i));
  11462. if (HAS_GMCH_DISPLAY(dev))
  11463. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  11464. }
  11465. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  11466. if (HAS_DDI(dev_priv->dev))
  11467. error->num_transcoders++; /* Account for eDP. */
  11468. for (i = 0; i < error->num_transcoders; i++) {
  11469. enum transcoder cpu_transcoder = transcoders[i];
  11470. error->transcoder[i].power_domain_on =
  11471. intel_display_power_enabled_unlocked(dev_priv,
  11472. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  11473. if (!error->transcoder[i].power_domain_on)
  11474. continue;
  11475. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  11476. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  11477. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  11478. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  11479. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  11480. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  11481. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  11482. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  11483. }
  11484. return error;
  11485. }
  11486. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  11487. void
  11488. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  11489. struct drm_device *dev,
  11490. struct intel_display_error_state *error)
  11491. {
  11492. struct drm_i915_private *dev_priv = dev->dev_private;
  11493. int i;
  11494. if (!error)
  11495. return;
  11496. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  11497. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11498. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  11499. error->power_well_driver);
  11500. for_each_pipe(dev_priv, i) {
  11501. err_printf(m, "Pipe [%d]:\n", i);
  11502. err_printf(m, " Power: %s\n",
  11503. error->pipe[i].power_domain_on ? "on" : "off");
  11504. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  11505. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  11506. err_printf(m, "Plane [%d]:\n", i);
  11507. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  11508. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  11509. if (INTEL_INFO(dev)->gen <= 3) {
  11510. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  11511. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  11512. }
  11513. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11514. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  11515. if (INTEL_INFO(dev)->gen >= 4) {
  11516. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  11517. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  11518. }
  11519. err_printf(m, "Cursor [%d]:\n", i);
  11520. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  11521. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  11522. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  11523. }
  11524. for (i = 0; i < error->num_transcoders; i++) {
  11525. err_printf(m, "CPU transcoder: %c\n",
  11526. transcoder_name(error->transcoder[i].cpu_transcoder));
  11527. err_printf(m, " Power: %s\n",
  11528. error->transcoder[i].power_domain_on ? "on" : "off");
  11529. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  11530. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  11531. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  11532. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  11533. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  11534. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  11535. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  11536. }
  11537. }
  11538. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  11539. {
  11540. struct intel_crtc *crtc;
  11541. for_each_intel_crtc(dev, crtc) {
  11542. struct intel_unpin_work *work;
  11543. unsigned long irqflags;
  11544. spin_lock_irqsave(&dev->event_lock, irqflags);
  11545. work = crtc->unpin_work;
  11546. if (work && work->event &&
  11547. work->event->base.file_priv == file) {
  11548. kfree(work->event);
  11549. work->event = NULL;
  11550. }
  11551. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  11552. }
  11553. }