intel_display.c 370 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422
  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 struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
  95. {
  96. if (!connector->mst_port)
  97. return connector->encoder;
  98. else
  99. return &connector->mst_port->mst_encoders[pipe]->base;
  100. }
  101. typedef struct {
  102. int min, max;
  103. } intel_range_t;
  104. typedef struct {
  105. int dot_limit;
  106. int p2_slow, p2_fast;
  107. } intel_p2_t;
  108. typedef struct intel_limit intel_limit_t;
  109. struct intel_limit {
  110. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  111. intel_p2_t p2;
  112. };
  113. int
  114. intel_pch_rawclk(struct drm_device *dev)
  115. {
  116. struct drm_i915_private *dev_priv = dev->dev_private;
  117. WARN_ON(!HAS_PCH_SPLIT(dev));
  118. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  119. }
  120. static inline u32 /* units of 100MHz */
  121. intel_fdi_link_freq(struct drm_device *dev)
  122. {
  123. if (IS_GEN5(dev)) {
  124. struct drm_i915_private *dev_priv = dev->dev_private;
  125. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  126. } else
  127. return 27;
  128. }
  129. static const intel_limit_t intel_limits_i8xx_dac = {
  130. .dot = { .min = 25000, .max = 350000 },
  131. .vco = { .min = 908000, .max = 1512000 },
  132. .n = { .min = 2, .max = 16 },
  133. .m = { .min = 96, .max = 140 },
  134. .m1 = { .min = 18, .max = 26 },
  135. .m2 = { .min = 6, .max = 16 },
  136. .p = { .min = 4, .max = 128 },
  137. .p1 = { .min = 2, .max = 33 },
  138. .p2 = { .dot_limit = 165000,
  139. .p2_slow = 4, .p2_fast = 2 },
  140. };
  141. static const intel_limit_t intel_limits_i8xx_dvo = {
  142. .dot = { .min = 25000, .max = 350000 },
  143. .vco = { .min = 908000, .max = 1512000 },
  144. .n = { .min = 2, .max = 16 },
  145. .m = { .min = 96, .max = 140 },
  146. .m1 = { .min = 18, .max = 26 },
  147. .m2 = { .min = 6, .max = 16 },
  148. .p = { .min = 4, .max = 128 },
  149. .p1 = { .min = 2, .max = 33 },
  150. .p2 = { .dot_limit = 165000,
  151. .p2_slow = 4, .p2_fast = 4 },
  152. };
  153. static const intel_limit_t intel_limits_i8xx_lvds = {
  154. .dot = { .min = 25000, .max = 350000 },
  155. .vco = { .min = 908000, .max = 1512000 },
  156. .n = { .min = 2, .max = 16 },
  157. .m = { .min = 96, .max = 140 },
  158. .m1 = { .min = 18, .max = 26 },
  159. .m2 = { .min = 6, .max = 16 },
  160. .p = { .min = 4, .max = 128 },
  161. .p1 = { .min = 1, .max = 6 },
  162. .p2 = { .dot_limit = 165000,
  163. .p2_slow = 14, .p2_fast = 7 },
  164. };
  165. static const intel_limit_t intel_limits_i9xx_sdvo = {
  166. .dot = { .min = 20000, .max = 400000 },
  167. .vco = { .min = 1400000, .max = 2800000 },
  168. .n = { .min = 1, .max = 6 },
  169. .m = { .min = 70, .max = 120 },
  170. .m1 = { .min = 8, .max = 18 },
  171. .m2 = { .min = 3, .max = 7 },
  172. .p = { .min = 5, .max = 80 },
  173. .p1 = { .min = 1, .max = 8 },
  174. .p2 = { .dot_limit = 200000,
  175. .p2_slow = 10, .p2_fast = 5 },
  176. };
  177. static const intel_limit_t intel_limits_i9xx_lvds = {
  178. .dot = { .min = 20000, .max = 400000 },
  179. .vco = { .min = 1400000, .max = 2800000 },
  180. .n = { .min = 1, .max = 6 },
  181. .m = { .min = 70, .max = 120 },
  182. .m1 = { .min = 8, .max = 18 },
  183. .m2 = { .min = 3, .max = 7 },
  184. .p = { .min = 7, .max = 98 },
  185. .p1 = { .min = 1, .max = 8 },
  186. .p2 = { .dot_limit = 112000,
  187. .p2_slow = 14, .p2_fast = 7 },
  188. };
  189. static const intel_limit_t intel_limits_g4x_sdvo = {
  190. .dot = { .min = 25000, .max = 270000 },
  191. .vco = { .min = 1750000, .max = 3500000},
  192. .n = { .min = 1, .max = 4 },
  193. .m = { .min = 104, .max = 138 },
  194. .m1 = { .min = 17, .max = 23 },
  195. .m2 = { .min = 5, .max = 11 },
  196. .p = { .min = 10, .max = 30 },
  197. .p1 = { .min = 1, .max = 3},
  198. .p2 = { .dot_limit = 270000,
  199. .p2_slow = 10,
  200. .p2_fast = 10
  201. },
  202. };
  203. static const intel_limit_t intel_limits_g4x_hdmi = {
  204. .dot = { .min = 22000, .max = 400000 },
  205. .vco = { .min = 1750000, .max = 3500000},
  206. .n = { .min = 1, .max = 4 },
  207. .m = { .min = 104, .max = 138 },
  208. .m1 = { .min = 16, .max = 23 },
  209. .m2 = { .min = 5, .max = 11 },
  210. .p = { .min = 5, .max = 80 },
  211. .p1 = { .min = 1, .max = 8},
  212. .p2 = { .dot_limit = 165000,
  213. .p2_slow = 10, .p2_fast = 5 },
  214. };
  215. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  216. .dot = { .min = 20000, .max = 115000 },
  217. .vco = { .min = 1750000, .max = 3500000 },
  218. .n = { .min = 1, .max = 3 },
  219. .m = { .min = 104, .max = 138 },
  220. .m1 = { .min = 17, .max = 23 },
  221. .m2 = { .min = 5, .max = 11 },
  222. .p = { .min = 28, .max = 112 },
  223. .p1 = { .min = 2, .max = 8 },
  224. .p2 = { .dot_limit = 0,
  225. .p2_slow = 14, .p2_fast = 14
  226. },
  227. };
  228. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  229. .dot = { .min = 80000, .max = 224000 },
  230. .vco = { .min = 1750000, .max = 3500000 },
  231. .n = { .min = 1, .max = 3 },
  232. .m = { .min = 104, .max = 138 },
  233. .m1 = { .min = 17, .max = 23 },
  234. .m2 = { .min = 5, .max = 11 },
  235. .p = { .min = 14, .max = 42 },
  236. .p1 = { .min = 2, .max = 6 },
  237. .p2 = { .dot_limit = 0,
  238. .p2_slow = 7, .p2_fast = 7
  239. },
  240. };
  241. static const intel_limit_t intel_limits_pineview_sdvo = {
  242. .dot = { .min = 20000, .max = 400000},
  243. .vco = { .min = 1700000, .max = 3500000 },
  244. /* Pineview's Ncounter is a ring counter */
  245. .n = { .min = 3, .max = 6 },
  246. .m = { .min = 2, .max = 256 },
  247. /* Pineview only has one combined m divider, which we treat as m2. */
  248. .m1 = { .min = 0, .max = 0 },
  249. .m2 = { .min = 0, .max = 254 },
  250. .p = { .min = 5, .max = 80 },
  251. .p1 = { .min = 1, .max = 8 },
  252. .p2 = { .dot_limit = 200000,
  253. .p2_slow = 10, .p2_fast = 5 },
  254. };
  255. static const intel_limit_t intel_limits_pineview_lvds = {
  256. .dot = { .min = 20000, .max = 400000 },
  257. .vco = { .min = 1700000, .max = 3500000 },
  258. .n = { .min = 3, .max = 6 },
  259. .m = { .min = 2, .max = 256 },
  260. .m1 = { .min = 0, .max = 0 },
  261. .m2 = { .min = 0, .max = 254 },
  262. .p = { .min = 7, .max = 112 },
  263. .p1 = { .min = 1, .max = 8 },
  264. .p2 = { .dot_limit = 112000,
  265. .p2_slow = 14, .p2_fast = 14 },
  266. };
  267. /* Ironlake / Sandybridge
  268. *
  269. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  270. * the range value for them is (actual_value - 2).
  271. */
  272. static const intel_limit_t intel_limits_ironlake_dac = {
  273. .dot = { .min = 25000, .max = 350000 },
  274. .vco = { .min = 1760000, .max = 3510000 },
  275. .n = { .min = 1, .max = 5 },
  276. .m = { .min = 79, .max = 127 },
  277. .m1 = { .min = 12, .max = 22 },
  278. .m2 = { .min = 5, .max = 9 },
  279. .p = { .min = 5, .max = 80 },
  280. .p1 = { .min = 1, .max = 8 },
  281. .p2 = { .dot_limit = 225000,
  282. .p2_slow = 10, .p2_fast = 5 },
  283. };
  284. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  285. .dot = { .min = 25000, .max = 350000 },
  286. .vco = { .min = 1760000, .max = 3510000 },
  287. .n = { .min = 1, .max = 3 },
  288. .m = { .min = 79, .max = 118 },
  289. .m1 = { .min = 12, .max = 22 },
  290. .m2 = { .min = 5, .max = 9 },
  291. .p = { .min = 28, .max = 112 },
  292. .p1 = { .min = 2, .max = 8 },
  293. .p2 = { .dot_limit = 225000,
  294. .p2_slow = 14, .p2_fast = 14 },
  295. };
  296. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  297. .dot = { .min = 25000, .max = 350000 },
  298. .vco = { .min = 1760000, .max = 3510000 },
  299. .n = { .min = 1, .max = 3 },
  300. .m = { .min = 79, .max = 127 },
  301. .m1 = { .min = 12, .max = 22 },
  302. .m2 = { .min = 5, .max = 9 },
  303. .p = { .min = 14, .max = 56 },
  304. .p1 = { .min = 2, .max = 8 },
  305. .p2 = { .dot_limit = 225000,
  306. .p2_slow = 7, .p2_fast = 7 },
  307. };
  308. /* LVDS 100mhz refclk limits. */
  309. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  310. .dot = { .min = 25000, .max = 350000 },
  311. .vco = { .min = 1760000, .max = 3510000 },
  312. .n = { .min = 1, .max = 2 },
  313. .m = { .min = 79, .max = 126 },
  314. .m1 = { .min = 12, .max = 22 },
  315. .m2 = { .min = 5, .max = 9 },
  316. .p = { .min = 28, .max = 112 },
  317. .p1 = { .min = 2, .max = 8 },
  318. .p2 = { .dot_limit = 225000,
  319. .p2_slow = 14, .p2_fast = 14 },
  320. };
  321. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  322. .dot = { .min = 25000, .max = 350000 },
  323. .vco = { .min = 1760000, .max = 3510000 },
  324. .n = { .min = 1, .max = 3 },
  325. .m = { .min = 79, .max = 126 },
  326. .m1 = { .min = 12, .max = 22 },
  327. .m2 = { .min = 5, .max = 9 },
  328. .p = { .min = 14, .max = 42 },
  329. .p1 = { .min = 2, .max = 6 },
  330. .p2 = { .dot_limit = 225000,
  331. .p2_slow = 7, .p2_fast = 7 },
  332. };
  333. static const intel_limit_t intel_limits_vlv = {
  334. /*
  335. * These are the data rate limits (measured in fast clocks)
  336. * since those are the strictest limits we have. The fast
  337. * clock and actual rate limits are more relaxed, so checking
  338. * them would make no difference.
  339. */
  340. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  341. .vco = { .min = 4000000, .max = 6000000 },
  342. .n = { .min = 1, .max = 7 },
  343. .m1 = { .min = 2, .max = 3 },
  344. .m2 = { .min = 11, .max = 156 },
  345. .p1 = { .min = 2, .max = 3 },
  346. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  347. };
  348. static const intel_limit_t intel_limits_chv = {
  349. /*
  350. * These are the data rate limits (measured in fast clocks)
  351. * since those are the strictest limits we have. The fast
  352. * clock and actual rate limits are more relaxed, so checking
  353. * them would make no difference.
  354. */
  355. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  356. .vco = { .min = 4860000, .max = 6700000 },
  357. .n = { .min = 1, .max = 1 },
  358. .m1 = { .min = 2, .max = 2 },
  359. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  360. .p1 = { .min = 2, .max = 4 },
  361. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  362. };
  363. static void vlv_clock(int refclk, intel_clock_t *clock)
  364. {
  365. clock->m = clock->m1 * clock->m2;
  366. clock->p = clock->p1 * clock->p2;
  367. if (WARN_ON(clock->n == 0 || clock->p == 0))
  368. return;
  369. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  370. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  371. }
  372. /**
  373. * Returns whether any output on the specified pipe is of the specified type
  374. */
  375. static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
  376. {
  377. struct drm_device *dev = crtc->dev;
  378. struct intel_encoder *encoder;
  379. for_each_encoder_on_crtc(dev, crtc, encoder)
  380. if (encoder->type == type)
  381. return true;
  382. return false;
  383. }
  384. static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
  385. int refclk)
  386. {
  387. struct drm_device *dev = crtc->dev;
  388. const intel_limit_t *limit;
  389. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  390. if (intel_is_dual_link_lvds(dev)) {
  391. if (refclk == 100000)
  392. limit = &intel_limits_ironlake_dual_lvds_100m;
  393. else
  394. limit = &intel_limits_ironlake_dual_lvds;
  395. } else {
  396. if (refclk == 100000)
  397. limit = &intel_limits_ironlake_single_lvds_100m;
  398. else
  399. limit = &intel_limits_ironlake_single_lvds;
  400. }
  401. } else
  402. limit = &intel_limits_ironlake_dac;
  403. return limit;
  404. }
  405. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  406. {
  407. struct drm_device *dev = crtc->dev;
  408. const intel_limit_t *limit;
  409. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  410. if (intel_is_dual_link_lvds(dev))
  411. limit = &intel_limits_g4x_dual_channel_lvds;
  412. else
  413. limit = &intel_limits_g4x_single_channel_lvds;
  414. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  415. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  416. limit = &intel_limits_g4x_hdmi;
  417. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  418. limit = &intel_limits_g4x_sdvo;
  419. } else /* The option is for other outputs */
  420. limit = &intel_limits_i9xx_sdvo;
  421. return limit;
  422. }
  423. static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
  424. {
  425. struct drm_device *dev = crtc->dev;
  426. const intel_limit_t *limit;
  427. if (HAS_PCH_SPLIT(dev))
  428. limit = intel_ironlake_limit(crtc, refclk);
  429. else if (IS_G4X(dev)) {
  430. limit = intel_g4x_limit(crtc);
  431. } else if (IS_PINEVIEW(dev)) {
  432. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  433. limit = &intel_limits_pineview_lvds;
  434. else
  435. limit = &intel_limits_pineview_sdvo;
  436. } else if (IS_CHERRYVIEW(dev)) {
  437. limit = &intel_limits_chv;
  438. } else if (IS_VALLEYVIEW(dev)) {
  439. limit = &intel_limits_vlv;
  440. } else if (!IS_GEN2(dev)) {
  441. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  442. limit = &intel_limits_i9xx_lvds;
  443. else
  444. limit = &intel_limits_i9xx_sdvo;
  445. } else {
  446. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  447. limit = &intel_limits_i8xx_lvds;
  448. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
  449. limit = &intel_limits_i8xx_dvo;
  450. else
  451. limit = &intel_limits_i8xx_dac;
  452. }
  453. return limit;
  454. }
  455. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  456. static void pineview_clock(int refclk, intel_clock_t *clock)
  457. {
  458. clock->m = clock->m2 + 2;
  459. clock->p = clock->p1 * clock->p2;
  460. if (WARN_ON(clock->n == 0 || clock->p == 0))
  461. return;
  462. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  463. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  464. }
  465. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  466. {
  467. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  468. }
  469. static void i9xx_clock(int refclk, intel_clock_t *clock)
  470. {
  471. clock->m = i9xx_dpll_compute_m(clock);
  472. clock->p = clock->p1 * clock->p2;
  473. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  474. return;
  475. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  476. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  477. }
  478. static void chv_clock(int refclk, intel_clock_t *clock)
  479. {
  480. clock->m = clock->m1 * clock->m2;
  481. clock->p = clock->p1 * clock->p2;
  482. if (WARN_ON(clock->n == 0 || clock->p == 0))
  483. return;
  484. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  485. clock->n << 22);
  486. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  487. }
  488. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  489. /**
  490. * Returns whether the given set of divisors are valid for a given refclk with
  491. * the given connectors.
  492. */
  493. static bool intel_PLL_is_valid(struct drm_device *dev,
  494. const intel_limit_t *limit,
  495. const intel_clock_t *clock)
  496. {
  497. if (clock->n < limit->n.min || limit->n.max < clock->n)
  498. INTELPllInvalid("n out of range\n");
  499. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  500. INTELPllInvalid("p1 out of range\n");
  501. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  502. INTELPllInvalid("m2 out of range\n");
  503. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  504. INTELPllInvalid("m1 out of range\n");
  505. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
  506. if (clock->m1 <= clock->m2)
  507. INTELPllInvalid("m1 <= m2\n");
  508. if (!IS_VALLEYVIEW(dev)) {
  509. if (clock->p < limit->p.min || limit->p.max < clock->p)
  510. INTELPllInvalid("p out of range\n");
  511. if (clock->m < limit->m.min || limit->m.max < clock->m)
  512. INTELPllInvalid("m out of range\n");
  513. }
  514. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  515. INTELPllInvalid("vco out of range\n");
  516. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  517. * connector, etc., rather than just a single range.
  518. */
  519. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  520. INTELPllInvalid("dot out of range\n");
  521. return true;
  522. }
  523. static bool
  524. i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  525. int target, int refclk, intel_clock_t *match_clock,
  526. intel_clock_t *best_clock)
  527. {
  528. struct drm_device *dev = crtc->dev;
  529. intel_clock_t clock;
  530. int err = target;
  531. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  532. /*
  533. * For LVDS just rely on its current settings for dual-channel.
  534. * We haven't figured out how to reliably set up different
  535. * single/dual channel state, if we even can.
  536. */
  537. if (intel_is_dual_link_lvds(dev))
  538. clock.p2 = limit->p2.p2_fast;
  539. else
  540. clock.p2 = limit->p2.p2_slow;
  541. } else {
  542. if (target < limit->p2.dot_limit)
  543. clock.p2 = limit->p2.p2_slow;
  544. else
  545. clock.p2 = limit->p2.p2_fast;
  546. }
  547. memset(best_clock, 0, sizeof(*best_clock));
  548. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  549. clock.m1++) {
  550. for (clock.m2 = limit->m2.min;
  551. clock.m2 <= limit->m2.max; clock.m2++) {
  552. if (clock.m2 >= clock.m1)
  553. break;
  554. for (clock.n = limit->n.min;
  555. clock.n <= limit->n.max; clock.n++) {
  556. for (clock.p1 = limit->p1.min;
  557. clock.p1 <= limit->p1.max; clock.p1++) {
  558. int this_err;
  559. i9xx_clock(refclk, &clock);
  560. if (!intel_PLL_is_valid(dev, limit,
  561. &clock))
  562. continue;
  563. if (match_clock &&
  564. clock.p != match_clock->p)
  565. continue;
  566. this_err = abs(clock.dot - target);
  567. if (this_err < err) {
  568. *best_clock = clock;
  569. err = this_err;
  570. }
  571. }
  572. }
  573. }
  574. }
  575. return (err != target);
  576. }
  577. static bool
  578. pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  579. int target, int refclk, intel_clock_t *match_clock,
  580. intel_clock_t *best_clock)
  581. {
  582. struct drm_device *dev = crtc->dev;
  583. intel_clock_t clock;
  584. int err = target;
  585. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  586. /*
  587. * For LVDS just rely on its current settings for dual-channel.
  588. * We haven't figured out how to reliably set up different
  589. * single/dual channel state, if we even can.
  590. */
  591. if (intel_is_dual_link_lvds(dev))
  592. clock.p2 = limit->p2.p2_fast;
  593. else
  594. clock.p2 = limit->p2.p2_slow;
  595. } else {
  596. if (target < limit->p2.dot_limit)
  597. clock.p2 = limit->p2.p2_slow;
  598. else
  599. clock.p2 = limit->p2.p2_fast;
  600. }
  601. memset(best_clock, 0, sizeof(*best_clock));
  602. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  603. clock.m1++) {
  604. for (clock.m2 = limit->m2.min;
  605. clock.m2 <= limit->m2.max; clock.m2++) {
  606. for (clock.n = limit->n.min;
  607. clock.n <= limit->n.max; clock.n++) {
  608. for (clock.p1 = limit->p1.min;
  609. clock.p1 <= limit->p1.max; clock.p1++) {
  610. int this_err;
  611. pineview_clock(refclk, &clock);
  612. if (!intel_PLL_is_valid(dev, limit,
  613. &clock))
  614. continue;
  615. if (match_clock &&
  616. clock.p != match_clock->p)
  617. continue;
  618. this_err = abs(clock.dot - target);
  619. if (this_err < err) {
  620. *best_clock = clock;
  621. err = this_err;
  622. }
  623. }
  624. }
  625. }
  626. }
  627. return (err != target);
  628. }
  629. static bool
  630. g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  631. int target, int refclk, intel_clock_t *match_clock,
  632. intel_clock_t *best_clock)
  633. {
  634. struct drm_device *dev = crtc->dev;
  635. intel_clock_t clock;
  636. int max_n;
  637. bool found;
  638. /* approximately equals target * 0.00585 */
  639. int err_most = (target >> 8) + (target >> 9);
  640. found = false;
  641. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  642. if (intel_is_dual_link_lvds(dev))
  643. clock.p2 = limit->p2.p2_fast;
  644. else
  645. clock.p2 = limit->p2.p2_slow;
  646. } else {
  647. if (target < limit->p2.dot_limit)
  648. clock.p2 = limit->p2.p2_slow;
  649. else
  650. clock.p2 = limit->p2.p2_fast;
  651. }
  652. memset(best_clock, 0, sizeof(*best_clock));
  653. max_n = limit->n.max;
  654. /* based on hardware requirement, prefer smaller n to precision */
  655. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  656. /* based on hardware requirement, prefere larger m1,m2 */
  657. for (clock.m1 = limit->m1.max;
  658. clock.m1 >= limit->m1.min; clock.m1--) {
  659. for (clock.m2 = limit->m2.max;
  660. clock.m2 >= limit->m2.min; clock.m2--) {
  661. for (clock.p1 = limit->p1.max;
  662. clock.p1 >= limit->p1.min; clock.p1--) {
  663. int this_err;
  664. i9xx_clock(refclk, &clock);
  665. if (!intel_PLL_is_valid(dev, limit,
  666. &clock))
  667. continue;
  668. this_err = abs(clock.dot - target);
  669. if (this_err < err_most) {
  670. *best_clock = clock;
  671. err_most = this_err;
  672. max_n = clock.n;
  673. found = true;
  674. }
  675. }
  676. }
  677. }
  678. }
  679. return found;
  680. }
  681. static bool
  682. vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  683. int target, int refclk, intel_clock_t *match_clock,
  684. intel_clock_t *best_clock)
  685. {
  686. struct drm_device *dev = crtc->dev;
  687. intel_clock_t clock;
  688. unsigned int bestppm = 1000000;
  689. /* min update 19.2 MHz */
  690. int max_n = min(limit->n.max, refclk / 19200);
  691. bool found = false;
  692. target *= 5; /* fast clock */
  693. memset(best_clock, 0, sizeof(*best_clock));
  694. /* based on hardware requirement, prefer smaller n to precision */
  695. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  696. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  697. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  698. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  699. clock.p = clock.p1 * clock.p2;
  700. /* based on hardware requirement, prefer bigger m1,m2 values */
  701. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  702. unsigned int ppm, diff;
  703. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  704. refclk * clock.m1);
  705. vlv_clock(refclk, &clock);
  706. if (!intel_PLL_is_valid(dev, limit,
  707. &clock))
  708. continue;
  709. diff = abs(clock.dot - target);
  710. ppm = div_u64(1000000ULL * diff, target);
  711. if (ppm < 100 && clock.p > best_clock->p) {
  712. bestppm = 0;
  713. *best_clock = clock;
  714. found = true;
  715. }
  716. if (bestppm >= 10 && ppm < bestppm - 10) {
  717. bestppm = ppm;
  718. *best_clock = clock;
  719. found = true;
  720. }
  721. }
  722. }
  723. }
  724. }
  725. return found;
  726. }
  727. static bool
  728. chv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  729. int target, int refclk, intel_clock_t *match_clock,
  730. intel_clock_t *best_clock)
  731. {
  732. struct drm_device *dev = crtc->dev;
  733. intel_clock_t clock;
  734. uint64_t m2;
  735. int found = false;
  736. memset(best_clock, 0, sizeof(*best_clock));
  737. /*
  738. * Based on hardware doc, the n always set to 1, and m1 always
  739. * set to 2. If requires to support 200Mhz refclk, we need to
  740. * revisit this because n may not 1 anymore.
  741. */
  742. clock.n = 1, clock.m1 = 2;
  743. target *= 5; /* fast clock */
  744. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  745. for (clock.p2 = limit->p2.p2_fast;
  746. clock.p2 >= limit->p2.p2_slow;
  747. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  748. clock.p = clock.p1 * clock.p2;
  749. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  750. clock.n) << 22, refclk * clock.m1);
  751. if (m2 > INT_MAX/clock.m1)
  752. continue;
  753. clock.m2 = m2;
  754. chv_clock(refclk, &clock);
  755. if (!intel_PLL_is_valid(dev, limit, &clock))
  756. continue;
  757. /* based on hardware requirement, prefer bigger p
  758. */
  759. if (clock.p > best_clock->p) {
  760. *best_clock = clock;
  761. found = true;
  762. }
  763. }
  764. }
  765. return found;
  766. }
  767. bool intel_crtc_active(struct drm_crtc *crtc)
  768. {
  769. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  770. /* Be paranoid as we can arrive here with only partial
  771. * state retrieved from the hardware during setup.
  772. *
  773. * We can ditch the adjusted_mode.crtc_clock check as soon
  774. * as Haswell has gained clock readout/fastboot support.
  775. *
  776. * We can ditch the crtc->primary->fb check as soon as we can
  777. * properly reconstruct framebuffers.
  778. */
  779. return intel_crtc->active && crtc->primary->fb &&
  780. intel_crtc->config.adjusted_mode.crtc_clock;
  781. }
  782. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  783. enum pipe pipe)
  784. {
  785. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  786. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  787. return intel_crtc->config.cpu_transcoder;
  788. }
  789. static void g4x_wait_for_vblank(struct drm_device *dev, int pipe)
  790. {
  791. struct drm_i915_private *dev_priv = dev->dev_private;
  792. u32 frame, frame_reg = PIPE_FRMCOUNT_GM45(pipe);
  793. frame = I915_READ(frame_reg);
  794. if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
  795. WARN(1, "vblank wait timed out\n");
  796. }
  797. /**
  798. * intel_wait_for_vblank - wait for vblank on a given pipe
  799. * @dev: drm device
  800. * @pipe: pipe to wait for
  801. *
  802. * Wait for vblank to occur on a given pipe. Needed for various bits of
  803. * mode setting code.
  804. */
  805. void intel_wait_for_vblank(struct drm_device *dev, int pipe)
  806. {
  807. struct drm_i915_private *dev_priv = dev->dev_private;
  808. int pipestat_reg = PIPESTAT(pipe);
  809. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
  810. g4x_wait_for_vblank(dev, pipe);
  811. return;
  812. }
  813. /* Clear existing vblank status. Note this will clear any other
  814. * sticky status fields as well.
  815. *
  816. * This races with i915_driver_irq_handler() with the result
  817. * that either function could miss a vblank event. Here it is not
  818. * fatal, as we will either wait upon the next vblank interrupt or
  819. * timeout. Generally speaking intel_wait_for_vblank() is only
  820. * called during modeset at which time the GPU should be idle and
  821. * should *not* be performing page flips and thus not waiting on
  822. * vblanks...
  823. * Currently, the result of us stealing a vblank from the irq
  824. * handler is that a single frame will be skipped during swapbuffers.
  825. */
  826. I915_WRITE(pipestat_reg,
  827. I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
  828. /* Wait for vblank interrupt bit to set */
  829. if (wait_for(I915_READ(pipestat_reg) &
  830. PIPE_VBLANK_INTERRUPT_STATUS,
  831. 50))
  832. DRM_DEBUG_KMS("vblank wait timed out\n");
  833. }
  834. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  835. {
  836. struct drm_i915_private *dev_priv = dev->dev_private;
  837. u32 reg = PIPEDSL(pipe);
  838. u32 line1, line2;
  839. u32 line_mask;
  840. if (IS_GEN2(dev))
  841. line_mask = DSL_LINEMASK_GEN2;
  842. else
  843. line_mask = DSL_LINEMASK_GEN3;
  844. line1 = I915_READ(reg) & line_mask;
  845. mdelay(5);
  846. line2 = I915_READ(reg) & line_mask;
  847. return line1 == line2;
  848. }
  849. /*
  850. * intel_wait_for_pipe_off - wait for pipe to turn off
  851. * @dev: drm device
  852. * @pipe: pipe to wait for
  853. *
  854. * After disabling a pipe, we can't wait for vblank in the usual way,
  855. * spinning on the vblank interrupt status bit, since we won't actually
  856. * see an interrupt when the pipe is disabled.
  857. *
  858. * On Gen4 and above:
  859. * wait for the pipe register state bit to turn off
  860. *
  861. * Otherwise:
  862. * wait for the display line value to settle (it usually
  863. * ends up stopping at the start of the next frame).
  864. *
  865. */
  866. void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
  867. {
  868. struct drm_i915_private *dev_priv = dev->dev_private;
  869. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  870. pipe);
  871. if (INTEL_INFO(dev)->gen >= 4) {
  872. int reg = PIPECONF(cpu_transcoder);
  873. /* Wait for the Pipe State to go off */
  874. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  875. 100))
  876. WARN(1, "pipe_off wait timed out\n");
  877. } else {
  878. /* Wait for the display line to settle */
  879. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  880. WARN(1, "pipe_off wait timed out\n");
  881. }
  882. }
  883. /*
  884. * ibx_digital_port_connected - is the specified port connected?
  885. * @dev_priv: i915 private structure
  886. * @port: the port to test
  887. *
  888. * Returns true if @port is connected, false otherwise.
  889. */
  890. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  891. struct intel_digital_port *port)
  892. {
  893. u32 bit;
  894. if (HAS_PCH_IBX(dev_priv->dev)) {
  895. switch (port->port) {
  896. case PORT_B:
  897. bit = SDE_PORTB_HOTPLUG;
  898. break;
  899. case PORT_C:
  900. bit = SDE_PORTC_HOTPLUG;
  901. break;
  902. case PORT_D:
  903. bit = SDE_PORTD_HOTPLUG;
  904. break;
  905. default:
  906. return true;
  907. }
  908. } else {
  909. switch (port->port) {
  910. case PORT_B:
  911. bit = SDE_PORTB_HOTPLUG_CPT;
  912. break;
  913. case PORT_C:
  914. bit = SDE_PORTC_HOTPLUG_CPT;
  915. break;
  916. case PORT_D:
  917. bit = SDE_PORTD_HOTPLUG_CPT;
  918. break;
  919. default:
  920. return true;
  921. }
  922. }
  923. return I915_READ(SDEISR) & bit;
  924. }
  925. static const char *state_string(bool enabled)
  926. {
  927. return enabled ? "on" : "off";
  928. }
  929. /* Only for pre-ILK configs */
  930. void assert_pll(struct drm_i915_private *dev_priv,
  931. enum pipe pipe, bool state)
  932. {
  933. int reg;
  934. u32 val;
  935. bool cur_state;
  936. reg = DPLL(pipe);
  937. val = I915_READ(reg);
  938. cur_state = !!(val & DPLL_VCO_ENABLE);
  939. WARN(cur_state != state,
  940. "PLL state assertion failure (expected %s, current %s)\n",
  941. state_string(state), state_string(cur_state));
  942. }
  943. /* XXX: the dsi pll is shared between MIPI DSI ports */
  944. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  945. {
  946. u32 val;
  947. bool cur_state;
  948. mutex_lock(&dev_priv->dpio_lock);
  949. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  950. mutex_unlock(&dev_priv->dpio_lock);
  951. cur_state = val & DSI_PLL_VCO_EN;
  952. WARN(cur_state != state,
  953. "DSI PLL state assertion failure (expected %s, current %s)\n",
  954. state_string(state), state_string(cur_state));
  955. }
  956. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  957. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  958. struct intel_shared_dpll *
  959. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  960. {
  961. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  962. if (crtc->config.shared_dpll < 0)
  963. return NULL;
  964. return &dev_priv->shared_dplls[crtc->config.shared_dpll];
  965. }
  966. /* For ILK+ */
  967. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  968. struct intel_shared_dpll *pll,
  969. bool state)
  970. {
  971. bool cur_state;
  972. struct intel_dpll_hw_state hw_state;
  973. if (WARN (!pll,
  974. "asserting DPLL %s with no DPLL\n", state_string(state)))
  975. return;
  976. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  977. WARN(cur_state != state,
  978. "%s assertion failure (expected %s, current %s)\n",
  979. pll->name, state_string(state), state_string(cur_state));
  980. }
  981. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  982. enum pipe pipe, bool state)
  983. {
  984. int reg;
  985. u32 val;
  986. bool cur_state;
  987. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  988. pipe);
  989. if (HAS_DDI(dev_priv->dev)) {
  990. /* DDI does not have a specific FDI_TX register */
  991. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  992. val = I915_READ(reg);
  993. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  994. } else {
  995. reg = FDI_TX_CTL(pipe);
  996. val = I915_READ(reg);
  997. cur_state = !!(val & FDI_TX_ENABLE);
  998. }
  999. WARN(cur_state != state,
  1000. "FDI TX state assertion failure (expected %s, current %s)\n",
  1001. state_string(state), state_string(cur_state));
  1002. }
  1003. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1004. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1005. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1006. enum pipe pipe, bool state)
  1007. {
  1008. int reg;
  1009. u32 val;
  1010. bool cur_state;
  1011. reg = FDI_RX_CTL(pipe);
  1012. val = I915_READ(reg);
  1013. cur_state = !!(val & FDI_RX_ENABLE);
  1014. WARN(cur_state != state,
  1015. "FDI RX state assertion failure (expected %s, current %s)\n",
  1016. state_string(state), state_string(cur_state));
  1017. }
  1018. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1019. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1020. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1021. enum pipe pipe)
  1022. {
  1023. int reg;
  1024. u32 val;
  1025. /* ILK FDI PLL is always enabled */
  1026. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  1027. return;
  1028. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1029. if (HAS_DDI(dev_priv->dev))
  1030. return;
  1031. reg = FDI_TX_CTL(pipe);
  1032. val = I915_READ(reg);
  1033. WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1034. }
  1035. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1036. enum pipe pipe, bool state)
  1037. {
  1038. int reg;
  1039. u32 val;
  1040. bool cur_state;
  1041. reg = FDI_RX_CTL(pipe);
  1042. val = I915_READ(reg);
  1043. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1044. WARN(cur_state != state,
  1045. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1046. state_string(state), state_string(cur_state));
  1047. }
  1048. static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1049. enum pipe pipe)
  1050. {
  1051. int pp_reg, lvds_reg;
  1052. u32 val;
  1053. enum pipe panel_pipe = PIPE_A;
  1054. bool locked = true;
  1055. if (HAS_PCH_SPLIT(dev_priv->dev)) {
  1056. pp_reg = PCH_PP_CONTROL;
  1057. lvds_reg = PCH_LVDS;
  1058. } else {
  1059. pp_reg = PP_CONTROL;
  1060. lvds_reg = LVDS;
  1061. }
  1062. val = I915_READ(pp_reg);
  1063. if (!(val & PANEL_POWER_ON) ||
  1064. ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
  1065. locked = false;
  1066. if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
  1067. panel_pipe = PIPE_B;
  1068. WARN(panel_pipe == pipe && locked,
  1069. "panel assertion failure, pipe %c regs locked\n",
  1070. pipe_name(pipe));
  1071. }
  1072. static void assert_cursor(struct drm_i915_private *dev_priv,
  1073. enum pipe pipe, bool state)
  1074. {
  1075. struct drm_device *dev = dev_priv->dev;
  1076. bool cur_state;
  1077. if (IS_845G(dev) || IS_I865G(dev))
  1078. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1079. else
  1080. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1081. WARN(cur_state != state,
  1082. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1083. pipe_name(pipe), state_string(state), state_string(cur_state));
  1084. }
  1085. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1086. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1087. void assert_pipe(struct drm_i915_private *dev_priv,
  1088. enum pipe pipe, bool state)
  1089. {
  1090. int reg;
  1091. u32 val;
  1092. bool cur_state;
  1093. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1094. pipe);
  1095. /* if we need the pipe A quirk it must be always on */
  1096. if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
  1097. state = true;
  1098. if (!intel_display_power_enabled(dev_priv,
  1099. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1100. cur_state = false;
  1101. } else {
  1102. reg = PIPECONF(cpu_transcoder);
  1103. val = I915_READ(reg);
  1104. cur_state = !!(val & PIPECONF_ENABLE);
  1105. }
  1106. WARN(cur_state != state,
  1107. "pipe %c assertion failure (expected %s, current %s)\n",
  1108. pipe_name(pipe), state_string(state), state_string(cur_state));
  1109. }
  1110. static void assert_plane(struct drm_i915_private *dev_priv,
  1111. enum plane plane, bool state)
  1112. {
  1113. int reg;
  1114. u32 val;
  1115. bool cur_state;
  1116. reg = DSPCNTR(plane);
  1117. val = I915_READ(reg);
  1118. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1119. WARN(cur_state != state,
  1120. "plane %c assertion failure (expected %s, current %s)\n",
  1121. plane_name(plane), state_string(state), state_string(cur_state));
  1122. }
  1123. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1124. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1125. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1126. enum pipe pipe)
  1127. {
  1128. struct drm_device *dev = dev_priv->dev;
  1129. int reg, i;
  1130. u32 val;
  1131. int cur_pipe;
  1132. /* Primary planes are fixed to pipes on gen4+ */
  1133. if (INTEL_INFO(dev)->gen >= 4) {
  1134. reg = DSPCNTR(pipe);
  1135. val = I915_READ(reg);
  1136. WARN(val & DISPLAY_PLANE_ENABLE,
  1137. "plane %c assertion failure, should be disabled but not\n",
  1138. plane_name(pipe));
  1139. return;
  1140. }
  1141. /* Need to check both planes against the pipe */
  1142. for_each_pipe(i) {
  1143. reg = DSPCNTR(i);
  1144. val = I915_READ(reg);
  1145. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1146. DISPPLANE_SEL_PIPE_SHIFT;
  1147. WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1148. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1149. plane_name(i), pipe_name(pipe));
  1150. }
  1151. }
  1152. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1153. enum pipe pipe)
  1154. {
  1155. struct drm_device *dev = dev_priv->dev;
  1156. int reg, sprite;
  1157. u32 val;
  1158. if (IS_VALLEYVIEW(dev)) {
  1159. for_each_sprite(pipe, sprite) {
  1160. reg = SPCNTR(pipe, sprite);
  1161. val = I915_READ(reg);
  1162. WARN(val & SP_ENABLE,
  1163. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1164. sprite_name(pipe, sprite), pipe_name(pipe));
  1165. }
  1166. } else if (INTEL_INFO(dev)->gen >= 7) {
  1167. reg = SPRCTL(pipe);
  1168. val = I915_READ(reg);
  1169. WARN(val & SPRITE_ENABLE,
  1170. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1171. plane_name(pipe), pipe_name(pipe));
  1172. } else if (INTEL_INFO(dev)->gen >= 5) {
  1173. reg = DVSCNTR(pipe);
  1174. val = I915_READ(reg);
  1175. WARN(val & DVS_ENABLE,
  1176. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1177. plane_name(pipe), pipe_name(pipe));
  1178. }
  1179. }
  1180. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1181. {
  1182. u32 val;
  1183. bool enabled;
  1184. WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1185. val = I915_READ(PCH_DREF_CONTROL);
  1186. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1187. DREF_SUPERSPREAD_SOURCE_MASK));
  1188. WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1189. }
  1190. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1191. enum pipe pipe)
  1192. {
  1193. int reg;
  1194. u32 val;
  1195. bool enabled;
  1196. reg = PCH_TRANSCONF(pipe);
  1197. val = I915_READ(reg);
  1198. enabled = !!(val & TRANS_ENABLE);
  1199. WARN(enabled,
  1200. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1201. pipe_name(pipe));
  1202. }
  1203. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1204. enum pipe pipe, u32 port_sel, u32 val)
  1205. {
  1206. if ((val & DP_PORT_EN) == 0)
  1207. return false;
  1208. if (HAS_PCH_CPT(dev_priv->dev)) {
  1209. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1210. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1211. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1212. return false;
  1213. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1214. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1215. return false;
  1216. } else {
  1217. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1218. return false;
  1219. }
  1220. return true;
  1221. }
  1222. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1223. enum pipe pipe, u32 val)
  1224. {
  1225. if ((val & SDVO_ENABLE) == 0)
  1226. return false;
  1227. if (HAS_PCH_CPT(dev_priv->dev)) {
  1228. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1229. return false;
  1230. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1231. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1232. return false;
  1233. } else {
  1234. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1235. return false;
  1236. }
  1237. return true;
  1238. }
  1239. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1240. enum pipe pipe, u32 val)
  1241. {
  1242. if ((val & LVDS_PORT_EN) == 0)
  1243. return false;
  1244. if (HAS_PCH_CPT(dev_priv->dev)) {
  1245. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1246. return false;
  1247. } else {
  1248. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1249. return false;
  1250. }
  1251. return true;
  1252. }
  1253. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1254. enum pipe pipe, u32 val)
  1255. {
  1256. if ((val & ADPA_DAC_ENABLE) == 0)
  1257. return false;
  1258. if (HAS_PCH_CPT(dev_priv->dev)) {
  1259. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1260. return false;
  1261. } else {
  1262. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1263. return false;
  1264. }
  1265. return true;
  1266. }
  1267. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1268. enum pipe pipe, int reg, u32 port_sel)
  1269. {
  1270. u32 val = I915_READ(reg);
  1271. WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1272. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1273. reg, pipe_name(pipe));
  1274. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1275. && (val & DP_PIPEB_SELECT),
  1276. "IBX PCH dp port still using transcoder B\n");
  1277. }
  1278. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1279. enum pipe pipe, int reg)
  1280. {
  1281. u32 val = I915_READ(reg);
  1282. WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1283. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1284. reg, pipe_name(pipe));
  1285. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1286. && (val & SDVO_PIPE_B_SELECT),
  1287. "IBX PCH hdmi port still using transcoder B\n");
  1288. }
  1289. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1290. enum pipe pipe)
  1291. {
  1292. int reg;
  1293. u32 val;
  1294. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1295. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1296. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1297. reg = PCH_ADPA;
  1298. val = I915_READ(reg);
  1299. WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1300. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1301. pipe_name(pipe));
  1302. reg = PCH_LVDS;
  1303. val = I915_READ(reg);
  1304. WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1305. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1306. pipe_name(pipe));
  1307. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1308. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1309. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1310. }
  1311. static void intel_init_dpio(struct drm_device *dev)
  1312. {
  1313. struct drm_i915_private *dev_priv = dev->dev_private;
  1314. if (!IS_VALLEYVIEW(dev))
  1315. return;
  1316. /*
  1317. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1318. * CHV x1 PHY (DP/HDMI D)
  1319. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1320. */
  1321. if (IS_CHERRYVIEW(dev)) {
  1322. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1323. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1324. } else {
  1325. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1326. }
  1327. }
  1328. static void vlv_enable_pll(struct intel_crtc *crtc)
  1329. {
  1330. struct drm_device *dev = crtc->base.dev;
  1331. struct drm_i915_private *dev_priv = dev->dev_private;
  1332. int reg = DPLL(crtc->pipe);
  1333. u32 dpll = crtc->config.dpll_hw_state.dpll;
  1334. assert_pipe_disabled(dev_priv, crtc->pipe);
  1335. /* No really, not for ILK+ */
  1336. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1337. /* PLL is protected by panel, make sure we can write it */
  1338. if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
  1339. assert_panel_unlocked(dev_priv, crtc->pipe);
  1340. I915_WRITE(reg, dpll);
  1341. POSTING_READ(reg);
  1342. udelay(150);
  1343. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1344. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1345. I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
  1346. POSTING_READ(DPLL_MD(crtc->pipe));
  1347. /* We do this three times for luck */
  1348. I915_WRITE(reg, dpll);
  1349. POSTING_READ(reg);
  1350. udelay(150); /* wait for warmup */
  1351. I915_WRITE(reg, dpll);
  1352. POSTING_READ(reg);
  1353. udelay(150); /* wait for warmup */
  1354. I915_WRITE(reg, dpll);
  1355. POSTING_READ(reg);
  1356. udelay(150); /* wait for warmup */
  1357. }
  1358. static void chv_enable_pll(struct intel_crtc *crtc)
  1359. {
  1360. struct drm_device *dev = crtc->base.dev;
  1361. struct drm_i915_private *dev_priv = dev->dev_private;
  1362. int pipe = crtc->pipe;
  1363. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1364. u32 tmp;
  1365. assert_pipe_disabled(dev_priv, crtc->pipe);
  1366. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1367. mutex_lock(&dev_priv->dpio_lock);
  1368. /* Enable back the 10bit clock to display controller */
  1369. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1370. tmp |= DPIO_DCLKP_EN;
  1371. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1372. /*
  1373. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1374. */
  1375. udelay(1);
  1376. /* Enable PLL */
  1377. I915_WRITE(DPLL(pipe), crtc->config.dpll_hw_state.dpll);
  1378. /* Check PLL is locked */
  1379. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1380. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1381. /* not sure when this should be written */
  1382. I915_WRITE(DPLL_MD(pipe), crtc->config.dpll_hw_state.dpll_md);
  1383. POSTING_READ(DPLL_MD(pipe));
  1384. mutex_unlock(&dev_priv->dpio_lock);
  1385. }
  1386. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1387. {
  1388. struct drm_device *dev = crtc->base.dev;
  1389. struct drm_i915_private *dev_priv = dev->dev_private;
  1390. int reg = DPLL(crtc->pipe);
  1391. u32 dpll = crtc->config.dpll_hw_state.dpll;
  1392. assert_pipe_disabled(dev_priv, crtc->pipe);
  1393. /* No really, not for ILK+ */
  1394. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1395. /* PLL is protected by panel, make sure we can write it */
  1396. if (IS_MOBILE(dev) && !IS_I830(dev))
  1397. assert_panel_unlocked(dev_priv, crtc->pipe);
  1398. I915_WRITE(reg, dpll);
  1399. /* Wait for the clocks to stabilize. */
  1400. POSTING_READ(reg);
  1401. udelay(150);
  1402. if (INTEL_INFO(dev)->gen >= 4) {
  1403. I915_WRITE(DPLL_MD(crtc->pipe),
  1404. crtc->config.dpll_hw_state.dpll_md);
  1405. } else {
  1406. /* The pixel multiplier can only be updated once the
  1407. * DPLL is enabled and the clocks are stable.
  1408. *
  1409. * So write it again.
  1410. */
  1411. I915_WRITE(reg, dpll);
  1412. }
  1413. /* We do this three times for luck */
  1414. I915_WRITE(reg, dpll);
  1415. POSTING_READ(reg);
  1416. udelay(150); /* wait for warmup */
  1417. I915_WRITE(reg, dpll);
  1418. POSTING_READ(reg);
  1419. udelay(150); /* wait for warmup */
  1420. I915_WRITE(reg, dpll);
  1421. POSTING_READ(reg);
  1422. udelay(150); /* wait for warmup */
  1423. }
  1424. /**
  1425. * i9xx_disable_pll - disable a PLL
  1426. * @dev_priv: i915 private structure
  1427. * @pipe: pipe PLL to disable
  1428. *
  1429. * Disable the PLL for @pipe, making sure the pipe is off first.
  1430. *
  1431. * Note! This is for pre-ILK only.
  1432. */
  1433. static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1434. {
  1435. /* Don't disable pipe A or pipe A PLLs if needed */
  1436. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1437. return;
  1438. /* Make sure the pipe isn't still relying on us */
  1439. assert_pipe_disabled(dev_priv, pipe);
  1440. I915_WRITE(DPLL(pipe), 0);
  1441. POSTING_READ(DPLL(pipe));
  1442. }
  1443. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1444. {
  1445. u32 val = 0;
  1446. /* Make sure the pipe isn't still relying on us */
  1447. assert_pipe_disabled(dev_priv, pipe);
  1448. /*
  1449. * Leave integrated clock source and reference clock enabled for pipe B.
  1450. * The latter is needed for VGA hotplug / manual detection.
  1451. */
  1452. if (pipe == PIPE_B)
  1453. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1454. I915_WRITE(DPLL(pipe), val);
  1455. POSTING_READ(DPLL(pipe));
  1456. }
  1457. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1458. {
  1459. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1460. u32 val;
  1461. /* Make sure the pipe isn't still relying on us */
  1462. assert_pipe_disabled(dev_priv, pipe);
  1463. /* Set PLL en = 0 */
  1464. val = DPLL_SSC_REF_CLOCK_CHV;
  1465. if (pipe != PIPE_A)
  1466. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1467. I915_WRITE(DPLL(pipe), val);
  1468. POSTING_READ(DPLL(pipe));
  1469. mutex_lock(&dev_priv->dpio_lock);
  1470. /* Disable 10bit clock to display controller */
  1471. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1472. val &= ~DPIO_DCLKP_EN;
  1473. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1474. /* disable left/right clock distribution */
  1475. if (pipe != PIPE_B) {
  1476. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1477. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1478. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1479. } else {
  1480. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1481. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1482. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1483. }
  1484. mutex_unlock(&dev_priv->dpio_lock);
  1485. }
  1486. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1487. struct intel_digital_port *dport)
  1488. {
  1489. u32 port_mask;
  1490. int dpll_reg;
  1491. switch (dport->port) {
  1492. case PORT_B:
  1493. port_mask = DPLL_PORTB_READY_MASK;
  1494. dpll_reg = DPLL(0);
  1495. break;
  1496. case PORT_C:
  1497. port_mask = DPLL_PORTC_READY_MASK;
  1498. dpll_reg = DPLL(0);
  1499. break;
  1500. case PORT_D:
  1501. port_mask = DPLL_PORTD_READY_MASK;
  1502. dpll_reg = DPIO_PHY_STATUS;
  1503. break;
  1504. default:
  1505. BUG();
  1506. }
  1507. if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
  1508. WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
  1509. port_name(dport->port), I915_READ(dpll_reg));
  1510. }
  1511. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1512. {
  1513. struct drm_device *dev = crtc->base.dev;
  1514. struct drm_i915_private *dev_priv = dev->dev_private;
  1515. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1516. if (WARN_ON(pll == NULL))
  1517. return;
  1518. WARN_ON(!pll->refcount);
  1519. if (pll->active == 0) {
  1520. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1521. WARN_ON(pll->on);
  1522. assert_shared_dpll_disabled(dev_priv, pll);
  1523. pll->mode_set(dev_priv, pll);
  1524. }
  1525. }
  1526. /**
  1527. * intel_enable_shared_dpll - enable PCH PLL
  1528. * @dev_priv: i915 private structure
  1529. * @pipe: pipe PLL to enable
  1530. *
  1531. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1532. * drives the transcoder clock.
  1533. */
  1534. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1535. {
  1536. struct drm_device *dev = crtc->base.dev;
  1537. struct drm_i915_private *dev_priv = dev->dev_private;
  1538. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1539. if (WARN_ON(pll == NULL))
  1540. return;
  1541. if (WARN_ON(pll->refcount == 0))
  1542. return;
  1543. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1544. pll->name, pll->active, pll->on,
  1545. crtc->base.base.id);
  1546. if (pll->active++) {
  1547. WARN_ON(!pll->on);
  1548. assert_shared_dpll_enabled(dev_priv, pll);
  1549. return;
  1550. }
  1551. WARN_ON(pll->on);
  1552. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1553. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1554. pll->enable(dev_priv, pll);
  1555. pll->on = true;
  1556. }
  1557. void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1558. {
  1559. struct drm_device *dev = crtc->base.dev;
  1560. struct drm_i915_private *dev_priv = dev->dev_private;
  1561. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1562. /* PCH only available on ILK+ */
  1563. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1564. if (WARN_ON(pll == NULL))
  1565. return;
  1566. if (WARN_ON(pll->refcount == 0))
  1567. return;
  1568. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1569. pll->name, pll->active, pll->on,
  1570. crtc->base.base.id);
  1571. if (WARN_ON(pll->active == 0)) {
  1572. assert_shared_dpll_disabled(dev_priv, pll);
  1573. return;
  1574. }
  1575. assert_shared_dpll_enabled(dev_priv, pll);
  1576. WARN_ON(!pll->on);
  1577. if (--pll->active)
  1578. return;
  1579. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1580. pll->disable(dev_priv, pll);
  1581. pll->on = false;
  1582. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1583. }
  1584. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1585. enum pipe pipe)
  1586. {
  1587. struct drm_device *dev = dev_priv->dev;
  1588. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1589. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1590. uint32_t reg, val, pipeconf_val;
  1591. /* PCH only available on ILK+ */
  1592. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1593. /* Make sure PCH DPLL is enabled */
  1594. assert_shared_dpll_enabled(dev_priv,
  1595. intel_crtc_to_shared_dpll(intel_crtc));
  1596. /* FDI must be feeding us bits for PCH ports */
  1597. assert_fdi_tx_enabled(dev_priv, pipe);
  1598. assert_fdi_rx_enabled(dev_priv, pipe);
  1599. if (HAS_PCH_CPT(dev)) {
  1600. /* Workaround: Set the timing override bit before enabling the
  1601. * pch transcoder. */
  1602. reg = TRANS_CHICKEN2(pipe);
  1603. val = I915_READ(reg);
  1604. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1605. I915_WRITE(reg, val);
  1606. }
  1607. reg = PCH_TRANSCONF(pipe);
  1608. val = I915_READ(reg);
  1609. pipeconf_val = I915_READ(PIPECONF(pipe));
  1610. if (HAS_PCH_IBX(dev_priv->dev)) {
  1611. /*
  1612. * make the BPC in transcoder be consistent with
  1613. * that in pipeconf reg.
  1614. */
  1615. val &= ~PIPECONF_BPC_MASK;
  1616. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1617. }
  1618. val &= ~TRANS_INTERLACE_MASK;
  1619. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1620. if (HAS_PCH_IBX(dev_priv->dev) &&
  1621. intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
  1622. val |= TRANS_LEGACY_INTERLACED_ILK;
  1623. else
  1624. val |= TRANS_INTERLACED;
  1625. else
  1626. val |= TRANS_PROGRESSIVE;
  1627. I915_WRITE(reg, val | TRANS_ENABLE);
  1628. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1629. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1630. }
  1631. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1632. enum transcoder cpu_transcoder)
  1633. {
  1634. u32 val, pipeconf_val;
  1635. /* PCH only available on ILK+ */
  1636. BUG_ON(INTEL_INFO(dev_priv->dev)->gen < 5);
  1637. /* FDI must be feeding us bits for PCH ports */
  1638. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1639. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1640. /* Workaround: set timing override bit. */
  1641. val = I915_READ(_TRANSA_CHICKEN2);
  1642. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1643. I915_WRITE(_TRANSA_CHICKEN2, val);
  1644. val = TRANS_ENABLE;
  1645. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1646. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1647. PIPECONF_INTERLACED_ILK)
  1648. val |= TRANS_INTERLACED;
  1649. else
  1650. val |= TRANS_PROGRESSIVE;
  1651. I915_WRITE(LPT_TRANSCONF, val);
  1652. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1653. DRM_ERROR("Failed to enable PCH transcoder\n");
  1654. }
  1655. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1656. enum pipe pipe)
  1657. {
  1658. struct drm_device *dev = dev_priv->dev;
  1659. uint32_t reg, val;
  1660. /* FDI relies on the transcoder */
  1661. assert_fdi_tx_disabled(dev_priv, pipe);
  1662. assert_fdi_rx_disabled(dev_priv, pipe);
  1663. /* Ports must be off as well */
  1664. assert_pch_ports_disabled(dev_priv, pipe);
  1665. reg = PCH_TRANSCONF(pipe);
  1666. val = I915_READ(reg);
  1667. val &= ~TRANS_ENABLE;
  1668. I915_WRITE(reg, val);
  1669. /* wait for PCH transcoder off, transcoder state */
  1670. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1671. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1672. if (!HAS_PCH_IBX(dev)) {
  1673. /* Workaround: Clear the timing override chicken bit again. */
  1674. reg = TRANS_CHICKEN2(pipe);
  1675. val = I915_READ(reg);
  1676. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1677. I915_WRITE(reg, val);
  1678. }
  1679. }
  1680. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1681. {
  1682. u32 val;
  1683. val = I915_READ(LPT_TRANSCONF);
  1684. val &= ~TRANS_ENABLE;
  1685. I915_WRITE(LPT_TRANSCONF, val);
  1686. /* wait for PCH transcoder off, transcoder state */
  1687. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1688. DRM_ERROR("Failed to disable PCH transcoder\n");
  1689. /* Workaround: clear timing override bit. */
  1690. val = I915_READ(_TRANSA_CHICKEN2);
  1691. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1692. I915_WRITE(_TRANSA_CHICKEN2, val);
  1693. }
  1694. /**
  1695. * intel_enable_pipe - enable a pipe, asserting requirements
  1696. * @crtc: crtc responsible for the pipe
  1697. *
  1698. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1699. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1700. */
  1701. static void intel_enable_pipe(struct intel_crtc *crtc)
  1702. {
  1703. struct drm_device *dev = crtc->base.dev;
  1704. struct drm_i915_private *dev_priv = dev->dev_private;
  1705. enum pipe pipe = crtc->pipe;
  1706. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1707. pipe);
  1708. enum pipe pch_transcoder;
  1709. int reg;
  1710. u32 val;
  1711. assert_planes_disabled(dev_priv, pipe);
  1712. assert_cursor_disabled(dev_priv, pipe);
  1713. assert_sprites_disabled(dev_priv, pipe);
  1714. if (HAS_PCH_LPT(dev_priv->dev))
  1715. pch_transcoder = TRANSCODER_A;
  1716. else
  1717. pch_transcoder = pipe;
  1718. /*
  1719. * A pipe without a PLL won't actually be able to drive bits from
  1720. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1721. * need the check.
  1722. */
  1723. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1724. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DSI))
  1725. assert_dsi_pll_enabled(dev_priv);
  1726. else
  1727. assert_pll_enabled(dev_priv, pipe);
  1728. else {
  1729. if (crtc->config.has_pch_encoder) {
  1730. /* if driving the PCH, we need FDI enabled */
  1731. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1732. assert_fdi_tx_pll_enabled(dev_priv,
  1733. (enum pipe) cpu_transcoder);
  1734. }
  1735. /* FIXME: assert CPU port conditions for SNB+ */
  1736. }
  1737. reg = PIPECONF(cpu_transcoder);
  1738. val = I915_READ(reg);
  1739. if (val & PIPECONF_ENABLE) {
  1740. WARN_ON(!(pipe == PIPE_A &&
  1741. dev_priv->quirks & QUIRK_PIPEA_FORCE));
  1742. return;
  1743. }
  1744. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1745. POSTING_READ(reg);
  1746. }
  1747. /**
  1748. * intel_disable_pipe - disable a pipe, asserting requirements
  1749. * @dev_priv: i915 private structure
  1750. * @pipe: pipe to disable
  1751. *
  1752. * Disable @pipe, making sure that various hardware specific requirements
  1753. * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
  1754. *
  1755. * @pipe should be %PIPE_A or %PIPE_B.
  1756. *
  1757. * Will wait until the pipe has shut down before returning.
  1758. */
  1759. static void intel_disable_pipe(struct drm_i915_private *dev_priv,
  1760. enum pipe pipe)
  1761. {
  1762. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1763. pipe);
  1764. int reg;
  1765. u32 val;
  1766. /*
  1767. * Make sure planes won't keep trying to pump pixels to us,
  1768. * or we might hang the display.
  1769. */
  1770. assert_planes_disabled(dev_priv, pipe);
  1771. assert_cursor_disabled(dev_priv, pipe);
  1772. assert_sprites_disabled(dev_priv, pipe);
  1773. /* Don't disable pipe A or pipe A PLLs if needed */
  1774. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1775. return;
  1776. reg = PIPECONF(cpu_transcoder);
  1777. val = I915_READ(reg);
  1778. if ((val & PIPECONF_ENABLE) == 0)
  1779. return;
  1780. I915_WRITE(reg, val & ~PIPECONF_ENABLE);
  1781. intel_wait_for_pipe_off(dev_priv->dev, pipe);
  1782. }
  1783. /*
  1784. * Plane regs are double buffered, going from enabled->disabled needs a
  1785. * trigger in order to latch. The display address reg provides this.
  1786. */
  1787. void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
  1788. enum plane plane)
  1789. {
  1790. struct drm_device *dev = dev_priv->dev;
  1791. u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
  1792. I915_WRITE(reg, I915_READ(reg));
  1793. POSTING_READ(reg);
  1794. }
  1795. /**
  1796. * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  1797. * @dev_priv: i915 private structure
  1798. * @plane: plane to enable
  1799. * @pipe: pipe being fed
  1800. *
  1801. * Enable @plane on @pipe, making sure that @pipe is running first.
  1802. */
  1803. static void intel_enable_primary_hw_plane(struct drm_i915_private *dev_priv,
  1804. enum plane plane, enum pipe pipe)
  1805. {
  1806. struct drm_device *dev = dev_priv->dev;
  1807. struct intel_crtc *intel_crtc =
  1808. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  1809. int reg;
  1810. u32 val;
  1811. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1812. assert_pipe_enabled(dev_priv, pipe);
  1813. if (intel_crtc->primary_enabled)
  1814. return;
  1815. intel_crtc->primary_enabled = true;
  1816. reg = DSPCNTR(plane);
  1817. val = I915_READ(reg);
  1818. WARN_ON(val & DISPLAY_PLANE_ENABLE);
  1819. I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
  1820. intel_flush_primary_plane(dev_priv, plane);
  1821. /*
  1822. * BDW signals flip done immediately if the plane
  1823. * is disabled, even if the plane enable is already
  1824. * armed to occur at the next vblank :(
  1825. */
  1826. if (IS_BROADWELL(dev))
  1827. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1828. }
  1829. /**
  1830. * intel_disable_primary_hw_plane - disable the primary hardware plane
  1831. * @dev_priv: i915 private structure
  1832. * @plane: plane to disable
  1833. * @pipe: pipe consuming the data
  1834. *
  1835. * Disable @plane; should be an independent operation.
  1836. */
  1837. static void intel_disable_primary_hw_plane(struct drm_i915_private *dev_priv,
  1838. enum plane plane, enum pipe pipe)
  1839. {
  1840. struct intel_crtc *intel_crtc =
  1841. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  1842. int reg;
  1843. u32 val;
  1844. if (!intel_crtc->primary_enabled)
  1845. return;
  1846. intel_crtc->primary_enabled = false;
  1847. reg = DSPCNTR(plane);
  1848. val = I915_READ(reg);
  1849. WARN_ON((val & DISPLAY_PLANE_ENABLE) == 0);
  1850. I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
  1851. intel_flush_primary_plane(dev_priv, plane);
  1852. }
  1853. static bool need_vtd_wa(struct drm_device *dev)
  1854. {
  1855. #ifdef CONFIG_INTEL_IOMMU
  1856. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1857. return true;
  1858. #endif
  1859. return false;
  1860. }
  1861. static int intel_align_height(struct drm_device *dev, int height, bool tiled)
  1862. {
  1863. int tile_height;
  1864. tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
  1865. return ALIGN(height, tile_height);
  1866. }
  1867. int
  1868. intel_pin_and_fence_fb_obj(struct drm_device *dev,
  1869. struct drm_i915_gem_object *obj,
  1870. struct intel_engine_cs *pipelined)
  1871. {
  1872. struct drm_i915_private *dev_priv = dev->dev_private;
  1873. u32 alignment;
  1874. int ret;
  1875. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1876. switch (obj->tiling_mode) {
  1877. case I915_TILING_NONE:
  1878. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1879. alignment = 128 * 1024;
  1880. else if (INTEL_INFO(dev)->gen >= 4)
  1881. alignment = 4 * 1024;
  1882. else
  1883. alignment = 64 * 1024;
  1884. break;
  1885. case I915_TILING_X:
  1886. /* pin() will align the object as required by fence */
  1887. alignment = 0;
  1888. break;
  1889. case I915_TILING_Y:
  1890. WARN(1, "Y tiled bo slipped through, driver bug!\n");
  1891. return -EINVAL;
  1892. default:
  1893. BUG();
  1894. }
  1895. /* Note that the w/a also requires 64 PTE of padding following the
  1896. * bo. We currently fill all unused PTE with the shadow page and so
  1897. * we should always have valid PTE following the scanout preventing
  1898. * the VT-d warning.
  1899. */
  1900. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1901. alignment = 256 * 1024;
  1902. dev_priv->mm.interruptible = false;
  1903. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1904. if (ret)
  1905. goto err_interruptible;
  1906. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1907. * fence, whereas 965+ only requires a fence if using
  1908. * framebuffer compression. For simplicity, we always install
  1909. * a fence as the cost is not that onerous.
  1910. */
  1911. ret = i915_gem_object_get_fence(obj);
  1912. if (ret)
  1913. goto err_unpin;
  1914. i915_gem_object_pin_fence(obj);
  1915. dev_priv->mm.interruptible = true;
  1916. return 0;
  1917. err_unpin:
  1918. i915_gem_object_unpin_from_display_plane(obj);
  1919. err_interruptible:
  1920. dev_priv->mm.interruptible = true;
  1921. return ret;
  1922. }
  1923. void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
  1924. {
  1925. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  1926. i915_gem_object_unpin_fence(obj);
  1927. i915_gem_object_unpin_from_display_plane(obj);
  1928. }
  1929. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  1930. * is assumed to be a power-of-two. */
  1931. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  1932. unsigned int tiling_mode,
  1933. unsigned int cpp,
  1934. unsigned int pitch)
  1935. {
  1936. if (tiling_mode != I915_TILING_NONE) {
  1937. unsigned int tile_rows, tiles;
  1938. tile_rows = *y / 8;
  1939. *y %= 8;
  1940. tiles = *x / (512/cpp);
  1941. *x %= 512/cpp;
  1942. return tile_rows * pitch * 8 + tiles * 4096;
  1943. } else {
  1944. unsigned int offset;
  1945. offset = *y * pitch + *x * cpp;
  1946. *y = 0;
  1947. *x = (offset & 4095) / cpp;
  1948. return offset & -4096;
  1949. }
  1950. }
  1951. int intel_format_to_fourcc(int format)
  1952. {
  1953. switch (format) {
  1954. case DISPPLANE_8BPP:
  1955. return DRM_FORMAT_C8;
  1956. case DISPPLANE_BGRX555:
  1957. return DRM_FORMAT_XRGB1555;
  1958. case DISPPLANE_BGRX565:
  1959. return DRM_FORMAT_RGB565;
  1960. default:
  1961. case DISPPLANE_BGRX888:
  1962. return DRM_FORMAT_XRGB8888;
  1963. case DISPPLANE_RGBX888:
  1964. return DRM_FORMAT_XBGR8888;
  1965. case DISPPLANE_BGRX101010:
  1966. return DRM_FORMAT_XRGB2101010;
  1967. case DISPPLANE_RGBX101010:
  1968. return DRM_FORMAT_XBGR2101010;
  1969. }
  1970. }
  1971. static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
  1972. struct intel_plane_config *plane_config)
  1973. {
  1974. struct drm_device *dev = crtc->base.dev;
  1975. struct drm_i915_gem_object *obj = NULL;
  1976. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  1977. u32 base = plane_config->base;
  1978. if (plane_config->size == 0)
  1979. return false;
  1980. obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
  1981. plane_config->size);
  1982. if (!obj)
  1983. return false;
  1984. if (plane_config->tiled) {
  1985. obj->tiling_mode = I915_TILING_X;
  1986. obj->stride = crtc->base.primary->fb->pitches[0];
  1987. }
  1988. mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
  1989. mode_cmd.width = crtc->base.primary->fb->width;
  1990. mode_cmd.height = crtc->base.primary->fb->height;
  1991. mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
  1992. mutex_lock(&dev->struct_mutex);
  1993. if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
  1994. &mode_cmd, obj)) {
  1995. DRM_DEBUG_KMS("intel fb init failed\n");
  1996. goto out_unref_obj;
  1997. }
  1998. obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
  1999. mutex_unlock(&dev->struct_mutex);
  2000. DRM_DEBUG_KMS("plane fb obj %p\n", obj);
  2001. return true;
  2002. out_unref_obj:
  2003. drm_gem_object_unreference(&obj->base);
  2004. mutex_unlock(&dev->struct_mutex);
  2005. return false;
  2006. }
  2007. static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
  2008. struct intel_plane_config *plane_config)
  2009. {
  2010. struct drm_device *dev = intel_crtc->base.dev;
  2011. struct drm_crtc *c;
  2012. struct intel_crtc *i;
  2013. struct drm_i915_gem_object *obj;
  2014. if (!intel_crtc->base.primary->fb)
  2015. return;
  2016. if (intel_alloc_plane_obj(intel_crtc, plane_config))
  2017. return;
  2018. kfree(intel_crtc->base.primary->fb);
  2019. intel_crtc->base.primary->fb = NULL;
  2020. /*
  2021. * Failed to alloc the obj, check to see if we should share
  2022. * an fb with another CRTC instead
  2023. */
  2024. for_each_crtc(dev, c) {
  2025. i = to_intel_crtc(c);
  2026. if (c == &intel_crtc->base)
  2027. continue;
  2028. if (!i->active)
  2029. continue;
  2030. obj = intel_fb_obj(c->primary->fb);
  2031. if (obj == NULL)
  2032. continue;
  2033. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2034. drm_framebuffer_reference(c->primary->fb);
  2035. intel_crtc->base.primary->fb = c->primary->fb;
  2036. obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  2037. break;
  2038. }
  2039. }
  2040. }
  2041. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2042. struct drm_framebuffer *fb,
  2043. int x, int y)
  2044. {
  2045. struct drm_device *dev = crtc->dev;
  2046. struct drm_i915_private *dev_priv = dev->dev_private;
  2047. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2048. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2049. int plane = intel_crtc->plane;
  2050. unsigned long linear_offset;
  2051. u32 dspcntr;
  2052. u32 reg;
  2053. reg = DSPCNTR(plane);
  2054. dspcntr = I915_READ(reg);
  2055. /* Mask out pixel format bits in case we change it */
  2056. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  2057. switch (fb->pixel_format) {
  2058. case DRM_FORMAT_C8:
  2059. dspcntr |= DISPPLANE_8BPP;
  2060. break;
  2061. case DRM_FORMAT_XRGB1555:
  2062. case DRM_FORMAT_ARGB1555:
  2063. dspcntr |= DISPPLANE_BGRX555;
  2064. break;
  2065. case DRM_FORMAT_RGB565:
  2066. dspcntr |= DISPPLANE_BGRX565;
  2067. break;
  2068. case DRM_FORMAT_XRGB8888:
  2069. case DRM_FORMAT_ARGB8888:
  2070. dspcntr |= DISPPLANE_BGRX888;
  2071. break;
  2072. case DRM_FORMAT_XBGR8888:
  2073. case DRM_FORMAT_ABGR8888:
  2074. dspcntr |= DISPPLANE_RGBX888;
  2075. break;
  2076. case DRM_FORMAT_XRGB2101010:
  2077. case DRM_FORMAT_ARGB2101010:
  2078. dspcntr |= DISPPLANE_BGRX101010;
  2079. break;
  2080. case DRM_FORMAT_XBGR2101010:
  2081. case DRM_FORMAT_ABGR2101010:
  2082. dspcntr |= DISPPLANE_RGBX101010;
  2083. break;
  2084. default:
  2085. BUG();
  2086. }
  2087. if (INTEL_INFO(dev)->gen >= 4) {
  2088. if (obj->tiling_mode != I915_TILING_NONE)
  2089. dspcntr |= DISPPLANE_TILED;
  2090. else
  2091. dspcntr &= ~DISPPLANE_TILED;
  2092. }
  2093. if (IS_G4X(dev))
  2094. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2095. I915_WRITE(reg, dspcntr);
  2096. linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  2097. if (INTEL_INFO(dev)->gen >= 4) {
  2098. intel_crtc->dspaddr_offset =
  2099. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2100. fb->bits_per_pixel / 8,
  2101. fb->pitches[0]);
  2102. linear_offset -= intel_crtc->dspaddr_offset;
  2103. } else {
  2104. intel_crtc->dspaddr_offset = linear_offset;
  2105. }
  2106. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2107. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2108. fb->pitches[0]);
  2109. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2110. if (INTEL_INFO(dev)->gen >= 4) {
  2111. I915_WRITE(DSPSURF(plane),
  2112. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2113. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2114. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2115. } else
  2116. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2117. POSTING_READ(reg);
  2118. }
  2119. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2120. struct drm_framebuffer *fb,
  2121. int x, int y)
  2122. {
  2123. struct drm_device *dev = crtc->dev;
  2124. struct drm_i915_private *dev_priv = dev->dev_private;
  2125. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2126. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2127. int plane = intel_crtc->plane;
  2128. unsigned long linear_offset;
  2129. u32 dspcntr;
  2130. u32 reg;
  2131. reg = DSPCNTR(plane);
  2132. dspcntr = I915_READ(reg);
  2133. /* Mask out pixel format bits in case we change it */
  2134. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  2135. switch (fb->pixel_format) {
  2136. case DRM_FORMAT_C8:
  2137. dspcntr |= DISPPLANE_8BPP;
  2138. break;
  2139. case DRM_FORMAT_RGB565:
  2140. dspcntr |= DISPPLANE_BGRX565;
  2141. break;
  2142. case DRM_FORMAT_XRGB8888:
  2143. case DRM_FORMAT_ARGB8888:
  2144. dspcntr |= DISPPLANE_BGRX888;
  2145. break;
  2146. case DRM_FORMAT_XBGR8888:
  2147. case DRM_FORMAT_ABGR8888:
  2148. dspcntr |= DISPPLANE_RGBX888;
  2149. break;
  2150. case DRM_FORMAT_XRGB2101010:
  2151. case DRM_FORMAT_ARGB2101010:
  2152. dspcntr |= DISPPLANE_BGRX101010;
  2153. break;
  2154. case DRM_FORMAT_XBGR2101010:
  2155. case DRM_FORMAT_ABGR2101010:
  2156. dspcntr |= DISPPLANE_RGBX101010;
  2157. break;
  2158. default:
  2159. BUG();
  2160. }
  2161. if (obj->tiling_mode != I915_TILING_NONE)
  2162. dspcntr |= DISPPLANE_TILED;
  2163. else
  2164. dspcntr &= ~DISPPLANE_TILED;
  2165. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2166. dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
  2167. else
  2168. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2169. I915_WRITE(reg, dspcntr);
  2170. linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  2171. intel_crtc->dspaddr_offset =
  2172. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2173. fb->bits_per_pixel / 8,
  2174. fb->pitches[0]);
  2175. linear_offset -= intel_crtc->dspaddr_offset;
  2176. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2177. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2178. fb->pitches[0]);
  2179. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2180. I915_WRITE(DSPSURF(plane),
  2181. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2182. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2183. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2184. } else {
  2185. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2186. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2187. }
  2188. POSTING_READ(reg);
  2189. }
  2190. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2191. static int
  2192. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2193. int x, int y, enum mode_set_atomic state)
  2194. {
  2195. struct drm_device *dev = crtc->dev;
  2196. struct drm_i915_private *dev_priv = dev->dev_private;
  2197. if (dev_priv->display.disable_fbc)
  2198. dev_priv->display.disable_fbc(dev);
  2199. intel_increase_pllclock(dev, to_intel_crtc(crtc)->pipe);
  2200. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2201. return 0;
  2202. }
  2203. void intel_display_handle_reset(struct drm_device *dev)
  2204. {
  2205. struct drm_i915_private *dev_priv = dev->dev_private;
  2206. struct drm_crtc *crtc;
  2207. /*
  2208. * Flips in the rings have been nuked by the reset,
  2209. * so complete all pending flips so that user space
  2210. * will get its events and not get stuck.
  2211. *
  2212. * Also update the base address of all primary
  2213. * planes to the the last fb to make sure we're
  2214. * showing the correct fb after a reset.
  2215. *
  2216. * Need to make two loops over the crtcs so that we
  2217. * don't try to grab a crtc mutex before the
  2218. * pending_flip_queue really got woken up.
  2219. */
  2220. for_each_crtc(dev, crtc) {
  2221. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2222. enum plane plane = intel_crtc->plane;
  2223. intel_prepare_page_flip(dev, plane);
  2224. intel_finish_page_flip_plane(dev, plane);
  2225. }
  2226. for_each_crtc(dev, crtc) {
  2227. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2228. drm_modeset_lock(&crtc->mutex, NULL);
  2229. /*
  2230. * FIXME: Once we have proper support for primary planes (and
  2231. * disabling them without disabling the entire crtc) allow again
  2232. * a NULL crtc->primary->fb.
  2233. */
  2234. if (intel_crtc->active && crtc->primary->fb)
  2235. dev_priv->display.update_primary_plane(crtc,
  2236. crtc->primary->fb,
  2237. crtc->x,
  2238. crtc->y);
  2239. drm_modeset_unlock(&crtc->mutex);
  2240. }
  2241. }
  2242. static int
  2243. intel_finish_fb(struct drm_framebuffer *old_fb)
  2244. {
  2245. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2246. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2247. bool was_interruptible = dev_priv->mm.interruptible;
  2248. int ret;
  2249. /* Big Hammer, we also need to ensure that any pending
  2250. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2251. * current scanout is retired before unpinning the old
  2252. * framebuffer.
  2253. *
  2254. * This should only fail upon a hung GPU, in which case we
  2255. * can safely continue.
  2256. */
  2257. dev_priv->mm.interruptible = false;
  2258. ret = i915_gem_object_finish_gpu(obj);
  2259. dev_priv->mm.interruptible = was_interruptible;
  2260. return ret;
  2261. }
  2262. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2263. {
  2264. struct drm_device *dev = crtc->dev;
  2265. struct drm_i915_private *dev_priv = dev->dev_private;
  2266. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2267. unsigned long flags;
  2268. bool pending;
  2269. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2270. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2271. return false;
  2272. spin_lock_irqsave(&dev->event_lock, flags);
  2273. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2274. spin_unlock_irqrestore(&dev->event_lock, flags);
  2275. return pending;
  2276. }
  2277. static int
  2278. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  2279. struct drm_framebuffer *fb)
  2280. {
  2281. struct drm_device *dev = crtc->dev;
  2282. struct drm_i915_private *dev_priv = dev->dev_private;
  2283. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2284. enum pipe pipe = intel_crtc->pipe;
  2285. struct drm_framebuffer *old_fb = crtc->primary->fb;
  2286. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2287. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
  2288. int ret;
  2289. if (intel_crtc_has_pending_flip(crtc)) {
  2290. DRM_ERROR("pipe is still busy with an old pageflip\n");
  2291. return -EBUSY;
  2292. }
  2293. /* no fb bound */
  2294. if (!fb) {
  2295. DRM_ERROR("No FB bound\n");
  2296. return 0;
  2297. }
  2298. if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
  2299. DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
  2300. plane_name(intel_crtc->plane),
  2301. INTEL_INFO(dev)->num_pipes);
  2302. return -EINVAL;
  2303. }
  2304. mutex_lock(&dev->struct_mutex);
  2305. ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
  2306. if (ret == 0)
  2307. i915_gem_track_fb(old_obj, obj,
  2308. INTEL_FRONTBUFFER_PRIMARY(pipe));
  2309. mutex_unlock(&dev->struct_mutex);
  2310. if (ret != 0) {
  2311. DRM_ERROR("pin & fence failed\n");
  2312. return ret;
  2313. }
  2314. /*
  2315. * Update pipe size and adjust fitter if needed: the reason for this is
  2316. * that in compute_mode_changes we check the native mode (not the pfit
  2317. * mode) to see if we can flip rather than do a full mode set. In the
  2318. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2319. * pfit state, we'll end up with a big fb scanned out into the wrong
  2320. * sized surface.
  2321. *
  2322. * To fix this properly, we need to hoist the checks up into
  2323. * compute_mode_changes (or above), check the actual pfit state and
  2324. * whether the platform allows pfit disable with pipe active, and only
  2325. * then update the pipesrc and pfit state, even on the flip path.
  2326. */
  2327. if (i915.fastboot) {
  2328. const struct drm_display_mode *adjusted_mode =
  2329. &intel_crtc->config.adjusted_mode;
  2330. I915_WRITE(PIPESRC(intel_crtc->pipe),
  2331. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2332. (adjusted_mode->crtc_vdisplay - 1));
  2333. if (!intel_crtc->config.pch_pfit.enabled &&
  2334. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2335. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2336. I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
  2337. I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
  2338. I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
  2339. }
  2340. intel_crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
  2341. intel_crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
  2342. }
  2343. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2344. if (intel_crtc->active)
  2345. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  2346. crtc->primary->fb = fb;
  2347. crtc->x = x;
  2348. crtc->y = y;
  2349. if (old_fb) {
  2350. if (intel_crtc->active && old_fb != fb)
  2351. intel_wait_for_vblank(dev, intel_crtc->pipe);
  2352. mutex_lock(&dev->struct_mutex);
  2353. intel_unpin_fb_obj(old_obj);
  2354. mutex_unlock(&dev->struct_mutex);
  2355. }
  2356. mutex_lock(&dev->struct_mutex);
  2357. intel_update_fbc(dev);
  2358. mutex_unlock(&dev->struct_mutex);
  2359. return 0;
  2360. }
  2361. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2362. {
  2363. struct drm_device *dev = crtc->dev;
  2364. struct drm_i915_private *dev_priv = dev->dev_private;
  2365. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2366. int pipe = intel_crtc->pipe;
  2367. u32 reg, temp;
  2368. /* enable normal train */
  2369. reg = FDI_TX_CTL(pipe);
  2370. temp = I915_READ(reg);
  2371. if (IS_IVYBRIDGE(dev)) {
  2372. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2373. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2374. } else {
  2375. temp &= ~FDI_LINK_TRAIN_NONE;
  2376. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2377. }
  2378. I915_WRITE(reg, temp);
  2379. reg = FDI_RX_CTL(pipe);
  2380. temp = I915_READ(reg);
  2381. if (HAS_PCH_CPT(dev)) {
  2382. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2383. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2384. } else {
  2385. temp &= ~FDI_LINK_TRAIN_NONE;
  2386. temp |= FDI_LINK_TRAIN_NONE;
  2387. }
  2388. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2389. /* wait one idle pattern time */
  2390. POSTING_READ(reg);
  2391. udelay(1000);
  2392. /* IVB wants error correction enabled */
  2393. if (IS_IVYBRIDGE(dev))
  2394. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2395. FDI_FE_ERRC_ENABLE);
  2396. }
  2397. static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
  2398. {
  2399. return crtc->base.enabled && crtc->active &&
  2400. crtc->config.has_pch_encoder;
  2401. }
  2402. static void ivb_modeset_global_resources(struct drm_device *dev)
  2403. {
  2404. struct drm_i915_private *dev_priv = dev->dev_private;
  2405. struct intel_crtc *pipe_B_crtc =
  2406. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  2407. struct intel_crtc *pipe_C_crtc =
  2408. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
  2409. uint32_t temp;
  2410. /*
  2411. * When everything is off disable fdi C so that we could enable fdi B
  2412. * with all lanes. Note that we don't care about enabled pipes without
  2413. * an enabled pch encoder.
  2414. */
  2415. if (!pipe_has_enabled_pch(pipe_B_crtc) &&
  2416. !pipe_has_enabled_pch(pipe_C_crtc)) {
  2417. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2418. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2419. temp = I915_READ(SOUTH_CHICKEN1);
  2420. temp &= ~FDI_BC_BIFURCATION_SELECT;
  2421. DRM_DEBUG_KMS("disabling fdi C rx\n");
  2422. I915_WRITE(SOUTH_CHICKEN1, temp);
  2423. }
  2424. }
  2425. /* The FDI link training functions for ILK/Ibexpeak. */
  2426. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2427. {
  2428. struct drm_device *dev = crtc->dev;
  2429. struct drm_i915_private *dev_priv = dev->dev_private;
  2430. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2431. int pipe = intel_crtc->pipe;
  2432. u32 reg, temp, tries;
  2433. /* FDI needs bits from pipe first */
  2434. assert_pipe_enabled(dev_priv, pipe);
  2435. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2436. for train result */
  2437. reg = FDI_RX_IMR(pipe);
  2438. temp = I915_READ(reg);
  2439. temp &= ~FDI_RX_SYMBOL_LOCK;
  2440. temp &= ~FDI_RX_BIT_LOCK;
  2441. I915_WRITE(reg, temp);
  2442. I915_READ(reg);
  2443. udelay(150);
  2444. /* enable CPU FDI TX and PCH FDI RX */
  2445. reg = FDI_TX_CTL(pipe);
  2446. temp = I915_READ(reg);
  2447. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2448. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2449. temp &= ~FDI_LINK_TRAIN_NONE;
  2450. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2451. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2452. reg = FDI_RX_CTL(pipe);
  2453. temp = I915_READ(reg);
  2454. temp &= ~FDI_LINK_TRAIN_NONE;
  2455. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2456. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2457. POSTING_READ(reg);
  2458. udelay(150);
  2459. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2460. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2461. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2462. FDI_RX_PHASE_SYNC_POINTER_EN);
  2463. reg = FDI_RX_IIR(pipe);
  2464. for (tries = 0; tries < 5; tries++) {
  2465. temp = I915_READ(reg);
  2466. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2467. if ((temp & FDI_RX_BIT_LOCK)) {
  2468. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2469. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2470. break;
  2471. }
  2472. }
  2473. if (tries == 5)
  2474. DRM_ERROR("FDI train 1 fail!\n");
  2475. /* Train 2 */
  2476. reg = FDI_TX_CTL(pipe);
  2477. temp = I915_READ(reg);
  2478. temp &= ~FDI_LINK_TRAIN_NONE;
  2479. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2480. I915_WRITE(reg, temp);
  2481. reg = FDI_RX_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. POSTING_READ(reg);
  2487. udelay(150);
  2488. reg = FDI_RX_IIR(pipe);
  2489. for (tries = 0; tries < 5; tries++) {
  2490. temp = I915_READ(reg);
  2491. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2492. if (temp & FDI_RX_SYMBOL_LOCK) {
  2493. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2494. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2495. break;
  2496. }
  2497. }
  2498. if (tries == 5)
  2499. DRM_ERROR("FDI train 2 fail!\n");
  2500. DRM_DEBUG_KMS("FDI train done\n");
  2501. }
  2502. static const int snb_b_fdi_train_param[] = {
  2503. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2504. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2505. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2506. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2507. };
  2508. /* The FDI link training functions for SNB/Cougarpoint. */
  2509. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2510. {
  2511. struct drm_device *dev = crtc->dev;
  2512. struct drm_i915_private *dev_priv = dev->dev_private;
  2513. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2514. int pipe = intel_crtc->pipe;
  2515. u32 reg, temp, i, retry;
  2516. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2517. for train result */
  2518. reg = FDI_RX_IMR(pipe);
  2519. temp = I915_READ(reg);
  2520. temp &= ~FDI_RX_SYMBOL_LOCK;
  2521. temp &= ~FDI_RX_BIT_LOCK;
  2522. I915_WRITE(reg, temp);
  2523. POSTING_READ(reg);
  2524. udelay(150);
  2525. /* enable CPU FDI TX and PCH FDI RX */
  2526. reg = FDI_TX_CTL(pipe);
  2527. temp = I915_READ(reg);
  2528. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2529. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2530. temp &= ~FDI_LINK_TRAIN_NONE;
  2531. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2532. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2533. /* SNB-B */
  2534. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2535. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2536. I915_WRITE(FDI_RX_MISC(pipe),
  2537. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2538. reg = FDI_RX_CTL(pipe);
  2539. temp = I915_READ(reg);
  2540. if (HAS_PCH_CPT(dev)) {
  2541. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2542. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2543. } else {
  2544. temp &= ~FDI_LINK_TRAIN_NONE;
  2545. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2546. }
  2547. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2548. POSTING_READ(reg);
  2549. udelay(150);
  2550. for (i = 0; i < 4; i++) {
  2551. reg = FDI_TX_CTL(pipe);
  2552. temp = I915_READ(reg);
  2553. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2554. temp |= snb_b_fdi_train_param[i];
  2555. I915_WRITE(reg, temp);
  2556. POSTING_READ(reg);
  2557. udelay(500);
  2558. for (retry = 0; retry < 5; retry++) {
  2559. reg = FDI_RX_IIR(pipe);
  2560. temp = I915_READ(reg);
  2561. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2562. if (temp & FDI_RX_BIT_LOCK) {
  2563. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2564. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2565. break;
  2566. }
  2567. udelay(50);
  2568. }
  2569. if (retry < 5)
  2570. break;
  2571. }
  2572. if (i == 4)
  2573. DRM_ERROR("FDI train 1 fail!\n");
  2574. /* Train 2 */
  2575. reg = FDI_TX_CTL(pipe);
  2576. temp = I915_READ(reg);
  2577. temp &= ~FDI_LINK_TRAIN_NONE;
  2578. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2579. if (IS_GEN6(dev)) {
  2580. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2581. /* SNB-B */
  2582. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2583. }
  2584. I915_WRITE(reg, temp);
  2585. reg = FDI_RX_CTL(pipe);
  2586. temp = I915_READ(reg);
  2587. if (HAS_PCH_CPT(dev)) {
  2588. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2589. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2590. } else {
  2591. temp &= ~FDI_LINK_TRAIN_NONE;
  2592. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2593. }
  2594. I915_WRITE(reg, temp);
  2595. POSTING_READ(reg);
  2596. udelay(150);
  2597. for (i = 0; i < 4; i++) {
  2598. reg = FDI_TX_CTL(pipe);
  2599. temp = I915_READ(reg);
  2600. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2601. temp |= snb_b_fdi_train_param[i];
  2602. I915_WRITE(reg, temp);
  2603. POSTING_READ(reg);
  2604. udelay(500);
  2605. for (retry = 0; retry < 5; retry++) {
  2606. reg = FDI_RX_IIR(pipe);
  2607. temp = I915_READ(reg);
  2608. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2609. if (temp & FDI_RX_SYMBOL_LOCK) {
  2610. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2611. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2612. break;
  2613. }
  2614. udelay(50);
  2615. }
  2616. if (retry < 5)
  2617. break;
  2618. }
  2619. if (i == 4)
  2620. DRM_ERROR("FDI train 2 fail!\n");
  2621. DRM_DEBUG_KMS("FDI train done.\n");
  2622. }
  2623. /* Manual link training for Ivy Bridge A0 parts */
  2624. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2625. {
  2626. struct drm_device *dev = crtc->dev;
  2627. struct drm_i915_private *dev_priv = dev->dev_private;
  2628. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2629. int pipe = intel_crtc->pipe;
  2630. u32 reg, temp, i, j;
  2631. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2632. for train result */
  2633. reg = FDI_RX_IMR(pipe);
  2634. temp = I915_READ(reg);
  2635. temp &= ~FDI_RX_SYMBOL_LOCK;
  2636. temp &= ~FDI_RX_BIT_LOCK;
  2637. I915_WRITE(reg, temp);
  2638. POSTING_READ(reg);
  2639. udelay(150);
  2640. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  2641. I915_READ(FDI_RX_IIR(pipe)));
  2642. /* Try each vswing and preemphasis setting twice before moving on */
  2643. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  2644. /* disable first in case we need to retry */
  2645. reg = FDI_TX_CTL(pipe);
  2646. temp = I915_READ(reg);
  2647. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2648. temp &= ~FDI_TX_ENABLE;
  2649. I915_WRITE(reg, temp);
  2650. reg = FDI_RX_CTL(pipe);
  2651. temp = I915_READ(reg);
  2652. temp &= ~FDI_LINK_TRAIN_AUTO;
  2653. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2654. temp &= ~FDI_RX_ENABLE;
  2655. I915_WRITE(reg, temp);
  2656. /* enable CPU FDI TX and PCH FDI RX */
  2657. reg = FDI_TX_CTL(pipe);
  2658. temp = I915_READ(reg);
  2659. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2660. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2661. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2662. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2663. temp |= snb_b_fdi_train_param[j/2];
  2664. temp |= FDI_COMPOSITE_SYNC;
  2665. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2666. I915_WRITE(FDI_RX_MISC(pipe),
  2667. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2668. reg = FDI_RX_CTL(pipe);
  2669. temp = I915_READ(reg);
  2670. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2671. temp |= FDI_COMPOSITE_SYNC;
  2672. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2673. POSTING_READ(reg);
  2674. udelay(1); /* should be 0.5us */
  2675. for (i = 0; i < 4; i++) {
  2676. reg = FDI_RX_IIR(pipe);
  2677. temp = I915_READ(reg);
  2678. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2679. if (temp & FDI_RX_BIT_LOCK ||
  2680. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2681. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2682. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  2683. i);
  2684. break;
  2685. }
  2686. udelay(1); /* should be 0.5us */
  2687. }
  2688. if (i == 4) {
  2689. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  2690. continue;
  2691. }
  2692. /* Train 2 */
  2693. reg = FDI_TX_CTL(pipe);
  2694. temp = I915_READ(reg);
  2695. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2696. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2697. I915_WRITE(reg, temp);
  2698. reg = FDI_RX_CTL(pipe);
  2699. temp = I915_READ(reg);
  2700. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2701. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2702. I915_WRITE(reg, temp);
  2703. POSTING_READ(reg);
  2704. udelay(2); /* should be 1.5us */
  2705. for (i = 0; i < 4; i++) {
  2706. reg = FDI_RX_IIR(pipe);
  2707. temp = I915_READ(reg);
  2708. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2709. if (temp & FDI_RX_SYMBOL_LOCK ||
  2710. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  2711. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2712. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  2713. i);
  2714. goto train_done;
  2715. }
  2716. udelay(2); /* should be 1.5us */
  2717. }
  2718. if (i == 4)
  2719. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  2720. }
  2721. train_done:
  2722. DRM_DEBUG_KMS("FDI train done.\n");
  2723. }
  2724. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  2725. {
  2726. struct drm_device *dev = intel_crtc->base.dev;
  2727. struct drm_i915_private *dev_priv = dev->dev_private;
  2728. int pipe = intel_crtc->pipe;
  2729. u32 reg, temp;
  2730. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2731. reg = FDI_RX_CTL(pipe);
  2732. temp = I915_READ(reg);
  2733. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  2734. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2735. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2736. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2737. POSTING_READ(reg);
  2738. udelay(200);
  2739. /* Switch from Rawclk to PCDclk */
  2740. temp = I915_READ(reg);
  2741. I915_WRITE(reg, temp | FDI_PCDCLK);
  2742. POSTING_READ(reg);
  2743. udelay(200);
  2744. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2745. reg = FDI_TX_CTL(pipe);
  2746. temp = I915_READ(reg);
  2747. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2748. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2749. POSTING_READ(reg);
  2750. udelay(100);
  2751. }
  2752. }
  2753. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  2754. {
  2755. struct drm_device *dev = intel_crtc->base.dev;
  2756. struct drm_i915_private *dev_priv = dev->dev_private;
  2757. int pipe = intel_crtc->pipe;
  2758. u32 reg, temp;
  2759. /* Switch from PCDclk to Rawclk */
  2760. reg = FDI_RX_CTL(pipe);
  2761. temp = I915_READ(reg);
  2762. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2763. /* Disable CPU FDI TX PLL */
  2764. reg = FDI_TX_CTL(pipe);
  2765. temp = I915_READ(reg);
  2766. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2767. POSTING_READ(reg);
  2768. udelay(100);
  2769. reg = FDI_RX_CTL(pipe);
  2770. temp = I915_READ(reg);
  2771. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2772. /* Wait for the clocks to turn off. */
  2773. POSTING_READ(reg);
  2774. udelay(100);
  2775. }
  2776. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2777. {
  2778. struct drm_device *dev = crtc->dev;
  2779. struct drm_i915_private *dev_priv = dev->dev_private;
  2780. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2781. int pipe = intel_crtc->pipe;
  2782. u32 reg, temp;
  2783. /* disable CPU FDI tx and PCH FDI rx */
  2784. reg = FDI_TX_CTL(pipe);
  2785. temp = I915_READ(reg);
  2786. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2787. POSTING_READ(reg);
  2788. reg = FDI_RX_CTL(pipe);
  2789. temp = I915_READ(reg);
  2790. temp &= ~(0x7 << 16);
  2791. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2792. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2793. POSTING_READ(reg);
  2794. udelay(100);
  2795. /* Ironlake workaround, disable clock pointer after downing FDI */
  2796. if (HAS_PCH_IBX(dev))
  2797. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2798. /* still set train pattern 1 */
  2799. reg = FDI_TX_CTL(pipe);
  2800. temp = I915_READ(reg);
  2801. temp &= ~FDI_LINK_TRAIN_NONE;
  2802. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2803. I915_WRITE(reg, temp);
  2804. reg = FDI_RX_CTL(pipe);
  2805. temp = I915_READ(reg);
  2806. if (HAS_PCH_CPT(dev)) {
  2807. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2808. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2809. } else {
  2810. temp &= ~FDI_LINK_TRAIN_NONE;
  2811. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2812. }
  2813. /* BPC in FDI rx is consistent with that in PIPECONF */
  2814. temp &= ~(0x07 << 16);
  2815. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2816. I915_WRITE(reg, temp);
  2817. POSTING_READ(reg);
  2818. udelay(100);
  2819. }
  2820. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  2821. {
  2822. struct intel_crtc *crtc;
  2823. /* Note that we don't need to be called with mode_config.lock here
  2824. * as our list of CRTC objects is static for the lifetime of the
  2825. * device and so cannot disappear as we iterate. Similarly, we can
  2826. * happily treat the predicates as racy, atomic checks as userspace
  2827. * cannot claim and pin a new fb without at least acquring the
  2828. * struct_mutex and so serialising with us.
  2829. */
  2830. for_each_intel_crtc(dev, crtc) {
  2831. if (atomic_read(&crtc->unpin_work_count) == 0)
  2832. continue;
  2833. if (crtc->unpin_work)
  2834. intel_wait_for_vblank(dev, crtc->pipe);
  2835. return true;
  2836. }
  2837. return false;
  2838. }
  2839. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  2840. {
  2841. struct drm_device *dev = crtc->dev;
  2842. struct drm_i915_private *dev_priv = dev->dev_private;
  2843. if (crtc->primary->fb == NULL)
  2844. return;
  2845. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  2846. WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  2847. !intel_crtc_has_pending_flip(crtc),
  2848. 60*HZ) == 0);
  2849. mutex_lock(&dev->struct_mutex);
  2850. intel_finish_fb(crtc->primary->fb);
  2851. mutex_unlock(&dev->struct_mutex);
  2852. }
  2853. /* Program iCLKIP clock to the desired frequency */
  2854. static void lpt_program_iclkip(struct drm_crtc *crtc)
  2855. {
  2856. struct drm_device *dev = crtc->dev;
  2857. struct drm_i915_private *dev_priv = dev->dev_private;
  2858. int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
  2859. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  2860. u32 temp;
  2861. mutex_lock(&dev_priv->dpio_lock);
  2862. /* It is necessary to ungate the pixclk gate prior to programming
  2863. * the divisors, and gate it back when it is done.
  2864. */
  2865. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  2866. /* Disable SSCCTL */
  2867. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  2868. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  2869. SBI_SSCCTL_DISABLE,
  2870. SBI_ICLK);
  2871. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  2872. if (clock == 20000) {
  2873. auxdiv = 1;
  2874. divsel = 0x41;
  2875. phaseinc = 0x20;
  2876. } else {
  2877. /* The iCLK virtual clock root frequency is in MHz,
  2878. * but the adjusted_mode->crtc_clock in in KHz. To get the
  2879. * divisors, it is necessary to divide one by another, so we
  2880. * convert the virtual clock precision to KHz here for higher
  2881. * precision.
  2882. */
  2883. u32 iclk_virtual_root_freq = 172800 * 1000;
  2884. u32 iclk_pi_range = 64;
  2885. u32 desired_divisor, msb_divisor_value, pi_value;
  2886. desired_divisor = (iclk_virtual_root_freq / clock);
  2887. msb_divisor_value = desired_divisor / iclk_pi_range;
  2888. pi_value = desired_divisor % iclk_pi_range;
  2889. auxdiv = 0;
  2890. divsel = msb_divisor_value - 2;
  2891. phaseinc = pi_value;
  2892. }
  2893. /* This should not happen with any sane values */
  2894. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  2895. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  2896. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  2897. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  2898. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  2899. clock,
  2900. auxdiv,
  2901. divsel,
  2902. phasedir,
  2903. phaseinc);
  2904. /* Program SSCDIVINTPHASE6 */
  2905. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  2906. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  2907. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  2908. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  2909. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  2910. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  2911. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  2912. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  2913. /* Program SSCAUXDIV */
  2914. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  2915. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  2916. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  2917. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  2918. /* Enable modulator and associated divider */
  2919. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  2920. temp &= ~SBI_SSCCTL_DISABLE;
  2921. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  2922. /* Wait for initialization time */
  2923. udelay(24);
  2924. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  2925. mutex_unlock(&dev_priv->dpio_lock);
  2926. }
  2927. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  2928. enum pipe pch_transcoder)
  2929. {
  2930. struct drm_device *dev = crtc->base.dev;
  2931. struct drm_i915_private *dev_priv = dev->dev_private;
  2932. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  2933. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  2934. I915_READ(HTOTAL(cpu_transcoder)));
  2935. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  2936. I915_READ(HBLANK(cpu_transcoder)));
  2937. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  2938. I915_READ(HSYNC(cpu_transcoder)));
  2939. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  2940. I915_READ(VTOTAL(cpu_transcoder)));
  2941. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  2942. I915_READ(VBLANK(cpu_transcoder)));
  2943. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  2944. I915_READ(VSYNC(cpu_transcoder)));
  2945. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  2946. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  2947. }
  2948. static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
  2949. {
  2950. struct drm_i915_private *dev_priv = dev->dev_private;
  2951. uint32_t temp;
  2952. temp = I915_READ(SOUTH_CHICKEN1);
  2953. if (temp & FDI_BC_BIFURCATION_SELECT)
  2954. return;
  2955. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2956. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2957. temp |= FDI_BC_BIFURCATION_SELECT;
  2958. DRM_DEBUG_KMS("enabling fdi C rx\n");
  2959. I915_WRITE(SOUTH_CHICKEN1, temp);
  2960. POSTING_READ(SOUTH_CHICKEN1);
  2961. }
  2962. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  2963. {
  2964. struct drm_device *dev = intel_crtc->base.dev;
  2965. struct drm_i915_private *dev_priv = dev->dev_private;
  2966. switch (intel_crtc->pipe) {
  2967. case PIPE_A:
  2968. break;
  2969. case PIPE_B:
  2970. if (intel_crtc->config.fdi_lanes > 2)
  2971. WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
  2972. else
  2973. cpt_enable_fdi_bc_bifurcation(dev);
  2974. break;
  2975. case PIPE_C:
  2976. cpt_enable_fdi_bc_bifurcation(dev);
  2977. break;
  2978. default:
  2979. BUG();
  2980. }
  2981. }
  2982. /*
  2983. * Enable PCH resources required for PCH ports:
  2984. * - PCH PLLs
  2985. * - FDI training & RX/TX
  2986. * - update transcoder timings
  2987. * - DP transcoding bits
  2988. * - transcoder
  2989. */
  2990. static void ironlake_pch_enable(struct drm_crtc *crtc)
  2991. {
  2992. struct drm_device *dev = crtc->dev;
  2993. struct drm_i915_private *dev_priv = dev->dev_private;
  2994. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2995. int pipe = intel_crtc->pipe;
  2996. u32 reg, temp;
  2997. assert_pch_transcoder_disabled(dev_priv, pipe);
  2998. if (IS_IVYBRIDGE(dev))
  2999. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3000. /* Write the TU size bits before fdi link training, so that error
  3001. * detection works. */
  3002. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3003. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3004. /* For PCH output, training FDI link */
  3005. dev_priv->display.fdi_link_train(crtc);
  3006. /* We need to program the right clock selection before writing the pixel
  3007. * mutliplier into the DPLL. */
  3008. if (HAS_PCH_CPT(dev)) {
  3009. u32 sel;
  3010. temp = I915_READ(PCH_DPLL_SEL);
  3011. temp |= TRANS_DPLL_ENABLE(pipe);
  3012. sel = TRANS_DPLLB_SEL(pipe);
  3013. if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
  3014. temp |= sel;
  3015. else
  3016. temp &= ~sel;
  3017. I915_WRITE(PCH_DPLL_SEL, temp);
  3018. }
  3019. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3020. * transcoder, and we actually should do this to not upset any PCH
  3021. * transcoder that already use the clock when we share it.
  3022. *
  3023. * Note that enable_shared_dpll tries to do the right thing, but
  3024. * get_shared_dpll unconditionally resets the pll - we need that to have
  3025. * the right LVDS enable sequence. */
  3026. intel_enable_shared_dpll(intel_crtc);
  3027. /* set transcoder timing, panel must allow it */
  3028. assert_panel_unlocked(dev_priv, pipe);
  3029. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3030. intel_fdi_normal_train(crtc);
  3031. /* For PCH DP, enable TRANS_DP_CTL */
  3032. if (HAS_PCH_CPT(dev) &&
  3033. (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  3034. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  3035. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3036. reg = TRANS_DP_CTL(pipe);
  3037. temp = I915_READ(reg);
  3038. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3039. TRANS_DP_SYNC_MASK |
  3040. TRANS_DP_BPC_MASK);
  3041. temp |= (TRANS_DP_OUTPUT_ENABLE |
  3042. TRANS_DP_ENH_FRAMING);
  3043. temp |= bpc << 9; /* same format but at 11:9 */
  3044. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3045. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3046. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3047. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3048. switch (intel_trans_dp_port_sel(crtc)) {
  3049. case PCH_DP_B:
  3050. temp |= TRANS_DP_PORT_SEL_B;
  3051. break;
  3052. case PCH_DP_C:
  3053. temp |= TRANS_DP_PORT_SEL_C;
  3054. break;
  3055. case PCH_DP_D:
  3056. temp |= TRANS_DP_PORT_SEL_D;
  3057. break;
  3058. default:
  3059. BUG();
  3060. }
  3061. I915_WRITE(reg, temp);
  3062. }
  3063. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3064. }
  3065. static void lpt_pch_enable(struct drm_crtc *crtc)
  3066. {
  3067. struct drm_device *dev = crtc->dev;
  3068. struct drm_i915_private *dev_priv = dev->dev_private;
  3069. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3070. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3071. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3072. lpt_program_iclkip(crtc);
  3073. /* Set transcoder timing. */
  3074. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3075. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3076. }
  3077. void intel_put_shared_dpll(struct intel_crtc *crtc)
  3078. {
  3079. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3080. if (pll == NULL)
  3081. return;
  3082. if (pll->refcount == 0) {
  3083. WARN(1, "bad %s refcount\n", pll->name);
  3084. return;
  3085. }
  3086. if (--pll->refcount == 0) {
  3087. WARN_ON(pll->on);
  3088. WARN_ON(pll->active);
  3089. }
  3090. crtc->config.shared_dpll = DPLL_ID_PRIVATE;
  3091. }
  3092. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
  3093. {
  3094. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3095. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3096. enum intel_dpll_id i;
  3097. if (pll) {
  3098. DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
  3099. crtc->base.base.id, pll->name);
  3100. intel_put_shared_dpll(crtc);
  3101. }
  3102. if (HAS_PCH_IBX(dev_priv->dev)) {
  3103. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3104. i = (enum intel_dpll_id) crtc->pipe;
  3105. pll = &dev_priv->shared_dplls[i];
  3106. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3107. crtc->base.base.id, pll->name);
  3108. WARN_ON(pll->refcount);
  3109. goto found;
  3110. }
  3111. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3112. pll = &dev_priv->shared_dplls[i];
  3113. /* Only want to check enabled timings first */
  3114. if (pll->refcount == 0)
  3115. continue;
  3116. if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
  3117. sizeof(pll->hw_state)) == 0) {
  3118. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
  3119. crtc->base.base.id,
  3120. pll->name, pll->refcount, pll->active);
  3121. goto found;
  3122. }
  3123. }
  3124. /* Ok no matching timings, maybe there's a free one? */
  3125. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3126. pll = &dev_priv->shared_dplls[i];
  3127. if (pll->refcount == 0) {
  3128. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3129. crtc->base.base.id, pll->name);
  3130. goto found;
  3131. }
  3132. }
  3133. return NULL;
  3134. found:
  3135. if (pll->refcount == 0)
  3136. pll->hw_state = crtc->config.dpll_hw_state;
  3137. crtc->config.shared_dpll = i;
  3138. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3139. pipe_name(crtc->pipe));
  3140. pll->refcount++;
  3141. return pll;
  3142. }
  3143. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3144. {
  3145. struct drm_i915_private *dev_priv = dev->dev_private;
  3146. int dslreg = PIPEDSL(pipe);
  3147. u32 temp;
  3148. temp = I915_READ(dslreg);
  3149. udelay(500);
  3150. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3151. if (wait_for(I915_READ(dslreg) != temp, 5))
  3152. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3153. }
  3154. }
  3155. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3156. {
  3157. struct drm_device *dev = crtc->base.dev;
  3158. struct drm_i915_private *dev_priv = dev->dev_private;
  3159. int pipe = crtc->pipe;
  3160. if (crtc->config.pch_pfit.enabled) {
  3161. /* Force use of hard-coded filter coefficients
  3162. * as some pre-programmed values are broken,
  3163. * e.g. x201.
  3164. */
  3165. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3166. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3167. PF_PIPE_SEL_IVB(pipe));
  3168. else
  3169. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3170. I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
  3171. I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
  3172. }
  3173. }
  3174. static void intel_enable_planes(struct drm_crtc *crtc)
  3175. {
  3176. struct drm_device *dev = crtc->dev;
  3177. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3178. struct drm_plane *plane;
  3179. struct intel_plane *intel_plane;
  3180. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3181. intel_plane = to_intel_plane(plane);
  3182. if (intel_plane->pipe == pipe)
  3183. intel_plane_restore(&intel_plane->base);
  3184. }
  3185. }
  3186. static void intel_disable_planes(struct drm_crtc *crtc)
  3187. {
  3188. struct drm_device *dev = crtc->dev;
  3189. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3190. struct drm_plane *plane;
  3191. struct intel_plane *intel_plane;
  3192. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3193. intel_plane = to_intel_plane(plane);
  3194. if (intel_plane->pipe == pipe)
  3195. intel_plane_disable(&intel_plane->base);
  3196. }
  3197. }
  3198. void hsw_enable_ips(struct intel_crtc *crtc)
  3199. {
  3200. struct drm_device *dev = crtc->base.dev;
  3201. struct drm_i915_private *dev_priv = dev->dev_private;
  3202. if (!crtc->config.ips_enabled)
  3203. return;
  3204. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3205. intel_wait_for_vblank(dev, crtc->pipe);
  3206. assert_plane_enabled(dev_priv, crtc->plane);
  3207. if (IS_BROADWELL(dev)) {
  3208. mutex_lock(&dev_priv->rps.hw_lock);
  3209. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3210. mutex_unlock(&dev_priv->rps.hw_lock);
  3211. /* Quoting Art Runyan: "its not safe to expect any particular
  3212. * value in IPS_CTL bit 31 after enabling IPS through the
  3213. * mailbox." Moreover, the mailbox may return a bogus state,
  3214. * so we need to just enable it and continue on.
  3215. */
  3216. } else {
  3217. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3218. /* The bit only becomes 1 in the next vblank, so this wait here
  3219. * is essentially intel_wait_for_vblank. If we don't have this
  3220. * and don't wait for vblanks until the end of crtc_enable, then
  3221. * the HW state readout code will complain that the expected
  3222. * IPS_CTL value is not the one we read. */
  3223. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3224. DRM_ERROR("Timed out waiting for IPS enable\n");
  3225. }
  3226. }
  3227. void hsw_disable_ips(struct intel_crtc *crtc)
  3228. {
  3229. struct drm_device *dev = crtc->base.dev;
  3230. struct drm_i915_private *dev_priv = dev->dev_private;
  3231. if (!crtc->config.ips_enabled)
  3232. return;
  3233. assert_plane_enabled(dev_priv, crtc->plane);
  3234. if (IS_BROADWELL(dev)) {
  3235. mutex_lock(&dev_priv->rps.hw_lock);
  3236. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3237. mutex_unlock(&dev_priv->rps.hw_lock);
  3238. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3239. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3240. DRM_ERROR("Timed out waiting for IPS disable\n");
  3241. } else {
  3242. I915_WRITE(IPS_CTL, 0);
  3243. POSTING_READ(IPS_CTL);
  3244. }
  3245. /* We need to wait for a vblank before we can disable the plane. */
  3246. intel_wait_for_vblank(dev, crtc->pipe);
  3247. }
  3248. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3249. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3250. {
  3251. struct drm_device *dev = crtc->dev;
  3252. struct drm_i915_private *dev_priv = dev->dev_private;
  3253. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3254. enum pipe pipe = intel_crtc->pipe;
  3255. int palreg = PALETTE(pipe);
  3256. int i;
  3257. bool reenable_ips = false;
  3258. /* The clocks have to be on to load the palette. */
  3259. if (!crtc->enabled || !intel_crtc->active)
  3260. return;
  3261. if (!HAS_PCH_SPLIT(dev_priv->dev)) {
  3262. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  3263. assert_dsi_pll_enabled(dev_priv);
  3264. else
  3265. assert_pll_enabled(dev_priv, pipe);
  3266. }
  3267. /* use legacy palette for Ironlake */
  3268. if (!HAS_GMCH_DISPLAY(dev))
  3269. palreg = LGC_PALETTE(pipe);
  3270. /* Workaround : Do not read or write the pipe palette/gamma data while
  3271. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3272. */
  3273. if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
  3274. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3275. GAMMA_MODE_MODE_SPLIT)) {
  3276. hsw_disable_ips(intel_crtc);
  3277. reenable_ips = true;
  3278. }
  3279. for (i = 0; i < 256; i++) {
  3280. I915_WRITE(palreg + 4 * i,
  3281. (intel_crtc->lut_r[i] << 16) |
  3282. (intel_crtc->lut_g[i] << 8) |
  3283. intel_crtc->lut_b[i]);
  3284. }
  3285. if (reenable_ips)
  3286. hsw_enable_ips(intel_crtc);
  3287. }
  3288. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  3289. {
  3290. if (!enable && intel_crtc->overlay) {
  3291. struct drm_device *dev = intel_crtc->base.dev;
  3292. struct drm_i915_private *dev_priv = dev->dev_private;
  3293. mutex_lock(&dev->struct_mutex);
  3294. dev_priv->mm.interruptible = false;
  3295. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3296. dev_priv->mm.interruptible = true;
  3297. mutex_unlock(&dev->struct_mutex);
  3298. }
  3299. /* Let userspace switch the overlay on again. In most cases userspace
  3300. * has to recompute where to put it anyway.
  3301. */
  3302. }
  3303. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  3304. {
  3305. struct drm_device *dev = crtc->dev;
  3306. struct drm_i915_private *dev_priv = dev->dev_private;
  3307. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3308. int pipe = intel_crtc->pipe;
  3309. int plane = intel_crtc->plane;
  3310. drm_vblank_on(dev, pipe);
  3311. intel_enable_primary_hw_plane(dev_priv, plane, pipe);
  3312. intel_enable_planes(crtc);
  3313. intel_crtc_update_cursor(crtc, true);
  3314. intel_crtc_dpms_overlay(intel_crtc, true);
  3315. hsw_enable_ips(intel_crtc);
  3316. mutex_lock(&dev->struct_mutex);
  3317. intel_update_fbc(dev);
  3318. mutex_unlock(&dev->struct_mutex);
  3319. /*
  3320. * FIXME: Once we grow proper nuclear flip support out of this we need
  3321. * to compute the mask of flip planes precisely. For the time being
  3322. * consider this a flip from a NULL plane.
  3323. */
  3324. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3325. }
  3326. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  3327. {
  3328. struct drm_device *dev = crtc->dev;
  3329. struct drm_i915_private *dev_priv = dev->dev_private;
  3330. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3331. int pipe = intel_crtc->pipe;
  3332. int plane = intel_crtc->plane;
  3333. intel_crtc_wait_for_pending_flips(crtc);
  3334. if (dev_priv->fbc.plane == plane)
  3335. intel_disable_fbc(dev);
  3336. hsw_disable_ips(intel_crtc);
  3337. intel_crtc_dpms_overlay(intel_crtc, false);
  3338. intel_crtc_update_cursor(crtc, false);
  3339. intel_disable_planes(crtc);
  3340. intel_disable_primary_hw_plane(dev_priv, plane, pipe);
  3341. /*
  3342. * FIXME: Once we grow proper nuclear flip support out of this we need
  3343. * to compute the mask of flip planes precisely. For the time being
  3344. * consider this a flip to a NULL plane.
  3345. */
  3346. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3347. drm_vblank_off(dev, pipe);
  3348. }
  3349. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  3350. {
  3351. struct drm_device *dev = crtc->dev;
  3352. struct drm_i915_private *dev_priv = dev->dev_private;
  3353. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3354. struct intel_encoder *encoder;
  3355. int pipe = intel_crtc->pipe;
  3356. enum plane plane = intel_crtc->plane;
  3357. WARN_ON(!crtc->enabled);
  3358. if (intel_crtc->active)
  3359. return;
  3360. if (intel_crtc->config.has_pch_encoder)
  3361. intel_prepare_shared_dpll(intel_crtc);
  3362. if (intel_crtc->config.has_dp_encoder)
  3363. intel_dp_set_m_n(intel_crtc);
  3364. intel_set_pipe_timings(intel_crtc);
  3365. if (intel_crtc->config.has_pch_encoder) {
  3366. intel_cpu_transcoder_set_m_n(intel_crtc,
  3367. &intel_crtc->config.fdi_m_n, NULL);
  3368. }
  3369. ironlake_set_pipeconf(crtc);
  3370. /* Set up the display plane register */
  3371. I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
  3372. POSTING_READ(DSPCNTR(plane));
  3373. dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
  3374. crtc->x, crtc->y);
  3375. intel_crtc->active = true;
  3376. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3377. intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
  3378. for_each_encoder_on_crtc(dev, crtc, encoder)
  3379. if (encoder->pre_enable)
  3380. encoder->pre_enable(encoder);
  3381. if (intel_crtc->config.has_pch_encoder) {
  3382. /* Note: FDI PLL enabling _must_ be done before we enable the
  3383. * cpu pipes, hence this is separate from all the other fdi/pch
  3384. * enabling. */
  3385. ironlake_fdi_pll_enable(intel_crtc);
  3386. } else {
  3387. assert_fdi_tx_disabled(dev_priv, pipe);
  3388. assert_fdi_rx_disabled(dev_priv, pipe);
  3389. }
  3390. ironlake_pfit_enable(intel_crtc);
  3391. /*
  3392. * On ILK+ LUT must be loaded before the pipe is running but with
  3393. * clocks enabled
  3394. */
  3395. intel_crtc_load_lut(crtc);
  3396. intel_update_watermarks(crtc);
  3397. intel_enable_pipe(intel_crtc);
  3398. if (intel_crtc->config.has_pch_encoder)
  3399. ironlake_pch_enable(crtc);
  3400. for_each_encoder_on_crtc(dev, crtc, encoder)
  3401. encoder->enable(encoder);
  3402. if (HAS_PCH_CPT(dev))
  3403. cpt_verify_modeset(dev, intel_crtc->pipe);
  3404. intel_crtc_enable_planes(crtc);
  3405. }
  3406. /* IPS only exists on ULT machines and is tied to pipe A. */
  3407. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  3408. {
  3409. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  3410. }
  3411. /*
  3412. * This implements the workaround described in the "notes" section of the mode
  3413. * set sequence documentation. When going from no pipes or single pipe to
  3414. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  3415. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  3416. */
  3417. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  3418. {
  3419. struct drm_device *dev = crtc->base.dev;
  3420. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  3421. /* We want to get the other_active_crtc only if there's only 1 other
  3422. * active crtc. */
  3423. for_each_intel_crtc(dev, crtc_it) {
  3424. if (!crtc_it->active || crtc_it == crtc)
  3425. continue;
  3426. if (other_active_crtc)
  3427. return;
  3428. other_active_crtc = crtc_it;
  3429. }
  3430. if (!other_active_crtc)
  3431. return;
  3432. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3433. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3434. }
  3435. static void haswell_crtc_enable(struct drm_crtc *crtc)
  3436. {
  3437. struct drm_device *dev = crtc->dev;
  3438. struct drm_i915_private *dev_priv = dev->dev_private;
  3439. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3440. struct intel_encoder *encoder;
  3441. int pipe = intel_crtc->pipe;
  3442. enum plane plane = intel_crtc->plane;
  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. /* Set up the display plane register */
  3458. I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
  3459. POSTING_READ(DSPCNTR(plane));
  3460. dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
  3461. crtc->x, crtc->y);
  3462. intel_crtc->active = true;
  3463. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3464. for_each_encoder_on_crtc(dev, crtc, encoder)
  3465. if (encoder->pre_enable)
  3466. encoder->pre_enable(encoder);
  3467. if (intel_crtc->config.has_pch_encoder) {
  3468. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
  3469. dev_priv->display.fdi_link_train(crtc);
  3470. }
  3471. intel_ddi_enable_pipe_clock(intel_crtc);
  3472. ironlake_pfit_enable(intel_crtc);
  3473. /*
  3474. * On ILK+ LUT must be loaded before the pipe is running but with
  3475. * clocks enabled
  3476. */
  3477. intel_crtc_load_lut(crtc);
  3478. intel_ddi_set_pipe_settings(crtc);
  3479. intel_ddi_enable_transcoder_func(crtc);
  3480. intel_update_watermarks(crtc);
  3481. intel_enable_pipe(intel_crtc);
  3482. if (intel_crtc->config.has_pch_encoder)
  3483. lpt_pch_enable(crtc);
  3484. if (intel_crtc->config.dp_encoder_is_mst)
  3485. intel_ddi_set_vc_payload_alloc(crtc, true);
  3486. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3487. encoder->enable(encoder);
  3488. intel_opregion_notify_encoder(encoder, true);
  3489. }
  3490. /* If we change the relative order between pipe/planes enabling, we need
  3491. * to change the workaround. */
  3492. haswell_mode_set_planes_workaround(intel_crtc);
  3493. intel_crtc_enable_planes(crtc);
  3494. }
  3495. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  3496. {
  3497. struct drm_device *dev = crtc->base.dev;
  3498. struct drm_i915_private *dev_priv = dev->dev_private;
  3499. int pipe = crtc->pipe;
  3500. /* To avoid upsetting the power well on haswell only disable the pfit if
  3501. * it's in use. The hw state code will make sure we get this right. */
  3502. if (crtc->config.pch_pfit.enabled) {
  3503. I915_WRITE(PF_CTL(pipe), 0);
  3504. I915_WRITE(PF_WIN_POS(pipe), 0);
  3505. I915_WRITE(PF_WIN_SZ(pipe), 0);
  3506. }
  3507. }
  3508. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  3509. {
  3510. struct drm_device *dev = crtc->dev;
  3511. struct drm_i915_private *dev_priv = dev->dev_private;
  3512. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3513. struct intel_encoder *encoder;
  3514. int pipe = intel_crtc->pipe;
  3515. u32 reg, temp;
  3516. if (!intel_crtc->active)
  3517. return;
  3518. intel_crtc_disable_planes(crtc);
  3519. for_each_encoder_on_crtc(dev, crtc, encoder)
  3520. encoder->disable(encoder);
  3521. if (intel_crtc->config.has_pch_encoder)
  3522. intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
  3523. intel_disable_pipe(dev_priv, pipe);
  3524. if (intel_crtc->config.dp_encoder_is_mst)
  3525. intel_ddi_set_vc_payload_alloc(crtc, false);
  3526. ironlake_pfit_disable(intel_crtc);
  3527. for_each_encoder_on_crtc(dev, crtc, encoder)
  3528. if (encoder->post_disable)
  3529. encoder->post_disable(encoder);
  3530. if (intel_crtc->config.has_pch_encoder) {
  3531. ironlake_fdi_disable(crtc);
  3532. ironlake_disable_pch_transcoder(dev_priv, pipe);
  3533. intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
  3534. if (HAS_PCH_CPT(dev)) {
  3535. /* disable TRANS_DP_CTL */
  3536. reg = TRANS_DP_CTL(pipe);
  3537. temp = I915_READ(reg);
  3538. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  3539. TRANS_DP_PORT_SEL_MASK);
  3540. temp |= TRANS_DP_PORT_SEL_NONE;
  3541. I915_WRITE(reg, temp);
  3542. /* disable DPLL_SEL */
  3543. temp = I915_READ(PCH_DPLL_SEL);
  3544. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  3545. I915_WRITE(PCH_DPLL_SEL, temp);
  3546. }
  3547. /* disable PCH DPLL */
  3548. intel_disable_shared_dpll(intel_crtc);
  3549. ironlake_fdi_pll_disable(intel_crtc);
  3550. }
  3551. intel_crtc->active = false;
  3552. intel_update_watermarks(crtc);
  3553. mutex_lock(&dev->struct_mutex);
  3554. intel_update_fbc(dev);
  3555. mutex_unlock(&dev->struct_mutex);
  3556. }
  3557. static void haswell_crtc_disable(struct drm_crtc *crtc)
  3558. {
  3559. struct drm_device *dev = crtc->dev;
  3560. struct drm_i915_private *dev_priv = dev->dev_private;
  3561. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3562. struct intel_encoder *encoder;
  3563. int pipe = intel_crtc->pipe;
  3564. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3565. if (!intel_crtc->active)
  3566. return;
  3567. intel_crtc_disable_planes(crtc);
  3568. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3569. intel_opregion_notify_encoder(encoder, false);
  3570. encoder->disable(encoder);
  3571. }
  3572. if (intel_crtc->config.has_pch_encoder)
  3573. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
  3574. intel_disable_pipe(dev_priv, pipe);
  3575. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  3576. ironlake_pfit_disable(intel_crtc);
  3577. intel_ddi_disable_pipe_clock(intel_crtc);
  3578. if (intel_crtc->config.has_pch_encoder) {
  3579. lpt_disable_pch_transcoder(dev_priv);
  3580. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
  3581. intel_ddi_fdi_disable(crtc);
  3582. }
  3583. for_each_encoder_on_crtc(dev, crtc, encoder)
  3584. if (encoder->post_disable)
  3585. encoder->post_disable(encoder);
  3586. intel_crtc->active = false;
  3587. intel_update_watermarks(crtc);
  3588. mutex_lock(&dev->struct_mutex);
  3589. intel_update_fbc(dev);
  3590. mutex_unlock(&dev->struct_mutex);
  3591. if (intel_crtc_to_shared_dpll(intel_crtc))
  3592. intel_disable_shared_dpll(intel_crtc);
  3593. }
  3594. static void ironlake_crtc_off(struct drm_crtc *crtc)
  3595. {
  3596. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3597. intel_put_shared_dpll(intel_crtc);
  3598. }
  3599. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  3600. {
  3601. struct drm_device *dev = crtc->base.dev;
  3602. struct drm_i915_private *dev_priv = dev->dev_private;
  3603. struct intel_crtc_config *pipe_config = &crtc->config;
  3604. if (!crtc->config.gmch_pfit.control)
  3605. return;
  3606. /*
  3607. * The panel fitter should only be adjusted whilst the pipe is disabled,
  3608. * according to register description and PRM.
  3609. */
  3610. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  3611. assert_pipe_disabled(dev_priv, crtc->pipe);
  3612. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  3613. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  3614. /* Border color in case we don't scale up to the full screen. Black by
  3615. * default, change to something else for debugging. */
  3616. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  3617. }
  3618. static enum intel_display_power_domain port_to_power_domain(enum port port)
  3619. {
  3620. switch (port) {
  3621. case PORT_A:
  3622. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  3623. case PORT_B:
  3624. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  3625. case PORT_C:
  3626. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  3627. case PORT_D:
  3628. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  3629. default:
  3630. WARN_ON_ONCE(1);
  3631. return POWER_DOMAIN_PORT_OTHER;
  3632. }
  3633. }
  3634. #define for_each_power_domain(domain, mask) \
  3635. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  3636. if ((1 << (domain)) & (mask))
  3637. enum intel_display_power_domain
  3638. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  3639. {
  3640. struct drm_device *dev = intel_encoder->base.dev;
  3641. struct intel_digital_port *intel_dig_port;
  3642. switch (intel_encoder->type) {
  3643. case INTEL_OUTPUT_UNKNOWN:
  3644. /* Only DDI platforms should ever use this output type */
  3645. WARN_ON_ONCE(!HAS_DDI(dev));
  3646. case INTEL_OUTPUT_DISPLAYPORT:
  3647. case INTEL_OUTPUT_HDMI:
  3648. case INTEL_OUTPUT_EDP:
  3649. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  3650. return port_to_power_domain(intel_dig_port->port);
  3651. case INTEL_OUTPUT_DP_MST:
  3652. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  3653. return port_to_power_domain(intel_dig_port->port);
  3654. case INTEL_OUTPUT_ANALOG:
  3655. return POWER_DOMAIN_PORT_CRT;
  3656. case INTEL_OUTPUT_DSI:
  3657. return POWER_DOMAIN_PORT_DSI;
  3658. default:
  3659. return POWER_DOMAIN_PORT_OTHER;
  3660. }
  3661. }
  3662. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  3663. {
  3664. struct drm_device *dev = crtc->dev;
  3665. struct intel_encoder *intel_encoder;
  3666. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3667. enum pipe pipe = intel_crtc->pipe;
  3668. unsigned long mask;
  3669. enum transcoder transcoder;
  3670. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  3671. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  3672. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  3673. if (intel_crtc->config.pch_pfit.enabled ||
  3674. intel_crtc->config.pch_pfit.force_thru)
  3675. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  3676. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  3677. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  3678. return mask;
  3679. }
  3680. void intel_display_set_init_power(struct drm_i915_private *dev_priv,
  3681. bool enable)
  3682. {
  3683. if (dev_priv->power_domains.init_power_on == enable)
  3684. return;
  3685. if (enable)
  3686. intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
  3687. else
  3688. intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
  3689. dev_priv->power_domains.init_power_on = enable;
  3690. }
  3691. static void modeset_update_crtc_power_domains(struct drm_device *dev)
  3692. {
  3693. struct drm_i915_private *dev_priv = dev->dev_private;
  3694. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  3695. struct intel_crtc *crtc;
  3696. /*
  3697. * First get all needed power domains, then put all unneeded, to avoid
  3698. * any unnecessary toggling of the power wells.
  3699. */
  3700. for_each_intel_crtc(dev, crtc) {
  3701. enum intel_display_power_domain domain;
  3702. if (!crtc->base.enabled)
  3703. continue;
  3704. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  3705. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  3706. intel_display_power_get(dev_priv, domain);
  3707. }
  3708. for_each_intel_crtc(dev, crtc) {
  3709. enum intel_display_power_domain domain;
  3710. for_each_power_domain(domain, crtc->enabled_power_domains)
  3711. intel_display_power_put(dev_priv, domain);
  3712. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  3713. }
  3714. intel_display_set_init_power(dev_priv, false);
  3715. }
  3716. /* returns HPLL frequency in kHz */
  3717. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  3718. {
  3719. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  3720. /* Obtain SKU information */
  3721. mutex_lock(&dev_priv->dpio_lock);
  3722. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  3723. CCK_FUSE_HPLL_FREQ_MASK;
  3724. mutex_unlock(&dev_priv->dpio_lock);
  3725. return vco_freq[hpll_freq] * 1000;
  3726. }
  3727. static void vlv_update_cdclk(struct drm_device *dev)
  3728. {
  3729. struct drm_i915_private *dev_priv = dev->dev_private;
  3730. dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  3731. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz",
  3732. dev_priv->vlv_cdclk_freq);
  3733. /*
  3734. * Program the gmbus_freq based on the cdclk frequency.
  3735. * BSpec erroneously claims we should aim for 4MHz, but
  3736. * in fact 1MHz is the correct frequency.
  3737. */
  3738. I915_WRITE(GMBUSFREQ_VLV, dev_priv->vlv_cdclk_freq);
  3739. }
  3740. /* Adjust CDclk dividers to allow high res or save power if possible */
  3741. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  3742. {
  3743. struct drm_i915_private *dev_priv = dev->dev_private;
  3744. u32 val, cmd;
  3745. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  3746. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  3747. cmd = 2;
  3748. else if (cdclk == 266667)
  3749. cmd = 1;
  3750. else
  3751. cmd = 0;
  3752. mutex_lock(&dev_priv->rps.hw_lock);
  3753. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  3754. val &= ~DSPFREQGUAR_MASK;
  3755. val |= (cmd << DSPFREQGUAR_SHIFT);
  3756. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  3757. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  3758. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  3759. 50)) {
  3760. DRM_ERROR("timed out waiting for CDclk change\n");
  3761. }
  3762. mutex_unlock(&dev_priv->rps.hw_lock);
  3763. if (cdclk == 400000) {
  3764. u32 divider, vco;
  3765. vco = valleyview_get_vco(dev_priv);
  3766. divider = DIV_ROUND_CLOSEST(vco << 1, cdclk) - 1;
  3767. mutex_lock(&dev_priv->dpio_lock);
  3768. /* adjust cdclk divider */
  3769. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  3770. val &= ~DISPLAY_FREQUENCY_VALUES;
  3771. val |= divider;
  3772. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  3773. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  3774. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  3775. 50))
  3776. DRM_ERROR("timed out waiting for CDclk change\n");
  3777. mutex_unlock(&dev_priv->dpio_lock);
  3778. }
  3779. mutex_lock(&dev_priv->dpio_lock);
  3780. /* adjust self-refresh exit latency value */
  3781. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  3782. val &= ~0x7f;
  3783. /*
  3784. * For high bandwidth configs, we set a higher latency in the bunit
  3785. * so that the core display fetch happens in time to avoid underruns.
  3786. */
  3787. if (cdclk == 400000)
  3788. val |= 4500 / 250; /* 4.5 usec */
  3789. else
  3790. val |= 3000 / 250; /* 3.0 usec */
  3791. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  3792. mutex_unlock(&dev_priv->dpio_lock);
  3793. vlv_update_cdclk(dev);
  3794. }
  3795. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  3796. int max_pixclk)
  3797. {
  3798. int vco = valleyview_get_vco(dev_priv);
  3799. int freq_320 = (vco << 1) % 320000 != 0 ? 333333 : 320000;
  3800. /*
  3801. * Really only a few cases to deal with, as only 4 CDclks are supported:
  3802. * 200MHz
  3803. * 267MHz
  3804. * 320/333MHz (depends on HPLL freq)
  3805. * 400MHz
  3806. * So we check to see whether we're above 90% of the lower bin and
  3807. * adjust if needed.
  3808. *
  3809. * We seem to get an unstable or solid color picture at 200MHz.
  3810. * Not sure what's wrong. For now use 200MHz only when all pipes
  3811. * are off.
  3812. */
  3813. if (max_pixclk > freq_320*9/10)
  3814. return 400000;
  3815. else if (max_pixclk > 266667*9/10)
  3816. return freq_320;
  3817. else if (max_pixclk > 0)
  3818. return 266667;
  3819. else
  3820. return 200000;
  3821. }
  3822. /* compute the max pixel clock for new configuration */
  3823. static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
  3824. {
  3825. struct drm_device *dev = dev_priv->dev;
  3826. struct intel_crtc *intel_crtc;
  3827. int max_pixclk = 0;
  3828. for_each_intel_crtc(dev, intel_crtc) {
  3829. if (intel_crtc->new_enabled)
  3830. max_pixclk = max(max_pixclk,
  3831. intel_crtc->new_config->adjusted_mode.crtc_clock);
  3832. }
  3833. return max_pixclk;
  3834. }
  3835. static void valleyview_modeset_global_pipes(struct drm_device *dev,
  3836. unsigned *prepare_pipes)
  3837. {
  3838. struct drm_i915_private *dev_priv = dev->dev_private;
  3839. struct intel_crtc *intel_crtc;
  3840. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  3841. if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
  3842. dev_priv->vlv_cdclk_freq)
  3843. return;
  3844. /* disable/enable all currently active pipes while we change cdclk */
  3845. for_each_intel_crtc(dev, intel_crtc)
  3846. if (intel_crtc->base.enabled)
  3847. *prepare_pipes |= (1 << intel_crtc->pipe);
  3848. }
  3849. static void valleyview_modeset_global_resources(struct drm_device *dev)
  3850. {
  3851. struct drm_i915_private *dev_priv = dev->dev_private;
  3852. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  3853. int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  3854. if (req_cdclk != dev_priv->vlv_cdclk_freq)
  3855. valleyview_set_cdclk(dev, req_cdclk);
  3856. modeset_update_crtc_power_domains(dev);
  3857. }
  3858. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  3859. {
  3860. struct drm_device *dev = crtc->dev;
  3861. struct drm_i915_private *dev_priv = dev->dev_private;
  3862. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3863. struct intel_encoder *encoder;
  3864. int pipe = intel_crtc->pipe;
  3865. int plane = intel_crtc->plane;
  3866. bool is_dsi;
  3867. u32 dspcntr;
  3868. WARN_ON(!crtc->enabled);
  3869. if (intel_crtc->active)
  3870. return;
  3871. is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
  3872. if (!is_dsi && !IS_CHERRYVIEW(dev))
  3873. vlv_prepare_pll(intel_crtc);
  3874. /* Set up the display plane register */
  3875. dspcntr = DISPPLANE_GAMMA_ENABLE;
  3876. if (intel_crtc->config.has_dp_encoder)
  3877. intel_dp_set_m_n(intel_crtc);
  3878. intel_set_pipe_timings(intel_crtc);
  3879. /* pipesrc and dspsize control the size that is scaled from,
  3880. * which should always be the user's requested size.
  3881. */
  3882. I915_WRITE(DSPSIZE(plane),
  3883. ((intel_crtc->config.pipe_src_h - 1) << 16) |
  3884. (intel_crtc->config.pipe_src_w - 1));
  3885. I915_WRITE(DSPPOS(plane), 0);
  3886. i9xx_set_pipeconf(intel_crtc);
  3887. I915_WRITE(DSPCNTR(plane), dspcntr);
  3888. POSTING_READ(DSPCNTR(plane));
  3889. dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
  3890. crtc->x, crtc->y);
  3891. intel_crtc->active = true;
  3892. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3893. for_each_encoder_on_crtc(dev, crtc, encoder)
  3894. if (encoder->pre_pll_enable)
  3895. encoder->pre_pll_enable(encoder);
  3896. if (!is_dsi) {
  3897. if (IS_CHERRYVIEW(dev))
  3898. chv_enable_pll(intel_crtc);
  3899. else
  3900. vlv_enable_pll(intel_crtc);
  3901. }
  3902. for_each_encoder_on_crtc(dev, crtc, encoder)
  3903. if (encoder->pre_enable)
  3904. encoder->pre_enable(encoder);
  3905. i9xx_pfit_enable(intel_crtc);
  3906. intel_crtc_load_lut(crtc);
  3907. intel_update_watermarks(crtc);
  3908. intel_enable_pipe(intel_crtc);
  3909. for_each_encoder_on_crtc(dev, crtc, encoder)
  3910. encoder->enable(encoder);
  3911. intel_crtc_enable_planes(crtc);
  3912. /* Underruns don't raise interrupts, so check manually. */
  3913. i9xx_check_fifo_underruns(dev);
  3914. }
  3915. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  3916. {
  3917. struct drm_device *dev = crtc->base.dev;
  3918. struct drm_i915_private *dev_priv = dev->dev_private;
  3919. I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
  3920. I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
  3921. }
  3922. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  3923. {
  3924. struct drm_device *dev = crtc->dev;
  3925. struct drm_i915_private *dev_priv = dev->dev_private;
  3926. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3927. struct intel_encoder *encoder;
  3928. int pipe = intel_crtc->pipe;
  3929. int plane = intel_crtc->plane;
  3930. u32 dspcntr;
  3931. WARN_ON(!crtc->enabled);
  3932. if (intel_crtc->active)
  3933. return;
  3934. i9xx_set_pll_dividers(intel_crtc);
  3935. /* Set up the display plane register */
  3936. dspcntr = DISPPLANE_GAMMA_ENABLE;
  3937. if (pipe == 0)
  3938. dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
  3939. else
  3940. dspcntr |= DISPPLANE_SEL_PIPE_B;
  3941. if (intel_crtc->config.has_dp_encoder)
  3942. intel_dp_set_m_n(intel_crtc);
  3943. intel_set_pipe_timings(intel_crtc);
  3944. /* pipesrc and dspsize control the size that is scaled from,
  3945. * which should always be the user's requested size.
  3946. */
  3947. I915_WRITE(DSPSIZE(plane),
  3948. ((intel_crtc->config.pipe_src_h - 1) << 16) |
  3949. (intel_crtc->config.pipe_src_w - 1));
  3950. I915_WRITE(DSPPOS(plane), 0);
  3951. i9xx_set_pipeconf(intel_crtc);
  3952. I915_WRITE(DSPCNTR(plane), dspcntr);
  3953. POSTING_READ(DSPCNTR(plane));
  3954. dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
  3955. crtc->x, crtc->y);
  3956. intel_crtc->active = true;
  3957. if (!IS_GEN2(dev))
  3958. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3959. for_each_encoder_on_crtc(dev, crtc, encoder)
  3960. if (encoder->pre_enable)
  3961. encoder->pre_enable(encoder);
  3962. i9xx_enable_pll(intel_crtc);
  3963. i9xx_pfit_enable(intel_crtc);
  3964. intel_crtc_load_lut(crtc);
  3965. intel_update_watermarks(crtc);
  3966. intel_enable_pipe(intel_crtc);
  3967. for_each_encoder_on_crtc(dev, crtc, encoder)
  3968. encoder->enable(encoder);
  3969. intel_crtc_enable_planes(crtc);
  3970. /*
  3971. * Gen2 reports pipe underruns whenever all planes are disabled.
  3972. * So don't enable underrun reporting before at least some planes
  3973. * are enabled.
  3974. * FIXME: Need to fix the logic to work when we turn off all planes
  3975. * but leave the pipe running.
  3976. */
  3977. if (IS_GEN2(dev))
  3978. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3979. /* Underruns don't raise interrupts, so check manually. */
  3980. i9xx_check_fifo_underruns(dev);
  3981. }
  3982. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  3983. {
  3984. struct drm_device *dev = crtc->base.dev;
  3985. struct drm_i915_private *dev_priv = dev->dev_private;
  3986. if (!crtc->config.gmch_pfit.control)
  3987. return;
  3988. assert_pipe_disabled(dev_priv, crtc->pipe);
  3989. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  3990. I915_READ(PFIT_CONTROL));
  3991. I915_WRITE(PFIT_CONTROL, 0);
  3992. }
  3993. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  3994. {
  3995. struct drm_device *dev = crtc->dev;
  3996. struct drm_i915_private *dev_priv = dev->dev_private;
  3997. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3998. struct intel_encoder *encoder;
  3999. int pipe = intel_crtc->pipe;
  4000. if (!intel_crtc->active)
  4001. return;
  4002. /*
  4003. * Gen2 reports pipe underruns whenever all planes are disabled.
  4004. * So diasble underrun reporting before all the planes get disabled.
  4005. * FIXME: Need to fix the logic to work when we turn off all planes
  4006. * but leave the pipe running.
  4007. */
  4008. if (IS_GEN2(dev))
  4009. intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
  4010. /*
  4011. * Vblank time updates from the shadow to live plane control register
  4012. * are blocked if the memory self-refresh mode is active at that
  4013. * moment. So to make sure the plane gets truly disabled, disable
  4014. * first the self-refresh mode. The self-refresh enable bit in turn
  4015. * will be checked/applied by the HW only at the next frame start
  4016. * event which is after the vblank start event, so we need to have a
  4017. * wait-for-vblank between disabling the plane and the pipe.
  4018. */
  4019. intel_set_memory_cxsr(dev_priv, false);
  4020. intel_crtc_disable_planes(crtc);
  4021. for_each_encoder_on_crtc(dev, crtc, encoder)
  4022. encoder->disable(encoder);
  4023. /*
  4024. * On gen2 planes are double buffered but the pipe isn't, so we must
  4025. * wait for planes to fully turn off before disabling the pipe.
  4026. * We also need to wait on all gmch platforms because of the
  4027. * self-refresh mode constraint explained above.
  4028. */
  4029. intel_wait_for_vblank(dev, pipe);
  4030. intel_disable_pipe(dev_priv, pipe);
  4031. i9xx_pfit_disable(intel_crtc);
  4032. for_each_encoder_on_crtc(dev, crtc, encoder)
  4033. if (encoder->post_disable)
  4034. encoder->post_disable(encoder);
  4035. if (!intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI)) {
  4036. if (IS_CHERRYVIEW(dev))
  4037. chv_disable_pll(dev_priv, pipe);
  4038. else if (IS_VALLEYVIEW(dev))
  4039. vlv_disable_pll(dev_priv, pipe);
  4040. else
  4041. i9xx_disable_pll(dev_priv, pipe);
  4042. }
  4043. if (!IS_GEN2(dev))
  4044. intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
  4045. intel_crtc->active = false;
  4046. intel_update_watermarks(crtc);
  4047. mutex_lock(&dev->struct_mutex);
  4048. intel_update_fbc(dev);
  4049. mutex_unlock(&dev->struct_mutex);
  4050. }
  4051. static void i9xx_crtc_off(struct drm_crtc *crtc)
  4052. {
  4053. }
  4054. static void intel_crtc_update_sarea(struct drm_crtc *crtc,
  4055. bool enabled)
  4056. {
  4057. struct drm_device *dev = crtc->dev;
  4058. struct drm_i915_master_private *master_priv;
  4059. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4060. int pipe = intel_crtc->pipe;
  4061. if (!dev->primary->master)
  4062. return;
  4063. master_priv = dev->primary->master->driver_priv;
  4064. if (!master_priv->sarea_priv)
  4065. return;
  4066. switch (pipe) {
  4067. case 0:
  4068. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  4069. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  4070. break;
  4071. case 1:
  4072. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  4073. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  4074. break;
  4075. default:
  4076. DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
  4077. break;
  4078. }
  4079. }
  4080. /* Master function to enable/disable CRTC and corresponding power wells */
  4081. void intel_crtc_control(struct drm_crtc *crtc, bool enable)
  4082. {
  4083. struct drm_device *dev = crtc->dev;
  4084. struct drm_i915_private *dev_priv = dev->dev_private;
  4085. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4086. enum intel_display_power_domain domain;
  4087. unsigned long domains;
  4088. if (enable) {
  4089. if (!intel_crtc->active) {
  4090. domains = get_crtc_power_domains(crtc);
  4091. for_each_power_domain(domain, domains)
  4092. intel_display_power_get(dev_priv, domain);
  4093. intel_crtc->enabled_power_domains = domains;
  4094. dev_priv->display.crtc_enable(crtc);
  4095. }
  4096. } else {
  4097. if (intel_crtc->active) {
  4098. dev_priv->display.crtc_disable(crtc);
  4099. domains = intel_crtc->enabled_power_domains;
  4100. for_each_power_domain(domain, domains)
  4101. intel_display_power_put(dev_priv, domain);
  4102. intel_crtc->enabled_power_domains = 0;
  4103. }
  4104. }
  4105. }
  4106. /**
  4107. * Sets the power management mode of the pipe and plane.
  4108. */
  4109. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  4110. {
  4111. struct drm_device *dev = crtc->dev;
  4112. struct intel_encoder *intel_encoder;
  4113. bool enable = false;
  4114. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4115. enable |= intel_encoder->connectors_active;
  4116. intel_crtc_control(crtc, enable);
  4117. intel_crtc_update_sarea(crtc, enable);
  4118. }
  4119. static void intel_crtc_disable(struct drm_crtc *crtc)
  4120. {
  4121. struct drm_device *dev = crtc->dev;
  4122. struct drm_connector *connector;
  4123. struct drm_i915_private *dev_priv = dev->dev_private;
  4124. struct drm_i915_gem_object *old_obj = intel_fb_obj(crtc->primary->fb);
  4125. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  4126. /* crtc should still be enabled when we disable it. */
  4127. WARN_ON(!crtc->enabled);
  4128. dev_priv->display.crtc_disable(crtc);
  4129. intel_crtc_update_sarea(crtc, false);
  4130. dev_priv->display.off(crtc);
  4131. if (crtc->primary->fb) {
  4132. mutex_lock(&dev->struct_mutex);
  4133. intel_unpin_fb_obj(old_obj);
  4134. i915_gem_track_fb(old_obj, NULL,
  4135. INTEL_FRONTBUFFER_PRIMARY(pipe));
  4136. mutex_unlock(&dev->struct_mutex);
  4137. crtc->primary->fb = NULL;
  4138. }
  4139. /* Update computed state. */
  4140. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  4141. if (!connector->encoder || !connector->encoder->crtc)
  4142. continue;
  4143. if (connector->encoder->crtc != crtc)
  4144. continue;
  4145. connector->dpms = DRM_MODE_DPMS_OFF;
  4146. to_intel_encoder(connector->encoder)->connectors_active = false;
  4147. }
  4148. }
  4149. void intel_encoder_destroy(struct drm_encoder *encoder)
  4150. {
  4151. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4152. drm_encoder_cleanup(encoder);
  4153. kfree(intel_encoder);
  4154. }
  4155. /* Simple dpms helper for encoders with just one connector, no cloning and only
  4156. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  4157. * state of the entire output pipe. */
  4158. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  4159. {
  4160. if (mode == DRM_MODE_DPMS_ON) {
  4161. encoder->connectors_active = true;
  4162. intel_crtc_update_dpms(encoder->base.crtc);
  4163. } else {
  4164. encoder->connectors_active = false;
  4165. intel_crtc_update_dpms(encoder->base.crtc);
  4166. }
  4167. }
  4168. /* Cross check the actual hw state with our own modeset state tracking (and it's
  4169. * internal consistency). */
  4170. static void intel_connector_check_state(struct intel_connector *connector)
  4171. {
  4172. if (connector->get_hw_state(connector)) {
  4173. struct intel_encoder *encoder = connector->encoder;
  4174. struct drm_crtc *crtc;
  4175. bool encoder_enabled;
  4176. enum pipe pipe;
  4177. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4178. connector->base.base.id,
  4179. connector->base.name);
  4180. /* there is no real hw state for MST connectors */
  4181. if (connector->mst_port)
  4182. return;
  4183. WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  4184. "wrong connector dpms state\n");
  4185. WARN(connector->base.encoder != &encoder->base,
  4186. "active connector not linked to encoder\n");
  4187. if (encoder) {
  4188. WARN(!encoder->connectors_active,
  4189. "encoder->connectors_active not set\n");
  4190. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  4191. WARN(!encoder_enabled, "encoder not enabled\n");
  4192. if (WARN_ON(!encoder->base.crtc))
  4193. return;
  4194. crtc = encoder->base.crtc;
  4195. WARN(!crtc->enabled, "crtc not enabled\n");
  4196. WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  4197. WARN(pipe != to_intel_crtc(crtc)->pipe,
  4198. "encoder active on the wrong pipe\n");
  4199. }
  4200. }
  4201. }
  4202. /* Even simpler default implementation, if there's really no special case to
  4203. * consider. */
  4204. void intel_connector_dpms(struct drm_connector *connector, int mode)
  4205. {
  4206. /* All the simple cases only support two dpms states. */
  4207. if (mode != DRM_MODE_DPMS_ON)
  4208. mode = DRM_MODE_DPMS_OFF;
  4209. if (mode == connector->dpms)
  4210. return;
  4211. connector->dpms = mode;
  4212. /* Only need to change hw state when actually enabled */
  4213. if (connector->encoder)
  4214. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  4215. intel_modeset_check_state(connector->dev);
  4216. }
  4217. /* Simple connector->get_hw_state implementation for encoders that support only
  4218. * one connector and no cloning and hence the encoder state determines the state
  4219. * of the connector. */
  4220. bool intel_connector_get_hw_state(struct intel_connector *connector)
  4221. {
  4222. enum pipe pipe = 0;
  4223. struct intel_encoder *encoder = connector->encoder;
  4224. return encoder->get_hw_state(encoder, &pipe);
  4225. }
  4226. static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  4227. struct intel_crtc_config *pipe_config)
  4228. {
  4229. struct drm_i915_private *dev_priv = dev->dev_private;
  4230. struct intel_crtc *pipe_B_crtc =
  4231. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  4232. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  4233. pipe_name(pipe), pipe_config->fdi_lanes);
  4234. if (pipe_config->fdi_lanes > 4) {
  4235. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  4236. pipe_name(pipe), pipe_config->fdi_lanes);
  4237. return false;
  4238. }
  4239. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  4240. if (pipe_config->fdi_lanes > 2) {
  4241. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  4242. pipe_config->fdi_lanes);
  4243. return false;
  4244. } else {
  4245. return true;
  4246. }
  4247. }
  4248. if (INTEL_INFO(dev)->num_pipes == 2)
  4249. return true;
  4250. /* Ivybridge 3 pipe is really complicated */
  4251. switch (pipe) {
  4252. case PIPE_A:
  4253. return true;
  4254. case PIPE_B:
  4255. if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
  4256. pipe_config->fdi_lanes > 2) {
  4257. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4258. pipe_name(pipe), pipe_config->fdi_lanes);
  4259. return false;
  4260. }
  4261. return true;
  4262. case PIPE_C:
  4263. if (!pipe_has_enabled_pch(pipe_B_crtc) ||
  4264. pipe_B_crtc->config.fdi_lanes <= 2) {
  4265. if (pipe_config->fdi_lanes > 2) {
  4266. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4267. pipe_name(pipe), pipe_config->fdi_lanes);
  4268. return false;
  4269. }
  4270. } else {
  4271. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  4272. return false;
  4273. }
  4274. return true;
  4275. default:
  4276. BUG();
  4277. }
  4278. }
  4279. #define RETRY 1
  4280. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  4281. struct intel_crtc_config *pipe_config)
  4282. {
  4283. struct drm_device *dev = intel_crtc->base.dev;
  4284. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4285. int lane, link_bw, fdi_dotclock;
  4286. bool setup_ok, needs_recompute = false;
  4287. retry:
  4288. /* FDI is a binary signal running at ~2.7GHz, encoding
  4289. * each output octet as 10 bits. The actual frequency
  4290. * is stored as a divider into a 100MHz clock, and the
  4291. * mode pixel clock is stored in units of 1KHz.
  4292. * Hence the bw of each lane in terms of the mode signal
  4293. * is:
  4294. */
  4295. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4296. fdi_dotclock = adjusted_mode->crtc_clock;
  4297. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  4298. pipe_config->pipe_bpp);
  4299. pipe_config->fdi_lanes = lane;
  4300. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  4301. link_bw, &pipe_config->fdi_m_n);
  4302. setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  4303. intel_crtc->pipe, pipe_config);
  4304. if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
  4305. pipe_config->pipe_bpp -= 2*3;
  4306. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  4307. pipe_config->pipe_bpp);
  4308. needs_recompute = true;
  4309. pipe_config->bw_constrained = true;
  4310. goto retry;
  4311. }
  4312. if (needs_recompute)
  4313. return RETRY;
  4314. return setup_ok ? 0 : -EINVAL;
  4315. }
  4316. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  4317. struct intel_crtc_config *pipe_config)
  4318. {
  4319. pipe_config->ips_enabled = i915.enable_ips &&
  4320. hsw_crtc_supports_ips(crtc) &&
  4321. pipe_config->pipe_bpp <= 24;
  4322. }
  4323. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  4324. struct intel_crtc_config *pipe_config)
  4325. {
  4326. struct drm_device *dev = crtc->base.dev;
  4327. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4328. /* FIXME should check pixel clock limits on all platforms */
  4329. if (INTEL_INFO(dev)->gen < 4) {
  4330. struct drm_i915_private *dev_priv = dev->dev_private;
  4331. int clock_limit =
  4332. dev_priv->display.get_display_clock_speed(dev);
  4333. /*
  4334. * Enable pixel doubling when the dot clock
  4335. * is > 90% of the (display) core speed.
  4336. *
  4337. * GDG double wide on either pipe,
  4338. * otherwise pipe A only.
  4339. */
  4340. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  4341. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  4342. clock_limit *= 2;
  4343. pipe_config->double_wide = true;
  4344. }
  4345. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  4346. return -EINVAL;
  4347. }
  4348. /*
  4349. * Pipe horizontal size must be even in:
  4350. * - DVO ganged mode
  4351. * - LVDS dual channel mode
  4352. * - Double wide pipe
  4353. */
  4354. if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4355. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  4356. pipe_config->pipe_src_w &= ~1;
  4357. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  4358. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  4359. */
  4360. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  4361. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  4362. return -EINVAL;
  4363. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
  4364. pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
  4365. } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
  4366. /* only a 8bpc pipe, with 6bpc dither through the panel fitter
  4367. * for lvds. */
  4368. pipe_config->pipe_bpp = 8*3;
  4369. }
  4370. if (HAS_IPS(dev))
  4371. hsw_compute_ips_config(crtc, pipe_config);
  4372. /*
  4373. * XXX: PCH/WRPLL clock sharing is done in ->mode_set, so make sure the
  4374. * old clock survives for now.
  4375. */
  4376. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev) || HAS_DDI(dev))
  4377. pipe_config->shared_dpll = crtc->config.shared_dpll;
  4378. if (pipe_config->has_pch_encoder)
  4379. return ironlake_fdi_compute_config(crtc, pipe_config);
  4380. return 0;
  4381. }
  4382. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  4383. {
  4384. struct drm_i915_private *dev_priv = dev->dev_private;
  4385. int vco = valleyview_get_vco(dev_priv);
  4386. u32 val;
  4387. int divider;
  4388. mutex_lock(&dev_priv->dpio_lock);
  4389. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4390. mutex_unlock(&dev_priv->dpio_lock);
  4391. divider = val & DISPLAY_FREQUENCY_VALUES;
  4392. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  4393. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4394. "cdclk change in progress\n");
  4395. return DIV_ROUND_CLOSEST(vco << 1, divider + 1);
  4396. }
  4397. static int i945_get_display_clock_speed(struct drm_device *dev)
  4398. {
  4399. return 400000;
  4400. }
  4401. static int i915_get_display_clock_speed(struct drm_device *dev)
  4402. {
  4403. return 333000;
  4404. }
  4405. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  4406. {
  4407. return 200000;
  4408. }
  4409. static int pnv_get_display_clock_speed(struct drm_device *dev)
  4410. {
  4411. u16 gcfgc = 0;
  4412. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4413. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4414. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  4415. return 267000;
  4416. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  4417. return 333000;
  4418. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  4419. return 444000;
  4420. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  4421. return 200000;
  4422. default:
  4423. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  4424. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  4425. return 133000;
  4426. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  4427. return 167000;
  4428. }
  4429. }
  4430. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  4431. {
  4432. u16 gcfgc = 0;
  4433. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4434. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  4435. return 133000;
  4436. else {
  4437. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4438. case GC_DISPLAY_CLOCK_333_MHZ:
  4439. return 333000;
  4440. default:
  4441. case GC_DISPLAY_CLOCK_190_200_MHZ:
  4442. return 190000;
  4443. }
  4444. }
  4445. }
  4446. static int i865_get_display_clock_speed(struct drm_device *dev)
  4447. {
  4448. return 266000;
  4449. }
  4450. static int i855_get_display_clock_speed(struct drm_device *dev)
  4451. {
  4452. u16 hpllcc = 0;
  4453. /* Assume that the hardware is in the high speed state. This
  4454. * should be the default.
  4455. */
  4456. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  4457. case GC_CLOCK_133_200:
  4458. case GC_CLOCK_100_200:
  4459. return 200000;
  4460. case GC_CLOCK_166_250:
  4461. return 250000;
  4462. case GC_CLOCK_100_133:
  4463. return 133000;
  4464. }
  4465. /* Shouldn't happen */
  4466. return 0;
  4467. }
  4468. static int i830_get_display_clock_speed(struct drm_device *dev)
  4469. {
  4470. return 133000;
  4471. }
  4472. static void
  4473. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  4474. {
  4475. while (*num > DATA_LINK_M_N_MASK ||
  4476. *den > DATA_LINK_M_N_MASK) {
  4477. *num >>= 1;
  4478. *den >>= 1;
  4479. }
  4480. }
  4481. static void compute_m_n(unsigned int m, unsigned int n,
  4482. uint32_t *ret_m, uint32_t *ret_n)
  4483. {
  4484. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  4485. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  4486. intel_reduce_m_n_ratio(ret_m, ret_n);
  4487. }
  4488. void
  4489. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  4490. int pixel_clock, int link_clock,
  4491. struct intel_link_m_n *m_n)
  4492. {
  4493. m_n->tu = 64;
  4494. compute_m_n(bits_per_pixel * pixel_clock,
  4495. link_clock * nlanes * 8,
  4496. &m_n->gmch_m, &m_n->gmch_n);
  4497. compute_m_n(pixel_clock, link_clock,
  4498. &m_n->link_m, &m_n->link_n);
  4499. }
  4500. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  4501. {
  4502. if (i915.panel_use_ssc >= 0)
  4503. return i915.panel_use_ssc != 0;
  4504. return dev_priv->vbt.lvds_use_ssc
  4505. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  4506. }
  4507. static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
  4508. {
  4509. struct drm_device *dev = crtc->dev;
  4510. struct drm_i915_private *dev_priv = dev->dev_private;
  4511. int refclk;
  4512. if (IS_VALLEYVIEW(dev)) {
  4513. refclk = 100000;
  4514. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  4515. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4516. refclk = dev_priv->vbt.lvds_ssc_freq;
  4517. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  4518. } else if (!IS_GEN2(dev)) {
  4519. refclk = 96000;
  4520. } else {
  4521. refclk = 48000;
  4522. }
  4523. return refclk;
  4524. }
  4525. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  4526. {
  4527. return (1 << dpll->n) << 16 | dpll->m2;
  4528. }
  4529. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  4530. {
  4531. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  4532. }
  4533. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  4534. intel_clock_t *reduced_clock)
  4535. {
  4536. struct drm_device *dev = crtc->base.dev;
  4537. u32 fp, fp2 = 0;
  4538. if (IS_PINEVIEW(dev)) {
  4539. fp = pnv_dpll_compute_fp(&crtc->config.dpll);
  4540. if (reduced_clock)
  4541. fp2 = pnv_dpll_compute_fp(reduced_clock);
  4542. } else {
  4543. fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
  4544. if (reduced_clock)
  4545. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  4546. }
  4547. crtc->config.dpll_hw_state.fp0 = fp;
  4548. crtc->lowfreq_avail = false;
  4549. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4550. reduced_clock && i915.powersave) {
  4551. crtc->config.dpll_hw_state.fp1 = fp2;
  4552. crtc->lowfreq_avail = true;
  4553. } else {
  4554. crtc->config.dpll_hw_state.fp1 = fp;
  4555. }
  4556. }
  4557. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  4558. pipe)
  4559. {
  4560. u32 reg_val;
  4561. /*
  4562. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  4563. * and set it to a reasonable value instead.
  4564. */
  4565. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4566. reg_val &= 0xffffff00;
  4567. reg_val |= 0x00000030;
  4568. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4569. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4570. reg_val &= 0x8cffffff;
  4571. reg_val = 0x8c000000;
  4572. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4573. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4574. reg_val &= 0xffffff00;
  4575. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4576. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4577. reg_val &= 0x00ffffff;
  4578. reg_val |= 0xb0000000;
  4579. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4580. }
  4581. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  4582. struct intel_link_m_n *m_n)
  4583. {
  4584. struct drm_device *dev = crtc->base.dev;
  4585. struct drm_i915_private *dev_priv = dev->dev_private;
  4586. int pipe = crtc->pipe;
  4587. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4588. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  4589. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  4590. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  4591. }
  4592. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  4593. struct intel_link_m_n *m_n,
  4594. struct intel_link_m_n *m2_n2)
  4595. {
  4596. struct drm_device *dev = crtc->base.dev;
  4597. struct drm_i915_private *dev_priv = dev->dev_private;
  4598. int pipe = crtc->pipe;
  4599. enum transcoder transcoder = crtc->config.cpu_transcoder;
  4600. if (INTEL_INFO(dev)->gen >= 5) {
  4601. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4602. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  4603. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  4604. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  4605. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  4606. * for gen < 8) and if DRRS is supported (to make sure the
  4607. * registers are not unnecessarily accessed).
  4608. */
  4609. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  4610. crtc->config.has_drrs) {
  4611. I915_WRITE(PIPE_DATA_M2(transcoder),
  4612. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  4613. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  4614. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  4615. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  4616. }
  4617. } else {
  4618. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4619. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  4620. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  4621. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  4622. }
  4623. }
  4624. void intel_dp_set_m_n(struct intel_crtc *crtc)
  4625. {
  4626. if (crtc->config.has_pch_encoder)
  4627. intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
  4628. else
  4629. intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n,
  4630. &crtc->config.dp_m2_n2);
  4631. }
  4632. static void vlv_update_pll(struct intel_crtc *crtc)
  4633. {
  4634. u32 dpll, dpll_md;
  4635. /*
  4636. * Enable DPIO clock input. We should never disable the reference
  4637. * clock for pipe B, since VGA hotplug / manual detection depends
  4638. * on it.
  4639. */
  4640. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  4641. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  4642. /* We should never disable this, set it here for state tracking */
  4643. if (crtc->pipe == PIPE_B)
  4644. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4645. dpll |= DPLL_VCO_ENABLE;
  4646. crtc->config.dpll_hw_state.dpll = dpll;
  4647. dpll_md = (crtc->config.pixel_multiplier - 1)
  4648. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4649. crtc->config.dpll_hw_state.dpll_md = dpll_md;
  4650. }
  4651. static void vlv_prepare_pll(struct intel_crtc *crtc)
  4652. {
  4653. struct drm_device *dev = crtc->base.dev;
  4654. struct drm_i915_private *dev_priv = dev->dev_private;
  4655. int pipe = crtc->pipe;
  4656. u32 mdiv;
  4657. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  4658. u32 coreclk, reg_val;
  4659. mutex_lock(&dev_priv->dpio_lock);
  4660. bestn = crtc->config.dpll.n;
  4661. bestm1 = crtc->config.dpll.m1;
  4662. bestm2 = crtc->config.dpll.m2;
  4663. bestp1 = crtc->config.dpll.p1;
  4664. bestp2 = crtc->config.dpll.p2;
  4665. /* See eDP HDMI DPIO driver vbios notes doc */
  4666. /* PLL B needs special handling */
  4667. if (pipe == PIPE_B)
  4668. vlv_pllb_recal_opamp(dev_priv, pipe);
  4669. /* Set up Tx target for periodic Rcomp update */
  4670. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  4671. /* Disable target IRef on PLL */
  4672. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  4673. reg_val &= 0x00ffffff;
  4674. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  4675. /* Disable fast lock */
  4676. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  4677. /* Set idtafcrecal before PLL is enabled */
  4678. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  4679. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  4680. mdiv |= ((bestn << DPIO_N_SHIFT));
  4681. mdiv |= (1 << DPIO_K_SHIFT);
  4682. /*
  4683. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  4684. * but we don't support that).
  4685. * Note: don't use the DAC post divider as it seems unstable.
  4686. */
  4687. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  4688. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4689. mdiv |= DPIO_ENABLE_CALIBRATION;
  4690. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4691. /* Set HBR and RBR LPF coefficients */
  4692. if (crtc->config.port_clock == 162000 ||
  4693. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
  4694. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
  4695. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4696. 0x009f0003);
  4697. else
  4698. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4699. 0x00d0000f);
  4700. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
  4701. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
  4702. /* Use SSC source */
  4703. if (pipe == PIPE_A)
  4704. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4705. 0x0df40000);
  4706. else
  4707. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4708. 0x0df70000);
  4709. } else { /* HDMI or VGA */
  4710. /* Use bend source */
  4711. if (pipe == PIPE_A)
  4712. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4713. 0x0df70000);
  4714. else
  4715. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4716. 0x0df40000);
  4717. }
  4718. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  4719. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  4720. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
  4721. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
  4722. coreclk |= 0x01000000;
  4723. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  4724. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  4725. mutex_unlock(&dev_priv->dpio_lock);
  4726. }
  4727. static void chv_update_pll(struct intel_crtc *crtc)
  4728. {
  4729. struct drm_device *dev = crtc->base.dev;
  4730. struct drm_i915_private *dev_priv = dev->dev_private;
  4731. int pipe = crtc->pipe;
  4732. int dpll_reg = DPLL(crtc->pipe);
  4733. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  4734. u32 loopfilter, intcoeff;
  4735. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  4736. int refclk;
  4737. crtc->config.dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  4738. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  4739. DPLL_VCO_ENABLE;
  4740. if (pipe != PIPE_A)
  4741. crtc->config.dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4742. crtc->config.dpll_hw_state.dpll_md =
  4743. (crtc->config.pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4744. bestn = crtc->config.dpll.n;
  4745. bestm2_frac = crtc->config.dpll.m2 & 0x3fffff;
  4746. bestm1 = crtc->config.dpll.m1;
  4747. bestm2 = crtc->config.dpll.m2 >> 22;
  4748. bestp1 = crtc->config.dpll.p1;
  4749. bestp2 = crtc->config.dpll.p2;
  4750. /*
  4751. * Enable Refclk and SSC
  4752. */
  4753. I915_WRITE(dpll_reg,
  4754. crtc->config.dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  4755. mutex_lock(&dev_priv->dpio_lock);
  4756. /* p1 and p2 divider */
  4757. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  4758. 5 << DPIO_CHV_S1_DIV_SHIFT |
  4759. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  4760. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  4761. 1 << DPIO_CHV_K_DIV_SHIFT);
  4762. /* Feedback post-divider - m2 */
  4763. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  4764. /* Feedback refclk divider - n and m1 */
  4765. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  4766. DPIO_CHV_M1_DIV_BY_2 |
  4767. 1 << DPIO_CHV_N_DIV_SHIFT);
  4768. /* M2 fraction division */
  4769. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  4770. /* M2 fraction division enable */
  4771. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
  4772. DPIO_CHV_FRAC_DIV_EN |
  4773. (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
  4774. /* Loop filter */
  4775. refclk = i9xx_get_refclk(&crtc->base, 0);
  4776. loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
  4777. 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
  4778. if (refclk == 100000)
  4779. intcoeff = 11;
  4780. else if (refclk == 38400)
  4781. intcoeff = 10;
  4782. else
  4783. intcoeff = 9;
  4784. loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
  4785. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  4786. /* AFC Recal */
  4787. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  4788. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  4789. DPIO_AFC_RECAL);
  4790. mutex_unlock(&dev_priv->dpio_lock);
  4791. }
  4792. static void i9xx_update_pll(struct intel_crtc *crtc,
  4793. intel_clock_t *reduced_clock,
  4794. int num_connectors)
  4795. {
  4796. struct drm_device *dev = crtc->base.dev;
  4797. struct drm_i915_private *dev_priv = dev->dev_private;
  4798. u32 dpll;
  4799. bool is_sdvo;
  4800. struct dpll *clock = &crtc->config.dpll;
  4801. i9xx_update_pll_dividers(crtc, reduced_clock);
  4802. is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
  4803. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
  4804. dpll = DPLL_VGA_MODE_DIS;
  4805. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
  4806. dpll |= DPLLB_MODE_LVDS;
  4807. else
  4808. dpll |= DPLLB_MODE_DAC_SERIAL;
  4809. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  4810. dpll |= (crtc->config.pixel_multiplier - 1)
  4811. << SDVO_MULTIPLIER_SHIFT_HIRES;
  4812. }
  4813. if (is_sdvo)
  4814. dpll |= DPLL_SDVO_HIGH_SPEED;
  4815. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
  4816. dpll |= DPLL_SDVO_HIGH_SPEED;
  4817. /* compute bitmask from p1 value */
  4818. if (IS_PINEVIEW(dev))
  4819. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  4820. else {
  4821. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4822. if (IS_G4X(dev) && reduced_clock)
  4823. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  4824. }
  4825. switch (clock->p2) {
  4826. case 5:
  4827. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  4828. break;
  4829. case 7:
  4830. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  4831. break;
  4832. case 10:
  4833. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  4834. break;
  4835. case 14:
  4836. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  4837. break;
  4838. }
  4839. if (INTEL_INFO(dev)->gen >= 4)
  4840. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  4841. if (crtc->config.sdvo_tv_clock)
  4842. dpll |= PLL_REF_INPUT_TVCLKINBC;
  4843. else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4844. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4845. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4846. else
  4847. dpll |= PLL_REF_INPUT_DREFCLK;
  4848. dpll |= DPLL_VCO_ENABLE;
  4849. crtc->config.dpll_hw_state.dpll = dpll;
  4850. if (INTEL_INFO(dev)->gen >= 4) {
  4851. u32 dpll_md = (crtc->config.pixel_multiplier - 1)
  4852. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4853. crtc->config.dpll_hw_state.dpll_md = dpll_md;
  4854. }
  4855. }
  4856. static void i8xx_update_pll(struct intel_crtc *crtc,
  4857. intel_clock_t *reduced_clock,
  4858. int num_connectors)
  4859. {
  4860. struct drm_device *dev = crtc->base.dev;
  4861. struct drm_i915_private *dev_priv = dev->dev_private;
  4862. u32 dpll;
  4863. struct dpll *clock = &crtc->config.dpll;
  4864. i9xx_update_pll_dividers(crtc, reduced_clock);
  4865. dpll = DPLL_VGA_MODE_DIS;
  4866. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
  4867. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4868. } else {
  4869. if (clock->p1 == 2)
  4870. dpll |= PLL_P1_DIVIDE_BY_TWO;
  4871. else
  4872. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4873. if (clock->p2 == 4)
  4874. dpll |= PLL_P2_DIVIDE_BY_4;
  4875. }
  4876. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
  4877. dpll |= DPLL_DVO_2X_MODE;
  4878. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4879. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4880. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4881. else
  4882. dpll |= PLL_REF_INPUT_DREFCLK;
  4883. dpll |= DPLL_VCO_ENABLE;
  4884. crtc->config.dpll_hw_state.dpll = dpll;
  4885. }
  4886. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  4887. {
  4888. struct drm_device *dev = intel_crtc->base.dev;
  4889. struct drm_i915_private *dev_priv = dev->dev_private;
  4890. enum pipe pipe = intel_crtc->pipe;
  4891. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  4892. struct drm_display_mode *adjusted_mode =
  4893. &intel_crtc->config.adjusted_mode;
  4894. uint32_t crtc_vtotal, crtc_vblank_end;
  4895. int vsyncshift = 0;
  4896. /* We need to be careful not to changed the adjusted mode, for otherwise
  4897. * the hw state checker will get angry at the mismatch. */
  4898. crtc_vtotal = adjusted_mode->crtc_vtotal;
  4899. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  4900. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4901. /* the chip adds 2 halflines automatically */
  4902. crtc_vtotal -= 1;
  4903. crtc_vblank_end -= 1;
  4904. if (intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
  4905. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  4906. else
  4907. vsyncshift = adjusted_mode->crtc_hsync_start -
  4908. adjusted_mode->crtc_htotal / 2;
  4909. if (vsyncshift < 0)
  4910. vsyncshift += adjusted_mode->crtc_htotal;
  4911. }
  4912. if (INTEL_INFO(dev)->gen > 3)
  4913. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  4914. I915_WRITE(HTOTAL(cpu_transcoder),
  4915. (adjusted_mode->crtc_hdisplay - 1) |
  4916. ((adjusted_mode->crtc_htotal - 1) << 16));
  4917. I915_WRITE(HBLANK(cpu_transcoder),
  4918. (adjusted_mode->crtc_hblank_start - 1) |
  4919. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  4920. I915_WRITE(HSYNC(cpu_transcoder),
  4921. (adjusted_mode->crtc_hsync_start - 1) |
  4922. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  4923. I915_WRITE(VTOTAL(cpu_transcoder),
  4924. (adjusted_mode->crtc_vdisplay - 1) |
  4925. ((crtc_vtotal - 1) << 16));
  4926. I915_WRITE(VBLANK(cpu_transcoder),
  4927. (adjusted_mode->crtc_vblank_start - 1) |
  4928. ((crtc_vblank_end - 1) << 16));
  4929. I915_WRITE(VSYNC(cpu_transcoder),
  4930. (adjusted_mode->crtc_vsync_start - 1) |
  4931. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  4932. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  4933. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  4934. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  4935. * bits. */
  4936. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  4937. (pipe == PIPE_B || pipe == PIPE_C))
  4938. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  4939. /* pipesrc controls the size that is scaled from, which should
  4940. * always be the user's requested size.
  4941. */
  4942. I915_WRITE(PIPESRC(pipe),
  4943. ((intel_crtc->config.pipe_src_w - 1) << 16) |
  4944. (intel_crtc->config.pipe_src_h - 1));
  4945. }
  4946. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  4947. struct intel_crtc_config *pipe_config)
  4948. {
  4949. struct drm_device *dev = crtc->base.dev;
  4950. struct drm_i915_private *dev_priv = dev->dev_private;
  4951. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  4952. uint32_t tmp;
  4953. tmp = I915_READ(HTOTAL(cpu_transcoder));
  4954. pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  4955. pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  4956. tmp = I915_READ(HBLANK(cpu_transcoder));
  4957. pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  4958. pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  4959. tmp = I915_READ(HSYNC(cpu_transcoder));
  4960. pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  4961. pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  4962. tmp = I915_READ(VTOTAL(cpu_transcoder));
  4963. pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  4964. pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  4965. tmp = I915_READ(VBLANK(cpu_transcoder));
  4966. pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  4967. pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  4968. tmp = I915_READ(VSYNC(cpu_transcoder));
  4969. pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  4970. pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  4971. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  4972. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  4973. pipe_config->adjusted_mode.crtc_vtotal += 1;
  4974. pipe_config->adjusted_mode.crtc_vblank_end += 1;
  4975. }
  4976. tmp = I915_READ(PIPESRC(crtc->pipe));
  4977. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  4978. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  4979. pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
  4980. pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
  4981. }
  4982. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  4983. struct intel_crtc_config *pipe_config)
  4984. {
  4985. mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
  4986. mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
  4987. mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
  4988. mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
  4989. mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
  4990. mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
  4991. mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
  4992. mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
  4993. mode->flags = pipe_config->adjusted_mode.flags;
  4994. mode->clock = pipe_config->adjusted_mode.crtc_clock;
  4995. mode->flags |= pipe_config->adjusted_mode.flags;
  4996. }
  4997. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  4998. {
  4999. struct drm_device *dev = intel_crtc->base.dev;
  5000. struct drm_i915_private *dev_priv = dev->dev_private;
  5001. uint32_t pipeconf;
  5002. pipeconf = 0;
  5003. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  5004. I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
  5005. pipeconf |= PIPECONF_ENABLE;
  5006. if (intel_crtc->config.double_wide)
  5007. pipeconf |= PIPECONF_DOUBLE_WIDE;
  5008. /* only g4x and later have fancy bpc/dither controls */
  5009. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5010. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  5011. if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
  5012. pipeconf |= PIPECONF_DITHER_EN |
  5013. PIPECONF_DITHER_TYPE_SP;
  5014. switch (intel_crtc->config.pipe_bpp) {
  5015. case 18:
  5016. pipeconf |= PIPECONF_6BPC;
  5017. break;
  5018. case 24:
  5019. pipeconf |= PIPECONF_8BPC;
  5020. break;
  5021. case 30:
  5022. pipeconf |= PIPECONF_10BPC;
  5023. break;
  5024. default:
  5025. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5026. BUG();
  5027. }
  5028. }
  5029. if (HAS_PIPE_CXSR(dev)) {
  5030. if (intel_crtc->lowfreq_avail) {
  5031. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  5032. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  5033. } else {
  5034. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  5035. }
  5036. }
  5037. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  5038. if (INTEL_INFO(dev)->gen < 4 ||
  5039. intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
  5040. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  5041. else
  5042. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  5043. } else
  5044. pipeconf |= PIPECONF_PROGRESSIVE;
  5045. if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
  5046. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  5047. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  5048. POSTING_READ(PIPECONF(intel_crtc->pipe));
  5049. }
  5050. static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
  5051. int x, int y,
  5052. struct drm_framebuffer *fb)
  5053. {
  5054. struct drm_device *dev = crtc->dev;
  5055. struct drm_i915_private *dev_priv = dev->dev_private;
  5056. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5057. int refclk, num_connectors = 0;
  5058. intel_clock_t clock, reduced_clock;
  5059. bool ok, has_reduced_clock = false;
  5060. bool is_lvds = false, is_dsi = false;
  5061. struct intel_encoder *encoder;
  5062. const intel_limit_t *limit;
  5063. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5064. switch (encoder->type) {
  5065. case INTEL_OUTPUT_LVDS:
  5066. is_lvds = true;
  5067. break;
  5068. case INTEL_OUTPUT_DSI:
  5069. is_dsi = true;
  5070. break;
  5071. }
  5072. num_connectors++;
  5073. }
  5074. if (is_dsi)
  5075. return 0;
  5076. if (!intel_crtc->config.clock_set) {
  5077. refclk = i9xx_get_refclk(crtc, num_connectors);
  5078. /*
  5079. * Returns a set of divisors for the desired target clock with
  5080. * the given refclk, or FALSE. The returned values represent
  5081. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  5082. * 2) / p1 / p2.
  5083. */
  5084. limit = intel_limit(crtc, refclk);
  5085. ok = dev_priv->display.find_dpll(limit, crtc,
  5086. intel_crtc->config.port_clock,
  5087. refclk, NULL, &clock);
  5088. if (!ok) {
  5089. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5090. return -EINVAL;
  5091. }
  5092. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5093. /*
  5094. * Ensure we match the reduced clock's P to the target
  5095. * clock. If the clocks don't match, we can't switch
  5096. * the display clock by using the FP0/FP1. In such case
  5097. * we will disable the LVDS downclock feature.
  5098. */
  5099. has_reduced_clock =
  5100. dev_priv->display.find_dpll(limit, crtc,
  5101. dev_priv->lvds_downclock,
  5102. refclk, &clock,
  5103. &reduced_clock);
  5104. }
  5105. /* Compat-code for transition, will disappear. */
  5106. intel_crtc->config.dpll.n = clock.n;
  5107. intel_crtc->config.dpll.m1 = clock.m1;
  5108. intel_crtc->config.dpll.m2 = clock.m2;
  5109. intel_crtc->config.dpll.p1 = clock.p1;
  5110. intel_crtc->config.dpll.p2 = clock.p2;
  5111. }
  5112. if (IS_GEN2(dev)) {
  5113. i8xx_update_pll(intel_crtc,
  5114. has_reduced_clock ? &reduced_clock : NULL,
  5115. num_connectors);
  5116. } else if (IS_CHERRYVIEW(dev)) {
  5117. chv_update_pll(intel_crtc);
  5118. } else if (IS_VALLEYVIEW(dev)) {
  5119. vlv_update_pll(intel_crtc);
  5120. } else {
  5121. i9xx_update_pll(intel_crtc,
  5122. has_reduced_clock ? &reduced_clock : NULL,
  5123. num_connectors);
  5124. }
  5125. return 0;
  5126. }
  5127. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  5128. struct intel_crtc_config *pipe_config)
  5129. {
  5130. struct drm_device *dev = crtc->base.dev;
  5131. struct drm_i915_private *dev_priv = dev->dev_private;
  5132. uint32_t tmp;
  5133. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  5134. return;
  5135. tmp = I915_READ(PFIT_CONTROL);
  5136. if (!(tmp & PFIT_ENABLE))
  5137. return;
  5138. /* Check whether the pfit is attached to our pipe. */
  5139. if (INTEL_INFO(dev)->gen < 4) {
  5140. if (crtc->pipe != PIPE_B)
  5141. return;
  5142. } else {
  5143. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  5144. return;
  5145. }
  5146. pipe_config->gmch_pfit.control = tmp;
  5147. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  5148. if (INTEL_INFO(dev)->gen < 5)
  5149. pipe_config->gmch_pfit.lvds_border_bits =
  5150. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  5151. }
  5152. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  5153. struct intel_crtc_config *pipe_config)
  5154. {
  5155. struct drm_device *dev = crtc->base.dev;
  5156. struct drm_i915_private *dev_priv = dev->dev_private;
  5157. int pipe = pipe_config->cpu_transcoder;
  5158. intel_clock_t clock;
  5159. u32 mdiv;
  5160. int refclk = 100000;
  5161. /* In case of MIPI DPLL will not even be used */
  5162. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  5163. return;
  5164. mutex_lock(&dev_priv->dpio_lock);
  5165. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  5166. mutex_unlock(&dev_priv->dpio_lock);
  5167. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  5168. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  5169. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  5170. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  5171. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  5172. vlv_clock(refclk, &clock);
  5173. /* clock.dot is the fast clock */
  5174. pipe_config->port_clock = clock.dot / 5;
  5175. }
  5176. static void i9xx_get_plane_config(struct intel_crtc *crtc,
  5177. struct intel_plane_config *plane_config)
  5178. {
  5179. struct drm_device *dev = crtc->base.dev;
  5180. struct drm_i915_private *dev_priv = dev->dev_private;
  5181. u32 val, base, offset;
  5182. int pipe = crtc->pipe, plane = crtc->plane;
  5183. int fourcc, pixel_format;
  5184. int aligned_height;
  5185. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  5186. if (!crtc->base.primary->fb) {
  5187. DRM_DEBUG_KMS("failed to alloc fb\n");
  5188. return;
  5189. }
  5190. val = I915_READ(DSPCNTR(plane));
  5191. if (INTEL_INFO(dev)->gen >= 4)
  5192. if (val & DISPPLANE_TILED)
  5193. plane_config->tiled = true;
  5194. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  5195. fourcc = intel_format_to_fourcc(pixel_format);
  5196. crtc->base.primary->fb->pixel_format = fourcc;
  5197. crtc->base.primary->fb->bits_per_pixel =
  5198. drm_format_plane_cpp(fourcc, 0) * 8;
  5199. if (INTEL_INFO(dev)->gen >= 4) {
  5200. if (plane_config->tiled)
  5201. offset = I915_READ(DSPTILEOFF(plane));
  5202. else
  5203. offset = I915_READ(DSPLINOFF(plane));
  5204. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  5205. } else {
  5206. base = I915_READ(DSPADDR(plane));
  5207. }
  5208. plane_config->base = base;
  5209. val = I915_READ(PIPESRC(pipe));
  5210. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  5211. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  5212. val = I915_READ(DSPSTRIDE(pipe));
  5213. crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
  5214. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  5215. plane_config->tiled);
  5216. plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
  5217. aligned_height);
  5218. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  5219. pipe, plane, crtc->base.primary->fb->width,
  5220. crtc->base.primary->fb->height,
  5221. crtc->base.primary->fb->bits_per_pixel, base,
  5222. crtc->base.primary->fb->pitches[0],
  5223. plane_config->size);
  5224. }
  5225. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  5226. struct intel_crtc_config *pipe_config)
  5227. {
  5228. struct drm_device *dev = crtc->base.dev;
  5229. struct drm_i915_private *dev_priv = dev->dev_private;
  5230. int pipe = pipe_config->cpu_transcoder;
  5231. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5232. intel_clock_t clock;
  5233. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  5234. int refclk = 100000;
  5235. mutex_lock(&dev_priv->dpio_lock);
  5236. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  5237. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  5238. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  5239. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  5240. mutex_unlock(&dev_priv->dpio_lock);
  5241. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  5242. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  5243. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  5244. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  5245. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  5246. chv_clock(refclk, &clock);
  5247. /* clock.dot is the fast clock */
  5248. pipe_config->port_clock = clock.dot / 5;
  5249. }
  5250. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  5251. struct intel_crtc_config *pipe_config)
  5252. {
  5253. struct drm_device *dev = crtc->base.dev;
  5254. struct drm_i915_private *dev_priv = dev->dev_private;
  5255. uint32_t tmp;
  5256. if (!intel_display_power_enabled(dev_priv,
  5257. POWER_DOMAIN_PIPE(crtc->pipe)))
  5258. return false;
  5259. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5260. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5261. tmp = I915_READ(PIPECONF(crtc->pipe));
  5262. if (!(tmp & PIPECONF_ENABLE))
  5263. return false;
  5264. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5265. switch (tmp & PIPECONF_BPC_MASK) {
  5266. case PIPECONF_6BPC:
  5267. pipe_config->pipe_bpp = 18;
  5268. break;
  5269. case PIPECONF_8BPC:
  5270. pipe_config->pipe_bpp = 24;
  5271. break;
  5272. case PIPECONF_10BPC:
  5273. pipe_config->pipe_bpp = 30;
  5274. break;
  5275. default:
  5276. break;
  5277. }
  5278. }
  5279. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  5280. pipe_config->limited_color_range = true;
  5281. if (INTEL_INFO(dev)->gen < 4)
  5282. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  5283. intel_get_pipe_timings(crtc, pipe_config);
  5284. i9xx_get_pfit_config(crtc, pipe_config);
  5285. if (INTEL_INFO(dev)->gen >= 4) {
  5286. tmp = I915_READ(DPLL_MD(crtc->pipe));
  5287. pipe_config->pixel_multiplier =
  5288. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  5289. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  5290. pipe_config->dpll_hw_state.dpll_md = tmp;
  5291. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5292. tmp = I915_READ(DPLL(crtc->pipe));
  5293. pipe_config->pixel_multiplier =
  5294. ((tmp & SDVO_MULTIPLIER_MASK)
  5295. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  5296. } else {
  5297. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  5298. * port and will be fixed up in the encoder->get_config
  5299. * function. */
  5300. pipe_config->pixel_multiplier = 1;
  5301. }
  5302. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  5303. if (!IS_VALLEYVIEW(dev)) {
  5304. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  5305. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  5306. } else {
  5307. /* Mask out read-only status bits. */
  5308. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  5309. DPLL_PORTC_READY_MASK |
  5310. DPLL_PORTB_READY_MASK);
  5311. }
  5312. if (IS_CHERRYVIEW(dev))
  5313. chv_crtc_clock_get(crtc, pipe_config);
  5314. else if (IS_VALLEYVIEW(dev))
  5315. vlv_crtc_clock_get(crtc, pipe_config);
  5316. else
  5317. i9xx_crtc_clock_get(crtc, pipe_config);
  5318. return true;
  5319. }
  5320. static void ironlake_init_pch_refclk(struct drm_device *dev)
  5321. {
  5322. struct drm_i915_private *dev_priv = dev->dev_private;
  5323. struct drm_mode_config *mode_config = &dev->mode_config;
  5324. struct intel_encoder *encoder;
  5325. u32 val, final;
  5326. bool has_lvds = false;
  5327. bool has_cpu_edp = false;
  5328. bool has_panel = false;
  5329. bool has_ck505 = false;
  5330. bool can_ssc = false;
  5331. /* We need to take the global config into account */
  5332. list_for_each_entry(encoder, &mode_config->encoder_list,
  5333. base.head) {
  5334. switch (encoder->type) {
  5335. case INTEL_OUTPUT_LVDS:
  5336. has_panel = true;
  5337. has_lvds = true;
  5338. break;
  5339. case INTEL_OUTPUT_EDP:
  5340. has_panel = true;
  5341. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  5342. has_cpu_edp = true;
  5343. break;
  5344. }
  5345. }
  5346. if (HAS_PCH_IBX(dev)) {
  5347. has_ck505 = dev_priv->vbt.display_clock_mode;
  5348. can_ssc = has_ck505;
  5349. } else {
  5350. has_ck505 = false;
  5351. can_ssc = true;
  5352. }
  5353. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  5354. has_panel, has_lvds, has_ck505);
  5355. /* Ironlake: try to setup display ref clock before DPLL
  5356. * enabling. This is only under driver's control after
  5357. * PCH B stepping, previous chipset stepping should be
  5358. * ignoring this setting.
  5359. */
  5360. val = I915_READ(PCH_DREF_CONTROL);
  5361. /* As we must carefully and slowly disable/enable each source in turn,
  5362. * compute the final state we want first and check if we need to
  5363. * make any changes at all.
  5364. */
  5365. final = val;
  5366. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  5367. if (has_ck505)
  5368. final |= DREF_NONSPREAD_CK505_ENABLE;
  5369. else
  5370. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  5371. final &= ~DREF_SSC_SOURCE_MASK;
  5372. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5373. final &= ~DREF_SSC1_ENABLE;
  5374. if (has_panel) {
  5375. final |= DREF_SSC_SOURCE_ENABLE;
  5376. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5377. final |= DREF_SSC1_ENABLE;
  5378. if (has_cpu_edp) {
  5379. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5380. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5381. else
  5382. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5383. } else
  5384. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5385. } else {
  5386. final |= DREF_SSC_SOURCE_DISABLE;
  5387. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5388. }
  5389. if (final == val)
  5390. return;
  5391. /* Always enable nonspread source */
  5392. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  5393. if (has_ck505)
  5394. val |= DREF_NONSPREAD_CK505_ENABLE;
  5395. else
  5396. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  5397. if (has_panel) {
  5398. val &= ~DREF_SSC_SOURCE_MASK;
  5399. val |= DREF_SSC_SOURCE_ENABLE;
  5400. /* SSC must be turned on before enabling the CPU output */
  5401. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5402. DRM_DEBUG_KMS("Using SSC on panel\n");
  5403. val |= DREF_SSC1_ENABLE;
  5404. } else
  5405. val &= ~DREF_SSC1_ENABLE;
  5406. /* Get SSC going before enabling the outputs */
  5407. I915_WRITE(PCH_DREF_CONTROL, val);
  5408. POSTING_READ(PCH_DREF_CONTROL);
  5409. udelay(200);
  5410. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5411. /* Enable CPU source on CPU attached eDP */
  5412. if (has_cpu_edp) {
  5413. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5414. DRM_DEBUG_KMS("Using SSC on eDP\n");
  5415. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5416. } else
  5417. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5418. } else
  5419. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5420. I915_WRITE(PCH_DREF_CONTROL, val);
  5421. POSTING_READ(PCH_DREF_CONTROL);
  5422. udelay(200);
  5423. } else {
  5424. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  5425. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5426. /* Turn off CPU output */
  5427. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5428. I915_WRITE(PCH_DREF_CONTROL, val);
  5429. POSTING_READ(PCH_DREF_CONTROL);
  5430. udelay(200);
  5431. /* Turn off the SSC source */
  5432. val &= ~DREF_SSC_SOURCE_MASK;
  5433. val |= DREF_SSC_SOURCE_DISABLE;
  5434. /* Turn off SSC1 */
  5435. val &= ~DREF_SSC1_ENABLE;
  5436. I915_WRITE(PCH_DREF_CONTROL, val);
  5437. POSTING_READ(PCH_DREF_CONTROL);
  5438. udelay(200);
  5439. }
  5440. BUG_ON(val != final);
  5441. }
  5442. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  5443. {
  5444. uint32_t tmp;
  5445. tmp = I915_READ(SOUTH_CHICKEN2);
  5446. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  5447. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5448. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  5449. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  5450. DRM_ERROR("FDI mPHY reset assert timeout\n");
  5451. tmp = I915_READ(SOUTH_CHICKEN2);
  5452. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  5453. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5454. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  5455. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  5456. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  5457. }
  5458. /* WaMPhyProgramming:hsw */
  5459. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  5460. {
  5461. uint32_t tmp;
  5462. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  5463. tmp &= ~(0xFF << 24);
  5464. tmp |= (0x12 << 24);
  5465. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  5466. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  5467. tmp |= (1 << 11);
  5468. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  5469. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  5470. tmp |= (1 << 11);
  5471. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  5472. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  5473. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5474. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  5475. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  5476. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5477. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  5478. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  5479. tmp &= ~(7 << 13);
  5480. tmp |= (5 << 13);
  5481. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  5482. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  5483. tmp &= ~(7 << 13);
  5484. tmp |= (5 << 13);
  5485. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  5486. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  5487. tmp &= ~0xFF;
  5488. tmp |= 0x1C;
  5489. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  5490. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  5491. tmp &= ~0xFF;
  5492. tmp |= 0x1C;
  5493. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  5494. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  5495. tmp &= ~(0xFF << 16);
  5496. tmp |= (0x1C << 16);
  5497. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  5498. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  5499. tmp &= ~(0xFF << 16);
  5500. tmp |= (0x1C << 16);
  5501. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  5502. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  5503. tmp |= (1 << 27);
  5504. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  5505. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  5506. tmp |= (1 << 27);
  5507. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  5508. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  5509. tmp &= ~(0xF << 28);
  5510. tmp |= (4 << 28);
  5511. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  5512. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  5513. tmp &= ~(0xF << 28);
  5514. tmp |= (4 << 28);
  5515. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  5516. }
  5517. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  5518. * Programming" based on the parameters passed:
  5519. * - Sequence to enable CLKOUT_DP
  5520. * - Sequence to enable CLKOUT_DP without spread
  5521. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  5522. */
  5523. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  5524. bool with_fdi)
  5525. {
  5526. struct drm_i915_private *dev_priv = dev->dev_private;
  5527. uint32_t reg, tmp;
  5528. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  5529. with_spread = true;
  5530. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  5531. with_fdi, "LP PCH doesn't have FDI\n"))
  5532. with_fdi = false;
  5533. mutex_lock(&dev_priv->dpio_lock);
  5534. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5535. tmp &= ~SBI_SSCCTL_DISABLE;
  5536. tmp |= SBI_SSCCTL_PATHALT;
  5537. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5538. udelay(24);
  5539. if (with_spread) {
  5540. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5541. tmp &= ~SBI_SSCCTL_PATHALT;
  5542. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5543. if (with_fdi) {
  5544. lpt_reset_fdi_mphy(dev_priv);
  5545. lpt_program_fdi_mphy(dev_priv);
  5546. }
  5547. }
  5548. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5549. SBI_GEN0 : SBI_DBUFF0;
  5550. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5551. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5552. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5553. mutex_unlock(&dev_priv->dpio_lock);
  5554. }
  5555. /* Sequence to disable CLKOUT_DP */
  5556. static void lpt_disable_clkout_dp(struct drm_device *dev)
  5557. {
  5558. struct drm_i915_private *dev_priv = dev->dev_private;
  5559. uint32_t reg, tmp;
  5560. mutex_lock(&dev_priv->dpio_lock);
  5561. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5562. SBI_GEN0 : SBI_DBUFF0;
  5563. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5564. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5565. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5566. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5567. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  5568. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  5569. tmp |= SBI_SSCCTL_PATHALT;
  5570. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5571. udelay(32);
  5572. }
  5573. tmp |= SBI_SSCCTL_DISABLE;
  5574. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5575. }
  5576. mutex_unlock(&dev_priv->dpio_lock);
  5577. }
  5578. static void lpt_init_pch_refclk(struct drm_device *dev)
  5579. {
  5580. struct drm_mode_config *mode_config = &dev->mode_config;
  5581. struct intel_encoder *encoder;
  5582. bool has_vga = false;
  5583. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  5584. switch (encoder->type) {
  5585. case INTEL_OUTPUT_ANALOG:
  5586. has_vga = true;
  5587. break;
  5588. }
  5589. }
  5590. if (has_vga)
  5591. lpt_enable_clkout_dp(dev, true, true);
  5592. else
  5593. lpt_disable_clkout_dp(dev);
  5594. }
  5595. /*
  5596. * Initialize reference clocks when the driver loads
  5597. */
  5598. void intel_init_pch_refclk(struct drm_device *dev)
  5599. {
  5600. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  5601. ironlake_init_pch_refclk(dev);
  5602. else if (HAS_PCH_LPT(dev))
  5603. lpt_init_pch_refclk(dev);
  5604. }
  5605. static int ironlake_get_refclk(struct drm_crtc *crtc)
  5606. {
  5607. struct drm_device *dev = crtc->dev;
  5608. struct drm_i915_private *dev_priv = dev->dev_private;
  5609. struct intel_encoder *encoder;
  5610. int num_connectors = 0;
  5611. bool is_lvds = false;
  5612. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5613. switch (encoder->type) {
  5614. case INTEL_OUTPUT_LVDS:
  5615. is_lvds = true;
  5616. break;
  5617. }
  5618. num_connectors++;
  5619. }
  5620. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5621. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  5622. dev_priv->vbt.lvds_ssc_freq);
  5623. return dev_priv->vbt.lvds_ssc_freq;
  5624. }
  5625. return 120000;
  5626. }
  5627. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  5628. {
  5629. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  5630. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5631. int pipe = intel_crtc->pipe;
  5632. uint32_t val;
  5633. val = 0;
  5634. switch (intel_crtc->config.pipe_bpp) {
  5635. case 18:
  5636. val |= PIPECONF_6BPC;
  5637. break;
  5638. case 24:
  5639. val |= PIPECONF_8BPC;
  5640. break;
  5641. case 30:
  5642. val |= PIPECONF_10BPC;
  5643. break;
  5644. case 36:
  5645. val |= PIPECONF_12BPC;
  5646. break;
  5647. default:
  5648. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5649. BUG();
  5650. }
  5651. if (intel_crtc->config.dither)
  5652. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  5653. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  5654. val |= PIPECONF_INTERLACED_ILK;
  5655. else
  5656. val |= PIPECONF_PROGRESSIVE;
  5657. if (intel_crtc->config.limited_color_range)
  5658. val |= PIPECONF_COLOR_RANGE_SELECT;
  5659. I915_WRITE(PIPECONF(pipe), val);
  5660. POSTING_READ(PIPECONF(pipe));
  5661. }
  5662. /*
  5663. * Set up the pipe CSC unit.
  5664. *
  5665. * Currently only full range RGB to limited range RGB conversion
  5666. * is supported, but eventually this should handle various
  5667. * RGB<->YCbCr scenarios as well.
  5668. */
  5669. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  5670. {
  5671. struct drm_device *dev = crtc->dev;
  5672. struct drm_i915_private *dev_priv = dev->dev_private;
  5673. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5674. int pipe = intel_crtc->pipe;
  5675. uint16_t coeff = 0x7800; /* 1.0 */
  5676. /*
  5677. * TODO: Check what kind of values actually come out of the pipe
  5678. * with these coeff/postoff values and adjust to get the best
  5679. * accuracy. Perhaps we even need to take the bpc value into
  5680. * consideration.
  5681. */
  5682. if (intel_crtc->config.limited_color_range)
  5683. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  5684. /*
  5685. * GY/GU and RY/RU should be the other way around according
  5686. * to BSpec, but reality doesn't agree. Just set them up in
  5687. * a way that results in the correct picture.
  5688. */
  5689. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  5690. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  5691. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  5692. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  5693. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  5694. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  5695. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  5696. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  5697. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  5698. if (INTEL_INFO(dev)->gen > 6) {
  5699. uint16_t postoff = 0;
  5700. if (intel_crtc->config.limited_color_range)
  5701. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  5702. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  5703. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  5704. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  5705. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  5706. } else {
  5707. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  5708. if (intel_crtc->config.limited_color_range)
  5709. mode |= CSC_BLACK_SCREEN_OFFSET;
  5710. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  5711. }
  5712. }
  5713. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  5714. {
  5715. struct drm_device *dev = crtc->dev;
  5716. struct drm_i915_private *dev_priv = dev->dev_private;
  5717. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5718. enum pipe pipe = intel_crtc->pipe;
  5719. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  5720. uint32_t val;
  5721. val = 0;
  5722. if (IS_HASWELL(dev) && intel_crtc->config.dither)
  5723. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  5724. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  5725. val |= PIPECONF_INTERLACED_ILK;
  5726. else
  5727. val |= PIPECONF_PROGRESSIVE;
  5728. I915_WRITE(PIPECONF(cpu_transcoder), val);
  5729. POSTING_READ(PIPECONF(cpu_transcoder));
  5730. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  5731. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  5732. if (IS_BROADWELL(dev)) {
  5733. val = 0;
  5734. switch (intel_crtc->config.pipe_bpp) {
  5735. case 18:
  5736. val |= PIPEMISC_DITHER_6_BPC;
  5737. break;
  5738. case 24:
  5739. val |= PIPEMISC_DITHER_8_BPC;
  5740. break;
  5741. case 30:
  5742. val |= PIPEMISC_DITHER_10_BPC;
  5743. break;
  5744. case 36:
  5745. val |= PIPEMISC_DITHER_12_BPC;
  5746. break;
  5747. default:
  5748. /* Case prevented by pipe_config_set_bpp. */
  5749. BUG();
  5750. }
  5751. if (intel_crtc->config.dither)
  5752. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  5753. I915_WRITE(PIPEMISC(pipe), val);
  5754. }
  5755. }
  5756. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  5757. intel_clock_t *clock,
  5758. bool *has_reduced_clock,
  5759. intel_clock_t *reduced_clock)
  5760. {
  5761. struct drm_device *dev = crtc->dev;
  5762. struct drm_i915_private *dev_priv = dev->dev_private;
  5763. struct intel_encoder *intel_encoder;
  5764. int refclk;
  5765. const intel_limit_t *limit;
  5766. bool ret, is_lvds = false;
  5767. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  5768. switch (intel_encoder->type) {
  5769. case INTEL_OUTPUT_LVDS:
  5770. is_lvds = true;
  5771. break;
  5772. }
  5773. }
  5774. refclk = ironlake_get_refclk(crtc);
  5775. /*
  5776. * Returns a set of divisors for the desired target clock with the given
  5777. * refclk, or FALSE. The returned values represent the clock equation:
  5778. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  5779. */
  5780. limit = intel_limit(crtc, refclk);
  5781. ret = dev_priv->display.find_dpll(limit, crtc,
  5782. to_intel_crtc(crtc)->config.port_clock,
  5783. refclk, NULL, clock);
  5784. if (!ret)
  5785. return false;
  5786. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5787. /*
  5788. * Ensure we match the reduced clock's P to the target clock.
  5789. * If the clocks don't match, we can't switch the display clock
  5790. * by using the FP0/FP1. In such case we will disable the LVDS
  5791. * downclock feature.
  5792. */
  5793. *has_reduced_clock =
  5794. dev_priv->display.find_dpll(limit, crtc,
  5795. dev_priv->lvds_downclock,
  5796. refclk, clock,
  5797. reduced_clock);
  5798. }
  5799. return true;
  5800. }
  5801. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  5802. {
  5803. /*
  5804. * Account for spread spectrum to avoid
  5805. * oversubscribing the link. Max center spread
  5806. * is 2.5%; use 5% for safety's sake.
  5807. */
  5808. u32 bps = target_clock * bpp * 21 / 20;
  5809. return DIV_ROUND_UP(bps, link_bw * 8);
  5810. }
  5811. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  5812. {
  5813. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  5814. }
  5815. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  5816. u32 *fp,
  5817. intel_clock_t *reduced_clock, u32 *fp2)
  5818. {
  5819. struct drm_crtc *crtc = &intel_crtc->base;
  5820. struct drm_device *dev = crtc->dev;
  5821. struct drm_i915_private *dev_priv = dev->dev_private;
  5822. struct intel_encoder *intel_encoder;
  5823. uint32_t dpll;
  5824. int factor, num_connectors = 0;
  5825. bool is_lvds = false, is_sdvo = false;
  5826. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  5827. switch (intel_encoder->type) {
  5828. case INTEL_OUTPUT_LVDS:
  5829. is_lvds = true;
  5830. break;
  5831. case INTEL_OUTPUT_SDVO:
  5832. case INTEL_OUTPUT_HDMI:
  5833. is_sdvo = true;
  5834. break;
  5835. }
  5836. num_connectors++;
  5837. }
  5838. /* Enable autotuning of the PLL clock (if permissible) */
  5839. factor = 21;
  5840. if (is_lvds) {
  5841. if ((intel_panel_use_ssc(dev_priv) &&
  5842. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  5843. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  5844. factor = 25;
  5845. } else if (intel_crtc->config.sdvo_tv_clock)
  5846. factor = 20;
  5847. if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
  5848. *fp |= FP_CB_TUNE;
  5849. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  5850. *fp2 |= FP_CB_TUNE;
  5851. dpll = 0;
  5852. if (is_lvds)
  5853. dpll |= DPLLB_MODE_LVDS;
  5854. else
  5855. dpll |= DPLLB_MODE_DAC_SERIAL;
  5856. dpll |= (intel_crtc->config.pixel_multiplier - 1)
  5857. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  5858. if (is_sdvo)
  5859. dpll |= DPLL_SDVO_HIGH_SPEED;
  5860. if (intel_crtc->config.has_dp_encoder)
  5861. dpll |= DPLL_SDVO_HIGH_SPEED;
  5862. /* compute bitmask from p1 value */
  5863. dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5864. /* also FPA1 */
  5865. dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5866. switch (intel_crtc->config.dpll.p2) {
  5867. case 5:
  5868. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5869. break;
  5870. case 7:
  5871. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5872. break;
  5873. case 10:
  5874. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5875. break;
  5876. case 14:
  5877. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5878. break;
  5879. }
  5880. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5881. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5882. else
  5883. dpll |= PLL_REF_INPUT_DREFCLK;
  5884. return dpll | DPLL_VCO_ENABLE;
  5885. }
  5886. static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
  5887. int x, int y,
  5888. struct drm_framebuffer *fb)
  5889. {
  5890. struct drm_device *dev = crtc->dev;
  5891. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5892. int num_connectors = 0;
  5893. intel_clock_t clock, reduced_clock;
  5894. u32 dpll = 0, fp = 0, fp2 = 0;
  5895. bool ok, has_reduced_clock = false;
  5896. bool is_lvds = false;
  5897. struct intel_encoder *encoder;
  5898. struct intel_shared_dpll *pll;
  5899. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5900. switch (encoder->type) {
  5901. case INTEL_OUTPUT_LVDS:
  5902. is_lvds = true;
  5903. break;
  5904. }
  5905. num_connectors++;
  5906. }
  5907. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  5908. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  5909. ok = ironlake_compute_clocks(crtc, &clock,
  5910. &has_reduced_clock, &reduced_clock);
  5911. if (!ok && !intel_crtc->config.clock_set) {
  5912. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5913. return -EINVAL;
  5914. }
  5915. /* Compat-code for transition, will disappear. */
  5916. if (!intel_crtc->config.clock_set) {
  5917. intel_crtc->config.dpll.n = clock.n;
  5918. intel_crtc->config.dpll.m1 = clock.m1;
  5919. intel_crtc->config.dpll.m2 = clock.m2;
  5920. intel_crtc->config.dpll.p1 = clock.p1;
  5921. intel_crtc->config.dpll.p2 = clock.p2;
  5922. }
  5923. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  5924. if (intel_crtc->config.has_pch_encoder) {
  5925. fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
  5926. if (has_reduced_clock)
  5927. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  5928. dpll = ironlake_compute_dpll(intel_crtc,
  5929. &fp, &reduced_clock,
  5930. has_reduced_clock ? &fp2 : NULL);
  5931. intel_crtc->config.dpll_hw_state.dpll = dpll;
  5932. intel_crtc->config.dpll_hw_state.fp0 = fp;
  5933. if (has_reduced_clock)
  5934. intel_crtc->config.dpll_hw_state.fp1 = fp2;
  5935. else
  5936. intel_crtc->config.dpll_hw_state.fp1 = fp;
  5937. pll = intel_get_shared_dpll(intel_crtc);
  5938. if (pll == NULL) {
  5939. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  5940. pipe_name(intel_crtc->pipe));
  5941. return -EINVAL;
  5942. }
  5943. } else
  5944. intel_put_shared_dpll(intel_crtc);
  5945. if (is_lvds && has_reduced_clock && i915.powersave)
  5946. intel_crtc->lowfreq_avail = true;
  5947. else
  5948. intel_crtc->lowfreq_avail = false;
  5949. return 0;
  5950. }
  5951. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  5952. struct intel_link_m_n *m_n)
  5953. {
  5954. struct drm_device *dev = crtc->base.dev;
  5955. struct drm_i915_private *dev_priv = dev->dev_private;
  5956. enum pipe pipe = crtc->pipe;
  5957. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  5958. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  5959. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  5960. & ~TU_SIZE_MASK;
  5961. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  5962. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  5963. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  5964. }
  5965. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  5966. enum transcoder transcoder,
  5967. struct intel_link_m_n *m_n,
  5968. struct intel_link_m_n *m2_n2)
  5969. {
  5970. struct drm_device *dev = crtc->base.dev;
  5971. struct drm_i915_private *dev_priv = dev->dev_private;
  5972. enum pipe pipe = crtc->pipe;
  5973. if (INTEL_INFO(dev)->gen >= 5) {
  5974. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  5975. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  5976. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  5977. & ~TU_SIZE_MASK;
  5978. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  5979. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  5980. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  5981. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  5982. * gen < 8) and if DRRS is supported (to make sure the
  5983. * registers are not unnecessarily read).
  5984. */
  5985. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  5986. crtc->config.has_drrs) {
  5987. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  5988. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  5989. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  5990. & ~TU_SIZE_MASK;
  5991. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  5992. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  5993. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  5994. }
  5995. } else {
  5996. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  5997. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  5998. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  5999. & ~TU_SIZE_MASK;
  6000. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  6001. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  6002. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6003. }
  6004. }
  6005. void intel_dp_get_m_n(struct intel_crtc *crtc,
  6006. struct intel_crtc_config *pipe_config)
  6007. {
  6008. if (crtc->config.has_pch_encoder)
  6009. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  6010. else
  6011. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6012. &pipe_config->dp_m_n,
  6013. &pipe_config->dp_m2_n2);
  6014. }
  6015. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  6016. struct intel_crtc_config *pipe_config)
  6017. {
  6018. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6019. &pipe_config->fdi_m_n, NULL);
  6020. }
  6021. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  6022. struct intel_crtc_config *pipe_config)
  6023. {
  6024. struct drm_device *dev = crtc->base.dev;
  6025. struct drm_i915_private *dev_priv = dev->dev_private;
  6026. uint32_t tmp;
  6027. tmp = I915_READ(PF_CTL(crtc->pipe));
  6028. if (tmp & PF_ENABLE) {
  6029. pipe_config->pch_pfit.enabled = true;
  6030. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  6031. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  6032. /* We currently do not free assignements of panel fitters on
  6033. * ivb/hsw (since we don't use the higher upscaling modes which
  6034. * differentiates them) so just WARN about this case for now. */
  6035. if (IS_GEN7(dev)) {
  6036. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  6037. PF_PIPE_SEL_IVB(crtc->pipe));
  6038. }
  6039. }
  6040. }
  6041. static void ironlake_get_plane_config(struct intel_crtc *crtc,
  6042. struct intel_plane_config *plane_config)
  6043. {
  6044. struct drm_device *dev = crtc->base.dev;
  6045. struct drm_i915_private *dev_priv = dev->dev_private;
  6046. u32 val, base, offset;
  6047. int pipe = crtc->pipe, plane = crtc->plane;
  6048. int fourcc, pixel_format;
  6049. int aligned_height;
  6050. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  6051. if (!crtc->base.primary->fb) {
  6052. DRM_DEBUG_KMS("failed to alloc fb\n");
  6053. return;
  6054. }
  6055. val = I915_READ(DSPCNTR(plane));
  6056. if (INTEL_INFO(dev)->gen >= 4)
  6057. if (val & DISPPLANE_TILED)
  6058. plane_config->tiled = true;
  6059. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6060. fourcc = intel_format_to_fourcc(pixel_format);
  6061. crtc->base.primary->fb->pixel_format = fourcc;
  6062. crtc->base.primary->fb->bits_per_pixel =
  6063. drm_format_plane_cpp(fourcc, 0) * 8;
  6064. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6065. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  6066. offset = I915_READ(DSPOFFSET(plane));
  6067. } else {
  6068. if (plane_config->tiled)
  6069. offset = I915_READ(DSPTILEOFF(plane));
  6070. else
  6071. offset = I915_READ(DSPLINOFF(plane));
  6072. }
  6073. plane_config->base = base;
  6074. val = I915_READ(PIPESRC(pipe));
  6075. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  6076. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  6077. val = I915_READ(DSPSTRIDE(pipe));
  6078. crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
  6079. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  6080. plane_config->tiled);
  6081. plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
  6082. aligned_height);
  6083. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6084. pipe, plane, crtc->base.primary->fb->width,
  6085. crtc->base.primary->fb->height,
  6086. crtc->base.primary->fb->bits_per_pixel, base,
  6087. crtc->base.primary->fb->pitches[0],
  6088. plane_config->size);
  6089. }
  6090. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  6091. struct intel_crtc_config *pipe_config)
  6092. {
  6093. struct drm_device *dev = crtc->base.dev;
  6094. struct drm_i915_private *dev_priv = dev->dev_private;
  6095. uint32_t tmp;
  6096. if (!intel_display_power_enabled(dev_priv,
  6097. POWER_DOMAIN_PIPE(crtc->pipe)))
  6098. return false;
  6099. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6100. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6101. tmp = I915_READ(PIPECONF(crtc->pipe));
  6102. if (!(tmp & PIPECONF_ENABLE))
  6103. return false;
  6104. switch (tmp & PIPECONF_BPC_MASK) {
  6105. case PIPECONF_6BPC:
  6106. pipe_config->pipe_bpp = 18;
  6107. break;
  6108. case PIPECONF_8BPC:
  6109. pipe_config->pipe_bpp = 24;
  6110. break;
  6111. case PIPECONF_10BPC:
  6112. pipe_config->pipe_bpp = 30;
  6113. break;
  6114. case PIPECONF_12BPC:
  6115. pipe_config->pipe_bpp = 36;
  6116. break;
  6117. default:
  6118. break;
  6119. }
  6120. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  6121. pipe_config->limited_color_range = true;
  6122. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  6123. struct intel_shared_dpll *pll;
  6124. pipe_config->has_pch_encoder = true;
  6125. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  6126. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6127. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6128. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6129. if (HAS_PCH_IBX(dev_priv->dev)) {
  6130. pipe_config->shared_dpll =
  6131. (enum intel_dpll_id) crtc->pipe;
  6132. } else {
  6133. tmp = I915_READ(PCH_DPLL_SEL);
  6134. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  6135. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  6136. else
  6137. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  6138. }
  6139. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6140. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6141. &pipe_config->dpll_hw_state));
  6142. tmp = pipe_config->dpll_hw_state.dpll;
  6143. pipe_config->pixel_multiplier =
  6144. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  6145. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  6146. ironlake_pch_clock_get(crtc, pipe_config);
  6147. } else {
  6148. pipe_config->pixel_multiplier = 1;
  6149. }
  6150. intel_get_pipe_timings(crtc, pipe_config);
  6151. ironlake_get_pfit_config(crtc, pipe_config);
  6152. return true;
  6153. }
  6154. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  6155. {
  6156. struct drm_device *dev = dev_priv->dev;
  6157. struct intel_crtc *crtc;
  6158. for_each_intel_crtc(dev, crtc)
  6159. WARN(crtc->active, "CRTC for pipe %c enabled\n",
  6160. pipe_name(crtc->pipe));
  6161. WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  6162. WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  6163. WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  6164. WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  6165. WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  6166. WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  6167. "CPU PWM1 enabled\n");
  6168. if (IS_HASWELL(dev))
  6169. WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  6170. "CPU PWM2 enabled\n");
  6171. WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  6172. "PCH PWM1 enabled\n");
  6173. WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  6174. "Utility pin enabled\n");
  6175. WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  6176. /*
  6177. * In theory we can still leave IRQs enabled, as long as only the HPD
  6178. * interrupts remain enabled. We used to check for that, but since it's
  6179. * gen-specific and since we only disable LCPLL after we fully disable
  6180. * the interrupts, the check below should be enough.
  6181. */
  6182. WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  6183. }
  6184. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  6185. {
  6186. struct drm_device *dev = dev_priv->dev;
  6187. if (IS_HASWELL(dev))
  6188. return I915_READ(D_COMP_HSW);
  6189. else
  6190. return I915_READ(D_COMP_BDW);
  6191. }
  6192. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  6193. {
  6194. struct drm_device *dev = dev_priv->dev;
  6195. if (IS_HASWELL(dev)) {
  6196. mutex_lock(&dev_priv->rps.hw_lock);
  6197. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  6198. val))
  6199. DRM_ERROR("Failed to write to D_COMP\n");
  6200. mutex_unlock(&dev_priv->rps.hw_lock);
  6201. } else {
  6202. I915_WRITE(D_COMP_BDW, val);
  6203. POSTING_READ(D_COMP_BDW);
  6204. }
  6205. }
  6206. /*
  6207. * This function implements pieces of two sequences from BSpec:
  6208. * - Sequence for display software to disable LCPLL
  6209. * - Sequence for display software to allow package C8+
  6210. * The steps implemented here are just the steps that actually touch the LCPLL
  6211. * register. Callers should take care of disabling all the display engine
  6212. * functions, doing the mode unset, fixing interrupts, etc.
  6213. */
  6214. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  6215. bool switch_to_fclk, bool allow_power_down)
  6216. {
  6217. uint32_t val;
  6218. assert_can_disable_lcpll(dev_priv);
  6219. val = I915_READ(LCPLL_CTL);
  6220. if (switch_to_fclk) {
  6221. val |= LCPLL_CD_SOURCE_FCLK;
  6222. I915_WRITE(LCPLL_CTL, val);
  6223. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  6224. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  6225. DRM_ERROR("Switching to FCLK failed\n");
  6226. val = I915_READ(LCPLL_CTL);
  6227. }
  6228. val |= LCPLL_PLL_DISABLE;
  6229. I915_WRITE(LCPLL_CTL, val);
  6230. POSTING_READ(LCPLL_CTL);
  6231. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  6232. DRM_ERROR("LCPLL still locked\n");
  6233. val = hsw_read_dcomp(dev_priv);
  6234. val |= D_COMP_COMP_DISABLE;
  6235. hsw_write_dcomp(dev_priv, val);
  6236. ndelay(100);
  6237. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  6238. 1))
  6239. DRM_ERROR("D_COMP RCOMP still in progress\n");
  6240. if (allow_power_down) {
  6241. val = I915_READ(LCPLL_CTL);
  6242. val |= LCPLL_POWER_DOWN_ALLOW;
  6243. I915_WRITE(LCPLL_CTL, val);
  6244. POSTING_READ(LCPLL_CTL);
  6245. }
  6246. }
  6247. /*
  6248. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  6249. * source.
  6250. */
  6251. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  6252. {
  6253. uint32_t val;
  6254. unsigned long irqflags;
  6255. val = I915_READ(LCPLL_CTL);
  6256. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  6257. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  6258. return;
  6259. /*
  6260. * Make sure we're not on PC8 state before disabling PC8, otherwise
  6261. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  6262. *
  6263. * The other problem is that hsw_restore_lcpll() is called as part of
  6264. * the runtime PM resume sequence, so we can't just call
  6265. * gen6_gt_force_wake_get() because that function calls
  6266. * intel_runtime_pm_get(), and we can't change the runtime PM refcount
  6267. * while we are on the resume sequence. So to solve this problem we have
  6268. * to call special forcewake code that doesn't touch runtime PM and
  6269. * doesn't enable the forcewake delayed work.
  6270. */
  6271. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  6272. if (dev_priv->uncore.forcewake_count++ == 0)
  6273. dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
  6274. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  6275. if (val & LCPLL_POWER_DOWN_ALLOW) {
  6276. val &= ~LCPLL_POWER_DOWN_ALLOW;
  6277. I915_WRITE(LCPLL_CTL, val);
  6278. POSTING_READ(LCPLL_CTL);
  6279. }
  6280. val = hsw_read_dcomp(dev_priv);
  6281. val |= D_COMP_COMP_FORCE;
  6282. val &= ~D_COMP_COMP_DISABLE;
  6283. hsw_write_dcomp(dev_priv, val);
  6284. val = I915_READ(LCPLL_CTL);
  6285. val &= ~LCPLL_PLL_DISABLE;
  6286. I915_WRITE(LCPLL_CTL, val);
  6287. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  6288. DRM_ERROR("LCPLL not locked yet\n");
  6289. if (val & LCPLL_CD_SOURCE_FCLK) {
  6290. val = I915_READ(LCPLL_CTL);
  6291. val &= ~LCPLL_CD_SOURCE_FCLK;
  6292. I915_WRITE(LCPLL_CTL, val);
  6293. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  6294. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  6295. DRM_ERROR("Switching back to LCPLL failed\n");
  6296. }
  6297. /* See the big comment above. */
  6298. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  6299. if (--dev_priv->uncore.forcewake_count == 0)
  6300. dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
  6301. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  6302. }
  6303. /*
  6304. * Package states C8 and deeper are really deep PC states that can only be
  6305. * reached when all the devices on the system allow it, so even if the graphics
  6306. * device allows PC8+, it doesn't mean the system will actually get to these
  6307. * states. Our driver only allows PC8+ when going into runtime PM.
  6308. *
  6309. * The requirements for PC8+ are that all the outputs are disabled, the power
  6310. * well is disabled and most interrupts are disabled, and these are also
  6311. * requirements for runtime PM. When these conditions are met, we manually do
  6312. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  6313. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  6314. * hang the machine.
  6315. *
  6316. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  6317. * the state of some registers, so when we come back from PC8+ we need to
  6318. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  6319. * need to take care of the registers kept by RC6. Notice that this happens even
  6320. * if we don't put the device in PCI D3 state (which is what currently happens
  6321. * because of the runtime PM support).
  6322. *
  6323. * For more, read "Display Sequences for Package C8" on the hardware
  6324. * documentation.
  6325. */
  6326. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  6327. {
  6328. struct drm_device *dev = dev_priv->dev;
  6329. uint32_t val;
  6330. DRM_DEBUG_KMS("Enabling package C8+\n");
  6331. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6332. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6333. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  6334. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6335. }
  6336. lpt_disable_clkout_dp(dev);
  6337. hsw_disable_lcpll(dev_priv, true, true);
  6338. }
  6339. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  6340. {
  6341. struct drm_device *dev = dev_priv->dev;
  6342. uint32_t val;
  6343. DRM_DEBUG_KMS("Disabling package C8+\n");
  6344. hsw_restore_lcpll(dev_priv);
  6345. lpt_init_pch_refclk(dev);
  6346. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6347. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6348. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  6349. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6350. }
  6351. intel_prepare_ddi(dev);
  6352. }
  6353. static void snb_modeset_global_resources(struct drm_device *dev)
  6354. {
  6355. modeset_update_crtc_power_domains(dev);
  6356. }
  6357. static void haswell_modeset_global_resources(struct drm_device *dev)
  6358. {
  6359. modeset_update_crtc_power_domains(dev);
  6360. }
  6361. static int haswell_crtc_mode_set(struct drm_crtc *crtc,
  6362. int x, int y,
  6363. struct drm_framebuffer *fb)
  6364. {
  6365. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6366. if (!intel_ddi_pll_select(intel_crtc))
  6367. return -EINVAL;
  6368. intel_crtc->lowfreq_avail = false;
  6369. return 0;
  6370. }
  6371. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  6372. struct intel_crtc_config *pipe_config)
  6373. {
  6374. struct drm_device *dev = crtc->base.dev;
  6375. struct drm_i915_private *dev_priv = dev->dev_private;
  6376. struct intel_shared_dpll *pll;
  6377. enum port port;
  6378. uint32_t tmp;
  6379. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  6380. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  6381. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  6382. switch (pipe_config->ddi_pll_sel) {
  6383. case PORT_CLK_SEL_WRPLL1:
  6384. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  6385. break;
  6386. case PORT_CLK_SEL_WRPLL2:
  6387. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  6388. break;
  6389. }
  6390. if (pipe_config->shared_dpll >= 0) {
  6391. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6392. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6393. &pipe_config->dpll_hw_state));
  6394. }
  6395. /*
  6396. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  6397. * DDI E. So just check whether this pipe is wired to DDI E and whether
  6398. * the PCH transcoder is on.
  6399. */
  6400. if ((port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  6401. pipe_config->has_pch_encoder = true;
  6402. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  6403. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6404. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6405. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6406. }
  6407. }
  6408. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  6409. struct intel_crtc_config *pipe_config)
  6410. {
  6411. struct drm_device *dev = crtc->base.dev;
  6412. struct drm_i915_private *dev_priv = dev->dev_private;
  6413. enum intel_display_power_domain pfit_domain;
  6414. uint32_t tmp;
  6415. if (!intel_display_power_enabled(dev_priv,
  6416. POWER_DOMAIN_PIPE(crtc->pipe)))
  6417. return false;
  6418. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6419. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6420. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  6421. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  6422. enum pipe trans_edp_pipe;
  6423. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  6424. default:
  6425. WARN(1, "unknown pipe linked to edp transcoder\n");
  6426. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  6427. case TRANS_DDI_EDP_INPUT_A_ON:
  6428. trans_edp_pipe = PIPE_A;
  6429. break;
  6430. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  6431. trans_edp_pipe = PIPE_B;
  6432. break;
  6433. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  6434. trans_edp_pipe = PIPE_C;
  6435. break;
  6436. }
  6437. if (trans_edp_pipe == crtc->pipe)
  6438. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  6439. }
  6440. if (!intel_display_power_enabled(dev_priv,
  6441. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  6442. return false;
  6443. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  6444. if (!(tmp & PIPECONF_ENABLE))
  6445. return false;
  6446. haswell_get_ddi_port_state(crtc, pipe_config);
  6447. intel_get_pipe_timings(crtc, pipe_config);
  6448. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  6449. if (intel_display_power_enabled(dev_priv, pfit_domain))
  6450. ironlake_get_pfit_config(crtc, pipe_config);
  6451. if (IS_HASWELL(dev))
  6452. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  6453. (I915_READ(IPS_CTL) & IPS_ENABLE);
  6454. pipe_config->pixel_multiplier = 1;
  6455. return true;
  6456. }
  6457. static struct {
  6458. int clock;
  6459. u32 config;
  6460. } hdmi_audio_clock[] = {
  6461. { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
  6462. { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
  6463. { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
  6464. { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
  6465. { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
  6466. { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
  6467. { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
  6468. { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
  6469. { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
  6470. { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
  6471. };
  6472. /* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
  6473. static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
  6474. {
  6475. int i;
  6476. for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
  6477. if (mode->clock == hdmi_audio_clock[i].clock)
  6478. break;
  6479. }
  6480. if (i == ARRAY_SIZE(hdmi_audio_clock)) {
  6481. DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
  6482. i = 1;
  6483. }
  6484. DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
  6485. hdmi_audio_clock[i].clock,
  6486. hdmi_audio_clock[i].config);
  6487. return hdmi_audio_clock[i].config;
  6488. }
  6489. static bool intel_eld_uptodate(struct drm_connector *connector,
  6490. int reg_eldv, uint32_t bits_eldv,
  6491. int reg_elda, uint32_t bits_elda,
  6492. int reg_edid)
  6493. {
  6494. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6495. uint8_t *eld = connector->eld;
  6496. uint32_t i;
  6497. i = I915_READ(reg_eldv);
  6498. i &= bits_eldv;
  6499. if (!eld[0])
  6500. return !i;
  6501. if (!i)
  6502. return false;
  6503. i = I915_READ(reg_elda);
  6504. i &= ~bits_elda;
  6505. I915_WRITE(reg_elda, i);
  6506. for (i = 0; i < eld[2]; i++)
  6507. if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
  6508. return false;
  6509. return true;
  6510. }
  6511. static void g4x_write_eld(struct drm_connector *connector,
  6512. struct drm_crtc *crtc,
  6513. struct drm_display_mode *mode)
  6514. {
  6515. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6516. uint8_t *eld = connector->eld;
  6517. uint32_t eldv;
  6518. uint32_t len;
  6519. uint32_t i;
  6520. i = I915_READ(G4X_AUD_VID_DID);
  6521. if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
  6522. eldv = G4X_ELDV_DEVCL_DEVBLC;
  6523. else
  6524. eldv = G4X_ELDV_DEVCTG;
  6525. if (intel_eld_uptodate(connector,
  6526. G4X_AUD_CNTL_ST, eldv,
  6527. G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
  6528. G4X_HDMIW_HDMIEDID))
  6529. return;
  6530. i = I915_READ(G4X_AUD_CNTL_ST);
  6531. i &= ~(eldv | G4X_ELD_ADDR);
  6532. len = (i >> 9) & 0x1f; /* ELD buffer size */
  6533. I915_WRITE(G4X_AUD_CNTL_ST, i);
  6534. if (!eld[0])
  6535. return;
  6536. len = min_t(uint8_t, eld[2], len);
  6537. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6538. for (i = 0; i < len; i++)
  6539. I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
  6540. i = I915_READ(G4X_AUD_CNTL_ST);
  6541. i |= eldv;
  6542. I915_WRITE(G4X_AUD_CNTL_ST, i);
  6543. }
  6544. static void haswell_write_eld(struct drm_connector *connector,
  6545. struct drm_crtc *crtc,
  6546. struct drm_display_mode *mode)
  6547. {
  6548. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6549. uint8_t *eld = connector->eld;
  6550. uint32_t eldv;
  6551. uint32_t i;
  6552. int len;
  6553. int pipe = to_intel_crtc(crtc)->pipe;
  6554. int tmp;
  6555. int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
  6556. int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
  6557. int aud_config = HSW_AUD_CFG(pipe);
  6558. int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
  6559. /* Audio output enable */
  6560. DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
  6561. tmp = I915_READ(aud_cntrl_st2);
  6562. tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
  6563. I915_WRITE(aud_cntrl_st2, tmp);
  6564. POSTING_READ(aud_cntrl_st2);
  6565. assert_pipe_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
  6566. /* Set ELD valid state */
  6567. tmp = I915_READ(aud_cntrl_st2);
  6568. DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
  6569. tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
  6570. I915_WRITE(aud_cntrl_st2, tmp);
  6571. tmp = I915_READ(aud_cntrl_st2);
  6572. DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
  6573. /* Enable HDMI mode */
  6574. tmp = I915_READ(aud_config);
  6575. DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
  6576. /* clear N_programing_enable and N_value_index */
  6577. tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
  6578. I915_WRITE(aud_config, tmp);
  6579. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  6580. eldv = AUDIO_ELD_VALID_A << (pipe * 4);
  6581. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  6582. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  6583. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  6584. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  6585. } else {
  6586. I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
  6587. }
  6588. if (intel_eld_uptodate(connector,
  6589. aud_cntrl_st2, eldv,
  6590. aud_cntl_st, IBX_ELD_ADDRESS,
  6591. hdmiw_hdmiedid))
  6592. return;
  6593. i = I915_READ(aud_cntrl_st2);
  6594. i &= ~eldv;
  6595. I915_WRITE(aud_cntrl_st2, i);
  6596. if (!eld[0])
  6597. return;
  6598. i = I915_READ(aud_cntl_st);
  6599. i &= ~IBX_ELD_ADDRESS;
  6600. I915_WRITE(aud_cntl_st, i);
  6601. i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
  6602. DRM_DEBUG_DRIVER("port num:%d\n", i);
  6603. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  6604. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6605. for (i = 0; i < len; i++)
  6606. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  6607. i = I915_READ(aud_cntrl_st2);
  6608. i |= eldv;
  6609. I915_WRITE(aud_cntrl_st2, i);
  6610. }
  6611. static void ironlake_write_eld(struct drm_connector *connector,
  6612. struct drm_crtc *crtc,
  6613. struct drm_display_mode *mode)
  6614. {
  6615. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6616. uint8_t *eld = connector->eld;
  6617. uint32_t eldv;
  6618. uint32_t i;
  6619. int len;
  6620. int hdmiw_hdmiedid;
  6621. int aud_config;
  6622. int aud_cntl_st;
  6623. int aud_cntrl_st2;
  6624. int pipe = to_intel_crtc(crtc)->pipe;
  6625. if (HAS_PCH_IBX(connector->dev)) {
  6626. hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
  6627. aud_config = IBX_AUD_CFG(pipe);
  6628. aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
  6629. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  6630. } else if (IS_VALLEYVIEW(connector->dev)) {
  6631. hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
  6632. aud_config = VLV_AUD_CFG(pipe);
  6633. aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
  6634. aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
  6635. } else {
  6636. hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
  6637. aud_config = CPT_AUD_CFG(pipe);
  6638. aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
  6639. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  6640. }
  6641. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  6642. if (IS_VALLEYVIEW(connector->dev)) {
  6643. struct intel_encoder *intel_encoder;
  6644. struct intel_digital_port *intel_dig_port;
  6645. intel_encoder = intel_attached_encoder(connector);
  6646. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  6647. i = intel_dig_port->port;
  6648. } else {
  6649. i = I915_READ(aud_cntl_st);
  6650. i = (i >> 29) & DIP_PORT_SEL_MASK;
  6651. /* DIP_Port_Select, 0x1 = PortB */
  6652. }
  6653. if (!i) {
  6654. DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
  6655. /* operate blindly on all ports */
  6656. eldv = IBX_ELD_VALIDB;
  6657. eldv |= IBX_ELD_VALIDB << 4;
  6658. eldv |= IBX_ELD_VALIDB << 8;
  6659. } else {
  6660. DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
  6661. eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
  6662. }
  6663. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  6664. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  6665. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  6666. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  6667. } else {
  6668. I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
  6669. }
  6670. if (intel_eld_uptodate(connector,
  6671. aud_cntrl_st2, eldv,
  6672. aud_cntl_st, IBX_ELD_ADDRESS,
  6673. hdmiw_hdmiedid))
  6674. return;
  6675. i = I915_READ(aud_cntrl_st2);
  6676. i &= ~eldv;
  6677. I915_WRITE(aud_cntrl_st2, i);
  6678. if (!eld[0])
  6679. return;
  6680. i = I915_READ(aud_cntl_st);
  6681. i &= ~IBX_ELD_ADDRESS;
  6682. I915_WRITE(aud_cntl_st, i);
  6683. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  6684. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6685. for (i = 0; i < len; i++)
  6686. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  6687. i = I915_READ(aud_cntrl_st2);
  6688. i |= eldv;
  6689. I915_WRITE(aud_cntrl_st2, i);
  6690. }
  6691. void intel_write_eld(struct drm_encoder *encoder,
  6692. struct drm_display_mode *mode)
  6693. {
  6694. struct drm_crtc *crtc = encoder->crtc;
  6695. struct drm_connector *connector;
  6696. struct drm_device *dev = encoder->dev;
  6697. struct drm_i915_private *dev_priv = dev->dev_private;
  6698. connector = drm_select_eld(encoder, mode);
  6699. if (!connector)
  6700. return;
  6701. DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  6702. connector->base.id,
  6703. connector->name,
  6704. connector->encoder->base.id,
  6705. connector->encoder->name);
  6706. connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
  6707. if (dev_priv->display.write_eld)
  6708. dev_priv->display.write_eld(connector, crtc, mode);
  6709. }
  6710. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  6711. {
  6712. struct drm_device *dev = crtc->dev;
  6713. struct drm_i915_private *dev_priv = dev->dev_private;
  6714. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6715. uint32_t cntl;
  6716. if (base != intel_crtc->cursor_base) {
  6717. /* On these chipsets we can only modify the base whilst
  6718. * the cursor is disabled.
  6719. */
  6720. if (intel_crtc->cursor_cntl) {
  6721. I915_WRITE(_CURACNTR, 0);
  6722. POSTING_READ(_CURACNTR);
  6723. intel_crtc->cursor_cntl = 0;
  6724. }
  6725. I915_WRITE(_CURABASE, base);
  6726. POSTING_READ(_CURABASE);
  6727. }
  6728. /* XXX width must be 64, stride 256 => 0x00 << 28 */
  6729. cntl = 0;
  6730. if (base)
  6731. cntl = (CURSOR_ENABLE |
  6732. CURSOR_GAMMA_ENABLE |
  6733. CURSOR_FORMAT_ARGB);
  6734. if (intel_crtc->cursor_cntl != cntl) {
  6735. I915_WRITE(_CURACNTR, cntl);
  6736. POSTING_READ(_CURACNTR);
  6737. intel_crtc->cursor_cntl = cntl;
  6738. }
  6739. }
  6740. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  6741. {
  6742. struct drm_device *dev = crtc->dev;
  6743. struct drm_i915_private *dev_priv = dev->dev_private;
  6744. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6745. int pipe = intel_crtc->pipe;
  6746. uint32_t cntl;
  6747. cntl = 0;
  6748. if (base) {
  6749. cntl = MCURSOR_GAMMA_ENABLE;
  6750. switch (intel_crtc->cursor_width) {
  6751. case 64:
  6752. cntl |= CURSOR_MODE_64_ARGB_AX;
  6753. break;
  6754. case 128:
  6755. cntl |= CURSOR_MODE_128_ARGB_AX;
  6756. break;
  6757. case 256:
  6758. cntl |= CURSOR_MODE_256_ARGB_AX;
  6759. break;
  6760. default:
  6761. WARN_ON(1);
  6762. return;
  6763. }
  6764. cntl |= pipe << 28; /* Connect to correct pipe */
  6765. }
  6766. if (intel_crtc->cursor_cntl != cntl) {
  6767. I915_WRITE(CURCNTR(pipe), cntl);
  6768. POSTING_READ(CURCNTR(pipe));
  6769. intel_crtc->cursor_cntl = cntl;
  6770. }
  6771. /* and commit changes on next vblank */
  6772. I915_WRITE(CURBASE(pipe), base);
  6773. POSTING_READ(CURBASE(pipe));
  6774. }
  6775. static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
  6776. {
  6777. struct drm_device *dev = crtc->dev;
  6778. struct drm_i915_private *dev_priv = dev->dev_private;
  6779. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6780. int pipe = intel_crtc->pipe;
  6781. uint32_t cntl;
  6782. cntl = 0;
  6783. if (base) {
  6784. cntl = MCURSOR_GAMMA_ENABLE;
  6785. switch (intel_crtc->cursor_width) {
  6786. case 64:
  6787. cntl |= CURSOR_MODE_64_ARGB_AX;
  6788. break;
  6789. case 128:
  6790. cntl |= CURSOR_MODE_128_ARGB_AX;
  6791. break;
  6792. case 256:
  6793. cntl |= CURSOR_MODE_256_ARGB_AX;
  6794. break;
  6795. default:
  6796. WARN_ON(1);
  6797. return;
  6798. }
  6799. }
  6800. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  6801. cntl |= CURSOR_PIPE_CSC_ENABLE;
  6802. if (intel_crtc->cursor_cntl != cntl) {
  6803. I915_WRITE(CURCNTR(pipe), cntl);
  6804. POSTING_READ(CURCNTR(pipe));
  6805. intel_crtc->cursor_cntl = cntl;
  6806. }
  6807. /* and commit changes on next vblank */
  6808. I915_WRITE(CURBASE(pipe), base);
  6809. POSTING_READ(CURBASE(pipe));
  6810. }
  6811. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  6812. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  6813. bool on)
  6814. {
  6815. struct drm_device *dev = crtc->dev;
  6816. struct drm_i915_private *dev_priv = dev->dev_private;
  6817. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6818. int pipe = intel_crtc->pipe;
  6819. int x = crtc->cursor_x;
  6820. int y = crtc->cursor_y;
  6821. u32 base = 0, pos = 0;
  6822. if (on)
  6823. base = intel_crtc->cursor_addr;
  6824. if (x >= intel_crtc->config.pipe_src_w)
  6825. base = 0;
  6826. if (y >= intel_crtc->config.pipe_src_h)
  6827. base = 0;
  6828. if (x < 0) {
  6829. if (x + intel_crtc->cursor_width <= 0)
  6830. base = 0;
  6831. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  6832. x = -x;
  6833. }
  6834. pos |= x << CURSOR_X_SHIFT;
  6835. if (y < 0) {
  6836. if (y + intel_crtc->cursor_height <= 0)
  6837. base = 0;
  6838. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  6839. y = -y;
  6840. }
  6841. pos |= y << CURSOR_Y_SHIFT;
  6842. if (base == 0 && intel_crtc->cursor_base == 0)
  6843. return;
  6844. I915_WRITE(CURPOS(pipe), pos);
  6845. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev) || IS_BROADWELL(dev))
  6846. ivb_update_cursor(crtc, base);
  6847. else if (IS_845G(dev) || IS_I865G(dev))
  6848. i845_update_cursor(crtc, base);
  6849. else
  6850. i9xx_update_cursor(crtc, base);
  6851. intel_crtc->cursor_base = base;
  6852. }
  6853. /*
  6854. * intel_crtc_cursor_set_obj - Set cursor to specified GEM object
  6855. *
  6856. * Note that the object's reference will be consumed if the update fails. If
  6857. * the update succeeds, the reference of the old object (if any) will be
  6858. * consumed.
  6859. */
  6860. static int intel_crtc_cursor_set_obj(struct drm_crtc *crtc,
  6861. struct drm_i915_gem_object *obj,
  6862. uint32_t width, uint32_t height)
  6863. {
  6864. struct drm_device *dev = crtc->dev;
  6865. struct drm_i915_private *dev_priv = dev->dev_private;
  6866. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6867. enum pipe pipe = intel_crtc->pipe;
  6868. unsigned old_width;
  6869. uint32_t addr;
  6870. int ret;
  6871. /* if we want to turn off the cursor ignore width and height */
  6872. if (!obj) {
  6873. DRM_DEBUG_KMS("cursor off\n");
  6874. addr = 0;
  6875. obj = NULL;
  6876. mutex_lock(&dev->struct_mutex);
  6877. goto finish;
  6878. }
  6879. /* Check for which cursor types we support */
  6880. if (!((width == 64 && height == 64) ||
  6881. (width == 128 && height == 128 && !IS_GEN2(dev)) ||
  6882. (width == 256 && height == 256 && !IS_GEN2(dev)))) {
  6883. DRM_DEBUG("Cursor dimension not supported\n");
  6884. return -EINVAL;
  6885. }
  6886. if (obj->base.size < width * height * 4) {
  6887. DRM_DEBUG_KMS("buffer is too small\n");
  6888. ret = -ENOMEM;
  6889. goto fail;
  6890. }
  6891. /* we only need to pin inside GTT if cursor is non-phy */
  6892. mutex_lock(&dev->struct_mutex);
  6893. if (!INTEL_INFO(dev)->cursor_needs_physical) {
  6894. unsigned alignment;
  6895. if (obj->tiling_mode) {
  6896. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  6897. ret = -EINVAL;
  6898. goto fail_locked;
  6899. }
  6900. /* Note that the w/a also requires 2 PTE of padding following
  6901. * the bo. We currently fill all unused PTE with the shadow
  6902. * page and so we should always have valid PTE following the
  6903. * cursor preventing the VT-d warning.
  6904. */
  6905. alignment = 0;
  6906. if (need_vtd_wa(dev))
  6907. alignment = 64*1024;
  6908. ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
  6909. if (ret) {
  6910. DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
  6911. goto fail_locked;
  6912. }
  6913. ret = i915_gem_object_put_fence(obj);
  6914. if (ret) {
  6915. DRM_DEBUG_KMS("failed to release fence for cursor");
  6916. goto fail_unpin;
  6917. }
  6918. addr = i915_gem_obj_ggtt_offset(obj);
  6919. } else {
  6920. int align = IS_I830(dev) ? 16 * 1024 : 256;
  6921. ret = i915_gem_object_attach_phys(obj, align);
  6922. if (ret) {
  6923. DRM_DEBUG_KMS("failed to attach phys object\n");
  6924. goto fail_locked;
  6925. }
  6926. addr = obj->phys_handle->busaddr;
  6927. }
  6928. if (IS_GEN2(dev))
  6929. I915_WRITE(CURSIZE, (height << 12) | width);
  6930. finish:
  6931. if (intel_crtc->cursor_bo) {
  6932. if (!INTEL_INFO(dev)->cursor_needs_physical)
  6933. i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
  6934. }
  6935. i915_gem_track_fb(intel_crtc->cursor_bo, obj,
  6936. INTEL_FRONTBUFFER_CURSOR(pipe));
  6937. mutex_unlock(&dev->struct_mutex);
  6938. old_width = intel_crtc->cursor_width;
  6939. intel_crtc->cursor_addr = addr;
  6940. intel_crtc->cursor_bo = obj;
  6941. intel_crtc->cursor_width = width;
  6942. intel_crtc->cursor_height = height;
  6943. if (intel_crtc->active) {
  6944. if (old_width != width)
  6945. intel_update_watermarks(crtc);
  6946. intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
  6947. }
  6948. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_CURSOR(pipe));
  6949. return 0;
  6950. fail_unpin:
  6951. i915_gem_object_unpin_from_display_plane(obj);
  6952. fail_locked:
  6953. mutex_unlock(&dev->struct_mutex);
  6954. fail:
  6955. drm_gem_object_unreference_unlocked(&obj->base);
  6956. return ret;
  6957. }
  6958. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  6959. u16 *blue, uint32_t start, uint32_t size)
  6960. {
  6961. int end = (start + size > 256) ? 256 : start + size, i;
  6962. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6963. for (i = start; i < end; i++) {
  6964. intel_crtc->lut_r[i] = red[i] >> 8;
  6965. intel_crtc->lut_g[i] = green[i] >> 8;
  6966. intel_crtc->lut_b[i] = blue[i] >> 8;
  6967. }
  6968. intel_crtc_load_lut(crtc);
  6969. }
  6970. /* VESA 640x480x72Hz mode to set on the pipe */
  6971. static struct drm_display_mode load_detect_mode = {
  6972. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  6973. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  6974. };
  6975. struct drm_framebuffer *
  6976. __intel_framebuffer_create(struct drm_device *dev,
  6977. struct drm_mode_fb_cmd2 *mode_cmd,
  6978. struct drm_i915_gem_object *obj)
  6979. {
  6980. struct intel_framebuffer *intel_fb;
  6981. int ret;
  6982. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6983. if (!intel_fb) {
  6984. drm_gem_object_unreference_unlocked(&obj->base);
  6985. return ERR_PTR(-ENOMEM);
  6986. }
  6987. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  6988. if (ret)
  6989. goto err;
  6990. return &intel_fb->base;
  6991. err:
  6992. drm_gem_object_unreference_unlocked(&obj->base);
  6993. kfree(intel_fb);
  6994. return ERR_PTR(ret);
  6995. }
  6996. static struct drm_framebuffer *
  6997. intel_framebuffer_create(struct drm_device *dev,
  6998. struct drm_mode_fb_cmd2 *mode_cmd,
  6999. struct drm_i915_gem_object *obj)
  7000. {
  7001. struct drm_framebuffer *fb;
  7002. int ret;
  7003. ret = i915_mutex_lock_interruptible(dev);
  7004. if (ret)
  7005. return ERR_PTR(ret);
  7006. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  7007. mutex_unlock(&dev->struct_mutex);
  7008. return fb;
  7009. }
  7010. static u32
  7011. intel_framebuffer_pitch_for_width(int width, int bpp)
  7012. {
  7013. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  7014. return ALIGN(pitch, 64);
  7015. }
  7016. static u32
  7017. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  7018. {
  7019. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  7020. return PAGE_ALIGN(pitch * mode->vdisplay);
  7021. }
  7022. static struct drm_framebuffer *
  7023. intel_framebuffer_create_for_mode(struct drm_device *dev,
  7024. struct drm_display_mode *mode,
  7025. int depth, int bpp)
  7026. {
  7027. struct drm_i915_gem_object *obj;
  7028. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  7029. obj = i915_gem_alloc_object(dev,
  7030. intel_framebuffer_size_for_mode(mode, bpp));
  7031. if (obj == NULL)
  7032. return ERR_PTR(-ENOMEM);
  7033. mode_cmd.width = mode->hdisplay;
  7034. mode_cmd.height = mode->vdisplay;
  7035. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  7036. bpp);
  7037. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  7038. return intel_framebuffer_create(dev, &mode_cmd, obj);
  7039. }
  7040. static struct drm_framebuffer *
  7041. mode_fits_in_fbdev(struct drm_device *dev,
  7042. struct drm_display_mode *mode)
  7043. {
  7044. #ifdef CONFIG_DRM_I915_FBDEV
  7045. struct drm_i915_private *dev_priv = dev->dev_private;
  7046. struct drm_i915_gem_object *obj;
  7047. struct drm_framebuffer *fb;
  7048. if (!dev_priv->fbdev)
  7049. return NULL;
  7050. if (!dev_priv->fbdev->fb)
  7051. return NULL;
  7052. obj = dev_priv->fbdev->fb->obj;
  7053. BUG_ON(!obj);
  7054. fb = &dev_priv->fbdev->fb->base;
  7055. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  7056. fb->bits_per_pixel))
  7057. return NULL;
  7058. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  7059. return NULL;
  7060. return fb;
  7061. #else
  7062. return NULL;
  7063. #endif
  7064. }
  7065. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  7066. struct drm_display_mode *mode,
  7067. struct intel_load_detect_pipe *old,
  7068. struct drm_modeset_acquire_ctx *ctx)
  7069. {
  7070. struct intel_crtc *intel_crtc;
  7071. struct intel_encoder *intel_encoder =
  7072. intel_attached_encoder(connector);
  7073. struct drm_crtc *possible_crtc;
  7074. struct drm_encoder *encoder = &intel_encoder->base;
  7075. struct drm_crtc *crtc = NULL;
  7076. struct drm_device *dev = encoder->dev;
  7077. struct drm_framebuffer *fb;
  7078. struct drm_mode_config *config = &dev->mode_config;
  7079. int ret, i = -1;
  7080. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7081. connector->base.id, connector->name,
  7082. encoder->base.id, encoder->name);
  7083. drm_modeset_acquire_init(ctx, 0);
  7084. retry:
  7085. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  7086. if (ret)
  7087. goto fail_unlock;
  7088. /*
  7089. * Algorithm gets a little messy:
  7090. *
  7091. * - if the connector already has an assigned crtc, use it (but make
  7092. * sure it's on first)
  7093. *
  7094. * - try to find the first unused crtc that can drive this connector,
  7095. * and use that if we find one
  7096. */
  7097. /* See if we already have a CRTC for this connector */
  7098. if (encoder->crtc) {
  7099. crtc = encoder->crtc;
  7100. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7101. if (ret)
  7102. goto fail_unlock;
  7103. old->dpms_mode = connector->dpms;
  7104. old->load_detect_temp = false;
  7105. /* Make sure the crtc and connector are running */
  7106. if (connector->dpms != DRM_MODE_DPMS_ON)
  7107. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  7108. return true;
  7109. }
  7110. /* Find an unused one (if possible) */
  7111. for_each_crtc(dev, possible_crtc) {
  7112. i++;
  7113. if (!(encoder->possible_crtcs & (1 << i)))
  7114. continue;
  7115. if (!possible_crtc->enabled) {
  7116. crtc = possible_crtc;
  7117. break;
  7118. }
  7119. }
  7120. /*
  7121. * If we didn't find an unused CRTC, don't use any.
  7122. */
  7123. if (!crtc) {
  7124. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  7125. goto fail_unlock;
  7126. }
  7127. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7128. if (ret)
  7129. goto fail_unlock;
  7130. intel_encoder->new_crtc = to_intel_crtc(crtc);
  7131. to_intel_connector(connector)->new_encoder = intel_encoder;
  7132. intel_crtc = to_intel_crtc(crtc);
  7133. intel_crtc->new_enabled = true;
  7134. intel_crtc->new_config = &intel_crtc->config;
  7135. old->dpms_mode = connector->dpms;
  7136. old->load_detect_temp = true;
  7137. old->release_fb = NULL;
  7138. if (!mode)
  7139. mode = &load_detect_mode;
  7140. /* We need a framebuffer large enough to accommodate all accesses
  7141. * that the plane may generate whilst we perform load detection.
  7142. * We can not rely on the fbcon either being present (we get called
  7143. * during its initialisation to detect all boot displays, or it may
  7144. * not even exist) or that it is large enough to satisfy the
  7145. * requested mode.
  7146. */
  7147. fb = mode_fits_in_fbdev(dev, mode);
  7148. if (fb == NULL) {
  7149. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  7150. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  7151. old->release_fb = fb;
  7152. } else
  7153. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  7154. if (IS_ERR(fb)) {
  7155. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  7156. goto fail;
  7157. }
  7158. if (intel_set_mode(crtc, mode, 0, 0, fb)) {
  7159. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  7160. if (old->release_fb)
  7161. old->release_fb->funcs->destroy(old->release_fb);
  7162. goto fail;
  7163. }
  7164. /* let the connector get through one full cycle before testing */
  7165. intel_wait_for_vblank(dev, intel_crtc->pipe);
  7166. return true;
  7167. fail:
  7168. intel_crtc->new_enabled = crtc->enabled;
  7169. if (intel_crtc->new_enabled)
  7170. intel_crtc->new_config = &intel_crtc->config;
  7171. else
  7172. intel_crtc->new_config = NULL;
  7173. fail_unlock:
  7174. if (ret == -EDEADLK) {
  7175. drm_modeset_backoff(ctx);
  7176. goto retry;
  7177. }
  7178. drm_modeset_drop_locks(ctx);
  7179. drm_modeset_acquire_fini(ctx);
  7180. return false;
  7181. }
  7182. void intel_release_load_detect_pipe(struct drm_connector *connector,
  7183. struct intel_load_detect_pipe *old,
  7184. struct drm_modeset_acquire_ctx *ctx)
  7185. {
  7186. struct intel_encoder *intel_encoder =
  7187. intel_attached_encoder(connector);
  7188. struct drm_encoder *encoder = &intel_encoder->base;
  7189. struct drm_crtc *crtc = encoder->crtc;
  7190. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7191. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7192. connector->base.id, connector->name,
  7193. encoder->base.id, encoder->name);
  7194. if (old->load_detect_temp) {
  7195. to_intel_connector(connector)->new_encoder = NULL;
  7196. intel_encoder->new_crtc = NULL;
  7197. intel_crtc->new_enabled = false;
  7198. intel_crtc->new_config = NULL;
  7199. intel_set_mode(crtc, NULL, 0, 0, NULL);
  7200. if (old->release_fb) {
  7201. drm_framebuffer_unregister_private(old->release_fb);
  7202. drm_framebuffer_unreference(old->release_fb);
  7203. }
  7204. goto unlock;
  7205. return;
  7206. }
  7207. /* Switch crtc and encoder back off if necessary */
  7208. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  7209. connector->funcs->dpms(connector, old->dpms_mode);
  7210. unlock:
  7211. drm_modeset_drop_locks(ctx);
  7212. drm_modeset_acquire_fini(ctx);
  7213. }
  7214. static int i9xx_pll_refclk(struct drm_device *dev,
  7215. const struct intel_crtc_config *pipe_config)
  7216. {
  7217. struct drm_i915_private *dev_priv = dev->dev_private;
  7218. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7219. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  7220. return dev_priv->vbt.lvds_ssc_freq;
  7221. else if (HAS_PCH_SPLIT(dev))
  7222. return 120000;
  7223. else if (!IS_GEN2(dev))
  7224. return 96000;
  7225. else
  7226. return 48000;
  7227. }
  7228. /* Returns the clock of the currently programmed mode of the given pipe. */
  7229. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  7230. struct intel_crtc_config *pipe_config)
  7231. {
  7232. struct drm_device *dev = crtc->base.dev;
  7233. struct drm_i915_private *dev_priv = dev->dev_private;
  7234. int pipe = pipe_config->cpu_transcoder;
  7235. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7236. u32 fp;
  7237. intel_clock_t clock;
  7238. int refclk = i9xx_pll_refclk(dev, pipe_config);
  7239. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  7240. fp = pipe_config->dpll_hw_state.fp0;
  7241. else
  7242. fp = pipe_config->dpll_hw_state.fp1;
  7243. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  7244. if (IS_PINEVIEW(dev)) {
  7245. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  7246. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7247. } else {
  7248. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  7249. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7250. }
  7251. if (!IS_GEN2(dev)) {
  7252. if (IS_PINEVIEW(dev))
  7253. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  7254. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  7255. else
  7256. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  7257. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7258. switch (dpll & DPLL_MODE_MASK) {
  7259. case DPLLB_MODE_DAC_SERIAL:
  7260. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  7261. 5 : 10;
  7262. break;
  7263. case DPLLB_MODE_LVDS:
  7264. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  7265. 7 : 14;
  7266. break;
  7267. default:
  7268. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  7269. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  7270. return;
  7271. }
  7272. if (IS_PINEVIEW(dev))
  7273. pineview_clock(refclk, &clock);
  7274. else
  7275. i9xx_clock(refclk, &clock);
  7276. } else {
  7277. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  7278. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  7279. if (is_lvds) {
  7280. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  7281. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7282. if (lvds & LVDS_CLKB_POWER_UP)
  7283. clock.p2 = 7;
  7284. else
  7285. clock.p2 = 14;
  7286. } else {
  7287. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  7288. clock.p1 = 2;
  7289. else {
  7290. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  7291. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  7292. }
  7293. if (dpll & PLL_P2_DIVIDE_BY_4)
  7294. clock.p2 = 4;
  7295. else
  7296. clock.p2 = 2;
  7297. }
  7298. i9xx_clock(refclk, &clock);
  7299. }
  7300. /*
  7301. * This value includes pixel_multiplier. We will use
  7302. * port_clock to compute adjusted_mode.crtc_clock in the
  7303. * encoder's get_config() function.
  7304. */
  7305. pipe_config->port_clock = clock.dot;
  7306. }
  7307. int intel_dotclock_calculate(int link_freq,
  7308. const struct intel_link_m_n *m_n)
  7309. {
  7310. /*
  7311. * The calculation for the data clock is:
  7312. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  7313. * But we want to avoid losing precison if possible, so:
  7314. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  7315. *
  7316. * and the link clock is simpler:
  7317. * link_clock = (m * link_clock) / n
  7318. */
  7319. if (!m_n->link_n)
  7320. return 0;
  7321. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  7322. }
  7323. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  7324. struct intel_crtc_config *pipe_config)
  7325. {
  7326. struct drm_device *dev = crtc->base.dev;
  7327. /* read out port_clock from the DPLL */
  7328. i9xx_crtc_clock_get(crtc, pipe_config);
  7329. /*
  7330. * This value does not include pixel_multiplier.
  7331. * We will check that port_clock and adjusted_mode.crtc_clock
  7332. * agree once we know their relationship in the encoder's
  7333. * get_config() function.
  7334. */
  7335. pipe_config->adjusted_mode.crtc_clock =
  7336. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  7337. &pipe_config->fdi_m_n);
  7338. }
  7339. /** Returns the currently programmed mode of the given pipe. */
  7340. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  7341. struct drm_crtc *crtc)
  7342. {
  7343. struct drm_i915_private *dev_priv = dev->dev_private;
  7344. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7345. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  7346. struct drm_display_mode *mode;
  7347. struct intel_crtc_config pipe_config;
  7348. int htot = I915_READ(HTOTAL(cpu_transcoder));
  7349. int hsync = I915_READ(HSYNC(cpu_transcoder));
  7350. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  7351. int vsync = I915_READ(VSYNC(cpu_transcoder));
  7352. enum pipe pipe = intel_crtc->pipe;
  7353. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  7354. if (!mode)
  7355. return NULL;
  7356. /*
  7357. * Construct a pipe_config sufficient for getting the clock info
  7358. * back out of crtc_clock_get.
  7359. *
  7360. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  7361. * to use a real value here instead.
  7362. */
  7363. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  7364. pipe_config.pixel_multiplier = 1;
  7365. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  7366. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  7367. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  7368. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  7369. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  7370. mode->hdisplay = (htot & 0xffff) + 1;
  7371. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  7372. mode->hsync_start = (hsync & 0xffff) + 1;
  7373. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  7374. mode->vdisplay = (vtot & 0xffff) + 1;
  7375. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  7376. mode->vsync_start = (vsync & 0xffff) + 1;
  7377. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  7378. drm_mode_set_name(mode);
  7379. return mode;
  7380. }
  7381. static void intel_increase_pllclock(struct drm_device *dev,
  7382. enum pipe pipe)
  7383. {
  7384. struct drm_i915_private *dev_priv = dev->dev_private;
  7385. int dpll_reg = DPLL(pipe);
  7386. int dpll;
  7387. if (!HAS_GMCH_DISPLAY(dev))
  7388. return;
  7389. if (!dev_priv->lvds_downclock_avail)
  7390. return;
  7391. dpll = I915_READ(dpll_reg);
  7392. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  7393. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  7394. assert_panel_unlocked(dev_priv, pipe);
  7395. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  7396. I915_WRITE(dpll_reg, dpll);
  7397. intel_wait_for_vblank(dev, pipe);
  7398. dpll = I915_READ(dpll_reg);
  7399. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  7400. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  7401. }
  7402. }
  7403. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  7404. {
  7405. struct drm_device *dev = crtc->dev;
  7406. struct drm_i915_private *dev_priv = dev->dev_private;
  7407. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7408. if (!HAS_GMCH_DISPLAY(dev))
  7409. return;
  7410. if (!dev_priv->lvds_downclock_avail)
  7411. return;
  7412. /*
  7413. * Since this is called by a timer, we should never get here in
  7414. * the manual case.
  7415. */
  7416. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  7417. int pipe = intel_crtc->pipe;
  7418. int dpll_reg = DPLL(pipe);
  7419. int dpll;
  7420. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  7421. assert_panel_unlocked(dev_priv, pipe);
  7422. dpll = I915_READ(dpll_reg);
  7423. dpll |= DISPLAY_RATE_SELECT_FPA1;
  7424. I915_WRITE(dpll_reg, dpll);
  7425. intel_wait_for_vblank(dev, pipe);
  7426. dpll = I915_READ(dpll_reg);
  7427. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  7428. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  7429. }
  7430. }
  7431. void intel_mark_busy(struct drm_device *dev)
  7432. {
  7433. struct drm_i915_private *dev_priv = dev->dev_private;
  7434. if (dev_priv->mm.busy)
  7435. return;
  7436. intel_runtime_pm_get(dev_priv);
  7437. i915_update_gfx_val(dev_priv);
  7438. dev_priv->mm.busy = true;
  7439. }
  7440. void intel_mark_idle(struct drm_device *dev)
  7441. {
  7442. struct drm_i915_private *dev_priv = dev->dev_private;
  7443. struct drm_crtc *crtc;
  7444. if (!dev_priv->mm.busy)
  7445. return;
  7446. dev_priv->mm.busy = false;
  7447. if (!i915.powersave)
  7448. goto out;
  7449. for_each_crtc(dev, crtc) {
  7450. if (!crtc->primary->fb)
  7451. continue;
  7452. intel_decrease_pllclock(crtc);
  7453. }
  7454. if (INTEL_INFO(dev)->gen >= 6)
  7455. gen6_rps_idle(dev->dev_private);
  7456. out:
  7457. intel_runtime_pm_put(dev_priv);
  7458. }
  7459. /**
  7460. * intel_mark_fb_busy - mark given planes as busy
  7461. * @dev: DRM device
  7462. * @frontbuffer_bits: bits for the affected planes
  7463. * @ring: optional ring for asynchronous commands
  7464. *
  7465. * This function gets called every time the screen contents change. It can be
  7466. * used to keep e.g. the update rate at the nominal refresh rate with DRRS.
  7467. */
  7468. static void intel_mark_fb_busy(struct drm_device *dev,
  7469. unsigned frontbuffer_bits,
  7470. struct intel_engine_cs *ring)
  7471. {
  7472. enum pipe pipe;
  7473. if (!i915.powersave)
  7474. return;
  7475. for_each_pipe(pipe) {
  7476. if (!(frontbuffer_bits & INTEL_FRONTBUFFER_ALL_MASK(pipe)))
  7477. continue;
  7478. intel_increase_pllclock(dev, pipe);
  7479. if (ring && intel_fbc_enabled(dev))
  7480. ring->fbc_dirty = true;
  7481. }
  7482. }
  7483. /**
  7484. * intel_fb_obj_invalidate - invalidate frontbuffer object
  7485. * @obj: GEM object to invalidate
  7486. * @ring: set for asynchronous rendering
  7487. *
  7488. * This function gets called every time rendering on the given object starts and
  7489. * frontbuffer caching (fbc, low refresh rate for DRRS, panel self refresh) must
  7490. * be invalidated. If @ring is non-NULL any subsequent invalidation will be delayed
  7491. * until the rendering completes or a flip on this frontbuffer plane is
  7492. * scheduled.
  7493. */
  7494. void intel_fb_obj_invalidate(struct drm_i915_gem_object *obj,
  7495. struct intel_engine_cs *ring)
  7496. {
  7497. struct drm_device *dev = obj->base.dev;
  7498. struct drm_i915_private *dev_priv = dev->dev_private;
  7499. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  7500. if (!obj->frontbuffer_bits)
  7501. return;
  7502. if (ring) {
  7503. mutex_lock(&dev_priv->fb_tracking.lock);
  7504. dev_priv->fb_tracking.busy_bits
  7505. |= obj->frontbuffer_bits;
  7506. dev_priv->fb_tracking.flip_bits
  7507. &= ~obj->frontbuffer_bits;
  7508. mutex_unlock(&dev_priv->fb_tracking.lock);
  7509. }
  7510. intel_mark_fb_busy(dev, obj->frontbuffer_bits, ring);
  7511. intel_edp_psr_invalidate(dev, obj->frontbuffer_bits);
  7512. }
  7513. /**
  7514. * intel_frontbuffer_flush - flush frontbuffer
  7515. * @dev: DRM device
  7516. * @frontbuffer_bits: frontbuffer plane tracking bits
  7517. *
  7518. * This function gets called every time rendering on the given planes has
  7519. * completed and frontbuffer caching can be started again. Flushes will get
  7520. * delayed if they're blocked by some oustanding asynchronous rendering.
  7521. *
  7522. * Can be called without any locks held.
  7523. */
  7524. void intel_frontbuffer_flush(struct drm_device *dev,
  7525. unsigned frontbuffer_bits)
  7526. {
  7527. struct drm_i915_private *dev_priv = dev->dev_private;
  7528. /* Delay flushing when rings are still busy.*/
  7529. mutex_lock(&dev_priv->fb_tracking.lock);
  7530. frontbuffer_bits &= ~dev_priv->fb_tracking.busy_bits;
  7531. mutex_unlock(&dev_priv->fb_tracking.lock);
  7532. intel_mark_fb_busy(dev, frontbuffer_bits, NULL);
  7533. intel_edp_psr_flush(dev, frontbuffer_bits);
  7534. }
  7535. /**
  7536. * intel_fb_obj_flush - flush frontbuffer object
  7537. * @obj: GEM object to flush
  7538. * @retire: set when retiring asynchronous rendering
  7539. *
  7540. * This function gets called every time rendering on the given object has
  7541. * completed and frontbuffer caching can be started again. If @retire is true
  7542. * then any delayed flushes will be unblocked.
  7543. */
  7544. void intel_fb_obj_flush(struct drm_i915_gem_object *obj,
  7545. bool retire)
  7546. {
  7547. struct drm_device *dev = obj->base.dev;
  7548. struct drm_i915_private *dev_priv = dev->dev_private;
  7549. unsigned frontbuffer_bits;
  7550. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  7551. if (!obj->frontbuffer_bits)
  7552. return;
  7553. frontbuffer_bits = obj->frontbuffer_bits;
  7554. if (retire) {
  7555. mutex_lock(&dev_priv->fb_tracking.lock);
  7556. /* Filter out new bits since rendering started. */
  7557. frontbuffer_bits &= dev_priv->fb_tracking.busy_bits;
  7558. dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits;
  7559. mutex_unlock(&dev_priv->fb_tracking.lock);
  7560. }
  7561. intel_frontbuffer_flush(dev, frontbuffer_bits);
  7562. }
  7563. /**
  7564. * intel_frontbuffer_flip_prepare - prepare asnychronous frontbuffer flip
  7565. * @dev: DRM device
  7566. * @frontbuffer_bits: frontbuffer plane tracking bits
  7567. *
  7568. * This function gets called after scheduling a flip on @obj. The actual
  7569. * frontbuffer flushing will be delayed until completion is signalled with
  7570. * intel_frontbuffer_flip_complete. If an invalidate happens in between this
  7571. * flush will be cancelled.
  7572. *
  7573. * Can be called without any locks held.
  7574. */
  7575. void intel_frontbuffer_flip_prepare(struct drm_device *dev,
  7576. unsigned frontbuffer_bits)
  7577. {
  7578. struct drm_i915_private *dev_priv = dev->dev_private;
  7579. mutex_lock(&dev_priv->fb_tracking.lock);
  7580. dev_priv->fb_tracking.flip_bits
  7581. |= frontbuffer_bits;
  7582. mutex_unlock(&dev_priv->fb_tracking.lock);
  7583. }
  7584. /**
  7585. * intel_frontbuffer_flip_complete - complete asynchronous frontbuffer flush
  7586. * @dev: DRM device
  7587. * @frontbuffer_bits: frontbuffer plane tracking bits
  7588. *
  7589. * This function gets called after the flip has been latched and will complete
  7590. * on the next vblank. It will execute the fush if it hasn't been cancalled yet.
  7591. *
  7592. * Can be called without any locks held.
  7593. */
  7594. void intel_frontbuffer_flip_complete(struct drm_device *dev,
  7595. unsigned frontbuffer_bits)
  7596. {
  7597. struct drm_i915_private *dev_priv = dev->dev_private;
  7598. mutex_lock(&dev_priv->fb_tracking.lock);
  7599. /* Mask any cancelled flips. */
  7600. frontbuffer_bits &= dev_priv->fb_tracking.flip_bits;
  7601. dev_priv->fb_tracking.flip_bits &= ~frontbuffer_bits;
  7602. mutex_unlock(&dev_priv->fb_tracking.lock);
  7603. intel_frontbuffer_flush(dev, frontbuffer_bits);
  7604. }
  7605. static void intel_crtc_destroy(struct drm_crtc *crtc)
  7606. {
  7607. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7608. struct drm_device *dev = crtc->dev;
  7609. struct intel_unpin_work *work;
  7610. unsigned long flags;
  7611. spin_lock_irqsave(&dev->event_lock, flags);
  7612. work = intel_crtc->unpin_work;
  7613. intel_crtc->unpin_work = NULL;
  7614. spin_unlock_irqrestore(&dev->event_lock, flags);
  7615. if (work) {
  7616. cancel_work_sync(&work->work);
  7617. kfree(work);
  7618. }
  7619. drm_crtc_cleanup(crtc);
  7620. kfree(intel_crtc);
  7621. }
  7622. static void intel_unpin_work_fn(struct work_struct *__work)
  7623. {
  7624. struct intel_unpin_work *work =
  7625. container_of(__work, struct intel_unpin_work, work);
  7626. struct drm_device *dev = work->crtc->dev;
  7627. enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
  7628. mutex_lock(&dev->struct_mutex);
  7629. intel_unpin_fb_obj(work->old_fb_obj);
  7630. drm_gem_object_unreference(&work->pending_flip_obj->base);
  7631. drm_gem_object_unreference(&work->old_fb_obj->base);
  7632. intel_update_fbc(dev);
  7633. mutex_unlock(&dev->struct_mutex);
  7634. intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  7635. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  7636. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  7637. kfree(work);
  7638. }
  7639. static void do_intel_finish_page_flip(struct drm_device *dev,
  7640. struct drm_crtc *crtc)
  7641. {
  7642. struct drm_i915_private *dev_priv = dev->dev_private;
  7643. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7644. struct intel_unpin_work *work;
  7645. unsigned long flags;
  7646. /* Ignore early vblank irqs */
  7647. if (intel_crtc == NULL)
  7648. return;
  7649. spin_lock_irqsave(&dev->event_lock, flags);
  7650. work = intel_crtc->unpin_work;
  7651. /* Ensure we don't miss a work->pending update ... */
  7652. smp_rmb();
  7653. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  7654. spin_unlock_irqrestore(&dev->event_lock, flags);
  7655. return;
  7656. }
  7657. /* and that the unpin work is consistent wrt ->pending. */
  7658. smp_rmb();
  7659. intel_crtc->unpin_work = NULL;
  7660. if (work->event)
  7661. drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
  7662. drm_crtc_vblank_put(crtc);
  7663. spin_unlock_irqrestore(&dev->event_lock, flags);
  7664. wake_up_all(&dev_priv->pending_flip_queue);
  7665. queue_work(dev_priv->wq, &work->work);
  7666. trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
  7667. }
  7668. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  7669. {
  7670. struct drm_i915_private *dev_priv = dev->dev_private;
  7671. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  7672. do_intel_finish_page_flip(dev, crtc);
  7673. }
  7674. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  7675. {
  7676. struct drm_i915_private *dev_priv = dev->dev_private;
  7677. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  7678. do_intel_finish_page_flip(dev, crtc);
  7679. }
  7680. /* Is 'a' after or equal to 'b'? */
  7681. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  7682. {
  7683. return !((a - b) & 0x80000000);
  7684. }
  7685. static bool page_flip_finished(struct intel_crtc *crtc)
  7686. {
  7687. struct drm_device *dev = crtc->base.dev;
  7688. struct drm_i915_private *dev_priv = dev->dev_private;
  7689. /*
  7690. * The relevant registers doen't exist on pre-ctg.
  7691. * As the flip done interrupt doesn't trigger for mmio
  7692. * flips on gmch platforms, a flip count check isn't
  7693. * really needed there. But since ctg has the registers,
  7694. * include it in the check anyway.
  7695. */
  7696. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  7697. return true;
  7698. /*
  7699. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  7700. * used the same base address. In that case the mmio flip might
  7701. * have completed, but the CS hasn't even executed the flip yet.
  7702. *
  7703. * A flip count check isn't enough as the CS might have updated
  7704. * the base address just after start of vblank, but before we
  7705. * managed to process the interrupt. This means we'd complete the
  7706. * CS flip too soon.
  7707. *
  7708. * Combining both checks should get us a good enough result. It may
  7709. * still happen that the CS flip has been executed, but has not
  7710. * yet actually completed. But in case the base address is the same
  7711. * anyway, we don't really care.
  7712. */
  7713. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  7714. crtc->unpin_work->gtt_offset &&
  7715. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  7716. crtc->unpin_work->flip_count);
  7717. }
  7718. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  7719. {
  7720. struct drm_i915_private *dev_priv = dev->dev_private;
  7721. struct intel_crtc *intel_crtc =
  7722. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  7723. unsigned long flags;
  7724. /* NB: An MMIO update of the plane base pointer will also
  7725. * generate a page-flip completion irq, i.e. every modeset
  7726. * is also accompanied by a spurious intel_prepare_page_flip().
  7727. */
  7728. spin_lock_irqsave(&dev->event_lock, flags);
  7729. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  7730. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  7731. spin_unlock_irqrestore(&dev->event_lock, flags);
  7732. }
  7733. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  7734. {
  7735. /* Ensure that the work item is consistent when activating it ... */
  7736. smp_wmb();
  7737. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  7738. /* and that it is marked active as soon as the irq could fire. */
  7739. smp_wmb();
  7740. }
  7741. static int intel_gen2_queue_flip(struct drm_device *dev,
  7742. struct drm_crtc *crtc,
  7743. struct drm_framebuffer *fb,
  7744. struct drm_i915_gem_object *obj,
  7745. struct intel_engine_cs *ring,
  7746. uint32_t flags)
  7747. {
  7748. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7749. u32 flip_mask;
  7750. int ret;
  7751. ret = intel_ring_begin(ring, 6);
  7752. if (ret)
  7753. return ret;
  7754. /* Can't queue multiple flips, so wait for the previous
  7755. * one to finish before executing the next.
  7756. */
  7757. if (intel_crtc->plane)
  7758. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7759. else
  7760. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7761. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7762. intel_ring_emit(ring, MI_NOOP);
  7763. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7764. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7765. intel_ring_emit(ring, fb->pitches[0]);
  7766. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7767. intel_ring_emit(ring, 0); /* aux display base address, unused */
  7768. intel_mark_page_flip_active(intel_crtc);
  7769. __intel_ring_advance(ring);
  7770. return 0;
  7771. }
  7772. static int intel_gen3_queue_flip(struct drm_device *dev,
  7773. struct drm_crtc *crtc,
  7774. struct drm_framebuffer *fb,
  7775. struct drm_i915_gem_object *obj,
  7776. struct intel_engine_cs *ring,
  7777. uint32_t flags)
  7778. {
  7779. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7780. u32 flip_mask;
  7781. int ret;
  7782. ret = intel_ring_begin(ring, 6);
  7783. if (ret)
  7784. return ret;
  7785. if (intel_crtc->plane)
  7786. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7787. else
  7788. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7789. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7790. intel_ring_emit(ring, MI_NOOP);
  7791. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  7792. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7793. intel_ring_emit(ring, fb->pitches[0]);
  7794. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7795. intel_ring_emit(ring, MI_NOOP);
  7796. intel_mark_page_flip_active(intel_crtc);
  7797. __intel_ring_advance(ring);
  7798. return 0;
  7799. }
  7800. static int intel_gen4_queue_flip(struct drm_device *dev,
  7801. struct drm_crtc *crtc,
  7802. struct drm_framebuffer *fb,
  7803. struct drm_i915_gem_object *obj,
  7804. struct intel_engine_cs *ring,
  7805. uint32_t flags)
  7806. {
  7807. struct drm_i915_private *dev_priv = dev->dev_private;
  7808. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7809. uint32_t pf, pipesrc;
  7810. int ret;
  7811. ret = intel_ring_begin(ring, 4);
  7812. if (ret)
  7813. return ret;
  7814. /* i965+ uses the linear or tiled offsets from the
  7815. * Display Registers (which do not change across a page-flip)
  7816. * so we need only reprogram the base address.
  7817. */
  7818. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7819. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7820. intel_ring_emit(ring, fb->pitches[0]);
  7821. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  7822. obj->tiling_mode);
  7823. /* XXX Enabling the panel-fitter across page-flip is so far
  7824. * untested on non-native modes, so ignore it for now.
  7825. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  7826. */
  7827. pf = 0;
  7828. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7829. intel_ring_emit(ring, pf | pipesrc);
  7830. intel_mark_page_flip_active(intel_crtc);
  7831. __intel_ring_advance(ring);
  7832. return 0;
  7833. }
  7834. static int intel_gen6_queue_flip(struct drm_device *dev,
  7835. struct drm_crtc *crtc,
  7836. struct drm_framebuffer *fb,
  7837. struct drm_i915_gem_object *obj,
  7838. struct intel_engine_cs *ring,
  7839. uint32_t flags)
  7840. {
  7841. struct drm_i915_private *dev_priv = dev->dev_private;
  7842. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7843. uint32_t pf, pipesrc;
  7844. int ret;
  7845. ret = intel_ring_begin(ring, 4);
  7846. if (ret)
  7847. return ret;
  7848. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7849. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7850. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  7851. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7852. /* Contrary to the suggestions in the documentation,
  7853. * "Enable Panel Fitter" does not seem to be required when page
  7854. * flipping with a non-native mode, and worse causes a normal
  7855. * modeset to fail.
  7856. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  7857. */
  7858. pf = 0;
  7859. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7860. intel_ring_emit(ring, pf | pipesrc);
  7861. intel_mark_page_flip_active(intel_crtc);
  7862. __intel_ring_advance(ring);
  7863. return 0;
  7864. }
  7865. static int intel_gen7_queue_flip(struct drm_device *dev,
  7866. struct drm_crtc *crtc,
  7867. struct drm_framebuffer *fb,
  7868. struct drm_i915_gem_object *obj,
  7869. struct intel_engine_cs *ring,
  7870. uint32_t flags)
  7871. {
  7872. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7873. uint32_t plane_bit = 0;
  7874. int len, ret;
  7875. switch (intel_crtc->plane) {
  7876. case PLANE_A:
  7877. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  7878. break;
  7879. case PLANE_B:
  7880. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  7881. break;
  7882. case PLANE_C:
  7883. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  7884. break;
  7885. default:
  7886. WARN_ONCE(1, "unknown plane in flip command\n");
  7887. return -ENODEV;
  7888. }
  7889. len = 4;
  7890. if (ring->id == RCS) {
  7891. len += 6;
  7892. /*
  7893. * On Gen 8, SRM is now taking an extra dword to accommodate
  7894. * 48bits addresses, and we need a NOOP for the batch size to
  7895. * stay even.
  7896. */
  7897. if (IS_GEN8(dev))
  7898. len += 2;
  7899. }
  7900. /*
  7901. * BSpec MI_DISPLAY_FLIP for IVB:
  7902. * "The full packet must be contained within the same cache line."
  7903. *
  7904. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  7905. * cacheline, if we ever start emitting more commands before
  7906. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  7907. * then do the cacheline alignment, and finally emit the
  7908. * MI_DISPLAY_FLIP.
  7909. */
  7910. ret = intel_ring_cacheline_align(ring);
  7911. if (ret)
  7912. return ret;
  7913. ret = intel_ring_begin(ring, len);
  7914. if (ret)
  7915. return ret;
  7916. /* Unmask the flip-done completion message. Note that the bspec says that
  7917. * we should do this for both the BCS and RCS, and that we must not unmask
  7918. * more than one flip event at any time (or ensure that one flip message
  7919. * can be sent by waiting for flip-done prior to queueing new flips).
  7920. * Experimentation says that BCS works despite DERRMR masking all
  7921. * flip-done completion events and that unmasking all planes at once
  7922. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  7923. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  7924. */
  7925. if (ring->id == RCS) {
  7926. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  7927. intel_ring_emit(ring, DERRMR);
  7928. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  7929. DERRMR_PIPEB_PRI_FLIP_DONE |
  7930. DERRMR_PIPEC_PRI_FLIP_DONE));
  7931. if (IS_GEN8(dev))
  7932. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  7933. MI_SRM_LRM_GLOBAL_GTT);
  7934. else
  7935. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  7936. MI_SRM_LRM_GLOBAL_GTT);
  7937. intel_ring_emit(ring, DERRMR);
  7938. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  7939. if (IS_GEN8(dev)) {
  7940. intel_ring_emit(ring, 0);
  7941. intel_ring_emit(ring, MI_NOOP);
  7942. }
  7943. }
  7944. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  7945. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  7946. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7947. intel_ring_emit(ring, (MI_NOOP));
  7948. intel_mark_page_flip_active(intel_crtc);
  7949. __intel_ring_advance(ring);
  7950. return 0;
  7951. }
  7952. static bool use_mmio_flip(struct intel_engine_cs *ring,
  7953. struct drm_i915_gem_object *obj)
  7954. {
  7955. /*
  7956. * This is not being used for older platforms, because
  7957. * non-availability of flip done interrupt forces us to use
  7958. * CS flips. Older platforms derive flip done using some clever
  7959. * tricks involving the flip_pending status bits and vblank irqs.
  7960. * So using MMIO flips there would disrupt this mechanism.
  7961. */
  7962. if (ring == NULL)
  7963. return true;
  7964. if (INTEL_INFO(ring->dev)->gen < 5)
  7965. return false;
  7966. if (i915.use_mmio_flip < 0)
  7967. return false;
  7968. else if (i915.use_mmio_flip > 0)
  7969. return true;
  7970. else
  7971. return ring != obj->ring;
  7972. }
  7973. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  7974. {
  7975. struct drm_device *dev = intel_crtc->base.dev;
  7976. struct drm_i915_private *dev_priv = dev->dev_private;
  7977. struct intel_framebuffer *intel_fb =
  7978. to_intel_framebuffer(intel_crtc->base.primary->fb);
  7979. struct drm_i915_gem_object *obj = intel_fb->obj;
  7980. u32 dspcntr;
  7981. u32 reg;
  7982. intel_mark_page_flip_active(intel_crtc);
  7983. reg = DSPCNTR(intel_crtc->plane);
  7984. dspcntr = I915_READ(reg);
  7985. if (INTEL_INFO(dev)->gen >= 4) {
  7986. if (obj->tiling_mode != I915_TILING_NONE)
  7987. dspcntr |= DISPPLANE_TILED;
  7988. else
  7989. dspcntr &= ~DISPPLANE_TILED;
  7990. }
  7991. I915_WRITE(reg, dspcntr);
  7992. I915_WRITE(DSPSURF(intel_crtc->plane),
  7993. intel_crtc->unpin_work->gtt_offset);
  7994. POSTING_READ(DSPSURF(intel_crtc->plane));
  7995. }
  7996. static int intel_postpone_flip(struct drm_i915_gem_object *obj)
  7997. {
  7998. struct intel_engine_cs *ring;
  7999. int ret;
  8000. lockdep_assert_held(&obj->base.dev->struct_mutex);
  8001. if (!obj->last_write_seqno)
  8002. return 0;
  8003. ring = obj->ring;
  8004. if (i915_seqno_passed(ring->get_seqno(ring, true),
  8005. obj->last_write_seqno))
  8006. return 0;
  8007. ret = i915_gem_check_olr(ring, obj->last_write_seqno);
  8008. if (ret)
  8009. return ret;
  8010. if (WARN_ON(!ring->irq_get(ring)))
  8011. return 0;
  8012. return 1;
  8013. }
  8014. void intel_notify_mmio_flip(struct intel_engine_cs *ring)
  8015. {
  8016. struct drm_i915_private *dev_priv = to_i915(ring->dev);
  8017. struct intel_crtc *intel_crtc;
  8018. unsigned long irq_flags;
  8019. u32 seqno;
  8020. seqno = ring->get_seqno(ring, false);
  8021. spin_lock_irqsave(&dev_priv->mmio_flip_lock, irq_flags);
  8022. for_each_intel_crtc(ring->dev, intel_crtc) {
  8023. struct intel_mmio_flip *mmio_flip;
  8024. mmio_flip = &intel_crtc->mmio_flip;
  8025. if (mmio_flip->seqno == 0)
  8026. continue;
  8027. if (ring->id != mmio_flip->ring_id)
  8028. continue;
  8029. if (i915_seqno_passed(seqno, mmio_flip->seqno)) {
  8030. intel_do_mmio_flip(intel_crtc);
  8031. mmio_flip->seqno = 0;
  8032. ring->irq_put(ring);
  8033. }
  8034. }
  8035. spin_unlock_irqrestore(&dev_priv->mmio_flip_lock, irq_flags);
  8036. }
  8037. static int intel_queue_mmio_flip(struct drm_device *dev,
  8038. struct drm_crtc *crtc,
  8039. struct drm_framebuffer *fb,
  8040. struct drm_i915_gem_object *obj,
  8041. struct intel_engine_cs *ring,
  8042. uint32_t flags)
  8043. {
  8044. struct drm_i915_private *dev_priv = dev->dev_private;
  8045. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8046. unsigned long irq_flags;
  8047. int ret;
  8048. if (WARN_ON(intel_crtc->mmio_flip.seqno))
  8049. return -EBUSY;
  8050. ret = intel_postpone_flip(obj);
  8051. if (ret < 0)
  8052. return ret;
  8053. if (ret == 0) {
  8054. intel_do_mmio_flip(intel_crtc);
  8055. return 0;
  8056. }
  8057. spin_lock_irqsave(&dev_priv->mmio_flip_lock, irq_flags);
  8058. intel_crtc->mmio_flip.seqno = obj->last_write_seqno;
  8059. intel_crtc->mmio_flip.ring_id = obj->ring->id;
  8060. spin_unlock_irqrestore(&dev_priv->mmio_flip_lock, irq_flags);
  8061. /*
  8062. * Double check to catch cases where irq fired before
  8063. * mmio flip data was ready
  8064. */
  8065. intel_notify_mmio_flip(obj->ring);
  8066. return 0;
  8067. }
  8068. static int intel_default_queue_flip(struct drm_device *dev,
  8069. struct drm_crtc *crtc,
  8070. struct drm_framebuffer *fb,
  8071. struct drm_i915_gem_object *obj,
  8072. struct intel_engine_cs *ring,
  8073. uint32_t flags)
  8074. {
  8075. return -ENODEV;
  8076. }
  8077. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  8078. struct drm_framebuffer *fb,
  8079. struct drm_pending_vblank_event *event,
  8080. uint32_t page_flip_flags)
  8081. {
  8082. struct drm_device *dev = crtc->dev;
  8083. struct drm_i915_private *dev_priv = dev->dev_private;
  8084. struct drm_framebuffer *old_fb = crtc->primary->fb;
  8085. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  8086. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8087. enum pipe pipe = intel_crtc->pipe;
  8088. struct intel_unpin_work *work;
  8089. struct intel_engine_cs *ring;
  8090. unsigned long flags;
  8091. int ret;
  8092. /*
  8093. * drm_mode_page_flip_ioctl() should already catch this, but double
  8094. * check to be safe. In the future we may enable pageflipping from
  8095. * a disabled primary plane.
  8096. */
  8097. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  8098. return -EBUSY;
  8099. /* Can't change pixel format via MI display flips. */
  8100. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  8101. return -EINVAL;
  8102. /*
  8103. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  8104. * Note that pitch changes could also affect these register.
  8105. */
  8106. if (INTEL_INFO(dev)->gen > 3 &&
  8107. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  8108. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  8109. return -EINVAL;
  8110. if (i915_terminally_wedged(&dev_priv->gpu_error))
  8111. goto out_hang;
  8112. work = kzalloc(sizeof(*work), GFP_KERNEL);
  8113. if (work == NULL)
  8114. return -ENOMEM;
  8115. work->event = event;
  8116. work->crtc = crtc;
  8117. work->old_fb_obj = intel_fb_obj(old_fb);
  8118. INIT_WORK(&work->work, intel_unpin_work_fn);
  8119. ret = drm_crtc_vblank_get(crtc);
  8120. if (ret)
  8121. goto free_work;
  8122. /* We borrow the event spin lock for protecting unpin_work */
  8123. spin_lock_irqsave(&dev->event_lock, flags);
  8124. if (intel_crtc->unpin_work) {
  8125. spin_unlock_irqrestore(&dev->event_lock, flags);
  8126. kfree(work);
  8127. drm_crtc_vblank_put(crtc);
  8128. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  8129. return -EBUSY;
  8130. }
  8131. intel_crtc->unpin_work = work;
  8132. spin_unlock_irqrestore(&dev->event_lock, flags);
  8133. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  8134. flush_workqueue(dev_priv->wq);
  8135. ret = i915_mutex_lock_interruptible(dev);
  8136. if (ret)
  8137. goto cleanup;
  8138. /* Reference the objects for the scheduled work. */
  8139. drm_gem_object_reference(&work->old_fb_obj->base);
  8140. drm_gem_object_reference(&obj->base);
  8141. crtc->primary->fb = fb;
  8142. work->pending_flip_obj = obj;
  8143. work->enable_stall_check = true;
  8144. atomic_inc(&intel_crtc->unpin_work_count);
  8145. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  8146. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  8147. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  8148. if (IS_VALLEYVIEW(dev)) {
  8149. ring = &dev_priv->ring[BCS];
  8150. if (obj->tiling_mode != work->old_fb_obj->tiling_mode)
  8151. /* vlv: DISPLAY_FLIP fails to change tiling */
  8152. ring = NULL;
  8153. } else if (IS_IVYBRIDGE(dev)) {
  8154. ring = &dev_priv->ring[BCS];
  8155. } else if (INTEL_INFO(dev)->gen >= 7) {
  8156. ring = obj->ring;
  8157. if (ring == NULL || ring->id != RCS)
  8158. ring = &dev_priv->ring[BCS];
  8159. } else {
  8160. ring = &dev_priv->ring[RCS];
  8161. }
  8162. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  8163. if (ret)
  8164. goto cleanup_pending;
  8165. work->gtt_offset =
  8166. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
  8167. if (use_mmio_flip(ring, obj))
  8168. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  8169. page_flip_flags);
  8170. else
  8171. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
  8172. page_flip_flags);
  8173. if (ret)
  8174. goto cleanup_unpin;
  8175. i915_gem_track_fb(work->old_fb_obj, obj,
  8176. INTEL_FRONTBUFFER_PRIMARY(pipe));
  8177. intel_disable_fbc(dev);
  8178. intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  8179. mutex_unlock(&dev->struct_mutex);
  8180. trace_i915_flip_request(intel_crtc->plane, obj);
  8181. return 0;
  8182. cleanup_unpin:
  8183. intel_unpin_fb_obj(obj);
  8184. cleanup_pending:
  8185. atomic_dec(&intel_crtc->unpin_work_count);
  8186. crtc->primary->fb = old_fb;
  8187. drm_gem_object_unreference(&work->old_fb_obj->base);
  8188. drm_gem_object_unreference(&obj->base);
  8189. mutex_unlock(&dev->struct_mutex);
  8190. cleanup:
  8191. spin_lock_irqsave(&dev->event_lock, flags);
  8192. intel_crtc->unpin_work = NULL;
  8193. spin_unlock_irqrestore(&dev->event_lock, flags);
  8194. drm_crtc_vblank_put(crtc);
  8195. free_work:
  8196. kfree(work);
  8197. if (ret == -EIO) {
  8198. out_hang:
  8199. intel_crtc_wait_for_pending_flips(crtc);
  8200. ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
  8201. if (ret == 0 && event)
  8202. drm_send_vblank_event(dev, pipe, event);
  8203. }
  8204. return ret;
  8205. }
  8206. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  8207. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  8208. .load_lut = intel_crtc_load_lut,
  8209. };
  8210. /**
  8211. * intel_modeset_update_staged_output_state
  8212. *
  8213. * Updates the staged output configuration state, e.g. after we've read out the
  8214. * current hw state.
  8215. */
  8216. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  8217. {
  8218. struct intel_crtc *crtc;
  8219. struct intel_encoder *encoder;
  8220. struct intel_connector *connector;
  8221. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8222. base.head) {
  8223. connector->new_encoder =
  8224. to_intel_encoder(connector->base.encoder);
  8225. }
  8226. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8227. base.head) {
  8228. encoder->new_crtc =
  8229. to_intel_crtc(encoder->base.crtc);
  8230. }
  8231. for_each_intel_crtc(dev, crtc) {
  8232. crtc->new_enabled = crtc->base.enabled;
  8233. if (crtc->new_enabled)
  8234. crtc->new_config = &crtc->config;
  8235. else
  8236. crtc->new_config = NULL;
  8237. }
  8238. }
  8239. /**
  8240. * intel_modeset_commit_output_state
  8241. *
  8242. * This function copies the stage display pipe configuration to the real one.
  8243. */
  8244. static void intel_modeset_commit_output_state(struct drm_device *dev)
  8245. {
  8246. struct intel_crtc *crtc;
  8247. struct intel_encoder *encoder;
  8248. struct intel_connector *connector;
  8249. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8250. base.head) {
  8251. connector->base.encoder = &connector->new_encoder->base;
  8252. }
  8253. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8254. base.head) {
  8255. encoder->base.crtc = &encoder->new_crtc->base;
  8256. }
  8257. for_each_intel_crtc(dev, crtc) {
  8258. crtc->base.enabled = crtc->new_enabled;
  8259. }
  8260. }
  8261. static void
  8262. connected_sink_compute_bpp(struct intel_connector *connector,
  8263. struct intel_crtc_config *pipe_config)
  8264. {
  8265. int bpp = pipe_config->pipe_bpp;
  8266. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8267. connector->base.base.id,
  8268. connector->base.name);
  8269. /* Don't use an invalid EDID bpc value */
  8270. if (connector->base.display_info.bpc &&
  8271. connector->base.display_info.bpc * 3 < bpp) {
  8272. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8273. bpp, connector->base.display_info.bpc*3);
  8274. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  8275. }
  8276. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8277. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  8278. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8279. bpp);
  8280. pipe_config->pipe_bpp = 24;
  8281. }
  8282. }
  8283. static int
  8284. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8285. struct drm_framebuffer *fb,
  8286. struct intel_crtc_config *pipe_config)
  8287. {
  8288. struct drm_device *dev = crtc->base.dev;
  8289. struct intel_connector *connector;
  8290. int bpp;
  8291. switch (fb->pixel_format) {
  8292. case DRM_FORMAT_C8:
  8293. bpp = 8*3; /* since we go through a colormap */
  8294. break;
  8295. case DRM_FORMAT_XRGB1555:
  8296. case DRM_FORMAT_ARGB1555:
  8297. /* checked in intel_framebuffer_init already */
  8298. if (WARN_ON(INTEL_INFO(dev)->gen > 3))
  8299. return -EINVAL;
  8300. case DRM_FORMAT_RGB565:
  8301. bpp = 6*3; /* min is 18bpp */
  8302. break;
  8303. case DRM_FORMAT_XBGR8888:
  8304. case DRM_FORMAT_ABGR8888:
  8305. /* checked in intel_framebuffer_init already */
  8306. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8307. return -EINVAL;
  8308. case DRM_FORMAT_XRGB8888:
  8309. case DRM_FORMAT_ARGB8888:
  8310. bpp = 8*3;
  8311. break;
  8312. case DRM_FORMAT_XRGB2101010:
  8313. case DRM_FORMAT_ARGB2101010:
  8314. case DRM_FORMAT_XBGR2101010:
  8315. case DRM_FORMAT_ABGR2101010:
  8316. /* checked in intel_framebuffer_init already */
  8317. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8318. return -EINVAL;
  8319. bpp = 10*3;
  8320. break;
  8321. /* TODO: gen4+ supports 16 bpc floating point, too. */
  8322. default:
  8323. DRM_DEBUG_KMS("unsupported depth\n");
  8324. return -EINVAL;
  8325. }
  8326. pipe_config->pipe_bpp = bpp;
  8327. /* Clamp display bpp to EDID value */
  8328. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8329. base.head) {
  8330. if (!connector->new_encoder ||
  8331. connector->new_encoder->new_crtc != crtc)
  8332. continue;
  8333. connected_sink_compute_bpp(connector, pipe_config);
  8334. }
  8335. return bpp;
  8336. }
  8337. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  8338. {
  8339. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  8340. "type: 0x%x flags: 0x%x\n",
  8341. mode->crtc_clock,
  8342. mode->crtc_hdisplay, mode->crtc_hsync_start,
  8343. mode->crtc_hsync_end, mode->crtc_htotal,
  8344. mode->crtc_vdisplay, mode->crtc_vsync_start,
  8345. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  8346. }
  8347. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  8348. struct intel_crtc_config *pipe_config,
  8349. const char *context)
  8350. {
  8351. DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
  8352. context, pipe_name(crtc->pipe));
  8353. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  8354. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  8355. pipe_config->pipe_bpp, pipe_config->dither);
  8356. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8357. pipe_config->has_pch_encoder,
  8358. pipe_config->fdi_lanes,
  8359. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  8360. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  8361. pipe_config->fdi_m_n.tu);
  8362. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8363. pipe_config->has_dp_encoder,
  8364. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  8365. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  8366. pipe_config->dp_m_n.tu);
  8367. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  8368. pipe_config->has_dp_encoder,
  8369. pipe_config->dp_m2_n2.gmch_m,
  8370. pipe_config->dp_m2_n2.gmch_n,
  8371. pipe_config->dp_m2_n2.link_m,
  8372. pipe_config->dp_m2_n2.link_n,
  8373. pipe_config->dp_m2_n2.tu);
  8374. DRM_DEBUG_KMS("requested mode:\n");
  8375. drm_mode_debug_printmodeline(&pipe_config->requested_mode);
  8376. DRM_DEBUG_KMS("adjusted mode:\n");
  8377. drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
  8378. intel_dump_crtc_timings(&pipe_config->adjusted_mode);
  8379. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  8380. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  8381. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  8382. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  8383. pipe_config->gmch_pfit.control,
  8384. pipe_config->gmch_pfit.pgm_ratios,
  8385. pipe_config->gmch_pfit.lvds_border_bits);
  8386. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  8387. pipe_config->pch_pfit.pos,
  8388. pipe_config->pch_pfit.size,
  8389. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  8390. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  8391. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  8392. }
  8393. static bool encoders_cloneable(const struct intel_encoder *a,
  8394. const struct intel_encoder *b)
  8395. {
  8396. /* masks could be asymmetric, so check both ways */
  8397. return a == b || (a->cloneable & (1 << b->type) &&
  8398. b->cloneable & (1 << a->type));
  8399. }
  8400. static bool check_single_encoder_cloning(struct intel_crtc *crtc,
  8401. struct intel_encoder *encoder)
  8402. {
  8403. struct drm_device *dev = crtc->base.dev;
  8404. struct intel_encoder *source_encoder;
  8405. list_for_each_entry(source_encoder,
  8406. &dev->mode_config.encoder_list, base.head) {
  8407. if (source_encoder->new_crtc != crtc)
  8408. continue;
  8409. if (!encoders_cloneable(encoder, source_encoder))
  8410. return false;
  8411. }
  8412. return true;
  8413. }
  8414. static bool check_encoder_cloning(struct intel_crtc *crtc)
  8415. {
  8416. struct drm_device *dev = crtc->base.dev;
  8417. struct intel_encoder *encoder;
  8418. list_for_each_entry(encoder,
  8419. &dev->mode_config.encoder_list, base.head) {
  8420. if (encoder->new_crtc != crtc)
  8421. continue;
  8422. if (!check_single_encoder_cloning(crtc, encoder))
  8423. return false;
  8424. }
  8425. return true;
  8426. }
  8427. static struct intel_crtc_config *
  8428. intel_modeset_pipe_config(struct drm_crtc *crtc,
  8429. struct drm_framebuffer *fb,
  8430. struct drm_display_mode *mode)
  8431. {
  8432. struct drm_device *dev = crtc->dev;
  8433. struct intel_encoder *encoder;
  8434. struct intel_crtc_config *pipe_config;
  8435. int plane_bpp, ret = -EINVAL;
  8436. bool retry = true;
  8437. if (!check_encoder_cloning(to_intel_crtc(crtc))) {
  8438. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  8439. return ERR_PTR(-EINVAL);
  8440. }
  8441. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8442. if (!pipe_config)
  8443. return ERR_PTR(-ENOMEM);
  8444. drm_mode_copy(&pipe_config->adjusted_mode, mode);
  8445. drm_mode_copy(&pipe_config->requested_mode, mode);
  8446. pipe_config->cpu_transcoder =
  8447. (enum transcoder) to_intel_crtc(crtc)->pipe;
  8448. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8449. /*
  8450. * Sanitize sync polarity flags based on requested ones. If neither
  8451. * positive or negative polarity is requested, treat this as meaning
  8452. * negative polarity.
  8453. */
  8454. if (!(pipe_config->adjusted_mode.flags &
  8455. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  8456. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  8457. if (!(pipe_config->adjusted_mode.flags &
  8458. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  8459. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  8460. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  8461. * plane pixel format and any sink constraints into account. Returns the
  8462. * source plane bpp so that dithering can be selected on mismatches
  8463. * after encoders and crtc also have had their say. */
  8464. plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  8465. fb, pipe_config);
  8466. if (plane_bpp < 0)
  8467. goto fail;
  8468. /*
  8469. * Determine the real pipe dimensions. Note that stereo modes can
  8470. * increase the actual pipe size due to the frame doubling and
  8471. * insertion of additional space for blanks between the frame. This
  8472. * is stored in the crtc timings. We use the requested mode to do this
  8473. * computation to clearly distinguish it from the adjusted mode, which
  8474. * can be changed by the connectors in the below retry loop.
  8475. */
  8476. drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
  8477. pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
  8478. pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
  8479. encoder_retry:
  8480. /* Ensure the port clock defaults are reset when retrying. */
  8481. pipe_config->port_clock = 0;
  8482. pipe_config->pixel_multiplier = 1;
  8483. /* Fill in default crtc timings, allow encoders to overwrite them. */
  8484. drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
  8485. /* Pass our mode to the connectors and the CRTC to give them a chance to
  8486. * adjust it according to limitations or connector properties, and also
  8487. * a chance to reject the mode entirely.
  8488. */
  8489. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8490. base.head) {
  8491. if (&encoder->new_crtc->base != crtc)
  8492. continue;
  8493. if (!(encoder->compute_config(encoder, pipe_config))) {
  8494. DRM_DEBUG_KMS("Encoder config failure\n");
  8495. goto fail;
  8496. }
  8497. }
  8498. /* Set default port clock if not overwritten by the encoder. Needs to be
  8499. * done afterwards in case the encoder adjusts the mode. */
  8500. if (!pipe_config->port_clock)
  8501. pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
  8502. * pipe_config->pixel_multiplier;
  8503. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  8504. if (ret < 0) {
  8505. DRM_DEBUG_KMS("CRTC fixup failed\n");
  8506. goto fail;
  8507. }
  8508. if (ret == RETRY) {
  8509. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  8510. ret = -EINVAL;
  8511. goto fail;
  8512. }
  8513. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  8514. retry = false;
  8515. goto encoder_retry;
  8516. }
  8517. pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
  8518. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  8519. plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  8520. return pipe_config;
  8521. fail:
  8522. kfree(pipe_config);
  8523. return ERR_PTR(ret);
  8524. }
  8525. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  8526. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  8527. static void
  8528. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  8529. unsigned *prepare_pipes, unsigned *disable_pipes)
  8530. {
  8531. struct intel_crtc *intel_crtc;
  8532. struct drm_device *dev = crtc->dev;
  8533. struct intel_encoder *encoder;
  8534. struct intel_connector *connector;
  8535. struct drm_crtc *tmp_crtc;
  8536. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  8537. /* Check which crtcs have changed outputs connected to them, these need
  8538. * to be part of the prepare_pipes mask. We don't (yet) support global
  8539. * modeset across multiple crtcs, so modeset_pipes will only have one
  8540. * bit set at most. */
  8541. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8542. base.head) {
  8543. if (connector->base.encoder == &connector->new_encoder->base)
  8544. continue;
  8545. if (connector->base.encoder) {
  8546. tmp_crtc = connector->base.encoder->crtc;
  8547. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8548. }
  8549. if (connector->new_encoder)
  8550. *prepare_pipes |=
  8551. 1 << connector->new_encoder->new_crtc->pipe;
  8552. }
  8553. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8554. base.head) {
  8555. if (encoder->base.crtc == &encoder->new_crtc->base)
  8556. continue;
  8557. if (encoder->base.crtc) {
  8558. tmp_crtc = encoder->base.crtc;
  8559. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8560. }
  8561. if (encoder->new_crtc)
  8562. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  8563. }
  8564. /* Check for pipes that will be enabled/disabled ... */
  8565. for_each_intel_crtc(dev, intel_crtc) {
  8566. if (intel_crtc->base.enabled == intel_crtc->new_enabled)
  8567. continue;
  8568. if (!intel_crtc->new_enabled)
  8569. *disable_pipes |= 1 << intel_crtc->pipe;
  8570. else
  8571. *prepare_pipes |= 1 << intel_crtc->pipe;
  8572. }
  8573. /* set_mode is also used to update properties on life display pipes. */
  8574. intel_crtc = to_intel_crtc(crtc);
  8575. if (intel_crtc->new_enabled)
  8576. *prepare_pipes |= 1 << intel_crtc->pipe;
  8577. /*
  8578. * For simplicity do a full modeset on any pipe where the output routing
  8579. * changed. We could be more clever, but that would require us to be
  8580. * more careful with calling the relevant encoder->mode_set functions.
  8581. */
  8582. if (*prepare_pipes)
  8583. *modeset_pipes = *prepare_pipes;
  8584. /* ... and mask these out. */
  8585. *modeset_pipes &= ~(*disable_pipes);
  8586. *prepare_pipes &= ~(*disable_pipes);
  8587. /*
  8588. * HACK: We don't (yet) fully support global modesets. intel_set_config
  8589. * obies this rule, but the modeset restore mode of
  8590. * intel_modeset_setup_hw_state does not.
  8591. */
  8592. *modeset_pipes &= 1 << intel_crtc->pipe;
  8593. *prepare_pipes &= 1 << intel_crtc->pipe;
  8594. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  8595. *modeset_pipes, *prepare_pipes, *disable_pipes);
  8596. }
  8597. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  8598. {
  8599. struct drm_encoder *encoder;
  8600. struct drm_device *dev = crtc->dev;
  8601. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  8602. if (encoder->crtc == crtc)
  8603. return true;
  8604. return false;
  8605. }
  8606. static void
  8607. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  8608. {
  8609. struct intel_encoder *intel_encoder;
  8610. struct intel_crtc *intel_crtc;
  8611. struct drm_connector *connector;
  8612. list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
  8613. base.head) {
  8614. if (!intel_encoder->base.crtc)
  8615. continue;
  8616. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  8617. if (prepare_pipes & (1 << intel_crtc->pipe))
  8618. intel_encoder->connectors_active = false;
  8619. }
  8620. intel_modeset_commit_output_state(dev);
  8621. /* Double check state. */
  8622. for_each_intel_crtc(dev, intel_crtc) {
  8623. WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
  8624. WARN_ON(intel_crtc->new_config &&
  8625. intel_crtc->new_config != &intel_crtc->config);
  8626. WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
  8627. }
  8628. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  8629. if (!connector->encoder || !connector->encoder->crtc)
  8630. continue;
  8631. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  8632. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  8633. struct drm_property *dpms_property =
  8634. dev->mode_config.dpms_property;
  8635. connector->dpms = DRM_MODE_DPMS_ON;
  8636. drm_object_property_set_value(&connector->base,
  8637. dpms_property,
  8638. DRM_MODE_DPMS_ON);
  8639. intel_encoder = to_intel_encoder(connector->encoder);
  8640. intel_encoder->connectors_active = true;
  8641. }
  8642. }
  8643. }
  8644. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  8645. {
  8646. int diff;
  8647. if (clock1 == clock2)
  8648. return true;
  8649. if (!clock1 || !clock2)
  8650. return false;
  8651. diff = abs(clock1 - clock2);
  8652. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  8653. return true;
  8654. return false;
  8655. }
  8656. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  8657. list_for_each_entry((intel_crtc), \
  8658. &(dev)->mode_config.crtc_list, \
  8659. base.head) \
  8660. if (mask & (1 <<(intel_crtc)->pipe))
  8661. static bool
  8662. intel_pipe_config_compare(struct drm_device *dev,
  8663. struct intel_crtc_config *current_config,
  8664. struct intel_crtc_config *pipe_config)
  8665. {
  8666. #define PIPE_CONF_CHECK_X(name) \
  8667. if (current_config->name != pipe_config->name) { \
  8668. DRM_ERROR("mismatch in " #name " " \
  8669. "(expected 0x%08x, found 0x%08x)\n", \
  8670. current_config->name, \
  8671. pipe_config->name); \
  8672. return false; \
  8673. }
  8674. #define PIPE_CONF_CHECK_I(name) \
  8675. if (current_config->name != pipe_config->name) { \
  8676. DRM_ERROR("mismatch in " #name " " \
  8677. "(expected %i, found %i)\n", \
  8678. current_config->name, \
  8679. pipe_config->name); \
  8680. return false; \
  8681. }
  8682. /* This is required for BDW+ where there is only one set of registers for
  8683. * switching between high and low RR.
  8684. * This macro can be used whenever a comparison has to be made between one
  8685. * hw state and multiple sw state variables.
  8686. */
  8687. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  8688. if ((current_config->name != pipe_config->name) && \
  8689. (current_config->alt_name != pipe_config->name)) { \
  8690. DRM_ERROR("mismatch in " #name " " \
  8691. "(expected %i or %i, found %i)\n", \
  8692. current_config->name, \
  8693. current_config->alt_name, \
  8694. pipe_config->name); \
  8695. return false; \
  8696. }
  8697. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  8698. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  8699. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  8700. "(expected %i, found %i)\n", \
  8701. current_config->name & (mask), \
  8702. pipe_config->name & (mask)); \
  8703. return false; \
  8704. }
  8705. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  8706. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  8707. DRM_ERROR("mismatch in " #name " " \
  8708. "(expected %i, found %i)\n", \
  8709. current_config->name, \
  8710. pipe_config->name); \
  8711. return false; \
  8712. }
  8713. #define PIPE_CONF_QUIRK(quirk) \
  8714. ((current_config->quirks | pipe_config->quirks) & (quirk))
  8715. PIPE_CONF_CHECK_I(cpu_transcoder);
  8716. PIPE_CONF_CHECK_I(has_pch_encoder);
  8717. PIPE_CONF_CHECK_I(fdi_lanes);
  8718. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  8719. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  8720. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  8721. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  8722. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  8723. PIPE_CONF_CHECK_I(has_dp_encoder);
  8724. if (INTEL_INFO(dev)->gen < 8) {
  8725. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  8726. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  8727. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  8728. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  8729. PIPE_CONF_CHECK_I(dp_m_n.tu);
  8730. if (current_config->has_drrs) {
  8731. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
  8732. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
  8733. PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
  8734. PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
  8735. PIPE_CONF_CHECK_I(dp_m2_n2.tu);
  8736. }
  8737. } else {
  8738. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
  8739. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
  8740. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
  8741. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
  8742. PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
  8743. }
  8744. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
  8745. PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
  8746. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
  8747. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
  8748. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
  8749. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
  8750. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
  8751. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
  8752. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
  8753. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
  8754. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
  8755. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
  8756. PIPE_CONF_CHECK_I(pixel_multiplier);
  8757. PIPE_CONF_CHECK_I(has_hdmi_sink);
  8758. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  8759. IS_VALLEYVIEW(dev))
  8760. PIPE_CONF_CHECK_I(limited_color_range);
  8761. PIPE_CONF_CHECK_I(has_audio);
  8762. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8763. DRM_MODE_FLAG_INTERLACE);
  8764. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  8765. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8766. DRM_MODE_FLAG_PHSYNC);
  8767. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8768. DRM_MODE_FLAG_NHSYNC);
  8769. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8770. DRM_MODE_FLAG_PVSYNC);
  8771. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8772. DRM_MODE_FLAG_NVSYNC);
  8773. }
  8774. PIPE_CONF_CHECK_I(pipe_src_w);
  8775. PIPE_CONF_CHECK_I(pipe_src_h);
  8776. /*
  8777. * FIXME: BIOS likes to set up a cloned config with lvds+external
  8778. * screen. Since we don't yet re-compute the pipe config when moving
  8779. * just the lvds port away to another pipe the sw tracking won't match.
  8780. *
  8781. * Proper atomic modesets with recomputed global state will fix this.
  8782. * Until then just don't check gmch state for inherited modes.
  8783. */
  8784. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  8785. PIPE_CONF_CHECK_I(gmch_pfit.control);
  8786. /* pfit ratios are autocomputed by the hw on gen4+ */
  8787. if (INTEL_INFO(dev)->gen < 4)
  8788. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  8789. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  8790. }
  8791. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  8792. if (current_config->pch_pfit.enabled) {
  8793. PIPE_CONF_CHECK_I(pch_pfit.pos);
  8794. PIPE_CONF_CHECK_I(pch_pfit.size);
  8795. }
  8796. /* BDW+ don't expose a synchronous way to read the state */
  8797. if (IS_HASWELL(dev))
  8798. PIPE_CONF_CHECK_I(ips_enabled);
  8799. PIPE_CONF_CHECK_I(double_wide);
  8800. PIPE_CONF_CHECK_X(ddi_pll_sel);
  8801. PIPE_CONF_CHECK_I(shared_dpll);
  8802. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  8803. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  8804. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  8805. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  8806. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  8807. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  8808. PIPE_CONF_CHECK_I(pipe_bpp);
  8809. PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
  8810. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  8811. #undef PIPE_CONF_CHECK_X
  8812. #undef PIPE_CONF_CHECK_I
  8813. #undef PIPE_CONF_CHECK_I_ALT
  8814. #undef PIPE_CONF_CHECK_FLAGS
  8815. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  8816. #undef PIPE_CONF_QUIRK
  8817. return true;
  8818. }
  8819. static void
  8820. check_connector_state(struct drm_device *dev)
  8821. {
  8822. struct intel_connector *connector;
  8823. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8824. base.head) {
  8825. /* This also checks the encoder/connector hw state with the
  8826. * ->get_hw_state callbacks. */
  8827. intel_connector_check_state(connector);
  8828. WARN(&connector->new_encoder->base != connector->base.encoder,
  8829. "connector's staged encoder doesn't match current encoder\n");
  8830. }
  8831. }
  8832. static void
  8833. check_encoder_state(struct drm_device *dev)
  8834. {
  8835. struct intel_encoder *encoder;
  8836. struct intel_connector *connector;
  8837. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8838. base.head) {
  8839. bool enabled = false;
  8840. bool active = false;
  8841. enum pipe pipe, tracked_pipe;
  8842. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  8843. encoder->base.base.id,
  8844. encoder->base.name);
  8845. WARN(&encoder->new_crtc->base != encoder->base.crtc,
  8846. "encoder's stage crtc doesn't match current crtc\n");
  8847. WARN(encoder->connectors_active && !encoder->base.crtc,
  8848. "encoder's active_connectors set, but no crtc\n");
  8849. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8850. base.head) {
  8851. if (connector->base.encoder != &encoder->base)
  8852. continue;
  8853. enabled = true;
  8854. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  8855. active = true;
  8856. }
  8857. /*
  8858. * for MST connectors if we unplug the connector is gone
  8859. * away but the encoder is still connected to a crtc
  8860. * until a modeset happens in response to the hotplug.
  8861. */
  8862. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  8863. continue;
  8864. WARN(!!encoder->base.crtc != enabled,
  8865. "encoder's enabled state mismatch "
  8866. "(expected %i, found %i)\n",
  8867. !!encoder->base.crtc, enabled);
  8868. WARN(active && !encoder->base.crtc,
  8869. "active encoder with no crtc\n");
  8870. WARN(encoder->connectors_active != active,
  8871. "encoder's computed active state doesn't match tracked active state "
  8872. "(expected %i, found %i)\n", active, encoder->connectors_active);
  8873. active = encoder->get_hw_state(encoder, &pipe);
  8874. WARN(active != encoder->connectors_active,
  8875. "encoder's hw state doesn't match sw tracking "
  8876. "(expected %i, found %i)\n",
  8877. encoder->connectors_active, active);
  8878. if (!encoder->base.crtc)
  8879. continue;
  8880. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  8881. WARN(active && pipe != tracked_pipe,
  8882. "active encoder's pipe doesn't match"
  8883. "(expected %i, found %i)\n",
  8884. tracked_pipe, pipe);
  8885. }
  8886. }
  8887. static void
  8888. check_crtc_state(struct drm_device *dev)
  8889. {
  8890. struct drm_i915_private *dev_priv = dev->dev_private;
  8891. struct intel_crtc *crtc;
  8892. struct intel_encoder *encoder;
  8893. struct intel_crtc_config pipe_config;
  8894. for_each_intel_crtc(dev, crtc) {
  8895. bool enabled = false;
  8896. bool active = false;
  8897. memset(&pipe_config, 0, sizeof(pipe_config));
  8898. DRM_DEBUG_KMS("[CRTC:%d]\n",
  8899. crtc->base.base.id);
  8900. WARN(crtc->active && !crtc->base.enabled,
  8901. "active crtc, but not enabled in sw tracking\n");
  8902. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8903. base.head) {
  8904. if (encoder->base.crtc != &crtc->base)
  8905. continue;
  8906. enabled = true;
  8907. if (encoder->connectors_active)
  8908. active = true;
  8909. }
  8910. WARN(active != crtc->active,
  8911. "crtc's computed active state doesn't match tracked active state "
  8912. "(expected %i, found %i)\n", active, crtc->active);
  8913. WARN(enabled != crtc->base.enabled,
  8914. "crtc's computed enabled state doesn't match tracked enabled state "
  8915. "(expected %i, found %i)\n", enabled, crtc->base.enabled);
  8916. active = dev_priv->display.get_pipe_config(crtc,
  8917. &pipe_config);
  8918. /* hw state is inconsistent with the pipe A quirk */
  8919. if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
  8920. active = crtc->active;
  8921. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8922. base.head) {
  8923. enum pipe pipe;
  8924. if (encoder->base.crtc != &crtc->base)
  8925. continue;
  8926. if (encoder->get_hw_state(encoder, &pipe))
  8927. encoder->get_config(encoder, &pipe_config);
  8928. }
  8929. WARN(crtc->active != active,
  8930. "crtc active state doesn't match with hw state "
  8931. "(expected %i, found %i)\n", crtc->active, active);
  8932. if (active &&
  8933. !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
  8934. WARN(1, "pipe state doesn't match!\n");
  8935. intel_dump_pipe_config(crtc, &pipe_config,
  8936. "[hw state]");
  8937. intel_dump_pipe_config(crtc, &crtc->config,
  8938. "[sw state]");
  8939. }
  8940. }
  8941. }
  8942. static void
  8943. check_shared_dpll_state(struct drm_device *dev)
  8944. {
  8945. struct drm_i915_private *dev_priv = dev->dev_private;
  8946. struct intel_crtc *crtc;
  8947. struct intel_dpll_hw_state dpll_hw_state;
  8948. int i;
  8949. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  8950. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  8951. int enabled_crtcs = 0, active_crtcs = 0;
  8952. bool active;
  8953. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  8954. DRM_DEBUG_KMS("%s\n", pll->name);
  8955. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  8956. WARN(pll->active > pll->refcount,
  8957. "more active pll users than references: %i vs %i\n",
  8958. pll->active, pll->refcount);
  8959. WARN(pll->active && !pll->on,
  8960. "pll in active use but not on in sw tracking\n");
  8961. WARN(pll->on && !pll->active,
  8962. "pll in on but not on in use in sw tracking\n");
  8963. WARN(pll->on != active,
  8964. "pll on state mismatch (expected %i, found %i)\n",
  8965. pll->on, active);
  8966. for_each_intel_crtc(dev, crtc) {
  8967. if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
  8968. enabled_crtcs++;
  8969. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  8970. active_crtcs++;
  8971. }
  8972. WARN(pll->active != active_crtcs,
  8973. "pll active crtcs mismatch (expected %i, found %i)\n",
  8974. pll->active, active_crtcs);
  8975. WARN(pll->refcount != enabled_crtcs,
  8976. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  8977. pll->refcount, enabled_crtcs);
  8978. WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
  8979. sizeof(dpll_hw_state)),
  8980. "pll hw state mismatch\n");
  8981. }
  8982. }
  8983. void
  8984. intel_modeset_check_state(struct drm_device *dev)
  8985. {
  8986. check_connector_state(dev);
  8987. check_encoder_state(dev);
  8988. check_crtc_state(dev);
  8989. check_shared_dpll_state(dev);
  8990. }
  8991. void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
  8992. int dotclock)
  8993. {
  8994. /*
  8995. * FDI already provided one idea for the dotclock.
  8996. * Yell if the encoder disagrees.
  8997. */
  8998. WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
  8999. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9000. pipe_config->adjusted_mode.crtc_clock, dotclock);
  9001. }
  9002. static void update_scanline_offset(struct intel_crtc *crtc)
  9003. {
  9004. struct drm_device *dev = crtc->base.dev;
  9005. /*
  9006. * The scanline counter increments at the leading edge of hsync.
  9007. *
  9008. * On most platforms it starts counting from vtotal-1 on the
  9009. * first active line. That means the scanline counter value is
  9010. * always one less than what we would expect. Ie. just after
  9011. * start of vblank, which also occurs at start of hsync (on the
  9012. * last active line), the scanline counter will read vblank_start-1.
  9013. *
  9014. * On gen2 the scanline counter starts counting from 1 instead
  9015. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  9016. * to keep the value positive), instead of adding one.
  9017. *
  9018. * On HSW+ the behaviour of the scanline counter depends on the output
  9019. * type. For DP ports it behaves like most other platforms, but on HDMI
  9020. * there's an extra 1 line difference. So we need to add two instead of
  9021. * one to the value.
  9022. */
  9023. if (IS_GEN2(dev)) {
  9024. const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
  9025. int vtotal;
  9026. vtotal = mode->crtc_vtotal;
  9027. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  9028. vtotal /= 2;
  9029. crtc->scanline_offset = vtotal - 1;
  9030. } else if (HAS_DDI(dev) &&
  9031. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI)) {
  9032. crtc->scanline_offset = 2;
  9033. } else
  9034. crtc->scanline_offset = 1;
  9035. }
  9036. static int __intel_set_mode(struct drm_crtc *crtc,
  9037. struct drm_display_mode *mode,
  9038. int x, int y, struct drm_framebuffer *fb)
  9039. {
  9040. struct drm_device *dev = crtc->dev;
  9041. struct drm_i915_private *dev_priv = dev->dev_private;
  9042. struct drm_display_mode *saved_mode;
  9043. struct intel_crtc_config *pipe_config = NULL;
  9044. struct intel_crtc *intel_crtc;
  9045. unsigned disable_pipes, prepare_pipes, modeset_pipes;
  9046. int ret = 0;
  9047. saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
  9048. if (!saved_mode)
  9049. return -ENOMEM;
  9050. intel_modeset_affected_pipes(crtc, &modeset_pipes,
  9051. &prepare_pipes, &disable_pipes);
  9052. *saved_mode = crtc->mode;
  9053. /* Hack: Because we don't (yet) support global modeset on multiple
  9054. * crtcs, we don't keep track of the new mode for more than one crtc.
  9055. * Hence simply check whether any bit is set in modeset_pipes in all the
  9056. * pieces of code that are not yet converted to deal with mutliple crtcs
  9057. * changing their mode at the same time. */
  9058. if (modeset_pipes) {
  9059. pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
  9060. if (IS_ERR(pipe_config)) {
  9061. ret = PTR_ERR(pipe_config);
  9062. pipe_config = NULL;
  9063. goto out;
  9064. }
  9065. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  9066. "[modeset]");
  9067. to_intel_crtc(crtc)->new_config = pipe_config;
  9068. }
  9069. /*
  9070. * See if the config requires any additional preparation, e.g.
  9071. * to adjust global state with pipes off. We need to do this
  9072. * here so we can get the modeset_pipe updated config for the new
  9073. * mode set on this crtc. For other crtcs we need to use the
  9074. * adjusted_mode bits in the crtc directly.
  9075. */
  9076. if (IS_VALLEYVIEW(dev)) {
  9077. valleyview_modeset_global_pipes(dev, &prepare_pipes);
  9078. /* may have added more to prepare_pipes than we should */
  9079. prepare_pipes &= ~disable_pipes;
  9080. }
  9081. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  9082. intel_crtc_disable(&intel_crtc->base);
  9083. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9084. if (intel_crtc->base.enabled)
  9085. dev_priv->display.crtc_disable(&intel_crtc->base);
  9086. }
  9087. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  9088. * to set it here already despite that we pass it down the callchain.
  9089. */
  9090. if (modeset_pipes) {
  9091. crtc->mode = *mode;
  9092. /* mode_set/enable/disable functions rely on a correct pipe
  9093. * config. */
  9094. to_intel_crtc(crtc)->config = *pipe_config;
  9095. to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
  9096. /*
  9097. * Calculate and store various constants which
  9098. * are later needed by vblank and swap-completion
  9099. * timestamping. They are derived from true hwmode.
  9100. */
  9101. drm_calc_timestamping_constants(crtc,
  9102. &pipe_config->adjusted_mode);
  9103. }
  9104. /* Only after disabling all output pipelines that will be changed can we
  9105. * update the the output configuration. */
  9106. intel_modeset_update_state(dev, prepare_pipes);
  9107. if (dev_priv->display.modeset_global_resources)
  9108. dev_priv->display.modeset_global_resources(dev);
  9109. /* Set up the DPLL and any encoders state that needs to adjust or depend
  9110. * on the DPLL.
  9111. */
  9112. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9113. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9114. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
  9115. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9116. mutex_lock(&dev->struct_mutex);
  9117. ret = intel_pin_and_fence_fb_obj(dev,
  9118. obj,
  9119. NULL);
  9120. if (ret != 0) {
  9121. DRM_ERROR("pin & fence failed\n");
  9122. mutex_unlock(&dev->struct_mutex);
  9123. goto done;
  9124. }
  9125. if (old_fb)
  9126. intel_unpin_fb_obj(old_obj);
  9127. i915_gem_track_fb(old_obj, obj,
  9128. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
  9129. mutex_unlock(&dev->struct_mutex);
  9130. crtc->primary->fb = fb;
  9131. crtc->x = x;
  9132. crtc->y = y;
  9133. ret = dev_priv->display.crtc_mode_set(&intel_crtc->base,
  9134. x, y, fb);
  9135. if (ret)
  9136. goto done;
  9137. }
  9138. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  9139. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9140. update_scanline_offset(intel_crtc);
  9141. dev_priv->display.crtc_enable(&intel_crtc->base);
  9142. }
  9143. /* FIXME: add subpixel order */
  9144. done:
  9145. if (ret && crtc->enabled)
  9146. crtc->mode = *saved_mode;
  9147. out:
  9148. kfree(pipe_config);
  9149. kfree(saved_mode);
  9150. return ret;
  9151. }
  9152. static int intel_set_mode(struct drm_crtc *crtc,
  9153. struct drm_display_mode *mode,
  9154. int x, int y, struct drm_framebuffer *fb)
  9155. {
  9156. int ret;
  9157. ret = __intel_set_mode(crtc, mode, x, y, fb);
  9158. if (ret == 0)
  9159. intel_modeset_check_state(crtc->dev);
  9160. return ret;
  9161. }
  9162. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  9163. {
  9164. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
  9165. }
  9166. #undef for_each_intel_crtc_masked
  9167. static void intel_set_config_free(struct intel_set_config *config)
  9168. {
  9169. if (!config)
  9170. return;
  9171. kfree(config->save_connector_encoders);
  9172. kfree(config->save_encoder_crtcs);
  9173. kfree(config->save_crtc_enabled);
  9174. kfree(config);
  9175. }
  9176. static int intel_set_config_save_state(struct drm_device *dev,
  9177. struct intel_set_config *config)
  9178. {
  9179. struct drm_crtc *crtc;
  9180. struct drm_encoder *encoder;
  9181. struct drm_connector *connector;
  9182. int count;
  9183. config->save_crtc_enabled =
  9184. kcalloc(dev->mode_config.num_crtc,
  9185. sizeof(bool), GFP_KERNEL);
  9186. if (!config->save_crtc_enabled)
  9187. return -ENOMEM;
  9188. config->save_encoder_crtcs =
  9189. kcalloc(dev->mode_config.num_encoder,
  9190. sizeof(struct drm_crtc *), GFP_KERNEL);
  9191. if (!config->save_encoder_crtcs)
  9192. return -ENOMEM;
  9193. config->save_connector_encoders =
  9194. kcalloc(dev->mode_config.num_connector,
  9195. sizeof(struct drm_encoder *), GFP_KERNEL);
  9196. if (!config->save_connector_encoders)
  9197. return -ENOMEM;
  9198. /* Copy data. Note that driver private data is not affected.
  9199. * Should anything bad happen only the expected state is
  9200. * restored, not the drivers personal bookkeeping.
  9201. */
  9202. count = 0;
  9203. for_each_crtc(dev, crtc) {
  9204. config->save_crtc_enabled[count++] = crtc->enabled;
  9205. }
  9206. count = 0;
  9207. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  9208. config->save_encoder_crtcs[count++] = encoder->crtc;
  9209. }
  9210. count = 0;
  9211. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  9212. config->save_connector_encoders[count++] = connector->encoder;
  9213. }
  9214. return 0;
  9215. }
  9216. static void intel_set_config_restore_state(struct drm_device *dev,
  9217. struct intel_set_config *config)
  9218. {
  9219. struct intel_crtc *crtc;
  9220. struct intel_encoder *encoder;
  9221. struct intel_connector *connector;
  9222. int count;
  9223. count = 0;
  9224. for_each_intel_crtc(dev, crtc) {
  9225. crtc->new_enabled = config->save_crtc_enabled[count++];
  9226. if (crtc->new_enabled)
  9227. crtc->new_config = &crtc->config;
  9228. else
  9229. crtc->new_config = NULL;
  9230. }
  9231. count = 0;
  9232. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  9233. encoder->new_crtc =
  9234. to_intel_crtc(config->save_encoder_crtcs[count++]);
  9235. }
  9236. count = 0;
  9237. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  9238. connector->new_encoder =
  9239. to_intel_encoder(config->save_connector_encoders[count++]);
  9240. }
  9241. }
  9242. static bool
  9243. is_crtc_connector_off(struct drm_mode_set *set)
  9244. {
  9245. int i;
  9246. if (set->num_connectors == 0)
  9247. return false;
  9248. if (WARN_ON(set->connectors == NULL))
  9249. return false;
  9250. for (i = 0; i < set->num_connectors; i++)
  9251. if (set->connectors[i]->encoder &&
  9252. set->connectors[i]->encoder->crtc == set->crtc &&
  9253. set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
  9254. return true;
  9255. return false;
  9256. }
  9257. static void
  9258. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  9259. struct intel_set_config *config)
  9260. {
  9261. /* We should be able to check here if the fb has the same properties
  9262. * and then just flip_or_move it */
  9263. if (is_crtc_connector_off(set)) {
  9264. config->mode_changed = true;
  9265. } else if (set->crtc->primary->fb != set->fb) {
  9266. /*
  9267. * If we have no fb, we can only flip as long as the crtc is
  9268. * active, otherwise we need a full mode set. The crtc may
  9269. * be active if we've only disabled the primary plane, or
  9270. * in fastboot situations.
  9271. */
  9272. if (set->crtc->primary->fb == NULL) {
  9273. struct intel_crtc *intel_crtc =
  9274. to_intel_crtc(set->crtc);
  9275. if (intel_crtc->active) {
  9276. DRM_DEBUG_KMS("crtc has no fb, will flip\n");
  9277. config->fb_changed = true;
  9278. } else {
  9279. DRM_DEBUG_KMS("inactive crtc, full mode set\n");
  9280. config->mode_changed = true;
  9281. }
  9282. } else if (set->fb == NULL) {
  9283. config->mode_changed = true;
  9284. } else if (set->fb->pixel_format !=
  9285. set->crtc->primary->fb->pixel_format) {
  9286. config->mode_changed = true;
  9287. } else {
  9288. config->fb_changed = true;
  9289. }
  9290. }
  9291. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  9292. config->fb_changed = true;
  9293. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  9294. DRM_DEBUG_KMS("modes are different, full mode set\n");
  9295. drm_mode_debug_printmodeline(&set->crtc->mode);
  9296. drm_mode_debug_printmodeline(set->mode);
  9297. config->mode_changed = true;
  9298. }
  9299. DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
  9300. set->crtc->base.id, config->mode_changed, config->fb_changed);
  9301. }
  9302. static int
  9303. intel_modeset_stage_output_state(struct drm_device *dev,
  9304. struct drm_mode_set *set,
  9305. struct intel_set_config *config)
  9306. {
  9307. struct intel_connector *connector;
  9308. struct intel_encoder *encoder;
  9309. struct intel_crtc *crtc;
  9310. int ro;
  9311. /* The upper layers ensure that we either disable a crtc or have a list
  9312. * of connectors. For paranoia, double-check this. */
  9313. WARN_ON(!set->fb && (set->num_connectors != 0));
  9314. WARN_ON(set->fb && (set->num_connectors == 0));
  9315. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9316. base.head) {
  9317. /* Otherwise traverse passed in connector list and get encoders
  9318. * for them. */
  9319. for (ro = 0; ro < set->num_connectors; ro++) {
  9320. if (set->connectors[ro] == &connector->base) {
  9321. connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
  9322. break;
  9323. }
  9324. }
  9325. /* If we disable the crtc, disable all its connectors. Also, if
  9326. * the connector is on the changing crtc but not on the new
  9327. * connector list, disable it. */
  9328. if ((!set->fb || ro == set->num_connectors) &&
  9329. connector->base.encoder &&
  9330. connector->base.encoder->crtc == set->crtc) {
  9331. connector->new_encoder = NULL;
  9332. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  9333. connector->base.base.id,
  9334. connector->base.name);
  9335. }
  9336. if (&connector->new_encoder->base != connector->base.encoder) {
  9337. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  9338. config->mode_changed = true;
  9339. }
  9340. }
  9341. /* connector->new_encoder is now updated for all connectors. */
  9342. /* Update crtc of enabled connectors. */
  9343. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9344. base.head) {
  9345. struct drm_crtc *new_crtc;
  9346. if (!connector->new_encoder)
  9347. continue;
  9348. new_crtc = connector->new_encoder->base.crtc;
  9349. for (ro = 0; ro < set->num_connectors; ro++) {
  9350. if (set->connectors[ro] == &connector->base)
  9351. new_crtc = set->crtc;
  9352. }
  9353. /* Make sure the new CRTC will work with the encoder */
  9354. if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
  9355. new_crtc)) {
  9356. return -EINVAL;
  9357. }
  9358. connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
  9359. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  9360. connector->base.base.id,
  9361. connector->base.name,
  9362. new_crtc->base.id);
  9363. }
  9364. /* Check for any encoders that needs to be disabled. */
  9365. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  9366. base.head) {
  9367. int num_connectors = 0;
  9368. list_for_each_entry(connector,
  9369. &dev->mode_config.connector_list,
  9370. base.head) {
  9371. if (connector->new_encoder == encoder) {
  9372. WARN_ON(!connector->new_encoder->new_crtc);
  9373. num_connectors++;
  9374. }
  9375. }
  9376. if (num_connectors == 0)
  9377. encoder->new_crtc = NULL;
  9378. else if (num_connectors > 1)
  9379. return -EINVAL;
  9380. /* Only now check for crtc changes so we don't miss encoders
  9381. * that will be disabled. */
  9382. if (&encoder->new_crtc->base != encoder->base.crtc) {
  9383. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  9384. config->mode_changed = true;
  9385. }
  9386. }
  9387. /* Now we've also updated encoder->new_crtc for all encoders. */
  9388. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9389. base.head) {
  9390. if (connector->new_encoder)
  9391. if (connector->new_encoder != connector->encoder)
  9392. connector->encoder = connector->new_encoder;
  9393. }
  9394. for_each_intel_crtc(dev, crtc) {
  9395. crtc->new_enabled = false;
  9396. list_for_each_entry(encoder,
  9397. &dev->mode_config.encoder_list,
  9398. base.head) {
  9399. if (encoder->new_crtc == crtc) {
  9400. crtc->new_enabled = true;
  9401. break;
  9402. }
  9403. }
  9404. if (crtc->new_enabled != crtc->base.enabled) {
  9405. DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
  9406. crtc->new_enabled ? "en" : "dis");
  9407. config->mode_changed = true;
  9408. }
  9409. if (crtc->new_enabled)
  9410. crtc->new_config = &crtc->config;
  9411. else
  9412. crtc->new_config = NULL;
  9413. }
  9414. return 0;
  9415. }
  9416. static void disable_crtc_nofb(struct intel_crtc *crtc)
  9417. {
  9418. struct drm_device *dev = crtc->base.dev;
  9419. struct intel_encoder *encoder;
  9420. struct intel_connector *connector;
  9421. DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
  9422. pipe_name(crtc->pipe));
  9423. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  9424. if (connector->new_encoder &&
  9425. connector->new_encoder->new_crtc == crtc)
  9426. connector->new_encoder = NULL;
  9427. }
  9428. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  9429. if (encoder->new_crtc == crtc)
  9430. encoder->new_crtc = NULL;
  9431. }
  9432. crtc->new_enabled = false;
  9433. crtc->new_config = NULL;
  9434. }
  9435. static int intel_crtc_set_config(struct drm_mode_set *set)
  9436. {
  9437. struct drm_device *dev;
  9438. struct drm_mode_set save_set;
  9439. struct intel_set_config *config;
  9440. int ret;
  9441. BUG_ON(!set);
  9442. BUG_ON(!set->crtc);
  9443. BUG_ON(!set->crtc->helper_private);
  9444. /* Enforce sane interface api - has been abused by the fb helper. */
  9445. BUG_ON(!set->mode && set->fb);
  9446. BUG_ON(set->fb && set->num_connectors == 0);
  9447. if (set->fb) {
  9448. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  9449. set->crtc->base.id, set->fb->base.id,
  9450. (int)set->num_connectors, set->x, set->y);
  9451. } else {
  9452. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  9453. }
  9454. dev = set->crtc->dev;
  9455. ret = -ENOMEM;
  9456. config = kzalloc(sizeof(*config), GFP_KERNEL);
  9457. if (!config)
  9458. goto out_config;
  9459. ret = intel_set_config_save_state(dev, config);
  9460. if (ret)
  9461. goto out_config;
  9462. save_set.crtc = set->crtc;
  9463. save_set.mode = &set->crtc->mode;
  9464. save_set.x = set->crtc->x;
  9465. save_set.y = set->crtc->y;
  9466. save_set.fb = set->crtc->primary->fb;
  9467. /* Compute whether we need a full modeset, only an fb base update or no
  9468. * change at all. In the future we might also check whether only the
  9469. * mode changed, e.g. for LVDS where we only change the panel fitter in
  9470. * such cases. */
  9471. intel_set_config_compute_mode_changes(set, config);
  9472. ret = intel_modeset_stage_output_state(dev, set, config);
  9473. if (ret)
  9474. goto fail;
  9475. if (config->mode_changed) {
  9476. ret = intel_set_mode(set->crtc, set->mode,
  9477. set->x, set->y, set->fb);
  9478. } else if (config->fb_changed) {
  9479. struct drm_i915_private *dev_priv = dev->dev_private;
  9480. struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
  9481. intel_crtc_wait_for_pending_flips(set->crtc);
  9482. ret = intel_pipe_set_base(set->crtc,
  9483. set->x, set->y, set->fb);
  9484. /*
  9485. * We need to make sure the primary plane is re-enabled if it
  9486. * has previously been turned off.
  9487. */
  9488. if (!intel_crtc->primary_enabled && ret == 0) {
  9489. WARN_ON(!intel_crtc->active);
  9490. intel_enable_primary_hw_plane(dev_priv, intel_crtc->plane,
  9491. intel_crtc->pipe);
  9492. }
  9493. /*
  9494. * In the fastboot case this may be our only check of the
  9495. * state after boot. It would be better to only do it on
  9496. * the first update, but we don't have a nice way of doing that
  9497. * (and really, set_config isn't used much for high freq page
  9498. * flipping, so increasing its cost here shouldn't be a big
  9499. * deal).
  9500. */
  9501. if (i915.fastboot && ret == 0)
  9502. intel_modeset_check_state(set->crtc->dev);
  9503. }
  9504. if (ret) {
  9505. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  9506. set->crtc->base.id, ret);
  9507. fail:
  9508. intel_set_config_restore_state(dev, config);
  9509. /*
  9510. * HACK: if the pipe was on, but we didn't have a framebuffer,
  9511. * force the pipe off to avoid oopsing in the modeset code
  9512. * due to fb==NULL. This should only happen during boot since
  9513. * we don't yet reconstruct the FB from the hardware state.
  9514. */
  9515. if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
  9516. disable_crtc_nofb(to_intel_crtc(save_set.crtc));
  9517. /* Try to restore the config */
  9518. if (config->mode_changed &&
  9519. intel_set_mode(save_set.crtc, save_set.mode,
  9520. save_set.x, save_set.y, save_set.fb))
  9521. DRM_ERROR("failed to restore config after modeset failure\n");
  9522. }
  9523. out_config:
  9524. intel_set_config_free(config);
  9525. return ret;
  9526. }
  9527. static const struct drm_crtc_funcs intel_crtc_funcs = {
  9528. .gamma_set = intel_crtc_gamma_set,
  9529. .set_config = intel_crtc_set_config,
  9530. .destroy = intel_crtc_destroy,
  9531. .page_flip = intel_crtc_page_flip,
  9532. };
  9533. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  9534. struct intel_shared_dpll *pll,
  9535. struct intel_dpll_hw_state *hw_state)
  9536. {
  9537. uint32_t val;
  9538. if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_PLLS))
  9539. return false;
  9540. val = I915_READ(PCH_DPLL(pll->id));
  9541. hw_state->dpll = val;
  9542. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  9543. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  9544. return val & DPLL_VCO_ENABLE;
  9545. }
  9546. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  9547. struct intel_shared_dpll *pll)
  9548. {
  9549. I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
  9550. I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
  9551. }
  9552. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  9553. struct intel_shared_dpll *pll)
  9554. {
  9555. /* PCH refclock must be enabled first */
  9556. ibx_assert_pch_refclk_enabled(dev_priv);
  9557. I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
  9558. /* Wait for the clocks to stabilize. */
  9559. POSTING_READ(PCH_DPLL(pll->id));
  9560. udelay(150);
  9561. /* The pixel multiplier can only be updated once the
  9562. * DPLL is enabled and the clocks are stable.
  9563. *
  9564. * So write it again.
  9565. */
  9566. I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
  9567. POSTING_READ(PCH_DPLL(pll->id));
  9568. udelay(200);
  9569. }
  9570. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  9571. struct intel_shared_dpll *pll)
  9572. {
  9573. struct drm_device *dev = dev_priv->dev;
  9574. struct intel_crtc *crtc;
  9575. /* Make sure no transcoder isn't still depending on us. */
  9576. for_each_intel_crtc(dev, crtc) {
  9577. if (intel_crtc_to_shared_dpll(crtc) == pll)
  9578. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  9579. }
  9580. I915_WRITE(PCH_DPLL(pll->id), 0);
  9581. POSTING_READ(PCH_DPLL(pll->id));
  9582. udelay(200);
  9583. }
  9584. static char *ibx_pch_dpll_names[] = {
  9585. "PCH DPLL A",
  9586. "PCH DPLL B",
  9587. };
  9588. static void ibx_pch_dpll_init(struct drm_device *dev)
  9589. {
  9590. struct drm_i915_private *dev_priv = dev->dev_private;
  9591. int i;
  9592. dev_priv->num_shared_dpll = 2;
  9593. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9594. dev_priv->shared_dplls[i].id = i;
  9595. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  9596. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  9597. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  9598. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  9599. dev_priv->shared_dplls[i].get_hw_state =
  9600. ibx_pch_dpll_get_hw_state;
  9601. }
  9602. }
  9603. static void intel_shared_dpll_init(struct drm_device *dev)
  9604. {
  9605. struct drm_i915_private *dev_priv = dev->dev_private;
  9606. if (HAS_DDI(dev))
  9607. intel_ddi_pll_init(dev);
  9608. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  9609. ibx_pch_dpll_init(dev);
  9610. else
  9611. dev_priv->num_shared_dpll = 0;
  9612. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  9613. }
  9614. static int
  9615. intel_primary_plane_disable(struct drm_plane *plane)
  9616. {
  9617. struct drm_device *dev = plane->dev;
  9618. struct drm_i915_private *dev_priv = dev->dev_private;
  9619. struct intel_plane *intel_plane = to_intel_plane(plane);
  9620. struct intel_crtc *intel_crtc;
  9621. if (!plane->fb)
  9622. return 0;
  9623. BUG_ON(!plane->crtc);
  9624. intel_crtc = to_intel_crtc(plane->crtc);
  9625. /*
  9626. * Even though we checked plane->fb above, it's still possible that
  9627. * the primary plane has been implicitly disabled because the crtc
  9628. * coordinates given weren't visible, or because we detected
  9629. * that it was 100% covered by a sprite plane. Or, the CRTC may be
  9630. * off and we've set a fb, but haven't actually turned on the CRTC yet.
  9631. * In either case, we need to unpin the FB and let the fb pointer get
  9632. * updated, but otherwise we don't need to touch the hardware.
  9633. */
  9634. if (!intel_crtc->primary_enabled)
  9635. goto disable_unpin;
  9636. intel_crtc_wait_for_pending_flips(plane->crtc);
  9637. intel_disable_primary_hw_plane(dev_priv, intel_plane->plane,
  9638. intel_plane->pipe);
  9639. disable_unpin:
  9640. mutex_lock(&dev->struct_mutex);
  9641. i915_gem_track_fb(intel_fb_obj(plane->fb), NULL,
  9642. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
  9643. intel_unpin_fb_obj(intel_fb_obj(plane->fb));
  9644. mutex_unlock(&dev->struct_mutex);
  9645. plane->fb = NULL;
  9646. return 0;
  9647. }
  9648. static int
  9649. intel_primary_plane_setplane(struct drm_plane *plane, struct drm_crtc *crtc,
  9650. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  9651. unsigned int crtc_w, unsigned int crtc_h,
  9652. uint32_t src_x, uint32_t src_y,
  9653. uint32_t src_w, uint32_t src_h)
  9654. {
  9655. struct drm_device *dev = crtc->dev;
  9656. struct drm_i915_private *dev_priv = dev->dev_private;
  9657. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9658. struct intel_plane *intel_plane = to_intel_plane(plane);
  9659. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9660. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  9661. struct drm_rect dest = {
  9662. /* integer pixels */
  9663. .x1 = crtc_x,
  9664. .y1 = crtc_y,
  9665. .x2 = crtc_x + crtc_w,
  9666. .y2 = crtc_y + crtc_h,
  9667. };
  9668. struct drm_rect src = {
  9669. /* 16.16 fixed point */
  9670. .x1 = src_x,
  9671. .y1 = src_y,
  9672. .x2 = src_x + src_w,
  9673. .y2 = src_y + src_h,
  9674. };
  9675. const struct drm_rect clip = {
  9676. /* integer pixels */
  9677. .x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0,
  9678. .y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0,
  9679. };
  9680. bool visible;
  9681. int ret;
  9682. ret = drm_plane_helper_check_update(plane, crtc, fb,
  9683. &src, &dest, &clip,
  9684. DRM_PLANE_HELPER_NO_SCALING,
  9685. DRM_PLANE_HELPER_NO_SCALING,
  9686. false, true, &visible);
  9687. if (ret)
  9688. return ret;
  9689. /*
  9690. * If the CRTC isn't enabled, we're just pinning the framebuffer,
  9691. * updating the fb pointer, and returning without touching the
  9692. * hardware. This allows us to later do a drmModeSetCrtc with fb=-1 to
  9693. * turn on the display with all planes setup as desired.
  9694. */
  9695. if (!crtc->enabled) {
  9696. mutex_lock(&dev->struct_mutex);
  9697. /*
  9698. * If we already called setplane while the crtc was disabled,
  9699. * we may have an fb pinned; unpin it.
  9700. */
  9701. if (plane->fb)
  9702. intel_unpin_fb_obj(old_obj);
  9703. i915_gem_track_fb(old_obj, obj,
  9704. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
  9705. /* Pin and return without programming hardware */
  9706. ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
  9707. mutex_unlock(&dev->struct_mutex);
  9708. return ret;
  9709. }
  9710. intel_crtc_wait_for_pending_flips(crtc);
  9711. /*
  9712. * If clipping results in a non-visible primary plane, we'll disable
  9713. * the primary plane. Note that this is a bit different than what
  9714. * happens if userspace explicitly disables the plane by passing fb=0
  9715. * because plane->fb still gets set and pinned.
  9716. */
  9717. if (!visible) {
  9718. mutex_lock(&dev->struct_mutex);
  9719. /*
  9720. * Try to pin the new fb first so that we can bail out if we
  9721. * fail.
  9722. */
  9723. if (plane->fb != fb) {
  9724. ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
  9725. if (ret) {
  9726. mutex_unlock(&dev->struct_mutex);
  9727. return ret;
  9728. }
  9729. }
  9730. i915_gem_track_fb(old_obj, obj,
  9731. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
  9732. if (intel_crtc->primary_enabled)
  9733. intel_disable_primary_hw_plane(dev_priv,
  9734. intel_plane->plane,
  9735. intel_plane->pipe);
  9736. if (plane->fb != fb)
  9737. if (plane->fb)
  9738. intel_unpin_fb_obj(old_obj);
  9739. mutex_unlock(&dev->struct_mutex);
  9740. return 0;
  9741. }
  9742. ret = intel_pipe_set_base(crtc, src.x1, src.y1, fb);
  9743. if (ret)
  9744. return ret;
  9745. if (!intel_crtc->primary_enabled)
  9746. intel_enable_primary_hw_plane(dev_priv, intel_crtc->plane,
  9747. intel_crtc->pipe);
  9748. return 0;
  9749. }
  9750. /* Common destruction function for both primary and cursor planes */
  9751. static void intel_plane_destroy(struct drm_plane *plane)
  9752. {
  9753. struct intel_plane *intel_plane = to_intel_plane(plane);
  9754. drm_plane_cleanup(plane);
  9755. kfree(intel_plane);
  9756. }
  9757. static const struct drm_plane_funcs intel_primary_plane_funcs = {
  9758. .update_plane = intel_primary_plane_setplane,
  9759. .disable_plane = intel_primary_plane_disable,
  9760. .destroy = intel_plane_destroy,
  9761. };
  9762. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  9763. int pipe)
  9764. {
  9765. struct intel_plane *primary;
  9766. const uint32_t *intel_primary_formats;
  9767. int num_formats;
  9768. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  9769. if (primary == NULL)
  9770. return NULL;
  9771. primary->can_scale = false;
  9772. primary->max_downscale = 1;
  9773. primary->pipe = pipe;
  9774. primary->plane = pipe;
  9775. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  9776. primary->plane = !pipe;
  9777. if (INTEL_INFO(dev)->gen <= 3) {
  9778. intel_primary_formats = intel_primary_formats_gen2;
  9779. num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
  9780. } else {
  9781. intel_primary_formats = intel_primary_formats_gen4;
  9782. num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
  9783. }
  9784. drm_universal_plane_init(dev, &primary->base, 0,
  9785. &intel_primary_plane_funcs,
  9786. intel_primary_formats, num_formats,
  9787. DRM_PLANE_TYPE_PRIMARY);
  9788. return &primary->base;
  9789. }
  9790. static int
  9791. intel_cursor_plane_disable(struct drm_plane *plane)
  9792. {
  9793. if (!plane->fb)
  9794. return 0;
  9795. BUG_ON(!plane->crtc);
  9796. return intel_crtc_cursor_set_obj(plane->crtc, NULL, 0, 0);
  9797. }
  9798. static int
  9799. intel_cursor_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
  9800. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  9801. unsigned int crtc_w, unsigned int crtc_h,
  9802. uint32_t src_x, uint32_t src_y,
  9803. uint32_t src_w, uint32_t src_h)
  9804. {
  9805. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9806. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  9807. struct drm_i915_gem_object *obj = intel_fb->obj;
  9808. struct drm_rect dest = {
  9809. /* integer pixels */
  9810. .x1 = crtc_x,
  9811. .y1 = crtc_y,
  9812. .x2 = crtc_x + crtc_w,
  9813. .y2 = crtc_y + crtc_h,
  9814. };
  9815. struct drm_rect src = {
  9816. /* 16.16 fixed point */
  9817. .x1 = src_x,
  9818. .y1 = src_y,
  9819. .x2 = src_x + src_w,
  9820. .y2 = src_y + src_h,
  9821. };
  9822. const struct drm_rect clip = {
  9823. /* integer pixels */
  9824. .x2 = intel_crtc->config.pipe_src_w,
  9825. .y2 = intel_crtc->config.pipe_src_h,
  9826. };
  9827. bool visible;
  9828. int ret;
  9829. ret = drm_plane_helper_check_update(plane, crtc, fb,
  9830. &src, &dest, &clip,
  9831. DRM_PLANE_HELPER_NO_SCALING,
  9832. DRM_PLANE_HELPER_NO_SCALING,
  9833. true, true, &visible);
  9834. if (ret)
  9835. return ret;
  9836. crtc->cursor_x = crtc_x;
  9837. crtc->cursor_y = crtc_y;
  9838. if (fb != crtc->cursor->fb) {
  9839. return intel_crtc_cursor_set_obj(crtc, obj, crtc_w, crtc_h);
  9840. } else {
  9841. intel_crtc_update_cursor(crtc, visible);
  9842. return 0;
  9843. }
  9844. }
  9845. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  9846. .update_plane = intel_cursor_plane_update,
  9847. .disable_plane = intel_cursor_plane_disable,
  9848. .destroy = intel_plane_destroy,
  9849. };
  9850. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  9851. int pipe)
  9852. {
  9853. struct intel_plane *cursor;
  9854. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  9855. if (cursor == NULL)
  9856. return NULL;
  9857. cursor->can_scale = false;
  9858. cursor->max_downscale = 1;
  9859. cursor->pipe = pipe;
  9860. cursor->plane = pipe;
  9861. drm_universal_plane_init(dev, &cursor->base, 0,
  9862. &intel_cursor_plane_funcs,
  9863. intel_cursor_formats,
  9864. ARRAY_SIZE(intel_cursor_formats),
  9865. DRM_PLANE_TYPE_CURSOR);
  9866. return &cursor->base;
  9867. }
  9868. static void intel_crtc_init(struct drm_device *dev, int pipe)
  9869. {
  9870. struct drm_i915_private *dev_priv = dev->dev_private;
  9871. struct intel_crtc *intel_crtc;
  9872. struct drm_plane *primary = NULL;
  9873. struct drm_plane *cursor = NULL;
  9874. int i, ret;
  9875. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  9876. if (intel_crtc == NULL)
  9877. return;
  9878. primary = intel_primary_plane_create(dev, pipe);
  9879. if (!primary)
  9880. goto fail;
  9881. cursor = intel_cursor_plane_create(dev, pipe);
  9882. if (!cursor)
  9883. goto fail;
  9884. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  9885. cursor, &intel_crtc_funcs);
  9886. if (ret)
  9887. goto fail;
  9888. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  9889. for (i = 0; i < 256; i++) {
  9890. intel_crtc->lut_r[i] = i;
  9891. intel_crtc->lut_g[i] = i;
  9892. intel_crtc->lut_b[i] = i;
  9893. }
  9894. /*
  9895. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  9896. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  9897. */
  9898. intel_crtc->pipe = pipe;
  9899. intel_crtc->plane = pipe;
  9900. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  9901. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  9902. intel_crtc->plane = !pipe;
  9903. }
  9904. intel_crtc->cursor_base = ~0;
  9905. intel_crtc->cursor_cntl = ~0;
  9906. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  9907. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  9908. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  9909. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  9910. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  9911. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  9912. return;
  9913. fail:
  9914. if (primary)
  9915. drm_plane_cleanup(primary);
  9916. if (cursor)
  9917. drm_plane_cleanup(cursor);
  9918. kfree(intel_crtc);
  9919. }
  9920. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  9921. {
  9922. struct drm_encoder *encoder = connector->base.encoder;
  9923. struct drm_device *dev = connector->base.dev;
  9924. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  9925. if (!encoder)
  9926. return INVALID_PIPE;
  9927. return to_intel_crtc(encoder->crtc)->pipe;
  9928. }
  9929. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  9930. struct drm_file *file)
  9931. {
  9932. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  9933. struct drm_crtc *drmmode_crtc;
  9934. struct intel_crtc *crtc;
  9935. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  9936. return -ENODEV;
  9937. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  9938. if (!drmmode_crtc) {
  9939. DRM_ERROR("no such CRTC id\n");
  9940. return -ENOENT;
  9941. }
  9942. crtc = to_intel_crtc(drmmode_crtc);
  9943. pipe_from_crtc_id->pipe = crtc->pipe;
  9944. return 0;
  9945. }
  9946. static int intel_encoder_clones(struct intel_encoder *encoder)
  9947. {
  9948. struct drm_device *dev = encoder->base.dev;
  9949. struct intel_encoder *source_encoder;
  9950. int index_mask = 0;
  9951. int entry = 0;
  9952. list_for_each_entry(source_encoder,
  9953. &dev->mode_config.encoder_list, base.head) {
  9954. if (encoders_cloneable(encoder, source_encoder))
  9955. index_mask |= (1 << entry);
  9956. entry++;
  9957. }
  9958. return index_mask;
  9959. }
  9960. static bool has_edp_a(struct drm_device *dev)
  9961. {
  9962. struct drm_i915_private *dev_priv = dev->dev_private;
  9963. if (!IS_MOBILE(dev))
  9964. return false;
  9965. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  9966. return false;
  9967. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  9968. return false;
  9969. return true;
  9970. }
  9971. const char *intel_output_name(int output)
  9972. {
  9973. static const char *names[] = {
  9974. [INTEL_OUTPUT_UNUSED] = "Unused",
  9975. [INTEL_OUTPUT_ANALOG] = "Analog",
  9976. [INTEL_OUTPUT_DVO] = "DVO",
  9977. [INTEL_OUTPUT_SDVO] = "SDVO",
  9978. [INTEL_OUTPUT_LVDS] = "LVDS",
  9979. [INTEL_OUTPUT_TVOUT] = "TV",
  9980. [INTEL_OUTPUT_HDMI] = "HDMI",
  9981. [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
  9982. [INTEL_OUTPUT_EDP] = "eDP",
  9983. [INTEL_OUTPUT_DSI] = "DSI",
  9984. [INTEL_OUTPUT_UNKNOWN] = "Unknown",
  9985. };
  9986. if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
  9987. return "Invalid";
  9988. return names[output];
  9989. }
  9990. static bool intel_crt_present(struct drm_device *dev)
  9991. {
  9992. struct drm_i915_private *dev_priv = dev->dev_private;
  9993. if (IS_ULT(dev))
  9994. return false;
  9995. if (IS_CHERRYVIEW(dev))
  9996. return false;
  9997. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  9998. return false;
  9999. return true;
  10000. }
  10001. static void intel_setup_outputs(struct drm_device *dev)
  10002. {
  10003. struct drm_i915_private *dev_priv = dev->dev_private;
  10004. struct intel_encoder *encoder;
  10005. bool dpd_is_edp = false;
  10006. intel_lvds_init(dev);
  10007. if (intel_crt_present(dev))
  10008. intel_crt_init(dev);
  10009. if (HAS_DDI(dev)) {
  10010. int found;
  10011. /* Haswell uses DDI functions to detect digital outputs */
  10012. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  10013. /* DDI A only supports eDP */
  10014. if (found)
  10015. intel_ddi_init(dev, PORT_A);
  10016. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  10017. * register */
  10018. found = I915_READ(SFUSE_STRAP);
  10019. if (found & SFUSE_STRAP_DDIB_DETECTED)
  10020. intel_ddi_init(dev, PORT_B);
  10021. if (found & SFUSE_STRAP_DDIC_DETECTED)
  10022. intel_ddi_init(dev, PORT_C);
  10023. if (found & SFUSE_STRAP_DDID_DETECTED)
  10024. intel_ddi_init(dev, PORT_D);
  10025. } else if (HAS_PCH_SPLIT(dev)) {
  10026. int found;
  10027. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  10028. if (has_edp_a(dev))
  10029. intel_dp_init(dev, DP_A, PORT_A);
  10030. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  10031. /* PCH SDVOB multiplex with HDMIB */
  10032. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  10033. if (!found)
  10034. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  10035. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  10036. intel_dp_init(dev, PCH_DP_B, PORT_B);
  10037. }
  10038. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  10039. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  10040. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  10041. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  10042. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  10043. intel_dp_init(dev, PCH_DP_C, PORT_C);
  10044. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  10045. intel_dp_init(dev, PCH_DP_D, PORT_D);
  10046. } else if (IS_VALLEYVIEW(dev)) {
  10047. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
  10048. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  10049. PORT_B);
  10050. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
  10051. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  10052. }
  10053. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
  10054. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  10055. PORT_C);
  10056. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
  10057. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  10058. }
  10059. if (IS_CHERRYVIEW(dev)) {
  10060. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED) {
  10061. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  10062. PORT_D);
  10063. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  10064. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  10065. }
  10066. }
  10067. intel_dsi_init(dev);
  10068. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  10069. bool found = false;
  10070. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10071. DRM_DEBUG_KMS("probing SDVOB\n");
  10072. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  10073. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  10074. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  10075. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  10076. }
  10077. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  10078. intel_dp_init(dev, DP_B, PORT_B);
  10079. }
  10080. /* Before G4X SDVOC doesn't have its own detect register */
  10081. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10082. DRM_DEBUG_KMS("probing SDVOC\n");
  10083. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  10084. }
  10085. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  10086. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  10087. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  10088. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  10089. }
  10090. if (SUPPORTS_INTEGRATED_DP(dev))
  10091. intel_dp_init(dev, DP_C, PORT_C);
  10092. }
  10093. if (SUPPORTS_INTEGRATED_DP(dev) &&
  10094. (I915_READ(DP_D) & DP_DETECTED))
  10095. intel_dp_init(dev, DP_D, PORT_D);
  10096. } else if (IS_GEN2(dev))
  10097. intel_dvo_init(dev);
  10098. if (SUPPORTS_TV(dev))
  10099. intel_tv_init(dev);
  10100. intel_edp_psr_init(dev);
  10101. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  10102. encoder->base.possible_crtcs = encoder->crtc_mask;
  10103. encoder->base.possible_clones =
  10104. intel_encoder_clones(encoder);
  10105. }
  10106. intel_init_pch_refclk(dev);
  10107. drm_helper_move_panel_connectors_to_head(dev);
  10108. }
  10109. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  10110. {
  10111. struct drm_device *dev = fb->dev;
  10112. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10113. drm_framebuffer_cleanup(fb);
  10114. mutex_lock(&dev->struct_mutex);
  10115. WARN_ON(!intel_fb->obj->framebuffer_references--);
  10116. drm_gem_object_unreference(&intel_fb->obj->base);
  10117. mutex_unlock(&dev->struct_mutex);
  10118. kfree(intel_fb);
  10119. }
  10120. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  10121. struct drm_file *file,
  10122. unsigned int *handle)
  10123. {
  10124. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10125. struct drm_i915_gem_object *obj = intel_fb->obj;
  10126. return drm_gem_handle_create(file, &obj->base, handle);
  10127. }
  10128. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  10129. .destroy = intel_user_framebuffer_destroy,
  10130. .create_handle = intel_user_framebuffer_create_handle,
  10131. };
  10132. static int intel_framebuffer_init(struct drm_device *dev,
  10133. struct intel_framebuffer *intel_fb,
  10134. struct drm_mode_fb_cmd2 *mode_cmd,
  10135. struct drm_i915_gem_object *obj)
  10136. {
  10137. int aligned_height;
  10138. int pitch_limit;
  10139. int ret;
  10140. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  10141. if (obj->tiling_mode == I915_TILING_Y) {
  10142. DRM_DEBUG("hardware does not support tiling Y\n");
  10143. return -EINVAL;
  10144. }
  10145. if (mode_cmd->pitches[0] & 63) {
  10146. DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
  10147. mode_cmd->pitches[0]);
  10148. return -EINVAL;
  10149. }
  10150. if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
  10151. pitch_limit = 32*1024;
  10152. } else if (INTEL_INFO(dev)->gen >= 4) {
  10153. if (obj->tiling_mode)
  10154. pitch_limit = 16*1024;
  10155. else
  10156. pitch_limit = 32*1024;
  10157. } else if (INTEL_INFO(dev)->gen >= 3) {
  10158. if (obj->tiling_mode)
  10159. pitch_limit = 8*1024;
  10160. else
  10161. pitch_limit = 16*1024;
  10162. } else
  10163. /* XXX DSPC is limited to 4k tiled */
  10164. pitch_limit = 8*1024;
  10165. if (mode_cmd->pitches[0] > pitch_limit) {
  10166. DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
  10167. obj->tiling_mode ? "tiled" : "linear",
  10168. mode_cmd->pitches[0], pitch_limit);
  10169. return -EINVAL;
  10170. }
  10171. if (obj->tiling_mode != I915_TILING_NONE &&
  10172. mode_cmd->pitches[0] != obj->stride) {
  10173. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  10174. mode_cmd->pitches[0], obj->stride);
  10175. return -EINVAL;
  10176. }
  10177. /* Reject formats not supported by any plane early. */
  10178. switch (mode_cmd->pixel_format) {
  10179. case DRM_FORMAT_C8:
  10180. case DRM_FORMAT_RGB565:
  10181. case DRM_FORMAT_XRGB8888:
  10182. case DRM_FORMAT_ARGB8888:
  10183. break;
  10184. case DRM_FORMAT_XRGB1555:
  10185. case DRM_FORMAT_ARGB1555:
  10186. if (INTEL_INFO(dev)->gen > 3) {
  10187. DRM_DEBUG("unsupported pixel format: %s\n",
  10188. drm_get_format_name(mode_cmd->pixel_format));
  10189. return -EINVAL;
  10190. }
  10191. break;
  10192. case DRM_FORMAT_XBGR8888:
  10193. case DRM_FORMAT_ABGR8888:
  10194. case DRM_FORMAT_XRGB2101010:
  10195. case DRM_FORMAT_ARGB2101010:
  10196. case DRM_FORMAT_XBGR2101010:
  10197. case DRM_FORMAT_ABGR2101010:
  10198. if (INTEL_INFO(dev)->gen < 4) {
  10199. DRM_DEBUG("unsupported pixel format: %s\n",
  10200. drm_get_format_name(mode_cmd->pixel_format));
  10201. return -EINVAL;
  10202. }
  10203. break;
  10204. case DRM_FORMAT_YUYV:
  10205. case DRM_FORMAT_UYVY:
  10206. case DRM_FORMAT_YVYU:
  10207. case DRM_FORMAT_VYUY:
  10208. if (INTEL_INFO(dev)->gen < 5) {
  10209. DRM_DEBUG("unsupported pixel format: %s\n",
  10210. drm_get_format_name(mode_cmd->pixel_format));
  10211. return -EINVAL;
  10212. }
  10213. break;
  10214. default:
  10215. DRM_DEBUG("unsupported pixel format: %s\n",
  10216. drm_get_format_name(mode_cmd->pixel_format));
  10217. return -EINVAL;
  10218. }
  10219. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  10220. if (mode_cmd->offsets[0] != 0)
  10221. return -EINVAL;
  10222. aligned_height = intel_align_height(dev, mode_cmd->height,
  10223. obj->tiling_mode);
  10224. /* FIXME drm helper for size checks (especially planar formats)? */
  10225. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  10226. return -EINVAL;
  10227. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  10228. intel_fb->obj = obj;
  10229. intel_fb->obj->framebuffer_references++;
  10230. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  10231. if (ret) {
  10232. DRM_ERROR("framebuffer init failed %d\n", ret);
  10233. return ret;
  10234. }
  10235. return 0;
  10236. }
  10237. static struct drm_framebuffer *
  10238. intel_user_framebuffer_create(struct drm_device *dev,
  10239. struct drm_file *filp,
  10240. struct drm_mode_fb_cmd2 *mode_cmd)
  10241. {
  10242. struct drm_i915_gem_object *obj;
  10243. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  10244. mode_cmd->handles[0]));
  10245. if (&obj->base == NULL)
  10246. return ERR_PTR(-ENOENT);
  10247. return intel_framebuffer_create(dev, mode_cmd, obj);
  10248. }
  10249. #ifndef CONFIG_DRM_I915_FBDEV
  10250. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  10251. {
  10252. }
  10253. #endif
  10254. static const struct drm_mode_config_funcs intel_mode_funcs = {
  10255. .fb_create = intel_user_framebuffer_create,
  10256. .output_poll_changed = intel_fbdev_output_poll_changed,
  10257. };
  10258. /* Set up chip specific display functions */
  10259. static void intel_init_display(struct drm_device *dev)
  10260. {
  10261. struct drm_i915_private *dev_priv = dev->dev_private;
  10262. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  10263. dev_priv->display.find_dpll = g4x_find_best_dpll;
  10264. else if (IS_CHERRYVIEW(dev))
  10265. dev_priv->display.find_dpll = chv_find_best_dpll;
  10266. else if (IS_VALLEYVIEW(dev))
  10267. dev_priv->display.find_dpll = vlv_find_best_dpll;
  10268. else if (IS_PINEVIEW(dev))
  10269. dev_priv->display.find_dpll = pnv_find_best_dpll;
  10270. else
  10271. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  10272. if (HAS_DDI(dev)) {
  10273. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  10274. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  10275. dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
  10276. dev_priv->display.crtc_enable = haswell_crtc_enable;
  10277. dev_priv->display.crtc_disable = haswell_crtc_disable;
  10278. dev_priv->display.off = ironlake_crtc_off;
  10279. dev_priv->display.update_primary_plane =
  10280. ironlake_update_primary_plane;
  10281. } else if (HAS_PCH_SPLIT(dev)) {
  10282. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  10283. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  10284. dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
  10285. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  10286. dev_priv->display.crtc_disable = ironlake_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 (IS_VALLEYVIEW(dev)) {
  10291. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10292. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  10293. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  10294. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  10295. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10296. dev_priv->display.off = i9xx_crtc_off;
  10297. dev_priv->display.update_primary_plane =
  10298. i9xx_update_primary_plane;
  10299. } else {
  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 = i9xx_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. }
  10309. /* Returns the core display clock speed */
  10310. if (IS_VALLEYVIEW(dev))
  10311. dev_priv->display.get_display_clock_speed =
  10312. valleyview_get_display_clock_speed;
  10313. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  10314. dev_priv->display.get_display_clock_speed =
  10315. i945_get_display_clock_speed;
  10316. else if (IS_I915G(dev))
  10317. dev_priv->display.get_display_clock_speed =
  10318. i915_get_display_clock_speed;
  10319. else if (IS_I945GM(dev) || IS_845G(dev))
  10320. dev_priv->display.get_display_clock_speed =
  10321. i9xx_misc_get_display_clock_speed;
  10322. else if (IS_PINEVIEW(dev))
  10323. dev_priv->display.get_display_clock_speed =
  10324. pnv_get_display_clock_speed;
  10325. else if (IS_I915GM(dev))
  10326. dev_priv->display.get_display_clock_speed =
  10327. i915gm_get_display_clock_speed;
  10328. else if (IS_I865G(dev))
  10329. dev_priv->display.get_display_clock_speed =
  10330. i865_get_display_clock_speed;
  10331. else if (IS_I85X(dev))
  10332. dev_priv->display.get_display_clock_speed =
  10333. i855_get_display_clock_speed;
  10334. else /* 852, 830 */
  10335. dev_priv->display.get_display_clock_speed =
  10336. i830_get_display_clock_speed;
  10337. if (HAS_PCH_SPLIT(dev)) {
  10338. if (IS_GEN5(dev)) {
  10339. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  10340. dev_priv->display.write_eld = ironlake_write_eld;
  10341. } else if (IS_GEN6(dev)) {
  10342. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  10343. dev_priv->display.write_eld = ironlake_write_eld;
  10344. dev_priv->display.modeset_global_resources =
  10345. snb_modeset_global_resources;
  10346. } else if (IS_IVYBRIDGE(dev)) {
  10347. /* FIXME: detect B0+ stepping and use auto training */
  10348. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  10349. dev_priv->display.write_eld = ironlake_write_eld;
  10350. dev_priv->display.modeset_global_resources =
  10351. ivb_modeset_global_resources;
  10352. } else if (IS_HASWELL(dev) || IS_GEN8(dev)) {
  10353. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  10354. dev_priv->display.write_eld = haswell_write_eld;
  10355. dev_priv->display.modeset_global_resources =
  10356. haswell_modeset_global_resources;
  10357. }
  10358. } else if (IS_G4X(dev)) {
  10359. dev_priv->display.write_eld = g4x_write_eld;
  10360. } else if (IS_VALLEYVIEW(dev)) {
  10361. dev_priv->display.modeset_global_resources =
  10362. valleyview_modeset_global_resources;
  10363. dev_priv->display.write_eld = ironlake_write_eld;
  10364. }
  10365. /* Default just returns -ENODEV to indicate unsupported */
  10366. dev_priv->display.queue_flip = intel_default_queue_flip;
  10367. switch (INTEL_INFO(dev)->gen) {
  10368. case 2:
  10369. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  10370. break;
  10371. case 3:
  10372. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  10373. break;
  10374. case 4:
  10375. case 5:
  10376. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  10377. break;
  10378. case 6:
  10379. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  10380. break;
  10381. case 7:
  10382. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  10383. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  10384. break;
  10385. }
  10386. intel_panel_init_backlight_funcs(dev);
  10387. }
  10388. /*
  10389. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  10390. * resume, or other times. This quirk makes sure that's the case for
  10391. * affected systems.
  10392. */
  10393. static void quirk_pipea_force(struct drm_device *dev)
  10394. {
  10395. struct drm_i915_private *dev_priv = dev->dev_private;
  10396. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  10397. DRM_INFO("applying pipe a force quirk\n");
  10398. }
  10399. /*
  10400. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  10401. */
  10402. static void quirk_ssc_force_disable(struct drm_device *dev)
  10403. {
  10404. struct drm_i915_private *dev_priv = dev->dev_private;
  10405. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  10406. DRM_INFO("applying lvds SSC disable quirk\n");
  10407. }
  10408. /*
  10409. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  10410. * brightness value
  10411. */
  10412. static void quirk_invert_brightness(struct drm_device *dev)
  10413. {
  10414. struct drm_i915_private *dev_priv = dev->dev_private;
  10415. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  10416. DRM_INFO("applying inverted panel brightness quirk\n");
  10417. }
  10418. /* Some VBT's incorrectly indicate no backlight is present */
  10419. static void quirk_backlight_present(struct drm_device *dev)
  10420. {
  10421. struct drm_i915_private *dev_priv = dev->dev_private;
  10422. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  10423. DRM_INFO("applying backlight present quirk\n");
  10424. }
  10425. struct intel_quirk {
  10426. int device;
  10427. int subsystem_vendor;
  10428. int subsystem_device;
  10429. void (*hook)(struct drm_device *dev);
  10430. };
  10431. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  10432. struct intel_dmi_quirk {
  10433. void (*hook)(struct drm_device *dev);
  10434. const struct dmi_system_id (*dmi_id_list)[];
  10435. };
  10436. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  10437. {
  10438. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  10439. return 1;
  10440. }
  10441. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  10442. {
  10443. .dmi_id_list = &(const struct dmi_system_id[]) {
  10444. {
  10445. .callback = intel_dmi_reverse_brightness,
  10446. .ident = "NCR Corporation",
  10447. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  10448. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  10449. },
  10450. },
  10451. { } /* terminating entry */
  10452. },
  10453. .hook = quirk_invert_brightness,
  10454. },
  10455. };
  10456. static struct intel_quirk intel_quirks[] = {
  10457. /* HP Mini needs pipe A force quirk (LP: #322104) */
  10458. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  10459. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  10460. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  10461. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  10462. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  10463. /* Lenovo U160 cannot use SSC on LVDS */
  10464. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  10465. /* Sony Vaio Y cannot use SSC on LVDS */
  10466. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  10467. /* Acer Aspire 5734Z must invert backlight brightness */
  10468. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  10469. /* Acer/eMachines G725 */
  10470. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  10471. /* Acer/eMachines e725 */
  10472. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  10473. /* Acer/Packard Bell NCL20 */
  10474. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  10475. /* Acer Aspire 4736Z */
  10476. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  10477. /* Acer Aspire 5336 */
  10478. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  10479. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  10480. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  10481. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  10482. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  10483. /* HP Chromebook 14 (Celeron 2955U) */
  10484. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  10485. };
  10486. static void intel_init_quirks(struct drm_device *dev)
  10487. {
  10488. struct pci_dev *d = dev->pdev;
  10489. int i;
  10490. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  10491. struct intel_quirk *q = &intel_quirks[i];
  10492. if (d->device == q->device &&
  10493. (d->subsystem_vendor == q->subsystem_vendor ||
  10494. q->subsystem_vendor == PCI_ANY_ID) &&
  10495. (d->subsystem_device == q->subsystem_device ||
  10496. q->subsystem_device == PCI_ANY_ID))
  10497. q->hook(dev);
  10498. }
  10499. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  10500. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  10501. intel_dmi_quirks[i].hook(dev);
  10502. }
  10503. }
  10504. /* Disable the VGA plane that we never use */
  10505. static void i915_disable_vga(struct drm_device *dev)
  10506. {
  10507. struct drm_i915_private *dev_priv = dev->dev_private;
  10508. u8 sr1;
  10509. u32 vga_reg = i915_vgacntrl_reg(dev);
  10510. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  10511. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  10512. outb(SR01, VGA_SR_INDEX);
  10513. sr1 = inb(VGA_SR_DATA);
  10514. outb(sr1 | 1<<5, VGA_SR_DATA);
  10515. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  10516. udelay(300);
  10517. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  10518. POSTING_READ(vga_reg);
  10519. }
  10520. void intel_modeset_init_hw(struct drm_device *dev)
  10521. {
  10522. intel_prepare_ddi(dev);
  10523. if (IS_VALLEYVIEW(dev))
  10524. vlv_update_cdclk(dev);
  10525. intel_init_clock_gating(dev);
  10526. intel_enable_gt_powersave(dev);
  10527. }
  10528. void intel_modeset_suspend_hw(struct drm_device *dev)
  10529. {
  10530. intel_suspend_hw(dev);
  10531. }
  10532. void intel_modeset_init(struct drm_device *dev)
  10533. {
  10534. struct drm_i915_private *dev_priv = dev->dev_private;
  10535. int sprite, ret;
  10536. enum pipe pipe;
  10537. struct intel_crtc *crtc;
  10538. drm_mode_config_init(dev);
  10539. dev->mode_config.min_width = 0;
  10540. dev->mode_config.min_height = 0;
  10541. dev->mode_config.preferred_depth = 24;
  10542. dev->mode_config.prefer_shadow = 1;
  10543. dev->mode_config.funcs = &intel_mode_funcs;
  10544. intel_init_quirks(dev);
  10545. intel_init_pm(dev);
  10546. if (INTEL_INFO(dev)->num_pipes == 0)
  10547. return;
  10548. intel_init_display(dev);
  10549. if (IS_GEN2(dev)) {
  10550. dev->mode_config.max_width = 2048;
  10551. dev->mode_config.max_height = 2048;
  10552. } else if (IS_GEN3(dev)) {
  10553. dev->mode_config.max_width = 4096;
  10554. dev->mode_config.max_height = 4096;
  10555. } else {
  10556. dev->mode_config.max_width = 8192;
  10557. dev->mode_config.max_height = 8192;
  10558. }
  10559. if (IS_GEN2(dev)) {
  10560. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  10561. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  10562. } else {
  10563. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  10564. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  10565. }
  10566. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  10567. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  10568. INTEL_INFO(dev)->num_pipes,
  10569. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  10570. for_each_pipe(pipe) {
  10571. intel_crtc_init(dev, pipe);
  10572. for_each_sprite(pipe, sprite) {
  10573. ret = intel_plane_init(dev, pipe, sprite);
  10574. if (ret)
  10575. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  10576. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  10577. }
  10578. }
  10579. intel_init_dpio(dev);
  10580. intel_shared_dpll_init(dev);
  10581. /* Just disable it once at startup */
  10582. i915_disable_vga(dev);
  10583. intel_setup_outputs(dev);
  10584. /* Just in case the BIOS is doing something questionable. */
  10585. intel_disable_fbc(dev);
  10586. drm_modeset_lock_all(dev);
  10587. intel_modeset_setup_hw_state(dev, false);
  10588. drm_modeset_unlock_all(dev);
  10589. for_each_intel_crtc(dev, crtc) {
  10590. if (!crtc->active)
  10591. continue;
  10592. /*
  10593. * Note that reserving the BIOS fb up front prevents us
  10594. * from stuffing other stolen allocations like the ring
  10595. * on top. This prevents some ugliness at boot time, and
  10596. * can even allow for smooth boot transitions if the BIOS
  10597. * fb is large enough for the active pipe configuration.
  10598. */
  10599. if (dev_priv->display.get_plane_config) {
  10600. dev_priv->display.get_plane_config(crtc,
  10601. &crtc->plane_config);
  10602. /*
  10603. * If the fb is shared between multiple heads, we'll
  10604. * just get the first one.
  10605. */
  10606. intel_find_plane_obj(crtc, &crtc->plane_config);
  10607. }
  10608. }
  10609. }
  10610. static void intel_enable_pipe_a(struct drm_device *dev)
  10611. {
  10612. struct intel_connector *connector;
  10613. struct drm_connector *crt = NULL;
  10614. struct intel_load_detect_pipe load_detect_temp;
  10615. struct drm_modeset_acquire_ctx ctx;
  10616. /* We can't just switch on the pipe A, we need to set things up with a
  10617. * proper mode and output configuration. As a gross hack, enable pipe A
  10618. * by enabling the load detect pipe once. */
  10619. list_for_each_entry(connector,
  10620. &dev->mode_config.connector_list,
  10621. base.head) {
  10622. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  10623. crt = &connector->base;
  10624. break;
  10625. }
  10626. }
  10627. if (!crt)
  10628. return;
  10629. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, &ctx))
  10630. intel_release_load_detect_pipe(crt, &load_detect_temp, &ctx);
  10631. }
  10632. static bool
  10633. intel_check_plane_mapping(struct intel_crtc *crtc)
  10634. {
  10635. struct drm_device *dev = crtc->base.dev;
  10636. struct drm_i915_private *dev_priv = dev->dev_private;
  10637. u32 reg, val;
  10638. if (INTEL_INFO(dev)->num_pipes == 1)
  10639. return true;
  10640. reg = DSPCNTR(!crtc->plane);
  10641. val = I915_READ(reg);
  10642. if ((val & DISPLAY_PLANE_ENABLE) &&
  10643. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  10644. return false;
  10645. return true;
  10646. }
  10647. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  10648. {
  10649. struct drm_device *dev = crtc->base.dev;
  10650. struct drm_i915_private *dev_priv = dev->dev_private;
  10651. u32 reg;
  10652. /* Clear any frame start delays used for debugging left by the BIOS */
  10653. reg = PIPECONF(crtc->config.cpu_transcoder);
  10654. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  10655. /* restore vblank interrupts to correct state */
  10656. if (crtc->active)
  10657. drm_vblank_on(dev, crtc->pipe);
  10658. else
  10659. drm_vblank_off(dev, crtc->pipe);
  10660. /* We need to sanitize the plane -> pipe mapping first because this will
  10661. * disable the crtc (and hence change the state) if it is wrong. Note
  10662. * that gen4+ has a fixed plane -> pipe mapping. */
  10663. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  10664. struct intel_connector *connector;
  10665. bool plane;
  10666. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  10667. crtc->base.base.id);
  10668. /* Pipe has the wrong plane attached and the plane is active.
  10669. * Temporarily change the plane mapping and disable everything
  10670. * ... */
  10671. plane = crtc->plane;
  10672. crtc->plane = !plane;
  10673. crtc->primary_enabled = true;
  10674. dev_priv->display.crtc_disable(&crtc->base);
  10675. crtc->plane = plane;
  10676. /* ... and break all links. */
  10677. list_for_each_entry(connector, &dev->mode_config.connector_list,
  10678. base.head) {
  10679. if (connector->encoder->base.crtc != &crtc->base)
  10680. continue;
  10681. connector->base.dpms = DRM_MODE_DPMS_OFF;
  10682. connector->base.encoder = NULL;
  10683. }
  10684. /* multiple connectors may have the same encoder:
  10685. * handle them and break crtc link separately */
  10686. list_for_each_entry(connector, &dev->mode_config.connector_list,
  10687. base.head)
  10688. if (connector->encoder->base.crtc == &crtc->base) {
  10689. connector->encoder->base.crtc = NULL;
  10690. connector->encoder->connectors_active = false;
  10691. }
  10692. WARN_ON(crtc->active);
  10693. crtc->base.enabled = false;
  10694. }
  10695. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  10696. crtc->pipe == PIPE_A && !crtc->active) {
  10697. /* BIOS forgot to enable pipe A, this mostly happens after
  10698. * resume. Force-enable the pipe to fix this, the update_dpms
  10699. * call below we restore the pipe to the right state, but leave
  10700. * the required bits on. */
  10701. intel_enable_pipe_a(dev);
  10702. }
  10703. /* Adjust the state of the output pipe according to whether we
  10704. * have active connectors/encoders. */
  10705. intel_crtc_update_dpms(&crtc->base);
  10706. if (crtc->active != crtc->base.enabled) {
  10707. struct intel_encoder *encoder;
  10708. /* This can happen either due to bugs in the get_hw_state
  10709. * functions or because the pipe is force-enabled due to the
  10710. * pipe A quirk. */
  10711. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  10712. crtc->base.base.id,
  10713. crtc->base.enabled ? "enabled" : "disabled",
  10714. crtc->active ? "enabled" : "disabled");
  10715. crtc->base.enabled = crtc->active;
  10716. /* Because we only establish the connector -> encoder ->
  10717. * crtc links if something is active, this means the
  10718. * crtc is now deactivated. Break the links. connector
  10719. * -> encoder links are only establish when things are
  10720. * actually up, hence no need to break them. */
  10721. WARN_ON(crtc->active);
  10722. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  10723. WARN_ON(encoder->connectors_active);
  10724. encoder->base.crtc = NULL;
  10725. }
  10726. }
  10727. if (crtc->active || IS_VALLEYVIEW(dev) || INTEL_INFO(dev)->gen < 5) {
  10728. /*
  10729. * We start out with underrun reporting disabled to avoid races.
  10730. * For correct bookkeeping mark this on active crtcs.
  10731. *
  10732. * Also on gmch platforms we dont have any hardware bits to
  10733. * disable the underrun reporting. Which means we need to start
  10734. * out with underrun reporting disabled also on inactive pipes,
  10735. * since otherwise we'll complain about the garbage we read when
  10736. * e.g. coming up after runtime pm.
  10737. *
  10738. * No protection against concurrent access is required - at
  10739. * worst a fifo underrun happens which also sets this to false.
  10740. */
  10741. crtc->cpu_fifo_underrun_disabled = true;
  10742. crtc->pch_fifo_underrun_disabled = true;
  10743. update_scanline_offset(crtc);
  10744. }
  10745. }
  10746. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  10747. {
  10748. struct intel_connector *connector;
  10749. struct drm_device *dev = encoder->base.dev;
  10750. /* We need to check both for a crtc link (meaning that the
  10751. * encoder is active and trying to read from a pipe) and the
  10752. * pipe itself being active. */
  10753. bool has_active_crtc = encoder->base.crtc &&
  10754. to_intel_crtc(encoder->base.crtc)->active;
  10755. if (encoder->connectors_active && !has_active_crtc) {
  10756. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  10757. encoder->base.base.id,
  10758. encoder->base.name);
  10759. /* Connector is active, but has no active pipe. This is
  10760. * fallout from our resume register restoring. Disable
  10761. * the encoder manually again. */
  10762. if (encoder->base.crtc) {
  10763. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  10764. encoder->base.base.id,
  10765. encoder->base.name);
  10766. encoder->disable(encoder);
  10767. if (encoder->post_disable)
  10768. encoder->post_disable(encoder);
  10769. }
  10770. encoder->base.crtc = NULL;
  10771. encoder->connectors_active = false;
  10772. /* Inconsistent output/port/pipe state happens presumably due to
  10773. * a bug in one of the get_hw_state functions. Or someplace else
  10774. * in our code, like the register restore mess on resume. Clamp
  10775. * things to off as a safer default. */
  10776. list_for_each_entry(connector,
  10777. &dev->mode_config.connector_list,
  10778. base.head) {
  10779. if (connector->encoder != encoder)
  10780. continue;
  10781. connector->base.dpms = DRM_MODE_DPMS_OFF;
  10782. connector->base.encoder = NULL;
  10783. }
  10784. }
  10785. /* Enabled encoders without active connectors will be fixed in
  10786. * the crtc fixup. */
  10787. }
  10788. void i915_redisable_vga_power_on(struct drm_device *dev)
  10789. {
  10790. struct drm_i915_private *dev_priv = dev->dev_private;
  10791. u32 vga_reg = i915_vgacntrl_reg(dev);
  10792. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  10793. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  10794. i915_disable_vga(dev);
  10795. }
  10796. }
  10797. void i915_redisable_vga(struct drm_device *dev)
  10798. {
  10799. struct drm_i915_private *dev_priv = dev->dev_private;
  10800. /* This function can be called both from intel_modeset_setup_hw_state or
  10801. * at a very early point in our resume sequence, where the power well
  10802. * structures are not yet restored. Since this function is at a very
  10803. * paranoid "someone might have enabled VGA while we were not looking"
  10804. * level, just check if the power well is enabled instead of trying to
  10805. * follow the "don't touch the power well if we don't need it" policy
  10806. * the rest of the driver uses. */
  10807. if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_VGA))
  10808. return;
  10809. i915_redisable_vga_power_on(dev);
  10810. }
  10811. static bool primary_get_hw_state(struct intel_crtc *crtc)
  10812. {
  10813. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  10814. if (!crtc->active)
  10815. return false;
  10816. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  10817. }
  10818. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  10819. {
  10820. struct drm_i915_private *dev_priv = dev->dev_private;
  10821. enum pipe pipe;
  10822. struct intel_crtc *crtc;
  10823. struct intel_encoder *encoder;
  10824. struct intel_connector *connector;
  10825. int i;
  10826. for_each_intel_crtc(dev, crtc) {
  10827. memset(&crtc->config, 0, sizeof(crtc->config));
  10828. crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  10829. crtc->active = dev_priv->display.get_pipe_config(crtc,
  10830. &crtc->config);
  10831. crtc->base.enabled = crtc->active;
  10832. crtc->primary_enabled = primary_get_hw_state(crtc);
  10833. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  10834. crtc->base.base.id,
  10835. crtc->active ? "enabled" : "disabled");
  10836. }
  10837. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10838. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10839. pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
  10840. pll->active = 0;
  10841. for_each_intel_crtc(dev, crtc) {
  10842. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10843. pll->active++;
  10844. }
  10845. pll->refcount = pll->active;
  10846. DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
  10847. pll->name, pll->refcount, pll->on);
  10848. if (pll->refcount)
  10849. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  10850. }
  10851. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  10852. base.head) {
  10853. pipe = 0;
  10854. if (encoder->get_hw_state(encoder, &pipe)) {
  10855. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  10856. encoder->base.crtc = &crtc->base;
  10857. encoder->get_config(encoder, &crtc->config);
  10858. } else {
  10859. encoder->base.crtc = NULL;
  10860. }
  10861. encoder->connectors_active = false;
  10862. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  10863. encoder->base.base.id,
  10864. encoder->base.name,
  10865. encoder->base.crtc ? "enabled" : "disabled",
  10866. pipe_name(pipe));
  10867. }
  10868. list_for_each_entry(connector, &dev->mode_config.connector_list,
  10869. base.head) {
  10870. if (connector->get_hw_state(connector)) {
  10871. connector->base.dpms = DRM_MODE_DPMS_ON;
  10872. connector->encoder->connectors_active = true;
  10873. connector->base.encoder = &connector->encoder->base;
  10874. } else {
  10875. connector->base.dpms = DRM_MODE_DPMS_OFF;
  10876. connector->base.encoder = NULL;
  10877. }
  10878. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  10879. connector->base.base.id,
  10880. connector->base.name,
  10881. connector->base.encoder ? "enabled" : "disabled");
  10882. }
  10883. }
  10884. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  10885. * and i915 state tracking structures. */
  10886. void intel_modeset_setup_hw_state(struct drm_device *dev,
  10887. bool force_restore)
  10888. {
  10889. struct drm_i915_private *dev_priv = dev->dev_private;
  10890. enum pipe pipe;
  10891. struct intel_crtc *crtc;
  10892. struct intel_encoder *encoder;
  10893. int i;
  10894. intel_modeset_readout_hw_state(dev);
  10895. /*
  10896. * Now that we have the config, copy it to each CRTC struct
  10897. * Note that this could go away if we move to using crtc_config
  10898. * checking everywhere.
  10899. */
  10900. for_each_intel_crtc(dev, crtc) {
  10901. if (crtc->active && i915.fastboot) {
  10902. intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
  10903. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  10904. crtc->base.base.id);
  10905. drm_mode_debug_printmodeline(&crtc->base.mode);
  10906. }
  10907. }
  10908. /* HW state is read out, now we need to sanitize this mess. */
  10909. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  10910. base.head) {
  10911. intel_sanitize_encoder(encoder);
  10912. }
  10913. for_each_pipe(pipe) {
  10914. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  10915. intel_sanitize_crtc(crtc);
  10916. intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
  10917. }
  10918. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10919. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10920. if (!pll->on || pll->active)
  10921. continue;
  10922. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  10923. pll->disable(dev_priv, pll);
  10924. pll->on = false;
  10925. }
  10926. if (HAS_PCH_SPLIT(dev))
  10927. ilk_wm_get_hw_state(dev);
  10928. if (force_restore) {
  10929. i915_redisable_vga(dev);
  10930. /*
  10931. * We need to use raw interfaces for restoring state to avoid
  10932. * checking (bogus) intermediate states.
  10933. */
  10934. for_each_pipe(pipe) {
  10935. struct drm_crtc *crtc =
  10936. dev_priv->pipe_to_crtc_mapping[pipe];
  10937. __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
  10938. crtc->primary->fb);
  10939. }
  10940. } else {
  10941. intel_modeset_update_staged_output_state(dev);
  10942. }
  10943. intel_modeset_check_state(dev);
  10944. }
  10945. void intel_modeset_gem_init(struct drm_device *dev)
  10946. {
  10947. struct drm_crtc *c;
  10948. struct drm_i915_gem_object *obj;
  10949. mutex_lock(&dev->struct_mutex);
  10950. intel_init_gt_powersave(dev);
  10951. mutex_unlock(&dev->struct_mutex);
  10952. intel_modeset_init_hw(dev);
  10953. intel_setup_overlay(dev);
  10954. /*
  10955. * Make sure any fbs we allocated at startup are properly
  10956. * pinned & fenced. When we do the allocation it's too early
  10957. * for this.
  10958. */
  10959. mutex_lock(&dev->struct_mutex);
  10960. for_each_crtc(dev, c) {
  10961. obj = intel_fb_obj(c->primary->fb);
  10962. if (obj == NULL)
  10963. continue;
  10964. if (intel_pin_and_fence_fb_obj(dev, obj, NULL)) {
  10965. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  10966. to_intel_crtc(c)->pipe);
  10967. drm_framebuffer_unreference(c->primary->fb);
  10968. c->primary->fb = NULL;
  10969. }
  10970. }
  10971. mutex_unlock(&dev->struct_mutex);
  10972. }
  10973. void intel_connector_unregister(struct intel_connector *intel_connector)
  10974. {
  10975. struct drm_connector *connector = &intel_connector->base;
  10976. intel_panel_destroy_backlight(connector);
  10977. drm_connector_unregister(connector);
  10978. }
  10979. void intel_modeset_cleanup(struct drm_device *dev)
  10980. {
  10981. struct drm_i915_private *dev_priv = dev->dev_private;
  10982. struct drm_connector *connector;
  10983. /*
  10984. * Interrupts and polling as the first thing to avoid creating havoc.
  10985. * Too much stuff here (turning of rps, connectors, ...) would
  10986. * experience fancy races otherwise.
  10987. */
  10988. drm_irq_uninstall(dev);
  10989. cancel_work_sync(&dev_priv->hotplug_work);
  10990. dev_priv->pm._irqs_disabled = true;
  10991. /*
  10992. * Due to the hpd irq storm handling the hotplug work can re-arm the
  10993. * poll handlers. Hence disable polling after hpd handling is shut down.
  10994. */
  10995. drm_kms_helper_poll_fini(dev);
  10996. mutex_lock(&dev->struct_mutex);
  10997. intel_unregister_dsm_handler();
  10998. intel_disable_fbc(dev);
  10999. intel_disable_gt_powersave(dev);
  11000. ironlake_teardown_rc6(dev);
  11001. mutex_unlock(&dev->struct_mutex);
  11002. /* flush any delayed tasks or pending work */
  11003. flush_scheduled_work();
  11004. /* destroy the backlight and sysfs files before encoders/connectors */
  11005. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  11006. struct intel_connector *intel_connector;
  11007. intel_connector = to_intel_connector(connector);
  11008. intel_connector->unregister(intel_connector);
  11009. }
  11010. drm_mode_config_cleanup(dev);
  11011. intel_cleanup_overlay(dev);
  11012. mutex_lock(&dev->struct_mutex);
  11013. intel_cleanup_gt_powersave(dev);
  11014. mutex_unlock(&dev->struct_mutex);
  11015. }
  11016. /*
  11017. * Return which encoder is currently attached for connector.
  11018. */
  11019. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  11020. {
  11021. return &intel_attached_encoder(connector)->base;
  11022. }
  11023. void intel_connector_attach_encoder(struct intel_connector *connector,
  11024. struct intel_encoder *encoder)
  11025. {
  11026. connector->encoder = encoder;
  11027. drm_mode_connector_attach_encoder(&connector->base,
  11028. &encoder->base);
  11029. }
  11030. /*
  11031. * set vga decode state - true == enable VGA decode
  11032. */
  11033. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  11034. {
  11035. struct drm_i915_private *dev_priv = dev->dev_private;
  11036. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  11037. u16 gmch_ctrl;
  11038. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  11039. DRM_ERROR("failed to read control word\n");
  11040. return -EIO;
  11041. }
  11042. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  11043. return 0;
  11044. if (state)
  11045. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  11046. else
  11047. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  11048. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  11049. DRM_ERROR("failed to write control word\n");
  11050. return -EIO;
  11051. }
  11052. return 0;
  11053. }
  11054. struct intel_display_error_state {
  11055. u32 power_well_driver;
  11056. int num_transcoders;
  11057. struct intel_cursor_error_state {
  11058. u32 control;
  11059. u32 position;
  11060. u32 base;
  11061. u32 size;
  11062. } cursor[I915_MAX_PIPES];
  11063. struct intel_pipe_error_state {
  11064. bool power_domain_on;
  11065. u32 source;
  11066. u32 stat;
  11067. } pipe[I915_MAX_PIPES];
  11068. struct intel_plane_error_state {
  11069. u32 control;
  11070. u32 stride;
  11071. u32 size;
  11072. u32 pos;
  11073. u32 addr;
  11074. u32 surface;
  11075. u32 tile_offset;
  11076. } plane[I915_MAX_PIPES];
  11077. struct intel_transcoder_error_state {
  11078. bool power_domain_on;
  11079. enum transcoder cpu_transcoder;
  11080. u32 conf;
  11081. u32 htotal;
  11082. u32 hblank;
  11083. u32 hsync;
  11084. u32 vtotal;
  11085. u32 vblank;
  11086. u32 vsync;
  11087. } transcoder[4];
  11088. };
  11089. struct intel_display_error_state *
  11090. intel_display_capture_error_state(struct drm_device *dev)
  11091. {
  11092. struct drm_i915_private *dev_priv = dev->dev_private;
  11093. struct intel_display_error_state *error;
  11094. int transcoders[] = {
  11095. TRANSCODER_A,
  11096. TRANSCODER_B,
  11097. TRANSCODER_C,
  11098. TRANSCODER_EDP,
  11099. };
  11100. int i;
  11101. if (INTEL_INFO(dev)->num_pipes == 0)
  11102. return NULL;
  11103. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  11104. if (error == NULL)
  11105. return NULL;
  11106. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11107. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  11108. for_each_pipe(i) {
  11109. error->pipe[i].power_domain_on =
  11110. intel_display_power_enabled_unlocked(dev_priv,
  11111. POWER_DOMAIN_PIPE(i));
  11112. if (!error->pipe[i].power_domain_on)
  11113. continue;
  11114. error->cursor[i].control = I915_READ(CURCNTR(i));
  11115. error->cursor[i].position = I915_READ(CURPOS(i));
  11116. error->cursor[i].base = I915_READ(CURBASE(i));
  11117. error->plane[i].control = I915_READ(DSPCNTR(i));
  11118. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  11119. if (INTEL_INFO(dev)->gen <= 3) {
  11120. error->plane[i].size = I915_READ(DSPSIZE(i));
  11121. error->plane[i].pos = I915_READ(DSPPOS(i));
  11122. }
  11123. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11124. error->plane[i].addr = I915_READ(DSPADDR(i));
  11125. if (INTEL_INFO(dev)->gen >= 4) {
  11126. error->plane[i].surface = I915_READ(DSPSURF(i));
  11127. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  11128. }
  11129. error->pipe[i].source = I915_READ(PIPESRC(i));
  11130. if (HAS_GMCH_DISPLAY(dev))
  11131. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  11132. }
  11133. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  11134. if (HAS_DDI(dev_priv->dev))
  11135. error->num_transcoders++; /* Account for eDP. */
  11136. for (i = 0; i < error->num_transcoders; i++) {
  11137. enum transcoder cpu_transcoder = transcoders[i];
  11138. error->transcoder[i].power_domain_on =
  11139. intel_display_power_enabled_unlocked(dev_priv,
  11140. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  11141. if (!error->transcoder[i].power_domain_on)
  11142. continue;
  11143. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  11144. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  11145. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  11146. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  11147. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  11148. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  11149. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  11150. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  11151. }
  11152. return error;
  11153. }
  11154. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  11155. void
  11156. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  11157. struct drm_device *dev,
  11158. struct intel_display_error_state *error)
  11159. {
  11160. int i;
  11161. if (!error)
  11162. return;
  11163. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  11164. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11165. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  11166. error->power_well_driver);
  11167. for_each_pipe(i) {
  11168. err_printf(m, "Pipe [%d]:\n", i);
  11169. err_printf(m, " Power: %s\n",
  11170. error->pipe[i].power_domain_on ? "on" : "off");
  11171. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  11172. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  11173. err_printf(m, "Plane [%d]:\n", i);
  11174. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  11175. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  11176. if (INTEL_INFO(dev)->gen <= 3) {
  11177. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  11178. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  11179. }
  11180. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11181. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  11182. if (INTEL_INFO(dev)->gen >= 4) {
  11183. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  11184. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  11185. }
  11186. err_printf(m, "Cursor [%d]:\n", i);
  11187. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  11188. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  11189. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  11190. }
  11191. for (i = 0; i < error->num_transcoders; i++) {
  11192. err_printf(m, "CPU transcoder: %c\n",
  11193. transcoder_name(error->transcoder[i].cpu_transcoder));
  11194. err_printf(m, " Power: %s\n",
  11195. error->transcoder[i].power_domain_on ? "on" : "off");
  11196. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  11197. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  11198. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  11199. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  11200. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  11201. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  11202. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  11203. }
  11204. }