intel_display.c 389 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091
  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_atomic_helper.h>
  40. #include <drm/drm_dp_helper.h>
  41. #include <drm/drm_crtc_helper.h>
  42. #include <drm/drm_plane_helper.h>
  43. #include <drm/drm_rect.h>
  44. #include <linux/dma_remapping.h>
  45. /* Primary plane formats supported by all gen */
  46. #define COMMON_PRIMARY_FORMATS \
  47. DRM_FORMAT_C8, \
  48. DRM_FORMAT_RGB565, \
  49. DRM_FORMAT_XRGB8888, \
  50. DRM_FORMAT_ARGB8888
  51. /* Primary plane formats for gen <= 3 */
  52. static const uint32_t intel_primary_formats_gen2[] = {
  53. COMMON_PRIMARY_FORMATS,
  54. DRM_FORMAT_XRGB1555,
  55. DRM_FORMAT_ARGB1555,
  56. };
  57. /* Primary plane formats for gen >= 4 */
  58. static const uint32_t intel_primary_formats_gen4[] = {
  59. COMMON_PRIMARY_FORMATS, \
  60. DRM_FORMAT_XBGR8888,
  61. DRM_FORMAT_ABGR8888,
  62. DRM_FORMAT_XRGB2101010,
  63. DRM_FORMAT_ARGB2101010,
  64. DRM_FORMAT_XBGR2101010,
  65. DRM_FORMAT_ABGR2101010,
  66. };
  67. /* Cursor formats */
  68. static const uint32_t intel_cursor_formats[] = {
  69. DRM_FORMAT_ARGB8888,
  70. };
  71. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  72. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  73. struct intel_crtc_state *pipe_config);
  74. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  75. struct intel_crtc_state *pipe_config);
  76. static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
  77. int x, int y, struct drm_framebuffer *old_fb);
  78. static int intel_framebuffer_init(struct drm_device *dev,
  79. struct intel_framebuffer *ifb,
  80. struct drm_mode_fb_cmd2 *mode_cmd,
  81. struct drm_i915_gem_object *obj);
  82. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  83. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  84. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  85. struct intel_link_m_n *m_n,
  86. struct intel_link_m_n *m2_n2);
  87. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  88. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  89. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  90. static void vlv_prepare_pll(struct intel_crtc *crtc,
  91. const struct intel_crtc_state *pipe_config);
  92. static void chv_prepare_pll(struct intel_crtc *crtc,
  93. const struct intel_crtc_state *pipe_config);
  94. static void intel_begin_crtc_commit(struct drm_crtc *crtc);
  95. static void intel_finish_crtc_commit(struct drm_crtc *crtc);
  96. static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
  97. {
  98. if (!connector->mst_port)
  99. return connector->encoder;
  100. else
  101. return &connector->mst_port->mst_encoders[pipe]->base;
  102. }
  103. typedef struct {
  104. int min, max;
  105. } intel_range_t;
  106. typedef struct {
  107. int dot_limit;
  108. int p2_slow, p2_fast;
  109. } intel_p2_t;
  110. typedef struct intel_limit intel_limit_t;
  111. struct intel_limit {
  112. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  113. intel_p2_t p2;
  114. };
  115. int
  116. intel_pch_rawclk(struct drm_device *dev)
  117. {
  118. struct drm_i915_private *dev_priv = dev->dev_private;
  119. WARN_ON(!HAS_PCH_SPLIT(dev));
  120. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  121. }
  122. static inline u32 /* units of 100MHz */
  123. intel_fdi_link_freq(struct drm_device *dev)
  124. {
  125. if (IS_GEN5(dev)) {
  126. struct drm_i915_private *dev_priv = dev->dev_private;
  127. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  128. } else
  129. return 27;
  130. }
  131. static const intel_limit_t intel_limits_i8xx_dac = {
  132. .dot = { .min = 25000, .max = 350000 },
  133. .vco = { .min = 908000, .max = 1512000 },
  134. .n = { .min = 2, .max = 16 },
  135. .m = { .min = 96, .max = 140 },
  136. .m1 = { .min = 18, .max = 26 },
  137. .m2 = { .min = 6, .max = 16 },
  138. .p = { .min = 4, .max = 128 },
  139. .p1 = { .min = 2, .max = 33 },
  140. .p2 = { .dot_limit = 165000,
  141. .p2_slow = 4, .p2_fast = 2 },
  142. };
  143. static const intel_limit_t intel_limits_i8xx_dvo = {
  144. .dot = { .min = 25000, .max = 350000 },
  145. .vco = { .min = 908000, .max = 1512000 },
  146. .n = { .min = 2, .max = 16 },
  147. .m = { .min = 96, .max = 140 },
  148. .m1 = { .min = 18, .max = 26 },
  149. .m2 = { .min = 6, .max = 16 },
  150. .p = { .min = 4, .max = 128 },
  151. .p1 = { .min = 2, .max = 33 },
  152. .p2 = { .dot_limit = 165000,
  153. .p2_slow = 4, .p2_fast = 4 },
  154. };
  155. static const intel_limit_t intel_limits_i8xx_lvds = {
  156. .dot = { .min = 25000, .max = 350000 },
  157. .vco = { .min = 908000, .max = 1512000 },
  158. .n = { .min = 2, .max = 16 },
  159. .m = { .min = 96, .max = 140 },
  160. .m1 = { .min = 18, .max = 26 },
  161. .m2 = { .min = 6, .max = 16 },
  162. .p = { .min = 4, .max = 128 },
  163. .p1 = { .min = 1, .max = 6 },
  164. .p2 = { .dot_limit = 165000,
  165. .p2_slow = 14, .p2_fast = 7 },
  166. };
  167. static const intel_limit_t intel_limits_i9xx_sdvo = {
  168. .dot = { .min = 20000, .max = 400000 },
  169. .vco = { .min = 1400000, .max = 2800000 },
  170. .n = { .min = 1, .max = 6 },
  171. .m = { .min = 70, .max = 120 },
  172. .m1 = { .min = 8, .max = 18 },
  173. .m2 = { .min = 3, .max = 7 },
  174. .p = { .min = 5, .max = 80 },
  175. .p1 = { .min = 1, .max = 8 },
  176. .p2 = { .dot_limit = 200000,
  177. .p2_slow = 10, .p2_fast = 5 },
  178. };
  179. static const intel_limit_t intel_limits_i9xx_lvds = {
  180. .dot = { .min = 20000, .max = 400000 },
  181. .vco = { .min = 1400000, .max = 2800000 },
  182. .n = { .min = 1, .max = 6 },
  183. .m = { .min = 70, .max = 120 },
  184. .m1 = { .min = 8, .max = 18 },
  185. .m2 = { .min = 3, .max = 7 },
  186. .p = { .min = 7, .max = 98 },
  187. .p1 = { .min = 1, .max = 8 },
  188. .p2 = { .dot_limit = 112000,
  189. .p2_slow = 14, .p2_fast = 7 },
  190. };
  191. static const intel_limit_t intel_limits_g4x_sdvo = {
  192. .dot = { .min = 25000, .max = 270000 },
  193. .vco = { .min = 1750000, .max = 3500000},
  194. .n = { .min = 1, .max = 4 },
  195. .m = { .min = 104, .max = 138 },
  196. .m1 = { .min = 17, .max = 23 },
  197. .m2 = { .min = 5, .max = 11 },
  198. .p = { .min = 10, .max = 30 },
  199. .p1 = { .min = 1, .max = 3},
  200. .p2 = { .dot_limit = 270000,
  201. .p2_slow = 10,
  202. .p2_fast = 10
  203. },
  204. };
  205. static const intel_limit_t intel_limits_g4x_hdmi = {
  206. .dot = { .min = 22000, .max = 400000 },
  207. .vco = { .min = 1750000, .max = 3500000},
  208. .n = { .min = 1, .max = 4 },
  209. .m = { .min = 104, .max = 138 },
  210. .m1 = { .min = 16, .max = 23 },
  211. .m2 = { .min = 5, .max = 11 },
  212. .p = { .min = 5, .max = 80 },
  213. .p1 = { .min = 1, .max = 8},
  214. .p2 = { .dot_limit = 165000,
  215. .p2_slow = 10, .p2_fast = 5 },
  216. };
  217. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  218. .dot = { .min = 20000, .max = 115000 },
  219. .vco = { .min = 1750000, .max = 3500000 },
  220. .n = { .min = 1, .max = 3 },
  221. .m = { .min = 104, .max = 138 },
  222. .m1 = { .min = 17, .max = 23 },
  223. .m2 = { .min = 5, .max = 11 },
  224. .p = { .min = 28, .max = 112 },
  225. .p1 = { .min = 2, .max = 8 },
  226. .p2 = { .dot_limit = 0,
  227. .p2_slow = 14, .p2_fast = 14
  228. },
  229. };
  230. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  231. .dot = { .min = 80000, .max = 224000 },
  232. .vco = { .min = 1750000, .max = 3500000 },
  233. .n = { .min = 1, .max = 3 },
  234. .m = { .min = 104, .max = 138 },
  235. .m1 = { .min = 17, .max = 23 },
  236. .m2 = { .min = 5, .max = 11 },
  237. .p = { .min = 14, .max = 42 },
  238. .p1 = { .min = 2, .max = 6 },
  239. .p2 = { .dot_limit = 0,
  240. .p2_slow = 7, .p2_fast = 7
  241. },
  242. };
  243. static const intel_limit_t intel_limits_pineview_sdvo = {
  244. .dot = { .min = 20000, .max = 400000},
  245. .vco = { .min = 1700000, .max = 3500000 },
  246. /* Pineview's Ncounter is a ring counter */
  247. .n = { .min = 3, .max = 6 },
  248. .m = { .min = 2, .max = 256 },
  249. /* Pineview only has one combined m divider, which we treat as m2. */
  250. .m1 = { .min = 0, .max = 0 },
  251. .m2 = { .min = 0, .max = 254 },
  252. .p = { .min = 5, .max = 80 },
  253. .p1 = { .min = 1, .max = 8 },
  254. .p2 = { .dot_limit = 200000,
  255. .p2_slow = 10, .p2_fast = 5 },
  256. };
  257. static const intel_limit_t intel_limits_pineview_lvds = {
  258. .dot = { .min = 20000, .max = 400000 },
  259. .vco = { .min = 1700000, .max = 3500000 },
  260. .n = { .min = 3, .max = 6 },
  261. .m = { .min = 2, .max = 256 },
  262. .m1 = { .min = 0, .max = 0 },
  263. .m2 = { .min = 0, .max = 254 },
  264. .p = { .min = 7, .max = 112 },
  265. .p1 = { .min = 1, .max = 8 },
  266. .p2 = { .dot_limit = 112000,
  267. .p2_slow = 14, .p2_fast = 14 },
  268. };
  269. /* Ironlake / Sandybridge
  270. *
  271. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  272. * the range value for them is (actual_value - 2).
  273. */
  274. static const intel_limit_t intel_limits_ironlake_dac = {
  275. .dot = { .min = 25000, .max = 350000 },
  276. .vco = { .min = 1760000, .max = 3510000 },
  277. .n = { .min = 1, .max = 5 },
  278. .m = { .min = 79, .max = 127 },
  279. .m1 = { .min = 12, .max = 22 },
  280. .m2 = { .min = 5, .max = 9 },
  281. .p = { .min = 5, .max = 80 },
  282. .p1 = { .min = 1, .max = 8 },
  283. .p2 = { .dot_limit = 225000,
  284. .p2_slow = 10, .p2_fast = 5 },
  285. };
  286. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  287. .dot = { .min = 25000, .max = 350000 },
  288. .vco = { .min = 1760000, .max = 3510000 },
  289. .n = { .min = 1, .max = 3 },
  290. .m = { .min = 79, .max = 118 },
  291. .m1 = { .min = 12, .max = 22 },
  292. .m2 = { .min = 5, .max = 9 },
  293. .p = { .min = 28, .max = 112 },
  294. .p1 = { .min = 2, .max = 8 },
  295. .p2 = { .dot_limit = 225000,
  296. .p2_slow = 14, .p2_fast = 14 },
  297. };
  298. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  299. .dot = { .min = 25000, .max = 350000 },
  300. .vco = { .min = 1760000, .max = 3510000 },
  301. .n = { .min = 1, .max = 3 },
  302. .m = { .min = 79, .max = 127 },
  303. .m1 = { .min = 12, .max = 22 },
  304. .m2 = { .min = 5, .max = 9 },
  305. .p = { .min = 14, .max = 56 },
  306. .p1 = { .min = 2, .max = 8 },
  307. .p2 = { .dot_limit = 225000,
  308. .p2_slow = 7, .p2_fast = 7 },
  309. };
  310. /* LVDS 100mhz refclk limits. */
  311. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  312. .dot = { .min = 25000, .max = 350000 },
  313. .vco = { .min = 1760000, .max = 3510000 },
  314. .n = { .min = 1, .max = 2 },
  315. .m = { .min = 79, .max = 126 },
  316. .m1 = { .min = 12, .max = 22 },
  317. .m2 = { .min = 5, .max = 9 },
  318. .p = { .min = 28, .max = 112 },
  319. .p1 = { .min = 2, .max = 8 },
  320. .p2 = { .dot_limit = 225000,
  321. .p2_slow = 14, .p2_fast = 14 },
  322. };
  323. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  324. .dot = { .min = 25000, .max = 350000 },
  325. .vco = { .min = 1760000, .max = 3510000 },
  326. .n = { .min = 1, .max = 3 },
  327. .m = { .min = 79, .max = 126 },
  328. .m1 = { .min = 12, .max = 22 },
  329. .m2 = { .min = 5, .max = 9 },
  330. .p = { .min = 14, .max = 42 },
  331. .p1 = { .min = 2, .max = 6 },
  332. .p2 = { .dot_limit = 225000,
  333. .p2_slow = 7, .p2_fast = 7 },
  334. };
  335. static const intel_limit_t intel_limits_vlv = {
  336. /*
  337. * These are the data rate limits (measured in fast clocks)
  338. * since those are the strictest limits we have. The fast
  339. * clock and actual rate limits are more relaxed, so checking
  340. * them would make no difference.
  341. */
  342. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  343. .vco = { .min = 4000000, .max = 6000000 },
  344. .n = { .min = 1, .max = 7 },
  345. .m1 = { .min = 2, .max = 3 },
  346. .m2 = { .min = 11, .max = 156 },
  347. .p1 = { .min = 2, .max = 3 },
  348. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  349. };
  350. static const intel_limit_t intel_limits_chv = {
  351. /*
  352. * These are the data rate limits (measured in fast clocks)
  353. * since those are the strictest limits we have. The fast
  354. * clock and actual rate limits are more relaxed, so checking
  355. * them would make no difference.
  356. */
  357. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  358. .vco = { .min = 4860000, .max = 6700000 },
  359. .n = { .min = 1, .max = 1 },
  360. .m1 = { .min = 2, .max = 2 },
  361. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  362. .p1 = { .min = 2, .max = 4 },
  363. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  364. };
  365. static void vlv_clock(int refclk, intel_clock_t *clock)
  366. {
  367. clock->m = clock->m1 * clock->m2;
  368. clock->p = clock->p1 * clock->p2;
  369. if (WARN_ON(clock->n == 0 || clock->p == 0))
  370. return;
  371. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  372. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  373. }
  374. /**
  375. * Returns whether any output on the specified pipe is of the specified type
  376. */
  377. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  378. {
  379. struct drm_device *dev = crtc->base.dev;
  380. struct intel_encoder *encoder;
  381. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  382. if (encoder->type == type)
  383. return true;
  384. return false;
  385. }
  386. /**
  387. * Returns whether any output on the specified pipe will have the specified
  388. * type after a staged modeset is complete, i.e., the same as
  389. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  390. * encoder->crtc.
  391. */
  392. static bool intel_pipe_will_have_type(struct intel_crtc *crtc, int type)
  393. {
  394. struct drm_device *dev = crtc->base.dev;
  395. struct intel_encoder *encoder;
  396. for_each_intel_encoder(dev, encoder)
  397. if (encoder->new_crtc == crtc && encoder->type == type)
  398. return true;
  399. return false;
  400. }
  401. static const intel_limit_t *intel_ironlake_limit(struct intel_crtc *crtc,
  402. int refclk)
  403. {
  404. struct drm_device *dev = crtc->base.dev;
  405. const intel_limit_t *limit;
  406. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  407. if (intel_is_dual_link_lvds(dev)) {
  408. if (refclk == 100000)
  409. limit = &intel_limits_ironlake_dual_lvds_100m;
  410. else
  411. limit = &intel_limits_ironlake_dual_lvds;
  412. } else {
  413. if (refclk == 100000)
  414. limit = &intel_limits_ironlake_single_lvds_100m;
  415. else
  416. limit = &intel_limits_ironlake_single_lvds;
  417. }
  418. } else
  419. limit = &intel_limits_ironlake_dac;
  420. return limit;
  421. }
  422. static const intel_limit_t *intel_g4x_limit(struct intel_crtc *crtc)
  423. {
  424. struct drm_device *dev = crtc->base.dev;
  425. const intel_limit_t *limit;
  426. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  427. if (intel_is_dual_link_lvds(dev))
  428. limit = &intel_limits_g4x_dual_channel_lvds;
  429. else
  430. limit = &intel_limits_g4x_single_channel_lvds;
  431. } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_HDMI) ||
  432. intel_pipe_will_have_type(crtc, INTEL_OUTPUT_ANALOG)) {
  433. limit = &intel_limits_g4x_hdmi;
  434. } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_SDVO)) {
  435. limit = &intel_limits_g4x_sdvo;
  436. } else /* The option is for other outputs */
  437. limit = &intel_limits_i9xx_sdvo;
  438. return limit;
  439. }
  440. static const intel_limit_t *intel_limit(struct intel_crtc *crtc, int refclk)
  441. {
  442. struct drm_device *dev = crtc->base.dev;
  443. const intel_limit_t *limit;
  444. if (HAS_PCH_SPLIT(dev))
  445. limit = intel_ironlake_limit(crtc, refclk);
  446. else if (IS_G4X(dev)) {
  447. limit = intel_g4x_limit(crtc);
  448. } else if (IS_PINEVIEW(dev)) {
  449. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  450. limit = &intel_limits_pineview_lvds;
  451. else
  452. limit = &intel_limits_pineview_sdvo;
  453. } else if (IS_CHERRYVIEW(dev)) {
  454. limit = &intel_limits_chv;
  455. } else if (IS_VALLEYVIEW(dev)) {
  456. limit = &intel_limits_vlv;
  457. } else if (!IS_GEN2(dev)) {
  458. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  459. limit = &intel_limits_i9xx_lvds;
  460. else
  461. limit = &intel_limits_i9xx_sdvo;
  462. } else {
  463. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  464. limit = &intel_limits_i8xx_lvds;
  465. else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_DVO))
  466. limit = &intel_limits_i8xx_dvo;
  467. else
  468. limit = &intel_limits_i8xx_dac;
  469. }
  470. return limit;
  471. }
  472. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  473. static void pineview_clock(int refclk, intel_clock_t *clock)
  474. {
  475. clock->m = clock->m2 + 2;
  476. clock->p = clock->p1 * clock->p2;
  477. if (WARN_ON(clock->n == 0 || clock->p == 0))
  478. return;
  479. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  480. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  481. }
  482. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  483. {
  484. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  485. }
  486. static void i9xx_clock(int refclk, intel_clock_t *clock)
  487. {
  488. clock->m = i9xx_dpll_compute_m(clock);
  489. clock->p = clock->p1 * clock->p2;
  490. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  491. return;
  492. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  493. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  494. }
  495. static void chv_clock(int refclk, intel_clock_t *clock)
  496. {
  497. clock->m = clock->m1 * clock->m2;
  498. clock->p = clock->p1 * clock->p2;
  499. if (WARN_ON(clock->n == 0 || clock->p == 0))
  500. return;
  501. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  502. clock->n << 22);
  503. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  504. }
  505. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  506. /**
  507. * Returns whether the given set of divisors are valid for a given refclk with
  508. * the given connectors.
  509. */
  510. static bool intel_PLL_is_valid(struct drm_device *dev,
  511. const intel_limit_t *limit,
  512. const intel_clock_t *clock)
  513. {
  514. if (clock->n < limit->n.min || limit->n.max < clock->n)
  515. INTELPllInvalid("n out of range\n");
  516. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  517. INTELPllInvalid("p1 out of range\n");
  518. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  519. INTELPllInvalid("m2 out of range\n");
  520. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  521. INTELPllInvalid("m1 out of range\n");
  522. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
  523. if (clock->m1 <= clock->m2)
  524. INTELPllInvalid("m1 <= m2\n");
  525. if (!IS_VALLEYVIEW(dev)) {
  526. if (clock->p < limit->p.min || limit->p.max < clock->p)
  527. INTELPllInvalid("p out of range\n");
  528. if (clock->m < limit->m.min || limit->m.max < clock->m)
  529. INTELPllInvalid("m out of range\n");
  530. }
  531. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  532. INTELPllInvalid("vco out of range\n");
  533. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  534. * connector, etc., rather than just a single range.
  535. */
  536. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  537. INTELPllInvalid("dot out of range\n");
  538. return true;
  539. }
  540. static bool
  541. i9xx_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  542. int target, int refclk, intel_clock_t *match_clock,
  543. intel_clock_t *best_clock)
  544. {
  545. struct drm_device *dev = crtc->base.dev;
  546. intel_clock_t clock;
  547. int err = target;
  548. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  549. /*
  550. * For LVDS just rely on its current settings for dual-channel.
  551. * We haven't figured out how to reliably set up different
  552. * single/dual channel state, if we even can.
  553. */
  554. if (intel_is_dual_link_lvds(dev))
  555. clock.p2 = limit->p2.p2_fast;
  556. else
  557. clock.p2 = limit->p2.p2_slow;
  558. } else {
  559. if (target < limit->p2.dot_limit)
  560. clock.p2 = limit->p2.p2_slow;
  561. else
  562. clock.p2 = limit->p2.p2_fast;
  563. }
  564. memset(best_clock, 0, sizeof(*best_clock));
  565. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  566. clock.m1++) {
  567. for (clock.m2 = limit->m2.min;
  568. clock.m2 <= limit->m2.max; clock.m2++) {
  569. if (clock.m2 >= clock.m1)
  570. break;
  571. for (clock.n = limit->n.min;
  572. clock.n <= limit->n.max; clock.n++) {
  573. for (clock.p1 = limit->p1.min;
  574. clock.p1 <= limit->p1.max; clock.p1++) {
  575. int this_err;
  576. i9xx_clock(refclk, &clock);
  577. if (!intel_PLL_is_valid(dev, limit,
  578. &clock))
  579. continue;
  580. if (match_clock &&
  581. clock.p != match_clock->p)
  582. continue;
  583. this_err = abs(clock.dot - target);
  584. if (this_err < err) {
  585. *best_clock = clock;
  586. err = this_err;
  587. }
  588. }
  589. }
  590. }
  591. }
  592. return (err != target);
  593. }
  594. static bool
  595. pnv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  596. int target, int refclk, intel_clock_t *match_clock,
  597. intel_clock_t *best_clock)
  598. {
  599. struct drm_device *dev = crtc->base.dev;
  600. intel_clock_t clock;
  601. int err = target;
  602. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  603. /*
  604. * For LVDS just rely on its current settings for dual-channel.
  605. * We haven't figured out how to reliably set up different
  606. * single/dual channel state, if we even can.
  607. */
  608. if (intel_is_dual_link_lvds(dev))
  609. clock.p2 = limit->p2.p2_fast;
  610. else
  611. clock.p2 = limit->p2.p2_slow;
  612. } else {
  613. if (target < limit->p2.dot_limit)
  614. clock.p2 = limit->p2.p2_slow;
  615. else
  616. clock.p2 = limit->p2.p2_fast;
  617. }
  618. memset(best_clock, 0, sizeof(*best_clock));
  619. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  620. clock.m1++) {
  621. for (clock.m2 = limit->m2.min;
  622. clock.m2 <= limit->m2.max; clock.m2++) {
  623. for (clock.n = limit->n.min;
  624. clock.n <= limit->n.max; clock.n++) {
  625. for (clock.p1 = limit->p1.min;
  626. clock.p1 <= limit->p1.max; clock.p1++) {
  627. int this_err;
  628. pineview_clock(refclk, &clock);
  629. if (!intel_PLL_is_valid(dev, limit,
  630. &clock))
  631. continue;
  632. if (match_clock &&
  633. clock.p != match_clock->p)
  634. continue;
  635. this_err = abs(clock.dot - target);
  636. if (this_err < err) {
  637. *best_clock = clock;
  638. err = this_err;
  639. }
  640. }
  641. }
  642. }
  643. }
  644. return (err != target);
  645. }
  646. static bool
  647. g4x_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  648. int target, int refclk, intel_clock_t *match_clock,
  649. intel_clock_t *best_clock)
  650. {
  651. struct drm_device *dev = crtc->base.dev;
  652. intel_clock_t clock;
  653. int max_n;
  654. bool found;
  655. /* approximately equals target * 0.00585 */
  656. int err_most = (target >> 8) + (target >> 9);
  657. found = false;
  658. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  659. if (intel_is_dual_link_lvds(dev))
  660. clock.p2 = limit->p2.p2_fast;
  661. else
  662. clock.p2 = limit->p2.p2_slow;
  663. } else {
  664. if (target < limit->p2.dot_limit)
  665. clock.p2 = limit->p2.p2_slow;
  666. else
  667. clock.p2 = limit->p2.p2_fast;
  668. }
  669. memset(best_clock, 0, sizeof(*best_clock));
  670. max_n = limit->n.max;
  671. /* based on hardware requirement, prefer smaller n to precision */
  672. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  673. /* based on hardware requirement, prefere larger m1,m2 */
  674. for (clock.m1 = limit->m1.max;
  675. clock.m1 >= limit->m1.min; clock.m1--) {
  676. for (clock.m2 = limit->m2.max;
  677. clock.m2 >= limit->m2.min; clock.m2--) {
  678. for (clock.p1 = limit->p1.max;
  679. clock.p1 >= limit->p1.min; clock.p1--) {
  680. int this_err;
  681. i9xx_clock(refclk, &clock);
  682. if (!intel_PLL_is_valid(dev, limit,
  683. &clock))
  684. continue;
  685. this_err = abs(clock.dot - target);
  686. if (this_err < err_most) {
  687. *best_clock = clock;
  688. err_most = this_err;
  689. max_n = clock.n;
  690. found = true;
  691. }
  692. }
  693. }
  694. }
  695. }
  696. return found;
  697. }
  698. static bool
  699. vlv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  700. int target, int refclk, intel_clock_t *match_clock,
  701. intel_clock_t *best_clock)
  702. {
  703. struct drm_device *dev = crtc->base.dev;
  704. intel_clock_t clock;
  705. unsigned int bestppm = 1000000;
  706. /* min update 19.2 MHz */
  707. int max_n = min(limit->n.max, refclk / 19200);
  708. bool found = false;
  709. target *= 5; /* fast clock */
  710. memset(best_clock, 0, sizeof(*best_clock));
  711. /* based on hardware requirement, prefer smaller n to precision */
  712. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  713. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  714. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  715. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  716. clock.p = clock.p1 * clock.p2;
  717. /* based on hardware requirement, prefer bigger m1,m2 values */
  718. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  719. unsigned int ppm, diff;
  720. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  721. refclk * clock.m1);
  722. vlv_clock(refclk, &clock);
  723. if (!intel_PLL_is_valid(dev, limit,
  724. &clock))
  725. continue;
  726. diff = abs(clock.dot - target);
  727. ppm = div_u64(1000000ULL * diff, target);
  728. if (ppm < 100 && clock.p > best_clock->p) {
  729. bestppm = 0;
  730. *best_clock = clock;
  731. found = true;
  732. }
  733. if (bestppm >= 10 && ppm < bestppm - 10) {
  734. bestppm = ppm;
  735. *best_clock = clock;
  736. found = true;
  737. }
  738. }
  739. }
  740. }
  741. }
  742. return found;
  743. }
  744. static bool
  745. chv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  746. int target, int refclk, intel_clock_t *match_clock,
  747. intel_clock_t *best_clock)
  748. {
  749. struct drm_device *dev = crtc->base.dev;
  750. intel_clock_t clock;
  751. uint64_t m2;
  752. int found = false;
  753. memset(best_clock, 0, sizeof(*best_clock));
  754. /*
  755. * Based on hardware doc, the n always set to 1, and m1 always
  756. * set to 2. If requires to support 200Mhz refclk, we need to
  757. * revisit this because n may not 1 anymore.
  758. */
  759. clock.n = 1, clock.m1 = 2;
  760. target *= 5; /* fast clock */
  761. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  762. for (clock.p2 = limit->p2.p2_fast;
  763. clock.p2 >= limit->p2.p2_slow;
  764. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  765. clock.p = clock.p1 * clock.p2;
  766. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  767. clock.n) << 22, refclk * clock.m1);
  768. if (m2 > INT_MAX/clock.m1)
  769. continue;
  770. clock.m2 = m2;
  771. chv_clock(refclk, &clock);
  772. if (!intel_PLL_is_valid(dev, limit, &clock))
  773. continue;
  774. /* based on hardware requirement, prefer bigger p
  775. */
  776. if (clock.p > best_clock->p) {
  777. *best_clock = clock;
  778. found = true;
  779. }
  780. }
  781. }
  782. return found;
  783. }
  784. bool intel_crtc_active(struct drm_crtc *crtc)
  785. {
  786. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  787. /* Be paranoid as we can arrive here with only partial
  788. * state retrieved from the hardware during setup.
  789. *
  790. * We can ditch the adjusted_mode.crtc_clock check as soon
  791. * as Haswell has gained clock readout/fastboot support.
  792. *
  793. * We can ditch the crtc->primary->fb check as soon as we can
  794. * properly reconstruct framebuffers.
  795. */
  796. return intel_crtc->active && crtc->primary->fb &&
  797. intel_crtc->config->base.adjusted_mode.crtc_clock;
  798. }
  799. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  800. enum pipe pipe)
  801. {
  802. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  803. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  804. return intel_crtc->config->cpu_transcoder;
  805. }
  806. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  807. {
  808. struct drm_i915_private *dev_priv = dev->dev_private;
  809. u32 reg = PIPEDSL(pipe);
  810. u32 line1, line2;
  811. u32 line_mask;
  812. if (IS_GEN2(dev))
  813. line_mask = DSL_LINEMASK_GEN2;
  814. else
  815. line_mask = DSL_LINEMASK_GEN3;
  816. line1 = I915_READ(reg) & line_mask;
  817. mdelay(5);
  818. line2 = I915_READ(reg) & line_mask;
  819. return line1 == line2;
  820. }
  821. /*
  822. * intel_wait_for_pipe_off - wait for pipe to turn off
  823. * @crtc: crtc whose pipe to wait for
  824. *
  825. * After disabling a pipe, we can't wait for vblank in the usual way,
  826. * spinning on the vblank interrupt status bit, since we won't actually
  827. * see an interrupt when the pipe is disabled.
  828. *
  829. * On Gen4 and above:
  830. * wait for the pipe register state bit to turn off
  831. *
  832. * Otherwise:
  833. * wait for the display line value to settle (it usually
  834. * ends up stopping at the start of the next frame).
  835. *
  836. */
  837. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  838. {
  839. struct drm_device *dev = crtc->base.dev;
  840. struct drm_i915_private *dev_priv = dev->dev_private;
  841. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  842. enum pipe pipe = crtc->pipe;
  843. if (INTEL_INFO(dev)->gen >= 4) {
  844. int reg = PIPECONF(cpu_transcoder);
  845. /* Wait for the Pipe State to go off */
  846. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  847. 100))
  848. WARN(1, "pipe_off wait timed out\n");
  849. } else {
  850. /* Wait for the display line to settle */
  851. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  852. WARN(1, "pipe_off wait timed out\n");
  853. }
  854. }
  855. /*
  856. * ibx_digital_port_connected - is the specified port connected?
  857. * @dev_priv: i915 private structure
  858. * @port: the port to test
  859. *
  860. * Returns true if @port is connected, false otherwise.
  861. */
  862. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  863. struct intel_digital_port *port)
  864. {
  865. u32 bit;
  866. if (HAS_PCH_IBX(dev_priv->dev)) {
  867. switch (port->port) {
  868. case PORT_B:
  869. bit = SDE_PORTB_HOTPLUG;
  870. break;
  871. case PORT_C:
  872. bit = SDE_PORTC_HOTPLUG;
  873. break;
  874. case PORT_D:
  875. bit = SDE_PORTD_HOTPLUG;
  876. break;
  877. default:
  878. return true;
  879. }
  880. } else {
  881. switch (port->port) {
  882. case PORT_B:
  883. bit = SDE_PORTB_HOTPLUG_CPT;
  884. break;
  885. case PORT_C:
  886. bit = SDE_PORTC_HOTPLUG_CPT;
  887. break;
  888. case PORT_D:
  889. bit = SDE_PORTD_HOTPLUG_CPT;
  890. break;
  891. default:
  892. return true;
  893. }
  894. }
  895. return I915_READ(SDEISR) & bit;
  896. }
  897. static const char *state_string(bool enabled)
  898. {
  899. return enabled ? "on" : "off";
  900. }
  901. /* Only for pre-ILK configs */
  902. void assert_pll(struct drm_i915_private *dev_priv,
  903. enum pipe pipe, bool state)
  904. {
  905. int reg;
  906. u32 val;
  907. bool cur_state;
  908. reg = DPLL(pipe);
  909. val = I915_READ(reg);
  910. cur_state = !!(val & DPLL_VCO_ENABLE);
  911. I915_STATE_WARN(cur_state != state,
  912. "PLL state assertion failure (expected %s, current %s)\n",
  913. state_string(state), state_string(cur_state));
  914. }
  915. /* XXX: the dsi pll is shared between MIPI DSI ports */
  916. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  917. {
  918. u32 val;
  919. bool cur_state;
  920. mutex_lock(&dev_priv->dpio_lock);
  921. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  922. mutex_unlock(&dev_priv->dpio_lock);
  923. cur_state = val & DSI_PLL_VCO_EN;
  924. I915_STATE_WARN(cur_state != state,
  925. "DSI PLL state assertion failure (expected %s, current %s)\n",
  926. state_string(state), state_string(cur_state));
  927. }
  928. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  929. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  930. struct intel_shared_dpll *
  931. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  932. {
  933. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  934. if (crtc->config->shared_dpll < 0)
  935. return NULL;
  936. return &dev_priv->shared_dplls[crtc->config->shared_dpll];
  937. }
  938. /* For ILK+ */
  939. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  940. struct intel_shared_dpll *pll,
  941. bool state)
  942. {
  943. bool cur_state;
  944. struct intel_dpll_hw_state hw_state;
  945. if (WARN (!pll,
  946. "asserting DPLL %s with no DPLL\n", state_string(state)))
  947. return;
  948. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  949. I915_STATE_WARN(cur_state != state,
  950. "%s assertion failure (expected %s, current %s)\n",
  951. pll->name, state_string(state), state_string(cur_state));
  952. }
  953. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  954. enum pipe pipe, bool state)
  955. {
  956. int reg;
  957. u32 val;
  958. bool cur_state;
  959. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  960. pipe);
  961. if (HAS_DDI(dev_priv->dev)) {
  962. /* DDI does not have a specific FDI_TX register */
  963. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  964. val = I915_READ(reg);
  965. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  966. } else {
  967. reg = FDI_TX_CTL(pipe);
  968. val = I915_READ(reg);
  969. cur_state = !!(val & FDI_TX_ENABLE);
  970. }
  971. I915_STATE_WARN(cur_state != state,
  972. "FDI TX state assertion failure (expected %s, current %s)\n",
  973. state_string(state), state_string(cur_state));
  974. }
  975. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  976. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  977. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  978. enum pipe pipe, bool state)
  979. {
  980. int reg;
  981. u32 val;
  982. bool cur_state;
  983. reg = FDI_RX_CTL(pipe);
  984. val = I915_READ(reg);
  985. cur_state = !!(val & FDI_RX_ENABLE);
  986. I915_STATE_WARN(cur_state != state,
  987. "FDI RX state assertion failure (expected %s, current %s)\n",
  988. state_string(state), state_string(cur_state));
  989. }
  990. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  991. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  992. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  993. enum pipe pipe)
  994. {
  995. int reg;
  996. u32 val;
  997. /* ILK FDI PLL is always enabled */
  998. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  999. return;
  1000. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1001. if (HAS_DDI(dev_priv->dev))
  1002. return;
  1003. reg = FDI_TX_CTL(pipe);
  1004. val = I915_READ(reg);
  1005. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1006. }
  1007. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1008. enum pipe pipe, bool state)
  1009. {
  1010. int reg;
  1011. u32 val;
  1012. bool cur_state;
  1013. reg = FDI_RX_CTL(pipe);
  1014. val = I915_READ(reg);
  1015. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1016. I915_STATE_WARN(cur_state != state,
  1017. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1018. state_string(state), state_string(cur_state));
  1019. }
  1020. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1021. enum pipe pipe)
  1022. {
  1023. struct drm_device *dev = dev_priv->dev;
  1024. int pp_reg;
  1025. u32 val;
  1026. enum pipe panel_pipe = PIPE_A;
  1027. bool locked = true;
  1028. if (WARN_ON(HAS_DDI(dev)))
  1029. return;
  1030. if (HAS_PCH_SPLIT(dev)) {
  1031. u32 port_sel;
  1032. pp_reg = PCH_PP_CONTROL;
  1033. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1034. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1035. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1036. panel_pipe = PIPE_B;
  1037. /* XXX: else fix for eDP */
  1038. } else if (IS_VALLEYVIEW(dev)) {
  1039. /* presumably write lock depends on pipe, not port select */
  1040. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1041. panel_pipe = pipe;
  1042. } else {
  1043. pp_reg = PP_CONTROL;
  1044. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1045. panel_pipe = PIPE_B;
  1046. }
  1047. val = I915_READ(pp_reg);
  1048. if (!(val & PANEL_POWER_ON) ||
  1049. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1050. locked = false;
  1051. I915_STATE_WARN(panel_pipe == pipe && locked,
  1052. "panel assertion failure, pipe %c regs locked\n",
  1053. pipe_name(pipe));
  1054. }
  1055. static void assert_cursor(struct drm_i915_private *dev_priv,
  1056. enum pipe pipe, bool state)
  1057. {
  1058. struct drm_device *dev = dev_priv->dev;
  1059. bool cur_state;
  1060. if (IS_845G(dev) || IS_I865G(dev))
  1061. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1062. else
  1063. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1064. I915_STATE_WARN(cur_state != state,
  1065. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1066. pipe_name(pipe), state_string(state), state_string(cur_state));
  1067. }
  1068. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1069. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1070. void assert_pipe(struct drm_i915_private *dev_priv,
  1071. enum pipe pipe, bool state)
  1072. {
  1073. int reg;
  1074. u32 val;
  1075. bool cur_state;
  1076. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1077. pipe);
  1078. /* if we need the pipe quirk it must be always on */
  1079. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1080. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1081. state = true;
  1082. if (!intel_display_power_is_enabled(dev_priv,
  1083. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1084. cur_state = false;
  1085. } else {
  1086. reg = PIPECONF(cpu_transcoder);
  1087. val = I915_READ(reg);
  1088. cur_state = !!(val & PIPECONF_ENABLE);
  1089. }
  1090. I915_STATE_WARN(cur_state != state,
  1091. "pipe %c assertion failure (expected %s, current %s)\n",
  1092. pipe_name(pipe), state_string(state), state_string(cur_state));
  1093. }
  1094. static void assert_plane(struct drm_i915_private *dev_priv,
  1095. enum plane plane, bool state)
  1096. {
  1097. int reg;
  1098. u32 val;
  1099. bool cur_state;
  1100. reg = DSPCNTR(plane);
  1101. val = I915_READ(reg);
  1102. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1103. I915_STATE_WARN(cur_state != state,
  1104. "plane %c assertion failure (expected %s, current %s)\n",
  1105. plane_name(plane), state_string(state), state_string(cur_state));
  1106. }
  1107. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1108. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1109. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1110. enum pipe pipe)
  1111. {
  1112. struct drm_device *dev = dev_priv->dev;
  1113. int reg, i;
  1114. u32 val;
  1115. int cur_pipe;
  1116. /* Primary planes are fixed to pipes on gen4+ */
  1117. if (INTEL_INFO(dev)->gen >= 4) {
  1118. reg = DSPCNTR(pipe);
  1119. val = I915_READ(reg);
  1120. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1121. "plane %c assertion failure, should be disabled but not\n",
  1122. plane_name(pipe));
  1123. return;
  1124. }
  1125. /* Need to check both planes against the pipe */
  1126. for_each_pipe(dev_priv, i) {
  1127. reg = DSPCNTR(i);
  1128. val = I915_READ(reg);
  1129. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1130. DISPPLANE_SEL_PIPE_SHIFT;
  1131. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1132. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1133. plane_name(i), pipe_name(pipe));
  1134. }
  1135. }
  1136. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1137. enum pipe pipe)
  1138. {
  1139. struct drm_device *dev = dev_priv->dev;
  1140. int reg, sprite;
  1141. u32 val;
  1142. if (INTEL_INFO(dev)->gen >= 9) {
  1143. for_each_sprite(pipe, sprite) {
  1144. val = I915_READ(PLANE_CTL(pipe, sprite));
  1145. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1146. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1147. sprite, pipe_name(pipe));
  1148. }
  1149. } else if (IS_VALLEYVIEW(dev)) {
  1150. for_each_sprite(pipe, sprite) {
  1151. reg = SPCNTR(pipe, sprite);
  1152. val = I915_READ(reg);
  1153. I915_STATE_WARN(val & SP_ENABLE,
  1154. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1155. sprite_name(pipe, sprite), pipe_name(pipe));
  1156. }
  1157. } else if (INTEL_INFO(dev)->gen >= 7) {
  1158. reg = SPRCTL(pipe);
  1159. val = I915_READ(reg);
  1160. I915_STATE_WARN(val & SPRITE_ENABLE,
  1161. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1162. plane_name(pipe), pipe_name(pipe));
  1163. } else if (INTEL_INFO(dev)->gen >= 5) {
  1164. reg = DVSCNTR(pipe);
  1165. val = I915_READ(reg);
  1166. I915_STATE_WARN(val & DVS_ENABLE,
  1167. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1168. plane_name(pipe), pipe_name(pipe));
  1169. }
  1170. }
  1171. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1172. {
  1173. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1174. drm_crtc_vblank_put(crtc);
  1175. }
  1176. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1177. {
  1178. u32 val;
  1179. bool enabled;
  1180. I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1181. val = I915_READ(PCH_DREF_CONTROL);
  1182. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1183. DREF_SUPERSPREAD_SOURCE_MASK));
  1184. I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1185. }
  1186. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1187. enum pipe pipe)
  1188. {
  1189. int reg;
  1190. u32 val;
  1191. bool enabled;
  1192. reg = PCH_TRANSCONF(pipe);
  1193. val = I915_READ(reg);
  1194. enabled = !!(val & TRANS_ENABLE);
  1195. I915_STATE_WARN(enabled,
  1196. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1197. pipe_name(pipe));
  1198. }
  1199. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1200. enum pipe pipe, u32 port_sel, u32 val)
  1201. {
  1202. if ((val & DP_PORT_EN) == 0)
  1203. return false;
  1204. if (HAS_PCH_CPT(dev_priv->dev)) {
  1205. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1206. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1207. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1208. return false;
  1209. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1210. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1211. return false;
  1212. } else {
  1213. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1214. return false;
  1215. }
  1216. return true;
  1217. }
  1218. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1219. enum pipe pipe, u32 val)
  1220. {
  1221. if ((val & SDVO_ENABLE) == 0)
  1222. return false;
  1223. if (HAS_PCH_CPT(dev_priv->dev)) {
  1224. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1225. return false;
  1226. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1227. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1228. return false;
  1229. } else {
  1230. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1231. return false;
  1232. }
  1233. return true;
  1234. }
  1235. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1236. enum pipe pipe, u32 val)
  1237. {
  1238. if ((val & LVDS_PORT_EN) == 0)
  1239. return false;
  1240. if (HAS_PCH_CPT(dev_priv->dev)) {
  1241. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1242. return false;
  1243. } else {
  1244. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1245. return false;
  1246. }
  1247. return true;
  1248. }
  1249. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1250. enum pipe pipe, u32 val)
  1251. {
  1252. if ((val & ADPA_DAC_ENABLE) == 0)
  1253. return false;
  1254. if (HAS_PCH_CPT(dev_priv->dev)) {
  1255. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1256. return false;
  1257. } else {
  1258. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1259. return false;
  1260. }
  1261. return true;
  1262. }
  1263. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1264. enum pipe pipe, int reg, u32 port_sel)
  1265. {
  1266. u32 val = I915_READ(reg);
  1267. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1268. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1269. reg, pipe_name(pipe));
  1270. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1271. && (val & DP_PIPEB_SELECT),
  1272. "IBX PCH dp port still using transcoder B\n");
  1273. }
  1274. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1275. enum pipe pipe, int reg)
  1276. {
  1277. u32 val = I915_READ(reg);
  1278. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1279. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1280. reg, pipe_name(pipe));
  1281. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1282. && (val & SDVO_PIPE_B_SELECT),
  1283. "IBX PCH hdmi port still using transcoder B\n");
  1284. }
  1285. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1286. enum pipe pipe)
  1287. {
  1288. int reg;
  1289. u32 val;
  1290. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1291. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1292. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1293. reg = PCH_ADPA;
  1294. val = I915_READ(reg);
  1295. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1296. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1297. pipe_name(pipe));
  1298. reg = PCH_LVDS;
  1299. val = I915_READ(reg);
  1300. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1301. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1302. pipe_name(pipe));
  1303. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1304. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1305. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1306. }
  1307. static void intel_init_dpio(struct drm_device *dev)
  1308. {
  1309. struct drm_i915_private *dev_priv = dev->dev_private;
  1310. if (!IS_VALLEYVIEW(dev))
  1311. return;
  1312. /*
  1313. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1314. * CHV x1 PHY (DP/HDMI D)
  1315. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1316. */
  1317. if (IS_CHERRYVIEW(dev)) {
  1318. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1319. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1320. } else {
  1321. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1322. }
  1323. }
  1324. static void vlv_enable_pll(struct intel_crtc *crtc,
  1325. const struct intel_crtc_state *pipe_config)
  1326. {
  1327. struct drm_device *dev = crtc->base.dev;
  1328. struct drm_i915_private *dev_priv = dev->dev_private;
  1329. int reg = DPLL(crtc->pipe);
  1330. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1331. assert_pipe_disabled(dev_priv, crtc->pipe);
  1332. /* No really, not for ILK+ */
  1333. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1334. /* PLL is protected by panel, make sure we can write it */
  1335. if (IS_MOBILE(dev_priv->dev))
  1336. assert_panel_unlocked(dev_priv, crtc->pipe);
  1337. I915_WRITE(reg, dpll);
  1338. POSTING_READ(reg);
  1339. udelay(150);
  1340. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1341. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1342. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1343. POSTING_READ(DPLL_MD(crtc->pipe));
  1344. /* We do this three times for luck */
  1345. I915_WRITE(reg, dpll);
  1346. POSTING_READ(reg);
  1347. udelay(150); /* wait for warmup */
  1348. I915_WRITE(reg, dpll);
  1349. POSTING_READ(reg);
  1350. udelay(150); /* wait for warmup */
  1351. I915_WRITE(reg, dpll);
  1352. POSTING_READ(reg);
  1353. udelay(150); /* wait for warmup */
  1354. }
  1355. static void chv_enable_pll(struct intel_crtc *crtc,
  1356. const struct intel_crtc_state *pipe_config)
  1357. {
  1358. struct drm_device *dev = crtc->base.dev;
  1359. struct drm_i915_private *dev_priv = dev->dev_private;
  1360. int pipe = crtc->pipe;
  1361. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1362. u32 tmp;
  1363. assert_pipe_disabled(dev_priv, crtc->pipe);
  1364. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1365. mutex_lock(&dev_priv->dpio_lock);
  1366. /* Enable back the 10bit clock to display controller */
  1367. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1368. tmp |= DPIO_DCLKP_EN;
  1369. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1370. /*
  1371. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1372. */
  1373. udelay(1);
  1374. /* Enable PLL */
  1375. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1376. /* Check PLL is locked */
  1377. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1378. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1379. /* not sure when this should be written */
  1380. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1381. POSTING_READ(DPLL_MD(pipe));
  1382. mutex_unlock(&dev_priv->dpio_lock);
  1383. }
  1384. static int intel_num_dvo_pipes(struct drm_device *dev)
  1385. {
  1386. struct intel_crtc *crtc;
  1387. int count = 0;
  1388. for_each_intel_crtc(dev, crtc)
  1389. count += crtc->active &&
  1390. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1391. return count;
  1392. }
  1393. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1394. {
  1395. struct drm_device *dev = crtc->base.dev;
  1396. struct drm_i915_private *dev_priv = dev->dev_private;
  1397. int reg = DPLL(crtc->pipe);
  1398. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1399. assert_pipe_disabled(dev_priv, crtc->pipe);
  1400. /* No really, not for ILK+ */
  1401. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1402. /* PLL is protected by panel, make sure we can write it */
  1403. if (IS_MOBILE(dev) && !IS_I830(dev))
  1404. assert_panel_unlocked(dev_priv, crtc->pipe);
  1405. /* Enable DVO 2x clock on both PLLs if necessary */
  1406. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1407. /*
  1408. * It appears to be important that we don't enable this
  1409. * for the current pipe before otherwise configuring the
  1410. * PLL. No idea how this should be handled if multiple
  1411. * DVO outputs are enabled simultaneosly.
  1412. */
  1413. dpll |= DPLL_DVO_2X_MODE;
  1414. I915_WRITE(DPLL(!crtc->pipe),
  1415. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1416. }
  1417. /* Wait for the clocks to stabilize. */
  1418. POSTING_READ(reg);
  1419. udelay(150);
  1420. if (INTEL_INFO(dev)->gen >= 4) {
  1421. I915_WRITE(DPLL_MD(crtc->pipe),
  1422. crtc->config->dpll_hw_state.dpll_md);
  1423. } else {
  1424. /* The pixel multiplier can only be updated once the
  1425. * DPLL is enabled and the clocks are stable.
  1426. *
  1427. * So write it again.
  1428. */
  1429. I915_WRITE(reg, dpll);
  1430. }
  1431. /* We do this three times for luck */
  1432. I915_WRITE(reg, dpll);
  1433. POSTING_READ(reg);
  1434. udelay(150); /* wait for warmup */
  1435. I915_WRITE(reg, dpll);
  1436. POSTING_READ(reg);
  1437. udelay(150); /* wait for warmup */
  1438. I915_WRITE(reg, dpll);
  1439. POSTING_READ(reg);
  1440. udelay(150); /* wait for warmup */
  1441. }
  1442. /**
  1443. * i9xx_disable_pll - disable a PLL
  1444. * @dev_priv: i915 private structure
  1445. * @pipe: pipe PLL to disable
  1446. *
  1447. * Disable the PLL for @pipe, making sure the pipe is off first.
  1448. *
  1449. * Note! This is for pre-ILK only.
  1450. */
  1451. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1452. {
  1453. struct drm_device *dev = crtc->base.dev;
  1454. struct drm_i915_private *dev_priv = dev->dev_private;
  1455. enum pipe pipe = crtc->pipe;
  1456. /* Disable DVO 2x clock on both PLLs if necessary */
  1457. if (IS_I830(dev) &&
  1458. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1459. intel_num_dvo_pipes(dev) == 1) {
  1460. I915_WRITE(DPLL(PIPE_B),
  1461. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1462. I915_WRITE(DPLL(PIPE_A),
  1463. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1464. }
  1465. /* Don't disable pipe or pipe PLLs if needed */
  1466. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1467. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1468. return;
  1469. /* Make sure the pipe isn't still relying on us */
  1470. assert_pipe_disabled(dev_priv, pipe);
  1471. I915_WRITE(DPLL(pipe), 0);
  1472. POSTING_READ(DPLL(pipe));
  1473. }
  1474. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1475. {
  1476. u32 val = 0;
  1477. /* Make sure the pipe isn't still relying on us */
  1478. assert_pipe_disabled(dev_priv, pipe);
  1479. /*
  1480. * Leave integrated clock source and reference clock enabled for pipe B.
  1481. * The latter is needed for VGA hotplug / manual detection.
  1482. */
  1483. if (pipe == PIPE_B)
  1484. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1485. I915_WRITE(DPLL(pipe), val);
  1486. POSTING_READ(DPLL(pipe));
  1487. }
  1488. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1489. {
  1490. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1491. u32 val;
  1492. /* Make sure the pipe isn't still relying on us */
  1493. assert_pipe_disabled(dev_priv, pipe);
  1494. /* Set PLL en = 0 */
  1495. val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
  1496. if (pipe != PIPE_A)
  1497. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1498. I915_WRITE(DPLL(pipe), val);
  1499. POSTING_READ(DPLL(pipe));
  1500. mutex_lock(&dev_priv->dpio_lock);
  1501. /* Disable 10bit clock to display controller */
  1502. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1503. val &= ~DPIO_DCLKP_EN;
  1504. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1505. /* disable left/right clock distribution */
  1506. if (pipe != PIPE_B) {
  1507. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1508. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1509. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1510. } else {
  1511. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1512. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1513. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1514. }
  1515. mutex_unlock(&dev_priv->dpio_lock);
  1516. }
  1517. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1518. struct intel_digital_port *dport)
  1519. {
  1520. u32 port_mask;
  1521. int dpll_reg;
  1522. switch (dport->port) {
  1523. case PORT_B:
  1524. port_mask = DPLL_PORTB_READY_MASK;
  1525. dpll_reg = DPLL(0);
  1526. break;
  1527. case PORT_C:
  1528. port_mask = DPLL_PORTC_READY_MASK;
  1529. dpll_reg = DPLL(0);
  1530. break;
  1531. case PORT_D:
  1532. port_mask = DPLL_PORTD_READY_MASK;
  1533. dpll_reg = DPIO_PHY_STATUS;
  1534. break;
  1535. default:
  1536. BUG();
  1537. }
  1538. if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
  1539. WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
  1540. port_name(dport->port), I915_READ(dpll_reg));
  1541. }
  1542. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1543. {
  1544. struct drm_device *dev = crtc->base.dev;
  1545. struct drm_i915_private *dev_priv = dev->dev_private;
  1546. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1547. if (WARN_ON(pll == NULL))
  1548. return;
  1549. WARN_ON(!pll->config.crtc_mask);
  1550. if (pll->active == 0) {
  1551. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1552. WARN_ON(pll->on);
  1553. assert_shared_dpll_disabled(dev_priv, pll);
  1554. pll->mode_set(dev_priv, pll);
  1555. }
  1556. }
  1557. /**
  1558. * intel_enable_shared_dpll - enable PCH PLL
  1559. * @dev_priv: i915 private structure
  1560. * @pipe: pipe PLL to enable
  1561. *
  1562. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1563. * drives the transcoder clock.
  1564. */
  1565. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1566. {
  1567. struct drm_device *dev = crtc->base.dev;
  1568. struct drm_i915_private *dev_priv = dev->dev_private;
  1569. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1570. if (WARN_ON(pll == NULL))
  1571. return;
  1572. if (WARN_ON(pll->config.crtc_mask == 0))
  1573. return;
  1574. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1575. pll->name, pll->active, pll->on,
  1576. crtc->base.base.id);
  1577. if (pll->active++) {
  1578. WARN_ON(!pll->on);
  1579. assert_shared_dpll_enabled(dev_priv, pll);
  1580. return;
  1581. }
  1582. WARN_ON(pll->on);
  1583. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1584. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1585. pll->enable(dev_priv, pll);
  1586. pll->on = true;
  1587. }
  1588. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1589. {
  1590. struct drm_device *dev = crtc->base.dev;
  1591. struct drm_i915_private *dev_priv = dev->dev_private;
  1592. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1593. /* PCH only available on ILK+ */
  1594. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1595. if (WARN_ON(pll == NULL))
  1596. return;
  1597. if (WARN_ON(pll->config.crtc_mask == 0))
  1598. return;
  1599. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1600. pll->name, pll->active, pll->on,
  1601. crtc->base.base.id);
  1602. if (WARN_ON(pll->active == 0)) {
  1603. assert_shared_dpll_disabled(dev_priv, pll);
  1604. return;
  1605. }
  1606. assert_shared_dpll_enabled(dev_priv, pll);
  1607. WARN_ON(!pll->on);
  1608. if (--pll->active)
  1609. return;
  1610. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1611. pll->disable(dev_priv, pll);
  1612. pll->on = false;
  1613. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1614. }
  1615. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1616. enum pipe pipe)
  1617. {
  1618. struct drm_device *dev = dev_priv->dev;
  1619. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1620. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1621. uint32_t reg, val, pipeconf_val;
  1622. /* PCH only available on ILK+ */
  1623. BUG_ON(!HAS_PCH_SPLIT(dev));
  1624. /* Make sure PCH DPLL is enabled */
  1625. assert_shared_dpll_enabled(dev_priv,
  1626. intel_crtc_to_shared_dpll(intel_crtc));
  1627. /* FDI must be feeding us bits for PCH ports */
  1628. assert_fdi_tx_enabled(dev_priv, pipe);
  1629. assert_fdi_rx_enabled(dev_priv, pipe);
  1630. if (HAS_PCH_CPT(dev)) {
  1631. /* Workaround: Set the timing override bit before enabling the
  1632. * pch transcoder. */
  1633. reg = TRANS_CHICKEN2(pipe);
  1634. val = I915_READ(reg);
  1635. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1636. I915_WRITE(reg, val);
  1637. }
  1638. reg = PCH_TRANSCONF(pipe);
  1639. val = I915_READ(reg);
  1640. pipeconf_val = I915_READ(PIPECONF(pipe));
  1641. if (HAS_PCH_IBX(dev_priv->dev)) {
  1642. /*
  1643. * make the BPC in transcoder be consistent with
  1644. * that in pipeconf reg.
  1645. */
  1646. val &= ~PIPECONF_BPC_MASK;
  1647. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1648. }
  1649. val &= ~TRANS_INTERLACE_MASK;
  1650. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1651. if (HAS_PCH_IBX(dev_priv->dev) &&
  1652. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1653. val |= TRANS_LEGACY_INTERLACED_ILK;
  1654. else
  1655. val |= TRANS_INTERLACED;
  1656. else
  1657. val |= TRANS_PROGRESSIVE;
  1658. I915_WRITE(reg, val | TRANS_ENABLE);
  1659. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1660. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1661. }
  1662. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1663. enum transcoder cpu_transcoder)
  1664. {
  1665. u32 val, pipeconf_val;
  1666. /* PCH only available on ILK+ */
  1667. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1668. /* FDI must be feeding us bits for PCH ports */
  1669. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1670. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1671. /* Workaround: set timing override bit. */
  1672. val = I915_READ(_TRANSA_CHICKEN2);
  1673. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1674. I915_WRITE(_TRANSA_CHICKEN2, val);
  1675. val = TRANS_ENABLE;
  1676. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1677. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1678. PIPECONF_INTERLACED_ILK)
  1679. val |= TRANS_INTERLACED;
  1680. else
  1681. val |= TRANS_PROGRESSIVE;
  1682. I915_WRITE(LPT_TRANSCONF, val);
  1683. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1684. DRM_ERROR("Failed to enable PCH transcoder\n");
  1685. }
  1686. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1687. enum pipe pipe)
  1688. {
  1689. struct drm_device *dev = dev_priv->dev;
  1690. uint32_t reg, val;
  1691. /* FDI relies on the transcoder */
  1692. assert_fdi_tx_disabled(dev_priv, pipe);
  1693. assert_fdi_rx_disabled(dev_priv, pipe);
  1694. /* Ports must be off as well */
  1695. assert_pch_ports_disabled(dev_priv, pipe);
  1696. reg = PCH_TRANSCONF(pipe);
  1697. val = I915_READ(reg);
  1698. val &= ~TRANS_ENABLE;
  1699. I915_WRITE(reg, val);
  1700. /* wait for PCH transcoder off, transcoder state */
  1701. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1702. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1703. if (!HAS_PCH_IBX(dev)) {
  1704. /* Workaround: Clear the timing override chicken bit again. */
  1705. reg = TRANS_CHICKEN2(pipe);
  1706. val = I915_READ(reg);
  1707. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1708. I915_WRITE(reg, val);
  1709. }
  1710. }
  1711. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1712. {
  1713. u32 val;
  1714. val = I915_READ(LPT_TRANSCONF);
  1715. val &= ~TRANS_ENABLE;
  1716. I915_WRITE(LPT_TRANSCONF, val);
  1717. /* wait for PCH transcoder off, transcoder state */
  1718. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1719. DRM_ERROR("Failed to disable PCH transcoder\n");
  1720. /* Workaround: clear timing override bit. */
  1721. val = I915_READ(_TRANSA_CHICKEN2);
  1722. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1723. I915_WRITE(_TRANSA_CHICKEN2, val);
  1724. }
  1725. /**
  1726. * intel_enable_pipe - enable a pipe, asserting requirements
  1727. * @crtc: crtc responsible for the pipe
  1728. *
  1729. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1730. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1731. */
  1732. static void intel_enable_pipe(struct intel_crtc *crtc)
  1733. {
  1734. struct drm_device *dev = crtc->base.dev;
  1735. struct drm_i915_private *dev_priv = dev->dev_private;
  1736. enum pipe pipe = crtc->pipe;
  1737. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1738. pipe);
  1739. enum pipe pch_transcoder;
  1740. int reg;
  1741. u32 val;
  1742. assert_planes_disabled(dev_priv, pipe);
  1743. assert_cursor_disabled(dev_priv, pipe);
  1744. assert_sprites_disabled(dev_priv, pipe);
  1745. if (HAS_PCH_LPT(dev_priv->dev))
  1746. pch_transcoder = TRANSCODER_A;
  1747. else
  1748. pch_transcoder = pipe;
  1749. /*
  1750. * A pipe without a PLL won't actually be able to drive bits from
  1751. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1752. * need the check.
  1753. */
  1754. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1755. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1756. assert_dsi_pll_enabled(dev_priv);
  1757. else
  1758. assert_pll_enabled(dev_priv, pipe);
  1759. else {
  1760. if (crtc->config->has_pch_encoder) {
  1761. /* if driving the PCH, we need FDI enabled */
  1762. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1763. assert_fdi_tx_pll_enabled(dev_priv,
  1764. (enum pipe) cpu_transcoder);
  1765. }
  1766. /* FIXME: assert CPU port conditions for SNB+ */
  1767. }
  1768. reg = PIPECONF(cpu_transcoder);
  1769. val = I915_READ(reg);
  1770. if (val & PIPECONF_ENABLE) {
  1771. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1772. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1773. return;
  1774. }
  1775. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1776. POSTING_READ(reg);
  1777. }
  1778. /**
  1779. * intel_disable_pipe - disable a pipe, asserting requirements
  1780. * @crtc: crtc whose pipes is to be disabled
  1781. *
  1782. * Disable the pipe of @crtc, making sure that various hardware
  1783. * specific requirements are met, if applicable, e.g. plane
  1784. * disabled, panel fitter off, etc.
  1785. *
  1786. * Will wait until the pipe has shut down before returning.
  1787. */
  1788. static void intel_disable_pipe(struct intel_crtc *crtc)
  1789. {
  1790. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1791. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1792. enum pipe pipe = crtc->pipe;
  1793. int reg;
  1794. u32 val;
  1795. /*
  1796. * Make sure planes won't keep trying to pump pixels to us,
  1797. * or we might hang the display.
  1798. */
  1799. assert_planes_disabled(dev_priv, pipe);
  1800. assert_cursor_disabled(dev_priv, pipe);
  1801. assert_sprites_disabled(dev_priv, pipe);
  1802. reg = PIPECONF(cpu_transcoder);
  1803. val = I915_READ(reg);
  1804. if ((val & PIPECONF_ENABLE) == 0)
  1805. return;
  1806. /*
  1807. * Double wide has implications for planes
  1808. * so best keep it disabled when not needed.
  1809. */
  1810. if (crtc->config->double_wide)
  1811. val &= ~PIPECONF_DOUBLE_WIDE;
  1812. /* Don't disable pipe or pipe PLLs if needed */
  1813. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1814. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1815. val &= ~PIPECONF_ENABLE;
  1816. I915_WRITE(reg, val);
  1817. if ((val & PIPECONF_ENABLE) == 0)
  1818. intel_wait_for_pipe_off(crtc);
  1819. }
  1820. /*
  1821. * Plane regs are double buffered, going from enabled->disabled needs a
  1822. * trigger in order to latch. The display address reg provides this.
  1823. */
  1824. void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
  1825. enum plane plane)
  1826. {
  1827. struct drm_device *dev = dev_priv->dev;
  1828. u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
  1829. I915_WRITE(reg, I915_READ(reg));
  1830. POSTING_READ(reg);
  1831. }
  1832. /**
  1833. * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  1834. * @plane: plane to be enabled
  1835. * @crtc: crtc for the plane
  1836. *
  1837. * Enable @plane on @crtc, making sure that the pipe is running first.
  1838. */
  1839. static void intel_enable_primary_hw_plane(struct drm_plane *plane,
  1840. struct drm_crtc *crtc)
  1841. {
  1842. struct drm_device *dev = plane->dev;
  1843. struct drm_i915_private *dev_priv = dev->dev_private;
  1844. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1845. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1846. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1847. if (intel_crtc->primary_enabled)
  1848. return;
  1849. intel_crtc->primary_enabled = true;
  1850. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1851. crtc->x, crtc->y);
  1852. /*
  1853. * BDW signals flip done immediately if the plane
  1854. * is disabled, even if the plane enable is already
  1855. * armed to occur at the next vblank :(
  1856. */
  1857. if (IS_BROADWELL(dev))
  1858. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1859. }
  1860. /**
  1861. * intel_disable_primary_hw_plane - disable the primary hardware plane
  1862. * @plane: plane to be disabled
  1863. * @crtc: crtc for the plane
  1864. *
  1865. * Disable @plane on @crtc, making sure that the pipe is running first.
  1866. */
  1867. static void intel_disable_primary_hw_plane(struct drm_plane *plane,
  1868. struct drm_crtc *crtc)
  1869. {
  1870. struct drm_device *dev = plane->dev;
  1871. struct drm_i915_private *dev_priv = dev->dev_private;
  1872. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1873. if (WARN_ON(!intel_crtc->active))
  1874. return;
  1875. if (!intel_crtc->primary_enabled)
  1876. return;
  1877. intel_crtc->primary_enabled = false;
  1878. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1879. crtc->x, crtc->y);
  1880. }
  1881. static bool need_vtd_wa(struct drm_device *dev)
  1882. {
  1883. #ifdef CONFIG_INTEL_IOMMU
  1884. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1885. return true;
  1886. #endif
  1887. return false;
  1888. }
  1889. int
  1890. intel_fb_align_height(struct drm_device *dev, int height,
  1891. uint32_t pixel_format,
  1892. uint64_t fb_format_modifier)
  1893. {
  1894. int tile_height;
  1895. tile_height = fb_format_modifier == I915_FORMAT_MOD_X_TILED ?
  1896. (IS_GEN2(dev) ? 16 : 8) : 1;
  1897. return ALIGN(height, tile_height);
  1898. }
  1899. int
  1900. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  1901. struct drm_framebuffer *fb,
  1902. struct intel_engine_cs *pipelined)
  1903. {
  1904. struct drm_device *dev = fb->dev;
  1905. struct drm_i915_private *dev_priv = dev->dev_private;
  1906. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1907. u32 alignment;
  1908. int ret;
  1909. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1910. switch (obj->tiling_mode) {
  1911. case I915_TILING_NONE:
  1912. if (INTEL_INFO(dev)->gen >= 9)
  1913. alignment = 256 * 1024;
  1914. else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1915. alignment = 128 * 1024;
  1916. else if (INTEL_INFO(dev)->gen >= 4)
  1917. alignment = 4 * 1024;
  1918. else
  1919. alignment = 64 * 1024;
  1920. break;
  1921. case I915_TILING_X:
  1922. if (INTEL_INFO(dev)->gen >= 9)
  1923. alignment = 256 * 1024;
  1924. else {
  1925. /* pin() will align the object as required by fence */
  1926. alignment = 0;
  1927. }
  1928. break;
  1929. case I915_TILING_Y:
  1930. WARN(1, "Y tiled bo slipped through, driver bug!\n");
  1931. return -EINVAL;
  1932. default:
  1933. BUG();
  1934. }
  1935. /* Note that the w/a also requires 64 PTE of padding following the
  1936. * bo. We currently fill all unused PTE with the shadow page and so
  1937. * we should always have valid PTE following the scanout preventing
  1938. * the VT-d warning.
  1939. */
  1940. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1941. alignment = 256 * 1024;
  1942. /*
  1943. * Global gtt pte registers are special registers which actually forward
  1944. * writes to a chunk of system memory. Which means that there is no risk
  1945. * that the register values disappear as soon as we call
  1946. * intel_runtime_pm_put(), so it is correct to wrap only the
  1947. * pin/unpin/fence and not more.
  1948. */
  1949. intel_runtime_pm_get(dev_priv);
  1950. dev_priv->mm.interruptible = false;
  1951. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1952. if (ret)
  1953. goto err_interruptible;
  1954. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1955. * fence, whereas 965+ only requires a fence if using
  1956. * framebuffer compression. For simplicity, we always install
  1957. * a fence as the cost is not that onerous.
  1958. */
  1959. ret = i915_gem_object_get_fence(obj);
  1960. if (ret)
  1961. goto err_unpin;
  1962. i915_gem_object_pin_fence(obj);
  1963. dev_priv->mm.interruptible = true;
  1964. intel_runtime_pm_put(dev_priv);
  1965. return 0;
  1966. err_unpin:
  1967. i915_gem_object_unpin_from_display_plane(obj);
  1968. err_interruptible:
  1969. dev_priv->mm.interruptible = true;
  1970. intel_runtime_pm_put(dev_priv);
  1971. return ret;
  1972. }
  1973. void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
  1974. {
  1975. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  1976. i915_gem_object_unpin_fence(obj);
  1977. i915_gem_object_unpin_from_display_plane(obj);
  1978. }
  1979. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  1980. * is assumed to be a power-of-two. */
  1981. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  1982. unsigned int tiling_mode,
  1983. unsigned int cpp,
  1984. unsigned int pitch)
  1985. {
  1986. if (tiling_mode != I915_TILING_NONE) {
  1987. unsigned int tile_rows, tiles;
  1988. tile_rows = *y / 8;
  1989. *y %= 8;
  1990. tiles = *x / (512/cpp);
  1991. *x %= 512/cpp;
  1992. return tile_rows * pitch * 8 + tiles * 4096;
  1993. } else {
  1994. unsigned int offset;
  1995. offset = *y * pitch + *x * cpp;
  1996. *y = 0;
  1997. *x = (offset & 4095) / cpp;
  1998. return offset & -4096;
  1999. }
  2000. }
  2001. static int i9xx_format_to_fourcc(int format)
  2002. {
  2003. switch (format) {
  2004. case DISPPLANE_8BPP:
  2005. return DRM_FORMAT_C8;
  2006. case DISPPLANE_BGRX555:
  2007. return DRM_FORMAT_XRGB1555;
  2008. case DISPPLANE_BGRX565:
  2009. return DRM_FORMAT_RGB565;
  2010. default:
  2011. case DISPPLANE_BGRX888:
  2012. return DRM_FORMAT_XRGB8888;
  2013. case DISPPLANE_RGBX888:
  2014. return DRM_FORMAT_XBGR8888;
  2015. case DISPPLANE_BGRX101010:
  2016. return DRM_FORMAT_XRGB2101010;
  2017. case DISPPLANE_RGBX101010:
  2018. return DRM_FORMAT_XBGR2101010;
  2019. }
  2020. }
  2021. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2022. {
  2023. switch (format) {
  2024. case PLANE_CTL_FORMAT_RGB_565:
  2025. return DRM_FORMAT_RGB565;
  2026. default:
  2027. case PLANE_CTL_FORMAT_XRGB_8888:
  2028. if (rgb_order) {
  2029. if (alpha)
  2030. return DRM_FORMAT_ABGR8888;
  2031. else
  2032. return DRM_FORMAT_XBGR8888;
  2033. } else {
  2034. if (alpha)
  2035. return DRM_FORMAT_ARGB8888;
  2036. else
  2037. return DRM_FORMAT_XRGB8888;
  2038. }
  2039. case PLANE_CTL_FORMAT_XRGB_2101010:
  2040. if (rgb_order)
  2041. return DRM_FORMAT_XBGR2101010;
  2042. else
  2043. return DRM_FORMAT_XRGB2101010;
  2044. }
  2045. }
  2046. static bool
  2047. intel_alloc_plane_obj(struct intel_crtc *crtc,
  2048. struct intel_initial_plane_config *plane_config)
  2049. {
  2050. struct drm_device *dev = crtc->base.dev;
  2051. struct drm_i915_gem_object *obj = NULL;
  2052. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2053. struct drm_framebuffer *fb = &plane_config->fb->base;
  2054. u32 base = plane_config->base;
  2055. if (plane_config->size == 0)
  2056. return false;
  2057. obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
  2058. plane_config->size);
  2059. if (!obj)
  2060. return false;
  2061. obj->tiling_mode = plane_config->tiling;
  2062. if (obj->tiling_mode == I915_TILING_X)
  2063. obj->stride = fb->pitches[0];
  2064. mode_cmd.pixel_format = fb->pixel_format;
  2065. mode_cmd.width = fb->width;
  2066. mode_cmd.height = fb->height;
  2067. mode_cmd.pitches[0] = fb->pitches[0];
  2068. mode_cmd.modifier[0] = fb->modifier[0];
  2069. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2070. mutex_lock(&dev->struct_mutex);
  2071. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2072. &mode_cmd, obj)) {
  2073. DRM_DEBUG_KMS("intel fb init failed\n");
  2074. goto out_unref_obj;
  2075. }
  2076. obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
  2077. mutex_unlock(&dev->struct_mutex);
  2078. DRM_DEBUG_KMS("plane fb obj %p\n", obj);
  2079. return true;
  2080. out_unref_obj:
  2081. drm_gem_object_unreference(&obj->base);
  2082. mutex_unlock(&dev->struct_mutex);
  2083. return false;
  2084. }
  2085. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2086. static void
  2087. update_state_fb(struct drm_plane *plane)
  2088. {
  2089. if (plane->fb == plane->state->fb)
  2090. return;
  2091. if (plane->state->fb)
  2092. drm_framebuffer_unreference(plane->state->fb);
  2093. plane->state->fb = plane->fb;
  2094. if (plane->state->fb)
  2095. drm_framebuffer_reference(plane->state->fb);
  2096. }
  2097. static void
  2098. intel_find_plane_obj(struct intel_crtc *intel_crtc,
  2099. struct intel_initial_plane_config *plane_config)
  2100. {
  2101. struct drm_device *dev = intel_crtc->base.dev;
  2102. struct drm_i915_private *dev_priv = dev->dev_private;
  2103. struct drm_crtc *c;
  2104. struct intel_crtc *i;
  2105. struct drm_i915_gem_object *obj;
  2106. if (!plane_config->fb)
  2107. return;
  2108. if (intel_alloc_plane_obj(intel_crtc, plane_config)) {
  2109. struct drm_plane *primary = intel_crtc->base.primary;
  2110. primary->fb = &plane_config->fb->base;
  2111. primary->state->crtc = &intel_crtc->base;
  2112. update_state_fb(primary);
  2113. return;
  2114. }
  2115. kfree(plane_config->fb);
  2116. /*
  2117. * Failed to alloc the obj, check to see if we should share
  2118. * an fb with another CRTC instead
  2119. */
  2120. for_each_crtc(dev, c) {
  2121. i = to_intel_crtc(c);
  2122. if (c == &intel_crtc->base)
  2123. continue;
  2124. if (!i->active)
  2125. continue;
  2126. obj = intel_fb_obj(c->primary->fb);
  2127. if (obj == NULL)
  2128. continue;
  2129. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2130. struct drm_plane *primary = intel_crtc->base.primary;
  2131. if (obj->tiling_mode != I915_TILING_NONE)
  2132. dev_priv->preserve_bios_swizzle = true;
  2133. drm_framebuffer_reference(c->primary->fb);
  2134. primary->fb = c->primary->fb;
  2135. primary->state->crtc = &intel_crtc->base;
  2136. update_state_fb(intel_crtc->base.primary);
  2137. obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  2138. break;
  2139. }
  2140. }
  2141. }
  2142. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2143. struct drm_framebuffer *fb,
  2144. int x, int y)
  2145. {
  2146. struct drm_device *dev = crtc->dev;
  2147. struct drm_i915_private *dev_priv = dev->dev_private;
  2148. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2149. struct drm_i915_gem_object *obj;
  2150. int plane = intel_crtc->plane;
  2151. unsigned long linear_offset;
  2152. u32 dspcntr;
  2153. u32 reg = DSPCNTR(plane);
  2154. int pixel_size;
  2155. if (!intel_crtc->primary_enabled) {
  2156. I915_WRITE(reg, 0);
  2157. if (INTEL_INFO(dev)->gen >= 4)
  2158. I915_WRITE(DSPSURF(plane), 0);
  2159. else
  2160. I915_WRITE(DSPADDR(plane), 0);
  2161. POSTING_READ(reg);
  2162. return;
  2163. }
  2164. obj = intel_fb_obj(fb);
  2165. if (WARN_ON(obj == NULL))
  2166. return;
  2167. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2168. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2169. dspcntr |= DISPLAY_PLANE_ENABLE;
  2170. if (INTEL_INFO(dev)->gen < 4) {
  2171. if (intel_crtc->pipe == PIPE_B)
  2172. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2173. /* pipesrc and dspsize control the size that is scaled from,
  2174. * which should always be the user's requested size.
  2175. */
  2176. I915_WRITE(DSPSIZE(plane),
  2177. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2178. (intel_crtc->config->pipe_src_w - 1));
  2179. I915_WRITE(DSPPOS(plane), 0);
  2180. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2181. I915_WRITE(PRIMSIZE(plane),
  2182. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2183. (intel_crtc->config->pipe_src_w - 1));
  2184. I915_WRITE(PRIMPOS(plane), 0);
  2185. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2186. }
  2187. switch (fb->pixel_format) {
  2188. case DRM_FORMAT_C8:
  2189. dspcntr |= DISPPLANE_8BPP;
  2190. break;
  2191. case DRM_FORMAT_XRGB1555:
  2192. case DRM_FORMAT_ARGB1555:
  2193. dspcntr |= DISPPLANE_BGRX555;
  2194. break;
  2195. case DRM_FORMAT_RGB565:
  2196. dspcntr |= DISPPLANE_BGRX565;
  2197. break;
  2198. case DRM_FORMAT_XRGB8888:
  2199. case DRM_FORMAT_ARGB8888:
  2200. dspcntr |= DISPPLANE_BGRX888;
  2201. break;
  2202. case DRM_FORMAT_XBGR8888:
  2203. case DRM_FORMAT_ABGR8888:
  2204. dspcntr |= DISPPLANE_RGBX888;
  2205. break;
  2206. case DRM_FORMAT_XRGB2101010:
  2207. case DRM_FORMAT_ARGB2101010:
  2208. dspcntr |= DISPPLANE_BGRX101010;
  2209. break;
  2210. case DRM_FORMAT_XBGR2101010:
  2211. case DRM_FORMAT_ABGR2101010:
  2212. dspcntr |= DISPPLANE_RGBX101010;
  2213. break;
  2214. default:
  2215. BUG();
  2216. }
  2217. if (INTEL_INFO(dev)->gen >= 4 &&
  2218. obj->tiling_mode != I915_TILING_NONE)
  2219. dspcntr |= DISPPLANE_TILED;
  2220. if (IS_G4X(dev))
  2221. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2222. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2223. if (INTEL_INFO(dev)->gen >= 4) {
  2224. intel_crtc->dspaddr_offset =
  2225. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2226. pixel_size,
  2227. fb->pitches[0]);
  2228. linear_offset -= intel_crtc->dspaddr_offset;
  2229. } else {
  2230. intel_crtc->dspaddr_offset = linear_offset;
  2231. }
  2232. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2233. dspcntr |= DISPPLANE_ROTATE_180;
  2234. x += (intel_crtc->config->pipe_src_w - 1);
  2235. y += (intel_crtc->config->pipe_src_h - 1);
  2236. /* Finding the last pixel of the last line of the display
  2237. data and adding to linear_offset*/
  2238. linear_offset +=
  2239. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2240. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2241. }
  2242. I915_WRITE(reg, dspcntr);
  2243. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2244. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2245. fb->pitches[0]);
  2246. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2247. if (INTEL_INFO(dev)->gen >= 4) {
  2248. I915_WRITE(DSPSURF(plane),
  2249. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2250. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2251. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2252. } else
  2253. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2254. POSTING_READ(reg);
  2255. }
  2256. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2257. struct drm_framebuffer *fb,
  2258. int x, int y)
  2259. {
  2260. struct drm_device *dev = crtc->dev;
  2261. struct drm_i915_private *dev_priv = dev->dev_private;
  2262. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2263. struct drm_i915_gem_object *obj;
  2264. int plane = intel_crtc->plane;
  2265. unsigned long linear_offset;
  2266. u32 dspcntr;
  2267. u32 reg = DSPCNTR(plane);
  2268. int pixel_size;
  2269. if (!intel_crtc->primary_enabled) {
  2270. I915_WRITE(reg, 0);
  2271. I915_WRITE(DSPSURF(plane), 0);
  2272. POSTING_READ(reg);
  2273. return;
  2274. }
  2275. obj = intel_fb_obj(fb);
  2276. if (WARN_ON(obj == NULL))
  2277. return;
  2278. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2279. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2280. dspcntr |= DISPLAY_PLANE_ENABLE;
  2281. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2282. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2283. switch (fb->pixel_format) {
  2284. case DRM_FORMAT_C8:
  2285. dspcntr |= DISPPLANE_8BPP;
  2286. break;
  2287. case DRM_FORMAT_RGB565:
  2288. dspcntr |= DISPPLANE_BGRX565;
  2289. break;
  2290. case DRM_FORMAT_XRGB8888:
  2291. case DRM_FORMAT_ARGB8888:
  2292. dspcntr |= DISPPLANE_BGRX888;
  2293. break;
  2294. case DRM_FORMAT_XBGR8888:
  2295. case DRM_FORMAT_ABGR8888:
  2296. dspcntr |= DISPPLANE_RGBX888;
  2297. break;
  2298. case DRM_FORMAT_XRGB2101010:
  2299. case DRM_FORMAT_ARGB2101010:
  2300. dspcntr |= DISPPLANE_BGRX101010;
  2301. break;
  2302. case DRM_FORMAT_XBGR2101010:
  2303. case DRM_FORMAT_ABGR2101010:
  2304. dspcntr |= DISPPLANE_RGBX101010;
  2305. break;
  2306. default:
  2307. BUG();
  2308. }
  2309. if (obj->tiling_mode != I915_TILING_NONE)
  2310. dspcntr |= DISPPLANE_TILED;
  2311. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2312. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2313. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2314. intel_crtc->dspaddr_offset =
  2315. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2316. pixel_size,
  2317. fb->pitches[0]);
  2318. linear_offset -= intel_crtc->dspaddr_offset;
  2319. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2320. dspcntr |= DISPPLANE_ROTATE_180;
  2321. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2322. x += (intel_crtc->config->pipe_src_w - 1);
  2323. y += (intel_crtc->config->pipe_src_h - 1);
  2324. /* Finding the last pixel of the last line of the display
  2325. data and adding to linear_offset*/
  2326. linear_offset +=
  2327. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2328. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2329. }
  2330. }
  2331. I915_WRITE(reg, dspcntr);
  2332. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2333. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2334. fb->pitches[0]);
  2335. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2336. I915_WRITE(DSPSURF(plane),
  2337. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2338. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2339. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2340. } else {
  2341. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2342. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2343. }
  2344. POSTING_READ(reg);
  2345. }
  2346. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2347. struct drm_framebuffer *fb,
  2348. int x, int y)
  2349. {
  2350. struct drm_device *dev = crtc->dev;
  2351. struct drm_i915_private *dev_priv = dev->dev_private;
  2352. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2353. struct intel_framebuffer *intel_fb;
  2354. struct drm_i915_gem_object *obj;
  2355. int pipe = intel_crtc->pipe;
  2356. u32 plane_ctl, stride;
  2357. if (!intel_crtc->primary_enabled) {
  2358. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2359. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2360. POSTING_READ(PLANE_CTL(pipe, 0));
  2361. return;
  2362. }
  2363. plane_ctl = PLANE_CTL_ENABLE |
  2364. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2365. PLANE_CTL_PIPE_CSC_ENABLE;
  2366. switch (fb->pixel_format) {
  2367. case DRM_FORMAT_RGB565:
  2368. plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
  2369. break;
  2370. case DRM_FORMAT_XRGB8888:
  2371. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2372. break;
  2373. case DRM_FORMAT_XBGR8888:
  2374. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2375. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2376. break;
  2377. case DRM_FORMAT_XRGB2101010:
  2378. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2379. break;
  2380. case DRM_FORMAT_XBGR2101010:
  2381. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2382. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2383. break;
  2384. default:
  2385. BUG();
  2386. }
  2387. intel_fb = to_intel_framebuffer(fb);
  2388. obj = intel_fb->obj;
  2389. /*
  2390. * The stride is either expressed as a multiple of 64 bytes chunks for
  2391. * linear buffers or in number of tiles for tiled buffers.
  2392. */
  2393. switch (obj->tiling_mode) {
  2394. case I915_TILING_NONE:
  2395. stride = fb->pitches[0] >> 6;
  2396. break;
  2397. case I915_TILING_X:
  2398. plane_ctl |= PLANE_CTL_TILED_X;
  2399. stride = fb->pitches[0] >> 9;
  2400. break;
  2401. default:
  2402. BUG();
  2403. }
  2404. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2405. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180))
  2406. plane_ctl |= PLANE_CTL_ROTATE_180;
  2407. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2408. DRM_DEBUG_KMS("Writing base %08lX %d,%d,%d,%d pitch=%d\n",
  2409. i915_gem_obj_ggtt_offset(obj),
  2410. x, y, fb->width, fb->height,
  2411. fb->pitches[0]);
  2412. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2413. I915_WRITE(PLANE_OFFSET(pipe, 0), (y << 16) | x);
  2414. I915_WRITE(PLANE_SIZE(pipe, 0),
  2415. (intel_crtc->config->pipe_src_h - 1) << 16 |
  2416. (intel_crtc->config->pipe_src_w - 1));
  2417. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2418. I915_WRITE(PLANE_SURF(pipe, 0), i915_gem_obj_ggtt_offset(obj));
  2419. POSTING_READ(PLANE_SURF(pipe, 0));
  2420. }
  2421. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2422. static int
  2423. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2424. int x, int y, enum mode_set_atomic state)
  2425. {
  2426. struct drm_device *dev = crtc->dev;
  2427. struct drm_i915_private *dev_priv = dev->dev_private;
  2428. if (dev_priv->display.disable_fbc)
  2429. dev_priv->display.disable_fbc(dev);
  2430. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2431. return 0;
  2432. }
  2433. static void intel_complete_page_flips(struct drm_device *dev)
  2434. {
  2435. struct drm_crtc *crtc;
  2436. for_each_crtc(dev, crtc) {
  2437. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2438. enum plane plane = intel_crtc->plane;
  2439. intel_prepare_page_flip(dev, plane);
  2440. intel_finish_page_flip_plane(dev, plane);
  2441. }
  2442. }
  2443. static void intel_update_primary_planes(struct drm_device *dev)
  2444. {
  2445. struct drm_i915_private *dev_priv = dev->dev_private;
  2446. struct drm_crtc *crtc;
  2447. for_each_crtc(dev, crtc) {
  2448. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2449. drm_modeset_lock(&crtc->mutex, NULL);
  2450. /*
  2451. * FIXME: Once we have proper support for primary planes (and
  2452. * disabling them without disabling the entire crtc) allow again
  2453. * a NULL crtc->primary->fb.
  2454. */
  2455. if (intel_crtc->active && crtc->primary->fb)
  2456. dev_priv->display.update_primary_plane(crtc,
  2457. crtc->primary->fb,
  2458. crtc->x,
  2459. crtc->y);
  2460. drm_modeset_unlock(&crtc->mutex);
  2461. }
  2462. }
  2463. void intel_prepare_reset(struct drm_device *dev)
  2464. {
  2465. struct drm_i915_private *dev_priv = to_i915(dev);
  2466. struct intel_crtc *crtc;
  2467. /* no reset support for gen2 */
  2468. if (IS_GEN2(dev))
  2469. return;
  2470. /* reset doesn't touch the display */
  2471. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2472. return;
  2473. drm_modeset_lock_all(dev);
  2474. /*
  2475. * Disabling the crtcs gracefully seems nicer. Also the
  2476. * g33 docs say we should at least disable all the planes.
  2477. */
  2478. for_each_intel_crtc(dev, crtc) {
  2479. if (crtc->active)
  2480. dev_priv->display.crtc_disable(&crtc->base);
  2481. }
  2482. }
  2483. void intel_finish_reset(struct drm_device *dev)
  2484. {
  2485. struct drm_i915_private *dev_priv = to_i915(dev);
  2486. /*
  2487. * Flips in the rings will be nuked by the reset,
  2488. * so complete all pending flips so that user space
  2489. * will get its events and not get stuck.
  2490. */
  2491. intel_complete_page_flips(dev);
  2492. /* no reset support for gen2 */
  2493. if (IS_GEN2(dev))
  2494. return;
  2495. /* reset doesn't touch the display */
  2496. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2497. /*
  2498. * Flips in the rings have been nuked by the reset,
  2499. * so update the base address of all primary
  2500. * planes to the the last fb to make sure we're
  2501. * showing the correct fb after a reset.
  2502. */
  2503. intel_update_primary_planes(dev);
  2504. return;
  2505. }
  2506. /*
  2507. * The display has been reset as well,
  2508. * so need a full re-initialization.
  2509. */
  2510. intel_runtime_pm_disable_interrupts(dev_priv);
  2511. intel_runtime_pm_enable_interrupts(dev_priv);
  2512. intel_modeset_init_hw(dev);
  2513. spin_lock_irq(&dev_priv->irq_lock);
  2514. if (dev_priv->display.hpd_irq_setup)
  2515. dev_priv->display.hpd_irq_setup(dev);
  2516. spin_unlock_irq(&dev_priv->irq_lock);
  2517. intel_modeset_setup_hw_state(dev, true);
  2518. intel_hpd_init(dev_priv);
  2519. drm_modeset_unlock_all(dev);
  2520. }
  2521. static int
  2522. intel_finish_fb(struct drm_framebuffer *old_fb)
  2523. {
  2524. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2525. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2526. bool was_interruptible = dev_priv->mm.interruptible;
  2527. int ret;
  2528. /* Big Hammer, we also need to ensure that any pending
  2529. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2530. * current scanout is retired before unpinning the old
  2531. * framebuffer.
  2532. *
  2533. * This should only fail upon a hung GPU, in which case we
  2534. * can safely continue.
  2535. */
  2536. dev_priv->mm.interruptible = false;
  2537. ret = i915_gem_object_finish_gpu(obj);
  2538. dev_priv->mm.interruptible = was_interruptible;
  2539. return ret;
  2540. }
  2541. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2542. {
  2543. struct drm_device *dev = crtc->dev;
  2544. struct drm_i915_private *dev_priv = dev->dev_private;
  2545. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2546. bool pending;
  2547. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2548. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2549. return false;
  2550. spin_lock_irq(&dev->event_lock);
  2551. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2552. spin_unlock_irq(&dev->event_lock);
  2553. return pending;
  2554. }
  2555. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2556. {
  2557. struct drm_device *dev = crtc->base.dev;
  2558. struct drm_i915_private *dev_priv = dev->dev_private;
  2559. const struct drm_display_mode *adjusted_mode;
  2560. if (!i915.fastboot)
  2561. return;
  2562. /*
  2563. * Update pipe size and adjust fitter if needed: the reason for this is
  2564. * that in compute_mode_changes we check the native mode (not the pfit
  2565. * mode) to see if we can flip rather than do a full mode set. In the
  2566. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2567. * pfit state, we'll end up with a big fb scanned out into the wrong
  2568. * sized surface.
  2569. *
  2570. * To fix this properly, we need to hoist the checks up into
  2571. * compute_mode_changes (or above), check the actual pfit state and
  2572. * whether the platform allows pfit disable with pipe active, and only
  2573. * then update the pipesrc and pfit state, even on the flip path.
  2574. */
  2575. adjusted_mode = &crtc->config->base.adjusted_mode;
  2576. I915_WRITE(PIPESRC(crtc->pipe),
  2577. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2578. (adjusted_mode->crtc_vdisplay - 1));
  2579. if (!crtc->config->pch_pfit.enabled &&
  2580. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2581. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2582. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2583. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2584. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2585. }
  2586. crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
  2587. crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
  2588. }
  2589. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2590. {
  2591. struct drm_device *dev = crtc->dev;
  2592. struct drm_i915_private *dev_priv = dev->dev_private;
  2593. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2594. int pipe = intel_crtc->pipe;
  2595. u32 reg, temp;
  2596. /* enable normal train */
  2597. reg = FDI_TX_CTL(pipe);
  2598. temp = I915_READ(reg);
  2599. if (IS_IVYBRIDGE(dev)) {
  2600. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2601. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2602. } else {
  2603. temp &= ~FDI_LINK_TRAIN_NONE;
  2604. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2605. }
  2606. I915_WRITE(reg, temp);
  2607. reg = FDI_RX_CTL(pipe);
  2608. temp = I915_READ(reg);
  2609. if (HAS_PCH_CPT(dev)) {
  2610. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2611. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2612. } else {
  2613. temp &= ~FDI_LINK_TRAIN_NONE;
  2614. temp |= FDI_LINK_TRAIN_NONE;
  2615. }
  2616. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2617. /* wait one idle pattern time */
  2618. POSTING_READ(reg);
  2619. udelay(1000);
  2620. /* IVB wants error correction enabled */
  2621. if (IS_IVYBRIDGE(dev))
  2622. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2623. FDI_FE_ERRC_ENABLE);
  2624. }
  2625. static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
  2626. {
  2627. return crtc->base.enabled && crtc->active &&
  2628. crtc->config->has_pch_encoder;
  2629. }
  2630. static void ivb_modeset_global_resources(struct drm_device *dev)
  2631. {
  2632. struct drm_i915_private *dev_priv = dev->dev_private;
  2633. struct intel_crtc *pipe_B_crtc =
  2634. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  2635. struct intel_crtc *pipe_C_crtc =
  2636. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
  2637. uint32_t temp;
  2638. /*
  2639. * When everything is off disable fdi C so that we could enable fdi B
  2640. * with all lanes. Note that we don't care about enabled pipes without
  2641. * an enabled pch encoder.
  2642. */
  2643. if (!pipe_has_enabled_pch(pipe_B_crtc) &&
  2644. !pipe_has_enabled_pch(pipe_C_crtc)) {
  2645. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2646. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2647. temp = I915_READ(SOUTH_CHICKEN1);
  2648. temp &= ~FDI_BC_BIFURCATION_SELECT;
  2649. DRM_DEBUG_KMS("disabling fdi C rx\n");
  2650. I915_WRITE(SOUTH_CHICKEN1, temp);
  2651. }
  2652. }
  2653. /* The FDI link training functions for ILK/Ibexpeak. */
  2654. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2655. {
  2656. struct drm_device *dev = crtc->dev;
  2657. struct drm_i915_private *dev_priv = dev->dev_private;
  2658. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2659. int pipe = intel_crtc->pipe;
  2660. u32 reg, temp, tries;
  2661. /* FDI needs bits from pipe first */
  2662. assert_pipe_enabled(dev_priv, pipe);
  2663. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2664. for train result */
  2665. reg = FDI_RX_IMR(pipe);
  2666. temp = I915_READ(reg);
  2667. temp &= ~FDI_RX_SYMBOL_LOCK;
  2668. temp &= ~FDI_RX_BIT_LOCK;
  2669. I915_WRITE(reg, temp);
  2670. I915_READ(reg);
  2671. udelay(150);
  2672. /* enable CPU FDI TX and PCH FDI RX */
  2673. reg = FDI_TX_CTL(pipe);
  2674. temp = I915_READ(reg);
  2675. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2676. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2677. temp &= ~FDI_LINK_TRAIN_NONE;
  2678. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2679. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2680. reg = FDI_RX_CTL(pipe);
  2681. temp = I915_READ(reg);
  2682. temp &= ~FDI_LINK_TRAIN_NONE;
  2683. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2684. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2685. POSTING_READ(reg);
  2686. udelay(150);
  2687. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2688. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2689. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2690. FDI_RX_PHASE_SYNC_POINTER_EN);
  2691. reg = FDI_RX_IIR(pipe);
  2692. for (tries = 0; tries < 5; tries++) {
  2693. temp = I915_READ(reg);
  2694. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2695. if ((temp & FDI_RX_BIT_LOCK)) {
  2696. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2697. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2698. break;
  2699. }
  2700. }
  2701. if (tries == 5)
  2702. DRM_ERROR("FDI train 1 fail!\n");
  2703. /* Train 2 */
  2704. reg = FDI_TX_CTL(pipe);
  2705. temp = I915_READ(reg);
  2706. temp &= ~FDI_LINK_TRAIN_NONE;
  2707. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2708. I915_WRITE(reg, temp);
  2709. reg = FDI_RX_CTL(pipe);
  2710. temp = I915_READ(reg);
  2711. temp &= ~FDI_LINK_TRAIN_NONE;
  2712. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2713. I915_WRITE(reg, temp);
  2714. POSTING_READ(reg);
  2715. udelay(150);
  2716. reg = FDI_RX_IIR(pipe);
  2717. for (tries = 0; tries < 5; tries++) {
  2718. temp = I915_READ(reg);
  2719. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2720. if (temp & FDI_RX_SYMBOL_LOCK) {
  2721. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2722. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2723. break;
  2724. }
  2725. }
  2726. if (tries == 5)
  2727. DRM_ERROR("FDI train 2 fail!\n");
  2728. DRM_DEBUG_KMS("FDI train done\n");
  2729. }
  2730. static const int snb_b_fdi_train_param[] = {
  2731. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2732. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2733. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2734. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2735. };
  2736. /* The FDI link training functions for SNB/Cougarpoint. */
  2737. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2738. {
  2739. struct drm_device *dev = crtc->dev;
  2740. struct drm_i915_private *dev_priv = dev->dev_private;
  2741. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2742. int pipe = intel_crtc->pipe;
  2743. u32 reg, temp, i, retry;
  2744. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2745. for train result */
  2746. reg = FDI_RX_IMR(pipe);
  2747. temp = I915_READ(reg);
  2748. temp &= ~FDI_RX_SYMBOL_LOCK;
  2749. temp &= ~FDI_RX_BIT_LOCK;
  2750. I915_WRITE(reg, temp);
  2751. POSTING_READ(reg);
  2752. udelay(150);
  2753. /* enable CPU FDI TX and PCH FDI RX */
  2754. reg = FDI_TX_CTL(pipe);
  2755. temp = I915_READ(reg);
  2756. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2757. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2758. temp &= ~FDI_LINK_TRAIN_NONE;
  2759. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2760. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2761. /* SNB-B */
  2762. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2763. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2764. I915_WRITE(FDI_RX_MISC(pipe),
  2765. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2766. reg = FDI_RX_CTL(pipe);
  2767. temp = I915_READ(reg);
  2768. if (HAS_PCH_CPT(dev)) {
  2769. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2770. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2771. } else {
  2772. temp &= ~FDI_LINK_TRAIN_NONE;
  2773. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2774. }
  2775. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2776. POSTING_READ(reg);
  2777. udelay(150);
  2778. for (i = 0; i < 4; i++) {
  2779. reg = FDI_TX_CTL(pipe);
  2780. temp = I915_READ(reg);
  2781. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2782. temp |= snb_b_fdi_train_param[i];
  2783. I915_WRITE(reg, temp);
  2784. POSTING_READ(reg);
  2785. udelay(500);
  2786. for (retry = 0; retry < 5; retry++) {
  2787. reg = FDI_RX_IIR(pipe);
  2788. temp = I915_READ(reg);
  2789. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2790. if (temp & FDI_RX_BIT_LOCK) {
  2791. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2792. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2793. break;
  2794. }
  2795. udelay(50);
  2796. }
  2797. if (retry < 5)
  2798. break;
  2799. }
  2800. if (i == 4)
  2801. DRM_ERROR("FDI train 1 fail!\n");
  2802. /* Train 2 */
  2803. reg = FDI_TX_CTL(pipe);
  2804. temp = I915_READ(reg);
  2805. temp &= ~FDI_LINK_TRAIN_NONE;
  2806. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2807. if (IS_GEN6(dev)) {
  2808. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2809. /* SNB-B */
  2810. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2811. }
  2812. I915_WRITE(reg, temp);
  2813. reg = FDI_RX_CTL(pipe);
  2814. temp = I915_READ(reg);
  2815. if (HAS_PCH_CPT(dev)) {
  2816. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2817. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2818. } else {
  2819. temp &= ~FDI_LINK_TRAIN_NONE;
  2820. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2821. }
  2822. I915_WRITE(reg, temp);
  2823. POSTING_READ(reg);
  2824. udelay(150);
  2825. for (i = 0; i < 4; i++) {
  2826. reg = FDI_TX_CTL(pipe);
  2827. temp = I915_READ(reg);
  2828. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2829. temp |= snb_b_fdi_train_param[i];
  2830. I915_WRITE(reg, temp);
  2831. POSTING_READ(reg);
  2832. udelay(500);
  2833. for (retry = 0; retry < 5; retry++) {
  2834. reg = FDI_RX_IIR(pipe);
  2835. temp = I915_READ(reg);
  2836. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2837. if (temp & FDI_RX_SYMBOL_LOCK) {
  2838. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2839. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2840. break;
  2841. }
  2842. udelay(50);
  2843. }
  2844. if (retry < 5)
  2845. break;
  2846. }
  2847. if (i == 4)
  2848. DRM_ERROR("FDI train 2 fail!\n");
  2849. DRM_DEBUG_KMS("FDI train done.\n");
  2850. }
  2851. /* Manual link training for Ivy Bridge A0 parts */
  2852. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2853. {
  2854. struct drm_device *dev = crtc->dev;
  2855. struct drm_i915_private *dev_priv = dev->dev_private;
  2856. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2857. int pipe = intel_crtc->pipe;
  2858. u32 reg, temp, i, j;
  2859. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2860. for train result */
  2861. reg = FDI_RX_IMR(pipe);
  2862. temp = I915_READ(reg);
  2863. temp &= ~FDI_RX_SYMBOL_LOCK;
  2864. temp &= ~FDI_RX_BIT_LOCK;
  2865. I915_WRITE(reg, temp);
  2866. POSTING_READ(reg);
  2867. udelay(150);
  2868. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  2869. I915_READ(FDI_RX_IIR(pipe)));
  2870. /* Try each vswing and preemphasis setting twice before moving on */
  2871. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  2872. /* disable first in case we need to retry */
  2873. reg = FDI_TX_CTL(pipe);
  2874. temp = I915_READ(reg);
  2875. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2876. temp &= ~FDI_TX_ENABLE;
  2877. I915_WRITE(reg, temp);
  2878. reg = FDI_RX_CTL(pipe);
  2879. temp = I915_READ(reg);
  2880. temp &= ~FDI_LINK_TRAIN_AUTO;
  2881. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2882. temp &= ~FDI_RX_ENABLE;
  2883. I915_WRITE(reg, temp);
  2884. /* enable CPU FDI TX and PCH FDI RX */
  2885. reg = FDI_TX_CTL(pipe);
  2886. temp = I915_READ(reg);
  2887. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2888. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2889. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2890. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2891. temp |= snb_b_fdi_train_param[j/2];
  2892. temp |= FDI_COMPOSITE_SYNC;
  2893. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2894. I915_WRITE(FDI_RX_MISC(pipe),
  2895. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2896. reg = FDI_RX_CTL(pipe);
  2897. temp = I915_READ(reg);
  2898. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2899. temp |= FDI_COMPOSITE_SYNC;
  2900. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2901. POSTING_READ(reg);
  2902. udelay(1); /* should be 0.5us */
  2903. for (i = 0; i < 4; i++) {
  2904. reg = FDI_RX_IIR(pipe);
  2905. temp = I915_READ(reg);
  2906. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2907. if (temp & FDI_RX_BIT_LOCK ||
  2908. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2909. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2910. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  2911. i);
  2912. break;
  2913. }
  2914. udelay(1); /* should be 0.5us */
  2915. }
  2916. if (i == 4) {
  2917. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  2918. continue;
  2919. }
  2920. /* Train 2 */
  2921. reg = FDI_TX_CTL(pipe);
  2922. temp = I915_READ(reg);
  2923. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2924. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2925. I915_WRITE(reg, temp);
  2926. reg = FDI_RX_CTL(pipe);
  2927. temp = I915_READ(reg);
  2928. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2929. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2930. I915_WRITE(reg, temp);
  2931. POSTING_READ(reg);
  2932. udelay(2); /* should be 1.5us */
  2933. for (i = 0; i < 4; i++) {
  2934. reg = FDI_RX_IIR(pipe);
  2935. temp = I915_READ(reg);
  2936. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2937. if (temp & FDI_RX_SYMBOL_LOCK ||
  2938. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  2939. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2940. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  2941. i);
  2942. goto train_done;
  2943. }
  2944. udelay(2); /* should be 1.5us */
  2945. }
  2946. if (i == 4)
  2947. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  2948. }
  2949. train_done:
  2950. DRM_DEBUG_KMS("FDI train done.\n");
  2951. }
  2952. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  2953. {
  2954. struct drm_device *dev = intel_crtc->base.dev;
  2955. struct drm_i915_private *dev_priv = dev->dev_private;
  2956. int pipe = intel_crtc->pipe;
  2957. u32 reg, temp;
  2958. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2959. reg = FDI_RX_CTL(pipe);
  2960. temp = I915_READ(reg);
  2961. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  2962. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2963. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2964. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2965. POSTING_READ(reg);
  2966. udelay(200);
  2967. /* Switch from Rawclk to PCDclk */
  2968. temp = I915_READ(reg);
  2969. I915_WRITE(reg, temp | FDI_PCDCLK);
  2970. POSTING_READ(reg);
  2971. udelay(200);
  2972. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2973. reg = FDI_TX_CTL(pipe);
  2974. temp = I915_READ(reg);
  2975. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2976. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2977. POSTING_READ(reg);
  2978. udelay(100);
  2979. }
  2980. }
  2981. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  2982. {
  2983. struct drm_device *dev = intel_crtc->base.dev;
  2984. struct drm_i915_private *dev_priv = dev->dev_private;
  2985. int pipe = intel_crtc->pipe;
  2986. u32 reg, temp;
  2987. /* Switch from PCDclk to Rawclk */
  2988. reg = FDI_RX_CTL(pipe);
  2989. temp = I915_READ(reg);
  2990. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2991. /* Disable CPU FDI TX PLL */
  2992. reg = FDI_TX_CTL(pipe);
  2993. temp = I915_READ(reg);
  2994. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2995. POSTING_READ(reg);
  2996. udelay(100);
  2997. reg = FDI_RX_CTL(pipe);
  2998. temp = I915_READ(reg);
  2999. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3000. /* Wait for the clocks to turn off. */
  3001. POSTING_READ(reg);
  3002. udelay(100);
  3003. }
  3004. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3005. {
  3006. struct drm_device *dev = crtc->dev;
  3007. struct drm_i915_private *dev_priv = dev->dev_private;
  3008. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3009. int pipe = intel_crtc->pipe;
  3010. u32 reg, temp;
  3011. /* disable CPU FDI tx and PCH FDI rx */
  3012. reg = FDI_TX_CTL(pipe);
  3013. temp = I915_READ(reg);
  3014. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3015. POSTING_READ(reg);
  3016. reg = FDI_RX_CTL(pipe);
  3017. temp = I915_READ(reg);
  3018. temp &= ~(0x7 << 16);
  3019. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3020. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3021. POSTING_READ(reg);
  3022. udelay(100);
  3023. /* Ironlake workaround, disable clock pointer after downing FDI */
  3024. if (HAS_PCH_IBX(dev))
  3025. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3026. /* still set train pattern 1 */
  3027. reg = FDI_TX_CTL(pipe);
  3028. temp = I915_READ(reg);
  3029. temp &= ~FDI_LINK_TRAIN_NONE;
  3030. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3031. I915_WRITE(reg, temp);
  3032. reg = FDI_RX_CTL(pipe);
  3033. temp = I915_READ(reg);
  3034. if (HAS_PCH_CPT(dev)) {
  3035. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3036. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3037. } else {
  3038. temp &= ~FDI_LINK_TRAIN_NONE;
  3039. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3040. }
  3041. /* BPC in FDI rx is consistent with that in PIPECONF */
  3042. temp &= ~(0x07 << 16);
  3043. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3044. I915_WRITE(reg, temp);
  3045. POSTING_READ(reg);
  3046. udelay(100);
  3047. }
  3048. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3049. {
  3050. struct intel_crtc *crtc;
  3051. /* Note that we don't need to be called with mode_config.lock here
  3052. * as our list of CRTC objects is static for the lifetime of the
  3053. * device and so cannot disappear as we iterate. Similarly, we can
  3054. * happily treat the predicates as racy, atomic checks as userspace
  3055. * cannot claim and pin a new fb without at least acquring the
  3056. * struct_mutex and so serialising with us.
  3057. */
  3058. for_each_intel_crtc(dev, crtc) {
  3059. if (atomic_read(&crtc->unpin_work_count) == 0)
  3060. continue;
  3061. if (crtc->unpin_work)
  3062. intel_wait_for_vblank(dev, crtc->pipe);
  3063. return true;
  3064. }
  3065. return false;
  3066. }
  3067. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3068. {
  3069. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3070. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3071. /* ensure that the unpin work is consistent wrt ->pending. */
  3072. smp_rmb();
  3073. intel_crtc->unpin_work = NULL;
  3074. if (work->event)
  3075. drm_send_vblank_event(intel_crtc->base.dev,
  3076. intel_crtc->pipe,
  3077. work->event);
  3078. drm_crtc_vblank_put(&intel_crtc->base);
  3079. wake_up_all(&dev_priv->pending_flip_queue);
  3080. queue_work(dev_priv->wq, &work->work);
  3081. trace_i915_flip_complete(intel_crtc->plane,
  3082. work->pending_flip_obj);
  3083. }
  3084. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3085. {
  3086. struct drm_device *dev = crtc->dev;
  3087. struct drm_i915_private *dev_priv = dev->dev_private;
  3088. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3089. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3090. !intel_crtc_has_pending_flip(crtc),
  3091. 60*HZ) == 0)) {
  3092. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3093. spin_lock_irq(&dev->event_lock);
  3094. if (intel_crtc->unpin_work) {
  3095. WARN_ONCE(1, "Removing stuck page flip\n");
  3096. page_flip_completed(intel_crtc);
  3097. }
  3098. spin_unlock_irq(&dev->event_lock);
  3099. }
  3100. if (crtc->primary->fb) {
  3101. mutex_lock(&dev->struct_mutex);
  3102. intel_finish_fb(crtc->primary->fb);
  3103. mutex_unlock(&dev->struct_mutex);
  3104. }
  3105. }
  3106. /* Program iCLKIP clock to the desired frequency */
  3107. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3108. {
  3109. struct drm_device *dev = crtc->dev;
  3110. struct drm_i915_private *dev_priv = dev->dev_private;
  3111. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3112. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3113. u32 temp;
  3114. mutex_lock(&dev_priv->dpio_lock);
  3115. /* It is necessary to ungate the pixclk gate prior to programming
  3116. * the divisors, and gate it back when it is done.
  3117. */
  3118. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3119. /* Disable SSCCTL */
  3120. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3121. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3122. SBI_SSCCTL_DISABLE,
  3123. SBI_ICLK);
  3124. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3125. if (clock == 20000) {
  3126. auxdiv = 1;
  3127. divsel = 0x41;
  3128. phaseinc = 0x20;
  3129. } else {
  3130. /* The iCLK virtual clock root frequency is in MHz,
  3131. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3132. * divisors, it is necessary to divide one by another, so we
  3133. * convert the virtual clock precision to KHz here for higher
  3134. * precision.
  3135. */
  3136. u32 iclk_virtual_root_freq = 172800 * 1000;
  3137. u32 iclk_pi_range = 64;
  3138. u32 desired_divisor, msb_divisor_value, pi_value;
  3139. desired_divisor = (iclk_virtual_root_freq / clock);
  3140. msb_divisor_value = desired_divisor / iclk_pi_range;
  3141. pi_value = desired_divisor % iclk_pi_range;
  3142. auxdiv = 0;
  3143. divsel = msb_divisor_value - 2;
  3144. phaseinc = pi_value;
  3145. }
  3146. /* This should not happen with any sane values */
  3147. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3148. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3149. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3150. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3151. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3152. clock,
  3153. auxdiv,
  3154. divsel,
  3155. phasedir,
  3156. phaseinc);
  3157. /* Program SSCDIVINTPHASE6 */
  3158. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3159. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3160. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3161. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3162. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3163. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3164. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3165. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3166. /* Program SSCAUXDIV */
  3167. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3168. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3169. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3170. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3171. /* Enable modulator and associated divider */
  3172. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3173. temp &= ~SBI_SSCCTL_DISABLE;
  3174. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3175. /* Wait for initialization time */
  3176. udelay(24);
  3177. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3178. mutex_unlock(&dev_priv->dpio_lock);
  3179. }
  3180. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3181. enum pipe pch_transcoder)
  3182. {
  3183. struct drm_device *dev = crtc->base.dev;
  3184. struct drm_i915_private *dev_priv = dev->dev_private;
  3185. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3186. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3187. I915_READ(HTOTAL(cpu_transcoder)));
  3188. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3189. I915_READ(HBLANK(cpu_transcoder)));
  3190. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3191. I915_READ(HSYNC(cpu_transcoder)));
  3192. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3193. I915_READ(VTOTAL(cpu_transcoder)));
  3194. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3195. I915_READ(VBLANK(cpu_transcoder)));
  3196. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3197. I915_READ(VSYNC(cpu_transcoder)));
  3198. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3199. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3200. }
  3201. static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
  3202. {
  3203. struct drm_i915_private *dev_priv = dev->dev_private;
  3204. uint32_t temp;
  3205. temp = I915_READ(SOUTH_CHICKEN1);
  3206. if (temp & FDI_BC_BIFURCATION_SELECT)
  3207. return;
  3208. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3209. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3210. temp |= FDI_BC_BIFURCATION_SELECT;
  3211. DRM_DEBUG_KMS("enabling fdi C rx\n");
  3212. I915_WRITE(SOUTH_CHICKEN1, temp);
  3213. POSTING_READ(SOUTH_CHICKEN1);
  3214. }
  3215. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3216. {
  3217. struct drm_device *dev = intel_crtc->base.dev;
  3218. struct drm_i915_private *dev_priv = dev->dev_private;
  3219. switch (intel_crtc->pipe) {
  3220. case PIPE_A:
  3221. break;
  3222. case PIPE_B:
  3223. if (intel_crtc->config->fdi_lanes > 2)
  3224. WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
  3225. else
  3226. cpt_enable_fdi_bc_bifurcation(dev);
  3227. break;
  3228. case PIPE_C:
  3229. cpt_enable_fdi_bc_bifurcation(dev);
  3230. break;
  3231. default:
  3232. BUG();
  3233. }
  3234. }
  3235. /*
  3236. * Enable PCH resources required for PCH ports:
  3237. * - PCH PLLs
  3238. * - FDI training & RX/TX
  3239. * - update transcoder timings
  3240. * - DP transcoding bits
  3241. * - transcoder
  3242. */
  3243. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3244. {
  3245. struct drm_device *dev = crtc->dev;
  3246. struct drm_i915_private *dev_priv = dev->dev_private;
  3247. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3248. int pipe = intel_crtc->pipe;
  3249. u32 reg, temp;
  3250. assert_pch_transcoder_disabled(dev_priv, pipe);
  3251. if (IS_IVYBRIDGE(dev))
  3252. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3253. /* Write the TU size bits before fdi link training, so that error
  3254. * detection works. */
  3255. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3256. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3257. /* For PCH output, training FDI link */
  3258. dev_priv->display.fdi_link_train(crtc);
  3259. /* We need to program the right clock selection before writing the pixel
  3260. * mutliplier into the DPLL. */
  3261. if (HAS_PCH_CPT(dev)) {
  3262. u32 sel;
  3263. temp = I915_READ(PCH_DPLL_SEL);
  3264. temp |= TRANS_DPLL_ENABLE(pipe);
  3265. sel = TRANS_DPLLB_SEL(pipe);
  3266. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3267. temp |= sel;
  3268. else
  3269. temp &= ~sel;
  3270. I915_WRITE(PCH_DPLL_SEL, temp);
  3271. }
  3272. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3273. * transcoder, and we actually should do this to not upset any PCH
  3274. * transcoder that already use the clock when we share it.
  3275. *
  3276. * Note that enable_shared_dpll tries to do the right thing, but
  3277. * get_shared_dpll unconditionally resets the pll - we need that to have
  3278. * the right LVDS enable sequence. */
  3279. intel_enable_shared_dpll(intel_crtc);
  3280. /* set transcoder timing, panel must allow it */
  3281. assert_panel_unlocked(dev_priv, pipe);
  3282. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3283. intel_fdi_normal_train(crtc);
  3284. /* For PCH DP, enable TRANS_DP_CTL */
  3285. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3286. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3287. reg = TRANS_DP_CTL(pipe);
  3288. temp = I915_READ(reg);
  3289. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3290. TRANS_DP_SYNC_MASK |
  3291. TRANS_DP_BPC_MASK);
  3292. temp |= (TRANS_DP_OUTPUT_ENABLE |
  3293. TRANS_DP_ENH_FRAMING);
  3294. temp |= bpc << 9; /* same format but at 11:9 */
  3295. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3296. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3297. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3298. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3299. switch (intel_trans_dp_port_sel(crtc)) {
  3300. case PCH_DP_B:
  3301. temp |= TRANS_DP_PORT_SEL_B;
  3302. break;
  3303. case PCH_DP_C:
  3304. temp |= TRANS_DP_PORT_SEL_C;
  3305. break;
  3306. case PCH_DP_D:
  3307. temp |= TRANS_DP_PORT_SEL_D;
  3308. break;
  3309. default:
  3310. BUG();
  3311. }
  3312. I915_WRITE(reg, temp);
  3313. }
  3314. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3315. }
  3316. static void lpt_pch_enable(struct drm_crtc *crtc)
  3317. {
  3318. struct drm_device *dev = crtc->dev;
  3319. struct drm_i915_private *dev_priv = dev->dev_private;
  3320. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3321. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3322. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3323. lpt_program_iclkip(crtc);
  3324. /* Set transcoder timing. */
  3325. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3326. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3327. }
  3328. void intel_put_shared_dpll(struct intel_crtc *crtc)
  3329. {
  3330. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3331. if (pll == NULL)
  3332. return;
  3333. if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
  3334. WARN(1, "bad %s crtc mask\n", pll->name);
  3335. return;
  3336. }
  3337. pll->config.crtc_mask &= ~(1 << crtc->pipe);
  3338. if (pll->config.crtc_mask == 0) {
  3339. WARN_ON(pll->on);
  3340. WARN_ON(pll->active);
  3341. }
  3342. crtc->config->shared_dpll = DPLL_ID_PRIVATE;
  3343. }
  3344. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3345. struct intel_crtc_state *crtc_state)
  3346. {
  3347. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3348. struct intel_shared_dpll *pll;
  3349. enum intel_dpll_id i;
  3350. if (HAS_PCH_IBX(dev_priv->dev)) {
  3351. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3352. i = (enum intel_dpll_id) crtc->pipe;
  3353. pll = &dev_priv->shared_dplls[i];
  3354. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3355. crtc->base.base.id, pll->name);
  3356. WARN_ON(pll->new_config->crtc_mask);
  3357. goto found;
  3358. }
  3359. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3360. pll = &dev_priv->shared_dplls[i];
  3361. /* Only want to check enabled timings first */
  3362. if (pll->new_config->crtc_mask == 0)
  3363. continue;
  3364. if (memcmp(&crtc_state->dpll_hw_state,
  3365. &pll->new_config->hw_state,
  3366. sizeof(pll->new_config->hw_state)) == 0) {
  3367. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3368. crtc->base.base.id, pll->name,
  3369. pll->new_config->crtc_mask,
  3370. pll->active);
  3371. goto found;
  3372. }
  3373. }
  3374. /* Ok no matching timings, maybe there's a free one? */
  3375. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3376. pll = &dev_priv->shared_dplls[i];
  3377. if (pll->new_config->crtc_mask == 0) {
  3378. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3379. crtc->base.base.id, pll->name);
  3380. goto found;
  3381. }
  3382. }
  3383. return NULL;
  3384. found:
  3385. if (pll->new_config->crtc_mask == 0)
  3386. pll->new_config->hw_state = crtc_state->dpll_hw_state;
  3387. crtc_state->shared_dpll = i;
  3388. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3389. pipe_name(crtc->pipe));
  3390. pll->new_config->crtc_mask |= 1 << crtc->pipe;
  3391. return pll;
  3392. }
  3393. /**
  3394. * intel_shared_dpll_start_config - start a new PLL staged config
  3395. * @dev_priv: DRM device
  3396. * @clear_pipes: mask of pipes that will have their PLLs freed
  3397. *
  3398. * Starts a new PLL staged config, copying the current config but
  3399. * releasing the references of pipes specified in clear_pipes.
  3400. */
  3401. static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
  3402. unsigned clear_pipes)
  3403. {
  3404. struct intel_shared_dpll *pll;
  3405. enum intel_dpll_id i;
  3406. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3407. pll = &dev_priv->shared_dplls[i];
  3408. pll->new_config = kmemdup(&pll->config, sizeof pll->config,
  3409. GFP_KERNEL);
  3410. if (!pll->new_config)
  3411. goto cleanup;
  3412. pll->new_config->crtc_mask &= ~clear_pipes;
  3413. }
  3414. return 0;
  3415. cleanup:
  3416. while (--i >= 0) {
  3417. pll = &dev_priv->shared_dplls[i];
  3418. kfree(pll->new_config);
  3419. pll->new_config = NULL;
  3420. }
  3421. return -ENOMEM;
  3422. }
  3423. static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
  3424. {
  3425. struct intel_shared_dpll *pll;
  3426. enum intel_dpll_id i;
  3427. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3428. pll = &dev_priv->shared_dplls[i];
  3429. WARN_ON(pll->new_config == &pll->config);
  3430. pll->config = *pll->new_config;
  3431. kfree(pll->new_config);
  3432. pll->new_config = NULL;
  3433. }
  3434. }
  3435. static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
  3436. {
  3437. struct intel_shared_dpll *pll;
  3438. enum intel_dpll_id i;
  3439. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3440. pll = &dev_priv->shared_dplls[i];
  3441. WARN_ON(pll->new_config == &pll->config);
  3442. kfree(pll->new_config);
  3443. pll->new_config = NULL;
  3444. }
  3445. }
  3446. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3447. {
  3448. struct drm_i915_private *dev_priv = dev->dev_private;
  3449. int dslreg = PIPEDSL(pipe);
  3450. u32 temp;
  3451. temp = I915_READ(dslreg);
  3452. udelay(500);
  3453. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3454. if (wait_for(I915_READ(dslreg) != temp, 5))
  3455. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3456. }
  3457. }
  3458. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3459. {
  3460. struct drm_device *dev = crtc->base.dev;
  3461. struct drm_i915_private *dev_priv = dev->dev_private;
  3462. int pipe = crtc->pipe;
  3463. if (crtc->config->pch_pfit.enabled) {
  3464. I915_WRITE(PS_CTL(pipe), PS_ENABLE);
  3465. I915_WRITE(PS_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3466. I915_WRITE(PS_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3467. }
  3468. }
  3469. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3470. {
  3471. struct drm_device *dev = crtc->base.dev;
  3472. struct drm_i915_private *dev_priv = dev->dev_private;
  3473. int pipe = crtc->pipe;
  3474. if (crtc->config->pch_pfit.enabled) {
  3475. /* Force use of hard-coded filter coefficients
  3476. * as some pre-programmed values are broken,
  3477. * e.g. x201.
  3478. */
  3479. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3480. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3481. PF_PIPE_SEL_IVB(pipe));
  3482. else
  3483. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3484. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3485. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3486. }
  3487. }
  3488. static void intel_enable_sprite_planes(struct drm_crtc *crtc)
  3489. {
  3490. struct drm_device *dev = crtc->dev;
  3491. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3492. struct drm_plane *plane;
  3493. struct intel_plane *intel_plane;
  3494. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3495. intel_plane = to_intel_plane(plane);
  3496. if (intel_plane->pipe == pipe)
  3497. intel_plane_restore(&intel_plane->base);
  3498. }
  3499. }
  3500. static void intel_disable_sprite_planes(struct drm_crtc *crtc)
  3501. {
  3502. struct drm_device *dev = crtc->dev;
  3503. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3504. struct drm_plane *plane;
  3505. struct intel_plane *intel_plane;
  3506. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3507. intel_plane = to_intel_plane(plane);
  3508. if (intel_plane->pipe == pipe)
  3509. plane->funcs->disable_plane(plane);
  3510. }
  3511. }
  3512. void hsw_enable_ips(struct intel_crtc *crtc)
  3513. {
  3514. struct drm_device *dev = crtc->base.dev;
  3515. struct drm_i915_private *dev_priv = dev->dev_private;
  3516. if (!crtc->config->ips_enabled)
  3517. return;
  3518. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3519. intel_wait_for_vblank(dev, crtc->pipe);
  3520. assert_plane_enabled(dev_priv, crtc->plane);
  3521. if (IS_BROADWELL(dev)) {
  3522. mutex_lock(&dev_priv->rps.hw_lock);
  3523. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3524. mutex_unlock(&dev_priv->rps.hw_lock);
  3525. /* Quoting Art Runyan: "its not safe to expect any particular
  3526. * value in IPS_CTL bit 31 after enabling IPS through the
  3527. * mailbox." Moreover, the mailbox may return a bogus state,
  3528. * so we need to just enable it and continue on.
  3529. */
  3530. } else {
  3531. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3532. /* The bit only becomes 1 in the next vblank, so this wait here
  3533. * is essentially intel_wait_for_vblank. If we don't have this
  3534. * and don't wait for vblanks until the end of crtc_enable, then
  3535. * the HW state readout code will complain that the expected
  3536. * IPS_CTL value is not the one we read. */
  3537. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3538. DRM_ERROR("Timed out waiting for IPS enable\n");
  3539. }
  3540. }
  3541. void hsw_disable_ips(struct intel_crtc *crtc)
  3542. {
  3543. struct drm_device *dev = crtc->base.dev;
  3544. struct drm_i915_private *dev_priv = dev->dev_private;
  3545. if (!crtc->config->ips_enabled)
  3546. return;
  3547. assert_plane_enabled(dev_priv, crtc->plane);
  3548. if (IS_BROADWELL(dev)) {
  3549. mutex_lock(&dev_priv->rps.hw_lock);
  3550. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3551. mutex_unlock(&dev_priv->rps.hw_lock);
  3552. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3553. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3554. DRM_ERROR("Timed out waiting for IPS disable\n");
  3555. } else {
  3556. I915_WRITE(IPS_CTL, 0);
  3557. POSTING_READ(IPS_CTL);
  3558. }
  3559. /* We need to wait for a vblank before we can disable the plane. */
  3560. intel_wait_for_vblank(dev, crtc->pipe);
  3561. }
  3562. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3563. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3564. {
  3565. struct drm_device *dev = crtc->dev;
  3566. struct drm_i915_private *dev_priv = dev->dev_private;
  3567. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3568. enum pipe pipe = intel_crtc->pipe;
  3569. int palreg = PALETTE(pipe);
  3570. int i;
  3571. bool reenable_ips = false;
  3572. /* The clocks have to be on to load the palette. */
  3573. if (!crtc->enabled || !intel_crtc->active)
  3574. return;
  3575. if (!HAS_PCH_SPLIT(dev_priv->dev)) {
  3576. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3577. assert_dsi_pll_enabled(dev_priv);
  3578. else
  3579. assert_pll_enabled(dev_priv, pipe);
  3580. }
  3581. /* use legacy palette for Ironlake */
  3582. if (!HAS_GMCH_DISPLAY(dev))
  3583. palreg = LGC_PALETTE(pipe);
  3584. /* Workaround : Do not read or write the pipe palette/gamma data while
  3585. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3586. */
  3587. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  3588. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3589. GAMMA_MODE_MODE_SPLIT)) {
  3590. hsw_disable_ips(intel_crtc);
  3591. reenable_ips = true;
  3592. }
  3593. for (i = 0; i < 256; i++) {
  3594. I915_WRITE(palreg + 4 * i,
  3595. (intel_crtc->lut_r[i] << 16) |
  3596. (intel_crtc->lut_g[i] << 8) |
  3597. intel_crtc->lut_b[i]);
  3598. }
  3599. if (reenable_ips)
  3600. hsw_enable_ips(intel_crtc);
  3601. }
  3602. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  3603. {
  3604. if (!enable && intel_crtc->overlay) {
  3605. struct drm_device *dev = intel_crtc->base.dev;
  3606. struct drm_i915_private *dev_priv = dev->dev_private;
  3607. mutex_lock(&dev->struct_mutex);
  3608. dev_priv->mm.interruptible = false;
  3609. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3610. dev_priv->mm.interruptible = true;
  3611. mutex_unlock(&dev->struct_mutex);
  3612. }
  3613. /* Let userspace switch the overlay on again. In most cases userspace
  3614. * has to recompute where to put it anyway.
  3615. */
  3616. }
  3617. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  3618. {
  3619. struct drm_device *dev = crtc->dev;
  3620. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3621. int pipe = intel_crtc->pipe;
  3622. intel_enable_primary_hw_plane(crtc->primary, crtc);
  3623. intel_enable_sprite_planes(crtc);
  3624. intel_crtc_update_cursor(crtc, true);
  3625. intel_crtc_dpms_overlay(intel_crtc, true);
  3626. hsw_enable_ips(intel_crtc);
  3627. mutex_lock(&dev->struct_mutex);
  3628. intel_fbc_update(dev);
  3629. mutex_unlock(&dev->struct_mutex);
  3630. /*
  3631. * FIXME: Once we grow proper nuclear flip support out of this we need
  3632. * to compute the mask of flip planes precisely. For the time being
  3633. * consider this a flip from a NULL plane.
  3634. */
  3635. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3636. }
  3637. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  3638. {
  3639. struct drm_device *dev = crtc->dev;
  3640. struct drm_i915_private *dev_priv = dev->dev_private;
  3641. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3642. int pipe = intel_crtc->pipe;
  3643. intel_crtc_wait_for_pending_flips(crtc);
  3644. if (dev_priv->fbc.crtc == intel_crtc)
  3645. intel_fbc_disable(dev);
  3646. hsw_disable_ips(intel_crtc);
  3647. intel_crtc_dpms_overlay(intel_crtc, false);
  3648. intel_crtc_update_cursor(crtc, false);
  3649. intel_disable_sprite_planes(crtc);
  3650. intel_disable_primary_hw_plane(crtc->primary, crtc);
  3651. /*
  3652. * FIXME: Once we grow proper nuclear flip support out of this we need
  3653. * to compute the mask of flip planes precisely. For the time being
  3654. * consider this a flip to a NULL plane.
  3655. */
  3656. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3657. }
  3658. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  3659. {
  3660. struct drm_device *dev = crtc->dev;
  3661. struct drm_i915_private *dev_priv = dev->dev_private;
  3662. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3663. struct intel_encoder *encoder;
  3664. int pipe = intel_crtc->pipe;
  3665. WARN_ON(!crtc->enabled);
  3666. if (intel_crtc->active)
  3667. return;
  3668. if (intel_crtc->config->has_pch_encoder)
  3669. intel_prepare_shared_dpll(intel_crtc);
  3670. if (intel_crtc->config->has_dp_encoder)
  3671. intel_dp_set_m_n(intel_crtc);
  3672. intel_set_pipe_timings(intel_crtc);
  3673. if (intel_crtc->config->has_pch_encoder) {
  3674. intel_cpu_transcoder_set_m_n(intel_crtc,
  3675. &intel_crtc->config->fdi_m_n, NULL);
  3676. }
  3677. ironlake_set_pipeconf(crtc);
  3678. intel_crtc->active = true;
  3679. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3680. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  3681. for_each_encoder_on_crtc(dev, crtc, encoder)
  3682. if (encoder->pre_enable)
  3683. encoder->pre_enable(encoder);
  3684. if (intel_crtc->config->has_pch_encoder) {
  3685. /* Note: FDI PLL enabling _must_ be done before we enable the
  3686. * cpu pipes, hence this is separate from all the other fdi/pch
  3687. * enabling. */
  3688. ironlake_fdi_pll_enable(intel_crtc);
  3689. } else {
  3690. assert_fdi_tx_disabled(dev_priv, pipe);
  3691. assert_fdi_rx_disabled(dev_priv, pipe);
  3692. }
  3693. ironlake_pfit_enable(intel_crtc);
  3694. /*
  3695. * On ILK+ LUT must be loaded before the pipe is running but with
  3696. * clocks enabled
  3697. */
  3698. intel_crtc_load_lut(crtc);
  3699. intel_update_watermarks(crtc);
  3700. intel_enable_pipe(intel_crtc);
  3701. if (intel_crtc->config->has_pch_encoder)
  3702. ironlake_pch_enable(crtc);
  3703. assert_vblank_disabled(crtc);
  3704. drm_crtc_vblank_on(crtc);
  3705. for_each_encoder_on_crtc(dev, crtc, encoder)
  3706. encoder->enable(encoder);
  3707. if (HAS_PCH_CPT(dev))
  3708. cpt_verify_modeset(dev, intel_crtc->pipe);
  3709. intel_crtc_enable_planes(crtc);
  3710. }
  3711. /* IPS only exists on ULT machines and is tied to pipe A. */
  3712. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  3713. {
  3714. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  3715. }
  3716. /*
  3717. * This implements the workaround described in the "notes" section of the mode
  3718. * set sequence documentation. When going from no pipes or single pipe to
  3719. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  3720. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  3721. */
  3722. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  3723. {
  3724. struct drm_device *dev = crtc->base.dev;
  3725. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  3726. /* We want to get the other_active_crtc only if there's only 1 other
  3727. * active crtc. */
  3728. for_each_intel_crtc(dev, crtc_it) {
  3729. if (!crtc_it->active || crtc_it == crtc)
  3730. continue;
  3731. if (other_active_crtc)
  3732. return;
  3733. other_active_crtc = crtc_it;
  3734. }
  3735. if (!other_active_crtc)
  3736. return;
  3737. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3738. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3739. }
  3740. static void haswell_crtc_enable(struct drm_crtc *crtc)
  3741. {
  3742. struct drm_device *dev = crtc->dev;
  3743. struct drm_i915_private *dev_priv = dev->dev_private;
  3744. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3745. struct intel_encoder *encoder;
  3746. int pipe = intel_crtc->pipe;
  3747. WARN_ON(!crtc->enabled);
  3748. if (intel_crtc->active)
  3749. return;
  3750. if (intel_crtc_to_shared_dpll(intel_crtc))
  3751. intel_enable_shared_dpll(intel_crtc);
  3752. if (intel_crtc->config->has_dp_encoder)
  3753. intel_dp_set_m_n(intel_crtc);
  3754. intel_set_pipe_timings(intel_crtc);
  3755. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  3756. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  3757. intel_crtc->config->pixel_multiplier - 1);
  3758. }
  3759. if (intel_crtc->config->has_pch_encoder) {
  3760. intel_cpu_transcoder_set_m_n(intel_crtc,
  3761. &intel_crtc->config->fdi_m_n, NULL);
  3762. }
  3763. haswell_set_pipeconf(crtc);
  3764. intel_set_pipe_csc(crtc);
  3765. intel_crtc->active = true;
  3766. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3767. for_each_encoder_on_crtc(dev, crtc, encoder)
  3768. if (encoder->pre_enable)
  3769. encoder->pre_enable(encoder);
  3770. if (intel_crtc->config->has_pch_encoder) {
  3771. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  3772. true);
  3773. dev_priv->display.fdi_link_train(crtc);
  3774. }
  3775. intel_ddi_enable_pipe_clock(intel_crtc);
  3776. if (IS_SKYLAKE(dev))
  3777. skylake_pfit_enable(intel_crtc);
  3778. else
  3779. ironlake_pfit_enable(intel_crtc);
  3780. /*
  3781. * On ILK+ LUT must be loaded before the pipe is running but with
  3782. * clocks enabled
  3783. */
  3784. intel_crtc_load_lut(crtc);
  3785. intel_ddi_set_pipe_settings(crtc);
  3786. intel_ddi_enable_transcoder_func(crtc);
  3787. intel_update_watermarks(crtc);
  3788. intel_enable_pipe(intel_crtc);
  3789. if (intel_crtc->config->has_pch_encoder)
  3790. lpt_pch_enable(crtc);
  3791. if (intel_crtc->config->dp_encoder_is_mst)
  3792. intel_ddi_set_vc_payload_alloc(crtc, true);
  3793. assert_vblank_disabled(crtc);
  3794. drm_crtc_vblank_on(crtc);
  3795. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3796. encoder->enable(encoder);
  3797. intel_opregion_notify_encoder(encoder, true);
  3798. }
  3799. /* If we change the relative order between pipe/planes enabling, we need
  3800. * to change the workaround. */
  3801. haswell_mode_set_planes_workaround(intel_crtc);
  3802. intel_crtc_enable_planes(crtc);
  3803. }
  3804. static void skylake_pfit_disable(struct intel_crtc *crtc)
  3805. {
  3806. struct drm_device *dev = crtc->base.dev;
  3807. struct drm_i915_private *dev_priv = dev->dev_private;
  3808. int pipe = crtc->pipe;
  3809. /* To avoid upsetting the power well on haswell only disable the pfit if
  3810. * it's in use. The hw state code will make sure we get this right. */
  3811. if (crtc->config->pch_pfit.enabled) {
  3812. I915_WRITE(PS_CTL(pipe), 0);
  3813. I915_WRITE(PS_WIN_POS(pipe), 0);
  3814. I915_WRITE(PS_WIN_SZ(pipe), 0);
  3815. }
  3816. }
  3817. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  3818. {
  3819. struct drm_device *dev = crtc->base.dev;
  3820. struct drm_i915_private *dev_priv = dev->dev_private;
  3821. int pipe = crtc->pipe;
  3822. /* To avoid upsetting the power well on haswell only disable the pfit if
  3823. * it's in use. The hw state code will make sure we get this right. */
  3824. if (crtc->config->pch_pfit.enabled) {
  3825. I915_WRITE(PF_CTL(pipe), 0);
  3826. I915_WRITE(PF_WIN_POS(pipe), 0);
  3827. I915_WRITE(PF_WIN_SZ(pipe), 0);
  3828. }
  3829. }
  3830. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  3831. {
  3832. struct drm_device *dev = crtc->dev;
  3833. struct drm_i915_private *dev_priv = dev->dev_private;
  3834. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3835. struct intel_encoder *encoder;
  3836. int pipe = intel_crtc->pipe;
  3837. u32 reg, temp;
  3838. if (!intel_crtc->active)
  3839. return;
  3840. intel_crtc_disable_planes(crtc);
  3841. for_each_encoder_on_crtc(dev, crtc, encoder)
  3842. encoder->disable(encoder);
  3843. drm_crtc_vblank_off(crtc);
  3844. assert_vblank_disabled(crtc);
  3845. if (intel_crtc->config->has_pch_encoder)
  3846. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  3847. intel_disable_pipe(intel_crtc);
  3848. ironlake_pfit_disable(intel_crtc);
  3849. for_each_encoder_on_crtc(dev, crtc, encoder)
  3850. if (encoder->post_disable)
  3851. encoder->post_disable(encoder);
  3852. if (intel_crtc->config->has_pch_encoder) {
  3853. ironlake_fdi_disable(crtc);
  3854. ironlake_disable_pch_transcoder(dev_priv, pipe);
  3855. if (HAS_PCH_CPT(dev)) {
  3856. /* disable TRANS_DP_CTL */
  3857. reg = TRANS_DP_CTL(pipe);
  3858. temp = I915_READ(reg);
  3859. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  3860. TRANS_DP_PORT_SEL_MASK);
  3861. temp |= TRANS_DP_PORT_SEL_NONE;
  3862. I915_WRITE(reg, temp);
  3863. /* disable DPLL_SEL */
  3864. temp = I915_READ(PCH_DPLL_SEL);
  3865. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  3866. I915_WRITE(PCH_DPLL_SEL, temp);
  3867. }
  3868. /* disable PCH DPLL */
  3869. intel_disable_shared_dpll(intel_crtc);
  3870. ironlake_fdi_pll_disable(intel_crtc);
  3871. }
  3872. intel_crtc->active = false;
  3873. intel_update_watermarks(crtc);
  3874. mutex_lock(&dev->struct_mutex);
  3875. intel_fbc_update(dev);
  3876. mutex_unlock(&dev->struct_mutex);
  3877. }
  3878. static void haswell_crtc_disable(struct drm_crtc *crtc)
  3879. {
  3880. struct drm_device *dev = crtc->dev;
  3881. struct drm_i915_private *dev_priv = dev->dev_private;
  3882. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3883. struct intel_encoder *encoder;
  3884. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3885. if (!intel_crtc->active)
  3886. return;
  3887. intel_crtc_disable_planes(crtc);
  3888. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3889. intel_opregion_notify_encoder(encoder, false);
  3890. encoder->disable(encoder);
  3891. }
  3892. drm_crtc_vblank_off(crtc);
  3893. assert_vblank_disabled(crtc);
  3894. if (intel_crtc->config->has_pch_encoder)
  3895. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  3896. false);
  3897. intel_disable_pipe(intel_crtc);
  3898. if (intel_crtc->config->dp_encoder_is_mst)
  3899. intel_ddi_set_vc_payload_alloc(crtc, false);
  3900. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  3901. if (IS_SKYLAKE(dev))
  3902. skylake_pfit_disable(intel_crtc);
  3903. else
  3904. ironlake_pfit_disable(intel_crtc);
  3905. intel_ddi_disable_pipe_clock(intel_crtc);
  3906. if (intel_crtc->config->has_pch_encoder) {
  3907. lpt_disable_pch_transcoder(dev_priv);
  3908. intel_ddi_fdi_disable(crtc);
  3909. }
  3910. for_each_encoder_on_crtc(dev, crtc, encoder)
  3911. if (encoder->post_disable)
  3912. encoder->post_disable(encoder);
  3913. intel_crtc->active = false;
  3914. intel_update_watermarks(crtc);
  3915. mutex_lock(&dev->struct_mutex);
  3916. intel_fbc_update(dev);
  3917. mutex_unlock(&dev->struct_mutex);
  3918. if (intel_crtc_to_shared_dpll(intel_crtc))
  3919. intel_disable_shared_dpll(intel_crtc);
  3920. }
  3921. static void ironlake_crtc_off(struct drm_crtc *crtc)
  3922. {
  3923. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3924. intel_put_shared_dpll(intel_crtc);
  3925. }
  3926. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  3927. {
  3928. struct drm_device *dev = crtc->base.dev;
  3929. struct drm_i915_private *dev_priv = dev->dev_private;
  3930. struct intel_crtc_state *pipe_config = crtc->config;
  3931. if (!pipe_config->gmch_pfit.control)
  3932. return;
  3933. /*
  3934. * The panel fitter should only be adjusted whilst the pipe is disabled,
  3935. * according to register description and PRM.
  3936. */
  3937. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  3938. assert_pipe_disabled(dev_priv, crtc->pipe);
  3939. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  3940. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  3941. /* Border color in case we don't scale up to the full screen. Black by
  3942. * default, change to something else for debugging. */
  3943. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  3944. }
  3945. static enum intel_display_power_domain port_to_power_domain(enum port port)
  3946. {
  3947. switch (port) {
  3948. case PORT_A:
  3949. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  3950. case PORT_B:
  3951. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  3952. case PORT_C:
  3953. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  3954. case PORT_D:
  3955. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  3956. default:
  3957. WARN_ON_ONCE(1);
  3958. return POWER_DOMAIN_PORT_OTHER;
  3959. }
  3960. }
  3961. #define for_each_power_domain(domain, mask) \
  3962. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  3963. if ((1 << (domain)) & (mask))
  3964. enum intel_display_power_domain
  3965. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  3966. {
  3967. struct drm_device *dev = intel_encoder->base.dev;
  3968. struct intel_digital_port *intel_dig_port;
  3969. switch (intel_encoder->type) {
  3970. case INTEL_OUTPUT_UNKNOWN:
  3971. /* Only DDI platforms should ever use this output type */
  3972. WARN_ON_ONCE(!HAS_DDI(dev));
  3973. case INTEL_OUTPUT_DISPLAYPORT:
  3974. case INTEL_OUTPUT_HDMI:
  3975. case INTEL_OUTPUT_EDP:
  3976. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  3977. return port_to_power_domain(intel_dig_port->port);
  3978. case INTEL_OUTPUT_DP_MST:
  3979. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  3980. return port_to_power_domain(intel_dig_port->port);
  3981. case INTEL_OUTPUT_ANALOG:
  3982. return POWER_DOMAIN_PORT_CRT;
  3983. case INTEL_OUTPUT_DSI:
  3984. return POWER_DOMAIN_PORT_DSI;
  3985. default:
  3986. return POWER_DOMAIN_PORT_OTHER;
  3987. }
  3988. }
  3989. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  3990. {
  3991. struct drm_device *dev = crtc->dev;
  3992. struct intel_encoder *intel_encoder;
  3993. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3994. enum pipe pipe = intel_crtc->pipe;
  3995. unsigned long mask;
  3996. enum transcoder transcoder;
  3997. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  3998. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  3999. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4000. if (intel_crtc->config->pch_pfit.enabled ||
  4001. intel_crtc->config->pch_pfit.force_thru)
  4002. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4003. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4004. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4005. return mask;
  4006. }
  4007. static void modeset_update_crtc_power_domains(struct drm_device *dev)
  4008. {
  4009. struct drm_i915_private *dev_priv = dev->dev_private;
  4010. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  4011. struct intel_crtc *crtc;
  4012. /*
  4013. * First get all needed power domains, then put all unneeded, to avoid
  4014. * any unnecessary toggling of the power wells.
  4015. */
  4016. for_each_intel_crtc(dev, crtc) {
  4017. enum intel_display_power_domain domain;
  4018. if (!crtc->base.enabled)
  4019. continue;
  4020. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  4021. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  4022. intel_display_power_get(dev_priv, domain);
  4023. }
  4024. if (dev_priv->display.modeset_global_resources)
  4025. dev_priv->display.modeset_global_resources(dev);
  4026. for_each_intel_crtc(dev, crtc) {
  4027. enum intel_display_power_domain domain;
  4028. for_each_power_domain(domain, crtc->enabled_power_domains)
  4029. intel_display_power_put(dev_priv, domain);
  4030. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  4031. }
  4032. intel_display_set_init_power(dev_priv, false);
  4033. }
  4034. /* returns HPLL frequency in kHz */
  4035. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  4036. {
  4037. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  4038. /* Obtain SKU information */
  4039. mutex_lock(&dev_priv->dpio_lock);
  4040. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  4041. CCK_FUSE_HPLL_FREQ_MASK;
  4042. mutex_unlock(&dev_priv->dpio_lock);
  4043. return vco_freq[hpll_freq] * 1000;
  4044. }
  4045. static void vlv_update_cdclk(struct drm_device *dev)
  4046. {
  4047. struct drm_i915_private *dev_priv = dev->dev_private;
  4048. dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4049. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4050. dev_priv->vlv_cdclk_freq);
  4051. /*
  4052. * Program the gmbus_freq based on the cdclk frequency.
  4053. * BSpec erroneously claims we should aim for 4MHz, but
  4054. * in fact 1MHz is the correct frequency.
  4055. */
  4056. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->vlv_cdclk_freq, 1000));
  4057. }
  4058. /* Adjust CDclk dividers to allow high res or save power if possible */
  4059. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4060. {
  4061. struct drm_i915_private *dev_priv = dev->dev_private;
  4062. u32 val, cmd;
  4063. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  4064. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4065. cmd = 2;
  4066. else if (cdclk == 266667)
  4067. cmd = 1;
  4068. else
  4069. cmd = 0;
  4070. mutex_lock(&dev_priv->rps.hw_lock);
  4071. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4072. val &= ~DSPFREQGUAR_MASK;
  4073. val |= (cmd << DSPFREQGUAR_SHIFT);
  4074. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4075. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4076. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4077. 50)) {
  4078. DRM_ERROR("timed out waiting for CDclk change\n");
  4079. }
  4080. mutex_unlock(&dev_priv->rps.hw_lock);
  4081. if (cdclk == 400000) {
  4082. u32 divider;
  4083. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4084. mutex_lock(&dev_priv->dpio_lock);
  4085. /* adjust cdclk divider */
  4086. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4087. val &= ~DISPLAY_FREQUENCY_VALUES;
  4088. val |= divider;
  4089. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4090. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4091. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4092. 50))
  4093. DRM_ERROR("timed out waiting for CDclk change\n");
  4094. mutex_unlock(&dev_priv->dpio_lock);
  4095. }
  4096. mutex_lock(&dev_priv->dpio_lock);
  4097. /* adjust self-refresh exit latency value */
  4098. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4099. val &= ~0x7f;
  4100. /*
  4101. * For high bandwidth configs, we set a higher latency in the bunit
  4102. * so that the core display fetch happens in time to avoid underruns.
  4103. */
  4104. if (cdclk == 400000)
  4105. val |= 4500 / 250; /* 4.5 usec */
  4106. else
  4107. val |= 3000 / 250; /* 3.0 usec */
  4108. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4109. mutex_unlock(&dev_priv->dpio_lock);
  4110. vlv_update_cdclk(dev);
  4111. }
  4112. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4113. {
  4114. struct drm_i915_private *dev_priv = dev->dev_private;
  4115. u32 val, cmd;
  4116. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  4117. switch (cdclk) {
  4118. case 400000:
  4119. cmd = 3;
  4120. break;
  4121. case 333333:
  4122. case 320000:
  4123. cmd = 2;
  4124. break;
  4125. case 266667:
  4126. cmd = 1;
  4127. break;
  4128. case 200000:
  4129. cmd = 0;
  4130. break;
  4131. default:
  4132. MISSING_CASE(cdclk);
  4133. return;
  4134. }
  4135. mutex_lock(&dev_priv->rps.hw_lock);
  4136. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4137. val &= ~DSPFREQGUAR_MASK_CHV;
  4138. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4139. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4140. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4141. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4142. 50)) {
  4143. DRM_ERROR("timed out waiting for CDclk change\n");
  4144. }
  4145. mutex_unlock(&dev_priv->rps.hw_lock);
  4146. vlv_update_cdclk(dev);
  4147. }
  4148. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4149. int max_pixclk)
  4150. {
  4151. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4152. /* FIXME: Punit isn't quite ready yet */
  4153. if (IS_CHERRYVIEW(dev_priv->dev))
  4154. return 400000;
  4155. /*
  4156. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4157. * 200MHz
  4158. * 267MHz
  4159. * 320/333MHz (depends on HPLL freq)
  4160. * 400MHz
  4161. * So we check to see whether we're above 90% of the lower bin and
  4162. * adjust if needed.
  4163. *
  4164. * We seem to get an unstable or solid color picture at 200MHz.
  4165. * Not sure what's wrong. For now use 200MHz only when all pipes
  4166. * are off.
  4167. */
  4168. if (max_pixclk > freq_320*9/10)
  4169. return 400000;
  4170. else if (max_pixclk > 266667*9/10)
  4171. return freq_320;
  4172. else if (max_pixclk > 0)
  4173. return 266667;
  4174. else
  4175. return 200000;
  4176. }
  4177. /* compute the max pixel clock for new configuration */
  4178. static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
  4179. {
  4180. struct drm_device *dev = dev_priv->dev;
  4181. struct intel_crtc *intel_crtc;
  4182. int max_pixclk = 0;
  4183. for_each_intel_crtc(dev, intel_crtc) {
  4184. if (intel_crtc->new_enabled)
  4185. max_pixclk = max(max_pixclk,
  4186. intel_crtc->new_config->base.adjusted_mode.crtc_clock);
  4187. }
  4188. return max_pixclk;
  4189. }
  4190. static void valleyview_modeset_global_pipes(struct drm_device *dev,
  4191. unsigned *prepare_pipes)
  4192. {
  4193. struct drm_i915_private *dev_priv = dev->dev_private;
  4194. struct intel_crtc *intel_crtc;
  4195. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4196. if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
  4197. dev_priv->vlv_cdclk_freq)
  4198. return;
  4199. /* disable/enable all currently active pipes while we change cdclk */
  4200. for_each_intel_crtc(dev, intel_crtc)
  4201. if (intel_crtc->base.enabled)
  4202. *prepare_pipes |= (1 << intel_crtc->pipe);
  4203. }
  4204. static void valleyview_modeset_global_resources(struct drm_device *dev)
  4205. {
  4206. struct drm_i915_private *dev_priv = dev->dev_private;
  4207. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4208. int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  4209. if (req_cdclk != dev_priv->vlv_cdclk_freq) {
  4210. /*
  4211. * FIXME: We can end up here with all power domains off, yet
  4212. * with a CDCLK frequency other than the minimum. To account
  4213. * for this take the PIPE-A power domain, which covers the HW
  4214. * blocks needed for the following programming. This can be
  4215. * removed once it's guaranteed that we get here either with
  4216. * the minimum CDCLK set, or the required power domains
  4217. * enabled.
  4218. */
  4219. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  4220. if (IS_CHERRYVIEW(dev))
  4221. cherryview_set_cdclk(dev, req_cdclk);
  4222. else
  4223. valleyview_set_cdclk(dev, req_cdclk);
  4224. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  4225. }
  4226. }
  4227. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  4228. {
  4229. struct drm_device *dev = crtc->dev;
  4230. struct drm_i915_private *dev_priv = to_i915(dev);
  4231. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4232. struct intel_encoder *encoder;
  4233. int pipe = intel_crtc->pipe;
  4234. bool is_dsi;
  4235. WARN_ON(!crtc->enabled);
  4236. if (intel_crtc->active)
  4237. return;
  4238. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  4239. if (!is_dsi) {
  4240. if (IS_CHERRYVIEW(dev))
  4241. chv_prepare_pll(intel_crtc, intel_crtc->config);
  4242. else
  4243. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  4244. }
  4245. if (intel_crtc->config->has_dp_encoder)
  4246. intel_dp_set_m_n(intel_crtc);
  4247. intel_set_pipe_timings(intel_crtc);
  4248. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  4249. struct drm_i915_private *dev_priv = dev->dev_private;
  4250. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  4251. I915_WRITE(CHV_CANVAS(pipe), 0);
  4252. }
  4253. i9xx_set_pipeconf(intel_crtc);
  4254. intel_crtc->active = true;
  4255. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4256. for_each_encoder_on_crtc(dev, crtc, encoder)
  4257. if (encoder->pre_pll_enable)
  4258. encoder->pre_pll_enable(encoder);
  4259. if (!is_dsi) {
  4260. if (IS_CHERRYVIEW(dev))
  4261. chv_enable_pll(intel_crtc, intel_crtc->config);
  4262. else
  4263. vlv_enable_pll(intel_crtc, intel_crtc->config);
  4264. }
  4265. for_each_encoder_on_crtc(dev, crtc, encoder)
  4266. if (encoder->pre_enable)
  4267. encoder->pre_enable(encoder);
  4268. i9xx_pfit_enable(intel_crtc);
  4269. intel_crtc_load_lut(crtc);
  4270. intel_update_watermarks(crtc);
  4271. intel_enable_pipe(intel_crtc);
  4272. assert_vblank_disabled(crtc);
  4273. drm_crtc_vblank_on(crtc);
  4274. for_each_encoder_on_crtc(dev, crtc, encoder)
  4275. encoder->enable(encoder);
  4276. intel_crtc_enable_planes(crtc);
  4277. /* Underruns don't raise interrupts, so check manually. */
  4278. i9xx_check_fifo_underruns(dev_priv);
  4279. }
  4280. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  4281. {
  4282. struct drm_device *dev = crtc->base.dev;
  4283. struct drm_i915_private *dev_priv = dev->dev_private;
  4284. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  4285. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  4286. }
  4287. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  4288. {
  4289. struct drm_device *dev = crtc->dev;
  4290. struct drm_i915_private *dev_priv = to_i915(dev);
  4291. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4292. struct intel_encoder *encoder;
  4293. int pipe = intel_crtc->pipe;
  4294. WARN_ON(!crtc->enabled);
  4295. if (intel_crtc->active)
  4296. return;
  4297. i9xx_set_pll_dividers(intel_crtc);
  4298. if (intel_crtc->config->has_dp_encoder)
  4299. intel_dp_set_m_n(intel_crtc);
  4300. intel_set_pipe_timings(intel_crtc);
  4301. i9xx_set_pipeconf(intel_crtc);
  4302. intel_crtc->active = true;
  4303. if (!IS_GEN2(dev))
  4304. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4305. for_each_encoder_on_crtc(dev, crtc, encoder)
  4306. if (encoder->pre_enable)
  4307. encoder->pre_enable(encoder);
  4308. i9xx_enable_pll(intel_crtc);
  4309. i9xx_pfit_enable(intel_crtc);
  4310. intel_crtc_load_lut(crtc);
  4311. intel_update_watermarks(crtc);
  4312. intel_enable_pipe(intel_crtc);
  4313. assert_vblank_disabled(crtc);
  4314. drm_crtc_vblank_on(crtc);
  4315. for_each_encoder_on_crtc(dev, crtc, encoder)
  4316. encoder->enable(encoder);
  4317. intel_crtc_enable_planes(crtc);
  4318. /*
  4319. * Gen2 reports pipe underruns whenever all planes are disabled.
  4320. * So don't enable underrun reporting before at least some planes
  4321. * are enabled.
  4322. * FIXME: Need to fix the logic to work when we turn off all planes
  4323. * but leave the pipe running.
  4324. */
  4325. if (IS_GEN2(dev))
  4326. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4327. /* Underruns don't raise interrupts, so check manually. */
  4328. i9xx_check_fifo_underruns(dev_priv);
  4329. }
  4330. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4331. {
  4332. struct drm_device *dev = crtc->base.dev;
  4333. struct drm_i915_private *dev_priv = dev->dev_private;
  4334. if (!crtc->config->gmch_pfit.control)
  4335. return;
  4336. assert_pipe_disabled(dev_priv, crtc->pipe);
  4337. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4338. I915_READ(PFIT_CONTROL));
  4339. I915_WRITE(PFIT_CONTROL, 0);
  4340. }
  4341. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  4342. {
  4343. struct drm_device *dev = crtc->dev;
  4344. struct drm_i915_private *dev_priv = dev->dev_private;
  4345. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4346. struct intel_encoder *encoder;
  4347. int pipe = intel_crtc->pipe;
  4348. if (!intel_crtc->active)
  4349. return;
  4350. /*
  4351. * Gen2 reports pipe underruns whenever all planes are disabled.
  4352. * So diasble underrun reporting before all the planes get disabled.
  4353. * FIXME: Need to fix the logic to work when we turn off all planes
  4354. * but leave the pipe running.
  4355. */
  4356. if (IS_GEN2(dev))
  4357. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4358. /*
  4359. * Vblank time updates from the shadow to live plane control register
  4360. * are blocked if the memory self-refresh mode is active at that
  4361. * moment. So to make sure the plane gets truly disabled, disable
  4362. * first the self-refresh mode. The self-refresh enable bit in turn
  4363. * will be checked/applied by the HW only at the next frame start
  4364. * event which is after the vblank start event, so we need to have a
  4365. * wait-for-vblank between disabling the plane and the pipe.
  4366. */
  4367. intel_set_memory_cxsr(dev_priv, false);
  4368. intel_crtc_disable_planes(crtc);
  4369. /*
  4370. * On gen2 planes are double buffered but the pipe isn't, so we must
  4371. * wait for planes to fully turn off before disabling the pipe.
  4372. * We also need to wait on all gmch platforms because of the
  4373. * self-refresh mode constraint explained above.
  4374. */
  4375. intel_wait_for_vblank(dev, pipe);
  4376. for_each_encoder_on_crtc(dev, crtc, encoder)
  4377. encoder->disable(encoder);
  4378. drm_crtc_vblank_off(crtc);
  4379. assert_vblank_disabled(crtc);
  4380. intel_disable_pipe(intel_crtc);
  4381. i9xx_pfit_disable(intel_crtc);
  4382. for_each_encoder_on_crtc(dev, crtc, encoder)
  4383. if (encoder->post_disable)
  4384. encoder->post_disable(encoder);
  4385. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  4386. if (IS_CHERRYVIEW(dev))
  4387. chv_disable_pll(dev_priv, pipe);
  4388. else if (IS_VALLEYVIEW(dev))
  4389. vlv_disable_pll(dev_priv, pipe);
  4390. else
  4391. i9xx_disable_pll(intel_crtc);
  4392. }
  4393. if (!IS_GEN2(dev))
  4394. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4395. intel_crtc->active = false;
  4396. intel_update_watermarks(crtc);
  4397. mutex_lock(&dev->struct_mutex);
  4398. intel_fbc_update(dev);
  4399. mutex_unlock(&dev->struct_mutex);
  4400. }
  4401. static void i9xx_crtc_off(struct drm_crtc *crtc)
  4402. {
  4403. }
  4404. /* Master function to enable/disable CRTC and corresponding power wells */
  4405. void intel_crtc_control(struct drm_crtc *crtc, bool enable)
  4406. {
  4407. struct drm_device *dev = crtc->dev;
  4408. struct drm_i915_private *dev_priv = dev->dev_private;
  4409. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4410. enum intel_display_power_domain domain;
  4411. unsigned long domains;
  4412. if (enable) {
  4413. if (!intel_crtc->active) {
  4414. domains = get_crtc_power_domains(crtc);
  4415. for_each_power_domain(domain, domains)
  4416. intel_display_power_get(dev_priv, domain);
  4417. intel_crtc->enabled_power_domains = domains;
  4418. dev_priv->display.crtc_enable(crtc);
  4419. }
  4420. } else {
  4421. if (intel_crtc->active) {
  4422. dev_priv->display.crtc_disable(crtc);
  4423. domains = intel_crtc->enabled_power_domains;
  4424. for_each_power_domain(domain, domains)
  4425. intel_display_power_put(dev_priv, domain);
  4426. intel_crtc->enabled_power_domains = 0;
  4427. }
  4428. }
  4429. }
  4430. /**
  4431. * Sets the power management mode of the pipe and plane.
  4432. */
  4433. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  4434. {
  4435. struct drm_device *dev = crtc->dev;
  4436. struct intel_encoder *intel_encoder;
  4437. bool enable = false;
  4438. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4439. enable |= intel_encoder->connectors_active;
  4440. intel_crtc_control(crtc, enable);
  4441. }
  4442. static void intel_crtc_disable(struct drm_crtc *crtc)
  4443. {
  4444. struct drm_device *dev = crtc->dev;
  4445. struct drm_connector *connector;
  4446. struct drm_i915_private *dev_priv = dev->dev_private;
  4447. /* crtc should still be enabled when we disable it. */
  4448. WARN_ON(!crtc->enabled);
  4449. dev_priv->display.crtc_disable(crtc);
  4450. dev_priv->display.off(crtc);
  4451. crtc->primary->funcs->disable_plane(crtc->primary);
  4452. /* Update computed state. */
  4453. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  4454. if (!connector->encoder || !connector->encoder->crtc)
  4455. continue;
  4456. if (connector->encoder->crtc != crtc)
  4457. continue;
  4458. connector->dpms = DRM_MODE_DPMS_OFF;
  4459. to_intel_encoder(connector->encoder)->connectors_active = false;
  4460. }
  4461. }
  4462. void intel_encoder_destroy(struct drm_encoder *encoder)
  4463. {
  4464. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4465. drm_encoder_cleanup(encoder);
  4466. kfree(intel_encoder);
  4467. }
  4468. /* Simple dpms helper for encoders with just one connector, no cloning and only
  4469. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  4470. * state of the entire output pipe. */
  4471. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  4472. {
  4473. if (mode == DRM_MODE_DPMS_ON) {
  4474. encoder->connectors_active = true;
  4475. intel_crtc_update_dpms(encoder->base.crtc);
  4476. } else {
  4477. encoder->connectors_active = false;
  4478. intel_crtc_update_dpms(encoder->base.crtc);
  4479. }
  4480. }
  4481. /* Cross check the actual hw state with our own modeset state tracking (and it's
  4482. * internal consistency). */
  4483. static void intel_connector_check_state(struct intel_connector *connector)
  4484. {
  4485. if (connector->get_hw_state(connector)) {
  4486. struct intel_encoder *encoder = connector->encoder;
  4487. struct drm_crtc *crtc;
  4488. bool encoder_enabled;
  4489. enum pipe pipe;
  4490. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4491. connector->base.base.id,
  4492. connector->base.name);
  4493. /* there is no real hw state for MST connectors */
  4494. if (connector->mst_port)
  4495. return;
  4496. I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  4497. "wrong connector dpms state\n");
  4498. I915_STATE_WARN(connector->base.encoder != &encoder->base,
  4499. "active connector not linked to encoder\n");
  4500. if (encoder) {
  4501. I915_STATE_WARN(!encoder->connectors_active,
  4502. "encoder->connectors_active not set\n");
  4503. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  4504. I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
  4505. if (I915_STATE_WARN_ON(!encoder->base.crtc))
  4506. return;
  4507. crtc = encoder->base.crtc;
  4508. I915_STATE_WARN(!crtc->enabled, "crtc not enabled\n");
  4509. I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  4510. I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
  4511. "encoder active on the wrong pipe\n");
  4512. }
  4513. }
  4514. }
  4515. /* Even simpler default implementation, if there's really no special case to
  4516. * consider. */
  4517. void intel_connector_dpms(struct drm_connector *connector, int mode)
  4518. {
  4519. /* All the simple cases only support two dpms states. */
  4520. if (mode != DRM_MODE_DPMS_ON)
  4521. mode = DRM_MODE_DPMS_OFF;
  4522. if (mode == connector->dpms)
  4523. return;
  4524. connector->dpms = mode;
  4525. /* Only need to change hw state when actually enabled */
  4526. if (connector->encoder)
  4527. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  4528. intel_modeset_check_state(connector->dev);
  4529. }
  4530. /* Simple connector->get_hw_state implementation for encoders that support only
  4531. * one connector and no cloning and hence the encoder state determines the state
  4532. * of the connector. */
  4533. bool intel_connector_get_hw_state(struct intel_connector *connector)
  4534. {
  4535. enum pipe pipe = 0;
  4536. struct intel_encoder *encoder = connector->encoder;
  4537. return encoder->get_hw_state(encoder, &pipe);
  4538. }
  4539. static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  4540. struct intel_crtc_state *pipe_config)
  4541. {
  4542. struct drm_i915_private *dev_priv = dev->dev_private;
  4543. struct intel_crtc *pipe_B_crtc =
  4544. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  4545. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  4546. pipe_name(pipe), pipe_config->fdi_lanes);
  4547. if (pipe_config->fdi_lanes > 4) {
  4548. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  4549. pipe_name(pipe), pipe_config->fdi_lanes);
  4550. return false;
  4551. }
  4552. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  4553. if (pipe_config->fdi_lanes > 2) {
  4554. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  4555. pipe_config->fdi_lanes);
  4556. return false;
  4557. } else {
  4558. return true;
  4559. }
  4560. }
  4561. if (INTEL_INFO(dev)->num_pipes == 2)
  4562. return true;
  4563. /* Ivybridge 3 pipe is really complicated */
  4564. switch (pipe) {
  4565. case PIPE_A:
  4566. return true;
  4567. case PIPE_B:
  4568. if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
  4569. pipe_config->fdi_lanes > 2) {
  4570. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4571. pipe_name(pipe), pipe_config->fdi_lanes);
  4572. return false;
  4573. }
  4574. return true;
  4575. case PIPE_C:
  4576. if (!pipe_has_enabled_pch(pipe_B_crtc) ||
  4577. pipe_B_crtc->config->fdi_lanes <= 2) {
  4578. if (pipe_config->fdi_lanes > 2) {
  4579. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4580. pipe_name(pipe), pipe_config->fdi_lanes);
  4581. return false;
  4582. }
  4583. } else {
  4584. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  4585. return false;
  4586. }
  4587. return true;
  4588. default:
  4589. BUG();
  4590. }
  4591. }
  4592. #define RETRY 1
  4593. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  4594. struct intel_crtc_state *pipe_config)
  4595. {
  4596. struct drm_device *dev = intel_crtc->base.dev;
  4597. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  4598. int lane, link_bw, fdi_dotclock;
  4599. bool setup_ok, needs_recompute = false;
  4600. retry:
  4601. /* FDI is a binary signal running at ~2.7GHz, encoding
  4602. * each output octet as 10 bits. The actual frequency
  4603. * is stored as a divider into a 100MHz clock, and the
  4604. * mode pixel clock is stored in units of 1KHz.
  4605. * Hence the bw of each lane in terms of the mode signal
  4606. * is:
  4607. */
  4608. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4609. fdi_dotclock = adjusted_mode->crtc_clock;
  4610. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  4611. pipe_config->pipe_bpp);
  4612. pipe_config->fdi_lanes = lane;
  4613. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  4614. link_bw, &pipe_config->fdi_m_n);
  4615. setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  4616. intel_crtc->pipe, pipe_config);
  4617. if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
  4618. pipe_config->pipe_bpp -= 2*3;
  4619. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  4620. pipe_config->pipe_bpp);
  4621. needs_recompute = true;
  4622. pipe_config->bw_constrained = true;
  4623. goto retry;
  4624. }
  4625. if (needs_recompute)
  4626. return RETRY;
  4627. return setup_ok ? 0 : -EINVAL;
  4628. }
  4629. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  4630. struct intel_crtc_state *pipe_config)
  4631. {
  4632. pipe_config->ips_enabled = i915.enable_ips &&
  4633. hsw_crtc_supports_ips(crtc) &&
  4634. pipe_config->pipe_bpp <= 24;
  4635. }
  4636. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  4637. struct intel_crtc_state *pipe_config)
  4638. {
  4639. struct drm_device *dev = crtc->base.dev;
  4640. struct drm_i915_private *dev_priv = dev->dev_private;
  4641. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  4642. /* FIXME should check pixel clock limits on all platforms */
  4643. if (INTEL_INFO(dev)->gen < 4) {
  4644. int clock_limit =
  4645. dev_priv->display.get_display_clock_speed(dev);
  4646. /*
  4647. * Enable pixel doubling when the dot clock
  4648. * is > 90% of the (display) core speed.
  4649. *
  4650. * GDG double wide on either pipe,
  4651. * otherwise pipe A only.
  4652. */
  4653. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  4654. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  4655. clock_limit *= 2;
  4656. pipe_config->double_wide = true;
  4657. }
  4658. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  4659. return -EINVAL;
  4660. }
  4661. /*
  4662. * Pipe horizontal size must be even in:
  4663. * - DVO ganged mode
  4664. * - LVDS dual channel mode
  4665. * - Double wide pipe
  4666. */
  4667. if ((intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  4668. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  4669. pipe_config->pipe_src_w &= ~1;
  4670. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  4671. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  4672. */
  4673. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  4674. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  4675. return -EINVAL;
  4676. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
  4677. pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
  4678. } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
  4679. /* only a 8bpc pipe, with 6bpc dither through the panel fitter
  4680. * for lvds. */
  4681. pipe_config->pipe_bpp = 8*3;
  4682. }
  4683. if (HAS_IPS(dev))
  4684. hsw_compute_ips_config(crtc, pipe_config);
  4685. if (pipe_config->has_pch_encoder)
  4686. return ironlake_fdi_compute_config(crtc, pipe_config);
  4687. return 0;
  4688. }
  4689. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  4690. {
  4691. struct drm_i915_private *dev_priv = dev->dev_private;
  4692. u32 val;
  4693. int divider;
  4694. /* FIXME: Punit isn't quite ready yet */
  4695. if (IS_CHERRYVIEW(dev))
  4696. return 400000;
  4697. if (dev_priv->hpll_freq == 0)
  4698. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  4699. mutex_lock(&dev_priv->dpio_lock);
  4700. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4701. mutex_unlock(&dev_priv->dpio_lock);
  4702. divider = val & DISPLAY_FREQUENCY_VALUES;
  4703. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  4704. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4705. "cdclk change in progress\n");
  4706. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  4707. }
  4708. static int i945_get_display_clock_speed(struct drm_device *dev)
  4709. {
  4710. return 400000;
  4711. }
  4712. static int i915_get_display_clock_speed(struct drm_device *dev)
  4713. {
  4714. return 333000;
  4715. }
  4716. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  4717. {
  4718. return 200000;
  4719. }
  4720. static int pnv_get_display_clock_speed(struct drm_device *dev)
  4721. {
  4722. u16 gcfgc = 0;
  4723. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4724. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4725. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  4726. return 267000;
  4727. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  4728. return 333000;
  4729. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  4730. return 444000;
  4731. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  4732. return 200000;
  4733. default:
  4734. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  4735. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  4736. return 133000;
  4737. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  4738. return 167000;
  4739. }
  4740. }
  4741. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  4742. {
  4743. u16 gcfgc = 0;
  4744. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4745. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  4746. return 133000;
  4747. else {
  4748. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4749. case GC_DISPLAY_CLOCK_333_MHZ:
  4750. return 333000;
  4751. default:
  4752. case GC_DISPLAY_CLOCK_190_200_MHZ:
  4753. return 190000;
  4754. }
  4755. }
  4756. }
  4757. static int i865_get_display_clock_speed(struct drm_device *dev)
  4758. {
  4759. return 266000;
  4760. }
  4761. static int i855_get_display_clock_speed(struct drm_device *dev)
  4762. {
  4763. u16 hpllcc = 0;
  4764. /* Assume that the hardware is in the high speed state. This
  4765. * should be the default.
  4766. */
  4767. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  4768. case GC_CLOCK_133_200:
  4769. case GC_CLOCK_100_200:
  4770. return 200000;
  4771. case GC_CLOCK_166_250:
  4772. return 250000;
  4773. case GC_CLOCK_100_133:
  4774. return 133000;
  4775. }
  4776. /* Shouldn't happen */
  4777. return 0;
  4778. }
  4779. static int i830_get_display_clock_speed(struct drm_device *dev)
  4780. {
  4781. return 133000;
  4782. }
  4783. static void
  4784. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  4785. {
  4786. while (*num > DATA_LINK_M_N_MASK ||
  4787. *den > DATA_LINK_M_N_MASK) {
  4788. *num >>= 1;
  4789. *den >>= 1;
  4790. }
  4791. }
  4792. static void compute_m_n(unsigned int m, unsigned int n,
  4793. uint32_t *ret_m, uint32_t *ret_n)
  4794. {
  4795. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  4796. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  4797. intel_reduce_m_n_ratio(ret_m, ret_n);
  4798. }
  4799. void
  4800. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  4801. int pixel_clock, int link_clock,
  4802. struct intel_link_m_n *m_n)
  4803. {
  4804. m_n->tu = 64;
  4805. compute_m_n(bits_per_pixel * pixel_clock,
  4806. link_clock * nlanes * 8,
  4807. &m_n->gmch_m, &m_n->gmch_n);
  4808. compute_m_n(pixel_clock, link_clock,
  4809. &m_n->link_m, &m_n->link_n);
  4810. }
  4811. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  4812. {
  4813. if (i915.panel_use_ssc >= 0)
  4814. return i915.panel_use_ssc != 0;
  4815. return dev_priv->vbt.lvds_use_ssc
  4816. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  4817. }
  4818. static int i9xx_get_refclk(struct intel_crtc *crtc, int num_connectors)
  4819. {
  4820. struct drm_device *dev = crtc->base.dev;
  4821. struct drm_i915_private *dev_priv = dev->dev_private;
  4822. int refclk;
  4823. if (IS_VALLEYVIEW(dev)) {
  4824. refclk = 100000;
  4825. } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  4826. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4827. refclk = dev_priv->vbt.lvds_ssc_freq;
  4828. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  4829. } else if (!IS_GEN2(dev)) {
  4830. refclk = 96000;
  4831. } else {
  4832. refclk = 48000;
  4833. }
  4834. return refclk;
  4835. }
  4836. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  4837. {
  4838. return (1 << dpll->n) << 16 | dpll->m2;
  4839. }
  4840. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  4841. {
  4842. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  4843. }
  4844. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  4845. struct intel_crtc_state *crtc_state,
  4846. intel_clock_t *reduced_clock)
  4847. {
  4848. struct drm_device *dev = crtc->base.dev;
  4849. u32 fp, fp2 = 0;
  4850. if (IS_PINEVIEW(dev)) {
  4851. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  4852. if (reduced_clock)
  4853. fp2 = pnv_dpll_compute_fp(reduced_clock);
  4854. } else {
  4855. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  4856. if (reduced_clock)
  4857. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  4858. }
  4859. crtc_state->dpll_hw_state.fp0 = fp;
  4860. crtc->lowfreq_avail = false;
  4861. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  4862. reduced_clock && i915.powersave) {
  4863. crtc_state->dpll_hw_state.fp1 = fp2;
  4864. crtc->lowfreq_avail = true;
  4865. } else {
  4866. crtc_state->dpll_hw_state.fp1 = fp;
  4867. }
  4868. }
  4869. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  4870. pipe)
  4871. {
  4872. u32 reg_val;
  4873. /*
  4874. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  4875. * and set it to a reasonable value instead.
  4876. */
  4877. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4878. reg_val &= 0xffffff00;
  4879. reg_val |= 0x00000030;
  4880. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4881. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4882. reg_val &= 0x8cffffff;
  4883. reg_val = 0x8c000000;
  4884. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4885. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4886. reg_val &= 0xffffff00;
  4887. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4888. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4889. reg_val &= 0x00ffffff;
  4890. reg_val |= 0xb0000000;
  4891. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4892. }
  4893. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  4894. struct intel_link_m_n *m_n)
  4895. {
  4896. struct drm_device *dev = crtc->base.dev;
  4897. struct drm_i915_private *dev_priv = dev->dev_private;
  4898. int pipe = crtc->pipe;
  4899. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4900. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  4901. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  4902. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  4903. }
  4904. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  4905. struct intel_link_m_n *m_n,
  4906. struct intel_link_m_n *m2_n2)
  4907. {
  4908. struct drm_device *dev = crtc->base.dev;
  4909. struct drm_i915_private *dev_priv = dev->dev_private;
  4910. int pipe = crtc->pipe;
  4911. enum transcoder transcoder = crtc->config->cpu_transcoder;
  4912. if (INTEL_INFO(dev)->gen >= 5) {
  4913. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4914. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  4915. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  4916. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  4917. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  4918. * for gen < 8) and if DRRS is supported (to make sure the
  4919. * registers are not unnecessarily accessed).
  4920. */
  4921. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  4922. crtc->config->has_drrs) {
  4923. I915_WRITE(PIPE_DATA_M2(transcoder),
  4924. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  4925. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  4926. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  4927. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  4928. }
  4929. } else {
  4930. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4931. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  4932. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  4933. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  4934. }
  4935. }
  4936. void intel_dp_set_m_n(struct intel_crtc *crtc)
  4937. {
  4938. if (crtc->config->has_pch_encoder)
  4939. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  4940. else
  4941. intel_cpu_transcoder_set_m_n(crtc, &crtc->config->dp_m_n,
  4942. &crtc->config->dp_m2_n2);
  4943. }
  4944. static void vlv_update_pll(struct intel_crtc *crtc,
  4945. struct intel_crtc_state *pipe_config)
  4946. {
  4947. u32 dpll, dpll_md;
  4948. /*
  4949. * Enable DPIO clock input. We should never disable the reference
  4950. * clock for pipe B, since VGA hotplug / manual detection depends
  4951. * on it.
  4952. */
  4953. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  4954. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  4955. /* We should never disable this, set it here for state tracking */
  4956. if (crtc->pipe == PIPE_B)
  4957. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4958. dpll |= DPLL_VCO_ENABLE;
  4959. pipe_config->dpll_hw_state.dpll = dpll;
  4960. dpll_md = (pipe_config->pixel_multiplier - 1)
  4961. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4962. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  4963. }
  4964. static void vlv_prepare_pll(struct intel_crtc *crtc,
  4965. const struct intel_crtc_state *pipe_config)
  4966. {
  4967. struct drm_device *dev = crtc->base.dev;
  4968. struct drm_i915_private *dev_priv = dev->dev_private;
  4969. int pipe = crtc->pipe;
  4970. u32 mdiv;
  4971. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  4972. u32 coreclk, reg_val;
  4973. mutex_lock(&dev_priv->dpio_lock);
  4974. bestn = pipe_config->dpll.n;
  4975. bestm1 = pipe_config->dpll.m1;
  4976. bestm2 = pipe_config->dpll.m2;
  4977. bestp1 = pipe_config->dpll.p1;
  4978. bestp2 = pipe_config->dpll.p2;
  4979. /* See eDP HDMI DPIO driver vbios notes doc */
  4980. /* PLL B needs special handling */
  4981. if (pipe == PIPE_B)
  4982. vlv_pllb_recal_opamp(dev_priv, pipe);
  4983. /* Set up Tx target for periodic Rcomp update */
  4984. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  4985. /* Disable target IRef on PLL */
  4986. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  4987. reg_val &= 0x00ffffff;
  4988. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  4989. /* Disable fast lock */
  4990. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  4991. /* Set idtafcrecal before PLL is enabled */
  4992. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  4993. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  4994. mdiv |= ((bestn << DPIO_N_SHIFT));
  4995. mdiv |= (1 << DPIO_K_SHIFT);
  4996. /*
  4997. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  4998. * but we don't support that).
  4999. * Note: don't use the DAC post divider as it seems unstable.
  5000. */
  5001. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5002. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5003. mdiv |= DPIO_ENABLE_CALIBRATION;
  5004. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5005. /* Set HBR and RBR LPF coefficients */
  5006. if (pipe_config->port_clock == 162000 ||
  5007. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  5008. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  5009. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5010. 0x009f0003);
  5011. else
  5012. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5013. 0x00d0000f);
  5014. if (pipe_config->has_dp_encoder) {
  5015. /* Use SSC source */
  5016. if (pipe == PIPE_A)
  5017. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5018. 0x0df40000);
  5019. else
  5020. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5021. 0x0df70000);
  5022. } else { /* HDMI or VGA */
  5023. /* Use bend source */
  5024. if (pipe == PIPE_A)
  5025. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5026. 0x0df70000);
  5027. else
  5028. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5029. 0x0df40000);
  5030. }
  5031. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5032. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5033. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  5034. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  5035. coreclk |= 0x01000000;
  5036. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5037. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5038. mutex_unlock(&dev_priv->dpio_lock);
  5039. }
  5040. static void chv_update_pll(struct intel_crtc *crtc,
  5041. struct intel_crtc_state *pipe_config)
  5042. {
  5043. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  5044. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  5045. DPLL_VCO_ENABLE;
  5046. if (crtc->pipe != PIPE_A)
  5047. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5048. pipe_config->dpll_hw_state.dpll_md =
  5049. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5050. }
  5051. static void chv_prepare_pll(struct intel_crtc *crtc,
  5052. const struct intel_crtc_state *pipe_config)
  5053. {
  5054. struct drm_device *dev = crtc->base.dev;
  5055. struct drm_i915_private *dev_priv = dev->dev_private;
  5056. int pipe = crtc->pipe;
  5057. int dpll_reg = DPLL(crtc->pipe);
  5058. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5059. u32 loopfilter, intcoeff;
  5060. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5061. int refclk;
  5062. bestn = pipe_config->dpll.n;
  5063. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5064. bestm1 = pipe_config->dpll.m1;
  5065. bestm2 = pipe_config->dpll.m2 >> 22;
  5066. bestp1 = pipe_config->dpll.p1;
  5067. bestp2 = pipe_config->dpll.p2;
  5068. /*
  5069. * Enable Refclk and SSC
  5070. */
  5071. I915_WRITE(dpll_reg,
  5072. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5073. mutex_lock(&dev_priv->dpio_lock);
  5074. /* p1 and p2 divider */
  5075. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5076. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5077. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5078. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5079. 1 << DPIO_CHV_K_DIV_SHIFT);
  5080. /* Feedback post-divider - m2 */
  5081. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5082. /* Feedback refclk divider - n and m1 */
  5083. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5084. DPIO_CHV_M1_DIV_BY_2 |
  5085. 1 << DPIO_CHV_N_DIV_SHIFT);
  5086. /* M2 fraction division */
  5087. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5088. /* M2 fraction division enable */
  5089. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
  5090. DPIO_CHV_FRAC_DIV_EN |
  5091. (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
  5092. /* Loop filter */
  5093. refclk = i9xx_get_refclk(crtc, 0);
  5094. loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
  5095. 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
  5096. if (refclk == 100000)
  5097. intcoeff = 11;
  5098. else if (refclk == 38400)
  5099. intcoeff = 10;
  5100. else
  5101. intcoeff = 9;
  5102. loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
  5103. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5104. /* AFC Recal */
  5105. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5106. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5107. DPIO_AFC_RECAL);
  5108. mutex_unlock(&dev_priv->dpio_lock);
  5109. }
  5110. /**
  5111. * vlv_force_pll_on - forcibly enable just the PLL
  5112. * @dev_priv: i915 private structure
  5113. * @pipe: pipe PLL to enable
  5114. * @dpll: PLL configuration
  5115. *
  5116. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5117. * in cases where we need the PLL enabled even when @pipe is not going to
  5118. * be enabled.
  5119. */
  5120. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  5121. const struct dpll *dpll)
  5122. {
  5123. struct intel_crtc *crtc =
  5124. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  5125. struct intel_crtc_state pipe_config = {
  5126. .pixel_multiplier = 1,
  5127. .dpll = *dpll,
  5128. };
  5129. if (IS_CHERRYVIEW(dev)) {
  5130. chv_update_pll(crtc, &pipe_config);
  5131. chv_prepare_pll(crtc, &pipe_config);
  5132. chv_enable_pll(crtc, &pipe_config);
  5133. } else {
  5134. vlv_update_pll(crtc, &pipe_config);
  5135. vlv_prepare_pll(crtc, &pipe_config);
  5136. vlv_enable_pll(crtc, &pipe_config);
  5137. }
  5138. }
  5139. /**
  5140. * vlv_force_pll_off - forcibly disable just the PLL
  5141. * @dev_priv: i915 private structure
  5142. * @pipe: pipe PLL to disable
  5143. *
  5144. * Disable the PLL for @pipe. To be used in cases where we need
  5145. * the PLL enabled even when @pipe is not going to be enabled.
  5146. */
  5147. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  5148. {
  5149. if (IS_CHERRYVIEW(dev))
  5150. chv_disable_pll(to_i915(dev), pipe);
  5151. else
  5152. vlv_disable_pll(to_i915(dev), pipe);
  5153. }
  5154. static void i9xx_update_pll(struct intel_crtc *crtc,
  5155. struct intel_crtc_state *crtc_state,
  5156. intel_clock_t *reduced_clock,
  5157. int num_connectors)
  5158. {
  5159. struct drm_device *dev = crtc->base.dev;
  5160. struct drm_i915_private *dev_priv = dev->dev_private;
  5161. u32 dpll;
  5162. bool is_sdvo;
  5163. struct dpll *clock = &crtc_state->dpll;
  5164. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5165. is_sdvo = intel_pipe_will_have_type(crtc, INTEL_OUTPUT_SDVO) ||
  5166. intel_pipe_will_have_type(crtc, INTEL_OUTPUT_HDMI);
  5167. dpll = DPLL_VGA_MODE_DIS;
  5168. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  5169. dpll |= DPLLB_MODE_LVDS;
  5170. else
  5171. dpll |= DPLLB_MODE_DAC_SERIAL;
  5172. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5173. dpll |= (crtc_state->pixel_multiplier - 1)
  5174. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5175. }
  5176. if (is_sdvo)
  5177. dpll |= DPLL_SDVO_HIGH_SPEED;
  5178. if (crtc_state->has_dp_encoder)
  5179. dpll |= DPLL_SDVO_HIGH_SPEED;
  5180. /* compute bitmask from p1 value */
  5181. if (IS_PINEVIEW(dev))
  5182. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5183. else {
  5184. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5185. if (IS_G4X(dev) && reduced_clock)
  5186. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5187. }
  5188. switch (clock->p2) {
  5189. case 5:
  5190. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5191. break;
  5192. case 7:
  5193. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5194. break;
  5195. case 10:
  5196. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5197. break;
  5198. case 14:
  5199. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5200. break;
  5201. }
  5202. if (INTEL_INFO(dev)->gen >= 4)
  5203. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5204. if (crtc_state->sdvo_tv_clock)
  5205. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5206. else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  5207. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5208. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5209. else
  5210. dpll |= PLL_REF_INPUT_DREFCLK;
  5211. dpll |= DPLL_VCO_ENABLE;
  5212. crtc_state->dpll_hw_state.dpll = dpll;
  5213. if (INTEL_INFO(dev)->gen >= 4) {
  5214. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  5215. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5216. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  5217. }
  5218. }
  5219. static void i8xx_update_pll(struct intel_crtc *crtc,
  5220. struct intel_crtc_state *crtc_state,
  5221. intel_clock_t *reduced_clock,
  5222. int num_connectors)
  5223. {
  5224. struct drm_device *dev = crtc->base.dev;
  5225. struct drm_i915_private *dev_priv = dev->dev_private;
  5226. u32 dpll;
  5227. struct dpll *clock = &crtc_state->dpll;
  5228. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5229. dpll = DPLL_VGA_MODE_DIS;
  5230. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  5231. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5232. } else {
  5233. if (clock->p1 == 2)
  5234. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5235. else
  5236. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5237. if (clock->p2 == 4)
  5238. dpll |= PLL_P2_DIVIDE_BY_4;
  5239. }
  5240. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc, INTEL_OUTPUT_DVO))
  5241. dpll |= DPLL_DVO_2X_MODE;
  5242. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  5243. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5244. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5245. else
  5246. dpll |= PLL_REF_INPUT_DREFCLK;
  5247. dpll |= DPLL_VCO_ENABLE;
  5248. crtc_state->dpll_hw_state.dpll = dpll;
  5249. }
  5250. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5251. {
  5252. struct drm_device *dev = intel_crtc->base.dev;
  5253. struct drm_i915_private *dev_priv = dev->dev_private;
  5254. enum pipe pipe = intel_crtc->pipe;
  5255. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  5256. struct drm_display_mode *adjusted_mode =
  5257. &intel_crtc->config->base.adjusted_mode;
  5258. uint32_t crtc_vtotal, crtc_vblank_end;
  5259. int vsyncshift = 0;
  5260. /* We need to be careful not to changed the adjusted mode, for otherwise
  5261. * the hw state checker will get angry at the mismatch. */
  5262. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5263. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5264. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5265. /* the chip adds 2 halflines automatically */
  5266. crtc_vtotal -= 1;
  5267. crtc_vblank_end -= 1;
  5268. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  5269. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5270. else
  5271. vsyncshift = adjusted_mode->crtc_hsync_start -
  5272. adjusted_mode->crtc_htotal / 2;
  5273. if (vsyncshift < 0)
  5274. vsyncshift += adjusted_mode->crtc_htotal;
  5275. }
  5276. if (INTEL_INFO(dev)->gen > 3)
  5277. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5278. I915_WRITE(HTOTAL(cpu_transcoder),
  5279. (adjusted_mode->crtc_hdisplay - 1) |
  5280. ((adjusted_mode->crtc_htotal - 1) << 16));
  5281. I915_WRITE(HBLANK(cpu_transcoder),
  5282. (adjusted_mode->crtc_hblank_start - 1) |
  5283. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5284. I915_WRITE(HSYNC(cpu_transcoder),
  5285. (adjusted_mode->crtc_hsync_start - 1) |
  5286. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5287. I915_WRITE(VTOTAL(cpu_transcoder),
  5288. (adjusted_mode->crtc_vdisplay - 1) |
  5289. ((crtc_vtotal - 1) << 16));
  5290. I915_WRITE(VBLANK(cpu_transcoder),
  5291. (adjusted_mode->crtc_vblank_start - 1) |
  5292. ((crtc_vblank_end - 1) << 16));
  5293. I915_WRITE(VSYNC(cpu_transcoder),
  5294. (adjusted_mode->crtc_vsync_start - 1) |
  5295. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5296. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5297. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5298. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5299. * bits. */
  5300. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  5301. (pipe == PIPE_B || pipe == PIPE_C))
  5302. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5303. /* pipesrc controls the size that is scaled from, which should
  5304. * always be the user's requested size.
  5305. */
  5306. I915_WRITE(PIPESRC(pipe),
  5307. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  5308. (intel_crtc->config->pipe_src_h - 1));
  5309. }
  5310. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5311. struct intel_crtc_state *pipe_config)
  5312. {
  5313. struct drm_device *dev = crtc->base.dev;
  5314. struct drm_i915_private *dev_priv = dev->dev_private;
  5315. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5316. uint32_t tmp;
  5317. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5318. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5319. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5320. tmp = I915_READ(HBLANK(cpu_transcoder));
  5321. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5322. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5323. tmp = I915_READ(HSYNC(cpu_transcoder));
  5324. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5325. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5326. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5327. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5328. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  5329. tmp = I915_READ(VBLANK(cpu_transcoder));
  5330. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  5331. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  5332. tmp = I915_READ(VSYNC(cpu_transcoder));
  5333. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  5334. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  5335. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  5336. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  5337. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  5338. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  5339. }
  5340. tmp = I915_READ(PIPESRC(crtc->pipe));
  5341. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  5342. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  5343. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  5344. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  5345. }
  5346. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  5347. struct intel_crtc_state *pipe_config)
  5348. {
  5349. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  5350. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  5351. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  5352. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  5353. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  5354. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  5355. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  5356. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  5357. mode->flags = pipe_config->base.adjusted_mode.flags;
  5358. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  5359. mode->flags |= pipe_config->base.adjusted_mode.flags;
  5360. }
  5361. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  5362. {
  5363. struct drm_device *dev = intel_crtc->base.dev;
  5364. struct drm_i915_private *dev_priv = dev->dev_private;
  5365. uint32_t pipeconf;
  5366. pipeconf = 0;
  5367. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  5368. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  5369. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  5370. if (intel_crtc->config->double_wide)
  5371. pipeconf |= PIPECONF_DOUBLE_WIDE;
  5372. /* only g4x and later have fancy bpc/dither controls */
  5373. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5374. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  5375. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  5376. pipeconf |= PIPECONF_DITHER_EN |
  5377. PIPECONF_DITHER_TYPE_SP;
  5378. switch (intel_crtc->config->pipe_bpp) {
  5379. case 18:
  5380. pipeconf |= PIPECONF_6BPC;
  5381. break;
  5382. case 24:
  5383. pipeconf |= PIPECONF_8BPC;
  5384. break;
  5385. case 30:
  5386. pipeconf |= PIPECONF_10BPC;
  5387. break;
  5388. default:
  5389. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5390. BUG();
  5391. }
  5392. }
  5393. if (HAS_PIPE_CXSR(dev)) {
  5394. if (intel_crtc->lowfreq_avail) {
  5395. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  5396. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  5397. } else {
  5398. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  5399. }
  5400. }
  5401. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  5402. if (INTEL_INFO(dev)->gen < 4 ||
  5403. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  5404. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  5405. else
  5406. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  5407. } else
  5408. pipeconf |= PIPECONF_PROGRESSIVE;
  5409. if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
  5410. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  5411. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  5412. POSTING_READ(PIPECONF(intel_crtc->pipe));
  5413. }
  5414. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  5415. struct intel_crtc_state *crtc_state)
  5416. {
  5417. struct drm_device *dev = crtc->base.dev;
  5418. struct drm_i915_private *dev_priv = dev->dev_private;
  5419. int refclk, num_connectors = 0;
  5420. intel_clock_t clock, reduced_clock;
  5421. bool ok, has_reduced_clock = false;
  5422. bool is_lvds = false, is_dsi = false;
  5423. struct intel_encoder *encoder;
  5424. const intel_limit_t *limit;
  5425. for_each_intel_encoder(dev, encoder) {
  5426. if (encoder->new_crtc != crtc)
  5427. continue;
  5428. switch (encoder->type) {
  5429. case INTEL_OUTPUT_LVDS:
  5430. is_lvds = true;
  5431. break;
  5432. case INTEL_OUTPUT_DSI:
  5433. is_dsi = true;
  5434. break;
  5435. default:
  5436. break;
  5437. }
  5438. num_connectors++;
  5439. }
  5440. if (is_dsi)
  5441. return 0;
  5442. if (!crtc_state->clock_set) {
  5443. refclk = i9xx_get_refclk(crtc, num_connectors);
  5444. /*
  5445. * Returns a set of divisors for the desired target clock with
  5446. * the given refclk, or FALSE. The returned values represent
  5447. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  5448. * 2) / p1 / p2.
  5449. */
  5450. limit = intel_limit(crtc, refclk);
  5451. ok = dev_priv->display.find_dpll(limit, crtc,
  5452. crtc_state->port_clock,
  5453. refclk, NULL, &clock);
  5454. if (!ok) {
  5455. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5456. return -EINVAL;
  5457. }
  5458. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5459. /*
  5460. * Ensure we match the reduced clock's P to the target
  5461. * clock. If the clocks don't match, we can't switch
  5462. * the display clock by using the FP0/FP1. In such case
  5463. * we will disable the LVDS downclock feature.
  5464. */
  5465. has_reduced_clock =
  5466. dev_priv->display.find_dpll(limit, crtc,
  5467. dev_priv->lvds_downclock,
  5468. refclk, &clock,
  5469. &reduced_clock);
  5470. }
  5471. /* Compat-code for transition, will disappear. */
  5472. crtc_state->dpll.n = clock.n;
  5473. crtc_state->dpll.m1 = clock.m1;
  5474. crtc_state->dpll.m2 = clock.m2;
  5475. crtc_state->dpll.p1 = clock.p1;
  5476. crtc_state->dpll.p2 = clock.p2;
  5477. }
  5478. if (IS_GEN2(dev)) {
  5479. i8xx_update_pll(crtc, crtc_state,
  5480. has_reduced_clock ? &reduced_clock : NULL,
  5481. num_connectors);
  5482. } else if (IS_CHERRYVIEW(dev)) {
  5483. chv_update_pll(crtc, crtc_state);
  5484. } else if (IS_VALLEYVIEW(dev)) {
  5485. vlv_update_pll(crtc, crtc_state);
  5486. } else {
  5487. i9xx_update_pll(crtc, crtc_state,
  5488. has_reduced_clock ? &reduced_clock : NULL,
  5489. num_connectors);
  5490. }
  5491. return 0;
  5492. }
  5493. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  5494. struct intel_crtc_state *pipe_config)
  5495. {
  5496. struct drm_device *dev = crtc->base.dev;
  5497. struct drm_i915_private *dev_priv = dev->dev_private;
  5498. uint32_t tmp;
  5499. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  5500. return;
  5501. tmp = I915_READ(PFIT_CONTROL);
  5502. if (!(tmp & PFIT_ENABLE))
  5503. return;
  5504. /* Check whether the pfit is attached to our pipe. */
  5505. if (INTEL_INFO(dev)->gen < 4) {
  5506. if (crtc->pipe != PIPE_B)
  5507. return;
  5508. } else {
  5509. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  5510. return;
  5511. }
  5512. pipe_config->gmch_pfit.control = tmp;
  5513. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  5514. if (INTEL_INFO(dev)->gen < 5)
  5515. pipe_config->gmch_pfit.lvds_border_bits =
  5516. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  5517. }
  5518. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  5519. struct intel_crtc_state *pipe_config)
  5520. {
  5521. struct drm_device *dev = crtc->base.dev;
  5522. struct drm_i915_private *dev_priv = dev->dev_private;
  5523. int pipe = pipe_config->cpu_transcoder;
  5524. intel_clock_t clock;
  5525. u32 mdiv;
  5526. int refclk = 100000;
  5527. /* In case of MIPI DPLL will not even be used */
  5528. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  5529. return;
  5530. mutex_lock(&dev_priv->dpio_lock);
  5531. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  5532. mutex_unlock(&dev_priv->dpio_lock);
  5533. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  5534. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  5535. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  5536. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  5537. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  5538. vlv_clock(refclk, &clock);
  5539. /* clock.dot is the fast clock */
  5540. pipe_config->port_clock = clock.dot / 5;
  5541. }
  5542. static void
  5543. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  5544. struct intel_initial_plane_config *plane_config)
  5545. {
  5546. struct drm_device *dev = crtc->base.dev;
  5547. struct drm_i915_private *dev_priv = dev->dev_private;
  5548. u32 val, base, offset;
  5549. int pipe = crtc->pipe, plane = crtc->plane;
  5550. int fourcc, pixel_format;
  5551. int aligned_height;
  5552. struct drm_framebuffer *fb;
  5553. struct intel_framebuffer *intel_fb;
  5554. val = I915_READ(DSPCNTR(plane));
  5555. if (!(val & DISPLAY_PLANE_ENABLE))
  5556. return;
  5557. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  5558. if (!intel_fb) {
  5559. DRM_DEBUG_KMS("failed to alloc fb\n");
  5560. return;
  5561. }
  5562. fb = &intel_fb->base;
  5563. if (INTEL_INFO(dev)->gen >= 4) {
  5564. if (val & DISPPLANE_TILED) {
  5565. plane_config->tiling = I915_TILING_X;
  5566. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  5567. }
  5568. }
  5569. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  5570. fourcc = i9xx_format_to_fourcc(pixel_format);
  5571. fb->pixel_format = fourcc;
  5572. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  5573. if (INTEL_INFO(dev)->gen >= 4) {
  5574. if (plane_config->tiling)
  5575. offset = I915_READ(DSPTILEOFF(plane));
  5576. else
  5577. offset = I915_READ(DSPLINOFF(plane));
  5578. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  5579. } else {
  5580. base = I915_READ(DSPADDR(plane));
  5581. }
  5582. plane_config->base = base;
  5583. val = I915_READ(PIPESRC(pipe));
  5584. fb->width = ((val >> 16) & 0xfff) + 1;
  5585. fb->height = ((val >> 0) & 0xfff) + 1;
  5586. val = I915_READ(DSPSTRIDE(pipe));
  5587. fb->pitches[0] = val & 0xffffffc0;
  5588. aligned_height = intel_fb_align_height(dev, fb->height,
  5589. fb->pixel_format,
  5590. fb->modifier[0]);
  5591. plane_config->size = PAGE_ALIGN(fb->pitches[0] * aligned_height);
  5592. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  5593. pipe_name(pipe), plane, fb->width, fb->height,
  5594. fb->bits_per_pixel, base, fb->pitches[0],
  5595. plane_config->size);
  5596. plane_config->fb = intel_fb;
  5597. }
  5598. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  5599. struct intel_crtc_state *pipe_config)
  5600. {
  5601. struct drm_device *dev = crtc->base.dev;
  5602. struct drm_i915_private *dev_priv = dev->dev_private;
  5603. int pipe = pipe_config->cpu_transcoder;
  5604. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5605. intel_clock_t clock;
  5606. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  5607. int refclk = 100000;
  5608. mutex_lock(&dev_priv->dpio_lock);
  5609. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  5610. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  5611. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  5612. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  5613. mutex_unlock(&dev_priv->dpio_lock);
  5614. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  5615. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  5616. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  5617. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  5618. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  5619. chv_clock(refclk, &clock);
  5620. /* clock.dot is the fast clock */
  5621. pipe_config->port_clock = clock.dot / 5;
  5622. }
  5623. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  5624. struct intel_crtc_state *pipe_config)
  5625. {
  5626. struct drm_device *dev = crtc->base.dev;
  5627. struct drm_i915_private *dev_priv = dev->dev_private;
  5628. uint32_t tmp;
  5629. if (!intel_display_power_is_enabled(dev_priv,
  5630. POWER_DOMAIN_PIPE(crtc->pipe)))
  5631. return false;
  5632. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5633. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5634. tmp = I915_READ(PIPECONF(crtc->pipe));
  5635. if (!(tmp & PIPECONF_ENABLE))
  5636. return false;
  5637. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5638. switch (tmp & PIPECONF_BPC_MASK) {
  5639. case PIPECONF_6BPC:
  5640. pipe_config->pipe_bpp = 18;
  5641. break;
  5642. case PIPECONF_8BPC:
  5643. pipe_config->pipe_bpp = 24;
  5644. break;
  5645. case PIPECONF_10BPC:
  5646. pipe_config->pipe_bpp = 30;
  5647. break;
  5648. default:
  5649. break;
  5650. }
  5651. }
  5652. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  5653. pipe_config->limited_color_range = true;
  5654. if (INTEL_INFO(dev)->gen < 4)
  5655. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  5656. intel_get_pipe_timings(crtc, pipe_config);
  5657. i9xx_get_pfit_config(crtc, pipe_config);
  5658. if (INTEL_INFO(dev)->gen >= 4) {
  5659. tmp = I915_READ(DPLL_MD(crtc->pipe));
  5660. pipe_config->pixel_multiplier =
  5661. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  5662. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  5663. pipe_config->dpll_hw_state.dpll_md = tmp;
  5664. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5665. tmp = I915_READ(DPLL(crtc->pipe));
  5666. pipe_config->pixel_multiplier =
  5667. ((tmp & SDVO_MULTIPLIER_MASK)
  5668. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  5669. } else {
  5670. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  5671. * port and will be fixed up in the encoder->get_config
  5672. * function. */
  5673. pipe_config->pixel_multiplier = 1;
  5674. }
  5675. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  5676. if (!IS_VALLEYVIEW(dev)) {
  5677. /*
  5678. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  5679. * on 830. Filter it out here so that we don't
  5680. * report errors due to that.
  5681. */
  5682. if (IS_I830(dev))
  5683. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  5684. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  5685. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  5686. } else {
  5687. /* Mask out read-only status bits. */
  5688. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  5689. DPLL_PORTC_READY_MASK |
  5690. DPLL_PORTB_READY_MASK);
  5691. }
  5692. if (IS_CHERRYVIEW(dev))
  5693. chv_crtc_clock_get(crtc, pipe_config);
  5694. else if (IS_VALLEYVIEW(dev))
  5695. vlv_crtc_clock_get(crtc, pipe_config);
  5696. else
  5697. i9xx_crtc_clock_get(crtc, pipe_config);
  5698. return true;
  5699. }
  5700. static void ironlake_init_pch_refclk(struct drm_device *dev)
  5701. {
  5702. struct drm_i915_private *dev_priv = dev->dev_private;
  5703. struct intel_encoder *encoder;
  5704. u32 val, final;
  5705. bool has_lvds = false;
  5706. bool has_cpu_edp = false;
  5707. bool has_panel = false;
  5708. bool has_ck505 = false;
  5709. bool can_ssc = false;
  5710. /* We need to take the global config into account */
  5711. for_each_intel_encoder(dev, encoder) {
  5712. switch (encoder->type) {
  5713. case INTEL_OUTPUT_LVDS:
  5714. has_panel = true;
  5715. has_lvds = true;
  5716. break;
  5717. case INTEL_OUTPUT_EDP:
  5718. has_panel = true;
  5719. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  5720. has_cpu_edp = true;
  5721. break;
  5722. default:
  5723. break;
  5724. }
  5725. }
  5726. if (HAS_PCH_IBX(dev)) {
  5727. has_ck505 = dev_priv->vbt.display_clock_mode;
  5728. can_ssc = has_ck505;
  5729. } else {
  5730. has_ck505 = false;
  5731. can_ssc = true;
  5732. }
  5733. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  5734. has_panel, has_lvds, has_ck505);
  5735. /* Ironlake: try to setup display ref clock before DPLL
  5736. * enabling. This is only under driver's control after
  5737. * PCH B stepping, previous chipset stepping should be
  5738. * ignoring this setting.
  5739. */
  5740. val = I915_READ(PCH_DREF_CONTROL);
  5741. /* As we must carefully and slowly disable/enable each source in turn,
  5742. * compute the final state we want first and check if we need to
  5743. * make any changes at all.
  5744. */
  5745. final = val;
  5746. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  5747. if (has_ck505)
  5748. final |= DREF_NONSPREAD_CK505_ENABLE;
  5749. else
  5750. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  5751. final &= ~DREF_SSC_SOURCE_MASK;
  5752. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5753. final &= ~DREF_SSC1_ENABLE;
  5754. if (has_panel) {
  5755. final |= DREF_SSC_SOURCE_ENABLE;
  5756. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5757. final |= DREF_SSC1_ENABLE;
  5758. if (has_cpu_edp) {
  5759. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5760. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5761. else
  5762. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5763. } else
  5764. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5765. } else {
  5766. final |= DREF_SSC_SOURCE_DISABLE;
  5767. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5768. }
  5769. if (final == val)
  5770. return;
  5771. /* Always enable nonspread source */
  5772. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  5773. if (has_ck505)
  5774. val |= DREF_NONSPREAD_CK505_ENABLE;
  5775. else
  5776. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  5777. if (has_panel) {
  5778. val &= ~DREF_SSC_SOURCE_MASK;
  5779. val |= DREF_SSC_SOURCE_ENABLE;
  5780. /* SSC must be turned on before enabling the CPU output */
  5781. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5782. DRM_DEBUG_KMS("Using SSC on panel\n");
  5783. val |= DREF_SSC1_ENABLE;
  5784. } else
  5785. val &= ~DREF_SSC1_ENABLE;
  5786. /* Get SSC going before enabling the outputs */
  5787. I915_WRITE(PCH_DREF_CONTROL, val);
  5788. POSTING_READ(PCH_DREF_CONTROL);
  5789. udelay(200);
  5790. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5791. /* Enable CPU source on CPU attached eDP */
  5792. if (has_cpu_edp) {
  5793. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5794. DRM_DEBUG_KMS("Using SSC on eDP\n");
  5795. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5796. } else
  5797. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5798. } else
  5799. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5800. I915_WRITE(PCH_DREF_CONTROL, val);
  5801. POSTING_READ(PCH_DREF_CONTROL);
  5802. udelay(200);
  5803. } else {
  5804. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  5805. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5806. /* Turn off CPU output */
  5807. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5808. I915_WRITE(PCH_DREF_CONTROL, val);
  5809. POSTING_READ(PCH_DREF_CONTROL);
  5810. udelay(200);
  5811. /* Turn off the SSC source */
  5812. val &= ~DREF_SSC_SOURCE_MASK;
  5813. val |= DREF_SSC_SOURCE_DISABLE;
  5814. /* Turn off SSC1 */
  5815. val &= ~DREF_SSC1_ENABLE;
  5816. I915_WRITE(PCH_DREF_CONTROL, val);
  5817. POSTING_READ(PCH_DREF_CONTROL);
  5818. udelay(200);
  5819. }
  5820. BUG_ON(val != final);
  5821. }
  5822. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  5823. {
  5824. uint32_t tmp;
  5825. tmp = I915_READ(SOUTH_CHICKEN2);
  5826. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  5827. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5828. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  5829. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  5830. DRM_ERROR("FDI mPHY reset assert timeout\n");
  5831. tmp = I915_READ(SOUTH_CHICKEN2);
  5832. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  5833. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5834. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  5835. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  5836. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  5837. }
  5838. /* WaMPhyProgramming:hsw */
  5839. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  5840. {
  5841. uint32_t tmp;
  5842. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  5843. tmp &= ~(0xFF << 24);
  5844. tmp |= (0x12 << 24);
  5845. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  5846. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  5847. tmp |= (1 << 11);
  5848. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  5849. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  5850. tmp |= (1 << 11);
  5851. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  5852. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  5853. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5854. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  5855. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  5856. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5857. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  5858. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  5859. tmp &= ~(7 << 13);
  5860. tmp |= (5 << 13);
  5861. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  5862. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  5863. tmp &= ~(7 << 13);
  5864. tmp |= (5 << 13);
  5865. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  5866. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  5867. tmp &= ~0xFF;
  5868. tmp |= 0x1C;
  5869. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  5870. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  5871. tmp &= ~0xFF;
  5872. tmp |= 0x1C;
  5873. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  5874. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  5875. tmp &= ~(0xFF << 16);
  5876. tmp |= (0x1C << 16);
  5877. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  5878. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  5879. tmp &= ~(0xFF << 16);
  5880. tmp |= (0x1C << 16);
  5881. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  5882. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  5883. tmp |= (1 << 27);
  5884. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  5885. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  5886. tmp |= (1 << 27);
  5887. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  5888. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  5889. tmp &= ~(0xF << 28);
  5890. tmp |= (4 << 28);
  5891. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  5892. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  5893. tmp &= ~(0xF << 28);
  5894. tmp |= (4 << 28);
  5895. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  5896. }
  5897. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  5898. * Programming" based on the parameters passed:
  5899. * - Sequence to enable CLKOUT_DP
  5900. * - Sequence to enable CLKOUT_DP without spread
  5901. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  5902. */
  5903. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  5904. bool with_fdi)
  5905. {
  5906. struct drm_i915_private *dev_priv = dev->dev_private;
  5907. uint32_t reg, tmp;
  5908. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  5909. with_spread = true;
  5910. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  5911. with_fdi, "LP PCH doesn't have FDI\n"))
  5912. with_fdi = false;
  5913. mutex_lock(&dev_priv->dpio_lock);
  5914. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5915. tmp &= ~SBI_SSCCTL_DISABLE;
  5916. tmp |= SBI_SSCCTL_PATHALT;
  5917. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5918. udelay(24);
  5919. if (with_spread) {
  5920. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5921. tmp &= ~SBI_SSCCTL_PATHALT;
  5922. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5923. if (with_fdi) {
  5924. lpt_reset_fdi_mphy(dev_priv);
  5925. lpt_program_fdi_mphy(dev_priv);
  5926. }
  5927. }
  5928. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5929. SBI_GEN0 : SBI_DBUFF0;
  5930. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5931. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5932. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5933. mutex_unlock(&dev_priv->dpio_lock);
  5934. }
  5935. /* Sequence to disable CLKOUT_DP */
  5936. static void lpt_disable_clkout_dp(struct drm_device *dev)
  5937. {
  5938. struct drm_i915_private *dev_priv = dev->dev_private;
  5939. uint32_t reg, tmp;
  5940. mutex_lock(&dev_priv->dpio_lock);
  5941. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5942. SBI_GEN0 : SBI_DBUFF0;
  5943. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5944. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5945. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5946. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5947. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  5948. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  5949. tmp |= SBI_SSCCTL_PATHALT;
  5950. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5951. udelay(32);
  5952. }
  5953. tmp |= SBI_SSCCTL_DISABLE;
  5954. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5955. }
  5956. mutex_unlock(&dev_priv->dpio_lock);
  5957. }
  5958. static void lpt_init_pch_refclk(struct drm_device *dev)
  5959. {
  5960. struct intel_encoder *encoder;
  5961. bool has_vga = false;
  5962. for_each_intel_encoder(dev, encoder) {
  5963. switch (encoder->type) {
  5964. case INTEL_OUTPUT_ANALOG:
  5965. has_vga = true;
  5966. break;
  5967. default:
  5968. break;
  5969. }
  5970. }
  5971. if (has_vga)
  5972. lpt_enable_clkout_dp(dev, true, true);
  5973. else
  5974. lpt_disable_clkout_dp(dev);
  5975. }
  5976. /*
  5977. * Initialize reference clocks when the driver loads
  5978. */
  5979. void intel_init_pch_refclk(struct drm_device *dev)
  5980. {
  5981. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  5982. ironlake_init_pch_refclk(dev);
  5983. else if (HAS_PCH_LPT(dev))
  5984. lpt_init_pch_refclk(dev);
  5985. }
  5986. static int ironlake_get_refclk(struct drm_crtc *crtc)
  5987. {
  5988. struct drm_device *dev = crtc->dev;
  5989. struct drm_i915_private *dev_priv = dev->dev_private;
  5990. struct intel_encoder *encoder;
  5991. int num_connectors = 0;
  5992. bool is_lvds = false;
  5993. for_each_intel_encoder(dev, encoder) {
  5994. if (encoder->new_crtc != to_intel_crtc(crtc))
  5995. continue;
  5996. switch (encoder->type) {
  5997. case INTEL_OUTPUT_LVDS:
  5998. is_lvds = true;
  5999. break;
  6000. default:
  6001. break;
  6002. }
  6003. num_connectors++;
  6004. }
  6005. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  6006. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  6007. dev_priv->vbt.lvds_ssc_freq);
  6008. return dev_priv->vbt.lvds_ssc_freq;
  6009. }
  6010. return 120000;
  6011. }
  6012. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  6013. {
  6014. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  6015. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6016. int pipe = intel_crtc->pipe;
  6017. uint32_t val;
  6018. val = 0;
  6019. switch (intel_crtc->config->pipe_bpp) {
  6020. case 18:
  6021. val |= PIPECONF_6BPC;
  6022. break;
  6023. case 24:
  6024. val |= PIPECONF_8BPC;
  6025. break;
  6026. case 30:
  6027. val |= PIPECONF_10BPC;
  6028. break;
  6029. case 36:
  6030. val |= PIPECONF_12BPC;
  6031. break;
  6032. default:
  6033. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6034. BUG();
  6035. }
  6036. if (intel_crtc->config->dither)
  6037. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6038. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6039. val |= PIPECONF_INTERLACED_ILK;
  6040. else
  6041. val |= PIPECONF_PROGRESSIVE;
  6042. if (intel_crtc->config->limited_color_range)
  6043. val |= PIPECONF_COLOR_RANGE_SELECT;
  6044. I915_WRITE(PIPECONF(pipe), val);
  6045. POSTING_READ(PIPECONF(pipe));
  6046. }
  6047. /*
  6048. * Set up the pipe CSC unit.
  6049. *
  6050. * Currently only full range RGB to limited range RGB conversion
  6051. * is supported, but eventually this should handle various
  6052. * RGB<->YCbCr scenarios as well.
  6053. */
  6054. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  6055. {
  6056. struct drm_device *dev = crtc->dev;
  6057. struct drm_i915_private *dev_priv = dev->dev_private;
  6058. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6059. int pipe = intel_crtc->pipe;
  6060. uint16_t coeff = 0x7800; /* 1.0 */
  6061. /*
  6062. * TODO: Check what kind of values actually come out of the pipe
  6063. * with these coeff/postoff values and adjust to get the best
  6064. * accuracy. Perhaps we even need to take the bpc value into
  6065. * consideration.
  6066. */
  6067. if (intel_crtc->config->limited_color_range)
  6068. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  6069. /*
  6070. * GY/GU and RY/RU should be the other way around according
  6071. * to BSpec, but reality doesn't agree. Just set them up in
  6072. * a way that results in the correct picture.
  6073. */
  6074. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  6075. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  6076. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  6077. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  6078. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  6079. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  6080. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  6081. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  6082. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  6083. if (INTEL_INFO(dev)->gen > 6) {
  6084. uint16_t postoff = 0;
  6085. if (intel_crtc->config->limited_color_range)
  6086. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  6087. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  6088. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  6089. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  6090. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  6091. } else {
  6092. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  6093. if (intel_crtc->config->limited_color_range)
  6094. mode |= CSC_BLACK_SCREEN_OFFSET;
  6095. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  6096. }
  6097. }
  6098. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6099. {
  6100. struct drm_device *dev = crtc->dev;
  6101. struct drm_i915_private *dev_priv = dev->dev_private;
  6102. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6103. enum pipe pipe = intel_crtc->pipe;
  6104. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6105. uint32_t val;
  6106. val = 0;
  6107. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  6108. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6109. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6110. val |= PIPECONF_INTERLACED_ILK;
  6111. else
  6112. val |= PIPECONF_PROGRESSIVE;
  6113. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6114. POSTING_READ(PIPECONF(cpu_transcoder));
  6115. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  6116. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  6117. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  6118. val = 0;
  6119. switch (intel_crtc->config->pipe_bpp) {
  6120. case 18:
  6121. val |= PIPEMISC_DITHER_6_BPC;
  6122. break;
  6123. case 24:
  6124. val |= PIPEMISC_DITHER_8_BPC;
  6125. break;
  6126. case 30:
  6127. val |= PIPEMISC_DITHER_10_BPC;
  6128. break;
  6129. case 36:
  6130. val |= PIPEMISC_DITHER_12_BPC;
  6131. break;
  6132. default:
  6133. /* Case prevented by pipe_config_set_bpp. */
  6134. BUG();
  6135. }
  6136. if (intel_crtc->config->dither)
  6137. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  6138. I915_WRITE(PIPEMISC(pipe), val);
  6139. }
  6140. }
  6141. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  6142. struct intel_crtc_state *crtc_state,
  6143. intel_clock_t *clock,
  6144. bool *has_reduced_clock,
  6145. intel_clock_t *reduced_clock)
  6146. {
  6147. struct drm_device *dev = crtc->dev;
  6148. struct drm_i915_private *dev_priv = dev->dev_private;
  6149. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6150. int refclk;
  6151. const intel_limit_t *limit;
  6152. bool ret, is_lvds = false;
  6153. is_lvds = intel_pipe_will_have_type(intel_crtc, INTEL_OUTPUT_LVDS);
  6154. refclk = ironlake_get_refclk(crtc);
  6155. /*
  6156. * Returns a set of divisors for the desired target clock with the given
  6157. * refclk, or FALSE. The returned values represent the clock equation:
  6158. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  6159. */
  6160. limit = intel_limit(intel_crtc, refclk);
  6161. ret = dev_priv->display.find_dpll(limit, intel_crtc,
  6162. crtc_state->port_clock,
  6163. refclk, NULL, clock);
  6164. if (!ret)
  6165. return false;
  6166. if (is_lvds && dev_priv->lvds_downclock_avail) {
  6167. /*
  6168. * Ensure we match the reduced clock's P to the target clock.
  6169. * If the clocks don't match, we can't switch the display clock
  6170. * by using the FP0/FP1. In such case we will disable the LVDS
  6171. * downclock feature.
  6172. */
  6173. *has_reduced_clock =
  6174. dev_priv->display.find_dpll(limit, intel_crtc,
  6175. dev_priv->lvds_downclock,
  6176. refclk, clock,
  6177. reduced_clock);
  6178. }
  6179. return true;
  6180. }
  6181. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  6182. {
  6183. /*
  6184. * Account for spread spectrum to avoid
  6185. * oversubscribing the link. Max center spread
  6186. * is 2.5%; use 5% for safety's sake.
  6187. */
  6188. u32 bps = target_clock * bpp * 21 / 20;
  6189. return DIV_ROUND_UP(bps, link_bw * 8);
  6190. }
  6191. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  6192. {
  6193. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  6194. }
  6195. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  6196. struct intel_crtc_state *crtc_state,
  6197. u32 *fp,
  6198. intel_clock_t *reduced_clock, u32 *fp2)
  6199. {
  6200. struct drm_crtc *crtc = &intel_crtc->base;
  6201. struct drm_device *dev = crtc->dev;
  6202. struct drm_i915_private *dev_priv = dev->dev_private;
  6203. struct intel_encoder *intel_encoder;
  6204. uint32_t dpll;
  6205. int factor, num_connectors = 0;
  6206. bool is_lvds = false, is_sdvo = false;
  6207. for_each_intel_encoder(dev, intel_encoder) {
  6208. if (intel_encoder->new_crtc != to_intel_crtc(crtc))
  6209. continue;
  6210. switch (intel_encoder->type) {
  6211. case INTEL_OUTPUT_LVDS:
  6212. is_lvds = true;
  6213. break;
  6214. case INTEL_OUTPUT_SDVO:
  6215. case INTEL_OUTPUT_HDMI:
  6216. is_sdvo = true;
  6217. break;
  6218. default:
  6219. break;
  6220. }
  6221. num_connectors++;
  6222. }
  6223. /* Enable autotuning of the PLL clock (if permissible) */
  6224. factor = 21;
  6225. if (is_lvds) {
  6226. if ((intel_panel_use_ssc(dev_priv) &&
  6227. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6228. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  6229. factor = 25;
  6230. } else if (crtc_state->sdvo_tv_clock)
  6231. factor = 20;
  6232. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  6233. *fp |= FP_CB_TUNE;
  6234. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  6235. *fp2 |= FP_CB_TUNE;
  6236. dpll = 0;
  6237. if (is_lvds)
  6238. dpll |= DPLLB_MODE_LVDS;
  6239. else
  6240. dpll |= DPLLB_MODE_DAC_SERIAL;
  6241. dpll |= (crtc_state->pixel_multiplier - 1)
  6242. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6243. if (is_sdvo)
  6244. dpll |= DPLL_SDVO_HIGH_SPEED;
  6245. if (crtc_state->has_dp_encoder)
  6246. dpll |= DPLL_SDVO_HIGH_SPEED;
  6247. /* compute bitmask from p1 value */
  6248. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6249. /* also FPA1 */
  6250. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6251. switch (crtc_state->dpll.p2) {
  6252. case 5:
  6253. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6254. break;
  6255. case 7:
  6256. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6257. break;
  6258. case 10:
  6259. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6260. break;
  6261. case 14:
  6262. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6263. break;
  6264. }
  6265. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6266. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6267. else
  6268. dpll |= PLL_REF_INPUT_DREFCLK;
  6269. return dpll | DPLL_VCO_ENABLE;
  6270. }
  6271. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  6272. struct intel_crtc_state *crtc_state)
  6273. {
  6274. struct drm_device *dev = crtc->base.dev;
  6275. intel_clock_t clock, reduced_clock;
  6276. u32 dpll = 0, fp = 0, fp2 = 0;
  6277. bool ok, has_reduced_clock = false;
  6278. bool is_lvds = false;
  6279. struct intel_shared_dpll *pll;
  6280. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  6281. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  6282. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  6283. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  6284. &has_reduced_clock, &reduced_clock);
  6285. if (!ok && !crtc_state->clock_set) {
  6286. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6287. return -EINVAL;
  6288. }
  6289. /* Compat-code for transition, will disappear. */
  6290. if (!crtc_state->clock_set) {
  6291. crtc_state->dpll.n = clock.n;
  6292. crtc_state->dpll.m1 = clock.m1;
  6293. crtc_state->dpll.m2 = clock.m2;
  6294. crtc_state->dpll.p1 = clock.p1;
  6295. crtc_state->dpll.p2 = clock.p2;
  6296. }
  6297. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  6298. if (crtc_state->has_pch_encoder) {
  6299. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6300. if (has_reduced_clock)
  6301. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  6302. dpll = ironlake_compute_dpll(crtc, crtc_state,
  6303. &fp, &reduced_clock,
  6304. has_reduced_clock ? &fp2 : NULL);
  6305. crtc_state->dpll_hw_state.dpll = dpll;
  6306. crtc_state->dpll_hw_state.fp0 = fp;
  6307. if (has_reduced_clock)
  6308. crtc_state->dpll_hw_state.fp1 = fp2;
  6309. else
  6310. crtc_state->dpll_hw_state.fp1 = fp;
  6311. pll = intel_get_shared_dpll(crtc, crtc_state);
  6312. if (pll == NULL) {
  6313. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  6314. pipe_name(crtc->pipe));
  6315. return -EINVAL;
  6316. }
  6317. }
  6318. if (is_lvds && has_reduced_clock && i915.powersave)
  6319. crtc->lowfreq_avail = true;
  6320. else
  6321. crtc->lowfreq_avail = false;
  6322. return 0;
  6323. }
  6324. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  6325. struct intel_link_m_n *m_n)
  6326. {
  6327. struct drm_device *dev = crtc->base.dev;
  6328. struct drm_i915_private *dev_priv = dev->dev_private;
  6329. enum pipe pipe = crtc->pipe;
  6330. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  6331. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  6332. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  6333. & ~TU_SIZE_MASK;
  6334. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  6335. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  6336. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6337. }
  6338. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  6339. enum transcoder transcoder,
  6340. struct intel_link_m_n *m_n,
  6341. struct intel_link_m_n *m2_n2)
  6342. {
  6343. struct drm_device *dev = crtc->base.dev;
  6344. struct drm_i915_private *dev_priv = dev->dev_private;
  6345. enum pipe pipe = crtc->pipe;
  6346. if (INTEL_INFO(dev)->gen >= 5) {
  6347. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  6348. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  6349. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  6350. & ~TU_SIZE_MASK;
  6351. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  6352. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  6353. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6354. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  6355. * gen < 8) and if DRRS is supported (to make sure the
  6356. * registers are not unnecessarily read).
  6357. */
  6358. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  6359. crtc->config->has_drrs) {
  6360. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  6361. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  6362. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  6363. & ~TU_SIZE_MASK;
  6364. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  6365. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  6366. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6367. }
  6368. } else {
  6369. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  6370. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  6371. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  6372. & ~TU_SIZE_MASK;
  6373. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  6374. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  6375. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6376. }
  6377. }
  6378. void intel_dp_get_m_n(struct intel_crtc *crtc,
  6379. struct intel_crtc_state *pipe_config)
  6380. {
  6381. if (pipe_config->has_pch_encoder)
  6382. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  6383. else
  6384. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6385. &pipe_config->dp_m_n,
  6386. &pipe_config->dp_m2_n2);
  6387. }
  6388. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  6389. struct intel_crtc_state *pipe_config)
  6390. {
  6391. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6392. &pipe_config->fdi_m_n, NULL);
  6393. }
  6394. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  6395. struct intel_crtc_state *pipe_config)
  6396. {
  6397. struct drm_device *dev = crtc->base.dev;
  6398. struct drm_i915_private *dev_priv = dev->dev_private;
  6399. uint32_t tmp;
  6400. tmp = I915_READ(PS_CTL(crtc->pipe));
  6401. if (tmp & PS_ENABLE) {
  6402. pipe_config->pch_pfit.enabled = true;
  6403. pipe_config->pch_pfit.pos = I915_READ(PS_WIN_POS(crtc->pipe));
  6404. pipe_config->pch_pfit.size = I915_READ(PS_WIN_SZ(crtc->pipe));
  6405. }
  6406. }
  6407. static void
  6408. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  6409. struct intel_initial_plane_config *plane_config)
  6410. {
  6411. struct drm_device *dev = crtc->base.dev;
  6412. struct drm_i915_private *dev_priv = dev->dev_private;
  6413. u32 val, base, offset, stride_mult;
  6414. int pipe = crtc->pipe;
  6415. int fourcc, pixel_format;
  6416. int aligned_height;
  6417. struct drm_framebuffer *fb;
  6418. struct intel_framebuffer *intel_fb;
  6419. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6420. if (!intel_fb) {
  6421. DRM_DEBUG_KMS("failed to alloc fb\n");
  6422. return;
  6423. }
  6424. fb = &intel_fb->base;
  6425. val = I915_READ(PLANE_CTL(pipe, 0));
  6426. if (!(val & PLANE_CTL_ENABLE))
  6427. goto error;
  6428. if (val & PLANE_CTL_TILED_MASK) {
  6429. plane_config->tiling = I915_TILING_X;
  6430. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6431. }
  6432. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  6433. fourcc = skl_format_to_fourcc(pixel_format,
  6434. val & PLANE_CTL_ORDER_RGBX,
  6435. val & PLANE_CTL_ALPHA_MASK);
  6436. fb->pixel_format = fourcc;
  6437. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6438. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  6439. plane_config->base = base;
  6440. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  6441. val = I915_READ(PLANE_SIZE(pipe, 0));
  6442. fb->height = ((val >> 16) & 0xfff) + 1;
  6443. fb->width = ((val >> 0) & 0x1fff) + 1;
  6444. val = I915_READ(PLANE_STRIDE(pipe, 0));
  6445. switch (plane_config->tiling) {
  6446. case I915_TILING_NONE:
  6447. stride_mult = 64;
  6448. break;
  6449. case I915_TILING_X:
  6450. stride_mult = 512;
  6451. break;
  6452. default:
  6453. MISSING_CASE(plane_config->tiling);
  6454. goto error;
  6455. }
  6456. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  6457. aligned_height = intel_fb_align_height(dev, fb->height,
  6458. fb->pixel_format,
  6459. fb->modifier[0]);
  6460. plane_config->size = ALIGN(fb->pitches[0] * aligned_height, PAGE_SIZE);
  6461. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6462. pipe_name(pipe), fb->width, fb->height,
  6463. fb->bits_per_pixel, base, fb->pitches[0],
  6464. plane_config->size);
  6465. plane_config->fb = intel_fb;
  6466. return;
  6467. error:
  6468. kfree(fb);
  6469. }
  6470. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  6471. struct intel_crtc_state *pipe_config)
  6472. {
  6473. struct drm_device *dev = crtc->base.dev;
  6474. struct drm_i915_private *dev_priv = dev->dev_private;
  6475. uint32_t tmp;
  6476. tmp = I915_READ(PF_CTL(crtc->pipe));
  6477. if (tmp & PF_ENABLE) {
  6478. pipe_config->pch_pfit.enabled = true;
  6479. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  6480. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  6481. /* We currently do not free assignements of panel fitters on
  6482. * ivb/hsw (since we don't use the higher upscaling modes which
  6483. * differentiates them) so just WARN about this case for now. */
  6484. if (IS_GEN7(dev)) {
  6485. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  6486. PF_PIPE_SEL_IVB(crtc->pipe));
  6487. }
  6488. }
  6489. }
  6490. static void
  6491. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  6492. struct intel_initial_plane_config *plane_config)
  6493. {
  6494. struct drm_device *dev = crtc->base.dev;
  6495. struct drm_i915_private *dev_priv = dev->dev_private;
  6496. u32 val, base, offset;
  6497. int pipe = crtc->pipe;
  6498. int fourcc, pixel_format;
  6499. int aligned_height;
  6500. struct drm_framebuffer *fb;
  6501. struct intel_framebuffer *intel_fb;
  6502. val = I915_READ(DSPCNTR(pipe));
  6503. if (!(val & DISPLAY_PLANE_ENABLE))
  6504. return;
  6505. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6506. if (!intel_fb) {
  6507. DRM_DEBUG_KMS("failed to alloc fb\n");
  6508. return;
  6509. }
  6510. fb = &intel_fb->base;
  6511. if (INTEL_INFO(dev)->gen >= 4) {
  6512. if (val & DISPPLANE_TILED) {
  6513. plane_config->tiling = I915_TILING_X;
  6514. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6515. }
  6516. }
  6517. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6518. fourcc = i9xx_format_to_fourcc(pixel_format);
  6519. fb->pixel_format = fourcc;
  6520. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6521. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  6522. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  6523. offset = I915_READ(DSPOFFSET(pipe));
  6524. } else {
  6525. if (plane_config->tiling)
  6526. offset = I915_READ(DSPTILEOFF(pipe));
  6527. else
  6528. offset = I915_READ(DSPLINOFF(pipe));
  6529. }
  6530. plane_config->base = base;
  6531. val = I915_READ(PIPESRC(pipe));
  6532. fb->width = ((val >> 16) & 0xfff) + 1;
  6533. fb->height = ((val >> 0) & 0xfff) + 1;
  6534. val = I915_READ(DSPSTRIDE(pipe));
  6535. fb->pitches[0] = val & 0xffffffc0;
  6536. aligned_height = intel_fb_align_height(dev, fb->height,
  6537. fb->pixel_format,
  6538. fb->modifier[0]);
  6539. plane_config->size = PAGE_ALIGN(fb->pitches[0] * aligned_height);
  6540. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6541. pipe_name(pipe), fb->width, fb->height,
  6542. fb->bits_per_pixel, base, fb->pitches[0],
  6543. plane_config->size);
  6544. plane_config->fb = intel_fb;
  6545. }
  6546. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  6547. struct intel_crtc_state *pipe_config)
  6548. {
  6549. struct drm_device *dev = crtc->base.dev;
  6550. struct drm_i915_private *dev_priv = dev->dev_private;
  6551. uint32_t tmp;
  6552. if (!intel_display_power_is_enabled(dev_priv,
  6553. POWER_DOMAIN_PIPE(crtc->pipe)))
  6554. return false;
  6555. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6556. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6557. tmp = I915_READ(PIPECONF(crtc->pipe));
  6558. if (!(tmp & PIPECONF_ENABLE))
  6559. return false;
  6560. switch (tmp & PIPECONF_BPC_MASK) {
  6561. case PIPECONF_6BPC:
  6562. pipe_config->pipe_bpp = 18;
  6563. break;
  6564. case PIPECONF_8BPC:
  6565. pipe_config->pipe_bpp = 24;
  6566. break;
  6567. case PIPECONF_10BPC:
  6568. pipe_config->pipe_bpp = 30;
  6569. break;
  6570. case PIPECONF_12BPC:
  6571. pipe_config->pipe_bpp = 36;
  6572. break;
  6573. default:
  6574. break;
  6575. }
  6576. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  6577. pipe_config->limited_color_range = true;
  6578. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  6579. struct intel_shared_dpll *pll;
  6580. pipe_config->has_pch_encoder = true;
  6581. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  6582. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6583. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6584. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6585. if (HAS_PCH_IBX(dev_priv->dev)) {
  6586. pipe_config->shared_dpll =
  6587. (enum intel_dpll_id) crtc->pipe;
  6588. } else {
  6589. tmp = I915_READ(PCH_DPLL_SEL);
  6590. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  6591. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  6592. else
  6593. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  6594. }
  6595. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6596. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6597. &pipe_config->dpll_hw_state));
  6598. tmp = pipe_config->dpll_hw_state.dpll;
  6599. pipe_config->pixel_multiplier =
  6600. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  6601. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  6602. ironlake_pch_clock_get(crtc, pipe_config);
  6603. } else {
  6604. pipe_config->pixel_multiplier = 1;
  6605. }
  6606. intel_get_pipe_timings(crtc, pipe_config);
  6607. ironlake_get_pfit_config(crtc, pipe_config);
  6608. return true;
  6609. }
  6610. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  6611. {
  6612. struct drm_device *dev = dev_priv->dev;
  6613. struct intel_crtc *crtc;
  6614. for_each_intel_crtc(dev, crtc)
  6615. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  6616. pipe_name(crtc->pipe));
  6617. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  6618. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  6619. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  6620. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  6621. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  6622. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  6623. "CPU PWM1 enabled\n");
  6624. if (IS_HASWELL(dev))
  6625. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  6626. "CPU PWM2 enabled\n");
  6627. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  6628. "PCH PWM1 enabled\n");
  6629. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  6630. "Utility pin enabled\n");
  6631. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  6632. /*
  6633. * In theory we can still leave IRQs enabled, as long as only the HPD
  6634. * interrupts remain enabled. We used to check for that, but since it's
  6635. * gen-specific and since we only disable LCPLL after we fully disable
  6636. * the interrupts, the check below should be enough.
  6637. */
  6638. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  6639. }
  6640. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  6641. {
  6642. struct drm_device *dev = dev_priv->dev;
  6643. if (IS_HASWELL(dev))
  6644. return I915_READ(D_COMP_HSW);
  6645. else
  6646. return I915_READ(D_COMP_BDW);
  6647. }
  6648. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  6649. {
  6650. struct drm_device *dev = dev_priv->dev;
  6651. if (IS_HASWELL(dev)) {
  6652. mutex_lock(&dev_priv->rps.hw_lock);
  6653. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  6654. val))
  6655. DRM_ERROR("Failed to write to D_COMP\n");
  6656. mutex_unlock(&dev_priv->rps.hw_lock);
  6657. } else {
  6658. I915_WRITE(D_COMP_BDW, val);
  6659. POSTING_READ(D_COMP_BDW);
  6660. }
  6661. }
  6662. /*
  6663. * This function implements pieces of two sequences from BSpec:
  6664. * - Sequence for display software to disable LCPLL
  6665. * - Sequence for display software to allow package C8+
  6666. * The steps implemented here are just the steps that actually touch the LCPLL
  6667. * register. Callers should take care of disabling all the display engine
  6668. * functions, doing the mode unset, fixing interrupts, etc.
  6669. */
  6670. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  6671. bool switch_to_fclk, bool allow_power_down)
  6672. {
  6673. uint32_t val;
  6674. assert_can_disable_lcpll(dev_priv);
  6675. val = I915_READ(LCPLL_CTL);
  6676. if (switch_to_fclk) {
  6677. val |= LCPLL_CD_SOURCE_FCLK;
  6678. I915_WRITE(LCPLL_CTL, val);
  6679. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  6680. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  6681. DRM_ERROR("Switching to FCLK failed\n");
  6682. val = I915_READ(LCPLL_CTL);
  6683. }
  6684. val |= LCPLL_PLL_DISABLE;
  6685. I915_WRITE(LCPLL_CTL, val);
  6686. POSTING_READ(LCPLL_CTL);
  6687. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  6688. DRM_ERROR("LCPLL still locked\n");
  6689. val = hsw_read_dcomp(dev_priv);
  6690. val |= D_COMP_COMP_DISABLE;
  6691. hsw_write_dcomp(dev_priv, val);
  6692. ndelay(100);
  6693. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  6694. 1))
  6695. DRM_ERROR("D_COMP RCOMP still in progress\n");
  6696. if (allow_power_down) {
  6697. val = I915_READ(LCPLL_CTL);
  6698. val |= LCPLL_POWER_DOWN_ALLOW;
  6699. I915_WRITE(LCPLL_CTL, val);
  6700. POSTING_READ(LCPLL_CTL);
  6701. }
  6702. }
  6703. /*
  6704. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  6705. * source.
  6706. */
  6707. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  6708. {
  6709. uint32_t val;
  6710. val = I915_READ(LCPLL_CTL);
  6711. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  6712. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  6713. return;
  6714. /*
  6715. * Make sure we're not on PC8 state before disabling PC8, otherwise
  6716. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  6717. */
  6718. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  6719. if (val & LCPLL_POWER_DOWN_ALLOW) {
  6720. val &= ~LCPLL_POWER_DOWN_ALLOW;
  6721. I915_WRITE(LCPLL_CTL, val);
  6722. POSTING_READ(LCPLL_CTL);
  6723. }
  6724. val = hsw_read_dcomp(dev_priv);
  6725. val |= D_COMP_COMP_FORCE;
  6726. val &= ~D_COMP_COMP_DISABLE;
  6727. hsw_write_dcomp(dev_priv, val);
  6728. val = I915_READ(LCPLL_CTL);
  6729. val &= ~LCPLL_PLL_DISABLE;
  6730. I915_WRITE(LCPLL_CTL, val);
  6731. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  6732. DRM_ERROR("LCPLL not locked yet\n");
  6733. if (val & LCPLL_CD_SOURCE_FCLK) {
  6734. val = I915_READ(LCPLL_CTL);
  6735. val &= ~LCPLL_CD_SOURCE_FCLK;
  6736. I915_WRITE(LCPLL_CTL, val);
  6737. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  6738. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  6739. DRM_ERROR("Switching back to LCPLL failed\n");
  6740. }
  6741. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  6742. }
  6743. /*
  6744. * Package states C8 and deeper are really deep PC states that can only be
  6745. * reached when all the devices on the system allow it, so even if the graphics
  6746. * device allows PC8+, it doesn't mean the system will actually get to these
  6747. * states. Our driver only allows PC8+ when going into runtime PM.
  6748. *
  6749. * The requirements for PC8+ are that all the outputs are disabled, the power
  6750. * well is disabled and most interrupts are disabled, and these are also
  6751. * requirements for runtime PM. When these conditions are met, we manually do
  6752. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  6753. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  6754. * hang the machine.
  6755. *
  6756. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  6757. * the state of some registers, so when we come back from PC8+ we need to
  6758. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  6759. * need to take care of the registers kept by RC6. Notice that this happens even
  6760. * if we don't put the device in PCI D3 state (which is what currently happens
  6761. * because of the runtime PM support).
  6762. *
  6763. * For more, read "Display Sequences for Package C8" on the hardware
  6764. * documentation.
  6765. */
  6766. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  6767. {
  6768. struct drm_device *dev = dev_priv->dev;
  6769. uint32_t val;
  6770. DRM_DEBUG_KMS("Enabling package C8+\n");
  6771. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6772. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6773. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  6774. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6775. }
  6776. lpt_disable_clkout_dp(dev);
  6777. hsw_disable_lcpll(dev_priv, true, true);
  6778. }
  6779. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  6780. {
  6781. struct drm_device *dev = dev_priv->dev;
  6782. uint32_t val;
  6783. DRM_DEBUG_KMS("Disabling package C8+\n");
  6784. hsw_restore_lcpll(dev_priv);
  6785. lpt_init_pch_refclk(dev);
  6786. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6787. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6788. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  6789. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6790. }
  6791. intel_prepare_ddi(dev);
  6792. }
  6793. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  6794. struct intel_crtc_state *crtc_state)
  6795. {
  6796. if (!intel_ddi_pll_select(crtc, crtc_state))
  6797. return -EINVAL;
  6798. crtc->lowfreq_avail = false;
  6799. return 0;
  6800. }
  6801. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  6802. enum port port,
  6803. struct intel_crtc_state *pipe_config)
  6804. {
  6805. u32 temp, dpll_ctl1;
  6806. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  6807. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  6808. switch (pipe_config->ddi_pll_sel) {
  6809. case SKL_DPLL0:
  6810. /*
  6811. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  6812. * of the shared DPLL framework and thus needs to be read out
  6813. * separately
  6814. */
  6815. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  6816. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  6817. break;
  6818. case SKL_DPLL1:
  6819. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  6820. break;
  6821. case SKL_DPLL2:
  6822. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  6823. break;
  6824. case SKL_DPLL3:
  6825. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  6826. break;
  6827. }
  6828. }
  6829. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  6830. enum port port,
  6831. struct intel_crtc_state *pipe_config)
  6832. {
  6833. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  6834. switch (pipe_config->ddi_pll_sel) {
  6835. case PORT_CLK_SEL_WRPLL1:
  6836. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  6837. break;
  6838. case PORT_CLK_SEL_WRPLL2:
  6839. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  6840. break;
  6841. }
  6842. }
  6843. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  6844. struct intel_crtc_state *pipe_config)
  6845. {
  6846. struct drm_device *dev = crtc->base.dev;
  6847. struct drm_i915_private *dev_priv = dev->dev_private;
  6848. struct intel_shared_dpll *pll;
  6849. enum port port;
  6850. uint32_t tmp;
  6851. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  6852. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  6853. if (IS_SKYLAKE(dev))
  6854. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  6855. else
  6856. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  6857. if (pipe_config->shared_dpll >= 0) {
  6858. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6859. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6860. &pipe_config->dpll_hw_state));
  6861. }
  6862. /*
  6863. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  6864. * DDI E. So just check whether this pipe is wired to DDI E and whether
  6865. * the PCH transcoder is on.
  6866. */
  6867. if (INTEL_INFO(dev)->gen < 9 &&
  6868. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  6869. pipe_config->has_pch_encoder = true;
  6870. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  6871. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6872. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6873. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6874. }
  6875. }
  6876. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  6877. struct intel_crtc_state *pipe_config)
  6878. {
  6879. struct drm_device *dev = crtc->base.dev;
  6880. struct drm_i915_private *dev_priv = dev->dev_private;
  6881. enum intel_display_power_domain pfit_domain;
  6882. uint32_t tmp;
  6883. if (!intel_display_power_is_enabled(dev_priv,
  6884. POWER_DOMAIN_PIPE(crtc->pipe)))
  6885. return false;
  6886. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6887. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6888. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  6889. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  6890. enum pipe trans_edp_pipe;
  6891. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  6892. default:
  6893. WARN(1, "unknown pipe linked to edp transcoder\n");
  6894. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  6895. case TRANS_DDI_EDP_INPUT_A_ON:
  6896. trans_edp_pipe = PIPE_A;
  6897. break;
  6898. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  6899. trans_edp_pipe = PIPE_B;
  6900. break;
  6901. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  6902. trans_edp_pipe = PIPE_C;
  6903. break;
  6904. }
  6905. if (trans_edp_pipe == crtc->pipe)
  6906. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  6907. }
  6908. if (!intel_display_power_is_enabled(dev_priv,
  6909. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  6910. return false;
  6911. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  6912. if (!(tmp & PIPECONF_ENABLE))
  6913. return false;
  6914. haswell_get_ddi_port_state(crtc, pipe_config);
  6915. intel_get_pipe_timings(crtc, pipe_config);
  6916. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  6917. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  6918. if (IS_SKYLAKE(dev))
  6919. skylake_get_pfit_config(crtc, pipe_config);
  6920. else
  6921. ironlake_get_pfit_config(crtc, pipe_config);
  6922. }
  6923. if (IS_HASWELL(dev))
  6924. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  6925. (I915_READ(IPS_CTL) & IPS_ENABLE);
  6926. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  6927. pipe_config->pixel_multiplier =
  6928. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  6929. } else {
  6930. pipe_config->pixel_multiplier = 1;
  6931. }
  6932. return true;
  6933. }
  6934. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  6935. {
  6936. struct drm_device *dev = crtc->dev;
  6937. struct drm_i915_private *dev_priv = dev->dev_private;
  6938. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6939. uint32_t cntl = 0, size = 0;
  6940. if (base) {
  6941. unsigned int width = intel_crtc->cursor_width;
  6942. unsigned int height = intel_crtc->cursor_height;
  6943. unsigned int stride = roundup_pow_of_two(width) * 4;
  6944. switch (stride) {
  6945. default:
  6946. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  6947. width, stride);
  6948. stride = 256;
  6949. /* fallthrough */
  6950. case 256:
  6951. case 512:
  6952. case 1024:
  6953. case 2048:
  6954. break;
  6955. }
  6956. cntl |= CURSOR_ENABLE |
  6957. CURSOR_GAMMA_ENABLE |
  6958. CURSOR_FORMAT_ARGB |
  6959. CURSOR_STRIDE(stride);
  6960. size = (height << 12) | width;
  6961. }
  6962. if (intel_crtc->cursor_cntl != 0 &&
  6963. (intel_crtc->cursor_base != base ||
  6964. intel_crtc->cursor_size != size ||
  6965. intel_crtc->cursor_cntl != cntl)) {
  6966. /* On these chipsets we can only modify the base/size/stride
  6967. * whilst the cursor is disabled.
  6968. */
  6969. I915_WRITE(_CURACNTR, 0);
  6970. POSTING_READ(_CURACNTR);
  6971. intel_crtc->cursor_cntl = 0;
  6972. }
  6973. if (intel_crtc->cursor_base != base) {
  6974. I915_WRITE(_CURABASE, base);
  6975. intel_crtc->cursor_base = base;
  6976. }
  6977. if (intel_crtc->cursor_size != size) {
  6978. I915_WRITE(CURSIZE, size);
  6979. intel_crtc->cursor_size = size;
  6980. }
  6981. if (intel_crtc->cursor_cntl != cntl) {
  6982. I915_WRITE(_CURACNTR, cntl);
  6983. POSTING_READ(_CURACNTR);
  6984. intel_crtc->cursor_cntl = cntl;
  6985. }
  6986. }
  6987. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  6988. {
  6989. struct drm_device *dev = crtc->dev;
  6990. struct drm_i915_private *dev_priv = dev->dev_private;
  6991. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6992. int pipe = intel_crtc->pipe;
  6993. uint32_t cntl;
  6994. cntl = 0;
  6995. if (base) {
  6996. cntl = MCURSOR_GAMMA_ENABLE;
  6997. switch (intel_crtc->cursor_width) {
  6998. case 64:
  6999. cntl |= CURSOR_MODE_64_ARGB_AX;
  7000. break;
  7001. case 128:
  7002. cntl |= CURSOR_MODE_128_ARGB_AX;
  7003. break;
  7004. case 256:
  7005. cntl |= CURSOR_MODE_256_ARGB_AX;
  7006. break;
  7007. default:
  7008. MISSING_CASE(intel_crtc->cursor_width);
  7009. return;
  7010. }
  7011. cntl |= pipe << 28; /* Connect to correct pipe */
  7012. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  7013. cntl |= CURSOR_PIPE_CSC_ENABLE;
  7014. }
  7015. if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
  7016. cntl |= CURSOR_ROTATE_180;
  7017. if (intel_crtc->cursor_cntl != cntl) {
  7018. I915_WRITE(CURCNTR(pipe), cntl);
  7019. POSTING_READ(CURCNTR(pipe));
  7020. intel_crtc->cursor_cntl = cntl;
  7021. }
  7022. /* and commit changes on next vblank */
  7023. I915_WRITE(CURBASE(pipe), base);
  7024. POSTING_READ(CURBASE(pipe));
  7025. intel_crtc->cursor_base = base;
  7026. }
  7027. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  7028. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  7029. bool on)
  7030. {
  7031. struct drm_device *dev = crtc->dev;
  7032. struct drm_i915_private *dev_priv = dev->dev_private;
  7033. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7034. int pipe = intel_crtc->pipe;
  7035. int x = crtc->cursor_x;
  7036. int y = crtc->cursor_y;
  7037. u32 base = 0, pos = 0;
  7038. if (on)
  7039. base = intel_crtc->cursor_addr;
  7040. if (x >= intel_crtc->config->pipe_src_w)
  7041. base = 0;
  7042. if (y >= intel_crtc->config->pipe_src_h)
  7043. base = 0;
  7044. if (x < 0) {
  7045. if (x + intel_crtc->cursor_width <= 0)
  7046. base = 0;
  7047. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  7048. x = -x;
  7049. }
  7050. pos |= x << CURSOR_X_SHIFT;
  7051. if (y < 0) {
  7052. if (y + intel_crtc->cursor_height <= 0)
  7053. base = 0;
  7054. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  7055. y = -y;
  7056. }
  7057. pos |= y << CURSOR_Y_SHIFT;
  7058. if (base == 0 && intel_crtc->cursor_base == 0)
  7059. return;
  7060. I915_WRITE(CURPOS(pipe), pos);
  7061. /* ILK+ do this automagically */
  7062. if (HAS_GMCH_DISPLAY(dev) &&
  7063. crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
  7064. base += (intel_crtc->cursor_height *
  7065. intel_crtc->cursor_width - 1) * 4;
  7066. }
  7067. if (IS_845G(dev) || IS_I865G(dev))
  7068. i845_update_cursor(crtc, base);
  7069. else
  7070. i9xx_update_cursor(crtc, base);
  7071. }
  7072. static bool cursor_size_ok(struct drm_device *dev,
  7073. uint32_t width, uint32_t height)
  7074. {
  7075. if (width == 0 || height == 0)
  7076. return false;
  7077. /*
  7078. * 845g/865g are special in that they are only limited by
  7079. * the width of their cursors, the height is arbitrary up to
  7080. * the precision of the register. Everything else requires
  7081. * square cursors, limited to a few power-of-two sizes.
  7082. */
  7083. if (IS_845G(dev) || IS_I865G(dev)) {
  7084. if ((width & 63) != 0)
  7085. return false;
  7086. if (width > (IS_845G(dev) ? 64 : 512))
  7087. return false;
  7088. if (height > 1023)
  7089. return false;
  7090. } else {
  7091. switch (width | height) {
  7092. case 256:
  7093. case 128:
  7094. if (IS_GEN2(dev))
  7095. return false;
  7096. case 64:
  7097. break;
  7098. default:
  7099. return false;
  7100. }
  7101. }
  7102. return true;
  7103. }
  7104. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  7105. u16 *blue, uint32_t start, uint32_t size)
  7106. {
  7107. int end = (start + size > 256) ? 256 : start + size, i;
  7108. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7109. for (i = start; i < end; i++) {
  7110. intel_crtc->lut_r[i] = red[i] >> 8;
  7111. intel_crtc->lut_g[i] = green[i] >> 8;
  7112. intel_crtc->lut_b[i] = blue[i] >> 8;
  7113. }
  7114. intel_crtc_load_lut(crtc);
  7115. }
  7116. /* VESA 640x480x72Hz mode to set on the pipe */
  7117. static struct drm_display_mode load_detect_mode = {
  7118. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  7119. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  7120. };
  7121. struct drm_framebuffer *
  7122. __intel_framebuffer_create(struct drm_device *dev,
  7123. struct drm_mode_fb_cmd2 *mode_cmd,
  7124. struct drm_i915_gem_object *obj)
  7125. {
  7126. struct intel_framebuffer *intel_fb;
  7127. int ret;
  7128. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7129. if (!intel_fb) {
  7130. drm_gem_object_unreference(&obj->base);
  7131. return ERR_PTR(-ENOMEM);
  7132. }
  7133. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  7134. if (ret)
  7135. goto err;
  7136. return &intel_fb->base;
  7137. err:
  7138. drm_gem_object_unreference(&obj->base);
  7139. kfree(intel_fb);
  7140. return ERR_PTR(ret);
  7141. }
  7142. static struct drm_framebuffer *
  7143. intel_framebuffer_create(struct drm_device *dev,
  7144. struct drm_mode_fb_cmd2 *mode_cmd,
  7145. struct drm_i915_gem_object *obj)
  7146. {
  7147. struct drm_framebuffer *fb;
  7148. int ret;
  7149. ret = i915_mutex_lock_interruptible(dev);
  7150. if (ret)
  7151. return ERR_PTR(ret);
  7152. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  7153. mutex_unlock(&dev->struct_mutex);
  7154. return fb;
  7155. }
  7156. static u32
  7157. intel_framebuffer_pitch_for_width(int width, int bpp)
  7158. {
  7159. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  7160. return ALIGN(pitch, 64);
  7161. }
  7162. static u32
  7163. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  7164. {
  7165. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  7166. return PAGE_ALIGN(pitch * mode->vdisplay);
  7167. }
  7168. static struct drm_framebuffer *
  7169. intel_framebuffer_create_for_mode(struct drm_device *dev,
  7170. struct drm_display_mode *mode,
  7171. int depth, int bpp)
  7172. {
  7173. struct drm_i915_gem_object *obj;
  7174. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  7175. obj = i915_gem_alloc_object(dev,
  7176. intel_framebuffer_size_for_mode(mode, bpp));
  7177. if (obj == NULL)
  7178. return ERR_PTR(-ENOMEM);
  7179. mode_cmd.width = mode->hdisplay;
  7180. mode_cmd.height = mode->vdisplay;
  7181. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  7182. bpp);
  7183. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  7184. return intel_framebuffer_create(dev, &mode_cmd, obj);
  7185. }
  7186. static struct drm_framebuffer *
  7187. mode_fits_in_fbdev(struct drm_device *dev,
  7188. struct drm_display_mode *mode)
  7189. {
  7190. #ifdef CONFIG_DRM_I915_FBDEV
  7191. struct drm_i915_private *dev_priv = dev->dev_private;
  7192. struct drm_i915_gem_object *obj;
  7193. struct drm_framebuffer *fb;
  7194. if (!dev_priv->fbdev)
  7195. return NULL;
  7196. if (!dev_priv->fbdev->fb)
  7197. return NULL;
  7198. obj = dev_priv->fbdev->fb->obj;
  7199. BUG_ON(!obj);
  7200. fb = &dev_priv->fbdev->fb->base;
  7201. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  7202. fb->bits_per_pixel))
  7203. return NULL;
  7204. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  7205. return NULL;
  7206. return fb;
  7207. #else
  7208. return NULL;
  7209. #endif
  7210. }
  7211. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  7212. struct drm_display_mode *mode,
  7213. struct intel_load_detect_pipe *old,
  7214. struct drm_modeset_acquire_ctx *ctx)
  7215. {
  7216. struct intel_crtc *intel_crtc;
  7217. struct intel_encoder *intel_encoder =
  7218. intel_attached_encoder(connector);
  7219. struct drm_crtc *possible_crtc;
  7220. struct drm_encoder *encoder = &intel_encoder->base;
  7221. struct drm_crtc *crtc = NULL;
  7222. struct drm_device *dev = encoder->dev;
  7223. struct drm_framebuffer *fb;
  7224. struct drm_mode_config *config = &dev->mode_config;
  7225. int ret, i = -1;
  7226. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7227. connector->base.id, connector->name,
  7228. encoder->base.id, encoder->name);
  7229. retry:
  7230. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  7231. if (ret)
  7232. goto fail_unlock;
  7233. /*
  7234. * Algorithm gets a little messy:
  7235. *
  7236. * - if the connector already has an assigned crtc, use it (but make
  7237. * sure it's on first)
  7238. *
  7239. * - try to find the first unused crtc that can drive this connector,
  7240. * and use that if we find one
  7241. */
  7242. /* See if we already have a CRTC for this connector */
  7243. if (encoder->crtc) {
  7244. crtc = encoder->crtc;
  7245. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7246. if (ret)
  7247. goto fail_unlock;
  7248. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  7249. if (ret)
  7250. goto fail_unlock;
  7251. old->dpms_mode = connector->dpms;
  7252. old->load_detect_temp = false;
  7253. /* Make sure the crtc and connector are running */
  7254. if (connector->dpms != DRM_MODE_DPMS_ON)
  7255. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  7256. return true;
  7257. }
  7258. /* Find an unused one (if possible) */
  7259. for_each_crtc(dev, possible_crtc) {
  7260. i++;
  7261. if (!(encoder->possible_crtcs & (1 << i)))
  7262. continue;
  7263. if (possible_crtc->enabled)
  7264. continue;
  7265. /* This can occur when applying the pipe A quirk on resume. */
  7266. if (to_intel_crtc(possible_crtc)->new_enabled)
  7267. continue;
  7268. crtc = possible_crtc;
  7269. break;
  7270. }
  7271. /*
  7272. * If we didn't find an unused CRTC, don't use any.
  7273. */
  7274. if (!crtc) {
  7275. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  7276. goto fail_unlock;
  7277. }
  7278. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7279. if (ret)
  7280. goto fail_unlock;
  7281. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  7282. if (ret)
  7283. goto fail_unlock;
  7284. intel_encoder->new_crtc = to_intel_crtc(crtc);
  7285. to_intel_connector(connector)->new_encoder = intel_encoder;
  7286. intel_crtc = to_intel_crtc(crtc);
  7287. intel_crtc->new_enabled = true;
  7288. intel_crtc->new_config = intel_crtc->config;
  7289. old->dpms_mode = connector->dpms;
  7290. old->load_detect_temp = true;
  7291. old->release_fb = NULL;
  7292. if (!mode)
  7293. mode = &load_detect_mode;
  7294. /* We need a framebuffer large enough to accommodate all accesses
  7295. * that the plane may generate whilst we perform load detection.
  7296. * We can not rely on the fbcon either being present (we get called
  7297. * during its initialisation to detect all boot displays, or it may
  7298. * not even exist) or that it is large enough to satisfy the
  7299. * requested mode.
  7300. */
  7301. fb = mode_fits_in_fbdev(dev, mode);
  7302. if (fb == NULL) {
  7303. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  7304. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  7305. old->release_fb = fb;
  7306. } else
  7307. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  7308. if (IS_ERR(fb)) {
  7309. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  7310. goto fail;
  7311. }
  7312. if (intel_set_mode(crtc, mode, 0, 0, fb)) {
  7313. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  7314. if (old->release_fb)
  7315. old->release_fb->funcs->destroy(old->release_fb);
  7316. goto fail;
  7317. }
  7318. /* let the connector get through one full cycle before testing */
  7319. intel_wait_for_vblank(dev, intel_crtc->pipe);
  7320. return true;
  7321. fail:
  7322. intel_crtc->new_enabled = crtc->enabled;
  7323. if (intel_crtc->new_enabled)
  7324. intel_crtc->new_config = intel_crtc->config;
  7325. else
  7326. intel_crtc->new_config = NULL;
  7327. fail_unlock:
  7328. if (ret == -EDEADLK) {
  7329. drm_modeset_backoff(ctx);
  7330. goto retry;
  7331. }
  7332. return false;
  7333. }
  7334. void intel_release_load_detect_pipe(struct drm_connector *connector,
  7335. struct intel_load_detect_pipe *old)
  7336. {
  7337. struct intel_encoder *intel_encoder =
  7338. intel_attached_encoder(connector);
  7339. struct drm_encoder *encoder = &intel_encoder->base;
  7340. struct drm_crtc *crtc = encoder->crtc;
  7341. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7342. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7343. connector->base.id, connector->name,
  7344. encoder->base.id, encoder->name);
  7345. if (old->load_detect_temp) {
  7346. to_intel_connector(connector)->new_encoder = NULL;
  7347. intel_encoder->new_crtc = NULL;
  7348. intel_crtc->new_enabled = false;
  7349. intel_crtc->new_config = NULL;
  7350. intel_set_mode(crtc, NULL, 0, 0, NULL);
  7351. if (old->release_fb) {
  7352. drm_framebuffer_unregister_private(old->release_fb);
  7353. drm_framebuffer_unreference(old->release_fb);
  7354. }
  7355. return;
  7356. }
  7357. /* Switch crtc and encoder back off if necessary */
  7358. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  7359. connector->funcs->dpms(connector, old->dpms_mode);
  7360. }
  7361. static int i9xx_pll_refclk(struct drm_device *dev,
  7362. const struct intel_crtc_state *pipe_config)
  7363. {
  7364. struct drm_i915_private *dev_priv = dev->dev_private;
  7365. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7366. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  7367. return dev_priv->vbt.lvds_ssc_freq;
  7368. else if (HAS_PCH_SPLIT(dev))
  7369. return 120000;
  7370. else if (!IS_GEN2(dev))
  7371. return 96000;
  7372. else
  7373. return 48000;
  7374. }
  7375. /* Returns the clock of the currently programmed mode of the given pipe. */
  7376. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  7377. struct intel_crtc_state *pipe_config)
  7378. {
  7379. struct drm_device *dev = crtc->base.dev;
  7380. struct drm_i915_private *dev_priv = dev->dev_private;
  7381. int pipe = pipe_config->cpu_transcoder;
  7382. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7383. u32 fp;
  7384. intel_clock_t clock;
  7385. int refclk = i9xx_pll_refclk(dev, pipe_config);
  7386. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  7387. fp = pipe_config->dpll_hw_state.fp0;
  7388. else
  7389. fp = pipe_config->dpll_hw_state.fp1;
  7390. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  7391. if (IS_PINEVIEW(dev)) {
  7392. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  7393. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7394. } else {
  7395. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  7396. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7397. }
  7398. if (!IS_GEN2(dev)) {
  7399. if (IS_PINEVIEW(dev))
  7400. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  7401. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  7402. else
  7403. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  7404. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7405. switch (dpll & DPLL_MODE_MASK) {
  7406. case DPLLB_MODE_DAC_SERIAL:
  7407. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  7408. 5 : 10;
  7409. break;
  7410. case DPLLB_MODE_LVDS:
  7411. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  7412. 7 : 14;
  7413. break;
  7414. default:
  7415. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  7416. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  7417. return;
  7418. }
  7419. if (IS_PINEVIEW(dev))
  7420. pineview_clock(refclk, &clock);
  7421. else
  7422. i9xx_clock(refclk, &clock);
  7423. } else {
  7424. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  7425. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  7426. if (is_lvds) {
  7427. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  7428. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7429. if (lvds & LVDS_CLKB_POWER_UP)
  7430. clock.p2 = 7;
  7431. else
  7432. clock.p2 = 14;
  7433. } else {
  7434. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  7435. clock.p1 = 2;
  7436. else {
  7437. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  7438. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  7439. }
  7440. if (dpll & PLL_P2_DIVIDE_BY_4)
  7441. clock.p2 = 4;
  7442. else
  7443. clock.p2 = 2;
  7444. }
  7445. i9xx_clock(refclk, &clock);
  7446. }
  7447. /*
  7448. * This value includes pixel_multiplier. We will use
  7449. * port_clock to compute adjusted_mode.crtc_clock in the
  7450. * encoder's get_config() function.
  7451. */
  7452. pipe_config->port_clock = clock.dot;
  7453. }
  7454. int intel_dotclock_calculate(int link_freq,
  7455. const struct intel_link_m_n *m_n)
  7456. {
  7457. /*
  7458. * The calculation for the data clock is:
  7459. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  7460. * But we want to avoid losing precison if possible, so:
  7461. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  7462. *
  7463. * and the link clock is simpler:
  7464. * link_clock = (m * link_clock) / n
  7465. */
  7466. if (!m_n->link_n)
  7467. return 0;
  7468. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  7469. }
  7470. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  7471. struct intel_crtc_state *pipe_config)
  7472. {
  7473. struct drm_device *dev = crtc->base.dev;
  7474. /* read out port_clock from the DPLL */
  7475. i9xx_crtc_clock_get(crtc, pipe_config);
  7476. /*
  7477. * This value does not include pixel_multiplier.
  7478. * We will check that port_clock and adjusted_mode.crtc_clock
  7479. * agree once we know their relationship in the encoder's
  7480. * get_config() function.
  7481. */
  7482. pipe_config->base.adjusted_mode.crtc_clock =
  7483. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  7484. &pipe_config->fdi_m_n);
  7485. }
  7486. /** Returns the currently programmed mode of the given pipe. */
  7487. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  7488. struct drm_crtc *crtc)
  7489. {
  7490. struct drm_i915_private *dev_priv = dev->dev_private;
  7491. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7492. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7493. struct drm_display_mode *mode;
  7494. struct intel_crtc_state pipe_config;
  7495. int htot = I915_READ(HTOTAL(cpu_transcoder));
  7496. int hsync = I915_READ(HSYNC(cpu_transcoder));
  7497. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  7498. int vsync = I915_READ(VSYNC(cpu_transcoder));
  7499. enum pipe pipe = intel_crtc->pipe;
  7500. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  7501. if (!mode)
  7502. return NULL;
  7503. /*
  7504. * Construct a pipe_config sufficient for getting the clock info
  7505. * back out of crtc_clock_get.
  7506. *
  7507. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  7508. * to use a real value here instead.
  7509. */
  7510. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  7511. pipe_config.pixel_multiplier = 1;
  7512. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  7513. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  7514. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  7515. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  7516. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  7517. mode->hdisplay = (htot & 0xffff) + 1;
  7518. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  7519. mode->hsync_start = (hsync & 0xffff) + 1;
  7520. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  7521. mode->vdisplay = (vtot & 0xffff) + 1;
  7522. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  7523. mode->vsync_start = (vsync & 0xffff) + 1;
  7524. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  7525. drm_mode_set_name(mode);
  7526. return mode;
  7527. }
  7528. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  7529. {
  7530. struct drm_device *dev = crtc->dev;
  7531. struct drm_i915_private *dev_priv = dev->dev_private;
  7532. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7533. if (!HAS_GMCH_DISPLAY(dev))
  7534. return;
  7535. if (!dev_priv->lvds_downclock_avail)
  7536. return;
  7537. /*
  7538. * Since this is called by a timer, we should never get here in
  7539. * the manual case.
  7540. */
  7541. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  7542. int pipe = intel_crtc->pipe;
  7543. int dpll_reg = DPLL(pipe);
  7544. int dpll;
  7545. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  7546. assert_panel_unlocked(dev_priv, pipe);
  7547. dpll = I915_READ(dpll_reg);
  7548. dpll |= DISPLAY_RATE_SELECT_FPA1;
  7549. I915_WRITE(dpll_reg, dpll);
  7550. intel_wait_for_vblank(dev, pipe);
  7551. dpll = I915_READ(dpll_reg);
  7552. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  7553. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  7554. }
  7555. }
  7556. void intel_mark_busy(struct drm_device *dev)
  7557. {
  7558. struct drm_i915_private *dev_priv = dev->dev_private;
  7559. if (dev_priv->mm.busy)
  7560. return;
  7561. intel_runtime_pm_get(dev_priv);
  7562. i915_update_gfx_val(dev_priv);
  7563. dev_priv->mm.busy = true;
  7564. }
  7565. void intel_mark_idle(struct drm_device *dev)
  7566. {
  7567. struct drm_i915_private *dev_priv = dev->dev_private;
  7568. struct drm_crtc *crtc;
  7569. if (!dev_priv->mm.busy)
  7570. return;
  7571. dev_priv->mm.busy = false;
  7572. if (!i915.powersave)
  7573. goto out;
  7574. for_each_crtc(dev, crtc) {
  7575. if (!crtc->primary->fb)
  7576. continue;
  7577. intel_decrease_pllclock(crtc);
  7578. }
  7579. if (INTEL_INFO(dev)->gen >= 6)
  7580. gen6_rps_idle(dev->dev_private);
  7581. out:
  7582. intel_runtime_pm_put(dev_priv);
  7583. }
  7584. static void intel_crtc_set_state(struct intel_crtc *crtc,
  7585. struct intel_crtc_state *crtc_state)
  7586. {
  7587. kfree(crtc->config);
  7588. crtc->config = crtc_state;
  7589. crtc->base.state = &crtc_state->base;
  7590. }
  7591. static void intel_crtc_destroy(struct drm_crtc *crtc)
  7592. {
  7593. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7594. struct drm_device *dev = crtc->dev;
  7595. struct intel_unpin_work *work;
  7596. spin_lock_irq(&dev->event_lock);
  7597. work = intel_crtc->unpin_work;
  7598. intel_crtc->unpin_work = NULL;
  7599. spin_unlock_irq(&dev->event_lock);
  7600. if (work) {
  7601. cancel_work_sync(&work->work);
  7602. kfree(work);
  7603. }
  7604. intel_crtc_set_state(intel_crtc, NULL);
  7605. drm_crtc_cleanup(crtc);
  7606. kfree(intel_crtc);
  7607. }
  7608. static void intel_unpin_work_fn(struct work_struct *__work)
  7609. {
  7610. struct intel_unpin_work *work =
  7611. container_of(__work, struct intel_unpin_work, work);
  7612. struct drm_device *dev = work->crtc->dev;
  7613. enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
  7614. mutex_lock(&dev->struct_mutex);
  7615. intel_unpin_fb_obj(intel_fb_obj(work->old_fb));
  7616. drm_gem_object_unreference(&work->pending_flip_obj->base);
  7617. drm_framebuffer_unreference(work->old_fb);
  7618. intel_fbc_update(dev);
  7619. if (work->flip_queued_req)
  7620. i915_gem_request_assign(&work->flip_queued_req, NULL);
  7621. mutex_unlock(&dev->struct_mutex);
  7622. intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  7623. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  7624. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  7625. kfree(work);
  7626. }
  7627. static void do_intel_finish_page_flip(struct drm_device *dev,
  7628. struct drm_crtc *crtc)
  7629. {
  7630. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7631. struct intel_unpin_work *work;
  7632. unsigned long flags;
  7633. /* Ignore early vblank irqs */
  7634. if (intel_crtc == NULL)
  7635. return;
  7636. /*
  7637. * This is called both by irq handlers and the reset code (to complete
  7638. * lost pageflips) so needs the full irqsave spinlocks.
  7639. */
  7640. spin_lock_irqsave(&dev->event_lock, flags);
  7641. work = intel_crtc->unpin_work;
  7642. /* Ensure we don't miss a work->pending update ... */
  7643. smp_rmb();
  7644. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  7645. spin_unlock_irqrestore(&dev->event_lock, flags);
  7646. return;
  7647. }
  7648. page_flip_completed(intel_crtc);
  7649. spin_unlock_irqrestore(&dev->event_lock, flags);
  7650. }
  7651. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  7652. {
  7653. struct drm_i915_private *dev_priv = dev->dev_private;
  7654. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  7655. do_intel_finish_page_flip(dev, crtc);
  7656. }
  7657. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  7658. {
  7659. struct drm_i915_private *dev_priv = dev->dev_private;
  7660. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  7661. do_intel_finish_page_flip(dev, crtc);
  7662. }
  7663. /* Is 'a' after or equal to 'b'? */
  7664. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  7665. {
  7666. return !((a - b) & 0x80000000);
  7667. }
  7668. static bool page_flip_finished(struct intel_crtc *crtc)
  7669. {
  7670. struct drm_device *dev = crtc->base.dev;
  7671. struct drm_i915_private *dev_priv = dev->dev_private;
  7672. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  7673. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  7674. return true;
  7675. /*
  7676. * The relevant registers doen't exist on pre-ctg.
  7677. * As the flip done interrupt doesn't trigger for mmio
  7678. * flips on gmch platforms, a flip count check isn't
  7679. * really needed there. But since ctg has the registers,
  7680. * include it in the check anyway.
  7681. */
  7682. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  7683. return true;
  7684. /*
  7685. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  7686. * used the same base address. In that case the mmio flip might
  7687. * have completed, but the CS hasn't even executed the flip yet.
  7688. *
  7689. * A flip count check isn't enough as the CS might have updated
  7690. * the base address just after start of vblank, but before we
  7691. * managed to process the interrupt. This means we'd complete the
  7692. * CS flip too soon.
  7693. *
  7694. * Combining both checks should get us a good enough result. It may
  7695. * still happen that the CS flip has been executed, but has not
  7696. * yet actually completed. But in case the base address is the same
  7697. * anyway, we don't really care.
  7698. */
  7699. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  7700. crtc->unpin_work->gtt_offset &&
  7701. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  7702. crtc->unpin_work->flip_count);
  7703. }
  7704. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  7705. {
  7706. struct drm_i915_private *dev_priv = dev->dev_private;
  7707. struct intel_crtc *intel_crtc =
  7708. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  7709. unsigned long flags;
  7710. /*
  7711. * This is called both by irq handlers and the reset code (to complete
  7712. * lost pageflips) so needs the full irqsave spinlocks.
  7713. *
  7714. * NB: An MMIO update of the plane base pointer will also
  7715. * generate a page-flip completion irq, i.e. every modeset
  7716. * is also accompanied by a spurious intel_prepare_page_flip().
  7717. */
  7718. spin_lock_irqsave(&dev->event_lock, flags);
  7719. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  7720. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  7721. spin_unlock_irqrestore(&dev->event_lock, flags);
  7722. }
  7723. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  7724. {
  7725. /* Ensure that the work item is consistent when activating it ... */
  7726. smp_wmb();
  7727. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  7728. /* and that it is marked active as soon as the irq could fire. */
  7729. smp_wmb();
  7730. }
  7731. static int intel_gen2_queue_flip(struct drm_device *dev,
  7732. struct drm_crtc *crtc,
  7733. struct drm_framebuffer *fb,
  7734. struct drm_i915_gem_object *obj,
  7735. struct intel_engine_cs *ring,
  7736. uint32_t flags)
  7737. {
  7738. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7739. u32 flip_mask;
  7740. int ret;
  7741. ret = intel_ring_begin(ring, 6);
  7742. if (ret)
  7743. return ret;
  7744. /* Can't queue multiple flips, so wait for the previous
  7745. * one to finish before executing the next.
  7746. */
  7747. if (intel_crtc->plane)
  7748. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7749. else
  7750. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7751. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7752. intel_ring_emit(ring, MI_NOOP);
  7753. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7754. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7755. intel_ring_emit(ring, fb->pitches[0]);
  7756. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7757. intel_ring_emit(ring, 0); /* aux display base address, unused */
  7758. intel_mark_page_flip_active(intel_crtc);
  7759. __intel_ring_advance(ring);
  7760. return 0;
  7761. }
  7762. static int intel_gen3_queue_flip(struct drm_device *dev,
  7763. struct drm_crtc *crtc,
  7764. struct drm_framebuffer *fb,
  7765. struct drm_i915_gem_object *obj,
  7766. struct intel_engine_cs *ring,
  7767. uint32_t flags)
  7768. {
  7769. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7770. u32 flip_mask;
  7771. int ret;
  7772. ret = intel_ring_begin(ring, 6);
  7773. if (ret)
  7774. return ret;
  7775. if (intel_crtc->plane)
  7776. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7777. else
  7778. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7779. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7780. intel_ring_emit(ring, MI_NOOP);
  7781. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  7782. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7783. intel_ring_emit(ring, fb->pitches[0]);
  7784. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7785. intel_ring_emit(ring, MI_NOOP);
  7786. intel_mark_page_flip_active(intel_crtc);
  7787. __intel_ring_advance(ring);
  7788. return 0;
  7789. }
  7790. static int intel_gen4_queue_flip(struct drm_device *dev,
  7791. struct drm_crtc *crtc,
  7792. struct drm_framebuffer *fb,
  7793. struct drm_i915_gem_object *obj,
  7794. struct intel_engine_cs *ring,
  7795. uint32_t flags)
  7796. {
  7797. struct drm_i915_private *dev_priv = dev->dev_private;
  7798. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7799. uint32_t pf, pipesrc;
  7800. int ret;
  7801. ret = intel_ring_begin(ring, 4);
  7802. if (ret)
  7803. return ret;
  7804. /* i965+ uses the linear or tiled offsets from the
  7805. * Display Registers (which do not change across a page-flip)
  7806. * so we need only reprogram the base address.
  7807. */
  7808. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7809. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7810. intel_ring_emit(ring, fb->pitches[0]);
  7811. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  7812. obj->tiling_mode);
  7813. /* XXX Enabling the panel-fitter across page-flip is so far
  7814. * untested on non-native modes, so ignore it for now.
  7815. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  7816. */
  7817. pf = 0;
  7818. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7819. intel_ring_emit(ring, pf | pipesrc);
  7820. intel_mark_page_flip_active(intel_crtc);
  7821. __intel_ring_advance(ring);
  7822. return 0;
  7823. }
  7824. static int intel_gen6_queue_flip(struct drm_device *dev,
  7825. struct drm_crtc *crtc,
  7826. struct drm_framebuffer *fb,
  7827. struct drm_i915_gem_object *obj,
  7828. struct intel_engine_cs *ring,
  7829. uint32_t flags)
  7830. {
  7831. struct drm_i915_private *dev_priv = dev->dev_private;
  7832. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7833. uint32_t pf, pipesrc;
  7834. int ret;
  7835. ret = intel_ring_begin(ring, 4);
  7836. if (ret)
  7837. return ret;
  7838. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7839. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7840. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  7841. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7842. /* Contrary to the suggestions in the documentation,
  7843. * "Enable Panel Fitter" does not seem to be required when page
  7844. * flipping with a non-native mode, and worse causes a normal
  7845. * modeset to fail.
  7846. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  7847. */
  7848. pf = 0;
  7849. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7850. intel_ring_emit(ring, pf | pipesrc);
  7851. intel_mark_page_flip_active(intel_crtc);
  7852. __intel_ring_advance(ring);
  7853. return 0;
  7854. }
  7855. static int intel_gen7_queue_flip(struct drm_device *dev,
  7856. struct drm_crtc *crtc,
  7857. struct drm_framebuffer *fb,
  7858. struct drm_i915_gem_object *obj,
  7859. struct intel_engine_cs *ring,
  7860. uint32_t flags)
  7861. {
  7862. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7863. uint32_t plane_bit = 0;
  7864. int len, ret;
  7865. switch (intel_crtc->plane) {
  7866. case PLANE_A:
  7867. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  7868. break;
  7869. case PLANE_B:
  7870. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  7871. break;
  7872. case PLANE_C:
  7873. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  7874. break;
  7875. default:
  7876. WARN_ONCE(1, "unknown plane in flip command\n");
  7877. return -ENODEV;
  7878. }
  7879. len = 4;
  7880. if (ring->id == RCS) {
  7881. len += 6;
  7882. /*
  7883. * On Gen 8, SRM is now taking an extra dword to accommodate
  7884. * 48bits addresses, and we need a NOOP for the batch size to
  7885. * stay even.
  7886. */
  7887. if (IS_GEN8(dev))
  7888. len += 2;
  7889. }
  7890. /*
  7891. * BSpec MI_DISPLAY_FLIP for IVB:
  7892. * "The full packet must be contained within the same cache line."
  7893. *
  7894. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  7895. * cacheline, if we ever start emitting more commands before
  7896. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  7897. * then do the cacheline alignment, and finally emit the
  7898. * MI_DISPLAY_FLIP.
  7899. */
  7900. ret = intel_ring_cacheline_align(ring);
  7901. if (ret)
  7902. return ret;
  7903. ret = intel_ring_begin(ring, len);
  7904. if (ret)
  7905. return ret;
  7906. /* Unmask the flip-done completion message. Note that the bspec says that
  7907. * we should do this for both the BCS and RCS, and that we must not unmask
  7908. * more than one flip event at any time (or ensure that one flip message
  7909. * can be sent by waiting for flip-done prior to queueing new flips).
  7910. * Experimentation says that BCS works despite DERRMR masking all
  7911. * flip-done completion events and that unmasking all planes at once
  7912. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  7913. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  7914. */
  7915. if (ring->id == RCS) {
  7916. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  7917. intel_ring_emit(ring, DERRMR);
  7918. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  7919. DERRMR_PIPEB_PRI_FLIP_DONE |
  7920. DERRMR_PIPEC_PRI_FLIP_DONE));
  7921. if (IS_GEN8(dev))
  7922. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  7923. MI_SRM_LRM_GLOBAL_GTT);
  7924. else
  7925. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  7926. MI_SRM_LRM_GLOBAL_GTT);
  7927. intel_ring_emit(ring, DERRMR);
  7928. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  7929. if (IS_GEN8(dev)) {
  7930. intel_ring_emit(ring, 0);
  7931. intel_ring_emit(ring, MI_NOOP);
  7932. }
  7933. }
  7934. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  7935. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  7936. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7937. intel_ring_emit(ring, (MI_NOOP));
  7938. intel_mark_page_flip_active(intel_crtc);
  7939. __intel_ring_advance(ring);
  7940. return 0;
  7941. }
  7942. static bool use_mmio_flip(struct intel_engine_cs *ring,
  7943. struct drm_i915_gem_object *obj)
  7944. {
  7945. /*
  7946. * This is not being used for older platforms, because
  7947. * non-availability of flip done interrupt forces us to use
  7948. * CS flips. Older platforms derive flip done using some clever
  7949. * tricks involving the flip_pending status bits and vblank irqs.
  7950. * So using MMIO flips there would disrupt this mechanism.
  7951. */
  7952. if (ring == NULL)
  7953. return true;
  7954. if (INTEL_INFO(ring->dev)->gen < 5)
  7955. return false;
  7956. if (i915.use_mmio_flip < 0)
  7957. return false;
  7958. else if (i915.use_mmio_flip > 0)
  7959. return true;
  7960. else if (i915.enable_execlists)
  7961. return true;
  7962. else
  7963. return ring != i915_gem_request_get_ring(obj->last_read_req);
  7964. }
  7965. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
  7966. {
  7967. struct drm_device *dev = intel_crtc->base.dev;
  7968. struct drm_i915_private *dev_priv = dev->dev_private;
  7969. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  7970. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  7971. struct drm_i915_gem_object *obj = intel_fb->obj;
  7972. const enum pipe pipe = intel_crtc->pipe;
  7973. u32 ctl, stride;
  7974. ctl = I915_READ(PLANE_CTL(pipe, 0));
  7975. ctl &= ~PLANE_CTL_TILED_MASK;
  7976. if (obj->tiling_mode == I915_TILING_X)
  7977. ctl |= PLANE_CTL_TILED_X;
  7978. /*
  7979. * The stride is either expressed as a multiple of 64 bytes chunks for
  7980. * linear buffers or in number of tiles for tiled buffers.
  7981. */
  7982. stride = fb->pitches[0] >> 6;
  7983. if (obj->tiling_mode == I915_TILING_X)
  7984. stride = fb->pitches[0] >> 9; /* X tiles are 512 bytes wide */
  7985. /*
  7986. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  7987. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  7988. */
  7989. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  7990. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  7991. I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
  7992. POSTING_READ(PLANE_SURF(pipe, 0));
  7993. }
  7994. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
  7995. {
  7996. struct drm_device *dev = intel_crtc->base.dev;
  7997. struct drm_i915_private *dev_priv = dev->dev_private;
  7998. struct intel_framebuffer *intel_fb =
  7999. to_intel_framebuffer(intel_crtc->base.primary->fb);
  8000. struct drm_i915_gem_object *obj = intel_fb->obj;
  8001. u32 dspcntr;
  8002. u32 reg;
  8003. reg = DSPCNTR(intel_crtc->plane);
  8004. dspcntr = I915_READ(reg);
  8005. if (obj->tiling_mode != I915_TILING_NONE)
  8006. dspcntr |= DISPPLANE_TILED;
  8007. else
  8008. dspcntr &= ~DISPPLANE_TILED;
  8009. I915_WRITE(reg, dspcntr);
  8010. I915_WRITE(DSPSURF(intel_crtc->plane),
  8011. intel_crtc->unpin_work->gtt_offset);
  8012. POSTING_READ(DSPSURF(intel_crtc->plane));
  8013. }
  8014. /*
  8015. * XXX: This is the temporary way to update the plane registers until we get
  8016. * around to using the usual plane update functions for MMIO flips
  8017. */
  8018. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  8019. {
  8020. struct drm_device *dev = intel_crtc->base.dev;
  8021. bool atomic_update;
  8022. u32 start_vbl_count;
  8023. intel_mark_page_flip_active(intel_crtc);
  8024. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  8025. if (INTEL_INFO(dev)->gen >= 9)
  8026. skl_do_mmio_flip(intel_crtc);
  8027. else
  8028. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  8029. ilk_do_mmio_flip(intel_crtc);
  8030. if (atomic_update)
  8031. intel_pipe_update_end(intel_crtc, start_vbl_count);
  8032. }
  8033. static void intel_mmio_flip_work_func(struct work_struct *work)
  8034. {
  8035. struct intel_crtc *crtc =
  8036. container_of(work, struct intel_crtc, mmio_flip.work);
  8037. struct intel_mmio_flip *mmio_flip;
  8038. mmio_flip = &crtc->mmio_flip;
  8039. if (mmio_flip->req)
  8040. WARN_ON(__i915_wait_request(mmio_flip->req,
  8041. crtc->reset_counter,
  8042. false, NULL, NULL) != 0);
  8043. intel_do_mmio_flip(crtc);
  8044. if (mmio_flip->req) {
  8045. mutex_lock(&crtc->base.dev->struct_mutex);
  8046. i915_gem_request_assign(&mmio_flip->req, NULL);
  8047. mutex_unlock(&crtc->base.dev->struct_mutex);
  8048. }
  8049. }
  8050. static int intel_queue_mmio_flip(struct drm_device *dev,
  8051. struct drm_crtc *crtc,
  8052. struct drm_framebuffer *fb,
  8053. struct drm_i915_gem_object *obj,
  8054. struct intel_engine_cs *ring,
  8055. uint32_t flags)
  8056. {
  8057. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8058. i915_gem_request_assign(&intel_crtc->mmio_flip.req,
  8059. obj->last_write_req);
  8060. schedule_work(&intel_crtc->mmio_flip.work);
  8061. return 0;
  8062. }
  8063. static int intel_gen9_queue_flip(struct drm_device *dev,
  8064. struct drm_crtc *crtc,
  8065. struct drm_framebuffer *fb,
  8066. struct drm_i915_gem_object *obj,
  8067. struct intel_engine_cs *ring,
  8068. uint32_t flags)
  8069. {
  8070. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8071. uint32_t plane = 0, stride;
  8072. int ret;
  8073. switch(intel_crtc->pipe) {
  8074. case PIPE_A:
  8075. plane = MI_DISPLAY_FLIP_SKL_PLANE_1_A;
  8076. break;
  8077. case PIPE_B:
  8078. plane = MI_DISPLAY_FLIP_SKL_PLANE_1_B;
  8079. break;
  8080. case PIPE_C:
  8081. plane = MI_DISPLAY_FLIP_SKL_PLANE_1_C;
  8082. break;
  8083. default:
  8084. WARN_ONCE(1, "unknown plane in flip command\n");
  8085. return -ENODEV;
  8086. }
  8087. switch (obj->tiling_mode) {
  8088. case I915_TILING_NONE:
  8089. stride = fb->pitches[0] >> 6;
  8090. break;
  8091. case I915_TILING_X:
  8092. stride = fb->pitches[0] >> 9;
  8093. break;
  8094. default:
  8095. WARN_ONCE(1, "unknown tiling in flip command\n");
  8096. return -ENODEV;
  8097. }
  8098. ret = intel_ring_begin(ring, 10);
  8099. if (ret)
  8100. return ret;
  8101. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  8102. intel_ring_emit(ring, DERRMR);
  8103. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  8104. DERRMR_PIPEB_PRI_FLIP_DONE |
  8105. DERRMR_PIPEC_PRI_FLIP_DONE));
  8106. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  8107. MI_SRM_LRM_GLOBAL_GTT);
  8108. intel_ring_emit(ring, DERRMR);
  8109. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  8110. intel_ring_emit(ring, 0);
  8111. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane);
  8112. intel_ring_emit(ring, stride << 6 | obj->tiling_mode);
  8113. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8114. intel_mark_page_flip_active(intel_crtc);
  8115. __intel_ring_advance(ring);
  8116. return 0;
  8117. }
  8118. static int intel_default_queue_flip(struct drm_device *dev,
  8119. struct drm_crtc *crtc,
  8120. struct drm_framebuffer *fb,
  8121. struct drm_i915_gem_object *obj,
  8122. struct intel_engine_cs *ring,
  8123. uint32_t flags)
  8124. {
  8125. return -ENODEV;
  8126. }
  8127. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  8128. struct drm_crtc *crtc)
  8129. {
  8130. struct drm_i915_private *dev_priv = dev->dev_private;
  8131. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8132. struct intel_unpin_work *work = intel_crtc->unpin_work;
  8133. u32 addr;
  8134. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  8135. return true;
  8136. if (!work->enable_stall_check)
  8137. return false;
  8138. if (work->flip_ready_vblank == 0) {
  8139. if (work->flip_queued_req &&
  8140. !i915_gem_request_completed(work->flip_queued_req, true))
  8141. return false;
  8142. work->flip_ready_vblank = drm_vblank_count(dev, intel_crtc->pipe);
  8143. }
  8144. if (drm_vblank_count(dev, intel_crtc->pipe) - work->flip_ready_vblank < 3)
  8145. return false;
  8146. /* Potential stall - if we see that the flip has happened,
  8147. * assume a missed interrupt. */
  8148. if (INTEL_INFO(dev)->gen >= 4)
  8149. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  8150. else
  8151. addr = I915_READ(DSPADDR(intel_crtc->plane));
  8152. /* There is a potential issue here with a false positive after a flip
  8153. * to the same address. We could address this by checking for a
  8154. * non-incrementing frame counter.
  8155. */
  8156. return addr == work->gtt_offset;
  8157. }
  8158. void intel_check_page_flip(struct drm_device *dev, int pipe)
  8159. {
  8160. struct drm_i915_private *dev_priv = dev->dev_private;
  8161. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  8162. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8163. WARN_ON(!in_irq());
  8164. if (crtc == NULL)
  8165. return;
  8166. spin_lock(&dev->event_lock);
  8167. if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
  8168. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  8169. intel_crtc->unpin_work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  8170. page_flip_completed(intel_crtc);
  8171. }
  8172. spin_unlock(&dev->event_lock);
  8173. }
  8174. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  8175. struct drm_framebuffer *fb,
  8176. struct drm_pending_vblank_event *event,
  8177. uint32_t page_flip_flags)
  8178. {
  8179. struct drm_device *dev = crtc->dev;
  8180. struct drm_i915_private *dev_priv = dev->dev_private;
  8181. struct drm_framebuffer *old_fb = crtc->primary->fb;
  8182. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  8183. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8184. struct drm_plane *primary = crtc->primary;
  8185. enum pipe pipe = intel_crtc->pipe;
  8186. struct intel_unpin_work *work;
  8187. struct intel_engine_cs *ring;
  8188. int ret;
  8189. /*
  8190. * drm_mode_page_flip_ioctl() should already catch this, but double
  8191. * check to be safe. In the future we may enable pageflipping from
  8192. * a disabled primary plane.
  8193. */
  8194. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  8195. return -EBUSY;
  8196. /* Can't change pixel format via MI display flips. */
  8197. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  8198. return -EINVAL;
  8199. /*
  8200. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  8201. * Note that pitch changes could also affect these register.
  8202. */
  8203. if (INTEL_INFO(dev)->gen > 3 &&
  8204. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  8205. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  8206. return -EINVAL;
  8207. if (i915_terminally_wedged(&dev_priv->gpu_error))
  8208. goto out_hang;
  8209. work = kzalloc(sizeof(*work), GFP_KERNEL);
  8210. if (work == NULL)
  8211. return -ENOMEM;
  8212. work->event = event;
  8213. work->crtc = crtc;
  8214. work->old_fb = old_fb;
  8215. INIT_WORK(&work->work, intel_unpin_work_fn);
  8216. ret = drm_crtc_vblank_get(crtc);
  8217. if (ret)
  8218. goto free_work;
  8219. /* We borrow the event spin lock for protecting unpin_work */
  8220. spin_lock_irq(&dev->event_lock);
  8221. if (intel_crtc->unpin_work) {
  8222. /* Before declaring the flip queue wedged, check if
  8223. * the hardware completed the operation behind our backs.
  8224. */
  8225. if (__intel_pageflip_stall_check(dev, crtc)) {
  8226. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  8227. page_flip_completed(intel_crtc);
  8228. } else {
  8229. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  8230. spin_unlock_irq(&dev->event_lock);
  8231. drm_crtc_vblank_put(crtc);
  8232. kfree(work);
  8233. return -EBUSY;
  8234. }
  8235. }
  8236. intel_crtc->unpin_work = work;
  8237. spin_unlock_irq(&dev->event_lock);
  8238. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  8239. flush_workqueue(dev_priv->wq);
  8240. ret = i915_mutex_lock_interruptible(dev);
  8241. if (ret)
  8242. goto cleanup;
  8243. /* Reference the objects for the scheduled work. */
  8244. drm_framebuffer_reference(work->old_fb);
  8245. drm_gem_object_reference(&obj->base);
  8246. crtc->primary->fb = fb;
  8247. update_state_fb(crtc->primary);
  8248. work->pending_flip_obj = obj;
  8249. atomic_inc(&intel_crtc->unpin_work_count);
  8250. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  8251. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  8252. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  8253. if (IS_VALLEYVIEW(dev)) {
  8254. ring = &dev_priv->ring[BCS];
  8255. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  8256. /* vlv: DISPLAY_FLIP fails to change tiling */
  8257. ring = NULL;
  8258. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  8259. ring = &dev_priv->ring[BCS];
  8260. } else if (INTEL_INFO(dev)->gen >= 7) {
  8261. ring = i915_gem_request_get_ring(obj->last_read_req);
  8262. if (ring == NULL || ring->id != RCS)
  8263. ring = &dev_priv->ring[BCS];
  8264. } else {
  8265. ring = &dev_priv->ring[RCS];
  8266. }
  8267. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb, ring);
  8268. if (ret)
  8269. goto cleanup_pending;
  8270. work->gtt_offset =
  8271. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
  8272. if (use_mmio_flip(ring, obj)) {
  8273. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  8274. page_flip_flags);
  8275. if (ret)
  8276. goto cleanup_unpin;
  8277. i915_gem_request_assign(&work->flip_queued_req,
  8278. obj->last_write_req);
  8279. } else {
  8280. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
  8281. page_flip_flags);
  8282. if (ret)
  8283. goto cleanup_unpin;
  8284. i915_gem_request_assign(&work->flip_queued_req,
  8285. intel_ring_get_request(ring));
  8286. }
  8287. work->flip_queued_vblank = drm_vblank_count(dev, intel_crtc->pipe);
  8288. work->enable_stall_check = true;
  8289. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  8290. INTEL_FRONTBUFFER_PRIMARY(pipe));
  8291. intel_fbc_disable(dev);
  8292. intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  8293. mutex_unlock(&dev->struct_mutex);
  8294. trace_i915_flip_request(intel_crtc->plane, obj);
  8295. return 0;
  8296. cleanup_unpin:
  8297. intel_unpin_fb_obj(obj);
  8298. cleanup_pending:
  8299. atomic_dec(&intel_crtc->unpin_work_count);
  8300. crtc->primary->fb = old_fb;
  8301. update_state_fb(crtc->primary);
  8302. drm_framebuffer_unreference(work->old_fb);
  8303. drm_gem_object_unreference(&obj->base);
  8304. mutex_unlock(&dev->struct_mutex);
  8305. cleanup:
  8306. spin_lock_irq(&dev->event_lock);
  8307. intel_crtc->unpin_work = NULL;
  8308. spin_unlock_irq(&dev->event_lock);
  8309. drm_crtc_vblank_put(crtc);
  8310. free_work:
  8311. kfree(work);
  8312. if (ret == -EIO) {
  8313. out_hang:
  8314. ret = intel_plane_restore(primary);
  8315. if (ret == 0 && event) {
  8316. spin_lock_irq(&dev->event_lock);
  8317. drm_send_vblank_event(dev, pipe, event);
  8318. spin_unlock_irq(&dev->event_lock);
  8319. }
  8320. }
  8321. return ret;
  8322. }
  8323. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  8324. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  8325. .load_lut = intel_crtc_load_lut,
  8326. .atomic_begin = intel_begin_crtc_commit,
  8327. .atomic_flush = intel_finish_crtc_commit,
  8328. };
  8329. /**
  8330. * intel_modeset_update_staged_output_state
  8331. *
  8332. * Updates the staged output configuration state, e.g. after we've read out the
  8333. * current hw state.
  8334. */
  8335. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  8336. {
  8337. struct intel_crtc *crtc;
  8338. struct intel_encoder *encoder;
  8339. struct intel_connector *connector;
  8340. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8341. base.head) {
  8342. connector->new_encoder =
  8343. to_intel_encoder(connector->base.encoder);
  8344. }
  8345. for_each_intel_encoder(dev, encoder) {
  8346. encoder->new_crtc =
  8347. to_intel_crtc(encoder->base.crtc);
  8348. }
  8349. for_each_intel_crtc(dev, crtc) {
  8350. crtc->new_enabled = crtc->base.enabled;
  8351. if (crtc->new_enabled)
  8352. crtc->new_config = crtc->config;
  8353. else
  8354. crtc->new_config = NULL;
  8355. }
  8356. }
  8357. /**
  8358. * intel_modeset_commit_output_state
  8359. *
  8360. * This function copies the stage display pipe configuration to the real one.
  8361. */
  8362. static void intel_modeset_commit_output_state(struct drm_device *dev)
  8363. {
  8364. struct intel_crtc *crtc;
  8365. struct intel_encoder *encoder;
  8366. struct intel_connector *connector;
  8367. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8368. base.head) {
  8369. connector->base.encoder = &connector->new_encoder->base;
  8370. }
  8371. for_each_intel_encoder(dev, encoder) {
  8372. encoder->base.crtc = &encoder->new_crtc->base;
  8373. }
  8374. for_each_intel_crtc(dev, crtc) {
  8375. crtc->base.enabled = crtc->new_enabled;
  8376. }
  8377. }
  8378. static void
  8379. connected_sink_compute_bpp(struct intel_connector *connector,
  8380. struct intel_crtc_state *pipe_config)
  8381. {
  8382. int bpp = pipe_config->pipe_bpp;
  8383. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8384. connector->base.base.id,
  8385. connector->base.name);
  8386. /* Don't use an invalid EDID bpc value */
  8387. if (connector->base.display_info.bpc &&
  8388. connector->base.display_info.bpc * 3 < bpp) {
  8389. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8390. bpp, connector->base.display_info.bpc*3);
  8391. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  8392. }
  8393. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8394. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  8395. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8396. bpp);
  8397. pipe_config->pipe_bpp = 24;
  8398. }
  8399. }
  8400. static int
  8401. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8402. struct drm_framebuffer *fb,
  8403. struct intel_crtc_state *pipe_config)
  8404. {
  8405. struct drm_device *dev = crtc->base.dev;
  8406. struct intel_connector *connector;
  8407. int bpp;
  8408. switch (fb->pixel_format) {
  8409. case DRM_FORMAT_C8:
  8410. bpp = 8*3; /* since we go through a colormap */
  8411. break;
  8412. case DRM_FORMAT_XRGB1555:
  8413. case DRM_FORMAT_ARGB1555:
  8414. /* checked in intel_framebuffer_init already */
  8415. if (WARN_ON(INTEL_INFO(dev)->gen > 3))
  8416. return -EINVAL;
  8417. case DRM_FORMAT_RGB565:
  8418. bpp = 6*3; /* min is 18bpp */
  8419. break;
  8420. case DRM_FORMAT_XBGR8888:
  8421. case DRM_FORMAT_ABGR8888:
  8422. /* checked in intel_framebuffer_init already */
  8423. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8424. return -EINVAL;
  8425. case DRM_FORMAT_XRGB8888:
  8426. case DRM_FORMAT_ARGB8888:
  8427. bpp = 8*3;
  8428. break;
  8429. case DRM_FORMAT_XRGB2101010:
  8430. case DRM_FORMAT_ARGB2101010:
  8431. case DRM_FORMAT_XBGR2101010:
  8432. case DRM_FORMAT_ABGR2101010:
  8433. /* checked in intel_framebuffer_init already */
  8434. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8435. return -EINVAL;
  8436. bpp = 10*3;
  8437. break;
  8438. /* TODO: gen4+ supports 16 bpc floating point, too. */
  8439. default:
  8440. DRM_DEBUG_KMS("unsupported depth\n");
  8441. return -EINVAL;
  8442. }
  8443. pipe_config->pipe_bpp = bpp;
  8444. /* Clamp display bpp to EDID value */
  8445. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8446. base.head) {
  8447. if (!connector->new_encoder ||
  8448. connector->new_encoder->new_crtc != crtc)
  8449. continue;
  8450. connected_sink_compute_bpp(connector, pipe_config);
  8451. }
  8452. return bpp;
  8453. }
  8454. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  8455. {
  8456. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  8457. "type: 0x%x flags: 0x%x\n",
  8458. mode->crtc_clock,
  8459. mode->crtc_hdisplay, mode->crtc_hsync_start,
  8460. mode->crtc_hsync_end, mode->crtc_htotal,
  8461. mode->crtc_vdisplay, mode->crtc_vsync_start,
  8462. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  8463. }
  8464. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  8465. struct intel_crtc_state *pipe_config,
  8466. const char *context)
  8467. {
  8468. DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
  8469. context, pipe_name(crtc->pipe));
  8470. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  8471. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  8472. pipe_config->pipe_bpp, pipe_config->dither);
  8473. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8474. pipe_config->has_pch_encoder,
  8475. pipe_config->fdi_lanes,
  8476. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  8477. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  8478. pipe_config->fdi_m_n.tu);
  8479. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8480. pipe_config->has_dp_encoder,
  8481. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  8482. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  8483. pipe_config->dp_m_n.tu);
  8484. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  8485. pipe_config->has_dp_encoder,
  8486. pipe_config->dp_m2_n2.gmch_m,
  8487. pipe_config->dp_m2_n2.gmch_n,
  8488. pipe_config->dp_m2_n2.link_m,
  8489. pipe_config->dp_m2_n2.link_n,
  8490. pipe_config->dp_m2_n2.tu);
  8491. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  8492. pipe_config->has_audio,
  8493. pipe_config->has_infoframe);
  8494. DRM_DEBUG_KMS("requested mode:\n");
  8495. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  8496. DRM_DEBUG_KMS("adjusted mode:\n");
  8497. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  8498. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  8499. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  8500. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  8501. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  8502. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  8503. pipe_config->gmch_pfit.control,
  8504. pipe_config->gmch_pfit.pgm_ratios,
  8505. pipe_config->gmch_pfit.lvds_border_bits);
  8506. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  8507. pipe_config->pch_pfit.pos,
  8508. pipe_config->pch_pfit.size,
  8509. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  8510. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  8511. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  8512. }
  8513. static bool encoders_cloneable(const struct intel_encoder *a,
  8514. const struct intel_encoder *b)
  8515. {
  8516. /* masks could be asymmetric, so check both ways */
  8517. return a == b || (a->cloneable & (1 << b->type) &&
  8518. b->cloneable & (1 << a->type));
  8519. }
  8520. static bool check_single_encoder_cloning(struct intel_crtc *crtc,
  8521. struct intel_encoder *encoder)
  8522. {
  8523. struct drm_device *dev = crtc->base.dev;
  8524. struct intel_encoder *source_encoder;
  8525. for_each_intel_encoder(dev, source_encoder) {
  8526. if (source_encoder->new_crtc != crtc)
  8527. continue;
  8528. if (!encoders_cloneable(encoder, source_encoder))
  8529. return false;
  8530. }
  8531. return true;
  8532. }
  8533. static bool check_encoder_cloning(struct intel_crtc *crtc)
  8534. {
  8535. struct drm_device *dev = crtc->base.dev;
  8536. struct intel_encoder *encoder;
  8537. for_each_intel_encoder(dev, encoder) {
  8538. if (encoder->new_crtc != crtc)
  8539. continue;
  8540. if (!check_single_encoder_cloning(crtc, encoder))
  8541. return false;
  8542. }
  8543. return true;
  8544. }
  8545. static bool check_digital_port_conflicts(struct drm_device *dev)
  8546. {
  8547. struct intel_connector *connector;
  8548. unsigned int used_ports = 0;
  8549. /*
  8550. * Walk the connector list instead of the encoder
  8551. * list to detect the problem on ddi platforms
  8552. * where there's just one encoder per digital port.
  8553. */
  8554. list_for_each_entry(connector,
  8555. &dev->mode_config.connector_list, base.head) {
  8556. struct intel_encoder *encoder = connector->new_encoder;
  8557. if (!encoder)
  8558. continue;
  8559. WARN_ON(!encoder->new_crtc);
  8560. switch (encoder->type) {
  8561. unsigned int port_mask;
  8562. case INTEL_OUTPUT_UNKNOWN:
  8563. if (WARN_ON(!HAS_DDI(dev)))
  8564. break;
  8565. case INTEL_OUTPUT_DISPLAYPORT:
  8566. case INTEL_OUTPUT_HDMI:
  8567. case INTEL_OUTPUT_EDP:
  8568. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  8569. /* the same port mustn't appear more than once */
  8570. if (used_ports & port_mask)
  8571. return false;
  8572. used_ports |= port_mask;
  8573. default:
  8574. break;
  8575. }
  8576. }
  8577. return true;
  8578. }
  8579. static struct intel_crtc_state *
  8580. intel_modeset_pipe_config(struct drm_crtc *crtc,
  8581. struct drm_framebuffer *fb,
  8582. struct drm_display_mode *mode)
  8583. {
  8584. struct drm_device *dev = crtc->dev;
  8585. struct intel_encoder *encoder;
  8586. struct intel_crtc_state *pipe_config;
  8587. int plane_bpp, ret = -EINVAL;
  8588. bool retry = true;
  8589. if (!check_encoder_cloning(to_intel_crtc(crtc))) {
  8590. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  8591. return ERR_PTR(-EINVAL);
  8592. }
  8593. if (!check_digital_port_conflicts(dev)) {
  8594. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  8595. return ERR_PTR(-EINVAL);
  8596. }
  8597. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8598. if (!pipe_config)
  8599. return ERR_PTR(-ENOMEM);
  8600. drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
  8601. drm_mode_copy(&pipe_config->base.mode, mode);
  8602. pipe_config->cpu_transcoder =
  8603. (enum transcoder) to_intel_crtc(crtc)->pipe;
  8604. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8605. /*
  8606. * Sanitize sync polarity flags based on requested ones. If neither
  8607. * positive or negative polarity is requested, treat this as meaning
  8608. * negative polarity.
  8609. */
  8610. if (!(pipe_config->base.adjusted_mode.flags &
  8611. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  8612. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  8613. if (!(pipe_config->base.adjusted_mode.flags &
  8614. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  8615. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  8616. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  8617. * plane pixel format and any sink constraints into account. Returns the
  8618. * source plane bpp so that dithering can be selected on mismatches
  8619. * after encoders and crtc also have had their say. */
  8620. plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  8621. fb, pipe_config);
  8622. if (plane_bpp < 0)
  8623. goto fail;
  8624. /*
  8625. * Determine the real pipe dimensions. Note that stereo modes can
  8626. * increase the actual pipe size due to the frame doubling and
  8627. * insertion of additional space for blanks between the frame. This
  8628. * is stored in the crtc timings. We use the requested mode to do this
  8629. * computation to clearly distinguish it from the adjusted mode, which
  8630. * can be changed by the connectors in the below retry loop.
  8631. */
  8632. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  8633. &pipe_config->pipe_src_w,
  8634. &pipe_config->pipe_src_h);
  8635. encoder_retry:
  8636. /* Ensure the port clock defaults are reset when retrying. */
  8637. pipe_config->port_clock = 0;
  8638. pipe_config->pixel_multiplier = 1;
  8639. /* Fill in default crtc timings, allow encoders to overwrite them. */
  8640. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  8641. CRTC_STEREO_DOUBLE);
  8642. /* Pass our mode to the connectors and the CRTC to give them a chance to
  8643. * adjust it according to limitations or connector properties, and also
  8644. * a chance to reject the mode entirely.
  8645. */
  8646. for_each_intel_encoder(dev, encoder) {
  8647. if (&encoder->new_crtc->base != crtc)
  8648. continue;
  8649. if (!(encoder->compute_config(encoder, pipe_config))) {
  8650. DRM_DEBUG_KMS("Encoder config failure\n");
  8651. goto fail;
  8652. }
  8653. }
  8654. /* Set default port clock if not overwritten by the encoder. Needs to be
  8655. * done afterwards in case the encoder adjusts the mode. */
  8656. if (!pipe_config->port_clock)
  8657. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  8658. * pipe_config->pixel_multiplier;
  8659. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  8660. if (ret < 0) {
  8661. DRM_DEBUG_KMS("CRTC fixup failed\n");
  8662. goto fail;
  8663. }
  8664. if (ret == RETRY) {
  8665. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  8666. ret = -EINVAL;
  8667. goto fail;
  8668. }
  8669. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  8670. retry = false;
  8671. goto encoder_retry;
  8672. }
  8673. pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
  8674. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  8675. plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  8676. return pipe_config;
  8677. fail:
  8678. kfree(pipe_config);
  8679. return ERR_PTR(ret);
  8680. }
  8681. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  8682. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  8683. static void
  8684. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  8685. unsigned *prepare_pipes, unsigned *disable_pipes)
  8686. {
  8687. struct intel_crtc *intel_crtc;
  8688. struct drm_device *dev = crtc->dev;
  8689. struct intel_encoder *encoder;
  8690. struct intel_connector *connector;
  8691. struct drm_crtc *tmp_crtc;
  8692. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  8693. /* Check which crtcs have changed outputs connected to them, these need
  8694. * to be part of the prepare_pipes mask. We don't (yet) support global
  8695. * modeset across multiple crtcs, so modeset_pipes will only have one
  8696. * bit set at most. */
  8697. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8698. base.head) {
  8699. if (connector->base.encoder == &connector->new_encoder->base)
  8700. continue;
  8701. if (connector->base.encoder) {
  8702. tmp_crtc = connector->base.encoder->crtc;
  8703. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8704. }
  8705. if (connector->new_encoder)
  8706. *prepare_pipes |=
  8707. 1 << connector->new_encoder->new_crtc->pipe;
  8708. }
  8709. for_each_intel_encoder(dev, encoder) {
  8710. if (encoder->base.crtc == &encoder->new_crtc->base)
  8711. continue;
  8712. if (encoder->base.crtc) {
  8713. tmp_crtc = encoder->base.crtc;
  8714. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8715. }
  8716. if (encoder->new_crtc)
  8717. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  8718. }
  8719. /* Check for pipes that will be enabled/disabled ... */
  8720. for_each_intel_crtc(dev, intel_crtc) {
  8721. if (intel_crtc->base.enabled == intel_crtc->new_enabled)
  8722. continue;
  8723. if (!intel_crtc->new_enabled)
  8724. *disable_pipes |= 1 << intel_crtc->pipe;
  8725. else
  8726. *prepare_pipes |= 1 << intel_crtc->pipe;
  8727. }
  8728. /* set_mode is also used to update properties on life display pipes. */
  8729. intel_crtc = to_intel_crtc(crtc);
  8730. if (intel_crtc->new_enabled)
  8731. *prepare_pipes |= 1 << intel_crtc->pipe;
  8732. /*
  8733. * For simplicity do a full modeset on any pipe where the output routing
  8734. * changed. We could be more clever, but that would require us to be
  8735. * more careful with calling the relevant encoder->mode_set functions.
  8736. */
  8737. if (*prepare_pipes)
  8738. *modeset_pipes = *prepare_pipes;
  8739. /* ... and mask these out. */
  8740. *modeset_pipes &= ~(*disable_pipes);
  8741. *prepare_pipes &= ~(*disable_pipes);
  8742. /*
  8743. * HACK: We don't (yet) fully support global modesets. intel_set_config
  8744. * obies this rule, but the modeset restore mode of
  8745. * intel_modeset_setup_hw_state does not.
  8746. */
  8747. *modeset_pipes &= 1 << intel_crtc->pipe;
  8748. *prepare_pipes &= 1 << intel_crtc->pipe;
  8749. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  8750. *modeset_pipes, *prepare_pipes, *disable_pipes);
  8751. }
  8752. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  8753. {
  8754. struct drm_encoder *encoder;
  8755. struct drm_device *dev = crtc->dev;
  8756. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  8757. if (encoder->crtc == crtc)
  8758. return true;
  8759. return false;
  8760. }
  8761. static void
  8762. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  8763. {
  8764. struct drm_i915_private *dev_priv = dev->dev_private;
  8765. struct intel_encoder *intel_encoder;
  8766. struct intel_crtc *intel_crtc;
  8767. struct drm_connector *connector;
  8768. intel_shared_dpll_commit(dev_priv);
  8769. for_each_intel_encoder(dev, intel_encoder) {
  8770. if (!intel_encoder->base.crtc)
  8771. continue;
  8772. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  8773. if (prepare_pipes & (1 << intel_crtc->pipe))
  8774. intel_encoder->connectors_active = false;
  8775. }
  8776. intel_modeset_commit_output_state(dev);
  8777. /* Double check state. */
  8778. for_each_intel_crtc(dev, intel_crtc) {
  8779. WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
  8780. WARN_ON(intel_crtc->new_config &&
  8781. intel_crtc->new_config != intel_crtc->config);
  8782. WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
  8783. }
  8784. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  8785. if (!connector->encoder || !connector->encoder->crtc)
  8786. continue;
  8787. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  8788. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  8789. struct drm_property *dpms_property =
  8790. dev->mode_config.dpms_property;
  8791. connector->dpms = DRM_MODE_DPMS_ON;
  8792. drm_object_property_set_value(&connector->base,
  8793. dpms_property,
  8794. DRM_MODE_DPMS_ON);
  8795. intel_encoder = to_intel_encoder(connector->encoder);
  8796. intel_encoder->connectors_active = true;
  8797. }
  8798. }
  8799. }
  8800. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  8801. {
  8802. int diff;
  8803. if (clock1 == clock2)
  8804. return true;
  8805. if (!clock1 || !clock2)
  8806. return false;
  8807. diff = abs(clock1 - clock2);
  8808. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  8809. return true;
  8810. return false;
  8811. }
  8812. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  8813. list_for_each_entry((intel_crtc), \
  8814. &(dev)->mode_config.crtc_list, \
  8815. base.head) \
  8816. if (mask & (1 <<(intel_crtc)->pipe))
  8817. static bool
  8818. intel_pipe_config_compare(struct drm_device *dev,
  8819. struct intel_crtc_state *current_config,
  8820. struct intel_crtc_state *pipe_config)
  8821. {
  8822. #define PIPE_CONF_CHECK_X(name) \
  8823. if (current_config->name != pipe_config->name) { \
  8824. DRM_ERROR("mismatch in " #name " " \
  8825. "(expected 0x%08x, found 0x%08x)\n", \
  8826. current_config->name, \
  8827. pipe_config->name); \
  8828. return false; \
  8829. }
  8830. #define PIPE_CONF_CHECK_I(name) \
  8831. if (current_config->name != pipe_config->name) { \
  8832. DRM_ERROR("mismatch in " #name " " \
  8833. "(expected %i, found %i)\n", \
  8834. current_config->name, \
  8835. pipe_config->name); \
  8836. return false; \
  8837. }
  8838. /* This is required for BDW+ where there is only one set of registers for
  8839. * switching between high and low RR.
  8840. * This macro can be used whenever a comparison has to be made between one
  8841. * hw state and multiple sw state variables.
  8842. */
  8843. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  8844. if ((current_config->name != pipe_config->name) && \
  8845. (current_config->alt_name != pipe_config->name)) { \
  8846. DRM_ERROR("mismatch in " #name " " \
  8847. "(expected %i or %i, found %i)\n", \
  8848. current_config->name, \
  8849. current_config->alt_name, \
  8850. pipe_config->name); \
  8851. return false; \
  8852. }
  8853. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  8854. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  8855. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  8856. "(expected %i, found %i)\n", \
  8857. current_config->name & (mask), \
  8858. pipe_config->name & (mask)); \
  8859. return false; \
  8860. }
  8861. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  8862. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  8863. DRM_ERROR("mismatch in " #name " " \
  8864. "(expected %i, found %i)\n", \
  8865. current_config->name, \
  8866. pipe_config->name); \
  8867. return false; \
  8868. }
  8869. #define PIPE_CONF_QUIRK(quirk) \
  8870. ((current_config->quirks | pipe_config->quirks) & (quirk))
  8871. PIPE_CONF_CHECK_I(cpu_transcoder);
  8872. PIPE_CONF_CHECK_I(has_pch_encoder);
  8873. PIPE_CONF_CHECK_I(fdi_lanes);
  8874. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  8875. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  8876. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  8877. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  8878. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  8879. PIPE_CONF_CHECK_I(has_dp_encoder);
  8880. if (INTEL_INFO(dev)->gen < 8) {
  8881. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  8882. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  8883. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  8884. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  8885. PIPE_CONF_CHECK_I(dp_m_n.tu);
  8886. if (current_config->has_drrs) {
  8887. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
  8888. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
  8889. PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
  8890. PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
  8891. PIPE_CONF_CHECK_I(dp_m2_n2.tu);
  8892. }
  8893. } else {
  8894. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
  8895. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
  8896. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
  8897. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
  8898. PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
  8899. }
  8900. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  8901. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  8902. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  8903. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  8904. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  8905. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  8906. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  8907. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  8908. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  8909. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  8910. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  8911. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  8912. PIPE_CONF_CHECK_I(pixel_multiplier);
  8913. PIPE_CONF_CHECK_I(has_hdmi_sink);
  8914. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  8915. IS_VALLEYVIEW(dev))
  8916. PIPE_CONF_CHECK_I(limited_color_range);
  8917. PIPE_CONF_CHECK_I(has_infoframe);
  8918. PIPE_CONF_CHECK_I(has_audio);
  8919. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  8920. DRM_MODE_FLAG_INTERLACE);
  8921. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  8922. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  8923. DRM_MODE_FLAG_PHSYNC);
  8924. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  8925. DRM_MODE_FLAG_NHSYNC);
  8926. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  8927. DRM_MODE_FLAG_PVSYNC);
  8928. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  8929. DRM_MODE_FLAG_NVSYNC);
  8930. }
  8931. PIPE_CONF_CHECK_I(pipe_src_w);
  8932. PIPE_CONF_CHECK_I(pipe_src_h);
  8933. /*
  8934. * FIXME: BIOS likes to set up a cloned config with lvds+external
  8935. * screen. Since we don't yet re-compute the pipe config when moving
  8936. * just the lvds port away to another pipe the sw tracking won't match.
  8937. *
  8938. * Proper atomic modesets with recomputed global state will fix this.
  8939. * Until then just don't check gmch state for inherited modes.
  8940. */
  8941. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  8942. PIPE_CONF_CHECK_I(gmch_pfit.control);
  8943. /* pfit ratios are autocomputed by the hw on gen4+ */
  8944. if (INTEL_INFO(dev)->gen < 4)
  8945. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  8946. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  8947. }
  8948. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  8949. if (current_config->pch_pfit.enabled) {
  8950. PIPE_CONF_CHECK_I(pch_pfit.pos);
  8951. PIPE_CONF_CHECK_I(pch_pfit.size);
  8952. }
  8953. /* BDW+ don't expose a synchronous way to read the state */
  8954. if (IS_HASWELL(dev))
  8955. PIPE_CONF_CHECK_I(ips_enabled);
  8956. PIPE_CONF_CHECK_I(double_wide);
  8957. PIPE_CONF_CHECK_X(ddi_pll_sel);
  8958. PIPE_CONF_CHECK_I(shared_dpll);
  8959. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  8960. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  8961. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  8962. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  8963. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  8964. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  8965. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  8966. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  8967. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  8968. PIPE_CONF_CHECK_I(pipe_bpp);
  8969. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  8970. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  8971. #undef PIPE_CONF_CHECK_X
  8972. #undef PIPE_CONF_CHECK_I
  8973. #undef PIPE_CONF_CHECK_I_ALT
  8974. #undef PIPE_CONF_CHECK_FLAGS
  8975. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  8976. #undef PIPE_CONF_QUIRK
  8977. return true;
  8978. }
  8979. static void check_wm_state(struct drm_device *dev)
  8980. {
  8981. struct drm_i915_private *dev_priv = dev->dev_private;
  8982. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  8983. struct intel_crtc *intel_crtc;
  8984. int plane;
  8985. if (INTEL_INFO(dev)->gen < 9)
  8986. return;
  8987. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  8988. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  8989. for_each_intel_crtc(dev, intel_crtc) {
  8990. struct skl_ddb_entry *hw_entry, *sw_entry;
  8991. const enum pipe pipe = intel_crtc->pipe;
  8992. if (!intel_crtc->active)
  8993. continue;
  8994. /* planes */
  8995. for_each_plane(pipe, plane) {
  8996. hw_entry = &hw_ddb.plane[pipe][plane];
  8997. sw_entry = &sw_ddb->plane[pipe][plane];
  8998. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  8999. continue;
  9000. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  9001. "(expected (%u,%u), found (%u,%u))\n",
  9002. pipe_name(pipe), plane + 1,
  9003. sw_entry->start, sw_entry->end,
  9004. hw_entry->start, hw_entry->end);
  9005. }
  9006. /* cursor */
  9007. hw_entry = &hw_ddb.cursor[pipe];
  9008. sw_entry = &sw_ddb->cursor[pipe];
  9009. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  9010. continue;
  9011. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  9012. "(expected (%u,%u), found (%u,%u))\n",
  9013. pipe_name(pipe),
  9014. sw_entry->start, sw_entry->end,
  9015. hw_entry->start, hw_entry->end);
  9016. }
  9017. }
  9018. static void
  9019. check_connector_state(struct drm_device *dev)
  9020. {
  9021. struct intel_connector *connector;
  9022. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9023. base.head) {
  9024. /* This also checks the encoder/connector hw state with the
  9025. * ->get_hw_state callbacks. */
  9026. intel_connector_check_state(connector);
  9027. I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
  9028. "connector's staged encoder doesn't match current encoder\n");
  9029. }
  9030. }
  9031. static void
  9032. check_encoder_state(struct drm_device *dev)
  9033. {
  9034. struct intel_encoder *encoder;
  9035. struct intel_connector *connector;
  9036. for_each_intel_encoder(dev, encoder) {
  9037. bool enabled = false;
  9038. bool active = false;
  9039. enum pipe pipe, tracked_pipe;
  9040. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  9041. encoder->base.base.id,
  9042. encoder->base.name);
  9043. I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
  9044. "encoder's stage crtc doesn't match current crtc\n");
  9045. I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
  9046. "encoder's active_connectors set, but no crtc\n");
  9047. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9048. base.head) {
  9049. if (connector->base.encoder != &encoder->base)
  9050. continue;
  9051. enabled = true;
  9052. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  9053. active = true;
  9054. }
  9055. /*
  9056. * for MST connectors if we unplug the connector is gone
  9057. * away but the encoder is still connected to a crtc
  9058. * until a modeset happens in response to the hotplug.
  9059. */
  9060. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  9061. continue;
  9062. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  9063. "encoder's enabled state mismatch "
  9064. "(expected %i, found %i)\n",
  9065. !!encoder->base.crtc, enabled);
  9066. I915_STATE_WARN(active && !encoder->base.crtc,
  9067. "active encoder with no crtc\n");
  9068. I915_STATE_WARN(encoder->connectors_active != active,
  9069. "encoder's computed active state doesn't match tracked active state "
  9070. "(expected %i, found %i)\n", active, encoder->connectors_active);
  9071. active = encoder->get_hw_state(encoder, &pipe);
  9072. I915_STATE_WARN(active != encoder->connectors_active,
  9073. "encoder's hw state doesn't match sw tracking "
  9074. "(expected %i, found %i)\n",
  9075. encoder->connectors_active, active);
  9076. if (!encoder->base.crtc)
  9077. continue;
  9078. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  9079. I915_STATE_WARN(active && pipe != tracked_pipe,
  9080. "active encoder's pipe doesn't match"
  9081. "(expected %i, found %i)\n",
  9082. tracked_pipe, pipe);
  9083. }
  9084. }
  9085. static void
  9086. check_crtc_state(struct drm_device *dev)
  9087. {
  9088. struct drm_i915_private *dev_priv = dev->dev_private;
  9089. struct intel_crtc *crtc;
  9090. struct intel_encoder *encoder;
  9091. struct intel_crtc_state pipe_config;
  9092. for_each_intel_crtc(dev, crtc) {
  9093. bool enabled = false;
  9094. bool active = false;
  9095. memset(&pipe_config, 0, sizeof(pipe_config));
  9096. DRM_DEBUG_KMS("[CRTC:%d]\n",
  9097. crtc->base.base.id);
  9098. I915_STATE_WARN(crtc->active && !crtc->base.enabled,
  9099. "active crtc, but not enabled in sw tracking\n");
  9100. for_each_intel_encoder(dev, encoder) {
  9101. if (encoder->base.crtc != &crtc->base)
  9102. continue;
  9103. enabled = true;
  9104. if (encoder->connectors_active)
  9105. active = true;
  9106. }
  9107. I915_STATE_WARN(active != crtc->active,
  9108. "crtc's computed active state doesn't match tracked active state "
  9109. "(expected %i, found %i)\n", active, crtc->active);
  9110. I915_STATE_WARN(enabled != crtc->base.enabled,
  9111. "crtc's computed enabled state doesn't match tracked enabled state "
  9112. "(expected %i, found %i)\n", enabled, crtc->base.enabled);
  9113. active = dev_priv->display.get_pipe_config(crtc,
  9114. &pipe_config);
  9115. /* hw state is inconsistent with the pipe quirk */
  9116. if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  9117. (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  9118. active = crtc->active;
  9119. for_each_intel_encoder(dev, encoder) {
  9120. enum pipe pipe;
  9121. if (encoder->base.crtc != &crtc->base)
  9122. continue;
  9123. if (encoder->get_hw_state(encoder, &pipe))
  9124. encoder->get_config(encoder, &pipe_config);
  9125. }
  9126. I915_STATE_WARN(crtc->active != active,
  9127. "crtc active state doesn't match with hw state "
  9128. "(expected %i, found %i)\n", crtc->active, active);
  9129. if (active &&
  9130. !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
  9131. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  9132. intel_dump_pipe_config(crtc, &pipe_config,
  9133. "[hw state]");
  9134. intel_dump_pipe_config(crtc, crtc->config,
  9135. "[sw state]");
  9136. }
  9137. }
  9138. }
  9139. static void
  9140. check_shared_dpll_state(struct drm_device *dev)
  9141. {
  9142. struct drm_i915_private *dev_priv = dev->dev_private;
  9143. struct intel_crtc *crtc;
  9144. struct intel_dpll_hw_state dpll_hw_state;
  9145. int i;
  9146. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9147. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  9148. int enabled_crtcs = 0, active_crtcs = 0;
  9149. bool active;
  9150. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  9151. DRM_DEBUG_KMS("%s\n", pll->name);
  9152. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  9153. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  9154. "more active pll users than references: %i vs %i\n",
  9155. pll->active, hweight32(pll->config.crtc_mask));
  9156. I915_STATE_WARN(pll->active && !pll->on,
  9157. "pll in active use but not on in sw tracking\n");
  9158. I915_STATE_WARN(pll->on && !pll->active,
  9159. "pll in on but not on in use in sw tracking\n");
  9160. I915_STATE_WARN(pll->on != active,
  9161. "pll on state mismatch (expected %i, found %i)\n",
  9162. pll->on, active);
  9163. for_each_intel_crtc(dev, crtc) {
  9164. if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
  9165. enabled_crtcs++;
  9166. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  9167. active_crtcs++;
  9168. }
  9169. I915_STATE_WARN(pll->active != active_crtcs,
  9170. "pll active crtcs mismatch (expected %i, found %i)\n",
  9171. pll->active, active_crtcs);
  9172. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  9173. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  9174. hweight32(pll->config.crtc_mask), enabled_crtcs);
  9175. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  9176. sizeof(dpll_hw_state)),
  9177. "pll hw state mismatch\n");
  9178. }
  9179. }
  9180. void
  9181. intel_modeset_check_state(struct drm_device *dev)
  9182. {
  9183. check_wm_state(dev);
  9184. check_connector_state(dev);
  9185. check_encoder_state(dev);
  9186. check_crtc_state(dev);
  9187. check_shared_dpll_state(dev);
  9188. }
  9189. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  9190. int dotclock)
  9191. {
  9192. /*
  9193. * FDI already provided one idea for the dotclock.
  9194. * Yell if the encoder disagrees.
  9195. */
  9196. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  9197. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9198. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  9199. }
  9200. static void update_scanline_offset(struct intel_crtc *crtc)
  9201. {
  9202. struct drm_device *dev = crtc->base.dev;
  9203. /*
  9204. * The scanline counter increments at the leading edge of hsync.
  9205. *
  9206. * On most platforms it starts counting from vtotal-1 on the
  9207. * first active line. That means the scanline counter value is
  9208. * always one less than what we would expect. Ie. just after
  9209. * start of vblank, which also occurs at start of hsync (on the
  9210. * last active line), the scanline counter will read vblank_start-1.
  9211. *
  9212. * On gen2 the scanline counter starts counting from 1 instead
  9213. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  9214. * to keep the value positive), instead of adding one.
  9215. *
  9216. * On HSW+ the behaviour of the scanline counter depends on the output
  9217. * type. For DP ports it behaves like most other platforms, but on HDMI
  9218. * there's an extra 1 line difference. So we need to add two instead of
  9219. * one to the value.
  9220. */
  9221. if (IS_GEN2(dev)) {
  9222. const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
  9223. int vtotal;
  9224. vtotal = mode->crtc_vtotal;
  9225. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  9226. vtotal /= 2;
  9227. crtc->scanline_offset = vtotal - 1;
  9228. } else if (HAS_DDI(dev) &&
  9229. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  9230. crtc->scanline_offset = 2;
  9231. } else
  9232. crtc->scanline_offset = 1;
  9233. }
  9234. static struct intel_crtc_state *
  9235. intel_modeset_compute_config(struct drm_crtc *crtc,
  9236. struct drm_display_mode *mode,
  9237. struct drm_framebuffer *fb,
  9238. unsigned *modeset_pipes,
  9239. unsigned *prepare_pipes,
  9240. unsigned *disable_pipes)
  9241. {
  9242. struct intel_crtc_state *pipe_config = NULL;
  9243. intel_modeset_affected_pipes(crtc, modeset_pipes,
  9244. prepare_pipes, disable_pipes);
  9245. if ((*modeset_pipes) == 0)
  9246. goto out;
  9247. /*
  9248. * Note this needs changes when we start tracking multiple modes
  9249. * and crtcs. At that point we'll need to compute the whole config
  9250. * (i.e. one pipe_config for each crtc) rather than just the one
  9251. * for this crtc.
  9252. */
  9253. pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
  9254. if (IS_ERR(pipe_config)) {
  9255. goto out;
  9256. }
  9257. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  9258. "[modeset]");
  9259. out:
  9260. return pipe_config;
  9261. }
  9262. static int __intel_set_mode_setup_plls(struct drm_device *dev,
  9263. unsigned modeset_pipes,
  9264. unsigned disable_pipes)
  9265. {
  9266. struct drm_i915_private *dev_priv = to_i915(dev);
  9267. unsigned clear_pipes = modeset_pipes | disable_pipes;
  9268. struct intel_crtc *intel_crtc;
  9269. int ret = 0;
  9270. if (!dev_priv->display.crtc_compute_clock)
  9271. return 0;
  9272. ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
  9273. if (ret)
  9274. goto done;
  9275. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9276. struct intel_crtc_state *state = intel_crtc->new_config;
  9277. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  9278. state);
  9279. if (ret) {
  9280. intel_shared_dpll_abort_config(dev_priv);
  9281. goto done;
  9282. }
  9283. }
  9284. done:
  9285. return ret;
  9286. }
  9287. static int __intel_set_mode(struct drm_crtc *crtc,
  9288. struct drm_display_mode *mode,
  9289. int x, int y, struct drm_framebuffer *fb,
  9290. struct intel_crtc_state *pipe_config,
  9291. unsigned modeset_pipes,
  9292. unsigned prepare_pipes,
  9293. unsigned disable_pipes)
  9294. {
  9295. struct drm_device *dev = crtc->dev;
  9296. struct drm_i915_private *dev_priv = dev->dev_private;
  9297. struct drm_display_mode *saved_mode;
  9298. struct intel_crtc *intel_crtc;
  9299. int ret = 0;
  9300. saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
  9301. if (!saved_mode)
  9302. return -ENOMEM;
  9303. *saved_mode = crtc->mode;
  9304. if (modeset_pipes)
  9305. to_intel_crtc(crtc)->new_config = pipe_config;
  9306. /*
  9307. * See if the config requires any additional preparation, e.g.
  9308. * to adjust global state with pipes off. We need to do this
  9309. * here so we can get the modeset_pipe updated config for the new
  9310. * mode set on this crtc. For other crtcs we need to use the
  9311. * adjusted_mode bits in the crtc directly.
  9312. */
  9313. if (IS_VALLEYVIEW(dev)) {
  9314. valleyview_modeset_global_pipes(dev, &prepare_pipes);
  9315. /* may have added more to prepare_pipes than we should */
  9316. prepare_pipes &= ~disable_pipes;
  9317. }
  9318. ret = __intel_set_mode_setup_plls(dev, modeset_pipes, disable_pipes);
  9319. if (ret)
  9320. goto done;
  9321. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  9322. intel_crtc_disable(&intel_crtc->base);
  9323. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9324. if (intel_crtc->base.enabled)
  9325. dev_priv->display.crtc_disable(&intel_crtc->base);
  9326. }
  9327. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  9328. * to set it here already despite that we pass it down the callchain.
  9329. *
  9330. * Note we'll need to fix this up when we start tracking multiple
  9331. * pipes; here we assume a single modeset_pipe and only track the
  9332. * single crtc and mode.
  9333. */
  9334. if (modeset_pipes) {
  9335. crtc->mode = *mode;
  9336. /* mode_set/enable/disable functions rely on a correct pipe
  9337. * config. */
  9338. intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
  9339. /*
  9340. * Calculate and store various constants which
  9341. * are later needed by vblank and swap-completion
  9342. * timestamping. They are derived from true hwmode.
  9343. */
  9344. drm_calc_timestamping_constants(crtc,
  9345. &pipe_config->base.adjusted_mode);
  9346. }
  9347. /* Only after disabling all output pipelines that will be changed can we
  9348. * update the the output configuration. */
  9349. intel_modeset_update_state(dev, prepare_pipes);
  9350. modeset_update_crtc_power_domains(dev);
  9351. /* Set up the DPLL and any encoders state that needs to adjust or depend
  9352. * on the DPLL.
  9353. */
  9354. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9355. struct drm_plane *primary = intel_crtc->base.primary;
  9356. int vdisplay, hdisplay;
  9357. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  9358. ret = primary->funcs->update_plane(primary, &intel_crtc->base,
  9359. fb, 0, 0,
  9360. hdisplay, vdisplay,
  9361. x << 16, y << 16,
  9362. hdisplay << 16, vdisplay << 16);
  9363. }
  9364. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  9365. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9366. update_scanline_offset(intel_crtc);
  9367. dev_priv->display.crtc_enable(&intel_crtc->base);
  9368. }
  9369. /* FIXME: add subpixel order */
  9370. done:
  9371. if (ret && crtc->enabled)
  9372. crtc->mode = *saved_mode;
  9373. kfree(saved_mode);
  9374. return ret;
  9375. }
  9376. static int intel_set_mode_pipes(struct drm_crtc *crtc,
  9377. struct drm_display_mode *mode,
  9378. int x, int y, struct drm_framebuffer *fb,
  9379. struct intel_crtc_state *pipe_config,
  9380. unsigned modeset_pipes,
  9381. unsigned prepare_pipes,
  9382. unsigned disable_pipes)
  9383. {
  9384. int ret;
  9385. ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
  9386. prepare_pipes, disable_pipes);
  9387. if (ret == 0)
  9388. intel_modeset_check_state(crtc->dev);
  9389. return ret;
  9390. }
  9391. static int intel_set_mode(struct drm_crtc *crtc,
  9392. struct drm_display_mode *mode,
  9393. int x, int y, struct drm_framebuffer *fb)
  9394. {
  9395. struct intel_crtc_state *pipe_config;
  9396. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  9397. pipe_config = intel_modeset_compute_config(crtc, mode, fb,
  9398. &modeset_pipes,
  9399. &prepare_pipes,
  9400. &disable_pipes);
  9401. if (IS_ERR(pipe_config))
  9402. return PTR_ERR(pipe_config);
  9403. return intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
  9404. modeset_pipes, prepare_pipes,
  9405. disable_pipes);
  9406. }
  9407. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  9408. {
  9409. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
  9410. }
  9411. #undef for_each_intel_crtc_masked
  9412. static void intel_set_config_free(struct intel_set_config *config)
  9413. {
  9414. if (!config)
  9415. return;
  9416. kfree(config->save_connector_encoders);
  9417. kfree(config->save_encoder_crtcs);
  9418. kfree(config->save_crtc_enabled);
  9419. kfree(config);
  9420. }
  9421. static int intel_set_config_save_state(struct drm_device *dev,
  9422. struct intel_set_config *config)
  9423. {
  9424. struct drm_crtc *crtc;
  9425. struct drm_encoder *encoder;
  9426. struct drm_connector *connector;
  9427. int count;
  9428. config->save_crtc_enabled =
  9429. kcalloc(dev->mode_config.num_crtc,
  9430. sizeof(bool), GFP_KERNEL);
  9431. if (!config->save_crtc_enabled)
  9432. return -ENOMEM;
  9433. config->save_encoder_crtcs =
  9434. kcalloc(dev->mode_config.num_encoder,
  9435. sizeof(struct drm_crtc *), GFP_KERNEL);
  9436. if (!config->save_encoder_crtcs)
  9437. return -ENOMEM;
  9438. config->save_connector_encoders =
  9439. kcalloc(dev->mode_config.num_connector,
  9440. sizeof(struct drm_encoder *), GFP_KERNEL);
  9441. if (!config->save_connector_encoders)
  9442. return -ENOMEM;
  9443. /* Copy data. Note that driver private data is not affected.
  9444. * Should anything bad happen only the expected state is
  9445. * restored, not the drivers personal bookkeeping.
  9446. */
  9447. count = 0;
  9448. for_each_crtc(dev, crtc) {
  9449. config->save_crtc_enabled[count++] = crtc->enabled;
  9450. }
  9451. count = 0;
  9452. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  9453. config->save_encoder_crtcs[count++] = encoder->crtc;
  9454. }
  9455. count = 0;
  9456. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  9457. config->save_connector_encoders[count++] = connector->encoder;
  9458. }
  9459. return 0;
  9460. }
  9461. static void intel_set_config_restore_state(struct drm_device *dev,
  9462. struct intel_set_config *config)
  9463. {
  9464. struct intel_crtc *crtc;
  9465. struct intel_encoder *encoder;
  9466. struct intel_connector *connector;
  9467. int count;
  9468. count = 0;
  9469. for_each_intel_crtc(dev, crtc) {
  9470. crtc->new_enabled = config->save_crtc_enabled[count++];
  9471. if (crtc->new_enabled)
  9472. crtc->new_config = crtc->config;
  9473. else
  9474. crtc->new_config = NULL;
  9475. }
  9476. count = 0;
  9477. for_each_intel_encoder(dev, encoder) {
  9478. encoder->new_crtc =
  9479. to_intel_crtc(config->save_encoder_crtcs[count++]);
  9480. }
  9481. count = 0;
  9482. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  9483. connector->new_encoder =
  9484. to_intel_encoder(config->save_connector_encoders[count++]);
  9485. }
  9486. }
  9487. static bool
  9488. is_crtc_connector_off(struct drm_mode_set *set)
  9489. {
  9490. int i;
  9491. if (set->num_connectors == 0)
  9492. return false;
  9493. if (WARN_ON(set->connectors == NULL))
  9494. return false;
  9495. for (i = 0; i < set->num_connectors; i++)
  9496. if (set->connectors[i]->encoder &&
  9497. set->connectors[i]->encoder->crtc == set->crtc &&
  9498. set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
  9499. return true;
  9500. return false;
  9501. }
  9502. static void
  9503. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  9504. struct intel_set_config *config)
  9505. {
  9506. /* We should be able to check here if the fb has the same properties
  9507. * and then just flip_or_move it */
  9508. if (is_crtc_connector_off(set)) {
  9509. config->mode_changed = true;
  9510. } else if (set->crtc->primary->fb != set->fb) {
  9511. /*
  9512. * If we have no fb, we can only flip as long as the crtc is
  9513. * active, otherwise we need a full mode set. The crtc may
  9514. * be active if we've only disabled the primary plane, or
  9515. * in fastboot situations.
  9516. */
  9517. if (set->crtc->primary->fb == NULL) {
  9518. struct intel_crtc *intel_crtc =
  9519. to_intel_crtc(set->crtc);
  9520. if (intel_crtc->active) {
  9521. DRM_DEBUG_KMS("crtc has no fb, will flip\n");
  9522. config->fb_changed = true;
  9523. } else {
  9524. DRM_DEBUG_KMS("inactive crtc, full mode set\n");
  9525. config->mode_changed = true;
  9526. }
  9527. } else if (set->fb == NULL) {
  9528. config->mode_changed = true;
  9529. } else if (set->fb->pixel_format !=
  9530. set->crtc->primary->fb->pixel_format) {
  9531. config->mode_changed = true;
  9532. } else {
  9533. config->fb_changed = true;
  9534. }
  9535. }
  9536. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  9537. config->fb_changed = true;
  9538. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  9539. DRM_DEBUG_KMS("modes are different, full mode set\n");
  9540. drm_mode_debug_printmodeline(&set->crtc->mode);
  9541. drm_mode_debug_printmodeline(set->mode);
  9542. config->mode_changed = true;
  9543. }
  9544. DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
  9545. set->crtc->base.id, config->mode_changed, config->fb_changed);
  9546. }
  9547. static int
  9548. intel_modeset_stage_output_state(struct drm_device *dev,
  9549. struct drm_mode_set *set,
  9550. struct intel_set_config *config)
  9551. {
  9552. struct intel_connector *connector;
  9553. struct intel_encoder *encoder;
  9554. struct intel_crtc *crtc;
  9555. int ro;
  9556. /* The upper layers ensure that we either disable a crtc or have a list
  9557. * of connectors. For paranoia, double-check this. */
  9558. WARN_ON(!set->fb && (set->num_connectors != 0));
  9559. WARN_ON(set->fb && (set->num_connectors == 0));
  9560. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9561. base.head) {
  9562. /* Otherwise traverse passed in connector list and get encoders
  9563. * for them. */
  9564. for (ro = 0; ro < set->num_connectors; ro++) {
  9565. if (set->connectors[ro] == &connector->base) {
  9566. connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
  9567. break;
  9568. }
  9569. }
  9570. /* If we disable the crtc, disable all its connectors. Also, if
  9571. * the connector is on the changing crtc but not on the new
  9572. * connector list, disable it. */
  9573. if ((!set->fb || ro == set->num_connectors) &&
  9574. connector->base.encoder &&
  9575. connector->base.encoder->crtc == set->crtc) {
  9576. connector->new_encoder = NULL;
  9577. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  9578. connector->base.base.id,
  9579. connector->base.name);
  9580. }
  9581. if (&connector->new_encoder->base != connector->base.encoder) {
  9582. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  9583. config->mode_changed = true;
  9584. }
  9585. }
  9586. /* connector->new_encoder is now updated for all connectors. */
  9587. /* Update crtc of enabled connectors. */
  9588. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9589. base.head) {
  9590. struct drm_crtc *new_crtc;
  9591. if (!connector->new_encoder)
  9592. continue;
  9593. new_crtc = connector->new_encoder->base.crtc;
  9594. for (ro = 0; ro < set->num_connectors; ro++) {
  9595. if (set->connectors[ro] == &connector->base)
  9596. new_crtc = set->crtc;
  9597. }
  9598. /* Make sure the new CRTC will work with the encoder */
  9599. if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
  9600. new_crtc)) {
  9601. return -EINVAL;
  9602. }
  9603. connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
  9604. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  9605. connector->base.base.id,
  9606. connector->base.name,
  9607. new_crtc->base.id);
  9608. }
  9609. /* Check for any encoders that needs to be disabled. */
  9610. for_each_intel_encoder(dev, encoder) {
  9611. int num_connectors = 0;
  9612. list_for_each_entry(connector,
  9613. &dev->mode_config.connector_list,
  9614. base.head) {
  9615. if (connector->new_encoder == encoder) {
  9616. WARN_ON(!connector->new_encoder->new_crtc);
  9617. num_connectors++;
  9618. }
  9619. }
  9620. if (num_connectors == 0)
  9621. encoder->new_crtc = NULL;
  9622. else if (num_connectors > 1)
  9623. return -EINVAL;
  9624. /* Only now check for crtc changes so we don't miss encoders
  9625. * that will be disabled. */
  9626. if (&encoder->new_crtc->base != encoder->base.crtc) {
  9627. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  9628. config->mode_changed = true;
  9629. }
  9630. }
  9631. /* Now we've also updated encoder->new_crtc for all encoders. */
  9632. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9633. base.head) {
  9634. if (connector->new_encoder)
  9635. if (connector->new_encoder != connector->encoder)
  9636. connector->encoder = connector->new_encoder;
  9637. }
  9638. for_each_intel_crtc(dev, crtc) {
  9639. crtc->new_enabled = false;
  9640. for_each_intel_encoder(dev, encoder) {
  9641. if (encoder->new_crtc == crtc) {
  9642. crtc->new_enabled = true;
  9643. break;
  9644. }
  9645. }
  9646. if (crtc->new_enabled != crtc->base.enabled) {
  9647. DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
  9648. crtc->new_enabled ? "en" : "dis");
  9649. config->mode_changed = true;
  9650. }
  9651. if (crtc->new_enabled)
  9652. crtc->new_config = crtc->config;
  9653. else
  9654. crtc->new_config = NULL;
  9655. }
  9656. return 0;
  9657. }
  9658. static void disable_crtc_nofb(struct intel_crtc *crtc)
  9659. {
  9660. struct drm_device *dev = crtc->base.dev;
  9661. struct intel_encoder *encoder;
  9662. struct intel_connector *connector;
  9663. DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
  9664. pipe_name(crtc->pipe));
  9665. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  9666. if (connector->new_encoder &&
  9667. connector->new_encoder->new_crtc == crtc)
  9668. connector->new_encoder = NULL;
  9669. }
  9670. for_each_intel_encoder(dev, encoder) {
  9671. if (encoder->new_crtc == crtc)
  9672. encoder->new_crtc = NULL;
  9673. }
  9674. crtc->new_enabled = false;
  9675. crtc->new_config = NULL;
  9676. }
  9677. static int intel_crtc_set_config(struct drm_mode_set *set)
  9678. {
  9679. struct drm_device *dev;
  9680. struct drm_mode_set save_set;
  9681. struct intel_set_config *config;
  9682. struct intel_crtc_state *pipe_config;
  9683. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  9684. int ret;
  9685. BUG_ON(!set);
  9686. BUG_ON(!set->crtc);
  9687. BUG_ON(!set->crtc->helper_private);
  9688. /* Enforce sane interface api - has been abused by the fb helper. */
  9689. BUG_ON(!set->mode && set->fb);
  9690. BUG_ON(set->fb && set->num_connectors == 0);
  9691. if (set->fb) {
  9692. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  9693. set->crtc->base.id, set->fb->base.id,
  9694. (int)set->num_connectors, set->x, set->y);
  9695. } else {
  9696. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  9697. }
  9698. dev = set->crtc->dev;
  9699. ret = -ENOMEM;
  9700. config = kzalloc(sizeof(*config), GFP_KERNEL);
  9701. if (!config)
  9702. goto out_config;
  9703. ret = intel_set_config_save_state(dev, config);
  9704. if (ret)
  9705. goto out_config;
  9706. save_set.crtc = set->crtc;
  9707. save_set.mode = &set->crtc->mode;
  9708. save_set.x = set->crtc->x;
  9709. save_set.y = set->crtc->y;
  9710. save_set.fb = set->crtc->primary->fb;
  9711. /* Compute whether we need a full modeset, only an fb base update or no
  9712. * change at all. In the future we might also check whether only the
  9713. * mode changed, e.g. for LVDS where we only change the panel fitter in
  9714. * such cases. */
  9715. intel_set_config_compute_mode_changes(set, config);
  9716. ret = intel_modeset_stage_output_state(dev, set, config);
  9717. if (ret)
  9718. goto fail;
  9719. pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
  9720. set->fb,
  9721. &modeset_pipes,
  9722. &prepare_pipes,
  9723. &disable_pipes);
  9724. if (IS_ERR(pipe_config)) {
  9725. ret = PTR_ERR(pipe_config);
  9726. goto fail;
  9727. } else if (pipe_config) {
  9728. if (pipe_config->has_audio !=
  9729. to_intel_crtc(set->crtc)->config->has_audio)
  9730. config->mode_changed = true;
  9731. /*
  9732. * Note we have an issue here with infoframes: current code
  9733. * only updates them on the full mode set path per hw
  9734. * requirements. So here we should be checking for any
  9735. * required changes and forcing a mode set.
  9736. */
  9737. }
  9738. /* set_mode will free it in the mode_changed case */
  9739. if (!config->mode_changed)
  9740. kfree(pipe_config);
  9741. intel_update_pipe_size(to_intel_crtc(set->crtc));
  9742. if (config->mode_changed) {
  9743. ret = intel_set_mode_pipes(set->crtc, set->mode,
  9744. set->x, set->y, set->fb, pipe_config,
  9745. modeset_pipes, prepare_pipes,
  9746. disable_pipes);
  9747. } else if (config->fb_changed) {
  9748. struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
  9749. struct drm_plane *primary = set->crtc->primary;
  9750. int vdisplay, hdisplay;
  9751. drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
  9752. ret = primary->funcs->update_plane(primary, set->crtc, set->fb,
  9753. 0, 0, hdisplay, vdisplay,
  9754. set->x << 16, set->y << 16,
  9755. hdisplay << 16, vdisplay << 16);
  9756. /*
  9757. * We need to make sure the primary plane is re-enabled if it
  9758. * has previously been turned off.
  9759. */
  9760. if (!intel_crtc->primary_enabled && ret == 0) {
  9761. WARN_ON(!intel_crtc->active);
  9762. intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
  9763. }
  9764. /*
  9765. * In the fastboot case this may be our only check of the
  9766. * state after boot. It would be better to only do it on
  9767. * the first update, but we don't have a nice way of doing that
  9768. * (and really, set_config isn't used much for high freq page
  9769. * flipping, so increasing its cost here shouldn't be a big
  9770. * deal).
  9771. */
  9772. if (i915.fastboot && ret == 0)
  9773. intel_modeset_check_state(set->crtc->dev);
  9774. }
  9775. if (ret) {
  9776. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  9777. set->crtc->base.id, ret);
  9778. fail:
  9779. intel_set_config_restore_state(dev, config);
  9780. /*
  9781. * HACK: if the pipe was on, but we didn't have a framebuffer,
  9782. * force the pipe off to avoid oopsing in the modeset code
  9783. * due to fb==NULL. This should only happen during boot since
  9784. * we don't yet reconstruct the FB from the hardware state.
  9785. */
  9786. if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
  9787. disable_crtc_nofb(to_intel_crtc(save_set.crtc));
  9788. /* Try to restore the config */
  9789. if (config->mode_changed &&
  9790. intel_set_mode(save_set.crtc, save_set.mode,
  9791. save_set.x, save_set.y, save_set.fb))
  9792. DRM_ERROR("failed to restore config after modeset failure\n");
  9793. }
  9794. out_config:
  9795. intel_set_config_free(config);
  9796. return ret;
  9797. }
  9798. static const struct drm_crtc_funcs intel_crtc_funcs = {
  9799. .gamma_set = intel_crtc_gamma_set,
  9800. .set_config = intel_crtc_set_config,
  9801. .destroy = intel_crtc_destroy,
  9802. .page_flip = intel_crtc_page_flip,
  9803. .atomic_duplicate_state = intel_crtc_duplicate_state,
  9804. .atomic_destroy_state = intel_crtc_destroy_state,
  9805. };
  9806. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  9807. struct intel_shared_dpll *pll,
  9808. struct intel_dpll_hw_state *hw_state)
  9809. {
  9810. uint32_t val;
  9811. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  9812. return false;
  9813. val = I915_READ(PCH_DPLL(pll->id));
  9814. hw_state->dpll = val;
  9815. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  9816. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  9817. return val & DPLL_VCO_ENABLE;
  9818. }
  9819. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  9820. struct intel_shared_dpll *pll)
  9821. {
  9822. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  9823. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  9824. }
  9825. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  9826. struct intel_shared_dpll *pll)
  9827. {
  9828. /* PCH refclock must be enabled first */
  9829. ibx_assert_pch_refclk_enabled(dev_priv);
  9830. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  9831. /* Wait for the clocks to stabilize. */
  9832. POSTING_READ(PCH_DPLL(pll->id));
  9833. udelay(150);
  9834. /* The pixel multiplier can only be updated once the
  9835. * DPLL is enabled and the clocks are stable.
  9836. *
  9837. * So write it again.
  9838. */
  9839. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  9840. POSTING_READ(PCH_DPLL(pll->id));
  9841. udelay(200);
  9842. }
  9843. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  9844. struct intel_shared_dpll *pll)
  9845. {
  9846. struct drm_device *dev = dev_priv->dev;
  9847. struct intel_crtc *crtc;
  9848. /* Make sure no transcoder isn't still depending on us. */
  9849. for_each_intel_crtc(dev, crtc) {
  9850. if (intel_crtc_to_shared_dpll(crtc) == pll)
  9851. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  9852. }
  9853. I915_WRITE(PCH_DPLL(pll->id), 0);
  9854. POSTING_READ(PCH_DPLL(pll->id));
  9855. udelay(200);
  9856. }
  9857. static char *ibx_pch_dpll_names[] = {
  9858. "PCH DPLL A",
  9859. "PCH DPLL B",
  9860. };
  9861. static void ibx_pch_dpll_init(struct drm_device *dev)
  9862. {
  9863. struct drm_i915_private *dev_priv = dev->dev_private;
  9864. int i;
  9865. dev_priv->num_shared_dpll = 2;
  9866. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9867. dev_priv->shared_dplls[i].id = i;
  9868. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  9869. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  9870. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  9871. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  9872. dev_priv->shared_dplls[i].get_hw_state =
  9873. ibx_pch_dpll_get_hw_state;
  9874. }
  9875. }
  9876. static void intel_shared_dpll_init(struct drm_device *dev)
  9877. {
  9878. struct drm_i915_private *dev_priv = dev->dev_private;
  9879. if (HAS_DDI(dev))
  9880. intel_ddi_pll_init(dev);
  9881. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  9882. ibx_pch_dpll_init(dev);
  9883. else
  9884. dev_priv->num_shared_dpll = 0;
  9885. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  9886. }
  9887. /**
  9888. * intel_prepare_plane_fb - Prepare fb for usage on plane
  9889. * @plane: drm plane to prepare for
  9890. * @fb: framebuffer to prepare for presentation
  9891. *
  9892. * Prepares a framebuffer for usage on a display plane. Generally this
  9893. * involves pinning the underlying object and updating the frontbuffer tracking
  9894. * bits. Some older platforms need special physical address handling for
  9895. * cursor planes.
  9896. *
  9897. * Returns 0 on success, negative error code on failure.
  9898. */
  9899. int
  9900. intel_prepare_plane_fb(struct drm_plane *plane,
  9901. struct drm_framebuffer *fb)
  9902. {
  9903. struct drm_device *dev = plane->dev;
  9904. struct intel_plane *intel_plane = to_intel_plane(plane);
  9905. enum pipe pipe = intel_plane->pipe;
  9906. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9907. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  9908. unsigned frontbuffer_bits = 0;
  9909. int ret = 0;
  9910. if (!obj)
  9911. return 0;
  9912. switch (plane->type) {
  9913. case DRM_PLANE_TYPE_PRIMARY:
  9914. frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
  9915. break;
  9916. case DRM_PLANE_TYPE_CURSOR:
  9917. frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
  9918. break;
  9919. case DRM_PLANE_TYPE_OVERLAY:
  9920. frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
  9921. break;
  9922. }
  9923. mutex_lock(&dev->struct_mutex);
  9924. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  9925. INTEL_INFO(dev)->cursor_needs_physical) {
  9926. int align = IS_I830(dev) ? 16 * 1024 : 256;
  9927. ret = i915_gem_object_attach_phys(obj, align);
  9928. if (ret)
  9929. DRM_DEBUG_KMS("failed to attach phys object\n");
  9930. } else {
  9931. ret = intel_pin_and_fence_fb_obj(plane, fb, NULL);
  9932. }
  9933. if (ret == 0)
  9934. i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
  9935. mutex_unlock(&dev->struct_mutex);
  9936. return ret;
  9937. }
  9938. /**
  9939. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  9940. * @plane: drm plane to clean up for
  9941. * @fb: old framebuffer that was on plane
  9942. *
  9943. * Cleans up a framebuffer that has just been removed from a plane.
  9944. */
  9945. void
  9946. intel_cleanup_plane_fb(struct drm_plane *plane,
  9947. struct drm_framebuffer *fb)
  9948. {
  9949. struct drm_device *dev = plane->dev;
  9950. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9951. if (WARN_ON(!obj))
  9952. return;
  9953. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  9954. !INTEL_INFO(dev)->cursor_needs_physical) {
  9955. mutex_lock(&dev->struct_mutex);
  9956. intel_unpin_fb_obj(obj);
  9957. mutex_unlock(&dev->struct_mutex);
  9958. }
  9959. }
  9960. static int
  9961. intel_check_primary_plane(struct drm_plane *plane,
  9962. struct intel_plane_state *state)
  9963. {
  9964. struct drm_device *dev = plane->dev;
  9965. struct drm_i915_private *dev_priv = dev->dev_private;
  9966. struct drm_crtc *crtc = state->base.crtc;
  9967. struct intel_crtc *intel_crtc;
  9968. struct drm_framebuffer *fb = state->base.fb;
  9969. struct drm_rect *dest = &state->dst;
  9970. struct drm_rect *src = &state->src;
  9971. const struct drm_rect *clip = &state->clip;
  9972. int ret;
  9973. crtc = crtc ? crtc : plane->crtc;
  9974. intel_crtc = to_intel_crtc(crtc);
  9975. ret = drm_plane_helper_check_update(plane, crtc, fb,
  9976. src, dest, clip,
  9977. DRM_PLANE_HELPER_NO_SCALING,
  9978. DRM_PLANE_HELPER_NO_SCALING,
  9979. false, true, &state->visible);
  9980. if (ret)
  9981. return ret;
  9982. if (intel_crtc->active) {
  9983. intel_crtc->atomic.wait_for_flips = true;
  9984. /*
  9985. * FBC does not work on some platforms for rotated
  9986. * planes, so disable it when rotation is not 0 and
  9987. * update it when rotation is set back to 0.
  9988. *
  9989. * FIXME: This is redundant with the fbc update done in
  9990. * the primary plane enable function except that that
  9991. * one is done too late. We eventually need to unify
  9992. * this.
  9993. */
  9994. if (intel_crtc->primary_enabled &&
  9995. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  9996. dev_priv->fbc.crtc == intel_crtc &&
  9997. state->base.rotation != BIT(DRM_ROTATE_0)) {
  9998. intel_crtc->atomic.disable_fbc = true;
  9999. }
  10000. if (state->visible) {
  10001. /*
  10002. * BDW signals flip done immediately if the plane
  10003. * is disabled, even if the plane enable is already
  10004. * armed to occur at the next vblank :(
  10005. */
  10006. if (IS_BROADWELL(dev) && !intel_crtc->primary_enabled)
  10007. intel_crtc->atomic.wait_vblank = true;
  10008. }
  10009. intel_crtc->atomic.fb_bits |=
  10010. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  10011. intel_crtc->atomic.update_fbc = true;
  10012. }
  10013. return 0;
  10014. }
  10015. static void
  10016. intel_commit_primary_plane(struct drm_plane *plane,
  10017. struct intel_plane_state *state)
  10018. {
  10019. struct drm_crtc *crtc = state->base.crtc;
  10020. struct drm_framebuffer *fb = state->base.fb;
  10021. struct drm_device *dev = plane->dev;
  10022. struct drm_i915_private *dev_priv = dev->dev_private;
  10023. struct intel_crtc *intel_crtc;
  10024. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10025. struct intel_plane *intel_plane = to_intel_plane(plane);
  10026. struct drm_rect *src = &state->src;
  10027. crtc = crtc ? crtc : plane->crtc;
  10028. intel_crtc = to_intel_crtc(crtc);
  10029. plane->fb = fb;
  10030. crtc->x = src->x1 >> 16;
  10031. crtc->y = src->y1 >> 16;
  10032. intel_plane->obj = obj;
  10033. if (intel_crtc->active) {
  10034. if (state->visible) {
  10035. /* FIXME: kill this fastboot hack */
  10036. intel_update_pipe_size(intel_crtc);
  10037. intel_crtc->primary_enabled = true;
  10038. dev_priv->display.update_primary_plane(crtc, plane->fb,
  10039. crtc->x, crtc->y);
  10040. } else {
  10041. /*
  10042. * If clipping results in a non-visible primary plane,
  10043. * we'll disable the primary plane. Note that this is
  10044. * a bit different than what happens if userspace
  10045. * explicitly disables the plane by passing fb=0
  10046. * because plane->fb still gets set and pinned.
  10047. */
  10048. intel_disable_primary_hw_plane(plane, crtc);
  10049. }
  10050. }
  10051. }
  10052. static void intel_begin_crtc_commit(struct drm_crtc *crtc)
  10053. {
  10054. struct drm_device *dev = crtc->dev;
  10055. struct drm_i915_private *dev_priv = dev->dev_private;
  10056. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10057. struct intel_plane *intel_plane;
  10058. struct drm_plane *p;
  10059. unsigned fb_bits = 0;
  10060. /* Track fb's for any planes being disabled */
  10061. list_for_each_entry(p, &dev->mode_config.plane_list, head) {
  10062. intel_plane = to_intel_plane(p);
  10063. if (intel_crtc->atomic.disabled_planes &
  10064. (1 << drm_plane_index(p))) {
  10065. switch (p->type) {
  10066. case DRM_PLANE_TYPE_PRIMARY:
  10067. fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
  10068. break;
  10069. case DRM_PLANE_TYPE_CURSOR:
  10070. fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
  10071. break;
  10072. case DRM_PLANE_TYPE_OVERLAY:
  10073. fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
  10074. break;
  10075. }
  10076. mutex_lock(&dev->struct_mutex);
  10077. i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
  10078. mutex_unlock(&dev->struct_mutex);
  10079. }
  10080. }
  10081. if (intel_crtc->atomic.wait_for_flips)
  10082. intel_crtc_wait_for_pending_flips(crtc);
  10083. if (intel_crtc->atomic.disable_fbc)
  10084. intel_fbc_disable(dev);
  10085. if (intel_crtc->atomic.pre_disable_primary)
  10086. intel_pre_disable_primary(crtc);
  10087. if (intel_crtc->atomic.update_wm)
  10088. intel_update_watermarks(crtc);
  10089. intel_runtime_pm_get(dev_priv);
  10090. /* Perform vblank evasion around commit operation */
  10091. if (intel_crtc->active)
  10092. intel_crtc->atomic.evade =
  10093. intel_pipe_update_start(intel_crtc,
  10094. &intel_crtc->atomic.start_vbl_count);
  10095. }
  10096. static void intel_finish_crtc_commit(struct drm_crtc *crtc)
  10097. {
  10098. struct drm_device *dev = crtc->dev;
  10099. struct drm_i915_private *dev_priv = dev->dev_private;
  10100. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10101. struct drm_plane *p;
  10102. if (intel_crtc->atomic.evade)
  10103. intel_pipe_update_end(intel_crtc,
  10104. intel_crtc->atomic.start_vbl_count);
  10105. intel_runtime_pm_put(dev_priv);
  10106. if (intel_crtc->atomic.wait_vblank)
  10107. intel_wait_for_vblank(dev, intel_crtc->pipe);
  10108. intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
  10109. if (intel_crtc->atomic.update_fbc) {
  10110. mutex_lock(&dev->struct_mutex);
  10111. intel_fbc_update(dev);
  10112. mutex_unlock(&dev->struct_mutex);
  10113. }
  10114. if (intel_crtc->atomic.post_enable_primary)
  10115. intel_post_enable_primary(crtc);
  10116. drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
  10117. if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
  10118. intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
  10119. false, false);
  10120. memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
  10121. }
  10122. /**
  10123. * intel_plane_destroy - destroy a plane
  10124. * @plane: plane to destroy
  10125. *
  10126. * Common destruction function for all types of planes (primary, cursor,
  10127. * sprite).
  10128. */
  10129. void intel_plane_destroy(struct drm_plane *plane)
  10130. {
  10131. struct intel_plane *intel_plane = to_intel_plane(plane);
  10132. drm_plane_cleanup(plane);
  10133. kfree(intel_plane);
  10134. }
  10135. const struct drm_plane_funcs intel_plane_funcs = {
  10136. .update_plane = drm_atomic_helper_update_plane,
  10137. .disable_plane = drm_atomic_helper_disable_plane,
  10138. .destroy = intel_plane_destroy,
  10139. .set_property = drm_atomic_helper_plane_set_property,
  10140. .atomic_get_property = intel_plane_atomic_get_property,
  10141. .atomic_set_property = intel_plane_atomic_set_property,
  10142. .atomic_duplicate_state = intel_plane_duplicate_state,
  10143. .atomic_destroy_state = intel_plane_destroy_state,
  10144. };
  10145. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  10146. int pipe)
  10147. {
  10148. struct intel_plane *primary;
  10149. struct intel_plane_state *state;
  10150. const uint32_t *intel_primary_formats;
  10151. int num_formats;
  10152. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  10153. if (primary == NULL)
  10154. return NULL;
  10155. state = intel_create_plane_state(&primary->base);
  10156. if (!state) {
  10157. kfree(primary);
  10158. return NULL;
  10159. }
  10160. primary->base.state = &state->base;
  10161. primary->can_scale = false;
  10162. primary->max_downscale = 1;
  10163. primary->pipe = pipe;
  10164. primary->plane = pipe;
  10165. primary->check_plane = intel_check_primary_plane;
  10166. primary->commit_plane = intel_commit_primary_plane;
  10167. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  10168. primary->plane = !pipe;
  10169. if (INTEL_INFO(dev)->gen <= 3) {
  10170. intel_primary_formats = intel_primary_formats_gen2;
  10171. num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
  10172. } else {
  10173. intel_primary_formats = intel_primary_formats_gen4;
  10174. num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
  10175. }
  10176. drm_universal_plane_init(dev, &primary->base, 0,
  10177. &intel_plane_funcs,
  10178. intel_primary_formats, num_formats,
  10179. DRM_PLANE_TYPE_PRIMARY);
  10180. if (INTEL_INFO(dev)->gen >= 4) {
  10181. if (!dev->mode_config.rotation_property)
  10182. dev->mode_config.rotation_property =
  10183. drm_mode_create_rotation_property(dev,
  10184. BIT(DRM_ROTATE_0) |
  10185. BIT(DRM_ROTATE_180));
  10186. if (dev->mode_config.rotation_property)
  10187. drm_object_attach_property(&primary->base.base,
  10188. dev->mode_config.rotation_property,
  10189. state->base.rotation);
  10190. }
  10191. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  10192. return &primary->base;
  10193. }
  10194. static int
  10195. intel_check_cursor_plane(struct drm_plane *plane,
  10196. struct intel_plane_state *state)
  10197. {
  10198. struct drm_crtc *crtc = state->base.crtc;
  10199. struct drm_device *dev = plane->dev;
  10200. struct drm_framebuffer *fb = state->base.fb;
  10201. struct drm_rect *dest = &state->dst;
  10202. struct drm_rect *src = &state->src;
  10203. const struct drm_rect *clip = &state->clip;
  10204. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10205. struct intel_crtc *intel_crtc;
  10206. unsigned stride;
  10207. int ret;
  10208. crtc = crtc ? crtc : plane->crtc;
  10209. intel_crtc = to_intel_crtc(crtc);
  10210. ret = drm_plane_helper_check_update(plane, crtc, fb,
  10211. src, dest, clip,
  10212. DRM_PLANE_HELPER_NO_SCALING,
  10213. DRM_PLANE_HELPER_NO_SCALING,
  10214. true, true, &state->visible);
  10215. if (ret)
  10216. return ret;
  10217. /* if we want to turn off the cursor ignore width and height */
  10218. if (!obj)
  10219. goto finish;
  10220. /* Check for which cursor types we support */
  10221. if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
  10222. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  10223. state->base.crtc_w, state->base.crtc_h);
  10224. return -EINVAL;
  10225. }
  10226. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  10227. if (obj->base.size < stride * state->base.crtc_h) {
  10228. DRM_DEBUG_KMS("buffer is too small\n");
  10229. return -ENOMEM;
  10230. }
  10231. if (fb == crtc->cursor->fb)
  10232. return 0;
  10233. /* we only need to pin inside GTT if cursor is non-phy */
  10234. mutex_lock(&dev->struct_mutex);
  10235. if (!INTEL_INFO(dev)->cursor_needs_physical && obj->tiling_mode) {
  10236. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  10237. ret = -EINVAL;
  10238. }
  10239. mutex_unlock(&dev->struct_mutex);
  10240. finish:
  10241. if (intel_crtc->active) {
  10242. if (intel_crtc->cursor_width != state->base.crtc_w)
  10243. intel_crtc->atomic.update_wm = true;
  10244. intel_crtc->atomic.fb_bits |=
  10245. INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
  10246. }
  10247. return ret;
  10248. }
  10249. static void
  10250. intel_commit_cursor_plane(struct drm_plane *plane,
  10251. struct intel_plane_state *state)
  10252. {
  10253. struct drm_crtc *crtc = state->base.crtc;
  10254. struct drm_device *dev = plane->dev;
  10255. struct intel_crtc *intel_crtc;
  10256. struct intel_plane *intel_plane = to_intel_plane(plane);
  10257. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  10258. uint32_t addr;
  10259. crtc = crtc ? crtc : plane->crtc;
  10260. intel_crtc = to_intel_crtc(crtc);
  10261. plane->fb = state->base.fb;
  10262. crtc->cursor_x = state->base.crtc_x;
  10263. crtc->cursor_y = state->base.crtc_y;
  10264. intel_plane->obj = obj;
  10265. if (intel_crtc->cursor_bo == obj)
  10266. goto update;
  10267. if (!obj)
  10268. addr = 0;
  10269. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  10270. addr = i915_gem_obj_ggtt_offset(obj);
  10271. else
  10272. addr = obj->phys_handle->busaddr;
  10273. intel_crtc->cursor_addr = addr;
  10274. intel_crtc->cursor_bo = obj;
  10275. update:
  10276. intel_crtc->cursor_width = state->base.crtc_w;
  10277. intel_crtc->cursor_height = state->base.crtc_h;
  10278. if (intel_crtc->active)
  10279. intel_crtc_update_cursor(crtc, state->visible);
  10280. }
  10281. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  10282. int pipe)
  10283. {
  10284. struct intel_plane *cursor;
  10285. struct intel_plane_state *state;
  10286. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  10287. if (cursor == NULL)
  10288. return NULL;
  10289. state = intel_create_plane_state(&cursor->base);
  10290. if (!state) {
  10291. kfree(cursor);
  10292. return NULL;
  10293. }
  10294. cursor->base.state = &state->base;
  10295. cursor->can_scale = false;
  10296. cursor->max_downscale = 1;
  10297. cursor->pipe = pipe;
  10298. cursor->plane = pipe;
  10299. cursor->check_plane = intel_check_cursor_plane;
  10300. cursor->commit_plane = intel_commit_cursor_plane;
  10301. drm_universal_plane_init(dev, &cursor->base, 0,
  10302. &intel_plane_funcs,
  10303. intel_cursor_formats,
  10304. ARRAY_SIZE(intel_cursor_formats),
  10305. DRM_PLANE_TYPE_CURSOR);
  10306. if (INTEL_INFO(dev)->gen >= 4) {
  10307. if (!dev->mode_config.rotation_property)
  10308. dev->mode_config.rotation_property =
  10309. drm_mode_create_rotation_property(dev,
  10310. BIT(DRM_ROTATE_0) |
  10311. BIT(DRM_ROTATE_180));
  10312. if (dev->mode_config.rotation_property)
  10313. drm_object_attach_property(&cursor->base.base,
  10314. dev->mode_config.rotation_property,
  10315. state->base.rotation);
  10316. }
  10317. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  10318. return &cursor->base;
  10319. }
  10320. static void intel_crtc_init(struct drm_device *dev, int pipe)
  10321. {
  10322. struct drm_i915_private *dev_priv = dev->dev_private;
  10323. struct intel_crtc *intel_crtc;
  10324. struct intel_crtc_state *crtc_state = NULL;
  10325. struct drm_plane *primary = NULL;
  10326. struct drm_plane *cursor = NULL;
  10327. int i, ret;
  10328. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  10329. if (intel_crtc == NULL)
  10330. return;
  10331. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  10332. if (!crtc_state)
  10333. goto fail;
  10334. intel_crtc_set_state(intel_crtc, crtc_state);
  10335. primary = intel_primary_plane_create(dev, pipe);
  10336. if (!primary)
  10337. goto fail;
  10338. cursor = intel_cursor_plane_create(dev, pipe);
  10339. if (!cursor)
  10340. goto fail;
  10341. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  10342. cursor, &intel_crtc_funcs);
  10343. if (ret)
  10344. goto fail;
  10345. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  10346. for (i = 0; i < 256; i++) {
  10347. intel_crtc->lut_r[i] = i;
  10348. intel_crtc->lut_g[i] = i;
  10349. intel_crtc->lut_b[i] = i;
  10350. }
  10351. /*
  10352. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  10353. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  10354. */
  10355. intel_crtc->pipe = pipe;
  10356. intel_crtc->plane = pipe;
  10357. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  10358. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  10359. intel_crtc->plane = !pipe;
  10360. }
  10361. intel_crtc->cursor_base = ~0;
  10362. intel_crtc->cursor_cntl = ~0;
  10363. intel_crtc->cursor_size = ~0;
  10364. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  10365. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  10366. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  10367. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  10368. INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
  10369. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  10370. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  10371. return;
  10372. fail:
  10373. if (primary)
  10374. drm_plane_cleanup(primary);
  10375. if (cursor)
  10376. drm_plane_cleanup(cursor);
  10377. kfree(crtc_state);
  10378. kfree(intel_crtc);
  10379. }
  10380. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  10381. {
  10382. struct drm_encoder *encoder = connector->base.encoder;
  10383. struct drm_device *dev = connector->base.dev;
  10384. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  10385. if (!encoder || WARN_ON(!encoder->crtc))
  10386. return INVALID_PIPE;
  10387. return to_intel_crtc(encoder->crtc)->pipe;
  10388. }
  10389. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  10390. struct drm_file *file)
  10391. {
  10392. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  10393. struct drm_crtc *drmmode_crtc;
  10394. struct intel_crtc *crtc;
  10395. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  10396. return -ENODEV;
  10397. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  10398. if (!drmmode_crtc) {
  10399. DRM_ERROR("no such CRTC id\n");
  10400. return -ENOENT;
  10401. }
  10402. crtc = to_intel_crtc(drmmode_crtc);
  10403. pipe_from_crtc_id->pipe = crtc->pipe;
  10404. return 0;
  10405. }
  10406. static int intel_encoder_clones(struct intel_encoder *encoder)
  10407. {
  10408. struct drm_device *dev = encoder->base.dev;
  10409. struct intel_encoder *source_encoder;
  10410. int index_mask = 0;
  10411. int entry = 0;
  10412. for_each_intel_encoder(dev, source_encoder) {
  10413. if (encoders_cloneable(encoder, source_encoder))
  10414. index_mask |= (1 << entry);
  10415. entry++;
  10416. }
  10417. return index_mask;
  10418. }
  10419. static bool has_edp_a(struct drm_device *dev)
  10420. {
  10421. struct drm_i915_private *dev_priv = dev->dev_private;
  10422. if (!IS_MOBILE(dev))
  10423. return false;
  10424. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  10425. return false;
  10426. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  10427. return false;
  10428. return true;
  10429. }
  10430. static bool intel_crt_present(struct drm_device *dev)
  10431. {
  10432. struct drm_i915_private *dev_priv = dev->dev_private;
  10433. if (INTEL_INFO(dev)->gen >= 9)
  10434. return false;
  10435. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  10436. return false;
  10437. if (IS_CHERRYVIEW(dev))
  10438. return false;
  10439. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  10440. return false;
  10441. return true;
  10442. }
  10443. static void intel_setup_outputs(struct drm_device *dev)
  10444. {
  10445. struct drm_i915_private *dev_priv = dev->dev_private;
  10446. struct intel_encoder *encoder;
  10447. struct drm_connector *connector;
  10448. bool dpd_is_edp = false;
  10449. intel_lvds_init(dev);
  10450. if (intel_crt_present(dev))
  10451. intel_crt_init(dev);
  10452. if (HAS_DDI(dev)) {
  10453. int found;
  10454. /* Haswell uses DDI functions to detect digital outputs */
  10455. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  10456. /* DDI A only supports eDP */
  10457. if (found)
  10458. intel_ddi_init(dev, PORT_A);
  10459. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  10460. * register */
  10461. found = I915_READ(SFUSE_STRAP);
  10462. if (found & SFUSE_STRAP_DDIB_DETECTED)
  10463. intel_ddi_init(dev, PORT_B);
  10464. if (found & SFUSE_STRAP_DDIC_DETECTED)
  10465. intel_ddi_init(dev, PORT_C);
  10466. if (found & SFUSE_STRAP_DDID_DETECTED)
  10467. intel_ddi_init(dev, PORT_D);
  10468. } else if (HAS_PCH_SPLIT(dev)) {
  10469. int found;
  10470. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  10471. if (has_edp_a(dev))
  10472. intel_dp_init(dev, DP_A, PORT_A);
  10473. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  10474. /* PCH SDVOB multiplex with HDMIB */
  10475. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  10476. if (!found)
  10477. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  10478. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  10479. intel_dp_init(dev, PCH_DP_B, PORT_B);
  10480. }
  10481. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  10482. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  10483. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  10484. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  10485. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  10486. intel_dp_init(dev, PCH_DP_C, PORT_C);
  10487. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  10488. intel_dp_init(dev, PCH_DP_D, PORT_D);
  10489. } else if (IS_VALLEYVIEW(dev)) {
  10490. /*
  10491. * The DP_DETECTED bit is the latched state of the DDC
  10492. * SDA pin at boot. However since eDP doesn't require DDC
  10493. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  10494. * eDP ports may have been muxed to an alternate function.
  10495. * Thus we can't rely on the DP_DETECTED bit alone to detect
  10496. * eDP ports. Consult the VBT as well as DP_DETECTED to
  10497. * detect eDP ports.
  10498. */
  10499. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
  10500. !intel_dp_is_edp(dev, PORT_B))
  10501. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  10502. PORT_B);
  10503. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  10504. intel_dp_is_edp(dev, PORT_B))
  10505. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  10506. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
  10507. !intel_dp_is_edp(dev, PORT_C))
  10508. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  10509. PORT_C);
  10510. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  10511. intel_dp_is_edp(dev, PORT_C))
  10512. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  10513. if (IS_CHERRYVIEW(dev)) {
  10514. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  10515. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  10516. PORT_D);
  10517. /* eDP not supported on port D, so don't check VBT */
  10518. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  10519. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  10520. }
  10521. intel_dsi_init(dev);
  10522. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  10523. bool found = false;
  10524. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10525. DRM_DEBUG_KMS("probing SDVOB\n");
  10526. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  10527. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  10528. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  10529. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  10530. }
  10531. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  10532. intel_dp_init(dev, DP_B, PORT_B);
  10533. }
  10534. /* Before G4X SDVOC doesn't have its own detect register */
  10535. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10536. DRM_DEBUG_KMS("probing SDVOC\n");
  10537. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  10538. }
  10539. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  10540. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  10541. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  10542. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  10543. }
  10544. if (SUPPORTS_INTEGRATED_DP(dev))
  10545. intel_dp_init(dev, DP_C, PORT_C);
  10546. }
  10547. if (SUPPORTS_INTEGRATED_DP(dev) &&
  10548. (I915_READ(DP_D) & DP_DETECTED))
  10549. intel_dp_init(dev, DP_D, PORT_D);
  10550. } else if (IS_GEN2(dev))
  10551. intel_dvo_init(dev);
  10552. if (SUPPORTS_TV(dev))
  10553. intel_tv_init(dev);
  10554. /*
  10555. * FIXME: We don't have full atomic support yet, but we want to be
  10556. * able to enable/test plane updates via the atomic interface in the
  10557. * meantime. However as soon as we flip DRIVER_ATOMIC on, the DRM core
  10558. * will take some atomic codepaths to lookup properties during
  10559. * drmModeGetConnector() that unconditionally dereference
  10560. * connector->state.
  10561. *
  10562. * We create a dummy connector state here for each connector to ensure
  10563. * the DRM core doesn't try to dereference a NULL connector->state.
  10564. * The actual connector properties will never be updated or contain
  10565. * useful information, but since we're doing this specifically for
  10566. * testing/debug of the plane operations (and only when a specific
  10567. * kernel module option is given), that shouldn't really matter.
  10568. *
  10569. * Once atomic support for crtc's + connectors lands, this loop should
  10570. * be removed since we'll be setting up real connector state, which
  10571. * will contain Intel-specific properties.
  10572. */
  10573. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  10574. list_for_each_entry(connector,
  10575. &dev->mode_config.connector_list,
  10576. head) {
  10577. if (!WARN_ON(connector->state)) {
  10578. connector->state =
  10579. kzalloc(sizeof(*connector->state),
  10580. GFP_KERNEL);
  10581. }
  10582. }
  10583. }
  10584. intel_psr_init(dev);
  10585. for_each_intel_encoder(dev, encoder) {
  10586. encoder->base.possible_crtcs = encoder->crtc_mask;
  10587. encoder->base.possible_clones =
  10588. intel_encoder_clones(encoder);
  10589. }
  10590. intel_init_pch_refclk(dev);
  10591. drm_helper_move_panel_connectors_to_head(dev);
  10592. }
  10593. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  10594. {
  10595. struct drm_device *dev = fb->dev;
  10596. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10597. drm_framebuffer_cleanup(fb);
  10598. mutex_lock(&dev->struct_mutex);
  10599. WARN_ON(!intel_fb->obj->framebuffer_references--);
  10600. drm_gem_object_unreference(&intel_fb->obj->base);
  10601. mutex_unlock(&dev->struct_mutex);
  10602. kfree(intel_fb);
  10603. }
  10604. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  10605. struct drm_file *file,
  10606. unsigned int *handle)
  10607. {
  10608. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10609. struct drm_i915_gem_object *obj = intel_fb->obj;
  10610. return drm_gem_handle_create(file, &obj->base, handle);
  10611. }
  10612. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  10613. .destroy = intel_user_framebuffer_destroy,
  10614. .create_handle = intel_user_framebuffer_create_handle,
  10615. };
  10616. static int intel_framebuffer_init(struct drm_device *dev,
  10617. struct intel_framebuffer *intel_fb,
  10618. struct drm_mode_fb_cmd2 *mode_cmd,
  10619. struct drm_i915_gem_object *obj)
  10620. {
  10621. int aligned_height;
  10622. int pitch_limit;
  10623. int ret;
  10624. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  10625. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  10626. /* Enforce that fb modifier and tiling mode match, but only for
  10627. * X-tiled. This is needed for FBC. */
  10628. if (!!(obj->tiling_mode == I915_TILING_X) !=
  10629. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  10630. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  10631. return -EINVAL;
  10632. }
  10633. } else {
  10634. if (obj->tiling_mode == I915_TILING_X)
  10635. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  10636. else if (obj->tiling_mode == I915_TILING_Y) {
  10637. DRM_DEBUG("No Y tiling for legacy addfb\n");
  10638. return -EINVAL;
  10639. }
  10640. }
  10641. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_Y_TILED) {
  10642. DRM_DEBUG("hardware does not support tiling Y\n");
  10643. return -EINVAL;
  10644. }
  10645. if (mode_cmd->pitches[0] & 63) {
  10646. DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
  10647. mode_cmd->pitches[0]);
  10648. return -EINVAL;
  10649. }
  10650. if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
  10651. pitch_limit = 32*1024;
  10652. } else if (INTEL_INFO(dev)->gen >= 4) {
  10653. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)
  10654. pitch_limit = 16*1024;
  10655. else
  10656. pitch_limit = 32*1024;
  10657. } else if (INTEL_INFO(dev)->gen >= 3) {
  10658. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)
  10659. pitch_limit = 8*1024;
  10660. else
  10661. pitch_limit = 16*1024;
  10662. } else
  10663. /* XXX DSPC is limited to 4k tiled */
  10664. pitch_limit = 8*1024;
  10665. if (mode_cmd->pitches[0] > pitch_limit) {
  10666. DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
  10667. mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED ?
  10668. "tiled" : "linear",
  10669. mode_cmd->pitches[0], pitch_limit);
  10670. return -EINVAL;
  10671. }
  10672. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  10673. mode_cmd->pitches[0] != obj->stride) {
  10674. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  10675. mode_cmd->pitches[0], obj->stride);
  10676. return -EINVAL;
  10677. }
  10678. /* Reject formats not supported by any plane early. */
  10679. switch (mode_cmd->pixel_format) {
  10680. case DRM_FORMAT_C8:
  10681. case DRM_FORMAT_RGB565:
  10682. case DRM_FORMAT_XRGB8888:
  10683. case DRM_FORMAT_ARGB8888:
  10684. break;
  10685. case DRM_FORMAT_XRGB1555:
  10686. case DRM_FORMAT_ARGB1555:
  10687. if (INTEL_INFO(dev)->gen > 3) {
  10688. DRM_DEBUG("unsupported pixel format: %s\n",
  10689. drm_get_format_name(mode_cmd->pixel_format));
  10690. return -EINVAL;
  10691. }
  10692. break;
  10693. case DRM_FORMAT_XBGR8888:
  10694. case DRM_FORMAT_ABGR8888:
  10695. case DRM_FORMAT_XRGB2101010:
  10696. case DRM_FORMAT_ARGB2101010:
  10697. case DRM_FORMAT_XBGR2101010:
  10698. case DRM_FORMAT_ABGR2101010:
  10699. if (INTEL_INFO(dev)->gen < 4) {
  10700. DRM_DEBUG("unsupported pixel format: %s\n",
  10701. drm_get_format_name(mode_cmd->pixel_format));
  10702. return -EINVAL;
  10703. }
  10704. break;
  10705. case DRM_FORMAT_YUYV:
  10706. case DRM_FORMAT_UYVY:
  10707. case DRM_FORMAT_YVYU:
  10708. case DRM_FORMAT_VYUY:
  10709. if (INTEL_INFO(dev)->gen < 5) {
  10710. DRM_DEBUG("unsupported pixel format: %s\n",
  10711. drm_get_format_name(mode_cmd->pixel_format));
  10712. return -EINVAL;
  10713. }
  10714. break;
  10715. default:
  10716. DRM_DEBUG("unsupported pixel format: %s\n",
  10717. drm_get_format_name(mode_cmd->pixel_format));
  10718. return -EINVAL;
  10719. }
  10720. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  10721. if (mode_cmd->offsets[0] != 0)
  10722. return -EINVAL;
  10723. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  10724. mode_cmd->pixel_format,
  10725. mode_cmd->modifier[0]);
  10726. /* FIXME drm helper for size checks (especially planar formats)? */
  10727. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  10728. return -EINVAL;
  10729. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  10730. intel_fb->obj = obj;
  10731. intel_fb->obj->framebuffer_references++;
  10732. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  10733. if (ret) {
  10734. DRM_ERROR("framebuffer init failed %d\n", ret);
  10735. return ret;
  10736. }
  10737. return 0;
  10738. }
  10739. static struct drm_framebuffer *
  10740. intel_user_framebuffer_create(struct drm_device *dev,
  10741. struct drm_file *filp,
  10742. struct drm_mode_fb_cmd2 *mode_cmd)
  10743. {
  10744. struct drm_i915_gem_object *obj;
  10745. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  10746. mode_cmd->handles[0]));
  10747. if (&obj->base == NULL)
  10748. return ERR_PTR(-ENOENT);
  10749. return intel_framebuffer_create(dev, mode_cmd, obj);
  10750. }
  10751. #ifndef CONFIG_DRM_I915_FBDEV
  10752. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  10753. {
  10754. }
  10755. #endif
  10756. static const struct drm_mode_config_funcs intel_mode_funcs = {
  10757. .fb_create = intel_user_framebuffer_create,
  10758. .output_poll_changed = intel_fbdev_output_poll_changed,
  10759. .atomic_check = intel_atomic_check,
  10760. .atomic_commit = intel_atomic_commit,
  10761. };
  10762. /* Set up chip specific display functions */
  10763. static void intel_init_display(struct drm_device *dev)
  10764. {
  10765. struct drm_i915_private *dev_priv = dev->dev_private;
  10766. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  10767. dev_priv->display.find_dpll = g4x_find_best_dpll;
  10768. else if (IS_CHERRYVIEW(dev))
  10769. dev_priv->display.find_dpll = chv_find_best_dpll;
  10770. else if (IS_VALLEYVIEW(dev))
  10771. dev_priv->display.find_dpll = vlv_find_best_dpll;
  10772. else if (IS_PINEVIEW(dev))
  10773. dev_priv->display.find_dpll = pnv_find_best_dpll;
  10774. else
  10775. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  10776. if (INTEL_INFO(dev)->gen >= 9) {
  10777. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  10778. dev_priv->display.get_initial_plane_config =
  10779. skylake_get_initial_plane_config;
  10780. dev_priv->display.crtc_compute_clock =
  10781. haswell_crtc_compute_clock;
  10782. dev_priv->display.crtc_enable = haswell_crtc_enable;
  10783. dev_priv->display.crtc_disable = haswell_crtc_disable;
  10784. dev_priv->display.off = ironlake_crtc_off;
  10785. dev_priv->display.update_primary_plane =
  10786. skylake_update_primary_plane;
  10787. } else if (HAS_DDI(dev)) {
  10788. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  10789. dev_priv->display.get_initial_plane_config =
  10790. ironlake_get_initial_plane_config;
  10791. dev_priv->display.crtc_compute_clock =
  10792. haswell_crtc_compute_clock;
  10793. dev_priv->display.crtc_enable = haswell_crtc_enable;
  10794. dev_priv->display.crtc_disable = haswell_crtc_disable;
  10795. dev_priv->display.off = ironlake_crtc_off;
  10796. dev_priv->display.update_primary_plane =
  10797. ironlake_update_primary_plane;
  10798. } else if (HAS_PCH_SPLIT(dev)) {
  10799. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  10800. dev_priv->display.get_initial_plane_config =
  10801. ironlake_get_initial_plane_config;
  10802. dev_priv->display.crtc_compute_clock =
  10803. ironlake_crtc_compute_clock;
  10804. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  10805. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  10806. dev_priv->display.off = ironlake_crtc_off;
  10807. dev_priv->display.update_primary_plane =
  10808. ironlake_update_primary_plane;
  10809. } else if (IS_VALLEYVIEW(dev)) {
  10810. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10811. dev_priv->display.get_initial_plane_config =
  10812. i9xx_get_initial_plane_config;
  10813. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  10814. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  10815. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10816. dev_priv->display.off = i9xx_crtc_off;
  10817. dev_priv->display.update_primary_plane =
  10818. i9xx_update_primary_plane;
  10819. } else {
  10820. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10821. dev_priv->display.get_initial_plane_config =
  10822. i9xx_get_initial_plane_config;
  10823. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  10824. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  10825. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10826. dev_priv->display.off = i9xx_crtc_off;
  10827. dev_priv->display.update_primary_plane =
  10828. i9xx_update_primary_plane;
  10829. }
  10830. /* Returns the core display clock speed */
  10831. if (IS_VALLEYVIEW(dev))
  10832. dev_priv->display.get_display_clock_speed =
  10833. valleyview_get_display_clock_speed;
  10834. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  10835. dev_priv->display.get_display_clock_speed =
  10836. i945_get_display_clock_speed;
  10837. else if (IS_I915G(dev))
  10838. dev_priv->display.get_display_clock_speed =
  10839. i915_get_display_clock_speed;
  10840. else if (IS_I945GM(dev) || IS_845G(dev))
  10841. dev_priv->display.get_display_clock_speed =
  10842. i9xx_misc_get_display_clock_speed;
  10843. else if (IS_PINEVIEW(dev))
  10844. dev_priv->display.get_display_clock_speed =
  10845. pnv_get_display_clock_speed;
  10846. else if (IS_I915GM(dev))
  10847. dev_priv->display.get_display_clock_speed =
  10848. i915gm_get_display_clock_speed;
  10849. else if (IS_I865G(dev))
  10850. dev_priv->display.get_display_clock_speed =
  10851. i865_get_display_clock_speed;
  10852. else if (IS_I85X(dev))
  10853. dev_priv->display.get_display_clock_speed =
  10854. i855_get_display_clock_speed;
  10855. else /* 852, 830 */
  10856. dev_priv->display.get_display_clock_speed =
  10857. i830_get_display_clock_speed;
  10858. if (IS_GEN5(dev)) {
  10859. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  10860. } else if (IS_GEN6(dev)) {
  10861. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  10862. } else if (IS_IVYBRIDGE(dev)) {
  10863. /* FIXME: detect B0+ stepping and use auto training */
  10864. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  10865. dev_priv->display.modeset_global_resources =
  10866. ivb_modeset_global_resources;
  10867. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  10868. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  10869. } else if (IS_VALLEYVIEW(dev)) {
  10870. dev_priv->display.modeset_global_resources =
  10871. valleyview_modeset_global_resources;
  10872. }
  10873. /* Default just returns -ENODEV to indicate unsupported */
  10874. dev_priv->display.queue_flip = intel_default_queue_flip;
  10875. switch (INTEL_INFO(dev)->gen) {
  10876. case 2:
  10877. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  10878. break;
  10879. case 3:
  10880. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  10881. break;
  10882. case 4:
  10883. case 5:
  10884. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  10885. break;
  10886. case 6:
  10887. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  10888. break;
  10889. case 7:
  10890. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  10891. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  10892. break;
  10893. case 9:
  10894. dev_priv->display.queue_flip = intel_gen9_queue_flip;
  10895. break;
  10896. }
  10897. intel_panel_init_backlight_funcs(dev);
  10898. mutex_init(&dev_priv->pps_mutex);
  10899. }
  10900. /*
  10901. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  10902. * resume, or other times. This quirk makes sure that's the case for
  10903. * affected systems.
  10904. */
  10905. static void quirk_pipea_force(struct drm_device *dev)
  10906. {
  10907. struct drm_i915_private *dev_priv = dev->dev_private;
  10908. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  10909. DRM_INFO("applying pipe a force quirk\n");
  10910. }
  10911. static void quirk_pipeb_force(struct drm_device *dev)
  10912. {
  10913. struct drm_i915_private *dev_priv = dev->dev_private;
  10914. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  10915. DRM_INFO("applying pipe b force quirk\n");
  10916. }
  10917. /*
  10918. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  10919. */
  10920. static void quirk_ssc_force_disable(struct drm_device *dev)
  10921. {
  10922. struct drm_i915_private *dev_priv = dev->dev_private;
  10923. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  10924. DRM_INFO("applying lvds SSC disable quirk\n");
  10925. }
  10926. /*
  10927. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  10928. * brightness value
  10929. */
  10930. static void quirk_invert_brightness(struct drm_device *dev)
  10931. {
  10932. struct drm_i915_private *dev_priv = dev->dev_private;
  10933. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  10934. DRM_INFO("applying inverted panel brightness quirk\n");
  10935. }
  10936. /* Some VBT's incorrectly indicate no backlight is present */
  10937. static void quirk_backlight_present(struct drm_device *dev)
  10938. {
  10939. struct drm_i915_private *dev_priv = dev->dev_private;
  10940. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  10941. DRM_INFO("applying backlight present quirk\n");
  10942. }
  10943. struct intel_quirk {
  10944. int device;
  10945. int subsystem_vendor;
  10946. int subsystem_device;
  10947. void (*hook)(struct drm_device *dev);
  10948. };
  10949. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  10950. struct intel_dmi_quirk {
  10951. void (*hook)(struct drm_device *dev);
  10952. const struct dmi_system_id (*dmi_id_list)[];
  10953. };
  10954. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  10955. {
  10956. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  10957. return 1;
  10958. }
  10959. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  10960. {
  10961. .dmi_id_list = &(const struct dmi_system_id[]) {
  10962. {
  10963. .callback = intel_dmi_reverse_brightness,
  10964. .ident = "NCR Corporation",
  10965. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  10966. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  10967. },
  10968. },
  10969. { } /* terminating entry */
  10970. },
  10971. .hook = quirk_invert_brightness,
  10972. },
  10973. };
  10974. static struct intel_quirk intel_quirks[] = {
  10975. /* HP Mini needs pipe A force quirk (LP: #322104) */
  10976. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  10977. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  10978. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  10979. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  10980. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  10981. /* 830 needs to leave pipe A & dpll A up */
  10982. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  10983. /* 830 needs to leave pipe B & dpll B up */
  10984. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  10985. /* Lenovo U160 cannot use SSC on LVDS */
  10986. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  10987. /* Sony Vaio Y cannot use SSC on LVDS */
  10988. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  10989. /* Acer Aspire 5734Z must invert backlight brightness */
  10990. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  10991. /* Acer/eMachines G725 */
  10992. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  10993. /* Acer/eMachines e725 */
  10994. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  10995. /* Acer/Packard Bell NCL20 */
  10996. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  10997. /* Acer Aspire 4736Z */
  10998. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  10999. /* Acer Aspire 5336 */
  11000. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  11001. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  11002. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  11003. /* Acer C720 Chromebook (Core i3 4005U) */
  11004. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  11005. /* Apple Macbook 2,1 (Core 2 T7400) */
  11006. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  11007. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  11008. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  11009. /* HP Chromebook 14 (Celeron 2955U) */
  11010. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  11011. };
  11012. static void intel_init_quirks(struct drm_device *dev)
  11013. {
  11014. struct pci_dev *d = dev->pdev;
  11015. int i;
  11016. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  11017. struct intel_quirk *q = &intel_quirks[i];
  11018. if (d->device == q->device &&
  11019. (d->subsystem_vendor == q->subsystem_vendor ||
  11020. q->subsystem_vendor == PCI_ANY_ID) &&
  11021. (d->subsystem_device == q->subsystem_device ||
  11022. q->subsystem_device == PCI_ANY_ID))
  11023. q->hook(dev);
  11024. }
  11025. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  11026. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  11027. intel_dmi_quirks[i].hook(dev);
  11028. }
  11029. }
  11030. /* Disable the VGA plane that we never use */
  11031. static void i915_disable_vga(struct drm_device *dev)
  11032. {
  11033. struct drm_i915_private *dev_priv = dev->dev_private;
  11034. u8 sr1;
  11035. u32 vga_reg = i915_vgacntrl_reg(dev);
  11036. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  11037. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  11038. outb(SR01, VGA_SR_INDEX);
  11039. sr1 = inb(VGA_SR_DATA);
  11040. outb(sr1 | 1<<5, VGA_SR_DATA);
  11041. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  11042. udelay(300);
  11043. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  11044. POSTING_READ(vga_reg);
  11045. }
  11046. void intel_modeset_init_hw(struct drm_device *dev)
  11047. {
  11048. intel_prepare_ddi(dev);
  11049. if (IS_VALLEYVIEW(dev))
  11050. vlv_update_cdclk(dev);
  11051. intel_init_clock_gating(dev);
  11052. intel_enable_gt_powersave(dev);
  11053. }
  11054. void intel_modeset_init(struct drm_device *dev)
  11055. {
  11056. struct drm_i915_private *dev_priv = dev->dev_private;
  11057. int sprite, ret;
  11058. enum pipe pipe;
  11059. struct intel_crtc *crtc;
  11060. drm_mode_config_init(dev);
  11061. dev->mode_config.min_width = 0;
  11062. dev->mode_config.min_height = 0;
  11063. dev->mode_config.preferred_depth = 24;
  11064. dev->mode_config.prefer_shadow = 1;
  11065. dev->mode_config.funcs = &intel_mode_funcs;
  11066. intel_init_quirks(dev);
  11067. intel_init_pm(dev);
  11068. if (INTEL_INFO(dev)->num_pipes == 0)
  11069. return;
  11070. intel_init_display(dev);
  11071. intel_init_audio(dev);
  11072. if (IS_GEN2(dev)) {
  11073. dev->mode_config.max_width = 2048;
  11074. dev->mode_config.max_height = 2048;
  11075. } else if (IS_GEN3(dev)) {
  11076. dev->mode_config.max_width = 4096;
  11077. dev->mode_config.max_height = 4096;
  11078. } else {
  11079. dev->mode_config.max_width = 8192;
  11080. dev->mode_config.max_height = 8192;
  11081. }
  11082. if (IS_845G(dev) || IS_I865G(dev)) {
  11083. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  11084. dev->mode_config.cursor_height = 1023;
  11085. } else if (IS_GEN2(dev)) {
  11086. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  11087. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  11088. } else {
  11089. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  11090. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  11091. }
  11092. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  11093. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  11094. INTEL_INFO(dev)->num_pipes,
  11095. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  11096. for_each_pipe(dev_priv, pipe) {
  11097. intel_crtc_init(dev, pipe);
  11098. for_each_sprite(pipe, sprite) {
  11099. ret = intel_plane_init(dev, pipe, sprite);
  11100. if (ret)
  11101. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  11102. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  11103. }
  11104. }
  11105. intel_init_dpio(dev);
  11106. intel_shared_dpll_init(dev);
  11107. /* Just disable it once at startup */
  11108. i915_disable_vga(dev);
  11109. intel_setup_outputs(dev);
  11110. /* Just in case the BIOS is doing something questionable. */
  11111. intel_fbc_disable(dev);
  11112. drm_modeset_lock_all(dev);
  11113. intel_modeset_setup_hw_state(dev, false);
  11114. drm_modeset_unlock_all(dev);
  11115. for_each_intel_crtc(dev, crtc) {
  11116. if (!crtc->active)
  11117. continue;
  11118. /*
  11119. * Note that reserving the BIOS fb up front prevents us
  11120. * from stuffing other stolen allocations like the ring
  11121. * on top. This prevents some ugliness at boot time, and
  11122. * can even allow for smooth boot transitions if the BIOS
  11123. * fb is large enough for the active pipe configuration.
  11124. */
  11125. if (dev_priv->display.get_initial_plane_config) {
  11126. dev_priv->display.get_initial_plane_config(crtc,
  11127. &crtc->plane_config);
  11128. /*
  11129. * If the fb is shared between multiple heads, we'll
  11130. * just get the first one.
  11131. */
  11132. intel_find_plane_obj(crtc, &crtc->plane_config);
  11133. }
  11134. }
  11135. }
  11136. static void intel_enable_pipe_a(struct drm_device *dev)
  11137. {
  11138. struct intel_connector *connector;
  11139. struct drm_connector *crt = NULL;
  11140. struct intel_load_detect_pipe load_detect_temp;
  11141. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  11142. /* We can't just switch on the pipe A, we need to set things up with a
  11143. * proper mode and output configuration. As a gross hack, enable pipe A
  11144. * by enabling the load detect pipe once. */
  11145. list_for_each_entry(connector,
  11146. &dev->mode_config.connector_list,
  11147. base.head) {
  11148. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  11149. crt = &connector->base;
  11150. break;
  11151. }
  11152. }
  11153. if (!crt)
  11154. return;
  11155. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  11156. intel_release_load_detect_pipe(crt, &load_detect_temp);
  11157. }
  11158. static bool
  11159. intel_check_plane_mapping(struct intel_crtc *crtc)
  11160. {
  11161. struct drm_device *dev = crtc->base.dev;
  11162. struct drm_i915_private *dev_priv = dev->dev_private;
  11163. u32 reg, val;
  11164. if (INTEL_INFO(dev)->num_pipes == 1)
  11165. return true;
  11166. reg = DSPCNTR(!crtc->plane);
  11167. val = I915_READ(reg);
  11168. if ((val & DISPLAY_PLANE_ENABLE) &&
  11169. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  11170. return false;
  11171. return true;
  11172. }
  11173. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  11174. {
  11175. struct drm_device *dev = crtc->base.dev;
  11176. struct drm_i915_private *dev_priv = dev->dev_private;
  11177. u32 reg;
  11178. /* Clear any frame start delays used for debugging left by the BIOS */
  11179. reg = PIPECONF(crtc->config->cpu_transcoder);
  11180. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  11181. /* restore vblank interrupts to correct state */
  11182. if (crtc->active) {
  11183. update_scanline_offset(crtc);
  11184. drm_vblank_on(dev, crtc->pipe);
  11185. } else
  11186. drm_vblank_off(dev, crtc->pipe);
  11187. /* We need to sanitize the plane -> pipe mapping first because this will
  11188. * disable the crtc (and hence change the state) if it is wrong. Note
  11189. * that gen4+ has a fixed plane -> pipe mapping. */
  11190. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  11191. struct intel_connector *connector;
  11192. bool plane;
  11193. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  11194. crtc->base.base.id);
  11195. /* Pipe has the wrong plane attached and the plane is active.
  11196. * Temporarily change the plane mapping and disable everything
  11197. * ... */
  11198. plane = crtc->plane;
  11199. crtc->plane = !plane;
  11200. crtc->primary_enabled = true;
  11201. dev_priv->display.crtc_disable(&crtc->base);
  11202. crtc->plane = plane;
  11203. /* ... and break all links. */
  11204. list_for_each_entry(connector, &dev->mode_config.connector_list,
  11205. base.head) {
  11206. if (connector->encoder->base.crtc != &crtc->base)
  11207. continue;
  11208. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11209. connector->base.encoder = NULL;
  11210. }
  11211. /* multiple connectors may have the same encoder:
  11212. * handle them and break crtc link separately */
  11213. list_for_each_entry(connector, &dev->mode_config.connector_list,
  11214. base.head)
  11215. if (connector->encoder->base.crtc == &crtc->base) {
  11216. connector->encoder->base.crtc = NULL;
  11217. connector->encoder->connectors_active = false;
  11218. }
  11219. WARN_ON(crtc->active);
  11220. crtc->base.enabled = false;
  11221. }
  11222. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  11223. crtc->pipe == PIPE_A && !crtc->active) {
  11224. /* BIOS forgot to enable pipe A, this mostly happens after
  11225. * resume. Force-enable the pipe to fix this, the update_dpms
  11226. * call below we restore the pipe to the right state, but leave
  11227. * the required bits on. */
  11228. intel_enable_pipe_a(dev);
  11229. }
  11230. /* Adjust the state of the output pipe according to whether we
  11231. * have active connectors/encoders. */
  11232. intel_crtc_update_dpms(&crtc->base);
  11233. if (crtc->active != crtc->base.enabled) {
  11234. struct intel_encoder *encoder;
  11235. /* This can happen either due to bugs in the get_hw_state
  11236. * functions or because the pipe is force-enabled due to the
  11237. * pipe A quirk. */
  11238. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  11239. crtc->base.base.id,
  11240. crtc->base.enabled ? "enabled" : "disabled",
  11241. crtc->active ? "enabled" : "disabled");
  11242. crtc->base.enabled = crtc->active;
  11243. /* Because we only establish the connector -> encoder ->
  11244. * crtc links if something is active, this means the
  11245. * crtc is now deactivated. Break the links. connector
  11246. * -> encoder links are only establish when things are
  11247. * actually up, hence no need to break them. */
  11248. WARN_ON(crtc->active);
  11249. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  11250. WARN_ON(encoder->connectors_active);
  11251. encoder->base.crtc = NULL;
  11252. }
  11253. }
  11254. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  11255. /*
  11256. * We start out with underrun reporting disabled to avoid races.
  11257. * For correct bookkeeping mark this on active crtcs.
  11258. *
  11259. * Also on gmch platforms we dont have any hardware bits to
  11260. * disable the underrun reporting. Which means we need to start
  11261. * out with underrun reporting disabled also on inactive pipes,
  11262. * since otherwise we'll complain about the garbage we read when
  11263. * e.g. coming up after runtime pm.
  11264. *
  11265. * No protection against concurrent access is required - at
  11266. * worst a fifo underrun happens which also sets this to false.
  11267. */
  11268. crtc->cpu_fifo_underrun_disabled = true;
  11269. crtc->pch_fifo_underrun_disabled = true;
  11270. }
  11271. }
  11272. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  11273. {
  11274. struct intel_connector *connector;
  11275. struct drm_device *dev = encoder->base.dev;
  11276. /* We need to check both for a crtc link (meaning that the
  11277. * encoder is active and trying to read from a pipe) and the
  11278. * pipe itself being active. */
  11279. bool has_active_crtc = encoder->base.crtc &&
  11280. to_intel_crtc(encoder->base.crtc)->active;
  11281. if (encoder->connectors_active && !has_active_crtc) {
  11282. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  11283. encoder->base.base.id,
  11284. encoder->base.name);
  11285. /* Connector is active, but has no active pipe. This is
  11286. * fallout from our resume register restoring. Disable
  11287. * the encoder manually again. */
  11288. if (encoder->base.crtc) {
  11289. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  11290. encoder->base.base.id,
  11291. encoder->base.name);
  11292. encoder->disable(encoder);
  11293. if (encoder->post_disable)
  11294. encoder->post_disable(encoder);
  11295. }
  11296. encoder->base.crtc = NULL;
  11297. encoder->connectors_active = false;
  11298. /* Inconsistent output/port/pipe state happens presumably due to
  11299. * a bug in one of the get_hw_state functions. Or someplace else
  11300. * in our code, like the register restore mess on resume. Clamp
  11301. * things to off as a safer default. */
  11302. list_for_each_entry(connector,
  11303. &dev->mode_config.connector_list,
  11304. base.head) {
  11305. if (connector->encoder != encoder)
  11306. continue;
  11307. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11308. connector->base.encoder = NULL;
  11309. }
  11310. }
  11311. /* Enabled encoders without active connectors will be fixed in
  11312. * the crtc fixup. */
  11313. }
  11314. void i915_redisable_vga_power_on(struct drm_device *dev)
  11315. {
  11316. struct drm_i915_private *dev_priv = dev->dev_private;
  11317. u32 vga_reg = i915_vgacntrl_reg(dev);
  11318. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  11319. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  11320. i915_disable_vga(dev);
  11321. }
  11322. }
  11323. void i915_redisable_vga(struct drm_device *dev)
  11324. {
  11325. struct drm_i915_private *dev_priv = dev->dev_private;
  11326. /* This function can be called both from intel_modeset_setup_hw_state or
  11327. * at a very early point in our resume sequence, where the power well
  11328. * structures are not yet restored. Since this function is at a very
  11329. * paranoid "someone might have enabled VGA while we were not looking"
  11330. * level, just check if the power well is enabled instead of trying to
  11331. * follow the "don't touch the power well if we don't need it" policy
  11332. * the rest of the driver uses. */
  11333. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  11334. return;
  11335. i915_redisable_vga_power_on(dev);
  11336. }
  11337. static bool primary_get_hw_state(struct intel_crtc *crtc)
  11338. {
  11339. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  11340. if (!crtc->active)
  11341. return false;
  11342. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  11343. }
  11344. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  11345. {
  11346. struct drm_i915_private *dev_priv = dev->dev_private;
  11347. enum pipe pipe;
  11348. struct intel_crtc *crtc;
  11349. struct intel_encoder *encoder;
  11350. struct intel_connector *connector;
  11351. int i;
  11352. for_each_intel_crtc(dev, crtc) {
  11353. memset(crtc->config, 0, sizeof(*crtc->config));
  11354. crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  11355. crtc->active = dev_priv->display.get_pipe_config(crtc,
  11356. crtc->config);
  11357. crtc->base.enabled = crtc->active;
  11358. crtc->primary_enabled = primary_get_hw_state(crtc);
  11359. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  11360. crtc->base.base.id,
  11361. crtc->active ? "enabled" : "disabled");
  11362. }
  11363. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11364. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11365. pll->on = pll->get_hw_state(dev_priv, pll,
  11366. &pll->config.hw_state);
  11367. pll->active = 0;
  11368. pll->config.crtc_mask = 0;
  11369. for_each_intel_crtc(dev, crtc) {
  11370. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  11371. pll->active++;
  11372. pll->config.crtc_mask |= 1 << crtc->pipe;
  11373. }
  11374. }
  11375. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  11376. pll->name, pll->config.crtc_mask, pll->on);
  11377. if (pll->config.crtc_mask)
  11378. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  11379. }
  11380. for_each_intel_encoder(dev, encoder) {
  11381. pipe = 0;
  11382. if (encoder->get_hw_state(encoder, &pipe)) {
  11383. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11384. encoder->base.crtc = &crtc->base;
  11385. encoder->get_config(encoder, crtc->config);
  11386. } else {
  11387. encoder->base.crtc = NULL;
  11388. }
  11389. encoder->connectors_active = false;
  11390. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  11391. encoder->base.base.id,
  11392. encoder->base.name,
  11393. encoder->base.crtc ? "enabled" : "disabled",
  11394. pipe_name(pipe));
  11395. }
  11396. list_for_each_entry(connector, &dev->mode_config.connector_list,
  11397. base.head) {
  11398. if (connector->get_hw_state(connector)) {
  11399. connector->base.dpms = DRM_MODE_DPMS_ON;
  11400. connector->encoder->connectors_active = true;
  11401. connector->base.encoder = &connector->encoder->base;
  11402. } else {
  11403. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11404. connector->base.encoder = NULL;
  11405. }
  11406. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  11407. connector->base.base.id,
  11408. connector->base.name,
  11409. connector->base.encoder ? "enabled" : "disabled");
  11410. }
  11411. }
  11412. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  11413. * and i915 state tracking structures. */
  11414. void intel_modeset_setup_hw_state(struct drm_device *dev,
  11415. bool force_restore)
  11416. {
  11417. struct drm_i915_private *dev_priv = dev->dev_private;
  11418. enum pipe pipe;
  11419. struct intel_crtc *crtc;
  11420. struct intel_encoder *encoder;
  11421. int i;
  11422. intel_modeset_readout_hw_state(dev);
  11423. /*
  11424. * Now that we have the config, copy it to each CRTC struct
  11425. * Note that this could go away if we move to using crtc_config
  11426. * checking everywhere.
  11427. */
  11428. for_each_intel_crtc(dev, crtc) {
  11429. if (crtc->active && i915.fastboot) {
  11430. intel_mode_from_pipe_config(&crtc->base.mode,
  11431. crtc->config);
  11432. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  11433. crtc->base.base.id);
  11434. drm_mode_debug_printmodeline(&crtc->base.mode);
  11435. }
  11436. }
  11437. /* HW state is read out, now we need to sanitize this mess. */
  11438. for_each_intel_encoder(dev, encoder) {
  11439. intel_sanitize_encoder(encoder);
  11440. }
  11441. for_each_pipe(dev_priv, pipe) {
  11442. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11443. intel_sanitize_crtc(crtc);
  11444. intel_dump_pipe_config(crtc, crtc->config,
  11445. "[setup_hw_state]");
  11446. }
  11447. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11448. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11449. if (!pll->on || pll->active)
  11450. continue;
  11451. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  11452. pll->disable(dev_priv, pll);
  11453. pll->on = false;
  11454. }
  11455. if (IS_GEN9(dev))
  11456. skl_wm_get_hw_state(dev);
  11457. else if (HAS_PCH_SPLIT(dev))
  11458. ilk_wm_get_hw_state(dev);
  11459. if (force_restore) {
  11460. i915_redisable_vga(dev);
  11461. /*
  11462. * We need to use raw interfaces for restoring state to avoid
  11463. * checking (bogus) intermediate states.
  11464. */
  11465. for_each_pipe(dev_priv, pipe) {
  11466. struct drm_crtc *crtc =
  11467. dev_priv->pipe_to_crtc_mapping[pipe];
  11468. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
  11469. crtc->primary->fb);
  11470. }
  11471. } else {
  11472. intel_modeset_update_staged_output_state(dev);
  11473. }
  11474. intel_modeset_check_state(dev);
  11475. }
  11476. void intel_modeset_gem_init(struct drm_device *dev)
  11477. {
  11478. struct drm_i915_private *dev_priv = dev->dev_private;
  11479. struct drm_crtc *c;
  11480. struct drm_i915_gem_object *obj;
  11481. mutex_lock(&dev->struct_mutex);
  11482. intel_init_gt_powersave(dev);
  11483. mutex_unlock(&dev->struct_mutex);
  11484. /*
  11485. * There may be no VBT; and if the BIOS enabled SSC we can
  11486. * just keep using it to avoid unnecessary flicker. Whereas if the
  11487. * BIOS isn't using it, don't assume it will work even if the VBT
  11488. * indicates as much.
  11489. */
  11490. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11491. dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  11492. DREF_SSC1_ENABLE);
  11493. intel_modeset_init_hw(dev);
  11494. intel_setup_overlay(dev);
  11495. /*
  11496. * Make sure any fbs we allocated at startup are properly
  11497. * pinned & fenced. When we do the allocation it's too early
  11498. * for this.
  11499. */
  11500. mutex_lock(&dev->struct_mutex);
  11501. for_each_crtc(dev, c) {
  11502. obj = intel_fb_obj(c->primary->fb);
  11503. if (obj == NULL)
  11504. continue;
  11505. if (intel_pin_and_fence_fb_obj(c->primary,
  11506. c->primary->fb,
  11507. NULL)) {
  11508. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  11509. to_intel_crtc(c)->pipe);
  11510. drm_framebuffer_unreference(c->primary->fb);
  11511. c->primary->fb = NULL;
  11512. update_state_fb(c->primary);
  11513. }
  11514. }
  11515. mutex_unlock(&dev->struct_mutex);
  11516. intel_backlight_register(dev);
  11517. }
  11518. void intel_connector_unregister(struct intel_connector *intel_connector)
  11519. {
  11520. struct drm_connector *connector = &intel_connector->base;
  11521. intel_panel_destroy_backlight(connector);
  11522. drm_connector_unregister(connector);
  11523. }
  11524. void intel_modeset_cleanup(struct drm_device *dev)
  11525. {
  11526. struct drm_i915_private *dev_priv = dev->dev_private;
  11527. struct drm_connector *connector;
  11528. intel_disable_gt_powersave(dev);
  11529. intel_backlight_unregister(dev);
  11530. /*
  11531. * Interrupts and polling as the first thing to avoid creating havoc.
  11532. * Too much stuff here (turning of connectors, ...) would
  11533. * experience fancy races otherwise.
  11534. */
  11535. intel_irq_uninstall(dev_priv);
  11536. /*
  11537. * Due to the hpd irq storm handling the hotplug work can re-arm the
  11538. * poll handlers. Hence disable polling after hpd handling is shut down.
  11539. */
  11540. drm_kms_helper_poll_fini(dev);
  11541. mutex_lock(&dev->struct_mutex);
  11542. intel_unregister_dsm_handler();
  11543. intel_fbc_disable(dev);
  11544. ironlake_teardown_rc6(dev);
  11545. mutex_unlock(&dev->struct_mutex);
  11546. /* flush any delayed tasks or pending work */
  11547. flush_scheduled_work();
  11548. /* destroy the backlight and sysfs files before encoders/connectors */
  11549. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  11550. struct intel_connector *intel_connector;
  11551. intel_connector = to_intel_connector(connector);
  11552. intel_connector->unregister(intel_connector);
  11553. }
  11554. drm_mode_config_cleanup(dev);
  11555. intel_cleanup_overlay(dev);
  11556. mutex_lock(&dev->struct_mutex);
  11557. intel_cleanup_gt_powersave(dev);
  11558. mutex_unlock(&dev->struct_mutex);
  11559. }
  11560. /*
  11561. * Return which encoder is currently attached for connector.
  11562. */
  11563. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  11564. {
  11565. return &intel_attached_encoder(connector)->base;
  11566. }
  11567. void intel_connector_attach_encoder(struct intel_connector *connector,
  11568. struct intel_encoder *encoder)
  11569. {
  11570. connector->encoder = encoder;
  11571. drm_mode_connector_attach_encoder(&connector->base,
  11572. &encoder->base);
  11573. }
  11574. /*
  11575. * set vga decode state - true == enable VGA decode
  11576. */
  11577. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  11578. {
  11579. struct drm_i915_private *dev_priv = dev->dev_private;
  11580. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  11581. u16 gmch_ctrl;
  11582. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  11583. DRM_ERROR("failed to read control word\n");
  11584. return -EIO;
  11585. }
  11586. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  11587. return 0;
  11588. if (state)
  11589. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  11590. else
  11591. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  11592. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  11593. DRM_ERROR("failed to write control word\n");
  11594. return -EIO;
  11595. }
  11596. return 0;
  11597. }
  11598. struct intel_display_error_state {
  11599. u32 power_well_driver;
  11600. int num_transcoders;
  11601. struct intel_cursor_error_state {
  11602. u32 control;
  11603. u32 position;
  11604. u32 base;
  11605. u32 size;
  11606. } cursor[I915_MAX_PIPES];
  11607. struct intel_pipe_error_state {
  11608. bool power_domain_on;
  11609. u32 source;
  11610. u32 stat;
  11611. } pipe[I915_MAX_PIPES];
  11612. struct intel_plane_error_state {
  11613. u32 control;
  11614. u32 stride;
  11615. u32 size;
  11616. u32 pos;
  11617. u32 addr;
  11618. u32 surface;
  11619. u32 tile_offset;
  11620. } plane[I915_MAX_PIPES];
  11621. struct intel_transcoder_error_state {
  11622. bool power_domain_on;
  11623. enum transcoder cpu_transcoder;
  11624. u32 conf;
  11625. u32 htotal;
  11626. u32 hblank;
  11627. u32 hsync;
  11628. u32 vtotal;
  11629. u32 vblank;
  11630. u32 vsync;
  11631. } transcoder[4];
  11632. };
  11633. struct intel_display_error_state *
  11634. intel_display_capture_error_state(struct drm_device *dev)
  11635. {
  11636. struct drm_i915_private *dev_priv = dev->dev_private;
  11637. struct intel_display_error_state *error;
  11638. int transcoders[] = {
  11639. TRANSCODER_A,
  11640. TRANSCODER_B,
  11641. TRANSCODER_C,
  11642. TRANSCODER_EDP,
  11643. };
  11644. int i;
  11645. if (INTEL_INFO(dev)->num_pipes == 0)
  11646. return NULL;
  11647. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  11648. if (error == NULL)
  11649. return NULL;
  11650. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11651. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  11652. for_each_pipe(dev_priv, i) {
  11653. error->pipe[i].power_domain_on =
  11654. __intel_display_power_is_enabled(dev_priv,
  11655. POWER_DOMAIN_PIPE(i));
  11656. if (!error->pipe[i].power_domain_on)
  11657. continue;
  11658. error->cursor[i].control = I915_READ(CURCNTR(i));
  11659. error->cursor[i].position = I915_READ(CURPOS(i));
  11660. error->cursor[i].base = I915_READ(CURBASE(i));
  11661. error->plane[i].control = I915_READ(DSPCNTR(i));
  11662. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  11663. if (INTEL_INFO(dev)->gen <= 3) {
  11664. error->plane[i].size = I915_READ(DSPSIZE(i));
  11665. error->plane[i].pos = I915_READ(DSPPOS(i));
  11666. }
  11667. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11668. error->plane[i].addr = I915_READ(DSPADDR(i));
  11669. if (INTEL_INFO(dev)->gen >= 4) {
  11670. error->plane[i].surface = I915_READ(DSPSURF(i));
  11671. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  11672. }
  11673. error->pipe[i].source = I915_READ(PIPESRC(i));
  11674. if (HAS_GMCH_DISPLAY(dev))
  11675. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  11676. }
  11677. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  11678. if (HAS_DDI(dev_priv->dev))
  11679. error->num_transcoders++; /* Account for eDP. */
  11680. for (i = 0; i < error->num_transcoders; i++) {
  11681. enum transcoder cpu_transcoder = transcoders[i];
  11682. error->transcoder[i].power_domain_on =
  11683. __intel_display_power_is_enabled(dev_priv,
  11684. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  11685. if (!error->transcoder[i].power_domain_on)
  11686. continue;
  11687. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  11688. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  11689. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  11690. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  11691. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  11692. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  11693. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  11694. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  11695. }
  11696. return error;
  11697. }
  11698. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  11699. void
  11700. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  11701. struct drm_device *dev,
  11702. struct intel_display_error_state *error)
  11703. {
  11704. struct drm_i915_private *dev_priv = dev->dev_private;
  11705. int i;
  11706. if (!error)
  11707. return;
  11708. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  11709. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11710. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  11711. error->power_well_driver);
  11712. for_each_pipe(dev_priv, i) {
  11713. err_printf(m, "Pipe [%d]:\n", i);
  11714. err_printf(m, " Power: %s\n",
  11715. error->pipe[i].power_domain_on ? "on" : "off");
  11716. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  11717. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  11718. err_printf(m, "Plane [%d]:\n", i);
  11719. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  11720. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  11721. if (INTEL_INFO(dev)->gen <= 3) {
  11722. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  11723. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  11724. }
  11725. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11726. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  11727. if (INTEL_INFO(dev)->gen >= 4) {
  11728. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  11729. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  11730. }
  11731. err_printf(m, "Cursor [%d]:\n", i);
  11732. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  11733. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  11734. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  11735. }
  11736. for (i = 0; i < error->num_transcoders; i++) {
  11737. err_printf(m, "CPU transcoder: %c\n",
  11738. transcoder_name(error->transcoder[i].cpu_transcoder));
  11739. err_printf(m, " Power: %s\n",
  11740. error->transcoder[i].power_domain_on ? "on" : "off");
  11741. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  11742. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  11743. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  11744. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  11745. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  11746. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  11747. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  11748. }
  11749. }
  11750. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  11751. {
  11752. struct intel_crtc *crtc;
  11753. for_each_intel_crtc(dev, crtc) {
  11754. struct intel_unpin_work *work;
  11755. spin_lock_irq(&dev->event_lock);
  11756. work = crtc->unpin_work;
  11757. if (work && work->event &&
  11758. work->event->base.file_priv == file) {
  11759. kfree(work->event);
  11760. work->event = NULL;
  11761. }
  11762. spin_unlock_irq(&dev->event_lock);
  11763. }
  11764. }