intel_display.c 372 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include <drm/i915_drm.h>
  37. #include "i915_drv.h"
  38. #include "i915_trace.h"
  39. #include <drm/drm_dp_helper.h>
  40. #include <drm/drm_crtc_helper.h>
  41. #include <drm/drm_plane_helper.h>
  42. #include <drm/drm_rect.h>
  43. #include <linux/dma_remapping.h>
  44. /* Primary plane formats supported by all gen */
  45. #define COMMON_PRIMARY_FORMATS \
  46. DRM_FORMAT_C8, \
  47. DRM_FORMAT_RGB565, \
  48. DRM_FORMAT_XRGB8888, \
  49. DRM_FORMAT_ARGB8888
  50. /* Primary plane formats for gen <= 3 */
  51. static const uint32_t intel_primary_formats_gen2[] = {
  52. COMMON_PRIMARY_FORMATS,
  53. DRM_FORMAT_XRGB1555,
  54. DRM_FORMAT_ARGB1555,
  55. };
  56. /* Primary plane formats for gen >= 4 */
  57. static const uint32_t intel_primary_formats_gen4[] = {
  58. COMMON_PRIMARY_FORMATS, \
  59. DRM_FORMAT_XBGR8888,
  60. DRM_FORMAT_ABGR8888,
  61. DRM_FORMAT_XRGB2101010,
  62. DRM_FORMAT_ARGB2101010,
  63. DRM_FORMAT_XBGR2101010,
  64. DRM_FORMAT_ABGR2101010,
  65. };
  66. /* Cursor formats */
  67. static const uint32_t intel_cursor_formats[] = {
  68. DRM_FORMAT_ARGB8888,
  69. };
  70. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  71. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  72. struct intel_crtc_config *pipe_config);
  73. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  74. struct intel_crtc_config *pipe_config);
  75. static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
  76. int x, int y, struct drm_framebuffer *old_fb);
  77. static int intel_framebuffer_init(struct drm_device *dev,
  78. struct intel_framebuffer *ifb,
  79. struct drm_mode_fb_cmd2 *mode_cmd,
  80. struct drm_i915_gem_object *obj);
  81. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  82. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  83. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  84. struct intel_link_m_n *m_n,
  85. struct intel_link_m_n *m2_n2);
  86. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  87. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  88. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  89. static void vlv_prepare_pll(struct intel_crtc *crtc,
  90. const struct intel_crtc_config *pipe_config);
  91. static void chv_prepare_pll(struct intel_crtc *crtc,
  92. const struct intel_crtc_config *pipe_config);
  93. static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
  94. {
  95. if (!connector->mst_port)
  96. return connector->encoder;
  97. else
  98. return &connector->mst_port->mst_encoders[pipe]->base;
  99. }
  100. typedef struct {
  101. int min, max;
  102. } intel_range_t;
  103. typedef struct {
  104. int dot_limit;
  105. int p2_slow, p2_fast;
  106. } intel_p2_t;
  107. typedef struct intel_limit intel_limit_t;
  108. struct intel_limit {
  109. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  110. intel_p2_t p2;
  111. };
  112. int
  113. intel_pch_rawclk(struct drm_device *dev)
  114. {
  115. struct drm_i915_private *dev_priv = dev->dev_private;
  116. WARN_ON(!HAS_PCH_SPLIT(dev));
  117. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  118. }
  119. static inline u32 /* units of 100MHz */
  120. intel_fdi_link_freq(struct drm_device *dev)
  121. {
  122. if (IS_GEN5(dev)) {
  123. struct drm_i915_private *dev_priv = dev->dev_private;
  124. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  125. } else
  126. return 27;
  127. }
  128. static const intel_limit_t intel_limits_i8xx_dac = {
  129. .dot = { .min = 25000, .max = 350000 },
  130. .vco = { .min = 908000, .max = 1512000 },
  131. .n = { .min = 2, .max = 16 },
  132. .m = { .min = 96, .max = 140 },
  133. .m1 = { .min = 18, .max = 26 },
  134. .m2 = { .min = 6, .max = 16 },
  135. .p = { .min = 4, .max = 128 },
  136. .p1 = { .min = 2, .max = 33 },
  137. .p2 = { .dot_limit = 165000,
  138. .p2_slow = 4, .p2_fast = 2 },
  139. };
  140. static const intel_limit_t intel_limits_i8xx_dvo = {
  141. .dot = { .min = 25000, .max = 350000 },
  142. .vco = { .min = 908000, .max = 1512000 },
  143. .n = { .min = 2, .max = 16 },
  144. .m = { .min = 96, .max = 140 },
  145. .m1 = { .min = 18, .max = 26 },
  146. .m2 = { .min = 6, .max = 16 },
  147. .p = { .min = 4, .max = 128 },
  148. .p1 = { .min = 2, .max = 33 },
  149. .p2 = { .dot_limit = 165000,
  150. .p2_slow = 4, .p2_fast = 4 },
  151. };
  152. static const intel_limit_t intel_limits_i8xx_lvds = {
  153. .dot = { .min = 25000, .max = 350000 },
  154. .vco = { .min = 908000, .max = 1512000 },
  155. .n = { .min = 2, .max = 16 },
  156. .m = { .min = 96, .max = 140 },
  157. .m1 = { .min = 18, .max = 26 },
  158. .m2 = { .min = 6, .max = 16 },
  159. .p = { .min = 4, .max = 128 },
  160. .p1 = { .min = 1, .max = 6 },
  161. .p2 = { .dot_limit = 165000,
  162. .p2_slow = 14, .p2_fast = 7 },
  163. };
  164. static const intel_limit_t intel_limits_i9xx_sdvo = {
  165. .dot = { .min = 20000, .max = 400000 },
  166. .vco = { .min = 1400000, .max = 2800000 },
  167. .n = { .min = 1, .max = 6 },
  168. .m = { .min = 70, .max = 120 },
  169. .m1 = { .min = 8, .max = 18 },
  170. .m2 = { .min = 3, .max = 7 },
  171. .p = { .min = 5, .max = 80 },
  172. .p1 = { .min = 1, .max = 8 },
  173. .p2 = { .dot_limit = 200000,
  174. .p2_slow = 10, .p2_fast = 5 },
  175. };
  176. static const intel_limit_t intel_limits_i9xx_lvds = {
  177. .dot = { .min = 20000, .max = 400000 },
  178. .vco = { .min = 1400000, .max = 2800000 },
  179. .n = { .min = 1, .max = 6 },
  180. .m = { .min = 70, .max = 120 },
  181. .m1 = { .min = 8, .max = 18 },
  182. .m2 = { .min = 3, .max = 7 },
  183. .p = { .min = 7, .max = 98 },
  184. .p1 = { .min = 1, .max = 8 },
  185. .p2 = { .dot_limit = 112000,
  186. .p2_slow = 14, .p2_fast = 7 },
  187. };
  188. static const intel_limit_t intel_limits_g4x_sdvo = {
  189. .dot = { .min = 25000, .max = 270000 },
  190. .vco = { .min = 1750000, .max = 3500000},
  191. .n = { .min = 1, .max = 4 },
  192. .m = { .min = 104, .max = 138 },
  193. .m1 = { .min = 17, .max = 23 },
  194. .m2 = { .min = 5, .max = 11 },
  195. .p = { .min = 10, .max = 30 },
  196. .p1 = { .min = 1, .max = 3},
  197. .p2 = { .dot_limit = 270000,
  198. .p2_slow = 10,
  199. .p2_fast = 10
  200. },
  201. };
  202. static const intel_limit_t intel_limits_g4x_hdmi = {
  203. .dot = { .min = 22000, .max = 400000 },
  204. .vco = { .min = 1750000, .max = 3500000},
  205. .n = { .min = 1, .max = 4 },
  206. .m = { .min = 104, .max = 138 },
  207. .m1 = { .min = 16, .max = 23 },
  208. .m2 = { .min = 5, .max = 11 },
  209. .p = { .min = 5, .max = 80 },
  210. .p1 = { .min = 1, .max = 8},
  211. .p2 = { .dot_limit = 165000,
  212. .p2_slow = 10, .p2_fast = 5 },
  213. };
  214. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  215. .dot = { .min = 20000, .max = 115000 },
  216. .vco = { .min = 1750000, .max = 3500000 },
  217. .n = { .min = 1, .max = 3 },
  218. .m = { .min = 104, .max = 138 },
  219. .m1 = { .min = 17, .max = 23 },
  220. .m2 = { .min = 5, .max = 11 },
  221. .p = { .min = 28, .max = 112 },
  222. .p1 = { .min = 2, .max = 8 },
  223. .p2 = { .dot_limit = 0,
  224. .p2_slow = 14, .p2_fast = 14
  225. },
  226. };
  227. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  228. .dot = { .min = 80000, .max = 224000 },
  229. .vco = { .min = 1750000, .max = 3500000 },
  230. .n = { .min = 1, .max = 3 },
  231. .m = { .min = 104, .max = 138 },
  232. .m1 = { .min = 17, .max = 23 },
  233. .m2 = { .min = 5, .max = 11 },
  234. .p = { .min = 14, .max = 42 },
  235. .p1 = { .min = 2, .max = 6 },
  236. .p2 = { .dot_limit = 0,
  237. .p2_slow = 7, .p2_fast = 7
  238. },
  239. };
  240. static const intel_limit_t intel_limits_pineview_sdvo = {
  241. .dot = { .min = 20000, .max = 400000},
  242. .vco = { .min = 1700000, .max = 3500000 },
  243. /* Pineview's Ncounter is a ring counter */
  244. .n = { .min = 3, .max = 6 },
  245. .m = { .min = 2, .max = 256 },
  246. /* Pineview only has one combined m divider, which we treat as m2. */
  247. .m1 = { .min = 0, .max = 0 },
  248. .m2 = { .min = 0, .max = 254 },
  249. .p = { .min = 5, .max = 80 },
  250. .p1 = { .min = 1, .max = 8 },
  251. .p2 = { .dot_limit = 200000,
  252. .p2_slow = 10, .p2_fast = 5 },
  253. };
  254. static const intel_limit_t intel_limits_pineview_lvds = {
  255. .dot = { .min = 20000, .max = 400000 },
  256. .vco = { .min = 1700000, .max = 3500000 },
  257. .n = { .min = 3, .max = 6 },
  258. .m = { .min = 2, .max = 256 },
  259. .m1 = { .min = 0, .max = 0 },
  260. .m2 = { .min = 0, .max = 254 },
  261. .p = { .min = 7, .max = 112 },
  262. .p1 = { .min = 1, .max = 8 },
  263. .p2 = { .dot_limit = 112000,
  264. .p2_slow = 14, .p2_fast = 14 },
  265. };
  266. /* Ironlake / Sandybridge
  267. *
  268. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  269. * the range value for them is (actual_value - 2).
  270. */
  271. static const intel_limit_t intel_limits_ironlake_dac = {
  272. .dot = { .min = 25000, .max = 350000 },
  273. .vco = { .min = 1760000, .max = 3510000 },
  274. .n = { .min = 1, .max = 5 },
  275. .m = { .min = 79, .max = 127 },
  276. .m1 = { .min = 12, .max = 22 },
  277. .m2 = { .min = 5, .max = 9 },
  278. .p = { .min = 5, .max = 80 },
  279. .p1 = { .min = 1, .max = 8 },
  280. .p2 = { .dot_limit = 225000,
  281. .p2_slow = 10, .p2_fast = 5 },
  282. };
  283. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  284. .dot = { .min = 25000, .max = 350000 },
  285. .vco = { .min = 1760000, .max = 3510000 },
  286. .n = { .min = 1, .max = 3 },
  287. .m = { .min = 79, .max = 118 },
  288. .m1 = { .min = 12, .max = 22 },
  289. .m2 = { .min = 5, .max = 9 },
  290. .p = { .min = 28, .max = 112 },
  291. .p1 = { .min = 2, .max = 8 },
  292. .p2 = { .dot_limit = 225000,
  293. .p2_slow = 14, .p2_fast = 14 },
  294. };
  295. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  296. .dot = { .min = 25000, .max = 350000 },
  297. .vco = { .min = 1760000, .max = 3510000 },
  298. .n = { .min = 1, .max = 3 },
  299. .m = { .min = 79, .max = 127 },
  300. .m1 = { .min = 12, .max = 22 },
  301. .m2 = { .min = 5, .max = 9 },
  302. .p = { .min = 14, .max = 56 },
  303. .p1 = { .min = 2, .max = 8 },
  304. .p2 = { .dot_limit = 225000,
  305. .p2_slow = 7, .p2_fast = 7 },
  306. };
  307. /* LVDS 100mhz refclk limits. */
  308. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  309. .dot = { .min = 25000, .max = 350000 },
  310. .vco = { .min = 1760000, .max = 3510000 },
  311. .n = { .min = 1, .max = 2 },
  312. .m = { .min = 79, .max = 126 },
  313. .m1 = { .min = 12, .max = 22 },
  314. .m2 = { .min = 5, .max = 9 },
  315. .p = { .min = 28, .max = 112 },
  316. .p1 = { .min = 2, .max = 8 },
  317. .p2 = { .dot_limit = 225000,
  318. .p2_slow = 14, .p2_fast = 14 },
  319. };
  320. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  321. .dot = { .min = 25000, .max = 350000 },
  322. .vco = { .min = 1760000, .max = 3510000 },
  323. .n = { .min = 1, .max = 3 },
  324. .m = { .min = 79, .max = 126 },
  325. .m1 = { .min = 12, .max = 22 },
  326. .m2 = { .min = 5, .max = 9 },
  327. .p = { .min = 14, .max = 42 },
  328. .p1 = { .min = 2, .max = 6 },
  329. .p2 = { .dot_limit = 225000,
  330. .p2_slow = 7, .p2_fast = 7 },
  331. };
  332. static const intel_limit_t intel_limits_vlv = {
  333. /*
  334. * These are the data rate limits (measured in fast clocks)
  335. * since those are the strictest limits we have. The fast
  336. * clock and actual rate limits are more relaxed, so checking
  337. * them would make no difference.
  338. */
  339. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  340. .vco = { .min = 4000000, .max = 6000000 },
  341. .n = { .min = 1, .max = 7 },
  342. .m1 = { .min = 2, .max = 3 },
  343. .m2 = { .min = 11, .max = 156 },
  344. .p1 = { .min = 2, .max = 3 },
  345. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  346. };
  347. static const intel_limit_t intel_limits_chv = {
  348. /*
  349. * These are the data rate limits (measured in fast clocks)
  350. * since those are the strictest limits we have. The fast
  351. * clock and actual rate limits are more relaxed, so checking
  352. * them would make no difference.
  353. */
  354. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  355. .vco = { .min = 4860000, .max = 6700000 },
  356. .n = { .min = 1, .max = 1 },
  357. .m1 = { .min = 2, .max = 2 },
  358. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  359. .p1 = { .min = 2, .max = 4 },
  360. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  361. };
  362. static void vlv_clock(int refclk, intel_clock_t *clock)
  363. {
  364. clock->m = clock->m1 * clock->m2;
  365. clock->p = clock->p1 * clock->p2;
  366. if (WARN_ON(clock->n == 0 || clock->p == 0))
  367. return;
  368. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  369. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  370. }
  371. /**
  372. * Returns whether any output on the specified pipe is of the specified type
  373. */
  374. bool intel_pipe_has_type(struct intel_crtc *crtc, int type)
  375. {
  376. struct drm_device *dev = crtc->base.dev;
  377. struct intel_encoder *encoder;
  378. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  379. if (encoder->type == type)
  380. return true;
  381. return false;
  382. }
  383. static const intel_limit_t *intel_ironlake_limit(struct intel_crtc *crtc,
  384. int refclk)
  385. {
  386. struct drm_device *dev = crtc->base.dev;
  387. const intel_limit_t *limit;
  388. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  389. if (intel_is_dual_link_lvds(dev)) {
  390. if (refclk == 100000)
  391. limit = &intel_limits_ironlake_dual_lvds_100m;
  392. else
  393. limit = &intel_limits_ironlake_dual_lvds;
  394. } else {
  395. if (refclk == 100000)
  396. limit = &intel_limits_ironlake_single_lvds_100m;
  397. else
  398. limit = &intel_limits_ironlake_single_lvds;
  399. }
  400. } else
  401. limit = &intel_limits_ironlake_dac;
  402. return limit;
  403. }
  404. static const intel_limit_t *intel_g4x_limit(struct intel_crtc *crtc)
  405. {
  406. struct drm_device *dev = crtc->base.dev;
  407. const intel_limit_t *limit;
  408. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  409. if (intel_is_dual_link_lvds(dev))
  410. limit = &intel_limits_g4x_dual_channel_lvds;
  411. else
  412. limit = &intel_limits_g4x_single_channel_lvds;
  413. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  414. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  415. limit = &intel_limits_g4x_hdmi;
  416. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  417. limit = &intel_limits_g4x_sdvo;
  418. } else /* The option is for other outputs */
  419. limit = &intel_limits_i9xx_sdvo;
  420. return limit;
  421. }
  422. static const intel_limit_t *intel_limit(struct intel_crtc *crtc, int refclk)
  423. {
  424. struct drm_device *dev = crtc->base.dev;
  425. const intel_limit_t *limit;
  426. if (HAS_PCH_SPLIT(dev))
  427. limit = intel_ironlake_limit(crtc, refclk);
  428. else if (IS_G4X(dev)) {
  429. limit = intel_g4x_limit(crtc);
  430. } else if (IS_PINEVIEW(dev)) {
  431. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  432. limit = &intel_limits_pineview_lvds;
  433. else
  434. limit = &intel_limits_pineview_sdvo;
  435. } else if (IS_CHERRYVIEW(dev)) {
  436. limit = &intel_limits_chv;
  437. } else if (IS_VALLEYVIEW(dev)) {
  438. limit = &intel_limits_vlv;
  439. } else if (!IS_GEN2(dev)) {
  440. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  441. limit = &intel_limits_i9xx_lvds;
  442. else
  443. limit = &intel_limits_i9xx_sdvo;
  444. } else {
  445. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  446. limit = &intel_limits_i8xx_lvds;
  447. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
  448. limit = &intel_limits_i8xx_dvo;
  449. else
  450. limit = &intel_limits_i8xx_dac;
  451. }
  452. return limit;
  453. }
  454. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  455. static void pineview_clock(int refclk, intel_clock_t *clock)
  456. {
  457. clock->m = clock->m2 + 2;
  458. clock->p = clock->p1 * clock->p2;
  459. if (WARN_ON(clock->n == 0 || clock->p == 0))
  460. return;
  461. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  462. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  463. }
  464. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  465. {
  466. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  467. }
  468. static void i9xx_clock(int refclk, intel_clock_t *clock)
  469. {
  470. clock->m = i9xx_dpll_compute_m(clock);
  471. clock->p = clock->p1 * clock->p2;
  472. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  473. return;
  474. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  475. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  476. }
  477. static void chv_clock(int refclk, intel_clock_t *clock)
  478. {
  479. clock->m = clock->m1 * clock->m2;
  480. clock->p = clock->p1 * clock->p2;
  481. if (WARN_ON(clock->n == 0 || clock->p == 0))
  482. return;
  483. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  484. clock->n << 22);
  485. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  486. }
  487. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  488. /**
  489. * Returns whether the given set of divisors are valid for a given refclk with
  490. * the given connectors.
  491. */
  492. static bool intel_PLL_is_valid(struct drm_device *dev,
  493. const intel_limit_t *limit,
  494. const intel_clock_t *clock)
  495. {
  496. if (clock->n < limit->n.min || limit->n.max < clock->n)
  497. INTELPllInvalid("n out of range\n");
  498. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  499. INTELPllInvalid("p1 out of range\n");
  500. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  501. INTELPllInvalid("m2 out of range\n");
  502. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  503. INTELPllInvalid("m1 out of range\n");
  504. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
  505. if (clock->m1 <= clock->m2)
  506. INTELPllInvalid("m1 <= m2\n");
  507. if (!IS_VALLEYVIEW(dev)) {
  508. if (clock->p < limit->p.min || limit->p.max < clock->p)
  509. INTELPllInvalid("p out of range\n");
  510. if (clock->m < limit->m.min || limit->m.max < clock->m)
  511. INTELPllInvalid("m out of range\n");
  512. }
  513. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  514. INTELPllInvalid("vco out of range\n");
  515. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  516. * connector, etc., rather than just a single range.
  517. */
  518. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  519. INTELPllInvalid("dot out of range\n");
  520. return true;
  521. }
  522. static bool
  523. i9xx_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  524. int target, int refclk, intel_clock_t *match_clock,
  525. intel_clock_t *best_clock)
  526. {
  527. struct drm_device *dev = crtc->base.dev;
  528. intel_clock_t clock;
  529. int err = target;
  530. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  531. /*
  532. * For LVDS just rely on its current settings for dual-channel.
  533. * We haven't figured out how to reliably set up different
  534. * single/dual channel state, if we even can.
  535. */
  536. if (intel_is_dual_link_lvds(dev))
  537. clock.p2 = limit->p2.p2_fast;
  538. else
  539. clock.p2 = limit->p2.p2_slow;
  540. } else {
  541. if (target < limit->p2.dot_limit)
  542. clock.p2 = limit->p2.p2_slow;
  543. else
  544. clock.p2 = limit->p2.p2_fast;
  545. }
  546. memset(best_clock, 0, sizeof(*best_clock));
  547. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  548. clock.m1++) {
  549. for (clock.m2 = limit->m2.min;
  550. clock.m2 <= limit->m2.max; clock.m2++) {
  551. if (clock.m2 >= clock.m1)
  552. break;
  553. for (clock.n = limit->n.min;
  554. clock.n <= limit->n.max; clock.n++) {
  555. for (clock.p1 = limit->p1.min;
  556. clock.p1 <= limit->p1.max; clock.p1++) {
  557. int this_err;
  558. i9xx_clock(refclk, &clock);
  559. if (!intel_PLL_is_valid(dev, limit,
  560. &clock))
  561. continue;
  562. if (match_clock &&
  563. clock.p != match_clock->p)
  564. continue;
  565. this_err = abs(clock.dot - target);
  566. if (this_err < err) {
  567. *best_clock = clock;
  568. err = this_err;
  569. }
  570. }
  571. }
  572. }
  573. }
  574. return (err != target);
  575. }
  576. static bool
  577. pnv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  578. int target, int refclk, intel_clock_t *match_clock,
  579. intel_clock_t *best_clock)
  580. {
  581. struct drm_device *dev = crtc->base.dev;
  582. intel_clock_t clock;
  583. int err = target;
  584. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  585. /*
  586. * For LVDS just rely on its current settings for dual-channel.
  587. * We haven't figured out how to reliably set up different
  588. * single/dual channel state, if we even can.
  589. */
  590. if (intel_is_dual_link_lvds(dev))
  591. clock.p2 = limit->p2.p2_fast;
  592. else
  593. clock.p2 = limit->p2.p2_slow;
  594. } else {
  595. if (target < limit->p2.dot_limit)
  596. clock.p2 = limit->p2.p2_slow;
  597. else
  598. clock.p2 = limit->p2.p2_fast;
  599. }
  600. memset(best_clock, 0, sizeof(*best_clock));
  601. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  602. clock.m1++) {
  603. for (clock.m2 = limit->m2.min;
  604. clock.m2 <= limit->m2.max; clock.m2++) {
  605. for (clock.n = limit->n.min;
  606. clock.n <= limit->n.max; clock.n++) {
  607. for (clock.p1 = limit->p1.min;
  608. clock.p1 <= limit->p1.max; clock.p1++) {
  609. int this_err;
  610. pineview_clock(refclk, &clock);
  611. if (!intel_PLL_is_valid(dev, limit,
  612. &clock))
  613. continue;
  614. if (match_clock &&
  615. clock.p != match_clock->p)
  616. continue;
  617. this_err = abs(clock.dot - target);
  618. if (this_err < err) {
  619. *best_clock = clock;
  620. err = this_err;
  621. }
  622. }
  623. }
  624. }
  625. }
  626. return (err != target);
  627. }
  628. static bool
  629. g4x_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  630. int target, int refclk, intel_clock_t *match_clock,
  631. intel_clock_t *best_clock)
  632. {
  633. struct drm_device *dev = crtc->base.dev;
  634. intel_clock_t clock;
  635. int max_n;
  636. bool found;
  637. /* approximately equals target * 0.00585 */
  638. int err_most = (target >> 8) + (target >> 9);
  639. found = false;
  640. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  641. if (intel_is_dual_link_lvds(dev))
  642. clock.p2 = limit->p2.p2_fast;
  643. else
  644. clock.p2 = limit->p2.p2_slow;
  645. } else {
  646. if (target < limit->p2.dot_limit)
  647. clock.p2 = limit->p2.p2_slow;
  648. else
  649. clock.p2 = limit->p2.p2_fast;
  650. }
  651. memset(best_clock, 0, sizeof(*best_clock));
  652. max_n = limit->n.max;
  653. /* based on hardware requirement, prefer smaller n to precision */
  654. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  655. /* based on hardware requirement, prefere larger m1,m2 */
  656. for (clock.m1 = limit->m1.max;
  657. clock.m1 >= limit->m1.min; clock.m1--) {
  658. for (clock.m2 = limit->m2.max;
  659. clock.m2 >= limit->m2.min; clock.m2--) {
  660. for (clock.p1 = limit->p1.max;
  661. clock.p1 >= limit->p1.min; clock.p1--) {
  662. int this_err;
  663. i9xx_clock(refclk, &clock);
  664. if (!intel_PLL_is_valid(dev, limit,
  665. &clock))
  666. continue;
  667. this_err = abs(clock.dot - target);
  668. if (this_err < err_most) {
  669. *best_clock = clock;
  670. err_most = this_err;
  671. max_n = clock.n;
  672. found = true;
  673. }
  674. }
  675. }
  676. }
  677. }
  678. return found;
  679. }
  680. static bool
  681. vlv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  682. int target, int refclk, intel_clock_t *match_clock,
  683. intel_clock_t *best_clock)
  684. {
  685. struct drm_device *dev = crtc->base.dev;
  686. intel_clock_t clock;
  687. unsigned int bestppm = 1000000;
  688. /* min update 19.2 MHz */
  689. int max_n = min(limit->n.max, refclk / 19200);
  690. bool found = false;
  691. target *= 5; /* fast clock */
  692. memset(best_clock, 0, sizeof(*best_clock));
  693. /* based on hardware requirement, prefer smaller n to precision */
  694. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  695. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  696. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  697. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  698. clock.p = clock.p1 * clock.p2;
  699. /* based on hardware requirement, prefer bigger m1,m2 values */
  700. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  701. unsigned int ppm, diff;
  702. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  703. refclk * clock.m1);
  704. vlv_clock(refclk, &clock);
  705. if (!intel_PLL_is_valid(dev, limit,
  706. &clock))
  707. continue;
  708. diff = abs(clock.dot - target);
  709. ppm = div_u64(1000000ULL * diff, target);
  710. if (ppm < 100 && clock.p > best_clock->p) {
  711. bestppm = 0;
  712. *best_clock = clock;
  713. found = true;
  714. }
  715. if (bestppm >= 10 && ppm < bestppm - 10) {
  716. bestppm = ppm;
  717. *best_clock = clock;
  718. found = true;
  719. }
  720. }
  721. }
  722. }
  723. }
  724. return found;
  725. }
  726. static bool
  727. chv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  728. int target, int refclk, intel_clock_t *match_clock,
  729. intel_clock_t *best_clock)
  730. {
  731. struct drm_device *dev = crtc->base.dev;
  732. intel_clock_t clock;
  733. uint64_t m2;
  734. int found = false;
  735. memset(best_clock, 0, sizeof(*best_clock));
  736. /*
  737. * Based on hardware doc, the n always set to 1, and m1 always
  738. * set to 2. If requires to support 200Mhz refclk, we need to
  739. * revisit this because n may not 1 anymore.
  740. */
  741. clock.n = 1, clock.m1 = 2;
  742. target *= 5; /* fast clock */
  743. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  744. for (clock.p2 = limit->p2.p2_fast;
  745. clock.p2 >= limit->p2.p2_slow;
  746. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  747. clock.p = clock.p1 * clock.p2;
  748. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  749. clock.n) << 22, refclk * clock.m1);
  750. if (m2 > INT_MAX/clock.m1)
  751. continue;
  752. clock.m2 = m2;
  753. chv_clock(refclk, &clock);
  754. if (!intel_PLL_is_valid(dev, limit, &clock))
  755. continue;
  756. /* based on hardware requirement, prefer bigger p
  757. */
  758. if (clock.p > best_clock->p) {
  759. *best_clock = clock;
  760. found = true;
  761. }
  762. }
  763. }
  764. return found;
  765. }
  766. bool intel_crtc_active(struct drm_crtc *crtc)
  767. {
  768. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  769. /* Be paranoid as we can arrive here with only partial
  770. * state retrieved from the hardware during setup.
  771. *
  772. * We can ditch the adjusted_mode.crtc_clock check as soon
  773. * as Haswell has gained clock readout/fastboot support.
  774. *
  775. * We can ditch the crtc->primary->fb check as soon as we can
  776. * properly reconstruct framebuffers.
  777. */
  778. return intel_crtc->active && crtc->primary->fb &&
  779. intel_crtc->config.adjusted_mode.crtc_clock;
  780. }
  781. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  782. enum pipe pipe)
  783. {
  784. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  785. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  786. return intel_crtc->config.cpu_transcoder;
  787. }
  788. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  789. {
  790. struct drm_i915_private *dev_priv = dev->dev_private;
  791. u32 reg = PIPEDSL(pipe);
  792. u32 line1, line2;
  793. u32 line_mask;
  794. if (IS_GEN2(dev))
  795. line_mask = DSL_LINEMASK_GEN2;
  796. else
  797. line_mask = DSL_LINEMASK_GEN3;
  798. line1 = I915_READ(reg) & line_mask;
  799. mdelay(5);
  800. line2 = I915_READ(reg) & line_mask;
  801. return line1 == line2;
  802. }
  803. /*
  804. * intel_wait_for_pipe_off - wait for pipe to turn off
  805. * @crtc: crtc whose pipe to wait for
  806. *
  807. * After disabling a pipe, we can't wait for vblank in the usual way,
  808. * spinning on the vblank interrupt status bit, since we won't actually
  809. * see an interrupt when the pipe is disabled.
  810. *
  811. * On Gen4 and above:
  812. * wait for the pipe register state bit to turn off
  813. *
  814. * Otherwise:
  815. * wait for the display line value to settle (it usually
  816. * ends up stopping at the start of the next frame).
  817. *
  818. */
  819. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  820. {
  821. struct drm_device *dev = crtc->base.dev;
  822. struct drm_i915_private *dev_priv = dev->dev_private;
  823. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  824. enum pipe pipe = crtc->pipe;
  825. if (INTEL_INFO(dev)->gen >= 4) {
  826. int reg = PIPECONF(cpu_transcoder);
  827. /* Wait for the Pipe State to go off */
  828. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  829. 100))
  830. WARN(1, "pipe_off wait timed out\n");
  831. } else {
  832. /* Wait for the display line to settle */
  833. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  834. WARN(1, "pipe_off wait timed out\n");
  835. }
  836. }
  837. /*
  838. * ibx_digital_port_connected - is the specified port connected?
  839. * @dev_priv: i915 private structure
  840. * @port: the port to test
  841. *
  842. * Returns true if @port is connected, false otherwise.
  843. */
  844. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  845. struct intel_digital_port *port)
  846. {
  847. u32 bit;
  848. if (HAS_PCH_IBX(dev_priv->dev)) {
  849. switch (port->port) {
  850. case PORT_B:
  851. bit = SDE_PORTB_HOTPLUG;
  852. break;
  853. case PORT_C:
  854. bit = SDE_PORTC_HOTPLUG;
  855. break;
  856. case PORT_D:
  857. bit = SDE_PORTD_HOTPLUG;
  858. break;
  859. default:
  860. return true;
  861. }
  862. } else {
  863. switch (port->port) {
  864. case PORT_B:
  865. bit = SDE_PORTB_HOTPLUG_CPT;
  866. break;
  867. case PORT_C:
  868. bit = SDE_PORTC_HOTPLUG_CPT;
  869. break;
  870. case PORT_D:
  871. bit = SDE_PORTD_HOTPLUG_CPT;
  872. break;
  873. default:
  874. return true;
  875. }
  876. }
  877. return I915_READ(SDEISR) & bit;
  878. }
  879. static const char *state_string(bool enabled)
  880. {
  881. return enabled ? "on" : "off";
  882. }
  883. /* Only for pre-ILK configs */
  884. void assert_pll(struct drm_i915_private *dev_priv,
  885. enum pipe pipe, bool state)
  886. {
  887. int reg;
  888. u32 val;
  889. bool cur_state;
  890. reg = DPLL(pipe);
  891. val = I915_READ(reg);
  892. cur_state = !!(val & DPLL_VCO_ENABLE);
  893. WARN(cur_state != state,
  894. "PLL state assertion failure (expected %s, current %s)\n",
  895. state_string(state), state_string(cur_state));
  896. }
  897. /* XXX: the dsi pll is shared between MIPI DSI ports */
  898. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  899. {
  900. u32 val;
  901. bool cur_state;
  902. mutex_lock(&dev_priv->dpio_lock);
  903. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  904. mutex_unlock(&dev_priv->dpio_lock);
  905. cur_state = val & DSI_PLL_VCO_EN;
  906. WARN(cur_state != state,
  907. "DSI PLL state assertion failure (expected %s, current %s)\n",
  908. state_string(state), state_string(cur_state));
  909. }
  910. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  911. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  912. struct intel_shared_dpll *
  913. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  914. {
  915. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  916. if (crtc->config.shared_dpll < 0)
  917. return NULL;
  918. return &dev_priv->shared_dplls[crtc->config.shared_dpll];
  919. }
  920. /* For ILK+ */
  921. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  922. struct intel_shared_dpll *pll,
  923. bool state)
  924. {
  925. bool cur_state;
  926. struct intel_dpll_hw_state hw_state;
  927. if (WARN (!pll,
  928. "asserting DPLL %s with no DPLL\n", state_string(state)))
  929. return;
  930. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  931. WARN(cur_state != state,
  932. "%s assertion failure (expected %s, current %s)\n",
  933. pll->name, state_string(state), state_string(cur_state));
  934. }
  935. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  936. enum pipe pipe, bool state)
  937. {
  938. int reg;
  939. u32 val;
  940. bool cur_state;
  941. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  942. pipe);
  943. if (HAS_DDI(dev_priv->dev)) {
  944. /* DDI does not have a specific FDI_TX register */
  945. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  946. val = I915_READ(reg);
  947. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  948. } else {
  949. reg = FDI_TX_CTL(pipe);
  950. val = I915_READ(reg);
  951. cur_state = !!(val & FDI_TX_ENABLE);
  952. }
  953. WARN(cur_state != state,
  954. "FDI TX state assertion failure (expected %s, current %s)\n",
  955. state_string(state), state_string(cur_state));
  956. }
  957. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  958. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  959. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  960. enum pipe pipe, bool state)
  961. {
  962. int reg;
  963. u32 val;
  964. bool cur_state;
  965. reg = FDI_RX_CTL(pipe);
  966. val = I915_READ(reg);
  967. cur_state = !!(val & FDI_RX_ENABLE);
  968. WARN(cur_state != state,
  969. "FDI RX state assertion failure (expected %s, current %s)\n",
  970. state_string(state), state_string(cur_state));
  971. }
  972. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  973. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  974. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  975. enum pipe pipe)
  976. {
  977. int reg;
  978. u32 val;
  979. /* ILK FDI PLL is always enabled */
  980. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  981. return;
  982. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  983. if (HAS_DDI(dev_priv->dev))
  984. return;
  985. reg = FDI_TX_CTL(pipe);
  986. val = I915_READ(reg);
  987. WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  988. }
  989. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  990. enum pipe pipe, bool state)
  991. {
  992. int reg;
  993. u32 val;
  994. bool cur_state;
  995. reg = FDI_RX_CTL(pipe);
  996. val = I915_READ(reg);
  997. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  998. WARN(cur_state != state,
  999. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1000. state_string(state), state_string(cur_state));
  1001. }
  1002. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1003. enum pipe pipe)
  1004. {
  1005. struct drm_device *dev = dev_priv->dev;
  1006. int pp_reg;
  1007. u32 val;
  1008. enum pipe panel_pipe = PIPE_A;
  1009. bool locked = true;
  1010. if (WARN_ON(HAS_DDI(dev)))
  1011. return;
  1012. if (HAS_PCH_SPLIT(dev)) {
  1013. u32 port_sel;
  1014. pp_reg = PCH_PP_CONTROL;
  1015. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1016. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1017. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1018. panel_pipe = PIPE_B;
  1019. /* XXX: else fix for eDP */
  1020. } else if (IS_VALLEYVIEW(dev)) {
  1021. /* presumably write lock depends on pipe, not port select */
  1022. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1023. panel_pipe = pipe;
  1024. } else {
  1025. pp_reg = PP_CONTROL;
  1026. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1027. panel_pipe = PIPE_B;
  1028. }
  1029. val = I915_READ(pp_reg);
  1030. if (!(val & PANEL_POWER_ON) ||
  1031. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1032. locked = false;
  1033. WARN(panel_pipe == pipe && locked,
  1034. "panel assertion failure, pipe %c regs locked\n",
  1035. pipe_name(pipe));
  1036. }
  1037. static void assert_cursor(struct drm_i915_private *dev_priv,
  1038. enum pipe pipe, bool state)
  1039. {
  1040. struct drm_device *dev = dev_priv->dev;
  1041. bool cur_state;
  1042. if (IS_845G(dev) || IS_I865G(dev))
  1043. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1044. else
  1045. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1046. WARN(cur_state != state,
  1047. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1048. pipe_name(pipe), state_string(state), state_string(cur_state));
  1049. }
  1050. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1051. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1052. void assert_pipe(struct drm_i915_private *dev_priv,
  1053. enum pipe pipe, bool state)
  1054. {
  1055. int reg;
  1056. u32 val;
  1057. bool cur_state;
  1058. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1059. pipe);
  1060. /* if we need the pipe quirk it must be always on */
  1061. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1062. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1063. state = true;
  1064. if (!intel_display_power_is_enabled(dev_priv,
  1065. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1066. cur_state = false;
  1067. } else {
  1068. reg = PIPECONF(cpu_transcoder);
  1069. val = I915_READ(reg);
  1070. cur_state = !!(val & PIPECONF_ENABLE);
  1071. }
  1072. WARN(cur_state != state,
  1073. "pipe %c assertion failure (expected %s, current %s)\n",
  1074. pipe_name(pipe), state_string(state), state_string(cur_state));
  1075. }
  1076. static void assert_plane(struct drm_i915_private *dev_priv,
  1077. enum plane plane, bool state)
  1078. {
  1079. int reg;
  1080. u32 val;
  1081. bool cur_state;
  1082. reg = DSPCNTR(plane);
  1083. val = I915_READ(reg);
  1084. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1085. WARN(cur_state != state,
  1086. "plane %c assertion failure (expected %s, current %s)\n",
  1087. plane_name(plane), state_string(state), state_string(cur_state));
  1088. }
  1089. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1090. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1091. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1092. enum pipe pipe)
  1093. {
  1094. struct drm_device *dev = dev_priv->dev;
  1095. int reg, i;
  1096. u32 val;
  1097. int cur_pipe;
  1098. /* Primary planes are fixed to pipes on gen4+ */
  1099. if (INTEL_INFO(dev)->gen >= 4) {
  1100. reg = DSPCNTR(pipe);
  1101. val = I915_READ(reg);
  1102. WARN(val & DISPLAY_PLANE_ENABLE,
  1103. "plane %c assertion failure, should be disabled but not\n",
  1104. plane_name(pipe));
  1105. return;
  1106. }
  1107. /* Need to check both planes against the pipe */
  1108. for_each_pipe(dev_priv, i) {
  1109. reg = DSPCNTR(i);
  1110. val = I915_READ(reg);
  1111. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1112. DISPPLANE_SEL_PIPE_SHIFT;
  1113. WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1114. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1115. plane_name(i), pipe_name(pipe));
  1116. }
  1117. }
  1118. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1119. enum pipe pipe)
  1120. {
  1121. struct drm_device *dev = dev_priv->dev;
  1122. int reg, sprite;
  1123. u32 val;
  1124. if (INTEL_INFO(dev)->gen >= 9) {
  1125. for_each_sprite(pipe, sprite) {
  1126. val = I915_READ(PLANE_CTL(pipe, sprite));
  1127. WARN(val & PLANE_CTL_ENABLE,
  1128. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1129. sprite, pipe_name(pipe));
  1130. }
  1131. } else if (IS_VALLEYVIEW(dev)) {
  1132. for_each_sprite(pipe, sprite) {
  1133. reg = SPCNTR(pipe, sprite);
  1134. val = I915_READ(reg);
  1135. WARN(val & SP_ENABLE,
  1136. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1137. sprite_name(pipe, sprite), pipe_name(pipe));
  1138. }
  1139. } else if (INTEL_INFO(dev)->gen >= 7) {
  1140. reg = SPRCTL(pipe);
  1141. val = I915_READ(reg);
  1142. WARN(val & SPRITE_ENABLE,
  1143. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1144. plane_name(pipe), pipe_name(pipe));
  1145. } else if (INTEL_INFO(dev)->gen >= 5) {
  1146. reg = DVSCNTR(pipe);
  1147. val = I915_READ(reg);
  1148. WARN(val & DVS_ENABLE,
  1149. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1150. plane_name(pipe), pipe_name(pipe));
  1151. }
  1152. }
  1153. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1154. {
  1155. if (WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1156. drm_crtc_vblank_put(crtc);
  1157. }
  1158. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1159. {
  1160. u32 val;
  1161. bool enabled;
  1162. WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1163. val = I915_READ(PCH_DREF_CONTROL);
  1164. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1165. DREF_SUPERSPREAD_SOURCE_MASK));
  1166. WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1167. }
  1168. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1169. enum pipe pipe)
  1170. {
  1171. int reg;
  1172. u32 val;
  1173. bool enabled;
  1174. reg = PCH_TRANSCONF(pipe);
  1175. val = I915_READ(reg);
  1176. enabled = !!(val & TRANS_ENABLE);
  1177. WARN(enabled,
  1178. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1179. pipe_name(pipe));
  1180. }
  1181. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1182. enum pipe pipe, u32 port_sel, u32 val)
  1183. {
  1184. if ((val & DP_PORT_EN) == 0)
  1185. return false;
  1186. if (HAS_PCH_CPT(dev_priv->dev)) {
  1187. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1188. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1189. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1190. return false;
  1191. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1192. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1193. return false;
  1194. } else {
  1195. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1196. return false;
  1197. }
  1198. return true;
  1199. }
  1200. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1201. enum pipe pipe, u32 val)
  1202. {
  1203. if ((val & SDVO_ENABLE) == 0)
  1204. return false;
  1205. if (HAS_PCH_CPT(dev_priv->dev)) {
  1206. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1207. return false;
  1208. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1209. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1210. return false;
  1211. } else {
  1212. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1213. return false;
  1214. }
  1215. return true;
  1216. }
  1217. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1218. enum pipe pipe, u32 val)
  1219. {
  1220. if ((val & LVDS_PORT_EN) == 0)
  1221. return false;
  1222. if (HAS_PCH_CPT(dev_priv->dev)) {
  1223. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1224. return false;
  1225. } else {
  1226. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1227. return false;
  1228. }
  1229. return true;
  1230. }
  1231. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1232. enum pipe pipe, u32 val)
  1233. {
  1234. if ((val & ADPA_DAC_ENABLE) == 0)
  1235. return false;
  1236. if (HAS_PCH_CPT(dev_priv->dev)) {
  1237. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1238. return false;
  1239. } else {
  1240. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1241. return false;
  1242. }
  1243. return true;
  1244. }
  1245. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1246. enum pipe pipe, int reg, u32 port_sel)
  1247. {
  1248. u32 val = I915_READ(reg);
  1249. WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1250. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1251. reg, pipe_name(pipe));
  1252. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1253. && (val & DP_PIPEB_SELECT),
  1254. "IBX PCH dp port still using transcoder B\n");
  1255. }
  1256. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1257. enum pipe pipe, int reg)
  1258. {
  1259. u32 val = I915_READ(reg);
  1260. WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1261. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1262. reg, pipe_name(pipe));
  1263. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1264. && (val & SDVO_PIPE_B_SELECT),
  1265. "IBX PCH hdmi port still using transcoder B\n");
  1266. }
  1267. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1268. enum pipe pipe)
  1269. {
  1270. int reg;
  1271. u32 val;
  1272. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1273. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1274. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1275. reg = PCH_ADPA;
  1276. val = I915_READ(reg);
  1277. WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1278. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1279. pipe_name(pipe));
  1280. reg = PCH_LVDS;
  1281. val = I915_READ(reg);
  1282. WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1283. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1284. pipe_name(pipe));
  1285. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1286. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1287. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1288. }
  1289. static void intel_init_dpio(struct drm_device *dev)
  1290. {
  1291. struct drm_i915_private *dev_priv = dev->dev_private;
  1292. if (!IS_VALLEYVIEW(dev))
  1293. return;
  1294. /*
  1295. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1296. * CHV x1 PHY (DP/HDMI D)
  1297. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1298. */
  1299. if (IS_CHERRYVIEW(dev)) {
  1300. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1301. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1302. } else {
  1303. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1304. }
  1305. }
  1306. static void vlv_enable_pll(struct intel_crtc *crtc,
  1307. const struct intel_crtc_config *pipe_config)
  1308. {
  1309. struct drm_device *dev = crtc->base.dev;
  1310. struct drm_i915_private *dev_priv = dev->dev_private;
  1311. int reg = DPLL(crtc->pipe);
  1312. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1313. assert_pipe_disabled(dev_priv, crtc->pipe);
  1314. /* No really, not for ILK+ */
  1315. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1316. /* PLL is protected by panel, make sure we can write it */
  1317. if (IS_MOBILE(dev_priv->dev))
  1318. assert_panel_unlocked(dev_priv, crtc->pipe);
  1319. I915_WRITE(reg, dpll);
  1320. POSTING_READ(reg);
  1321. udelay(150);
  1322. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1323. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1324. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1325. POSTING_READ(DPLL_MD(crtc->pipe));
  1326. /* We do this three times for luck */
  1327. I915_WRITE(reg, dpll);
  1328. POSTING_READ(reg);
  1329. udelay(150); /* wait for warmup */
  1330. I915_WRITE(reg, dpll);
  1331. POSTING_READ(reg);
  1332. udelay(150); /* wait for warmup */
  1333. I915_WRITE(reg, dpll);
  1334. POSTING_READ(reg);
  1335. udelay(150); /* wait for warmup */
  1336. }
  1337. static void chv_enable_pll(struct intel_crtc *crtc,
  1338. const struct intel_crtc_config *pipe_config)
  1339. {
  1340. struct drm_device *dev = crtc->base.dev;
  1341. struct drm_i915_private *dev_priv = dev->dev_private;
  1342. int pipe = crtc->pipe;
  1343. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1344. u32 tmp;
  1345. assert_pipe_disabled(dev_priv, crtc->pipe);
  1346. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1347. mutex_lock(&dev_priv->dpio_lock);
  1348. /* Enable back the 10bit clock to display controller */
  1349. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1350. tmp |= DPIO_DCLKP_EN;
  1351. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1352. /*
  1353. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1354. */
  1355. udelay(1);
  1356. /* Enable PLL */
  1357. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1358. /* Check PLL is locked */
  1359. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1360. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1361. /* not sure when this should be written */
  1362. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1363. POSTING_READ(DPLL_MD(pipe));
  1364. mutex_unlock(&dev_priv->dpio_lock);
  1365. }
  1366. static int intel_num_dvo_pipes(struct drm_device *dev)
  1367. {
  1368. struct intel_crtc *crtc;
  1369. int count = 0;
  1370. for_each_intel_crtc(dev, crtc)
  1371. count += crtc->active &&
  1372. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1373. return count;
  1374. }
  1375. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1376. {
  1377. struct drm_device *dev = crtc->base.dev;
  1378. struct drm_i915_private *dev_priv = dev->dev_private;
  1379. int reg = DPLL(crtc->pipe);
  1380. u32 dpll = crtc->config.dpll_hw_state.dpll;
  1381. assert_pipe_disabled(dev_priv, crtc->pipe);
  1382. /* No really, not for ILK+ */
  1383. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1384. /* PLL is protected by panel, make sure we can write it */
  1385. if (IS_MOBILE(dev) && !IS_I830(dev))
  1386. assert_panel_unlocked(dev_priv, crtc->pipe);
  1387. /* Enable DVO 2x clock on both PLLs if necessary */
  1388. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1389. /*
  1390. * It appears to be important that we don't enable this
  1391. * for the current pipe before otherwise configuring the
  1392. * PLL. No idea how this should be handled if multiple
  1393. * DVO outputs are enabled simultaneosly.
  1394. */
  1395. dpll |= DPLL_DVO_2X_MODE;
  1396. I915_WRITE(DPLL(!crtc->pipe),
  1397. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1398. }
  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 intel_crtc *crtc)
  1434. {
  1435. struct drm_device *dev = crtc->base.dev;
  1436. struct drm_i915_private *dev_priv = dev->dev_private;
  1437. enum pipe pipe = crtc->pipe;
  1438. /* Disable DVO 2x clock on both PLLs if necessary */
  1439. if (IS_I830(dev) &&
  1440. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1441. intel_num_dvo_pipes(dev) == 1) {
  1442. I915_WRITE(DPLL(PIPE_B),
  1443. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1444. I915_WRITE(DPLL(PIPE_A),
  1445. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1446. }
  1447. /* Don't disable pipe or pipe PLLs if needed */
  1448. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1449. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1450. return;
  1451. /* Make sure the pipe isn't still relying on us */
  1452. assert_pipe_disabled(dev_priv, pipe);
  1453. I915_WRITE(DPLL(pipe), 0);
  1454. POSTING_READ(DPLL(pipe));
  1455. }
  1456. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1457. {
  1458. u32 val = 0;
  1459. /* Make sure the pipe isn't still relying on us */
  1460. assert_pipe_disabled(dev_priv, pipe);
  1461. /*
  1462. * Leave integrated clock source and reference clock enabled for pipe B.
  1463. * The latter is needed for VGA hotplug / manual detection.
  1464. */
  1465. if (pipe == PIPE_B)
  1466. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1467. I915_WRITE(DPLL(pipe), val);
  1468. POSTING_READ(DPLL(pipe));
  1469. }
  1470. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1471. {
  1472. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1473. u32 val;
  1474. /* Make sure the pipe isn't still relying on us */
  1475. assert_pipe_disabled(dev_priv, pipe);
  1476. /* Set PLL en = 0 */
  1477. val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
  1478. if (pipe != PIPE_A)
  1479. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1480. I915_WRITE(DPLL(pipe), val);
  1481. POSTING_READ(DPLL(pipe));
  1482. mutex_lock(&dev_priv->dpio_lock);
  1483. /* Disable 10bit clock to display controller */
  1484. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1485. val &= ~DPIO_DCLKP_EN;
  1486. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1487. /* disable left/right clock distribution */
  1488. if (pipe != PIPE_B) {
  1489. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1490. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1491. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1492. } else {
  1493. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1494. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1495. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1496. }
  1497. mutex_unlock(&dev_priv->dpio_lock);
  1498. }
  1499. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1500. struct intel_digital_port *dport)
  1501. {
  1502. u32 port_mask;
  1503. int dpll_reg;
  1504. switch (dport->port) {
  1505. case PORT_B:
  1506. port_mask = DPLL_PORTB_READY_MASK;
  1507. dpll_reg = DPLL(0);
  1508. break;
  1509. case PORT_C:
  1510. port_mask = DPLL_PORTC_READY_MASK;
  1511. dpll_reg = DPLL(0);
  1512. break;
  1513. case PORT_D:
  1514. port_mask = DPLL_PORTD_READY_MASK;
  1515. dpll_reg = DPIO_PHY_STATUS;
  1516. break;
  1517. default:
  1518. BUG();
  1519. }
  1520. if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
  1521. WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
  1522. port_name(dport->port), I915_READ(dpll_reg));
  1523. }
  1524. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1525. {
  1526. struct drm_device *dev = crtc->base.dev;
  1527. struct drm_i915_private *dev_priv = dev->dev_private;
  1528. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1529. if (WARN_ON(pll == NULL))
  1530. return;
  1531. WARN_ON(!pll->refcount);
  1532. if (pll->active == 0) {
  1533. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1534. WARN_ON(pll->on);
  1535. assert_shared_dpll_disabled(dev_priv, pll);
  1536. pll->mode_set(dev_priv, pll);
  1537. }
  1538. }
  1539. /**
  1540. * intel_enable_shared_dpll - enable PCH PLL
  1541. * @dev_priv: i915 private structure
  1542. * @pipe: pipe PLL to enable
  1543. *
  1544. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1545. * drives the transcoder clock.
  1546. */
  1547. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1548. {
  1549. struct drm_device *dev = crtc->base.dev;
  1550. struct drm_i915_private *dev_priv = dev->dev_private;
  1551. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1552. if (WARN_ON(pll == NULL))
  1553. return;
  1554. if (WARN_ON(pll->refcount == 0))
  1555. return;
  1556. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1557. pll->name, pll->active, pll->on,
  1558. crtc->base.base.id);
  1559. if (pll->active++) {
  1560. WARN_ON(!pll->on);
  1561. assert_shared_dpll_enabled(dev_priv, pll);
  1562. return;
  1563. }
  1564. WARN_ON(pll->on);
  1565. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1566. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1567. pll->enable(dev_priv, pll);
  1568. pll->on = true;
  1569. }
  1570. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1571. {
  1572. struct drm_device *dev = crtc->base.dev;
  1573. struct drm_i915_private *dev_priv = dev->dev_private;
  1574. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1575. /* PCH only available on ILK+ */
  1576. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1577. if (WARN_ON(pll == NULL))
  1578. return;
  1579. if (WARN_ON(pll->refcount == 0))
  1580. return;
  1581. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1582. pll->name, pll->active, pll->on,
  1583. crtc->base.base.id);
  1584. if (WARN_ON(pll->active == 0)) {
  1585. assert_shared_dpll_disabled(dev_priv, pll);
  1586. return;
  1587. }
  1588. assert_shared_dpll_enabled(dev_priv, pll);
  1589. WARN_ON(!pll->on);
  1590. if (--pll->active)
  1591. return;
  1592. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1593. pll->disable(dev_priv, pll);
  1594. pll->on = false;
  1595. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1596. }
  1597. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1598. enum pipe pipe)
  1599. {
  1600. struct drm_device *dev = dev_priv->dev;
  1601. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1602. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1603. uint32_t reg, val, pipeconf_val;
  1604. /* PCH only available on ILK+ */
  1605. BUG_ON(!HAS_PCH_SPLIT(dev));
  1606. /* Make sure PCH DPLL is enabled */
  1607. assert_shared_dpll_enabled(dev_priv,
  1608. intel_crtc_to_shared_dpll(intel_crtc));
  1609. /* FDI must be feeding us bits for PCH ports */
  1610. assert_fdi_tx_enabled(dev_priv, pipe);
  1611. assert_fdi_rx_enabled(dev_priv, pipe);
  1612. if (HAS_PCH_CPT(dev)) {
  1613. /* Workaround: Set the timing override bit before enabling the
  1614. * pch transcoder. */
  1615. reg = TRANS_CHICKEN2(pipe);
  1616. val = I915_READ(reg);
  1617. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1618. I915_WRITE(reg, val);
  1619. }
  1620. reg = PCH_TRANSCONF(pipe);
  1621. val = I915_READ(reg);
  1622. pipeconf_val = I915_READ(PIPECONF(pipe));
  1623. if (HAS_PCH_IBX(dev_priv->dev)) {
  1624. /*
  1625. * make the BPC in transcoder be consistent with
  1626. * that in pipeconf reg.
  1627. */
  1628. val &= ~PIPECONF_BPC_MASK;
  1629. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1630. }
  1631. val &= ~TRANS_INTERLACE_MASK;
  1632. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1633. if (HAS_PCH_IBX(dev_priv->dev) &&
  1634. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1635. val |= TRANS_LEGACY_INTERLACED_ILK;
  1636. else
  1637. val |= TRANS_INTERLACED;
  1638. else
  1639. val |= TRANS_PROGRESSIVE;
  1640. I915_WRITE(reg, val | TRANS_ENABLE);
  1641. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1642. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1643. }
  1644. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1645. enum transcoder cpu_transcoder)
  1646. {
  1647. u32 val, pipeconf_val;
  1648. /* PCH only available on ILK+ */
  1649. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1650. /* FDI must be feeding us bits for PCH ports */
  1651. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1652. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1653. /* Workaround: set timing override bit. */
  1654. val = I915_READ(_TRANSA_CHICKEN2);
  1655. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1656. I915_WRITE(_TRANSA_CHICKEN2, val);
  1657. val = TRANS_ENABLE;
  1658. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1659. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1660. PIPECONF_INTERLACED_ILK)
  1661. val |= TRANS_INTERLACED;
  1662. else
  1663. val |= TRANS_PROGRESSIVE;
  1664. I915_WRITE(LPT_TRANSCONF, val);
  1665. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1666. DRM_ERROR("Failed to enable PCH transcoder\n");
  1667. }
  1668. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1669. enum pipe pipe)
  1670. {
  1671. struct drm_device *dev = dev_priv->dev;
  1672. uint32_t reg, val;
  1673. /* FDI relies on the transcoder */
  1674. assert_fdi_tx_disabled(dev_priv, pipe);
  1675. assert_fdi_rx_disabled(dev_priv, pipe);
  1676. /* Ports must be off as well */
  1677. assert_pch_ports_disabled(dev_priv, pipe);
  1678. reg = PCH_TRANSCONF(pipe);
  1679. val = I915_READ(reg);
  1680. val &= ~TRANS_ENABLE;
  1681. I915_WRITE(reg, val);
  1682. /* wait for PCH transcoder off, transcoder state */
  1683. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1684. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1685. if (!HAS_PCH_IBX(dev)) {
  1686. /* Workaround: Clear the timing override chicken bit again. */
  1687. reg = TRANS_CHICKEN2(pipe);
  1688. val = I915_READ(reg);
  1689. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1690. I915_WRITE(reg, val);
  1691. }
  1692. }
  1693. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1694. {
  1695. u32 val;
  1696. val = I915_READ(LPT_TRANSCONF);
  1697. val &= ~TRANS_ENABLE;
  1698. I915_WRITE(LPT_TRANSCONF, val);
  1699. /* wait for PCH transcoder off, transcoder state */
  1700. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1701. DRM_ERROR("Failed to disable PCH transcoder\n");
  1702. /* Workaround: clear timing override bit. */
  1703. val = I915_READ(_TRANSA_CHICKEN2);
  1704. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1705. I915_WRITE(_TRANSA_CHICKEN2, val);
  1706. }
  1707. /**
  1708. * intel_enable_pipe - enable a pipe, asserting requirements
  1709. * @crtc: crtc responsible for the pipe
  1710. *
  1711. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1712. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1713. */
  1714. static void intel_enable_pipe(struct intel_crtc *crtc)
  1715. {
  1716. struct drm_device *dev = crtc->base.dev;
  1717. struct drm_i915_private *dev_priv = dev->dev_private;
  1718. enum pipe pipe = crtc->pipe;
  1719. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1720. pipe);
  1721. enum pipe pch_transcoder;
  1722. int reg;
  1723. u32 val;
  1724. assert_planes_disabled(dev_priv, pipe);
  1725. assert_cursor_disabled(dev_priv, pipe);
  1726. assert_sprites_disabled(dev_priv, pipe);
  1727. if (HAS_PCH_LPT(dev_priv->dev))
  1728. pch_transcoder = TRANSCODER_A;
  1729. else
  1730. pch_transcoder = pipe;
  1731. /*
  1732. * A pipe without a PLL won't actually be able to drive bits from
  1733. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1734. * need the check.
  1735. */
  1736. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1737. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1738. assert_dsi_pll_enabled(dev_priv);
  1739. else
  1740. assert_pll_enabled(dev_priv, pipe);
  1741. else {
  1742. if (crtc->config.has_pch_encoder) {
  1743. /* if driving the PCH, we need FDI enabled */
  1744. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1745. assert_fdi_tx_pll_enabled(dev_priv,
  1746. (enum pipe) cpu_transcoder);
  1747. }
  1748. /* FIXME: assert CPU port conditions for SNB+ */
  1749. }
  1750. reg = PIPECONF(cpu_transcoder);
  1751. val = I915_READ(reg);
  1752. if (val & PIPECONF_ENABLE) {
  1753. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1754. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1755. return;
  1756. }
  1757. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1758. POSTING_READ(reg);
  1759. }
  1760. /**
  1761. * intel_disable_pipe - disable a pipe, asserting requirements
  1762. * @crtc: crtc whose pipes is to be disabled
  1763. *
  1764. * Disable the pipe of @crtc, making sure that various hardware
  1765. * specific requirements are met, if applicable, e.g. plane
  1766. * disabled, panel fitter off, etc.
  1767. *
  1768. * Will wait until the pipe has shut down before returning.
  1769. */
  1770. static void intel_disable_pipe(struct intel_crtc *crtc)
  1771. {
  1772. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1773. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  1774. enum pipe pipe = crtc->pipe;
  1775. int reg;
  1776. u32 val;
  1777. /*
  1778. * Make sure planes won't keep trying to pump pixels to us,
  1779. * or we might hang the display.
  1780. */
  1781. assert_planes_disabled(dev_priv, pipe);
  1782. assert_cursor_disabled(dev_priv, pipe);
  1783. assert_sprites_disabled(dev_priv, pipe);
  1784. reg = PIPECONF(cpu_transcoder);
  1785. val = I915_READ(reg);
  1786. if ((val & PIPECONF_ENABLE) == 0)
  1787. return;
  1788. /*
  1789. * Double wide has implications for planes
  1790. * so best keep it disabled when not needed.
  1791. */
  1792. if (crtc->config.double_wide)
  1793. val &= ~PIPECONF_DOUBLE_WIDE;
  1794. /* Don't disable pipe or pipe PLLs if needed */
  1795. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1796. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1797. val &= ~PIPECONF_ENABLE;
  1798. I915_WRITE(reg, val);
  1799. if ((val & PIPECONF_ENABLE) == 0)
  1800. intel_wait_for_pipe_off(crtc);
  1801. }
  1802. /*
  1803. * Plane regs are double buffered, going from enabled->disabled needs a
  1804. * trigger in order to latch. The display address reg provides this.
  1805. */
  1806. void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
  1807. enum plane plane)
  1808. {
  1809. struct drm_device *dev = dev_priv->dev;
  1810. u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
  1811. I915_WRITE(reg, I915_READ(reg));
  1812. POSTING_READ(reg);
  1813. }
  1814. /**
  1815. * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  1816. * @plane: plane to be enabled
  1817. * @crtc: crtc for the plane
  1818. *
  1819. * Enable @plane on @crtc, making sure that the pipe is running first.
  1820. */
  1821. static void intel_enable_primary_hw_plane(struct drm_plane *plane,
  1822. struct drm_crtc *crtc)
  1823. {
  1824. struct drm_device *dev = plane->dev;
  1825. struct drm_i915_private *dev_priv = dev->dev_private;
  1826. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1827. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1828. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1829. if (intel_crtc->primary_enabled)
  1830. return;
  1831. intel_crtc->primary_enabled = true;
  1832. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1833. crtc->x, crtc->y);
  1834. /*
  1835. * BDW signals flip done immediately if the plane
  1836. * is disabled, even if the plane enable is already
  1837. * armed to occur at the next vblank :(
  1838. */
  1839. if (IS_BROADWELL(dev))
  1840. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1841. }
  1842. /**
  1843. * intel_disable_primary_hw_plane - disable the primary hardware plane
  1844. * @plane: plane to be disabled
  1845. * @crtc: crtc for the plane
  1846. *
  1847. * Disable @plane on @crtc, making sure that the pipe is running first.
  1848. */
  1849. static void intel_disable_primary_hw_plane(struct drm_plane *plane,
  1850. struct drm_crtc *crtc)
  1851. {
  1852. struct drm_device *dev = plane->dev;
  1853. struct drm_i915_private *dev_priv = dev->dev_private;
  1854. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1855. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1856. if (!intel_crtc->primary_enabled)
  1857. return;
  1858. intel_crtc->primary_enabled = false;
  1859. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1860. crtc->x, crtc->y);
  1861. }
  1862. static bool need_vtd_wa(struct drm_device *dev)
  1863. {
  1864. #ifdef CONFIG_INTEL_IOMMU
  1865. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1866. return true;
  1867. #endif
  1868. return false;
  1869. }
  1870. static int intel_align_height(struct drm_device *dev, int height, bool tiled)
  1871. {
  1872. int tile_height;
  1873. tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
  1874. return ALIGN(height, tile_height);
  1875. }
  1876. int
  1877. intel_pin_and_fence_fb_obj(struct drm_device *dev,
  1878. struct drm_i915_gem_object *obj,
  1879. struct intel_engine_cs *pipelined)
  1880. {
  1881. struct drm_i915_private *dev_priv = dev->dev_private;
  1882. u32 alignment;
  1883. int ret;
  1884. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1885. switch (obj->tiling_mode) {
  1886. case I915_TILING_NONE:
  1887. if (INTEL_INFO(dev)->gen >= 9)
  1888. alignment = 256 * 1024;
  1889. else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1890. alignment = 128 * 1024;
  1891. else if (INTEL_INFO(dev)->gen >= 4)
  1892. alignment = 4 * 1024;
  1893. else
  1894. alignment = 64 * 1024;
  1895. break;
  1896. case I915_TILING_X:
  1897. if (INTEL_INFO(dev)->gen >= 9)
  1898. alignment = 256 * 1024;
  1899. else {
  1900. /* pin() will align the object as required by fence */
  1901. alignment = 0;
  1902. }
  1903. break;
  1904. case I915_TILING_Y:
  1905. WARN(1, "Y tiled bo slipped through, driver bug!\n");
  1906. return -EINVAL;
  1907. default:
  1908. BUG();
  1909. }
  1910. /* Note that the w/a also requires 64 PTE of padding following the
  1911. * bo. We currently fill all unused PTE with the shadow page and so
  1912. * we should always have valid PTE following the scanout preventing
  1913. * the VT-d warning.
  1914. */
  1915. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1916. alignment = 256 * 1024;
  1917. /*
  1918. * Global gtt pte registers are special registers which actually forward
  1919. * writes to a chunk of system memory. Which means that there is no risk
  1920. * that the register values disappear as soon as we call
  1921. * intel_runtime_pm_put(), so it is correct to wrap only the
  1922. * pin/unpin/fence and not more.
  1923. */
  1924. intel_runtime_pm_get(dev_priv);
  1925. dev_priv->mm.interruptible = false;
  1926. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1927. if (ret)
  1928. goto err_interruptible;
  1929. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1930. * fence, whereas 965+ only requires a fence if using
  1931. * framebuffer compression. For simplicity, we always install
  1932. * a fence as the cost is not that onerous.
  1933. */
  1934. ret = i915_gem_object_get_fence(obj);
  1935. if (ret)
  1936. goto err_unpin;
  1937. i915_gem_object_pin_fence(obj);
  1938. dev_priv->mm.interruptible = true;
  1939. intel_runtime_pm_put(dev_priv);
  1940. return 0;
  1941. err_unpin:
  1942. i915_gem_object_unpin_from_display_plane(obj);
  1943. err_interruptible:
  1944. dev_priv->mm.interruptible = true;
  1945. intel_runtime_pm_put(dev_priv);
  1946. return ret;
  1947. }
  1948. void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
  1949. {
  1950. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  1951. i915_gem_object_unpin_fence(obj);
  1952. i915_gem_object_unpin_from_display_plane(obj);
  1953. }
  1954. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  1955. * is assumed to be a power-of-two. */
  1956. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  1957. unsigned int tiling_mode,
  1958. unsigned int cpp,
  1959. unsigned int pitch)
  1960. {
  1961. if (tiling_mode != I915_TILING_NONE) {
  1962. unsigned int tile_rows, tiles;
  1963. tile_rows = *y / 8;
  1964. *y %= 8;
  1965. tiles = *x / (512/cpp);
  1966. *x %= 512/cpp;
  1967. return tile_rows * pitch * 8 + tiles * 4096;
  1968. } else {
  1969. unsigned int offset;
  1970. offset = *y * pitch + *x * cpp;
  1971. *y = 0;
  1972. *x = (offset & 4095) / cpp;
  1973. return offset & -4096;
  1974. }
  1975. }
  1976. int intel_format_to_fourcc(int format)
  1977. {
  1978. switch (format) {
  1979. case DISPPLANE_8BPP:
  1980. return DRM_FORMAT_C8;
  1981. case DISPPLANE_BGRX555:
  1982. return DRM_FORMAT_XRGB1555;
  1983. case DISPPLANE_BGRX565:
  1984. return DRM_FORMAT_RGB565;
  1985. default:
  1986. case DISPPLANE_BGRX888:
  1987. return DRM_FORMAT_XRGB8888;
  1988. case DISPPLANE_RGBX888:
  1989. return DRM_FORMAT_XBGR8888;
  1990. case DISPPLANE_BGRX101010:
  1991. return DRM_FORMAT_XRGB2101010;
  1992. case DISPPLANE_RGBX101010:
  1993. return DRM_FORMAT_XBGR2101010;
  1994. }
  1995. }
  1996. static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
  1997. struct intel_plane_config *plane_config)
  1998. {
  1999. struct drm_device *dev = crtc->base.dev;
  2000. struct drm_i915_gem_object *obj = NULL;
  2001. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2002. u32 base = plane_config->base;
  2003. if (plane_config->size == 0)
  2004. return false;
  2005. obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
  2006. plane_config->size);
  2007. if (!obj)
  2008. return false;
  2009. if (plane_config->tiled) {
  2010. obj->tiling_mode = I915_TILING_X;
  2011. obj->stride = crtc->base.primary->fb->pitches[0];
  2012. }
  2013. mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
  2014. mode_cmd.width = crtc->base.primary->fb->width;
  2015. mode_cmd.height = crtc->base.primary->fb->height;
  2016. mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
  2017. mutex_lock(&dev->struct_mutex);
  2018. if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
  2019. &mode_cmd, obj)) {
  2020. DRM_DEBUG_KMS("intel fb init failed\n");
  2021. goto out_unref_obj;
  2022. }
  2023. obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
  2024. mutex_unlock(&dev->struct_mutex);
  2025. DRM_DEBUG_KMS("plane fb obj %p\n", obj);
  2026. return true;
  2027. out_unref_obj:
  2028. drm_gem_object_unreference(&obj->base);
  2029. mutex_unlock(&dev->struct_mutex);
  2030. return false;
  2031. }
  2032. static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
  2033. struct intel_plane_config *plane_config)
  2034. {
  2035. struct drm_device *dev = intel_crtc->base.dev;
  2036. struct drm_i915_private *dev_priv = dev->dev_private;
  2037. struct drm_crtc *c;
  2038. struct intel_crtc *i;
  2039. struct drm_i915_gem_object *obj;
  2040. if (!intel_crtc->base.primary->fb)
  2041. return;
  2042. if (intel_alloc_plane_obj(intel_crtc, plane_config))
  2043. return;
  2044. kfree(intel_crtc->base.primary->fb);
  2045. intel_crtc->base.primary->fb = NULL;
  2046. /*
  2047. * Failed to alloc the obj, check to see if we should share
  2048. * an fb with another CRTC instead
  2049. */
  2050. for_each_crtc(dev, c) {
  2051. i = to_intel_crtc(c);
  2052. if (c == &intel_crtc->base)
  2053. continue;
  2054. if (!i->active)
  2055. continue;
  2056. obj = intel_fb_obj(c->primary->fb);
  2057. if (obj == NULL)
  2058. continue;
  2059. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2060. if (obj->tiling_mode != I915_TILING_NONE)
  2061. dev_priv->preserve_bios_swizzle = true;
  2062. drm_framebuffer_reference(c->primary->fb);
  2063. intel_crtc->base.primary->fb = c->primary->fb;
  2064. obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  2065. break;
  2066. }
  2067. }
  2068. }
  2069. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2070. struct drm_framebuffer *fb,
  2071. int x, int y)
  2072. {
  2073. struct drm_device *dev = crtc->dev;
  2074. struct drm_i915_private *dev_priv = dev->dev_private;
  2075. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2076. struct drm_i915_gem_object *obj;
  2077. int plane = intel_crtc->plane;
  2078. unsigned long linear_offset;
  2079. u32 dspcntr;
  2080. u32 reg = DSPCNTR(plane);
  2081. int pixel_size;
  2082. if (!intel_crtc->primary_enabled) {
  2083. I915_WRITE(reg, 0);
  2084. if (INTEL_INFO(dev)->gen >= 4)
  2085. I915_WRITE(DSPSURF(plane), 0);
  2086. else
  2087. I915_WRITE(DSPADDR(plane), 0);
  2088. POSTING_READ(reg);
  2089. return;
  2090. }
  2091. obj = intel_fb_obj(fb);
  2092. if (WARN_ON(obj == NULL))
  2093. return;
  2094. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2095. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2096. dspcntr |= DISPLAY_PLANE_ENABLE;
  2097. if (INTEL_INFO(dev)->gen < 4) {
  2098. if (intel_crtc->pipe == PIPE_B)
  2099. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2100. /* pipesrc and dspsize control the size that is scaled from,
  2101. * which should always be the user's requested size.
  2102. */
  2103. I915_WRITE(DSPSIZE(plane),
  2104. ((intel_crtc->config.pipe_src_h - 1) << 16) |
  2105. (intel_crtc->config.pipe_src_w - 1));
  2106. I915_WRITE(DSPPOS(plane), 0);
  2107. }
  2108. switch (fb->pixel_format) {
  2109. case DRM_FORMAT_C8:
  2110. dspcntr |= DISPPLANE_8BPP;
  2111. break;
  2112. case DRM_FORMAT_XRGB1555:
  2113. case DRM_FORMAT_ARGB1555:
  2114. dspcntr |= DISPPLANE_BGRX555;
  2115. break;
  2116. case DRM_FORMAT_RGB565:
  2117. dspcntr |= DISPPLANE_BGRX565;
  2118. break;
  2119. case DRM_FORMAT_XRGB8888:
  2120. case DRM_FORMAT_ARGB8888:
  2121. dspcntr |= DISPPLANE_BGRX888;
  2122. break;
  2123. case DRM_FORMAT_XBGR8888:
  2124. case DRM_FORMAT_ABGR8888:
  2125. dspcntr |= DISPPLANE_RGBX888;
  2126. break;
  2127. case DRM_FORMAT_XRGB2101010:
  2128. case DRM_FORMAT_ARGB2101010:
  2129. dspcntr |= DISPPLANE_BGRX101010;
  2130. break;
  2131. case DRM_FORMAT_XBGR2101010:
  2132. case DRM_FORMAT_ABGR2101010:
  2133. dspcntr |= DISPPLANE_RGBX101010;
  2134. break;
  2135. default:
  2136. BUG();
  2137. }
  2138. if (INTEL_INFO(dev)->gen >= 4 &&
  2139. obj->tiling_mode != I915_TILING_NONE)
  2140. dspcntr |= DISPPLANE_TILED;
  2141. if (IS_G4X(dev))
  2142. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2143. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2144. if (INTEL_INFO(dev)->gen >= 4) {
  2145. intel_crtc->dspaddr_offset =
  2146. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2147. pixel_size,
  2148. fb->pitches[0]);
  2149. linear_offset -= intel_crtc->dspaddr_offset;
  2150. } else {
  2151. intel_crtc->dspaddr_offset = linear_offset;
  2152. }
  2153. if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
  2154. dspcntr |= DISPPLANE_ROTATE_180;
  2155. x += (intel_crtc->config.pipe_src_w - 1);
  2156. y += (intel_crtc->config.pipe_src_h - 1);
  2157. /* Finding the last pixel of the last line of the display
  2158. data and adding to linear_offset*/
  2159. linear_offset +=
  2160. (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
  2161. (intel_crtc->config.pipe_src_w - 1) * pixel_size;
  2162. }
  2163. I915_WRITE(reg, dspcntr);
  2164. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2165. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2166. fb->pitches[0]);
  2167. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2168. if (INTEL_INFO(dev)->gen >= 4) {
  2169. I915_WRITE(DSPSURF(plane),
  2170. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2171. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2172. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2173. } else
  2174. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2175. POSTING_READ(reg);
  2176. }
  2177. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2178. struct drm_framebuffer *fb,
  2179. int x, int y)
  2180. {
  2181. struct drm_device *dev = crtc->dev;
  2182. struct drm_i915_private *dev_priv = dev->dev_private;
  2183. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2184. struct drm_i915_gem_object *obj;
  2185. int plane = intel_crtc->plane;
  2186. unsigned long linear_offset;
  2187. u32 dspcntr;
  2188. u32 reg = DSPCNTR(plane);
  2189. int pixel_size;
  2190. if (!intel_crtc->primary_enabled) {
  2191. I915_WRITE(reg, 0);
  2192. I915_WRITE(DSPSURF(plane), 0);
  2193. POSTING_READ(reg);
  2194. return;
  2195. }
  2196. obj = intel_fb_obj(fb);
  2197. if (WARN_ON(obj == NULL))
  2198. return;
  2199. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2200. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2201. dspcntr |= DISPLAY_PLANE_ENABLE;
  2202. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2203. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2204. switch (fb->pixel_format) {
  2205. case DRM_FORMAT_C8:
  2206. dspcntr |= DISPPLANE_8BPP;
  2207. break;
  2208. case DRM_FORMAT_RGB565:
  2209. dspcntr |= DISPPLANE_BGRX565;
  2210. break;
  2211. case DRM_FORMAT_XRGB8888:
  2212. case DRM_FORMAT_ARGB8888:
  2213. dspcntr |= DISPPLANE_BGRX888;
  2214. break;
  2215. case DRM_FORMAT_XBGR8888:
  2216. case DRM_FORMAT_ABGR8888:
  2217. dspcntr |= DISPPLANE_RGBX888;
  2218. break;
  2219. case DRM_FORMAT_XRGB2101010:
  2220. case DRM_FORMAT_ARGB2101010:
  2221. dspcntr |= DISPPLANE_BGRX101010;
  2222. break;
  2223. case DRM_FORMAT_XBGR2101010:
  2224. case DRM_FORMAT_ABGR2101010:
  2225. dspcntr |= DISPPLANE_RGBX101010;
  2226. break;
  2227. default:
  2228. BUG();
  2229. }
  2230. if (obj->tiling_mode != I915_TILING_NONE)
  2231. dspcntr |= DISPPLANE_TILED;
  2232. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2233. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2234. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2235. intel_crtc->dspaddr_offset =
  2236. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2237. pixel_size,
  2238. fb->pitches[0]);
  2239. linear_offset -= intel_crtc->dspaddr_offset;
  2240. if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
  2241. dspcntr |= DISPPLANE_ROTATE_180;
  2242. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2243. x += (intel_crtc->config.pipe_src_w - 1);
  2244. y += (intel_crtc->config.pipe_src_h - 1);
  2245. /* Finding the last pixel of the last line of the display
  2246. data and adding to linear_offset*/
  2247. linear_offset +=
  2248. (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
  2249. (intel_crtc->config.pipe_src_w - 1) * pixel_size;
  2250. }
  2251. }
  2252. I915_WRITE(reg, dspcntr);
  2253. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2254. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2255. fb->pitches[0]);
  2256. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2257. I915_WRITE(DSPSURF(plane),
  2258. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2259. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2260. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2261. } else {
  2262. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2263. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2264. }
  2265. POSTING_READ(reg);
  2266. }
  2267. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2268. struct drm_framebuffer *fb,
  2269. int x, int y)
  2270. {
  2271. struct drm_device *dev = crtc->dev;
  2272. struct drm_i915_private *dev_priv = dev->dev_private;
  2273. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2274. struct intel_framebuffer *intel_fb;
  2275. struct drm_i915_gem_object *obj;
  2276. int pipe = intel_crtc->pipe;
  2277. u32 plane_ctl, stride;
  2278. if (!intel_crtc->primary_enabled) {
  2279. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2280. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2281. POSTING_READ(PLANE_CTL(pipe, 0));
  2282. return;
  2283. }
  2284. plane_ctl = PLANE_CTL_ENABLE |
  2285. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2286. PLANE_CTL_PIPE_CSC_ENABLE;
  2287. switch (fb->pixel_format) {
  2288. case DRM_FORMAT_RGB565:
  2289. plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
  2290. break;
  2291. case DRM_FORMAT_XRGB8888:
  2292. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2293. break;
  2294. case DRM_FORMAT_XBGR8888:
  2295. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2296. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2297. break;
  2298. case DRM_FORMAT_XRGB2101010:
  2299. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2300. break;
  2301. case DRM_FORMAT_XBGR2101010:
  2302. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2303. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2304. break;
  2305. default:
  2306. BUG();
  2307. }
  2308. intel_fb = to_intel_framebuffer(fb);
  2309. obj = intel_fb->obj;
  2310. /*
  2311. * The stride is either expressed as a multiple of 64 bytes chunks for
  2312. * linear buffers or in number of tiles for tiled buffers.
  2313. */
  2314. switch (obj->tiling_mode) {
  2315. case I915_TILING_NONE:
  2316. stride = fb->pitches[0] >> 6;
  2317. break;
  2318. case I915_TILING_X:
  2319. plane_ctl |= PLANE_CTL_TILED_X;
  2320. stride = fb->pitches[0] >> 9;
  2321. break;
  2322. default:
  2323. BUG();
  2324. }
  2325. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2326. if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180))
  2327. plane_ctl |= PLANE_CTL_ROTATE_180;
  2328. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2329. DRM_DEBUG_KMS("Writing base %08lX %d,%d,%d,%d pitch=%d\n",
  2330. i915_gem_obj_ggtt_offset(obj),
  2331. x, y, fb->width, fb->height,
  2332. fb->pitches[0]);
  2333. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2334. I915_WRITE(PLANE_OFFSET(pipe, 0), (y << 16) | x);
  2335. I915_WRITE(PLANE_SIZE(pipe, 0),
  2336. (intel_crtc->config.pipe_src_h - 1) << 16 |
  2337. (intel_crtc->config.pipe_src_w - 1));
  2338. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2339. I915_WRITE(PLANE_SURF(pipe, 0), i915_gem_obj_ggtt_offset(obj));
  2340. POSTING_READ(PLANE_SURF(pipe, 0));
  2341. }
  2342. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2343. static int
  2344. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2345. int x, int y, enum mode_set_atomic state)
  2346. {
  2347. struct drm_device *dev = crtc->dev;
  2348. struct drm_i915_private *dev_priv = dev->dev_private;
  2349. if (dev_priv->display.disable_fbc)
  2350. dev_priv->display.disable_fbc(dev);
  2351. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2352. return 0;
  2353. }
  2354. void intel_display_handle_reset(struct drm_device *dev)
  2355. {
  2356. struct drm_i915_private *dev_priv = dev->dev_private;
  2357. struct drm_crtc *crtc;
  2358. /*
  2359. * Flips in the rings have been nuked by the reset,
  2360. * so complete all pending flips so that user space
  2361. * will get its events and not get stuck.
  2362. *
  2363. * Also update the base address of all primary
  2364. * planes to the the last fb to make sure we're
  2365. * showing the correct fb after a reset.
  2366. *
  2367. * Need to make two loops over the crtcs so that we
  2368. * don't try to grab a crtc mutex before the
  2369. * pending_flip_queue really got woken up.
  2370. */
  2371. for_each_crtc(dev, crtc) {
  2372. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2373. enum plane plane = intel_crtc->plane;
  2374. intel_prepare_page_flip(dev, plane);
  2375. intel_finish_page_flip_plane(dev, plane);
  2376. }
  2377. for_each_crtc(dev, crtc) {
  2378. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2379. drm_modeset_lock(&crtc->mutex, NULL);
  2380. /*
  2381. * FIXME: Once we have proper support for primary planes (and
  2382. * disabling them without disabling the entire crtc) allow again
  2383. * a NULL crtc->primary->fb.
  2384. */
  2385. if (intel_crtc->active && crtc->primary->fb)
  2386. dev_priv->display.update_primary_plane(crtc,
  2387. crtc->primary->fb,
  2388. crtc->x,
  2389. crtc->y);
  2390. drm_modeset_unlock(&crtc->mutex);
  2391. }
  2392. }
  2393. static int
  2394. intel_finish_fb(struct drm_framebuffer *old_fb)
  2395. {
  2396. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2397. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2398. bool was_interruptible = dev_priv->mm.interruptible;
  2399. int ret;
  2400. /* Big Hammer, we also need to ensure that any pending
  2401. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2402. * current scanout is retired before unpinning the old
  2403. * framebuffer.
  2404. *
  2405. * This should only fail upon a hung GPU, in which case we
  2406. * can safely continue.
  2407. */
  2408. dev_priv->mm.interruptible = false;
  2409. ret = i915_gem_object_finish_gpu(obj);
  2410. dev_priv->mm.interruptible = was_interruptible;
  2411. return ret;
  2412. }
  2413. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2414. {
  2415. struct drm_device *dev = crtc->dev;
  2416. struct drm_i915_private *dev_priv = dev->dev_private;
  2417. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2418. bool pending;
  2419. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2420. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2421. return false;
  2422. spin_lock_irq(&dev->event_lock);
  2423. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2424. spin_unlock_irq(&dev->event_lock);
  2425. return pending;
  2426. }
  2427. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2428. {
  2429. struct drm_device *dev = crtc->base.dev;
  2430. struct drm_i915_private *dev_priv = dev->dev_private;
  2431. const struct drm_display_mode *adjusted_mode;
  2432. if (!i915.fastboot)
  2433. return;
  2434. /*
  2435. * Update pipe size and adjust fitter if needed: the reason for this is
  2436. * that in compute_mode_changes we check the native mode (not the pfit
  2437. * mode) to see if we can flip rather than do a full mode set. In the
  2438. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2439. * pfit state, we'll end up with a big fb scanned out into the wrong
  2440. * sized surface.
  2441. *
  2442. * To fix this properly, we need to hoist the checks up into
  2443. * compute_mode_changes (or above), check the actual pfit state and
  2444. * whether the platform allows pfit disable with pipe active, and only
  2445. * then update the pipesrc and pfit state, even on the flip path.
  2446. */
  2447. adjusted_mode = &crtc->config.adjusted_mode;
  2448. I915_WRITE(PIPESRC(crtc->pipe),
  2449. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2450. (adjusted_mode->crtc_vdisplay - 1));
  2451. if (!crtc->config.pch_pfit.enabled &&
  2452. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2453. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2454. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2455. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2456. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2457. }
  2458. crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
  2459. crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
  2460. }
  2461. static int
  2462. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  2463. struct drm_framebuffer *fb)
  2464. {
  2465. struct drm_device *dev = crtc->dev;
  2466. struct drm_i915_private *dev_priv = dev->dev_private;
  2467. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2468. enum pipe pipe = intel_crtc->pipe;
  2469. struct drm_framebuffer *old_fb = crtc->primary->fb;
  2470. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2471. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
  2472. int ret;
  2473. if (intel_crtc_has_pending_flip(crtc)) {
  2474. DRM_ERROR("pipe is still busy with an old pageflip\n");
  2475. return -EBUSY;
  2476. }
  2477. /* no fb bound */
  2478. if (!fb) {
  2479. DRM_ERROR("No FB bound\n");
  2480. return 0;
  2481. }
  2482. if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
  2483. DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
  2484. plane_name(intel_crtc->plane),
  2485. INTEL_INFO(dev)->num_pipes);
  2486. return -EINVAL;
  2487. }
  2488. mutex_lock(&dev->struct_mutex);
  2489. ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
  2490. if (ret == 0)
  2491. i915_gem_track_fb(old_obj, obj,
  2492. INTEL_FRONTBUFFER_PRIMARY(pipe));
  2493. mutex_unlock(&dev->struct_mutex);
  2494. if (ret != 0) {
  2495. DRM_ERROR("pin & fence failed\n");
  2496. return ret;
  2497. }
  2498. intel_update_pipe_size(intel_crtc);
  2499. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2500. if (intel_crtc->active)
  2501. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  2502. crtc->primary->fb = fb;
  2503. crtc->x = x;
  2504. crtc->y = y;
  2505. if (old_fb) {
  2506. if (intel_crtc->active && old_fb != fb)
  2507. intel_wait_for_vblank(dev, intel_crtc->pipe);
  2508. mutex_lock(&dev->struct_mutex);
  2509. intel_unpin_fb_obj(old_obj);
  2510. mutex_unlock(&dev->struct_mutex);
  2511. }
  2512. mutex_lock(&dev->struct_mutex);
  2513. intel_update_fbc(dev);
  2514. mutex_unlock(&dev->struct_mutex);
  2515. return 0;
  2516. }
  2517. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2518. {
  2519. struct drm_device *dev = crtc->dev;
  2520. struct drm_i915_private *dev_priv = dev->dev_private;
  2521. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2522. int pipe = intel_crtc->pipe;
  2523. u32 reg, temp;
  2524. /* enable normal train */
  2525. reg = FDI_TX_CTL(pipe);
  2526. temp = I915_READ(reg);
  2527. if (IS_IVYBRIDGE(dev)) {
  2528. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2529. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2530. } else {
  2531. temp &= ~FDI_LINK_TRAIN_NONE;
  2532. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2533. }
  2534. I915_WRITE(reg, temp);
  2535. reg = FDI_RX_CTL(pipe);
  2536. temp = I915_READ(reg);
  2537. if (HAS_PCH_CPT(dev)) {
  2538. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2539. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2540. } else {
  2541. temp &= ~FDI_LINK_TRAIN_NONE;
  2542. temp |= FDI_LINK_TRAIN_NONE;
  2543. }
  2544. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2545. /* wait one idle pattern time */
  2546. POSTING_READ(reg);
  2547. udelay(1000);
  2548. /* IVB wants error correction enabled */
  2549. if (IS_IVYBRIDGE(dev))
  2550. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2551. FDI_FE_ERRC_ENABLE);
  2552. }
  2553. static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
  2554. {
  2555. return crtc->base.enabled && crtc->active &&
  2556. crtc->config.has_pch_encoder;
  2557. }
  2558. static void ivb_modeset_global_resources(struct drm_device *dev)
  2559. {
  2560. struct drm_i915_private *dev_priv = dev->dev_private;
  2561. struct intel_crtc *pipe_B_crtc =
  2562. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  2563. struct intel_crtc *pipe_C_crtc =
  2564. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
  2565. uint32_t temp;
  2566. /*
  2567. * When everything is off disable fdi C so that we could enable fdi B
  2568. * with all lanes. Note that we don't care about enabled pipes without
  2569. * an enabled pch encoder.
  2570. */
  2571. if (!pipe_has_enabled_pch(pipe_B_crtc) &&
  2572. !pipe_has_enabled_pch(pipe_C_crtc)) {
  2573. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2574. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2575. temp = I915_READ(SOUTH_CHICKEN1);
  2576. temp &= ~FDI_BC_BIFURCATION_SELECT;
  2577. DRM_DEBUG_KMS("disabling fdi C rx\n");
  2578. I915_WRITE(SOUTH_CHICKEN1, temp);
  2579. }
  2580. }
  2581. /* The FDI link training functions for ILK/Ibexpeak. */
  2582. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2583. {
  2584. struct drm_device *dev = crtc->dev;
  2585. struct drm_i915_private *dev_priv = dev->dev_private;
  2586. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2587. int pipe = intel_crtc->pipe;
  2588. u32 reg, temp, tries;
  2589. /* FDI needs bits from pipe first */
  2590. assert_pipe_enabled(dev_priv, pipe);
  2591. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2592. for train result */
  2593. reg = FDI_RX_IMR(pipe);
  2594. temp = I915_READ(reg);
  2595. temp &= ~FDI_RX_SYMBOL_LOCK;
  2596. temp &= ~FDI_RX_BIT_LOCK;
  2597. I915_WRITE(reg, temp);
  2598. I915_READ(reg);
  2599. udelay(150);
  2600. /* enable CPU FDI TX and PCH FDI RX */
  2601. reg = FDI_TX_CTL(pipe);
  2602. temp = I915_READ(reg);
  2603. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2604. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2605. temp &= ~FDI_LINK_TRAIN_NONE;
  2606. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2607. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2608. reg = FDI_RX_CTL(pipe);
  2609. temp = I915_READ(reg);
  2610. temp &= ~FDI_LINK_TRAIN_NONE;
  2611. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2612. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2613. POSTING_READ(reg);
  2614. udelay(150);
  2615. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2616. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2617. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2618. FDI_RX_PHASE_SYNC_POINTER_EN);
  2619. reg = FDI_RX_IIR(pipe);
  2620. for (tries = 0; tries < 5; tries++) {
  2621. temp = I915_READ(reg);
  2622. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2623. if ((temp & FDI_RX_BIT_LOCK)) {
  2624. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2625. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2626. break;
  2627. }
  2628. }
  2629. if (tries == 5)
  2630. DRM_ERROR("FDI train 1 fail!\n");
  2631. /* Train 2 */
  2632. reg = FDI_TX_CTL(pipe);
  2633. temp = I915_READ(reg);
  2634. temp &= ~FDI_LINK_TRAIN_NONE;
  2635. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2636. I915_WRITE(reg, temp);
  2637. reg = FDI_RX_CTL(pipe);
  2638. temp = I915_READ(reg);
  2639. temp &= ~FDI_LINK_TRAIN_NONE;
  2640. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2641. I915_WRITE(reg, temp);
  2642. POSTING_READ(reg);
  2643. udelay(150);
  2644. reg = FDI_RX_IIR(pipe);
  2645. for (tries = 0; tries < 5; tries++) {
  2646. temp = I915_READ(reg);
  2647. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2648. if (temp & FDI_RX_SYMBOL_LOCK) {
  2649. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2650. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2651. break;
  2652. }
  2653. }
  2654. if (tries == 5)
  2655. DRM_ERROR("FDI train 2 fail!\n");
  2656. DRM_DEBUG_KMS("FDI train done\n");
  2657. }
  2658. static const int snb_b_fdi_train_param[] = {
  2659. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2660. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2661. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2662. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2663. };
  2664. /* The FDI link training functions for SNB/Cougarpoint. */
  2665. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2666. {
  2667. struct drm_device *dev = crtc->dev;
  2668. struct drm_i915_private *dev_priv = dev->dev_private;
  2669. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2670. int pipe = intel_crtc->pipe;
  2671. u32 reg, temp, i, retry;
  2672. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2673. for train result */
  2674. reg = FDI_RX_IMR(pipe);
  2675. temp = I915_READ(reg);
  2676. temp &= ~FDI_RX_SYMBOL_LOCK;
  2677. temp &= ~FDI_RX_BIT_LOCK;
  2678. I915_WRITE(reg, temp);
  2679. POSTING_READ(reg);
  2680. udelay(150);
  2681. /* enable CPU FDI TX and PCH FDI RX */
  2682. reg = FDI_TX_CTL(pipe);
  2683. temp = I915_READ(reg);
  2684. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2685. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2686. temp &= ~FDI_LINK_TRAIN_NONE;
  2687. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2688. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2689. /* SNB-B */
  2690. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2691. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2692. I915_WRITE(FDI_RX_MISC(pipe),
  2693. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2694. reg = FDI_RX_CTL(pipe);
  2695. temp = I915_READ(reg);
  2696. if (HAS_PCH_CPT(dev)) {
  2697. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2698. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2699. } else {
  2700. temp &= ~FDI_LINK_TRAIN_NONE;
  2701. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2702. }
  2703. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2704. POSTING_READ(reg);
  2705. udelay(150);
  2706. for (i = 0; i < 4; i++) {
  2707. reg = FDI_TX_CTL(pipe);
  2708. temp = I915_READ(reg);
  2709. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2710. temp |= snb_b_fdi_train_param[i];
  2711. I915_WRITE(reg, temp);
  2712. POSTING_READ(reg);
  2713. udelay(500);
  2714. for (retry = 0; retry < 5; retry++) {
  2715. reg = FDI_RX_IIR(pipe);
  2716. temp = I915_READ(reg);
  2717. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2718. if (temp & FDI_RX_BIT_LOCK) {
  2719. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2720. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2721. break;
  2722. }
  2723. udelay(50);
  2724. }
  2725. if (retry < 5)
  2726. break;
  2727. }
  2728. if (i == 4)
  2729. DRM_ERROR("FDI train 1 fail!\n");
  2730. /* Train 2 */
  2731. reg = FDI_TX_CTL(pipe);
  2732. temp = I915_READ(reg);
  2733. temp &= ~FDI_LINK_TRAIN_NONE;
  2734. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2735. if (IS_GEN6(dev)) {
  2736. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2737. /* SNB-B */
  2738. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2739. }
  2740. I915_WRITE(reg, temp);
  2741. reg = FDI_RX_CTL(pipe);
  2742. temp = I915_READ(reg);
  2743. if (HAS_PCH_CPT(dev)) {
  2744. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2745. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2746. } else {
  2747. temp &= ~FDI_LINK_TRAIN_NONE;
  2748. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2749. }
  2750. I915_WRITE(reg, temp);
  2751. POSTING_READ(reg);
  2752. udelay(150);
  2753. for (i = 0; i < 4; i++) {
  2754. reg = FDI_TX_CTL(pipe);
  2755. temp = I915_READ(reg);
  2756. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2757. temp |= snb_b_fdi_train_param[i];
  2758. I915_WRITE(reg, temp);
  2759. POSTING_READ(reg);
  2760. udelay(500);
  2761. for (retry = 0; retry < 5; retry++) {
  2762. reg = FDI_RX_IIR(pipe);
  2763. temp = I915_READ(reg);
  2764. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2765. if (temp & FDI_RX_SYMBOL_LOCK) {
  2766. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2767. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2768. break;
  2769. }
  2770. udelay(50);
  2771. }
  2772. if (retry < 5)
  2773. break;
  2774. }
  2775. if (i == 4)
  2776. DRM_ERROR("FDI train 2 fail!\n");
  2777. DRM_DEBUG_KMS("FDI train done.\n");
  2778. }
  2779. /* Manual link training for Ivy Bridge A0 parts */
  2780. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2781. {
  2782. struct drm_device *dev = crtc->dev;
  2783. struct drm_i915_private *dev_priv = dev->dev_private;
  2784. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2785. int pipe = intel_crtc->pipe;
  2786. u32 reg, temp, i, j;
  2787. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2788. for train result */
  2789. reg = FDI_RX_IMR(pipe);
  2790. temp = I915_READ(reg);
  2791. temp &= ~FDI_RX_SYMBOL_LOCK;
  2792. temp &= ~FDI_RX_BIT_LOCK;
  2793. I915_WRITE(reg, temp);
  2794. POSTING_READ(reg);
  2795. udelay(150);
  2796. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  2797. I915_READ(FDI_RX_IIR(pipe)));
  2798. /* Try each vswing and preemphasis setting twice before moving on */
  2799. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  2800. /* disable first in case we need to retry */
  2801. reg = FDI_TX_CTL(pipe);
  2802. temp = I915_READ(reg);
  2803. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2804. temp &= ~FDI_TX_ENABLE;
  2805. I915_WRITE(reg, temp);
  2806. reg = FDI_RX_CTL(pipe);
  2807. temp = I915_READ(reg);
  2808. temp &= ~FDI_LINK_TRAIN_AUTO;
  2809. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2810. temp &= ~FDI_RX_ENABLE;
  2811. I915_WRITE(reg, temp);
  2812. /* enable CPU FDI TX and PCH FDI RX */
  2813. reg = FDI_TX_CTL(pipe);
  2814. temp = I915_READ(reg);
  2815. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2816. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2817. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2818. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2819. temp |= snb_b_fdi_train_param[j/2];
  2820. temp |= FDI_COMPOSITE_SYNC;
  2821. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2822. I915_WRITE(FDI_RX_MISC(pipe),
  2823. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2824. reg = FDI_RX_CTL(pipe);
  2825. temp = I915_READ(reg);
  2826. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2827. temp |= FDI_COMPOSITE_SYNC;
  2828. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2829. POSTING_READ(reg);
  2830. udelay(1); /* should be 0.5us */
  2831. for (i = 0; i < 4; i++) {
  2832. reg = FDI_RX_IIR(pipe);
  2833. temp = I915_READ(reg);
  2834. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2835. if (temp & FDI_RX_BIT_LOCK ||
  2836. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2837. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2838. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  2839. i);
  2840. break;
  2841. }
  2842. udelay(1); /* should be 0.5us */
  2843. }
  2844. if (i == 4) {
  2845. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  2846. continue;
  2847. }
  2848. /* Train 2 */
  2849. reg = FDI_TX_CTL(pipe);
  2850. temp = I915_READ(reg);
  2851. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2852. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2853. I915_WRITE(reg, temp);
  2854. reg = FDI_RX_CTL(pipe);
  2855. temp = I915_READ(reg);
  2856. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2857. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2858. I915_WRITE(reg, temp);
  2859. POSTING_READ(reg);
  2860. udelay(2); /* should be 1.5us */
  2861. for (i = 0; i < 4; i++) {
  2862. reg = FDI_RX_IIR(pipe);
  2863. temp = I915_READ(reg);
  2864. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2865. if (temp & FDI_RX_SYMBOL_LOCK ||
  2866. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  2867. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2868. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  2869. i);
  2870. goto train_done;
  2871. }
  2872. udelay(2); /* should be 1.5us */
  2873. }
  2874. if (i == 4)
  2875. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  2876. }
  2877. train_done:
  2878. DRM_DEBUG_KMS("FDI train done.\n");
  2879. }
  2880. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  2881. {
  2882. struct drm_device *dev = intel_crtc->base.dev;
  2883. struct drm_i915_private *dev_priv = dev->dev_private;
  2884. int pipe = intel_crtc->pipe;
  2885. u32 reg, temp;
  2886. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2887. reg = FDI_RX_CTL(pipe);
  2888. temp = I915_READ(reg);
  2889. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  2890. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2891. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2892. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2893. POSTING_READ(reg);
  2894. udelay(200);
  2895. /* Switch from Rawclk to PCDclk */
  2896. temp = I915_READ(reg);
  2897. I915_WRITE(reg, temp | FDI_PCDCLK);
  2898. POSTING_READ(reg);
  2899. udelay(200);
  2900. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2901. reg = FDI_TX_CTL(pipe);
  2902. temp = I915_READ(reg);
  2903. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2904. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2905. POSTING_READ(reg);
  2906. udelay(100);
  2907. }
  2908. }
  2909. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  2910. {
  2911. struct drm_device *dev = intel_crtc->base.dev;
  2912. struct drm_i915_private *dev_priv = dev->dev_private;
  2913. int pipe = intel_crtc->pipe;
  2914. u32 reg, temp;
  2915. /* Switch from PCDclk to Rawclk */
  2916. reg = FDI_RX_CTL(pipe);
  2917. temp = I915_READ(reg);
  2918. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2919. /* Disable CPU FDI TX PLL */
  2920. reg = FDI_TX_CTL(pipe);
  2921. temp = I915_READ(reg);
  2922. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2923. POSTING_READ(reg);
  2924. udelay(100);
  2925. reg = FDI_RX_CTL(pipe);
  2926. temp = I915_READ(reg);
  2927. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2928. /* Wait for the clocks to turn off. */
  2929. POSTING_READ(reg);
  2930. udelay(100);
  2931. }
  2932. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2933. {
  2934. struct drm_device *dev = crtc->dev;
  2935. struct drm_i915_private *dev_priv = dev->dev_private;
  2936. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2937. int pipe = intel_crtc->pipe;
  2938. u32 reg, temp;
  2939. /* disable CPU FDI tx and PCH FDI rx */
  2940. reg = FDI_TX_CTL(pipe);
  2941. temp = I915_READ(reg);
  2942. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2943. POSTING_READ(reg);
  2944. reg = FDI_RX_CTL(pipe);
  2945. temp = I915_READ(reg);
  2946. temp &= ~(0x7 << 16);
  2947. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2948. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2949. POSTING_READ(reg);
  2950. udelay(100);
  2951. /* Ironlake workaround, disable clock pointer after downing FDI */
  2952. if (HAS_PCH_IBX(dev))
  2953. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2954. /* still set train pattern 1 */
  2955. reg = FDI_TX_CTL(pipe);
  2956. temp = I915_READ(reg);
  2957. temp &= ~FDI_LINK_TRAIN_NONE;
  2958. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2959. I915_WRITE(reg, temp);
  2960. reg = FDI_RX_CTL(pipe);
  2961. temp = I915_READ(reg);
  2962. if (HAS_PCH_CPT(dev)) {
  2963. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2964. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2965. } else {
  2966. temp &= ~FDI_LINK_TRAIN_NONE;
  2967. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2968. }
  2969. /* BPC in FDI rx is consistent with that in PIPECONF */
  2970. temp &= ~(0x07 << 16);
  2971. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2972. I915_WRITE(reg, temp);
  2973. POSTING_READ(reg);
  2974. udelay(100);
  2975. }
  2976. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  2977. {
  2978. struct intel_crtc *crtc;
  2979. /* Note that we don't need to be called with mode_config.lock here
  2980. * as our list of CRTC objects is static for the lifetime of the
  2981. * device and so cannot disappear as we iterate. Similarly, we can
  2982. * happily treat the predicates as racy, atomic checks as userspace
  2983. * cannot claim and pin a new fb without at least acquring the
  2984. * struct_mutex and so serialising with us.
  2985. */
  2986. for_each_intel_crtc(dev, crtc) {
  2987. if (atomic_read(&crtc->unpin_work_count) == 0)
  2988. continue;
  2989. if (crtc->unpin_work)
  2990. intel_wait_for_vblank(dev, crtc->pipe);
  2991. return true;
  2992. }
  2993. return false;
  2994. }
  2995. static void page_flip_completed(struct intel_crtc *intel_crtc)
  2996. {
  2997. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  2998. struct intel_unpin_work *work = intel_crtc->unpin_work;
  2999. /* ensure that the unpin work is consistent wrt ->pending. */
  3000. smp_rmb();
  3001. intel_crtc->unpin_work = NULL;
  3002. if (work->event)
  3003. drm_send_vblank_event(intel_crtc->base.dev,
  3004. intel_crtc->pipe,
  3005. work->event);
  3006. drm_crtc_vblank_put(&intel_crtc->base);
  3007. wake_up_all(&dev_priv->pending_flip_queue);
  3008. queue_work(dev_priv->wq, &work->work);
  3009. trace_i915_flip_complete(intel_crtc->plane,
  3010. work->pending_flip_obj);
  3011. }
  3012. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3013. {
  3014. struct drm_device *dev = crtc->dev;
  3015. struct drm_i915_private *dev_priv = dev->dev_private;
  3016. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3017. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3018. !intel_crtc_has_pending_flip(crtc),
  3019. 60*HZ) == 0)) {
  3020. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3021. spin_lock_irq(&dev->event_lock);
  3022. if (intel_crtc->unpin_work) {
  3023. WARN_ONCE(1, "Removing stuck page flip\n");
  3024. page_flip_completed(intel_crtc);
  3025. }
  3026. spin_unlock_irq(&dev->event_lock);
  3027. }
  3028. if (crtc->primary->fb) {
  3029. mutex_lock(&dev->struct_mutex);
  3030. intel_finish_fb(crtc->primary->fb);
  3031. mutex_unlock(&dev->struct_mutex);
  3032. }
  3033. }
  3034. /* Program iCLKIP clock to the desired frequency */
  3035. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3036. {
  3037. struct drm_device *dev = crtc->dev;
  3038. struct drm_i915_private *dev_priv = dev->dev_private;
  3039. int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
  3040. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3041. u32 temp;
  3042. mutex_lock(&dev_priv->dpio_lock);
  3043. /* It is necessary to ungate the pixclk gate prior to programming
  3044. * the divisors, and gate it back when it is done.
  3045. */
  3046. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3047. /* Disable SSCCTL */
  3048. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3049. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3050. SBI_SSCCTL_DISABLE,
  3051. SBI_ICLK);
  3052. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3053. if (clock == 20000) {
  3054. auxdiv = 1;
  3055. divsel = 0x41;
  3056. phaseinc = 0x20;
  3057. } else {
  3058. /* The iCLK virtual clock root frequency is in MHz,
  3059. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3060. * divisors, it is necessary to divide one by another, so we
  3061. * convert the virtual clock precision to KHz here for higher
  3062. * precision.
  3063. */
  3064. u32 iclk_virtual_root_freq = 172800 * 1000;
  3065. u32 iclk_pi_range = 64;
  3066. u32 desired_divisor, msb_divisor_value, pi_value;
  3067. desired_divisor = (iclk_virtual_root_freq / clock);
  3068. msb_divisor_value = desired_divisor / iclk_pi_range;
  3069. pi_value = desired_divisor % iclk_pi_range;
  3070. auxdiv = 0;
  3071. divsel = msb_divisor_value - 2;
  3072. phaseinc = pi_value;
  3073. }
  3074. /* This should not happen with any sane values */
  3075. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3076. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3077. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3078. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3079. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3080. clock,
  3081. auxdiv,
  3082. divsel,
  3083. phasedir,
  3084. phaseinc);
  3085. /* Program SSCDIVINTPHASE6 */
  3086. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3087. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3088. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3089. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3090. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3091. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3092. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3093. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3094. /* Program SSCAUXDIV */
  3095. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3096. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3097. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3098. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3099. /* Enable modulator and associated divider */
  3100. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3101. temp &= ~SBI_SSCCTL_DISABLE;
  3102. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3103. /* Wait for initialization time */
  3104. udelay(24);
  3105. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3106. mutex_unlock(&dev_priv->dpio_lock);
  3107. }
  3108. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3109. enum pipe pch_transcoder)
  3110. {
  3111. struct drm_device *dev = crtc->base.dev;
  3112. struct drm_i915_private *dev_priv = dev->dev_private;
  3113. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  3114. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3115. I915_READ(HTOTAL(cpu_transcoder)));
  3116. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3117. I915_READ(HBLANK(cpu_transcoder)));
  3118. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3119. I915_READ(HSYNC(cpu_transcoder)));
  3120. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3121. I915_READ(VTOTAL(cpu_transcoder)));
  3122. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3123. I915_READ(VBLANK(cpu_transcoder)));
  3124. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3125. I915_READ(VSYNC(cpu_transcoder)));
  3126. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3127. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3128. }
  3129. static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
  3130. {
  3131. struct drm_i915_private *dev_priv = dev->dev_private;
  3132. uint32_t temp;
  3133. temp = I915_READ(SOUTH_CHICKEN1);
  3134. if (temp & FDI_BC_BIFURCATION_SELECT)
  3135. return;
  3136. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3137. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3138. temp |= FDI_BC_BIFURCATION_SELECT;
  3139. DRM_DEBUG_KMS("enabling fdi C rx\n");
  3140. I915_WRITE(SOUTH_CHICKEN1, temp);
  3141. POSTING_READ(SOUTH_CHICKEN1);
  3142. }
  3143. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3144. {
  3145. struct drm_device *dev = intel_crtc->base.dev;
  3146. struct drm_i915_private *dev_priv = dev->dev_private;
  3147. switch (intel_crtc->pipe) {
  3148. case PIPE_A:
  3149. break;
  3150. case PIPE_B:
  3151. if (intel_crtc->config.fdi_lanes > 2)
  3152. WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
  3153. else
  3154. cpt_enable_fdi_bc_bifurcation(dev);
  3155. break;
  3156. case PIPE_C:
  3157. cpt_enable_fdi_bc_bifurcation(dev);
  3158. break;
  3159. default:
  3160. BUG();
  3161. }
  3162. }
  3163. /*
  3164. * Enable PCH resources required for PCH ports:
  3165. * - PCH PLLs
  3166. * - FDI training & RX/TX
  3167. * - update transcoder timings
  3168. * - DP transcoding bits
  3169. * - transcoder
  3170. */
  3171. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3172. {
  3173. struct drm_device *dev = crtc->dev;
  3174. struct drm_i915_private *dev_priv = dev->dev_private;
  3175. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3176. int pipe = intel_crtc->pipe;
  3177. u32 reg, temp;
  3178. assert_pch_transcoder_disabled(dev_priv, pipe);
  3179. if (IS_IVYBRIDGE(dev))
  3180. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3181. /* Write the TU size bits before fdi link training, so that error
  3182. * detection works. */
  3183. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3184. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3185. /* For PCH output, training FDI link */
  3186. dev_priv->display.fdi_link_train(crtc);
  3187. /* We need to program the right clock selection before writing the pixel
  3188. * mutliplier into the DPLL. */
  3189. if (HAS_PCH_CPT(dev)) {
  3190. u32 sel;
  3191. temp = I915_READ(PCH_DPLL_SEL);
  3192. temp |= TRANS_DPLL_ENABLE(pipe);
  3193. sel = TRANS_DPLLB_SEL(pipe);
  3194. if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
  3195. temp |= sel;
  3196. else
  3197. temp &= ~sel;
  3198. I915_WRITE(PCH_DPLL_SEL, temp);
  3199. }
  3200. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3201. * transcoder, and we actually should do this to not upset any PCH
  3202. * transcoder that already use the clock when we share it.
  3203. *
  3204. * Note that enable_shared_dpll tries to do the right thing, but
  3205. * get_shared_dpll unconditionally resets the pll - we need that to have
  3206. * the right LVDS enable sequence. */
  3207. intel_enable_shared_dpll(intel_crtc);
  3208. /* set transcoder timing, panel must allow it */
  3209. assert_panel_unlocked(dev_priv, pipe);
  3210. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3211. intel_fdi_normal_train(crtc);
  3212. /* For PCH DP, enable TRANS_DP_CTL */
  3213. if (HAS_PCH_CPT(dev) &&
  3214. (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  3215. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_EDP))) {
  3216. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3217. reg = TRANS_DP_CTL(pipe);
  3218. temp = I915_READ(reg);
  3219. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3220. TRANS_DP_SYNC_MASK |
  3221. TRANS_DP_BPC_MASK);
  3222. temp |= (TRANS_DP_OUTPUT_ENABLE |
  3223. TRANS_DP_ENH_FRAMING);
  3224. temp |= bpc << 9; /* same format but at 11:9 */
  3225. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3226. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3227. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3228. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3229. switch (intel_trans_dp_port_sel(crtc)) {
  3230. case PCH_DP_B:
  3231. temp |= TRANS_DP_PORT_SEL_B;
  3232. break;
  3233. case PCH_DP_C:
  3234. temp |= TRANS_DP_PORT_SEL_C;
  3235. break;
  3236. case PCH_DP_D:
  3237. temp |= TRANS_DP_PORT_SEL_D;
  3238. break;
  3239. default:
  3240. BUG();
  3241. }
  3242. I915_WRITE(reg, temp);
  3243. }
  3244. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3245. }
  3246. static void lpt_pch_enable(struct drm_crtc *crtc)
  3247. {
  3248. struct drm_device *dev = crtc->dev;
  3249. struct drm_i915_private *dev_priv = dev->dev_private;
  3250. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3251. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3252. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3253. lpt_program_iclkip(crtc);
  3254. /* Set transcoder timing. */
  3255. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3256. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3257. }
  3258. void intel_put_shared_dpll(struct intel_crtc *crtc)
  3259. {
  3260. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3261. if (pll == NULL)
  3262. return;
  3263. if (pll->refcount == 0) {
  3264. WARN(1, "bad %s refcount\n", pll->name);
  3265. return;
  3266. }
  3267. if (--pll->refcount == 0) {
  3268. WARN_ON(pll->on);
  3269. WARN_ON(pll->active);
  3270. }
  3271. crtc->config.shared_dpll = DPLL_ID_PRIVATE;
  3272. }
  3273. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
  3274. {
  3275. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3276. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3277. enum intel_dpll_id i;
  3278. if (pll) {
  3279. DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
  3280. crtc->base.base.id, pll->name);
  3281. intel_put_shared_dpll(crtc);
  3282. }
  3283. if (HAS_PCH_IBX(dev_priv->dev)) {
  3284. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3285. i = (enum intel_dpll_id) crtc->pipe;
  3286. pll = &dev_priv->shared_dplls[i];
  3287. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3288. crtc->base.base.id, pll->name);
  3289. WARN_ON(pll->refcount);
  3290. goto found;
  3291. }
  3292. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3293. pll = &dev_priv->shared_dplls[i];
  3294. /* Only want to check enabled timings first */
  3295. if (pll->refcount == 0)
  3296. continue;
  3297. if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
  3298. sizeof(pll->hw_state)) == 0) {
  3299. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
  3300. crtc->base.base.id,
  3301. pll->name, pll->refcount, pll->active);
  3302. goto found;
  3303. }
  3304. }
  3305. /* Ok no matching timings, maybe there's a free one? */
  3306. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3307. pll = &dev_priv->shared_dplls[i];
  3308. if (pll->refcount == 0) {
  3309. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3310. crtc->base.base.id, pll->name);
  3311. goto found;
  3312. }
  3313. }
  3314. return NULL;
  3315. found:
  3316. if (pll->refcount == 0)
  3317. pll->hw_state = crtc->config.dpll_hw_state;
  3318. crtc->config.shared_dpll = i;
  3319. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3320. pipe_name(crtc->pipe));
  3321. pll->refcount++;
  3322. return pll;
  3323. }
  3324. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3325. {
  3326. struct drm_i915_private *dev_priv = dev->dev_private;
  3327. int dslreg = PIPEDSL(pipe);
  3328. u32 temp;
  3329. temp = I915_READ(dslreg);
  3330. udelay(500);
  3331. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3332. if (wait_for(I915_READ(dslreg) != temp, 5))
  3333. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3334. }
  3335. }
  3336. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3337. {
  3338. struct drm_device *dev = crtc->base.dev;
  3339. struct drm_i915_private *dev_priv = dev->dev_private;
  3340. int pipe = crtc->pipe;
  3341. if (crtc->config.pch_pfit.enabled) {
  3342. /* Force use of hard-coded filter coefficients
  3343. * as some pre-programmed values are broken,
  3344. * e.g. x201.
  3345. */
  3346. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3347. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3348. PF_PIPE_SEL_IVB(pipe));
  3349. else
  3350. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3351. I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
  3352. I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
  3353. }
  3354. }
  3355. static void intel_enable_planes(struct drm_crtc *crtc)
  3356. {
  3357. struct drm_device *dev = crtc->dev;
  3358. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3359. struct drm_plane *plane;
  3360. struct intel_plane *intel_plane;
  3361. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3362. intel_plane = to_intel_plane(plane);
  3363. if (intel_plane->pipe == pipe)
  3364. intel_plane_restore(&intel_plane->base);
  3365. }
  3366. }
  3367. static void intel_disable_planes(struct drm_crtc *crtc)
  3368. {
  3369. struct drm_device *dev = crtc->dev;
  3370. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3371. struct drm_plane *plane;
  3372. struct intel_plane *intel_plane;
  3373. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3374. intel_plane = to_intel_plane(plane);
  3375. if (intel_plane->pipe == pipe)
  3376. intel_plane_disable(&intel_plane->base);
  3377. }
  3378. }
  3379. void hsw_enable_ips(struct intel_crtc *crtc)
  3380. {
  3381. struct drm_device *dev = crtc->base.dev;
  3382. struct drm_i915_private *dev_priv = dev->dev_private;
  3383. if (!crtc->config.ips_enabled)
  3384. return;
  3385. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3386. intel_wait_for_vblank(dev, crtc->pipe);
  3387. assert_plane_enabled(dev_priv, crtc->plane);
  3388. if (IS_BROADWELL(dev)) {
  3389. mutex_lock(&dev_priv->rps.hw_lock);
  3390. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3391. mutex_unlock(&dev_priv->rps.hw_lock);
  3392. /* Quoting Art Runyan: "its not safe to expect any particular
  3393. * value in IPS_CTL bit 31 after enabling IPS through the
  3394. * mailbox." Moreover, the mailbox may return a bogus state,
  3395. * so we need to just enable it and continue on.
  3396. */
  3397. } else {
  3398. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3399. /* The bit only becomes 1 in the next vblank, so this wait here
  3400. * is essentially intel_wait_for_vblank. If we don't have this
  3401. * and don't wait for vblanks until the end of crtc_enable, then
  3402. * the HW state readout code will complain that the expected
  3403. * IPS_CTL value is not the one we read. */
  3404. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3405. DRM_ERROR("Timed out waiting for IPS enable\n");
  3406. }
  3407. }
  3408. void hsw_disable_ips(struct intel_crtc *crtc)
  3409. {
  3410. struct drm_device *dev = crtc->base.dev;
  3411. struct drm_i915_private *dev_priv = dev->dev_private;
  3412. if (!crtc->config.ips_enabled)
  3413. return;
  3414. assert_plane_enabled(dev_priv, crtc->plane);
  3415. if (IS_BROADWELL(dev)) {
  3416. mutex_lock(&dev_priv->rps.hw_lock);
  3417. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3418. mutex_unlock(&dev_priv->rps.hw_lock);
  3419. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3420. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3421. DRM_ERROR("Timed out waiting for IPS disable\n");
  3422. } else {
  3423. I915_WRITE(IPS_CTL, 0);
  3424. POSTING_READ(IPS_CTL);
  3425. }
  3426. /* We need to wait for a vblank before we can disable the plane. */
  3427. intel_wait_for_vblank(dev, crtc->pipe);
  3428. }
  3429. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3430. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3431. {
  3432. struct drm_device *dev = crtc->dev;
  3433. struct drm_i915_private *dev_priv = dev->dev_private;
  3434. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3435. enum pipe pipe = intel_crtc->pipe;
  3436. int palreg = PALETTE(pipe);
  3437. int i;
  3438. bool reenable_ips = false;
  3439. /* The clocks have to be on to load the palette. */
  3440. if (!crtc->enabled || !intel_crtc->active)
  3441. return;
  3442. if (!HAS_PCH_SPLIT(dev_priv->dev)) {
  3443. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3444. assert_dsi_pll_enabled(dev_priv);
  3445. else
  3446. assert_pll_enabled(dev_priv, pipe);
  3447. }
  3448. /* use legacy palette for Ironlake */
  3449. if (!HAS_GMCH_DISPLAY(dev))
  3450. palreg = LGC_PALETTE(pipe);
  3451. /* Workaround : Do not read or write the pipe palette/gamma data while
  3452. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3453. */
  3454. if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
  3455. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3456. GAMMA_MODE_MODE_SPLIT)) {
  3457. hsw_disable_ips(intel_crtc);
  3458. reenable_ips = true;
  3459. }
  3460. for (i = 0; i < 256; i++) {
  3461. I915_WRITE(palreg + 4 * i,
  3462. (intel_crtc->lut_r[i] << 16) |
  3463. (intel_crtc->lut_g[i] << 8) |
  3464. intel_crtc->lut_b[i]);
  3465. }
  3466. if (reenable_ips)
  3467. hsw_enable_ips(intel_crtc);
  3468. }
  3469. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  3470. {
  3471. if (!enable && intel_crtc->overlay) {
  3472. struct drm_device *dev = intel_crtc->base.dev;
  3473. struct drm_i915_private *dev_priv = dev->dev_private;
  3474. mutex_lock(&dev->struct_mutex);
  3475. dev_priv->mm.interruptible = false;
  3476. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3477. dev_priv->mm.interruptible = true;
  3478. mutex_unlock(&dev->struct_mutex);
  3479. }
  3480. /* Let userspace switch the overlay on again. In most cases userspace
  3481. * has to recompute where to put it anyway.
  3482. */
  3483. }
  3484. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  3485. {
  3486. struct drm_device *dev = crtc->dev;
  3487. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3488. int pipe = intel_crtc->pipe;
  3489. intel_enable_primary_hw_plane(crtc->primary, crtc);
  3490. intel_enable_planes(crtc);
  3491. intel_crtc_update_cursor(crtc, true);
  3492. intel_crtc_dpms_overlay(intel_crtc, true);
  3493. hsw_enable_ips(intel_crtc);
  3494. mutex_lock(&dev->struct_mutex);
  3495. intel_update_fbc(dev);
  3496. mutex_unlock(&dev->struct_mutex);
  3497. /*
  3498. * FIXME: Once we grow proper nuclear flip support out of this we need
  3499. * to compute the mask of flip planes precisely. For the time being
  3500. * consider this a flip from a NULL plane.
  3501. */
  3502. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3503. }
  3504. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  3505. {
  3506. struct drm_device *dev = crtc->dev;
  3507. struct drm_i915_private *dev_priv = dev->dev_private;
  3508. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3509. int pipe = intel_crtc->pipe;
  3510. int plane = intel_crtc->plane;
  3511. intel_crtc_wait_for_pending_flips(crtc);
  3512. if (dev_priv->fbc.plane == plane)
  3513. intel_disable_fbc(dev);
  3514. hsw_disable_ips(intel_crtc);
  3515. intel_crtc_dpms_overlay(intel_crtc, false);
  3516. intel_crtc_update_cursor(crtc, false);
  3517. intel_disable_planes(crtc);
  3518. intel_disable_primary_hw_plane(crtc->primary, crtc);
  3519. /*
  3520. * FIXME: Once we grow proper nuclear flip support out of this we need
  3521. * to compute the mask of flip planes precisely. For the time being
  3522. * consider this a flip to a NULL plane.
  3523. */
  3524. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3525. }
  3526. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  3527. {
  3528. struct drm_device *dev = crtc->dev;
  3529. struct drm_i915_private *dev_priv = dev->dev_private;
  3530. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3531. struct intel_encoder *encoder;
  3532. int pipe = intel_crtc->pipe;
  3533. WARN_ON(!crtc->enabled);
  3534. if (intel_crtc->active)
  3535. return;
  3536. if (intel_crtc->config.has_pch_encoder)
  3537. intel_prepare_shared_dpll(intel_crtc);
  3538. if (intel_crtc->config.has_dp_encoder)
  3539. intel_dp_set_m_n(intel_crtc);
  3540. intel_set_pipe_timings(intel_crtc);
  3541. if (intel_crtc->config.has_pch_encoder) {
  3542. intel_cpu_transcoder_set_m_n(intel_crtc,
  3543. &intel_crtc->config.fdi_m_n, NULL);
  3544. }
  3545. ironlake_set_pipeconf(crtc);
  3546. intel_crtc->active = true;
  3547. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3548. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  3549. for_each_encoder_on_crtc(dev, crtc, encoder)
  3550. if (encoder->pre_enable)
  3551. encoder->pre_enable(encoder);
  3552. if (intel_crtc->config.has_pch_encoder) {
  3553. /* Note: FDI PLL enabling _must_ be done before we enable the
  3554. * cpu pipes, hence this is separate from all the other fdi/pch
  3555. * enabling. */
  3556. ironlake_fdi_pll_enable(intel_crtc);
  3557. } else {
  3558. assert_fdi_tx_disabled(dev_priv, pipe);
  3559. assert_fdi_rx_disabled(dev_priv, pipe);
  3560. }
  3561. ironlake_pfit_enable(intel_crtc);
  3562. /*
  3563. * On ILK+ LUT must be loaded before the pipe is running but with
  3564. * clocks enabled
  3565. */
  3566. intel_crtc_load_lut(crtc);
  3567. intel_update_watermarks(crtc);
  3568. intel_enable_pipe(intel_crtc);
  3569. if (intel_crtc->config.has_pch_encoder)
  3570. ironlake_pch_enable(crtc);
  3571. for_each_encoder_on_crtc(dev, crtc, encoder)
  3572. encoder->enable(encoder);
  3573. if (HAS_PCH_CPT(dev))
  3574. cpt_verify_modeset(dev, intel_crtc->pipe);
  3575. assert_vblank_disabled(crtc);
  3576. drm_crtc_vblank_on(crtc);
  3577. intel_crtc_enable_planes(crtc);
  3578. }
  3579. /* IPS only exists on ULT machines and is tied to pipe A. */
  3580. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  3581. {
  3582. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  3583. }
  3584. /*
  3585. * This implements the workaround described in the "notes" section of the mode
  3586. * set sequence documentation. When going from no pipes or single pipe to
  3587. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  3588. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  3589. */
  3590. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  3591. {
  3592. struct drm_device *dev = crtc->base.dev;
  3593. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  3594. /* We want to get the other_active_crtc only if there's only 1 other
  3595. * active crtc. */
  3596. for_each_intel_crtc(dev, crtc_it) {
  3597. if (!crtc_it->active || crtc_it == crtc)
  3598. continue;
  3599. if (other_active_crtc)
  3600. return;
  3601. other_active_crtc = crtc_it;
  3602. }
  3603. if (!other_active_crtc)
  3604. return;
  3605. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3606. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3607. }
  3608. static void haswell_crtc_enable(struct drm_crtc *crtc)
  3609. {
  3610. struct drm_device *dev = crtc->dev;
  3611. struct drm_i915_private *dev_priv = dev->dev_private;
  3612. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3613. struct intel_encoder *encoder;
  3614. int pipe = intel_crtc->pipe;
  3615. WARN_ON(!crtc->enabled);
  3616. if (intel_crtc->active)
  3617. return;
  3618. if (intel_crtc_to_shared_dpll(intel_crtc))
  3619. intel_enable_shared_dpll(intel_crtc);
  3620. if (intel_crtc->config.has_dp_encoder)
  3621. intel_dp_set_m_n(intel_crtc);
  3622. intel_set_pipe_timings(intel_crtc);
  3623. if (intel_crtc->config.cpu_transcoder != TRANSCODER_EDP) {
  3624. I915_WRITE(PIPE_MULT(intel_crtc->config.cpu_transcoder),
  3625. intel_crtc->config.pixel_multiplier - 1);
  3626. }
  3627. if (intel_crtc->config.has_pch_encoder) {
  3628. intel_cpu_transcoder_set_m_n(intel_crtc,
  3629. &intel_crtc->config.fdi_m_n, NULL);
  3630. }
  3631. haswell_set_pipeconf(crtc);
  3632. intel_set_pipe_csc(crtc);
  3633. intel_crtc->active = true;
  3634. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3635. for_each_encoder_on_crtc(dev, crtc, encoder)
  3636. if (encoder->pre_enable)
  3637. encoder->pre_enable(encoder);
  3638. if (intel_crtc->config.has_pch_encoder) {
  3639. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  3640. true);
  3641. dev_priv->display.fdi_link_train(crtc);
  3642. }
  3643. intel_ddi_enable_pipe_clock(intel_crtc);
  3644. ironlake_pfit_enable(intel_crtc);
  3645. /*
  3646. * On ILK+ LUT must be loaded before the pipe is running but with
  3647. * clocks enabled
  3648. */
  3649. intel_crtc_load_lut(crtc);
  3650. intel_ddi_set_pipe_settings(crtc);
  3651. intel_ddi_enable_transcoder_func(crtc);
  3652. intel_update_watermarks(crtc);
  3653. intel_enable_pipe(intel_crtc);
  3654. if (intel_crtc->config.has_pch_encoder)
  3655. lpt_pch_enable(crtc);
  3656. if (intel_crtc->config.dp_encoder_is_mst)
  3657. intel_ddi_set_vc_payload_alloc(crtc, true);
  3658. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3659. encoder->enable(encoder);
  3660. intel_opregion_notify_encoder(encoder, true);
  3661. }
  3662. assert_vblank_disabled(crtc);
  3663. drm_crtc_vblank_on(crtc);
  3664. /* If we change the relative order between pipe/planes enabling, we need
  3665. * to change the workaround. */
  3666. haswell_mode_set_planes_workaround(intel_crtc);
  3667. intel_crtc_enable_planes(crtc);
  3668. }
  3669. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  3670. {
  3671. struct drm_device *dev = crtc->base.dev;
  3672. struct drm_i915_private *dev_priv = dev->dev_private;
  3673. int pipe = crtc->pipe;
  3674. /* To avoid upsetting the power well on haswell only disable the pfit if
  3675. * it's in use. The hw state code will make sure we get this right. */
  3676. if (crtc->config.pch_pfit.enabled) {
  3677. I915_WRITE(PF_CTL(pipe), 0);
  3678. I915_WRITE(PF_WIN_POS(pipe), 0);
  3679. I915_WRITE(PF_WIN_SZ(pipe), 0);
  3680. }
  3681. }
  3682. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  3683. {
  3684. struct drm_device *dev = crtc->dev;
  3685. struct drm_i915_private *dev_priv = dev->dev_private;
  3686. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3687. struct intel_encoder *encoder;
  3688. int pipe = intel_crtc->pipe;
  3689. u32 reg, temp;
  3690. if (!intel_crtc->active)
  3691. return;
  3692. intel_crtc_disable_planes(crtc);
  3693. drm_crtc_vblank_off(crtc);
  3694. assert_vblank_disabled(crtc);
  3695. for_each_encoder_on_crtc(dev, crtc, encoder)
  3696. encoder->disable(encoder);
  3697. if (intel_crtc->config.has_pch_encoder)
  3698. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  3699. intel_disable_pipe(intel_crtc);
  3700. ironlake_pfit_disable(intel_crtc);
  3701. for_each_encoder_on_crtc(dev, crtc, encoder)
  3702. if (encoder->post_disable)
  3703. encoder->post_disable(encoder);
  3704. if (intel_crtc->config.has_pch_encoder) {
  3705. ironlake_fdi_disable(crtc);
  3706. ironlake_disable_pch_transcoder(dev_priv, pipe);
  3707. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  3708. if (HAS_PCH_CPT(dev)) {
  3709. /* disable TRANS_DP_CTL */
  3710. reg = TRANS_DP_CTL(pipe);
  3711. temp = I915_READ(reg);
  3712. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  3713. TRANS_DP_PORT_SEL_MASK);
  3714. temp |= TRANS_DP_PORT_SEL_NONE;
  3715. I915_WRITE(reg, temp);
  3716. /* disable DPLL_SEL */
  3717. temp = I915_READ(PCH_DPLL_SEL);
  3718. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  3719. I915_WRITE(PCH_DPLL_SEL, temp);
  3720. }
  3721. /* disable PCH DPLL */
  3722. intel_disable_shared_dpll(intel_crtc);
  3723. ironlake_fdi_pll_disable(intel_crtc);
  3724. }
  3725. intel_crtc->active = false;
  3726. intel_update_watermarks(crtc);
  3727. mutex_lock(&dev->struct_mutex);
  3728. intel_update_fbc(dev);
  3729. mutex_unlock(&dev->struct_mutex);
  3730. }
  3731. static void haswell_crtc_disable(struct drm_crtc *crtc)
  3732. {
  3733. struct drm_device *dev = crtc->dev;
  3734. struct drm_i915_private *dev_priv = dev->dev_private;
  3735. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3736. struct intel_encoder *encoder;
  3737. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3738. if (!intel_crtc->active)
  3739. return;
  3740. intel_crtc_disable_planes(crtc);
  3741. drm_crtc_vblank_off(crtc);
  3742. assert_vblank_disabled(crtc);
  3743. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3744. intel_opregion_notify_encoder(encoder, false);
  3745. encoder->disable(encoder);
  3746. }
  3747. if (intel_crtc->config.has_pch_encoder)
  3748. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  3749. false);
  3750. intel_disable_pipe(intel_crtc);
  3751. if (intel_crtc->config.dp_encoder_is_mst)
  3752. intel_ddi_set_vc_payload_alloc(crtc, false);
  3753. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  3754. ironlake_pfit_disable(intel_crtc);
  3755. intel_ddi_disable_pipe_clock(intel_crtc);
  3756. if (intel_crtc->config.has_pch_encoder) {
  3757. lpt_disable_pch_transcoder(dev_priv);
  3758. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  3759. true);
  3760. intel_ddi_fdi_disable(crtc);
  3761. }
  3762. for_each_encoder_on_crtc(dev, crtc, encoder)
  3763. if (encoder->post_disable)
  3764. encoder->post_disable(encoder);
  3765. intel_crtc->active = false;
  3766. intel_update_watermarks(crtc);
  3767. mutex_lock(&dev->struct_mutex);
  3768. intel_update_fbc(dev);
  3769. mutex_unlock(&dev->struct_mutex);
  3770. if (intel_crtc_to_shared_dpll(intel_crtc))
  3771. intel_disable_shared_dpll(intel_crtc);
  3772. }
  3773. static void ironlake_crtc_off(struct drm_crtc *crtc)
  3774. {
  3775. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3776. intel_put_shared_dpll(intel_crtc);
  3777. }
  3778. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  3779. {
  3780. struct drm_device *dev = crtc->base.dev;
  3781. struct drm_i915_private *dev_priv = dev->dev_private;
  3782. struct intel_crtc_config *pipe_config = &crtc->config;
  3783. if (!crtc->config.gmch_pfit.control)
  3784. return;
  3785. /*
  3786. * The panel fitter should only be adjusted whilst the pipe is disabled,
  3787. * according to register description and PRM.
  3788. */
  3789. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  3790. assert_pipe_disabled(dev_priv, crtc->pipe);
  3791. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  3792. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  3793. /* Border color in case we don't scale up to the full screen. Black by
  3794. * default, change to something else for debugging. */
  3795. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  3796. }
  3797. static enum intel_display_power_domain port_to_power_domain(enum port port)
  3798. {
  3799. switch (port) {
  3800. case PORT_A:
  3801. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  3802. case PORT_B:
  3803. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  3804. case PORT_C:
  3805. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  3806. case PORT_D:
  3807. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  3808. default:
  3809. WARN_ON_ONCE(1);
  3810. return POWER_DOMAIN_PORT_OTHER;
  3811. }
  3812. }
  3813. #define for_each_power_domain(domain, mask) \
  3814. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  3815. if ((1 << (domain)) & (mask))
  3816. enum intel_display_power_domain
  3817. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  3818. {
  3819. struct drm_device *dev = intel_encoder->base.dev;
  3820. struct intel_digital_port *intel_dig_port;
  3821. switch (intel_encoder->type) {
  3822. case INTEL_OUTPUT_UNKNOWN:
  3823. /* Only DDI platforms should ever use this output type */
  3824. WARN_ON_ONCE(!HAS_DDI(dev));
  3825. case INTEL_OUTPUT_DISPLAYPORT:
  3826. case INTEL_OUTPUT_HDMI:
  3827. case INTEL_OUTPUT_EDP:
  3828. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  3829. return port_to_power_domain(intel_dig_port->port);
  3830. case INTEL_OUTPUT_DP_MST:
  3831. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  3832. return port_to_power_domain(intel_dig_port->port);
  3833. case INTEL_OUTPUT_ANALOG:
  3834. return POWER_DOMAIN_PORT_CRT;
  3835. case INTEL_OUTPUT_DSI:
  3836. return POWER_DOMAIN_PORT_DSI;
  3837. default:
  3838. return POWER_DOMAIN_PORT_OTHER;
  3839. }
  3840. }
  3841. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  3842. {
  3843. struct drm_device *dev = crtc->dev;
  3844. struct intel_encoder *intel_encoder;
  3845. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3846. enum pipe pipe = intel_crtc->pipe;
  3847. unsigned long mask;
  3848. enum transcoder transcoder;
  3849. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  3850. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  3851. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  3852. if (intel_crtc->config.pch_pfit.enabled ||
  3853. intel_crtc->config.pch_pfit.force_thru)
  3854. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  3855. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  3856. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  3857. return mask;
  3858. }
  3859. static void modeset_update_crtc_power_domains(struct drm_device *dev)
  3860. {
  3861. struct drm_i915_private *dev_priv = dev->dev_private;
  3862. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  3863. struct intel_crtc *crtc;
  3864. /*
  3865. * First get all needed power domains, then put all unneeded, to avoid
  3866. * any unnecessary toggling of the power wells.
  3867. */
  3868. for_each_intel_crtc(dev, crtc) {
  3869. enum intel_display_power_domain domain;
  3870. if (!crtc->base.enabled)
  3871. continue;
  3872. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  3873. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  3874. intel_display_power_get(dev_priv, domain);
  3875. }
  3876. for_each_intel_crtc(dev, crtc) {
  3877. enum intel_display_power_domain domain;
  3878. for_each_power_domain(domain, crtc->enabled_power_domains)
  3879. intel_display_power_put(dev_priv, domain);
  3880. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  3881. }
  3882. intel_display_set_init_power(dev_priv, false);
  3883. }
  3884. /* returns HPLL frequency in kHz */
  3885. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  3886. {
  3887. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  3888. /* Obtain SKU information */
  3889. mutex_lock(&dev_priv->dpio_lock);
  3890. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  3891. CCK_FUSE_HPLL_FREQ_MASK;
  3892. mutex_unlock(&dev_priv->dpio_lock);
  3893. return vco_freq[hpll_freq] * 1000;
  3894. }
  3895. static void vlv_update_cdclk(struct drm_device *dev)
  3896. {
  3897. struct drm_i915_private *dev_priv = dev->dev_private;
  3898. dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  3899. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  3900. dev_priv->vlv_cdclk_freq);
  3901. /*
  3902. * Program the gmbus_freq based on the cdclk frequency.
  3903. * BSpec erroneously claims we should aim for 4MHz, but
  3904. * in fact 1MHz is the correct frequency.
  3905. */
  3906. I915_WRITE(GMBUSFREQ_VLV, dev_priv->vlv_cdclk_freq);
  3907. }
  3908. /* Adjust CDclk dividers to allow high res or save power if possible */
  3909. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  3910. {
  3911. struct drm_i915_private *dev_priv = dev->dev_private;
  3912. u32 val, cmd;
  3913. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  3914. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  3915. cmd = 2;
  3916. else if (cdclk == 266667)
  3917. cmd = 1;
  3918. else
  3919. cmd = 0;
  3920. mutex_lock(&dev_priv->rps.hw_lock);
  3921. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  3922. val &= ~DSPFREQGUAR_MASK;
  3923. val |= (cmd << DSPFREQGUAR_SHIFT);
  3924. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  3925. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  3926. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  3927. 50)) {
  3928. DRM_ERROR("timed out waiting for CDclk change\n");
  3929. }
  3930. mutex_unlock(&dev_priv->rps.hw_lock);
  3931. if (cdclk == 400000) {
  3932. u32 divider, vco;
  3933. vco = valleyview_get_vco(dev_priv);
  3934. divider = DIV_ROUND_CLOSEST(vco << 1, cdclk) - 1;
  3935. mutex_lock(&dev_priv->dpio_lock);
  3936. /* adjust cdclk divider */
  3937. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  3938. val &= ~DISPLAY_FREQUENCY_VALUES;
  3939. val |= divider;
  3940. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  3941. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  3942. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  3943. 50))
  3944. DRM_ERROR("timed out waiting for CDclk change\n");
  3945. mutex_unlock(&dev_priv->dpio_lock);
  3946. }
  3947. mutex_lock(&dev_priv->dpio_lock);
  3948. /* adjust self-refresh exit latency value */
  3949. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  3950. val &= ~0x7f;
  3951. /*
  3952. * For high bandwidth configs, we set a higher latency in the bunit
  3953. * so that the core display fetch happens in time to avoid underruns.
  3954. */
  3955. if (cdclk == 400000)
  3956. val |= 4500 / 250; /* 4.5 usec */
  3957. else
  3958. val |= 3000 / 250; /* 3.0 usec */
  3959. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  3960. mutex_unlock(&dev_priv->dpio_lock);
  3961. vlv_update_cdclk(dev);
  3962. }
  3963. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  3964. {
  3965. struct drm_i915_private *dev_priv = dev->dev_private;
  3966. u32 val, cmd;
  3967. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  3968. switch (cdclk) {
  3969. case 400000:
  3970. cmd = 3;
  3971. break;
  3972. case 333333:
  3973. case 320000:
  3974. cmd = 2;
  3975. break;
  3976. case 266667:
  3977. cmd = 1;
  3978. break;
  3979. case 200000:
  3980. cmd = 0;
  3981. break;
  3982. default:
  3983. WARN_ON(1);
  3984. return;
  3985. }
  3986. mutex_lock(&dev_priv->rps.hw_lock);
  3987. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  3988. val &= ~DSPFREQGUAR_MASK_CHV;
  3989. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  3990. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  3991. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  3992. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  3993. 50)) {
  3994. DRM_ERROR("timed out waiting for CDclk change\n");
  3995. }
  3996. mutex_unlock(&dev_priv->rps.hw_lock);
  3997. vlv_update_cdclk(dev);
  3998. }
  3999. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4000. int max_pixclk)
  4001. {
  4002. int vco = valleyview_get_vco(dev_priv);
  4003. int freq_320 = (vco << 1) % 320000 != 0 ? 333333 : 320000;
  4004. /* FIXME: Punit isn't quite ready yet */
  4005. if (IS_CHERRYVIEW(dev_priv->dev))
  4006. return 400000;
  4007. /*
  4008. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4009. * 200MHz
  4010. * 267MHz
  4011. * 320/333MHz (depends on HPLL freq)
  4012. * 400MHz
  4013. * So we check to see whether we're above 90% of the lower bin and
  4014. * adjust if needed.
  4015. *
  4016. * We seem to get an unstable or solid color picture at 200MHz.
  4017. * Not sure what's wrong. For now use 200MHz only when all pipes
  4018. * are off.
  4019. */
  4020. if (max_pixclk > freq_320*9/10)
  4021. return 400000;
  4022. else if (max_pixclk > 266667*9/10)
  4023. return freq_320;
  4024. else if (max_pixclk > 0)
  4025. return 266667;
  4026. else
  4027. return 200000;
  4028. }
  4029. /* compute the max pixel clock for new configuration */
  4030. static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
  4031. {
  4032. struct drm_device *dev = dev_priv->dev;
  4033. struct intel_crtc *intel_crtc;
  4034. int max_pixclk = 0;
  4035. for_each_intel_crtc(dev, intel_crtc) {
  4036. if (intel_crtc->new_enabled)
  4037. max_pixclk = max(max_pixclk,
  4038. intel_crtc->new_config->adjusted_mode.crtc_clock);
  4039. }
  4040. return max_pixclk;
  4041. }
  4042. static void valleyview_modeset_global_pipes(struct drm_device *dev,
  4043. unsigned *prepare_pipes)
  4044. {
  4045. struct drm_i915_private *dev_priv = dev->dev_private;
  4046. struct intel_crtc *intel_crtc;
  4047. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4048. if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
  4049. dev_priv->vlv_cdclk_freq)
  4050. return;
  4051. /* disable/enable all currently active pipes while we change cdclk */
  4052. for_each_intel_crtc(dev, intel_crtc)
  4053. if (intel_crtc->base.enabled)
  4054. *prepare_pipes |= (1 << intel_crtc->pipe);
  4055. }
  4056. static void valleyview_modeset_global_resources(struct drm_device *dev)
  4057. {
  4058. struct drm_i915_private *dev_priv = dev->dev_private;
  4059. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4060. int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  4061. if (req_cdclk != dev_priv->vlv_cdclk_freq) {
  4062. if (IS_CHERRYVIEW(dev))
  4063. cherryview_set_cdclk(dev, req_cdclk);
  4064. else
  4065. valleyview_set_cdclk(dev, req_cdclk);
  4066. }
  4067. modeset_update_crtc_power_domains(dev);
  4068. }
  4069. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  4070. {
  4071. struct drm_device *dev = crtc->dev;
  4072. struct drm_i915_private *dev_priv = to_i915(dev);
  4073. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4074. struct intel_encoder *encoder;
  4075. int pipe = intel_crtc->pipe;
  4076. bool is_dsi;
  4077. WARN_ON(!crtc->enabled);
  4078. if (intel_crtc->active)
  4079. return;
  4080. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  4081. if (!is_dsi) {
  4082. if (IS_CHERRYVIEW(dev))
  4083. chv_prepare_pll(intel_crtc, &intel_crtc->config);
  4084. else
  4085. vlv_prepare_pll(intel_crtc, &intel_crtc->config);
  4086. }
  4087. if (intel_crtc->config.has_dp_encoder)
  4088. intel_dp_set_m_n(intel_crtc);
  4089. intel_set_pipe_timings(intel_crtc);
  4090. i9xx_set_pipeconf(intel_crtc);
  4091. intel_crtc->active = true;
  4092. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4093. for_each_encoder_on_crtc(dev, crtc, encoder)
  4094. if (encoder->pre_pll_enable)
  4095. encoder->pre_pll_enable(encoder);
  4096. if (!is_dsi) {
  4097. if (IS_CHERRYVIEW(dev))
  4098. chv_enable_pll(intel_crtc, &intel_crtc->config);
  4099. else
  4100. vlv_enable_pll(intel_crtc, &intel_crtc->config);
  4101. }
  4102. for_each_encoder_on_crtc(dev, crtc, encoder)
  4103. if (encoder->pre_enable)
  4104. encoder->pre_enable(encoder);
  4105. i9xx_pfit_enable(intel_crtc);
  4106. intel_crtc_load_lut(crtc);
  4107. intel_update_watermarks(crtc);
  4108. intel_enable_pipe(intel_crtc);
  4109. for_each_encoder_on_crtc(dev, crtc, encoder)
  4110. encoder->enable(encoder);
  4111. assert_vblank_disabled(crtc);
  4112. drm_crtc_vblank_on(crtc);
  4113. intel_crtc_enable_planes(crtc);
  4114. /* Underruns don't raise interrupts, so check manually. */
  4115. i9xx_check_fifo_underruns(dev_priv);
  4116. }
  4117. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  4118. {
  4119. struct drm_device *dev = crtc->base.dev;
  4120. struct drm_i915_private *dev_priv = dev->dev_private;
  4121. I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
  4122. I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
  4123. }
  4124. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  4125. {
  4126. struct drm_device *dev = crtc->dev;
  4127. struct drm_i915_private *dev_priv = to_i915(dev);
  4128. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4129. struct intel_encoder *encoder;
  4130. int pipe = intel_crtc->pipe;
  4131. WARN_ON(!crtc->enabled);
  4132. if (intel_crtc->active)
  4133. return;
  4134. i9xx_set_pll_dividers(intel_crtc);
  4135. if (intel_crtc->config.has_dp_encoder)
  4136. intel_dp_set_m_n(intel_crtc);
  4137. intel_set_pipe_timings(intel_crtc);
  4138. i9xx_set_pipeconf(intel_crtc);
  4139. intel_crtc->active = true;
  4140. if (!IS_GEN2(dev))
  4141. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4142. for_each_encoder_on_crtc(dev, crtc, encoder)
  4143. if (encoder->pre_enable)
  4144. encoder->pre_enable(encoder);
  4145. i9xx_enable_pll(intel_crtc);
  4146. i9xx_pfit_enable(intel_crtc);
  4147. intel_crtc_load_lut(crtc);
  4148. intel_update_watermarks(crtc);
  4149. intel_enable_pipe(intel_crtc);
  4150. for_each_encoder_on_crtc(dev, crtc, encoder)
  4151. encoder->enable(encoder);
  4152. assert_vblank_disabled(crtc);
  4153. drm_crtc_vblank_on(crtc);
  4154. intel_crtc_enable_planes(crtc);
  4155. /*
  4156. * Gen2 reports pipe underruns whenever all planes are disabled.
  4157. * So don't enable underrun reporting before at least some planes
  4158. * are enabled.
  4159. * FIXME: Need to fix the logic to work when we turn off all planes
  4160. * but leave the pipe running.
  4161. */
  4162. if (IS_GEN2(dev))
  4163. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4164. /* Underruns don't raise interrupts, so check manually. */
  4165. i9xx_check_fifo_underruns(dev_priv);
  4166. }
  4167. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4168. {
  4169. struct drm_device *dev = crtc->base.dev;
  4170. struct drm_i915_private *dev_priv = dev->dev_private;
  4171. if (!crtc->config.gmch_pfit.control)
  4172. return;
  4173. assert_pipe_disabled(dev_priv, crtc->pipe);
  4174. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4175. I915_READ(PFIT_CONTROL));
  4176. I915_WRITE(PFIT_CONTROL, 0);
  4177. }
  4178. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  4179. {
  4180. struct drm_device *dev = crtc->dev;
  4181. struct drm_i915_private *dev_priv = dev->dev_private;
  4182. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4183. struct intel_encoder *encoder;
  4184. int pipe = intel_crtc->pipe;
  4185. if (!intel_crtc->active)
  4186. return;
  4187. /*
  4188. * Gen2 reports pipe underruns whenever all planes are disabled.
  4189. * So diasble underrun reporting before all the planes get disabled.
  4190. * FIXME: Need to fix the logic to work when we turn off all planes
  4191. * but leave the pipe running.
  4192. */
  4193. if (IS_GEN2(dev))
  4194. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4195. /*
  4196. * Vblank time updates from the shadow to live plane control register
  4197. * are blocked if the memory self-refresh mode is active at that
  4198. * moment. So to make sure the plane gets truly disabled, disable
  4199. * first the self-refresh mode. The self-refresh enable bit in turn
  4200. * will be checked/applied by the HW only at the next frame start
  4201. * event which is after the vblank start event, so we need to have a
  4202. * wait-for-vblank between disabling the plane and the pipe.
  4203. */
  4204. intel_set_memory_cxsr(dev_priv, false);
  4205. intel_crtc_disable_planes(crtc);
  4206. /*
  4207. * On gen2 planes are double buffered but the pipe isn't, so we must
  4208. * wait for planes to fully turn off before disabling the pipe.
  4209. * We also need to wait on all gmch platforms because of the
  4210. * self-refresh mode constraint explained above.
  4211. */
  4212. intel_wait_for_vblank(dev, pipe);
  4213. drm_crtc_vblank_off(crtc);
  4214. assert_vblank_disabled(crtc);
  4215. for_each_encoder_on_crtc(dev, crtc, encoder)
  4216. encoder->disable(encoder);
  4217. intel_disable_pipe(intel_crtc);
  4218. i9xx_pfit_disable(intel_crtc);
  4219. for_each_encoder_on_crtc(dev, crtc, encoder)
  4220. if (encoder->post_disable)
  4221. encoder->post_disable(encoder);
  4222. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  4223. if (IS_CHERRYVIEW(dev))
  4224. chv_disable_pll(dev_priv, pipe);
  4225. else if (IS_VALLEYVIEW(dev))
  4226. vlv_disable_pll(dev_priv, pipe);
  4227. else
  4228. i9xx_disable_pll(intel_crtc);
  4229. }
  4230. if (!IS_GEN2(dev))
  4231. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4232. intel_crtc->active = false;
  4233. intel_update_watermarks(crtc);
  4234. mutex_lock(&dev->struct_mutex);
  4235. intel_update_fbc(dev);
  4236. mutex_unlock(&dev->struct_mutex);
  4237. }
  4238. static void i9xx_crtc_off(struct drm_crtc *crtc)
  4239. {
  4240. }
  4241. static void intel_crtc_update_sarea(struct drm_crtc *crtc,
  4242. bool enabled)
  4243. {
  4244. struct drm_device *dev = crtc->dev;
  4245. struct drm_i915_master_private *master_priv;
  4246. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4247. int pipe = intel_crtc->pipe;
  4248. if (!dev->primary->master)
  4249. return;
  4250. master_priv = dev->primary->master->driver_priv;
  4251. if (!master_priv->sarea_priv)
  4252. return;
  4253. switch (pipe) {
  4254. case 0:
  4255. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  4256. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  4257. break;
  4258. case 1:
  4259. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  4260. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  4261. break;
  4262. default:
  4263. DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
  4264. break;
  4265. }
  4266. }
  4267. /* Master function to enable/disable CRTC and corresponding power wells */
  4268. void intel_crtc_control(struct drm_crtc *crtc, bool enable)
  4269. {
  4270. struct drm_device *dev = crtc->dev;
  4271. struct drm_i915_private *dev_priv = dev->dev_private;
  4272. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4273. enum intel_display_power_domain domain;
  4274. unsigned long domains;
  4275. if (enable) {
  4276. if (!intel_crtc->active) {
  4277. domains = get_crtc_power_domains(crtc);
  4278. for_each_power_domain(domain, domains)
  4279. intel_display_power_get(dev_priv, domain);
  4280. intel_crtc->enabled_power_domains = domains;
  4281. dev_priv->display.crtc_enable(crtc);
  4282. }
  4283. } else {
  4284. if (intel_crtc->active) {
  4285. dev_priv->display.crtc_disable(crtc);
  4286. domains = intel_crtc->enabled_power_domains;
  4287. for_each_power_domain(domain, domains)
  4288. intel_display_power_put(dev_priv, domain);
  4289. intel_crtc->enabled_power_domains = 0;
  4290. }
  4291. }
  4292. }
  4293. /**
  4294. * Sets the power management mode of the pipe and plane.
  4295. */
  4296. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  4297. {
  4298. struct drm_device *dev = crtc->dev;
  4299. struct intel_encoder *intel_encoder;
  4300. bool enable = false;
  4301. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4302. enable |= intel_encoder->connectors_active;
  4303. intel_crtc_control(crtc, enable);
  4304. intel_crtc_update_sarea(crtc, enable);
  4305. }
  4306. static void intel_crtc_disable(struct drm_crtc *crtc)
  4307. {
  4308. struct drm_device *dev = crtc->dev;
  4309. struct drm_connector *connector;
  4310. struct drm_i915_private *dev_priv = dev->dev_private;
  4311. struct drm_i915_gem_object *old_obj = intel_fb_obj(crtc->primary->fb);
  4312. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  4313. /* crtc should still be enabled when we disable it. */
  4314. WARN_ON(!crtc->enabled);
  4315. dev_priv->display.crtc_disable(crtc);
  4316. intel_crtc_update_sarea(crtc, false);
  4317. dev_priv->display.off(crtc);
  4318. if (crtc->primary->fb) {
  4319. mutex_lock(&dev->struct_mutex);
  4320. intel_unpin_fb_obj(old_obj);
  4321. i915_gem_track_fb(old_obj, NULL,
  4322. INTEL_FRONTBUFFER_PRIMARY(pipe));
  4323. mutex_unlock(&dev->struct_mutex);
  4324. crtc->primary->fb = NULL;
  4325. }
  4326. /* Update computed state. */
  4327. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  4328. if (!connector->encoder || !connector->encoder->crtc)
  4329. continue;
  4330. if (connector->encoder->crtc != crtc)
  4331. continue;
  4332. connector->dpms = DRM_MODE_DPMS_OFF;
  4333. to_intel_encoder(connector->encoder)->connectors_active = false;
  4334. }
  4335. }
  4336. void intel_encoder_destroy(struct drm_encoder *encoder)
  4337. {
  4338. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4339. drm_encoder_cleanup(encoder);
  4340. kfree(intel_encoder);
  4341. }
  4342. /* Simple dpms helper for encoders with just one connector, no cloning and only
  4343. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  4344. * state of the entire output pipe. */
  4345. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  4346. {
  4347. if (mode == DRM_MODE_DPMS_ON) {
  4348. encoder->connectors_active = true;
  4349. intel_crtc_update_dpms(encoder->base.crtc);
  4350. } else {
  4351. encoder->connectors_active = false;
  4352. intel_crtc_update_dpms(encoder->base.crtc);
  4353. }
  4354. }
  4355. /* Cross check the actual hw state with our own modeset state tracking (and it's
  4356. * internal consistency). */
  4357. static void intel_connector_check_state(struct intel_connector *connector)
  4358. {
  4359. if (connector->get_hw_state(connector)) {
  4360. struct intel_encoder *encoder = connector->encoder;
  4361. struct drm_crtc *crtc;
  4362. bool encoder_enabled;
  4363. enum pipe pipe;
  4364. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4365. connector->base.base.id,
  4366. connector->base.name);
  4367. /* there is no real hw state for MST connectors */
  4368. if (connector->mst_port)
  4369. return;
  4370. WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  4371. "wrong connector dpms state\n");
  4372. WARN(connector->base.encoder != &encoder->base,
  4373. "active connector not linked to encoder\n");
  4374. if (encoder) {
  4375. WARN(!encoder->connectors_active,
  4376. "encoder->connectors_active not set\n");
  4377. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  4378. WARN(!encoder_enabled, "encoder not enabled\n");
  4379. if (WARN_ON(!encoder->base.crtc))
  4380. return;
  4381. crtc = encoder->base.crtc;
  4382. WARN(!crtc->enabled, "crtc not enabled\n");
  4383. WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  4384. WARN(pipe != to_intel_crtc(crtc)->pipe,
  4385. "encoder active on the wrong pipe\n");
  4386. }
  4387. }
  4388. }
  4389. /* Even simpler default implementation, if there's really no special case to
  4390. * consider. */
  4391. void intel_connector_dpms(struct drm_connector *connector, int mode)
  4392. {
  4393. /* All the simple cases only support two dpms states. */
  4394. if (mode != DRM_MODE_DPMS_ON)
  4395. mode = DRM_MODE_DPMS_OFF;
  4396. if (mode == connector->dpms)
  4397. return;
  4398. connector->dpms = mode;
  4399. /* Only need to change hw state when actually enabled */
  4400. if (connector->encoder)
  4401. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  4402. intel_modeset_check_state(connector->dev);
  4403. }
  4404. /* Simple connector->get_hw_state implementation for encoders that support only
  4405. * one connector and no cloning and hence the encoder state determines the state
  4406. * of the connector. */
  4407. bool intel_connector_get_hw_state(struct intel_connector *connector)
  4408. {
  4409. enum pipe pipe = 0;
  4410. struct intel_encoder *encoder = connector->encoder;
  4411. return encoder->get_hw_state(encoder, &pipe);
  4412. }
  4413. static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  4414. struct intel_crtc_config *pipe_config)
  4415. {
  4416. struct drm_i915_private *dev_priv = dev->dev_private;
  4417. struct intel_crtc *pipe_B_crtc =
  4418. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  4419. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  4420. pipe_name(pipe), pipe_config->fdi_lanes);
  4421. if (pipe_config->fdi_lanes > 4) {
  4422. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  4423. pipe_name(pipe), pipe_config->fdi_lanes);
  4424. return false;
  4425. }
  4426. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  4427. if (pipe_config->fdi_lanes > 2) {
  4428. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  4429. pipe_config->fdi_lanes);
  4430. return false;
  4431. } else {
  4432. return true;
  4433. }
  4434. }
  4435. if (INTEL_INFO(dev)->num_pipes == 2)
  4436. return true;
  4437. /* Ivybridge 3 pipe is really complicated */
  4438. switch (pipe) {
  4439. case PIPE_A:
  4440. return true;
  4441. case PIPE_B:
  4442. if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
  4443. pipe_config->fdi_lanes > 2) {
  4444. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4445. pipe_name(pipe), pipe_config->fdi_lanes);
  4446. return false;
  4447. }
  4448. return true;
  4449. case PIPE_C:
  4450. if (!pipe_has_enabled_pch(pipe_B_crtc) ||
  4451. pipe_B_crtc->config.fdi_lanes <= 2) {
  4452. if (pipe_config->fdi_lanes > 2) {
  4453. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4454. pipe_name(pipe), pipe_config->fdi_lanes);
  4455. return false;
  4456. }
  4457. } else {
  4458. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  4459. return false;
  4460. }
  4461. return true;
  4462. default:
  4463. BUG();
  4464. }
  4465. }
  4466. #define RETRY 1
  4467. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  4468. struct intel_crtc_config *pipe_config)
  4469. {
  4470. struct drm_device *dev = intel_crtc->base.dev;
  4471. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4472. int lane, link_bw, fdi_dotclock;
  4473. bool setup_ok, needs_recompute = false;
  4474. retry:
  4475. /* FDI is a binary signal running at ~2.7GHz, encoding
  4476. * each output octet as 10 bits. The actual frequency
  4477. * is stored as a divider into a 100MHz clock, and the
  4478. * mode pixel clock is stored in units of 1KHz.
  4479. * Hence the bw of each lane in terms of the mode signal
  4480. * is:
  4481. */
  4482. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4483. fdi_dotclock = adjusted_mode->crtc_clock;
  4484. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  4485. pipe_config->pipe_bpp);
  4486. pipe_config->fdi_lanes = lane;
  4487. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  4488. link_bw, &pipe_config->fdi_m_n);
  4489. setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  4490. intel_crtc->pipe, pipe_config);
  4491. if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
  4492. pipe_config->pipe_bpp -= 2*3;
  4493. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  4494. pipe_config->pipe_bpp);
  4495. needs_recompute = true;
  4496. pipe_config->bw_constrained = true;
  4497. goto retry;
  4498. }
  4499. if (needs_recompute)
  4500. return RETRY;
  4501. return setup_ok ? 0 : -EINVAL;
  4502. }
  4503. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  4504. struct intel_crtc_config *pipe_config)
  4505. {
  4506. pipe_config->ips_enabled = i915.enable_ips &&
  4507. hsw_crtc_supports_ips(crtc) &&
  4508. pipe_config->pipe_bpp <= 24;
  4509. }
  4510. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  4511. struct intel_crtc_config *pipe_config)
  4512. {
  4513. struct drm_device *dev = crtc->base.dev;
  4514. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4515. /* FIXME should check pixel clock limits on all platforms */
  4516. if (INTEL_INFO(dev)->gen < 4) {
  4517. struct drm_i915_private *dev_priv = dev->dev_private;
  4518. int clock_limit =
  4519. dev_priv->display.get_display_clock_speed(dev);
  4520. /*
  4521. * Enable pixel doubling when the dot clock
  4522. * is > 90% of the (display) core speed.
  4523. *
  4524. * GDG double wide on either pipe,
  4525. * otherwise pipe A only.
  4526. */
  4527. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  4528. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  4529. clock_limit *= 2;
  4530. pipe_config->double_wide = true;
  4531. }
  4532. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  4533. return -EINVAL;
  4534. }
  4535. /*
  4536. * Pipe horizontal size must be even in:
  4537. * - DVO ganged mode
  4538. * - LVDS dual channel mode
  4539. * - Double wide pipe
  4540. */
  4541. if ((intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  4542. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  4543. pipe_config->pipe_src_w &= ~1;
  4544. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  4545. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  4546. */
  4547. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  4548. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  4549. return -EINVAL;
  4550. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
  4551. pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
  4552. } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
  4553. /* only a 8bpc pipe, with 6bpc dither through the panel fitter
  4554. * for lvds. */
  4555. pipe_config->pipe_bpp = 8*3;
  4556. }
  4557. if (HAS_IPS(dev))
  4558. hsw_compute_ips_config(crtc, pipe_config);
  4559. /*
  4560. * XXX: PCH/WRPLL clock sharing is done in ->mode_set, so make sure the
  4561. * old clock survives for now.
  4562. */
  4563. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev) || HAS_DDI(dev))
  4564. pipe_config->shared_dpll = crtc->config.shared_dpll;
  4565. if (pipe_config->has_pch_encoder)
  4566. return ironlake_fdi_compute_config(crtc, pipe_config);
  4567. return 0;
  4568. }
  4569. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  4570. {
  4571. struct drm_i915_private *dev_priv = dev->dev_private;
  4572. int vco = valleyview_get_vco(dev_priv);
  4573. u32 val;
  4574. int divider;
  4575. /* FIXME: Punit isn't quite ready yet */
  4576. if (IS_CHERRYVIEW(dev))
  4577. return 400000;
  4578. mutex_lock(&dev_priv->dpio_lock);
  4579. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4580. mutex_unlock(&dev_priv->dpio_lock);
  4581. divider = val & DISPLAY_FREQUENCY_VALUES;
  4582. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  4583. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4584. "cdclk change in progress\n");
  4585. return DIV_ROUND_CLOSEST(vco << 1, divider + 1);
  4586. }
  4587. static int i945_get_display_clock_speed(struct drm_device *dev)
  4588. {
  4589. return 400000;
  4590. }
  4591. static int i915_get_display_clock_speed(struct drm_device *dev)
  4592. {
  4593. return 333000;
  4594. }
  4595. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  4596. {
  4597. return 200000;
  4598. }
  4599. static int pnv_get_display_clock_speed(struct drm_device *dev)
  4600. {
  4601. u16 gcfgc = 0;
  4602. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4603. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4604. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  4605. return 267000;
  4606. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  4607. return 333000;
  4608. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  4609. return 444000;
  4610. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  4611. return 200000;
  4612. default:
  4613. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  4614. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  4615. return 133000;
  4616. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  4617. return 167000;
  4618. }
  4619. }
  4620. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  4621. {
  4622. u16 gcfgc = 0;
  4623. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4624. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  4625. return 133000;
  4626. else {
  4627. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4628. case GC_DISPLAY_CLOCK_333_MHZ:
  4629. return 333000;
  4630. default:
  4631. case GC_DISPLAY_CLOCK_190_200_MHZ:
  4632. return 190000;
  4633. }
  4634. }
  4635. }
  4636. static int i865_get_display_clock_speed(struct drm_device *dev)
  4637. {
  4638. return 266000;
  4639. }
  4640. static int i855_get_display_clock_speed(struct drm_device *dev)
  4641. {
  4642. u16 hpllcc = 0;
  4643. /* Assume that the hardware is in the high speed state. This
  4644. * should be the default.
  4645. */
  4646. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  4647. case GC_CLOCK_133_200:
  4648. case GC_CLOCK_100_200:
  4649. return 200000;
  4650. case GC_CLOCK_166_250:
  4651. return 250000;
  4652. case GC_CLOCK_100_133:
  4653. return 133000;
  4654. }
  4655. /* Shouldn't happen */
  4656. return 0;
  4657. }
  4658. static int i830_get_display_clock_speed(struct drm_device *dev)
  4659. {
  4660. return 133000;
  4661. }
  4662. static void
  4663. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  4664. {
  4665. while (*num > DATA_LINK_M_N_MASK ||
  4666. *den > DATA_LINK_M_N_MASK) {
  4667. *num >>= 1;
  4668. *den >>= 1;
  4669. }
  4670. }
  4671. static void compute_m_n(unsigned int m, unsigned int n,
  4672. uint32_t *ret_m, uint32_t *ret_n)
  4673. {
  4674. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  4675. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  4676. intel_reduce_m_n_ratio(ret_m, ret_n);
  4677. }
  4678. void
  4679. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  4680. int pixel_clock, int link_clock,
  4681. struct intel_link_m_n *m_n)
  4682. {
  4683. m_n->tu = 64;
  4684. compute_m_n(bits_per_pixel * pixel_clock,
  4685. link_clock * nlanes * 8,
  4686. &m_n->gmch_m, &m_n->gmch_n);
  4687. compute_m_n(pixel_clock, link_clock,
  4688. &m_n->link_m, &m_n->link_n);
  4689. }
  4690. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  4691. {
  4692. if (i915.panel_use_ssc >= 0)
  4693. return i915.panel_use_ssc != 0;
  4694. return dev_priv->vbt.lvds_use_ssc
  4695. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  4696. }
  4697. static int i9xx_get_refclk(struct intel_crtc *crtc, int num_connectors)
  4698. {
  4699. struct drm_device *dev = crtc->base.dev;
  4700. struct drm_i915_private *dev_priv = dev->dev_private;
  4701. int refclk;
  4702. if (IS_VALLEYVIEW(dev)) {
  4703. refclk = 100000;
  4704. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  4705. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4706. refclk = dev_priv->vbt.lvds_ssc_freq;
  4707. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  4708. } else if (!IS_GEN2(dev)) {
  4709. refclk = 96000;
  4710. } else {
  4711. refclk = 48000;
  4712. }
  4713. return refclk;
  4714. }
  4715. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  4716. {
  4717. return (1 << dpll->n) << 16 | dpll->m2;
  4718. }
  4719. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  4720. {
  4721. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  4722. }
  4723. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  4724. intel_clock_t *reduced_clock)
  4725. {
  4726. struct drm_device *dev = crtc->base.dev;
  4727. u32 fp, fp2 = 0;
  4728. if (IS_PINEVIEW(dev)) {
  4729. fp = pnv_dpll_compute_fp(&crtc->config.dpll);
  4730. if (reduced_clock)
  4731. fp2 = pnv_dpll_compute_fp(reduced_clock);
  4732. } else {
  4733. fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
  4734. if (reduced_clock)
  4735. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  4736. }
  4737. crtc->config.dpll_hw_state.fp0 = fp;
  4738. crtc->lowfreq_avail = false;
  4739. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  4740. reduced_clock && i915.powersave) {
  4741. crtc->config.dpll_hw_state.fp1 = fp2;
  4742. crtc->lowfreq_avail = true;
  4743. } else {
  4744. crtc->config.dpll_hw_state.fp1 = fp;
  4745. }
  4746. }
  4747. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  4748. pipe)
  4749. {
  4750. u32 reg_val;
  4751. /*
  4752. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  4753. * and set it to a reasonable value instead.
  4754. */
  4755. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4756. reg_val &= 0xffffff00;
  4757. reg_val |= 0x00000030;
  4758. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4759. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4760. reg_val &= 0x8cffffff;
  4761. reg_val = 0x8c000000;
  4762. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4763. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4764. reg_val &= 0xffffff00;
  4765. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4766. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4767. reg_val &= 0x00ffffff;
  4768. reg_val |= 0xb0000000;
  4769. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4770. }
  4771. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  4772. struct intel_link_m_n *m_n)
  4773. {
  4774. struct drm_device *dev = crtc->base.dev;
  4775. struct drm_i915_private *dev_priv = dev->dev_private;
  4776. int pipe = crtc->pipe;
  4777. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4778. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  4779. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  4780. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  4781. }
  4782. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  4783. struct intel_link_m_n *m_n,
  4784. struct intel_link_m_n *m2_n2)
  4785. {
  4786. struct drm_device *dev = crtc->base.dev;
  4787. struct drm_i915_private *dev_priv = dev->dev_private;
  4788. int pipe = crtc->pipe;
  4789. enum transcoder transcoder = crtc->config.cpu_transcoder;
  4790. if (INTEL_INFO(dev)->gen >= 5) {
  4791. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4792. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  4793. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  4794. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  4795. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  4796. * for gen < 8) and if DRRS is supported (to make sure the
  4797. * registers are not unnecessarily accessed).
  4798. */
  4799. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  4800. crtc->config.has_drrs) {
  4801. I915_WRITE(PIPE_DATA_M2(transcoder),
  4802. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  4803. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  4804. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  4805. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  4806. }
  4807. } else {
  4808. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4809. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  4810. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  4811. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  4812. }
  4813. }
  4814. void intel_dp_set_m_n(struct intel_crtc *crtc)
  4815. {
  4816. if (crtc->config.has_pch_encoder)
  4817. intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
  4818. else
  4819. intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n,
  4820. &crtc->config.dp_m2_n2);
  4821. }
  4822. static void vlv_update_pll(struct intel_crtc *crtc,
  4823. struct intel_crtc_config *pipe_config)
  4824. {
  4825. u32 dpll, dpll_md;
  4826. /*
  4827. * Enable DPIO clock input. We should never disable the reference
  4828. * clock for pipe B, since VGA hotplug / manual detection depends
  4829. * on it.
  4830. */
  4831. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  4832. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  4833. /* We should never disable this, set it here for state tracking */
  4834. if (crtc->pipe == PIPE_B)
  4835. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4836. dpll |= DPLL_VCO_ENABLE;
  4837. pipe_config->dpll_hw_state.dpll = dpll;
  4838. dpll_md = (pipe_config->pixel_multiplier - 1)
  4839. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4840. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  4841. }
  4842. static void vlv_prepare_pll(struct intel_crtc *crtc,
  4843. const struct intel_crtc_config *pipe_config)
  4844. {
  4845. struct drm_device *dev = crtc->base.dev;
  4846. struct drm_i915_private *dev_priv = dev->dev_private;
  4847. int pipe = crtc->pipe;
  4848. u32 mdiv;
  4849. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  4850. u32 coreclk, reg_val;
  4851. mutex_lock(&dev_priv->dpio_lock);
  4852. bestn = pipe_config->dpll.n;
  4853. bestm1 = pipe_config->dpll.m1;
  4854. bestm2 = pipe_config->dpll.m2;
  4855. bestp1 = pipe_config->dpll.p1;
  4856. bestp2 = pipe_config->dpll.p2;
  4857. /* See eDP HDMI DPIO driver vbios notes doc */
  4858. /* PLL B needs special handling */
  4859. if (pipe == PIPE_B)
  4860. vlv_pllb_recal_opamp(dev_priv, pipe);
  4861. /* Set up Tx target for periodic Rcomp update */
  4862. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  4863. /* Disable target IRef on PLL */
  4864. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  4865. reg_val &= 0x00ffffff;
  4866. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  4867. /* Disable fast lock */
  4868. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  4869. /* Set idtafcrecal before PLL is enabled */
  4870. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  4871. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  4872. mdiv |= ((bestn << DPIO_N_SHIFT));
  4873. mdiv |= (1 << DPIO_K_SHIFT);
  4874. /*
  4875. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  4876. * but we don't support that).
  4877. * Note: don't use the DAC post divider as it seems unstable.
  4878. */
  4879. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  4880. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4881. mdiv |= DPIO_ENABLE_CALIBRATION;
  4882. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4883. /* Set HBR and RBR LPF coefficients */
  4884. if (pipe_config->port_clock == 162000 ||
  4885. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  4886. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  4887. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4888. 0x009f0003);
  4889. else
  4890. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4891. 0x00d0000f);
  4892. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP) ||
  4893. intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  4894. /* Use SSC source */
  4895. if (pipe == PIPE_A)
  4896. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4897. 0x0df40000);
  4898. else
  4899. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4900. 0x0df70000);
  4901. } else { /* HDMI or VGA */
  4902. /* Use bend source */
  4903. if (pipe == PIPE_A)
  4904. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4905. 0x0df70000);
  4906. else
  4907. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4908. 0x0df40000);
  4909. }
  4910. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  4911. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  4912. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  4913. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  4914. coreclk |= 0x01000000;
  4915. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  4916. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  4917. mutex_unlock(&dev_priv->dpio_lock);
  4918. }
  4919. static void chv_update_pll(struct intel_crtc *crtc,
  4920. struct intel_crtc_config *pipe_config)
  4921. {
  4922. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  4923. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  4924. DPLL_VCO_ENABLE;
  4925. if (crtc->pipe != PIPE_A)
  4926. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4927. pipe_config->dpll_hw_state.dpll_md =
  4928. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4929. }
  4930. static void chv_prepare_pll(struct intel_crtc *crtc,
  4931. const struct intel_crtc_config *pipe_config)
  4932. {
  4933. struct drm_device *dev = crtc->base.dev;
  4934. struct drm_i915_private *dev_priv = dev->dev_private;
  4935. int pipe = crtc->pipe;
  4936. int dpll_reg = DPLL(crtc->pipe);
  4937. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  4938. u32 loopfilter, intcoeff;
  4939. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  4940. int refclk;
  4941. bestn = pipe_config->dpll.n;
  4942. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  4943. bestm1 = pipe_config->dpll.m1;
  4944. bestm2 = pipe_config->dpll.m2 >> 22;
  4945. bestp1 = pipe_config->dpll.p1;
  4946. bestp2 = pipe_config->dpll.p2;
  4947. /*
  4948. * Enable Refclk and SSC
  4949. */
  4950. I915_WRITE(dpll_reg,
  4951. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  4952. mutex_lock(&dev_priv->dpio_lock);
  4953. /* p1 and p2 divider */
  4954. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  4955. 5 << DPIO_CHV_S1_DIV_SHIFT |
  4956. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  4957. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  4958. 1 << DPIO_CHV_K_DIV_SHIFT);
  4959. /* Feedback post-divider - m2 */
  4960. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  4961. /* Feedback refclk divider - n and m1 */
  4962. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  4963. DPIO_CHV_M1_DIV_BY_2 |
  4964. 1 << DPIO_CHV_N_DIV_SHIFT);
  4965. /* M2 fraction division */
  4966. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  4967. /* M2 fraction division enable */
  4968. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
  4969. DPIO_CHV_FRAC_DIV_EN |
  4970. (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
  4971. /* Loop filter */
  4972. refclk = i9xx_get_refclk(crtc, 0);
  4973. loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
  4974. 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
  4975. if (refclk == 100000)
  4976. intcoeff = 11;
  4977. else if (refclk == 38400)
  4978. intcoeff = 10;
  4979. else
  4980. intcoeff = 9;
  4981. loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
  4982. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  4983. /* AFC Recal */
  4984. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  4985. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  4986. DPIO_AFC_RECAL);
  4987. mutex_unlock(&dev_priv->dpio_lock);
  4988. }
  4989. /**
  4990. * vlv_force_pll_on - forcibly enable just the PLL
  4991. * @dev_priv: i915 private structure
  4992. * @pipe: pipe PLL to enable
  4993. * @dpll: PLL configuration
  4994. *
  4995. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  4996. * in cases where we need the PLL enabled even when @pipe is not going to
  4997. * be enabled.
  4998. */
  4999. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  5000. const struct dpll *dpll)
  5001. {
  5002. struct intel_crtc *crtc =
  5003. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  5004. struct intel_crtc_config pipe_config = {
  5005. .pixel_multiplier = 1,
  5006. .dpll = *dpll,
  5007. };
  5008. if (IS_CHERRYVIEW(dev)) {
  5009. chv_update_pll(crtc, &pipe_config);
  5010. chv_prepare_pll(crtc, &pipe_config);
  5011. chv_enable_pll(crtc, &pipe_config);
  5012. } else {
  5013. vlv_update_pll(crtc, &pipe_config);
  5014. vlv_prepare_pll(crtc, &pipe_config);
  5015. vlv_enable_pll(crtc, &pipe_config);
  5016. }
  5017. }
  5018. /**
  5019. * vlv_force_pll_off - forcibly disable just the PLL
  5020. * @dev_priv: i915 private structure
  5021. * @pipe: pipe PLL to disable
  5022. *
  5023. * Disable the PLL for @pipe. To be used in cases where we need
  5024. * the PLL enabled even when @pipe is not going to be enabled.
  5025. */
  5026. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  5027. {
  5028. if (IS_CHERRYVIEW(dev))
  5029. chv_disable_pll(to_i915(dev), pipe);
  5030. else
  5031. vlv_disable_pll(to_i915(dev), pipe);
  5032. }
  5033. static void i9xx_update_pll(struct intel_crtc *crtc,
  5034. intel_clock_t *reduced_clock,
  5035. int num_connectors)
  5036. {
  5037. struct drm_device *dev = crtc->base.dev;
  5038. struct drm_i915_private *dev_priv = dev->dev_private;
  5039. u32 dpll;
  5040. bool is_sdvo;
  5041. struct dpll *clock = &crtc->config.dpll;
  5042. i9xx_update_pll_dividers(crtc, reduced_clock);
  5043. is_sdvo = intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO) ||
  5044. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
  5045. dpll = DPLL_VGA_MODE_DIS;
  5046. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  5047. dpll |= DPLLB_MODE_LVDS;
  5048. else
  5049. dpll |= DPLLB_MODE_DAC_SERIAL;
  5050. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5051. dpll |= (crtc->config.pixel_multiplier - 1)
  5052. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5053. }
  5054. if (is_sdvo)
  5055. dpll |= DPLL_SDVO_HIGH_SPEED;
  5056. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
  5057. dpll |= DPLL_SDVO_HIGH_SPEED;
  5058. /* compute bitmask from p1 value */
  5059. if (IS_PINEVIEW(dev))
  5060. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5061. else {
  5062. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5063. if (IS_G4X(dev) && reduced_clock)
  5064. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5065. }
  5066. switch (clock->p2) {
  5067. case 5:
  5068. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5069. break;
  5070. case 7:
  5071. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5072. break;
  5073. case 10:
  5074. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5075. break;
  5076. case 14:
  5077. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5078. break;
  5079. }
  5080. if (INTEL_INFO(dev)->gen >= 4)
  5081. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5082. if (crtc->config.sdvo_tv_clock)
  5083. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5084. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  5085. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5086. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5087. else
  5088. dpll |= PLL_REF_INPUT_DREFCLK;
  5089. dpll |= DPLL_VCO_ENABLE;
  5090. crtc->config.dpll_hw_state.dpll = dpll;
  5091. if (INTEL_INFO(dev)->gen >= 4) {
  5092. u32 dpll_md = (crtc->config.pixel_multiplier - 1)
  5093. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5094. crtc->config.dpll_hw_state.dpll_md = dpll_md;
  5095. }
  5096. }
  5097. static void i8xx_update_pll(struct intel_crtc *crtc,
  5098. intel_clock_t *reduced_clock,
  5099. int num_connectors)
  5100. {
  5101. struct drm_device *dev = crtc->base.dev;
  5102. struct drm_i915_private *dev_priv = dev->dev_private;
  5103. u32 dpll;
  5104. struct dpll *clock = &crtc->config.dpll;
  5105. i9xx_update_pll_dividers(crtc, reduced_clock);
  5106. dpll = DPLL_VGA_MODE_DIS;
  5107. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  5108. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5109. } else {
  5110. if (clock->p1 == 2)
  5111. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5112. else
  5113. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5114. if (clock->p2 == 4)
  5115. dpll |= PLL_P2_DIVIDE_BY_4;
  5116. }
  5117. if (!IS_I830(dev) && intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
  5118. dpll |= DPLL_DVO_2X_MODE;
  5119. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  5120. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5121. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5122. else
  5123. dpll |= PLL_REF_INPUT_DREFCLK;
  5124. dpll |= DPLL_VCO_ENABLE;
  5125. crtc->config.dpll_hw_state.dpll = dpll;
  5126. }
  5127. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5128. {
  5129. struct drm_device *dev = intel_crtc->base.dev;
  5130. struct drm_i915_private *dev_priv = dev->dev_private;
  5131. enum pipe pipe = intel_crtc->pipe;
  5132. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  5133. struct drm_display_mode *adjusted_mode =
  5134. &intel_crtc->config.adjusted_mode;
  5135. uint32_t crtc_vtotal, crtc_vblank_end;
  5136. int vsyncshift = 0;
  5137. /* We need to be careful not to changed the adjusted mode, for otherwise
  5138. * the hw state checker will get angry at the mismatch. */
  5139. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5140. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5141. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5142. /* the chip adds 2 halflines automatically */
  5143. crtc_vtotal -= 1;
  5144. crtc_vblank_end -= 1;
  5145. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  5146. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5147. else
  5148. vsyncshift = adjusted_mode->crtc_hsync_start -
  5149. adjusted_mode->crtc_htotal / 2;
  5150. if (vsyncshift < 0)
  5151. vsyncshift += adjusted_mode->crtc_htotal;
  5152. }
  5153. if (INTEL_INFO(dev)->gen > 3)
  5154. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5155. I915_WRITE(HTOTAL(cpu_transcoder),
  5156. (adjusted_mode->crtc_hdisplay - 1) |
  5157. ((adjusted_mode->crtc_htotal - 1) << 16));
  5158. I915_WRITE(HBLANK(cpu_transcoder),
  5159. (adjusted_mode->crtc_hblank_start - 1) |
  5160. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5161. I915_WRITE(HSYNC(cpu_transcoder),
  5162. (adjusted_mode->crtc_hsync_start - 1) |
  5163. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5164. I915_WRITE(VTOTAL(cpu_transcoder),
  5165. (adjusted_mode->crtc_vdisplay - 1) |
  5166. ((crtc_vtotal - 1) << 16));
  5167. I915_WRITE(VBLANK(cpu_transcoder),
  5168. (adjusted_mode->crtc_vblank_start - 1) |
  5169. ((crtc_vblank_end - 1) << 16));
  5170. I915_WRITE(VSYNC(cpu_transcoder),
  5171. (adjusted_mode->crtc_vsync_start - 1) |
  5172. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5173. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5174. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5175. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5176. * bits. */
  5177. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  5178. (pipe == PIPE_B || pipe == PIPE_C))
  5179. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5180. /* pipesrc controls the size that is scaled from, which should
  5181. * always be the user's requested size.
  5182. */
  5183. I915_WRITE(PIPESRC(pipe),
  5184. ((intel_crtc->config.pipe_src_w - 1) << 16) |
  5185. (intel_crtc->config.pipe_src_h - 1));
  5186. }
  5187. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5188. struct intel_crtc_config *pipe_config)
  5189. {
  5190. struct drm_device *dev = crtc->base.dev;
  5191. struct drm_i915_private *dev_priv = dev->dev_private;
  5192. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5193. uint32_t tmp;
  5194. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5195. pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5196. pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5197. tmp = I915_READ(HBLANK(cpu_transcoder));
  5198. pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5199. pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5200. tmp = I915_READ(HSYNC(cpu_transcoder));
  5201. pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5202. pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5203. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5204. pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5205. pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  5206. tmp = I915_READ(VBLANK(cpu_transcoder));
  5207. pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  5208. pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  5209. tmp = I915_READ(VSYNC(cpu_transcoder));
  5210. pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  5211. pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  5212. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  5213. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  5214. pipe_config->adjusted_mode.crtc_vtotal += 1;
  5215. pipe_config->adjusted_mode.crtc_vblank_end += 1;
  5216. }
  5217. tmp = I915_READ(PIPESRC(crtc->pipe));
  5218. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  5219. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  5220. pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
  5221. pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
  5222. }
  5223. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  5224. struct intel_crtc_config *pipe_config)
  5225. {
  5226. mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
  5227. mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
  5228. mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
  5229. mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
  5230. mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
  5231. mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
  5232. mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
  5233. mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
  5234. mode->flags = pipe_config->adjusted_mode.flags;
  5235. mode->clock = pipe_config->adjusted_mode.crtc_clock;
  5236. mode->flags |= pipe_config->adjusted_mode.flags;
  5237. }
  5238. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  5239. {
  5240. struct drm_device *dev = intel_crtc->base.dev;
  5241. struct drm_i915_private *dev_priv = dev->dev_private;
  5242. uint32_t pipeconf;
  5243. pipeconf = 0;
  5244. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  5245. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  5246. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  5247. if (intel_crtc->config.double_wide)
  5248. pipeconf |= PIPECONF_DOUBLE_WIDE;
  5249. /* only g4x and later have fancy bpc/dither controls */
  5250. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5251. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  5252. if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
  5253. pipeconf |= PIPECONF_DITHER_EN |
  5254. PIPECONF_DITHER_TYPE_SP;
  5255. switch (intel_crtc->config.pipe_bpp) {
  5256. case 18:
  5257. pipeconf |= PIPECONF_6BPC;
  5258. break;
  5259. case 24:
  5260. pipeconf |= PIPECONF_8BPC;
  5261. break;
  5262. case 30:
  5263. pipeconf |= PIPECONF_10BPC;
  5264. break;
  5265. default:
  5266. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5267. BUG();
  5268. }
  5269. }
  5270. if (HAS_PIPE_CXSR(dev)) {
  5271. if (intel_crtc->lowfreq_avail) {
  5272. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  5273. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  5274. } else {
  5275. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  5276. }
  5277. }
  5278. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  5279. if (INTEL_INFO(dev)->gen < 4 ||
  5280. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  5281. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  5282. else
  5283. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  5284. } else
  5285. pipeconf |= PIPECONF_PROGRESSIVE;
  5286. if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
  5287. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  5288. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  5289. POSTING_READ(PIPECONF(intel_crtc->pipe));
  5290. }
  5291. static int i9xx_crtc_mode_set(struct intel_crtc *crtc,
  5292. int x, int y,
  5293. struct drm_framebuffer *fb)
  5294. {
  5295. struct drm_device *dev = crtc->base.dev;
  5296. struct drm_i915_private *dev_priv = dev->dev_private;
  5297. int refclk, num_connectors = 0;
  5298. intel_clock_t clock, reduced_clock;
  5299. bool ok, has_reduced_clock = false;
  5300. bool is_lvds = false, is_dsi = false;
  5301. struct intel_encoder *encoder;
  5302. const intel_limit_t *limit;
  5303. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  5304. switch (encoder->type) {
  5305. case INTEL_OUTPUT_LVDS:
  5306. is_lvds = true;
  5307. break;
  5308. case INTEL_OUTPUT_DSI:
  5309. is_dsi = true;
  5310. break;
  5311. default:
  5312. break;
  5313. }
  5314. num_connectors++;
  5315. }
  5316. if (is_dsi)
  5317. return 0;
  5318. if (!crtc->config.clock_set) {
  5319. refclk = i9xx_get_refclk(crtc, num_connectors);
  5320. /*
  5321. * Returns a set of divisors for the desired target clock with
  5322. * the given refclk, or FALSE. The returned values represent
  5323. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  5324. * 2) / p1 / p2.
  5325. */
  5326. limit = intel_limit(crtc, refclk);
  5327. ok = dev_priv->display.find_dpll(limit, crtc,
  5328. crtc->config.port_clock,
  5329. refclk, NULL, &clock);
  5330. if (!ok) {
  5331. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5332. return -EINVAL;
  5333. }
  5334. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5335. /*
  5336. * Ensure we match the reduced clock's P to the target
  5337. * clock. If the clocks don't match, we can't switch
  5338. * the display clock by using the FP0/FP1. In such case
  5339. * we will disable the LVDS downclock feature.
  5340. */
  5341. has_reduced_clock =
  5342. dev_priv->display.find_dpll(limit, crtc,
  5343. dev_priv->lvds_downclock,
  5344. refclk, &clock,
  5345. &reduced_clock);
  5346. }
  5347. /* Compat-code for transition, will disappear. */
  5348. crtc->config.dpll.n = clock.n;
  5349. crtc->config.dpll.m1 = clock.m1;
  5350. crtc->config.dpll.m2 = clock.m2;
  5351. crtc->config.dpll.p1 = clock.p1;
  5352. crtc->config.dpll.p2 = clock.p2;
  5353. }
  5354. if (IS_GEN2(dev)) {
  5355. i8xx_update_pll(crtc,
  5356. has_reduced_clock ? &reduced_clock : NULL,
  5357. num_connectors);
  5358. } else if (IS_CHERRYVIEW(dev)) {
  5359. chv_update_pll(crtc, &crtc->config);
  5360. } else if (IS_VALLEYVIEW(dev)) {
  5361. vlv_update_pll(crtc, &crtc->config);
  5362. } else {
  5363. i9xx_update_pll(crtc,
  5364. has_reduced_clock ? &reduced_clock : NULL,
  5365. num_connectors);
  5366. }
  5367. return 0;
  5368. }
  5369. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  5370. struct intel_crtc_config *pipe_config)
  5371. {
  5372. struct drm_device *dev = crtc->base.dev;
  5373. struct drm_i915_private *dev_priv = dev->dev_private;
  5374. uint32_t tmp;
  5375. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  5376. return;
  5377. tmp = I915_READ(PFIT_CONTROL);
  5378. if (!(tmp & PFIT_ENABLE))
  5379. return;
  5380. /* Check whether the pfit is attached to our pipe. */
  5381. if (INTEL_INFO(dev)->gen < 4) {
  5382. if (crtc->pipe != PIPE_B)
  5383. return;
  5384. } else {
  5385. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  5386. return;
  5387. }
  5388. pipe_config->gmch_pfit.control = tmp;
  5389. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  5390. if (INTEL_INFO(dev)->gen < 5)
  5391. pipe_config->gmch_pfit.lvds_border_bits =
  5392. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  5393. }
  5394. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  5395. struct intel_crtc_config *pipe_config)
  5396. {
  5397. struct drm_device *dev = crtc->base.dev;
  5398. struct drm_i915_private *dev_priv = dev->dev_private;
  5399. int pipe = pipe_config->cpu_transcoder;
  5400. intel_clock_t clock;
  5401. u32 mdiv;
  5402. int refclk = 100000;
  5403. /* In case of MIPI DPLL will not even be used */
  5404. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  5405. return;
  5406. mutex_lock(&dev_priv->dpio_lock);
  5407. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  5408. mutex_unlock(&dev_priv->dpio_lock);
  5409. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  5410. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  5411. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  5412. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  5413. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  5414. vlv_clock(refclk, &clock);
  5415. /* clock.dot is the fast clock */
  5416. pipe_config->port_clock = clock.dot / 5;
  5417. }
  5418. static void i9xx_get_plane_config(struct intel_crtc *crtc,
  5419. struct intel_plane_config *plane_config)
  5420. {
  5421. struct drm_device *dev = crtc->base.dev;
  5422. struct drm_i915_private *dev_priv = dev->dev_private;
  5423. u32 val, base, offset;
  5424. int pipe = crtc->pipe, plane = crtc->plane;
  5425. int fourcc, pixel_format;
  5426. int aligned_height;
  5427. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  5428. if (!crtc->base.primary->fb) {
  5429. DRM_DEBUG_KMS("failed to alloc fb\n");
  5430. return;
  5431. }
  5432. val = I915_READ(DSPCNTR(plane));
  5433. if (INTEL_INFO(dev)->gen >= 4)
  5434. if (val & DISPPLANE_TILED)
  5435. plane_config->tiled = true;
  5436. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  5437. fourcc = intel_format_to_fourcc(pixel_format);
  5438. crtc->base.primary->fb->pixel_format = fourcc;
  5439. crtc->base.primary->fb->bits_per_pixel =
  5440. drm_format_plane_cpp(fourcc, 0) * 8;
  5441. if (INTEL_INFO(dev)->gen >= 4) {
  5442. if (plane_config->tiled)
  5443. offset = I915_READ(DSPTILEOFF(plane));
  5444. else
  5445. offset = I915_READ(DSPLINOFF(plane));
  5446. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  5447. } else {
  5448. base = I915_READ(DSPADDR(plane));
  5449. }
  5450. plane_config->base = base;
  5451. val = I915_READ(PIPESRC(pipe));
  5452. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  5453. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  5454. val = I915_READ(DSPSTRIDE(pipe));
  5455. crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
  5456. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  5457. plane_config->tiled);
  5458. plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
  5459. aligned_height);
  5460. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  5461. pipe, plane, crtc->base.primary->fb->width,
  5462. crtc->base.primary->fb->height,
  5463. crtc->base.primary->fb->bits_per_pixel, base,
  5464. crtc->base.primary->fb->pitches[0],
  5465. plane_config->size);
  5466. }
  5467. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  5468. struct intel_crtc_config *pipe_config)
  5469. {
  5470. struct drm_device *dev = crtc->base.dev;
  5471. struct drm_i915_private *dev_priv = dev->dev_private;
  5472. int pipe = pipe_config->cpu_transcoder;
  5473. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5474. intel_clock_t clock;
  5475. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  5476. int refclk = 100000;
  5477. mutex_lock(&dev_priv->dpio_lock);
  5478. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  5479. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  5480. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  5481. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  5482. mutex_unlock(&dev_priv->dpio_lock);
  5483. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  5484. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  5485. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  5486. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  5487. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  5488. chv_clock(refclk, &clock);
  5489. /* clock.dot is the fast clock */
  5490. pipe_config->port_clock = clock.dot / 5;
  5491. }
  5492. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  5493. struct intel_crtc_config *pipe_config)
  5494. {
  5495. struct drm_device *dev = crtc->base.dev;
  5496. struct drm_i915_private *dev_priv = dev->dev_private;
  5497. uint32_t tmp;
  5498. if (!intel_display_power_is_enabled(dev_priv,
  5499. POWER_DOMAIN_PIPE(crtc->pipe)))
  5500. return false;
  5501. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5502. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5503. tmp = I915_READ(PIPECONF(crtc->pipe));
  5504. if (!(tmp & PIPECONF_ENABLE))
  5505. return false;
  5506. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5507. switch (tmp & PIPECONF_BPC_MASK) {
  5508. case PIPECONF_6BPC:
  5509. pipe_config->pipe_bpp = 18;
  5510. break;
  5511. case PIPECONF_8BPC:
  5512. pipe_config->pipe_bpp = 24;
  5513. break;
  5514. case PIPECONF_10BPC:
  5515. pipe_config->pipe_bpp = 30;
  5516. break;
  5517. default:
  5518. break;
  5519. }
  5520. }
  5521. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  5522. pipe_config->limited_color_range = true;
  5523. if (INTEL_INFO(dev)->gen < 4)
  5524. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  5525. intel_get_pipe_timings(crtc, pipe_config);
  5526. i9xx_get_pfit_config(crtc, pipe_config);
  5527. if (INTEL_INFO(dev)->gen >= 4) {
  5528. tmp = I915_READ(DPLL_MD(crtc->pipe));
  5529. pipe_config->pixel_multiplier =
  5530. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  5531. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  5532. pipe_config->dpll_hw_state.dpll_md = tmp;
  5533. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5534. tmp = I915_READ(DPLL(crtc->pipe));
  5535. pipe_config->pixel_multiplier =
  5536. ((tmp & SDVO_MULTIPLIER_MASK)
  5537. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  5538. } else {
  5539. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  5540. * port and will be fixed up in the encoder->get_config
  5541. * function. */
  5542. pipe_config->pixel_multiplier = 1;
  5543. }
  5544. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  5545. if (!IS_VALLEYVIEW(dev)) {
  5546. /*
  5547. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  5548. * on 830. Filter it out here so that we don't
  5549. * report errors due to that.
  5550. */
  5551. if (IS_I830(dev))
  5552. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  5553. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  5554. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  5555. } else {
  5556. /* Mask out read-only status bits. */
  5557. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  5558. DPLL_PORTC_READY_MASK |
  5559. DPLL_PORTB_READY_MASK);
  5560. }
  5561. if (IS_CHERRYVIEW(dev))
  5562. chv_crtc_clock_get(crtc, pipe_config);
  5563. else if (IS_VALLEYVIEW(dev))
  5564. vlv_crtc_clock_get(crtc, pipe_config);
  5565. else
  5566. i9xx_crtc_clock_get(crtc, pipe_config);
  5567. return true;
  5568. }
  5569. static void ironlake_init_pch_refclk(struct drm_device *dev)
  5570. {
  5571. struct drm_i915_private *dev_priv = dev->dev_private;
  5572. struct intel_encoder *encoder;
  5573. u32 val, final;
  5574. bool has_lvds = false;
  5575. bool has_cpu_edp = false;
  5576. bool has_panel = false;
  5577. bool has_ck505 = false;
  5578. bool can_ssc = false;
  5579. /* We need to take the global config into account */
  5580. for_each_intel_encoder(dev, encoder) {
  5581. switch (encoder->type) {
  5582. case INTEL_OUTPUT_LVDS:
  5583. has_panel = true;
  5584. has_lvds = true;
  5585. break;
  5586. case INTEL_OUTPUT_EDP:
  5587. has_panel = true;
  5588. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  5589. has_cpu_edp = true;
  5590. break;
  5591. default:
  5592. break;
  5593. }
  5594. }
  5595. if (HAS_PCH_IBX(dev)) {
  5596. has_ck505 = dev_priv->vbt.display_clock_mode;
  5597. can_ssc = has_ck505;
  5598. } else {
  5599. has_ck505 = false;
  5600. can_ssc = true;
  5601. }
  5602. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  5603. has_panel, has_lvds, has_ck505);
  5604. /* Ironlake: try to setup display ref clock before DPLL
  5605. * enabling. This is only under driver's control after
  5606. * PCH B stepping, previous chipset stepping should be
  5607. * ignoring this setting.
  5608. */
  5609. val = I915_READ(PCH_DREF_CONTROL);
  5610. /* As we must carefully and slowly disable/enable each source in turn,
  5611. * compute the final state we want first and check if we need to
  5612. * make any changes at all.
  5613. */
  5614. final = val;
  5615. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  5616. if (has_ck505)
  5617. final |= DREF_NONSPREAD_CK505_ENABLE;
  5618. else
  5619. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  5620. final &= ~DREF_SSC_SOURCE_MASK;
  5621. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5622. final &= ~DREF_SSC1_ENABLE;
  5623. if (has_panel) {
  5624. final |= DREF_SSC_SOURCE_ENABLE;
  5625. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5626. final |= DREF_SSC1_ENABLE;
  5627. if (has_cpu_edp) {
  5628. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5629. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5630. else
  5631. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5632. } else
  5633. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5634. } else {
  5635. final |= DREF_SSC_SOURCE_DISABLE;
  5636. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5637. }
  5638. if (final == val)
  5639. return;
  5640. /* Always enable nonspread source */
  5641. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  5642. if (has_ck505)
  5643. val |= DREF_NONSPREAD_CK505_ENABLE;
  5644. else
  5645. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  5646. if (has_panel) {
  5647. val &= ~DREF_SSC_SOURCE_MASK;
  5648. val |= DREF_SSC_SOURCE_ENABLE;
  5649. /* SSC must be turned on before enabling the CPU output */
  5650. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5651. DRM_DEBUG_KMS("Using SSC on panel\n");
  5652. val |= DREF_SSC1_ENABLE;
  5653. } else
  5654. val &= ~DREF_SSC1_ENABLE;
  5655. /* Get SSC going before enabling the outputs */
  5656. I915_WRITE(PCH_DREF_CONTROL, val);
  5657. POSTING_READ(PCH_DREF_CONTROL);
  5658. udelay(200);
  5659. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5660. /* Enable CPU source on CPU attached eDP */
  5661. if (has_cpu_edp) {
  5662. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5663. DRM_DEBUG_KMS("Using SSC on eDP\n");
  5664. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5665. } else
  5666. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5667. } else
  5668. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5669. I915_WRITE(PCH_DREF_CONTROL, val);
  5670. POSTING_READ(PCH_DREF_CONTROL);
  5671. udelay(200);
  5672. } else {
  5673. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  5674. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5675. /* Turn off CPU output */
  5676. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5677. I915_WRITE(PCH_DREF_CONTROL, val);
  5678. POSTING_READ(PCH_DREF_CONTROL);
  5679. udelay(200);
  5680. /* Turn off the SSC source */
  5681. val &= ~DREF_SSC_SOURCE_MASK;
  5682. val |= DREF_SSC_SOURCE_DISABLE;
  5683. /* Turn off SSC1 */
  5684. val &= ~DREF_SSC1_ENABLE;
  5685. I915_WRITE(PCH_DREF_CONTROL, val);
  5686. POSTING_READ(PCH_DREF_CONTROL);
  5687. udelay(200);
  5688. }
  5689. BUG_ON(val != final);
  5690. }
  5691. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  5692. {
  5693. uint32_t tmp;
  5694. tmp = I915_READ(SOUTH_CHICKEN2);
  5695. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  5696. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5697. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  5698. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  5699. DRM_ERROR("FDI mPHY reset assert timeout\n");
  5700. tmp = I915_READ(SOUTH_CHICKEN2);
  5701. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  5702. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5703. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  5704. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  5705. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  5706. }
  5707. /* WaMPhyProgramming:hsw */
  5708. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  5709. {
  5710. uint32_t tmp;
  5711. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  5712. tmp &= ~(0xFF << 24);
  5713. tmp |= (0x12 << 24);
  5714. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  5715. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  5716. tmp |= (1 << 11);
  5717. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  5718. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  5719. tmp |= (1 << 11);
  5720. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  5721. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  5722. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5723. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  5724. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  5725. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5726. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  5727. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  5728. tmp &= ~(7 << 13);
  5729. tmp |= (5 << 13);
  5730. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  5731. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  5732. tmp &= ~(7 << 13);
  5733. tmp |= (5 << 13);
  5734. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  5735. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  5736. tmp &= ~0xFF;
  5737. tmp |= 0x1C;
  5738. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  5739. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  5740. tmp &= ~0xFF;
  5741. tmp |= 0x1C;
  5742. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  5743. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  5744. tmp &= ~(0xFF << 16);
  5745. tmp |= (0x1C << 16);
  5746. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  5747. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  5748. tmp &= ~(0xFF << 16);
  5749. tmp |= (0x1C << 16);
  5750. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  5751. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  5752. tmp |= (1 << 27);
  5753. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  5754. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  5755. tmp |= (1 << 27);
  5756. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  5757. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  5758. tmp &= ~(0xF << 28);
  5759. tmp |= (4 << 28);
  5760. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  5761. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  5762. tmp &= ~(0xF << 28);
  5763. tmp |= (4 << 28);
  5764. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  5765. }
  5766. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  5767. * Programming" based on the parameters passed:
  5768. * - Sequence to enable CLKOUT_DP
  5769. * - Sequence to enable CLKOUT_DP without spread
  5770. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  5771. */
  5772. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  5773. bool with_fdi)
  5774. {
  5775. struct drm_i915_private *dev_priv = dev->dev_private;
  5776. uint32_t reg, tmp;
  5777. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  5778. with_spread = true;
  5779. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  5780. with_fdi, "LP PCH doesn't have FDI\n"))
  5781. with_fdi = false;
  5782. mutex_lock(&dev_priv->dpio_lock);
  5783. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5784. tmp &= ~SBI_SSCCTL_DISABLE;
  5785. tmp |= SBI_SSCCTL_PATHALT;
  5786. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5787. udelay(24);
  5788. if (with_spread) {
  5789. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5790. tmp &= ~SBI_SSCCTL_PATHALT;
  5791. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5792. if (with_fdi) {
  5793. lpt_reset_fdi_mphy(dev_priv);
  5794. lpt_program_fdi_mphy(dev_priv);
  5795. }
  5796. }
  5797. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5798. SBI_GEN0 : SBI_DBUFF0;
  5799. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5800. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5801. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5802. mutex_unlock(&dev_priv->dpio_lock);
  5803. }
  5804. /* Sequence to disable CLKOUT_DP */
  5805. static void lpt_disable_clkout_dp(struct drm_device *dev)
  5806. {
  5807. struct drm_i915_private *dev_priv = dev->dev_private;
  5808. uint32_t reg, tmp;
  5809. mutex_lock(&dev_priv->dpio_lock);
  5810. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5811. SBI_GEN0 : SBI_DBUFF0;
  5812. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5813. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5814. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5815. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5816. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  5817. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  5818. tmp |= SBI_SSCCTL_PATHALT;
  5819. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5820. udelay(32);
  5821. }
  5822. tmp |= SBI_SSCCTL_DISABLE;
  5823. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5824. }
  5825. mutex_unlock(&dev_priv->dpio_lock);
  5826. }
  5827. static void lpt_init_pch_refclk(struct drm_device *dev)
  5828. {
  5829. struct intel_encoder *encoder;
  5830. bool has_vga = false;
  5831. for_each_intel_encoder(dev, encoder) {
  5832. switch (encoder->type) {
  5833. case INTEL_OUTPUT_ANALOG:
  5834. has_vga = true;
  5835. break;
  5836. default:
  5837. break;
  5838. }
  5839. }
  5840. if (has_vga)
  5841. lpt_enable_clkout_dp(dev, true, true);
  5842. else
  5843. lpt_disable_clkout_dp(dev);
  5844. }
  5845. /*
  5846. * Initialize reference clocks when the driver loads
  5847. */
  5848. void intel_init_pch_refclk(struct drm_device *dev)
  5849. {
  5850. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  5851. ironlake_init_pch_refclk(dev);
  5852. else if (HAS_PCH_LPT(dev))
  5853. lpt_init_pch_refclk(dev);
  5854. }
  5855. static int ironlake_get_refclk(struct drm_crtc *crtc)
  5856. {
  5857. struct drm_device *dev = crtc->dev;
  5858. struct drm_i915_private *dev_priv = dev->dev_private;
  5859. struct intel_encoder *encoder;
  5860. int num_connectors = 0;
  5861. bool is_lvds = false;
  5862. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5863. switch (encoder->type) {
  5864. case INTEL_OUTPUT_LVDS:
  5865. is_lvds = true;
  5866. break;
  5867. default:
  5868. break;
  5869. }
  5870. num_connectors++;
  5871. }
  5872. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5873. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  5874. dev_priv->vbt.lvds_ssc_freq);
  5875. return dev_priv->vbt.lvds_ssc_freq;
  5876. }
  5877. return 120000;
  5878. }
  5879. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  5880. {
  5881. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  5882. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5883. int pipe = intel_crtc->pipe;
  5884. uint32_t val;
  5885. val = 0;
  5886. switch (intel_crtc->config.pipe_bpp) {
  5887. case 18:
  5888. val |= PIPECONF_6BPC;
  5889. break;
  5890. case 24:
  5891. val |= PIPECONF_8BPC;
  5892. break;
  5893. case 30:
  5894. val |= PIPECONF_10BPC;
  5895. break;
  5896. case 36:
  5897. val |= PIPECONF_12BPC;
  5898. break;
  5899. default:
  5900. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5901. BUG();
  5902. }
  5903. if (intel_crtc->config.dither)
  5904. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  5905. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  5906. val |= PIPECONF_INTERLACED_ILK;
  5907. else
  5908. val |= PIPECONF_PROGRESSIVE;
  5909. if (intel_crtc->config.limited_color_range)
  5910. val |= PIPECONF_COLOR_RANGE_SELECT;
  5911. I915_WRITE(PIPECONF(pipe), val);
  5912. POSTING_READ(PIPECONF(pipe));
  5913. }
  5914. /*
  5915. * Set up the pipe CSC unit.
  5916. *
  5917. * Currently only full range RGB to limited range RGB conversion
  5918. * is supported, but eventually this should handle various
  5919. * RGB<->YCbCr scenarios as well.
  5920. */
  5921. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  5922. {
  5923. struct drm_device *dev = crtc->dev;
  5924. struct drm_i915_private *dev_priv = dev->dev_private;
  5925. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5926. int pipe = intel_crtc->pipe;
  5927. uint16_t coeff = 0x7800; /* 1.0 */
  5928. /*
  5929. * TODO: Check what kind of values actually come out of the pipe
  5930. * with these coeff/postoff values and adjust to get the best
  5931. * accuracy. Perhaps we even need to take the bpc value into
  5932. * consideration.
  5933. */
  5934. if (intel_crtc->config.limited_color_range)
  5935. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  5936. /*
  5937. * GY/GU and RY/RU should be the other way around according
  5938. * to BSpec, but reality doesn't agree. Just set them up in
  5939. * a way that results in the correct picture.
  5940. */
  5941. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  5942. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  5943. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  5944. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  5945. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  5946. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  5947. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  5948. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  5949. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  5950. if (INTEL_INFO(dev)->gen > 6) {
  5951. uint16_t postoff = 0;
  5952. if (intel_crtc->config.limited_color_range)
  5953. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  5954. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  5955. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  5956. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  5957. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  5958. } else {
  5959. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  5960. if (intel_crtc->config.limited_color_range)
  5961. mode |= CSC_BLACK_SCREEN_OFFSET;
  5962. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  5963. }
  5964. }
  5965. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  5966. {
  5967. struct drm_device *dev = crtc->dev;
  5968. struct drm_i915_private *dev_priv = dev->dev_private;
  5969. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5970. enum pipe pipe = intel_crtc->pipe;
  5971. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  5972. uint32_t val;
  5973. val = 0;
  5974. if (IS_HASWELL(dev) && intel_crtc->config.dither)
  5975. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  5976. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  5977. val |= PIPECONF_INTERLACED_ILK;
  5978. else
  5979. val |= PIPECONF_PROGRESSIVE;
  5980. I915_WRITE(PIPECONF(cpu_transcoder), val);
  5981. POSTING_READ(PIPECONF(cpu_transcoder));
  5982. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  5983. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  5984. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  5985. val = 0;
  5986. switch (intel_crtc->config.pipe_bpp) {
  5987. case 18:
  5988. val |= PIPEMISC_DITHER_6_BPC;
  5989. break;
  5990. case 24:
  5991. val |= PIPEMISC_DITHER_8_BPC;
  5992. break;
  5993. case 30:
  5994. val |= PIPEMISC_DITHER_10_BPC;
  5995. break;
  5996. case 36:
  5997. val |= PIPEMISC_DITHER_12_BPC;
  5998. break;
  5999. default:
  6000. /* Case prevented by pipe_config_set_bpp. */
  6001. BUG();
  6002. }
  6003. if (intel_crtc->config.dither)
  6004. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  6005. I915_WRITE(PIPEMISC(pipe), val);
  6006. }
  6007. }
  6008. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  6009. intel_clock_t *clock,
  6010. bool *has_reduced_clock,
  6011. intel_clock_t *reduced_clock)
  6012. {
  6013. struct drm_device *dev = crtc->dev;
  6014. struct drm_i915_private *dev_priv = dev->dev_private;
  6015. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6016. int refclk;
  6017. const intel_limit_t *limit;
  6018. bool ret, is_lvds = false;
  6019. is_lvds = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_LVDS);
  6020. refclk = ironlake_get_refclk(crtc);
  6021. /*
  6022. * Returns a set of divisors for the desired target clock with the given
  6023. * refclk, or FALSE. The returned values represent the clock equation:
  6024. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  6025. */
  6026. limit = intel_limit(intel_crtc, refclk);
  6027. ret = dev_priv->display.find_dpll(limit, intel_crtc,
  6028. intel_crtc->config.port_clock,
  6029. refclk, NULL, clock);
  6030. if (!ret)
  6031. return false;
  6032. if (is_lvds && dev_priv->lvds_downclock_avail) {
  6033. /*
  6034. * Ensure we match the reduced clock's P to the target clock.
  6035. * If the clocks don't match, we can't switch the display clock
  6036. * by using the FP0/FP1. In such case we will disable the LVDS
  6037. * downclock feature.
  6038. */
  6039. *has_reduced_clock =
  6040. dev_priv->display.find_dpll(limit, intel_crtc,
  6041. dev_priv->lvds_downclock,
  6042. refclk, clock,
  6043. reduced_clock);
  6044. }
  6045. return true;
  6046. }
  6047. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  6048. {
  6049. /*
  6050. * Account for spread spectrum to avoid
  6051. * oversubscribing the link. Max center spread
  6052. * is 2.5%; use 5% for safety's sake.
  6053. */
  6054. u32 bps = target_clock * bpp * 21 / 20;
  6055. return DIV_ROUND_UP(bps, link_bw * 8);
  6056. }
  6057. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  6058. {
  6059. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  6060. }
  6061. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  6062. u32 *fp,
  6063. intel_clock_t *reduced_clock, u32 *fp2)
  6064. {
  6065. struct drm_crtc *crtc = &intel_crtc->base;
  6066. struct drm_device *dev = crtc->dev;
  6067. struct drm_i915_private *dev_priv = dev->dev_private;
  6068. struct intel_encoder *intel_encoder;
  6069. uint32_t dpll;
  6070. int factor, num_connectors = 0;
  6071. bool is_lvds = false, is_sdvo = false;
  6072. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  6073. switch (intel_encoder->type) {
  6074. case INTEL_OUTPUT_LVDS:
  6075. is_lvds = true;
  6076. break;
  6077. case INTEL_OUTPUT_SDVO:
  6078. case INTEL_OUTPUT_HDMI:
  6079. is_sdvo = true;
  6080. break;
  6081. default:
  6082. break;
  6083. }
  6084. num_connectors++;
  6085. }
  6086. /* Enable autotuning of the PLL clock (if permissible) */
  6087. factor = 21;
  6088. if (is_lvds) {
  6089. if ((intel_panel_use_ssc(dev_priv) &&
  6090. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6091. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  6092. factor = 25;
  6093. } else if (intel_crtc->config.sdvo_tv_clock)
  6094. factor = 20;
  6095. if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
  6096. *fp |= FP_CB_TUNE;
  6097. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  6098. *fp2 |= FP_CB_TUNE;
  6099. dpll = 0;
  6100. if (is_lvds)
  6101. dpll |= DPLLB_MODE_LVDS;
  6102. else
  6103. dpll |= DPLLB_MODE_DAC_SERIAL;
  6104. dpll |= (intel_crtc->config.pixel_multiplier - 1)
  6105. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6106. if (is_sdvo)
  6107. dpll |= DPLL_SDVO_HIGH_SPEED;
  6108. if (intel_crtc->config.has_dp_encoder)
  6109. dpll |= DPLL_SDVO_HIGH_SPEED;
  6110. /* compute bitmask from p1 value */
  6111. dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6112. /* also FPA1 */
  6113. dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6114. switch (intel_crtc->config.dpll.p2) {
  6115. case 5:
  6116. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6117. break;
  6118. case 7:
  6119. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6120. break;
  6121. case 10:
  6122. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6123. break;
  6124. case 14:
  6125. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6126. break;
  6127. }
  6128. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6129. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6130. else
  6131. dpll |= PLL_REF_INPUT_DREFCLK;
  6132. return dpll | DPLL_VCO_ENABLE;
  6133. }
  6134. static int ironlake_crtc_mode_set(struct intel_crtc *crtc,
  6135. int x, int y,
  6136. struct drm_framebuffer *fb)
  6137. {
  6138. struct drm_device *dev = crtc->base.dev;
  6139. intel_clock_t clock, reduced_clock;
  6140. u32 dpll = 0, fp = 0, fp2 = 0;
  6141. bool ok, has_reduced_clock = false;
  6142. bool is_lvds = false;
  6143. struct intel_shared_dpll *pll;
  6144. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  6145. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  6146. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  6147. ok = ironlake_compute_clocks(&crtc->base, &clock,
  6148. &has_reduced_clock, &reduced_clock);
  6149. if (!ok && !crtc->config.clock_set) {
  6150. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6151. return -EINVAL;
  6152. }
  6153. /* Compat-code for transition, will disappear. */
  6154. if (!crtc->config.clock_set) {
  6155. crtc->config.dpll.n = clock.n;
  6156. crtc->config.dpll.m1 = clock.m1;
  6157. crtc->config.dpll.m2 = clock.m2;
  6158. crtc->config.dpll.p1 = clock.p1;
  6159. crtc->config.dpll.p2 = clock.p2;
  6160. }
  6161. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  6162. if (crtc->config.has_pch_encoder) {
  6163. fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
  6164. if (has_reduced_clock)
  6165. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  6166. dpll = ironlake_compute_dpll(crtc,
  6167. &fp, &reduced_clock,
  6168. has_reduced_clock ? &fp2 : NULL);
  6169. crtc->config.dpll_hw_state.dpll = dpll;
  6170. crtc->config.dpll_hw_state.fp0 = fp;
  6171. if (has_reduced_clock)
  6172. crtc->config.dpll_hw_state.fp1 = fp2;
  6173. else
  6174. crtc->config.dpll_hw_state.fp1 = fp;
  6175. pll = intel_get_shared_dpll(crtc);
  6176. if (pll == NULL) {
  6177. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  6178. pipe_name(crtc->pipe));
  6179. return -EINVAL;
  6180. }
  6181. } else
  6182. intel_put_shared_dpll(crtc);
  6183. if (is_lvds && has_reduced_clock && i915.powersave)
  6184. crtc->lowfreq_avail = true;
  6185. else
  6186. crtc->lowfreq_avail = false;
  6187. return 0;
  6188. }
  6189. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  6190. struct intel_link_m_n *m_n)
  6191. {
  6192. struct drm_device *dev = crtc->base.dev;
  6193. struct drm_i915_private *dev_priv = dev->dev_private;
  6194. enum pipe pipe = crtc->pipe;
  6195. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  6196. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  6197. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  6198. & ~TU_SIZE_MASK;
  6199. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  6200. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  6201. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6202. }
  6203. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  6204. enum transcoder transcoder,
  6205. struct intel_link_m_n *m_n,
  6206. struct intel_link_m_n *m2_n2)
  6207. {
  6208. struct drm_device *dev = crtc->base.dev;
  6209. struct drm_i915_private *dev_priv = dev->dev_private;
  6210. enum pipe pipe = crtc->pipe;
  6211. if (INTEL_INFO(dev)->gen >= 5) {
  6212. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  6213. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  6214. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  6215. & ~TU_SIZE_MASK;
  6216. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  6217. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  6218. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6219. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  6220. * gen < 8) and if DRRS is supported (to make sure the
  6221. * registers are not unnecessarily read).
  6222. */
  6223. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  6224. crtc->config.has_drrs) {
  6225. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  6226. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  6227. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  6228. & ~TU_SIZE_MASK;
  6229. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  6230. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  6231. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6232. }
  6233. } else {
  6234. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  6235. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  6236. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  6237. & ~TU_SIZE_MASK;
  6238. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  6239. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  6240. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6241. }
  6242. }
  6243. void intel_dp_get_m_n(struct intel_crtc *crtc,
  6244. struct intel_crtc_config *pipe_config)
  6245. {
  6246. if (crtc->config.has_pch_encoder)
  6247. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  6248. else
  6249. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6250. &pipe_config->dp_m_n,
  6251. &pipe_config->dp_m2_n2);
  6252. }
  6253. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  6254. struct intel_crtc_config *pipe_config)
  6255. {
  6256. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6257. &pipe_config->fdi_m_n, NULL);
  6258. }
  6259. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  6260. struct intel_crtc_config *pipe_config)
  6261. {
  6262. struct drm_device *dev = crtc->base.dev;
  6263. struct drm_i915_private *dev_priv = dev->dev_private;
  6264. uint32_t tmp;
  6265. tmp = I915_READ(PF_CTL(crtc->pipe));
  6266. if (tmp & PF_ENABLE) {
  6267. pipe_config->pch_pfit.enabled = true;
  6268. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  6269. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  6270. /* We currently do not free assignements of panel fitters on
  6271. * ivb/hsw (since we don't use the higher upscaling modes which
  6272. * differentiates them) so just WARN about this case for now. */
  6273. if (IS_GEN7(dev)) {
  6274. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  6275. PF_PIPE_SEL_IVB(crtc->pipe));
  6276. }
  6277. }
  6278. }
  6279. static void ironlake_get_plane_config(struct intel_crtc *crtc,
  6280. struct intel_plane_config *plane_config)
  6281. {
  6282. struct drm_device *dev = crtc->base.dev;
  6283. struct drm_i915_private *dev_priv = dev->dev_private;
  6284. u32 val, base, offset;
  6285. int pipe = crtc->pipe, plane = crtc->plane;
  6286. int fourcc, pixel_format;
  6287. int aligned_height;
  6288. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  6289. if (!crtc->base.primary->fb) {
  6290. DRM_DEBUG_KMS("failed to alloc fb\n");
  6291. return;
  6292. }
  6293. val = I915_READ(DSPCNTR(plane));
  6294. if (INTEL_INFO(dev)->gen >= 4)
  6295. if (val & DISPPLANE_TILED)
  6296. plane_config->tiled = true;
  6297. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6298. fourcc = intel_format_to_fourcc(pixel_format);
  6299. crtc->base.primary->fb->pixel_format = fourcc;
  6300. crtc->base.primary->fb->bits_per_pixel =
  6301. drm_format_plane_cpp(fourcc, 0) * 8;
  6302. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6303. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  6304. offset = I915_READ(DSPOFFSET(plane));
  6305. } else {
  6306. if (plane_config->tiled)
  6307. offset = I915_READ(DSPTILEOFF(plane));
  6308. else
  6309. offset = I915_READ(DSPLINOFF(plane));
  6310. }
  6311. plane_config->base = base;
  6312. val = I915_READ(PIPESRC(pipe));
  6313. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  6314. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  6315. val = I915_READ(DSPSTRIDE(pipe));
  6316. crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
  6317. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  6318. plane_config->tiled);
  6319. plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
  6320. aligned_height);
  6321. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6322. pipe, plane, crtc->base.primary->fb->width,
  6323. crtc->base.primary->fb->height,
  6324. crtc->base.primary->fb->bits_per_pixel, base,
  6325. crtc->base.primary->fb->pitches[0],
  6326. plane_config->size);
  6327. }
  6328. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  6329. struct intel_crtc_config *pipe_config)
  6330. {
  6331. struct drm_device *dev = crtc->base.dev;
  6332. struct drm_i915_private *dev_priv = dev->dev_private;
  6333. uint32_t tmp;
  6334. if (!intel_display_power_is_enabled(dev_priv,
  6335. POWER_DOMAIN_PIPE(crtc->pipe)))
  6336. return false;
  6337. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6338. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6339. tmp = I915_READ(PIPECONF(crtc->pipe));
  6340. if (!(tmp & PIPECONF_ENABLE))
  6341. return false;
  6342. switch (tmp & PIPECONF_BPC_MASK) {
  6343. case PIPECONF_6BPC:
  6344. pipe_config->pipe_bpp = 18;
  6345. break;
  6346. case PIPECONF_8BPC:
  6347. pipe_config->pipe_bpp = 24;
  6348. break;
  6349. case PIPECONF_10BPC:
  6350. pipe_config->pipe_bpp = 30;
  6351. break;
  6352. case PIPECONF_12BPC:
  6353. pipe_config->pipe_bpp = 36;
  6354. break;
  6355. default:
  6356. break;
  6357. }
  6358. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  6359. pipe_config->limited_color_range = true;
  6360. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  6361. struct intel_shared_dpll *pll;
  6362. pipe_config->has_pch_encoder = true;
  6363. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  6364. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6365. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6366. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6367. if (HAS_PCH_IBX(dev_priv->dev)) {
  6368. pipe_config->shared_dpll =
  6369. (enum intel_dpll_id) crtc->pipe;
  6370. } else {
  6371. tmp = I915_READ(PCH_DPLL_SEL);
  6372. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  6373. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  6374. else
  6375. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  6376. }
  6377. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6378. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6379. &pipe_config->dpll_hw_state));
  6380. tmp = pipe_config->dpll_hw_state.dpll;
  6381. pipe_config->pixel_multiplier =
  6382. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  6383. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  6384. ironlake_pch_clock_get(crtc, pipe_config);
  6385. } else {
  6386. pipe_config->pixel_multiplier = 1;
  6387. }
  6388. intel_get_pipe_timings(crtc, pipe_config);
  6389. ironlake_get_pfit_config(crtc, pipe_config);
  6390. return true;
  6391. }
  6392. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  6393. {
  6394. struct drm_device *dev = dev_priv->dev;
  6395. struct intel_crtc *crtc;
  6396. for_each_intel_crtc(dev, crtc)
  6397. WARN(crtc->active, "CRTC for pipe %c enabled\n",
  6398. pipe_name(crtc->pipe));
  6399. WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  6400. WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  6401. WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  6402. WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  6403. WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  6404. WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  6405. "CPU PWM1 enabled\n");
  6406. if (IS_HASWELL(dev))
  6407. WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  6408. "CPU PWM2 enabled\n");
  6409. WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  6410. "PCH PWM1 enabled\n");
  6411. WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  6412. "Utility pin enabled\n");
  6413. WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  6414. /*
  6415. * In theory we can still leave IRQs enabled, as long as only the HPD
  6416. * interrupts remain enabled. We used to check for that, but since it's
  6417. * gen-specific and since we only disable LCPLL after we fully disable
  6418. * the interrupts, the check below should be enough.
  6419. */
  6420. WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  6421. }
  6422. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  6423. {
  6424. struct drm_device *dev = dev_priv->dev;
  6425. if (IS_HASWELL(dev))
  6426. return I915_READ(D_COMP_HSW);
  6427. else
  6428. return I915_READ(D_COMP_BDW);
  6429. }
  6430. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  6431. {
  6432. struct drm_device *dev = dev_priv->dev;
  6433. if (IS_HASWELL(dev)) {
  6434. mutex_lock(&dev_priv->rps.hw_lock);
  6435. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  6436. val))
  6437. DRM_ERROR("Failed to write to D_COMP\n");
  6438. mutex_unlock(&dev_priv->rps.hw_lock);
  6439. } else {
  6440. I915_WRITE(D_COMP_BDW, val);
  6441. POSTING_READ(D_COMP_BDW);
  6442. }
  6443. }
  6444. /*
  6445. * This function implements pieces of two sequences from BSpec:
  6446. * - Sequence for display software to disable LCPLL
  6447. * - Sequence for display software to allow package C8+
  6448. * The steps implemented here are just the steps that actually touch the LCPLL
  6449. * register. Callers should take care of disabling all the display engine
  6450. * functions, doing the mode unset, fixing interrupts, etc.
  6451. */
  6452. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  6453. bool switch_to_fclk, bool allow_power_down)
  6454. {
  6455. uint32_t val;
  6456. assert_can_disable_lcpll(dev_priv);
  6457. val = I915_READ(LCPLL_CTL);
  6458. if (switch_to_fclk) {
  6459. val |= LCPLL_CD_SOURCE_FCLK;
  6460. I915_WRITE(LCPLL_CTL, val);
  6461. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  6462. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  6463. DRM_ERROR("Switching to FCLK failed\n");
  6464. val = I915_READ(LCPLL_CTL);
  6465. }
  6466. val |= LCPLL_PLL_DISABLE;
  6467. I915_WRITE(LCPLL_CTL, val);
  6468. POSTING_READ(LCPLL_CTL);
  6469. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  6470. DRM_ERROR("LCPLL still locked\n");
  6471. val = hsw_read_dcomp(dev_priv);
  6472. val |= D_COMP_COMP_DISABLE;
  6473. hsw_write_dcomp(dev_priv, val);
  6474. ndelay(100);
  6475. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  6476. 1))
  6477. DRM_ERROR("D_COMP RCOMP still in progress\n");
  6478. if (allow_power_down) {
  6479. val = I915_READ(LCPLL_CTL);
  6480. val |= LCPLL_POWER_DOWN_ALLOW;
  6481. I915_WRITE(LCPLL_CTL, val);
  6482. POSTING_READ(LCPLL_CTL);
  6483. }
  6484. }
  6485. /*
  6486. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  6487. * source.
  6488. */
  6489. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  6490. {
  6491. uint32_t val;
  6492. val = I915_READ(LCPLL_CTL);
  6493. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  6494. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  6495. return;
  6496. /*
  6497. * Make sure we're not on PC8 state before disabling PC8, otherwise
  6498. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  6499. *
  6500. * The other problem is that hsw_restore_lcpll() is called as part of
  6501. * the runtime PM resume sequence, so we can't just call
  6502. * gen6_gt_force_wake_get() because that function calls
  6503. * intel_runtime_pm_get(), and we can't change the runtime PM refcount
  6504. * while we are on the resume sequence. So to solve this problem we have
  6505. * to call special forcewake code that doesn't touch runtime PM and
  6506. * doesn't enable the forcewake delayed work.
  6507. */
  6508. spin_lock_irq(&dev_priv->uncore.lock);
  6509. if (dev_priv->uncore.forcewake_count++ == 0)
  6510. dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
  6511. spin_unlock_irq(&dev_priv->uncore.lock);
  6512. if (val & LCPLL_POWER_DOWN_ALLOW) {
  6513. val &= ~LCPLL_POWER_DOWN_ALLOW;
  6514. I915_WRITE(LCPLL_CTL, val);
  6515. POSTING_READ(LCPLL_CTL);
  6516. }
  6517. val = hsw_read_dcomp(dev_priv);
  6518. val |= D_COMP_COMP_FORCE;
  6519. val &= ~D_COMP_COMP_DISABLE;
  6520. hsw_write_dcomp(dev_priv, val);
  6521. val = I915_READ(LCPLL_CTL);
  6522. val &= ~LCPLL_PLL_DISABLE;
  6523. I915_WRITE(LCPLL_CTL, val);
  6524. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  6525. DRM_ERROR("LCPLL not locked yet\n");
  6526. if (val & LCPLL_CD_SOURCE_FCLK) {
  6527. val = I915_READ(LCPLL_CTL);
  6528. val &= ~LCPLL_CD_SOURCE_FCLK;
  6529. I915_WRITE(LCPLL_CTL, val);
  6530. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  6531. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  6532. DRM_ERROR("Switching back to LCPLL failed\n");
  6533. }
  6534. /* See the big comment above. */
  6535. spin_lock_irq(&dev_priv->uncore.lock);
  6536. if (--dev_priv->uncore.forcewake_count == 0)
  6537. dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
  6538. spin_unlock_irq(&dev_priv->uncore.lock);
  6539. }
  6540. /*
  6541. * Package states C8 and deeper are really deep PC states that can only be
  6542. * reached when all the devices on the system allow it, so even if the graphics
  6543. * device allows PC8+, it doesn't mean the system will actually get to these
  6544. * states. Our driver only allows PC8+ when going into runtime PM.
  6545. *
  6546. * The requirements for PC8+ are that all the outputs are disabled, the power
  6547. * well is disabled and most interrupts are disabled, and these are also
  6548. * requirements for runtime PM. When these conditions are met, we manually do
  6549. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  6550. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  6551. * hang the machine.
  6552. *
  6553. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  6554. * the state of some registers, so when we come back from PC8+ we need to
  6555. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  6556. * need to take care of the registers kept by RC6. Notice that this happens even
  6557. * if we don't put the device in PCI D3 state (which is what currently happens
  6558. * because of the runtime PM support).
  6559. *
  6560. * For more, read "Display Sequences for Package C8" on the hardware
  6561. * documentation.
  6562. */
  6563. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  6564. {
  6565. struct drm_device *dev = dev_priv->dev;
  6566. uint32_t val;
  6567. DRM_DEBUG_KMS("Enabling package C8+\n");
  6568. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6569. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6570. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  6571. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6572. }
  6573. lpt_disable_clkout_dp(dev);
  6574. hsw_disable_lcpll(dev_priv, true, true);
  6575. }
  6576. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  6577. {
  6578. struct drm_device *dev = dev_priv->dev;
  6579. uint32_t val;
  6580. DRM_DEBUG_KMS("Disabling package C8+\n");
  6581. hsw_restore_lcpll(dev_priv);
  6582. lpt_init_pch_refclk(dev);
  6583. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6584. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6585. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  6586. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6587. }
  6588. intel_prepare_ddi(dev);
  6589. }
  6590. static void snb_modeset_global_resources(struct drm_device *dev)
  6591. {
  6592. modeset_update_crtc_power_domains(dev);
  6593. }
  6594. static void haswell_modeset_global_resources(struct drm_device *dev)
  6595. {
  6596. modeset_update_crtc_power_domains(dev);
  6597. }
  6598. static int haswell_crtc_mode_set(struct intel_crtc *crtc,
  6599. int x, int y,
  6600. struct drm_framebuffer *fb)
  6601. {
  6602. if (!intel_ddi_pll_select(crtc))
  6603. return -EINVAL;
  6604. crtc->lowfreq_avail = false;
  6605. return 0;
  6606. }
  6607. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  6608. enum port port,
  6609. struct intel_crtc_config *pipe_config)
  6610. {
  6611. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  6612. switch (pipe_config->ddi_pll_sel) {
  6613. case PORT_CLK_SEL_WRPLL1:
  6614. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  6615. break;
  6616. case PORT_CLK_SEL_WRPLL2:
  6617. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  6618. break;
  6619. }
  6620. }
  6621. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  6622. struct intel_crtc_config *pipe_config)
  6623. {
  6624. struct drm_device *dev = crtc->base.dev;
  6625. struct drm_i915_private *dev_priv = dev->dev_private;
  6626. struct intel_shared_dpll *pll;
  6627. enum port port;
  6628. uint32_t tmp;
  6629. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  6630. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  6631. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  6632. if (pipe_config->shared_dpll >= 0) {
  6633. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6634. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6635. &pipe_config->dpll_hw_state));
  6636. }
  6637. /*
  6638. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  6639. * DDI E. So just check whether this pipe is wired to DDI E and whether
  6640. * the PCH transcoder is on.
  6641. */
  6642. if (INTEL_INFO(dev)->gen < 9 &&
  6643. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  6644. pipe_config->has_pch_encoder = true;
  6645. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  6646. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6647. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6648. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6649. }
  6650. }
  6651. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  6652. struct intel_crtc_config *pipe_config)
  6653. {
  6654. struct drm_device *dev = crtc->base.dev;
  6655. struct drm_i915_private *dev_priv = dev->dev_private;
  6656. enum intel_display_power_domain pfit_domain;
  6657. uint32_t tmp;
  6658. if (!intel_display_power_is_enabled(dev_priv,
  6659. POWER_DOMAIN_PIPE(crtc->pipe)))
  6660. return false;
  6661. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6662. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6663. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  6664. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  6665. enum pipe trans_edp_pipe;
  6666. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  6667. default:
  6668. WARN(1, "unknown pipe linked to edp transcoder\n");
  6669. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  6670. case TRANS_DDI_EDP_INPUT_A_ON:
  6671. trans_edp_pipe = PIPE_A;
  6672. break;
  6673. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  6674. trans_edp_pipe = PIPE_B;
  6675. break;
  6676. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  6677. trans_edp_pipe = PIPE_C;
  6678. break;
  6679. }
  6680. if (trans_edp_pipe == crtc->pipe)
  6681. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  6682. }
  6683. if (!intel_display_power_is_enabled(dev_priv,
  6684. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  6685. return false;
  6686. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  6687. if (!(tmp & PIPECONF_ENABLE))
  6688. return false;
  6689. haswell_get_ddi_port_state(crtc, pipe_config);
  6690. intel_get_pipe_timings(crtc, pipe_config);
  6691. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  6692. if (intel_display_power_is_enabled(dev_priv, pfit_domain))
  6693. ironlake_get_pfit_config(crtc, pipe_config);
  6694. if (IS_HASWELL(dev))
  6695. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  6696. (I915_READ(IPS_CTL) & IPS_ENABLE);
  6697. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  6698. pipe_config->pixel_multiplier =
  6699. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  6700. } else {
  6701. pipe_config->pixel_multiplier = 1;
  6702. }
  6703. return true;
  6704. }
  6705. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  6706. {
  6707. struct drm_device *dev = crtc->dev;
  6708. struct drm_i915_private *dev_priv = dev->dev_private;
  6709. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6710. uint32_t cntl = 0, size = 0;
  6711. if (base) {
  6712. unsigned int width = intel_crtc->cursor_width;
  6713. unsigned int height = intel_crtc->cursor_height;
  6714. unsigned int stride = roundup_pow_of_two(width) * 4;
  6715. switch (stride) {
  6716. default:
  6717. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  6718. width, stride);
  6719. stride = 256;
  6720. /* fallthrough */
  6721. case 256:
  6722. case 512:
  6723. case 1024:
  6724. case 2048:
  6725. break;
  6726. }
  6727. cntl |= CURSOR_ENABLE |
  6728. CURSOR_GAMMA_ENABLE |
  6729. CURSOR_FORMAT_ARGB |
  6730. CURSOR_STRIDE(stride);
  6731. size = (height << 12) | width;
  6732. }
  6733. if (intel_crtc->cursor_cntl != 0 &&
  6734. (intel_crtc->cursor_base != base ||
  6735. intel_crtc->cursor_size != size ||
  6736. intel_crtc->cursor_cntl != cntl)) {
  6737. /* On these chipsets we can only modify the base/size/stride
  6738. * whilst the cursor is disabled.
  6739. */
  6740. I915_WRITE(_CURACNTR, 0);
  6741. POSTING_READ(_CURACNTR);
  6742. intel_crtc->cursor_cntl = 0;
  6743. }
  6744. if (intel_crtc->cursor_base != base) {
  6745. I915_WRITE(_CURABASE, base);
  6746. intel_crtc->cursor_base = base;
  6747. }
  6748. if (intel_crtc->cursor_size != size) {
  6749. I915_WRITE(CURSIZE, size);
  6750. intel_crtc->cursor_size = size;
  6751. }
  6752. if (intel_crtc->cursor_cntl != cntl) {
  6753. I915_WRITE(_CURACNTR, cntl);
  6754. POSTING_READ(_CURACNTR);
  6755. intel_crtc->cursor_cntl = cntl;
  6756. }
  6757. }
  6758. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  6759. {
  6760. struct drm_device *dev = crtc->dev;
  6761. struct drm_i915_private *dev_priv = dev->dev_private;
  6762. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6763. int pipe = intel_crtc->pipe;
  6764. uint32_t cntl;
  6765. cntl = 0;
  6766. if (base) {
  6767. cntl = MCURSOR_GAMMA_ENABLE;
  6768. switch (intel_crtc->cursor_width) {
  6769. case 64:
  6770. cntl |= CURSOR_MODE_64_ARGB_AX;
  6771. break;
  6772. case 128:
  6773. cntl |= CURSOR_MODE_128_ARGB_AX;
  6774. break;
  6775. case 256:
  6776. cntl |= CURSOR_MODE_256_ARGB_AX;
  6777. break;
  6778. default:
  6779. WARN_ON(1);
  6780. return;
  6781. }
  6782. cntl |= pipe << 28; /* Connect to correct pipe */
  6783. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  6784. cntl |= CURSOR_PIPE_CSC_ENABLE;
  6785. }
  6786. if (to_intel_plane(crtc->cursor)->rotation == BIT(DRM_ROTATE_180))
  6787. cntl |= CURSOR_ROTATE_180;
  6788. if (intel_crtc->cursor_cntl != cntl) {
  6789. I915_WRITE(CURCNTR(pipe), cntl);
  6790. POSTING_READ(CURCNTR(pipe));
  6791. intel_crtc->cursor_cntl = cntl;
  6792. }
  6793. /* and commit changes on next vblank */
  6794. I915_WRITE(CURBASE(pipe), base);
  6795. POSTING_READ(CURBASE(pipe));
  6796. intel_crtc->cursor_base = base;
  6797. }
  6798. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  6799. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  6800. bool on)
  6801. {
  6802. struct drm_device *dev = crtc->dev;
  6803. struct drm_i915_private *dev_priv = dev->dev_private;
  6804. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6805. int pipe = intel_crtc->pipe;
  6806. int x = crtc->cursor_x;
  6807. int y = crtc->cursor_y;
  6808. u32 base = 0, pos = 0;
  6809. if (on)
  6810. base = intel_crtc->cursor_addr;
  6811. if (x >= intel_crtc->config.pipe_src_w)
  6812. base = 0;
  6813. if (y >= intel_crtc->config.pipe_src_h)
  6814. base = 0;
  6815. if (x < 0) {
  6816. if (x + intel_crtc->cursor_width <= 0)
  6817. base = 0;
  6818. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  6819. x = -x;
  6820. }
  6821. pos |= x << CURSOR_X_SHIFT;
  6822. if (y < 0) {
  6823. if (y + intel_crtc->cursor_height <= 0)
  6824. base = 0;
  6825. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  6826. y = -y;
  6827. }
  6828. pos |= y << CURSOR_Y_SHIFT;
  6829. if (base == 0 && intel_crtc->cursor_base == 0)
  6830. return;
  6831. I915_WRITE(CURPOS(pipe), pos);
  6832. /* ILK+ do this automagically */
  6833. if (HAS_GMCH_DISPLAY(dev) &&
  6834. to_intel_plane(crtc->cursor)->rotation == BIT(DRM_ROTATE_180)) {
  6835. base += (intel_crtc->cursor_height *
  6836. intel_crtc->cursor_width - 1) * 4;
  6837. }
  6838. if (IS_845G(dev) || IS_I865G(dev))
  6839. i845_update_cursor(crtc, base);
  6840. else
  6841. i9xx_update_cursor(crtc, base);
  6842. }
  6843. static bool cursor_size_ok(struct drm_device *dev,
  6844. uint32_t width, uint32_t height)
  6845. {
  6846. if (width == 0 || height == 0)
  6847. return false;
  6848. /*
  6849. * 845g/865g are special in that they are only limited by
  6850. * the width of their cursors, the height is arbitrary up to
  6851. * the precision of the register. Everything else requires
  6852. * square cursors, limited to a few power-of-two sizes.
  6853. */
  6854. if (IS_845G(dev) || IS_I865G(dev)) {
  6855. if ((width & 63) != 0)
  6856. return false;
  6857. if (width > (IS_845G(dev) ? 64 : 512))
  6858. return false;
  6859. if (height > 1023)
  6860. return false;
  6861. } else {
  6862. switch (width | height) {
  6863. case 256:
  6864. case 128:
  6865. if (IS_GEN2(dev))
  6866. return false;
  6867. case 64:
  6868. break;
  6869. default:
  6870. return false;
  6871. }
  6872. }
  6873. return true;
  6874. }
  6875. static int intel_crtc_cursor_set_obj(struct drm_crtc *crtc,
  6876. struct drm_i915_gem_object *obj,
  6877. uint32_t width, uint32_t height)
  6878. {
  6879. struct drm_device *dev = crtc->dev;
  6880. struct drm_i915_private *dev_priv = dev->dev_private;
  6881. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6882. enum pipe pipe = intel_crtc->pipe;
  6883. unsigned old_width;
  6884. uint32_t addr;
  6885. int ret;
  6886. /* if we want to turn off the cursor ignore width and height */
  6887. if (!obj) {
  6888. DRM_DEBUG_KMS("cursor off\n");
  6889. addr = 0;
  6890. mutex_lock(&dev->struct_mutex);
  6891. goto finish;
  6892. }
  6893. /* we only need to pin inside GTT if cursor is non-phy */
  6894. mutex_lock(&dev->struct_mutex);
  6895. if (!INTEL_INFO(dev)->cursor_needs_physical) {
  6896. unsigned alignment;
  6897. /*
  6898. * Global gtt pte registers are special registers which actually
  6899. * forward writes to a chunk of system memory. Which means that
  6900. * there is no risk that the register values disappear as soon
  6901. * as we call intel_runtime_pm_put(), so it is correct to wrap
  6902. * only the pin/unpin/fence and not more.
  6903. */
  6904. intel_runtime_pm_get(dev_priv);
  6905. /* Note that the w/a also requires 2 PTE of padding following
  6906. * the bo. We currently fill all unused PTE with the shadow
  6907. * page and so we should always have valid PTE following the
  6908. * cursor preventing the VT-d warning.
  6909. */
  6910. alignment = 0;
  6911. if (need_vtd_wa(dev))
  6912. alignment = 64*1024;
  6913. ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
  6914. if (ret) {
  6915. DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
  6916. intel_runtime_pm_put(dev_priv);
  6917. goto fail_locked;
  6918. }
  6919. ret = i915_gem_object_put_fence(obj);
  6920. if (ret) {
  6921. DRM_DEBUG_KMS("failed to release fence for cursor");
  6922. intel_runtime_pm_put(dev_priv);
  6923. goto fail_unpin;
  6924. }
  6925. addr = i915_gem_obj_ggtt_offset(obj);
  6926. intel_runtime_pm_put(dev_priv);
  6927. } else {
  6928. int align = IS_I830(dev) ? 16 * 1024 : 256;
  6929. ret = i915_gem_object_attach_phys(obj, align);
  6930. if (ret) {
  6931. DRM_DEBUG_KMS("failed to attach phys object\n");
  6932. goto fail_locked;
  6933. }
  6934. addr = obj->phys_handle->busaddr;
  6935. }
  6936. finish:
  6937. if (intel_crtc->cursor_bo) {
  6938. if (!INTEL_INFO(dev)->cursor_needs_physical)
  6939. i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
  6940. }
  6941. i915_gem_track_fb(intel_crtc->cursor_bo, obj,
  6942. INTEL_FRONTBUFFER_CURSOR(pipe));
  6943. mutex_unlock(&dev->struct_mutex);
  6944. old_width = intel_crtc->cursor_width;
  6945. intel_crtc->cursor_addr = addr;
  6946. intel_crtc->cursor_bo = obj;
  6947. intel_crtc->cursor_width = width;
  6948. intel_crtc->cursor_height = height;
  6949. if (intel_crtc->active) {
  6950. if (old_width != width)
  6951. intel_update_watermarks(crtc);
  6952. intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
  6953. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_CURSOR(pipe));
  6954. }
  6955. return 0;
  6956. fail_unpin:
  6957. i915_gem_object_unpin_from_display_plane(obj);
  6958. fail_locked:
  6959. mutex_unlock(&dev->struct_mutex);
  6960. return ret;
  6961. }
  6962. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  6963. u16 *blue, uint32_t start, uint32_t size)
  6964. {
  6965. int end = (start + size > 256) ? 256 : start + size, i;
  6966. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6967. for (i = start; i < end; i++) {
  6968. intel_crtc->lut_r[i] = red[i] >> 8;
  6969. intel_crtc->lut_g[i] = green[i] >> 8;
  6970. intel_crtc->lut_b[i] = blue[i] >> 8;
  6971. }
  6972. intel_crtc_load_lut(crtc);
  6973. }
  6974. /* VESA 640x480x72Hz mode to set on the pipe */
  6975. static struct drm_display_mode load_detect_mode = {
  6976. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  6977. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  6978. };
  6979. struct drm_framebuffer *
  6980. __intel_framebuffer_create(struct drm_device *dev,
  6981. struct drm_mode_fb_cmd2 *mode_cmd,
  6982. struct drm_i915_gem_object *obj)
  6983. {
  6984. struct intel_framebuffer *intel_fb;
  6985. int ret;
  6986. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6987. if (!intel_fb) {
  6988. drm_gem_object_unreference_unlocked(&obj->base);
  6989. return ERR_PTR(-ENOMEM);
  6990. }
  6991. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  6992. if (ret)
  6993. goto err;
  6994. return &intel_fb->base;
  6995. err:
  6996. drm_gem_object_unreference_unlocked(&obj->base);
  6997. kfree(intel_fb);
  6998. return ERR_PTR(ret);
  6999. }
  7000. static struct drm_framebuffer *
  7001. intel_framebuffer_create(struct drm_device *dev,
  7002. struct drm_mode_fb_cmd2 *mode_cmd,
  7003. struct drm_i915_gem_object *obj)
  7004. {
  7005. struct drm_framebuffer *fb;
  7006. int ret;
  7007. ret = i915_mutex_lock_interruptible(dev);
  7008. if (ret)
  7009. return ERR_PTR(ret);
  7010. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  7011. mutex_unlock(&dev->struct_mutex);
  7012. return fb;
  7013. }
  7014. static u32
  7015. intel_framebuffer_pitch_for_width(int width, int bpp)
  7016. {
  7017. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  7018. return ALIGN(pitch, 64);
  7019. }
  7020. static u32
  7021. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  7022. {
  7023. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  7024. return PAGE_ALIGN(pitch * mode->vdisplay);
  7025. }
  7026. static struct drm_framebuffer *
  7027. intel_framebuffer_create_for_mode(struct drm_device *dev,
  7028. struct drm_display_mode *mode,
  7029. int depth, int bpp)
  7030. {
  7031. struct drm_i915_gem_object *obj;
  7032. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  7033. obj = i915_gem_alloc_object(dev,
  7034. intel_framebuffer_size_for_mode(mode, bpp));
  7035. if (obj == NULL)
  7036. return ERR_PTR(-ENOMEM);
  7037. mode_cmd.width = mode->hdisplay;
  7038. mode_cmd.height = mode->vdisplay;
  7039. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  7040. bpp);
  7041. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  7042. return intel_framebuffer_create(dev, &mode_cmd, obj);
  7043. }
  7044. static struct drm_framebuffer *
  7045. mode_fits_in_fbdev(struct drm_device *dev,
  7046. struct drm_display_mode *mode)
  7047. {
  7048. #ifdef CONFIG_DRM_I915_FBDEV
  7049. struct drm_i915_private *dev_priv = dev->dev_private;
  7050. struct drm_i915_gem_object *obj;
  7051. struct drm_framebuffer *fb;
  7052. if (!dev_priv->fbdev)
  7053. return NULL;
  7054. if (!dev_priv->fbdev->fb)
  7055. return NULL;
  7056. obj = dev_priv->fbdev->fb->obj;
  7057. BUG_ON(!obj);
  7058. fb = &dev_priv->fbdev->fb->base;
  7059. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  7060. fb->bits_per_pixel))
  7061. return NULL;
  7062. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  7063. return NULL;
  7064. return fb;
  7065. #else
  7066. return NULL;
  7067. #endif
  7068. }
  7069. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  7070. struct drm_display_mode *mode,
  7071. struct intel_load_detect_pipe *old,
  7072. struct drm_modeset_acquire_ctx *ctx)
  7073. {
  7074. struct intel_crtc *intel_crtc;
  7075. struct intel_encoder *intel_encoder =
  7076. intel_attached_encoder(connector);
  7077. struct drm_crtc *possible_crtc;
  7078. struct drm_encoder *encoder = &intel_encoder->base;
  7079. struct drm_crtc *crtc = NULL;
  7080. struct drm_device *dev = encoder->dev;
  7081. struct drm_framebuffer *fb;
  7082. struct drm_mode_config *config = &dev->mode_config;
  7083. int ret, i = -1;
  7084. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7085. connector->base.id, connector->name,
  7086. encoder->base.id, encoder->name);
  7087. retry:
  7088. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  7089. if (ret)
  7090. goto fail_unlock;
  7091. /*
  7092. * Algorithm gets a little messy:
  7093. *
  7094. * - if the connector already has an assigned crtc, use it (but make
  7095. * sure it's on first)
  7096. *
  7097. * - try to find the first unused crtc that can drive this connector,
  7098. * and use that if we find one
  7099. */
  7100. /* See if we already have a CRTC for this connector */
  7101. if (encoder->crtc) {
  7102. crtc = encoder->crtc;
  7103. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7104. if (ret)
  7105. goto fail_unlock;
  7106. old->dpms_mode = connector->dpms;
  7107. old->load_detect_temp = false;
  7108. /* Make sure the crtc and connector are running */
  7109. if (connector->dpms != DRM_MODE_DPMS_ON)
  7110. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  7111. return true;
  7112. }
  7113. /* Find an unused one (if possible) */
  7114. for_each_crtc(dev, possible_crtc) {
  7115. i++;
  7116. if (!(encoder->possible_crtcs & (1 << i)))
  7117. continue;
  7118. if (possible_crtc->enabled)
  7119. continue;
  7120. /* This can occur when applying the pipe A quirk on resume. */
  7121. if (to_intel_crtc(possible_crtc)->new_enabled)
  7122. continue;
  7123. crtc = possible_crtc;
  7124. break;
  7125. }
  7126. /*
  7127. * If we didn't find an unused CRTC, don't use any.
  7128. */
  7129. if (!crtc) {
  7130. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  7131. goto fail_unlock;
  7132. }
  7133. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7134. if (ret)
  7135. goto fail_unlock;
  7136. intel_encoder->new_crtc = to_intel_crtc(crtc);
  7137. to_intel_connector(connector)->new_encoder = intel_encoder;
  7138. intel_crtc = to_intel_crtc(crtc);
  7139. intel_crtc->new_enabled = true;
  7140. intel_crtc->new_config = &intel_crtc->config;
  7141. old->dpms_mode = connector->dpms;
  7142. old->load_detect_temp = true;
  7143. old->release_fb = NULL;
  7144. if (!mode)
  7145. mode = &load_detect_mode;
  7146. /* We need a framebuffer large enough to accommodate all accesses
  7147. * that the plane may generate whilst we perform load detection.
  7148. * We can not rely on the fbcon either being present (we get called
  7149. * during its initialisation to detect all boot displays, or it may
  7150. * not even exist) or that it is large enough to satisfy the
  7151. * requested mode.
  7152. */
  7153. fb = mode_fits_in_fbdev(dev, mode);
  7154. if (fb == NULL) {
  7155. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  7156. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  7157. old->release_fb = fb;
  7158. } else
  7159. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  7160. if (IS_ERR(fb)) {
  7161. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  7162. goto fail;
  7163. }
  7164. if (intel_set_mode(crtc, mode, 0, 0, fb)) {
  7165. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  7166. if (old->release_fb)
  7167. old->release_fb->funcs->destroy(old->release_fb);
  7168. goto fail;
  7169. }
  7170. /* let the connector get through one full cycle before testing */
  7171. intel_wait_for_vblank(dev, intel_crtc->pipe);
  7172. return true;
  7173. fail:
  7174. intel_crtc->new_enabled = crtc->enabled;
  7175. if (intel_crtc->new_enabled)
  7176. intel_crtc->new_config = &intel_crtc->config;
  7177. else
  7178. intel_crtc->new_config = NULL;
  7179. fail_unlock:
  7180. if (ret == -EDEADLK) {
  7181. drm_modeset_backoff(ctx);
  7182. goto retry;
  7183. }
  7184. return false;
  7185. }
  7186. void intel_release_load_detect_pipe(struct drm_connector *connector,
  7187. struct intel_load_detect_pipe *old)
  7188. {
  7189. struct intel_encoder *intel_encoder =
  7190. intel_attached_encoder(connector);
  7191. struct drm_encoder *encoder = &intel_encoder->base;
  7192. struct drm_crtc *crtc = encoder->crtc;
  7193. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7194. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7195. connector->base.id, connector->name,
  7196. encoder->base.id, encoder->name);
  7197. if (old->load_detect_temp) {
  7198. to_intel_connector(connector)->new_encoder = NULL;
  7199. intel_encoder->new_crtc = NULL;
  7200. intel_crtc->new_enabled = false;
  7201. intel_crtc->new_config = NULL;
  7202. intel_set_mode(crtc, NULL, 0, 0, NULL);
  7203. if (old->release_fb) {
  7204. drm_framebuffer_unregister_private(old->release_fb);
  7205. drm_framebuffer_unreference(old->release_fb);
  7206. }
  7207. return;
  7208. }
  7209. /* Switch crtc and encoder back off if necessary */
  7210. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  7211. connector->funcs->dpms(connector, old->dpms_mode);
  7212. }
  7213. static int i9xx_pll_refclk(struct drm_device *dev,
  7214. const struct intel_crtc_config *pipe_config)
  7215. {
  7216. struct drm_i915_private *dev_priv = dev->dev_private;
  7217. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7218. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  7219. return dev_priv->vbt.lvds_ssc_freq;
  7220. else if (HAS_PCH_SPLIT(dev))
  7221. return 120000;
  7222. else if (!IS_GEN2(dev))
  7223. return 96000;
  7224. else
  7225. return 48000;
  7226. }
  7227. /* Returns the clock of the currently programmed mode of the given pipe. */
  7228. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  7229. struct intel_crtc_config *pipe_config)
  7230. {
  7231. struct drm_device *dev = crtc->base.dev;
  7232. struct drm_i915_private *dev_priv = dev->dev_private;
  7233. int pipe = pipe_config->cpu_transcoder;
  7234. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7235. u32 fp;
  7236. intel_clock_t clock;
  7237. int refclk = i9xx_pll_refclk(dev, pipe_config);
  7238. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  7239. fp = pipe_config->dpll_hw_state.fp0;
  7240. else
  7241. fp = pipe_config->dpll_hw_state.fp1;
  7242. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  7243. if (IS_PINEVIEW(dev)) {
  7244. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  7245. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7246. } else {
  7247. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  7248. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7249. }
  7250. if (!IS_GEN2(dev)) {
  7251. if (IS_PINEVIEW(dev))
  7252. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  7253. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  7254. else
  7255. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  7256. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7257. switch (dpll & DPLL_MODE_MASK) {
  7258. case DPLLB_MODE_DAC_SERIAL:
  7259. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  7260. 5 : 10;
  7261. break;
  7262. case DPLLB_MODE_LVDS:
  7263. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  7264. 7 : 14;
  7265. break;
  7266. default:
  7267. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  7268. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  7269. return;
  7270. }
  7271. if (IS_PINEVIEW(dev))
  7272. pineview_clock(refclk, &clock);
  7273. else
  7274. i9xx_clock(refclk, &clock);
  7275. } else {
  7276. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  7277. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  7278. if (is_lvds) {
  7279. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  7280. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7281. if (lvds & LVDS_CLKB_POWER_UP)
  7282. clock.p2 = 7;
  7283. else
  7284. clock.p2 = 14;
  7285. } else {
  7286. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  7287. clock.p1 = 2;
  7288. else {
  7289. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  7290. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  7291. }
  7292. if (dpll & PLL_P2_DIVIDE_BY_4)
  7293. clock.p2 = 4;
  7294. else
  7295. clock.p2 = 2;
  7296. }
  7297. i9xx_clock(refclk, &clock);
  7298. }
  7299. /*
  7300. * This value includes pixel_multiplier. We will use
  7301. * port_clock to compute adjusted_mode.crtc_clock in the
  7302. * encoder's get_config() function.
  7303. */
  7304. pipe_config->port_clock = clock.dot;
  7305. }
  7306. int intel_dotclock_calculate(int link_freq,
  7307. const struct intel_link_m_n *m_n)
  7308. {
  7309. /*
  7310. * The calculation for the data clock is:
  7311. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  7312. * But we want to avoid losing precison if possible, so:
  7313. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  7314. *
  7315. * and the link clock is simpler:
  7316. * link_clock = (m * link_clock) / n
  7317. */
  7318. if (!m_n->link_n)
  7319. return 0;
  7320. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  7321. }
  7322. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  7323. struct intel_crtc_config *pipe_config)
  7324. {
  7325. struct drm_device *dev = crtc->base.dev;
  7326. /* read out port_clock from the DPLL */
  7327. i9xx_crtc_clock_get(crtc, pipe_config);
  7328. /*
  7329. * This value does not include pixel_multiplier.
  7330. * We will check that port_clock and adjusted_mode.crtc_clock
  7331. * agree once we know their relationship in the encoder's
  7332. * get_config() function.
  7333. */
  7334. pipe_config->adjusted_mode.crtc_clock =
  7335. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  7336. &pipe_config->fdi_m_n);
  7337. }
  7338. /** Returns the currently programmed mode of the given pipe. */
  7339. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  7340. struct drm_crtc *crtc)
  7341. {
  7342. struct drm_i915_private *dev_priv = dev->dev_private;
  7343. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7344. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  7345. struct drm_display_mode *mode;
  7346. struct intel_crtc_config pipe_config;
  7347. int htot = I915_READ(HTOTAL(cpu_transcoder));
  7348. int hsync = I915_READ(HSYNC(cpu_transcoder));
  7349. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  7350. int vsync = I915_READ(VSYNC(cpu_transcoder));
  7351. enum pipe pipe = intel_crtc->pipe;
  7352. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  7353. if (!mode)
  7354. return NULL;
  7355. /*
  7356. * Construct a pipe_config sufficient for getting the clock info
  7357. * back out of crtc_clock_get.
  7358. *
  7359. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  7360. * to use a real value here instead.
  7361. */
  7362. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  7363. pipe_config.pixel_multiplier = 1;
  7364. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  7365. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  7366. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  7367. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  7368. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  7369. mode->hdisplay = (htot & 0xffff) + 1;
  7370. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  7371. mode->hsync_start = (hsync & 0xffff) + 1;
  7372. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  7373. mode->vdisplay = (vtot & 0xffff) + 1;
  7374. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  7375. mode->vsync_start = (vsync & 0xffff) + 1;
  7376. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  7377. drm_mode_set_name(mode);
  7378. return mode;
  7379. }
  7380. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  7381. {
  7382. struct drm_device *dev = crtc->dev;
  7383. struct drm_i915_private *dev_priv = dev->dev_private;
  7384. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7385. if (!HAS_GMCH_DISPLAY(dev))
  7386. return;
  7387. if (!dev_priv->lvds_downclock_avail)
  7388. return;
  7389. /*
  7390. * Since this is called by a timer, we should never get here in
  7391. * the manual case.
  7392. */
  7393. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  7394. int pipe = intel_crtc->pipe;
  7395. int dpll_reg = DPLL(pipe);
  7396. int dpll;
  7397. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  7398. assert_panel_unlocked(dev_priv, pipe);
  7399. dpll = I915_READ(dpll_reg);
  7400. dpll |= DISPLAY_RATE_SELECT_FPA1;
  7401. I915_WRITE(dpll_reg, dpll);
  7402. intel_wait_for_vblank(dev, pipe);
  7403. dpll = I915_READ(dpll_reg);
  7404. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  7405. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  7406. }
  7407. }
  7408. void intel_mark_busy(struct drm_device *dev)
  7409. {
  7410. struct drm_i915_private *dev_priv = dev->dev_private;
  7411. if (dev_priv->mm.busy)
  7412. return;
  7413. intel_runtime_pm_get(dev_priv);
  7414. i915_update_gfx_val(dev_priv);
  7415. dev_priv->mm.busy = true;
  7416. }
  7417. void intel_mark_idle(struct drm_device *dev)
  7418. {
  7419. struct drm_i915_private *dev_priv = dev->dev_private;
  7420. struct drm_crtc *crtc;
  7421. if (!dev_priv->mm.busy)
  7422. return;
  7423. dev_priv->mm.busy = false;
  7424. if (!i915.powersave)
  7425. goto out;
  7426. for_each_crtc(dev, crtc) {
  7427. if (!crtc->primary->fb)
  7428. continue;
  7429. intel_decrease_pllclock(crtc);
  7430. }
  7431. if (INTEL_INFO(dev)->gen >= 6)
  7432. gen6_rps_idle(dev->dev_private);
  7433. out:
  7434. intel_runtime_pm_put(dev_priv);
  7435. }
  7436. static void intel_crtc_destroy(struct drm_crtc *crtc)
  7437. {
  7438. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7439. struct drm_device *dev = crtc->dev;
  7440. struct intel_unpin_work *work;
  7441. spin_lock_irq(&dev->event_lock);
  7442. work = intel_crtc->unpin_work;
  7443. intel_crtc->unpin_work = NULL;
  7444. spin_unlock_irq(&dev->event_lock);
  7445. if (work) {
  7446. cancel_work_sync(&work->work);
  7447. kfree(work);
  7448. }
  7449. drm_crtc_cleanup(crtc);
  7450. kfree(intel_crtc);
  7451. }
  7452. static void intel_unpin_work_fn(struct work_struct *__work)
  7453. {
  7454. struct intel_unpin_work *work =
  7455. container_of(__work, struct intel_unpin_work, work);
  7456. struct drm_device *dev = work->crtc->dev;
  7457. enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
  7458. mutex_lock(&dev->struct_mutex);
  7459. intel_unpin_fb_obj(work->old_fb_obj);
  7460. drm_gem_object_unreference(&work->pending_flip_obj->base);
  7461. drm_gem_object_unreference(&work->old_fb_obj->base);
  7462. intel_update_fbc(dev);
  7463. mutex_unlock(&dev->struct_mutex);
  7464. intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  7465. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  7466. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  7467. kfree(work);
  7468. }
  7469. static void do_intel_finish_page_flip(struct drm_device *dev,
  7470. struct drm_crtc *crtc)
  7471. {
  7472. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7473. struct intel_unpin_work *work;
  7474. unsigned long flags;
  7475. /* Ignore early vblank irqs */
  7476. if (intel_crtc == NULL)
  7477. return;
  7478. /*
  7479. * This is called both by irq handlers and the reset code (to complete
  7480. * lost pageflips) so needs the full irqsave spinlocks.
  7481. */
  7482. spin_lock_irqsave(&dev->event_lock, flags);
  7483. work = intel_crtc->unpin_work;
  7484. /* Ensure we don't miss a work->pending update ... */
  7485. smp_rmb();
  7486. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  7487. spin_unlock_irqrestore(&dev->event_lock, flags);
  7488. return;
  7489. }
  7490. page_flip_completed(intel_crtc);
  7491. spin_unlock_irqrestore(&dev->event_lock, flags);
  7492. }
  7493. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  7494. {
  7495. struct drm_i915_private *dev_priv = dev->dev_private;
  7496. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  7497. do_intel_finish_page_flip(dev, crtc);
  7498. }
  7499. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  7500. {
  7501. struct drm_i915_private *dev_priv = dev->dev_private;
  7502. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  7503. do_intel_finish_page_flip(dev, crtc);
  7504. }
  7505. /* Is 'a' after or equal to 'b'? */
  7506. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  7507. {
  7508. return !((a - b) & 0x80000000);
  7509. }
  7510. static bool page_flip_finished(struct intel_crtc *crtc)
  7511. {
  7512. struct drm_device *dev = crtc->base.dev;
  7513. struct drm_i915_private *dev_priv = dev->dev_private;
  7514. /*
  7515. * The relevant registers doen't exist on pre-ctg.
  7516. * As the flip done interrupt doesn't trigger for mmio
  7517. * flips on gmch platforms, a flip count check isn't
  7518. * really needed there. But since ctg has the registers,
  7519. * include it in the check anyway.
  7520. */
  7521. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  7522. return true;
  7523. /*
  7524. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  7525. * used the same base address. In that case the mmio flip might
  7526. * have completed, but the CS hasn't even executed the flip yet.
  7527. *
  7528. * A flip count check isn't enough as the CS might have updated
  7529. * the base address just after start of vblank, but before we
  7530. * managed to process the interrupt. This means we'd complete the
  7531. * CS flip too soon.
  7532. *
  7533. * Combining both checks should get us a good enough result. It may
  7534. * still happen that the CS flip has been executed, but has not
  7535. * yet actually completed. But in case the base address is the same
  7536. * anyway, we don't really care.
  7537. */
  7538. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  7539. crtc->unpin_work->gtt_offset &&
  7540. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  7541. crtc->unpin_work->flip_count);
  7542. }
  7543. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  7544. {
  7545. struct drm_i915_private *dev_priv = dev->dev_private;
  7546. struct intel_crtc *intel_crtc =
  7547. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  7548. unsigned long flags;
  7549. /*
  7550. * This is called both by irq handlers and the reset code (to complete
  7551. * lost pageflips) so needs the full irqsave spinlocks.
  7552. *
  7553. * NB: An MMIO update of the plane base pointer will also
  7554. * generate a page-flip completion irq, i.e. every modeset
  7555. * is also accompanied by a spurious intel_prepare_page_flip().
  7556. */
  7557. spin_lock_irqsave(&dev->event_lock, flags);
  7558. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  7559. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  7560. spin_unlock_irqrestore(&dev->event_lock, flags);
  7561. }
  7562. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  7563. {
  7564. /* Ensure that the work item is consistent when activating it ... */
  7565. smp_wmb();
  7566. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  7567. /* and that it is marked active as soon as the irq could fire. */
  7568. smp_wmb();
  7569. }
  7570. static int intel_gen2_queue_flip(struct drm_device *dev,
  7571. struct drm_crtc *crtc,
  7572. struct drm_framebuffer *fb,
  7573. struct drm_i915_gem_object *obj,
  7574. struct intel_engine_cs *ring,
  7575. uint32_t flags)
  7576. {
  7577. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7578. u32 flip_mask;
  7579. int ret;
  7580. ret = intel_ring_begin(ring, 6);
  7581. if (ret)
  7582. return ret;
  7583. /* Can't queue multiple flips, so wait for the previous
  7584. * one to finish before executing the next.
  7585. */
  7586. if (intel_crtc->plane)
  7587. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7588. else
  7589. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7590. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7591. intel_ring_emit(ring, MI_NOOP);
  7592. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7593. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7594. intel_ring_emit(ring, fb->pitches[0]);
  7595. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7596. intel_ring_emit(ring, 0); /* aux display base address, unused */
  7597. intel_mark_page_flip_active(intel_crtc);
  7598. __intel_ring_advance(ring);
  7599. return 0;
  7600. }
  7601. static int intel_gen3_queue_flip(struct drm_device *dev,
  7602. struct drm_crtc *crtc,
  7603. struct drm_framebuffer *fb,
  7604. struct drm_i915_gem_object *obj,
  7605. struct intel_engine_cs *ring,
  7606. uint32_t flags)
  7607. {
  7608. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7609. u32 flip_mask;
  7610. int ret;
  7611. ret = intel_ring_begin(ring, 6);
  7612. if (ret)
  7613. return ret;
  7614. if (intel_crtc->plane)
  7615. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7616. else
  7617. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7618. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7619. intel_ring_emit(ring, MI_NOOP);
  7620. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  7621. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7622. intel_ring_emit(ring, fb->pitches[0]);
  7623. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7624. intel_ring_emit(ring, MI_NOOP);
  7625. intel_mark_page_flip_active(intel_crtc);
  7626. __intel_ring_advance(ring);
  7627. return 0;
  7628. }
  7629. static int intel_gen4_queue_flip(struct drm_device *dev,
  7630. struct drm_crtc *crtc,
  7631. struct drm_framebuffer *fb,
  7632. struct drm_i915_gem_object *obj,
  7633. struct intel_engine_cs *ring,
  7634. uint32_t flags)
  7635. {
  7636. struct drm_i915_private *dev_priv = dev->dev_private;
  7637. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7638. uint32_t pf, pipesrc;
  7639. int ret;
  7640. ret = intel_ring_begin(ring, 4);
  7641. if (ret)
  7642. return ret;
  7643. /* i965+ uses the linear or tiled offsets from the
  7644. * Display Registers (which do not change across a page-flip)
  7645. * so we need only reprogram the base address.
  7646. */
  7647. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7648. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7649. intel_ring_emit(ring, fb->pitches[0]);
  7650. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  7651. obj->tiling_mode);
  7652. /* XXX Enabling the panel-fitter across page-flip is so far
  7653. * untested on non-native modes, so ignore it for now.
  7654. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  7655. */
  7656. pf = 0;
  7657. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7658. intel_ring_emit(ring, pf | pipesrc);
  7659. intel_mark_page_flip_active(intel_crtc);
  7660. __intel_ring_advance(ring);
  7661. return 0;
  7662. }
  7663. static int intel_gen6_queue_flip(struct drm_device *dev,
  7664. struct drm_crtc *crtc,
  7665. struct drm_framebuffer *fb,
  7666. struct drm_i915_gem_object *obj,
  7667. struct intel_engine_cs *ring,
  7668. uint32_t flags)
  7669. {
  7670. struct drm_i915_private *dev_priv = dev->dev_private;
  7671. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7672. uint32_t pf, pipesrc;
  7673. int ret;
  7674. ret = intel_ring_begin(ring, 4);
  7675. if (ret)
  7676. return ret;
  7677. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7678. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7679. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  7680. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7681. /* Contrary to the suggestions in the documentation,
  7682. * "Enable Panel Fitter" does not seem to be required when page
  7683. * flipping with a non-native mode, and worse causes a normal
  7684. * modeset to fail.
  7685. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  7686. */
  7687. pf = 0;
  7688. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7689. intel_ring_emit(ring, pf | pipesrc);
  7690. intel_mark_page_flip_active(intel_crtc);
  7691. __intel_ring_advance(ring);
  7692. return 0;
  7693. }
  7694. static int intel_gen7_queue_flip(struct drm_device *dev,
  7695. struct drm_crtc *crtc,
  7696. struct drm_framebuffer *fb,
  7697. struct drm_i915_gem_object *obj,
  7698. struct intel_engine_cs *ring,
  7699. uint32_t flags)
  7700. {
  7701. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7702. uint32_t plane_bit = 0;
  7703. int len, ret;
  7704. switch (intel_crtc->plane) {
  7705. case PLANE_A:
  7706. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  7707. break;
  7708. case PLANE_B:
  7709. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  7710. break;
  7711. case PLANE_C:
  7712. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  7713. break;
  7714. default:
  7715. WARN_ONCE(1, "unknown plane in flip command\n");
  7716. return -ENODEV;
  7717. }
  7718. len = 4;
  7719. if (ring->id == RCS) {
  7720. len += 6;
  7721. /*
  7722. * On Gen 8, SRM is now taking an extra dword to accommodate
  7723. * 48bits addresses, and we need a NOOP for the batch size to
  7724. * stay even.
  7725. */
  7726. if (IS_GEN8(dev))
  7727. len += 2;
  7728. }
  7729. /*
  7730. * BSpec MI_DISPLAY_FLIP for IVB:
  7731. * "The full packet must be contained within the same cache line."
  7732. *
  7733. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  7734. * cacheline, if we ever start emitting more commands before
  7735. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  7736. * then do the cacheline alignment, and finally emit the
  7737. * MI_DISPLAY_FLIP.
  7738. */
  7739. ret = intel_ring_cacheline_align(ring);
  7740. if (ret)
  7741. return ret;
  7742. ret = intel_ring_begin(ring, len);
  7743. if (ret)
  7744. return ret;
  7745. /* Unmask the flip-done completion message. Note that the bspec says that
  7746. * we should do this for both the BCS and RCS, and that we must not unmask
  7747. * more than one flip event at any time (or ensure that one flip message
  7748. * can be sent by waiting for flip-done prior to queueing new flips).
  7749. * Experimentation says that BCS works despite DERRMR masking all
  7750. * flip-done completion events and that unmasking all planes at once
  7751. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  7752. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  7753. */
  7754. if (ring->id == RCS) {
  7755. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  7756. intel_ring_emit(ring, DERRMR);
  7757. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  7758. DERRMR_PIPEB_PRI_FLIP_DONE |
  7759. DERRMR_PIPEC_PRI_FLIP_DONE));
  7760. if (IS_GEN8(dev))
  7761. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  7762. MI_SRM_LRM_GLOBAL_GTT);
  7763. else
  7764. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  7765. MI_SRM_LRM_GLOBAL_GTT);
  7766. intel_ring_emit(ring, DERRMR);
  7767. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  7768. if (IS_GEN8(dev)) {
  7769. intel_ring_emit(ring, 0);
  7770. intel_ring_emit(ring, MI_NOOP);
  7771. }
  7772. }
  7773. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  7774. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  7775. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7776. intel_ring_emit(ring, (MI_NOOP));
  7777. intel_mark_page_flip_active(intel_crtc);
  7778. __intel_ring_advance(ring);
  7779. return 0;
  7780. }
  7781. static bool use_mmio_flip(struct intel_engine_cs *ring,
  7782. struct drm_i915_gem_object *obj)
  7783. {
  7784. /*
  7785. * This is not being used for older platforms, because
  7786. * non-availability of flip done interrupt forces us to use
  7787. * CS flips. Older platforms derive flip done using some clever
  7788. * tricks involving the flip_pending status bits and vblank irqs.
  7789. * So using MMIO flips there would disrupt this mechanism.
  7790. */
  7791. if (ring == NULL)
  7792. return true;
  7793. if (INTEL_INFO(ring->dev)->gen < 5)
  7794. return false;
  7795. if (i915.use_mmio_flip < 0)
  7796. return false;
  7797. else if (i915.use_mmio_flip > 0)
  7798. return true;
  7799. else if (i915.enable_execlists)
  7800. return true;
  7801. else
  7802. return ring != obj->ring;
  7803. }
  7804. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  7805. {
  7806. struct drm_device *dev = intel_crtc->base.dev;
  7807. struct drm_i915_private *dev_priv = dev->dev_private;
  7808. struct intel_framebuffer *intel_fb =
  7809. to_intel_framebuffer(intel_crtc->base.primary->fb);
  7810. struct drm_i915_gem_object *obj = intel_fb->obj;
  7811. u32 dspcntr;
  7812. u32 reg;
  7813. intel_mark_page_flip_active(intel_crtc);
  7814. reg = DSPCNTR(intel_crtc->plane);
  7815. dspcntr = I915_READ(reg);
  7816. if (obj->tiling_mode != I915_TILING_NONE)
  7817. dspcntr |= DISPPLANE_TILED;
  7818. else
  7819. dspcntr &= ~DISPPLANE_TILED;
  7820. I915_WRITE(reg, dspcntr);
  7821. I915_WRITE(DSPSURF(intel_crtc->plane),
  7822. intel_crtc->unpin_work->gtt_offset);
  7823. POSTING_READ(DSPSURF(intel_crtc->plane));
  7824. }
  7825. static int intel_postpone_flip(struct drm_i915_gem_object *obj)
  7826. {
  7827. struct intel_engine_cs *ring;
  7828. int ret;
  7829. lockdep_assert_held(&obj->base.dev->struct_mutex);
  7830. if (!obj->last_write_seqno)
  7831. return 0;
  7832. ring = obj->ring;
  7833. if (i915_seqno_passed(ring->get_seqno(ring, true),
  7834. obj->last_write_seqno))
  7835. return 0;
  7836. ret = i915_gem_check_olr(ring, obj->last_write_seqno);
  7837. if (ret)
  7838. return ret;
  7839. if (WARN_ON(!ring->irq_get(ring)))
  7840. return 0;
  7841. return 1;
  7842. }
  7843. void intel_notify_mmio_flip(struct intel_engine_cs *ring)
  7844. {
  7845. struct drm_i915_private *dev_priv = to_i915(ring->dev);
  7846. struct intel_crtc *intel_crtc;
  7847. unsigned long irq_flags;
  7848. u32 seqno;
  7849. seqno = ring->get_seqno(ring, false);
  7850. spin_lock_irqsave(&dev_priv->mmio_flip_lock, irq_flags);
  7851. for_each_intel_crtc(ring->dev, intel_crtc) {
  7852. struct intel_mmio_flip *mmio_flip;
  7853. mmio_flip = &intel_crtc->mmio_flip;
  7854. if (mmio_flip->seqno == 0)
  7855. continue;
  7856. if (ring->id != mmio_flip->ring_id)
  7857. continue;
  7858. if (i915_seqno_passed(seqno, mmio_flip->seqno)) {
  7859. intel_do_mmio_flip(intel_crtc);
  7860. mmio_flip->seqno = 0;
  7861. ring->irq_put(ring);
  7862. }
  7863. }
  7864. spin_unlock_irqrestore(&dev_priv->mmio_flip_lock, irq_flags);
  7865. }
  7866. static int intel_queue_mmio_flip(struct drm_device *dev,
  7867. struct drm_crtc *crtc,
  7868. struct drm_framebuffer *fb,
  7869. struct drm_i915_gem_object *obj,
  7870. struct intel_engine_cs *ring,
  7871. uint32_t flags)
  7872. {
  7873. struct drm_i915_private *dev_priv = dev->dev_private;
  7874. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7875. int ret;
  7876. if (WARN_ON(intel_crtc->mmio_flip.seqno))
  7877. return -EBUSY;
  7878. ret = intel_postpone_flip(obj);
  7879. if (ret < 0)
  7880. return ret;
  7881. if (ret == 0) {
  7882. intel_do_mmio_flip(intel_crtc);
  7883. return 0;
  7884. }
  7885. spin_lock_irq(&dev_priv->mmio_flip_lock);
  7886. intel_crtc->mmio_flip.seqno = obj->last_write_seqno;
  7887. intel_crtc->mmio_flip.ring_id = obj->ring->id;
  7888. spin_unlock_irq(&dev_priv->mmio_flip_lock);
  7889. /*
  7890. * Double check to catch cases where irq fired before
  7891. * mmio flip data was ready
  7892. */
  7893. intel_notify_mmio_flip(obj->ring);
  7894. return 0;
  7895. }
  7896. static int intel_default_queue_flip(struct drm_device *dev,
  7897. struct drm_crtc *crtc,
  7898. struct drm_framebuffer *fb,
  7899. struct drm_i915_gem_object *obj,
  7900. struct intel_engine_cs *ring,
  7901. uint32_t flags)
  7902. {
  7903. return -ENODEV;
  7904. }
  7905. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  7906. struct drm_crtc *crtc)
  7907. {
  7908. struct drm_i915_private *dev_priv = dev->dev_private;
  7909. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7910. struct intel_unpin_work *work = intel_crtc->unpin_work;
  7911. u32 addr;
  7912. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  7913. return true;
  7914. if (!work->enable_stall_check)
  7915. return false;
  7916. if (work->flip_ready_vblank == 0) {
  7917. if (work->flip_queued_ring &&
  7918. !i915_seqno_passed(work->flip_queued_ring->get_seqno(work->flip_queued_ring, true),
  7919. work->flip_queued_seqno))
  7920. return false;
  7921. work->flip_ready_vblank = drm_vblank_count(dev, intel_crtc->pipe);
  7922. }
  7923. if (drm_vblank_count(dev, intel_crtc->pipe) - work->flip_ready_vblank < 3)
  7924. return false;
  7925. /* Potential stall - if we see that the flip has happened,
  7926. * assume a missed interrupt. */
  7927. if (INTEL_INFO(dev)->gen >= 4)
  7928. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  7929. else
  7930. addr = I915_READ(DSPADDR(intel_crtc->plane));
  7931. /* There is a potential issue here with a false positive after a flip
  7932. * to the same address. We could address this by checking for a
  7933. * non-incrementing frame counter.
  7934. */
  7935. return addr == work->gtt_offset;
  7936. }
  7937. void intel_check_page_flip(struct drm_device *dev, int pipe)
  7938. {
  7939. struct drm_i915_private *dev_priv = dev->dev_private;
  7940. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  7941. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7942. WARN_ON(!in_irq());
  7943. if (crtc == NULL)
  7944. return;
  7945. spin_lock(&dev->event_lock);
  7946. if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
  7947. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  7948. intel_crtc->unpin_work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  7949. page_flip_completed(intel_crtc);
  7950. }
  7951. spin_unlock(&dev->event_lock);
  7952. }
  7953. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  7954. struct drm_framebuffer *fb,
  7955. struct drm_pending_vblank_event *event,
  7956. uint32_t page_flip_flags)
  7957. {
  7958. struct drm_device *dev = crtc->dev;
  7959. struct drm_i915_private *dev_priv = dev->dev_private;
  7960. struct drm_framebuffer *old_fb = crtc->primary->fb;
  7961. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  7962. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7963. enum pipe pipe = intel_crtc->pipe;
  7964. struct intel_unpin_work *work;
  7965. struct intel_engine_cs *ring;
  7966. int ret;
  7967. /*
  7968. * drm_mode_page_flip_ioctl() should already catch this, but double
  7969. * check to be safe. In the future we may enable pageflipping from
  7970. * a disabled primary plane.
  7971. */
  7972. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  7973. return -EBUSY;
  7974. /* Can't change pixel format via MI display flips. */
  7975. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  7976. return -EINVAL;
  7977. /*
  7978. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  7979. * Note that pitch changes could also affect these register.
  7980. */
  7981. if (INTEL_INFO(dev)->gen > 3 &&
  7982. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  7983. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  7984. return -EINVAL;
  7985. if (i915_terminally_wedged(&dev_priv->gpu_error))
  7986. goto out_hang;
  7987. work = kzalloc(sizeof(*work), GFP_KERNEL);
  7988. if (work == NULL)
  7989. return -ENOMEM;
  7990. work->event = event;
  7991. work->crtc = crtc;
  7992. work->old_fb_obj = intel_fb_obj(old_fb);
  7993. INIT_WORK(&work->work, intel_unpin_work_fn);
  7994. ret = drm_crtc_vblank_get(crtc);
  7995. if (ret)
  7996. goto free_work;
  7997. /* We borrow the event spin lock for protecting unpin_work */
  7998. spin_lock_irq(&dev->event_lock);
  7999. if (intel_crtc->unpin_work) {
  8000. /* Before declaring the flip queue wedged, check if
  8001. * the hardware completed the operation behind our backs.
  8002. */
  8003. if (__intel_pageflip_stall_check(dev, crtc)) {
  8004. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  8005. page_flip_completed(intel_crtc);
  8006. } else {
  8007. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  8008. spin_unlock_irq(&dev->event_lock);
  8009. drm_crtc_vblank_put(crtc);
  8010. kfree(work);
  8011. return -EBUSY;
  8012. }
  8013. }
  8014. intel_crtc->unpin_work = work;
  8015. spin_unlock_irq(&dev->event_lock);
  8016. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  8017. flush_workqueue(dev_priv->wq);
  8018. ret = i915_mutex_lock_interruptible(dev);
  8019. if (ret)
  8020. goto cleanup;
  8021. /* Reference the objects for the scheduled work. */
  8022. drm_gem_object_reference(&work->old_fb_obj->base);
  8023. drm_gem_object_reference(&obj->base);
  8024. crtc->primary->fb = fb;
  8025. work->pending_flip_obj = obj;
  8026. atomic_inc(&intel_crtc->unpin_work_count);
  8027. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  8028. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  8029. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  8030. if (IS_VALLEYVIEW(dev)) {
  8031. ring = &dev_priv->ring[BCS];
  8032. if (obj->tiling_mode != work->old_fb_obj->tiling_mode)
  8033. /* vlv: DISPLAY_FLIP fails to change tiling */
  8034. ring = NULL;
  8035. } else if (IS_IVYBRIDGE(dev)) {
  8036. ring = &dev_priv->ring[BCS];
  8037. } else if (INTEL_INFO(dev)->gen >= 7) {
  8038. ring = obj->ring;
  8039. if (ring == NULL || ring->id != RCS)
  8040. ring = &dev_priv->ring[BCS];
  8041. } else {
  8042. ring = &dev_priv->ring[RCS];
  8043. }
  8044. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  8045. if (ret)
  8046. goto cleanup_pending;
  8047. work->gtt_offset =
  8048. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
  8049. if (use_mmio_flip(ring, obj)) {
  8050. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  8051. page_flip_flags);
  8052. if (ret)
  8053. goto cleanup_unpin;
  8054. work->flip_queued_seqno = obj->last_write_seqno;
  8055. work->flip_queued_ring = obj->ring;
  8056. } else {
  8057. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
  8058. page_flip_flags);
  8059. if (ret)
  8060. goto cleanup_unpin;
  8061. work->flip_queued_seqno = intel_ring_get_seqno(ring);
  8062. work->flip_queued_ring = ring;
  8063. }
  8064. work->flip_queued_vblank = drm_vblank_count(dev, intel_crtc->pipe);
  8065. work->enable_stall_check = true;
  8066. i915_gem_track_fb(work->old_fb_obj, obj,
  8067. INTEL_FRONTBUFFER_PRIMARY(pipe));
  8068. intel_disable_fbc(dev);
  8069. intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  8070. mutex_unlock(&dev->struct_mutex);
  8071. trace_i915_flip_request(intel_crtc->plane, obj);
  8072. return 0;
  8073. cleanup_unpin:
  8074. intel_unpin_fb_obj(obj);
  8075. cleanup_pending:
  8076. atomic_dec(&intel_crtc->unpin_work_count);
  8077. crtc->primary->fb = old_fb;
  8078. drm_gem_object_unreference(&work->old_fb_obj->base);
  8079. drm_gem_object_unreference(&obj->base);
  8080. mutex_unlock(&dev->struct_mutex);
  8081. cleanup:
  8082. spin_lock_irq(&dev->event_lock);
  8083. intel_crtc->unpin_work = NULL;
  8084. spin_unlock_irq(&dev->event_lock);
  8085. drm_crtc_vblank_put(crtc);
  8086. free_work:
  8087. kfree(work);
  8088. if (ret == -EIO) {
  8089. out_hang:
  8090. intel_crtc_wait_for_pending_flips(crtc);
  8091. ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
  8092. if (ret == 0 && event) {
  8093. spin_lock_irq(&dev->event_lock);
  8094. drm_send_vblank_event(dev, pipe, event);
  8095. spin_unlock_irq(&dev->event_lock);
  8096. }
  8097. }
  8098. return ret;
  8099. }
  8100. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  8101. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  8102. .load_lut = intel_crtc_load_lut,
  8103. };
  8104. /**
  8105. * intel_modeset_update_staged_output_state
  8106. *
  8107. * Updates the staged output configuration state, e.g. after we've read out the
  8108. * current hw state.
  8109. */
  8110. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  8111. {
  8112. struct intel_crtc *crtc;
  8113. struct intel_encoder *encoder;
  8114. struct intel_connector *connector;
  8115. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8116. base.head) {
  8117. connector->new_encoder =
  8118. to_intel_encoder(connector->base.encoder);
  8119. }
  8120. for_each_intel_encoder(dev, encoder) {
  8121. encoder->new_crtc =
  8122. to_intel_crtc(encoder->base.crtc);
  8123. }
  8124. for_each_intel_crtc(dev, crtc) {
  8125. crtc->new_enabled = crtc->base.enabled;
  8126. if (crtc->new_enabled)
  8127. crtc->new_config = &crtc->config;
  8128. else
  8129. crtc->new_config = NULL;
  8130. }
  8131. }
  8132. /**
  8133. * intel_modeset_commit_output_state
  8134. *
  8135. * This function copies the stage display pipe configuration to the real one.
  8136. */
  8137. static void intel_modeset_commit_output_state(struct drm_device *dev)
  8138. {
  8139. struct intel_crtc *crtc;
  8140. struct intel_encoder *encoder;
  8141. struct intel_connector *connector;
  8142. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8143. base.head) {
  8144. connector->base.encoder = &connector->new_encoder->base;
  8145. }
  8146. for_each_intel_encoder(dev, encoder) {
  8147. encoder->base.crtc = &encoder->new_crtc->base;
  8148. }
  8149. for_each_intel_crtc(dev, crtc) {
  8150. crtc->base.enabled = crtc->new_enabled;
  8151. }
  8152. }
  8153. static void
  8154. connected_sink_compute_bpp(struct intel_connector *connector,
  8155. struct intel_crtc_config *pipe_config)
  8156. {
  8157. int bpp = pipe_config->pipe_bpp;
  8158. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8159. connector->base.base.id,
  8160. connector->base.name);
  8161. /* Don't use an invalid EDID bpc value */
  8162. if (connector->base.display_info.bpc &&
  8163. connector->base.display_info.bpc * 3 < bpp) {
  8164. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8165. bpp, connector->base.display_info.bpc*3);
  8166. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  8167. }
  8168. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8169. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  8170. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8171. bpp);
  8172. pipe_config->pipe_bpp = 24;
  8173. }
  8174. }
  8175. static int
  8176. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8177. struct drm_framebuffer *fb,
  8178. struct intel_crtc_config *pipe_config)
  8179. {
  8180. struct drm_device *dev = crtc->base.dev;
  8181. struct intel_connector *connector;
  8182. int bpp;
  8183. switch (fb->pixel_format) {
  8184. case DRM_FORMAT_C8:
  8185. bpp = 8*3; /* since we go through a colormap */
  8186. break;
  8187. case DRM_FORMAT_XRGB1555:
  8188. case DRM_FORMAT_ARGB1555:
  8189. /* checked in intel_framebuffer_init already */
  8190. if (WARN_ON(INTEL_INFO(dev)->gen > 3))
  8191. return -EINVAL;
  8192. case DRM_FORMAT_RGB565:
  8193. bpp = 6*3; /* min is 18bpp */
  8194. break;
  8195. case DRM_FORMAT_XBGR8888:
  8196. case DRM_FORMAT_ABGR8888:
  8197. /* checked in intel_framebuffer_init already */
  8198. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8199. return -EINVAL;
  8200. case DRM_FORMAT_XRGB8888:
  8201. case DRM_FORMAT_ARGB8888:
  8202. bpp = 8*3;
  8203. break;
  8204. case DRM_FORMAT_XRGB2101010:
  8205. case DRM_FORMAT_ARGB2101010:
  8206. case DRM_FORMAT_XBGR2101010:
  8207. case DRM_FORMAT_ABGR2101010:
  8208. /* checked in intel_framebuffer_init already */
  8209. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8210. return -EINVAL;
  8211. bpp = 10*3;
  8212. break;
  8213. /* TODO: gen4+ supports 16 bpc floating point, too. */
  8214. default:
  8215. DRM_DEBUG_KMS("unsupported depth\n");
  8216. return -EINVAL;
  8217. }
  8218. pipe_config->pipe_bpp = bpp;
  8219. /* Clamp display bpp to EDID value */
  8220. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8221. base.head) {
  8222. if (!connector->new_encoder ||
  8223. connector->new_encoder->new_crtc != crtc)
  8224. continue;
  8225. connected_sink_compute_bpp(connector, pipe_config);
  8226. }
  8227. return bpp;
  8228. }
  8229. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  8230. {
  8231. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  8232. "type: 0x%x flags: 0x%x\n",
  8233. mode->crtc_clock,
  8234. mode->crtc_hdisplay, mode->crtc_hsync_start,
  8235. mode->crtc_hsync_end, mode->crtc_htotal,
  8236. mode->crtc_vdisplay, mode->crtc_vsync_start,
  8237. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  8238. }
  8239. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  8240. struct intel_crtc_config *pipe_config,
  8241. const char *context)
  8242. {
  8243. DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
  8244. context, pipe_name(crtc->pipe));
  8245. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  8246. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  8247. pipe_config->pipe_bpp, pipe_config->dither);
  8248. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8249. pipe_config->has_pch_encoder,
  8250. pipe_config->fdi_lanes,
  8251. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  8252. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  8253. pipe_config->fdi_m_n.tu);
  8254. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8255. pipe_config->has_dp_encoder,
  8256. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  8257. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  8258. pipe_config->dp_m_n.tu);
  8259. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  8260. pipe_config->has_dp_encoder,
  8261. pipe_config->dp_m2_n2.gmch_m,
  8262. pipe_config->dp_m2_n2.gmch_n,
  8263. pipe_config->dp_m2_n2.link_m,
  8264. pipe_config->dp_m2_n2.link_n,
  8265. pipe_config->dp_m2_n2.tu);
  8266. DRM_DEBUG_KMS("requested mode:\n");
  8267. drm_mode_debug_printmodeline(&pipe_config->requested_mode);
  8268. DRM_DEBUG_KMS("adjusted mode:\n");
  8269. drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
  8270. intel_dump_crtc_timings(&pipe_config->adjusted_mode);
  8271. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  8272. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  8273. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  8274. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  8275. pipe_config->gmch_pfit.control,
  8276. pipe_config->gmch_pfit.pgm_ratios,
  8277. pipe_config->gmch_pfit.lvds_border_bits);
  8278. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  8279. pipe_config->pch_pfit.pos,
  8280. pipe_config->pch_pfit.size,
  8281. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  8282. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  8283. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  8284. }
  8285. static bool encoders_cloneable(const struct intel_encoder *a,
  8286. const struct intel_encoder *b)
  8287. {
  8288. /* masks could be asymmetric, so check both ways */
  8289. return a == b || (a->cloneable & (1 << b->type) &&
  8290. b->cloneable & (1 << a->type));
  8291. }
  8292. static bool check_single_encoder_cloning(struct intel_crtc *crtc,
  8293. struct intel_encoder *encoder)
  8294. {
  8295. struct drm_device *dev = crtc->base.dev;
  8296. struct intel_encoder *source_encoder;
  8297. for_each_intel_encoder(dev, source_encoder) {
  8298. if (source_encoder->new_crtc != crtc)
  8299. continue;
  8300. if (!encoders_cloneable(encoder, source_encoder))
  8301. return false;
  8302. }
  8303. return true;
  8304. }
  8305. static bool check_encoder_cloning(struct intel_crtc *crtc)
  8306. {
  8307. struct drm_device *dev = crtc->base.dev;
  8308. struct intel_encoder *encoder;
  8309. for_each_intel_encoder(dev, encoder) {
  8310. if (encoder->new_crtc != crtc)
  8311. continue;
  8312. if (!check_single_encoder_cloning(crtc, encoder))
  8313. return false;
  8314. }
  8315. return true;
  8316. }
  8317. static struct intel_crtc_config *
  8318. intel_modeset_pipe_config(struct drm_crtc *crtc,
  8319. struct drm_framebuffer *fb,
  8320. struct drm_display_mode *mode)
  8321. {
  8322. struct drm_device *dev = crtc->dev;
  8323. struct intel_encoder *encoder;
  8324. struct intel_crtc_config *pipe_config;
  8325. int plane_bpp, ret = -EINVAL;
  8326. bool retry = true;
  8327. if (!check_encoder_cloning(to_intel_crtc(crtc))) {
  8328. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  8329. return ERR_PTR(-EINVAL);
  8330. }
  8331. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8332. if (!pipe_config)
  8333. return ERR_PTR(-ENOMEM);
  8334. drm_mode_copy(&pipe_config->adjusted_mode, mode);
  8335. drm_mode_copy(&pipe_config->requested_mode, mode);
  8336. pipe_config->cpu_transcoder =
  8337. (enum transcoder) to_intel_crtc(crtc)->pipe;
  8338. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8339. /*
  8340. * Sanitize sync polarity flags based on requested ones. If neither
  8341. * positive or negative polarity is requested, treat this as meaning
  8342. * negative polarity.
  8343. */
  8344. if (!(pipe_config->adjusted_mode.flags &
  8345. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  8346. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  8347. if (!(pipe_config->adjusted_mode.flags &
  8348. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  8349. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  8350. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  8351. * plane pixel format and any sink constraints into account. Returns the
  8352. * source plane bpp so that dithering can be selected on mismatches
  8353. * after encoders and crtc also have had their say. */
  8354. plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  8355. fb, pipe_config);
  8356. if (plane_bpp < 0)
  8357. goto fail;
  8358. /*
  8359. * Determine the real pipe dimensions. Note that stereo modes can
  8360. * increase the actual pipe size due to the frame doubling and
  8361. * insertion of additional space for blanks between the frame. This
  8362. * is stored in the crtc timings. We use the requested mode to do this
  8363. * computation to clearly distinguish it from the adjusted mode, which
  8364. * can be changed by the connectors in the below retry loop.
  8365. */
  8366. drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
  8367. pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
  8368. pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
  8369. encoder_retry:
  8370. /* Ensure the port clock defaults are reset when retrying. */
  8371. pipe_config->port_clock = 0;
  8372. pipe_config->pixel_multiplier = 1;
  8373. /* Fill in default crtc timings, allow encoders to overwrite them. */
  8374. drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
  8375. /* Pass our mode to the connectors and the CRTC to give them a chance to
  8376. * adjust it according to limitations or connector properties, and also
  8377. * a chance to reject the mode entirely.
  8378. */
  8379. for_each_intel_encoder(dev, encoder) {
  8380. if (&encoder->new_crtc->base != crtc)
  8381. continue;
  8382. if (!(encoder->compute_config(encoder, pipe_config))) {
  8383. DRM_DEBUG_KMS("Encoder config failure\n");
  8384. goto fail;
  8385. }
  8386. }
  8387. /* Set default port clock if not overwritten by the encoder. Needs to be
  8388. * done afterwards in case the encoder adjusts the mode. */
  8389. if (!pipe_config->port_clock)
  8390. pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
  8391. * pipe_config->pixel_multiplier;
  8392. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  8393. if (ret < 0) {
  8394. DRM_DEBUG_KMS("CRTC fixup failed\n");
  8395. goto fail;
  8396. }
  8397. if (ret == RETRY) {
  8398. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  8399. ret = -EINVAL;
  8400. goto fail;
  8401. }
  8402. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  8403. retry = false;
  8404. goto encoder_retry;
  8405. }
  8406. pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
  8407. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  8408. plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  8409. return pipe_config;
  8410. fail:
  8411. kfree(pipe_config);
  8412. return ERR_PTR(ret);
  8413. }
  8414. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  8415. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  8416. static void
  8417. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  8418. unsigned *prepare_pipes, unsigned *disable_pipes)
  8419. {
  8420. struct intel_crtc *intel_crtc;
  8421. struct drm_device *dev = crtc->dev;
  8422. struct intel_encoder *encoder;
  8423. struct intel_connector *connector;
  8424. struct drm_crtc *tmp_crtc;
  8425. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  8426. /* Check which crtcs have changed outputs connected to them, these need
  8427. * to be part of the prepare_pipes mask. We don't (yet) support global
  8428. * modeset across multiple crtcs, so modeset_pipes will only have one
  8429. * bit set at most. */
  8430. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8431. base.head) {
  8432. if (connector->base.encoder == &connector->new_encoder->base)
  8433. continue;
  8434. if (connector->base.encoder) {
  8435. tmp_crtc = connector->base.encoder->crtc;
  8436. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8437. }
  8438. if (connector->new_encoder)
  8439. *prepare_pipes |=
  8440. 1 << connector->new_encoder->new_crtc->pipe;
  8441. }
  8442. for_each_intel_encoder(dev, encoder) {
  8443. if (encoder->base.crtc == &encoder->new_crtc->base)
  8444. continue;
  8445. if (encoder->base.crtc) {
  8446. tmp_crtc = encoder->base.crtc;
  8447. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8448. }
  8449. if (encoder->new_crtc)
  8450. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  8451. }
  8452. /* Check for pipes that will be enabled/disabled ... */
  8453. for_each_intel_crtc(dev, intel_crtc) {
  8454. if (intel_crtc->base.enabled == intel_crtc->new_enabled)
  8455. continue;
  8456. if (!intel_crtc->new_enabled)
  8457. *disable_pipes |= 1 << intel_crtc->pipe;
  8458. else
  8459. *prepare_pipes |= 1 << intel_crtc->pipe;
  8460. }
  8461. /* set_mode is also used to update properties on life display pipes. */
  8462. intel_crtc = to_intel_crtc(crtc);
  8463. if (intel_crtc->new_enabled)
  8464. *prepare_pipes |= 1 << intel_crtc->pipe;
  8465. /*
  8466. * For simplicity do a full modeset on any pipe where the output routing
  8467. * changed. We could be more clever, but that would require us to be
  8468. * more careful with calling the relevant encoder->mode_set functions.
  8469. */
  8470. if (*prepare_pipes)
  8471. *modeset_pipes = *prepare_pipes;
  8472. /* ... and mask these out. */
  8473. *modeset_pipes &= ~(*disable_pipes);
  8474. *prepare_pipes &= ~(*disable_pipes);
  8475. /*
  8476. * HACK: We don't (yet) fully support global modesets. intel_set_config
  8477. * obies this rule, but the modeset restore mode of
  8478. * intel_modeset_setup_hw_state does not.
  8479. */
  8480. *modeset_pipes &= 1 << intel_crtc->pipe;
  8481. *prepare_pipes &= 1 << intel_crtc->pipe;
  8482. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  8483. *modeset_pipes, *prepare_pipes, *disable_pipes);
  8484. }
  8485. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  8486. {
  8487. struct drm_encoder *encoder;
  8488. struct drm_device *dev = crtc->dev;
  8489. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  8490. if (encoder->crtc == crtc)
  8491. return true;
  8492. return false;
  8493. }
  8494. static void
  8495. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  8496. {
  8497. struct intel_encoder *intel_encoder;
  8498. struct intel_crtc *intel_crtc;
  8499. struct drm_connector *connector;
  8500. for_each_intel_encoder(dev, intel_encoder) {
  8501. if (!intel_encoder->base.crtc)
  8502. continue;
  8503. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  8504. if (prepare_pipes & (1 << intel_crtc->pipe))
  8505. intel_encoder->connectors_active = false;
  8506. }
  8507. intel_modeset_commit_output_state(dev);
  8508. /* Double check state. */
  8509. for_each_intel_crtc(dev, intel_crtc) {
  8510. WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
  8511. WARN_ON(intel_crtc->new_config &&
  8512. intel_crtc->new_config != &intel_crtc->config);
  8513. WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
  8514. }
  8515. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  8516. if (!connector->encoder || !connector->encoder->crtc)
  8517. continue;
  8518. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  8519. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  8520. struct drm_property *dpms_property =
  8521. dev->mode_config.dpms_property;
  8522. connector->dpms = DRM_MODE_DPMS_ON;
  8523. drm_object_property_set_value(&connector->base,
  8524. dpms_property,
  8525. DRM_MODE_DPMS_ON);
  8526. intel_encoder = to_intel_encoder(connector->encoder);
  8527. intel_encoder->connectors_active = true;
  8528. }
  8529. }
  8530. }
  8531. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  8532. {
  8533. int diff;
  8534. if (clock1 == clock2)
  8535. return true;
  8536. if (!clock1 || !clock2)
  8537. return false;
  8538. diff = abs(clock1 - clock2);
  8539. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  8540. return true;
  8541. return false;
  8542. }
  8543. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  8544. list_for_each_entry((intel_crtc), \
  8545. &(dev)->mode_config.crtc_list, \
  8546. base.head) \
  8547. if (mask & (1 <<(intel_crtc)->pipe))
  8548. static bool
  8549. intel_pipe_config_compare(struct drm_device *dev,
  8550. struct intel_crtc_config *current_config,
  8551. struct intel_crtc_config *pipe_config)
  8552. {
  8553. #define PIPE_CONF_CHECK_X(name) \
  8554. if (current_config->name != pipe_config->name) { \
  8555. DRM_ERROR("mismatch in " #name " " \
  8556. "(expected 0x%08x, found 0x%08x)\n", \
  8557. current_config->name, \
  8558. pipe_config->name); \
  8559. return false; \
  8560. }
  8561. #define PIPE_CONF_CHECK_I(name) \
  8562. if (current_config->name != pipe_config->name) { \
  8563. DRM_ERROR("mismatch in " #name " " \
  8564. "(expected %i, found %i)\n", \
  8565. current_config->name, \
  8566. pipe_config->name); \
  8567. return false; \
  8568. }
  8569. /* This is required for BDW+ where there is only one set of registers for
  8570. * switching between high and low RR.
  8571. * This macro can be used whenever a comparison has to be made between one
  8572. * hw state and multiple sw state variables.
  8573. */
  8574. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  8575. if ((current_config->name != pipe_config->name) && \
  8576. (current_config->alt_name != pipe_config->name)) { \
  8577. DRM_ERROR("mismatch in " #name " " \
  8578. "(expected %i or %i, found %i)\n", \
  8579. current_config->name, \
  8580. current_config->alt_name, \
  8581. pipe_config->name); \
  8582. return false; \
  8583. }
  8584. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  8585. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  8586. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  8587. "(expected %i, found %i)\n", \
  8588. current_config->name & (mask), \
  8589. pipe_config->name & (mask)); \
  8590. return false; \
  8591. }
  8592. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  8593. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  8594. DRM_ERROR("mismatch in " #name " " \
  8595. "(expected %i, found %i)\n", \
  8596. current_config->name, \
  8597. pipe_config->name); \
  8598. return false; \
  8599. }
  8600. #define PIPE_CONF_QUIRK(quirk) \
  8601. ((current_config->quirks | pipe_config->quirks) & (quirk))
  8602. PIPE_CONF_CHECK_I(cpu_transcoder);
  8603. PIPE_CONF_CHECK_I(has_pch_encoder);
  8604. PIPE_CONF_CHECK_I(fdi_lanes);
  8605. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  8606. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  8607. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  8608. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  8609. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  8610. PIPE_CONF_CHECK_I(has_dp_encoder);
  8611. if (INTEL_INFO(dev)->gen < 8) {
  8612. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  8613. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  8614. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  8615. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  8616. PIPE_CONF_CHECK_I(dp_m_n.tu);
  8617. if (current_config->has_drrs) {
  8618. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
  8619. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
  8620. PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
  8621. PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
  8622. PIPE_CONF_CHECK_I(dp_m2_n2.tu);
  8623. }
  8624. } else {
  8625. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
  8626. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
  8627. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
  8628. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
  8629. PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
  8630. }
  8631. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
  8632. PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
  8633. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
  8634. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
  8635. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
  8636. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
  8637. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
  8638. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
  8639. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
  8640. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
  8641. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
  8642. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
  8643. PIPE_CONF_CHECK_I(pixel_multiplier);
  8644. PIPE_CONF_CHECK_I(has_hdmi_sink);
  8645. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  8646. IS_VALLEYVIEW(dev))
  8647. PIPE_CONF_CHECK_I(limited_color_range);
  8648. PIPE_CONF_CHECK_I(has_audio);
  8649. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8650. DRM_MODE_FLAG_INTERLACE);
  8651. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  8652. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8653. DRM_MODE_FLAG_PHSYNC);
  8654. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8655. DRM_MODE_FLAG_NHSYNC);
  8656. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8657. DRM_MODE_FLAG_PVSYNC);
  8658. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8659. DRM_MODE_FLAG_NVSYNC);
  8660. }
  8661. PIPE_CONF_CHECK_I(pipe_src_w);
  8662. PIPE_CONF_CHECK_I(pipe_src_h);
  8663. /*
  8664. * FIXME: BIOS likes to set up a cloned config with lvds+external
  8665. * screen. Since we don't yet re-compute the pipe config when moving
  8666. * just the lvds port away to another pipe the sw tracking won't match.
  8667. *
  8668. * Proper atomic modesets with recomputed global state will fix this.
  8669. * Until then just don't check gmch state for inherited modes.
  8670. */
  8671. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  8672. PIPE_CONF_CHECK_I(gmch_pfit.control);
  8673. /* pfit ratios are autocomputed by the hw on gen4+ */
  8674. if (INTEL_INFO(dev)->gen < 4)
  8675. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  8676. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  8677. }
  8678. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  8679. if (current_config->pch_pfit.enabled) {
  8680. PIPE_CONF_CHECK_I(pch_pfit.pos);
  8681. PIPE_CONF_CHECK_I(pch_pfit.size);
  8682. }
  8683. /* BDW+ don't expose a synchronous way to read the state */
  8684. if (IS_HASWELL(dev))
  8685. PIPE_CONF_CHECK_I(ips_enabled);
  8686. PIPE_CONF_CHECK_I(double_wide);
  8687. PIPE_CONF_CHECK_X(ddi_pll_sel);
  8688. PIPE_CONF_CHECK_I(shared_dpll);
  8689. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  8690. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  8691. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  8692. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  8693. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  8694. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  8695. PIPE_CONF_CHECK_I(pipe_bpp);
  8696. PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
  8697. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  8698. #undef PIPE_CONF_CHECK_X
  8699. #undef PIPE_CONF_CHECK_I
  8700. #undef PIPE_CONF_CHECK_I_ALT
  8701. #undef PIPE_CONF_CHECK_FLAGS
  8702. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  8703. #undef PIPE_CONF_QUIRK
  8704. return true;
  8705. }
  8706. static void
  8707. check_connector_state(struct drm_device *dev)
  8708. {
  8709. struct intel_connector *connector;
  8710. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8711. base.head) {
  8712. /* This also checks the encoder/connector hw state with the
  8713. * ->get_hw_state callbacks. */
  8714. intel_connector_check_state(connector);
  8715. WARN(&connector->new_encoder->base != connector->base.encoder,
  8716. "connector's staged encoder doesn't match current encoder\n");
  8717. }
  8718. }
  8719. static void
  8720. check_encoder_state(struct drm_device *dev)
  8721. {
  8722. struct intel_encoder *encoder;
  8723. struct intel_connector *connector;
  8724. for_each_intel_encoder(dev, encoder) {
  8725. bool enabled = false;
  8726. bool active = false;
  8727. enum pipe pipe, tracked_pipe;
  8728. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  8729. encoder->base.base.id,
  8730. encoder->base.name);
  8731. WARN(&encoder->new_crtc->base != encoder->base.crtc,
  8732. "encoder's stage crtc doesn't match current crtc\n");
  8733. WARN(encoder->connectors_active && !encoder->base.crtc,
  8734. "encoder's active_connectors set, but no crtc\n");
  8735. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8736. base.head) {
  8737. if (connector->base.encoder != &encoder->base)
  8738. continue;
  8739. enabled = true;
  8740. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  8741. active = true;
  8742. }
  8743. /*
  8744. * for MST connectors if we unplug the connector is gone
  8745. * away but the encoder is still connected to a crtc
  8746. * until a modeset happens in response to the hotplug.
  8747. */
  8748. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  8749. continue;
  8750. WARN(!!encoder->base.crtc != enabled,
  8751. "encoder's enabled state mismatch "
  8752. "(expected %i, found %i)\n",
  8753. !!encoder->base.crtc, enabled);
  8754. WARN(active && !encoder->base.crtc,
  8755. "active encoder with no crtc\n");
  8756. WARN(encoder->connectors_active != active,
  8757. "encoder's computed active state doesn't match tracked active state "
  8758. "(expected %i, found %i)\n", active, encoder->connectors_active);
  8759. active = encoder->get_hw_state(encoder, &pipe);
  8760. WARN(active != encoder->connectors_active,
  8761. "encoder's hw state doesn't match sw tracking "
  8762. "(expected %i, found %i)\n",
  8763. encoder->connectors_active, active);
  8764. if (!encoder->base.crtc)
  8765. continue;
  8766. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  8767. WARN(active && pipe != tracked_pipe,
  8768. "active encoder's pipe doesn't match"
  8769. "(expected %i, found %i)\n",
  8770. tracked_pipe, pipe);
  8771. }
  8772. }
  8773. static void
  8774. check_crtc_state(struct drm_device *dev)
  8775. {
  8776. struct drm_i915_private *dev_priv = dev->dev_private;
  8777. struct intel_crtc *crtc;
  8778. struct intel_encoder *encoder;
  8779. struct intel_crtc_config pipe_config;
  8780. for_each_intel_crtc(dev, crtc) {
  8781. bool enabled = false;
  8782. bool active = false;
  8783. memset(&pipe_config, 0, sizeof(pipe_config));
  8784. DRM_DEBUG_KMS("[CRTC:%d]\n",
  8785. crtc->base.base.id);
  8786. WARN(crtc->active && !crtc->base.enabled,
  8787. "active crtc, but not enabled in sw tracking\n");
  8788. for_each_intel_encoder(dev, encoder) {
  8789. if (encoder->base.crtc != &crtc->base)
  8790. continue;
  8791. enabled = true;
  8792. if (encoder->connectors_active)
  8793. active = true;
  8794. }
  8795. WARN(active != crtc->active,
  8796. "crtc's computed active state doesn't match tracked active state "
  8797. "(expected %i, found %i)\n", active, crtc->active);
  8798. WARN(enabled != crtc->base.enabled,
  8799. "crtc's computed enabled state doesn't match tracked enabled state "
  8800. "(expected %i, found %i)\n", enabled, crtc->base.enabled);
  8801. active = dev_priv->display.get_pipe_config(crtc,
  8802. &pipe_config);
  8803. /* hw state is inconsistent with the pipe quirk */
  8804. if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  8805. (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  8806. active = crtc->active;
  8807. for_each_intel_encoder(dev, encoder) {
  8808. enum pipe pipe;
  8809. if (encoder->base.crtc != &crtc->base)
  8810. continue;
  8811. if (encoder->get_hw_state(encoder, &pipe))
  8812. encoder->get_config(encoder, &pipe_config);
  8813. }
  8814. WARN(crtc->active != active,
  8815. "crtc active state doesn't match with hw state "
  8816. "(expected %i, found %i)\n", crtc->active, active);
  8817. if (active &&
  8818. !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
  8819. WARN(1, "pipe state doesn't match!\n");
  8820. intel_dump_pipe_config(crtc, &pipe_config,
  8821. "[hw state]");
  8822. intel_dump_pipe_config(crtc, &crtc->config,
  8823. "[sw state]");
  8824. }
  8825. }
  8826. }
  8827. static void
  8828. check_shared_dpll_state(struct drm_device *dev)
  8829. {
  8830. struct drm_i915_private *dev_priv = dev->dev_private;
  8831. struct intel_crtc *crtc;
  8832. struct intel_dpll_hw_state dpll_hw_state;
  8833. int i;
  8834. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  8835. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  8836. int enabled_crtcs = 0, active_crtcs = 0;
  8837. bool active;
  8838. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  8839. DRM_DEBUG_KMS("%s\n", pll->name);
  8840. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  8841. WARN(pll->active > pll->refcount,
  8842. "more active pll users than references: %i vs %i\n",
  8843. pll->active, pll->refcount);
  8844. WARN(pll->active && !pll->on,
  8845. "pll in active use but not on in sw tracking\n");
  8846. WARN(pll->on && !pll->active,
  8847. "pll in on but not on in use in sw tracking\n");
  8848. WARN(pll->on != active,
  8849. "pll on state mismatch (expected %i, found %i)\n",
  8850. pll->on, active);
  8851. for_each_intel_crtc(dev, crtc) {
  8852. if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
  8853. enabled_crtcs++;
  8854. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  8855. active_crtcs++;
  8856. }
  8857. WARN(pll->active != active_crtcs,
  8858. "pll active crtcs mismatch (expected %i, found %i)\n",
  8859. pll->active, active_crtcs);
  8860. WARN(pll->refcount != enabled_crtcs,
  8861. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  8862. pll->refcount, enabled_crtcs);
  8863. WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
  8864. sizeof(dpll_hw_state)),
  8865. "pll hw state mismatch\n");
  8866. }
  8867. }
  8868. void
  8869. intel_modeset_check_state(struct drm_device *dev)
  8870. {
  8871. check_connector_state(dev);
  8872. check_encoder_state(dev);
  8873. check_crtc_state(dev);
  8874. check_shared_dpll_state(dev);
  8875. }
  8876. void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
  8877. int dotclock)
  8878. {
  8879. /*
  8880. * FDI already provided one idea for the dotclock.
  8881. * Yell if the encoder disagrees.
  8882. */
  8883. WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
  8884. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  8885. pipe_config->adjusted_mode.crtc_clock, dotclock);
  8886. }
  8887. static void update_scanline_offset(struct intel_crtc *crtc)
  8888. {
  8889. struct drm_device *dev = crtc->base.dev;
  8890. /*
  8891. * The scanline counter increments at the leading edge of hsync.
  8892. *
  8893. * On most platforms it starts counting from vtotal-1 on the
  8894. * first active line. That means the scanline counter value is
  8895. * always one less than what we would expect. Ie. just after
  8896. * start of vblank, which also occurs at start of hsync (on the
  8897. * last active line), the scanline counter will read vblank_start-1.
  8898. *
  8899. * On gen2 the scanline counter starts counting from 1 instead
  8900. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  8901. * to keep the value positive), instead of adding one.
  8902. *
  8903. * On HSW+ the behaviour of the scanline counter depends on the output
  8904. * type. For DP ports it behaves like most other platforms, but on HDMI
  8905. * there's an extra 1 line difference. So we need to add two instead of
  8906. * one to the value.
  8907. */
  8908. if (IS_GEN2(dev)) {
  8909. const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
  8910. int vtotal;
  8911. vtotal = mode->crtc_vtotal;
  8912. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  8913. vtotal /= 2;
  8914. crtc->scanline_offset = vtotal - 1;
  8915. } else if (HAS_DDI(dev) &&
  8916. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  8917. crtc->scanline_offset = 2;
  8918. } else
  8919. crtc->scanline_offset = 1;
  8920. }
  8921. static int __intel_set_mode(struct drm_crtc *crtc,
  8922. struct drm_display_mode *mode,
  8923. int x, int y, struct drm_framebuffer *fb)
  8924. {
  8925. struct drm_device *dev = crtc->dev;
  8926. struct drm_i915_private *dev_priv = dev->dev_private;
  8927. struct drm_display_mode *saved_mode;
  8928. struct intel_crtc_config *pipe_config = NULL;
  8929. struct intel_crtc *intel_crtc;
  8930. unsigned disable_pipes, prepare_pipes, modeset_pipes;
  8931. int ret = 0;
  8932. saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
  8933. if (!saved_mode)
  8934. return -ENOMEM;
  8935. intel_modeset_affected_pipes(crtc, &modeset_pipes,
  8936. &prepare_pipes, &disable_pipes);
  8937. *saved_mode = crtc->mode;
  8938. /* Hack: Because we don't (yet) support global modeset on multiple
  8939. * crtcs, we don't keep track of the new mode for more than one crtc.
  8940. * Hence simply check whether any bit is set in modeset_pipes in all the
  8941. * pieces of code that are not yet converted to deal with mutliple crtcs
  8942. * changing their mode at the same time. */
  8943. if (modeset_pipes) {
  8944. pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
  8945. if (IS_ERR(pipe_config)) {
  8946. ret = PTR_ERR(pipe_config);
  8947. pipe_config = NULL;
  8948. goto out;
  8949. }
  8950. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  8951. "[modeset]");
  8952. to_intel_crtc(crtc)->new_config = pipe_config;
  8953. }
  8954. /*
  8955. * See if the config requires any additional preparation, e.g.
  8956. * to adjust global state with pipes off. We need to do this
  8957. * here so we can get the modeset_pipe updated config for the new
  8958. * mode set on this crtc. For other crtcs we need to use the
  8959. * adjusted_mode bits in the crtc directly.
  8960. */
  8961. if (IS_VALLEYVIEW(dev)) {
  8962. valleyview_modeset_global_pipes(dev, &prepare_pipes);
  8963. /* may have added more to prepare_pipes than we should */
  8964. prepare_pipes &= ~disable_pipes;
  8965. }
  8966. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  8967. intel_crtc_disable(&intel_crtc->base);
  8968. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  8969. if (intel_crtc->base.enabled)
  8970. dev_priv->display.crtc_disable(&intel_crtc->base);
  8971. }
  8972. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  8973. * to set it here already despite that we pass it down the callchain.
  8974. */
  8975. if (modeset_pipes) {
  8976. crtc->mode = *mode;
  8977. /* mode_set/enable/disable functions rely on a correct pipe
  8978. * config. */
  8979. to_intel_crtc(crtc)->config = *pipe_config;
  8980. to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
  8981. /*
  8982. * Calculate and store various constants which
  8983. * are later needed by vblank and swap-completion
  8984. * timestamping. They are derived from true hwmode.
  8985. */
  8986. drm_calc_timestamping_constants(crtc,
  8987. &pipe_config->adjusted_mode);
  8988. }
  8989. /* Only after disabling all output pipelines that will be changed can we
  8990. * update the the output configuration. */
  8991. intel_modeset_update_state(dev, prepare_pipes);
  8992. if (dev_priv->display.modeset_global_resources)
  8993. dev_priv->display.modeset_global_resources(dev);
  8994. /* Set up the DPLL and any encoders state that needs to adjust or depend
  8995. * on the DPLL.
  8996. */
  8997. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  8998. struct drm_framebuffer *old_fb = crtc->primary->fb;
  8999. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
  9000. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9001. mutex_lock(&dev->struct_mutex);
  9002. ret = intel_pin_and_fence_fb_obj(dev,
  9003. obj,
  9004. NULL);
  9005. if (ret != 0) {
  9006. DRM_ERROR("pin & fence failed\n");
  9007. mutex_unlock(&dev->struct_mutex);
  9008. goto done;
  9009. }
  9010. if (old_fb)
  9011. intel_unpin_fb_obj(old_obj);
  9012. i915_gem_track_fb(old_obj, obj,
  9013. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
  9014. mutex_unlock(&dev->struct_mutex);
  9015. crtc->primary->fb = fb;
  9016. crtc->x = x;
  9017. crtc->y = y;
  9018. ret = dev_priv->display.crtc_mode_set(intel_crtc, x, y, fb);
  9019. if (ret)
  9020. goto done;
  9021. }
  9022. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  9023. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9024. update_scanline_offset(intel_crtc);
  9025. dev_priv->display.crtc_enable(&intel_crtc->base);
  9026. }
  9027. /* FIXME: add subpixel order */
  9028. done:
  9029. if (ret && crtc->enabled)
  9030. crtc->mode = *saved_mode;
  9031. out:
  9032. kfree(pipe_config);
  9033. kfree(saved_mode);
  9034. return ret;
  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. int ret;
  9041. ret = __intel_set_mode(crtc, mode, x, y, fb);
  9042. if (ret == 0)
  9043. intel_modeset_check_state(crtc->dev);
  9044. return ret;
  9045. }
  9046. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  9047. {
  9048. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
  9049. }
  9050. #undef for_each_intel_crtc_masked
  9051. static void intel_set_config_free(struct intel_set_config *config)
  9052. {
  9053. if (!config)
  9054. return;
  9055. kfree(config->save_connector_encoders);
  9056. kfree(config->save_encoder_crtcs);
  9057. kfree(config->save_crtc_enabled);
  9058. kfree(config);
  9059. }
  9060. static int intel_set_config_save_state(struct drm_device *dev,
  9061. struct intel_set_config *config)
  9062. {
  9063. struct drm_crtc *crtc;
  9064. struct drm_encoder *encoder;
  9065. struct drm_connector *connector;
  9066. int count;
  9067. config->save_crtc_enabled =
  9068. kcalloc(dev->mode_config.num_crtc,
  9069. sizeof(bool), GFP_KERNEL);
  9070. if (!config->save_crtc_enabled)
  9071. return -ENOMEM;
  9072. config->save_encoder_crtcs =
  9073. kcalloc(dev->mode_config.num_encoder,
  9074. sizeof(struct drm_crtc *), GFP_KERNEL);
  9075. if (!config->save_encoder_crtcs)
  9076. return -ENOMEM;
  9077. config->save_connector_encoders =
  9078. kcalloc(dev->mode_config.num_connector,
  9079. sizeof(struct drm_encoder *), GFP_KERNEL);
  9080. if (!config->save_connector_encoders)
  9081. return -ENOMEM;
  9082. /* Copy data. Note that driver private data is not affected.
  9083. * Should anything bad happen only the expected state is
  9084. * restored, not the drivers personal bookkeeping.
  9085. */
  9086. count = 0;
  9087. for_each_crtc(dev, crtc) {
  9088. config->save_crtc_enabled[count++] = crtc->enabled;
  9089. }
  9090. count = 0;
  9091. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  9092. config->save_encoder_crtcs[count++] = encoder->crtc;
  9093. }
  9094. count = 0;
  9095. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  9096. config->save_connector_encoders[count++] = connector->encoder;
  9097. }
  9098. return 0;
  9099. }
  9100. static void intel_set_config_restore_state(struct drm_device *dev,
  9101. struct intel_set_config *config)
  9102. {
  9103. struct intel_crtc *crtc;
  9104. struct intel_encoder *encoder;
  9105. struct intel_connector *connector;
  9106. int count;
  9107. count = 0;
  9108. for_each_intel_crtc(dev, crtc) {
  9109. crtc->new_enabled = config->save_crtc_enabled[count++];
  9110. if (crtc->new_enabled)
  9111. crtc->new_config = &crtc->config;
  9112. else
  9113. crtc->new_config = NULL;
  9114. }
  9115. count = 0;
  9116. for_each_intel_encoder(dev, encoder) {
  9117. encoder->new_crtc =
  9118. to_intel_crtc(config->save_encoder_crtcs[count++]);
  9119. }
  9120. count = 0;
  9121. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  9122. connector->new_encoder =
  9123. to_intel_encoder(config->save_connector_encoders[count++]);
  9124. }
  9125. }
  9126. static bool
  9127. is_crtc_connector_off(struct drm_mode_set *set)
  9128. {
  9129. int i;
  9130. if (set->num_connectors == 0)
  9131. return false;
  9132. if (WARN_ON(set->connectors == NULL))
  9133. return false;
  9134. for (i = 0; i < set->num_connectors; i++)
  9135. if (set->connectors[i]->encoder &&
  9136. set->connectors[i]->encoder->crtc == set->crtc &&
  9137. set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
  9138. return true;
  9139. return false;
  9140. }
  9141. static void
  9142. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  9143. struct intel_set_config *config)
  9144. {
  9145. /* We should be able to check here if the fb has the same properties
  9146. * and then just flip_or_move it */
  9147. if (is_crtc_connector_off(set)) {
  9148. config->mode_changed = true;
  9149. } else if (set->crtc->primary->fb != set->fb) {
  9150. /*
  9151. * If we have no fb, we can only flip as long as the crtc is
  9152. * active, otherwise we need a full mode set. The crtc may
  9153. * be active if we've only disabled the primary plane, or
  9154. * in fastboot situations.
  9155. */
  9156. if (set->crtc->primary->fb == NULL) {
  9157. struct intel_crtc *intel_crtc =
  9158. to_intel_crtc(set->crtc);
  9159. if (intel_crtc->active) {
  9160. DRM_DEBUG_KMS("crtc has no fb, will flip\n");
  9161. config->fb_changed = true;
  9162. } else {
  9163. DRM_DEBUG_KMS("inactive crtc, full mode set\n");
  9164. config->mode_changed = true;
  9165. }
  9166. } else if (set->fb == NULL) {
  9167. config->mode_changed = true;
  9168. } else if (set->fb->pixel_format !=
  9169. set->crtc->primary->fb->pixel_format) {
  9170. config->mode_changed = true;
  9171. } else {
  9172. config->fb_changed = true;
  9173. }
  9174. }
  9175. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  9176. config->fb_changed = true;
  9177. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  9178. DRM_DEBUG_KMS("modes are different, full mode set\n");
  9179. drm_mode_debug_printmodeline(&set->crtc->mode);
  9180. drm_mode_debug_printmodeline(set->mode);
  9181. config->mode_changed = true;
  9182. }
  9183. DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
  9184. set->crtc->base.id, config->mode_changed, config->fb_changed);
  9185. }
  9186. static int
  9187. intel_modeset_stage_output_state(struct drm_device *dev,
  9188. struct drm_mode_set *set,
  9189. struct intel_set_config *config)
  9190. {
  9191. struct intel_connector *connector;
  9192. struct intel_encoder *encoder;
  9193. struct intel_crtc *crtc;
  9194. int ro;
  9195. /* The upper layers ensure that we either disable a crtc or have a list
  9196. * of connectors. For paranoia, double-check this. */
  9197. WARN_ON(!set->fb && (set->num_connectors != 0));
  9198. WARN_ON(set->fb && (set->num_connectors == 0));
  9199. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9200. base.head) {
  9201. /* Otherwise traverse passed in connector list and get encoders
  9202. * for them. */
  9203. for (ro = 0; ro < set->num_connectors; ro++) {
  9204. if (set->connectors[ro] == &connector->base) {
  9205. connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
  9206. break;
  9207. }
  9208. }
  9209. /* If we disable the crtc, disable all its connectors. Also, if
  9210. * the connector is on the changing crtc but not on the new
  9211. * connector list, disable it. */
  9212. if ((!set->fb || ro == set->num_connectors) &&
  9213. connector->base.encoder &&
  9214. connector->base.encoder->crtc == set->crtc) {
  9215. connector->new_encoder = NULL;
  9216. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  9217. connector->base.base.id,
  9218. connector->base.name);
  9219. }
  9220. if (&connector->new_encoder->base != connector->base.encoder) {
  9221. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  9222. config->mode_changed = true;
  9223. }
  9224. }
  9225. /* connector->new_encoder is now updated for all connectors. */
  9226. /* Update crtc of enabled connectors. */
  9227. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9228. base.head) {
  9229. struct drm_crtc *new_crtc;
  9230. if (!connector->new_encoder)
  9231. continue;
  9232. new_crtc = connector->new_encoder->base.crtc;
  9233. for (ro = 0; ro < set->num_connectors; ro++) {
  9234. if (set->connectors[ro] == &connector->base)
  9235. new_crtc = set->crtc;
  9236. }
  9237. /* Make sure the new CRTC will work with the encoder */
  9238. if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
  9239. new_crtc)) {
  9240. return -EINVAL;
  9241. }
  9242. connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
  9243. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  9244. connector->base.base.id,
  9245. connector->base.name,
  9246. new_crtc->base.id);
  9247. }
  9248. /* Check for any encoders that needs to be disabled. */
  9249. for_each_intel_encoder(dev, encoder) {
  9250. int num_connectors = 0;
  9251. list_for_each_entry(connector,
  9252. &dev->mode_config.connector_list,
  9253. base.head) {
  9254. if (connector->new_encoder == encoder) {
  9255. WARN_ON(!connector->new_encoder->new_crtc);
  9256. num_connectors++;
  9257. }
  9258. }
  9259. if (num_connectors == 0)
  9260. encoder->new_crtc = NULL;
  9261. else if (num_connectors > 1)
  9262. return -EINVAL;
  9263. /* Only now check for crtc changes so we don't miss encoders
  9264. * that will be disabled. */
  9265. if (&encoder->new_crtc->base != encoder->base.crtc) {
  9266. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  9267. config->mode_changed = true;
  9268. }
  9269. }
  9270. /* Now we've also updated encoder->new_crtc for all encoders. */
  9271. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9272. base.head) {
  9273. if (connector->new_encoder)
  9274. if (connector->new_encoder != connector->encoder)
  9275. connector->encoder = connector->new_encoder;
  9276. }
  9277. for_each_intel_crtc(dev, crtc) {
  9278. crtc->new_enabled = false;
  9279. for_each_intel_encoder(dev, encoder) {
  9280. if (encoder->new_crtc == crtc) {
  9281. crtc->new_enabled = true;
  9282. break;
  9283. }
  9284. }
  9285. if (crtc->new_enabled != crtc->base.enabled) {
  9286. DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
  9287. crtc->new_enabled ? "en" : "dis");
  9288. config->mode_changed = true;
  9289. }
  9290. if (crtc->new_enabled)
  9291. crtc->new_config = &crtc->config;
  9292. else
  9293. crtc->new_config = NULL;
  9294. }
  9295. return 0;
  9296. }
  9297. static void disable_crtc_nofb(struct intel_crtc *crtc)
  9298. {
  9299. struct drm_device *dev = crtc->base.dev;
  9300. struct intel_encoder *encoder;
  9301. struct intel_connector *connector;
  9302. DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
  9303. pipe_name(crtc->pipe));
  9304. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  9305. if (connector->new_encoder &&
  9306. connector->new_encoder->new_crtc == crtc)
  9307. connector->new_encoder = NULL;
  9308. }
  9309. for_each_intel_encoder(dev, encoder) {
  9310. if (encoder->new_crtc == crtc)
  9311. encoder->new_crtc = NULL;
  9312. }
  9313. crtc->new_enabled = false;
  9314. crtc->new_config = NULL;
  9315. }
  9316. static int intel_crtc_set_config(struct drm_mode_set *set)
  9317. {
  9318. struct drm_device *dev;
  9319. struct drm_mode_set save_set;
  9320. struct intel_set_config *config;
  9321. int ret;
  9322. BUG_ON(!set);
  9323. BUG_ON(!set->crtc);
  9324. BUG_ON(!set->crtc->helper_private);
  9325. /* Enforce sane interface api - has been abused by the fb helper. */
  9326. BUG_ON(!set->mode && set->fb);
  9327. BUG_ON(set->fb && set->num_connectors == 0);
  9328. if (set->fb) {
  9329. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  9330. set->crtc->base.id, set->fb->base.id,
  9331. (int)set->num_connectors, set->x, set->y);
  9332. } else {
  9333. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  9334. }
  9335. dev = set->crtc->dev;
  9336. ret = -ENOMEM;
  9337. config = kzalloc(sizeof(*config), GFP_KERNEL);
  9338. if (!config)
  9339. goto out_config;
  9340. ret = intel_set_config_save_state(dev, config);
  9341. if (ret)
  9342. goto out_config;
  9343. save_set.crtc = set->crtc;
  9344. save_set.mode = &set->crtc->mode;
  9345. save_set.x = set->crtc->x;
  9346. save_set.y = set->crtc->y;
  9347. save_set.fb = set->crtc->primary->fb;
  9348. /* Compute whether we need a full modeset, only an fb base update or no
  9349. * change at all. In the future we might also check whether only the
  9350. * mode changed, e.g. for LVDS where we only change the panel fitter in
  9351. * such cases. */
  9352. intel_set_config_compute_mode_changes(set, config);
  9353. ret = intel_modeset_stage_output_state(dev, set, config);
  9354. if (ret)
  9355. goto fail;
  9356. if (config->mode_changed) {
  9357. ret = intel_set_mode(set->crtc, set->mode,
  9358. set->x, set->y, set->fb);
  9359. } else if (config->fb_changed) {
  9360. struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
  9361. intel_crtc_wait_for_pending_flips(set->crtc);
  9362. ret = intel_pipe_set_base(set->crtc,
  9363. set->x, set->y, set->fb);
  9364. /*
  9365. * We need to make sure the primary plane is re-enabled if it
  9366. * has previously been turned off.
  9367. */
  9368. if (!intel_crtc->primary_enabled && ret == 0) {
  9369. WARN_ON(!intel_crtc->active);
  9370. intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
  9371. }
  9372. /*
  9373. * In the fastboot case this may be our only check of the
  9374. * state after boot. It would be better to only do it on
  9375. * the first update, but we don't have a nice way of doing that
  9376. * (and really, set_config isn't used much for high freq page
  9377. * flipping, so increasing its cost here shouldn't be a big
  9378. * deal).
  9379. */
  9380. if (i915.fastboot && ret == 0)
  9381. intel_modeset_check_state(set->crtc->dev);
  9382. }
  9383. if (ret) {
  9384. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  9385. set->crtc->base.id, ret);
  9386. fail:
  9387. intel_set_config_restore_state(dev, config);
  9388. /*
  9389. * HACK: if the pipe was on, but we didn't have a framebuffer,
  9390. * force the pipe off to avoid oopsing in the modeset code
  9391. * due to fb==NULL. This should only happen during boot since
  9392. * we don't yet reconstruct the FB from the hardware state.
  9393. */
  9394. if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
  9395. disable_crtc_nofb(to_intel_crtc(save_set.crtc));
  9396. /* Try to restore the config */
  9397. if (config->mode_changed &&
  9398. intel_set_mode(save_set.crtc, save_set.mode,
  9399. save_set.x, save_set.y, save_set.fb))
  9400. DRM_ERROR("failed to restore config after modeset failure\n");
  9401. }
  9402. out_config:
  9403. intel_set_config_free(config);
  9404. return ret;
  9405. }
  9406. static const struct drm_crtc_funcs intel_crtc_funcs = {
  9407. .gamma_set = intel_crtc_gamma_set,
  9408. .set_config = intel_crtc_set_config,
  9409. .destroy = intel_crtc_destroy,
  9410. .page_flip = intel_crtc_page_flip,
  9411. };
  9412. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  9413. struct intel_shared_dpll *pll,
  9414. struct intel_dpll_hw_state *hw_state)
  9415. {
  9416. uint32_t val;
  9417. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  9418. return false;
  9419. val = I915_READ(PCH_DPLL(pll->id));
  9420. hw_state->dpll = val;
  9421. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  9422. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  9423. return val & DPLL_VCO_ENABLE;
  9424. }
  9425. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  9426. struct intel_shared_dpll *pll)
  9427. {
  9428. I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
  9429. I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
  9430. }
  9431. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  9432. struct intel_shared_dpll *pll)
  9433. {
  9434. /* PCH refclock must be enabled first */
  9435. ibx_assert_pch_refclk_enabled(dev_priv);
  9436. I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
  9437. /* Wait for the clocks to stabilize. */
  9438. POSTING_READ(PCH_DPLL(pll->id));
  9439. udelay(150);
  9440. /* The pixel multiplier can only be updated once the
  9441. * DPLL is enabled and the clocks are stable.
  9442. *
  9443. * So write it again.
  9444. */
  9445. I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
  9446. POSTING_READ(PCH_DPLL(pll->id));
  9447. udelay(200);
  9448. }
  9449. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  9450. struct intel_shared_dpll *pll)
  9451. {
  9452. struct drm_device *dev = dev_priv->dev;
  9453. struct intel_crtc *crtc;
  9454. /* Make sure no transcoder isn't still depending on us. */
  9455. for_each_intel_crtc(dev, crtc) {
  9456. if (intel_crtc_to_shared_dpll(crtc) == pll)
  9457. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  9458. }
  9459. I915_WRITE(PCH_DPLL(pll->id), 0);
  9460. POSTING_READ(PCH_DPLL(pll->id));
  9461. udelay(200);
  9462. }
  9463. static char *ibx_pch_dpll_names[] = {
  9464. "PCH DPLL A",
  9465. "PCH DPLL B",
  9466. };
  9467. static void ibx_pch_dpll_init(struct drm_device *dev)
  9468. {
  9469. struct drm_i915_private *dev_priv = dev->dev_private;
  9470. int i;
  9471. dev_priv->num_shared_dpll = 2;
  9472. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9473. dev_priv->shared_dplls[i].id = i;
  9474. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  9475. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  9476. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  9477. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  9478. dev_priv->shared_dplls[i].get_hw_state =
  9479. ibx_pch_dpll_get_hw_state;
  9480. }
  9481. }
  9482. static void intel_shared_dpll_init(struct drm_device *dev)
  9483. {
  9484. struct drm_i915_private *dev_priv = dev->dev_private;
  9485. if (HAS_DDI(dev))
  9486. intel_ddi_pll_init(dev);
  9487. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  9488. ibx_pch_dpll_init(dev);
  9489. else
  9490. dev_priv->num_shared_dpll = 0;
  9491. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  9492. }
  9493. static int
  9494. intel_primary_plane_disable(struct drm_plane *plane)
  9495. {
  9496. struct drm_device *dev = plane->dev;
  9497. struct intel_crtc *intel_crtc;
  9498. if (!plane->fb)
  9499. return 0;
  9500. BUG_ON(!plane->crtc);
  9501. intel_crtc = to_intel_crtc(plane->crtc);
  9502. /*
  9503. * Even though we checked plane->fb above, it's still possible that
  9504. * the primary plane has been implicitly disabled because the crtc
  9505. * coordinates given weren't visible, or because we detected
  9506. * that it was 100% covered by a sprite plane. Or, the CRTC may be
  9507. * off and we've set a fb, but haven't actually turned on the CRTC yet.
  9508. * In either case, we need to unpin the FB and let the fb pointer get
  9509. * updated, but otherwise we don't need to touch the hardware.
  9510. */
  9511. if (!intel_crtc->primary_enabled)
  9512. goto disable_unpin;
  9513. intel_crtc_wait_for_pending_flips(plane->crtc);
  9514. intel_disable_primary_hw_plane(plane, plane->crtc);
  9515. disable_unpin:
  9516. mutex_lock(&dev->struct_mutex);
  9517. i915_gem_track_fb(intel_fb_obj(plane->fb), NULL,
  9518. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
  9519. intel_unpin_fb_obj(intel_fb_obj(plane->fb));
  9520. mutex_unlock(&dev->struct_mutex);
  9521. plane->fb = NULL;
  9522. return 0;
  9523. }
  9524. static int
  9525. intel_check_primary_plane(struct drm_plane *plane,
  9526. struct intel_plane_state *state)
  9527. {
  9528. struct drm_crtc *crtc = state->crtc;
  9529. struct drm_framebuffer *fb = state->fb;
  9530. struct drm_rect *dest = &state->dst;
  9531. struct drm_rect *src = &state->src;
  9532. const struct drm_rect *clip = &state->clip;
  9533. return drm_plane_helper_check_update(plane, crtc, fb,
  9534. src, dest, clip,
  9535. DRM_PLANE_HELPER_NO_SCALING,
  9536. DRM_PLANE_HELPER_NO_SCALING,
  9537. false, true, &state->visible);
  9538. }
  9539. static int
  9540. intel_prepare_primary_plane(struct drm_plane *plane,
  9541. struct intel_plane_state *state)
  9542. {
  9543. struct drm_crtc *crtc = state->crtc;
  9544. struct drm_framebuffer *fb = state->fb;
  9545. struct drm_device *dev = crtc->dev;
  9546. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9547. enum pipe pipe = intel_crtc->pipe;
  9548. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9549. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  9550. int ret;
  9551. intel_crtc_wait_for_pending_flips(crtc);
  9552. if (intel_crtc_has_pending_flip(crtc)) {
  9553. DRM_ERROR("pipe is still busy with an old pageflip\n");
  9554. return -EBUSY;
  9555. }
  9556. if (old_obj != obj) {
  9557. mutex_lock(&dev->struct_mutex);
  9558. ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
  9559. if (ret == 0)
  9560. i915_gem_track_fb(old_obj, obj,
  9561. INTEL_FRONTBUFFER_PRIMARY(pipe));
  9562. mutex_unlock(&dev->struct_mutex);
  9563. if (ret != 0) {
  9564. DRM_DEBUG_KMS("pin & fence failed\n");
  9565. return ret;
  9566. }
  9567. }
  9568. return 0;
  9569. }
  9570. static void
  9571. intel_commit_primary_plane(struct drm_plane *plane,
  9572. struct intel_plane_state *state)
  9573. {
  9574. struct drm_crtc *crtc = state->crtc;
  9575. struct drm_framebuffer *fb = state->fb;
  9576. struct drm_device *dev = crtc->dev;
  9577. struct drm_i915_private *dev_priv = dev->dev_private;
  9578. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9579. enum pipe pipe = intel_crtc->pipe;
  9580. struct drm_framebuffer *old_fb = plane->fb;
  9581. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9582. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  9583. struct intel_plane *intel_plane = to_intel_plane(plane);
  9584. struct drm_rect *src = &state->src;
  9585. crtc->primary->fb = fb;
  9586. crtc->x = src->x1;
  9587. crtc->y = src->y1;
  9588. intel_plane->crtc_x = state->orig_dst.x1;
  9589. intel_plane->crtc_y = state->orig_dst.y1;
  9590. intel_plane->crtc_w = drm_rect_width(&state->orig_dst);
  9591. intel_plane->crtc_h = drm_rect_height(&state->orig_dst);
  9592. intel_plane->src_x = state->orig_src.x1;
  9593. intel_plane->src_y = state->orig_src.y1;
  9594. intel_plane->src_w = drm_rect_width(&state->orig_src);
  9595. intel_plane->src_h = drm_rect_height(&state->orig_src);
  9596. intel_plane->obj = obj;
  9597. if (intel_crtc->active) {
  9598. /*
  9599. * FBC does not work on some platforms for rotated
  9600. * planes, so disable it when rotation is not 0 and
  9601. * update it when rotation is set back to 0.
  9602. *
  9603. * FIXME: This is redundant with the fbc update done in
  9604. * the primary plane enable function except that that
  9605. * one is done too late. We eventually need to unify
  9606. * this.
  9607. */
  9608. if (intel_crtc->primary_enabled &&
  9609. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  9610. dev_priv->fbc.plane == intel_crtc->plane &&
  9611. intel_plane->rotation != BIT(DRM_ROTATE_0)) {
  9612. intel_disable_fbc(dev);
  9613. }
  9614. if (state->visible) {
  9615. bool was_enabled = intel_crtc->primary_enabled;
  9616. /* FIXME: kill this fastboot hack */
  9617. intel_update_pipe_size(intel_crtc);
  9618. intel_crtc->primary_enabled = true;
  9619. dev_priv->display.update_primary_plane(crtc, plane->fb,
  9620. crtc->x, crtc->y);
  9621. /*
  9622. * BDW signals flip done immediately if the plane
  9623. * is disabled, even if the plane enable is already
  9624. * armed to occur at the next vblank :(
  9625. */
  9626. if (IS_BROADWELL(dev) && !was_enabled)
  9627. intel_wait_for_vblank(dev, intel_crtc->pipe);
  9628. } else {
  9629. /*
  9630. * If clipping results in a non-visible primary plane,
  9631. * we'll disable the primary plane. Note that this is
  9632. * a bit different than what happens if userspace
  9633. * explicitly disables the plane by passing fb=0
  9634. * because plane->fb still gets set and pinned.
  9635. */
  9636. intel_disable_primary_hw_plane(plane, crtc);
  9637. }
  9638. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  9639. mutex_lock(&dev->struct_mutex);
  9640. intel_update_fbc(dev);
  9641. mutex_unlock(&dev->struct_mutex);
  9642. }
  9643. if (old_fb && old_fb != fb) {
  9644. if (intel_crtc->active)
  9645. intel_wait_for_vblank(dev, intel_crtc->pipe);
  9646. mutex_lock(&dev->struct_mutex);
  9647. intel_unpin_fb_obj(old_obj);
  9648. mutex_unlock(&dev->struct_mutex);
  9649. }
  9650. }
  9651. static int
  9652. intel_primary_plane_setplane(struct drm_plane *plane, struct drm_crtc *crtc,
  9653. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  9654. unsigned int crtc_w, unsigned int crtc_h,
  9655. uint32_t src_x, uint32_t src_y,
  9656. uint32_t src_w, uint32_t src_h)
  9657. {
  9658. struct intel_plane_state state;
  9659. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9660. int ret;
  9661. state.crtc = crtc;
  9662. state.fb = fb;
  9663. /* sample coordinates in 16.16 fixed point */
  9664. state.src.x1 = src_x;
  9665. state.src.x2 = src_x + src_w;
  9666. state.src.y1 = src_y;
  9667. state.src.y2 = src_y + src_h;
  9668. /* integer pixels */
  9669. state.dst.x1 = crtc_x;
  9670. state.dst.x2 = crtc_x + crtc_w;
  9671. state.dst.y1 = crtc_y;
  9672. state.dst.y2 = crtc_y + crtc_h;
  9673. state.clip.x1 = 0;
  9674. state.clip.y1 = 0;
  9675. state.clip.x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0;
  9676. state.clip.y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0;
  9677. state.orig_src = state.src;
  9678. state.orig_dst = state.dst;
  9679. ret = intel_check_primary_plane(plane, &state);
  9680. if (ret)
  9681. return ret;
  9682. ret = intel_prepare_primary_plane(plane, &state);
  9683. if (ret)
  9684. return ret;
  9685. intel_commit_primary_plane(plane, &state);
  9686. return 0;
  9687. }
  9688. /* Common destruction function for both primary and cursor planes */
  9689. static void intel_plane_destroy(struct drm_plane *plane)
  9690. {
  9691. struct intel_plane *intel_plane = to_intel_plane(plane);
  9692. drm_plane_cleanup(plane);
  9693. kfree(intel_plane);
  9694. }
  9695. static const struct drm_plane_funcs intel_primary_plane_funcs = {
  9696. .update_plane = intel_primary_plane_setplane,
  9697. .disable_plane = intel_primary_plane_disable,
  9698. .destroy = intel_plane_destroy,
  9699. .set_property = intel_plane_set_property
  9700. };
  9701. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  9702. int pipe)
  9703. {
  9704. struct intel_plane *primary;
  9705. const uint32_t *intel_primary_formats;
  9706. int num_formats;
  9707. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  9708. if (primary == NULL)
  9709. return NULL;
  9710. primary->can_scale = false;
  9711. primary->max_downscale = 1;
  9712. primary->pipe = pipe;
  9713. primary->plane = pipe;
  9714. primary->rotation = BIT(DRM_ROTATE_0);
  9715. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  9716. primary->plane = !pipe;
  9717. if (INTEL_INFO(dev)->gen <= 3) {
  9718. intel_primary_formats = intel_primary_formats_gen2;
  9719. num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
  9720. } else {
  9721. intel_primary_formats = intel_primary_formats_gen4;
  9722. num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
  9723. }
  9724. drm_universal_plane_init(dev, &primary->base, 0,
  9725. &intel_primary_plane_funcs,
  9726. intel_primary_formats, num_formats,
  9727. DRM_PLANE_TYPE_PRIMARY);
  9728. if (INTEL_INFO(dev)->gen >= 4) {
  9729. if (!dev->mode_config.rotation_property)
  9730. dev->mode_config.rotation_property =
  9731. drm_mode_create_rotation_property(dev,
  9732. BIT(DRM_ROTATE_0) |
  9733. BIT(DRM_ROTATE_180));
  9734. if (dev->mode_config.rotation_property)
  9735. drm_object_attach_property(&primary->base.base,
  9736. dev->mode_config.rotation_property,
  9737. primary->rotation);
  9738. }
  9739. return &primary->base;
  9740. }
  9741. static int
  9742. intel_cursor_plane_disable(struct drm_plane *plane)
  9743. {
  9744. if (!plane->fb)
  9745. return 0;
  9746. BUG_ON(!plane->crtc);
  9747. return intel_crtc_cursor_set_obj(plane->crtc, NULL, 0, 0);
  9748. }
  9749. static int
  9750. intel_check_cursor_plane(struct drm_plane *plane,
  9751. struct intel_plane_state *state)
  9752. {
  9753. struct drm_crtc *crtc = state->crtc;
  9754. struct drm_device *dev = crtc->dev;
  9755. struct drm_framebuffer *fb = state->fb;
  9756. struct drm_rect *dest = &state->dst;
  9757. struct drm_rect *src = &state->src;
  9758. const struct drm_rect *clip = &state->clip;
  9759. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9760. int crtc_w, crtc_h;
  9761. unsigned stride;
  9762. int ret;
  9763. ret = drm_plane_helper_check_update(plane, crtc, fb,
  9764. src, dest, clip,
  9765. DRM_PLANE_HELPER_NO_SCALING,
  9766. DRM_PLANE_HELPER_NO_SCALING,
  9767. true, true, &state->visible);
  9768. if (ret)
  9769. return ret;
  9770. /* if we want to turn off the cursor ignore width and height */
  9771. if (!obj)
  9772. return 0;
  9773. /* Check for which cursor types we support */
  9774. crtc_w = drm_rect_width(&state->orig_dst);
  9775. crtc_h = drm_rect_height(&state->orig_dst);
  9776. if (!cursor_size_ok(dev, crtc_w, crtc_h)) {
  9777. DRM_DEBUG("Cursor dimension not supported\n");
  9778. return -EINVAL;
  9779. }
  9780. stride = roundup_pow_of_two(crtc_w) * 4;
  9781. if (obj->base.size < stride * crtc_h) {
  9782. DRM_DEBUG_KMS("buffer is too small\n");
  9783. return -ENOMEM;
  9784. }
  9785. if (fb == crtc->cursor->fb)
  9786. return 0;
  9787. /* we only need to pin inside GTT if cursor is non-phy */
  9788. mutex_lock(&dev->struct_mutex);
  9789. if (!INTEL_INFO(dev)->cursor_needs_physical && obj->tiling_mode) {
  9790. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  9791. ret = -EINVAL;
  9792. }
  9793. mutex_unlock(&dev->struct_mutex);
  9794. return ret;
  9795. }
  9796. static int
  9797. intel_commit_cursor_plane(struct drm_plane *plane,
  9798. struct intel_plane_state *state)
  9799. {
  9800. struct drm_crtc *crtc = state->crtc;
  9801. struct drm_framebuffer *fb = state->fb;
  9802. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9803. struct intel_plane *intel_plane = to_intel_plane(plane);
  9804. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  9805. struct drm_i915_gem_object *obj = intel_fb->obj;
  9806. int crtc_w, crtc_h;
  9807. crtc->cursor_x = state->orig_dst.x1;
  9808. crtc->cursor_y = state->orig_dst.y1;
  9809. intel_plane->crtc_x = state->orig_dst.x1;
  9810. intel_plane->crtc_y = state->orig_dst.y1;
  9811. intel_plane->crtc_w = drm_rect_width(&state->orig_dst);
  9812. intel_plane->crtc_h = drm_rect_height(&state->orig_dst);
  9813. intel_plane->src_x = state->orig_src.x1;
  9814. intel_plane->src_y = state->orig_src.y1;
  9815. intel_plane->src_w = drm_rect_width(&state->orig_src);
  9816. intel_plane->src_h = drm_rect_height(&state->orig_src);
  9817. intel_plane->obj = obj;
  9818. if (fb != crtc->cursor->fb) {
  9819. crtc_w = drm_rect_width(&state->orig_dst);
  9820. crtc_h = drm_rect_height(&state->orig_dst);
  9821. return intel_crtc_cursor_set_obj(crtc, obj, crtc_w, crtc_h);
  9822. } else {
  9823. intel_crtc_update_cursor(crtc, state->visible);
  9824. intel_frontbuffer_flip(crtc->dev,
  9825. INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe));
  9826. return 0;
  9827. }
  9828. }
  9829. static int
  9830. intel_cursor_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
  9831. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  9832. unsigned int crtc_w, unsigned int crtc_h,
  9833. uint32_t src_x, uint32_t src_y,
  9834. uint32_t src_w, uint32_t src_h)
  9835. {
  9836. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9837. struct intel_plane_state state;
  9838. int ret;
  9839. state.crtc = crtc;
  9840. state.fb = fb;
  9841. /* sample coordinates in 16.16 fixed point */
  9842. state.src.x1 = src_x;
  9843. state.src.x2 = src_x + src_w;
  9844. state.src.y1 = src_y;
  9845. state.src.y2 = src_y + src_h;
  9846. /* integer pixels */
  9847. state.dst.x1 = crtc_x;
  9848. state.dst.x2 = crtc_x + crtc_w;
  9849. state.dst.y1 = crtc_y;
  9850. state.dst.y2 = crtc_y + crtc_h;
  9851. state.clip.x1 = 0;
  9852. state.clip.y1 = 0;
  9853. state.clip.x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0;
  9854. state.clip.y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0;
  9855. state.orig_src = state.src;
  9856. state.orig_dst = state.dst;
  9857. ret = intel_check_cursor_plane(plane, &state);
  9858. if (ret)
  9859. return ret;
  9860. return intel_commit_cursor_plane(plane, &state);
  9861. }
  9862. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  9863. .update_plane = intel_cursor_plane_update,
  9864. .disable_plane = intel_cursor_plane_disable,
  9865. .destroy = intel_plane_destroy,
  9866. .set_property = intel_plane_set_property,
  9867. };
  9868. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  9869. int pipe)
  9870. {
  9871. struct intel_plane *cursor;
  9872. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  9873. if (cursor == NULL)
  9874. return NULL;
  9875. cursor->can_scale = false;
  9876. cursor->max_downscale = 1;
  9877. cursor->pipe = pipe;
  9878. cursor->plane = pipe;
  9879. cursor->rotation = BIT(DRM_ROTATE_0);
  9880. drm_universal_plane_init(dev, &cursor->base, 0,
  9881. &intel_cursor_plane_funcs,
  9882. intel_cursor_formats,
  9883. ARRAY_SIZE(intel_cursor_formats),
  9884. DRM_PLANE_TYPE_CURSOR);
  9885. if (INTEL_INFO(dev)->gen >= 4) {
  9886. if (!dev->mode_config.rotation_property)
  9887. dev->mode_config.rotation_property =
  9888. drm_mode_create_rotation_property(dev,
  9889. BIT(DRM_ROTATE_0) |
  9890. BIT(DRM_ROTATE_180));
  9891. if (dev->mode_config.rotation_property)
  9892. drm_object_attach_property(&cursor->base.base,
  9893. dev->mode_config.rotation_property,
  9894. cursor->rotation);
  9895. }
  9896. return &cursor->base;
  9897. }
  9898. static void intel_crtc_init(struct drm_device *dev, int pipe)
  9899. {
  9900. struct drm_i915_private *dev_priv = dev->dev_private;
  9901. struct intel_crtc *intel_crtc;
  9902. struct drm_plane *primary = NULL;
  9903. struct drm_plane *cursor = NULL;
  9904. int i, ret;
  9905. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  9906. if (intel_crtc == NULL)
  9907. return;
  9908. primary = intel_primary_plane_create(dev, pipe);
  9909. if (!primary)
  9910. goto fail;
  9911. cursor = intel_cursor_plane_create(dev, pipe);
  9912. if (!cursor)
  9913. goto fail;
  9914. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  9915. cursor, &intel_crtc_funcs);
  9916. if (ret)
  9917. goto fail;
  9918. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  9919. for (i = 0; i < 256; i++) {
  9920. intel_crtc->lut_r[i] = i;
  9921. intel_crtc->lut_g[i] = i;
  9922. intel_crtc->lut_b[i] = i;
  9923. }
  9924. /*
  9925. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  9926. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  9927. */
  9928. intel_crtc->pipe = pipe;
  9929. intel_crtc->plane = pipe;
  9930. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  9931. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  9932. intel_crtc->plane = !pipe;
  9933. }
  9934. intel_crtc->cursor_base = ~0;
  9935. intel_crtc->cursor_cntl = ~0;
  9936. intel_crtc->cursor_size = ~0;
  9937. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  9938. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  9939. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  9940. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  9941. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  9942. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  9943. return;
  9944. fail:
  9945. if (primary)
  9946. drm_plane_cleanup(primary);
  9947. if (cursor)
  9948. drm_plane_cleanup(cursor);
  9949. kfree(intel_crtc);
  9950. }
  9951. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  9952. {
  9953. struct drm_encoder *encoder = connector->base.encoder;
  9954. struct drm_device *dev = connector->base.dev;
  9955. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  9956. if (!encoder)
  9957. return INVALID_PIPE;
  9958. return to_intel_crtc(encoder->crtc)->pipe;
  9959. }
  9960. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  9961. struct drm_file *file)
  9962. {
  9963. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  9964. struct drm_crtc *drmmode_crtc;
  9965. struct intel_crtc *crtc;
  9966. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  9967. return -ENODEV;
  9968. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  9969. if (!drmmode_crtc) {
  9970. DRM_ERROR("no such CRTC id\n");
  9971. return -ENOENT;
  9972. }
  9973. crtc = to_intel_crtc(drmmode_crtc);
  9974. pipe_from_crtc_id->pipe = crtc->pipe;
  9975. return 0;
  9976. }
  9977. static int intel_encoder_clones(struct intel_encoder *encoder)
  9978. {
  9979. struct drm_device *dev = encoder->base.dev;
  9980. struct intel_encoder *source_encoder;
  9981. int index_mask = 0;
  9982. int entry = 0;
  9983. for_each_intel_encoder(dev, source_encoder) {
  9984. if (encoders_cloneable(encoder, source_encoder))
  9985. index_mask |= (1 << entry);
  9986. entry++;
  9987. }
  9988. return index_mask;
  9989. }
  9990. static bool has_edp_a(struct drm_device *dev)
  9991. {
  9992. struct drm_i915_private *dev_priv = dev->dev_private;
  9993. if (!IS_MOBILE(dev))
  9994. return false;
  9995. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  9996. return false;
  9997. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  9998. return false;
  9999. return true;
  10000. }
  10001. const char *intel_output_name(int output)
  10002. {
  10003. static const char *names[] = {
  10004. [INTEL_OUTPUT_UNUSED] = "Unused",
  10005. [INTEL_OUTPUT_ANALOG] = "Analog",
  10006. [INTEL_OUTPUT_DVO] = "DVO",
  10007. [INTEL_OUTPUT_SDVO] = "SDVO",
  10008. [INTEL_OUTPUT_LVDS] = "LVDS",
  10009. [INTEL_OUTPUT_TVOUT] = "TV",
  10010. [INTEL_OUTPUT_HDMI] = "HDMI",
  10011. [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
  10012. [INTEL_OUTPUT_EDP] = "eDP",
  10013. [INTEL_OUTPUT_DSI] = "DSI",
  10014. [INTEL_OUTPUT_UNKNOWN] = "Unknown",
  10015. };
  10016. if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
  10017. return "Invalid";
  10018. return names[output];
  10019. }
  10020. static bool intel_crt_present(struct drm_device *dev)
  10021. {
  10022. struct drm_i915_private *dev_priv = dev->dev_private;
  10023. if (INTEL_INFO(dev)->gen >= 9)
  10024. return false;
  10025. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  10026. return false;
  10027. if (IS_CHERRYVIEW(dev))
  10028. return false;
  10029. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  10030. return false;
  10031. return true;
  10032. }
  10033. static void intel_setup_outputs(struct drm_device *dev)
  10034. {
  10035. struct drm_i915_private *dev_priv = dev->dev_private;
  10036. struct intel_encoder *encoder;
  10037. bool dpd_is_edp = false;
  10038. intel_lvds_init(dev);
  10039. if (intel_crt_present(dev))
  10040. intel_crt_init(dev);
  10041. if (HAS_DDI(dev)) {
  10042. int found;
  10043. /* Haswell uses DDI functions to detect digital outputs */
  10044. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  10045. /* DDI A only supports eDP */
  10046. if (found)
  10047. intel_ddi_init(dev, PORT_A);
  10048. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  10049. * register */
  10050. found = I915_READ(SFUSE_STRAP);
  10051. if (found & SFUSE_STRAP_DDIB_DETECTED)
  10052. intel_ddi_init(dev, PORT_B);
  10053. if (found & SFUSE_STRAP_DDIC_DETECTED)
  10054. intel_ddi_init(dev, PORT_C);
  10055. if (found & SFUSE_STRAP_DDID_DETECTED)
  10056. intel_ddi_init(dev, PORT_D);
  10057. } else if (HAS_PCH_SPLIT(dev)) {
  10058. int found;
  10059. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  10060. if (has_edp_a(dev))
  10061. intel_dp_init(dev, DP_A, PORT_A);
  10062. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  10063. /* PCH SDVOB multiplex with HDMIB */
  10064. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  10065. if (!found)
  10066. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  10067. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  10068. intel_dp_init(dev, PCH_DP_B, PORT_B);
  10069. }
  10070. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  10071. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  10072. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  10073. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  10074. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  10075. intel_dp_init(dev, PCH_DP_C, PORT_C);
  10076. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  10077. intel_dp_init(dev, PCH_DP_D, PORT_D);
  10078. } else if (IS_VALLEYVIEW(dev)) {
  10079. /*
  10080. * The DP_DETECTED bit is the latched state of the DDC
  10081. * SDA pin at boot. However since eDP doesn't require DDC
  10082. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  10083. * eDP ports may have been muxed to an alternate function.
  10084. * Thus we can't rely on the DP_DETECTED bit alone to detect
  10085. * eDP ports. Consult the VBT as well as DP_DETECTED to
  10086. * detect eDP ports.
  10087. */
  10088. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED)
  10089. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  10090. PORT_B);
  10091. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  10092. intel_dp_is_edp(dev, PORT_B))
  10093. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  10094. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED)
  10095. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  10096. PORT_C);
  10097. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  10098. intel_dp_is_edp(dev, PORT_C))
  10099. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  10100. if (IS_CHERRYVIEW(dev)) {
  10101. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  10102. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  10103. PORT_D);
  10104. /* eDP not supported on port D, so don't check VBT */
  10105. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  10106. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  10107. }
  10108. intel_dsi_init(dev);
  10109. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  10110. bool found = false;
  10111. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10112. DRM_DEBUG_KMS("probing SDVOB\n");
  10113. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  10114. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  10115. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  10116. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  10117. }
  10118. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  10119. intel_dp_init(dev, DP_B, PORT_B);
  10120. }
  10121. /* Before G4X SDVOC doesn't have its own detect register */
  10122. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10123. DRM_DEBUG_KMS("probing SDVOC\n");
  10124. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  10125. }
  10126. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  10127. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  10128. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  10129. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  10130. }
  10131. if (SUPPORTS_INTEGRATED_DP(dev))
  10132. intel_dp_init(dev, DP_C, PORT_C);
  10133. }
  10134. if (SUPPORTS_INTEGRATED_DP(dev) &&
  10135. (I915_READ(DP_D) & DP_DETECTED))
  10136. intel_dp_init(dev, DP_D, PORT_D);
  10137. } else if (IS_GEN2(dev))
  10138. intel_dvo_init(dev);
  10139. if (SUPPORTS_TV(dev))
  10140. intel_tv_init(dev);
  10141. intel_edp_psr_init(dev);
  10142. for_each_intel_encoder(dev, encoder) {
  10143. encoder->base.possible_crtcs = encoder->crtc_mask;
  10144. encoder->base.possible_clones =
  10145. intel_encoder_clones(encoder);
  10146. }
  10147. intel_init_pch_refclk(dev);
  10148. drm_helper_move_panel_connectors_to_head(dev);
  10149. }
  10150. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  10151. {
  10152. struct drm_device *dev = fb->dev;
  10153. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10154. drm_framebuffer_cleanup(fb);
  10155. mutex_lock(&dev->struct_mutex);
  10156. WARN_ON(!intel_fb->obj->framebuffer_references--);
  10157. drm_gem_object_unreference(&intel_fb->obj->base);
  10158. mutex_unlock(&dev->struct_mutex);
  10159. kfree(intel_fb);
  10160. }
  10161. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  10162. struct drm_file *file,
  10163. unsigned int *handle)
  10164. {
  10165. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10166. struct drm_i915_gem_object *obj = intel_fb->obj;
  10167. return drm_gem_handle_create(file, &obj->base, handle);
  10168. }
  10169. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  10170. .destroy = intel_user_framebuffer_destroy,
  10171. .create_handle = intel_user_framebuffer_create_handle,
  10172. };
  10173. static int intel_framebuffer_init(struct drm_device *dev,
  10174. struct intel_framebuffer *intel_fb,
  10175. struct drm_mode_fb_cmd2 *mode_cmd,
  10176. struct drm_i915_gem_object *obj)
  10177. {
  10178. int aligned_height;
  10179. int pitch_limit;
  10180. int ret;
  10181. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  10182. if (obj->tiling_mode == I915_TILING_Y) {
  10183. DRM_DEBUG("hardware does not support tiling Y\n");
  10184. return -EINVAL;
  10185. }
  10186. if (mode_cmd->pitches[0] & 63) {
  10187. DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
  10188. mode_cmd->pitches[0]);
  10189. return -EINVAL;
  10190. }
  10191. if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
  10192. pitch_limit = 32*1024;
  10193. } else if (INTEL_INFO(dev)->gen >= 4) {
  10194. if (obj->tiling_mode)
  10195. pitch_limit = 16*1024;
  10196. else
  10197. pitch_limit = 32*1024;
  10198. } else if (INTEL_INFO(dev)->gen >= 3) {
  10199. if (obj->tiling_mode)
  10200. pitch_limit = 8*1024;
  10201. else
  10202. pitch_limit = 16*1024;
  10203. } else
  10204. /* XXX DSPC is limited to 4k tiled */
  10205. pitch_limit = 8*1024;
  10206. if (mode_cmd->pitches[0] > pitch_limit) {
  10207. DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
  10208. obj->tiling_mode ? "tiled" : "linear",
  10209. mode_cmd->pitches[0], pitch_limit);
  10210. return -EINVAL;
  10211. }
  10212. if (obj->tiling_mode != I915_TILING_NONE &&
  10213. mode_cmd->pitches[0] != obj->stride) {
  10214. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  10215. mode_cmd->pitches[0], obj->stride);
  10216. return -EINVAL;
  10217. }
  10218. /* Reject formats not supported by any plane early. */
  10219. switch (mode_cmd->pixel_format) {
  10220. case DRM_FORMAT_C8:
  10221. case DRM_FORMAT_RGB565:
  10222. case DRM_FORMAT_XRGB8888:
  10223. case DRM_FORMAT_ARGB8888:
  10224. break;
  10225. case DRM_FORMAT_XRGB1555:
  10226. case DRM_FORMAT_ARGB1555:
  10227. if (INTEL_INFO(dev)->gen > 3) {
  10228. DRM_DEBUG("unsupported pixel format: %s\n",
  10229. drm_get_format_name(mode_cmd->pixel_format));
  10230. return -EINVAL;
  10231. }
  10232. break;
  10233. case DRM_FORMAT_XBGR8888:
  10234. case DRM_FORMAT_ABGR8888:
  10235. case DRM_FORMAT_XRGB2101010:
  10236. case DRM_FORMAT_ARGB2101010:
  10237. case DRM_FORMAT_XBGR2101010:
  10238. case DRM_FORMAT_ABGR2101010:
  10239. if (INTEL_INFO(dev)->gen < 4) {
  10240. DRM_DEBUG("unsupported pixel format: %s\n",
  10241. drm_get_format_name(mode_cmd->pixel_format));
  10242. return -EINVAL;
  10243. }
  10244. break;
  10245. case DRM_FORMAT_YUYV:
  10246. case DRM_FORMAT_UYVY:
  10247. case DRM_FORMAT_YVYU:
  10248. case DRM_FORMAT_VYUY:
  10249. if (INTEL_INFO(dev)->gen < 5) {
  10250. DRM_DEBUG("unsupported pixel format: %s\n",
  10251. drm_get_format_name(mode_cmd->pixel_format));
  10252. return -EINVAL;
  10253. }
  10254. break;
  10255. default:
  10256. DRM_DEBUG("unsupported pixel format: %s\n",
  10257. drm_get_format_name(mode_cmd->pixel_format));
  10258. return -EINVAL;
  10259. }
  10260. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  10261. if (mode_cmd->offsets[0] != 0)
  10262. return -EINVAL;
  10263. aligned_height = intel_align_height(dev, mode_cmd->height,
  10264. obj->tiling_mode);
  10265. /* FIXME drm helper for size checks (especially planar formats)? */
  10266. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  10267. return -EINVAL;
  10268. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  10269. intel_fb->obj = obj;
  10270. intel_fb->obj->framebuffer_references++;
  10271. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  10272. if (ret) {
  10273. DRM_ERROR("framebuffer init failed %d\n", ret);
  10274. return ret;
  10275. }
  10276. return 0;
  10277. }
  10278. static struct drm_framebuffer *
  10279. intel_user_framebuffer_create(struct drm_device *dev,
  10280. struct drm_file *filp,
  10281. struct drm_mode_fb_cmd2 *mode_cmd)
  10282. {
  10283. struct drm_i915_gem_object *obj;
  10284. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  10285. mode_cmd->handles[0]));
  10286. if (&obj->base == NULL)
  10287. return ERR_PTR(-ENOENT);
  10288. return intel_framebuffer_create(dev, mode_cmd, obj);
  10289. }
  10290. #ifndef CONFIG_DRM_I915_FBDEV
  10291. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  10292. {
  10293. }
  10294. #endif
  10295. static const struct drm_mode_config_funcs intel_mode_funcs = {
  10296. .fb_create = intel_user_framebuffer_create,
  10297. .output_poll_changed = intel_fbdev_output_poll_changed,
  10298. };
  10299. /* Set up chip specific display functions */
  10300. static void intel_init_display(struct drm_device *dev)
  10301. {
  10302. struct drm_i915_private *dev_priv = dev->dev_private;
  10303. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  10304. dev_priv->display.find_dpll = g4x_find_best_dpll;
  10305. else if (IS_CHERRYVIEW(dev))
  10306. dev_priv->display.find_dpll = chv_find_best_dpll;
  10307. else if (IS_VALLEYVIEW(dev))
  10308. dev_priv->display.find_dpll = vlv_find_best_dpll;
  10309. else if (IS_PINEVIEW(dev))
  10310. dev_priv->display.find_dpll = pnv_find_best_dpll;
  10311. else
  10312. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  10313. if (HAS_DDI(dev)) {
  10314. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  10315. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  10316. dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
  10317. dev_priv->display.crtc_enable = haswell_crtc_enable;
  10318. dev_priv->display.crtc_disable = haswell_crtc_disable;
  10319. dev_priv->display.off = ironlake_crtc_off;
  10320. if (INTEL_INFO(dev)->gen >= 9)
  10321. dev_priv->display.update_primary_plane =
  10322. skylake_update_primary_plane;
  10323. else
  10324. dev_priv->display.update_primary_plane =
  10325. ironlake_update_primary_plane;
  10326. } else if (HAS_PCH_SPLIT(dev)) {
  10327. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  10328. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  10329. dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
  10330. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  10331. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  10332. dev_priv->display.off = ironlake_crtc_off;
  10333. dev_priv->display.update_primary_plane =
  10334. ironlake_update_primary_plane;
  10335. } else if (IS_VALLEYVIEW(dev)) {
  10336. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10337. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  10338. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  10339. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  10340. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10341. dev_priv->display.off = i9xx_crtc_off;
  10342. dev_priv->display.update_primary_plane =
  10343. i9xx_update_primary_plane;
  10344. } else {
  10345. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10346. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  10347. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  10348. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  10349. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10350. dev_priv->display.off = i9xx_crtc_off;
  10351. dev_priv->display.update_primary_plane =
  10352. i9xx_update_primary_plane;
  10353. }
  10354. /* Returns the core display clock speed */
  10355. if (IS_VALLEYVIEW(dev))
  10356. dev_priv->display.get_display_clock_speed =
  10357. valleyview_get_display_clock_speed;
  10358. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  10359. dev_priv->display.get_display_clock_speed =
  10360. i945_get_display_clock_speed;
  10361. else if (IS_I915G(dev))
  10362. dev_priv->display.get_display_clock_speed =
  10363. i915_get_display_clock_speed;
  10364. else if (IS_I945GM(dev) || IS_845G(dev))
  10365. dev_priv->display.get_display_clock_speed =
  10366. i9xx_misc_get_display_clock_speed;
  10367. else if (IS_PINEVIEW(dev))
  10368. dev_priv->display.get_display_clock_speed =
  10369. pnv_get_display_clock_speed;
  10370. else if (IS_I915GM(dev))
  10371. dev_priv->display.get_display_clock_speed =
  10372. i915gm_get_display_clock_speed;
  10373. else if (IS_I865G(dev))
  10374. dev_priv->display.get_display_clock_speed =
  10375. i865_get_display_clock_speed;
  10376. else if (IS_I85X(dev))
  10377. dev_priv->display.get_display_clock_speed =
  10378. i855_get_display_clock_speed;
  10379. else /* 852, 830 */
  10380. dev_priv->display.get_display_clock_speed =
  10381. i830_get_display_clock_speed;
  10382. if (IS_GEN5(dev)) {
  10383. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  10384. } else if (IS_GEN6(dev)) {
  10385. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  10386. dev_priv->display.modeset_global_resources =
  10387. snb_modeset_global_resources;
  10388. } else if (IS_IVYBRIDGE(dev)) {
  10389. /* FIXME: detect B0+ stepping and use auto training */
  10390. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  10391. dev_priv->display.modeset_global_resources =
  10392. ivb_modeset_global_resources;
  10393. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  10394. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  10395. dev_priv->display.modeset_global_resources =
  10396. haswell_modeset_global_resources;
  10397. } else if (IS_VALLEYVIEW(dev)) {
  10398. dev_priv->display.modeset_global_resources =
  10399. valleyview_modeset_global_resources;
  10400. } else if (INTEL_INFO(dev)->gen >= 9) {
  10401. dev_priv->display.modeset_global_resources =
  10402. haswell_modeset_global_resources;
  10403. }
  10404. /* Default just returns -ENODEV to indicate unsupported */
  10405. dev_priv->display.queue_flip = intel_default_queue_flip;
  10406. switch (INTEL_INFO(dev)->gen) {
  10407. case 2:
  10408. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  10409. break;
  10410. case 3:
  10411. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  10412. break;
  10413. case 4:
  10414. case 5:
  10415. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  10416. break;
  10417. case 6:
  10418. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  10419. break;
  10420. case 7:
  10421. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  10422. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  10423. break;
  10424. }
  10425. intel_panel_init_backlight_funcs(dev);
  10426. mutex_init(&dev_priv->pps_mutex);
  10427. }
  10428. /*
  10429. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  10430. * resume, or other times. This quirk makes sure that's the case for
  10431. * affected systems.
  10432. */
  10433. static void quirk_pipea_force(struct drm_device *dev)
  10434. {
  10435. struct drm_i915_private *dev_priv = dev->dev_private;
  10436. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  10437. DRM_INFO("applying pipe a force quirk\n");
  10438. }
  10439. static void quirk_pipeb_force(struct drm_device *dev)
  10440. {
  10441. struct drm_i915_private *dev_priv = dev->dev_private;
  10442. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  10443. DRM_INFO("applying pipe b force quirk\n");
  10444. }
  10445. /*
  10446. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  10447. */
  10448. static void quirk_ssc_force_disable(struct drm_device *dev)
  10449. {
  10450. struct drm_i915_private *dev_priv = dev->dev_private;
  10451. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  10452. DRM_INFO("applying lvds SSC disable quirk\n");
  10453. }
  10454. /*
  10455. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  10456. * brightness value
  10457. */
  10458. static void quirk_invert_brightness(struct drm_device *dev)
  10459. {
  10460. struct drm_i915_private *dev_priv = dev->dev_private;
  10461. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  10462. DRM_INFO("applying inverted panel brightness quirk\n");
  10463. }
  10464. /* Some VBT's incorrectly indicate no backlight is present */
  10465. static void quirk_backlight_present(struct drm_device *dev)
  10466. {
  10467. struct drm_i915_private *dev_priv = dev->dev_private;
  10468. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  10469. DRM_INFO("applying backlight present quirk\n");
  10470. }
  10471. struct intel_quirk {
  10472. int device;
  10473. int subsystem_vendor;
  10474. int subsystem_device;
  10475. void (*hook)(struct drm_device *dev);
  10476. };
  10477. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  10478. struct intel_dmi_quirk {
  10479. void (*hook)(struct drm_device *dev);
  10480. const struct dmi_system_id (*dmi_id_list)[];
  10481. };
  10482. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  10483. {
  10484. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  10485. return 1;
  10486. }
  10487. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  10488. {
  10489. .dmi_id_list = &(const struct dmi_system_id[]) {
  10490. {
  10491. .callback = intel_dmi_reverse_brightness,
  10492. .ident = "NCR Corporation",
  10493. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  10494. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  10495. },
  10496. },
  10497. { } /* terminating entry */
  10498. },
  10499. .hook = quirk_invert_brightness,
  10500. },
  10501. };
  10502. static struct intel_quirk intel_quirks[] = {
  10503. /* HP Mini needs pipe A force quirk (LP: #322104) */
  10504. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  10505. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  10506. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  10507. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  10508. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  10509. /* 830 needs to leave pipe A & dpll A up */
  10510. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  10511. /* 830 needs to leave pipe B & dpll B up */
  10512. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  10513. /* Lenovo U160 cannot use SSC on LVDS */
  10514. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  10515. /* Sony Vaio Y cannot use SSC on LVDS */
  10516. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  10517. /* Acer Aspire 5734Z must invert backlight brightness */
  10518. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  10519. /* Acer/eMachines G725 */
  10520. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  10521. /* Acer/eMachines e725 */
  10522. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  10523. /* Acer/Packard Bell NCL20 */
  10524. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  10525. /* Acer Aspire 4736Z */
  10526. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  10527. /* Acer Aspire 5336 */
  10528. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  10529. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  10530. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  10531. /* Acer C720 Chromebook (Core i3 4005U) */
  10532. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  10533. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  10534. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  10535. /* HP Chromebook 14 (Celeron 2955U) */
  10536. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  10537. };
  10538. static void intel_init_quirks(struct drm_device *dev)
  10539. {
  10540. struct pci_dev *d = dev->pdev;
  10541. int i;
  10542. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  10543. struct intel_quirk *q = &intel_quirks[i];
  10544. if (d->device == q->device &&
  10545. (d->subsystem_vendor == q->subsystem_vendor ||
  10546. q->subsystem_vendor == PCI_ANY_ID) &&
  10547. (d->subsystem_device == q->subsystem_device ||
  10548. q->subsystem_device == PCI_ANY_ID))
  10549. q->hook(dev);
  10550. }
  10551. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  10552. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  10553. intel_dmi_quirks[i].hook(dev);
  10554. }
  10555. }
  10556. /* Disable the VGA plane that we never use */
  10557. static void i915_disable_vga(struct drm_device *dev)
  10558. {
  10559. struct drm_i915_private *dev_priv = dev->dev_private;
  10560. u8 sr1;
  10561. u32 vga_reg = i915_vgacntrl_reg(dev);
  10562. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  10563. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  10564. outb(SR01, VGA_SR_INDEX);
  10565. sr1 = inb(VGA_SR_DATA);
  10566. outb(sr1 | 1<<5, VGA_SR_DATA);
  10567. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  10568. udelay(300);
  10569. /*
  10570. * Fujitsu-Siemens Lifebook S6010 (830) has problems resuming
  10571. * from S3 without preserving (some of?) the other bits.
  10572. */
  10573. I915_WRITE(vga_reg, dev_priv->bios_vgacntr | VGA_DISP_DISABLE);
  10574. POSTING_READ(vga_reg);
  10575. }
  10576. void intel_modeset_init_hw(struct drm_device *dev)
  10577. {
  10578. intel_prepare_ddi(dev);
  10579. if (IS_VALLEYVIEW(dev))
  10580. vlv_update_cdclk(dev);
  10581. intel_init_clock_gating(dev);
  10582. intel_enable_gt_powersave(dev);
  10583. }
  10584. void intel_modeset_init(struct drm_device *dev)
  10585. {
  10586. struct drm_i915_private *dev_priv = dev->dev_private;
  10587. int sprite, ret;
  10588. enum pipe pipe;
  10589. struct intel_crtc *crtc;
  10590. drm_mode_config_init(dev);
  10591. dev->mode_config.min_width = 0;
  10592. dev->mode_config.min_height = 0;
  10593. dev->mode_config.preferred_depth = 24;
  10594. dev->mode_config.prefer_shadow = 1;
  10595. dev->mode_config.funcs = &intel_mode_funcs;
  10596. intel_init_quirks(dev);
  10597. intel_init_pm(dev);
  10598. if (INTEL_INFO(dev)->num_pipes == 0)
  10599. return;
  10600. intel_init_display(dev);
  10601. intel_init_audio(dev);
  10602. if (IS_GEN2(dev)) {
  10603. dev->mode_config.max_width = 2048;
  10604. dev->mode_config.max_height = 2048;
  10605. } else if (IS_GEN3(dev)) {
  10606. dev->mode_config.max_width = 4096;
  10607. dev->mode_config.max_height = 4096;
  10608. } else {
  10609. dev->mode_config.max_width = 8192;
  10610. dev->mode_config.max_height = 8192;
  10611. }
  10612. if (IS_845G(dev) || IS_I865G(dev)) {
  10613. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  10614. dev->mode_config.cursor_height = 1023;
  10615. } else if (IS_GEN2(dev)) {
  10616. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  10617. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  10618. } else {
  10619. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  10620. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  10621. }
  10622. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  10623. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  10624. INTEL_INFO(dev)->num_pipes,
  10625. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  10626. for_each_pipe(dev_priv, pipe) {
  10627. intel_crtc_init(dev, pipe);
  10628. for_each_sprite(pipe, sprite) {
  10629. ret = intel_plane_init(dev, pipe, sprite);
  10630. if (ret)
  10631. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  10632. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  10633. }
  10634. }
  10635. intel_init_dpio(dev);
  10636. intel_shared_dpll_init(dev);
  10637. /* save the BIOS value before clobbering it */
  10638. dev_priv->bios_vgacntr = I915_READ(i915_vgacntrl_reg(dev));
  10639. /* Just disable it once at startup */
  10640. i915_disable_vga(dev);
  10641. intel_setup_outputs(dev);
  10642. /* Just in case the BIOS is doing something questionable. */
  10643. intel_disable_fbc(dev);
  10644. drm_modeset_lock_all(dev);
  10645. intel_modeset_setup_hw_state(dev, false);
  10646. drm_modeset_unlock_all(dev);
  10647. for_each_intel_crtc(dev, crtc) {
  10648. if (!crtc->active)
  10649. continue;
  10650. /*
  10651. * Note that reserving the BIOS fb up front prevents us
  10652. * from stuffing other stolen allocations like the ring
  10653. * on top. This prevents some ugliness at boot time, and
  10654. * can even allow for smooth boot transitions if the BIOS
  10655. * fb is large enough for the active pipe configuration.
  10656. */
  10657. if (dev_priv->display.get_plane_config) {
  10658. dev_priv->display.get_plane_config(crtc,
  10659. &crtc->plane_config);
  10660. /*
  10661. * If the fb is shared between multiple heads, we'll
  10662. * just get the first one.
  10663. */
  10664. intel_find_plane_obj(crtc, &crtc->plane_config);
  10665. }
  10666. }
  10667. }
  10668. static void intel_enable_pipe_a(struct drm_device *dev)
  10669. {
  10670. struct intel_connector *connector;
  10671. struct drm_connector *crt = NULL;
  10672. struct intel_load_detect_pipe load_detect_temp;
  10673. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  10674. /* We can't just switch on the pipe A, we need to set things up with a
  10675. * proper mode and output configuration. As a gross hack, enable pipe A
  10676. * by enabling the load detect pipe once. */
  10677. list_for_each_entry(connector,
  10678. &dev->mode_config.connector_list,
  10679. base.head) {
  10680. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  10681. crt = &connector->base;
  10682. break;
  10683. }
  10684. }
  10685. if (!crt)
  10686. return;
  10687. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  10688. intel_release_load_detect_pipe(crt, &load_detect_temp);
  10689. }
  10690. static bool
  10691. intel_check_plane_mapping(struct intel_crtc *crtc)
  10692. {
  10693. struct drm_device *dev = crtc->base.dev;
  10694. struct drm_i915_private *dev_priv = dev->dev_private;
  10695. u32 reg, val;
  10696. if (INTEL_INFO(dev)->num_pipes == 1)
  10697. return true;
  10698. reg = DSPCNTR(!crtc->plane);
  10699. val = I915_READ(reg);
  10700. if ((val & DISPLAY_PLANE_ENABLE) &&
  10701. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  10702. return false;
  10703. return true;
  10704. }
  10705. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  10706. {
  10707. struct drm_device *dev = crtc->base.dev;
  10708. struct drm_i915_private *dev_priv = dev->dev_private;
  10709. u32 reg;
  10710. /* Clear any frame start delays used for debugging left by the BIOS */
  10711. reg = PIPECONF(crtc->config.cpu_transcoder);
  10712. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  10713. /* restore vblank interrupts to correct state */
  10714. if (crtc->active) {
  10715. update_scanline_offset(crtc);
  10716. drm_vblank_on(dev, crtc->pipe);
  10717. } else
  10718. drm_vblank_off(dev, crtc->pipe);
  10719. /* We need to sanitize the plane -> pipe mapping first because this will
  10720. * disable the crtc (and hence change the state) if it is wrong. Note
  10721. * that gen4+ has a fixed plane -> pipe mapping. */
  10722. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  10723. struct intel_connector *connector;
  10724. bool plane;
  10725. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  10726. crtc->base.base.id);
  10727. /* Pipe has the wrong plane attached and the plane is active.
  10728. * Temporarily change the plane mapping and disable everything
  10729. * ... */
  10730. plane = crtc->plane;
  10731. crtc->plane = !plane;
  10732. crtc->primary_enabled = true;
  10733. dev_priv->display.crtc_disable(&crtc->base);
  10734. crtc->plane = plane;
  10735. /* ... and break all links. */
  10736. list_for_each_entry(connector, &dev->mode_config.connector_list,
  10737. base.head) {
  10738. if (connector->encoder->base.crtc != &crtc->base)
  10739. continue;
  10740. connector->base.dpms = DRM_MODE_DPMS_OFF;
  10741. connector->base.encoder = NULL;
  10742. }
  10743. /* multiple connectors may have the same encoder:
  10744. * handle them and break crtc link separately */
  10745. list_for_each_entry(connector, &dev->mode_config.connector_list,
  10746. base.head)
  10747. if (connector->encoder->base.crtc == &crtc->base) {
  10748. connector->encoder->base.crtc = NULL;
  10749. connector->encoder->connectors_active = false;
  10750. }
  10751. WARN_ON(crtc->active);
  10752. crtc->base.enabled = false;
  10753. }
  10754. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  10755. crtc->pipe == PIPE_A && !crtc->active) {
  10756. /* BIOS forgot to enable pipe A, this mostly happens after
  10757. * resume. Force-enable the pipe to fix this, the update_dpms
  10758. * call below we restore the pipe to the right state, but leave
  10759. * the required bits on. */
  10760. intel_enable_pipe_a(dev);
  10761. }
  10762. /* Adjust the state of the output pipe according to whether we
  10763. * have active connectors/encoders. */
  10764. intel_crtc_update_dpms(&crtc->base);
  10765. if (crtc->active != crtc->base.enabled) {
  10766. struct intel_encoder *encoder;
  10767. /* This can happen either due to bugs in the get_hw_state
  10768. * functions or because the pipe is force-enabled due to the
  10769. * pipe A quirk. */
  10770. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  10771. crtc->base.base.id,
  10772. crtc->base.enabled ? "enabled" : "disabled",
  10773. crtc->active ? "enabled" : "disabled");
  10774. crtc->base.enabled = crtc->active;
  10775. /* Because we only establish the connector -> encoder ->
  10776. * crtc links if something is active, this means the
  10777. * crtc is now deactivated. Break the links. connector
  10778. * -> encoder links are only establish when things are
  10779. * actually up, hence no need to break them. */
  10780. WARN_ON(crtc->active);
  10781. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  10782. WARN_ON(encoder->connectors_active);
  10783. encoder->base.crtc = NULL;
  10784. }
  10785. }
  10786. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  10787. /*
  10788. * We start out with underrun reporting disabled to avoid races.
  10789. * For correct bookkeeping mark this on active crtcs.
  10790. *
  10791. * Also on gmch platforms we dont have any hardware bits to
  10792. * disable the underrun reporting. Which means we need to start
  10793. * out with underrun reporting disabled also on inactive pipes,
  10794. * since otherwise we'll complain about the garbage we read when
  10795. * e.g. coming up after runtime pm.
  10796. *
  10797. * No protection against concurrent access is required - at
  10798. * worst a fifo underrun happens which also sets this to false.
  10799. */
  10800. crtc->cpu_fifo_underrun_disabled = true;
  10801. crtc->pch_fifo_underrun_disabled = true;
  10802. }
  10803. }
  10804. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  10805. {
  10806. struct intel_connector *connector;
  10807. struct drm_device *dev = encoder->base.dev;
  10808. /* We need to check both for a crtc link (meaning that the
  10809. * encoder is active and trying to read from a pipe) and the
  10810. * pipe itself being active. */
  10811. bool has_active_crtc = encoder->base.crtc &&
  10812. to_intel_crtc(encoder->base.crtc)->active;
  10813. if (encoder->connectors_active && !has_active_crtc) {
  10814. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  10815. encoder->base.base.id,
  10816. encoder->base.name);
  10817. /* Connector is active, but has no active pipe. This is
  10818. * fallout from our resume register restoring. Disable
  10819. * the encoder manually again. */
  10820. if (encoder->base.crtc) {
  10821. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  10822. encoder->base.base.id,
  10823. encoder->base.name);
  10824. encoder->disable(encoder);
  10825. if (encoder->post_disable)
  10826. encoder->post_disable(encoder);
  10827. }
  10828. encoder->base.crtc = NULL;
  10829. encoder->connectors_active = false;
  10830. /* Inconsistent output/port/pipe state happens presumably due to
  10831. * a bug in one of the get_hw_state functions. Or someplace else
  10832. * in our code, like the register restore mess on resume. Clamp
  10833. * things to off as a safer default. */
  10834. list_for_each_entry(connector,
  10835. &dev->mode_config.connector_list,
  10836. base.head) {
  10837. if (connector->encoder != encoder)
  10838. continue;
  10839. connector->base.dpms = DRM_MODE_DPMS_OFF;
  10840. connector->base.encoder = NULL;
  10841. }
  10842. }
  10843. /* Enabled encoders without active connectors will be fixed in
  10844. * the crtc fixup. */
  10845. }
  10846. void i915_redisable_vga_power_on(struct drm_device *dev)
  10847. {
  10848. struct drm_i915_private *dev_priv = dev->dev_private;
  10849. u32 vga_reg = i915_vgacntrl_reg(dev);
  10850. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  10851. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  10852. i915_disable_vga(dev);
  10853. }
  10854. }
  10855. void i915_redisable_vga(struct drm_device *dev)
  10856. {
  10857. struct drm_i915_private *dev_priv = dev->dev_private;
  10858. /* This function can be called both from intel_modeset_setup_hw_state or
  10859. * at a very early point in our resume sequence, where the power well
  10860. * structures are not yet restored. Since this function is at a very
  10861. * paranoid "someone might have enabled VGA while we were not looking"
  10862. * level, just check if the power well is enabled instead of trying to
  10863. * follow the "don't touch the power well if we don't need it" policy
  10864. * the rest of the driver uses. */
  10865. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  10866. return;
  10867. i915_redisable_vga_power_on(dev);
  10868. }
  10869. static bool primary_get_hw_state(struct intel_crtc *crtc)
  10870. {
  10871. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  10872. if (!crtc->active)
  10873. return false;
  10874. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  10875. }
  10876. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  10877. {
  10878. struct drm_i915_private *dev_priv = dev->dev_private;
  10879. enum pipe pipe;
  10880. struct intel_crtc *crtc;
  10881. struct intel_encoder *encoder;
  10882. struct intel_connector *connector;
  10883. int i;
  10884. for_each_intel_crtc(dev, crtc) {
  10885. memset(&crtc->config, 0, sizeof(crtc->config));
  10886. crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  10887. crtc->active = dev_priv->display.get_pipe_config(crtc,
  10888. &crtc->config);
  10889. crtc->base.enabled = crtc->active;
  10890. crtc->primary_enabled = primary_get_hw_state(crtc);
  10891. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  10892. crtc->base.base.id,
  10893. crtc->active ? "enabled" : "disabled");
  10894. }
  10895. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10896. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10897. pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
  10898. pll->active = 0;
  10899. for_each_intel_crtc(dev, crtc) {
  10900. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10901. pll->active++;
  10902. }
  10903. pll->refcount = pll->active;
  10904. DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
  10905. pll->name, pll->refcount, pll->on);
  10906. if (pll->refcount)
  10907. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  10908. }
  10909. for_each_intel_encoder(dev, encoder) {
  10910. pipe = 0;
  10911. if (encoder->get_hw_state(encoder, &pipe)) {
  10912. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  10913. encoder->base.crtc = &crtc->base;
  10914. encoder->get_config(encoder, &crtc->config);
  10915. } else {
  10916. encoder->base.crtc = NULL;
  10917. }
  10918. encoder->connectors_active = false;
  10919. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  10920. encoder->base.base.id,
  10921. encoder->base.name,
  10922. encoder->base.crtc ? "enabled" : "disabled",
  10923. pipe_name(pipe));
  10924. }
  10925. list_for_each_entry(connector, &dev->mode_config.connector_list,
  10926. base.head) {
  10927. if (connector->get_hw_state(connector)) {
  10928. connector->base.dpms = DRM_MODE_DPMS_ON;
  10929. connector->encoder->connectors_active = true;
  10930. connector->base.encoder = &connector->encoder->base;
  10931. } else {
  10932. connector->base.dpms = DRM_MODE_DPMS_OFF;
  10933. connector->base.encoder = NULL;
  10934. }
  10935. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  10936. connector->base.base.id,
  10937. connector->base.name,
  10938. connector->base.encoder ? "enabled" : "disabled");
  10939. }
  10940. }
  10941. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  10942. * and i915 state tracking structures. */
  10943. void intel_modeset_setup_hw_state(struct drm_device *dev,
  10944. bool force_restore)
  10945. {
  10946. struct drm_i915_private *dev_priv = dev->dev_private;
  10947. enum pipe pipe;
  10948. struct intel_crtc *crtc;
  10949. struct intel_encoder *encoder;
  10950. int i;
  10951. intel_modeset_readout_hw_state(dev);
  10952. /*
  10953. * Now that we have the config, copy it to each CRTC struct
  10954. * Note that this could go away if we move to using crtc_config
  10955. * checking everywhere.
  10956. */
  10957. for_each_intel_crtc(dev, crtc) {
  10958. if (crtc->active && i915.fastboot) {
  10959. intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
  10960. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  10961. crtc->base.base.id);
  10962. drm_mode_debug_printmodeline(&crtc->base.mode);
  10963. }
  10964. }
  10965. /* HW state is read out, now we need to sanitize this mess. */
  10966. for_each_intel_encoder(dev, encoder) {
  10967. intel_sanitize_encoder(encoder);
  10968. }
  10969. for_each_pipe(dev_priv, pipe) {
  10970. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  10971. intel_sanitize_crtc(crtc);
  10972. intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
  10973. }
  10974. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10975. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10976. if (!pll->on || pll->active)
  10977. continue;
  10978. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  10979. pll->disable(dev_priv, pll);
  10980. pll->on = false;
  10981. }
  10982. if (HAS_PCH_SPLIT(dev))
  10983. ilk_wm_get_hw_state(dev);
  10984. if (force_restore) {
  10985. i915_redisable_vga(dev);
  10986. /*
  10987. * We need to use raw interfaces for restoring state to avoid
  10988. * checking (bogus) intermediate states.
  10989. */
  10990. for_each_pipe(dev_priv, pipe) {
  10991. struct drm_crtc *crtc =
  10992. dev_priv->pipe_to_crtc_mapping[pipe];
  10993. __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
  10994. crtc->primary->fb);
  10995. }
  10996. } else {
  10997. intel_modeset_update_staged_output_state(dev);
  10998. }
  10999. intel_modeset_check_state(dev);
  11000. }
  11001. void intel_modeset_gem_init(struct drm_device *dev)
  11002. {
  11003. struct drm_crtc *c;
  11004. struct drm_i915_gem_object *obj;
  11005. mutex_lock(&dev->struct_mutex);
  11006. intel_init_gt_powersave(dev);
  11007. mutex_unlock(&dev->struct_mutex);
  11008. intel_modeset_init_hw(dev);
  11009. intel_setup_overlay(dev);
  11010. /*
  11011. * Make sure any fbs we allocated at startup are properly
  11012. * pinned & fenced. When we do the allocation it's too early
  11013. * for this.
  11014. */
  11015. mutex_lock(&dev->struct_mutex);
  11016. for_each_crtc(dev, c) {
  11017. obj = intel_fb_obj(c->primary->fb);
  11018. if (obj == NULL)
  11019. continue;
  11020. if (intel_pin_and_fence_fb_obj(dev, obj, NULL)) {
  11021. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  11022. to_intel_crtc(c)->pipe);
  11023. drm_framebuffer_unreference(c->primary->fb);
  11024. c->primary->fb = NULL;
  11025. }
  11026. }
  11027. mutex_unlock(&dev->struct_mutex);
  11028. }
  11029. void intel_connector_unregister(struct intel_connector *intel_connector)
  11030. {
  11031. struct drm_connector *connector = &intel_connector->base;
  11032. intel_panel_destroy_backlight(connector);
  11033. drm_connector_unregister(connector);
  11034. }
  11035. void intel_modeset_cleanup(struct drm_device *dev)
  11036. {
  11037. struct drm_i915_private *dev_priv = dev->dev_private;
  11038. struct drm_connector *connector;
  11039. /*
  11040. * Interrupts and polling as the first thing to avoid creating havoc.
  11041. * Too much stuff here (turning of rps, connectors, ...) would
  11042. * experience fancy races otherwise.
  11043. */
  11044. intel_irq_uninstall(dev_priv);
  11045. /*
  11046. * Due to the hpd irq storm handling the hotplug work can re-arm the
  11047. * poll handlers. Hence disable polling after hpd handling is shut down.
  11048. */
  11049. drm_kms_helper_poll_fini(dev);
  11050. mutex_lock(&dev->struct_mutex);
  11051. intel_unregister_dsm_handler();
  11052. intel_disable_fbc(dev);
  11053. intel_disable_gt_powersave(dev);
  11054. ironlake_teardown_rc6(dev);
  11055. mutex_unlock(&dev->struct_mutex);
  11056. /* flush any delayed tasks or pending work */
  11057. flush_scheduled_work();
  11058. /* destroy the backlight and sysfs files before encoders/connectors */
  11059. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  11060. struct intel_connector *intel_connector;
  11061. intel_connector = to_intel_connector(connector);
  11062. intel_connector->unregister(intel_connector);
  11063. }
  11064. drm_mode_config_cleanup(dev);
  11065. intel_cleanup_overlay(dev);
  11066. mutex_lock(&dev->struct_mutex);
  11067. intel_cleanup_gt_powersave(dev);
  11068. mutex_unlock(&dev->struct_mutex);
  11069. }
  11070. /*
  11071. * Return which encoder is currently attached for connector.
  11072. */
  11073. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  11074. {
  11075. return &intel_attached_encoder(connector)->base;
  11076. }
  11077. void intel_connector_attach_encoder(struct intel_connector *connector,
  11078. struct intel_encoder *encoder)
  11079. {
  11080. connector->encoder = encoder;
  11081. drm_mode_connector_attach_encoder(&connector->base,
  11082. &encoder->base);
  11083. }
  11084. /*
  11085. * set vga decode state - true == enable VGA decode
  11086. */
  11087. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  11088. {
  11089. struct drm_i915_private *dev_priv = dev->dev_private;
  11090. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  11091. u16 gmch_ctrl;
  11092. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  11093. DRM_ERROR("failed to read control word\n");
  11094. return -EIO;
  11095. }
  11096. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  11097. return 0;
  11098. if (state)
  11099. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  11100. else
  11101. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  11102. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  11103. DRM_ERROR("failed to write control word\n");
  11104. return -EIO;
  11105. }
  11106. return 0;
  11107. }
  11108. struct intel_display_error_state {
  11109. u32 power_well_driver;
  11110. int num_transcoders;
  11111. struct intel_cursor_error_state {
  11112. u32 control;
  11113. u32 position;
  11114. u32 base;
  11115. u32 size;
  11116. } cursor[I915_MAX_PIPES];
  11117. struct intel_pipe_error_state {
  11118. bool power_domain_on;
  11119. u32 source;
  11120. u32 stat;
  11121. } pipe[I915_MAX_PIPES];
  11122. struct intel_plane_error_state {
  11123. u32 control;
  11124. u32 stride;
  11125. u32 size;
  11126. u32 pos;
  11127. u32 addr;
  11128. u32 surface;
  11129. u32 tile_offset;
  11130. } plane[I915_MAX_PIPES];
  11131. struct intel_transcoder_error_state {
  11132. bool power_domain_on;
  11133. enum transcoder cpu_transcoder;
  11134. u32 conf;
  11135. u32 htotal;
  11136. u32 hblank;
  11137. u32 hsync;
  11138. u32 vtotal;
  11139. u32 vblank;
  11140. u32 vsync;
  11141. } transcoder[4];
  11142. };
  11143. struct intel_display_error_state *
  11144. intel_display_capture_error_state(struct drm_device *dev)
  11145. {
  11146. struct drm_i915_private *dev_priv = dev->dev_private;
  11147. struct intel_display_error_state *error;
  11148. int transcoders[] = {
  11149. TRANSCODER_A,
  11150. TRANSCODER_B,
  11151. TRANSCODER_C,
  11152. TRANSCODER_EDP,
  11153. };
  11154. int i;
  11155. if (INTEL_INFO(dev)->num_pipes == 0)
  11156. return NULL;
  11157. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  11158. if (error == NULL)
  11159. return NULL;
  11160. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11161. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  11162. for_each_pipe(dev_priv, i) {
  11163. error->pipe[i].power_domain_on =
  11164. __intel_display_power_is_enabled(dev_priv,
  11165. POWER_DOMAIN_PIPE(i));
  11166. if (!error->pipe[i].power_domain_on)
  11167. continue;
  11168. error->cursor[i].control = I915_READ(CURCNTR(i));
  11169. error->cursor[i].position = I915_READ(CURPOS(i));
  11170. error->cursor[i].base = I915_READ(CURBASE(i));
  11171. error->plane[i].control = I915_READ(DSPCNTR(i));
  11172. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  11173. if (INTEL_INFO(dev)->gen <= 3) {
  11174. error->plane[i].size = I915_READ(DSPSIZE(i));
  11175. error->plane[i].pos = I915_READ(DSPPOS(i));
  11176. }
  11177. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11178. error->plane[i].addr = I915_READ(DSPADDR(i));
  11179. if (INTEL_INFO(dev)->gen >= 4) {
  11180. error->plane[i].surface = I915_READ(DSPSURF(i));
  11181. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  11182. }
  11183. error->pipe[i].source = I915_READ(PIPESRC(i));
  11184. if (HAS_GMCH_DISPLAY(dev))
  11185. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  11186. }
  11187. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  11188. if (HAS_DDI(dev_priv->dev))
  11189. error->num_transcoders++; /* Account for eDP. */
  11190. for (i = 0; i < error->num_transcoders; i++) {
  11191. enum transcoder cpu_transcoder = transcoders[i];
  11192. error->transcoder[i].power_domain_on =
  11193. __intel_display_power_is_enabled(dev_priv,
  11194. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  11195. if (!error->transcoder[i].power_domain_on)
  11196. continue;
  11197. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  11198. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  11199. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  11200. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  11201. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  11202. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  11203. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  11204. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  11205. }
  11206. return error;
  11207. }
  11208. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  11209. void
  11210. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  11211. struct drm_device *dev,
  11212. struct intel_display_error_state *error)
  11213. {
  11214. struct drm_i915_private *dev_priv = dev->dev_private;
  11215. int i;
  11216. if (!error)
  11217. return;
  11218. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  11219. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11220. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  11221. error->power_well_driver);
  11222. for_each_pipe(dev_priv, i) {
  11223. err_printf(m, "Pipe [%d]:\n", i);
  11224. err_printf(m, " Power: %s\n",
  11225. error->pipe[i].power_domain_on ? "on" : "off");
  11226. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  11227. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  11228. err_printf(m, "Plane [%d]:\n", i);
  11229. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  11230. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  11231. if (INTEL_INFO(dev)->gen <= 3) {
  11232. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  11233. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  11234. }
  11235. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11236. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  11237. if (INTEL_INFO(dev)->gen >= 4) {
  11238. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  11239. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  11240. }
  11241. err_printf(m, "Cursor [%d]:\n", i);
  11242. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  11243. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  11244. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  11245. }
  11246. for (i = 0; i < error->num_transcoders; i++) {
  11247. err_printf(m, "CPU transcoder: %c\n",
  11248. transcoder_name(error->transcoder[i].cpu_transcoder));
  11249. err_printf(m, " Power: %s\n",
  11250. error->transcoder[i].power_domain_on ? "on" : "off");
  11251. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  11252. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  11253. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  11254. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  11255. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  11256. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  11257. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  11258. }
  11259. }
  11260. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  11261. {
  11262. struct intel_crtc *crtc;
  11263. for_each_intel_crtc(dev, crtc) {
  11264. struct intel_unpin_work *work;
  11265. spin_lock_irq(&dev->event_lock);
  11266. work = crtc->unpin_work;
  11267. if (work && work->event &&
  11268. work->event->base.file_priv == file) {
  11269. kfree(work->event);
  11270. work->event = NULL;
  11271. }
  11272. spin_unlock_irq(&dev->event_lock);
  11273. }
  11274. }