intel_display.c 370 KB

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