intel_display.c 392 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220
  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 = 4800000, .max = 6480000 },
  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. * FIXME: The intel_crtc->active here should be switched to
  797. * crtc->state->active once we have proper CRTC states wired up
  798. * for atomic.
  799. */
  800. return intel_crtc->active && crtc->primary->state->fb &&
  801. intel_crtc->config->base.adjusted_mode.crtc_clock;
  802. }
  803. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  804. enum pipe pipe)
  805. {
  806. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  807. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  808. return intel_crtc->config->cpu_transcoder;
  809. }
  810. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  811. {
  812. struct drm_i915_private *dev_priv = dev->dev_private;
  813. u32 reg = PIPEDSL(pipe);
  814. u32 line1, line2;
  815. u32 line_mask;
  816. if (IS_GEN2(dev))
  817. line_mask = DSL_LINEMASK_GEN2;
  818. else
  819. line_mask = DSL_LINEMASK_GEN3;
  820. line1 = I915_READ(reg) & line_mask;
  821. mdelay(5);
  822. line2 = I915_READ(reg) & line_mask;
  823. return line1 == line2;
  824. }
  825. /*
  826. * intel_wait_for_pipe_off - wait for pipe to turn off
  827. * @crtc: crtc whose pipe to wait for
  828. *
  829. * After disabling a pipe, we can't wait for vblank in the usual way,
  830. * spinning on the vblank interrupt status bit, since we won't actually
  831. * see an interrupt when the pipe is disabled.
  832. *
  833. * On Gen4 and above:
  834. * wait for the pipe register state bit to turn off
  835. *
  836. * Otherwise:
  837. * wait for the display line value to settle (it usually
  838. * ends up stopping at the start of the next frame).
  839. *
  840. */
  841. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  842. {
  843. struct drm_device *dev = crtc->base.dev;
  844. struct drm_i915_private *dev_priv = dev->dev_private;
  845. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  846. enum pipe pipe = crtc->pipe;
  847. if (INTEL_INFO(dev)->gen >= 4) {
  848. int reg = PIPECONF(cpu_transcoder);
  849. /* Wait for the Pipe State to go off */
  850. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  851. 100))
  852. WARN(1, "pipe_off wait timed out\n");
  853. } else {
  854. /* Wait for the display line to settle */
  855. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  856. WARN(1, "pipe_off wait timed out\n");
  857. }
  858. }
  859. /*
  860. * ibx_digital_port_connected - is the specified port connected?
  861. * @dev_priv: i915 private structure
  862. * @port: the port to test
  863. *
  864. * Returns true if @port is connected, false otherwise.
  865. */
  866. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  867. struct intel_digital_port *port)
  868. {
  869. u32 bit;
  870. if (HAS_PCH_IBX(dev_priv->dev)) {
  871. switch (port->port) {
  872. case PORT_B:
  873. bit = SDE_PORTB_HOTPLUG;
  874. break;
  875. case PORT_C:
  876. bit = SDE_PORTC_HOTPLUG;
  877. break;
  878. case PORT_D:
  879. bit = SDE_PORTD_HOTPLUG;
  880. break;
  881. default:
  882. return true;
  883. }
  884. } else {
  885. switch (port->port) {
  886. case PORT_B:
  887. bit = SDE_PORTB_HOTPLUG_CPT;
  888. break;
  889. case PORT_C:
  890. bit = SDE_PORTC_HOTPLUG_CPT;
  891. break;
  892. case PORT_D:
  893. bit = SDE_PORTD_HOTPLUG_CPT;
  894. break;
  895. default:
  896. return true;
  897. }
  898. }
  899. return I915_READ(SDEISR) & bit;
  900. }
  901. static const char *state_string(bool enabled)
  902. {
  903. return enabled ? "on" : "off";
  904. }
  905. /* Only for pre-ILK configs */
  906. void assert_pll(struct drm_i915_private *dev_priv,
  907. enum pipe pipe, bool state)
  908. {
  909. int reg;
  910. u32 val;
  911. bool cur_state;
  912. reg = DPLL(pipe);
  913. val = I915_READ(reg);
  914. cur_state = !!(val & DPLL_VCO_ENABLE);
  915. I915_STATE_WARN(cur_state != state,
  916. "PLL state assertion failure (expected %s, current %s)\n",
  917. state_string(state), state_string(cur_state));
  918. }
  919. /* XXX: the dsi pll is shared between MIPI DSI ports */
  920. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  921. {
  922. u32 val;
  923. bool cur_state;
  924. mutex_lock(&dev_priv->dpio_lock);
  925. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  926. mutex_unlock(&dev_priv->dpio_lock);
  927. cur_state = val & DSI_PLL_VCO_EN;
  928. I915_STATE_WARN(cur_state != state,
  929. "DSI PLL state assertion failure (expected %s, current %s)\n",
  930. state_string(state), state_string(cur_state));
  931. }
  932. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  933. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  934. struct intel_shared_dpll *
  935. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  936. {
  937. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  938. if (crtc->config->shared_dpll < 0)
  939. return NULL;
  940. return &dev_priv->shared_dplls[crtc->config->shared_dpll];
  941. }
  942. /* For ILK+ */
  943. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  944. struct intel_shared_dpll *pll,
  945. bool state)
  946. {
  947. bool cur_state;
  948. struct intel_dpll_hw_state hw_state;
  949. if (WARN (!pll,
  950. "asserting DPLL %s with no DPLL\n", state_string(state)))
  951. return;
  952. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  953. I915_STATE_WARN(cur_state != state,
  954. "%s assertion failure (expected %s, current %s)\n",
  955. pll->name, state_string(state), state_string(cur_state));
  956. }
  957. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  958. enum pipe pipe, bool state)
  959. {
  960. int reg;
  961. u32 val;
  962. bool cur_state;
  963. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  964. pipe);
  965. if (HAS_DDI(dev_priv->dev)) {
  966. /* DDI does not have a specific FDI_TX register */
  967. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  968. val = I915_READ(reg);
  969. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  970. } else {
  971. reg = FDI_TX_CTL(pipe);
  972. val = I915_READ(reg);
  973. cur_state = !!(val & FDI_TX_ENABLE);
  974. }
  975. I915_STATE_WARN(cur_state != state,
  976. "FDI TX state assertion failure (expected %s, current %s)\n",
  977. state_string(state), state_string(cur_state));
  978. }
  979. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  980. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  981. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  982. enum pipe pipe, bool state)
  983. {
  984. int reg;
  985. u32 val;
  986. bool cur_state;
  987. reg = FDI_RX_CTL(pipe);
  988. val = I915_READ(reg);
  989. cur_state = !!(val & FDI_RX_ENABLE);
  990. I915_STATE_WARN(cur_state != state,
  991. "FDI RX state assertion failure (expected %s, current %s)\n",
  992. state_string(state), state_string(cur_state));
  993. }
  994. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  995. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  996. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  997. enum pipe pipe)
  998. {
  999. int reg;
  1000. u32 val;
  1001. /* ILK FDI PLL is always enabled */
  1002. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  1003. return;
  1004. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1005. if (HAS_DDI(dev_priv->dev))
  1006. return;
  1007. reg = FDI_TX_CTL(pipe);
  1008. val = I915_READ(reg);
  1009. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1010. }
  1011. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1012. enum pipe pipe, bool state)
  1013. {
  1014. int reg;
  1015. u32 val;
  1016. bool cur_state;
  1017. reg = FDI_RX_CTL(pipe);
  1018. val = I915_READ(reg);
  1019. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1020. I915_STATE_WARN(cur_state != state,
  1021. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1022. state_string(state), state_string(cur_state));
  1023. }
  1024. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1025. enum pipe pipe)
  1026. {
  1027. struct drm_device *dev = dev_priv->dev;
  1028. int pp_reg;
  1029. u32 val;
  1030. enum pipe panel_pipe = PIPE_A;
  1031. bool locked = true;
  1032. if (WARN_ON(HAS_DDI(dev)))
  1033. return;
  1034. if (HAS_PCH_SPLIT(dev)) {
  1035. u32 port_sel;
  1036. pp_reg = PCH_PP_CONTROL;
  1037. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1038. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1039. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1040. panel_pipe = PIPE_B;
  1041. /* XXX: else fix for eDP */
  1042. } else if (IS_VALLEYVIEW(dev)) {
  1043. /* presumably write lock depends on pipe, not port select */
  1044. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1045. panel_pipe = pipe;
  1046. } else {
  1047. pp_reg = PP_CONTROL;
  1048. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1049. panel_pipe = PIPE_B;
  1050. }
  1051. val = I915_READ(pp_reg);
  1052. if (!(val & PANEL_POWER_ON) ||
  1053. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1054. locked = false;
  1055. I915_STATE_WARN(panel_pipe == pipe && locked,
  1056. "panel assertion failure, pipe %c regs locked\n",
  1057. pipe_name(pipe));
  1058. }
  1059. static void assert_cursor(struct drm_i915_private *dev_priv,
  1060. enum pipe pipe, bool state)
  1061. {
  1062. struct drm_device *dev = dev_priv->dev;
  1063. bool cur_state;
  1064. if (IS_845G(dev) || IS_I865G(dev))
  1065. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1066. else
  1067. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1068. I915_STATE_WARN(cur_state != state,
  1069. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1070. pipe_name(pipe), state_string(state), state_string(cur_state));
  1071. }
  1072. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1073. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1074. void assert_pipe(struct drm_i915_private *dev_priv,
  1075. enum pipe pipe, bool state)
  1076. {
  1077. int reg;
  1078. u32 val;
  1079. bool cur_state;
  1080. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1081. pipe);
  1082. /* if we need the pipe quirk it must be always on */
  1083. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1084. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1085. state = true;
  1086. if (!intel_display_power_is_enabled(dev_priv,
  1087. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1088. cur_state = false;
  1089. } else {
  1090. reg = PIPECONF(cpu_transcoder);
  1091. val = I915_READ(reg);
  1092. cur_state = !!(val & PIPECONF_ENABLE);
  1093. }
  1094. I915_STATE_WARN(cur_state != state,
  1095. "pipe %c assertion failure (expected %s, current %s)\n",
  1096. pipe_name(pipe), state_string(state), state_string(cur_state));
  1097. }
  1098. static void assert_plane(struct drm_i915_private *dev_priv,
  1099. enum plane plane, bool state)
  1100. {
  1101. int reg;
  1102. u32 val;
  1103. bool cur_state;
  1104. reg = DSPCNTR(plane);
  1105. val = I915_READ(reg);
  1106. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1107. I915_STATE_WARN(cur_state != state,
  1108. "plane %c assertion failure (expected %s, current %s)\n",
  1109. plane_name(plane), state_string(state), state_string(cur_state));
  1110. }
  1111. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1112. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1113. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1114. enum pipe pipe)
  1115. {
  1116. struct drm_device *dev = dev_priv->dev;
  1117. int reg, i;
  1118. u32 val;
  1119. int cur_pipe;
  1120. /* Primary planes are fixed to pipes on gen4+ */
  1121. if (INTEL_INFO(dev)->gen >= 4) {
  1122. reg = DSPCNTR(pipe);
  1123. val = I915_READ(reg);
  1124. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1125. "plane %c assertion failure, should be disabled but not\n",
  1126. plane_name(pipe));
  1127. return;
  1128. }
  1129. /* Need to check both planes against the pipe */
  1130. for_each_pipe(dev_priv, i) {
  1131. reg = DSPCNTR(i);
  1132. val = I915_READ(reg);
  1133. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1134. DISPPLANE_SEL_PIPE_SHIFT;
  1135. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1136. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1137. plane_name(i), pipe_name(pipe));
  1138. }
  1139. }
  1140. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1141. enum pipe pipe)
  1142. {
  1143. struct drm_device *dev = dev_priv->dev;
  1144. int reg, sprite;
  1145. u32 val;
  1146. if (INTEL_INFO(dev)->gen >= 9) {
  1147. for_each_sprite(dev_priv, pipe, sprite) {
  1148. val = I915_READ(PLANE_CTL(pipe, sprite));
  1149. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1150. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1151. sprite, pipe_name(pipe));
  1152. }
  1153. } else if (IS_VALLEYVIEW(dev)) {
  1154. for_each_sprite(dev_priv, pipe, sprite) {
  1155. reg = SPCNTR(pipe, sprite);
  1156. val = I915_READ(reg);
  1157. I915_STATE_WARN(val & SP_ENABLE,
  1158. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1159. sprite_name(pipe, sprite), pipe_name(pipe));
  1160. }
  1161. } else if (INTEL_INFO(dev)->gen >= 7) {
  1162. reg = SPRCTL(pipe);
  1163. val = I915_READ(reg);
  1164. I915_STATE_WARN(val & SPRITE_ENABLE,
  1165. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1166. plane_name(pipe), pipe_name(pipe));
  1167. } else if (INTEL_INFO(dev)->gen >= 5) {
  1168. reg = DVSCNTR(pipe);
  1169. val = I915_READ(reg);
  1170. I915_STATE_WARN(val & DVS_ENABLE,
  1171. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1172. plane_name(pipe), pipe_name(pipe));
  1173. }
  1174. }
  1175. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1176. {
  1177. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1178. drm_crtc_vblank_put(crtc);
  1179. }
  1180. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1181. {
  1182. u32 val;
  1183. bool enabled;
  1184. I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1185. val = I915_READ(PCH_DREF_CONTROL);
  1186. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1187. DREF_SUPERSPREAD_SOURCE_MASK));
  1188. I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1189. }
  1190. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1191. enum pipe pipe)
  1192. {
  1193. int reg;
  1194. u32 val;
  1195. bool enabled;
  1196. reg = PCH_TRANSCONF(pipe);
  1197. val = I915_READ(reg);
  1198. enabled = !!(val & TRANS_ENABLE);
  1199. I915_STATE_WARN(enabled,
  1200. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1201. pipe_name(pipe));
  1202. }
  1203. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1204. enum pipe pipe, u32 port_sel, u32 val)
  1205. {
  1206. if ((val & DP_PORT_EN) == 0)
  1207. return false;
  1208. if (HAS_PCH_CPT(dev_priv->dev)) {
  1209. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1210. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1211. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1212. return false;
  1213. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1214. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1215. return false;
  1216. } else {
  1217. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1218. return false;
  1219. }
  1220. return true;
  1221. }
  1222. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1223. enum pipe pipe, u32 val)
  1224. {
  1225. if ((val & SDVO_ENABLE) == 0)
  1226. return false;
  1227. if (HAS_PCH_CPT(dev_priv->dev)) {
  1228. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1229. return false;
  1230. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1231. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1232. return false;
  1233. } else {
  1234. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1235. return false;
  1236. }
  1237. return true;
  1238. }
  1239. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1240. enum pipe pipe, u32 val)
  1241. {
  1242. if ((val & LVDS_PORT_EN) == 0)
  1243. return false;
  1244. if (HAS_PCH_CPT(dev_priv->dev)) {
  1245. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1246. return false;
  1247. } else {
  1248. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1249. return false;
  1250. }
  1251. return true;
  1252. }
  1253. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1254. enum pipe pipe, u32 val)
  1255. {
  1256. if ((val & ADPA_DAC_ENABLE) == 0)
  1257. return false;
  1258. if (HAS_PCH_CPT(dev_priv->dev)) {
  1259. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1260. return false;
  1261. } else {
  1262. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1263. return false;
  1264. }
  1265. return true;
  1266. }
  1267. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1268. enum pipe pipe, int reg, u32 port_sel)
  1269. {
  1270. u32 val = I915_READ(reg);
  1271. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1272. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1273. reg, pipe_name(pipe));
  1274. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1275. && (val & DP_PIPEB_SELECT),
  1276. "IBX PCH dp port still using transcoder B\n");
  1277. }
  1278. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1279. enum pipe pipe, int reg)
  1280. {
  1281. u32 val = I915_READ(reg);
  1282. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1283. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1284. reg, pipe_name(pipe));
  1285. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1286. && (val & SDVO_PIPE_B_SELECT),
  1287. "IBX PCH hdmi port still using transcoder B\n");
  1288. }
  1289. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1290. enum pipe pipe)
  1291. {
  1292. int reg;
  1293. u32 val;
  1294. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1295. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1296. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1297. reg = PCH_ADPA;
  1298. val = I915_READ(reg);
  1299. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1300. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1301. pipe_name(pipe));
  1302. reg = PCH_LVDS;
  1303. val = I915_READ(reg);
  1304. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1305. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1306. pipe_name(pipe));
  1307. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1308. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1309. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1310. }
  1311. static void intel_init_dpio(struct drm_device *dev)
  1312. {
  1313. struct drm_i915_private *dev_priv = dev->dev_private;
  1314. if (!IS_VALLEYVIEW(dev))
  1315. return;
  1316. /*
  1317. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1318. * CHV x1 PHY (DP/HDMI D)
  1319. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1320. */
  1321. if (IS_CHERRYVIEW(dev)) {
  1322. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1323. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1324. } else {
  1325. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1326. }
  1327. }
  1328. static void vlv_enable_pll(struct intel_crtc *crtc,
  1329. const struct intel_crtc_state *pipe_config)
  1330. {
  1331. struct drm_device *dev = crtc->base.dev;
  1332. struct drm_i915_private *dev_priv = dev->dev_private;
  1333. int reg = DPLL(crtc->pipe);
  1334. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1335. assert_pipe_disabled(dev_priv, crtc->pipe);
  1336. /* No really, not for ILK+ */
  1337. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1338. /* PLL is protected by panel, make sure we can write it */
  1339. if (IS_MOBILE(dev_priv->dev))
  1340. assert_panel_unlocked(dev_priv, crtc->pipe);
  1341. I915_WRITE(reg, dpll);
  1342. POSTING_READ(reg);
  1343. udelay(150);
  1344. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1345. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1346. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1347. POSTING_READ(DPLL_MD(crtc->pipe));
  1348. /* We do this three times for luck */
  1349. I915_WRITE(reg, dpll);
  1350. POSTING_READ(reg);
  1351. udelay(150); /* wait for warmup */
  1352. I915_WRITE(reg, dpll);
  1353. POSTING_READ(reg);
  1354. udelay(150); /* wait for warmup */
  1355. I915_WRITE(reg, dpll);
  1356. POSTING_READ(reg);
  1357. udelay(150); /* wait for warmup */
  1358. }
  1359. static void chv_enable_pll(struct intel_crtc *crtc,
  1360. const struct intel_crtc_state *pipe_config)
  1361. {
  1362. struct drm_device *dev = crtc->base.dev;
  1363. struct drm_i915_private *dev_priv = dev->dev_private;
  1364. int pipe = crtc->pipe;
  1365. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1366. u32 tmp;
  1367. assert_pipe_disabled(dev_priv, crtc->pipe);
  1368. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1369. mutex_lock(&dev_priv->dpio_lock);
  1370. /* Enable back the 10bit clock to display controller */
  1371. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1372. tmp |= DPIO_DCLKP_EN;
  1373. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1374. /*
  1375. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1376. */
  1377. udelay(1);
  1378. /* Enable PLL */
  1379. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1380. /* Check PLL is locked */
  1381. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1382. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1383. /* not sure when this should be written */
  1384. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1385. POSTING_READ(DPLL_MD(pipe));
  1386. mutex_unlock(&dev_priv->dpio_lock);
  1387. }
  1388. static int intel_num_dvo_pipes(struct drm_device *dev)
  1389. {
  1390. struct intel_crtc *crtc;
  1391. int count = 0;
  1392. for_each_intel_crtc(dev, crtc)
  1393. count += crtc->active &&
  1394. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1395. return count;
  1396. }
  1397. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1398. {
  1399. struct drm_device *dev = crtc->base.dev;
  1400. struct drm_i915_private *dev_priv = dev->dev_private;
  1401. int reg = DPLL(crtc->pipe);
  1402. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1403. assert_pipe_disabled(dev_priv, crtc->pipe);
  1404. /* No really, not for ILK+ */
  1405. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1406. /* PLL is protected by panel, make sure we can write it */
  1407. if (IS_MOBILE(dev) && !IS_I830(dev))
  1408. assert_panel_unlocked(dev_priv, crtc->pipe);
  1409. /* Enable DVO 2x clock on both PLLs if necessary */
  1410. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1411. /*
  1412. * It appears to be important that we don't enable this
  1413. * for the current pipe before otherwise configuring the
  1414. * PLL. No idea how this should be handled if multiple
  1415. * DVO outputs are enabled simultaneosly.
  1416. */
  1417. dpll |= DPLL_DVO_2X_MODE;
  1418. I915_WRITE(DPLL(!crtc->pipe),
  1419. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1420. }
  1421. /* Wait for the clocks to stabilize. */
  1422. POSTING_READ(reg);
  1423. udelay(150);
  1424. if (INTEL_INFO(dev)->gen >= 4) {
  1425. I915_WRITE(DPLL_MD(crtc->pipe),
  1426. crtc->config->dpll_hw_state.dpll_md);
  1427. } else {
  1428. /* The pixel multiplier can only be updated once the
  1429. * DPLL is enabled and the clocks are stable.
  1430. *
  1431. * So write it again.
  1432. */
  1433. I915_WRITE(reg, dpll);
  1434. }
  1435. /* We do this three times for luck */
  1436. I915_WRITE(reg, dpll);
  1437. POSTING_READ(reg);
  1438. udelay(150); /* wait for warmup */
  1439. I915_WRITE(reg, dpll);
  1440. POSTING_READ(reg);
  1441. udelay(150); /* wait for warmup */
  1442. I915_WRITE(reg, dpll);
  1443. POSTING_READ(reg);
  1444. udelay(150); /* wait for warmup */
  1445. }
  1446. /**
  1447. * i9xx_disable_pll - disable a PLL
  1448. * @dev_priv: i915 private structure
  1449. * @pipe: pipe PLL to disable
  1450. *
  1451. * Disable the PLL for @pipe, making sure the pipe is off first.
  1452. *
  1453. * Note! This is for pre-ILK only.
  1454. */
  1455. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1456. {
  1457. struct drm_device *dev = crtc->base.dev;
  1458. struct drm_i915_private *dev_priv = dev->dev_private;
  1459. enum pipe pipe = crtc->pipe;
  1460. /* Disable DVO 2x clock on both PLLs if necessary */
  1461. if (IS_I830(dev) &&
  1462. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1463. intel_num_dvo_pipes(dev) == 1) {
  1464. I915_WRITE(DPLL(PIPE_B),
  1465. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1466. I915_WRITE(DPLL(PIPE_A),
  1467. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1468. }
  1469. /* Don't disable pipe or pipe PLLs if needed */
  1470. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1471. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1472. return;
  1473. /* Make sure the pipe isn't still relying on us */
  1474. assert_pipe_disabled(dev_priv, pipe);
  1475. I915_WRITE(DPLL(pipe), 0);
  1476. POSTING_READ(DPLL(pipe));
  1477. }
  1478. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1479. {
  1480. u32 val = 0;
  1481. /* Make sure the pipe isn't still relying on us */
  1482. assert_pipe_disabled(dev_priv, pipe);
  1483. /*
  1484. * Leave integrated clock source and reference clock enabled for pipe B.
  1485. * The latter is needed for VGA hotplug / manual detection.
  1486. */
  1487. if (pipe == PIPE_B)
  1488. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1489. I915_WRITE(DPLL(pipe), val);
  1490. POSTING_READ(DPLL(pipe));
  1491. }
  1492. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1493. {
  1494. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1495. u32 val;
  1496. /* Make sure the pipe isn't still relying on us */
  1497. assert_pipe_disabled(dev_priv, pipe);
  1498. /* Set PLL en = 0 */
  1499. val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
  1500. if (pipe != PIPE_A)
  1501. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1502. I915_WRITE(DPLL(pipe), val);
  1503. POSTING_READ(DPLL(pipe));
  1504. mutex_lock(&dev_priv->dpio_lock);
  1505. /* Disable 10bit clock to display controller */
  1506. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1507. val &= ~DPIO_DCLKP_EN;
  1508. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1509. /* disable left/right clock distribution */
  1510. if (pipe != PIPE_B) {
  1511. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1512. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1513. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1514. } else {
  1515. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1516. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1517. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1518. }
  1519. mutex_unlock(&dev_priv->dpio_lock);
  1520. }
  1521. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1522. struct intel_digital_port *dport)
  1523. {
  1524. u32 port_mask;
  1525. int dpll_reg;
  1526. switch (dport->port) {
  1527. case PORT_B:
  1528. port_mask = DPLL_PORTB_READY_MASK;
  1529. dpll_reg = DPLL(0);
  1530. break;
  1531. case PORT_C:
  1532. port_mask = DPLL_PORTC_READY_MASK;
  1533. dpll_reg = DPLL(0);
  1534. break;
  1535. case PORT_D:
  1536. port_mask = DPLL_PORTD_READY_MASK;
  1537. dpll_reg = DPIO_PHY_STATUS;
  1538. break;
  1539. default:
  1540. BUG();
  1541. }
  1542. if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
  1543. WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
  1544. port_name(dport->port), I915_READ(dpll_reg));
  1545. }
  1546. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1547. {
  1548. struct drm_device *dev = crtc->base.dev;
  1549. struct drm_i915_private *dev_priv = dev->dev_private;
  1550. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1551. if (WARN_ON(pll == NULL))
  1552. return;
  1553. WARN_ON(!pll->config.crtc_mask);
  1554. if (pll->active == 0) {
  1555. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1556. WARN_ON(pll->on);
  1557. assert_shared_dpll_disabled(dev_priv, pll);
  1558. pll->mode_set(dev_priv, pll);
  1559. }
  1560. }
  1561. /**
  1562. * intel_enable_shared_dpll - enable PCH PLL
  1563. * @dev_priv: i915 private structure
  1564. * @pipe: pipe PLL to enable
  1565. *
  1566. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1567. * drives the transcoder clock.
  1568. */
  1569. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1570. {
  1571. struct drm_device *dev = crtc->base.dev;
  1572. struct drm_i915_private *dev_priv = dev->dev_private;
  1573. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1574. if (WARN_ON(pll == NULL))
  1575. return;
  1576. if (WARN_ON(pll->config.crtc_mask == 0))
  1577. return;
  1578. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1579. pll->name, pll->active, pll->on,
  1580. crtc->base.base.id);
  1581. if (pll->active++) {
  1582. WARN_ON(!pll->on);
  1583. assert_shared_dpll_enabled(dev_priv, pll);
  1584. return;
  1585. }
  1586. WARN_ON(pll->on);
  1587. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1588. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1589. pll->enable(dev_priv, pll);
  1590. pll->on = true;
  1591. }
  1592. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1593. {
  1594. struct drm_device *dev = crtc->base.dev;
  1595. struct drm_i915_private *dev_priv = dev->dev_private;
  1596. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1597. /* PCH only available on ILK+ */
  1598. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1599. if (WARN_ON(pll == NULL))
  1600. return;
  1601. if (WARN_ON(pll->config.crtc_mask == 0))
  1602. return;
  1603. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1604. pll->name, pll->active, pll->on,
  1605. crtc->base.base.id);
  1606. if (WARN_ON(pll->active == 0)) {
  1607. assert_shared_dpll_disabled(dev_priv, pll);
  1608. return;
  1609. }
  1610. assert_shared_dpll_enabled(dev_priv, pll);
  1611. WARN_ON(!pll->on);
  1612. if (--pll->active)
  1613. return;
  1614. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1615. pll->disable(dev_priv, pll);
  1616. pll->on = false;
  1617. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1618. }
  1619. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1620. enum pipe pipe)
  1621. {
  1622. struct drm_device *dev = dev_priv->dev;
  1623. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1624. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1625. uint32_t reg, val, pipeconf_val;
  1626. /* PCH only available on ILK+ */
  1627. BUG_ON(!HAS_PCH_SPLIT(dev));
  1628. /* Make sure PCH DPLL is enabled */
  1629. assert_shared_dpll_enabled(dev_priv,
  1630. intel_crtc_to_shared_dpll(intel_crtc));
  1631. /* FDI must be feeding us bits for PCH ports */
  1632. assert_fdi_tx_enabled(dev_priv, pipe);
  1633. assert_fdi_rx_enabled(dev_priv, pipe);
  1634. if (HAS_PCH_CPT(dev)) {
  1635. /* Workaround: Set the timing override bit before enabling the
  1636. * pch transcoder. */
  1637. reg = TRANS_CHICKEN2(pipe);
  1638. val = I915_READ(reg);
  1639. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1640. I915_WRITE(reg, val);
  1641. }
  1642. reg = PCH_TRANSCONF(pipe);
  1643. val = I915_READ(reg);
  1644. pipeconf_val = I915_READ(PIPECONF(pipe));
  1645. if (HAS_PCH_IBX(dev_priv->dev)) {
  1646. /*
  1647. * make the BPC in transcoder be consistent with
  1648. * that in pipeconf reg.
  1649. */
  1650. val &= ~PIPECONF_BPC_MASK;
  1651. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1652. }
  1653. val &= ~TRANS_INTERLACE_MASK;
  1654. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1655. if (HAS_PCH_IBX(dev_priv->dev) &&
  1656. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1657. val |= TRANS_LEGACY_INTERLACED_ILK;
  1658. else
  1659. val |= TRANS_INTERLACED;
  1660. else
  1661. val |= TRANS_PROGRESSIVE;
  1662. I915_WRITE(reg, val | TRANS_ENABLE);
  1663. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1664. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1665. }
  1666. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1667. enum transcoder cpu_transcoder)
  1668. {
  1669. u32 val, pipeconf_val;
  1670. /* PCH only available on ILK+ */
  1671. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1672. /* FDI must be feeding us bits for PCH ports */
  1673. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1674. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1675. /* Workaround: set timing override bit. */
  1676. val = I915_READ(_TRANSA_CHICKEN2);
  1677. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1678. I915_WRITE(_TRANSA_CHICKEN2, val);
  1679. val = TRANS_ENABLE;
  1680. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1681. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1682. PIPECONF_INTERLACED_ILK)
  1683. val |= TRANS_INTERLACED;
  1684. else
  1685. val |= TRANS_PROGRESSIVE;
  1686. I915_WRITE(LPT_TRANSCONF, val);
  1687. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1688. DRM_ERROR("Failed to enable PCH transcoder\n");
  1689. }
  1690. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1691. enum pipe pipe)
  1692. {
  1693. struct drm_device *dev = dev_priv->dev;
  1694. uint32_t reg, val;
  1695. /* FDI relies on the transcoder */
  1696. assert_fdi_tx_disabled(dev_priv, pipe);
  1697. assert_fdi_rx_disabled(dev_priv, pipe);
  1698. /* Ports must be off as well */
  1699. assert_pch_ports_disabled(dev_priv, pipe);
  1700. reg = PCH_TRANSCONF(pipe);
  1701. val = I915_READ(reg);
  1702. val &= ~TRANS_ENABLE;
  1703. I915_WRITE(reg, val);
  1704. /* wait for PCH transcoder off, transcoder state */
  1705. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1706. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1707. if (!HAS_PCH_IBX(dev)) {
  1708. /* Workaround: Clear the timing override chicken bit again. */
  1709. reg = TRANS_CHICKEN2(pipe);
  1710. val = I915_READ(reg);
  1711. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1712. I915_WRITE(reg, val);
  1713. }
  1714. }
  1715. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1716. {
  1717. u32 val;
  1718. val = I915_READ(LPT_TRANSCONF);
  1719. val &= ~TRANS_ENABLE;
  1720. I915_WRITE(LPT_TRANSCONF, val);
  1721. /* wait for PCH transcoder off, transcoder state */
  1722. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1723. DRM_ERROR("Failed to disable PCH transcoder\n");
  1724. /* Workaround: clear timing override bit. */
  1725. val = I915_READ(_TRANSA_CHICKEN2);
  1726. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1727. I915_WRITE(_TRANSA_CHICKEN2, val);
  1728. }
  1729. /**
  1730. * intel_enable_pipe - enable a pipe, asserting requirements
  1731. * @crtc: crtc responsible for the pipe
  1732. *
  1733. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1734. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1735. */
  1736. static void intel_enable_pipe(struct intel_crtc *crtc)
  1737. {
  1738. struct drm_device *dev = crtc->base.dev;
  1739. struct drm_i915_private *dev_priv = dev->dev_private;
  1740. enum pipe pipe = crtc->pipe;
  1741. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1742. pipe);
  1743. enum pipe pch_transcoder;
  1744. int reg;
  1745. u32 val;
  1746. assert_planes_disabled(dev_priv, pipe);
  1747. assert_cursor_disabled(dev_priv, pipe);
  1748. assert_sprites_disabled(dev_priv, pipe);
  1749. if (HAS_PCH_LPT(dev_priv->dev))
  1750. pch_transcoder = TRANSCODER_A;
  1751. else
  1752. pch_transcoder = pipe;
  1753. /*
  1754. * A pipe without a PLL won't actually be able to drive bits from
  1755. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1756. * need the check.
  1757. */
  1758. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1759. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1760. assert_dsi_pll_enabled(dev_priv);
  1761. else
  1762. assert_pll_enabled(dev_priv, pipe);
  1763. else {
  1764. if (crtc->config->has_pch_encoder) {
  1765. /* if driving the PCH, we need FDI enabled */
  1766. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1767. assert_fdi_tx_pll_enabled(dev_priv,
  1768. (enum pipe) cpu_transcoder);
  1769. }
  1770. /* FIXME: assert CPU port conditions for SNB+ */
  1771. }
  1772. reg = PIPECONF(cpu_transcoder);
  1773. val = I915_READ(reg);
  1774. if (val & PIPECONF_ENABLE) {
  1775. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1776. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1777. return;
  1778. }
  1779. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1780. POSTING_READ(reg);
  1781. }
  1782. /**
  1783. * intel_disable_pipe - disable a pipe, asserting requirements
  1784. * @crtc: crtc whose pipes is to be disabled
  1785. *
  1786. * Disable the pipe of @crtc, making sure that various hardware
  1787. * specific requirements are met, if applicable, e.g. plane
  1788. * disabled, panel fitter off, etc.
  1789. *
  1790. * Will wait until the pipe has shut down before returning.
  1791. */
  1792. static void intel_disable_pipe(struct intel_crtc *crtc)
  1793. {
  1794. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1795. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1796. enum pipe pipe = crtc->pipe;
  1797. int reg;
  1798. u32 val;
  1799. /*
  1800. * Make sure planes won't keep trying to pump pixels to us,
  1801. * or we might hang the display.
  1802. */
  1803. assert_planes_disabled(dev_priv, pipe);
  1804. assert_cursor_disabled(dev_priv, pipe);
  1805. assert_sprites_disabled(dev_priv, pipe);
  1806. reg = PIPECONF(cpu_transcoder);
  1807. val = I915_READ(reg);
  1808. if ((val & PIPECONF_ENABLE) == 0)
  1809. return;
  1810. /*
  1811. * Double wide has implications for planes
  1812. * so best keep it disabled when not needed.
  1813. */
  1814. if (crtc->config->double_wide)
  1815. val &= ~PIPECONF_DOUBLE_WIDE;
  1816. /* Don't disable pipe or pipe PLLs if needed */
  1817. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1818. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1819. val &= ~PIPECONF_ENABLE;
  1820. I915_WRITE(reg, val);
  1821. if ((val & PIPECONF_ENABLE) == 0)
  1822. intel_wait_for_pipe_off(crtc);
  1823. }
  1824. /*
  1825. * Plane regs are double buffered, going from enabled->disabled needs a
  1826. * trigger in order to latch. The display address reg provides this.
  1827. */
  1828. void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
  1829. enum plane plane)
  1830. {
  1831. struct drm_device *dev = dev_priv->dev;
  1832. u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
  1833. I915_WRITE(reg, I915_READ(reg));
  1834. POSTING_READ(reg);
  1835. }
  1836. /**
  1837. * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  1838. * @plane: plane to be enabled
  1839. * @crtc: crtc for the plane
  1840. *
  1841. * Enable @plane on @crtc, making sure that the pipe is running first.
  1842. */
  1843. static void intel_enable_primary_hw_plane(struct drm_plane *plane,
  1844. struct drm_crtc *crtc)
  1845. {
  1846. struct drm_device *dev = plane->dev;
  1847. struct drm_i915_private *dev_priv = dev->dev_private;
  1848. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1849. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1850. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1851. if (intel_crtc->primary_enabled)
  1852. return;
  1853. intel_crtc->primary_enabled = true;
  1854. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1855. crtc->x, crtc->y);
  1856. /*
  1857. * BDW signals flip done immediately if the plane
  1858. * is disabled, even if the plane enable is already
  1859. * armed to occur at the next vblank :(
  1860. */
  1861. if (IS_BROADWELL(dev))
  1862. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1863. }
  1864. /**
  1865. * intel_disable_primary_hw_plane - disable the primary hardware plane
  1866. * @plane: plane to be disabled
  1867. * @crtc: crtc for the plane
  1868. *
  1869. * Disable @plane on @crtc, making sure that the pipe is running first.
  1870. */
  1871. static void intel_disable_primary_hw_plane(struct drm_plane *plane,
  1872. struct drm_crtc *crtc)
  1873. {
  1874. struct drm_device *dev = plane->dev;
  1875. struct drm_i915_private *dev_priv = dev->dev_private;
  1876. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1877. if (WARN_ON(!intel_crtc->active))
  1878. return;
  1879. if (!intel_crtc->primary_enabled)
  1880. return;
  1881. intel_crtc->primary_enabled = false;
  1882. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1883. crtc->x, crtc->y);
  1884. }
  1885. static bool need_vtd_wa(struct drm_device *dev)
  1886. {
  1887. #ifdef CONFIG_INTEL_IOMMU
  1888. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1889. return true;
  1890. #endif
  1891. return false;
  1892. }
  1893. int
  1894. intel_fb_align_height(struct drm_device *dev, int height,
  1895. uint32_t pixel_format,
  1896. uint64_t fb_format_modifier)
  1897. {
  1898. int tile_height;
  1899. uint32_t bits_per_pixel;
  1900. switch (fb_format_modifier) {
  1901. case DRM_FORMAT_MOD_NONE:
  1902. tile_height = 1;
  1903. break;
  1904. case I915_FORMAT_MOD_X_TILED:
  1905. tile_height = IS_GEN2(dev) ? 16 : 8;
  1906. break;
  1907. case I915_FORMAT_MOD_Y_TILED:
  1908. tile_height = 32;
  1909. break;
  1910. case I915_FORMAT_MOD_Yf_TILED:
  1911. bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
  1912. switch (bits_per_pixel) {
  1913. default:
  1914. case 8:
  1915. tile_height = 64;
  1916. break;
  1917. case 16:
  1918. case 32:
  1919. tile_height = 32;
  1920. break;
  1921. case 64:
  1922. tile_height = 16;
  1923. break;
  1924. case 128:
  1925. WARN_ONCE(1,
  1926. "128-bit pixels are not supported for display!");
  1927. tile_height = 16;
  1928. break;
  1929. }
  1930. break;
  1931. default:
  1932. MISSING_CASE(fb_format_modifier);
  1933. tile_height = 1;
  1934. break;
  1935. }
  1936. return ALIGN(height, tile_height);
  1937. }
  1938. int
  1939. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  1940. struct drm_framebuffer *fb,
  1941. struct intel_engine_cs *pipelined)
  1942. {
  1943. struct drm_device *dev = fb->dev;
  1944. struct drm_i915_private *dev_priv = dev->dev_private;
  1945. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1946. u32 alignment;
  1947. int ret;
  1948. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1949. switch (fb->modifier[0]) {
  1950. case DRM_FORMAT_MOD_NONE:
  1951. if (INTEL_INFO(dev)->gen >= 9)
  1952. alignment = 256 * 1024;
  1953. else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1954. alignment = 128 * 1024;
  1955. else if (INTEL_INFO(dev)->gen >= 4)
  1956. alignment = 4 * 1024;
  1957. else
  1958. alignment = 64 * 1024;
  1959. break;
  1960. case I915_FORMAT_MOD_X_TILED:
  1961. if (INTEL_INFO(dev)->gen >= 9)
  1962. alignment = 256 * 1024;
  1963. else {
  1964. /* pin() will align the object as required by fence */
  1965. alignment = 0;
  1966. }
  1967. break;
  1968. case I915_FORMAT_MOD_Y_TILED:
  1969. case I915_FORMAT_MOD_Yf_TILED:
  1970. if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
  1971. "Y tiling bo slipped through, driver bug!\n"))
  1972. return -EINVAL;
  1973. alignment = 1 * 1024 * 1024;
  1974. break;
  1975. default:
  1976. MISSING_CASE(fb->modifier[0]);
  1977. return -EINVAL;
  1978. }
  1979. /* Note that the w/a also requires 64 PTE of padding following the
  1980. * bo. We currently fill all unused PTE with the shadow page and so
  1981. * we should always have valid PTE following the scanout preventing
  1982. * the VT-d warning.
  1983. */
  1984. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1985. alignment = 256 * 1024;
  1986. /*
  1987. * Global gtt pte registers are special registers which actually forward
  1988. * writes to a chunk of system memory. Which means that there is no risk
  1989. * that the register values disappear as soon as we call
  1990. * intel_runtime_pm_put(), so it is correct to wrap only the
  1991. * pin/unpin/fence and not more.
  1992. */
  1993. intel_runtime_pm_get(dev_priv);
  1994. dev_priv->mm.interruptible = false;
  1995. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1996. if (ret)
  1997. goto err_interruptible;
  1998. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1999. * fence, whereas 965+ only requires a fence if using
  2000. * framebuffer compression. For simplicity, we always install
  2001. * a fence as the cost is not that onerous.
  2002. */
  2003. ret = i915_gem_object_get_fence(obj);
  2004. if (ret)
  2005. goto err_unpin;
  2006. i915_gem_object_pin_fence(obj);
  2007. dev_priv->mm.interruptible = true;
  2008. intel_runtime_pm_put(dev_priv);
  2009. return 0;
  2010. err_unpin:
  2011. i915_gem_object_unpin_from_display_plane(obj);
  2012. err_interruptible:
  2013. dev_priv->mm.interruptible = true;
  2014. intel_runtime_pm_put(dev_priv);
  2015. return ret;
  2016. }
  2017. static void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
  2018. {
  2019. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2020. i915_gem_object_unpin_fence(obj);
  2021. i915_gem_object_unpin_from_display_plane(obj);
  2022. }
  2023. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  2024. * is assumed to be a power-of-two. */
  2025. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  2026. unsigned int tiling_mode,
  2027. unsigned int cpp,
  2028. unsigned int pitch)
  2029. {
  2030. if (tiling_mode != I915_TILING_NONE) {
  2031. unsigned int tile_rows, tiles;
  2032. tile_rows = *y / 8;
  2033. *y %= 8;
  2034. tiles = *x / (512/cpp);
  2035. *x %= 512/cpp;
  2036. return tile_rows * pitch * 8 + tiles * 4096;
  2037. } else {
  2038. unsigned int offset;
  2039. offset = *y * pitch + *x * cpp;
  2040. *y = 0;
  2041. *x = (offset & 4095) / cpp;
  2042. return offset & -4096;
  2043. }
  2044. }
  2045. static int i9xx_format_to_fourcc(int format)
  2046. {
  2047. switch (format) {
  2048. case DISPPLANE_8BPP:
  2049. return DRM_FORMAT_C8;
  2050. case DISPPLANE_BGRX555:
  2051. return DRM_FORMAT_XRGB1555;
  2052. case DISPPLANE_BGRX565:
  2053. return DRM_FORMAT_RGB565;
  2054. default:
  2055. case DISPPLANE_BGRX888:
  2056. return DRM_FORMAT_XRGB8888;
  2057. case DISPPLANE_RGBX888:
  2058. return DRM_FORMAT_XBGR8888;
  2059. case DISPPLANE_BGRX101010:
  2060. return DRM_FORMAT_XRGB2101010;
  2061. case DISPPLANE_RGBX101010:
  2062. return DRM_FORMAT_XBGR2101010;
  2063. }
  2064. }
  2065. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2066. {
  2067. switch (format) {
  2068. case PLANE_CTL_FORMAT_RGB_565:
  2069. return DRM_FORMAT_RGB565;
  2070. default:
  2071. case PLANE_CTL_FORMAT_XRGB_8888:
  2072. if (rgb_order) {
  2073. if (alpha)
  2074. return DRM_FORMAT_ABGR8888;
  2075. else
  2076. return DRM_FORMAT_XBGR8888;
  2077. } else {
  2078. if (alpha)
  2079. return DRM_FORMAT_ARGB8888;
  2080. else
  2081. return DRM_FORMAT_XRGB8888;
  2082. }
  2083. case PLANE_CTL_FORMAT_XRGB_2101010:
  2084. if (rgb_order)
  2085. return DRM_FORMAT_XBGR2101010;
  2086. else
  2087. return DRM_FORMAT_XRGB2101010;
  2088. }
  2089. }
  2090. static bool
  2091. intel_alloc_plane_obj(struct intel_crtc *crtc,
  2092. struct intel_initial_plane_config *plane_config)
  2093. {
  2094. struct drm_device *dev = crtc->base.dev;
  2095. struct drm_i915_gem_object *obj = NULL;
  2096. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2097. struct drm_framebuffer *fb = &plane_config->fb->base;
  2098. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2099. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2100. PAGE_SIZE);
  2101. size_aligned -= base_aligned;
  2102. if (plane_config->size == 0)
  2103. return false;
  2104. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2105. base_aligned,
  2106. base_aligned,
  2107. size_aligned);
  2108. if (!obj)
  2109. return false;
  2110. obj->tiling_mode = plane_config->tiling;
  2111. if (obj->tiling_mode == I915_TILING_X)
  2112. obj->stride = fb->pitches[0];
  2113. mode_cmd.pixel_format = fb->pixel_format;
  2114. mode_cmd.width = fb->width;
  2115. mode_cmd.height = fb->height;
  2116. mode_cmd.pitches[0] = fb->pitches[0];
  2117. mode_cmd.modifier[0] = fb->modifier[0];
  2118. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2119. mutex_lock(&dev->struct_mutex);
  2120. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2121. &mode_cmd, obj)) {
  2122. DRM_DEBUG_KMS("intel fb init failed\n");
  2123. goto out_unref_obj;
  2124. }
  2125. obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
  2126. mutex_unlock(&dev->struct_mutex);
  2127. DRM_DEBUG_KMS("plane fb obj %p\n", obj);
  2128. return true;
  2129. out_unref_obj:
  2130. drm_gem_object_unreference(&obj->base);
  2131. mutex_unlock(&dev->struct_mutex);
  2132. return false;
  2133. }
  2134. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2135. static void
  2136. update_state_fb(struct drm_plane *plane)
  2137. {
  2138. if (plane->fb == plane->state->fb)
  2139. return;
  2140. if (plane->state->fb)
  2141. drm_framebuffer_unreference(plane->state->fb);
  2142. plane->state->fb = plane->fb;
  2143. if (plane->state->fb)
  2144. drm_framebuffer_reference(plane->state->fb);
  2145. }
  2146. static void
  2147. intel_find_plane_obj(struct intel_crtc *intel_crtc,
  2148. struct intel_initial_plane_config *plane_config)
  2149. {
  2150. struct drm_device *dev = intel_crtc->base.dev;
  2151. struct drm_i915_private *dev_priv = dev->dev_private;
  2152. struct drm_crtc *c;
  2153. struct intel_crtc *i;
  2154. struct drm_i915_gem_object *obj;
  2155. if (!plane_config->fb)
  2156. return;
  2157. if (intel_alloc_plane_obj(intel_crtc, plane_config)) {
  2158. struct drm_plane *primary = intel_crtc->base.primary;
  2159. primary->fb = &plane_config->fb->base;
  2160. primary->state->crtc = &intel_crtc->base;
  2161. update_state_fb(primary);
  2162. return;
  2163. }
  2164. kfree(plane_config->fb);
  2165. /*
  2166. * Failed to alloc the obj, check to see if we should share
  2167. * an fb with another CRTC instead
  2168. */
  2169. for_each_crtc(dev, c) {
  2170. i = to_intel_crtc(c);
  2171. if (c == &intel_crtc->base)
  2172. continue;
  2173. if (!i->active)
  2174. continue;
  2175. obj = intel_fb_obj(c->primary->fb);
  2176. if (obj == NULL)
  2177. continue;
  2178. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2179. struct drm_plane *primary = intel_crtc->base.primary;
  2180. if (obj->tiling_mode != I915_TILING_NONE)
  2181. dev_priv->preserve_bios_swizzle = true;
  2182. drm_framebuffer_reference(c->primary->fb);
  2183. primary->fb = c->primary->fb;
  2184. primary->state->crtc = &intel_crtc->base;
  2185. update_state_fb(intel_crtc->base.primary);
  2186. obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  2187. break;
  2188. }
  2189. }
  2190. }
  2191. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2192. struct drm_framebuffer *fb,
  2193. int x, int y)
  2194. {
  2195. struct drm_device *dev = crtc->dev;
  2196. struct drm_i915_private *dev_priv = dev->dev_private;
  2197. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2198. struct drm_i915_gem_object *obj;
  2199. int plane = intel_crtc->plane;
  2200. unsigned long linear_offset;
  2201. u32 dspcntr;
  2202. u32 reg = DSPCNTR(plane);
  2203. int pixel_size;
  2204. if (!intel_crtc->primary_enabled) {
  2205. I915_WRITE(reg, 0);
  2206. if (INTEL_INFO(dev)->gen >= 4)
  2207. I915_WRITE(DSPSURF(plane), 0);
  2208. else
  2209. I915_WRITE(DSPADDR(plane), 0);
  2210. POSTING_READ(reg);
  2211. return;
  2212. }
  2213. obj = intel_fb_obj(fb);
  2214. if (WARN_ON(obj == NULL))
  2215. return;
  2216. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2217. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2218. dspcntr |= DISPLAY_PLANE_ENABLE;
  2219. if (INTEL_INFO(dev)->gen < 4) {
  2220. if (intel_crtc->pipe == PIPE_B)
  2221. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2222. /* pipesrc and dspsize control the size that is scaled from,
  2223. * which should always be the user's requested size.
  2224. */
  2225. I915_WRITE(DSPSIZE(plane),
  2226. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2227. (intel_crtc->config->pipe_src_w - 1));
  2228. I915_WRITE(DSPPOS(plane), 0);
  2229. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2230. I915_WRITE(PRIMSIZE(plane),
  2231. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2232. (intel_crtc->config->pipe_src_w - 1));
  2233. I915_WRITE(PRIMPOS(plane), 0);
  2234. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2235. }
  2236. switch (fb->pixel_format) {
  2237. case DRM_FORMAT_C8:
  2238. dspcntr |= DISPPLANE_8BPP;
  2239. break;
  2240. case DRM_FORMAT_XRGB1555:
  2241. case DRM_FORMAT_ARGB1555:
  2242. dspcntr |= DISPPLANE_BGRX555;
  2243. break;
  2244. case DRM_FORMAT_RGB565:
  2245. dspcntr |= DISPPLANE_BGRX565;
  2246. break;
  2247. case DRM_FORMAT_XRGB8888:
  2248. case DRM_FORMAT_ARGB8888:
  2249. dspcntr |= DISPPLANE_BGRX888;
  2250. break;
  2251. case DRM_FORMAT_XBGR8888:
  2252. case DRM_FORMAT_ABGR8888:
  2253. dspcntr |= DISPPLANE_RGBX888;
  2254. break;
  2255. case DRM_FORMAT_XRGB2101010:
  2256. case DRM_FORMAT_ARGB2101010:
  2257. dspcntr |= DISPPLANE_BGRX101010;
  2258. break;
  2259. case DRM_FORMAT_XBGR2101010:
  2260. case DRM_FORMAT_ABGR2101010:
  2261. dspcntr |= DISPPLANE_RGBX101010;
  2262. break;
  2263. default:
  2264. BUG();
  2265. }
  2266. if (INTEL_INFO(dev)->gen >= 4 &&
  2267. obj->tiling_mode != I915_TILING_NONE)
  2268. dspcntr |= DISPPLANE_TILED;
  2269. if (IS_G4X(dev))
  2270. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2271. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2272. if (INTEL_INFO(dev)->gen >= 4) {
  2273. intel_crtc->dspaddr_offset =
  2274. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2275. pixel_size,
  2276. fb->pitches[0]);
  2277. linear_offset -= intel_crtc->dspaddr_offset;
  2278. } else {
  2279. intel_crtc->dspaddr_offset = linear_offset;
  2280. }
  2281. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2282. dspcntr |= DISPPLANE_ROTATE_180;
  2283. x += (intel_crtc->config->pipe_src_w - 1);
  2284. y += (intel_crtc->config->pipe_src_h - 1);
  2285. /* Finding the last pixel of the last line of the display
  2286. data and adding to linear_offset*/
  2287. linear_offset +=
  2288. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2289. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2290. }
  2291. I915_WRITE(reg, dspcntr);
  2292. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2293. if (INTEL_INFO(dev)->gen >= 4) {
  2294. I915_WRITE(DSPSURF(plane),
  2295. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2296. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2297. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2298. } else
  2299. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2300. POSTING_READ(reg);
  2301. }
  2302. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2303. struct drm_framebuffer *fb,
  2304. int x, int y)
  2305. {
  2306. struct drm_device *dev = crtc->dev;
  2307. struct drm_i915_private *dev_priv = dev->dev_private;
  2308. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2309. struct drm_i915_gem_object *obj;
  2310. int plane = intel_crtc->plane;
  2311. unsigned long linear_offset;
  2312. u32 dspcntr;
  2313. u32 reg = DSPCNTR(plane);
  2314. int pixel_size;
  2315. if (!intel_crtc->primary_enabled) {
  2316. I915_WRITE(reg, 0);
  2317. I915_WRITE(DSPSURF(plane), 0);
  2318. POSTING_READ(reg);
  2319. return;
  2320. }
  2321. obj = intel_fb_obj(fb);
  2322. if (WARN_ON(obj == NULL))
  2323. return;
  2324. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2325. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2326. dspcntr |= DISPLAY_PLANE_ENABLE;
  2327. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2328. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2329. switch (fb->pixel_format) {
  2330. case DRM_FORMAT_C8:
  2331. dspcntr |= DISPPLANE_8BPP;
  2332. break;
  2333. case DRM_FORMAT_RGB565:
  2334. dspcntr |= DISPPLANE_BGRX565;
  2335. break;
  2336. case DRM_FORMAT_XRGB8888:
  2337. case DRM_FORMAT_ARGB8888:
  2338. dspcntr |= DISPPLANE_BGRX888;
  2339. break;
  2340. case DRM_FORMAT_XBGR8888:
  2341. case DRM_FORMAT_ABGR8888:
  2342. dspcntr |= DISPPLANE_RGBX888;
  2343. break;
  2344. case DRM_FORMAT_XRGB2101010:
  2345. case DRM_FORMAT_ARGB2101010:
  2346. dspcntr |= DISPPLANE_BGRX101010;
  2347. break;
  2348. case DRM_FORMAT_XBGR2101010:
  2349. case DRM_FORMAT_ABGR2101010:
  2350. dspcntr |= DISPPLANE_RGBX101010;
  2351. break;
  2352. default:
  2353. BUG();
  2354. }
  2355. if (obj->tiling_mode != I915_TILING_NONE)
  2356. dspcntr |= DISPPLANE_TILED;
  2357. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2358. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2359. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2360. intel_crtc->dspaddr_offset =
  2361. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2362. pixel_size,
  2363. fb->pitches[0]);
  2364. linear_offset -= intel_crtc->dspaddr_offset;
  2365. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2366. dspcntr |= DISPPLANE_ROTATE_180;
  2367. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2368. x += (intel_crtc->config->pipe_src_w - 1);
  2369. y += (intel_crtc->config->pipe_src_h - 1);
  2370. /* Finding the last pixel of the last line of the display
  2371. data and adding to linear_offset*/
  2372. linear_offset +=
  2373. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2374. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2375. }
  2376. }
  2377. I915_WRITE(reg, dspcntr);
  2378. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2379. I915_WRITE(DSPSURF(plane),
  2380. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2381. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2382. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2383. } else {
  2384. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2385. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2386. }
  2387. POSTING_READ(reg);
  2388. }
  2389. u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
  2390. uint32_t pixel_format)
  2391. {
  2392. u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
  2393. /*
  2394. * The stride is either expressed as a multiple of 64 bytes
  2395. * chunks for linear buffers or in number of tiles for tiled
  2396. * buffers.
  2397. */
  2398. switch (fb_modifier) {
  2399. case DRM_FORMAT_MOD_NONE:
  2400. return 64;
  2401. case I915_FORMAT_MOD_X_TILED:
  2402. if (INTEL_INFO(dev)->gen == 2)
  2403. return 128;
  2404. return 512;
  2405. case I915_FORMAT_MOD_Y_TILED:
  2406. /* No need to check for old gens and Y tiling since this is
  2407. * about the display engine and those will be blocked before
  2408. * we get here.
  2409. */
  2410. return 128;
  2411. case I915_FORMAT_MOD_Yf_TILED:
  2412. if (bits_per_pixel == 8)
  2413. return 64;
  2414. else
  2415. return 128;
  2416. default:
  2417. MISSING_CASE(fb_modifier);
  2418. return 64;
  2419. }
  2420. }
  2421. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2422. struct drm_framebuffer *fb,
  2423. int x, int y)
  2424. {
  2425. struct drm_device *dev = crtc->dev;
  2426. struct drm_i915_private *dev_priv = dev->dev_private;
  2427. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2428. struct drm_i915_gem_object *obj;
  2429. int pipe = intel_crtc->pipe;
  2430. u32 plane_ctl, stride_div;
  2431. if (!intel_crtc->primary_enabled) {
  2432. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2433. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2434. POSTING_READ(PLANE_CTL(pipe, 0));
  2435. return;
  2436. }
  2437. plane_ctl = PLANE_CTL_ENABLE |
  2438. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2439. PLANE_CTL_PIPE_CSC_ENABLE;
  2440. switch (fb->pixel_format) {
  2441. case DRM_FORMAT_RGB565:
  2442. plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
  2443. break;
  2444. case DRM_FORMAT_XRGB8888:
  2445. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2446. break;
  2447. case DRM_FORMAT_ARGB8888:
  2448. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2449. plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2450. break;
  2451. case DRM_FORMAT_XBGR8888:
  2452. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2453. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2454. break;
  2455. case DRM_FORMAT_ABGR8888:
  2456. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2457. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2458. plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2459. break;
  2460. case DRM_FORMAT_XRGB2101010:
  2461. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2462. break;
  2463. case DRM_FORMAT_XBGR2101010:
  2464. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2465. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2466. break;
  2467. default:
  2468. BUG();
  2469. }
  2470. switch (fb->modifier[0]) {
  2471. case DRM_FORMAT_MOD_NONE:
  2472. break;
  2473. case I915_FORMAT_MOD_X_TILED:
  2474. plane_ctl |= PLANE_CTL_TILED_X;
  2475. break;
  2476. case I915_FORMAT_MOD_Y_TILED:
  2477. plane_ctl |= PLANE_CTL_TILED_Y;
  2478. break;
  2479. case I915_FORMAT_MOD_Yf_TILED:
  2480. plane_ctl |= PLANE_CTL_TILED_YF;
  2481. break;
  2482. default:
  2483. MISSING_CASE(fb->modifier[0]);
  2484. }
  2485. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2486. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180))
  2487. plane_ctl |= PLANE_CTL_ROTATE_180;
  2488. obj = intel_fb_obj(fb);
  2489. stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
  2490. fb->pixel_format);
  2491. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2492. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2493. I915_WRITE(PLANE_OFFSET(pipe, 0), (y << 16) | x);
  2494. I915_WRITE(PLANE_SIZE(pipe, 0),
  2495. (intel_crtc->config->pipe_src_h - 1) << 16 |
  2496. (intel_crtc->config->pipe_src_w - 1));
  2497. I915_WRITE(PLANE_STRIDE(pipe, 0), fb->pitches[0] / stride_div);
  2498. I915_WRITE(PLANE_SURF(pipe, 0), i915_gem_obj_ggtt_offset(obj));
  2499. POSTING_READ(PLANE_SURF(pipe, 0));
  2500. }
  2501. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2502. static int
  2503. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2504. int x, int y, enum mode_set_atomic state)
  2505. {
  2506. struct drm_device *dev = crtc->dev;
  2507. struct drm_i915_private *dev_priv = dev->dev_private;
  2508. if (dev_priv->display.disable_fbc)
  2509. dev_priv->display.disable_fbc(dev);
  2510. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2511. return 0;
  2512. }
  2513. static void intel_complete_page_flips(struct drm_device *dev)
  2514. {
  2515. struct drm_crtc *crtc;
  2516. for_each_crtc(dev, crtc) {
  2517. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2518. enum plane plane = intel_crtc->plane;
  2519. intel_prepare_page_flip(dev, plane);
  2520. intel_finish_page_flip_plane(dev, plane);
  2521. }
  2522. }
  2523. static void intel_update_primary_planes(struct drm_device *dev)
  2524. {
  2525. struct drm_i915_private *dev_priv = dev->dev_private;
  2526. struct drm_crtc *crtc;
  2527. for_each_crtc(dev, crtc) {
  2528. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2529. drm_modeset_lock(&crtc->mutex, NULL);
  2530. /*
  2531. * FIXME: Once we have proper support for primary planes (and
  2532. * disabling them without disabling the entire crtc) allow again
  2533. * a NULL crtc->primary->fb.
  2534. */
  2535. if (intel_crtc->active && crtc->primary->fb)
  2536. dev_priv->display.update_primary_plane(crtc,
  2537. crtc->primary->fb,
  2538. crtc->x,
  2539. crtc->y);
  2540. drm_modeset_unlock(&crtc->mutex);
  2541. }
  2542. }
  2543. void intel_prepare_reset(struct drm_device *dev)
  2544. {
  2545. struct drm_i915_private *dev_priv = to_i915(dev);
  2546. struct intel_crtc *crtc;
  2547. /* no reset support for gen2 */
  2548. if (IS_GEN2(dev))
  2549. return;
  2550. /* reset doesn't touch the display */
  2551. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2552. return;
  2553. drm_modeset_lock_all(dev);
  2554. /*
  2555. * Disabling the crtcs gracefully seems nicer. Also the
  2556. * g33 docs say we should at least disable all the planes.
  2557. */
  2558. for_each_intel_crtc(dev, crtc) {
  2559. if (crtc->active)
  2560. dev_priv->display.crtc_disable(&crtc->base);
  2561. }
  2562. }
  2563. void intel_finish_reset(struct drm_device *dev)
  2564. {
  2565. struct drm_i915_private *dev_priv = to_i915(dev);
  2566. /*
  2567. * Flips in the rings will be nuked by the reset,
  2568. * so complete all pending flips so that user space
  2569. * will get its events and not get stuck.
  2570. */
  2571. intel_complete_page_flips(dev);
  2572. /* no reset support for gen2 */
  2573. if (IS_GEN2(dev))
  2574. return;
  2575. /* reset doesn't touch the display */
  2576. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2577. /*
  2578. * Flips in the rings have been nuked by the reset,
  2579. * so update the base address of all primary
  2580. * planes to the the last fb to make sure we're
  2581. * showing the correct fb after a reset.
  2582. */
  2583. intel_update_primary_planes(dev);
  2584. return;
  2585. }
  2586. /*
  2587. * The display has been reset as well,
  2588. * so need a full re-initialization.
  2589. */
  2590. intel_runtime_pm_disable_interrupts(dev_priv);
  2591. intel_runtime_pm_enable_interrupts(dev_priv);
  2592. intel_modeset_init_hw(dev);
  2593. spin_lock_irq(&dev_priv->irq_lock);
  2594. if (dev_priv->display.hpd_irq_setup)
  2595. dev_priv->display.hpd_irq_setup(dev);
  2596. spin_unlock_irq(&dev_priv->irq_lock);
  2597. intel_modeset_setup_hw_state(dev, true);
  2598. intel_hpd_init(dev_priv);
  2599. drm_modeset_unlock_all(dev);
  2600. }
  2601. static int
  2602. intel_finish_fb(struct drm_framebuffer *old_fb)
  2603. {
  2604. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2605. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2606. bool was_interruptible = dev_priv->mm.interruptible;
  2607. int ret;
  2608. /* Big Hammer, we also need to ensure that any pending
  2609. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2610. * current scanout is retired before unpinning the old
  2611. * framebuffer.
  2612. *
  2613. * This should only fail upon a hung GPU, in which case we
  2614. * can safely continue.
  2615. */
  2616. dev_priv->mm.interruptible = false;
  2617. ret = i915_gem_object_finish_gpu(obj);
  2618. dev_priv->mm.interruptible = was_interruptible;
  2619. return ret;
  2620. }
  2621. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2622. {
  2623. struct drm_device *dev = crtc->dev;
  2624. struct drm_i915_private *dev_priv = dev->dev_private;
  2625. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2626. bool pending;
  2627. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2628. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2629. return false;
  2630. spin_lock_irq(&dev->event_lock);
  2631. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2632. spin_unlock_irq(&dev->event_lock);
  2633. return pending;
  2634. }
  2635. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2636. {
  2637. struct drm_device *dev = crtc->base.dev;
  2638. struct drm_i915_private *dev_priv = dev->dev_private;
  2639. const struct drm_display_mode *adjusted_mode;
  2640. if (!i915.fastboot)
  2641. return;
  2642. /*
  2643. * Update pipe size and adjust fitter if needed: the reason for this is
  2644. * that in compute_mode_changes we check the native mode (not the pfit
  2645. * mode) to see if we can flip rather than do a full mode set. In the
  2646. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2647. * pfit state, we'll end up with a big fb scanned out into the wrong
  2648. * sized surface.
  2649. *
  2650. * To fix this properly, we need to hoist the checks up into
  2651. * compute_mode_changes (or above), check the actual pfit state and
  2652. * whether the platform allows pfit disable with pipe active, and only
  2653. * then update the pipesrc and pfit state, even on the flip path.
  2654. */
  2655. adjusted_mode = &crtc->config->base.adjusted_mode;
  2656. I915_WRITE(PIPESRC(crtc->pipe),
  2657. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2658. (adjusted_mode->crtc_vdisplay - 1));
  2659. if (!crtc->config->pch_pfit.enabled &&
  2660. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2661. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2662. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2663. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2664. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2665. }
  2666. crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
  2667. crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
  2668. }
  2669. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2670. {
  2671. struct drm_device *dev = crtc->dev;
  2672. struct drm_i915_private *dev_priv = dev->dev_private;
  2673. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2674. int pipe = intel_crtc->pipe;
  2675. u32 reg, temp;
  2676. /* enable normal train */
  2677. reg = FDI_TX_CTL(pipe);
  2678. temp = I915_READ(reg);
  2679. if (IS_IVYBRIDGE(dev)) {
  2680. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2681. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2682. } else {
  2683. temp &= ~FDI_LINK_TRAIN_NONE;
  2684. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2685. }
  2686. I915_WRITE(reg, temp);
  2687. reg = FDI_RX_CTL(pipe);
  2688. temp = I915_READ(reg);
  2689. if (HAS_PCH_CPT(dev)) {
  2690. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2691. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2692. } else {
  2693. temp &= ~FDI_LINK_TRAIN_NONE;
  2694. temp |= FDI_LINK_TRAIN_NONE;
  2695. }
  2696. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2697. /* wait one idle pattern time */
  2698. POSTING_READ(reg);
  2699. udelay(1000);
  2700. /* IVB wants error correction enabled */
  2701. if (IS_IVYBRIDGE(dev))
  2702. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2703. FDI_FE_ERRC_ENABLE);
  2704. }
  2705. /* The FDI link training functions for ILK/Ibexpeak. */
  2706. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2707. {
  2708. struct drm_device *dev = crtc->dev;
  2709. struct drm_i915_private *dev_priv = dev->dev_private;
  2710. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2711. int pipe = intel_crtc->pipe;
  2712. u32 reg, temp, tries;
  2713. /* FDI needs bits from pipe first */
  2714. assert_pipe_enabled(dev_priv, pipe);
  2715. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2716. for train result */
  2717. reg = FDI_RX_IMR(pipe);
  2718. temp = I915_READ(reg);
  2719. temp &= ~FDI_RX_SYMBOL_LOCK;
  2720. temp &= ~FDI_RX_BIT_LOCK;
  2721. I915_WRITE(reg, temp);
  2722. I915_READ(reg);
  2723. udelay(150);
  2724. /* enable CPU FDI TX and PCH FDI RX */
  2725. reg = FDI_TX_CTL(pipe);
  2726. temp = I915_READ(reg);
  2727. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2728. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2729. temp &= ~FDI_LINK_TRAIN_NONE;
  2730. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2731. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2732. reg = FDI_RX_CTL(pipe);
  2733. temp = I915_READ(reg);
  2734. temp &= ~FDI_LINK_TRAIN_NONE;
  2735. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2736. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2737. POSTING_READ(reg);
  2738. udelay(150);
  2739. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2740. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2741. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2742. FDI_RX_PHASE_SYNC_POINTER_EN);
  2743. reg = FDI_RX_IIR(pipe);
  2744. for (tries = 0; tries < 5; tries++) {
  2745. temp = I915_READ(reg);
  2746. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2747. if ((temp & FDI_RX_BIT_LOCK)) {
  2748. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2749. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2750. break;
  2751. }
  2752. }
  2753. if (tries == 5)
  2754. DRM_ERROR("FDI train 1 fail!\n");
  2755. /* Train 2 */
  2756. reg = FDI_TX_CTL(pipe);
  2757. temp = I915_READ(reg);
  2758. temp &= ~FDI_LINK_TRAIN_NONE;
  2759. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2760. I915_WRITE(reg, temp);
  2761. reg = FDI_RX_CTL(pipe);
  2762. temp = I915_READ(reg);
  2763. temp &= ~FDI_LINK_TRAIN_NONE;
  2764. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2765. I915_WRITE(reg, temp);
  2766. POSTING_READ(reg);
  2767. udelay(150);
  2768. reg = FDI_RX_IIR(pipe);
  2769. for (tries = 0; tries < 5; tries++) {
  2770. temp = I915_READ(reg);
  2771. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2772. if (temp & FDI_RX_SYMBOL_LOCK) {
  2773. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2774. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2775. break;
  2776. }
  2777. }
  2778. if (tries == 5)
  2779. DRM_ERROR("FDI train 2 fail!\n");
  2780. DRM_DEBUG_KMS("FDI train done\n");
  2781. }
  2782. static const int snb_b_fdi_train_param[] = {
  2783. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2784. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2785. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2786. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2787. };
  2788. /* The FDI link training functions for SNB/Cougarpoint. */
  2789. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2790. {
  2791. struct drm_device *dev = crtc->dev;
  2792. struct drm_i915_private *dev_priv = dev->dev_private;
  2793. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2794. int pipe = intel_crtc->pipe;
  2795. u32 reg, temp, i, retry;
  2796. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2797. for train result */
  2798. reg = FDI_RX_IMR(pipe);
  2799. temp = I915_READ(reg);
  2800. temp &= ~FDI_RX_SYMBOL_LOCK;
  2801. temp &= ~FDI_RX_BIT_LOCK;
  2802. I915_WRITE(reg, temp);
  2803. POSTING_READ(reg);
  2804. udelay(150);
  2805. /* enable CPU FDI TX and PCH FDI RX */
  2806. reg = FDI_TX_CTL(pipe);
  2807. temp = I915_READ(reg);
  2808. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2809. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2810. temp &= ~FDI_LINK_TRAIN_NONE;
  2811. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2812. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2813. /* SNB-B */
  2814. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2815. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2816. I915_WRITE(FDI_RX_MISC(pipe),
  2817. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2818. reg = FDI_RX_CTL(pipe);
  2819. temp = I915_READ(reg);
  2820. if (HAS_PCH_CPT(dev)) {
  2821. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2822. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2823. } else {
  2824. temp &= ~FDI_LINK_TRAIN_NONE;
  2825. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2826. }
  2827. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2828. POSTING_READ(reg);
  2829. udelay(150);
  2830. for (i = 0; i < 4; i++) {
  2831. reg = FDI_TX_CTL(pipe);
  2832. temp = I915_READ(reg);
  2833. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2834. temp |= snb_b_fdi_train_param[i];
  2835. I915_WRITE(reg, temp);
  2836. POSTING_READ(reg);
  2837. udelay(500);
  2838. for (retry = 0; retry < 5; retry++) {
  2839. reg = FDI_RX_IIR(pipe);
  2840. temp = I915_READ(reg);
  2841. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2842. if (temp & FDI_RX_BIT_LOCK) {
  2843. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2844. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2845. break;
  2846. }
  2847. udelay(50);
  2848. }
  2849. if (retry < 5)
  2850. break;
  2851. }
  2852. if (i == 4)
  2853. DRM_ERROR("FDI train 1 fail!\n");
  2854. /* Train 2 */
  2855. reg = FDI_TX_CTL(pipe);
  2856. temp = I915_READ(reg);
  2857. temp &= ~FDI_LINK_TRAIN_NONE;
  2858. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2859. if (IS_GEN6(dev)) {
  2860. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2861. /* SNB-B */
  2862. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2863. }
  2864. I915_WRITE(reg, temp);
  2865. reg = FDI_RX_CTL(pipe);
  2866. temp = I915_READ(reg);
  2867. if (HAS_PCH_CPT(dev)) {
  2868. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2869. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2870. } else {
  2871. temp &= ~FDI_LINK_TRAIN_NONE;
  2872. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2873. }
  2874. I915_WRITE(reg, temp);
  2875. POSTING_READ(reg);
  2876. udelay(150);
  2877. for (i = 0; i < 4; i++) {
  2878. reg = FDI_TX_CTL(pipe);
  2879. temp = I915_READ(reg);
  2880. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2881. temp |= snb_b_fdi_train_param[i];
  2882. I915_WRITE(reg, temp);
  2883. POSTING_READ(reg);
  2884. udelay(500);
  2885. for (retry = 0; retry < 5; retry++) {
  2886. reg = FDI_RX_IIR(pipe);
  2887. temp = I915_READ(reg);
  2888. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2889. if (temp & FDI_RX_SYMBOL_LOCK) {
  2890. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2891. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2892. break;
  2893. }
  2894. udelay(50);
  2895. }
  2896. if (retry < 5)
  2897. break;
  2898. }
  2899. if (i == 4)
  2900. DRM_ERROR("FDI train 2 fail!\n");
  2901. DRM_DEBUG_KMS("FDI train done.\n");
  2902. }
  2903. /* Manual link training for Ivy Bridge A0 parts */
  2904. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2905. {
  2906. struct drm_device *dev = crtc->dev;
  2907. struct drm_i915_private *dev_priv = dev->dev_private;
  2908. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2909. int pipe = intel_crtc->pipe;
  2910. u32 reg, temp, i, j;
  2911. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2912. for train result */
  2913. reg = FDI_RX_IMR(pipe);
  2914. temp = I915_READ(reg);
  2915. temp &= ~FDI_RX_SYMBOL_LOCK;
  2916. temp &= ~FDI_RX_BIT_LOCK;
  2917. I915_WRITE(reg, temp);
  2918. POSTING_READ(reg);
  2919. udelay(150);
  2920. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  2921. I915_READ(FDI_RX_IIR(pipe)));
  2922. /* Try each vswing and preemphasis setting twice before moving on */
  2923. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  2924. /* disable first in case we need to retry */
  2925. reg = FDI_TX_CTL(pipe);
  2926. temp = I915_READ(reg);
  2927. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2928. temp &= ~FDI_TX_ENABLE;
  2929. I915_WRITE(reg, temp);
  2930. reg = FDI_RX_CTL(pipe);
  2931. temp = I915_READ(reg);
  2932. temp &= ~FDI_LINK_TRAIN_AUTO;
  2933. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2934. temp &= ~FDI_RX_ENABLE;
  2935. I915_WRITE(reg, temp);
  2936. /* enable CPU FDI TX and PCH FDI RX */
  2937. reg = FDI_TX_CTL(pipe);
  2938. temp = I915_READ(reg);
  2939. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2940. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2941. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2942. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2943. temp |= snb_b_fdi_train_param[j/2];
  2944. temp |= FDI_COMPOSITE_SYNC;
  2945. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2946. I915_WRITE(FDI_RX_MISC(pipe),
  2947. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2948. reg = FDI_RX_CTL(pipe);
  2949. temp = I915_READ(reg);
  2950. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2951. temp |= FDI_COMPOSITE_SYNC;
  2952. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2953. POSTING_READ(reg);
  2954. udelay(1); /* should be 0.5us */
  2955. for (i = 0; i < 4; i++) {
  2956. reg = FDI_RX_IIR(pipe);
  2957. temp = I915_READ(reg);
  2958. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2959. if (temp & FDI_RX_BIT_LOCK ||
  2960. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2961. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2962. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  2963. i);
  2964. break;
  2965. }
  2966. udelay(1); /* should be 0.5us */
  2967. }
  2968. if (i == 4) {
  2969. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  2970. continue;
  2971. }
  2972. /* Train 2 */
  2973. reg = FDI_TX_CTL(pipe);
  2974. temp = I915_READ(reg);
  2975. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2976. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2977. I915_WRITE(reg, temp);
  2978. reg = FDI_RX_CTL(pipe);
  2979. temp = I915_READ(reg);
  2980. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2981. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2982. I915_WRITE(reg, temp);
  2983. POSTING_READ(reg);
  2984. udelay(2); /* should be 1.5us */
  2985. for (i = 0; i < 4; i++) {
  2986. reg = FDI_RX_IIR(pipe);
  2987. temp = I915_READ(reg);
  2988. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2989. if (temp & FDI_RX_SYMBOL_LOCK ||
  2990. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  2991. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2992. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  2993. i);
  2994. goto train_done;
  2995. }
  2996. udelay(2); /* should be 1.5us */
  2997. }
  2998. if (i == 4)
  2999. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3000. }
  3001. train_done:
  3002. DRM_DEBUG_KMS("FDI train done.\n");
  3003. }
  3004. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3005. {
  3006. struct drm_device *dev = intel_crtc->base.dev;
  3007. struct drm_i915_private *dev_priv = dev->dev_private;
  3008. int pipe = intel_crtc->pipe;
  3009. u32 reg, temp;
  3010. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3011. reg = FDI_RX_CTL(pipe);
  3012. temp = I915_READ(reg);
  3013. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3014. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3015. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3016. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3017. POSTING_READ(reg);
  3018. udelay(200);
  3019. /* Switch from Rawclk to PCDclk */
  3020. temp = I915_READ(reg);
  3021. I915_WRITE(reg, temp | FDI_PCDCLK);
  3022. POSTING_READ(reg);
  3023. udelay(200);
  3024. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3025. reg = FDI_TX_CTL(pipe);
  3026. temp = I915_READ(reg);
  3027. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3028. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3029. POSTING_READ(reg);
  3030. udelay(100);
  3031. }
  3032. }
  3033. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3034. {
  3035. struct drm_device *dev = intel_crtc->base.dev;
  3036. struct drm_i915_private *dev_priv = dev->dev_private;
  3037. int pipe = intel_crtc->pipe;
  3038. u32 reg, temp;
  3039. /* Switch from PCDclk to Rawclk */
  3040. reg = FDI_RX_CTL(pipe);
  3041. temp = I915_READ(reg);
  3042. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3043. /* Disable CPU FDI TX PLL */
  3044. reg = FDI_TX_CTL(pipe);
  3045. temp = I915_READ(reg);
  3046. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3047. POSTING_READ(reg);
  3048. udelay(100);
  3049. reg = FDI_RX_CTL(pipe);
  3050. temp = I915_READ(reg);
  3051. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3052. /* Wait for the clocks to turn off. */
  3053. POSTING_READ(reg);
  3054. udelay(100);
  3055. }
  3056. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3057. {
  3058. struct drm_device *dev = crtc->dev;
  3059. struct drm_i915_private *dev_priv = dev->dev_private;
  3060. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3061. int pipe = intel_crtc->pipe;
  3062. u32 reg, temp;
  3063. /* disable CPU FDI tx and PCH FDI rx */
  3064. reg = FDI_TX_CTL(pipe);
  3065. temp = I915_READ(reg);
  3066. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3067. POSTING_READ(reg);
  3068. reg = FDI_RX_CTL(pipe);
  3069. temp = I915_READ(reg);
  3070. temp &= ~(0x7 << 16);
  3071. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3072. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3073. POSTING_READ(reg);
  3074. udelay(100);
  3075. /* Ironlake workaround, disable clock pointer after downing FDI */
  3076. if (HAS_PCH_IBX(dev))
  3077. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3078. /* still set train pattern 1 */
  3079. reg = FDI_TX_CTL(pipe);
  3080. temp = I915_READ(reg);
  3081. temp &= ~FDI_LINK_TRAIN_NONE;
  3082. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3083. I915_WRITE(reg, temp);
  3084. reg = FDI_RX_CTL(pipe);
  3085. temp = I915_READ(reg);
  3086. if (HAS_PCH_CPT(dev)) {
  3087. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3088. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3089. } else {
  3090. temp &= ~FDI_LINK_TRAIN_NONE;
  3091. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3092. }
  3093. /* BPC in FDI rx is consistent with that in PIPECONF */
  3094. temp &= ~(0x07 << 16);
  3095. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3096. I915_WRITE(reg, temp);
  3097. POSTING_READ(reg);
  3098. udelay(100);
  3099. }
  3100. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3101. {
  3102. struct intel_crtc *crtc;
  3103. /* Note that we don't need to be called with mode_config.lock here
  3104. * as our list of CRTC objects is static for the lifetime of the
  3105. * device and so cannot disappear as we iterate. Similarly, we can
  3106. * happily treat the predicates as racy, atomic checks as userspace
  3107. * cannot claim and pin a new fb without at least acquring the
  3108. * struct_mutex and so serialising with us.
  3109. */
  3110. for_each_intel_crtc(dev, crtc) {
  3111. if (atomic_read(&crtc->unpin_work_count) == 0)
  3112. continue;
  3113. if (crtc->unpin_work)
  3114. intel_wait_for_vblank(dev, crtc->pipe);
  3115. return true;
  3116. }
  3117. return false;
  3118. }
  3119. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3120. {
  3121. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3122. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3123. /* ensure that the unpin work is consistent wrt ->pending. */
  3124. smp_rmb();
  3125. intel_crtc->unpin_work = NULL;
  3126. if (work->event)
  3127. drm_send_vblank_event(intel_crtc->base.dev,
  3128. intel_crtc->pipe,
  3129. work->event);
  3130. drm_crtc_vblank_put(&intel_crtc->base);
  3131. wake_up_all(&dev_priv->pending_flip_queue);
  3132. queue_work(dev_priv->wq, &work->work);
  3133. trace_i915_flip_complete(intel_crtc->plane,
  3134. work->pending_flip_obj);
  3135. }
  3136. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3137. {
  3138. struct drm_device *dev = crtc->dev;
  3139. struct drm_i915_private *dev_priv = dev->dev_private;
  3140. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3141. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3142. !intel_crtc_has_pending_flip(crtc),
  3143. 60*HZ) == 0)) {
  3144. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3145. spin_lock_irq(&dev->event_lock);
  3146. if (intel_crtc->unpin_work) {
  3147. WARN_ONCE(1, "Removing stuck page flip\n");
  3148. page_flip_completed(intel_crtc);
  3149. }
  3150. spin_unlock_irq(&dev->event_lock);
  3151. }
  3152. if (crtc->primary->fb) {
  3153. mutex_lock(&dev->struct_mutex);
  3154. intel_finish_fb(crtc->primary->fb);
  3155. mutex_unlock(&dev->struct_mutex);
  3156. }
  3157. }
  3158. /* Program iCLKIP clock to the desired frequency */
  3159. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3160. {
  3161. struct drm_device *dev = crtc->dev;
  3162. struct drm_i915_private *dev_priv = dev->dev_private;
  3163. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3164. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3165. u32 temp;
  3166. mutex_lock(&dev_priv->dpio_lock);
  3167. /* It is necessary to ungate the pixclk gate prior to programming
  3168. * the divisors, and gate it back when it is done.
  3169. */
  3170. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3171. /* Disable SSCCTL */
  3172. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3173. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3174. SBI_SSCCTL_DISABLE,
  3175. SBI_ICLK);
  3176. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3177. if (clock == 20000) {
  3178. auxdiv = 1;
  3179. divsel = 0x41;
  3180. phaseinc = 0x20;
  3181. } else {
  3182. /* The iCLK virtual clock root frequency is in MHz,
  3183. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3184. * divisors, it is necessary to divide one by another, so we
  3185. * convert the virtual clock precision to KHz here for higher
  3186. * precision.
  3187. */
  3188. u32 iclk_virtual_root_freq = 172800 * 1000;
  3189. u32 iclk_pi_range = 64;
  3190. u32 desired_divisor, msb_divisor_value, pi_value;
  3191. desired_divisor = (iclk_virtual_root_freq / clock);
  3192. msb_divisor_value = desired_divisor / iclk_pi_range;
  3193. pi_value = desired_divisor % iclk_pi_range;
  3194. auxdiv = 0;
  3195. divsel = msb_divisor_value - 2;
  3196. phaseinc = pi_value;
  3197. }
  3198. /* This should not happen with any sane values */
  3199. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3200. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3201. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3202. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3203. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3204. clock,
  3205. auxdiv,
  3206. divsel,
  3207. phasedir,
  3208. phaseinc);
  3209. /* Program SSCDIVINTPHASE6 */
  3210. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3211. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3212. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3213. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3214. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3215. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3216. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3217. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3218. /* Program SSCAUXDIV */
  3219. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3220. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3221. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3222. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3223. /* Enable modulator and associated divider */
  3224. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3225. temp &= ~SBI_SSCCTL_DISABLE;
  3226. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3227. /* Wait for initialization time */
  3228. udelay(24);
  3229. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3230. mutex_unlock(&dev_priv->dpio_lock);
  3231. }
  3232. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3233. enum pipe pch_transcoder)
  3234. {
  3235. struct drm_device *dev = crtc->base.dev;
  3236. struct drm_i915_private *dev_priv = dev->dev_private;
  3237. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3238. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3239. I915_READ(HTOTAL(cpu_transcoder)));
  3240. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3241. I915_READ(HBLANK(cpu_transcoder)));
  3242. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3243. I915_READ(HSYNC(cpu_transcoder)));
  3244. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3245. I915_READ(VTOTAL(cpu_transcoder)));
  3246. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3247. I915_READ(VBLANK(cpu_transcoder)));
  3248. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3249. I915_READ(VSYNC(cpu_transcoder)));
  3250. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3251. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3252. }
  3253. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3254. {
  3255. struct drm_i915_private *dev_priv = dev->dev_private;
  3256. uint32_t temp;
  3257. temp = I915_READ(SOUTH_CHICKEN1);
  3258. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3259. return;
  3260. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3261. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3262. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3263. if (enable)
  3264. temp |= FDI_BC_BIFURCATION_SELECT;
  3265. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3266. I915_WRITE(SOUTH_CHICKEN1, temp);
  3267. POSTING_READ(SOUTH_CHICKEN1);
  3268. }
  3269. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3270. {
  3271. struct drm_device *dev = intel_crtc->base.dev;
  3272. switch (intel_crtc->pipe) {
  3273. case PIPE_A:
  3274. break;
  3275. case PIPE_B:
  3276. if (intel_crtc->config->fdi_lanes > 2)
  3277. cpt_set_fdi_bc_bifurcation(dev, false);
  3278. else
  3279. cpt_set_fdi_bc_bifurcation(dev, true);
  3280. break;
  3281. case PIPE_C:
  3282. cpt_set_fdi_bc_bifurcation(dev, true);
  3283. break;
  3284. default:
  3285. BUG();
  3286. }
  3287. }
  3288. /*
  3289. * Enable PCH resources required for PCH ports:
  3290. * - PCH PLLs
  3291. * - FDI training & RX/TX
  3292. * - update transcoder timings
  3293. * - DP transcoding bits
  3294. * - transcoder
  3295. */
  3296. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3297. {
  3298. struct drm_device *dev = crtc->dev;
  3299. struct drm_i915_private *dev_priv = dev->dev_private;
  3300. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3301. int pipe = intel_crtc->pipe;
  3302. u32 reg, temp;
  3303. assert_pch_transcoder_disabled(dev_priv, pipe);
  3304. if (IS_IVYBRIDGE(dev))
  3305. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3306. /* Write the TU size bits before fdi link training, so that error
  3307. * detection works. */
  3308. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3309. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3310. /* For PCH output, training FDI link */
  3311. dev_priv->display.fdi_link_train(crtc);
  3312. /* We need to program the right clock selection before writing the pixel
  3313. * mutliplier into the DPLL. */
  3314. if (HAS_PCH_CPT(dev)) {
  3315. u32 sel;
  3316. temp = I915_READ(PCH_DPLL_SEL);
  3317. temp |= TRANS_DPLL_ENABLE(pipe);
  3318. sel = TRANS_DPLLB_SEL(pipe);
  3319. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3320. temp |= sel;
  3321. else
  3322. temp &= ~sel;
  3323. I915_WRITE(PCH_DPLL_SEL, temp);
  3324. }
  3325. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3326. * transcoder, and we actually should do this to not upset any PCH
  3327. * transcoder that already use the clock when we share it.
  3328. *
  3329. * Note that enable_shared_dpll tries to do the right thing, but
  3330. * get_shared_dpll unconditionally resets the pll - we need that to have
  3331. * the right LVDS enable sequence. */
  3332. intel_enable_shared_dpll(intel_crtc);
  3333. /* set transcoder timing, panel must allow it */
  3334. assert_panel_unlocked(dev_priv, pipe);
  3335. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3336. intel_fdi_normal_train(crtc);
  3337. /* For PCH DP, enable TRANS_DP_CTL */
  3338. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3339. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3340. reg = TRANS_DP_CTL(pipe);
  3341. temp = I915_READ(reg);
  3342. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3343. TRANS_DP_SYNC_MASK |
  3344. TRANS_DP_BPC_MASK);
  3345. temp |= (TRANS_DP_OUTPUT_ENABLE |
  3346. TRANS_DP_ENH_FRAMING);
  3347. temp |= bpc << 9; /* same format but at 11:9 */
  3348. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3349. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3350. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3351. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3352. switch (intel_trans_dp_port_sel(crtc)) {
  3353. case PCH_DP_B:
  3354. temp |= TRANS_DP_PORT_SEL_B;
  3355. break;
  3356. case PCH_DP_C:
  3357. temp |= TRANS_DP_PORT_SEL_C;
  3358. break;
  3359. case PCH_DP_D:
  3360. temp |= TRANS_DP_PORT_SEL_D;
  3361. break;
  3362. default:
  3363. BUG();
  3364. }
  3365. I915_WRITE(reg, temp);
  3366. }
  3367. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3368. }
  3369. static void lpt_pch_enable(struct drm_crtc *crtc)
  3370. {
  3371. struct drm_device *dev = crtc->dev;
  3372. struct drm_i915_private *dev_priv = dev->dev_private;
  3373. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3374. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3375. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3376. lpt_program_iclkip(crtc);
  3377. /* Set transcoder timing. */
  3378. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3379. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3380. }
  3381. void intel_put_shared_dpll(struct intel_crtc *crtc)
  3382. {
  3383. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3384. if (pll == NULL)
  3385. return;
  3386. if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
  3387. WARN(1, "bad %s crtc mask\n", pll->name);
  3388. return;
  3389. }
  3390. pll->config.crtc_mask &= ~(1 << crtc->pipe);
  3391. if (pll->config.crtc_mask == 0) {
  3392. WARN_ON(pll->on);
  3393. WARN_ON(pll->active);
  3394. }
  3395. crtc->config->shared_dpll = DPLL_ID_PRIVATE;
  3396. }
  3397. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3398. struct intel_crtc_state *crtc_state)
  3399. {
  3400. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3401. struct intel_shared_dpll *pll;
  3402. enum intel_dpll_id i;
  3403. if (HAS_PCH_IBX(dev_priv->dev)) {
  3404. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3405. i = (enum intel_dpll_id) crtc->pipe;
  3406. pll = &dev_priv->shared_dplls[i];
  3407. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3408. crtc->base.base.id, pll->name);
  3409. WARN_ON(pll->new_config->crtc_mask);
  3410. goto found;
  3411. }
  3412. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3413. pll = &dev_priv->shared_dplls[i];
  3414. /* Only want to check enabled timings first */
  3415. if (pll->new_config->crtc_mask == 0)
  3416. continue;
  3417. if (memcmp(&crtc_state->dpll_hw_state,
  3418. &pll->new_config->hw_state,
  3419. sizeof(pll->new_config->hw_state)) == 0) {
  3420. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3421. crtc->base.base.id, pll->name,
  3422. pll->new_config->crtc_mask,
  3423. pll->active);
  3424. goto found;
  3425. }
  3426. }
  3427. /* Ok no matching timings, maybe there's a free one? */
  3428. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3429. pll = &dev_priv->shared_dplls[i];
  3430. if (pll->new_config->crtc_mask == 0) {
  3431. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3432. crtc->base.base.id, pll->name);
  3433. goto found;
  3434. }
  3435. }
  3436. return NULL;
  3437. found:
  3438. if (pll->new_config->crtc_mask == 0)
  3439. pll->new_config->hw_state = crtc_state->dpll_hw_state;
  3440. crtc_state->shared_dpll = i;
  3441. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3442. pipe_name(crtc->pipe));
  3443. pll->new_config->crtc_mask |= 1 << crtc->pipe;
  3444. return pll;
  3445. }
  3446. /**
  3447. * intel_shared_dpll_start_config - start a new PLL staged config
  3448. * @dev_priv: DRM device
  3449. * @clear_pipes: mask of pipes that will have their PLLs freed
  3450. *
  3451. * Starts a new PLL staged config, copying the current config but
  3452. * releasing the references of pipes specified in clear_pipes.
  3453. */
  3454. static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
  3455. unsigned clear_pipes)
  3456. {
  3457. struct intel_shared_dpll *pll;
  3458. enum intel_dpll_id i;
  3459. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3460. pll = &dev_priv->shared_dplls[i];
  3461. pll->new_config = kmemdup(&pll->config, sizeof pll->config,
  3462. GFP_KERNEL);
  3463. if (!pll->new_config)
  3464. goto cleanup;
  3465. pll->new_config->crtc_mask &= ~clear_pipes;
  3466. }
  3467. return 0;
  3468. cleanup:
  3469. while (--i >= 0) {
  3470. pll = &dev_priv->shared_dplls[i];
  3471. kfree(pll->new_config);
  3472. pll->new_config = NULL;
  3473. }
  3474. return -ENOMEM;
  3475. }
  3476. static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
  3477. {
  3478. struct intel_shared_dpll *pll;
  3479. enum intel_dpll_id i;
  3480. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3481. pll = &dev_priv->shared_dplls[i];
  3482. WARN_ON(pll->new_config == &pll->config);
  3483. pll->config = *pll->new_config;
  3484. kfree(pll->new_config);
  3485. pll->new_config = NULL;
  3486. }
  3487. }
  3488. static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
  3489. {
  3490. struct intel_shared_dpll *pll;
  3491. enum intel_dpll_id i;
  3492. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3493. pll = &dev_priv->shared_dplls[i];
  3494. WARN_ON(pll->new_config == &pll->config);
  3495. kfree(pll->new_config);
  3496. pll->new_config = NULL;
  3497. }
  3498. }
  3499. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3500. {
  3501. struct drm_i915_private *dev_priv = dev->dev_private;
  3502. int dslreg = PIPEDSL(pipe);
  3503. u32 temp;
  3504. temp = I915_READ(dslreg);
  3505. udelay(500);
  3506. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3507. if (wait_for(I915_READ(dslreg) != temp, 5))
  3508. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3509. }
  3510. }
  3511. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3512. {
  3513. struct drm_device *dev = crtc->base.dev;
  3514. struct drm_i915_private *dev_priv = dev->dev_private;
  3515. int pipe = crtc->pipe;
  3516. if (crtc->config->pch_pfit.enabled) {
  3517. I915_WRITE(PS_CTL(pipe), PS_ENABLE);
  3518. I915_WRITE(PS_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3519. I915_WRITE(PS_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3520. }
  3521. }
  3522. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3523. {
  3524. struct drm_device *dev = crtc->base.dev;
  3525. struct drm_i915_private *dev_priv = dev->dev_private;
  3526. int pipe = crtc->pipe;
  3527. if (crtc->config->pch_pfit.enabled) {
  3528. /* Force use of hard-coded filter coefficients
  3529. * as some pre-programmed values are broken,
  3530. * e.g. x201.
  3531. */
  3532. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3533. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3534. PF_PIPE_SEL_IVB(pipe));
  3535. else
  3536. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3537. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3538. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3539. }
  3540. }
  3541. static void intel_enable_sprite_planes(struct drm_crtc *crtc)
  3542. {
  3543. struct drm_device *dev = crtc->dev;
  3544. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3545. struct drm_plane *plane;
  3546. struct intel_plane *intel_plane;
  3547. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3548. intel_plane = to_intel_plane(plane);
  3549. if (intel_plane->pipe == pipe)
  3550. intel_plane_restore(&intel_plane->base);
  3551. }
  3552. }
  3553. /*
  3554. * Disable a plane internally without actually modifying the plane's state.
  3555. * This will allow us to easily restore the plane later by just reprogramming
  3556. * its state.
  3557. */
  3558. static void disable_plane_internal(struct drm_plane *plane)
  3559. {
  3560. struct intel_plane *intel_plane = to_intel_plane(plane);
  3561. struct drm_plane_state *state =
  3562. plane->funcs->atomic_duplicate_state(plane);
  3563. struct intel_plane_state *intel_state = to_intel_plane_state(state);
  3564. intel_state->visible = false;
  3565. intel_plane->commit_plane(plane, intel_state);
  3566. intel_plane_destroy_state(plane, state);
  3567. }
  3568. static void intel_disable_sprite_planes(struct drm_crtc *crtc)
  3569. {
  3570. struct drm_device *dev = crtc->dev;
  3571. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3572. struct drm_plane *plane;
  3573. struct intel_plane *intel_plane;
  3574. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3575. intel_plane = to_intel_plane(plane);
  3576. if (plane->fb && intel_plane->pipe == pipe)
  3577. disable_plane_internal(plane);
  3578. }
  3579. }
  3580. void hsw_enable_ips(struct intel_crtc *crtc)
  3581. {
  3582. struct drm_device *dev = crtc->base.dev;
  3583. struct drm_i915_private *dev_priv = dev->dev_private;
  3584. if (!crtc->config->ips_enabled)
  3585. return;
  3586. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3587. intel_wait_for_vblank(dev, crtc->pipe);
  3588. assert_plane_enabled(dev_priv, crtc->plane);
  3589. if (IS_BROADWELL(dev)) {
  3590. mutex_lock(&dev_priv->rps.hw_lock);
  3591. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3592. mutex_unlock(&dev_priv->rps.hw_lock);
  3593. /* Quoting Art Runyan: "its not safe to expect any particular
  3594. * value in IPS_CTL bit 31 after enabling IPS through the
  3595. * mailbox." Moreover, the mailbox may return a bogus state,
  3596. * so we need to just enable it and continue on.
  3597. */
  3598. } else {
  3599. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3600. /* The bit only becomes 1 in the next vblank, so this wait here
  3601. * is essentially intel_wait_for_vblank. If we don't have this
  3602. * and don't wait for vblanks until the end of crtc_enable, then
  3603. * the HW state readout code will complain that the expected
  3604. * IPS_CTL value is not the one we read. */
  3605. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3606. DRM_ERROR("Timed out waiting for IPS enable\n");
  3607. }
  3608. }
  3609. void hsw_disable_ips(struct intel_crtc *crtc)
  3610. {
  3611. struct drm_device *dev = crtc->base.dev;
  3612. struct drm_i915_private *dev_priv = dev->dev_private;
  3613. if (!crtc->config->ips_enabled)
  3614. return;
  3615. assert_plane_enabled(dev_priv, crtc->plane);
  3616. if (IS_BROADWELL(dev)) {
  3617. mutex_lock(&dev_priv->rps.hw_lock);
  3618. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3619. mutex_unlock(&dev_priv->rps.hw_lock);
  3620. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3621. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3622. DRM_ERROR("Timed out waiting for IPS disable\n");
  3623. } else {
  3624. I915_WRITE(IPS_CTL, 0);
  3625. POSTING_READ(IPS_CTL);
  3626. }
  3627. /* We need to wait for a vblank before we can disable the plane. */
  3628. intel_wait_for_vblank(dev, crtc->pipe);
  3629. }
  3630. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3631. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3632. {
  3633. struct drm_device *dev = crtc->dev;
  3634. struct drm_i915_private *dev_priv = dev->dev_private;
  3635. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3636. enum pipe pipe = intel_crtc->pipe;
  3637. int palreg = PALETTE(pipe);
  3638. int i;
  3639. bool reenable_ips = false;
  3640. /* The clocks have to be on to load the palette. */
  3641. if (!crtc->state->enable || !intel_crtc->active)
  3642. return;
  3643. if (!HAS_PCH_SPLIT(dev_priv->dev)) {
  3644. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3645. assert_dsi_pll_enabled(dev_priv);
  3646. else
  3647. assert_pll_enabled(dev_priv, pipe);
  3648. }
  3649. /* use legacy palette for Ironlake */
  3650. if (!HAS_GMCH_DISPLAY(dev))
  3651. palreg = LGC_PALETTE(pipe);
  3652. /* Workaround : Do not read or write the pipe palette/gamma data while
  3653. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3654. */
  3655. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  3656. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3657. GAMMA_MODE_MODE_SPLIT)) {
  3658. hsw_disable_ips(intel_crtc);
  3659. reenable_ips = true;
  3660. }
  3661. for (i = 0; i < 256; i++) {
  3662. I915_WRITE(palreg + 4 * i,
  3663. (intel_crtc->lut_r[i] << 16) |
  3664. (intel_crtc->lut_g[i] << 8) |
  3665. intel_crtc->lut_b[i]);
  3666. }
  3667. if (reenable_ips)
  3668. hsw_enable_ips(intel_crtc);
  3669. }
  3670. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  3671. {
  3672. if (!enable && intel_crtc->overlay) {
  3673. struct drm_device *dev = intel_crtc->base.dev;
  3674. struct drm_i915_private *dev_priv = dev->dev_private;
  3675. mutex_lock(&dev->struct_mutex);
  3676. dev_priv->mm.interruptible = false;
  3677. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3678. dev_priv->mm.interruptible = true;
  3679. mutex_unlock(&dev->struct_mutex);
  3680. }
  3681. /* Let userspace switch the overlay on again. In most cases userspace
  3682. * has to recompute where to put it anyway.
  3683. */
  3684. }
  3685. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  3686. {
  3687. struct drm_device *dev = crtc->dev;
  3688. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3689. int pipe = intel_crtc->pipe;
  3690. intel_enable_primary_hw_plane(crtc->primary, crtc);
  3691. intel_enable_sprite_planes(crtc);
  3692. intel_crtc_update_cursor(crtc, true);
  3693. intel_crtc_dpms_overlay(intel_crtc, true);
  3694. hsw_enable_ips(intel_crtc);
  3695. mutex_lock(&dev->struct_mutex);
  3696. intel_fbc_update(dev);
  3697. mutex_unlock(&dev->struct_mutex);
  3698. /*
  3699. * FIXME: Once we grow proper nuclear flip support out of this we need
  3700. * to compute the mask of flip planes precisely. For the time being
  3701. * consider this a flip from a NULL plane.
  3702. */
  3703. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3704. }
  3705. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  3706. {
  3707. struct drm_device *dev = crtc->dev;
  3708. struct drm_i915_private *dev_priv = dev->dev_private;
  3709. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3710. int pipe = intel_crtc->pipe;
  3711. intel_crtc_wait_for_pending_flips(crtc);
  3712. if (dev_priv->fbc.crtc == intel_crtc)
  3713. intel_fbc_disable(dev);
  3714. hsw_disable_ips(intel_crtc);
  3715. intel_crtc_dpms_overlay(intel_crtc, false);
  3716. intel_crtc_update_cursor(crtc, false);
  3717. intel_disable_sprite_planes(crtc);
  3718. intel_disable_primary_hw_plane(crtc->primary, crtc);
  3719. /*
  3720. * FIXME: Once we grow proper nuclear flip support out of this we need
  3721. * to compute the mask of flip planes precisely. For the time being
  3722. * consider this a flip to a NULL plane.
  3723. */
  3724. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3725. }
  3726. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  3727. {
  3728. struct drm_device *dev = crtc->dev;
  3729. struct drm_i915_private *dev_priv = dev->dev_private;
  3730. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3731. struct intel_encoder *encoder;
  3732. int pipe = intel_crtc->pipe;
  3733. WARN_ON(!crtc->state->enable);
  3734. if (intel_crtc->active)
  3735. return;
  3736. if (intel_crtc->config->has_pch_encoder)
  3737. intel_prepare_shared_dpll(intel_crtc);
  3738. if (intel_crtc->config->has_dp_encoder)
  3739. intel_dp_set_m_n(intel_crtc, M1_N1);
  3740. intel_set_pipe_timings(intel_crtc);
  3741. if (intel_crtc->config->has_pch_encoder) {
  3742. intel_cpu_transcoder_set_m_n(intel_crtc,
  3743. &intel_crtc->config->fdi_m_n, NULL);
  3744. }
  3745. ironlake_set_pipeconf(crtc);
  3746. intel_crtc->active = true;
  3747. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3748. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  3749. for_each_encoder_on_crtc(dev, crtc, encoder)
  3750. if (encoder->pre_enable)
  3751. encoder->pre_enable(encoder);
  3752. if (intel_crtc->config->has_pch_encoder) {
  3753. /* Note: FDI PLL enabling _must_ be done before we enable the
  3754. * cpu pipes, hence this is separate from all the other fdi/pch
  3755. * enabling. */
  3756. ironlake_fdi_pll_enable(intel_crtc);
  3757. } else {
  3758. assert_fdi_tx_disabled(dev_priv, pipe);
  3759. assert_fdi_rx_disabled(dev_priv, pipe);
  3760. }
  3761. ironlake_pfit_enable(intel_crtc);
  3762. /*
  3763. * On ILK+ LUT must be loaded before the pipe is running but with
  3764. * clocks enabled
  3765. */
  3766. intel_crtc_load_lut(crtc);
  3767. intel_update_watermarks(crtc);
  3768. intel_enable_pipe(intel_crtc);
  3769. if (intel_crtc->config->has_pch_encoder)
  3770. ironlake_pch_enable(crtc);
  3771. assert_vblank_disabled(crtc);
  3772. drm_crtc_vblank_on(crtc);
  3773. for_each_encoder_on_crtc(dev, crtc, encoder)
  3774. encoder->enable(encoder);
  3775. if (HAS_PCH_CPT(dev))
  3776. cpt_verify_modeset(dev, intel_crtc->pipe);
  3777. intel_crtc_enable_planes(crtc);
  3778. }
  3779. /* IPS only exists on ULT machines and is tied to pipe A. */
  3780. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  3781. {
  3782. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  3783. }
  3784. /*
  3785. * This implements the workaround described in the "notes" section of the mode
  3786. * set sequence documentation. When going from no pipes or single pipe to
  3787. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  3788. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  3789. */
  3790. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  3791. {
  3792. struct drm_device *dev = crtc->base.dev;
  3793. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  3794. /* We want to get the other_active_crtc only if there's only 1 other
  3795. * active crtc. */
  3796. for_each_intel_crtc(dev, crtc_it) {
  3797. if (!crtc_it->active || crtc_it == crtc)
  3798. continue;
  3799. if (other_active_crtc)
  3800. return;
  3801. other_active_crtc = crtc_it;
  3802. }
  3803. if (!other_active_crtc)
  3804. return;
  3805. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3806. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3807. }
  3808. static void haswell_crtc_enable(struct drm_crtc *crtc)
  3809. {
  3810. struct drm_device *dev = crtc->dev;
  3811. struct drm_i915_private *dev_priv = dev->dev_private;
  3812. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3813. struct intel_encoder *encoder;
  3814. int pipe = intel_crtc->pipe;
  3815. WARN_ON(!crtc->state->enable);
  3816. if (intel_crtc->active)
  3817. return;
  3818. if (intel_crtc_to_shared_dpll(intel_crtc))
  3819. intel_enable_shared_dpll(intel_crtc);
  3820. if (intel_crtc->config->has_dp_encoder)
  3821. intel_dp_set_m_n(intel_crtc, M1_N1);
  3822. intel_set_pipe_timings(intel_crtc);
  3823. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  3824. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  3825. intel_crtc->config->pixel_multiplier - 1);
  3826. }
  3827. if (intel_crtc->config->has_pch_encoder) {
  3828. intel_cpu_transcoder_set_m_n(intel_crtc,
  3829. &intel_crtc->config->fdi_m_n, NULL);
  3830. }
  3831. haswell_set_pipeconf(crtc);
  3832. intel_set_pipe_csc(crtc);
  3833. intel_crtc->active = true;
  3834. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3835. for_each_encoder_on_crtc(dev, crtc, encoder)
  3836. if (encoder->pre_enable)
  3837. encoder->pre_enable(encoder);
  3838. if (intel_crtc->config->has_pch_encoder) {
  3839. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  3840. true);
  3841. dev_priv->display.fdi_link_train(crtc);
  3842. }
  3843. intel_ddi_enable_pipe_clock(intel_crtc);
  3844. if (IS_SKYLAKE(dev))
  3845. skylake_pfit_enable(intel_crtc);
  3846. else
  3847. ironlake_pfit_enable(intel_crtc);
  3848. /*
  3849. * On ILK+ LUT must be loaded before the pipe is running but with
  3850. * clocks enabled
  3851. */
  3852. intel_crtc_load_lut(crtc);
  3853. intel_ddi_set_pipe_settings(crtc);
  3854. intel_ddi_enable_transcoder_func(crtc);
  3855. intel_update_watermarks(crtc);
  3856. intel_enable_pipe(intel_crtc);
  3857. if (intel_crtc->config->has_pch_encoder)
  3858. lpt_pch_enable(crtc);
  3859. if (intel_crtc->config->dp_encoder_is_mst)
  3860. intel_ddi_set_vc_payload_alloc(crtc, true);
  3861. assert_vblank_disabled(crtc);
  3862. drm_crtc_vblank_on(crtc);
  3863. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3864. encoder->enable(encoder);
  3865. intel_opregion_notify_encoder(encoder, true);
  3866. }
  3867. /* If we change the relative order between pipe/planes enabling, we need
  3868. * to change the workaround. */
  3869. haswell_mode_set_planes_workaround(intel_crtc);
  3870. intel_crtc_enable_planes(crtc);
  3871. }
  3872. static void skylake_pfit_disable(struct intel_crtc *crtc)
  3873. {
  3874. struct drm_device *dev = crtc->base.dev;
  3875. struct drm_i915_private *dev_priv = dev->dev_private;
  3876. int pipe = crtc->pipe;
  3877. /* To avoid upsetting the power well on haswell only disable the pfit if
  3878. * it's in use. The hw state code will make sure we get this right. */
  3879. if (crtc->config->pch_pfit.enabled) {
  3880. I915_WRITE(PS_CTL(pipe), 0);
  3881. I915_WRITE(PS_WIN_POS(pipe), 0);
  3882. I915_WRITE(PS_WIN_SZ(pipe), 0);
  3883. }
  3884. }
  3885. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  3886. {
  3887. struct drm_device *dev = crtc->base.dev;
  3888. struct drm_i915_private *dev_priv = dev->dev_private;
  3889. int pipe = crtc->pipe;
  3890. /* To avoid upsetting the power well on haswell only disable the pfit if
  3891. * it's in use. The hw state code will make sure we get this right. */
  3892. if (crtc->config->pch_pfit.enabled) {
  3893. I915_WRITE(PF_CTL(pipe), 0);
  3894. I915_WRITE(PF_WIN_POS(pipe), 0);
  3895. I915_WRITE(PF_WIN_SZ(pipe), 0);
  3896. }
  3897. }
  3898. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  3899. {
  3900. struct drm_device *dev = crtc->dev;
  3901. struct drm_i915_private *dev_priv = dev->dev_private;
  3902. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3903. struct intel_encoder *encoder;
  3904. int pipe = intel_crtc->pipe;
  3905. u32 reg, temp;
  3906. if (!intel_crtc->active)
  3907. return;
  3908. intel_crtc_disable_planes(crtc);
  3909. for_each_encoder_on_crtc(dev, crtc, encoder)
  3910. encoder->disable(encoder);
  3911. drm_crtc_vblank_off(crtc);
  3912. assert_vblank_disabled(crtc);
  3913. if (intel_crtc->config->has_pch_encoder)
  3914. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  3915. intel_disable_pipe(intel_crtc);
  3916. ironlake_pfit_disable(intel_crtc);
  3917. for_each_encoder_on_crtc(dev, crtc, encoder)
  3918. if (encoder->post_disable)
  3919. encoder->post_disable(encoder);
  3920. if (intel_crtc->config->has_pch_encoder) {
  3921. ironlake_fdi_disable(crtc);
  3922. ironlake_disable_pch_transcoder(dev_priv, pipe);
  3923. if (HAS_PCH_CPT(dev)) {
  3924. /* disable TRANS_DP_CTL */
  3925. reg = TRANS_DP_CTL(pipe);
  3926. temp = I915_READ(reg);
  3927. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  3928. TRANS_DP_PORT_SEL_MASK);
  3929. temp |= TRANS_DP_PORT_SEL_NONE;
  3930. I915_WRITE(reg, temp);
  3931. /* disable DPLL_SEL */
  3932. temp = I915_READ(PCH_DPLL_SEL);
  3933. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  3934. I915_WRITE(PCH_DPLL_SEL, temp);
  3935. }
  3936. /* disable PCH DPLL */
  3937. intel_disable_shared_dpll(intel_crtc);
  3938. ironlake_fdi_pll_disable(intel_crtc);
  3939. }
  3940. intel_crtc->active = false;
  3941. intel_update_watermarks(crtc);
  3942. mutex_lock(&dev->struct_mutex);
  3943. intel_fbc_update(dev);
  3944. mutex_unlock(&dev->struct_mutex);
  3945. }
  3946. static void haswell_crtc_disable(struct drm_crtc *crtc)
  3947. {
  3948. struct drm_device *dev = crtc->dev;
  3949. struct drm_i915_private *dev_priv = dev->dev_private;
  3950. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3951. struct intel_encoder *encoder;
  3952. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3953. if (!intel_crtc->active)
  3954. return;
  3955. intel_crtc_disable_planes(crtc);
  3956. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3957. intel_opregion_notify_encoder(encoder, false);
  3958. encoder->disable(encoder);
  3959. }
  3960. drm_crtc_vblank_off(crtc);
  3961. assert_vblank_disabled(crtc);
  3962. if (intel_crtc->config->has_pch_encoder)
  3963. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  3964. false);
  3965. intel_disable_pipe(intel_crtc);
  3966. if (intel_crtc->config->dp_encoder_is_mst)
  3967. intel_ddi_set_vc_payload_alloc(crtc, false);
  3968. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  3969. if (IS_SKYLAKE(dev))
  3970. skylake_pfit_disable(intel_crtc);
  3971. else
  3972. ironlake_pfit_disable(intel_crtc);
  3973. intel_ddi_disable_pipe_clock(intel_crtc);
  3974. if (intel_crtc->config->has_pch_encoder) {
  3975. lpt_disable_pch_transcoder(dev_priv);
  3976. intel_ddi_fdi_disable(crtc);
  3977. }
  3978. for_each_encoder_on_crtc(dev, crtc, encoder)
  3979. if (encoder->post_disable)
  3980. encoder->post_disable(encoder);
  3981. intel_crtc->active = false;
  3982. intel_update_watermarks(crtc);
  3983. mutex_lock(&dev->struct_mutex);
  3984. intel_fbc_update(dev);
  3985. mutex_unlock(&dev->struct_mutex);
  3986. if (intel_crtc_to_shared_dpll(intel_crtc))
  3987. intel_disable_shared_dpll(intel_crtc);
  3988. }
  3989. static void ironlake_crtc_off(struct drm_crtc *crtc)
  3990. {
  3991. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3992. intel_put_shared_dpll(intel_crtc);
  3993. }
  3994. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  3995. {
  3996. struct drm_device *dev = crtc->base.dev;
  3997. struct drm_i915_private *dev_priv = dev->dev_private;
  3998. struct intel_crtc_state *pipe_config = crtc->config;
  3999. if (!pipe_config->gmch_pfit.control)
  4000. return;
  4001. /*
  4002. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4003. * according to register description and PRM.
  4004. */
  4005. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4006. assert_pipe_disabled(dev_priv, crtc->pipe);
  4007. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4008. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4009. /* Border color in case we don't scale up to the full screen. Black by
  4010. * default, change to something else for debugging. */
  4011. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4012. }
  4013. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4014. {
  4015. switch (port) {
  4016. case PORT_A:
  4017. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  4018. case PORT_B:
  4019. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  4020. case PORT_C:
  4021. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  4022. case PORT_D:
  4023. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  4024. default:
  4025. WARN_ON_ONCE(1);
  4026. return POWER_DOMAIN_PORT_OTHER;
  4027. }
  4028. }
  4029. #define for_each_power_domain(domain, mask) \
  4030. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  4031. if ((1 << (domain)) & (mask))
  4032. enum intel_display_power_domain
  4033. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4034. {
  4035. struct drm_device *dev = intel_encoder->base.dev;
  4036. struct intel_digital_port *intel_dig_port;
  4037. switch (intel_encoder->type) {
  4038. case INTEL_OUTPUT_UNKNOWN:
  4039. /* Only DDI platforms should ever use this output type */
  4040. WARN_ON_ONCE(!HAS_DDI(dev));
  4041. case INTEL_OUTPUT_DISPLAYPORT:
  4042. case INTEL_OUTPUT_HDMI:
  4043. case INTEL_OUTPUT_EDP:
  4044. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4045. return port_to_power_domain(intel_dig_port->port);
  4046. case INTEL_OUTPUT_DP_MST:
  4047. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4048. return port_to_power_domain(intel_dig_port->port);
  4049. case INTEL_OUTPUT_ANALOG:
  4050. return POWER_DOMAIN_PORT_CRT;
  4051. case INTEL_OUTPUT_DSI:
  4052. return POWER_DOMAIN_PORT_DSI;
  4053. default:
  4054. return POWER_DOMAIN_PORT_OTHER;
  4055. }
  4056. }
  4057. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  4058. {
  4059. struct drm_device *dev = crtc->dev;
  4060. struct intel_encoder *intel_encoder;
  4061. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4062. enum pipe pipe = intel_crtc->pipe;
  4063. unsigned long mask;
  4064. enum transcoder transcoder;
  4065. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  4066. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4067. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4068. if (intel_crtc->config->pch_pfit.enabled ||
  4069. intel_crtc->config->pch_pfit.force_thru)
  4070. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4071. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4072. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4073. return mask;
  4074. }
  4075. static void modeset_update_crtc_power_domains(struct drm_device *dev)
  4076. {
  4077. struct drm_i915_private *dev_priv = dev->dev_private;
  4078. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  4079. struct intel_crtc *crtc;
  4080. /*
  4081. * First get all needed power domains, then put all unneeded, to avoid
  4082. * any unnecessary toggling of the power wells.
  4083. */
  4084. for_each_intel_crtc(dev, crtc) {
  4085. enum intel_display_power_domain domain;
  4086. if (!crtc->base.state->enable)
  4087. continue;
  4088. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  4089. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  4090. intel_display_power_get(dev_priv, domain);
  4091. }
  4092. if (dev_priv->display.modeset_global_resources)
  4093. dev_priv->display.modeset_global_resources(dev);
  4094. for_each_intel_crtc(dev, crtc) {
  4095. enum intel_display_power_domain domain;
  4096. for_each_power_domain(domain, crtc->enabled_power_domains)
  4097. intel_display_power_put(dev_priv, domain);
  4098. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  4099. }
  4100. intel_display_set_init_power(dev_priv, false);
  4101. }
  4102. /* returns HPLL frequency in kHz */
  4103. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  4104. {
  4105. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  4106. /* Obtain SKU information */
  4107. mutex_lock(&dev_priv->dpio_lock);
  4108. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  4109. CCK_FUSE_HPLL_FREQ_MASK;
  4110. mutex_unlock(&dev_priv->dpio_lock);
  4111. return vco_freq[hpll_freq] * 1000;
  4112. }
  4113. static void vlv_update_cdclk(struct drm_device *dev)
  4114. {
  4115. struct drm_i915_private *dev_priv = dev->dev_private;
  4116. dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4117. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4118. dev_priv->vlv_cdclk_freq);
  4119. /*
  4120. * Program the gmbus_freq based on the cdclk frequency.
  4121. * BSpec erroneously claims we should aim for 4MHz, but
  4122. * in fact 1MHz is the correct frequency.
  4123. */
  4124. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->vlv_cdclk_freq, 1000));
  4125. }
  4126. /* Adjust CDclk dividers to allow high res or save power if possible */
  4127. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4128. {
  4129. struct drm_i915_private *dev_priv = dev->dev_private;
  4130. u32 val, cmd;
  4131. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  4132. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4133. cmd = 2;
  4134. else if (cdclk == 266667)
  4135. cmd = 1;
  4136. else
  4137. cmd = 0;
  4138. mutex_lock(&dev_priv->rps.hw_lock);
  4139. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4140. val &= ~DSPFREQGUAR_MASK;
  4141. val |= (cmd << DSPFREQGUAR_SHIFT);
  4142. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4143. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4144. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4145. 50)) {
  4146. DRM_ERROR("timed out waiting for CDclk change\n");
  4147. }
  4148. mutex_unlock(&dev_priv->rps.hw_lock);
  4149. if (cdclk == 400000) {
  4150. u32 divider;
  4151. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4152. mutex_lock(&dev_priv->dpio_lock);
  4153. /* adjust cdclk divider */
  4154. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4155. val &= ~DISPLAY_FREQUENCY_VALUES;
  4156. val |= divider;
  4157. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4158. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4159. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4160. 50))
  4161. DRM_ERROR("timed out waiting for CDclk change\n");
  4162. mutex_unlock(&dev_priv->dpio_lock);
  4163. }
  4164. mutex_lock(&dev_priv->dpio_lock);
  4165. /* adjust self-refresh exit latency value */
  4166. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4167. val &= ~0x7f;
  4168. /*
  4169. * For high bandwidth configs, we set a higher latency in the bunit
  4170. * so that the core display fetch happens in time to avoid underruns.
  4171. */
  4172. if (cdclk == 400000)
  4173. val |= 4500 / 250; /* 4.5 usec */
  4174. else
  4175. val |= 3000 / 250; /* 3.0 usec */
  4176. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4177. mutex_unlock(&dev_priv->dpio_lock);
  4178. vlv_update_cdclk(dev);
  4179. }
  4180. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4181. {
  4182. struct drm_i915_private *dev_priv = dev->dev_private;
  4183. u32 val, cmd;
  4184. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  4185. switch (cdclk) {
  4186. case 333333:
  4187. case 320000:
  4188. case 266667:
  4189. case 200000:
  4190. break;
  4191. default:
  4192. MISSING_CASE(cdclk);
  4193. return;
  4194. }
  4195. /*
  4196. * Specs are full of misinformation, but testing on actual
  4197. * hardware has shown that we just need to write the desired
  4198. * CCK divider into the Punit register.
  4199. */
  4200. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4201. mutex_lock(&dev_priv->rps.hw_lock);
  4202. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4203. val &= ~DSPFREQGUAR_MASK_CHV;
  4204. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4205. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4206. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4207. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4208. 50)) {
  4209. DRM_ERROR("timed out waiting for CDclk change\n");
  4210. }
  4211. mutex_unlock(&dev_priv->rps.hw_lock);
  4212. vlv_update_cdclk(dev);
  4213. }
  4214. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4215. int max_pixclk)
  4216. {
  4217. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4218. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  4219. /*
  4220. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4221. * 200MHz
  4222. * 267MHz
  4223. * 320/333MHz (depends on HPLL freq)
  4224. * 400MHz (VLV only)
  4225. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  4226. * of the lower bin and adjust if needed.
  4227. *
  4228. * We seem to get an unstable or solid color picture at 200MHz.
  4229. * Not sure what's wrong. For now use 200MHz only when all pipes
  4230. * are off.
  4231. */
  4232. if (!IS_CHERRYVIEW(dev_priv) &&
  4233. max_pixclk > freq_320*limit/100)
  4234. return 400000;
  4235. else if (max_pixclk > 266667*limit/100)
  4236. return freq_320;
  4237. else if (max_pixclk > 0)
  4238. return 266667;
  4239. else
  4240. return 200000;
  4241. }
  4242. /* compute the max pixel clock for new configuration */
  4243. static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
  4244. {
  4245. struct drm_device *dev = dev_priv->dev;
  4246. struct intel_crtc *intel_crtc;
  4247. int max_pixclk = 0;
  4248. for_each_intel_crtc(dev, intel_crtc) {
  4249. if (intel_crtc->new_enabled)
  4250. max_pixclk = max(max_pixclk,
  4251. intel_crtc->new_config->base.adjusted_mode.crtc_clock);
  4252. }
  4253. return max_pixclk;
  4254. }
  4255. static void valleyview_modeset_global_pipes(struct drm_device *dev,
  4256. unsigned *prepare_pipes)
  4257. {
  4258. struct drm_i915_private *dev_priv = dev->dev_private;
  4259. struct intel_crtc *intel_crtc;
  4260. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4261. if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
  4262. dev_priv->vlv_cdclk_freq)
  4263. return;
  4264. /* disable/enable all currently active pipes while we change cdclk */
  4265. for_each_intel_crtc(dev, intel_crtc)
  4266. if (intel_crtc->base.state->enable)
  4267. *prepare_pipes |= (1 << intel_crtc->pipe);
  4268. }
  4269. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  4270. {
  4271. unsigned int credits, default_credits;
  4272. if (IS_CHERRYVIEW(dev_priv))
  4273. default_credits = PFI_CREDIT(12);
  4274. else
  4275. default_credits = PFI_CREDIT(8);
  4276. if (DIV_ROUND_CLOSEST(dev_priv->vlv_cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
  4277. /* CHV suggested value is 31 or 63 */
  4278. if (IS_CHERRYVIEW(dev_priv))
  4279. credits = PFI_CREDIT_31;
  4280. else
  4281. credits = PFI_CREDIT(15);
  4282. } else {
  4283. credits = default_credits;
  4284. }
  4285. /*
  4286. * WA - write default credits before re-programming
  4287. * FIXME: should we also set the resend bit here?
  4288. */
  4289. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  4290. default_credits);
  4291. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  4292. credits | PFI_CREDIT_RESEND);
  4293. /*
  4294. * FIXME is this guaranteed to clear
  4295. * immediately or should we poll for it?
  4296. */
  4297. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  4298. }
  4299. static void valleyview_modeset_global_resources(struct drm_device *dev)
  4300. {
  4301. struct drm_i915_private *dev_priv = dev->dev_private;
  4302. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4303. int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  4304. if (req_cdclk != dev_priv->vlv_cdclk_freq) {
  4305. /*
  4306. * FIXME: We can end up here with all power domains off, yet
  4307. * with a CDCLK frequency other than the minimum. To account
  4308. * for this take the PIPE-A power domain, which covers the HW
  4309. * blocks needed for the following programming. This can be
  4310. * removed once it's guaranteed that we get here either with
  4311. * the minimum CDCLK set, or the required power domains
  4312. * enabled.
  4313. */
  4314. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  4315. if (IS_CHERRYVIEW(dev))
  4316. cherryview_set_cdclk(dev, req_cdclk);
  4317. else
  4318. valleyview_set_cdclk(dev, req_cdclk);
  4319. vlv_program_pfi_credits(dev_priv);
  4320. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  4321. }
  4322. }
  4323. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  4324. {
  4325. struct drm_device *dev = crtc->dev;
  4326. struct drm_i915_private *dev_priv = to_i915(dev);
  4327. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4328. struct intel_encoder *encoder;
  4329. int pipe = intel_crtc->pipe;
  4330. bool is_dsi;
  4331. WARN_ON(!crtc->state->enable);
  4332. if (intel_crtc->active)
  4333. return;
  4334. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  4335. if (!is_dsi) {
  4336. if (IS_CHERRYVIEW(dev))
  4337. chv_prepare_pll(intel_crtc, intel_crtc->config);
  4338. else
  4339. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  4340. }
  4341. if (intel_crtc->config->has_dp_encoder)
  4342. intel_dp_set_m_n(intel_crtc, M1_N1);
  4343. intel_set_pipe_timings(intel_crtc);
  4344. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  4345. struct drm_i915_private *dev_priv = dev->dev_private;
  4346. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  4347. I915_WRITE(CHV_CANVAS(pipe), 0);
  4348. }
  4349. i9xx_set_pipeconf(intel_crtc);
  4350. intel_crtc->active = true;
  4351. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4352. for_each_encoder_on_crtc(dev, crtc, encoder)
  4353. if (encoder->pre_pll_enable)
  4354. encoder->pre_pll_enable(encoder);
  4355. if (!is_dsi) {
  4356. if (IS_CHERRYVIEW(dev))
  4357. chv_enable_pll(intel_crtc, intel_crtc->config);
  4358. else
  4359. vlv_enable_pll(intel_crtc, intel_crtc->config);
  4360. }
  4361. for_each_encoder_on_crtc(dev, crtc, encoder)
  4362. if (encoder->pre_enable)
  4363. encoder->pre_enable(encoder);
  4364. i9xx_pfit_enable(intel_crtc);
  4365. intel_crtc_load_lut(crtc);
  4366. intel_update_watermarks(crtc);
  4367. intel_enable_pipe(intel_crtc);
  4368. assert_vblank_disabled(crtc);
  4369. drm_crtc_vblank_on(crtc);
  4370. for_each_encoder_on_crtc(dev, crtc, encoder)
  4371. encoder->enable(encoder);
  4372. intel_crtc_enable_planes(crtc);
  4373. /* Underruns don't raise interrupts, so check manually. */
  4374. i9xx_check_fifo_underruns(dev_priv);
  4375. }
  4376. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  4377. {
  4378. struct drm_device *dev = crtc->base.dev;
  4379. struct drm_i915_private *dev_priv = dev->dev_private;
  4380. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  4381. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  4382. }
  4383. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  4384. {
  4385. struct drm_device *dev = crtc->dev;
  4386. struct drm_i915_private *dev_priv = to_i915(dev);
  4387. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4388. struct intel_encoder *encoder;
  4389. int pipe = intel_crtc->pipe;
  4390. WARN_ON(!crtc->state->enable);
  4391. if (intel_crtc->active)
  4392. return;
  4393. i9xx_set_pll_dividers(intel_crtc);
  4394. if (intel_crtc->config->has_dp_encoder)
  4395. intel_dp_set_m_n(intel_crtc, M1_N1);
  4396. intel_set_pipe_timings(intel_crtc);
  4397. i9xx_set_pipeconf(intel_crtc);
  4398. intel_crtc->active = true;
  4399. if (!IS_GEN2(dev))
  4400. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4401. for_each_encoder_on_crtc(dev, crtc, encoder)
  4402. if (encoder->pre_enable)
  4403. encoder->pre_enable(encoder);
  4404. i9xx_enable_pll(intel_crtc);
  4405. i9xx_pfit_enable(intel_crtc);
  4406. intel_crtc_load_lut(crtc);
  4407. intel_update_watermarks(crtc);
  4408. intel_enable_pipe(intel_crtc);
  4409. assert_vblank_disabled(crtc);
  4410. drm_crtc_vblank_on(crtc);
  4411. for_each_encoder_on_crtc(dev, crtc, encoder)
  4412. encoder->enable(encoder);
  4413. intel_crtc_enable_planes(crtc);
  4414. /*
  4415. * Gen2 reports pipe underruns whenever all planes are disabled.
  4416. * So don't enable underrun reporting before at least some planes
  4417. * are enabled.
  4418. * FIXME: Need to fix the logic to work when we turn off all planes
  4419. * but leave the pipe running.
  4420. */
  4421. if (IS_GEN2(dev))
  4422. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4423. /* Underruns don't raise interrupts, so check manually. */
  4424. i9xx_check_fifo_underruns(dev_priv);
  4425. }
  4426. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4427. {
  4428. struct drm_device *dev = crtc->base.dev;
  4429. struct drm_i915_private *dev_priv = dev->dev_private;
  4430. if (!crtc->config->gmch_pfit.control)
  4431. return;
  4432. assert_pipe_disabled(dev_priv, crtc->pipe);
  4433. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4434. I915_READ(PFIT_CONTROL));
  4435. I915_WRITE(PFIT_CONTROL, 0);
  4436. }
  4437. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  4438. {
  4439. struct drm_device *dev = crtc->dev;
  4440. struct drm_i915_private *dev_priv = dev->dev_private;
  4441. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4442. struct intel_encoder *encoder;
  4443. int pipe = intel_crtc->pipe;
  4444. if (!intel_crtc->active)
  4445. return;
  4446. /*
  4447. * Gen2 reports pipe underruns whenever all planes are disabled.
  4448. * So diasble underrun reporting before all the planes get disabled.
  4449. * FIXME: Need to fix the logic to work when we turn off all planes
  4450. * but leave the pipe running.
  4451. */
  4452. if (IS_GEN2(dev))
  4453. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4454. /*
  4455. * Vblank time updates from the shadow to live plane control register
  4456. * are blocked if the memory self-refresh mode is active at that
  4457. * moment. So to make sure the plane gets truly disabled, disable
  4458. * first the self-refresh mode. The self-refresh enable bit in turn
  4459. * will be checked/applied by the HW only at the next frame start
  4460. * event which is after the vblank start event, so we need to have a
  4461. * wait-for-vblank between disabling the plane and the pipe.
  4462. */
  4463. intel_set_memory_cxsr(dev_priv, false);
  4464. intel_crtc_disable_planes(crtc);
  4465. /*
  4466. * On gen2 planes are double buffered but the pipe isn't, so we must
  4467. * wait for planes to fully turn off before disabling the pipe.
  4468. * We also need to wait on all gmch platforms because of the
  4469. * self-refresh mode constraint explained above.
  4470. */
  4471. intel_wait_for_vblank(dev, pipe);
  4472. for_each_encoder_on_crtc(dev, crtc, encoder)
  4473. encoder->disable(encoder);
  4474. drm_crtc_vblank_off(crtc);
  4475. assert_vblank_disabled(crtc);
  4476. intel_disable_pipe(intel_crtc);
  4477. i9xx_pfit_disable(intel_crtc);
  4478. for_each_encoder_on_crtc(dev, crtc, encoder)
  4479. if (encoder->post_disable)
  4480. encoder->post_disable(encoder);
  4481. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  4482. if (IS_CHERRYVIEW(dev))
  4483. chv_disable_pll(dev_priv, pipe);
  4484. else if (IS_VALLEYVIEW(dev))
  4485. vlv_disable_pll(dev_priv, pipe);
  4486. else
  4487. i9xx_disable_pll(intel_crtc);
  4488. }
  4489. if (!IS_GEN2(dev))
  4490. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4491. intel_crtc->active = false;
  4492. intel_update_watermarks(crtc);
  4493. mutex_lock(&dev->struct_mutex);
  4494. intel_fbc_update(dev);
  4495. mutex_unlock(&dev->struct_mutex);
  4496. }
  4497. static void i9xx_crtc_off(struct drm_crtc *crtc)
  4498. {
  4499. }
  4500. /* Master function to enable/disable CRTC and corresponding power wells */
  4501. void intel_crtc_control(struct drm_crtc *crtc, bool enable)
  4502. {
  4503. struct drm_device *dev = crtc->dev;
  4504. struct drm_i915_private *dev_priv = dev->dev_private;
  4505. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4506. enum intel_display_power_domain domain;
  4507. unsigned long domains;
  4508. if (enable) {
  4509. if (!intel_crtc->active) {
  4510. domains = get_crtc_power_domains(crtc);
  4511. for_each_power_domain(domain, domains)
  4512. intel_display_power_get(dev_priv, domain);
  4513. intel_crtc->enabled_power_domains = domains;
  4514. dev_priv->display.crtc_enable(crtc);
  4515. }
  4516. } else {
  4517. if (intel_crtc->active) {
  4518. dev_priv->display.crtc_disable(crtc);
  4519. domains = intel_crtc->enabled_power_domains;
  4520. for_each_power_domain(domain, domains)
  4521. intel_display_power_put(dev_priv, domain);
  4522. intel_crtc->enabled_power_domains = 0;
  4523. }
  4524. }
  4525. }
  4526. /**
  4527. * Sets the power management mode of the pipe and plane.
  4528. */
  4529. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  4530. {
  4531. struct drm_device *dev = crtc->dev;
  4532. struct intel_encoder *intel_encoder;
  4533. bool enable = false;
  4534. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4535. enable |= intel_encoder->connectors_active;
  4536. intel_crtc_control(crtc, enable);
  4537. }
  4538. static void intel_crtc_disable(struct drm_crtc *crtc)
  4539. {
  4540. struct drm_device *dev = crtc->dev;
  4541. struct drm_connector *connector;
  4542. struct drm_i915_private *dev_priv = dev->dev_private;
  4543. /* crtc should still be enabled when we disable it. */
  4544. WARN_ON(!crtc->state->enable);
  4545. dev_priv->display.crtc_disable(crtc);
  4546. dev_priv->display.off(crtc);
  4547. crtc->primary->funcs->disable_plane(crtc->primary);
  4548. /* Update computed state. */
  4549. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  4550. if (!connector->encoder || !connector->encoder->crtc)
  4551. continue;
  4552. if (connector->encoder->crtc != crtc)
  4553. continue;
  4554. connector->dpms = DRM_MODE_DPMS_OFF;
  4555. to_intel_encoder(connector->encoder)->connectors_active = false;
  4556. }
  4557. }
  4558. void intel_encoder_destroy(struct drm_encoder *encoder)
  4559. {
  4560. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4561. drm_encoder_cleanup(encoder);
  4562. kfree(intel_encoder);
  4563. }
  4564. /* Simple dpms helper for encoders with just one connector, no cloning and only
  4565. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  4566. * state of the entire output pipe. */
  4567. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  4568. {
  4569. if (mode == DRM_MODE_DPMS_ON) {
  4570. encoder->connectors_active = true;
  4571. intel_crtc_update_dpms(encoder->base.crtc);
  4572. } else {
  4573. encoder->connectors_active = false;
  4574. intel_crtc_update_dpms(encoder->base.crtc);
  4575. }
  4576. }
  4577. /* Cross check the actual hw state with our own modeset state tracking (and it's
  4578. * internal consistency). */
  4579. static void intel_connector_check_state(struct intel_connector *connector)
  4580. {
  4581. if (connector->get_hw_state(connector)) {
  4582. struct intel_encoder *encoder = connector->encoder;
  4583. struct drm_crtc *crtc;
  4584. bool encoder_enabled;
  4585. enum pipe pipe;
  4586. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4587. connector->base.base.id,
  4588. connector->base.name);
  4589. /* there is no real hw state for MST connectors */
  4590. if (connector->mst_port)
  4591. return;
  4592. I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  4593. "wrong connector dpms state\n");
  4594. I915_STATE_WARN(connector->base.encoder != &encoder->base,
  4595. "active connector not linked to encoder\n");
  4596. if (encoder) {
  4597. I915_STATE_WARN(!encoder->connectors_active,
  4598. "encoder->connectors_active not set\n");
  4599. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  4600. I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
  4601. if (I915_STATE_WARN_ON(!encoder->base.crtc))
  4602. return;
  4603. crtc = encoder->base.crtc;
  4604. I915_STATE_WARN(!crtc->state->enable,
  4605. "crtc not enabled\n");
  4606. I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  4607. I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
  4608. "encoder active on the wrong pipe\n");
  4609. }
  4610. }
  4611. }
  4612. /* Even simpler default implementation, if there's really no special case to
  4613. * consider. */
  4614. void intel_connector_dpms(struct drm_connector *connector, int mode)
  4615. {
  4616. /* All the simple cases only support two dpms states. */
  4617. if (mode != DRM_MODE_DPMS_ON)
  4618. mode = DRM_MODE_DPMS_OFF;
  4619. if (mode == connector->dpms)
  4620. return;
  4621. connector->dpms = mode;
  4622. /* Only need to change hw state when actually enabled */
  4623. if (connector->encoder)
  4624. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  4625. intel_modeset_check_state(connector->dev);
  4626. }
  4627. /* Simple connector->get_hw_state implementation for encoders that support only
  4628. * one connector and no cloning and hence the encoder state determines the state
  4629. * of the connector. */
  4630. bool intel_connector_get_hw_state(struct intel_connector *connector)
  4631. {
  4632. enum pipe pipe = 0;
  4633. struct intel_encoder *encoder = connector->encoder;
  4634. return encoder->get_hw_state(encoder, &pipe);
  4635. }
  4636. static int pipe_required_fdi_lanes(struct drm_device *dev, enum pipe pipe)
  4637. {
  4638. struct intel_crtc *crtc =
  4639. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  4640. if (crtc->base.state->enable &&
  4641. crtc->config->has_pch_encoder)
  4642. return crtc->config->fdi_lanes;
  4643. return 0;
  4644. }
  4645. static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  4646. struct intel_crtc_state *pipe_config)
  4647. {
  4648. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  4649. pipe_name(pipe), pipe_config->fdi_lanes);
  4650. if (pipe_config->fdi_lanes > 4) {
  4651. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  4652. pipe_name(pipe), pipe_config->fdi_lanes);
  4653. return false;
  4654. }
  4655. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  4656. if (pipe_config->fdi_lanes > 2) {
  4657. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  4658. pipe_config->fdi_lanes);
  4659. return false;
  4660. } else {
  4661. return true;
  4662. }
  4663. }
  4664. if (INTEL_INFO(dev)->num_pipes == 2)
  4665. return true;
  4666. /* Ivybridge 3 pipe is really complicated */
  4667. switch (pipe) {
  4668. case PIPE_A:
  4669. return true;
  4670. case PIPE_B:
  4671. if (pipe_config->fdi_lanes > 2 &&
  4672. pipe_required_fdi_lanes(dev, PIPE_C) > 0) {
  4673. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4674. pipe_name(pipe), pipe_config->fdi_lanes);
  4675. return false;
  4676. }
  4677. return true;
  4678. case PIPE_C:
  4679. if (pipe_config->fdi_lanes > 2) {
  4680. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  4681. pipe_name(pipe), pipe_config->fdi_lanes);
  4682. return false;
  4683. }
  4684. if (pipe_required_fdi_lanes(dev, PIPE_B) > 2) {
  4685. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  4686. return false;
  4687. }
  4688. return true;
  4689. default:
  4690. BUG();
  4691. }
  4692. }
  4693. #define RETRY 1
  4694. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  4695. struct intel_crtc_state *pipe_config)
  4696. {
  4697. struct drm_device *dev = intel_crtc->base.dev;
  4698. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  4699. int lane, link_bw, fdi_dotclock;
  4700. bool setup_ok, needs_recompute = false;
  4701. retry:
  4702. /* FDI is a binary signal running at ~2.7GHz, encoding
  4703. * each output octet as 10 bits. The actual frequency
  4704. * is stored as a divider into a 100MHz clock, and the
  4705. * mode pixel clock is stored in units of 1KHz.
  4706. * Hence the bw of each lane in terms of the mode signal
  4707. * is:
  4708. */
  4709. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4710. fdi_dotclock = adjusted_mode->crtc_clock;
  4711. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  4712. pipe_config->pipe_bpp);
  4713. pipe_config->fdi_lanes = lane;
  4714. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  4715. link_bw, &pipe_config->fdi_m_n);
  4716. setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  4717. intel_crtc->pipe, pipe_config);
  4718. if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
  4719. pipe_config->pipe_bpp -= 2*3;
  4720. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  4721. pipe_config->pipe_bpp);
  4722. needs_recompute = true;
  4723. pipe_config->bw_constrained = true;
  4724. goto retry;
  4725. }
  4726. if (needs_recompute)
  4727. return RETRY;
  4728. return setup_ok ? 0 : -EINVAL;
  4729. }
  4730. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  4731. struct intel_crtc_state *pipe_config)
  4732. {
  4733. pipe_config->ips_enabled = i915.enable_ips &&
  4734. hsw_crtc_supports_ips(crtc) &&
  4735. pipe_config->pipe_bpp <= 24;
  4736. }
  4737. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  4738. struct intel_crtc_state *pipe_config)
  4739. {
  4740. struct drm_device *dev = crtc->base.dev;
  4741. struct drm_i915_private *dev_priv = dev->dev_private;
  4742. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  4743. /* FIXME should check pixel clock limits on all platforms */
  4744. if (INTEL_INFO(dev)->gen < 4) {
  4745. int clock_limit =
  4746. dev_priv->display.get_display_clock_speed(dev);
  4747. /*
  4748. * Enable pixel doubling when the dot clock
  4749. * is > 90% of the (display) core speed.
  4750. *
  4751. * GDG double wide on either pipe,
  4752. * otherwise pipe A only.
  4753. */
  4754. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  4755. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  4756. clock_limit *= 2;
  4757. pipe_config->double_wide = true;
  4758. }
  4759. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  4760. return -EINVAL;
  4761. }
  4762. /*
  4763. * Pipe horizontal size must be even in:
  4764. * - DVO ganged mode
  4765. * - LVDS dual channel mode
  4766. * - Double wide pipe
  4767. */
  4768. if ((intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  4769. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  4770. pipe_config->pipe_src_w &= ~1;
  4771. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  4772. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  4773. */
  4774. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  4775. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  4776. return -EINVAL;
  4777. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
  4778. pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
  4779. } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
  4780. /* only a 8bpc pipe, with 6bpc dither through the panel fitter
  4781. * for lvds. */
  4782. pipe_config->pipe_bpp = 8*3;
  4783. }
  4784. if (HAS_IPS(dev))
  4785. hsw_compute_ips_config(crtc, pipe_config);
  4786. if (pipe_config->has_pch_encoder)
  4787. return ironlake_fdi_compute_config(crtc, pipe_config);
  4788. return 0;
  4789. }
  4790. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  4791. {
  4792. struct drm_i915_private *dev_priv = dev->dev_private;
  4793. u32 val;
  4794. int divider;
  4795. if (dev_priv->hpll_freq == 0)
  4796. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  4797. mutex_lock(&dev_priv->dpio_lock);
  4798. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4799. mutex_unlock(&dev_priv->dpio_lock);
  4800. divider = val & DISPLAY_FREQUENCY_VALUES;
  4801. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  4802. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4803. "cdclk change in progress\n");
  4804. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  4805. }
  4806. static int i945_get_display_clock_speed(struct drm_device *dev)
  4807. {
  4808. return 400000;
  4809. }
  4810. static int i915_get_display_clock_speed(struct drm_device *dev)
  4811. {
  4812. return 333000;
  4813. }
  4814. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  4815. {
  4816. return 200000;
  4817. }
  4818. static int pnv_get_display_clock_speed(struct drm_device *dev)
  4819. {
  4820. u16 gcfgc = 0;
  4821. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4822. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4823. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  4824. return 267000;
  4825. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  4826. return 333000;
  4827. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  4828. return 444000;
  4829. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  4830. return 200000;
  4831. default:
  4832. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  4833. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  4834. return 133000;
  4835. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  4836. return 167000;
  4837. }
  4838. }
  4839. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  4840. {
  4841. u16 gcfgc = 0;
  4842. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4843. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  4844. return 133000;
  4845. else {
  4846. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4847. case GC_DISPLAY_CLOCK_333_MHZ:
  4848. return 333000;
  4849. default:
  4850. case GC_DISPLAY_CLOCK_190_200_MHZ:
  4851. return 190000;
  4852. }
  4853. }
  4854. }
  4855. static int i865_get_display_clock_speed(struct drm_device *dev)
  4856. {
  4857. return 266000;
  4858. }
  4859. static int i855_get_display_clock_speed(struct drm_device *dev)
  4860. {
  4861. u16 hpllcc = 0;
  4862. /* Assume that the hardware is in the high speed state. This
  4863. * should be the default.
  4864. */
  4865. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  4866. case GC_CLOCK_133_200:
  4867. case GC_CLOCK_100_200:
  4868. return 200000;
  4869. case GC_CLOCK_166_250:
  4870. return 250000;
  4871. case GC_CLOCK_100_133:
  4872. return 133000;
  4873. }
  4874. /* Shouldn't happen */
  4875. return 0;
  4876. }
  4877. static int i830_get_display_clock_speed(struct drm_device *dev)
  4878. {
  4879. return 133000;
  4880. }
  4881. static void
  4882. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  4883. {
  4884. while (*num > DATA_LINK_M_N_MASK ||
  4885. *den > DATA_LINK_M_N_MASK) {
  4886. *num >>= 1;
  4887. *den >>= 1;
  4888. }
  4889. }
  4890. static void compute_m_n(unsigned int m, unsigned int n,
  4891. uint32_t *ret_m, uint32_t *ret_n)
  4892. {
  4893. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  4894. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  4895. intel_reduce_m_n_ratio(ret_m, ret_n);
  4896. }
  4897. void
  4898. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  4899. int pixel_clock, int link_clock,
  4900. struct intel_link_m_n *m_n)
  4901. {
  4902. m_n->tu = 64;
  4903. compute_m_n(bits_per_pixel * pixel_clock,
  4904. link_clock * nlanes * 8,
  4905. &m_n->gmch_m, &m_n->gmch_n);
  4906. compute_m_n(pixel_clock, link_clock,
  4907. &m_n->link_m, &m_n->link_n);
  4908. }
  4909. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  4910. {
  4911. if (i915.panel_use_ssc >= 0)
  4912. return i915.panel_use_ssc != 0;
  4913. return dev_priv->vbt.lvds_use_ssc
  4914. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  4915. }
  4916. static int i9xx_get_refclk(struct intel_crtc *crtc, int num_connectors)
  4917. {
  4918. struct drm_device *dev = crtc->base.dev;
  4919. struct drm_i915_private *dev_priv = dev->dev_private;
  4920. int refclk;
  4921. if (IS_VALLEYVIEW(dev)) {
  4922. refclk = 100000;
  4923. } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  4924. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4925. refclk = dev_priv->vbt.lvds_ssc_freq;
  4926. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  4927. } else if (!IS_GEN2(dev)) {
  4928. refclk = 96000;
  4929. } else {
  4930. refclk = 48000;
  4931. }
  4932. return refclk;
  4933. }
  4934. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  4935. {
  4936. return (1 << dpll->n) << 16 | dpll->m2;
  4937. }
  4938. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  4939. {
  4940. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  4941. }
  4942. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  4943. struct intel_crtc_state *crtc_state,
  4944. intel_clock_t *reduced_clock)
  4945. {
  4946. struct drm_device *dev = crtc->base.dev;
  4947. u32 fp, fp2 = 0;
  4948. if (IS_PINEVIEW(dev)) {
  4949. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  4950. if (reduced_clock)
  4951. fp2 = pnv_dpll_compute_fp(reduced_clock);
  4952. } else {
  4953. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  4954. if (reduced_clock)
  4955. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  4956. }
  4957. crtc_state->dpll_hw_state.fp0 = fp;
  4958. crtc->lowfreq_avail = false;
  4959. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  4960. reduced_clock && i915.powersave) {
  4961. crtc_state->dpll_hw_state.fp1 = fp2;
  4962. crtc->lowfreq_avail = true;
  4963. } else {
  4964. crtc_state->dpll_hw_state.fp1 = fp;
  4965. }
  4966. }
  4967. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  4968. pipe)
  4969. {
  4970. u32 reg_val;
  4971. /*
  4972. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  4973. * and set it to a reasonable value instead.
  4974. */
  4975. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4976. reg_val &= 0xffffff00;
  4977. reg_val |= 0x00000030;
  4978. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4979. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4980. reg_val &= 0x8cffffff;
  4981. reg_val = 0x8c000000;
  4982. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4983. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4984. reg_val &= 0xffffff00;
  4985. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4986. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4987. reg_val &= 0x00ffffff;
  4988. reg_val |= 0xb0000000;
  4989. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4990. }
  4991. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  4992. struct intel_link_m_n *m_n)
  4993. {
  4994. struct drm_device *dev = crtc->base.dev;
  4995. struct drm_i915_private *dev_priv = dev->dev_private;
  4996. int pipe = crtc->pipe;
  4997. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4998. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  4999. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5000. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5001. }
  5002. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5003. struct intel_link_m_n *m_n,
  5004. struct intel_link_m_n *m2_n2)
  5005. {
  5006. struct drm_device *dev = crtc->base.dev;
  5007. struct drm_i915_private *dev_priv = dev->dev_private;
  5008. int pipe = crtc->pipe;
  5009. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5010. if (INTEL_INFO(dev)->gen >= 5) {
  5011. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5012. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5013. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5014. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5015. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5016. * for gen < 8) and if DRRS is supported (to make sure the
  5017. * registers are not unnecessarily accessed).
  5018. */
  5019. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  5020. crtc->config->has_drrs) {
  5021. I915_WRITE(PIPE_DATA_M2(transcoder),
  5022. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  5023. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  5024. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  5025. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  5026. }
  5027. } else {
  5028. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5029. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  5030. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  5031. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  5032. }
  5033. }
  5034. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  5035. {
  5036. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  5037. if (m_n == M1_N1) {
  5038. dp_m_n = &crtc->config->dp_m_n;
  5039. dp_m2_n2 = &crtc->config->dp_m2_n2;
  5040. } else if (m_n == M2_N2) {
  5041. /*
  5042. * M2_N2 registers are not supported. Hence m2_n2 divider value
  5043. * needs to be programmed into M1_N1.
  5044. */
  5045. dp_m_n = &crtc->config->dp_m2_n2;
  5046. } else {
  5047. DRM_ERROR("Unsupported divider value\n");
  5048. return;
  5049. }
  5050. if (crtc->config->has_pch_encoder)
  5051. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  5052. else
  5053. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  5054. }
  5055. static void vlv_update_pll(struct intel_crtc *crtc,
  5056. struct intel_crtc_state *pipe_config)
  5057. {
  5058. u32 dpll, dpll_md;
  5059. /*
  5060. * Enable DPIO clock input. We should never disable the reference
  5061. * clock for pipe B, since VGA hotplug / manual detection depends
  5062. * on it.
  5063. */
  5064. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  5065. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  5066. /* We should never disable this, set it here for state tracking */
  5067. if (crtc->pipe == PIPE_B)
  5068. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5069. dpll |= DPLL_VCO_ENABLE;
  5070. pipe_config->dpll_hw_state.dpll = dpll;
  5071. dpll_md = (pipe_config->pixel_multiplier - 1)
  5072. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5073. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  5074. }
  5075. static void vlv_prepare_pll(struct intel_crtc *crtc,
  5076. const struct intel_crtc_state *pipe_config)
  5077. {
  5078. struct drm_device *dev = crtc->base.dev;
  5079. struct drm_i915_private *dev_priv = dev->dev_private;
  5080. int pipe = crtc->pipe;
  5081. u32 mdiv;
  5082. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  5083. u32 coreclk, reg_val;
  5084. mutex_lock(&dev_priv->dpio_lock);
  5085. bestn = pipe_config->dpll.n;
  5086. bestm1 = pipe_config->dpll.m1;
  5087. bestm2 = pipe_config->dpll.m2;
  5088. bestp1 = pipe_config->dpll.p1;
  5089. bestp2 = pipe_config->dpll.p2;
  5090. /* See eDP HDMI DPIO driver vbios notes doc */
  5091. /* PLL B needs special handling */
  5092. if (pipe == PIPE_B)
  5093. vlv_pllb_recal_opamp(dev_priv, pipe);
  5094. /* Set up Tx target for periodic Rcomp update */
  5095. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  5096. /* Disable target IRef on PLL */
  5097. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  5098. reg_val &= 0x00ffffff;
  5099. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  5100. /* Disable fast lock */
  5101. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  5102. /* Set idtafcrecal before PLL is enabled */
  5103. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  5104. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  5105. mdiv |= ((bestn << DPIO_N_SHIFT));
  5106. mdiv |= (1 << DPIO_K_SHIFT);
  5107. /*
  5108. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  5109. * but we don't support that).
  5110. * Note: don't use the DAC post divider as it seems unstable.
  5111. */
  5112. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5113. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5114. mdiv |= DPIO_ENABLE_CALIBRATION;
  5115. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5116. /* Set HBR and RBR LPF coefficients */
  5117. if (pipe_config->port_clock == 162000 ||
  5118. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  5119. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  5120. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5121. 0x009f0003);
  5122. else
  5123. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5124. 0x00d0000f);
  5125. if (pipe_config->has_dp_encoder) {
  5126. /* Use SSC source */
  5127. if (pipe == PIPE_A)
  5128. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5129. 0x0df40000);
  5130. else
  5131. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5132. 0x0df70000);
  5133. } else { /* HDMI or VGA */
  5134. /* Use bend source */
  5135. if (pipe == PIPE_A)
  5136. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5137. 0x0df70000);
  5138. else
  5139. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5140. 0x0df40000);
  5141. }
  5142. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5143. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5144. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  5145. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  5146. coreclk |= 0x01000000;
  5147. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5148. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5149. mutex_unlock(&dev_priv->dpio_lock);
  5150. }
  5151. static void chv_update_pll(struct intel_crtc *crtc,
  5152. struct intel_crtc_state *pipe_config)
  5153. {
  5154. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  5155. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  5156. DPLL_VCO_ENABLE;
  5157. if (crtc->pipe != PIPE_A)
  5158. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5159. pipe_config->dpll_hw_state.dpll_md =
  5160. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5161. }
  5162. static void chv_prepare_pll(struct intel_crtc *crtc,
  5163. const struct intel_crtc_state *pipe_config)
  5164. {
  5165. struct drm_device *dev = crtc->base.dev;
  5166. struct drm_i915_private *dev_priv = dev->dev_private;
  5167. int pipe = crtc->pipe;
  5168. int dpll_reg = DPLL(crtc->pipe);
  5169. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5170. u32 loopfilter, tribuf_calcntr;
  5171. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5172. u32 dpio_val;
  5173. int vco;
  5174. bestn = pipe_config->dpll.n;
  5175. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5176. bestm1 = pipe_config->dpll.m1;
  5177. bestm2 = pipe_config->dpll.m2 >> 22;
  5178. bestp1 = pipe_config->dpll.p1;
  5179. bestp2 = pipe_config->dpll.p2;
  5180. vco = pipe_config->dpll.vco;
  5181. dpio_val = 0;
  5182. loopfilter = 0;
  5183. /*
  5184. * Enable Refclk and SSC
  5185. */
  5186. I915_WRITE(dpll_reg,
  5187. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5188. mutex_lock(&dev_priv->dpio_lock);
  5189. /* p1 and p2 divider */
  5190. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5191. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5192. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5193. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5194. 1 << DPIO_CHV_K_DIV_SHIFT);
  5195. /* Feedback post-divider - m2 */
  5196. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5197. /* Feedback refclk divider - n and m1 */
  5198. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5199. DPIO_CHV_M1_DIV_BY_2 |
  5200. 1 << DPIO_CHV_N_DIV_SHIFT);
  5201. /* M2 fraction division */
  5202. if (bestm2_frac)
  5203. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5204. /* M2 fraction division enable */
  5205. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  5206. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  5207. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  5208. if (bestm2_frac)
  5209. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  5210. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  5211. /* Program digital lock detect threshold */
  5212. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  5213. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  5214. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  5215. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  5216. if (!bestm2_frac)
  5217. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  5218. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  5219. /* Loop filter */
  5220. if (vco == 5400000) {
  5221. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  5222. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  5223. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5224. tribuf_calcntr = 0x9;
  5225. } else if (vco <= 6200000) {
  5226. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  5227. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  5228. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5229. tribuf_calcntr = 0x9;
  5230. } else if (vco <= 6480000) {
  5231. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5232. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5233. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5234. tribuf_calcntr = 0x8;
  5235. } else {
  5236. /* Not supported. Apply the same limits as in the max case */
  5237. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5238. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5239. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5240. tribuf_calcntr = 0;
  5241. }
  5242. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5243. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(pipe));
  5244. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  5245. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  5246. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  5247. /* AFC Recal */
  5248. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5249. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5250. DPIO_AFC_RECAL);
  5251. mutex_unlock(&dev_priv->dpio_lock);
  5252. }
  5253. /**
  5254. * vlv_force_pll_on - forcibly enable just the PLL
  5255. * @dev_priv: i915 private structure
  5256. * @pipe: pipe PLL to enable
  5257. * @dpll: PLL configuration
  5258. *
  5259. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5260. * in cases where we need the PLL enabled even when @pipe is not going to
  5261. * be enabled.
  5262. */
  5263. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  5264. const struct dpll *dpll)
  5265. {
  5266. struct intel_crtc *crtc =
  5267. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  5268. struct intel_crtc_state pipe_config = {
  5269. .pixel_multiplier = 1,
  5270. .dpll = *dpll,
  5271. };
  5272. if (IS_CHERRYVIEW(dev)) {
  5273. chv_update_pll(crtc, &pipe_config);
  5274. chv_prepare_pll(crtc, &pipe_config);
  5275. chv_enable_pll(crtc, &pipe_config);
  5276. } else {
  5277. vlv_update_pll(crtc, &pipe_config);
  5278. vlv_prepare_pll(crtc, &pipe_config);
  5279. vlv_enable_pll(crtc, &pipe_config);
  5280. }
  5281. }
  5282. /**
  5283. * vlv_force_pll_off - forcibly disable just the PLL
  5284. * @dev_priv: i915 private structure
  5285. * @pipe: pipe PLL to disable
  5286. *
  5287. * Disable the PLL for @pipe. To be used in cases where we need
  5288. * the PLL enabled even when @pipe is not going to be enabled.
  5289. */
  5290. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  5291. {
  5292. if (IS_CHERRYVIEW(dev))
  5293. chv_disable_pll(to_i915(dev), pipe);
  5294. else
  5295. vlv_disable_pll(to_i915(dev), pipe);
  5296. }
  5297. static void i9xx_update_pll(struct intel_crtc *crtc,
  5298. struct intel_crtc_state *crtc_state,
  5299. intel_clock_t *reduced_clock,
  5300. int num_connectors)
  5301. {
  5302. struct drm_device *dev = crtc->base.dev;
  5303. struct drm_i915_private *dev_priv = dev->dev_private;
  5304. u32 dpll;
  5305. bool is_sdvo;
  5306. struct dpll *clock = &crtc_state->dpll;
  5307. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5308. is_sdvo = intel_pipe_will_have_type(crtc, INTEL_OUTPUT_SDVO) ||
  5309. intel_pipe_will_have_type(crtc, INTEL_OUTPUT_HDMI);
  5310. dpll = DPLL_VGA_MODE_DIS;
  5311. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  5312. dpll |= DPLLB_MODE_LVDS;
  5313. else
  5314. dpll |= DPLLB_MODE_DAC_SERIAL;
  5315. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5316. dpll |= (crtc_state->pixel_multiplier - 1)
  5317. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5318. }
  5319. if (is_sdvo)
  5320. dpll |= DPLL_SDVO_HIGH_SPEED;
  5321. if (crtc_state->has_dp_encoder)
  5322. dpll |= DPLL_SDVO_HIGH_SPEED;
  5323. /* compute bitmask from p1 value */
  5324. if (IS_PINEVIEW(dev))
  5325. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5326. else {
  5327. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5328. if (IS_G4X(dev) && reduced_clock)
  5329. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5330. }
  5331. switch (clock->p2) {
  5332. case 5:
  5333. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5334. break;
  5335. case 7:
  5336. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5337. break;
  5338. case 10:
  5339. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5340. break;
  5341. case 14:
  5342. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5343. break;
  5344. }
  5345. if (INTEL_INFO(dev)->gen >= 4)
  5346. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5347. if (crtc_state->sdvo_tv_clock)
  5348. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5349. else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  5350. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5351. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5352. else
  5353. dpll |= PLL_REF_INPUT_DREFCLK;
  5354. dpll |= DPLL_VCO_ENABLE;
  5355. crtc_state->dpll_hw_state.dpll = dpll;
  5356. if (INTEL_INFO(dev)->gen >= 4) {
  5357. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  5358. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5359. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  5360. }
  5361. }
  5362. static void i8xx_update_pll(struct intel_crtc *crtc,
  5363. struct intel_crtc_state *crtc_state,
  5364. intel_clock_t *reduced_clock,
  5365. int num_connectors)
  5366. {
  5367. struct drm_device *dev = crtc->base.dev;
  5368. struct drm_i915_private *dev_priv = dev->dev_private;
  5369. u32 dpll;
  5370. struct dpll *clock = &crtc_state->dpll;
  5371. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5372. dpll = DPLL_VGA_MODE_DIS;
  5373. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  5374. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5375. } else {
  5376. if (clock->p1 == 2)
  5377. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5378. else
  5379. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5380. if (clock->p2 == 4)
  5381. dpll |= PLL_P2_DIVIDE_BY_4;
  5382. }
  5383. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc, INTEL_OUTPUT_DVO))
  5384. dpll |= DPLL_DVO_2X_MODE;
  5385. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  5386. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5387. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5388. else
  5389. dpll |= PLL_REF_INPUT_DREFCLK;
  5390. dpll |= DPLL_VCO_ENABLE;
  5391. crtc_state->dpll_hw_state.dpll = dpll;
  5392. }
  5393. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5394. {
  5395. struct drm_device *dev = intel_crtc->base.dev;
  5396. struct drm_i915_private *dev_priv = dev->dev_private;
  5397. enum pipe pipe = intel_crtc->pipe;
  5398. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  5399. struct drm_display_mode *adjusted_mode =
  5400. &intel_crtc->config->base.adjusted_mode;
  5401. uint32_t crtc_vtotal, crtc_vblank_end;
  5402. int vsyncshift = 0;
  5403. /* We need to be careful not to changed the adjusted mode, for otherwise
  5404. * the hw state checker will get angry at the mismatch. */
  5405. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5406. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5407. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5408. /* the chip adds 2 halflines automatically */
  5409. crtc_vtotal -= 1;
  5410. crtc_vblank_end -= 1;
  5411. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  5412. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5413. else
  5414. vsyncshift = adjusted_mode->crtc_hsync_start -
  5415. adjusted_mode->crtc_htotal / 2;
  5416. if (vsyncshift < 0)
  5417. vsyncshift += adjusted_mode->crtc_htotal;
  5418. }
  5419. if (INTEL_INFO(dev)->gen > 3)
  5420. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5421. I915_WRITE(HTOTAL(cpu_transcoder),
  5422. (adjusted_mode->crtc_hdisplay - 1) |
  5423. ((adjusted_mode->crtc_htotal - 1) << 16));
  5424. I915_WRITE(HBLANK(cpu_transcoder),
  5425. (adjusted_mode->crtc_hblank_start - 1) |
  5426. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5427. I915_WRITE(HSYNC(cpu_transcoder),
  5428. (adjusted_mode->crtc_hsync_start - 1) |
  5429. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5430. I915_WRITE(VTOTAL(cpu_transcoder),
  5431. (adjusted_mode->crtc_vdisplay - 1) |
  5432. ((crtc_vtotal - 1) << 16));
  5433. I915_WRITE(VBLANK(cpu_transcoder),
  5434. (adjusted_mode->crtc_vblank_start - 1) |
  5435. ((crtc_vblank_end - 1) << 16));
  5436. I915_WRITE(VSYNC(cpu_transcoder),
  5437. (adjusted_mode->crtc_vsync_start - 1) |
  5438. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5439. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5440. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5441. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5442. * bits. */
  5443. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  5444. (pipe == PIPE_B || pipe == PIPE_C))
  5445. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5446. /* pipesrc controls the size that is scaled from, which should
  5447. * always be the user's requested size.
  5448. */
  5449. I915_WRITE(PIPESRC(pipe),
  5450. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  5451. (intel_crtc->config->pipe_src_h - 1));
  5452. }
  5453. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5454. struct intel_crtc_state *pipe_config)
  5455. {
  5456. struct drm_device *dev = crtc->base.dev;
  5457. struct drm_i915_private *dev_priv = dev->dev_private;
  5458. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5459. uint32_t tmp;
  5460. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5461. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5462. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5463. tmp = I915_READ(HBLANK(cpu_transcoder));
  5464. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5465. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5466. tmp = I915_READ(HSYNC(cpu_transcoder));
  5467. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5468. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5469. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5470. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5471. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  5472. tmp = I915_READ(VBLANK(cpu_transcoder));
  5473. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  5474. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  5475. tmp = I915_READ(VSYNC(cpu_transcoder));
  5476. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  5477. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  5478. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  5479. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  5480. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  5481. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  5482. }
  5483. tmp = I915_READ(PIPESRC(crtc->pipe));
  5484. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  5485. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  5486. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  5487. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  5488. }
  5489. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  5490. struct intel_crtc_state *pipe_config)
  5491. {
  5492. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  5493. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  5494. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  5495. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  5496. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  5497. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  5498. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  5499. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  5500. mode->flags = pipe_config->base.adjusted_mode.flags;
  5501. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  5502. mode->flags |= pipe_config->base.adjusted_mode.flags;
  5503. }
  5504. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  5505. {
  5506. struct drm_device *dev = intel_crtc->base.dev;
  5507. struct drm_i915_private *dev_priv = dev->dev_private;
  5508. uint32_t pipeconf;
  5509. pipeconf = 0;
  5510. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  5511. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  5512. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  5513. if (intel_crtc->config->double_wide)
  5514. pipeconf |= PIPECONF_DOUBLE_WIDE;
  5515. /* only g4x and later have fancy bpc/dither controls */
  5516. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5517. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  5518. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  5519. pipeconf |= PIPECONF_DITHER_EN |
  5520. PIPECONF_DITHER_TYPE_SP;
  5521. switch (intel_crtc->config->pipe_bpp) {
  5522. case 18:
  5523. pipeconf |= PIPECONF_6BPC;
  5524. break;
  5525. case 24:
  5526. pipeconf |= PIPECONF_8BPC;
  5527. break;
  5528. case 30:
  5529. pipeconf |= PIPECONF_10BPC;
  5530. break;
  5531. default:
  5532. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5533. BUG();
  5534. }
  5535. }
  5536. if (HAS_PIPE_CXSR(dev)) {
  5537. if (intel_crtc->lowfreq_avail) {
  5538. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  5539. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  5540. } else {
  5541. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  5542. }
  5543. }
  5544. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  5545. if (INTEL_INFO(dev)->gen < 4 ||
  5546. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  5547. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  5548. else
  5549. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  5550. } else
  5551. pipeconf |= PIPECONF_PROGRESSIVE;
  5552. if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
  5553. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  5554. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  5555. POSTING_READ(PIPECONF(intel_crtc->pipe));
  5556. }
  5557. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  5558. struct intel_crtc_state *crtc_state)
  5559. {
  5560. struct drm_device *dev = crtc->base.dev;
  5561. struct drm_i915_private *dev_priv = dev->dev_private;
  5562. int refclk, num_connectors = 0;
  5563. intel_clock_t clock, reduced_clock;
  5564. bool ok, has_reduced_clock = false;
  5565. bool is_lvds = false, is_dsi = false;
  5566. struct intel_encoder *encoder;
  5567. const intel_limit_t *limit;
  5568. for_each_intel_encoder(dev, encoder) {
  5569. if (encoder->new_crtc != crtc)
  5570. continue;
  5571. switch (encoder->type) {
  5572. case INTEL_OUTPUT_LVDS:
  5573. is_lvds = true;
  5574. break;
  5575. case INTEL_OUTPUT_DSI:
  5576. is_dsi = true;
  5577. break;
  5578. default:
  5579. break;
  5580. }
  5581. num_connectors++;
  5582. }
  5583. if (is_dsi)
  5584. return 0;
  5585. if (!crtc_state->clock_set) {
  5586. refclk = i9xx_get_refclk(crtc, num_connectors);
  5587. /*
  5588. * Returns a set of divisors for the desired target clock with
  5589. * the given refclk, or FALSE. The returned values represent
  5590. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  5591. * 2) / p1 / p2.
  5592. */
  5593. limit = intel_limit(crtc, refclk);
  5594. ok = dev_priv->display.find_dpll(limit, crtc,
  5595. crtc_state->port_clock,
  5596. refclk, NULL, &clock);
  5597. if (!ok) {
  5598. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5599. return -EINVAL;
  5600. }
  5601. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5602. /*
  5603. * Ensure we match the reduced clock's P to the target
  5604. * clock. If the clocks don't match, we can't switch
  5605. * the display clock by using the FP0/FP1. In such case
  5606. * we will disable the LVDS downclock feature.
  5607. */
  5608. has_reduced_clock =
  5609. dev_priv->display.find_dpll(limit, crtc,
  5610. dev_priv->lvds_downclock,
  5611. refclk, &clock,
  5612. &reduced_clock);
  5613. }
  5614. /* Compat-code for transition, will disappear. */
  5615. crtc_state->dpll.n = clock.n;
  5616. crtc_state->dpll.m1 = clock.m1;
  5617. crtc_state->dpll.m2 = clock.m2;
  5618. crtc_state->dpll.p1 = clock.p1;
  5619. crtc_state->dpll.p2 = clock.p2;
  5620. }
  5621. if (IS_GEN2(dev)) {
  5622. i8xx_update_pll(crtc, crtc_state,
  5623. has_reduced_clock ? &reduced_clock : NULL,
  5624. num_connectors);
  5625. } else if (IS_CHERRYVIEW(dev)) {
  5626. chv_update_pll(crtc, crtc_state);
  5627. } else if (IS_VALLEYVIEW(dev)) {
  5628. vlv_update_pll(crtc, crtc_state);
  5629. } else {
  5630. i9xx_update_pll(crtc, crtc_state,
  5631. has_reduced_clock ? &reduced_clock : NULL,
  5632. num_connectors);
  5633. }
  5634. return 0;
  5635. }
  5636. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  5637. struct intel_crtc_state *pipe_config)
  5638. {
  5639. struct drm_device *dev = crtc->base.dev;
  5640. struct drm_i915_private *dev_priv = dev->dev_private;
  5641. uint32_t tmp;
  5642. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  5643. return;
  5644. tmp = I915_READ(PFIT_CONTROL);
  5645. if (!(tmp & PFIT_ENABLE))
  5646. return;
  5647. /* Check whether the pfit is attached to our pipe. */
  5648. if (INTEL_INFO(dev)->gen < 4) {
  5649. if (crtc->pipe != PIPE_B)
  5650. return;
  5651. } else {
  5652. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  5653. return;
  5654. }
  5655. pipe_config->gmch_pfit.control = tmp;
  5656. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  5657. if (INTEL_INFO(dev)->gen < 5)
  5658. pipe_config->gmch_pfit.lvds_border_bits =
  5659. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  5660. }
  5661. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  5662. struct intel_crtc_state *pipe_config)
  5663. {
  5664. struct drm_device *dev = crtc->base.dev;
  5665. struct drm_i915_private *dev_priv = dev->dev_private;
  5666. int pipe = pipe_config->cpu_transcoder;
  5667. intel_clock_t clock;
  5668. u32 mdiv;
  5669. int refclk = 100000;
  5670. /* In case of MIPI DPLL will not even be used */
  5671. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  5672. return;
  5673. mutex_lock(&dev_priv->dpio_lock);
  5674. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  5675. mutex_unlock(&dev_priv->dpio_lock);
  5676. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  5677. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  5678. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  5679. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  5680. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  5681. vlv_clock(refclk, &clock);
  5682. /* clock.dot is the fast clock */
  5683. pipe_config->port_clock = clock.dot / 5;
  5684. }
  5685. static void
  5686. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  5687. struct intel_initial_plane_config *plane_config)
  5688. {
  5689. struct drm_device *dev = crtc->base.dev;
  5690. struct drm_i915_private *dev_priv = dev->dev_private;
  5691. u32 val, base, offset;
  5692. int pipe = crtc->pipe, plane = crtc->plane;
  5693. int fourcc, pixel_format;
  5694. int aligned_height;
  5695. struct drm_framebuffer *fb;
  5696. struct intel_framebuffer *intel_fb;
  5697. val = I915_READ(DSPCNTR(plane));
  5698. if (!(val & DISPLAY_PLANE_ENABLE))
  5699. return;
  5700. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  5701. if (!intel_fb) {
  5702. DRM_DEBUG_KMS("failed to alloc fb\n");
  5703. return;
  5704. }
  5705. fb = &intel_fb->base;
  5706. if (INTEL_INFO(dev)->gen >= 4) {
  5707. if (val & DISPPLANE_TILED) {
  5708. plane_config->tiling = I915_TILING_X;
  5709. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  5710. }
  5711. }
  5712. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  5713. fourcc = i9xx_format_to_fourcc(pixel_format);
  5714. fb->pixel_format = fourcc;
  5715. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  5716. if (INTEL_INFO(dev)->gen >= 4) {
  5717. if (plane_config->tiling)
  5718. offset = I915_READ(DSPTILEOFF(plane));
  5719. else
  5720. offset = I915_READ(DSPLINOFF(plane));
  5721. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  5722. } else {
  5723. base = I915_READ(DSPADDR(plane));
  5724. }
  5725. plane_config->base = base;
  5726. val = I915_READ(PIPESRC(pipe));
  5727. fb->width = ((val >> 16) & 0xfff) + 1;
  5728. fb->height = ((val >> 0) & 0xfff) + 1;
  5729. val = I915_READ(DSPSTRIDE(pipe));
  5730. fb->pitches[0] = val & 0xffffffc0;
  5731. aligned_height = intel_fb_align_height(dev, fb->height,
  5732. fb->pixel_format,
  5733. fb->modifier[0]);
  5734. plane_config->size = fb->pitches[0] * aligned_height;
  5735. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  5736. pipe_name(pipe), plane, fb->width, fb->height,
  5737. fb->bits_per_pixel, base, fb->pitches[0],
  5738. plane_config->size);
  5739. plane_config->fb = intel_fb;
  5740. }
  5741. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  5742. struct intel_crtc_state *pipe_config)
  5743. {
  5744. struct drm_device *dev = crtc->base.dev;
  5745. struct drm_i915_private *dev_priv = dev->dev_private;
  5746. int pipe = pipe_config->cpu_transcoder;
  5747. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5748. intel_clock_t clock;
  5749. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  5750. int refclk = 100000;
  5751. mutex_lock(&dev_priv->dpio_lock);
  5752. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  5753. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  5754. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  5755. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  5756. mutex_unlock(&dev_priv->dpio_lock);
  5757. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  5758. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  5759. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  5760. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  5761. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  5762. chv_clock(refclk, &clock);
  5763. /* clock.dot is the fast clock */
  5764. pipe_config->port_clock = clock.dot / 5;
  5765. }
  5766. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  5767. struct intel_crtc_state *pipe_config)
  5768. {
  5769. struct drm_device *dev = crtc->base.dev;
  5770. struct drm_i915_private *dev_priv = dev->dev_private;
  5771. uint32_t tmp;
  5772. if (!intel_display_power_is_enabled(dev_priv,
  5773. POWER_DOMAIN_PIPE(crtc->pipe)))
  5774. return false;
  5775. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5776. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5777. tmp = I915_READ(PIPECONF(crtc->pipe));
  5778. if (!(tmp & PIPECONF_ENABLE))
  5779. return false;
  5780. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5781. switch (tmp & PIPECONF_BPC_MASK) {
  5782. case PIPECONF_6BPC:
  5783. pipe_config->pipe_bpp = 18;
  5784. break;
  5785. case PIPECONF_8BPC:
  5786. pipe_config->pipe_bpp = 24;
  5787. break;
  5788. case PIPECONF_10BPC:
  5789. pipe_config->pipe_bpp = 30;
  5790. break;
  5791. default:
  5792. break;
  5793. }
  5794. }
  5795. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  5796. pipe_config->limited_color_range = true;
  5797. if (INTEL_INFO(dev)->gen < 4)
  5798. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  5799. intel_get_pipe_timings(crtc, pipe_config);
  5800. i9xx_get_pfit_config(crtc, pipe_config);
  5801. if (INTEL_INFO(dev)->gen >= 4) {
  5802. tmp = I915_READ(DPLL_MD(crtc->pipe));
  5803. pipe_config->pixel_multiplier =
  5804. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  5805. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  5806. pipe_config->dpll_hw_state.dpll_md = tmp;
  5807. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5808. tmp = I915_READ(DPLL(crtc->pipe));
  5809. pipe_config->pixel_multiplier =
  5810. ((tmp & SDVO_MULTIPLIER_MASK)
  5811. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  5812. } else {
  5813. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  5814. * port and will be fixed up in the encoder->get_config
  5815. * function. */
  5816. pipe_config->pixel_multiplier = 1;
  5817. }
  5818. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  5819. if (!IS_VALLEYVIEW(dev)) {
  5820. /*
  5821. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  5822. * on 830. Filter it out here so that we don't
  5823. * report errors due to that.
  5824. */
  5825. if (IS_I830(dev))
  5826. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  5827. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  5828. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  5829. } else {
  5830. /* Mask out read-only status bits. */
  5831. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  5832. DPLL_PORTC_READY_MASK |
  5833. DPLL_PORTB_READY_MASK);
  5834. }
  5835. if (IS_CHERRYVIEW(dev))
  5836. chv_crtc_clock_get(crtc, pipe_config);
  5837. else if (IS_VALLEYVIEW(dev))
  5838. vlv_crtc_clock_get(crtc, pipe_config);
  5839. else
  5840. i9xx_crtc_clock_get(crtc, pipe_config);
  5841. return true;
  5842. }
  5843. static void ironlake_init_pch_refclk(struct drm_device *dev)
  5844. {
  5845. struct drm_i915_private *dev_priv = dev->dev_private;
  5846. struct intel_encoder *encoder;
  5847. u32 val, final;
  5848. bool has_lvds = false;
  5849. bool has_cpu_edp = false;
  5850. bool has_panel = false;
  5851. bool has_ck505 = false;
  5852. bool can_ssc = false;
  5853. /* We need to take the global config into account */
  5854. for_each_intel_encoder(dev, encoder) {
  5855. switch (encoder->type) {
  5856. case INTEL_OUTPUT_LVDS:
  5857. has_panel = true;
  5858. has_lvds = true;
  5859. break;
  5860. case INTEL_OUTPUT_EDP:
  5861. has_panel = true;
  5862. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  5863. has_cpu_edp = true;
  5864. break;
  5865. default:
  5866. break;
  5867. }
  5868. }
  5869. if (HAS_PCH_IBX(dev)) {
  5870. has_ck505 = dev_priv->vbt.display_clock_mode;
  5871. can_ssc = has_ck505;
  5872. } else {
  5873. has_ck505 = false;
  5874. can_ssc = true;
  5875. }
  5876. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  5877. has_panel, has_lvds, has_ck505);
  5878. /* Ironlake: try to setup display ref clock before DPLL
  5879. * enabling. This is only under driver's control after
  5880. * PCH B stepping, previous chipset stepping should be
  5881. * ignoring this setting.
  5882. */
  5883. val = I915_READ(PCH_DREF_CONTROL);
  5884. /* As we must carefully and slowly disable/enable each source in turn,
  5885. * compute the final state we want first and check if we need to
  5886. * make any changes at all.
  5887. */
  5888. final = val;
  5889. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  5890. if (has_ck505)
  5891. final |= DREF_NONSPREAD_CK505_ENABLE;
  5892. else
  5893. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  5894. final &= ~DREF_SSC_SOURCE_MASK;
  5895. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5896. final &= ~DREF_SSC1_ENABLE;
  5897. if (has_panel) {
  5898. final |= DREF_SSC_SOURCE_ENABLE;
  5899. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5900. final |= DREF_SSC1_ENABLE;
  5901. if (has_cpu_edp) {
  5902. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5903. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5904. else
  5905. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5906. } else
  5907. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5908. } else {
  5909. final |= DREF_SSC_SOURCE_DISABLE;
  5910. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5911. }
  5912. if (final == val)
  5913. return;
  5914. /* Always enable nonspread source */
  5915. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  5916. if (has_ck505)
  5917. val |= DREF_NONSPREAD_CK505_ENABLE;
  5918. else
  5919. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  5920. if (has_panel) {
  5921. val &= ~DREF_SSC_SOURCE_MASK;
  5922. val |= DREF_SSC_SOURCE_ENABLE;
  5923. /* SSC must be turned on before enabling the CPU output */
  5924. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5925. DRM_DEBUG_KMS("Using SSC on panel\n");
  5926. val |= DREF_SSC1_ENABLE;
  5927. } else
  5928. val &= ~DREF_SSC1_ENABLE;
  5929. /* Get SSC going before enabling the outputs */
  5930. I915_WRITE(PCH_DREF_CONTROL, val);
  5931. POSTING_READ(PCH_DREF_CONTROL);
  5932. udelay(200);
  5933. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5934. /* Enable CPU source on CPU attached eDP */
  5935. if (has_cpu_edp) {
  5936. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5937. DRM_DEBUG_KMS("Using SSC on eDP\n");
  5938. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5939. } else
  5940. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5941. } else
  5942. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5943. I915_WRITE(PCH_DREF_CONTROL, val);
  5944. POSTING_READ(PCH_DREF_CONTROL);
  5945. udelay(200);
  5946. } else {
  5947. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  5948. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5949. /* Turn off CPU output */
  5950. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5951. I915_WRITE(PCH_DREF_CONTROL, val);
  5952. POSTING_READ(PCH_DREF_CONTROL);
  5953. udelay(200);
  5954. /* Turn off the SSC source */
  5955. val &= ~DREF_SSC_SOURCE_MASK;
  5956. val |= DREF_SSC_SOURCE_DISABLE;
  5957. /* Turn off SSC1 */
  5958. val &= ~DREF_SSC1_ENABLE;
  5959. I915_WRITE(PCH_DREF_CONTROL, val);
  5960. POSTING_READ(PCH_DREF_CONTROL);
  5961. udelay(200);
  5962. }
  5963. BUG_ON(val != final);
  5964. }
  5965. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  5966. {
  5967. uint32_t tmp;
  5968. tmp = I915_READ(SOUTH_CHICKEN2);
  5969. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  5970. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5971. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  5972. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  5973. DRM_ERROR("FDI mPHY reset assert timeout\n");
  5974. tmp = I915_READ(SOUTH_CHICKEN2);
  5975. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  5976. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5977. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  5978. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  5979. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  5980. }
  5981. /* WaMPhyProgramming:hsw */
  5982. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  5983. {
  5984. uint32_t tmp;
  5985. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  5986. tmp &= ~(0xFF << 24);
  5987. tmp |= (0x12 << 24);
  5988. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  5989. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  5990. tmp |= (1 << 11);
  5991. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  5992. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  5993. tmp |= (1 << 11);
  5994. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  5995. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  5996. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5997. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  5998. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  5999. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6000. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6001. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6002. tmp &= ~(7 << 13);
  6003. tmp |= (5 << 13);
  6004. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6005. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6006. tmp &= ~(7 << 13);
  6007. tmp |= (5 << 13);
  6008. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6009. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6010. tmp &= ~0xFF;
  6011. tmp |= 0x1C;
  6012. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6013. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6014. tmp &= ~0xFF;
  6015. tmp |= 0x1C;
  6016. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  6017. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  6018. tmp &= ~(0xFF << 16);
  6019. tmp |= (0x1C << 16);
  6020. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  6021. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  6022. tmp &= ~(0xFF << 16);
  6023. tmp |= (0x1C << 16);
  6024. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  6025. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  6026. tmp |= (1 << 27);
  6027. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  6028. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  6029. tmp |= (1 << 27);
  6030. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  6031. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  6032. tmp &= ~(0xF << 28);
  6033. tmp |= (4 << 28);
  6034. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  6035. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  6036. tmp &= ~(0xF << 28);
  6037. tmp |= (4 << 28);
  6038. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  6039. }
  6040. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  6041. * Programming" based on the parameters passed:
  6042. * - Sequence to enable CLKOUT_DP
  6043. * - Sequence to enable CLKOUT_DP without spread
  6044. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  6045. */
  6046. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  6047. bool with_fdi)
  6048. {
  6049. struct drm_i915_private *dev_priv = dev->dev_private;
  6050. uint32_t reg, tmp;
  6051. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  6052. with_spread = true;
  6053. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  6054. with_fdi, "LP PCH doesn't have FDI\n"))
  6055. with_fdi = false;
  6056. mutex_lock(&dev_priv->dpio_lock);
  6057. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6058. tmp &= ~SBI_SSCCTL_DISABLE;
  6059. tmp |= SBI_SSCCTL_PATHALT;
  6060. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6061. udelay(24);
  6062. if (with_spread) {
  6063. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6064. tmp &= ~SBI_SSCCTL_PATHALT;
  6065. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6066. if (with_fdi) {
  6067. lpt_reset_fdi_mphy(dev_priv);
  6068. lpt_program_fdi_mphy(dev_priv);
  6069. }
  6070. }
  6071. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  6072. SBI_GEN0 : SBI_DBUFF0;
  6073. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6074. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6075. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6076. mutex_unlock(&dev_priv->dpio_lock);
  6077. }
  6078. /* Sequence to disable CLKOUT_DP */
  6079. static void lpt_disable_clkout_dp(struct drm_device *dev)
  6080. {
  6081. struct drm_i915_private *dev_priv = dev->dev_private;
  6082. uint32_t reg, tmp;
  6083. mutex_lock(&dev_priv->dpio_lock);
  6084. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  6085. SBI_GEN0 : SBI_DBUFF0;
  6086. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6087. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6088. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6089. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6090. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  6091. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  6092. tmp |= SBI_SSCCTL_PATHALT;
  6093. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6094. udelay(32);
  6095. }
  6096. tmp |= SBI_SSCCTL_DISABLE;
  6097. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6098. }
  6099. mutex_unlock(&dev_priv->dpio_lock);
  6100. }
  6101. static void lpt_init_pch_refclk(struct drm_device *dev)
  6102. {
  6103. struct intel_encoder *encoder;
  6104. bool has_vga = false;
  6105. for_each_intel_encoder(dev, encoder) {
  6106. switch (encoder->type) {
  6107. case INTEL_OUTPUT_ANALOG:
  6108. has_vga = true;
  6109. break;
  6110. default:
  6111. break;
  6112. }
  6113. }
  6114. if (has_vga)
  6115. lpt_enable_clkout_dp(dev, true, true);
  6116. else
  6117. lpt_disable_clkout_dp(dev);
  6118. }
  6119. /*
  6120. * Initialize reference clocks when the driver loads
  6121. */
  6122. void intel_init_pch_refclk(struct drm_device *dev)
  6123. {
  6124. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  6125. ironlake_init_pch_refclk(dev);
  6126. else if (HAS_PCH_LPT(dev))
  6127. lpt_init_pch_refclk(dev);
  6128. }
  6129. static int ironlake_get_refclk(struct drm_crtc *crtc)
  6130. {
  6131. struct drm_device *dev = crtc->dev;
  6132. struct drm_i915_private *dev_priv = dev->dev_private;
  6133. struct intel_encoder *encoder;
  6134. int num_connectors = 0;
  6135. bool is_lvds = false;
  6136. for_each_intel_encoder(dev, encoder) {
  6137. if (encoder->new_crtc != to_intel_crtc(crtc))
  6138. continue;
  6139. switch (encoder->type) {
  6140. case INTEL_OUTPUT_LVDS:
  6141. is_lvds = true;
  6142. break;
  6143. default:
  6144. break;
  6145. }
  6146. num_connectors++;
  6147. }
  6148. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  6149. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  6150. dev_priv->vbt.lvds_ssc_freq);
  6151. return dev_priv->vbt.lvds_ssc_freq;
  6152. }
  6153. return 120000;
  6154. }
  6155. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  6156. {
  6157. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  6158. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6159. int pipe = intel_crtc->pipe;
  6160. uint32_t val;
  6161. val = 0;
  6162. switch (intel_crtc->config->pipe_bpp) {
  6163. case 18:
  6164. val |= PIPECONF_6BPC;
  6165. break;
  6166. case 24:
  6167. val |= PIPECONF_8BPC;
  6168. break;
  6169. case 30:
  6170. val |= PIPECONF_10BPC;
  6171. break;
  6172. case 36:
  6173. val |= PIPECONF_12BPC;
  6174. break;
  6175. default:
  6176. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6177. BUG();
  6178. }
  6179. if (intel_crtc->config->dither)
  6180. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6181. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6182. val |= PIPECONF_INTERLACED_ILK;
  6183. else
  6184. val |= PIPECONF_PROGRESSIVE;
  6185. if (intel_crtc->config->limited_color_range)
  6186. val |= PIPECONF_COLOR_RANGE_SELECT;
  6187. I915_WRITE(PIPECONF(pipe), val);
  6188. POSTING_READ(PIPECONF(pipe));
  6189. }
  6190. /*
  6191. * Set up the pipe CSC unit.
  6192. *
  6193. * Currently only full range RGB to limited range RGB conversion
  6194. * is supported, but eventually this should handle various
  6195. * RGB<->YCbCr scenarios as well.
  6196. */
  6197. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  6198. {
  6199. struct drm_device *dev = crtc->dev;
  6200. struct drm_i915_private *dev_priv = dev->dev_private;
  6201. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6202. int pipe = intel_crtc->pipe;
  6203. uint16_t coeff = 0x7800; /* 1.0 */
  6204. /*
  6205. * TODO: Check what kind of values actually come out of the pipe
  6206. * with these coeff/postoff values and adjust to get the best
  6207. * accuracy. Perhaps we even need to take the bpc value into
  6208. * consideration.
  6209. */
  6210. if (intel_crtc->config->limited_color_range)
  6211. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  6212. /*
  6213. * GY/GU and RY/RU should be the other way around according
  6214. * to BSpec, but reality doesn't agree. Just set them up in
  6215. * a way that results in the correct picture.
  6216. */
  6217. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  6218. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  6219. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  6220. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  6221. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  6222. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  6223. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  6224. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  6225. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  6226. if (INTEL_INFO(dev)->gen > 6) {
  6227. uint16_t postoff = 0;
  6228. if (intel_crtc->config->limited_color_range)
  6229. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  6230. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  6231. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  6232. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  6233. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  6234. } else {
  6235. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  6236. if (intel_crtc->config->limited_color_range)
  6237. mode |= CSC_BLACK_SCREEN_OFFSET;
  6238. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  6239. }
  6240. }
  6241. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6242. {
  6243. struct drm_device *dev = crtc->dev;
  6244. struct drm_i915_private *dev_priv = dev->dev_private;
  6245. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6246. enum pipe pipe = intel_crtc->pipe;
  6247. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6248. uint32_t val;
  6249. val = 0;
  6250. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  6251. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6252. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6253. val |= PIPECONF_INTERLACED_ILK;
  6254. else
  6255. val |= PIPECONF_PROGRESSIVE;
  6256. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6257. POSTING_READ(PIPECONF(cpu_transcoder));
  6258. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  6259. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  6260. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  6261. val = 0;
  6262. switch (intel_crtc->config->pipe_bpp) {
  6263. case 18:
  6264. val |= PIPEMISC_DITHER_6_BPC;
  6265. break;
  6266. case 24:
  6267. val |= PIPEMISC_DITHER_8_BPC;
  6268. break;
  6269. case 30:
  6270. val |= PIPEMISC_DITHER_10_BPC;
  6271. break;
  6272. case 36:
  6273. val |= PIPEMISC_DITHER_12_BPC;
  6274. break;
  6275. default:
  6276. /* Case prevented by pipe_config_set_bpp. */
  6277. BUG();
  6278. }
  6279. if (intel_crtc->config->dither)
  6280. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  6281. I915_WRITE(PIPEMISC(pipe), val);
  6282. }
  6283. }
  6284. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  6285. struct intel_crtc_state *crtc_state,
  6286. intel_clock_t *clock,
  6287. bool *has_reduced_clock,
  6288. intel_clock_t *reduced_clock)
  6289. {
  6290. struct drm_device *dev = crtc->dev;
  6291. struct drm_i915_private *dev_priv = dev->dev_private;
  6292. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6293. int refclk;
  6294. const intel_limit_t *limit;
  6295. bool ret, is_lvds = false;
  6296. is_lvds = intel_pipe_will_have_type(intel_crtc, INTEL_OUTPUT_LVDS);
  6297. refclk = ironlake_get_refclk(crtc);
  6298. /*
  6299. * Returns a set of divisors for the desired target clock with the given
  6300. * refclk, or FALSE. The returned values represent the clock equation:
  6301. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  6302. */
  6303. limit = intel_limit(intel_crtc, refclk);
  6304. ret = dev_priv->display.find_dpll(limit, intel_crtc,
  6305. crtc_state->port_clock,
  6306. refclk, NULL, clock);
  6307. if (!ret)
  6308. return false;
  6309. if (is_lvds && dev_priv->lvds_downclock_avail) {
  6310. /*
  6311. * Ensure we match the reduced clock's P to the target clock.
  6312. * If the clocks don't match, we can't switch the display clock
  6313. * by using the FP0/FP1. In such case we will disable the LVDS
  6314. * downclock feature.
  6315. */
  6316. *has_reduced_clock =
  6317. dev_priv->display.find_dpll(limit, intel_crtc,
  6318. dev_priv->lvds_downclock,
  6319. refclk, clock,
  6320. reduced_clock);
  6321. }
  6322. return true;
  6323. }
  6324. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  6325. {
  6326. /*
  6327. * Account for spread spectrum to avoid
  6328. * oversubscribing the link. Max center spread
  6329. * is 2.5%; use 5% for safety's sake.
  6330. */
  6331. u32 bps = target_clock * bpp * 21 / 20;
  6332. return DIV_ROUND_UP(bps, link_bw * 8);
  6333. }
  6334. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  6335. {
  6336. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  6337. }
  6338. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  6339. struct intel_crtc_state *crtc_state,
  6340. u32 *fp,
  6341. intel_clock_t *reduced_clock, u32 *fp2)
  6342. {
  6343. struct drm_crtc *crtc = &intel_crtc->base;
  6344. struct drm_device *dev = crtc->dev;
  6345. struct drm_i915_private *dev_priv = dev->dev_private;
  6346. struct intel_encoder *intel_encoder;
  6347. uint32_t dpll;
  6348. int factor, num_connectors = 0;
  6349. bool is_lvds = false, is_sdvo = false;
  6350. for_each_intel_encoder(dev, intel_encoder) {
  6351. if (intel_encoder->new_crtc != to_intel_crtc(crtc))
  6352. continue;
  6353. switch (intel_encoder->type) {
  6354. case INTEL_OUTPUT_LVDS:
  6355. is_lvds = true;
  6356. break;
  6357. case INTEL_OUTPUT_SDVO:
  6358. case INTEL_OUTPUT_HDMI:
  6359. is_sdvo = true;
  6360. break;
  6361. default:
  6362. break;
  6363. }
  6364. num_connectors++;
  6365. }
  6366. /* Enable autotuning of the PLL clock (if permissible) */
  6367. factor = 21;
  6368. if (is_lvds) {
  6369. if ((intel_panel_use_ssc(dev_priv) &&
  6370. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6371. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  6372. factor = 25;
  6373. } else if (crtc_state->sdvo_tv_clock)
  6374. factor = 20;
  6375. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  6376. *fp |= FP_CB_TUNE;
  6377. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  6378. *fp2 |= FP_CB_TUNE;
  6379. dpll = 0;
  6380. if (is_lvds)
  6381. dpll |= DPLLB_MODE_LVDS;
  6382. else
  6383. dpll |= DPLLB_MODE_DAC_SERIAL;
  6384. dpll |= (crtc_state->pixel_multiplier - 1)
  6385. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6386. if (is_sdvo)
  6387. dpll |= DPLL_SDVO_HIGH_SPEED;
  6388. if (crtc_state->has_dp_encoder)
  6389. dpll |= DPLL_SDVO_HIGH_SPEED;
  6390. /* compute bitmask from p1 value */
  6391. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6392. /* also FPA1 */
  6393. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6394. switch (crtc_state->dpll.p2) {
  6395. case 5:
  6396. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6397. break;
  6398. case 7:
  6399. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6400. break;
  6401. case 10:
  6402. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6403. break;
  6404. case 14:
  6405. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6406. break;
  6407. }
  6408. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6409. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6410. else
  6411. dpll |= PLL_REF_INPUT_DREFCLK;
  6412. return dpll | DPLL_VCO_ENABLE;
  6413. }
  6414. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  6415. struct intel_crtc_state *crtc_state)
  6416. {
  6417. struct drm_device *dev = crtc->base.dev;
  6418. intel_clock_t clock, reduced_clock;
  6419. u32 dpll = 0, fp = 0, fp2 = 0;
  6420. bool ok, has_reduced_clock = false;
  6421. bool is_lvds = false;
  6422. struct intel_shared_dpll *pll;
  6423. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  6424. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  6425. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  6426. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  6427. &has_reduced_clock, &reduced_clock);
  6428. if (!ok && !crtc_state->clock_set) {
  6429. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6430. return -EINVAL;
  6431. }
  6432. /* Compat-code for transition, will disappear. */
  6433. if (!crtc_state->clock_set) {
  6434. crtc_state->dpll.n = clock.n;
  6435. crtc_state->dpll.m1 = clock.m1;
  6436. crtc_state->dpll.m2 = clock.m2;
  6437. crtc_state->dpll.p1 = clock.p1;
  6438. crtc_state->dpll.p2 = clock.p2;
  6439. }
  6440. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  6441. if (crtc_state->has_pch_encoder) {
  6442. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6443. if (has_reduced_clock)
  6444. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  6445. dpll = ironlake_compute_dpll(crtc, crtc_state,
  6446. &fp, &reduced_clock,
  6447. has_reduced_clock ? &fp2 : NULL);
  6448. crtc_state->dpll_hw_state.dpll = dpll;
  6449. crtc_state->dpll_hw_state.fp0 = fp;
  6450. if (has_reduced_clock)
  6451. crtc_state->dpll_hw_state.fp1 = fp2;
  6452. else
  6453. crtc_state->dpll_hw_state.fp1 = fp;
  6454. pll = intel_get_shared_dpll(crtc, crtc_state);
  6455. if (pll == NULL) {
  6456. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  6457. pipe_name(crtc->pipe));
  6458. return -EINVAL;
  6459. }
  6460. }
  6461. if (is_lvds && has_reduced_clock && i915.powersave)
  6462. crtc->lowfreq_avail = true;
  6463. else
  6464. crtc->lowfreq_avail = false;
  6465. return 0;
  6466. }
  6467. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  6468. struct intel_link_m_n *m_n)
  6469. {
  6470. struct drm_device *dev = crtc->base.dev;
  6471. struct drm_i915_private *dev_priv = dev->dev_private;
  6472. enum pipe pipe = crtc->pipe;
  6473. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  6474. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  6475. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  6476. & ~TU_SIZE_MASK;
  6477. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  6478. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  6479. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6480. }
  6481. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  6482. enum transcoder transcoder,
  6483. struct intel_link_m_n *m_n,
  6484. struct intel_link_m_n *m2_n2)
  6485. {
  6486. struct drm_device *dev = crtc->base.dev;
  6487. struct drm_i915_private *dev_priv = dev->dev_private;
  6488. enum pipe pipe = crtc->pipe;
  6489. if (INTEL_INFO(dev)->gen >= 5) {
  6490. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  6491. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  6492. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  6493. & ~TU_SIZE_MASK;
  6494. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  6495. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  6496. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6497. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  6498. * gen < 8) and if DRRS is supported (to make sure the
  6499. * registers are not unnecessarily read).
  6500. */
  6501. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  6502. crtc->config->has_drrs) {
  6503. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  6504. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  6505. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  6506. & ~TU_SIZE_MASK;
  6507. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  6508. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  6509. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6510. }
  6511. } else {
  6512. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  6513. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  6514. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  6515. & ~TU_SIZE_MASK;
  6516. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  6517. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  6518. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6519. }
  6520. }
  6521. void intel_dp_get_m_n(struct intel_crtc *crtc,
  6522. struct intel_crtc_state *pipe_config)
  6523. {
  6524. if (pipe_config->has_pch_encoder)
  6525. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  6526. else
  6527. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6528. &pipe_config->dp_m_n,
  6529. &pipe_config->dp_m2_n2);
  6530. }
  6531. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  6532. struct intel_crtc_state *pipe_config)
  6533. {
  6534. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6535. &pipe_config->fdi_m_n, NULL);
  6536. }
  6537. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  6538. struct intel_crtc_state *pipe_config)
  6539. {
  6540. struct drm_device *dev = crtc->base.dev;
  6541. struct drm_i915_private *dev_priv = dev->dev_private;
  6542. uint32_t tmp;
  6543. tmp = I915_READ(PS_CTL(crtc->pipe));
  6544. if (tmp & PS_ENABLE) {
  6545. pipe_config->pch_pfit.enabled = true;
  6546. pipe_config->pch_pfit.pos = I915_READ(PS_WIN_POS(crtc->pipe));
  6547. pipe_config->pch_pfit.size = I915_READ(PS_WIN_SZ(crtc->pipe));
  6548. }
  6549. }
  6550. static void
  6551. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  6552. struct intel_initial_plane_config *plane_config)
  6553. {
  6554. struct drm_device *dev = crtc->base.dev;
  6555. struct drm_i915_private *dev_priv = dev->dev_private;
  6556. u32 val, base, offset, stride_mult, tiling;
  6557. int pipe = crtc->pipe;
  6558. int fourcc, pixel_format;
  6559. int aligned_height;
  6560. struct drm_framebuffer *fb;
  6561. struct intel_framebuffer *intel_fb;
  6562. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6563. if (!intel_fb) {
  6564. DRM_DEBUG_KMS("failed to alloc fb\n");
  6565. return;
  6566. }
  6567. fb = &intel_fb->base;
  6568. val = I915_READ(PLANE_CTL(pipe, 0));
  6569. if (!(val & PLANE_CTL_ENABLE))
  6570. goto error;
  6571. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  6572. fourcc = skl_format_to_fourcc(pixel_format,
  6573. val & PLANE_CTL_ORDER_RGBX,
  6574. val & PLANE_CTL_ALPHA_MASK);
  6575. fb->pixel_format = fourcc;
  6576. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6577. tiling = val & PLANE_CTL_TILED_MASK;
  6578. switch (tiling) {
  6579. case PLANE_CTL_TILED_LINEAR:
  6580. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  6581. break;
  6582. case PLANE_CTL_TILED_X:
  6583. plane_config->tiling = I915_TILING_X;
  6584. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6585. break;
  6586. case PLANE_CTL_TILED_Y:
  6587. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  6588. break;
  6589. case PLANE_CTL_TILED_YF:
  6590. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  6591. break;
  6592. default:
  6593. MISSING_CASE(tiling);
  6594. goto error;
  6595. }
  6596. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  6597. plane_config->base = base;
  6598. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  6599. val = I915_READ(PLANE_SIZE(pipe, 0));
  6600. fb->height = ((val >> 16) & 0xfff) + 1;
  6601. fb->width = ((val >> 0) & 0x1fff) + 1;
  6602. val = I915_READ(PLANE_STRIDE(pipe, 0));
  6603. stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
  6604. fb->pixel_format);
  6605. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  6606. aligned_height = intel_fb_align_height(dev, fb->height,
  6607. fb->pixel_format,
  6608. fb->modifier[0]);
  6609. plane_config->size = fb->pitches[0] * aligned_height;
  6610. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6611. pipe_name(pipe), fb->width, fb->height,
  6612. fb->bits_per_pixel, base, fb->pitches[0],
  6613. plane_config->size);
  6614. plane_config->fb = intel_fb;
  6615. return;
  6616. error:
  6617. kfree(fb);
  6618. }
  6619. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  6620. struct intel_crtc_state *pipe_config)
  6621. {
  6622. struct drm_device *dev = crtc->base.dev;
  6623. struct drm_i915_private *dev_priv = dev->dev_private;
  6624. uint32_t tmp;
  6625. tmp = I915_READ(PF_CTL(crtc->pipe));
  6626. if (tmp & PF_ENABLE) {
  6627. pipe_config->pch_pfit.enabled = true;
  6628. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  6629. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  6630. /* We currently do not free assignements of panel fitters on
  6631. * ivb/hsw (since we don't use the higher upscaling modes which
  6632. * differentiates them) so just WARN about this case for now. */
  6633. if (IS_GEN7(dev)) {
  6634. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  6635. PF_PIPE_SEL_IVB(crtc->pipe));
  6636. }
  6637. }
  6638. }
  6639. static void
  6640. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  6641. struct intel_initial_plane_config *plane_config)
  6642. {
  6643. struct drm_device *dev = crtc->base.dev;
  6644. struct drm_i915_private *dev_priv = dev->dev_private;
  6645. u32 val, base, offset;
  6646. int pipe = crtc->pipe;
  6647. int fourcc, pixel_format;
  6648. int aligned_height;
  6649. struct drm_framebuffer *fb;
  6650. struct intel_framebuffer *intel_fb;
  6651. val = I915_READ(DSPCNTR(pipe));
  6652. if (!(val & DISPLAY_PLANE_ENABLE))
  6653. return;
  6654. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6655. if (!intel_fb) {
  6656. DRM_DEBUG_KMS("failed to alloc fb\n");
  6657. return;
  6658. }
  6659. fb = &intel_fb->base;
  6660. if (INTEL_INFO(dev)->gen >= 4) {
  6661. if (val & DISPPLANE_TILED) {
  6662. plane_config->tiling = I915_TILING_X;
  6663. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6664. }
  6665. }
  6666. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6667. fourcc = i9xx_format_to_fourcc(pixel_format);
  6668. fb->pixel_format = fourcc;
  6669. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6670. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  6671. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  6672. offset = I915_READ(DSPOFFSET(pipe));
  6673. } else {
  6674. if (plane_config->tiling)
  6675. offset = I915_READ(DSPTILEOFF(pipe));
  6676. else
  6677. offset = I915_READ(DSPLINOFF(pipe));
  6678. }
  6679. plane_config->base = base;
  6680. val = I915_READ(PIPESRC(pipe));
  6681. fb->width = ((val >> 16) & 0xfff) + 1;
  6682. fb->height = ((val >> 0) & 0xfff) + 1;
  6683. val = I915_READ(DSPSTRIDE(pipe));
  6684. fb->pitches[0] = val & 0xffffffc0;
  6685. aligned_height = intel_fb_align_height(dev, fb->height,
  6686. fb->pixel_format,
  6687. fb->modifier[0]);
  6688. plane_config->size = fb->pitches[0] * aligned_height;
  6689. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6690. pipe_name(pipe), fb->width, fb->height,
  6691. fb->bits_per_pixel, base, fb->pitches[0],
  6692. plane_config->size);
  6693. plane_config->fb = intel_fb;
  6694. }
  6695. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  6696. struct intel_crtc_state *pipe_config)
  6697. {
  6698. struct drm_device *dev = crtc->base.dev;
  6699. struct drm_i915_private *dev_priv = dev->dev_private;
  6700. uint32_t tmp;
  6701. if (!intel_display_power_is_enabled(dev_priv,
  6702. POWER_DOMAIN_PIPE(crtc->pipe)))
  6703. return false;
  6704. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6705. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6706. tmp = I915_READ(PIPECONF(crtc->pipe));
  6707. if (!(tmp & PIPECONF_ENABLE))
  6708. return false;
  6709. switch (tmp & PIPECONF_BPC_MASK) {
  6710. case PIPECONF_6BPC:
  6711. pipe_config->pipe_bpp = 18;
  6712. break;
  6713. case PIPECONF_8BPC:
  6714. pipe_config->pipe_bpp = 24;
  6715. break;
  6716. case PIPECONF_10BPC:
  6717. pipe_config->pipe_bpp = 30;
  6718. break;
  6719. case PIPECONF_12BPC:
  6720. pipe_config->pipe_bpp = 36;
  6721. break;
  6722. default:
  6723. break;
  6724. }
  6725. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  6726. pipe_config->limited_color_range = true;
  6727. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  6728. struct intel_shared_dpll *pll;
  6729. pipe_config->has_pch_encoder = true;
  6730. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  6731. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6732. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6733. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6734. if (HAS_PCH_IBX(dev_priv->dev)) {
  6735. pipe_config->shared_dpll =
  6736. (enum intel_dpll_id) crtc->pipe;
  6737. } else {
  6738. tmp = I915_READ(PCH_DPLL_SEL);
  6739. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  6740. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  6741. else
  6742. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  6743. }
  6744. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6745. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6746. &pipe_config->dpll_hw_state));
  6747. tmp = pipe_config->dpll_hw_state.dpll;
  6748. pipe_config->pixel_multiplier =
  6749. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  6750. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  6751. ironlake_pch_clock_get(crtc, pipe_config);
  6752. } else {
  6753. pipe_config->pixel_multiplier = 1;
  6754. }
  6755. intel_get_pipe_timings(crtc, pipe_config);
  6756. ironlake_get_pfit_config(crtc, pipe_config);
  6757. return true;
  6758. }
  6759. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  6760. {
  6761. struct drm_device *dev = dev_priv->dev;
  6762. struct intel_crtc *crtc;
  6763. for_each_intel_crtc(dev, crtc)
  6764. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  6765. pipe_name(crtc->pipe));
  6766. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  6767. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  6768. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  6769. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  6770. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  6771. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  6772. "CPU PWM1 enabled\n");
  6773. if (IS_HASWELL(dev))
  6774. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  6775. "CPU PWM2 enabled\n");
  6776. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  6777. "PCH PWM1 enabled\n");
  6778. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  6779. "Utility pin enabled\n");
  6780. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  6781. /*
  6782. * In theory we can still leave IRQs enabled, as long as only the HPD
  6783. * interrupts remain enabled. We used to check for that, but since it's
  6784. * gen-specific and since we only disable LCPLL after we fully disable
  6785. * the interrupts, the check below should be enough.
  6786. */
  6787. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  6788. }
  6789. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  6790. {
  6791. struct drm_device *dev = dev_priv->dev;
  6792. if (IS_HASWELL(dev))
  6793. return I915_READ(D_COMP_HSW);
  6794. else
  6795. return I915_READ(D_COMP_BDW);
  6796. }
  6797. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  6798. {
  6799. struct drm_device *dev = dev_priv->dev;
  6800. if (IS_HASWELL(dev)) {
  6801. mutex_lock(&dev_priv->rps.hw_lock);
  6802. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  6803. val))
  6804. DRM_ERROR("Failed to write to D_COMP\n");
  6805. mutex_unlock(&dev_priv->rps.hw_lock);
  6806. } else {
  6807. I915_WRITE(D_COMP_BDW, val);
  6808. POSTING_READ(D_COMP_BDW);
  6809. }
  6810. }
  6811. /*
  6812. * This function implements pieces of two sequences from BSpec:
  6813. * - Sequence for display software to disable LCPLL
  6814. * - Sequence for display software to allow package C8+
  6815. * The steps implemented here are just the steps that actually touch the LCPLL
  6816. * register. Callers should take care of disabling all the display engine
  6817. * functions, doing the mode unset, fixing interrupts, etc.
  6818. */
  6819. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  6820. bool switch_to_fclk, bool allow_power_down)
  6821. {
  6822. uint32_t val;
  6823. assert_can_disable_lcpll(dev_priv);
  6824. val = I915_READ(LCPLL_CTL);
  6825. if (switch_to_fclk) {
  6826. val |= LCPLL_CD_SOURCE_FCLK;
  6827. I915_WRITE(LCPLL_CTL, val);
  6828. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  6829. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  6830. DRM_ERROR("Switching to FCLK failed\n");
  6831. val = I915_READ(LCPLL_CTL);
  6832. }
  6833. val |= LCPLL_PLL_DISABLE;
  6834. I915_WRITE(LCPLL_CTL, val);
  6835. POSTING_READ(LCPLL_CTL);
  6836. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  6837. DRM_ERROR("LCPLL still locked\n");
  6838. val = hsw_read_dcomp(dev_priv);
  6839. val |= D_COMP_COMP_DISABLE;
  6840. hsw_write_dcomp(dev_priv, val);
  6841. ndelay(100);
  6842. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  6843. 1))
  6844. DRM_ERROR("D_COMP RCOMP still in progress\n");
  6845. if (allow_power_down) {
  6846. val = I915_READ(LCPLL_CTL);
  6847. val |= LCPLL_POWER_DOWN_ALLOW;
  6848. I915_WRITE(LCPLL_CTL, val);
  6849. POSTING_READ(LCPLL_CTL);
  6850. }
  6851. }
  6852. /*
  6853. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  6854. * source.
  6855. */
  6856. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  6857. {
  6858. uint32_t val;
  6859. val = I915_READ(LCPLL_CTL);
  6860. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  6861. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  6862. return;
  6863. /*
  6864. * Make sure we're not on PC8 state before disabling PC8, otherwise
  6865. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  6866. */
  6867. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  6868. if (val & LCPLL_POWER_DOWN_ALLOW) {
  6869. val &= ~LCPLL_POWER_DOWN_ALLOW;
  6870. I915_WRITE(LCPLL_CTL, val);
  6871. POSTING_READ(LCPLL_CTL);
  6872. }
  6873. val = hsw_read_dcomp(dev_priv);
  6874. val |= D_COMP_COMP_FORCE;
  6875. val &= ~D_COMP_COMP_DISABLE;
  6876. hsw_write_dcomp(dev_priv, val);
  6877. val = I915_READ(LCPLL_CTL);
  6878. val &= ~LCPLL_PLL_DISABLE;
  6879. I915_WRITE(LCPLL_CTL, val);
  6880. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  6881. DRM_ERROR("LCPLL not locked yet\n");
  6882. if (val & LCPLL_CD_SOURCE_FCLK) {
  6883. val = I915_READ(LCPLL_CTL);
  6884. val &= ~LCPLL_CD_SOURCE_FCLK;
  6885. I915_WRITE(LCPLL_CTL, val);
  6886. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  6887. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  6888. DRM_ERROR("Switching back to LCPLL failed\n");
  6889. }
  6890. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  6891. }
  6892. /*
  6893. * Package states C8 and deeper are really deep PC states that can only be
  6894. * reached when all the devices on the system allow it, so even if the graphics
  6895. * device allows PC8+, it doesn't mean the system will actually get to these
  6896. * states. Our driver only allows PC8+ when going into runtime PM.
  6897. *
  6898. * The requirements for PC8+ are that all the outputs are disabled, the power
  6899. * well is disabled and most interrupts are disabled, and these are also
  6900. * requirements for runtime PM. When these conditions are met, we manually do
  6901. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  6902. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  6903. * hang the machine.
  6904. *
  6905. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  6906. * the state of some registers, so when we come back from PC8+ we need to
  6907. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  6908. * need to take care of the registers kept by RC6. Notice that this happens even
  6909. * if we don't put the device in PCI D3 state (which is what currently happens
  6910. * because of the runtime PM support).
  6911. *
  6912. * For more, read "Display Sequences for Package C8" on the hardware
  6913. * documentation.
  6914. */
  6915. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  6916. {
  6917. struct drm_device *dev = dev_priv->dev;
  6918. uint32_t val;
  6919. DRM_DEBUG_KMS("Enabling package C8+\n");
  6920. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6921. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6922. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  6923. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6924. }
  6925. lpt_disable_clkout_dp(dev);
  6926. hsw_disable_lcpll(dev_priv, true, true);
  6927. }
  6928. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  6929. {
  6930. struct drm_device *dev = dev_priv->dev;
  6931. uint32_t val;
  6932. DRM_DEBUG_KMS("Disabling package C8+\n");
  6933. hsw_restore_lcpll(dev_priv);
  6934. lpt_init_pch_refclk(dev);
  6935. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6936. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6937. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  6938. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6939. }
  6940. intel_prepare_ddi(dev);
  6941. }
  6942. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  6943. struct intel_crtc_state *crtc_state)
  6944. {
  6945. if (!intel_ddi_pll_select(crtc, crtc_state))
  6946. return -EINVAL;
  6947. crtc->lowfreq_avail = false;
  6948. return 0;
  6949. }
  6950. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  6951. enum port port,
  6952. struct intel_crtc_state *pipe_config)
  6953. {
  6954. u32 temp, dpll_ctl1;
  6955. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  6956. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  6957. switch (pipe_config->ddi_pll_sel) {
  6958. case SKL_DPLL0:
  6959. /*
  6960. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  6961. * of the shared DPLL framework and thus needs to be read out
  6962. * separately
  6963. */
  6964. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  6965. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  6966. break;
  6967. case SKL_DPLL1:
  6968. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  6969. break;
  6970. case SKL_DPLL2:
  6971. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  6972. break;
  6973. case SKL_DPLL3:
  6974. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  6975. break;
  6976. }
  6977. }
  6978. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  6979. enum port port,
  6980. struct intel_crtc_state *pipe_config)
  6981. {
  6982. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  6983. switch (pipe_config->ddi_pll_sel) {
  6984. case PORT_CLK_SEL_WRPLL1:
  6985. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  6986. break;
  6987. case PORT_CLK_SEL_WRPLL2:
  6988. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  6989. break;
  6990. }
  6991. }
  6992. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  6993. struct intel_crtc_state *pipe_config)
  6994. {
  6995. struct drm_device *dev = crtc->base.dev;
  6996. struct drm_i915_private *dev_priv = dev->dev_private;
  6997. struct intel_shared_dpll *pll;
  6998. enum port port;
  6999. uint32_t tmp;
  7000. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  7001. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  7002. if (IS_SKYLAKE(dev))
  7003. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  7004. else
  7005. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  7006. if (pipe_config->shared_dpll >= 0) {
  7007. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7008. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7009. &pipe_config->dpll_hw_state));
  7010. }
  7011. /*
  7012. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  7013. * DDI E. So just check whether this pipe is wired to DDI E and whether
  7014. * the PCH transcoder is on.
  7015. */
  7016. if (INTEL_INFO(dev)->gen < 9 &&
  7017. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  7018. pipe_config->has_pch_encoder = true;
  7019. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  7020. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7021. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7022. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7023. }
  7024. }
  7025. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  7026. struct intel_crtc_state *pipe_config)
  7027. {
  7028. struct drm_device *dev = crtc->base.dev;
  7029. struct drm_i915_private *dev_priv = dev->dev_private;
  7030. enum intel_display_power_domain pfit_domain;
  7031. uint32_t tmp;
  7032. if (!intel_display_power_is_enabled(dev_priv,
  7033. POWER_DOMAIN_PIPE(crtc->pipe)))
  7034. return false;
  7035. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7036. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7037. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  7038. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  7039. enum pipe trans_edp_pipe;
  7040. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  7041. default:
  7042. WARN(1, "unknown pipe linked to edp transcoder\n");
  7043. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7044. case TRANS_DDI_EDP_INPUT_A_ON:
  7045. trans_edp_pipe = PIPE_A;
  7046. break;
  7047. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  7048. trans_edp_pipe = PIPE_B;
  7049. break;
  7050. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  7051. trans_edp_pipe = PIPE_C;
  7052. break;
  7053. }
  7054. if (trans_edp_pipe == crtc->pipe)
  7055. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  7056. }
  7057. if (!intel_display_power_is_enabled(dev_priv,
  7058. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  7059. return false;
  7060. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  7061. if (!(tmp & PIPECONF_ENABLE))
  7062. return false;
  7063. haswell_get_ddi_port_state(crtc, pipe_config);
  7064. intel_get_pipe_timings(crtc, pipe_config);
  7065. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  7066. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  7067. if (IS_SKYLAKE(dev))
  7068. skylake_get_pfit_config(crtc, pipe_config);
  7069. else
  7070. ironlake_get_pfit_config(crtc, pipe_config);
  7071. }
  7072. if (IS_HASWELL(dev))
  7073. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  7074. (I915_READ(IPS_CTL) & IPS_ENABLE);
  7075. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  7076. pipe_config->pixel_multiplier =
  7077. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  7078. } else {
  7079. pipe_config->pixel_multiplier = 1;
  7080. }
  7081. return true;
  7082. }
  7083. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  7084. {
  7085. struct drm_device *dev = crtc->dev;
  7086. struct drm_i915_private *dev_priv = dev->dev_private;
  7087. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7088. uint32_t cntl = 0, size = 0;
  7089. if (base) {
  7090. unsigned int width = intel_crtc->base.cursor->state->crtc_w;
  7091. unsigned int height = intel_crtc->base.cursor->state->crtc_h;
  7092. unsigned int stride = roundup_pow_of_two(width) * 4;
  7093. switch (stride) {
  7094. default:
  7095. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  7096. width, stride);
  7097. stride = 256;
  7098. /* fallthrough */
  7099. case 256:
  7100. case 512:
  7101. case 1024:
  7102. case 2048:
  7103. break;
  7104. }
  7105. cntl |= CURSOR_ENABLE |
  7106. CURSOR_GAMMA_ENABLE |
  7107. CURSOR_FORMAT_ARGB |
  7108. CURSOR_STRIDE(stride);
  7109. size = (height << 12) | width;
  7110. }
  7111. if (intel_crtc->cursor_cntl != 0 &&
  7112. (intel_crtc->cursor_base != base ||
  7113. intel_crtc->cursor_size != size ||
  7114. intel_crtc->cursor_cntl != cntl)) {
  7115. /* On these chipsets we can only modify the base/size/stride
  7116. * whilst the cursor is disabled.
  7117. */
  7118. I915_WRITE(_CURACNTR, 0);
  7119. POSTING_READ(_CURACNTR);
  7120. intel_crtc->cursor_cntl = 0;
  7121. }
  7122. if (intel_crtc->cursor_base != base) {
  7123. I915_WRITE(_CURABASE, base);
  7124. intel_crtc->cursor_base = base;
  7125. }
  7126. if (intel_crtc->cursor_size != size) {
  7127. I915_WRITE(CURSIZE, size);
  7128. intel_crtc->cursor_size = size;
  7129. }
  7130. if (intel_crtc->cursor_cntl != cntl) {
  7131. I915_WRITE(_CURACNTR, cntl);
  7132. POSTING_READ(_CURACNTR);
  7133. intel_crtc->cursor_cntl = cntl;
  7134. }
  7135. }
  7136. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  7137. {
  7138. struct drm_device *dev = crtc->dev;
  7139. struct drm_i915_private *dev_priv = dev->dev_private;
  7140. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7141. int pipe = intel_crtc->pipe;
  7142. uint32_t cntl;
  7143. cntl = 0;
  7144. if (base) {
  7145. cntl = MCURSOR_GAMMA_ENABLE;
  7146. switch (intel_crtc->base.cursor->state->crtc_w) {
  7147. case 64:
  7148. cntl |= CURSOR_MODE_64_ARGB_AX;
  7149. break;
  7150. case 128:
  7151. cntl |= CURSOR_MODE_128_ARGB_AX;
  7152. break;
  7153. case 256:
  7154. cntl |= CURSOR_MODE_256_ARGB_AX;
  7155. break;
  7156. default:
  7157. MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
  7158. return;
  7159. }
  7160. cntl |= pipe << 28; /* Connect to correct pipe */
  7161. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  7162. cntl |= CURSOR_PIPE_CSC_ENABLE;
  7163. }
  7164. if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
  7165. cntl |= CURSOR_ROTATE_180;
  7166. if (intel_crtc->cursor_cntl != cntl) {
  7167. I915_WRITE(CURCNTR(pipe), cntl);
  7168. POSTING_READ(CURCNTR(pipe));
  7169. intel_crtc->cursor_cntl = cntl;
  7170. }
  7171. /* and commit changes on next vblank */
  7172. I915_WRITE(CURBASE(pipe), base);
  7173. POSTING_READ(CURBASE(pipe));
  7174. intel_crtc->cursor_base = base;
  7175. }
  7176. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  7177. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  7178. bool on)
  7179. {
  7180. struct drm_device *dev = crtc->dev;
  7181. struct drm_i915_private *dev_priv = dev->dev_private;
  7182. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7183. int pipe = intel_crtc->pipe;
  7184. int x = crtc->cursor_x;
  7185. int y = crtc->cursor_y;
  7186. u32 base = 0, pos = 0;
  7187. if (on)
  7188. base = intel_crtc->cursor_addr;
  7189. if (x >= intel_crtc->config->pipe_src_w)
  7190. base = 0;
  7191. if (y >= intel_crtc->config->pipe_src_h)
  7192. base = 0;
  7193. if (x < 0) {
  7194. if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
  7195. base = 0;
  7196. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  7197. x = -x;
  7198. }
  7199. pos |= x << CURSOR_X_SHIFT;
  7200. if (y < 0) {
  7201. if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
  7202. base = 0;
  7203. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  7204. y = -y;
  7205. }
  7206. pos |= y << CURSOR_Y_SHIFT;
  7207. if (base == 0 && intel_crtc->cursor_base == 0)
  7208. return;
  7209. I915_WRITE(CURPOS(pipe), pos);
  7210. /* ILK+ do this automagically */
  7211. if (HAS_GMCH_DISPLAY(dev) &&
  7212. crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
  7213. base += (intel_crtc->base.cursor->state->crtc_h *
  7214. intel_crtc->base.cursor->state->crtc_w - 1) * 4;
  7215. }
  7216. if (IS_845G(dev) || IS_I865G(dev))
  7217. i845_update_cursor(crtc, base);
  7218. else
  7219. i9xx_update_cursor(crtc, base);
  7220. }
  7221. static bool cursor_size_ok(struct drm_device *dev,
  7222. uint32_t width, uint32_t height)
  7223. {
  7224. if (width == 0 || height == 0)
  7225. return false;
  7226. /*
  7227. * 845g/865g are special in that they are only limited by
  7228. * the width of their cursors, the height is arbitrary up to
  7229. * the precision of the register. Everything else requires
  7230. * square cursors, limited to a few power-of-two sizes.
  7231. */
  7232. if (IS_845G(dev) || IS_I865G(dev)) {
  7233. if ((width & 63) != 0)
  7234. return false;
  7235. if (width > (IS_845G(dev) ? 64 : 512))
  7236. return false;
  7237. if (height > 1023)
  7238. return false;
  7239. } else {
  7240. switch (width | height) {
  7241. case 256:
  7242. case 128:
  7243. if (IS_GEN2(dev))
  7244. return false;
  7245. case 64:
  7246. break;
  7247. default:
  7248. return false;
  7249. }
  7250. }
  7251. return true;
  7252. }
  7253. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  7254. u16 *blue, uint32_t start, uint32_t size)
  7255. {
  7256. int end = (start + size > 256) ? 256 : start + size, i;
  7257. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7258. for (i = start; i < end; i++) {
  7259. intel_crtc->lut_r[i] = red[i] >> 8;
  7260. intel_crtc->lut_g[i] = green[i] >> 8;
  7261. intel_crtc->lut_b[i] = blue[i] >> 8;
  7262. }
  7263. intel_crtc_load_lut(crtc);
  7264. }
  7265. /* VESA 640x480x72Hz mode to set on the pipe */
  7266. static struct drm_display_mode load_detect_mode = {
  7267. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  7268. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  7269. };
  7270. struct drm_framebuffer *
  7271. __intel_framebuffer_create(struct drm_device *dev,
  7272. struct drm_mode_fb_cmd2 *mode_cmd,
  7273. struct drm_i915_gem_object *obj)
  7274. {
  7275. struct intel_framebuffer *intel_fb;
  7276. int ret;
  7277. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7278. if (!intel_fb) {
  7279. drm_gem_object_unreference(&obj->base);
  7280. return ERR_PTR(-ENOMEM);
  7281. }
  7282. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  7283. if (ret)
  7284. goto err;
  7285. return &intel_fb->base;
  7286. err:
  7287. drm_gem_object_unreference(&obj->base);
  7288. kfree(intel_fb);
  7289. return ERR_PTR(ret);
  7290. }
  7291. static struct drm_framebuffer *
  7292. intel_framebuffer_create(struct drm_device *dev,
  7293. struct drm_mode_fb_cmd2 *mode_cmd,
  7294. struct drm_i915_gem_object *obj)
  7295. {
  7296. struct drm_framebuffer *fb;
  7297. int ret;
  7298. ret = i915_mutex_lock_interruptible(dev);
  7299. if (ret)
  7300. return ERR_PTR(ret);
  7301. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  7302. mutex_unlock(&dev->struct_mutex);
  7303. return fb;
  7304. }
  7305. static u32
  7306. intel_framebuffer_pitch_for_width(int width, int bpp)
  7307. {
  7308. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  7309. return ALIGN(pitch, 64);
  7310. }
  7311. static u32
  7312. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  7313. {
  7314. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  7315. return PAGE_ALIGN(pitch * mode->vdisplay);
  7316. }
  7317. static struct drm_framebuffer *
  7318. intel_framebuffer_create_for_mode(struct drm_device *dev,
  7319. struct drm_display_mode *mode,
  7320. int depth, int bpp)
  7321. {
  7322. struct drm_i915_gem_object *obj;
  7323. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  7324. obj = i915_gem_alloc_object(dev,
  7325. intel_framebuffer_size_for_mode(mode, bpp));
  7326. if (obj == NULL)
  7327. return ERR_PTR(-ENOMEM);
  7328. mode_cmd.width = mode->hdisplay;
  7329. mode_cmd.height = mode->vdisplay;
  7330. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  7331. bpp);
  7332. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  7333. return intel_framebuffer_create(dev, &mode_cmd, obj);
  7334. }
  7335. static struct drm_framebuffer *
  7336. mode_fits_in_fbdev(struct drm_device *dev,
  7337. struct drm_display_mode *mode)
  7338. {
  7339. #ifdef CONFIG_DRM_I915_FBDEV
  7340. struct drm_i915_private *dev_priv = dev->dev_private;
  7341. struct drm_i915_gem_object *obj;
  7342. struct drm_framebuffer *fb;
  7343. if (!dev_priv->fbdev)
  7344. return NULL;
  7345. if (!dev_priv->fbdev->fb)
  7346. return NULL;
  7347. obj = dev_priv->fbdev->fb->obj;
  7348. BUG_ON(!obj);
  7349. fb = &dev_priv->fbdev->fb->base;
  7350. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  7351. fb->bits_per_pixel))
  7352. return NULL;
  7353. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  7354. return NULL;
  7355. return fb;
  7356. #else
  7357. return NULL;
  7358. #endif
  7359. }
  7360. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  7361. struct drm_display_mode *mode,
  7362. struct intel_load_detect_pipe *old,
  7363. struct drm_modeset_acquire_ctx *ctx)
  7364. {
  7365. struct intel_crtc *intel_crtc;
  7366. struct intel_encoder *intel_encoder =
  7367. intel_attached_encoder(connector);
  7368. struct drm_crtc *possible_crtc;
  7369. struct drm_encoder *encoder = &intel_encoder->base;
  7370. struct drm_crtc *crtc = NULL;
  7371. struct drm_device *dev = encoder->dev;
  7372. struct drm_framebuffer *fb;
  7373. struct drm_mode_config *config = &dev->mode_config;
  7374. int ret, i = -1;
  7375. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7376. connector->base.id, connector->name,
  7377. encoder->base.id, encoder->name);
  7378. retry:
  7379. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  7380. if (ret)
  7381. goto fail_unlock;
  7382. /*
  7383. * Algorithm gets a little messy:
  7384. *
  7385. * - if the connector already has an assigned crtc, use it (but make
  7386. * sure it's on first)
  7387. *
  7388. * - try to find the first unused crtc that can drive this connector,
  7389. * and use that if we find one
  7390. */
  7391. /* See if we already have a CRTC for this connector */
  7392. if (encoder->crtc) {
  7393. crtc = encoder->crtc;
  7394. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7395. if (ret)
  7396. goto fail_unlock;
  7397. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  7398. if (ret)
  7399. goto fail_unlock;
  7400. old->dpms_mode = connector->dpms;
  7401. old->load_detect_temp = false;
  7402. /* Make sure the crtc and connector are running */
  7403. if (connector->dpms != DRM_MODE_DPMS_ON)
  7404. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  7405. return true;
  7406. }
  7407. /* Find an unused one (if possible) */
  7408. for_each_crtc(dev, possible_crtc) {
  7409. i++;
  7410. if (!(encoder->possible_crtcs & (1 << i)))
  7411. continue;
  7412. if (possible_crtc->state->enable)
  7413. continue;
  7414. /* This can occur when applying the pipe A quirk on resume. */
  7415. if (to_intel_crtc(possible_crtc)->new_enabled)
  7416. continue;
  7417. crtc = possible_crtc;
  7418. break;
  7419. }
  7420. /*
  7421. * If we didn't find an unused CRTC, don't use any.
  7422. */
  7423. if (!crtc) {
  7424. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  7425. goto fail_unlock;
  7426. }
  7427. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7428. if (ret)
  7429. goto fail_unlock;
  7430. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  7431. if (ret)
  7432. goto fail_unlock;
  7433. intel_encoder->new_crtc = to_intel_crtc(crtc);
  7434. to_intel_connector(connector)->new_encoder = intel_encoder;
  7435. intel_crtc = to_intel_crtc(crtc);
  7436. intel_crtc->new_enabled = true;
  7437. intel_crtc->new_config = intel_crtc->config;
  7438. old->dpms_mode = connector->dpms;
  7439. old->load_detect_temp = true;
  7440. old->release_fb = NULL;
  7441. if (!mode)
  7442. mode = &load_detect_mode;
  7443. /* We need a framebuffer large enough to accommodate all accesses
  7444. * that the plane may generate whilst we perform load detection.
  7445. * We can not rely on the fbcon either being present (we get called
  7446. * during its initialisation to detect all boot displays, or it may
  7447. * not even exist) or that it is large enough to satisfy the
  7448. * requested mode.
  7449. */
  7450. fb = mode_fits_in_fbdev(dev, mode);
  7451. if (fb == NULL) {
  7452. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  7453. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  7454. old->release_fb = fb;
  7455. } else
  7456. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  7457. if (IS_ERR(fb)) {
  7458. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  7459. goto fail;
  7460. }
  7461. if (intel_set_mode(crtc, mode, 0, 0, fb)) {
  7462. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  7463. if (old->release_fb)
  7464. old->release_fb->funcs->destroy(old->release_fb);
  7465. goto fail;
  7466. }
  7467. crtc->primary->crtc = crtc;
  7468. /* let the connector get through one full cycle before testing */
  7469. intel_wait_for_vblank(dev, intel_crtc->pipe);
  7470. return true;
  7471. fail:
  7472. intel_crtc->new_enabled = crtc->state->enable;
  7473. if (intel_crtc->new_enabled)
  7474. intel_crtc->new_config = intel_crtc->config;
  7475. else
  7476. intel_crtc->new_config = NULL;
  7477. fail_unlock:
  7478. if (ret == -EDEADLK) {
  7479. drm_modeset_backoff(ctx);
  7480. goto retry;
  7481. }
  7482. return false;
  7483. }
  7484. void intel_release_load_detect_pipe(struct drm_connector *connector,
  7485. struct intel_load_detect_pipe *old)
  7486. {
  7487. struct intel_encoder *intel_encoder =
  7488. intel_attached_encoder(connector);
  7489. struct drm_encoder *encoder = &intel_encoder->base;
  7490. struct drm_crtc *crtc = encoder->crtc;
  7491. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7492. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7493. connector->base.id, connector->name,
  7494. encoder->base.id, encoder->name);
  7495. if (old->load_detect_temp) {
  7496. to_intel_connector(connector)->new_encoder = NULL;
  7497. intel_encoder->new_crtc = NULL;
  7498. intel_crtc->new_enabled = false;
  7499. intel_crtc->new_config = NULL;
  7500. intel_set_mode(crtc, NULL, 0, 0, NULL);
  7501. if (old->release_fb) {
  7502. drm_framebuffer_unregister_private(old->release_fb);
  7503. drm_framebuffer_unreference(old->release_fb);
  7504. }
  7505. return;
  7506. }
  7507. /* Switch crtc and encoder back off if necessary */
  7508. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  7509. connector->funcs->dpms(connector, old->dpms_mode);
  7510. }
  7511. static int i9xx_pll_refclk(struct drm_device *dev,
  7512. const struct intel_crtc_state *pipe_config)
  7513. {
  7514. struct drm_i915_private *dev_priv = dev->dev_private;
  7515. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7516. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  7517. return dev_priv->vbt.lvds_ssc_freq;
  7518. else if (HAS_PCH_SPLIT(dev))
  7519. return 120000;
  7520. else if (!IS_GEN2(dev))
  7521. return 96000;
  7522. else
  7523. return 48000;
  7524. }
  7525. /* Returns the clock of the currently programmed mode of the given pipe. */
  7526. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  7527. struct intel_crtc_state *pipe_config)
  7528. {
  7529. struct drm_device *dev = crtc->base.dev;
  7530. struct drm_i915_private *dev_priv = dev->dev_private;
  7531. int pipe = pipe_config->cpu_transcoder;
  7532. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7533. u32 fp;
  7534. intel_clock_t clock;
  7535. int refclk = i9xx_pll_refclk(dev, pipe_config);
  7536. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  7537. fp = pipe_config->dpll_hw_state.fp0;
  7538. else
  7539. fp = pipe_config->dpll_hw_state.fp1;
  7540. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  7541. if (IS_PINEVIEW(dev)) {
  7542. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  7543. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7544. } else {
  7545. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  7546. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7547. }
  7548. if (!IS_GEN2(dev)) {
  7549. if (IS_PINEVIEW(dev))
  7550. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  7551. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  7552. else
  7553. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  7554. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7555. switch (dpll & DPLL_MODE_MASK) {
  7556. case DPLLB_MODE_DAC_SERIAL:
  7557. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  7558. 5 : 10;
  7559. break;
  7560. case DPLLB_MODE_LVDS:
  7561. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  7562. 7 : 14;
  7563. break;
  7564. default:
  7565. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  7566. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  7567. return;
  7568. }
  7569. if (IS_PINEVIEW(dev))
  7570. pineview_clock(refclk, &clock);
  7571. else
  7572. i9xx_clock(refclk, &clock);
  7573. } else {
  7574. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  7575. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  7576. if (is_lvds) {
  7577. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  7578. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7579. if (lvds & LVDS_CLKB_POWER_UP)
  7580. clock.p2 = 7;
  7581. else
  7582. clock.p2 = 14;
  7583. } else {
  7584. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  7585. clock.p1 = 2;
  7586. else {
  7587. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  7588. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  7589. }
  7590. if (dpll & PLL_P2_DIVIDE_BY_4)
  7591. clock.p2 = 4;
  7592. else
  7593. clock.p2 = 2;
  7594. }
  7595. i9xx_clock(refclk, &clock);
  7596. }
  7597. /*
  7598. * This value includes pixel_multiplier. We will use
  7599. * port_clock to compute adjusted_mode.crtc_clock in the
  7600. * encoder's get_config() function.
  7601. */
  7602. pipe_config->port_clock = clock.dot;
  7603. }
  7604. int intel_dotclock_calculate(int link_freq,
  7605. const struct intel_link_m_n *m_n)
  7606. {
  7607. /*
  7608. * The calculation for the data clock is:
  7609. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  7610. * But we want to avoid losing precison if possible, so:
  7611. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  7612. *
  7613. * and the link clock is simpler:
  7614. * link_clock = (m * link_clock) / n
  7615. */
  7616. if (!m_n->link_n)
  7617. return 0;
  7618. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  7619. }
  7620. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  7621. struct intel_crtc_state *pipe_config)
  7622. {
  7623. struct drm_device *dev = crtc->base.dev;
  7624. /* read out port_clock from the DPLL */
  7625. i9xx_crtc_clock_get(crtc, pipe_config);
  7626. /*
  7627. * This value does not include pixel_multiplier.
  7628. * We will check that port_clock and adjusted_mode.crtc_clock
  7629. * agree once we know their relationship in the encoder's
  7630. * get_config() function.
  7631. */
  7632. pipe_config->base.adjusted_mode.crtc_clock =
  7633. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  7634. &pipe_config->fdi_m_n);
  7635. }
  7636. /** Returns the currently programmed mode of the given pipe. */
  7637. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  7638. struct drm_crtc *crtc)
  7639. {
  7640. struct drm_i915_private *dev_priv = dev->dev_private;
  7641. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7642. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7643. struct drm_display_mode *mode;
  7644. struct intel_crtc_state pipe_config;
  7645. int htot = I915_READ(HTOTAL(cpu_transcoder));
  7646. int hsync = I915_READ(HSYNC(cpu_transcoder));
  7647. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  7648. int vsync = I915_READ(VSYNC(cpu_transcoder));
  7649. enum pipe pipe = intel_crtc->pipe;
  7650. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  7651. if (!mode)
  7652. return NULL;
  7653. /*
  7654. * Construct a pipe_config sufficient for getting the clock info
  7655. * back out of crtc_clock_get.
  7656. *
  7657. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  7658. * to use a real value here instead.
  7659. */
  7660. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  7661. pipe_config.pixel_multiplier = 1;
  7662. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  7663. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  7664. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  7665. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  7666. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  7667. mode->hdisplay = (htot & 0xffff) + 1;
  7668. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  7669. mode->hsync_start = (hsync & 0xffff) + 1;
  7670. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  7671. mode->vdisplay = (vtot & 0xffff) + 1;
  7672. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  7673. mode->vsync_start = (vsync & 0xffff) + 1;
  7674. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  7675. drm_mode_set_name(mode);
  7676. return mode;
  7677. }
  7678. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  7679. {
  7680. struct drm_device *dev = crtc->dev;
  7681. struct drm_i915_private *dev_priv = dev->dev_private;
  7682. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7683. if (!HAS_GMCH_DISPLAY(dev))
  7684. return;
  7685. if (!dev_priv->lvds_downclock_avail)
  7686. return;
  7687. /*
  7688. * Since this is called by a timer, we should never get here in
  7689. * the manual case.
  7690. */
  7691. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  7692. int pipe = intel_crtc->pipe;
  7693. int dpll_reg = DPLL(pipe);
  7694. int dpll;
  7695. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  7696. assert_panel_unlocked(dev_priv, pipe);
  7697. dpll = I915_READ(dpll_reg);
  7698. dpll |= DISPLAY_RATE_SELECT_FPA1;
  7699. I915_WRITE(dpll_reg, dpll);
  7700. intel_wait_for_vblank(dev, pipe);
  7701. dpll = I915_READ(dpll_reg);
  7702. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  7703. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  7704. }
  7705. }
  7706. void intel_mark_busy(struct drm_device *dev)
  7707. {
  7708. struct drm_i915_private *dev_priv = dev->dev_private;
  7709. if (dev_priv->mm.busy)
  7710. return;
  7711. intel_runtime_pm_get(dev_priv);
  7712. i915_update_gfx_val(dev_priv);
  7713. dev_priv->mm.busy = true;
  7714. }
  7715. void intel_mark_idle(struct drm_device *dev)
  7716. {
  7717. struct drm_i915_private *dev_priv = dev->dev_private;
  7718. struct drm_crtc *crtc;
  7719. if (!dev_priv->mm.busy)
  7720. return;
  7721. dev_priv->mm.busy = false;
  7722. if (!i915.powersave)
  7723. goto out;
  7724. for_each_crtc(dev, crtc) {
  7725. if (!crtc->primary->fb)
  7726. continue;
  7727. intel_decrease_pllclock(crtc);
  7728. }
  7729. if (INTEL_INFO(dev)->gen >= 6)
  7730. gen6_rps_idle(dev->dev_private);
  7731. out:
  7732. intel_runtime_pm_put(dev_priv);
  7733. }
  7734. static void intel_crtc_set_state(struct intel_crtc *crtc,
  7735. struct intel_crtc_state *crtc_state)
  7736. {
  7737. kfree(crtc->config);
  7738. crtc->config = crtc_state;
  7739. crtc->base.state = &crtc_state->base;
  7740. }
  7741. static void intel_crtc_destroy(struct drm_crtc *crtc)
  7742. {
  7743. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7744. struct drm_device *dev = crtc->dev;
  7745. struct intel_unpin_work *work;
  7746. spin_lock_irq(&dev->event_lock);
  7747. work = intel_crtc->unpin_work;
  7748. intel_crtc->unpin_work = NULL;
  7749. spin_unlock_irq(&dev->event_lock);
  7750. if (work) {
  7751. cancel_work_sync(&work->work);
  7752. kfree(work);
  7753. }
  7754. intel_crtc_set_state(intel_crtc, NULL);
  7755. drm_crtc_cleanup(crtc);
  7756. kfree(intel_crtc);
  7757. }
  7758. static void intel_unpin_work_fn(struct work_struct *__work)
  7759. {
  7760. struct intel_unpin_work *work =
  7761. container_of(__work, struct intel_unpin_work, work);
  7762. struct drm_device *dev = work->crtc->dev;
  7763. enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
  7764. mutex_lock(&dev->struct_mutex);
  7765. intel_unpin_fb_obj(intel_fb_obj(work->old_fb));
  7766. drm_gem_object_unreference(&work->pending_flip_obj->base);
  7767. intel_fbc_update(dev);
  7768. if (work->flip_queued_req)
  7769. i915_gem_request_assign(&work->flip_queued_req, NULL);
  7770. mutex_unlock(&dev->struct_mutex);
  7771. intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  7772. drm_framebuffer_unreference(work->old_fb);
  7773. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  7774. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  7775. kfree(work);
  7776. }
  7777. static void do_intel_finish_page_flip(struct drm_device *dev,
  7778. struct drm_crtc *crtc)
  7779. {
  7780. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7781. struct intel_unpin_work *work;
  7782. unsigned long flags;
  7783. /* Ignore early vblank irqs */
  7784. if (intel_crtc == NULL)
  7785. return;
  7786. /*
  7787. * This is called both by irq handlers and the reset code (to complete
  7788. * lost pageflips) so needs the full irqsave spinlocks.
  7789. */
  7790. spin_lock_irqsave(&dev->event_lock, flags);
  7791. work = intel_crtc->unpin_work;
  7792. /* Ensure we don't miss a work->pending update ... */
  7793. smp_rmb();
  7794. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  7795. spin_unlock_irqrestore(&dev->event_lock, flags);
  7796. return;
  7797. }
  7798. page_flip_completed(intel_crtc);
  7799. spin_unlock_irqrestore(&dev->event_lock, flags);
  7800. }
  7801. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  7802. {
  7803. struct drm_i915_private *dev_priv = dev->dev_private;
  7804. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  7805. do_intel_finish_page_flip(dev, crtc);
  7806. }
  7807. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  7808. {
  7809. struct drm_i915_private *dev_priv = dev->dev_private;
  7810. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  7811. do_intel_finish_page_flip(dev, crtc);
  7812. }
  7813. /* Is 'a' after or equal to 'b'? */
  7814. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  7815. {
  7816. return !((a - b) & 0x80000000);
  7817. }
  7818. static bool page_flip_finished(struct intel_crtc *crtc)
  7819. {
  7820. struct drm_device *dev = crtc->base.dev;
  7821. struct drm_i915_private *dev_priv = dev->dev_private;
  7822. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  7823. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  7824. return true;
  7825. /*
  7826. * The relevant registers doen't exist on pre-ctg.
  7827. * As the flip done interrupt doesn't trigger for mmio
  7828. * flips on gmch platforms, a flip count check isn't
  7829. * really needed there. But since ctg has the registers,
  7830. * include it in the check anyway.
  7831. */
  7832. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  7833. return true;
  7834. /*
  7835. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  7836. * used the same base address. In that case the mmio flip might
  7837. * have completed, but the CS hasn't even executed the flip yet.
  7838. *
  7839. * A flip count check isn't enough as the CS might have updated
  7840. * the base address just after start of vblank, but before we
  7841. * managed to process the interrupt. This means we'd complete the
  7842. * CS flip too soon.
  7843. *
  7844. * Combining both checks should get us a good enough result. It may
  7845. * still happen that the CS flip has been executed, but has not
  7846. * yet actually completed. But in case the base address is the same
  7847. * anyway, we don't really care.
  7848. */
  7849. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  7850. crtc->unpin_work->gtt_offset &&
  7851. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  7852. crtc->unpin_work->flip_count);
  7853. }
  7854. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  7855. {
  7856. struct drm_i915_private *dev_priv = dev->dev_private;
  7857. struct intel_crtc *intel_crtc =
  7858. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  7859. unsigned long flags;
  7860. /*
  7861. * This is called both by irq handlers and the reset code (to complete
  7862. * lost pageflips) so needs the full irqsave spinlocks.
  7863. *
  7864. * NB: An MMIO update of the plane base pointer will also
  7865. * generate a page-flip completion irq, i.e. every modeset
  7866. * is also accompanied by a spurious intel_prepare_page_flip().
  7867. */
  7868. spin_lock_irqsave(&dev->event_lock, flags);
  7869. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  7870. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  7871. spin_unlock_irqrestore(&dev->event_lock, flags);
  7872. }
  7873. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  7874. {
  7875. /* Ensure that the work item is consistent when activating it ... */
  7876. smp_wmb();
  7877. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  7878. /* and that it is marked active as soon as the irq could fire. */
  7879. smp_wmb();
  7880. }
  7881. static int intel_gen2_queue_flip(struct drm_device *dev,
  7882. struct drm_crtc *crtc,
  7883. struct drm_framebuffer *fb,
  7884. struct drm_i915_gem_object *obj,
  7885. struct intel_engine_cs *ring,
  7886. uint32_t flags)
  7887. {
  7888. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7889. u32 flip_mask;
  7890. int ret;
  7891. ret = intel_ring_begin(ring, 6);
  7892. if (ret)
  7893. return ret;
  7894. /* Can't queue multiple flips, so wait for the previous
  7895. * one to finish before executing the next.
  7896. */
  7897. if (intel_crtc->plane)
  7898. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7899. else
  7900. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7901. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7902. intel_ring_emit(ring, MI_NOOP);
  7903. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7904. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7905. intel_ring_emit(ring, fb->pitches[0]);
  7906. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7907. intel_ring_emit(ring, 0); /* aux display base address, unused */
  7908. intel_mark_page_flip_active(intel_crtc);
  7909. __intel_ring_advance(ring);
  7910. return 0;
  7911. }
  7912. static int intel_gen3_queue_flip(struct drm_device *dev,
  7913. struct drm_crtc *crtc,
  7914. struct drm_framebuffer *fb,
  7915. struct drm_i915_gem_object *obj,
  7916. struct intel_engine_cs *ring,
  7917. uint32_t flags)
  7918. {
  7919. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7920. u32 flip_mask;
  7921. int ret;
  7922. ret = intel_ring_begin(ring, 6);
  7923. if (ret)
  7924. return ret;
  7925. if (intel_crtc->plane)
  7926. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7927. else
  7928. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7929. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7930. intel_ring_emit(ring, MI_NOOP);
  7931. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  7932. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7933. intel_ring_emit(ring, fb->pitches[0]);
  7934. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7935. intel_ring_emit(ring, MI_NOOP);
  7936. intel_mark_page_flip_active(intel_crtc);
  7937. __intel_ring_advance(ring);
  7938. return 0;
  7939. }
  7940. static int intel_gen4_queue_flip(struct drm_device *dev,
  7941. struct drm_crtc *crtc,
  7942. struct drm_framebuffer *fb,
  7943. struct drm_i915_gem_object *obj,
  7944. struct intel_engine_cs *ring,
  7945. uint32_t flags)
  7946. {
  7947. struct drm_i915_private *dev_priv = dev->dev_private;
  7948. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7949. uint32_t pf, pipesrc;
  7950. int ret;
  7951. ret = intel_ring_begin(ring, 4);
  7952. if (ret)
  7953. return ret;
  7954. /* i965+ uses the linear or tiled offsets from the
  7955. * Display Registers (which do not change across a page-flip)
  7956. * so we need only reprogram the base address.
  7957. */
  7958. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7959. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7960. intel_ring_emit(ring, fb->pitches[0]);
  7961. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  7962. obj->tiling_mode);
  7963. /* XXX Enabling the panel-fitter across page-flip is so far
  7964. * untested on non-native modes, so ignore it for now.
  7965. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  7966. */
  7967. pf = 0;
  7968. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7969. intel_ring_emit(ring, pf | pipesrc);
  7970. intel_mark_page_flip_active(intel_crtc);
  7971. __intel_ring_advance(ring);
  7972. return 0;
  7973. }
  7974. static int intel_gen6_queue_flip(struct drm_device *dev,
  7975. struct drm_crtc *crtc,
  7976. struct drm_framebuffer *fb,
  7977. struct drm_i915_gem_object *obj,
  7978. struct intel_engine_cs *ring,
  7979. uint32_t flags)
  7980. {
  7981. struct drm_i915_private *dev_priv = dev->dev_private;
  7982. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7983. uint32_t pf, pipesrc;
  7984. int ret;
  7985. ret = intel_ring_begin(ring, 4);
  7986. if (ret)
  7987. return ret;
  7988. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7989. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7990. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  7991. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7992. /* Contrary to the suggestions in the documentation,
  7993. * "Enable Panel Fitter" does not seem to be required when page
  7994. * flipping with a non-native mode, and worse causes a normal
  7995. * modeset to fail.
  7996. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  7997. */
  7998. pf = 0;
  7999. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  8000. intel_ring_emit(ring, pf | pipesrc);
  8001. intel_mark_page_flip_active(intel_crtc);
  8002. __intel_ring_advance(ring);
  8003. return 0;
  8004. }
  8005. static int intel_gen7_queue_flip(struct drm_device *dev,
  8006. struct drm_crtc *crtc,
  8007. struct drm_framebuffer *fb,
  8008. struct drm_i915_gem_object *obj,
  8009. struct intel_engine_cs *ring,
  8010. uint32_t flags)
  8011. {
  8012. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8013. uint32_t plane_bit = 0;
  8014. int len, ret;
  8015. switch (intel_crtc->plane) {
  8016. case PLANE_A:
  8017. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  8018. break;
  8019. case PLANE_B:
  8020. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  8021. break;
  8022. case PLANE_C:
  8023. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  8024. break;
  8025. default:
  8026. WARN_ONCE(1, "unknown plane in flip command\n");
  8027. return -ENODEV;
  8028. }
  8029. len = 4;
  8030. if (ring->id == RCS) {
  8031. len += 6;
  8032. /*
  8033. * On Gen 8, SRM is now taking an extra dword to accommodate
  8034. * 48bits addresses, and we need a NOOP for the batch size to
  8035. * stay even.
  8036. */
  8037. if (IS_GEN8(dev))
  8038. len += 2;
  8039. }
  8040. /*
  8041. * BSpec MI_DISPLAY_FLIP for IVB:
  8042. * "The full packet must be contained within the same cache line."
  8043. *
  8044. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  8045. * cacheline, if we ever start emitting more commands before
  8046. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  8047. * then do the cacheline alignment, and finally emit the
  8048. * MI_DISPLAY_FLIP.
  8049. */
  8050. ret = intel_ring_cacheline_align(ring);
  8051. if (ret)
  8052. return ret;
  8053. ret = intel_ring_begin(ring, len);
  8054. if (ret)
  8055. return ret;
  8056. /* Unmask the flip-done completion message. Note that the bspec says that
  8057. * we should do this for both the BCS and RCS, and that we must not unmask
  8058. * more than one flip event at any time (or ensure that one flip message
  8059. * can be sent by waiting for flip-done prior to queueing new flips).
  8060. * Experimentation says that BCS works despite DERRMR masking all
  8061. * flip-done completion events and that unmasking all planes at once
  8062. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  8063. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  8064. */
  8065. if (ring->id == RCS) {
  8066. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  8067. intel_ring_emit(ring, DERRMR);
  8068. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  8069. DERRMR_PIPEB_PRI_FLIP_DONE |
  8070. DERRMR_PIPEC_PRI_FLIP_DONE));
  8071. if (IS_GEN8(dev))
  8072. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  8073. MI_SRM_LRM_GLOBAL_GTT);
  8074. else
  8075. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  8076. MI_SRM_LRM_GLOBAL_GTT);
  8077. intel_ring_emit(ring, DERRMR);
  8078. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  8079. if (IS_GEN8(dev)) {
  8080. intel_ring_emit(ring, 0);
  8081. intel_ring_emit(ring, MI_NOOP);
  8082. }
  8083. }
  8084. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  8085. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  8086. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8087. intel_ring_emit(ring, (MI_NOOP));
  8088. intel_mark_page_flip_active(intel_crtc);
  8089. __intel_ring_advance(ring);
  8090. return 0;
  8091. }
  8092. static bool use_mmio_flip(struct intel_engine_cs *ring,
  8093. struct drm_i915_gem_object *obj)
  8094. {
  8095. /*
  8096. * This is not being used for older platforms, because
  8097. * non-availability of flip done interrupt forces us to use
  8098. * CS flips. Older platforms derive flip done using some clever
  8099. * tricks involving the flip_pending status bits and vblank irqs.
  8100. * So using MMIO flips there would disrupt this mechanism.
  8101. */
  8102. if (ring == NULL)
  8103. return true;
  8104. if (INTEL_INFO(ring->dev)->gen < 5)
  8105. return false;
  8106. if (i915.use_mmio_flip < 0)
  8107. return false;
  8108. else if (i915.use_mmio_flip > 0)
  8109. return true;
  8110. else if (i915.enable_execlists)
  8111. return true;
  8112. else
  8113. return ring != i915_gem_request_get_ring(obj->last_read_req);
  8114. }
  8115. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
  8116. {
  8117. struct drm_device *dev = intel_crtc->base.dev;
  8118. struct drm_i915_private *dev_priv = dev->dev_private;
  8119. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  8120. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  8121. struct drm_i915_gem_object *obj = intel_fb->obj;
  8122. const enum pipe pipe = intel_crtc->pipe;
  8123. u32 ctl, stride;
  8124. ctl = I915_READ(PLANE_CTL(pipe, 0));
  8125. ctl &= ~PLANE_CTL_TILED_MASK;
  8126. if (obj->tiling_mode == I915_TILING_X)
  8127. ctl |= PLANE_CTL_TILED_X;
  8128. /*
  8129. * The stride is either expressed as a multiple of 64 bytes chunks for
  8130. * linear buffers or in number of tiles for tiled buffers.
  8131. */
  8132. stride = fb->pitches[0] >> 6;
  8133. if (obj->tiling_mode == I915_TILING_X)
  8134. stride = fb->pitches[0] >> 9; /* X tiles are 512 bytes wide */
  8135. /*
  8136. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  8137. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  8138. */
  8139. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  8140. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  8141. I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
  8142. POSTING_READ(PLANE_SURF(pipe, 0));
  8143. }
  8144. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
  8145. {
  8146. struct drm_device *dev = intel_crtc->base.dev;
  8147. struct drm_i915_private *dev_priv = dev->dev_private;
  8148. struct intel_framebuffer *intel_fb =
  8149. to_intel_framebuffer(intel_crtc->base.primary->fb);
  8150. struct drm_i915_gem_object *obj = intel_fb->obj;
  8151. u32 dspcntr;
  8152. u32 reg;
  8153. reg = DSPCNTR(intel_crtc->plane);
  8154. dspcntr = I915_READ(reg);
  8155. if (obj->tiling_mode != I915_TILING_NONE)
  8156. dspcntr |= DISPPLANE_TILED;
  8157. else
  8158. dspcntr &= ~DISPPLANE_TILED;
  8159. I915_WRITE(reg, dspcntr);
  8160. I915_WRITE(DSPSURF(intel_crtc->plane),
  8161. intel_crtc->unpin_work->gtt_offset);
  8162. POSTING_READ(DSPSURF(intel_crtc->plane));
  8163. }
  8164. /*
  8165. * XXX: This is the temporary way to update the plane registers until we get
  8166. * around to using the usual plane update functions for MMIO flips
  8167. */
  8168. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  8169. {
  8170. struct drm_device *dev = intel_crtc->base.dev;
  8171. bool atomic_update;
  8172. u32 start_vbl_count;
  8173. intel_mark_page_flip_active(intel_crtc);
  8174. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  8175. if (INTEL_INFO(dev)->gen >= 9)
  8176. skl_do_mmio_flip(intel_crtc);
  8177. else
  8178. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  8179. ilk_do_mmio_flip(intel_crtc);
  8180. if (atomic_update)
  8181. intel_pipe_update_end(intel_crtc, start_vbl_count);
  8182. }
  8183. static void intel_mmio_flip_work_func(struct work_struct *work)
  8184. {
  8185. struct intel_crtc *crtc =
  8186. container_of(work, struct intel_crtc, mmio_flip.work);
  8187. struct intel_mmio_flip *mmio_flip;
  8188. mmio_flip = &crtc->mmio_flip;
  8189. if (mmio_flip->req)
  8190. WARN_ON(__i915_wait_request(mmio_flip->req,
  8191. crtc->reset_counter,
  8192. false, NULL, NULL) != 0);
  8193. intel_do_mmio_flip(crtc);
  8194. if (mmio_flip->req) {
  8195. mutex_lock(&crtc->base.dev->struct_mutex);
  8196. i915_gem_request_assign(&mmio_flip->req, NULL);
  8197. mutex_unlock(&crtc->base.dev->struct_mutex);
  8198. }
  8199. }
  8200. static int intel_queue_mmio_flip(struct drm_device *dev,
  8201. struct drm_crtc *crtc,
  8202. struct drm_framebuffer *fb,
  8203. struct drm_i915_gem_object *obj,
  8204. struct intel_engine_cs *ring,
  8205. uint32_t flags)
  8206. {
  8207. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8208. i915_gem_request_assign(&intel_crtc->mmio_flip.req,
  8209. obj->last_write_req);
  8210. schedule_work(&intel_crtc->mmio_flip.work);
  8211. return 0;
  8212. }
  8213. static int intel_default_queue_flip(struct drm_device *dev,
  8214. struct drm_crtc *crtc,
  8215. struct drm_framebuffer *fb,
  8216. struct drm_i915_gem_object *obj,
  8217. struct intel_engine_cs *ring,
  8218. uint32_t flags)
  8219. {
  8220. return -ENODEV;
  8221. }
  8222. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  8223. struct drm_crtc *crtc)
  8224. {
  8225. struct drm_i915_private *dev_priv = dev->dev_private;
  8226. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8227. struct intel_unpin_work *work = intel_crtc->unpin_work;
  8228. u32 addr;
  8229. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  8230. return true;
  8231. if (!work->enable_stall_check)
  8232. return false;
  8233. if (work->flip_ready_vblank == 0) {
  8234. if (work->flip_queued_req &&
  8235. !i915_gem_request_completed(work->flip_queued_req, true))
  8236. return false;
  8237. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  8238. }
  8239. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  8240. return false;
  8241. /* Potential stall - if we see that the flip has happened,
  8242. * assume a missed interrupt. */
  8243. if (INTEL_INFO(dev)->gen >= 4)
  8244. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  8245. else
  8246. addr = I915_READ(DSPADDR(intel_crtc->plane));
  8247. /* There is a potential issue here with a false positive after a flip
  8248. * to the same address. We could address this by checking for a
  8249. * non-incrementing frame counter.
  8250. */
  8251. return addr == work->gtt_offset;
  8252. }
  8253. void intel_check_page_flip(struct drm_device *dev, int pipe)
  8254. {
  8255. struct drm_i915_private *dev_priv = dev->dev_private;
  8256. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  8257. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8258. WARN_ON(!in_irq());
  8259. if (crtc == NULL)
  8260. return;
  8261. spin_lock(&dev->event_lock);
  8262. if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
  8263. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  8264. intel_crtc->unpin_work->flip_queued_vblank,
  8265. drm_vblank_count(dev, pipe));
  8266. page_flip_completed(intel_crtc);
  8267. }
  8268. spin_unlock(&dev->event_lock);
  8269. }
  8270. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  8271. struct drm_framebuffer *fb,
  8272. struct drm_pending_vblank_event *event,
  8273. uint32_t page_flip_flags)
  8274. {
  8275. struct drm_device *dev = crtc->dev;
  8276. struct drm_i915_private *dev_priv = dev->dev_private;
  8277. struct drm_framebuffer *old_fb = crtc->primary->fb;
  8278. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  8279. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8280. struct drm_plane *primary = crtc->primary;
  8281. enum pipe pipe = intel_crtc->pipe;
  8282. struct intel_unpin_work *work;
  8283. struct intel_engine_cs *ring;
  8284. int ret;
  8285. /*
  8286. * drm_mode_page_flip_ioctl() should already catch this, but double
  8287. * check to be safe. In the future we may enable pageflipping from
  8288. * a disabled primary plane.
  8289. */
  8290. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  8291. return -EBUSY;
  8292. /* Can't change pixel format via MI display flips. */
  8293. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  8294. return -EINVAL;
  8295. /*
  8296. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  8297. * Note that pitch changes could also affect these register.
  8298. */
  8299. if (INTEL_INFO(dev)->gen > 3 &&
  8300. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  8301. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  8302. return -EINVAL;
  8303. if (i915_terminally_wedged(&dev_priv->gpu_error))
  8304. goto out_hang;
  8305. work = kzalloc(sizeof(*work), GFP_KERNEL);
  8306. if (work == NULL)
  8307. return -ENOMEM;
  8308. work->event = event;
  8309. work->crtc = crtc;
  8310. work->old_fb = old_fb;
  8311. INIT_WORK(&work->work, intel_unpin_work_fn);
  8312. ret = drm_crtc_vblank_get(crtc);
  8313. if (ret)
  8314. goto free_work;
  8315. /* We borrow the event spin lock for protecting unpin_work */
  8316. spin_lock_irq(&dev->event_lock);
  8317. if (intel_crtc->unpin_work) {
  8318. /* Before declaring the flip queue wedged, check if
  8319. * the hardware completed the operation behind our backs.
  8320. */
  8321. if (__intel_pageflip_stall_check(dev, crtc)) {
  8322. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  8323. page_flip_completed(intel_crtc);
  8324. } else {
  8325. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  8326. spin_unlock_irq(&dev->event_lock);
  8327. drm_crtc_vblank_put(crtc);
  8328. kfree(work);
  8329. return -EBUSY;
  8330. }
  8331. }
  8332. intel_crtc->unpin_work = work;
  8333. spin_unlock_irq(&dev->event_lock);
  8334. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  8335. flush_workqueue(dev_priv->wq);
  8336. /* Reference the objects for the scheduled work. */
  8337. drm_framebuffer_reference(work->old_fb);
  8338. drm_gem_object_reference(&obj->base);
  8339. crtc->primary->fb = fb;
  8340. update_state_fb(crtc->primary);
  8341. work->pending_flip_obj = obj;
  8342. ret = i915_mutex_lock_interruptible(dev);
  8343. if (ret)
  8344. goto cleanup;
  8345. atomic_inc(&intel_crtc->unpin_work_count);
  8346. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  8347. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  8348. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  8349. if (IS_VALLEYVIEW(dev)) {
  8350. ring = &dev_priv->ring[BCS];
  8351. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  8352. /* vlv: DISPLAY_FLIP fails to change tiling */
  8353. ring = NULL;
  8354. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  8355. ring = &dev_priv->ring[BCS];
  8356. } else if (INTEL_INFO(dev)->gen >= 7) {
  8357. ring = i915_gem_request_get_ring(obj->last_read_req);
  8358. if (ring == NULL || ring->id != RCS)
  8359. ring = &dev_priv->ring[BCS];
  8360. } else {
  8361. ring = &dev_priv->ring[RCS];
  8362. }
  8363. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb, ring);
  8364. if (ret)
  8365. goto cleanup_pending;
  8366. work->gtt_offset =
  8367. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
  8368. if (use_mmio_flip(ring, obj)) {
  8369. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  8370. page_flip_flags);
  8371. if (ret)
  8372. goto cleanup_unpin;
  8373. i915_gem_request_assign(&work->flip_queued_req,
  8374. obj->last_write_req);
  8375. } else {
  8376. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
  8377. page_flip_flags);
  8378. if (ret)
  8379. goto cleanup_unpin;
  8380. i915_gem_request_assign(&work->flip_queued_req,
  8381. intel_ring_get_request(ring));
  8382. }
  8383. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  8384. work->enable_stall_check = true;
  8385. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  8386. INTEL_FRONTBUFFER_PRIMARY(pipe));
  8387. intel_fbc_disable(dev);
  8388. intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  8389. mutex_unlock(&dev->struct_mutex);
  8390. trace_i915_flip_request(intel_crtc->plane, obj);
  8391. return 0;
  8392. cleanup_unpin:
  8393. intel_unpin_fb_obj(obj);
  8394. cleanup_pending:
  8395. atomic_dec(&intel_crtc->unpin_work_count);
  8396. mutex_unlock(&dev->struct_mutex);
  8397. cleanup:
  8398. crtc->primary->fb = old_fb;
  8399. update_state_fb(crtc->primary);
  8400. drm_gem_object_unreference_unlocked(&obj->base);
  8401. drm_framebuffer_unreference(work->old_fb);
  8402. spin_lock_irq(&dev->event_lock);
  8403. intel_crtc->unpin_work = NULL;
  8404. spin_unlock_irq(&dev->event_lock);
  8405. drm_crtc_vblank_put(crtc);
  8406. free_work:
  8407. kfree(work);
  8408. if (ret == -EIO) {
  8409. out_hang:
  8410. ret = intel_plane_restore(primary);
  8411. if (ret == 0 && event) {
  8412. spin_lock_irq(&dev->event_lock);
  8413. drm_send_vblank_event(dev, pipe, event);
  8414. spin_unlock_irq(&dev->event_lock);
  8415. }
  8416. }
  8417. return ret;
  8418. }
  8419. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  8420. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  8421. .load_lut = intel_crtc_load_lut,
  8422. .atomic_begin = intel_begin_crtc_commit,
  8423. .atomic_flush = intel_finish_crtc_commit,
  8424. };
  8425. /**
  8426. * intel_modeset_update_staged_output_state
  8427. *
  8428. * Updates the staged output configuration state, e.g. after we've read out the
  8429. * current hw state.
  8430. */
  8431. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  8432. {
  8433. struct intel_crtc *crtc;
  8434. struct intel_encoder *encoder;
  8435. struct intel_connector *connector;
  8436. for_each_intel_connector(dev, connector) {
  8437. connector->new_encoder =
  8438. to_intel_encoder(connector->base.encoder);
  8439. }
  8440. for_each_intel_encoder(dev, encoder) {
  8441. encoder->new_crtc =
  8442. to_intel_crtc(encoder->base.crtc);
  8443. }
  8444. for_each_intel_crtc(dev, crtc) {
  8445. crtc->new_enabled = crtc->base.state->enable;
  8446. if (crtc->new_enabled)
  8447. crtc->new_config = crtc->config;
  8448. else
  8449. crtc->new_config = NULL;
  8450. }
  8451. }
  8452. /**
  8453. * intel_modeset_commit_output_state
  8454. *
  8455. * This function copies the stage display pipe configuration to the real one.
  8456. */
  8457. static void intel_modeset_commit_output_state(struct drm_device *dev)
  8458. {
  8459. struct intel_crtc *crtc;
  8460. struct intel_encoder *encoder;
  8461. struct intel_connector *connector;
  8462. for_each_intel_connector(dev, connector) {
  8463. connector->base.encoder = &connector->new_encoder->base;
  8464. }
  8465. for_each_intel_encoder(dev, encoder) {
  8466. encoder->base.crtc = &encoder->new_crtc->base;
  8467. }
  8468. for_each_intel_crtc(dev, crtc) {
  8469. crtc->base.state->enable = crtc->new_enabled;
  8470. crtc->base.enabled = crtc->new_enabled;
  8471. }
  8472. }
  8473. static void
  8474. connected_sink_compute_bpp(struct intel_connector *connector,
  8475. struct intel_crtc_state *pipe_config)
  8476. {
  8477. int bpp = pipe_config->pipe_bpp;
  8478. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8479. connector->base.base.id,
  8480. connector->base.name);
  8481. /* Don't use an invalid EDID bpc value */
  8482. if (connector->base.display_info.bpc &&
  8483. connector->base.display_info.bpc * 3 < bpp) {
  8484. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8485. bpp, connector->base.display_info.bpc*3);
  8486. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  8487. }
  8488. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8489. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  8490. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8491. bpp);
  8492. pipe_config->pipe_bpp = 24;
  8493. }
  8494. }
  8495. static int
  8496. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8497. struct drm_framebuffer *fb,
  8498. struct intel_crtc_state *pipe_config)
  8499. {
  8500. struct drm_device *dev = crtc->base.dev;
  8501. struct intel_connector *connector;
  8502. int bpp;
  8503. switch (fb->pixel_format) {
  8504. case DRM_FORMAT_C8:
  8505. bpp = 8*3; /* since we go through a colormap */
  8506. break;
  8507. case DRM_FORMAT_XRGB1555:
  8508. case DRM_FORMAT_ARGB1555:
  8509. /* checked in intel_framebuffer_init already */
  8510. if (WARN_ON(INTEL_INFO(dev)->gen > 3))
  8511. return -EINVAL;
  8512. case DRM_FORMAT_RGB565:
  8513. bpp = 6*3; /* min is 18bpp */
  8514. break;
  8515. case DRM_FORMAT_XBGR8888:
  8516. case DRM_FORMAT_ABGR8888:
  8517. /* checked in intel_framebuffer_init already */
  8518. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8519. return -EINVAL;
  8520. case DRM_FORMAT_XRGB8888:
  8521. case DRM_FORMAT_ARGB8888:
  8522. bpp = 8*3;
  8523. break;
  8524. case DRM_FORMAT_XRGB2101010:
  8525. case DRM_FORMAT_ARGB2101010:
  8526. case DRM_FORMAT_XBGR2101010:
  8527. case DRM_FORMAT_ABGR2101010:
  8528. /* checked in intel_framebuffer_init already */
  8529. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8530. return -EINVAL;
  8531. bpp = 10*3;
  8532. break;
  8533. /* TODO: gen4+ supports 16 bpc floating point, too. */
  8534. default:
  8535. DRM_DEBUG_KMS("unsupported depth\n");
  8536. return -EINVAL;
  8537. }
  8538. pipe_config->pipe_bpp = bpp;
  8539. /* Clamp display bpp to EDID value */
  8540. for_each_intel_connector(dev, connector) {
  8541. if (!connector->new_encoder ||
  8542. connector->new_encoder->new_crtc != crtc)
  8543. continue;
  8544. connected_sink_compute_bpp(connector, pipe_config);
  8545. }
  8546. return bpp;
  8547. }
  8548. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  8549. {
  8550. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  8551. "type: 0x%x flags: 0x%x\n",
  8552. mode->crtc_clock,
  8553. mode->crtc_hdisplay, mode->crtc_hsync_start,
  8554. mode->crtc_hsync_end, mode->crtc_htotal,
  8555. mode->crtc_vdisplay, mode->crtc_vsync_start,
  8556. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  8557. }
  8558. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  8559. struct intel_crtc_state *pipe_config,
  8560. const char *context)
  8561. {
  8562. DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
  8563. context, pipe_name(crtc->pipe));
  8564. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  8565. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  8566. pipe_config->pipe_bpp, pipe_config->dither);
  8567. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8568. pipe_config->has_pch_encoder,
  8569. pipe_config->fdi_lanes,
  8570. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  8571. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  8572. pipe_config->fdi_m_n.tu);
  8573. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8574. pipe_config->has_dp_encoder,
  8575. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  8576. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  8577. pipe_config->dp_m_n.tu);
  8578. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  8579. pipe_config->has_dp_encoder,
  8580. pipe_config->dp_m2_n2.gmch_m,
  8581. pipe_config->dp_m2_n2.gmch_n,
  8582. pipe_config->dp_m2_n2.link_m,
  8583. pipe_config->dp_m2_n2.link_n,
  8584. pipe_config->dp_m2_n2.tu);
  8585. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  8586. pipe_config->has_audio,
  8587. pipe_config->has_infoframe);
  8588. DRM_DEBUG_KMS("requested mode:\n");
  8589. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  8590. DRM_DEBUG_KMS("adjusted mode:\n");
  8591. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  8592. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  8593. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  8594. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  8595. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  8596. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  8597. pipe_config->gmch_pfit.control,
  8598. pipe_config->gmch_pfit.pgm_ratios,
  8599. pipe_config->gmch_pfit.lvds_border_bits);
  8600. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  8601. pipe_config->pch_pfit.pos,
  8602. pipe_config->pch_pfit.size,
  8603. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  8604. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  8605. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  8606. }
  8607. static bool encoders_cloneable(const struct intel_encoder *a,
  8608. const struct intel_encoder *b)
  8609. {
  8610. /* masks could be asymmetric, so check both ways */
  8611. return a == b || (a->cloneable & (1 << b->type) &&
  8612. b->cloneable & (1 << a->type));
  8613. }
  8614. static bool check_single_encoder_cloning(struct intel_crtc *crtc,
  8615. struct intel_encoder *encoder)
  8616. {
  8617. struct drm_device *dev = crtc->base.dev;
  8618. struct intel_encoder *source_encoder;
  8619. for_each_intel_encoder(dev, source_encoder) {
  8620. if (source_encoder->new_crtc != crtc)
  8621. continue;
  8622. if (!encoders_cloneable(encoder, source_encoder))
  8623. return false;
  8624. }
  8625. return true;
  8626. }
  8627. static bool check_encoder_cloning(struct intel_crtc *crtc)
  8628. {
  8629. struct drm_device *dev = crtc->base.dev;
  8630. struct intel_encoder *encoder;
  8631. for_each_intel_encoder(dev, encoder) {
  8632. if (encoder->new_crtc != crtc)
  8633. continue;
  8634. if (!check_single_encoder_cloning(crtc, encoder))
  8635. return false;
  8636. }
  8637. return true;
  8638. }
  8639. static bool check_digital_port_conflicts(struct drm_device *dev)
  8640. {
  8641. struct intel_connector *connector;
  8642. unsigned int used_ports = 0;
  8643. /*
  8644. * Walk the connector list instead of the encoder
  8645. * list to detect the problem on ddi platforms
  8646. * where there's just one encoder per digital port.
  8647. */
  8648. for_each_intel_connector(dev, connector) {
  8649. struct intel_encoder *encoder = connector->new_encoder;
  8650. if (!encoder)
  8651. continue;
  8652. WARN_ON(!encoder->new_crtc);
  8653. switch (encoder->type) {
  8654. unsigned int port_mask;
  8655. case INTEL_OUTPUT_UNKNOWN:
  8656. if (WARN_ON(!HAS_DDI(dev)))
  8657. break;
  8658. case INTEL_OUTPUT_DISPLAYPORT:
  8659. case INTEL_OUTPUT_HDMI:
  8660. case INTEL_OUTPUT_EDP:
  8661. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  8662. /* the same port mustn't appear more than once */
  8663. if (used_ports & port_mask)
  8664. return false;
  8665. used_ports |= port_mask;
  8666. default:
  8667. break;
  8668. }
  8669. }
  8670. return true;
  8671. }
  8672. static struct intel_crtc_state *
  8673. intel_modeset_pipe_config(struct drm_crtc *crtc,
  8674. struct drm_framebuffer *fb,
  8675. struct drm_display_mode *mode)
  8676. {
  8677. struct drm_device *dev = crtc->dev;
  8678. struct intel_encoder *encoder;
  8679. struct intel_crtc_state *pipe_config;
  8680. int plane_bpp, ret = -EINVAL;
  8681. bool retry = true;
  8682. if (!check_encoder_cloning(to_intel_crtc(crtc))) {
  8683. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  8684. return ERR_PTR(-EINVAL);
  8685. }
  8686. if (!check_digital_port_conflicts(dev)) {
  8687. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  8688. return ERR_PTR(-EINVAL);
  8689. }
  8690. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8691. if (!pipe_config)
  8692. return ERR_PTR(-ENOMEM);
  8693. pipe_config->base.crtc = crtc;
  8694. drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
  8695. drm_mode_copy(&pipe_config->base.mode, mode);
  8696. pipe_config->cpu_transcoder =
  8697. (enum transcoder) to_intel_crtc(crtc)->pipe;
  8698. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8699. /*
  8700. * Sanitize sync polarity flags based on requested ones. If neither
  8701. * positive or negative polarity is requested, treat this as meaning
  8702. * negative polarity.
  8703. */
  8704. if (!(pipe_config->base.adjusted_mode.flags &
  8705. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  8706. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  8707. if (!(pipe_config->base.adjusted_mode.flags &
  8708. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  8709. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  8710. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  8711. * plane pixel format and any sink constraints into account. Returns the
  8712. * source plane bpp so that dithering can be selected on mismatches
  8713. * after encoders and crtc also have had their say. */
  8714. plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  8715. fb, pipe_config);
  8716. if (plane_bpp < 0)
  8717. goto fail;
  8718. /*
  8719. * Determine the real pipe dimensions. Note that stereo modes can
  8720. * increase the actual pipe size due to the frame doubling and
  8721. * insertion of additional space for blanks between the frame. This
  8722. * is stored in the crtc timings. We use the requested mode to do this
  8723. * computation to clearly distinguish it from the adjusted mode, which
  8724. * can be changed by the connectors in the below retry loop.
  8725. */
  8726. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  8727. &pipe_config->pipe_src_w,
  8728. &pipe_config->pipe_src_h);
  8729. encoder_retry:
  8730. /* Ensure the port clock defaults are reset when retrying. */
  8731. pipe_config->port_clock = 0;
  8732. pipe_config->pixel_multiplier = 1;
  8733. /* Fill in default crtc timings, allow encoders to overwrite them. */
  8734. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  8735. CRTC_STEREO_DOUBLE);
  8736. /* Pass our mode to the connectors and the CRTC to give them a chance to
  8737. * adjust it according to limitations or connector properties, and also
  8738. * a chance to reject the mode entirely.
  8739. */
  8740. for_each_intel_encoder(dev, encoder) {
  8741. if (&encoder->new_crtc->base != crtc)
  8742. continue;
  8743. if (!(encoder->compute_config(encoder, pipe_config))) {
  8744. DRM_DEBUG_KMS("Encoder config failure\n");
  8745. goto fail;
  8746. }
  8747. }
  8748. /* Set default port clock if not overwritten by the encoder. Needs to be
  8749. * done afterwards in case the encoder adjusts the mode. */
  8750. if (!pipe_config->port_clock)
  8751. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  8752. * pipe_config->pixel_multiplier;
  8753. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  8754. if (ret < 0) {
  8755. DRM_DEBUG_KMS("CRTC fixup failed\n");
  8756. goto fail;
  8757. }
  8758. if (ret == RETRY) {
  8759. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  8760. ret = -EINVAL;
  8761. goto fail;
  8762. }
  8763. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  8764. retry = false;
  8765. goto encoder_retry;
  8766. }
  8767. pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
  8768. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  8769. plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  8770. return pipe_config;
  8771. fail:
  8772. kfree(pipe_config);
  8773. return ERR_PTR(ret);
  8774. }
  8775. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  8776. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  8777. static void
  8778. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  8779. unsigned *prepare_pipes, unsigned *disable_pipes)
  8780. {
  8781. struct intel_crtc *intel_crtc;
  8782. struct drm_device *dev = crtc->dev;
  8783. struct intel_encoder *encoder;
  8784. struct intel_connector *connector;
  8785. struct drm_crtc *tmp_crtc;
  8786. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  8787. /* Check which crtcs have changed outputs connected to them, these need
  8788. * to be part of the prepare_pipes mask. We don't (yet) support global
  8789. * modeset across multiple crtcs, so modeset_pipes will only have one
  8790. * bit set at most. */
  8791. for_each_intel_connector(dev, connector) {
  8792. if (connector->base.encoder == &connector->new_encoder->base)
  8793. continue;
  8794. if (connector->base.encoder) {
  8795. tmp_crtc = connector->base.encoder->crtc;
  8796. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8797. }
  8798. if (connector->new_encoder)
  8799. *prepare_pipes |=
  8800. 1 << connector->new_encoder->new_crtc->pipe;
  8801. }
  8802. for_each_intel_encoder(dev, encoder) {
  8803. if (encoder->base.crtc == &encoder->new_crtc->base)
  8804. continue;
  8805. if (encoder->base.crtc) {
  8806. tmp_crtc = encoder->base.crtc;
  8807. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8808. }
  8809. if (encoder->new_crtc)
  8810. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  8811. }
  8812. /* Check for pipes that will be enabled/disabled ... */
  8813. for_each_intel_crtc(dev, intel_crtc) {
  8814. if (intel_crtc->base.state->enable == intel_crtc->new_enabled)
  8815. continue;
  8816. if (!intel_crtc->new_enabled)
  8817. *disable_pipes |= 1 << intel_crtc->pipe;
  8818. else
  8819. *prepare_pipes |= 1 << intel_crtc->pipe;
  8820. }
  8821. /* set_mode is also used to update properties on life display pipes. */
  8822. intel_crtc = to_intel_crtc(crtc);
  8823. if (intel_crtc->new_enabled)
  8824. *prepare_pipes |= 1 << intel_crtc->pipe;
  8825. /*
  8826. * For simplicity do a full modeset on any pipe where the output routing
  8827. * changed. We could be more clever, but that would require us to be
  8828. * more careful with calling the relevant encoder->mode_set functions.
  8829. */
  8830. if (*prepare_pipes)
  8831. *modeset_pipes = *prepare_pipes;
  8832. /* ... and mask these out. */
  8833. *modeset_pipes &= ~(*disable_pipes);
  8834. *prepare_pipes &= ~(*disable_pipes);
  8835. /*
  8836. * HACK: We don't (yet) fully support global modesets. intel_set_config
  8837. * obies this rule, but the modeset restore mode of
  8838. * intel_modeset_setup_hw_state does not.
  8839. */
  8840. *modeset_pipes &= 1 << intel_crtc->pipe;
  8841. *prepare_pipes &= 1 << intel_crtc->pipe;
  8842. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  8843. *modeset_pipes, *prepare_pipes, *disable_pipes);
  8844. }
  8845. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  8846. {
  8847. struct drm_encoder *encoder;
  8848. struct drm_device *dev = crtc->dev;
  8849. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  8850. if (encoder->crtc == crtc)
  8851. return true;
  8852. return false;
  8853. }
  8854. static void
  8855. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  8856. {
  8857. struct drm_i915_private *dev_priv = dev->dev_private;
  8858. struct intel_encoder *intel_encoder;
  8859. struct intel_crtc *intel_crtc;
  8860. struct drm_connector *connector;
  8861. intel_shared_dpll_commit(dev_priv);
  8862. for_each_intel_encoder(dev, intel_encoder) {
  8863. if (!intel_encoder->base.crtc)
  8864. continue;
  8865. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  8866. if (prepare_pipes & (1 << intel_crtc->pipe))
  8867. intel_encoder->connectors_active = false;
  8868. }
  8869. intel_modeset_commit_output_state(dev);
  8870. /* Double check state. */
  8871. for_each_intel_crtc(dev, intel_crtc) {
  8872. WARN_ON(intel_crtc->base.state->enable != intel_crtc_in_use(&intel_crtc->base));
  8873. WARN_ON(intel_crtc->new_config &&
  8874. intel_crtc->new_config != intel_crtc->config);
  8875. WARN_ON(intel_crtc->base.state->enable != !!intel_crtc->new_config);
  8876. }
  8877. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  8878. if (!connector->encoder || !connector->encoder->crtc)
  8879. continue;
  8880. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  8881. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  8882. struct drm_property *dpms_property =
  8883. dev->mode_config.dpms_property;
  8884. connector->dpms = DRM_MODE_DPMS_ON;
  8885. drm_object_property_set_value(&connector->base,
  8886. dpms_property,
  8887. DRM_MODE_DPMS_ON);
  8888. intel_encoder = to_intel_encoder(connector->encoder);
  8889. intel_encoder->connectors_active = true;
  8890. }
  8891. }
  8892. }
  8893. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  8894. {
  8895. int diff;
  8896. if (clock1 == clock2)
  8897. return true;
  8898. if (!clock1 || !clock2)
  8899. return false;
  8900. diff = abs(clock1 - clock2);
  8901. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  8902. return true;
  8903. return false;
  8904. }
  8905. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  8906. list_for_each_entry((intel_crtc), \
  8907. &(dev)->mode_config.crtc_list, \
  8908. base.head) \
  8909. if (mask & (1 <<(intel_crtc)->pipe))
  8910. static bool
  8911. intel_pipe_config_compare(struct drm_device *dev,
  8912. struct intel_crtc_state *current_config,
  8913. struct intel_crtc_state *pipe_config)
  8914. {
  8915. #define PIPE_CONF_CHECK_X(name) \
  8916. if (current_config->name != pipe_config->name) { \
  8917. DRM_ERROR("mismatch in " #name " " \
  8918. "(expected 0x%08x, found 0x%08x)\n", \
  8919. current_config->name, \
  8920. pipe_config->name); \
  8921. return false; \
  8922. }
  8923. #define PIPE_CONF_CHECK_I(name) \
  8924. if (current_config->name != pipe_config->name) { \
  8925. DRM_ERROR("mismatch in " #name " " \
  8926. "(expected %i, found %i)\n", \
  8927. current_config->name, \
  8928. pipe_config->name); \
  8929. return false; \
  8930. }
  8931. /* This is required for BDW+ where there is only one set of registers for
  8932. * switching between high and low RR.
  8933. * This macro can be used whenever a comparison has to be made between one
  8934. * hw state and multiple sw state variables.
  8935. */
  8936. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  8937. if ((current_config->name != pipe_config->name) && \
  8938. (current_config->alt_name != pipe_config->name)) { \
  8939. DRM_ERROR("mismatch in " #name " " \
  8940. "(expected %i or %i, found %i)\n", \
  8941. current_config->name, \
  8942. current_config->alt_name, \
  8943. pipe_config->name); \
  8944. return false; \
  8945. }
  8946. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  8947. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  8948. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  8949. "(expected %i, found %i)\n", \
  8950. current_config->name & (mask), \
  8951. pipe_config->name & (mask)); \
  8952. return false; \
  8953. }
  8954. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  8955. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  8956. DRM_ERROR("mismatch in " #name " " \
  8957. "(expected %i, found %i)\n", \
  8958. current_config->name, \
  8959. pipe_config->name); \
  8960. return false; \
  8961. }
  8962. #define PIPE_CONF_QUIRK(quirk) \
  8963. ((current_config->quirks | pipe_config->quirks) & (quirk))
  8964. PIPE_CONF_CHECK_I(cpu_transcoder);
  8965. PIPE_CONF_CHECK_I(has_pch_encoder);
  8966. PIPE_CONF_CHECK_I(fdi_lanes);
  8967. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  8968. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  8969. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  8970. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  8971. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  8972. PIPE_CONF_CHECK_I(has_dp_encoder);
  8973. if (INTEL_INFO(dev)->gen < 8) {
  8974. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  8975. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  8976. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  8977. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  8978. PIPE_CONF_CHECK_I(dp_m_n.tu);
  8979. if (current_config->has_drrs) {
  8980. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
  8981. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
  8982. PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
  8983. PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
  8984. PIPE_CONF_CHECK_I(dp_m2_n2.tu);
  8985. }
  8986. } else {
  8987. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
  8988. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
  8989. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
  8990. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
  8991. PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
  8992. }
  8993. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  8994. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  8995. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  8996. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  8997. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  8998. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  8999. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  9000. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  9001. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  9002. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  9003. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  9004. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  9005. PIPE_CONF_CHECK_I(pixel_multiplier);
  9006. PIPE_CONF_CHECK_I(has_hdmi_sink);
  9007. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  9008. IS_VALLEYVIEW(dev))
  9009. PIPE_CONF_CHECK_I(limited_color_range);
  9010. PIPE_CONF_CHECK_I(has_infoframe);
  9011. PIPE_CONF_CHECK_I(has_audio);
  9012. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9013. DRM_MODE_FLAG_INTERLACE);
  9014. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  9015. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9016. DRM_MODE_FLAG_PHSYNC);
  9017. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9018. DRM_MODE_FLAG_NHSYNC);
  9019. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9020. DRM_MODE_FLAG_PVSYNC);
  9021. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9022. DRM_MODE_FLAG_NVSYNC);
  9023. }
  9024. PIPE_CONF_CHECK_I(pipe_src_w);
  9025. PIPE_CONF_CHECK_I(pipe_src_h);
  9026. /*
  9027. * FIXME: BIOS likes to set up a cloned config with lvds+external
  9028. * screen. Since we don't yet re-compute the pipe config when moving
  9029. * just the lvds port away to another pipe the sw tracking won't match.
  9030. *
  9031. * Proper atomic modesets with recomputed global state will fix this.
  9032. * Until then just don't check gmch state for inherited modes.
  9033. */
  9034. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  9035. PIPE_CONF_CHECK_I(gmch_pfit.control);
  9036. /* pfit ratios are autocomputed by the hw on gen4+ */
  9037. if (INTEL_INFO(dev)->gen < 4)
  9038. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  9039. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  9040. }
  9041. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  9042. if (current_config->pch_pfit.enabled) {
  9043. PIPE_CONF_CHECK_I(pch_pfit.pos);
  9044. PIPE_CONF_CHECK_I(pch_pfit.size);
  9045. }
  9046. /* BDW+ don't expose a synchronous way to read the state */
  9047. if (IS_HASWELL(dev))
  9048. PIPE_CONF_CHECK_I(ips_enabled);
  9049. PIPE_CONF_CHECK_I(double_wide);
  9050. PIPE_CONF_CHECK_X(ddi_pll_sel);
  9051. PIPE_CONF_CHECK_I(shared_dpll);
  9052. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  9053. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  9054. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  9055. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  9056. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  9057. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  9058. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  9059. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  9060. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  9061. PIPE_CONF_CHECK_I(pipe_bpp);
  9062. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  9063. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  9064. #undef PIPE_CONF_CHECK_X
  9065. #undef PIPE_CONF_CHECK_I
  9066. #undef PIPE_CONF_CHECK_I_ALT
  9067. #undef PIPE_CONF_CHECK_FLAGS
  9068. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  9069. #undef PIPE_CONF_QUIRK
  9070. return true;
  9071. }
  9072. static void check_wm_state(struct drm_device *dev)
  9073. {
  9074. struct drm_i915_private *dev_priv = dev->dev_private;
  9075. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  9076. struct intel_crtc *intel_crtc;
  9077. int plane;
  9078. if (INTEL_INFO(dev)->gen < 9)
  9079. return;
  9080. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  9081. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  9082. for_each_intel_crtc(dev, intel_crtc) {
  9083. struct skl_ddb_entry *hw_entry, *sw_entry;
  9084. const enum pipe pipe = intel_crtc->pipe;
  9085. if (!intel_crtc->active)
  9086. continue;
  9087. /* planes */
  9088. for_each_plane(dev_priv, pipe, plane) {
  9089. hw_entry = &hw_ddb.plane[pipe][plane];
  9090. sw_entry = &sw_ddb->plane[pipe][plane];
  9091. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  9092. continue;
  9093. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  9094. "(expected (%u,%u), found (%u,%u))\n",
  9095. pipe_name(pipe), plane + 1,
  9096. sw_entry->start, sw_entry->end,
  9097. hw_entry->start, hw_entry->end);
  9098. }
  9099. /* cursor */
  9100. hw_entry = &hw_ddb.cursor[pipe];
  9101. sw_entry = &sw_ddb->cursor[pipe];
  9102. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  9103. continue;
  9104. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  9105. "(expected (%u,%u), found (%u,%u))\n",
  9106. pipe_name(pipe),
  9107. sw_entry->start, sw_entry->end,
  9108. hw_entry->start, hw_entry->end);
  9109. }
  9110. }
  9111. static void
  9112. check_connector_state(struct drm_device *dev)
  9113. {
  9114. struct intel_connector *connector;
  9115. for_each_intel_connector(dev, connector) {
  9116. /* This also checks the encoder/connector hw state with the
  9117. * ->get_hw_state callbacks. */
  9118. intel_connector_check_state(connector);
  9119. I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
  9120. "connector's staged encoder doesn't match current encoder\n");
  9121. }
  9122. }
  9123. static void
  9124. check_encoder_state(struct drm_device *dev)
  9125. {
  9126. struct intel_encoder *encoder;
  9127. struct intel_connector *connector;
  9128. for_each_intel_encoder(dev, encoder) {
  9129. bool enabled = false;
  9130. bool active = false;
  9131. enum pipe pipe, tracked_pipe;
  9132. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  9133. encoder->base.base.id,
  9134. encoder->base.name);
  9135. I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
  9136. "encoder's stage crtc doesn't match current crtc\n");
  9137. I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
  9138. "encoder's active_connectors set, but no crtc\n");
  9139. for_each_intel_connector(dev, connector) {
  9140. if (connector->base.encoder != &encoder->base)
  9141. continue;
  9142. enabled = true;
  9143. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  9144. active = true;
  9145. }
  9146. /*
  9147. * for MST connectors if we unplug the connector is gone
  9148. * away but the encoder is still connected to a crtc
  9149. * until a modeset happens in response to the hotplug.
  9150. */
  9151. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  9152. continue;
  9153. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  9154. "encoder's enabled state mismatch "
  9155. "(expected %i, found %i)\n",
  9156. !!encoder->base.crtc, enabled);
  9157. I915_STATE_WARN(active && !encoder->base.crtc,
  9158. "active encoder with no crtc\n");
  9159. I915_STATE_WARN(encoder->connectors_active != active,
  9160. "encoder's computed active state doesn't match tracked active state "
  9161. "(expected %i, found %i)\n", active, encoder->connectors_active);
  9162. active = encoder->get_hw_state(encoder, &pipe);
  9163. I915_STATE_WARN(active != encoder->connectors_active,
  9164. "encoder's hw state doesn't match sw tracking "
  9165. "(expected %i, found %i)\n",
  9166. encoder->connectors_active, active);
  9167. if (!encoder->base.crtc)
  9168. continue;
  9169. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  9170. I915_STATE_WARN(active && pipe != tracked_pipe,
  9171. "active encoder's pipe doesn't match"
  9172. "(expected %i, found %i)\n",
  9173. tracked_pipe, pipe);
  9174. }
  9175. }
  9176. static void
  9177. check_crtc_state(struct drm_device *dev)
  9178. {
  9179. struct drm_i915_private *dev_priv = dev->dev_private;
  9180. struct intel_crtc *crtc;
  9181. struct intel_encoder *encoder;
  9182. struct intel_crtc_state pipe_config;
  9183. for_each_intel_crtc(dev, crtc) {
  9184. bool enabled = false;
  9185. bool active = false;
  9186. memset(&pipe_config, 0, sizeof(pipe_config));
  9187. DRM_DEBUG_KMS("[CRTC:%d]\n",
  9188. crtc->base.base.id);
  9189. I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
  9190. "active crtc, but not enabled in sw tracking\n");
  9191. for_each_intel_encoder(dev, encoder) {
  9192. if (encoder->base.crtc != &crtc->base)
  9193. continue;
  9194. enabled = true;
  9195. if (encoder->connectors_active)
  9196. active = true;
  9197. }
  9198. I915_STATE_WARN(active != crtc->active,
  9199. "crtc's computed active state doesn't match tracked active state "
  9200. "(expected %i, found %i)\n", active, crtc->active);
  9201. I915_STATE_WARN(enabled != crtc->base.state->enable,
  9202. "crtc's computed enabled state doesn't match tracked enabled state "
  9203. "(expected %i, found %i)\n", enabled,
  9204. crtc->base.state->enable);
  9205. active = dev_priv->display.get_pipe_config(crtc,
  9206. &pipe_config);
  9207. /* hw state is inconsistent with the pipe quirk */
  9208. if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  9209. (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  9210. active = crtc->active;
  9211. for_each_intel_encoder(dev, encoder) {
  9212. enum pipe pipe;
  9213. if (encoder->base.crtc != &crtc->base)
  9214. continue;
  9215. if (encoder->get_hw_state(encoder, &pipe))
  9216. encoder->get_config(encoder, &pipe_config);
  9217. }
  9218. I915_STATE_WARN(crtc->active != active,
  9219. "crtc active state doesn't match with hw state "
  9220. "(expected %i, found %i)\n", crtc->active, active);
  9221. if (active &&
  9222. !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
  9223. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  9224. intel_dump_pipe_config(crtc, &pipe_config,
  9225. "[hw state]");
  9226. intel_dump_pipe_config(crtc, crtc->config,
  9227. "[sw state]");
  9228. }
  9229. }
  9230. }
  9231. static void
  9232. check_shared_dpll_state(struct drm_device *dev)
  9233. {
  9234. struct drm_i915_private *dev_priv = dev->dev_private;
  9235. struct intel_crtc *crtc;
  9236. struct intel_dpll_hw_state dpll_hw_state;
  9237. int i;
  9238. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9239. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  9240. int enabled_crtcs = 0, active_crtcs = 0;
  9241. bool active;
  9242. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  9243. DRM_DEBUG_KMS("%s\n", pll->name);
  9244. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  9245. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  9246. "more active pll users than references: %i vs %i\n",
  9247. pll->active, hweight32(pll->config.crtc_mask));
  9248. I915_STATE_WARN(pll->active && !pll->on,
  9249. "pll in active use but not on in sw tracking\n");
  9250. I915_STATE_WARN(pll->on && !pll->active,
  9251. "pll in on but not on in use in sw tracking\n");
  9252. I915_STATE_WARN(pll->on != active,
  9253. "pll on state mismatch (expected %i, found %i)\n",
  9254. pll->on, active);
  9255. for_each_intel_crtc(dev, crtc) {
  9256. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  9257. enabled_crtcs++;
  9258. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  9259. active_crtcs++;
  9260. }
  9261. I915_STATE_WARN(pll->active != active_crtcs,
  9262. "pll active crtcs mismatch (expected %i, found %i)\n",
  9263. pll->active, active_crtcs);
  9264. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  9265. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  9266. hweight32(pll->config.crtc_mask), enabled_crtcs);
  9267. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  9268. sizeof(dpll_hw_state)),
  9269. "pll hw state mismatch\n");
  9270. }
  9271. }
  9272. void
  9273. intel_modeset_check_state(struct drm_device *dev)
  9274. {
  9275. check_wm_state(dev);
  9276. check_connector_state(dev);
  9277. check_encoder_state(dev);
  9278. check_crtc_state(dev);
  9279. check_shared_dpll_state(dev);
  9280. }
  9281. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  9282. int dotclock)
  9283. {
  9284. /*
  9285. * FDI already provided one idea for the dotclock.
  9286. * Yell if the encoder disagrees.
  9287. */
  9288. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  9289. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9290. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  9291. }
  9292. static void update_scanline_offset(struct intel_crtc *crtc)
  9293. {
  9294. struct drm_device *dev = crtc->base.dev;
  9295. /*
  9296. * The scanline counter increments at the leading edge of hsync.
  9297. *
  9298. * On most platforms it starts counting from vtotal-1 on the
  9299. * first active line. That means the scanline counter value is
  9300. * always one less than what we would expect. Ie. just after
  9301. * start of vblank, which also occurs at start of hsync (on the
  9302. * last active line), the scanline counter will read vblank_start-1.
  9303. *
  9304. * On gen2 the scanline counter starts counting from 1 instead
  9305. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  9306. * to keep the value positive), instead of adding one.
  9307. *
  9308. * On HSW+ the behaviour of the scanline counter depends on the output
  9309. * type. For DP ports it behaves like most other platforms, but on HDMI
  9310. * there's an extra 1 line difference. So we need to add two instead of
  9311. * one to the value.
  9312. */
  9313. if (IS_GEN2(dev)) {
  9314. const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
  9315. int vtotal;
  9316. vtotal = mode->crtc_vtotal;
  9317. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  9318. vtotal /= 2;
  9319. crtc->scanline_offset = vtotal - 1;
  9320. } else if (HAS_DDI(dev) &&
  9321. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  9322. crtc->scanline_offset = 2;
  9323. } else
  9324. crtc->scanline_offset = 1;
  9325. }
  9326. static struct intel_crtc_state *
  9327. intel_modeset_compute_config(struct drm_crtc *crtc,
  9328. struct drm_display_mode *mode,
  9329. struct drm_framebuffer *fb,
  9330. unsigned *modeset_pipes,
  9331. unsigned *prepare_pipes,
  9332. unsigned *disable_pipes)
  9333. {
  9334. struct intel_crtc_state *pipe_config = NULL;
  9335. intel_modeset_affected_pipes(crtc, modeset_pipes,
  9336. prepare_pipes, disable_pipes);
  9337. if ((*modeset_pipes) == 0)
  9338. goto out;
  9339. /*
  9340. * Note this needs changes when we start tracking multiple modes
  9341. * and crtcs. At that point we'll need to compute the whole config
  9342. * (i.e. one pipe_config for each crtc) rather than just the one
  9343. * for this crtc.
  9344. */
  9345. pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
  9346. if (IS_ERR(pipe_config)) {
  9347. goto out;
  9348. }
  9349. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  9350. "[modeset]");
  9351. out:
  9352. return pipe_config;
  9353. }
  9354. static int __intel_set_mode_setup_plls(struct drm_device *dev,
  9355. unsigned modeset_pipes,
  9356. unsigned disable_pipes)
  9357. {
  9358. struct drm_i915_private *dev_priv = to_i915(dev);
  9359. unsigned clear_pipes = modeset_pipes | disable_pipes;
  9360. struct intel_crtc *intel_crtc;
  9361. int ret = 0;
  9362. if (!dev_priv->display.crtc_compute_clock)
  9363. return 0;
  9364. ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
  9365. if (ret)
  9366. goto done;
  9367. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9368. struct intel_crtc_state *state = intel_crtc->new_config;
  9369. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  9370. state);
  9371. if (ret) {
  9372. intel_shared_dpll_abort_config(dev_priv);
  9373. goto done;
  9374. }
  9375. }
  9376. done:
  9377. return ret;
  9378. }
  9379. static int __intel_set_mode(struct drm_crtc *crtc,
  9380. struct drm_display_mode *mode,
  9381. int x, int y, struct drm_framebuffer *fb,
  9382. struct intel_crtc_state *pipe_config,
  9383. unsigned modeset_pipes,
  9384. unsigned prepare_pipes,
  9385. unsigned disable_pipes)
  9386. {
  9387. struct drm_device *dev = crtc->dev;
  9388. struct drm_i915_private *dev_priv = dev->dev_private;
  9389. struct drm_display_mode *saved_mode;
  9390. struct intel_crtc *intel_crtc;
  9391. int ret = 0;
  9392. saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
  9393. if (!saved_mode)
  9394. return -ENOMEM;
  9395. *saved_mode = crtc->mode;
  9396. if (modeset_pipes)
  9397. to_intel_crtc(crtc)->new_config = pipe_config;
  9398. /*
  9399. * See if the config requires any additional preparation, e.g.
  9400. * to adjust global state with pipes off. We need to do this
  9401. * here so we can get the modeset_pipe updated config for the new
  9402. * mode set on this crtc. For other crtcs we need to use the
  9403. * adjusted_mode bits in the crtc directly.
  9404. */
  9405. if (IS_VALLEYVIEW(dev)) {
  9406. valleyview_modeset_global_pipes(dev, &prepare_pipes);
  9407. /* may have added more to prepare_pipes than we should */
  9408. prepare_pipes &= ~disable_pipes;
  9409. }
  9410. ret = __intel_set_mode_setup_plls(dev, modeset_pipes, disable_pipes);
  9411. if (ret)
  9412. goto done;
  9413. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  9414. intel_crtc_disable(&intel_crtc->base);
  9415. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9416. if (intel_crtc->base.state->enable)
  9417. dev_priv->display.crtc_disable(&intel_crtc->base);
  9418. }
  9419. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  9420. * to set it here already despite that we pass it down the callchain.
  9421. *
  9422. * Note we'll need to fix this up when we start tracking multiple
  9423. * pipes; here we assume a single modeset_pipe and only track the
  9424. * single crtc and mode.
  9425. */
  9426. if (modeset_pipes) {
  9427. crtc->mode = *mode;
  9428. /* mode_set/enable/disable functions rely on a correct pipe
  9429. * config. */
  9430. intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
  9431. /*
  9432. * Calculate and store various constants which
  9433. * are later needed by vblank and swap-completion
  9434. * timestamping. They are derived from true hwmode.
  9435. */
  9436. drm_calc_timestamping_constants(crtc,
  9437. &pipe_config->base.adjusted_mode);
  9438. }
  9439. /* Only after disabling all output pipelines that will be changed can we
  9440. * update the the output configuration. */
  9441. intel_modeset_update_state(dev, prepare_pipes);
  9442. modeset_update_crtc_power_domains(dev);
  9443. /* Set up the DPLL and any encoders state that needs to adjust or depend
  9444. * on the DPLL.
  9445. */
  9446. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9447. struct drm_plane *primary = intel_crtc->base.primary;
  9448. int vdisplay, hdisplay;
  9449. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  9450. ret = primary->funcs->update_plane(primary, &intel_crtc->base,
  9451. fb, 0, 0,
  9452. hdisplay, vdisplay,
  9453. x << 16, y << 16,
  9454. hdisplay << 16, vdisplay << 16);
  9455. }
  9456. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  9457. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9458. update_scanline_offset(intel_crtc);
  9459. dev_priv->display.crtc_enable(&intel_crtc->base);
  9460. }
  9461. /* FIXME: add subpixel order */
  9462. done:
  9463. if (ret && crtc->state->enable)
  9464. crtc->mode = *saved_mode;
  9465. kfree(saved_mode);
  9466. return ret;
  9467. }
  9468. static int intel_set_mode_pipes(struct drm_crtc *crtc,
  9469. struct drm_display_mode *mode,
  9470. int x, int y, struct drm_framebuffer *fb,
  9471. struct intel_crtc_state *pipe_config,
  9472. unsigned modeset_pipes,
  9473. unsigned prepare_pipes,
  9474. unsigned disable_pipes)
  9475. {
  9476. int ret;
  9477. ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
  9478. prepare_pipes, disable_pipes);
  9479. if (ret == 0)
  9480. intel_modeset_check_state(crtc->dev);
  9481. return ret;
  9482. }
  9483. static int intel_set_mode(struct drm_crtc *crtc,
  9484. struct drm_display_mode *mode,
  9485. int x, int y, struct drm_framebuffer *fb)
  9486. {
  9487. struct intel_crtc_state *pipe_config;
  9488. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  9489. pipe_config = intel_modeset_compute_config(crtc, mode, fb,
  9490. &modeset_pipes,
  9491. &prepare_pipes,
  9492. &disable_pipes);
  9493. if (IS_ERR(pipe_config))
  9494. return PTR_ERR(pipe_config);
  9495. return intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
  9496. modeset_pipes, prepare_pipes,
  9497. disable_pipes);
  9498. }
  9499. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  9500. {
  9501. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
  9502. }
  9503. #undef for_each_intel_crtc_masked
  9504. static void intel_set_config_free(struct intel_set_config *config)
  9505. {
  9506. if (!config)
  9507. return;
  9508. kfree(config->save_connector_encoders);
  9509. kfree(config->save_encoder_crtcs);
  9510. kfree(config->save_crtc_enabled);
  9511. kfree(config);
  9512. }
  9513. static int intel_set_config_save_state(struct drm_device *dev,
  9514. struct intel_set_config *config)
  9515. {
  9516. struct drm_crtc *crtc;
  9517. struct drm_encoder *encoder;
  9518. struct drm_connector *connector;
  9519. int count;
  9520. config->save_crtc_enabled =
  9521. kcalloc(dev->mode_config.num_crtc,
  9522. sizeof(bool), GFP_KERNEL);
  9523. if (!config->save_crtc_enabled)
  9524. return -ENOMEM;
  9525. config->save_encoder_crtcs =
  9526. kcalloc(dev->mode_config.num_encoder,
  9527. sizeof(struct drm_crtc *), GFP_KERNEL);
  9528. if (!config->save_encoder_crtcs)
  9529. return -ENOMEM;
  9530. config->save_connector_encoders =
  9531. kcalloc(dev->mode_config.num_connector,
  9532. sizeof(struct drm_encoder *), GFP_KERNEL);
  9533. if (!config->save_connector_encoders)
  9534. return -ENOMEM;
  9535. /* Copy data. Note that driver private data is not affected.
  9536. * Should anything bad happen only the expected state is
  9537. * restored, not the drivers personal bookkeeping.
  9538. */
  9539. count = 0;
  9540. for_each_crtc(dev, crtc) {
  9541. config->save_crtc_enabled[count++] = crtc->state->enable;
  9542. }
  9543. count = 0;
  9544. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  9545. config->save_encoder_crtcs[count++] = encoder->crtc;
  9546. }
  9547. count = 0;
  9548. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  9549. config->save_connector_encoders[count++] = connector->encoder;
  9550. }
  9551. return 0;
  9552. }
  9553. static void intel_set_config_restore_state(struct drm_device *dev,
  9554. struct intel_set_config *config)
  9555. {
  9556. struct intel_crtc *crtc;
  9557. struct intel_encoder *encoder;
  9558. struct intel_connector *connector;
  9559. int count;
  9560. count = 0;
  9561. for_each_intel_crtc(dev, crtc) {
  9562. crtc->new_enabled = config->save_crtc_enabled[count++];
  9563. if (crtc->new_enabled)
  9564. crtc->new_config = crtc->config;
  9565. else
  9566. crtc->new_config = NULL;
  9567. }
  9568. count = 0;
  9569. for_each_intel_encoder(dev, encoder) {
  9570. encoder->new_crtc =
  9571. to_intel_crtc(config->save_encoder_crtcs[count++]);
  9572. }
  9573. count = 0;
  9574. for_each_intel_connector(dev, connector) {
  9575. connector->new_encoder =
  9576. to_intel_encoder(config->save_connector_encoders[count++]);
  9577. }
  9578. }
  9579. static bool
  9580. is_crtc_connector_off(struct drm_mode_set *set)
  9581. {
  9582. int i;
  9583. if (set->num_connectors == 0)
  9584. return false;
  9585. if (WARN_ON(set->connectors == NULL))
  9586. return false;
  9587. for (i = 0; i < set->num_connectors; i++)
  9588. if (set->connectors[i]->encoder &&
  9589. set->connectors[i]->encoder->crtc == set->crtc &&
  9590. set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
  9591. return true;
  9592. return false;
  9593. }
  9594. static void
  9595. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  9596. struct intel_set_config *config)
  9597. {
  9598. /* We should be able to check here if the fb has the same properties
  9599. * and then just flip_or_move it */
  9600. if (is_crtc_connector_off(set)) {
  9601. config->mode_changed = true;
  9602. } else if (set->crtc->primary->fb != set->fb) {
  9603. /*
  9604. * If we have no fb, we can only flip as long as the crtc is
  9605. * active, otherwise we need a full mode set. The crtc may
  9606. * be active if we've only disabled the primary plane, or
  9607. * in fastboot situations.
  9608. */
  9609. if (set->crtc->primary->fb == NULL) {
  9610. struct intel_crtc *intel_crtc =
  9611. to_intel_crtc(set->crtc);
  9612. if (intel_crtc->active) {
  9613. DRM_DEBUG_KMS("crtc has no fb, will flip\n");
  9614. config->fb_changed = true;
  9615. } else {
  9616. DRM_DEBUG_KMS("inactive crtc, full mode set\n");
  9617. config->mode_changed = true;
  9618. }
  9619. } else if (set->fb == NULL) {
  9620. config->mode_changed = true;
  9621. } else if (set->fb->pixel_format !=
  9622. set->crtc->primary->fb->pixel_format) {
  9623. config->mode_changed = true;
  9624. } else {
  9625. config->fb_changed = true;
  9626. }
  9627. }
  9628. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  9629. config->fb_changed = true;
  9630. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  9631. DRM_DEBUG_KMS("modes are different, full mode set\n");
  9632. drm_mode_debug_printmodeline(&set->crtc->mode);
  9633. drm_mode_debug_printmodeline(set->mode);
  9634. config->mode_changed = true;
  9635. }
  9636. DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
  9637. set->crtc->base.id, config->mode_changed, config->fb_changed);
  9638. }
  9639. static int
  9640. intel_modeset_stage_output_state(struct drm_device *dev,
  9641. struct drm_mode_set *set,
  9642. struct intel_set_config *config)
  9643. {
  9644. struct intel_connector *connector;
  9645. struct intel_encoder *encoder;
  9646. struct intel_crtc *crtc;
  9647. int ro;
  9648. /* The upper layers ensure that we either disable a crtc or have a list
  9649. * of connectors. For paranoia, double-check this. */
  9650. WARN_ON(!set->fb && (set->num_connectors != 0));
  9651. WARN_ON(set->fb && (set->num_connectors == 0));
  9652. for_each_intel_connector(dev, connector) {
  9653. /* Otherwise traverse passed in connector list and get encoders
  9654. * for them. */
  9655. for (ro = 0; ro < set->num_connectors; ro++) {
  9656. if (set->connectors[ro] == &connector->base) {
  9657. connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
  9658. break;
  9659. }
  9660. }
  9661. /* If we disable the crtc, disable all its connectors. Also, if
  9662. * the connector is on the changing crtc but not on the new
  9663. * connector list, disable it. */
  9664. if ((!set->fb || ro == set->num_connectors) &&
  9665. connector->base.encoder &&
  9666. connector->base.encoder->crtc == set->crtc) {
  9667. connector->new_encoder = NULL;
  9668. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  9669. connector->base.base.id,
  9670. connector->base.name);
  9671. }
  9672. if (&connector->new_encoder->base != connector->base.encoder) {
  9673. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] encoder changed, full mode switch\n",
  9674. connector->base.base.id,
  9675. connector->base.name);
  9676. config->mode_changed = true;
  9677. }
  9678. }
  9679. /* connector->new_encoder is now updated for all connectors. */
  9680. /* Update crtc of enabled connectors. */
  9681. for_each_intel_connector(dev, connector) {
  9682. struct drm_crtc *new_crtc;
  9683. if (!connector->new_encoder)
  9684. continue;
  9685. new_crtc = connector->new_encoder->base.crtc;
  9686. for (ro = 0; ro < set->num_connectors; ro++) {
  9687. if (set->connectors[ro] == &connector->base)
  9688. new_crtc = set->crtc;
  9689. }
  9690. /* Make sure the new CRTC will work with the encoder */
  9691. if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
  9692. new_crtc)) {
  9693. return -EINVAL;
  9694. }
  9695. connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
  9696. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  9697. connector->base.base.id,
  9698. connector->base.name,
  9699. new_crtc->base.id);
  9700. }
  9701. /* Check for any encoders that needs to be disabled. */
  9702. for_each_intel_encoder(dev, encoder) {
  9703. int num_connectors = 0;
  9704. for_each_intel_connector(dev, connector) {
  9705. if (connector->new_encoder == encoder) {
  9706. WARN_ON(!connector->new_encoder->new_crtc);
  9707. num_connectors++;
  9708. }
  9709. }
  9710. if (num_connectors == 0)
  9711. encoder->new_crtc = NULL;
  9712. else if (num_connectors > 1)
  9713. return -EINVAL;
  9714. /* Only now check for crtc changes so we don't miss encoders
  9715. * that will be disabled. */
  9716. if (&encoder->new_crtc->base != encoder->base.crtc) {
  9717. DRM_DEBUG_KMS("[ENCODER:%d:%s] crtc changed, full mode switch\n",
  9718. encoder->base.base.id,
  9719. encoder->base.name);
  9720. config->mode_changed = true;
  9721. }
  9722. }
  9723. /* Now we've also updated encoder->new_crtc for all encoders. */
  9724. for_each_intel_connector(dev, connector) {
  9725. if (connector->new_encoder)
  9726. if (connector->new_encoder != connector->encoder)
  9727. connector->encoder = connector->new_encoder;
  9728. }
  9729. for_each_intel_crtc(dev, crtc) {
  9730. crtc->new_enabled = false;
  9731. for_each_intel_encoder(dev, encoder) {
  9732. if (encoder->new_crtc == crtc) {
  9733. crtc->new_enabled = true;
  9734. break;
  9735. }
  9736. }
  9737. if (crtc->new_enabled != crtc->base.state->enable) {
  9738. DRM_DEBUG_KMS("[CRTC:%d] %sabled, full mode switch\n",
  9739. crtc->base.base.id,
  9740. crtc->new_enabled ? "en" : "dis");
  9741. config->mode_changed = true;
  9742. }
  9743. if (crtc->new_enabled)
  9744. crtc->new_config = crtc->config;
  9745. else
  9746. crtc->new_config = NULL;
  9747. }
  9748. return 0;
  9749. }
  9750. static void disable_crtc_nofb(struct intel_crtc *crtc)
  9751. {
  9752. struct drm_device *dev = crtc->base.dev;
  9753. struct intel_encoder *encoder;
  9754. struct intel_connector *connector;
  9755. DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
  9756. pipe_name(crtc->pipe));
  9757. for_each_intel_connector(dev, connector) {
  9758. if (connector->new_encoder &&
  9759. connector->new_encoder->new_crtc == crtc)
  9760. connector->new_encoder = NULL;
  9761. }
  9762. for_each_intel_encoder(dev, encoder) {
  9763. if (encoder->new_crtc == crtc)
  9764. encoder->new_crtc = NULL;
  9765. }
  9766. crtc->new_enabled = false;
  9767. crtc->new_config = NULL;
  9768. }
  9769. static int intel_crtc_set_config(struct drm_mode_set *set)
  9770. {
  9771. struct drm_device *dev;
  9772. struct drm_mode_set save_set;
  9773. struct intel_set_config *config;
  9774. struct intel_crtc_state *pipe_config;
  9775. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  9776. int ret;
  9777. BUG_ON(!set);
  9778. BUG_ON(!set->crtc);
  9779. BUG_ON(!set->crtc->helper_private);
  9780. /* Enforce sane interface api - has been abused by the fb helper. */
  9781. BUG_ON(!set->mode && set->fb);
  9782. BUG_ON(set->fb && set->num_connectors == 0);
  9783. if (set->fb) {
  9784. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  9785. set->crtc->base.id, set->fb->base.id,
  9786. (int)set->num_connectors, set->x, set->y);
  9787. } else {
  9788. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  9789. }
  9790. dev = set->crtc->dev;
  9791. ret = -ENOMEM;
  9792. config = kzalloc(sizeof(*config), GFP_KERNEL);
  9793. if (!config)
  9794. goto out_config;
  9795. ret = intel_set_config_save_state(dev, config);
  9796. if (ret)
  9797. goto out_config;
  9798. save_set.crtc = set->crtc;
  9799. save_set.mode = &set->crtc->mode;
  9800. save_set.x = set->crtc->x;
  9801. save_set.y = set->crtc->y;
  9802. save_set.fb = set->crtc->primary->fb;
  9803. /* Compute whether we need a full modeset, only an fb base update or no
  9804. * change at all. In the future we might also check whether only the
  9805. * mode changed, e.g. for LVDS where we only change the panel fitter in
  9806. * such cases. */
  9807. intel_set_config_compute_mode_changes(set, config);
  9808. ret = intel_modeset_stage_output_state(dev, set, config);
  9809. if (ret)
  9810. goto fail;
  9811. pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
  9812. set->fb,
  9813. &modeset_pipes,
  9814. &prepare_pipes,
  9815. &disable_pipes);
  9816. if (IS_ERR(pipe_config)) {
  9817. ret = PTR_ERR(pipe_config);
  9818. goto fail;
  9819. } else if (pipe_config) {
  9820. if (pipe_config->has_audio !=
  9821. to_intel_crtc(set->crtc)->config->has_audio)
  9822. config->mode_changed = true;
  9823. /*
  9824. * Note we have an issue here with infoframes: current code
  9825. * only updates them on the full mode set path per hw
  9826. * requirements. So here we should be checking for any
  9827. * required changes and forcing a mode set.
  9828. */
  9829. }
  9830. /* set_mode will free it in the mode_changed case */
  9831. if (!config->mode_changed)
  9832. kfree(pipe_config);
  9833. intel_update_pipe_size(to_intel_crtc(set->crtc));
  9834. if (config->mode_changed) {
  9835. ret = intel_set_mode_pipes(set->crtc, set->mode,
  9836. set->x, set->y, set->fb, pipe_config,
  9837. modeset_pipes, prepare_pipes,
  9838. disable_pipes);
  9839. } else if (config->fb_changed) {
  9840. struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
  9841. struct drm_plane *primary = set->crtc->primary;
  9842. int vdisplay, hdisplay;
  9843. drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
  9844. ret = primary->funcs->update_plane(primary, set->crtc, set->fb,
  9845. 0, 0, hdisplay, vdisplay,
  9846. set->x << 16, set->y << 16,
  9847. hdisplay << 16, vdisplay << 16);
  9848. /*
  9849. * We need to make sure the primary plane is re-enabled if it
  9850. * has previously been turned off.
  9851. */
  9852. if (!intel_crtc->primary_enabled && ret == 0) {
  9853. WARN_ON(!intel_crtc->active);
  9854. intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
  9855. }
  9856. /*
  9857. * In the fastboot case this may be our only check of the
  9858. * state after boot. It would be better to only do it on
  9859. * the first update, but we don't have a nice way of doing that
  9860. * (and really, set_config isn't used much for high freq page
  9861. * flipping, so increasing its cost here shouldn't be a big
  9862. * deal).
  9863. */
  9864. if (i915.fastboot && ret == 0)
  9865. intel_modeset_check_state(set->crtc->dev);
  9866. }
  9867. if (ret) {
  9868. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  9869. set->crtc->base.id, ret);
  9870. fail:
  9871. intel_set_config_restore_state(dev, config);
  9872. /*
  9873. * HACK: if the pipe was on, but we didn't have a framebuffer,
  9874. * force the pipe off to avoid oopsing in the modeset code
  9875. * due to fb==NULL. This should only happen during boot since
  9876. * we don't yet reconstruct the FB from the hardware state.
  9877. */
  9878. if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
  9879. disable_crtc_nofb(to_intel_crtc(save_set.crtc));
  9880. /* Try to restore the config */
  9881. if (config->mode_changed &&
  9882. intel_set_mode(save_set.crtc, save_set.mode,
  9883. save_set.x, save_set.y, save_set.fb))
  9884. DRM_ERROR("failed to restore config after modeset failure\n");
  9885. }
  9886. out_config:
  9887. intel_set_config_free(config);
  9888. return ret;
  9889. }
  9890. static const struct drm_crtc_funcs intel_crtc_funcs = {
  9891. .gamma_set = intel_crtc_gamma_set,
  9892. .set_config = intel_crtc_set_config,
  9893. .destroy = intel_crtc_destroy,
  9894. .page_flip = intel_crtc_page_flip,
  9895. .atomic_duplicate_state = intel_crtc_duplicate_state,
  9896. .atomic_destroy_state = intel_crtc_destroy_state,
  9897. };
  9898. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  9899. struct intel_shared_dpll *pll,
  9900. struct intel_dpll_hw_state *hw_state)
  9901. {
  9902. uint32_t val;
  9903. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  9904. return false;
  9905. val = I915_READ(PCH_DPLL(pll->id));
  9906. hw_state->dpll = val;
  9907. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  9908. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  9909. return val & DPLL_VCO_ENABLE;
  9910. }
  9911. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  9912. struct intel_shared_dpll *pll)
  9913. {
  9914. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  9915. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  9916. }
  9917. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  9918. struct intel_shared_dpll *pll)
  9919. {
  9920. /* PCH refclock must be enabled first */
  9921. ibx_assert_pch_refclk_enabled(dev_priv);
  9922. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  9923. /* Wait for the clocks to stabilize. */
  9924. POSTING_READ(PCH_DPLL(pll->id));
  9925. udelay(150);
  9926. /* The pixel multiplier can only be updated once the
  9927. * DPLL is enabled and the clocks are stable.
  9928. *
  9929. * So write it again.
  9930. */
  9931. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  9932. POSTING_READ(PCH_DPLL(pll->id));
  9933. udelay(200);
  9934. }
  9935. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  9936. struct intel_shared_dpll *pll)
  9937. {
  9938. struct drm_device *dev = dev_priv->dev;
  9939. struct intel_crtc *crtc;
  9940. /* Make sure no transcoder isn't still depending on us. */
  9941. for_each_intel_crtc(dev, crtc) {
  9942. if (intel_crtc_to_shared_dpll(crtc) == pll)
  9943. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  9944. }
  9945. I915_WRITE(PCH_DPLL(pll->id), 0);
  9946. POSTING_READ(PCH_DPLL(pll->id));
  9947. udelay(200);
  9948. }
  9949. static char *ibx_pch_dpll_names[] = {
  9950. "PCH DPLL A",
  9951. "PCH DPLL B",
  9952. };
  9953. static void ibx_pch_dpll_init(struct drm_device *dev)
  9954. {
  9955. struct drm_i915_private *dev_priv = dev->dev_private;
  9956. int i;
  9957. dev_priv->num_shared_dpll = 2;
  9958. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9959. dev_priv->shared_dplls[i].id = i;
  9960. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  9961. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  9962. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  9963. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  9964. dev_priv->shared_dplls[i].get_hw_state =
  9965. ibx_pch_dpll_get_hw_state;
  9966. }
  9967. }
  9968. static void intel_shared_dpll_init(struct drm_device *dev)
  9969. {
  9970. struct drm_i915_private *dev_priv = dev->dev_private;
  9971. if (HAS_DDI(dev))
  9972. intel_ddi_pll_init(dev);
  9973. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  9974. ibx_pch_dpll_init(dev);
  9975. else
  9976. dev_priv->num_shared_dpll = 0;
  9977. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  9978. }
  9979. /**
  9980. * intel_prepare_plane_fb - Prepare fb for usage on plane
  9981. * @plane: drm plane to prepare for
  9982. * @fb: framebuffer to prepare for presentation
  9983. *
  9984. * Prepares a framebuffer for usage on a display plane. Generally this
  9985. * involves pinning the underlying object and updating the frontbuffer tracking
  9986. * bits. Some older platforms need special physical address handling for
  9987. * cursor planes.
  9988. *
  9989. * Returns 0 on success, negative error code on failure.
  9990. */
  9991. int
  9992. intel_prepare_plane_fb(struct drm_plane *plane,
  9993. struct drm_framebuffer *fb,
  9994. const struct drm_plane_state *new_state)
  9995. {
  9996. struct drm_device *dev = plane->dev;
  9997. struct intel_plane *intel_plane = to_intel_plane(plane);
  9998. enum pipe pipe = intel_plane->pipe;
  9999. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10000. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  10001. unsigned frontbuffer_bits = 0;
  10002. int ret = 0;
  10003. if (!obj)
  10004. return 0;
  10005. switch (plane->type) {
  10006. case DRM_PLANE_TYPE_PRIMARY:
  10007. frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
  10008. break;
  10009. case DRM_PLANE_TYPE_CURSOR:
  10010. frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
  10011. break;
  10012. case DRM_PLANE_TYPE_OVERLAY:
  10013. frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
  10014. break;
  10015. }
  10016. mutex_lock(&dev->struct_mutex);
  10017. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  10018. INTEL_INFO(dev)->cursor_needs_physical) {
  10019. int align = IS_I830(dev) ? 16 * 1024 : 256;
  10020. ret = i915_gem_object_attach_phys(obj, align);
  10021. if (ret)
  10022. DRM_DEBUG_KMS("failed to attach phys object\n");
  10023. } else {
  10024. ret = intel_pin_and_fence_fb_obj(plane, fb, NULL);
  10025. }
  10026. if (ret == 0)
  10027. i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
  10028. mutex_unlock(&dev->struct_mutex);
  10029. return ret;
  10030. }
  10031. /**
  10032. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  10033. * @plane: drm plane to clean up for
  10034. * @fb: old framebuffer that was on plane
  10035. *
  10036. * Cleans up a framebuffer that has just been removed from a plane.
  10037. */
  10038. void
  10039. intel_cleanup_plane_fb(struct drm_plane *plane,
  10040. struct drm_framebuffer *fb,
  10041. const struct drm_plane_state *old_state)
  10042. {
  10043. struct drm_device *dev = plane->dev;
  10044. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10045. if (WARN_ON(!obj))
  10046. return;
  10047. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  10048. !INTEL_INFO(dev)->cursor_needs_physical) {
  10049. mutex_lock(&dev->struct_mutex);
  10050. intel_unpin_fb_obj(obj);
  10051. mutex_unlock(&dev->struct_mutex);
  10052. }
  10053. }
  10054. static int
  10055. intel_check_primary_plane(struct drm_plane *plane,
  10056. struct intel_plane_state *state)
  10057. {
  10058. struct drm_device *dev = plane->dev;
  10059. struct drm_i915_private *dev_priv = dev->dev_private;
  10060. struct drm_crtc *crtc = state->base.crtc;
  10061. struct intel_crtc *intel_crtc;
  10062. struct drm_framebuffer *fb = state->base.fb;
  10063. struct drm_rect *dest = &state->dst;
  10064. struct drm_rect *src = &state->src;
  10065. const struct drm_rect *clip = &state->clip;
  10066. int ret;
  10067. crtc = crtc ? crtc : plane->crtc;
  10068. intel_crtc = to_intel_crtc(crtc);
  10069. ret = drm_plane_helper_check_update(plane, crtc, fb,
  10070. src, dest, clip,
  10071. DRM_PLANE_HELPER_NO_SCALING,
  10072. DRM_PLANE_HELPER_NO_SCALING,
  10073. false, true, &state->visible);
  10074. if (ret)
  10075. return ret;
  10076. if (intel_crtc->active) {
  10077. intel_crtc->atomic.wait_for_flips = true;
  10078. /*
  10079. * FBC does not work on some platforms for rotated
  10080. * planes, so disable it when rotation is not 0 and
  10081. * update it when rotation is set back to 0.
  10082. *
  10083. * FIXME: This is redundant with the fbc update done in
  10084. * the primary plane enable function except that that
  10085. * one is done too late. We eventually need to unify
  10086. * this.
  10087. */
  10088. if (intel_crtc->primary_enabled &&
  10089. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  10090. dev_priv->fbc.crtc == intel_crtc &&
  10091. state->base.rotation != BIT(DRM_ROTATE_0)) {
  10092. intel_crtc->atomic.disable_fbc = true;
  10093. }
  10094. if (state->visible) {
  10095. /*
  10096. * BDW signals flip done immediately if the plane
  10097. * is disabled, even if the plane enable is already
  10098. * armed to occur at the next vblank :(
  10099. */
  10100. if (IS_BROADWELL(dev) && !intel_crtc->primary_enabled)
  10101. intel_crtc->atomic.wait_vblank = true;
  10102. }
  10103. intel_crtc->atomic.fb_bits |=
  10104. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  10105. intel_crtc->atomic.update_fbc = true;
  10106. /* Update watermarks on tiling changes. */
  10107. if (!plane->state->fb || !state->base.fb ||
  10108. plane->state->fb->modifier[0] !=
  10109. state->base.fb->modifier[0])
  10110. intel_crtc->atomic.update_wm = true;
  10111. }
  10112. return 0;
  10113. }
  10114. static void
  10115. intel_commit_primary_plane(struct drm_plane *plane,
  10116. struct intel_plane_state *state)
  10117. {
  10118. struct drm_crtc *crtc = state->base.crtc;
  10119. struct drm_framebuffer *fb = state->base.fb;
  10120. struct drm_device *dev = plane->dev;
  10121. struct drm_i915_private *dev_priv = dev->dev_private;
  10122. struct intel_crtc *intel_crtc;
  10123. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10124. struct intel_plane *intel_plane = to_intel_plane(plane);
  10125. struct drm_rect *src = &state->src;
  10126. crtc = crtc ? crtc : plane->crtc;
  10127. intel_crtc = to_intel_crtc(crtc);
  10128. plane->fb = fb;
  10129. crtc->x = src->x1 >> 16;
  10130. crtc->y = src->y1 >> 16;
  10131. intel_plane->obj = obj;
  10132. if (intel_crtc->active) {
  10133. if (state->visible) {
  10134. /* FIXME: kill this fastboot hack */
  10135. intel_update_pipe_size(intel_crtc);
  10136. intel_crtc->primary_enabled = true;
  10137. dev_priv->display.update_primary_plane(crtc, plane->fb,
  10138. crtc->x, crtc->y);
  10139. } else {
  10140. /*
  10141. * If clipping results in a non-visible primary plane,
  10142. * we'll disable the primary plane. Note that this is
  10143. * a bit different than what happens if userspace
  10144. * explicitly disables the plane by passing fb=0
  10145. * because plane->fb still gets set and pinned.
  10146. */
  10147. intel_disable_primary_hw_plane(plane, crtc);
  10148. }
  10149. }
  10150. }
  10151. static void intel_begin_crtc_commit(struct drm_crtc *crtc)
  10152. {
  10153. struct drm_device *dev = crtc->dev;
  10154. struct drm_i915_private *dev_priv = dev->dev_private;
  10155. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10156. struct intel_plane *intel_plane;
  10157. struct drm_plane *p;
  10158. unsigned fb_bits = 0;
  10159. /* Track fb's for any planes being disabled */
  10160. list_for_each_entry(p, &dev->mode_config.plane_list, head) {
  10161. intel_plane = to_intel_plane(p);
  10162. if (intel_crtc->atomic.disabled_planes &
  10163. (1 << drm_plane_index(p))) {
  10164. switch (p->type) {
  10165. case DRM_PLANE_TYPE_PRIMARY:
  10166. fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
  10167. break;
  10168. case DRM_PLANE_TYPE_CURSOR:
  10169. fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
  10170. break;
  10171. case DRM_PLANE_TYPE_OVERLAY:
  10172. fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
  10173. break;
  10174. }
  10175. mutex_lock(&dev->struct_mutex);
  10176. i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
  10177. mutex_unlock(&dev->struct_mutex);
  10178. }
  10179. }
  10180. if (intel_crtc->atomic.wait_for_flips)
  10181. intel_crtc_wait_for_pending_flips(crtc);
  10182. if (intel_crtc->atomic.disable_fbc)
  10183. intel_fbc_disable(dev);
  10184. if (intel_crtc->atomic.pre_disable_primary)
  10185. intel_pre_disable_primary(crtc);
  10186. if (intel_crtc->atomic.update_wm)
  10187. intel_update_watermarks(crtc);
  10188. intel_runtime_pm_get(dev_priv);
  10189. /* Perform vblank evasion around commit operation */
  10190. if (intel_crtc->active)
  10191. intel_crtc->atomic.evade =
  10192. intel_pipe_update_start(intel_crtc,
  10193. &intel_crtc->atomic.start_vbl_count);
  10194. }
  10195. static void intel_finish_crtc_commit(struct drm_crtc *crtc)
  10196. {
  10197. struct drm_device *dev = crtc->dev;
  10198. struct drm_i915_private *dev_priv = dev->dev_private;
  10199. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10200. struct drm_plane *p;
  10201. if (intel_crtc->atomic.evade)
  10202. intel_pipe_update_end(intel_crtc,
  10203. intel_crtc->atomic.start_vbl_count);
  10204. intel_runtime_pm_put(dev_priv);
  10205. if (intel_crtc->atomic.wait_vblank)
  10206. intel_wait_for_vblank(dev, intel_crtc->pipe);
  10207. intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
  10208. if (intel_crtc->atomic.update_fbc) {
  10209. mutex_lock(&dev->struct_mutex);
  10210. intel_fbc_update(dev);
  10211. mutex_unlock(&dev->struct_mutex);
  10212. }
  10213. if (intel_crtc->atomic.post_enable_primary)
  10214. intel_post_enable_primary(crtc);
  10215. drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
  10216. if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
  10217. intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
  10218. false, false);
  10219. memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
  10220. }
  10221. /**
  10222. * intel_plane_destroy - destroy a plane
  10223. * @plane: plane to destroy
  10224. *
  10225. * Common destruction function for all types of planes (primary, cursor,
  10226. * sprite).
  10227. */
  10228. void intel_plane_destroy(struct drm_plane *plane)
  10229. {
  10230. struct intel_plane *intel_plane = to_intel_plane(plane);
  10231. drm_plane_cleanup(plane);
  10232. kfree(intel_plane);
  10233. }
  10234. const struct drm_plane_funcs intel_plane_funcs = {
  10235. .update_plane = drm_plane_helper_update,
  10236. .disable_plane = drm_plane_helper_disable,
  10237. .destroy = intel_plane_destroy,
  10238. .set_property = drm_atomic_helper_plane_set_property,
  10239. .atomic_get_property = intel_plane_atomic_get_property,
  10240. .atomic_set_property = intel_plane_atomic_set_property,
  10241. .atomic_duplicate_state = intel_plane_duplicate_state,
  10242. .atomic_destroy_state = intel_plane_destroy_state,
  10243. };
  10244. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  10245. int pipe)
  10246. {
  10247. struct intel_plane *primary;
  10248. struct intel_plane_state *state;
  10249. const uint32_t *intel_primary_formats;
  10250. int num_formats;
  10251. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  10252. if (primary == NULL)
  10253. return NULL;
  10254. state = intel_create_plane_state(&primary->base);
  10255. if (!state) {
  10256. kfree(primary);
  10257. return NULL;
  10258. }
  10259. primary->base.state = &state->base;
  10260. primary->can_scale = false;
  10261. primary->max_downscale = 1;
  10262. primary->pipe = pipe;
  10263. primary->plane = pipe;
  10264. primary->check_plane = intel_check_primary_plane;
  10265. primary->commit_plane = intel_commit_primary_plane;
  10266. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  10267. primary->plane = !pipe;
  10268. if (INTEL_INFO(dev)->gen <= 3) {
  10269. intel_primary_formats = intel_primary_formats_gen2;
  10270. num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
  10271. } else {
  10272. intel_primary_formats = intel_primary_formats_gen4;
  10273. num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
  10274. }
  10275. drm_universal_plane_init(dev, &primary->base, 0,
  10276. &intel_plane_funcs,
  10277. intel_primary_formats, num_formats,
  10278. DRM_PLANE_TYPE_PRIMARY);
  10279. if (INTEL_INFO(dev)->gen >= 4) {
  10280. if (!dev->mode_config.rotation_property)
  10281. dev->mode_config.rotation_property =
  10282. drm_mode_create_rotation_property(dev,
  10283. BIT(DRM_ROTATE_0) |
  10284. BIT(DRM_ROTATE_180));
  10285. if (dev->mode_config.rotation_property)
  10286. drm_object_attach_property(&primary->base.base,
  10287. dev->mode_config.rotation_property,
  10288. state->base.rotation);
  10289. }
  10290. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  10291. return &primary->base;
  10292. }
  10293. static int
  10294. intel_check_cursor_plane(struct drm_plane *plane,
  10295. struct intel_plane_state *state)
  10296. {
  10297. struct drm_crtc *crtc = state->base.crtc;
  10298. struct drm_device *dev = plane->dev;
  10299. struct drm_framebuffer *fb = state->base.fb;
  10300. struct drm_rect *dest = &state->dst;
  10301. struct drm_rect *src = &state->src;
  10302. const struct drm_rect *clip = &state->clip;
  10303. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10304. struct intel_crtc *intel_crtc;
  10305. unsigned stride;
  10306. int ret;
  10307. crtc = crtc ? crtc : plane->crtc;
  10308. intel_crtc = to_intel_crtc(crtc);
  10309. ret = drm_plane_helper_check_update(plane, crtc, fb,
  10310. src, dest, clip,
  10311. DRM_PLANE_HELPER_NO_SCALING,
  10312. DRM_PLANE_HELPER_NO_SCALING,
  10313. true, true, &state->visible);
  10314. if (ret)
  10315. return ret;
  10316. /* if we want to turn off the cursor ignore width and height */
  10317. if (!obj)
  10318. goto finish;
  10319. /* Check for which cursor types we support */
  10320. if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
  10321. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  10322. state->base.crtc_w, state->base.crtc_h);
  10323. return -EINVAL;
  10324. }
  10325. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  10326. if (obj->base.size < stride * state->base.crtc_h) {
  10327. DRM_DEBUG_KMS("buffer is too small\n");
  10328. return -ENOMEM;
  10329. }
  10330. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  10331. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  10332. ret = -EINVAL;
  10333. }
  10334. finish:
  10335. if (intel_crtc->active) {
  10336. if (plane->state->crtc_w != state->base.crtc_w)
  10337. intel_crtc->atomic.update_wm = true;
  10338. intel_crtc->atomic.fb_bits |=
  10339. INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
  10340. }
  10341. return ret;
  10342. }
  10343. static void
  10344. intel_commit_cursor_plane(struct drm_plane *plane,
  10345. struct intel_plane_state *state)
  10346. {
  10347. struct drm_crtc *crtc = state->base.crtc;
  10348. struct drm_device *dev = plane->dev;
  10349. struct intel_crtc *intel_crtc;
  10350. struct intel_plane *intel_plane = to_intel_plane(plane);
  10351. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  10352. uint32_t addr;
  10353. crtc = crtc ? crtc : plane->crtc;
  10354. intel_crtc = to_intel_crtc(crtc);
  10355. plane->fb = state->base.fb;
  10356. crtc->cursor_x = state->base.crtc_x;
  10357. crtc->cursor_y = state->base.crtc_y;
  10358. intel_plane->obj = obj;
  10359. if (intel_crtc->cursor_bo == obj)
  10360. goto update;
  10361. if (!obj)
  10362. addr = 0;
  10363. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  10364. addr = i915_gem_obj_ggtt_offset(obj);
  10365. else
  10366. addr = obj->phys_handle->busaddr;
  10367. intel_crtc->cursor_addr = addr;
  10368. intel_crtc->cursor_bo = obj;
  10369. update:
  10370. if (intel_crtc->active)
  10371. intel_crtc_update_cursor(crtc, state->visible);
  10372. }
  10373. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  10374. int pipe)
  10375. {
  10376. struct intel_plane *cursor;
  10377. struct intel_plane_state *state;
  10378. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  10379. if (cursor == NULL)
  10380. return NULL;
  10381. state = intel_create_plane_state(&cursor->base);
  10382. if (!state) {
  10383. kfree(cursor);
  10384. return NULL;
  10385. }
  10386. cursor->base.state = &state->base;
  10387. cursor->can_scale = false;
  10388. cursor->max_downscale = 1;
  10389. cursor->pipe = pipe;
  10390. cursor->plane = pipe;
  10391. cursor->check_plane = intel_check_cursor_plane;
  10392. cursor->commit_plane = intel_commit_cursor_plane;
  10393. drm_universal_plane_init(dev, &cursor->base, 0,
  10394. &intel_plane_funcs,
  10395. intel_cursor_formats,
  10396. ARRAY_SIZE(intel_cursor_formats),
  10397. DRM_PLANE_TYPE_CURSOR);
  10398. if (INTEL_INFO(dev)->gen >= 4) {
  10399. if (!dev->mode_config.rotation_property)
  10400. dev->mode_config.rotation_property =
  10401. drm_mode_create_rotation_property(dev,
  10402. BIT(DRM_ROTATE_0) |
  10403. BIT(DRM_ROTATE_180));
  10404. if (dev->mode_config.rotation_property)
  10405. drm_object_attach_property(&cursor->base.base,
  10406. dev->mode_config.rotation_property,
  10407. state->base.rotation);
  10408. }
  10409. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  10410. return &cursor->base;
  10411. }
  10412. static void intel_crtc_init(struct drm_device *dev, int pipe)
  10413. {
  10414. struct drm_i915_private *dev_priv = dev->dev_private;
  10415. struct intel_crtc *intel_crtc;
  10416. struct intel_crtc_state *crtc_state = NULL;
  10417. struct drm_plane *primary = NULL;
  10418. struct drm_plane *cursor = NULL;
  10419. int i, ret;
  10420. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  10421. if (intel_crtc == NULL)
  10422. return;
  10423. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  10424. if (!crtc_state)
  10425. goto fail;
  10426. intel_crtc_set_state(intel_crtc, crtc_state);
  10427. crtc_state->base.crtc = &intel_crtc->base;
  10428. primary = intel_primary_plane_create(dev, pipe);
  10429. if (!primary)
  10430. goto fail;
  10431. cursor = intel_cursor_plane_create(dev, pipe);
  10432. if (!cursor)
  10433. goto fail;
  10434. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  10435. cursor, &intel_crtc_funcs);
  10436. if (ret)
  10437. goto fail;
  10438. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  10439. for (i = 0; i < 256; i++) {
  10440. intel_crtc->lut_r[i] = i;
  10441. intel_crtc->lut_g[i] = i;
  10442. intel_crtc->lut_b[i] = i;
  10443. }
  10444. /*
  10445. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  10446. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  10447. */
  10448. intel_crtc->pipe = pipe;
  10449. intel_crtc->plane = pipe;
  10450. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  10451. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  10452. intel_crtc->plane = !pipe;
  10453. }
  10454. intel_crtc->cursor_base = ~0;
  10455. intel_crtc->cursor_cntl = ~0;
  10456. intel_crtc->cursor_size = ~0;
  10457. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  10458. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  10459. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  10460. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  10461. INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
  10462. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  10463. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  10464. return;
  10465. fail:
  10466. if (primary)
  10467. drm_plane_cleanup(primary);
  10468. if (cursor)
  10469. drm_plane_cleanup(cursor);
  10470. kfree(crtc_state);
  10471. kfree(intel_crtc);
  10472. }
  10473. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  10474. {
  10475. struct drm_encoder *encoder = connector->base.encoder;
  10476. struct drm_device *dev = connector->base.dev;
  10477. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  10478. if (!encoder || WARN_ON(!encoder->crtc))
  10479. return INVALID_PIPE;
  10480. return to_intel_crtc(encoder->crtc)->pipe;
  10481. }
  10482. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  10483. struct drm_file *file)
  10484. {
  10485. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  10486. struct drm_crtc *drmmode_crtc;
  10487. struct intel_crtc *crtc;
  10488. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  10489. if (!drmmode_crtc) {
  10490. DRM_ERROR("no such CRTC id\n");
  10491. return -ENOENT;
  10492. }
  10493. crtc = to_intel_crtc(drmmode_crtc);
  10494. pipe_from_crtc_id->pipe = crtc->pipe;
  10495. return 0;
  10496. }
  10497. static int intel_encoder_clones(struct intel_encoder *encoder)
  10498. {
  10499. struct drm_device *dev = encoder->base.dev;
  10500. struct intel_encoder *source_encoder;
  10501. int index_mask = 0;
  10502. int entry = 0;
  10503. for_each_intel_encoder(dev, source_encoder) {
  10504. if (encoders_cloneable(encoder, source_encoder))
  10505. index_mask |= (1 << entry);
  10506. entry++;
  10507. }
  10508. return index_mask;
  10509. }
  10510. static bool has_edp_a(struct drm_device *dev)
  10511. {
  10512. struct drm_i915_private *dev_priv = dev->dev_private;
  10513. if (!IS_MOBILE(dev))
  10514. return false;
  10515. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  10516. return false;
  10517. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  10518. return false;
  10519. return true;
  10520. }
  10521. static bool intel_crt_present(struct drm_device *dev)
  10522. {
  10523. struct drm_i915_private *dev_priv = dev->dev_private;
  10524. if (INTEL_INFO(dev)->gen >= 9)
  10525. return false;
  10526. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  10527. return false;
  10528. if (IS_CHERRYVIEW(dev))
  10529. return false;
  10530. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  10531. return false;
  10532. return true;
  10533. }
  10534. static void intel_setup_outputs(struct drm_device *dev)
  10535. {
  10536. struct drm_i915_private *dev_priv = dev->dev_private;
  10537. struct intel_encoder *encoder;
  10538. struct drm_connector *connector;
  10539. bool dpd_is_edp = false;
  10540. intel_lvds_init(dev);
  10541. if (intel_crt_present(dev))
  10542. intel_crt_init(dev);
  10543. if (HAS_DDI(dev)) {
  10544. int found;
  10545. /*
  10546. * Haswell uses DDI functions to detect digital outputs.
  10547. * On SKL pre-D0 the strap isn't connected, so we assume
  10548. * it's there.
  10549. */
  10550. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  10551. /* WaIgnoreDDIAStrap: skl */
  10552. if (found ||
  10553. (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
  10554. intel_ddi_init(dev, PORT_A);
  10555. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  10556. * register */
  10557. found = I915_READ(SFUSE_STRAP);
  10558. if (found & SFUSE_STRAP_DDIB_DETECTED)
  10559. intel_ddi_init(dev, PORT_B);
  10560. if (found & SFUSE_STRAP_DDIC_DETECTED)
  10561. intel_ddi_init(dev, PORT_C);
  10562. if (found & SFUSE_STRAP_DDID_DETECTED)
  10563. intel_ddi_init(dev, PORT_D);
  10564. } else if (HAS_PCH_SPLIT(dev)) {
  10565. int found;
  10566. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  10567. if (has_edp_a(dev))
  10568. intel_dp_init(dev, DP_A, PORT_A);
  10569. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  10570. /* PCH SDVOB multiplex with HDMIB */
  10571. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  10572. if (!found)
  10573. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  10574. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  10575. intel_dp_init(dev, PCH_DP_B, PORT_B);
  10576. }
  10577. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  10578. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  10579. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  10580. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  10581. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  10582. intel_dp_init(dev, PCH_DP_C, PORT_C);
  10583. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  10584. intel_dp_init(dev, PCH_DP_D, PORT_D);
  10585. } else if (IS_VALLEYVIEW(dev)) {
  10586. /*
  10587. * The DP_DETECTED bit is the latched state of the DDC
  10588. * SDA pin at boot. However since eDP doesn't require DDC
  10589. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  10590. * eDP ports may have been muxed to an alternate function.
  10591. * Thus we can't rely on the DP_DETECTED bit alone to detect
  10592. * eDP ports. Consult the VBT as well as DP_DETECTED to
  10593. * detect eDP ports.
  10594. */
  10595. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
  10596. !intel_dp_is_edp(dev, PORT_B))
  10597. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  10598. PORT_B);
  10599. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  10600. intel_dp_is_edp(dev, PORT_B))
  10601. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  10602. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
  10603. !intel_dp_is_edp(dev, PORT_C))
  10604. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  10605. PORT_C);
  10606. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  10607. intel_dp_is_edp(dev, PORT_C))
  10608. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  10609. if (IS_CHERRYVIEW(dev)) {
  10610. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  10611. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  10612. PORT_D);
  10613. /* eDP not supported on port D, so don't check VBT */
  10614. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  10615. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  10616. }
  10617. intel_dsi_init(dev);
  10618. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  10619. bool found = false;
  10620. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10621. DRM_DEBUG_KMS("probing SDVOB\n");
  10622. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  10623. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  10624. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  10625. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  10626. }
  10627. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  10628. intel_dp_init(dev, DP_B, PORT_B);
  10629. }
  10630. /* Before G4X SDVOC doesn't have its own detect register */
  10631. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10632. DRM_DEBUG_KMS("probing SDVOC\n");
  10633. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  10634. }
  10635. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  10636. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  10637. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  10638. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  10639. }
  10640. if (SUPPORTS_INTEGRATED_DP(dev))
  10641. intel_dp_init(dev, DP_C, PORT_C);
  10642. }
  10643. if (SUPPORTS_INTEGRATED_DP(dev) &&
  10644. (I915_READ(DP_D) & DP_DETECTED))
  10645. intel_dp_init(dev, DP_D, PORT_D);
  10646. } else if (IS_GEN2(dev))
  10647. intel_dvo_init(dev);
  10648. if (SUPPORTS_TV(dev))
  10649. intel_tv_init(dev);
  10650. /*
  10651. * FIXME: We don't have full atomic support yet, but we want to be
  10652. * able to enable/test plane updates via the atomic interface in the
  10653. * meantime. However as soon as we flip DRIVER_ATOMIC on, the DRM core
  10654. * will take some atomic codepaths to lookup properties during
  10655. * drmModeGetConnector() that unconditionally dereference
  10656. * connector->state.
  10657. *
  10658. * We create a dummy connector state here for each connector to ensure
  10659. * the DRM core doesn't try to dereference a NULL connector->state.
  10660. * The actual connector properties will never be updated or contain
  10661. * useful information, but since we're doing this specifically for
  10662. * testing/debug of the plane operations (and only when a specific
  10663. * kernel module option is given), that shouldn't really matter.
  10664. *
  10665. * Once atomic support for crtc's + connectors lands, this loop should
  10666. * be removed since we'll be setting up real connector state, which
  10667. * will contain Intel-specific properties.
  10668. */
  10669. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  10670. list_for_each_entry(connector,
  10671. &dev->mode_config.connector_list,
  10672. head) {
  10673. if (!WARN_ON(connector->state)) {
  10674. connector->state =
  10675. kzalloc(sizeof(*connector->state),
  10676. GFP_KERNEL);
  10677. }
  10678. }
  10679. }
  10680. intel_psr_init(dev);
  10681. for_each_intel_encoder(dev, encoder) {
  10682. encoder->base.possible_crtcs = encoder->crtc_mask;
  10683. encoder->base.possible_clones =
  10684. intel_encoder_clones(encoder);
  10685. }
  10686. intel_init_pch_refclk(dev);
  10687. drm_helper_move_panel_connectors_to_head(dev);
  10688. }
  10689. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  10690. {
  10691. struct drm_device *dev = fb->dev;
  10692. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10693. drm_framebuffer_cleanup(fb);
  10694. mutex_lock(&dev->struct_mutex);
  10695. WARN_ON(!intel_fb->obj->framebuffer_references--);
  10696. drm_gem_object_unreference(&intel_fb->obj->base);
  10697. mutex_unlock(&dev->struct_mutex);
  10698. kfree(intel_fb);
  10699. }
  10700. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  10701. struct drm_file *file,
  10702. unsigned int *handle)
  10703. {
  10704. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10705. struct drm_i915_gem_object *obj = intel_fb->obj;
  10706. return drm_gem_handle_create(file, &obj->base, handle);
  10707. }
  10708. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  10709. .destroy = intel_user_framebuffer_destroy,
  10710. .create_handle = intel_user_framebuffer_create_handle,
  10711. };
  10712. static
  10713. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  10714. uint32_t pixel_format)
  10715. {
  10716. u32 gen = INTEL_INFO(dev)->gen;
  10717. if (gen >= 9) {
  10718. /* "The stride in bytes must not exceed the of the size of 8K
  10719. * pixels and 32K bytes."
  10720. */
  10721. return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
  10722. } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
  10723. return 32*1024;
  10724. } else if (gen >= 4) {
  10725. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  10726. return 16*1024;
  10727. else
  10728. return 32*1024;
  10729. } else if (gen >= 3) {
  10730. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  10731. return 8*1024;
  10732. else
  10733. return 16*1024;
  10734. } else {
  10735. /* XXX DSPC is limited to 4k tiled */
  10736. return 8*1024;
  10737. }
  10738. }
  10739. static int intel_framebuffer_init(struct drm_device *dev,
  10740. struct intel_framebuffer *intel_fb,
  10741. struct drm_mode_fb_cmd2 *mode_cmd,
  10742. struct drm_i915_gem_object *obj)
  10743. {
  10744. int aligned_height;
  10745. int ret;
  10746. u32 pitch_limit, stride_alignment;
  10747. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  10748. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  10749. /* Enforce that fb modifier and tiling mode match, but only for
  10750. * X-tiled. This is needed for FBC. */
  10751. if (!!(obj->tiling_mode == I915_TILING_X) !=
  10752. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  10753. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  10754. return -EINVAL;
  10755. }
  10756. } else {
  10757. if (obj->tiling_mode == I915_TILING_X)
  10758. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  10759. else if (obj->tiling_mode == I915_TILING_Y) {
  10760. DRM_DEBUG("No Y tiling for legacy addfb\n");
  10761. return -EINVAL;
  10762. }
  10763. }
  10764. /* Passed in modifier sanity checking. */
  10765. switch (mode_cmd->modifier[0]) {
  10766. case I915_FORMAT_MOD_Y_TILED:
  10767. case I915_FORMAT_MOD_Yf_TILED:
  10768. if (INTEL_INFO(dev)->gen < 9) {
  10769. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  10770. mode_cmd->modifier[0]);
  10771. return -EINVAL;
  10772. }
  10773. case DRM_FORMAT_MOD_NONE:
  10774. case I915_FORMAT_MOD_X_TILED:
  10775. break;
  10776. default:
  10777. DRM_ERROR("Unsupported fb modifier 0x%llx!\n",
  10778. mode_cmd->modifier[0]);
  10779. return -EINVAL;
  10780. }
  10781. stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
  10782. mode_cmd->pixel_format);
  10783. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  10784. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  10785. mode_cmd->pitches[0], stride_alignment);
  10786. return -EINVAL;
  10787. }
  10788. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  10789. mode_cmd->pixel_format);
  10790. if (mode_cmd->pitches[0] > pitch_limit) {
  10791. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  10792. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  10793. "tiled" : "linear",
  10794. mode_cmd->pitches[0], pitch_limit);
  10795. return -EINVAL;
  10796. }
  10797. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  10798. mode_cmd->pitches[0] != obj->stride) {
  10799. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  10800. mode_cmd->pitches[0], obj->stride);
  10801. return -EINVAL;
  10802. }
  10803. /* Reject formats not supported by any plane early. */
  10804. switch (mode_cmd->pixel_format) {
  10805. case DRM_FORMAT_C8:
  10806. case DRM_FORMAT_RGB565:
  10807. case DRM_FORMAT_XRGB8888:
  10808. case DRM_FORMAT_ARGB8888:
  10809. break;
  10810. case DRM_FORMAT_XRGB1555:
  10811. case DRM_FORMAT_ARGB1555:
  10812. if (INTEL_INFO(dev)->gen > 3) {
  10813. DRM_DEBUG("unsupported pixel format: %s\n",
  10814. drm_get_format_name(mode_cmd->pixel_format));
  10815. return -EINVAL;
  10816. }
  10817. break;
  10818. case DRM_FORMAT_XBGR8888:
  10819. case DRM_FORMAT_ABGR8888:
  10820. case DRM_FORMAT_XRGB2101010:
  10821. case DRM_FORMAT_ARGB2101010:
  10822. case DRM_FORMAT_XBGR2101010:
  10823. case DRM_FORMAT_ABGR2101010:
  10824. if (INTEL_INFO(dev)->gen < 4) {
  10825. DRM_DEBUG("unsupported pixel format: %s\n",
  10826. drm_get_format_name(mode_cmd->pixel_format));
  10827. return -EINVAL;
  10828. }
  10829. break;
  10830. case DRM_FORMAT_YUYV:
  10831. case DRM_FORMAT_UYVY:
  10832. case DRM_FORMAT_YVYU:
  10833. case DRM_FORMAT_VYUY:
  10834. if (INTEL_INFO(dev)->gen < 5) {
  10835. DRM_DEBUG("unsupported pixel format: %s\n",
  10836. drm_get_format_name(mode_cmd->pixel_format));
  10837. return -EINVAL;
  10838. }
  10839. break;
  10840. default:
  10841. DRM_DEBUG("unsupported pixel format: %s\n",
  10842. drm_get_format_name(mode_cmd->pixel_format));
  10843. return -EINVAL;
  10844. }
  10845. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  10846. if (mode_cmd->offsets[0] != 0)
  10847. return -EINVAL;
  10848. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  10849. mode_cmd->pixel_format,
  10850. mode_cmd->modifier[0]);
  10851. /* FIXME drm helper for size checks (especially planar formats)? */
  10852. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  10853. return -EINVAL;
  10854. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  10855. intel_fb->obj = obj;
  10856. intel_fb->obj->framebuffer_references++;
  10857. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  10858. if (ret) {
  10859. DRM_ERROR("framebuffer init failed %d\n", ret);
  10860. return ret;
  10861. }
  10862. return 0;
  10863. }
  10864. static struct drm_framebuffer *
  10865. intel_user_framebuffer_create(struct drm_device *dev,
  10866. struct drm_file *filp,
  10867. struct drm_mode_fb_cmd2 *mode_cmd)
  10868. {
  10869. struct drm_i915_gem_object *obj;
  10870. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  10871. mode_cmd->handles[0]));
  10872. if (&obj->base == NULL)
  10873. return ERR_PTR(-ENOENT);
  10874. return intel_framebuffer_create(dev, mode_cmd, obj);
  10875. }
  10876. #ifndef CONFIG_DRM_I915_FBDEV
  10877. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  10878. {
  10879. }
  10880. #endif
  10881. static const struct drm_mode_config_funcs intel_mode_funcs = {
  10882. .fb_create = intel_user_framebuffer_create,
  10883. .output_poll_changed = intel_fbdev_output_poll_changed,
  10884. .atomic_check = intel_atomic_check,
  10885. .atomic_commit = intel_atomic_commit,
  10886. };
  10887. /* Set up chip specific display functions */
  10888. static void intel_init_display(struct drm_device *dev)
  10889. {
  10890. struct drm_i915_private *dev_priv = dev->dev_private;
  10891. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  10892. dev_priv->display.find_dpll = g4x_find_best_dpll;
  10893. else if (IS_CHERRYVIEW(dev))
  10894. dev_priv->display.find_dpll = chv_find_best_dpll;
  10895. else if (IS_VALLEYVIEW(dev))
  10896. dev_priv->display.find_dpll = vlv_find_best_dpll;
  10897. else if (IS_PINEVIEW(dev))
  10898. dev_priv->display.find_dpll = pnv_find_best_dpll;
  10899. else
  10900. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  10901. if (INTEL_INFO(dev)->gen >= 9) {
  10902. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  10903. dev_priv->display.get_initial_plane_config =
  10904. skylake_get_initial_plane_config;
  10905. dev_priv->display.crtc_compute_clock =
  10906. haswell_crtc_compute_clock;
  10907. dev_priv->display.crtc_enable = haswell_crtc_enable;
  10908. dev_priv->display.crtc_disable = haswell_crtc_disable;
  10909. dev_priv->display.off = ironlake_crtc_off;
  10910. dev_priv->display.update_primary_plane =
  10911. skylake_update_primary_plane;
  10912. } else if (HAS_DDI(dev)) {
  10913. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  10914. dev_priv->display.get_initial_plane_config =
  10915. ironlake_get_initial_plane_config;
  10916. dev_priv->display.crtc_compute_clock =
  10917. haswell_crtc_compute_clock;
  10918. dev_priv->display.crtc_enable = haswell_crtc_enable;
  10919. dev_priv->display.crtc_disable = haswell_crtc_disable;
  10920. dev_priv->display.off = ironlake_crtc_off;
  10921. dev_priv->display.update_primary_plane =
  10922. ironlake_update_primary_plane;
  10923. } else if (HAS_PCH_SPLIT(dev)) {
  10924. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  10925. dev_priv->display.get_initial_plane_config =
  10926. ironlake_get_initial_plane_config;
  10927. dev_priv->display.crtc_compute_clock =
  10928. ironlake_crtc_compute_clock;
  10929. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  10930. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  10931. dev_priv->display.off = ironlake_crtc_off;
  10932. dev_priv->display.update_primary_plane =
  10933. ironlake_update_primary_plane;
  10934. } else if (IS_VALLEYVIEW(dev)) {
  10935. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10936. dev_priv->display.get_initial_plane_config =
  10937. i9xx_get_initial_plane_config;
  10938. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  10939. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  10940. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10941. dev_priv->display.off = i9xx_crtc_off;
  10942. dev_priv->display.update_primary_plane =
  10943. i9xx_update_primary_plane;
  10944. } else {
  10945. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10946. dev_priv->display.get_initial_plane_config =
  10947. i9xx_get_initial_plane_config;
  10948. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  10949. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  10950. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10951. dev_priv->display.off = i9xx_crtc_off;
  10952. dev_priv->display.update_primary_plane =
  10953. i9xx_update_primary_plane;
  10954. }
  10955. /* Returns the core display clock speed */
  10956. if (IS_VALLEYVIEW(dev))
  10957. dev_priv->display.get_display_clock_speed =
  10958. valleyview_get_display_clock_speed;
  10959. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  10960. dev_priv->display.get_display_clock_speed =
  10961. i945_get_display_clock_speed;
  10962. else if (IS_I915G(dev))
  10963. dev_priv->display.get_display_clock_speed =
  10964. i915_get_display_clock_speed;
  10965. else if (IS_I945GM(dev) || IS_845G(dev))
  10966. dev_priv->display.get_display_clock_speed =
  10967. i9xx_misc_get_display_clock_speed;
  10968. else if (IS_PINEVIEW(dev))
  10969. dev_priv->display.get_display_clock_speed =
  10970. pnv_get_display_clock_speed;
  10971. else if (IS_I915GM(dev))
  10972. dev_priv->display.get_display_clock_speed =
  10973. i915gm_get_display_clock_speed;
  10974. else if (IS_I865G(dev))
  10975. dev_priv->display.get_display_clock_speed =
  10976. i865_get_display_clock_speed;
  10977. else if (IS_I85X(dev))
  10978. dev_priv->display.get_display_clock_speed =
  10979. i855_get_display_clock_speed;
  10980. else /* 852, 830 */
  10981. dev_priv->display.get_display_clock_speed =
  10982. i830_get_display_clock_speed;
  10983. if (IS_GEN5(dev)) {
  10984. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  10985. } else if (IS_GEN6(dev)) {
  10986. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  10987. } else if (IS_IVYBRIDGE(dev)) {
  10988. /* FIXME: detect B0+ stepping and use auto training */
  10989. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  10990. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  10991. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  10992. } else if (IS_VALLEYVIEW(dev)) {
  10993. dev_priv->display.modeset_global_resources =
  10994. valleyview_modeset_global_resources;
  10995. }
  10996. switch (INTEL_INFO(dev)->gen) {
  10997. case 2:
  10998. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  10999. break;
  11000. case 3:
  11001. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  11002. break;
  11003. case 4:
  11004. case 5:
  11005. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  11006. break;
  11007. case 6:
  11008. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  11009. break;
  11010. case 7:
  11011. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  11012. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  11013. break;
  11014. case 9:
  11015. /* Drop through - unsupported since execlist only. */
  11016. default:
  11017. /* Default just returns -ENODEV to indicate unsupported */
  11018. dev_priv->display.queue_flip = intel_default_queue_flip;
  11019. }
  11020. intel_panel_init_backlight_funcs(dev);
  11021. mutex_init(&dev_priv->pps_mutex);
  11022. }
  11023. /*
  11024. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  11025. * resume, or other times. This quirk makes sure that's the case for
  11026. * affected systems.
  11027. */
  11028. static void quirk_pipea_force(struct drm_device *dev)
  11029. {
  11030. struct drm_i915_private *dev_priv = dev->dev_private;
  11031. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  11032. DRM_INFO("applying pipe a force quirk\n");
  11033. }
  11034. static void quirk_pipeb_force(struct drm_device *dev)
  11035. {
  11036. struct drm_i915_private *dev_priv = dev->dev_private;
  11037. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  11038. DRM_INFO("applying pipe b force quirk\n");
  11039. }
  11040. /*
  11041. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  11042. */
  11043. static void quirk_ssc_force_disable(struct drm_device *dev)
  11044. {
  11045. struct drm_i915_private *dev_priv = dev->dev_private;
  11046. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  11047. DRM_INFO("applying lvds SSC disable quirk\n");
  11048. }
  11049. /*
  11050. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  11051. * brightness value
  11052. */
  11053. static void quirk_invert_brightness(struct drm_device *dev)
  11054. {
  11055. struct drm_i915_private *dev_priv = dev->dev_private;
  11056. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  11057. DRM_INFO("applying inverted panel brightness quirk\n");
  11058. }
  11059. /* Some VBT's incorrectly indicate no backlight is present */
  11060. static void quirk_backlight_present(struct drm_device *dev)
  11061. {
  11062. struct drm_i915_private *dev_priv = dev->dev_private;
  11063. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  11064. DRM_INFO("applying backlight present quirk\n");
  11065. }
  11066. struct intel_quirk {
  11067. int device;
  11068. int subsystem_vendor;
  11069. int subsystem_device;
  11070. void (*hook)(struct drm_device *dev);
  11071. };
  11072. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  11073. struct intel_dmi_quirk {
  11074. void (*hook)(struct drm_device *dev);
  11075. const struct dmi_system_id (*dmi_id_list)[];
  11076. };
  11077. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  11078. {
  11079. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  11080. return 1;
  11081. }
  11082. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  11083. {
  11084. .dmi_id_list = &(const struct dmi_system_id[]) {
  11085. {
  11086. .callback = intel_dmi_reverse_brightness,
  11087. .ident = "NCR Corporation",
  11088. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  11089. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  11090. },
  11091. },
  11092. { } /* terminating entry */
  11093. },
  11094. .hook = quirk_invert_brightness,
  11095. },
  11096. };
  11097. static struct intel_quirk intel_quirks[] = {
  11098. /* HP Mini needs pipe A force quirk (LP: #322104) */
  11099. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  11100. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  11101. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  11102. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  11103. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  11104. /* 830 needs to leave pipe A & dpll A up */
  11105. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  11106. /* 830 needs to leave pipe B & dpll B up */
  11107. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  11108. /* Lenovo U160 cannot use SSC on LVDS */
  11109. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  11110. /* Sony Vaio Y cannot use SSC on LVDS */
  11111. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  11112. /* Acer Aspire 5734Z must invert backlight brightness */
  11113. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  11114. /* Acer/eMachines G725 */
  11115. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  11116. /* Acer/eMachines e725 */
  11117. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  11118. /* Acer/Packard Bell NCL20 */
  11119. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  11120. /* Acer Aspire 4736Z */
  11121. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  11122. /* Acer Aspire 5336 */
  11123. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  11124. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  11125. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  11126. /* Acer C720 Chromebook (Core i3 4005U) */
  11127. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  11128. /* Apple Macbook 2,1 (Core 2 T7400) */
  11129. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  11130. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  11131. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  11132. /* HP Chromebook 14 (Celeron 2955U) */
  11133. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  11134. /* Dell Chromebook 11 */
  11135. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  11136. };
  11137. static void intel_init_quirks(struct drm_device *dev)
  11138. {
  11139. struct pci_dev *d = dev->pdev;
  11140. int i;
  11141. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  11142. struct intel_quirk *q = &intel_quirks[i];
  11143. if (d->device == q->device &&
  11144. (d->subsystem_vendor == q->subsystem_vendor ||
  11145. q->subsystem_vendor == PCI_ANY_ID) &&
  11146. (d->subsystem_device == q->subsystem_device ||
  11147. q->subsystem_device == PCI_ANY_ID))
  11148. q->hook(dev);
  11149. }
  11150. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  11151. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  11152. intel_dmi_quirks[i].hook(dev);
  11153. }
  11154. }
  11155. /* Disable the VGA plane that we never use */
  11156. static void i915_disable_vga(struct drm_device *dev)
  11157. {
  11158. struct drm_i915_private *dev_priv = dev->dev_private;
  11159. u8 sr1;
  11160. u32 vga_reg = i915_vgacntrl_reg(dev);
  11161. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  11162. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  11163. outb(SR01, VGA_SR_INDEX);
  11164. sr1 = inb(VGA_SR_DATA);
  11165. outb(sr1 | 1<<5, VGA_SR_DATA);
  11166. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  11167. udelay(300);
  11168. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  11169. POSTING_READ(vga_reg);
  11170. }
  11171. void intel_modeset_init_hw(struct drm_device *dev)
  11172. {
  11173. intel_prepare_ddi(dev);
  11174. if (IS_VALLEYVIEW(dev))
  11175. vlv_update_cdclk(dev);
  11176. intel_init_clock_gating(dev);
  11177. intel_enable_gt_powersave(dev);
  11178. }
  11179. void intel_modeset_init(struct drm_device *dev)
  11180. {
  11181. struct drm_i915_private *dev_priv = dev->dev_private;
  11182. int sprite, ret;
  11183. enum pipe pipe;
  11184. struct intel_crtc *crtc;
  11185. drm_mode_config_init(dev);
  11186. dev->mode_config.min_width = 0;
  11187. dev->mode_config.min_height = 0;
  11188. dev->mode_config.preferred_depth = 24;
  11189. dev->mode_config.prefer_shadow = 1;
  11190. dev->mode_config.allow_fb_modifiers = true;
  11191. dev->mode_config.funcs = &intel_mode_funcs;
  11192. intel_init_quirks(dev);
  11193. intel_init_pm(dev);
  11194. if (INTEL_INFO(dev)->num_pipes == 0)
  11195. return;
  11196. intel_init_display(dev);
  11197. intel_init_audio(dev);
  11198. if (IS_GEN2(dev)) {
  11199. dev->mode_config.max_width = 2048;
  11200. dev->mode_config.max_height = 2048;
  11201. } else if (IS_GEN3(dev)) {
  11202. dev->mode_config.max_width = 4096;
  11203. dev->mode_config.max_height = 4096;
  11204. } else {
  11205. dev->mode_config.max_width = 8192;
  11206. dev->mode_config.max_height = 8192;
  11207. }
  11208. if (IS_845G(dev) || IS_I865G(dev)) {
  11209. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  11210. dev->mode_config.cursor_height = 1023;
  11211. } else if (IS_GEN2(dev)) {
  11212. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  11213. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  11214. } else {
  11215. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  11216. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  11217. }
  11218. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  11219. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  11220. INTEL_INFO(dev)->num_pipes,
  11221. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  11222. for_each_pipe(dev_priv, pipe) {
  11223. intel_crtc_init(dev, pipe);
  11224. for_each_sprite(dev_priv, pipe, sprite) {
  11225. ret = intel_plane_init(dev, pipe, sprite);
  11226. if (ret)
  11227. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  11228. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  11229. }
  11230. }
  11231. intel_init_dpio(dev);
  11232. intel_shared_dpll_init(dev);
  11233. /* Just disable it once at startup */
  11234. i915_disable_vga(dev);
  11235. intel_setup_outputs(dev);
  11236. /* Just in case the BIOS is doing something questionable. */
  11237. intel_fbc_disable(dev);
  11238. drm_modeset_lock_all(dev);
  11239. intel_modeset_setup_hw_state(dev, false);
  11240. drm_modeset_unlock_all(dev);
  11241. for_each_intel_crtc(dev, crtc) {
  11242. if (!crtc->active)
  11243. continue;
  11244. /*
  11245. * Note that reserving the BIOS fb up front prevents us
  11246. * from stuffing other stolen allocations like the ring
  11247. * on top. This prevents some ugliness at boot time, and
  11248. * can even allow for smooth boot transitions if the BIOS
  11249. * fb is large enough for the active pipe configuration.
  11250. */
  11251. if (dev_priv->display.get_initial_plane_config) {
  11252. dev_priv->display.get_initial_plane_config(crtc,
  11253. &crtc->plane_config);
  11254. /*
  11255. * If the fb is shared between multiple heads, we'll
  11256. * just get the first one.
  11257. */
  11258. intel_find_plane_obj(crtc, &crtc->plane_config);
  11259. }
  11260. }
  11261. }
  11262. static void intel_enable_pipe_a(struct drm_device *dev)
  11263. {
  11264. struct intel_connector *connector;
  11265. struct drm_connector *crt = NULL;
  11266. struct intel_load_detect_pipe load_detect_temp;
  11267. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  11268. /* We can't just switch on the pipe A, we need to set things up with a
  11269. * proper mode and output configuration. As a gross hack, enable pipe A
  11270. * by enabling the load detect pipe once. */
  11271. for_each_intel_connector(dev, connector) {
  11272. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  11273. crt = &connector->base;
  11274. break;
  11275. }
  11276. }
  11277. if (!crt)
  11278. return;
  11279. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  11280. intel_release_load_detect_pipe(crt, &load_detect_temp);
  11281. }
  11282. static bool
  11283. intel_check_plane_mapping(struct intel_crtc *crtc)
  11284. {
  11285. struct drm_device *dev = crtc->base.dev;
  11286. struct drm_i915_private *dev_priv = dev->dev_private;
  11287. u32 reg, val;
  11288. if (INTEL_INFO(dev)->num_pipes == 1)
  11289. return true;
  11290. reg = DSPCNTR(!crtc->plane);
  11291. val = I915_READ(reg);
  11292. if ((val & DISPLAY_PLANE_ENABLE) &&
  11293. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  11294. return false;
  11295. return true;
  11296. }
  11297. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  11298. {
  11299. struct drm_device *dev = crtc->base.dev;
  11300. struct drm_i915_private *dev_priv = dev->dev_private;
  11301. u32 reg;
  11302. /* Clear any frame start delays used for debugging left by the BIOS */
  11303. reg = PIPECONF(crtc->config->cpu_transcoder);
  11304. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  11305. /* restore vblank interrupts to correct state */
  11306. drm_crtc_vblank_reset(&crtc->base);
  11307. if (crtc->active) {
  11308. update_scanline_offset(crtc);
  11309. drm_crtc_vblank_on(&crtc->base);
  11310. }
  11311. /* We need to sanitize the plane -> pipe mapping first because this will
  11312. * disable the crtc (and hence change the state) if it is wrong. Note
  11313. * that gen4+ has a fixed plane -> pipe mapping. */
  11314. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  11315. struct intel_connector *connector;
  11316. bool plane;
  11317. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  11318. crtc->base.base.id);
  11319. /* Pipe has the wrong plane attached and the plane is active.
  11320. * Temporarily change the plane mapping and disable everything
  11321. * ... */
  11322. plane = crtc->plane;
  11323. crtc->plane = !plane;
  11324. crtc->primary_enabled = true;
  11325. dev_priv->display.crtc_disable(&crtc->base);
  11326. crtc->plane = plane;
  11327. /* ... and break all links. */
  11328. for_each_intel_connector(dev, connector) {
  11329. if (connector->encoder->base.crtc != &crtc->base)
  11330. continue;
  11331. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11332. connector->base.encoder = NULL;
  11333. }
  11334. /* multiple connectors may have the same encoder:
  11335. * handle them and break crtc link separately */
  11336. for_each_intel_connector(dev, connector)
  11337. if (connector->encoder->base.crtc == &crtc->base) {
  11338. connector->encoder->base.crtc = NULL;
  11339. connector->encoder->connectors_active = false;
  11340. }
  11341. WARN_ON(crtc->active);
  11342. crtc->base.state->enable = false;
  11343. crtc->base.enabled = false;
  11344. }
  11345. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  11346. crtc->pipe == PIPE_A && !crtc->active) {
  11347. /* BIOS forgot to enable pipe A, this mostly happens after
  11348. * resume. Force-enable the pipe to fix this, the update_dpms
  11349. * call below we restore the pipe to the right state, but leave
  11350. * the required bits on. */
  11351. intel_enable_pipe_a(dev);
  11352. }
  11353. /* Adjust the state of the output pipe according to whether we
  11354. * have active connectors/encoders. */
  11355. intel_crtc_update_dpms(&crtc->base);
  11356. if (crtc->active != crtc->base.state->enable) {
  11357. struct intel_encoder *encoder;
  11358. /* This can happen either due to bugs in the get_hw_state
  11359. * functions or because the pipe is force-enabled due to the
  11360. * pipe A quirk. */
  11361. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  11362. crtc->base.base.id,
  11363. crtc->base.state->enable ? "enabled" : "disabled",
  11364. crtc->active ? "enabled" : "disabled");
  11365. crtc->base.state->enable = crtc->active;
  11366. crtc->base.enabled = crtc->active;
  11367. /* Because we only establish the connector -> encoder ->
  11368. * crtc links if something is active, this means the
  11369. * crtc is now deactivated. Break the links. connector
  11370. * -> encoder links are only establish when things are
  11371. * actually up, hence no need to break them. */
  11372. WARN_ON(crtc->active);
  11373. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  11374. WARN_ON(encoder->connectors_active);
  11375. encoder->base.crtc = NULL;
  11376. }
  11377. }
  11378. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  11379. /*
  11380. * We start out with underrun reporting disabled to avoid races.
  11381. * For correct bookkeeping mark this on active crtcs.
  11382. *
  11383. * Also on gmch platforms we dont have any hardware bits to
  11384. * disable the underrun reporting. Which means we need to start
  11385. * out with underrun reporting disabled also on inactive pipes,
  11386. * since otherwise we'll complain about the garbage we read when
  11387. * e.g. coming up after runtime pm.
  11388. *
  11389. * No protection against concurrent access is required - at
  11390. * worst a fifo underrun happens which also sets this to false.
  11391. */
  11392. crtc->cpu_fifo_underrun_disabled = true;
  11393. crtc->pch_fifo_underrun_disabled = true;
  11394. }
  11395. }
  11396. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  11397. {
  11398. struct intel_connector *connector;
  11399. struct drm_device *dev = encoder->base.dev;
  11400. /* We need to check both for a crtc link (meaning that the
  11401. * encoder is active and trying to read from a pipe) and the
  11402. * pipe itself being active. */
  11403. bool has_active_crtc = encoder->base.crtc &&
  11404. to_intel_crtc(encoder->base.crtc)->active;
  11405. if (encoder->connectors_active && !has_active_crtc) {
  11406. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  11407. encoder->base.base.id,
  11408. encoder->base.name);
  11409. /* Connector is active, but has no active pipe. This is
  11410. * fallout from our resume register restoring. Disable
  11411. * the encoder manually again. */
  11412. if (encoder->base.crtc) {
  11413. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  11414. encoder->base.base.id,
  11415. encoder->base.name);
  11416. encoder->disable(encoder);
  11417. if (encoder->post_disable)
  11418. encoder->post_disable(encoder);
  11419. }
  11420. encoder->base.crtc = NULL;
  11421. encoder->connectors_active = false;
  11422. /* Inconsistent output/port/pipe state happens presumably due to
  11423. * a bug in one of the get_hw_state functions. Or someplace else
  11424. * in our code, like the register restore mess on resume. Clamp
  11425. * things to off as a safer default. */
  11426. for_each_intel_connector(dev, connector) {
  11427. if (connector->encoder != encoder)
  11428. continue;
  11429. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11430. connector->base.encoder = NULL;
  11431. }
  11432. }
  11433. /* Enabled encoders without active connectors will be fixed in
  11434. * the crtc fixup. */
  11435. }
  11436. void i915_redisable_vga_power_on(struct drm_device *dev)
  11437. {
  11438. struct drm_i915_private *dev_priv = dev->dev_private;
  11439. u32 vga_reg = i915_vgacntrl_reg(dev);
  11440. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  11441. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  11442. i915_disable_vga(dev);
  11443. }
  11444. }
  11445. void i915_redisable_vga(struct drm_device *dev)
  11446. {
  11447. struct drm_i915_private *dev_priv = dev->dev_private;
  11448. /* This function can be called both from intel_modeset_setup_hw_state or
  11449. * at a very early point in our resume sequence, where the power well
  11450. * structures are not yet restored. Since this function is at a very
  11451. * paranoid "someone might have enabled VGA while we were not looking"
  11452. * level, just check if the power well is enabled instead of trying to
  11453. * follow the "don't touch the power well if we don't need it" policy
  11454. * the rest of the driver uses. */
  11455. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  11456. return;
  11457. i915_redisable_vga_power_on(dev);
  11458. }
  11459. static bool primary_get_hw_state(struct intel_crtc *crtc)
  11460. {
  11461. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  11462. if (!crtc->active)
  11463. return false;
  11464. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  11465. }
  11466. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  11467. {
  11468. struct drm_i915_private *dev_priv = dev->dev_private;
  11469. enum pipe pipe;
  11470. struct intel_crtc *crtc;
  11471. struct intel_encoder *encoder;
  11472. struct intel_connector *connector;
  11473. int i;
  11474. for_each_intel_crtc(dev, crtc) {
  11475. memset(crtc->config, 0, sizeof(*crtc->config));
  11476. crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  11477. crtc->active = dev_priv->display.get_pipe_config(crtc,
  11478. crtc->config);
  11479. crtc->base.state->enable = crtc->active;
  11480. crtc->base.enabled = crtc->active;
  11481. crtc->primary_enabled = primary_get_hw_state(crtc);
  11482. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  11483. crtc->base.base.id,
  11484. crtc->active ? "enabled" : "disabled");
  11485. }
  11486. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11487. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11488. pll->on = pll->get_hw_state(dev_priv, pll,
  11489. &pll->config.hw_state);
  11490. pll->active = 0;
  11491. pll->config.crtc_mask = 0;
  11492. for_each_intel_crtc(dev, crtc) {
  11493. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  11494. pll->active++;
  11495. pll->config.crtc_mask |= 1 << crtc->pipe;
  11496. }
  11497. }
  11498. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  11499. pll->name, pll->config.crtc_mask, pll->on);
  11500. if (pll->config.crtc_mask)
  11501. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  11502. }
  11503. for_each_intel_encoder(dev, encoder) {
  11504. pipe = 0;
  11505. if (encoder->get_hw_state(encoder, &pipe)) {
  11506. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11507. encoder->base.crtc = &crtc->base;
  11508. encoder->get_config(encoder, crtc->config);
  11509. } else {
  11510. encoder->base.crtc = NULL;
  11511. }
  11512. encoder->connectors_active = false;
  11513. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  11514. encoder->base.base.id,
  11515. encoder->base.name,
  11516. encoder->base.crtc ? "enabled" : "disabled",
  11517. pipe_name(pipe));
  11518. }
  11519. for_each_intel_connector(dev, connector) {
  11520. if (connector->get_hw_state(connector)) {
  11521. connector->base.dpms = DRM_MODE_DPMS_ON;
  11522. connector->encoder->connectors_active = true;
  11523. connector->base.encoder = &connector->encoder->base;
  11524. } else {
  11525. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11526. connector->base.encoder = NULL;
  11527. }
  11528. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  11529. connector->base.base.id,
  11530. connector->base.name,
  11531. connector->base.encoder ? "enabled" : "disabled");
  11532. }
  11533. }
  11534. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  11535. * and i915 state tracking structures. */
  11536. void intel_modeset_setup_hw_state(struct drm_device *dev,
  11537. bool force_restore)
  11538. {
  11539. struct drm_i915_private *dev_priv = dev->dev_private;
  11540. enum pipe pipe;
  11541. struct intel_crtc *crtc;
  11542. struct intel_encoder *encoder;
  11543. int i;
  11544. intel_modeset_readout_hw_state(dev);
  11545. /*
  11546. * Now that we have the config, copy it to each CRTC struct
  11547. * Note that this could go away if we move to using crtc_config
  11548. * checking everywhere.
  11549. */
  11550. for_each_intel_crtc(dev, crtc) {
  11551. if (crtc->active && i915.fastboot) {
  11552. intel_mode_from_pipe_config(&crtc->base.mode,
  11553. crtc->config);
  11554. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  11555. crtc->base.base.id);
  11556. drm_mode_debug_printmodeline(&crtc->base.mode);
  11557. }
  11558. }
  11559. /* HW state is read out, now we need to sanitize this mess. */
  11560. for_each_intel_encoder(dev, encoder) {
  11561. intel_sanitize_encoder(encoder);
  11562. }
  11563. for_each_pipe(dev_priv, pipe) {
  11564. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11565. intel_sanitize_crtc(crtc);
  11566. intel_dump_pipe_config(crtc, crtc->config,
  11567. "[setup_hw_state]");
  11568. }
  11569. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11570. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11571. if (!pll->on || pll->active)
  11572. continue;
  11573. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  11574. pll->disable(dev_priv, pll);
  11575. pll->on = false;
  11576. }
  11577. if (IS_GEN9(dev))
  11578. skl_wm_get_hw_state(dev);
  11579. else if (HAS_PCH_SPLIT(dev))
  11580. ilk_wm_get_hw_state(dev);
  11581. if (force_restore) {
  11582. i915_redisable_vga(dev);
  11583. /*
  11584. * We need to use raw interfaces for restoring state to avoid
  11585. * checking (bogus) intermediate states.
  11586. */
  11587. for_each_pipe(dev_priv, pipe) {
  11588. struct drm_crtc *crtc =
  11589. dev_priv->pipe_to_crtc_mapping[pipe];
  11590. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
  11591. crtc->primary->fb);
  11592. }
  11593. } else {
  11594. intel_modeset_update_staged_output_state(dev);
  11595. }
  11596. intel_modeset_check_state(dev);
  11597. }
  11598. void intel_modeset_gem_init(struct drm_device *dev)
  11599. {
  11600. struct drm_i915_private *dev_priv = dev->dev_private;
  11601. struct drm_crtc *c;
  11602. struct drm_i915_gem_object *obj;
  11603. mutex_lock(&dev->struct_mutex);
  11604. intel_init_gt_powersave(dev);
  11605. mutex_unlock(&dev->struct_mutex);
  11606. /*
  11607. * There may be no VBT; and if the BIOS enabled SSC we can
  11608. * just keep using it to avoid unnecessary flicker. Whereas if the
  11609. * BIOS isn't using it, don't assume it will work even if the VBT
  11610. * indicates as much.
  11611. */
  11612. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11613. dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  11614. DREF_SSC1_ENABLE);
  11615. intel_modeset_init_hw(dev);
  11616. intel_setup_overlay(dev);
  11617. /*
  11618. * Make sure any fbs we allocated at startup are properly
  11619. * pinned & fenced. When we do the allocation it's too early
  11620. * for this.
  11621. */
  11622. mutex_lock(&dev->struct_mutex);
  11623. for_each_crtc(dev, c) {
  11624. obj = intel_fb_obj(c->primary->fb);
  11625. if (obj == NULL)
  11626. continue;
  11627. if (intel_pin_and_fence_fb_obj(c->primary,
  11628. c->primary->fb,
  11629. NULL)) {
  11630. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  11631. to_intel_crtc(c)->pipe);
  11632. drm_framebuffer_unreference(c->primary->fb);
  11633. c->primary->fb = NULL;
  11634. update_state_fb(c->primary);
  11635. }
  11636. }
  11637. mutex_unlock(&dev->struct_mutex);
  11638. intel_backlight_register(dev);
  11639. }
  11640. void intel_connector_unregister(struct intel_connector *intel_connector)
  11641. {
  11642. struct drm_connector *connector = &intel_connector->base;
  11643. intel_panel_destroy_backlight(connector);
  11644. drm_connector_unregister(connector);
  11645. }
  11646. void intel_modeset_cleanup(struct drm_device *dev)
  11647. {
  11648. struct drm_i915_private *dev_priv = dev->dev_private;
  11649. struct drm_connector *connector;
  11650. intel_disable_gt_powersave(dev);
  11651. intel_backlight_unregister(dev);
  11652. /*
  11653. * Interrupts and polling as the first thing to avoid creating havoc.
  11654. * Too much stuff here (turning of connectors, ...) would
  11655. * experience fancy races otherwise.
  11656. */
  11657. intel_irq_uninstall(dev_priv);
  11658. /*
  11659. * Due to the hpd irq storm handling the hotplug work can re-arm the
  11660. * poll handlers. Hence disable polling after hpd handling is shut down.
  11661. */
  11662. drm_kms_helper_poll_fini(dev);
  11663. mutex_lock(&dev->struct_mutex);
  11664. intel_unregister_dsm_handler();
  11665. intel_fbc_disable(dev);
  11666. mutex_unlock(&dev->struct_mutex);
  11667. /* flush any delayed tasks or pending work */
  11668. flush_scheduled_work();
  11669. /* destroy the backlight and sysfs files before encoders/connectors */
  11670. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  11671. struct intel_connector *intel_connector;
  11672. intel_connector = to_intel_connector(connector);
  11673. intel_connector->unregister(intel_connector);
  11674. }
  11675. drm_mode_config_cleanup(dev);
  11676. intel_cleanup_overlay(dev);
  11677. mutex_lock(&dev->struct_mutex);
  11678. intel_cleanup_gt_powersave(dev);
  11679. mutex_unlock(&dev->struct_mutex);
  11680. }
  11681. /*
  11682. * Return which encoder is currently attached for connector.
  11683. */
  11684. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  11685. {
  11686. return &intel_attached_encoder(connector)->base;
  11687. }
  11688. void intel_connector_attach_encoder(struct intel_connector *connector,
  11689. struct intel_encoder *encoder)
  11690. {
  11691. connector->encoder = encoder;
  11692. drm_mode_connector_attach_encoder(&connector->base,
  11693. &encoder->base);
  11694. }
  11695. /*
  11696. * set vga decode state - true == enable VGA decode
  11697. */
  11698. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  11699. {
  11700. struct drm_i915_private *dev_priv = dev->dev_private;
  11701. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  11702. u16 gmch_ctrl;
  11703. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  11704. DRM_ERROR("failed to read control word\n");
  11705. return -EIO;
  11706. }
  11707. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  11708. return 0;
  11709. if (state)
  11710. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  11711. else
  11712. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  11713. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  11714. DRM_ERROR("failed to write control word\n");
  11715. return -EIO;
  11716. }
  11717. return 0;
  11718. }
  11719. struct intel_display_error_state {
  11720. u32 power_well_driver;
  11721. int num_transcoders;
  11722. struct intel_cursor_error_state {
  11723. u32 control;
  11724. u32 position;
  11725. u32 base;
  11726. u32 size;
  11727. } cursor[I915_MAX_PIPES];
  11728. struct intel_pipe_error_state {
  11729. bool power_domain_on;
  11730. u32 source;
  11731. u32 stat;
  11732. } pipe[I915_MAX_PIPES];
  11733. struct intel_plane_error_state {
  11734. u32 control;
  11735. u32 stride;
  11736. u32 size;
  11737. u32 pos;
  11738. u32 addr;
  11739. u32 surface;
  11740. u32 tile_offset;
  11741. } plane[I915_MAX_PIPES];
  11742. struct intel_transcoder_error_state {
  11743. bool power_domain_on;
  11744. enum transcoder cpu_transcoder;
  11745. u32 conf;
  11746. u32 htotal;
  11747. u32 hblank;
  11748. u32 hsync;
  11749. u32 vtotal;
  11750. u32 vblank;
  11751. u32 vsync;
  11752. } transcoder[4];
  11753. };
  11754. struct intel_display_error_state *
  11755. intel_display_capture_error_state(struct drm_device *dev)
  11756. {
  11757. struct drm_i915_private *dev_priv = dev->dev_private;
  11758. struct intel_display_error_state *error;
  11759. int transcoders[] = {
  11760. TRANSCODER_A,
  11761. TRANSCODER_B,
  11762. TRANSCODER_C,
  11763. TRANSCODER_EDP,
  11764. };
  11765. int i;
  11766. if (INTEL_INFO(dev)->num_pipes == 0)
  11767. return NULL;
  11768. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  11769. if (error == NULL)
  11770. return NULL;
  11771. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11772. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  11773. for_each_pipe(dev_priv, i) {
  11774. error->pipe[i].power_domain_on =
  11775. __intel_display_power_is_enabled(dev_priv,
  11776. POWER_DOMAIN_PIPE(i));
  11777. if (!error->pipe[i].power_domain_on)
  11778. continue;
  11779. error->cursor[i].control = I915_READ(CURCNTR(i));
  11780. error->cursor[i].position = I915_READ(CURPOS(i));
  11781. error->cursor[i].base = I915_READ(CURBASE(i));
  11782. error->plane[i].control = I915_READ(DSPCNTR(i));
  11783. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  11784. if (INTEL_INFO(dev)->gen <= 3) {
  11785. error->plane[i].size = I915_READ(DSPSIZE(i));
  11786. error->plane[i].pos = I915_READ(DSPPOS(i));
  11787. }
  11788. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11789. error->plane[i].addr = I915_READ(DSPADDR(i));
  11790. if (INTEL_INFO(dev)->gen >= 4) {
  11791. error->plane[i].surface = I915_READ(DSPSURF(i));
  11792. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  11793. }
  11794. error->pipe[i].source = I915_READ(PIPESRC(i));
  11795. if (HAS_GMCH_DISPLAY(dev))
  11796. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  11797. }
  11798. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  11799. if (HAS_DDI(dev_priv->dev))
  11800. error->num_transcoders++; /* Account for eDP. */
  11801. for (i = 0; i < error->num_transcoders; i++) {
  11802. enum transcoder cpu_transcoder = transcoders[i];
  11803. error->transcoder[i].power_domain_on =
  11804. __intel_display_power_is_enabled(dev_priv,
  11805. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  11806. if (!error->transcoder[i].power_domain_on)
  11807. continue;
  11808. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  11809. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  11810. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  11811. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  11812. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  11813. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  11814. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  11815. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  11816. }
  11817. return error;
  11818. }
  11819. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  11820. void
  11821. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  11822. struct drm_device *dev,
  11823. struct intel_display_error_state *error)
  11824. {
  11825. struct drm_i915_private *dev_priv = dev->dev_private;
  11826. int i;
  11827. if (!error)
  11828. return;
  11829. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  11830. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11831. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  11832. error->power_well_driver);
  11833. for_each_pipe(dev_priv, i) {
  11834. err_printf(m, "Pipe [%d]:\n", i);
  11835. err_printf(m, " Power: %s\n",
  11836. error->pipe[i].power_domain_on ? "on" : "off");
  11837. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  11838. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  11839. err_printf(m, "Plane [%d]:\n", i);
  11840. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  11841. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  11842. if (INTEL_INFO(dev)->gen <= 3) {
  11843. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  11844. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  11845. }
  11846. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11847. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  11848. if (INTEL_INFO(dev)->gen >= 4) {
  11849. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  11850. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  11851. }
  11852. err_printf(m, "Cursor [%d]:\n", i);
  11853. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  11854. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  11855. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  11856. }
  11857. for (i = 0; i < error->num_transcoders; i++) {
  11858. err_printf(m, "CPU transcoder: %c\n",
  11859. transcoder_name(error->transcoder[i].cpu_transcoder));
  11860. err_printf(m, " Power: %s\n",
  11861. error->transcoder[i].power_domain_on ? "on" : "off");
  11862. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  11863. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  11864. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  11865. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  11866. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  11867. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  11868. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  11869. }
  11870. }
  11871. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  11872. {
  11873. struct intel_crtc *crtc;
  11874. for_each_intel_crtc(dev, crtc) {
  11875. struct intel_unpin_work *work;
  11876. spin_lock_irq(&dev->event_lock);
  11877. work = crtc->unpin_work;
  11878. if (work && work->event &&
  11879. work->event->base.file_priv == file) {
  11880. kfree(work->event);
  11881. work->event = NULL;
  11882. }
  11883. spin_unlock_irq(&dev->event_lock);
  11884. }
  11885. }