intel_display.c 391 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166
  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. return intel_crtc->active && crtc->primary->fb &&
  797. intel_crtc->config->base.adjusted_mode.crtc_clock;
  798. }
  799. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  800. enum pipe pipe)
  801. {
  802. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  803. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  804. return intel_crtc->config->cpu_transcoder;
  805. }
  806. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  807. {
  808. struct drm_i915_private *dev_priv = dev->dev_private;
  809. u32 reg = PIPEDSL(pipe);
  810. u32 line1, line2;
  811. u32 line_mask;
  812. if (IS_GEN2(dev))
  813. line_mask = DSL_LINEMASK_GEN2;
  814. else
  815. line_mask = DSL_LINEMASK_GEN3;
  816. line1 = I915_READ(reg) & line_mask;
  817. mdelay(5);
  818. line2 = I915_READ(reg) & line_mask;
  819. return line1 == line2;
  820. }
  821. /*
  822. * intel_wait_for_pipe_off - wait for pipe to turn off
  823. * @crtc: crtc whose pipe to wait for
  824. *
  825. * After disabling a pipe, we can't wait for vblank in the usual way,
  826. * spinning on the vblank interrupt status bit, since we won't actually
  827. * see an interrupt when the pipe is disabled.
  828. *
  829. * On Gen4 and above:
  830. * wait for the pipe register state bit to turn off
  831. *
  832. * Otherwise:
  833. * wait for the display line value to settle (it usually
  834. * ends up stopping at the start of the next frame).
  835. *
  836. */
  837. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  838. {
  839. struct drm_device *dev = crtc->base.dev;
  840. struct drm_i915_private *dev_priv = dev->dev_private;
  841. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  842. enum pipe pipe = crtc->pipe;
  843. if (INTEL_INFO(dev)->gen >= 4) {
  844. int reg = PIPECONF(cpu_transcoder);
  845. /* Wait for the Pipe State to go off */
  846. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  847. 100))
  848. WARN(1, "pipe_off wait timed out\n");
  849. } else {
  850. /* Wait for the display line to settle */
  851. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  852. WARN(1, "pipe_off wait timed out\n");
  853. }
  854. }
  855. /*
  856. * ibx_digital_port_connected - is the specified port connected?
  857. * @dev_priv: i915 private structure
  858. * @port: the port to test
  859. *
  860. * Returns true if @port is connected, false otherwise.
  861. */
  862. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  863. struct intel_digital_port *port)
  864. {
  865. u32 bit;
  866. if (HAS_PCH_IBX(dev_priv->dev)) {
  867. switch (port->port) {
  868. case PORT_B:
  869. bit = SDE_PORTB_HOTPLUG;
  870. break;
  871. case PORT_C:
  872. bit = SDE_PORTC_HOTPLUG;
  873. break;
  874. case PORT_D:
  875. bit = SDE_PORTD_HOTPLUG;
  876. break;
  877. default:
  878. return true;
  879. }
  880. } else {
  881. switch (port->port) {
  882. case PORT_B:
  883. bit = SDE_PORTB_HOTPLUG_CPT;
  884. break;
  885. case PORT_C:
  886. bit = SDE_PORTC_HOTPLUG_CPT;
  887. break;
  888. case PORT_D:
  889. bit = SDE_PORTD_HOTPLUG_CPT;
  890. break;
  891. default:
  892. return true;
  893. }
  894. }
  895. return I915_READ(SDEISR) & bit;
  896. }
  897. static const char *state_string(bool enabled)
  898. {
  899. return enabled ? "on" : "off";
  900. }
  901. /* Only for pre-ILK configs */
  902. void assert_pll(struct drm_i915_private *dev_priv,
  903. enum pipe pipe, bool state)
  904. {
  905. int reg;
  906. u32 val;
  907. bool cur_state;
  908. reg = DPLL(pipe);
  909. val = I915_READ(reg);
  910. cur_state = !!(val & DPLL_VCO_ENABLE);
  911. I915_STATE_WARN(cur_state != state,
  912. "PLL state assertion failure (expected %s, current %s)\n",
  913. state_string(state), state_string(cur_state));
  914. }
  915. /* XXX: the dsi pll is shared between MIPI DSI ports */
  916. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  917. {
  918. u32 val;
  919. bool cur_state;
  920. mutex_lock(&dev_priv->dpio_lock);
  921. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  922. mutex_unlock(&dev_priv->dpio_lock);
  923. cur_state = val & DSI_PLL_VCO_EN;
  924. I915_STATE_WARN(cur_state != state,
  925. "DSI PLL state assertion failure (expected %s, current %s)\n",
  926. state_string(state), state_string(cur_state));
  927. }
  928. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  929. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  930. struct intel_shared_dpll *
  931. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  932. {
  933. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  934. if (crtc->config->shared_dpll < 0)
  935. return NULL;
  936. return &dev_priv->shared_dplls[crtc->config->shared_dpll];
  937. }
  938. /* For ILK+ */
  939. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  940. struct intel_shared_dpll *pll,
  941. bool state)
  942. {
  943. bool cur_state;
  944. struct intel_dpll_hw_state hw_state;
  945. if (WARN (!pll,
  946. "asserting DPLL %s with no DPLL\n", state_string(state)))
  947. return;
  948. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  949. I915_STATE_WARN(cur_state != state,
  950. "%s assertion failure (expected %s, current %s)\n",
  951. pll->name, state_string(state), state_string(cur_state));
  952. }
  953. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  954. enum pipe pipe, bool state)
  955. {
  956. int reg;
  957. u32 val;
  958. bool cur_state;
  959. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  960. pipe);
  961. if (HAS_DDI(dev_priv->dev)) {
  962. /* DDI does not have a specific FDI_TX register */
  963. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  964. val = I915_READ(reg);
  965. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  966. } else {
  967. reg = FDI_TX_CTL(pipe);
  968. val = I915_READ(reg);
  969. cur_state = !!(val & FDI_TX_ENABLE);
  970. }
  971. I915_STATE_WARN(cur_state != state,
  972. "FDI TX state assertion failure (expected %s, current %s)\n",
  973. state_string(state), state_string(cur_state));
  974. }
  975. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  976. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  977. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  978. enum pipe pipe, bool state)
  979. {
  980. int reg;
  981. u32 val;
  982. bool cur_state;
  983. reg = FDI_RX_CTL(pipe);
  984. val = I915_READ(reg);
  985. cur_state = !!(val & FDI_RX_ENABLE);
  986. I915_STATE_WARN(cur_state != state,
  987. "FDI RX state assertion failure (expected %s, current %s)\n",
  988. state_string(state), state_string(cur_state));
  989. }
  990. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  991. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  992. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  993. enum pipe pipe)
  994. {
  995. int reg;
  996. u32 val;
  997. /* ILK FDI PLL is always enabled */
  998. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  999. return;
  1000. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1001. if (HAS_DDI(dev_priv->dev))
  1002. return;
  1003. reg = FDI_TX_CTL(pipe);
  1004. val = I915_READ(reg);
  1005. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1006. }
  1007. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1008. enum pipe pipe, bool state)
  1009. {
  1010. int reg;
  1011. u32 val;
  1012. bool cur_state;
  1013. reg = FDI_RX_CTL(pipe);
  1014. val = I915_READ(reg);
  1015. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1016. I915_STATE_WARN(cur_state != state,
  1017. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1018. state_string(state), state_string(cur_state));
  1019. }
  1020. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1021. enum pipe pipe)
  1022. {
  1023. struct drm_device *dev = dev_priv->dev;
  1024. int pp_reg;
  1025. u32 val;
  1026. enum pipe panel_pipe = PIPE_A;
  1027. bool locked = true;
  1028. if (WARN_ON(HAS_DDI(dev)))
  1029. return;
  1030. if (HAS_PCH_SPLIT(dev)) {
  1031. u32 port_sel;
  1032. pp_reg = PCH_PP_CONTROL;
  1033. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1034. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1035. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1036. panel_pipe = PIPE_B;
  1037. /* XXX: else fix for eDP */
  1038. } else if (IS_VALLEYVIEW(dev)) {
  1039. /* presumably write lock depends on pipe, not port select */
  1040. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1041. panel_pipe = pipe;
  1042. } else {
  1043. pp_reg = PP_CONTROL;
  1044. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1045. panel_pipe = PIPE_B;
  1046. }
  1047. val = I915_READ(pp_reg);
  1048. if (!(val & PANEL_POWER_ON) ||
  1049. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1050. locked = false;
  1051. I915_STATE_WARN(panel_pipe == pipe && locked,
  1052. "panel assertion failure, pipe %c regs locked\n",
  1053. pipe_name(pipe));
  1054. }
  1055. static void assert_cursor(struct drm_i915_private *dev_priv,
  1056. enum pipe pipe, bool state)
  1057. {
  1058. struct drm_device *dev = dev_priv->dev;
  1059. bool cur_state;
  1060. if (IS_845G(dev) || IS_I865G(dev))
  1061. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1062. else
  1063. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1064. I915_STATE_WARN(cur_state != state,
  1065. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1066. pipe_name(pipe), state_string(state), state_string(cur_state));
  1067. }
  1068. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1069. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1070. void assert_pipe(struct drm_i915_private *dev_priv,
  1071. enum pipe pipe, bool state)
  1072. {
  1073. int reg;
  1074. u32 val;
  1075. bool cur_state;
  1076. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1077. pipe);
  1078. /* if we need the pipe quirk it must be always on */
  1079. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1080. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1081. state = true;
  1082. if (!intel_display_power_is_enabled(dev_priv,
  1083. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1084. cur_state = false;
  1085. } else {
  1086. reg = PIPECONF(cpu_transcoder);
  1087. val = I915_READ(reg);
  1088. cur_state = !!(val & PIPECONF_ENABLE);
  1089. }
  1090. I915_STATE_WARN(cur_state != state,
  1091. "pipe %c assertion failure (expected %s, current %s)\n",
  1092. pipe_name(pipe), state_string(state), state_string(cur_state));
  1093. }
  1094. static void assert_plane(struct drm_i915_private *dev_priv,
  1095. enum plane plane, bool state)
  1096. {
  1097. int reg;
  1098. u32 val;
  1099. bool cur_state;
  1100. reg = DSPCNTR(plane);
  1101. val = I915_READ(reg);
  1102. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1103. I915_STATE_WARN(cur_state != state,
  1104. "plane %c assertion failure (expected %s, current %s)\n",
  1105. plane_name(plane), state_string(state), state_string(cur_state));
  1106. }
  1107. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1108. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1109. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1110. enum pipe pipe)
  1111. {
  1112. struct drm_device *dev = dev_priv->dev;
  1113. int reg, i;
  1114. u32 val;
  1115. int cur_pipe;
  1116. /* Primary planes are fixed to pipes on gen4+ */
  1117. if (INTEL_INFO(dev)->gen >= 4) {
  1118. reg = DSPCNTR(pipe);
  1119. val = I915_READ(reg);
  1120. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1121. "plane %c assertion failure, should be disabled but not\n",
  1122. plane_name(pipe));
  1123. return;
  1124. }
  1125. /* Need to check both planes against the pipe */
  1126. for_each_pipe(dev_priv, i) {
  1127. reg = DSPCNTR(i);
  1128. val = I915_READ(reg);
  1129. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1130. DISPPLANE_SEL_PIPE_SHIFT;
  1131. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1132. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1133. plane_name(i), pipe_name(pipe));
  1134. }
  1135. }
  1136. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1137. enum pipe pipe)
  1138. {
  1139. struct drm_device *dev = dev_priv->dev;
  1140. int reg, sprite;
  1141. u32 val;
  1142. if (INTEL_INFO(dev)->gen >= 9) {
  1143. for_each_sprite(dev_priv, pipe, sprite) {
  1144. val = I915_READ(PLANE_CTL(pipe, sprite));
  1145. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1146. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1147. sprite, pipe_name(pipe));
  1148. }
  1149. } else if (IS_VALLEYVIEW(dev)) {
  1150. for_each_sprite(dev_priv, pipe, sprite) {
  1151. reg = SPCNTR(pipe, sprite);
  1152. val = I915_READ(reg);
  1153. I915_STATE_WARN(val & SP_ENABLE,
  1154. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1155. sprite_name(pipe, sprite), pipe_name(pipe));
  1156. }
  1157. } else if (INTEL_INFO(dev)->gen >= 7) {
  1158. reg = SPRCTL(pipe);
  1159. val = I915_READ(reg);
  1160. I915_STATE_WARN(val & SPRITE_ENABLE,
  1161. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1162. plane_name(pipe), pipe_name(pipe));
  1163. } else if (INTEL_INFO(dev)->gen >= 5) {
  1164. reg = DVSCNTR(pipe);
  1165. val = I915_READ(reg);
  1166. I915_STATE_WARN(val & DVS_ENABLE,
  1167. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1168. plane_name(pipe), pipe_name(pipe));
  1169. }
  1170. }
  1171. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1172. {
  1173. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1174. drm_crtc_vblank_put(crtc);
  1175. }
  1176. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1177. {
  1178. u32 val;
  1179. bool enabled;
  1180. I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1181. val = I915_READ(PCH_DREF_CONTROL);
  1182. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1183. DREF_SUPERSPREAD_SOURCE_MASK));
  1184. I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1185. }
  1186. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1187. enum pipe pipe)
  1188. {
  1189. int reg;
  1190. u32 val;
  1191. bool enabled;
  1192. reg = PCH_TRANSCONF(pipe);
  1193. val = I915_READ(reg);
  1194. enabled = !!(val & TRANS_ENABLE);
  1195. I915_STATE_WARN(enabled,
  1196. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1197. pipe_name(pipe));
  1198. }
  1199. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1200. enum pipe pipe, u32 port_sel, u32 val)
  1201. {
  1202. if ((val & DP_PORT_EN) == 0)
  1203. return false;
  1204. if (HAS_PCH_CPT(dev_priv->dev)) {
  1205. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1206. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1207. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1208. return false;
  1209. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1210. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1211. return false;
  1212. } else {
  1213. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1214. return false;
  1215. }
  1216. return true;
  1217. }
  1218. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1219. enum pipe pipe, u32 val)
  1220. {
  1221. if ((val & SDVO_ENABLE) == 0)
  1222. return false;
  1223. if (HAS_PCH_CPT(dev_priv->dev)) {
  1224. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1225. return false;
  1226. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1227. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1228. return false;
  1229. } else {
  1230. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1231. return false;
  1232. }
  1233. return true;
  1234. }
  1235. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1236. enum pipe pipe, u32 val)
  1237. {
  1238. if ((val & LVDS_PORT_EN) == 0)
  1239. return false;
  1240. if (HAS_PCH_CPT(dev_priv->dev)) {
  1241. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1242. return false;
  1243. } else {
  1244. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1245. return false;
  1246. }
  1247. return true;
  1248. }
  1249. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1250. enum pipe pipe, u32 val)
  1251. {
  1252. if ((val & ADPA_DAC_ENABLE) == 0)
  1253. return false;
  1254. if (HAS_PCH_CPT(dev_priv->dev)) {
  1255. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1256. return false;
  1257. } else {
  1258. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1259. return false;
  1260. }
  1261. return true;
  1262. }
  1263. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1264. enum pipe pipe, int reg, u32 port_sel)
  1265. {
  1266. u32 val = I915_READ(reg);
  1267. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1268. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1269. reg, pipe_name(pipe));
  1270. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1271. && (val & DP_PIPEB_SELECT),
  1272. "IBX PCH dp port still using transcoder B\n");
  1273. }
  1274. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1275. enum pipe pipe, int reg)
  1276. {
  1277. u32 val = I915_READ(reg);
  1278. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1279. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1280. reg, pipe_name(pipe));
  1281. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1282. && (val & SDVO_PIPE_B_SELECT),
  1283. "IBX PCH hdmi port still using transcoder B\n");
  1284. }
  1285. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1286. enum pipe pipe)
  1287. {
  1288. int reg;
  1289. u32 val;
  1290. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1291. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1292. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1293. reg = PCH_ADPA;
  1294. val = I915_READ(reg);
  1295. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1296. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1297. pipe_name(pipe));
  1298. reg = PCH_LVDS;
  1299. val = I915_READ(reg);
  1300. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1301. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1302. pipe_name(pipe));
  1303. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1304. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1305. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1306. }
  1307. static void intel_init_dpio(struct drm_device *dev)
  1308. {
  1309. struct drm_i915_private *dev_priv = dev->dev_private;
  1310. if (!IS_VALLEYVIEW(dev))
  1311. return;
  1312. /*
  1313. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1314. * CHV x1 PHY (DP/HDMI D)
  1315. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1316. */
  1317. if (IS_CHERRYVIEW(dev)) {
  1318. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1319. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1320. } else {
  1321. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1322. }
  1323. }
  1324. static void vlv_enable_pll(struct intel_crtc *crtc,
  1325. const struct intel_crtc_state *pipe_config)
  1326. {
  1327. struct drm_device *dev = crtc->base.dev;
  1328. struct drm_i915_private *dev_priv = dev->dev_private;
  1329. int reg = DPLL(crtc->pipe);
  1330. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1331. assert_pipe_disabled(dev_priv, crtc->pipe);
  1332. /* No really, not for ILK+ */
  1333. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1334. /* PLL is protected by panel, make sure we can write it */
  1335. if (IS_MOBILE(dev_priv->dev))
  1336. assert_panel_unlocked(dev_priv, crtc->pipe);
  1337. I915_WRITE(reg, dpll);
  1338. POSTING_READ(reg);
  1339. udelay(150);
  1340. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1341. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1342. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1343. POSTING_READ(DPLL_MD(crtc->pipe));
  1344. /* We do this three times for luck */
  1345. I915_WRITE(reg, dpll);
  1346. POSTING_READ(reg);
  1347. udelay(150); /* wait for warmup */
  1348. I915_WRITE(reg, dpll);
  1349. POSTING_READ(reg);
  1350. udelay(150); /* wait for warmup */
  1351. I915_WRITE(reg, dpll);
  1352. POSTING_READ(reg);
  1353. udelay(150); /* wait for warmup */
  1354. }
  1355. static void chv_enable_pll(struct intel_crtc *crtc,
  1356. const struct intel_crtc_state *pipe_config)
  1357. {
  1358. struct drm_device *dev = crtc->base.dev;
  1359. struct drm_i915_private *dev_priv = dev->dev_private;
  1360. int pipe = crtc->pipe;
  1361. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1362. u32 tmp;
  1363. assert_pipe_disabled(dev_priv, crtc->pipe);
  1364. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1365. mutex_lock(&dev_priv->dpio_lock);
  1366. /* Enable back the 10bit clock to display controller */
  1367. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1368. tmp |= DPIO_DCLKP_EN;
  1369. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1370. /*
  1371. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1372. */
  1373. udelay(1);
  1374. /* Enable PLL */
  1375. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1376. /* Check PLL is locked */
  1377. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1378. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1379. /* not sure when this should be written */
  1380. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1381. POSTING_READ(DPLL_MD(pipe));
  1382. mutex_unlock(&dev_priv->dpio_lock);
  1383. }
  1384. static int intel_num_dvo_pipes(struct drm_device *dev)
  1385. {
  1386. struct intel_crtc *crtc;
  1387. int count = 0;
  1388. for_each_intel_crtc(dev, crtc)
  1389. count += crtc->active &&
  1390. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1391. return count;
  1392. }
  1393. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1394. {
  1395. struct drm_device *dev = crtc->base.dev;
  1396. struct drm_i915_private *dev_priv = dev->dev_private;
  1397. int reg = DPLL(crtc->pipe);
  1398. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1399. assert_pipe_disabled(dev_priv, crtc->pipe);
  1400. /* No really, not for ILK+ */
  1401. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1402. /* PLL is protected by panel, make sure we can write it */
  1403. if (IS_MOBILE(dev) && !IS_I830(dev))
  1404. assert_panel_unlocked(dev_priv, crtc->pipe);
  1405. /* Enable DVO 2x clock on both PLLs if necessary */
  1406. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1407. /*
  1408. * It appears to be important that we don't enable this
  1409. * for the current pipe before otherwise configuring the
  1410. * PLL. No idea how this should be handled if multiple
  1411. * DVO outputs are enabled simultaneosly.
  1412. */
  1413. dpll |= DPLL_DVO_2X_MODE;
  1414. I915_WRITE(DPLL(!crtc->pipe),
  1415. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1416. }
  1417. /* Wait for the clocks to stabilize. */
  1418. POSTING_READ(reg);
  1419. udelay(150);
  1420. if (INTEL_INFO(dev)->gen >= 4) {
  1421. I915_WRITE(DPLL_MD(crtc->pipe),
  1422. crtc->config->dpll_hw_state.dpll_md);
  1423. } else {
  1424. /* The pixel multiplier can only be updated once the
  1425. * DPLL is enabled and the clocks are stable.
  1426. *
  1427. * So write it again.
  1428. */
  1429. I915_WRITE(reg, dpll);
  1430. }
  1431. /* We do this three times for luck */
  1432. I915_WRITE(reg, dpll);
  1433. POSTING_READ(reg);
  1434. udelay(150); /* wait for warmup */
  1435. I915_WRITE(reg, dpll);
  1436. POSTING_READ(reg);
  1437. udelay(150); /* wait for warmup */
  1438. I915_WRITE(reg, dpll);
  1439. POSTING_READ(reg);
  1440. udelay(150); /* wait for warmup */
  1441. }
  1442. /**
  1443. * i9xx_disable_pll - disable a PLL
  1444. * @dev_priv: i915 private structure
  1445. * @pipe: pipe PLL to disable
  1446. *
  1447. * Disable the PLL for @pipe, making sure the pipe is off first.
  1448. *
  1449. * Note! This is for pre-ILK only.
  1450. */
  1451. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1452. {
  1453. struct drm_device *dev = crtc->base.dev;
  1454. struct drm_i915_private *dev_priv = dev->dev_private;
  1455. enum pipe pipe = crtc->pipe;
  1456. /* Disable DVO 2x clock on both PLLs if necessary */
  1457. if (IS_I830(dev) &&
  1458. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1459. intel_num_dvo_pipes(dev) == 1) {
  1460. I915_WRITE(DPLL(PIPE_B),
  1461. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1462. I915_WRITE(DPLL(PIPE_A),
  1463. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1464. }
  1465. /* Don't disable pipe or pipe PLLs if needed */
  1466. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1467. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1468. return;
  1469. /* Make sure the pipe isn't still relying on us */
  1470. assert_pipe_disabled(dev_priv, pipe);
  1471. I915_WRITE(DPLL(pipe), 0);
  1472. POSTING_READ(DPLL(pipe));
  1473. }
  1474. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1475. {
  1476. u32 val = 0;
  1477. /* Make sure the pipe isn't still relying on us */
  1478. assert_pipe_disabled(dev_priv, pipe);
  1479. /*
  1480. * Leave integrated clock source and reference clock enabled for pipe B.
  1481. * The latter is needed for VGA hotplug / manual detection.
  1482. */
  1483. if (pipe == PIPE_B)
  1484. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1485. I915_WRITE(DPLL(pipe), val);
  1486. POSTING_READ(DPLL(pipe));
  1487. }
  1488. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1489. {
  1490. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1491. u32 val;
  1492. /* Make sure the pipe isn't still relying on us */
  1493. assert_pipe_disabled(dev_priv, pipe);
  1494. /* Set PLL en = 0 */
  1495. val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
  1496. if (pipe != PIPE_A)
  1497. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1498. I915_WRITE(DPLL(pipe), val);
  1499. POSTING_READ(DPLL(pipe));
  1500. mutex_lock(&dev_priv->dpio_lock);
  1501. /* Disable 10bit clock to display controller */
  1502. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1503. val &= ~DPIO_DCLKP_EN;
  1504. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1505. /* disable left/right clock distribution */
  1506. if (pipe != PIPE_B) {
  1507. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1508. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1509. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1510. } else {
  1511. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1512. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1513. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1514. }
  1515. mutex_unlock(&dev_priv->dpio_lock);
  1516. }
  1517. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1518. struct intel_digital_port *dport)
  1519. {
  1520. u32 port_mask;
  1521. int dpll_reg;
  1522. switch (dport->port) {
  1523. case PORT_B:
  1524. port_mask = DPLL_PORTB_READY_MASK;
  1525. dpll_reg = DPLL(0);
  1526. break;
  1527. case PORT_C:
  1528. port_mask = DPLL_PORTC_READY_MASK;
  1529. dpll_reg = DPLL(0);
  1530. break;
  1531. case PORT_D:
  1532. port_mask = DPLL_PORTD_READY_MASK;
  1533. dpll_reg = DPIO_PHY_STATUS;
  1534. break;
  1535. default:
  1536. BUG();
  1537. }
  1538. if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
  1539. WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
  1540. port_name(dport->port), I915_READ(dpll_reg));
  1541. }
  1542. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1543. {
  1544. struct drm_device *dev = crtc->base.dev;
  1545. struct drm_i915_private *dev_priv = dev->dev_private;
  1546. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1547. if (WARN_ON(pll == NULL))
  1548. return;
  1549. WARN_ON(!pll->config.crtc_mask);
  1550. if (pll->active == 0) {
  1551. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1552. WARN_ON(pll->on);
  1553. assert_shared_dpll_disabled(dev_priv, pll);
  1554. pll->mode_set(dev_priv, pll);
  1555. }
  1556. }
  1557. /**
  1558. * intel_enable_shared_dpll - enable PCH PLL
  1559. * @dev_priv: i915 private structure
  1560. * @pipe: pipe PLL to enable
  1561. *
  1562. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1563. * drives the transcoder clock.
  1564. */
  1565. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1566. {
  1567. struct drm_device *dev = crtc->base.dev;
  1568. struct drm_i915_private *dev_priv = dev->dev_private;
  1569. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1570. if (WARN_ON(pll == NULL))
  1571. return;
  1572. if (WARN_ON(pll->config.crtc_mask == 0))
  1573. return;
  1574. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1575. pll->name, pll->active, pll->on,
  1576. crtc->base.base.id);
  1577. if (pll->active++) {
  1578. WARN_ON(!pll->on);
  1579. assert_shared_dpll_enabled(dev_priv, pll);
  1580. return;
  1581. }
  1582. WARN_ON(pll->on);
  1583. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1584. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1585. pll->enable(dev_priv, pll);
  1586. pll->on = true;
  1587. }
  1588. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1589. {
  1590. struct drm_device *dev = crtc->base.dev;
  1591. struct drm_i915_private *dev_priv = dev->dev_private;
  1592. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1593. /* PCH only available on ILK+ */
  1594. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1595. if (WARN_ON(pll == NULL))
  1596. return;
  1597. if (WARN_ON(pll->config.crtc_mask == 0))
  1598. return;
  1599. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1600. pll->name, pll->active, pll->on,
  1601. crtc->base.base.id);
  1602. if (WARN_ON(pll->active == 0)) {
  1603. assert_shared_dpll_disabled(dev_priv, pll);
  1604. return;
  1605. }
  1606. assert_shared_dpll_enabled(dev_priv, pll);
  1607. WARN_ON(!pll->on);
  1608. if (--pll->active)
  1609. return;
  1610. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1611. pll->disable(dev_priv, pll);
  1612. pll->on = false;
  1613. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1614. }
  1615. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1616. enum pipe pipe)
  1617. {
  1618. struct drm_device *dev = dev_priv->dev;
  1619. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1620. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1621. uint32_t reg, val, pipeconf_val;
  1622. /* PCH only available on ILK+ */
  1623. BUG_ON(!HAS_PCH_SPLIT(dev));
  1624. /* Make sure PCH DPLL is enabled */
  1625. assert_shared_dpll_enabled(dev_priv,
  1626. intel_crtc_to_shared_dpll(intel_crtc));
  1627. /* FDI must be feeding us bits for PCH ports */
  1628. assert_fdi_tx_enabled(dev_priv, pipe);
  1629. assert_fdi_rx_enabled(dev_priv, pipe);
  1630. if (HAS_PCH_CPT(dev)) {
  1631. /* Workaround: Set the timing override bit before enabling the
  1632. * pch transcoder. */
  1633. reg = TRANS_CHICKEN2(pipe);
  1634. val = I915_READ(reg);
  1635. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1636. I915_WRITE(reg, val);
  1637. }
  1638. reg = PCH_TRANSCONF(pipe);
  1639. val = I915_READ(reg);
  1640. pipeconf_val = I915_READ(PIPECONF(pipe));
  1641. if (HAS_PCH_IBX(dev_priv->dev)) {
  1642. /*
  1643. * make the BPC in transcoder be consistent with
  1644. * that in pipeconf reg.
  1645. */
  1646. val &= ~PIPECONF_BPC_MASK;
  1647. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1648. }
  1649. val &= ~TRANS_INTERLACE_MASK;
  1650. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1651. if (HAS_PCH_IBX(dev_priv->dev) &&
  1652. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1653. val |= TRANS_LEGACY_INTERLACED_ILK;
  1654. else
  1655. val |= TRANS_INTERLACED;
  1656. else
  1657. val |= TRANS_PROGRESSIVE;
  1658. I915_WRITE(reg, val | TRANS_ENABLE);
  1659. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1660. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1661. }
  1662. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1663. enum transcoder cpu_transcoder)
  1664. {
  1665. u32 val, pipeconf_val;
  1666. /* PCH only available on ILK+ */
  1667. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1668. /* FDI must be feeding us bits for PCH ports */
  1669. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1670. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1671. /* Workaround: set timing override bit. */
  1672. val = I915_READ(_TRANSA_CHICKEN2);
  1673. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1674. I915_WRITE(_TRANSA_CHICKEN2, val);
  1675. val = TRANS_ENABLE;
  1676. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1677. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1678. PIPECONF_INTERLACED_ILK)
  1679. val |= TRANS_INTERLACED;
  1680. else
  1681. val |= TRANS_PROGRESSIVE;
  1682. I915_WRITE(LPT_TRANSCONF, val);
  1683. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1684. DRM_ERROR("Failed to enable PCH transcoder\n");
  1685. }
  1686. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1687. enum pipe pipe)
  1688. {
  1689. struct drm_device *dev = dev_priv->dev;
  1690. uint32_t reg, val;
  1691. /* FDI relies on the transcoder */
  1692. assert_fdi_tx_disabled(dev_priv, pipe);
  1693. assert_fdi_rx_disabled(dev_priv, pipe);
  1694. /* Ports must be off as well */
  1695. assert_pch_ports_disabled(dev_priv, pipe);
  1696. reg = PCH_TRANSCONF(pipe);
  1697. val = I915_READ(reg);
  1698. val &= ~TRANS_ENABLE;
  1699. I915_WRITE(reg, val);
  1700. /* wait for PCH transcoder off, transcoder state */
  1701. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1702. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1703. if (!HAS_PCH_IBX(dev)) {
  1704. /* Workaround: Clear the timing override chicken bit again. */
  1705. reg = TRANS_CHICKEN2(pipe);
  1706. val = I915_READ(reg);
  1707. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1708. I915_WRITE(reg, val);
  1709. }
  1710. }
  1711. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1712. {
  1713. u32 val;
  1714. val = I915_READ(LPT_TRANSCONF);
  1715. val &= ~TRANS_ENABLE;
  1716. I915_WRITE(LPT_TRANSCONF, val);
  1717. /* wait for PCH transcoder off, transcoder state */
  1718. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1719. DRM_ERROR("Failed to disable PCH transcoder\n");
  1720. /* Workaround: clear timing override bit. */
  1721. val = I915_READ(_TRANSA_CHICKEN2);
  1722. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1723. I915_WRITE(_TRANSA_CHICKEN2, val);
  1724. }
  1725. /**
  1726. * intel_enable_pipe - enable a pipe, asserting requirements
  1727. * @crtc: crtc responsible for the pipe
  1728. *
  1729. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1730. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1731. */
  1732. static void intel_enable_pipe(struct intel_crtc *crtc)
  1733. {
  1734. struct drm_device *dev = crtc->base.dev;
  1735. struct drm_i915_private *dev_priv = dev->dev_private;
  1736. enum pipe pipe = crtc->pipe;
  1737. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1738. pipe);
  1739. enum pipe pch_transcoder;
  1740. int reg;
  1741. u32 val;
  1742. assert_planes_disabled(dev_priv, pipe);
  1743. assert_cursor_disabled(dev_priv, pipe);
  1744. assert_sprites_disabled(dev_priv, pipe);
  1745. if (HAS_PCH_LPT(dev_priv->dev))
  1746. pch_transcoder = TRANSCODER_A;
  1747. else
  1748. pch_transcoder = pipe;
  1749. /*
  1750. * A pipe without a PLL won't actually be able to drive bits from
  1751. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1752. * need the check.
  1753. */
  1754. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1755. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1756. assert_dsi_pll_enabled(dev_priv);
  1757. else
  1758. assert_pll_enabled(dev_priv, pipe);
  1759. else {
  1760. if (crtc->config->has_pch_encoder) {
  1761. /* if driving the PCH, we need FDI enabled */
  1762. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1763. assert_fdi_tx_pll_enabled(dev_priv,
  1764. (enum pipe) cpu_transcoder);
  1765. }
  1766. /* FIXME: assert CPU port conditions for SNB+ */
  1767. }
  1768. reg = PIPECONF(cpu_transcoder);
  1769. val = I915_READ(reg);
  1770. if (val & PIPECONF_ENABLE) {
  1771. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1772. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1773. return;
  1774. }
  1775. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1776. POSTING_READ(reg);
  1777. }
  1778. /**
  1779. * intel_disable_pipe - disable a pipe, asserting requirements
  1780. * @crtc: crtc whose pipes is to be disabled
  1781. *
  1782. * Disable the pipe of @crtc, making sure that various hardware
  1783. * specific requirements are met, if applicable, e.g. plane
  1784. * disabled, panel fitter off, etc.
  1785. *
  1786. * Will wait until the pipe has shut down before returning.
  1787. */
  1788. static void intel_disable_pipe(struct intel_crtc *crtc)
  1789. {
  1790. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1791. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1792. enum pipe pipe = crtc->pipe;
  1793. int reg;
  1794. u32 val;
  1795. /*
  1796. * Make sure planes won't keep trying to pump pixels to us,
  1797. * or we might hang the display.
  1798. */
  1799. assert_planes_disabled(dev_priv, pipe);
  1800. assert_cursor_disabled(dev_priv, pipe);
  1801. assert_sprites_disabled(dev_priv, pipe);
  1802. reg = PIPECONF(cpu_transcoder);
  1803. val = I915_READ(reg);
  1804. if ((val & PIPECONF_ENABLE) == 0)
  1805. return;
  1806. /*
  1807. * Double wide has implications for planes
  1808. * so best keep it disabled when not needed.
  1809. */
  1810. if (crtc->config->double_wide)
  1811. val &= ~PIPECONF_DOUBLE_WIDE;
  1812. /* Don't disable pipe or pipe PLLs if needed */
  1813. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1814. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1815. val &= ~PIPECONF_ENABLE;
  1816. I915_WRITE(reg, val);
  1817. if ((val & PIPECONF_ENABLE) == 0)
  1818. intel_wait_for_pipe_off(crtc);
  1819. }
  1820. /*
  1821. * Plane regs are double buffered, going from enabled->disabled needs a
  1822. * trigger in order to latch. The display address reg provides this.
  1823. */
  1824. void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
  1825. enum plane plane)
  1826. {
  1827. struct drm_device *dev = dev_priv->dev;
  1828. u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
  1829. I915_WRITE(reg, I915_READ(reg));
  1830. POSTING_READ(reg);
  1831. }
  1832. /**
  1833. * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  1834. * @plane: plane to be enabled
  1835. * @crtc: crtc for the plane
  1836. *
  1837. * Enable @plane on @crtc, making sure that the pipe is running first.
  1838. */
  1839. static void intel_enable_primary_hw_plane(struct drm_plane *plane,
  1840. struct drm_crtc *crtc)
  1841. {
  1842. struct drm_device *dev = plane->dev;
  1843. struct drm_i915_private *dev_priv = dev->dev_private;
  1844. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1845. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1846. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1847. if (intel_crtc->primary_enabled)
  1848. return;
  1849. intel_crtc->primary_enabled = true;
  1850. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1851. crtc->x, crtc->y);
  1852. /*
  1853. * BDW signals flip done immediately if the plane
  1854. * is disabled, even if the plane enable is already
  1855. * armed to occur at the next vblank :(
  1856. */
  1857. if (IS_BROADWELL(dev))
  1858. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1859. }
  1860. /**
  1861. * intel_disable_primary_hw_plane - disable the primary hardware plane
  1862. * @plane: plane to be disabled
  1863. * @crtc: crtc for the plane
  1864. *
  1865. * Disable @plane on @crtc, making sure that the pipe is running first.
  1866. */
  1867. static void intel_disable_primary_hw_plane(struct drm_plane *plane,
  1868. struct drm_crtc *crtc)
  1869. {
  1870. struct drm_device *dev = plane->dev;
  1871. struct drm_i915_private *dev_priv = dev->dev_private;
  1872. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1873. if (WARN_ON(!intel_crtc->active))
  1874. return;
  1875. if (!intel_crtc->primary_enabled)
  1876. return;
  1877. intel_crtc->primary_enabled = false;
  1878. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1879. crtc->x, crtc->y);
  1880. }
  1881. static bool need_vtd_wa(struct drm_device *dev)
  1882. {
  1883. #ifdef CONFIG_INTEL_IOMMU
  1884. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1885. return true;
  1886. #endif
  1887. return false;
  1888. }
  1889. int
  1890. intel_fb_align_height(struct drm_device *dev, int height,
  1891. uint32_t pixel_format,
  1892. uint64_t fb_format_modifier)
  1893. {
  1894. int tile_height;
  1895. uint32_t bits_per_pixel;
  1896. switch (fb_format_modifier) {
  1897. case DRM_FORMAT_MOD_NONE:
  1898. tile_height = 1;
  1899. break;
  1900. case I915_FORMAT_MOD_X_TILED:
  1901. tile_height = IS_GEN2(dev) ? 16 : 8;
  1902. break;
  1903. case I915_FORMAT_MOD_Y_TILED:
  1904. tile_height = 32;
  1905. break;
  1906. case I915_FORMAT_MOD_Yf_TILED:
  1907. bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
  1908. switch (bits_per_pixel) {
  1909. default:
  1910. case 8:
  1911. tile_height = 64;
  1912. break;
  1913. case 16:
  1914. case 32:
  1915. tile_height = 32;
  1916. break;
  1917. case 64:
  1918. tile_height = 16;
  1919. break;
  1920. case 128:
  1921. WARN_ONCE(1,
  1922. "128-bit pixels are not supported for display!");
  1923. tile_height = 16;
  1924. break;
  1925. }
  1926. break;
  1927. default:
  1928. MISSING_CASE(fb_format_modifier);
  1929. tile_height = 1;
  1930. break;
  1931. }
  1932. return ALIGN(height, tile_height);
  1933. }
  1934. int
  1935. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  1936. struct drm_framebuffer *fb,
  1937. struct intel_engine_cs *pipelined)
  1938. {
  1939. struct drm_device *dev = fb->dev;
  1940. struct drm_i915_private *dev_priv = dev->dev_private;
  1941. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1942. u32 alignment;
  1943. int ret;
  1944. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1945. switch (fb->modifier[0]) {
  1946. case DRM_FORMAT_MOD_NONE:
  1947. if (INTEL_INFO(dev)->gen >= 9)
  1948. alignment = 256 * 1024;
  1949. else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1950. alignment = 128 * 1024;
  1951. else if (INTEL_INFO(dev)->gen >= 4)
  1952. alignment = 4 * 1024;
  1953. else
  1954. alignment = 64 * 1024;
  1955. break;
  1956. case I915_FORMAT_MOD_X_TILED:
  1957. if (INTEL_INFO(dev)->gen >= 9)
  1958. alignment = 256 * 1024;
  1959. else {
  1960. /* pin() will align the object as required by fence */
  1961. alignment = 0;
  1962. }
  1963. break;
  1964. case I915_FORMAT_MOD_Y_TILED:
  1965. case I915_FORMAT_MOD_Yf_TILED:
  1966. if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
  1967. "Y tiling bo slipped through, driver bug!\n"))
  1968. return -EINVAL;
  1969. alignment = 1 * 1024 * 1024;
  1970. break;
  1971. default:
  1972. MISSING_CASE(fb->modifier[0]);
  1973. return -EINVAL;
  1974. }
  1975. /* Note that the w/a also requires 64 PTE of padding following the
  1976. * bo. We currently fill all unused PTE with the shadow page and so
  1977. * we should always have valid PTE following the scanout preventing
  1978. * the VT-d warning.
  1979. */
  1980. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1981. alignment = 256 * 1024;
  1982. /*
  1983. * Global gtt pte registers are special registers which actually forward
  1984. * writes to a chunk of system memory. Which means that there is no risk
  1985. * that the register values disappear as soon as we call
  1986. * intel_runtime_pm_put(), so it is correct to wrap only the
  1987. * pin/unpin/fence and not more.
  1988. */
  1989. intel_runtime_pm_get(dev_priv);
  1990. dev_priv->mm.interruptible = false;
  1991. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1992. if (ret)
  1993. goto err_interruptible;
  1994. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1995. * fence, whereas 965+ only requires a fence if using
  1996. * framebuffer compression. For simplicity, we always install
  1997. * a fence as the cost is not that onerous.
  1998. */
  1999. ret = i915_gem_object_get_fence(obj);
  2000. if (ret)
  2001. goto err_unpin;
  2002. i915_gem_object_pin_fence(obj);
  2003. dev_priv->mm.interruptible = true;
  2004. intel_runtime_pm_put(dev_priv);
  2005. return 0;
  2006. err_unpin:
  2007. i915_gem_object_unpin_from_display_plane(obj);
  2008. err_interruptible:
  2009. dev_priv->mm.interruptible = true;
  2010. intel_runtime_pm_put(dev_priv);
  2011. return ret;
  2012. }
  2013. static void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
  2014. {
  2015. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2016. i915_gem_object_unpin_fence(obj);
  2017. i915_gem_object_unpin_from_display_plane(obj);
  2018. }
  2019. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  2020. * is assumed to be a power-of-two. */
  2021. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  2022. unsigned int tiling_mode,
  2023. unsigned int cpp,
  2024. unsigned int pitch)
  2025. {
  2026. if (tiling_mode != I915_TILING_NONE) {
  2027. unsigned int tile_rows, tiles;
  2028. tile_rows = *y / 8;
  2029. *y %= 8;
  2030. tiles = *x / (512/cpp);
  2031. *x %= 512/cpp;
  2032. return tile_rows * pitch * 8 + tiles * 4096;
  2033. } else {
  2034. unsigned int offset;
  2035. offset = *y * pitch + *x * cpp;
  2036. *y = 0;
  2037. *x = (offset & 4095) / cpp;
  2038. return offset & -4096;
  2039. }
  2040. }
  2041. static int i9xx_format_to_fourcc(int format)
  2042. {
  2043. switch (format) {
  2044. case DISPPLANE_8BPP:
  2045. return DRM_FORMAT_C8;
  2046. case DISPPLANE_BGRX555:
  2047. return DRM_FORMAT_XRGB1555;
  2048. case DISPPLANE_BGRX565:
  2049. return DRM_FORMAT_RGB565;
  2050. default:
  2051. case DISPPLANE_BGRX888:
  2052. return DRM_FORMAT_XRGB8888;
  2053. case DISPPLANE_RGBX888:
  2054. return DRM_FORMAT_XBGR8888;
  2055. case DISPPLANE_BGRX101010:
  2056. return DRM_FORMAT_XRGB2101010;
  2057. case DISPPLANE_RGBX101010:
  2058. return DRM_FORMAT_XBGR2101010;
  2059. }
  2060. }
  2061. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2062. {
  2063. switch (format) {
  2064. case PLANE_CTL_FORMAT_RGB_565:
  2065. return DRM_FORMAT_RGB565;
  2066. default:
  2067. case PLANE_CTL_FORMAT_XRGB_8888:
  2068. if (rgb_order) {
  2069. if (alpha)
  2070. return DRM_FORMAT_ABGR8888;
  2071. else
  2072. return DRM_FORMAT_XBGR8888;
  2073. } else {
  2074. if (alpha)
  2075. return DRM_FORMAT_ARGB8888;
  2076. else
  2077. return DRM_FORMAT_XRGB8888;
  2078. }
  2079. case PLANE_CTL_FORMAT_XRGB_2101010:
  2080. if (rgb_order)
  2081. return DRM_FORMAT_XBGR2101010;
  2082. else
  2083. return DRM_FORMAT_XRGB2101010;
  2084. }
  2085. }
  2086. static bool
  2087. intel_alloc_plane_obj(struct intel_crtc *crtc,
  2088. struct intel_initial_plane_config *plane_config)
  2089. {
  2090. struct drm_device *dev = crtc->base.dev;
  2091. struct drm_i915_gem_object *obj = NULL;
  2092. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2093. struct drm_framebuffer *fb = &plane_config->fb->base;
  2094. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2095. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2096. PAGE_SIZE);
  2097. size_aligned -= base_aligned;
  2098. if (plane_config->size == 0)
  2099. return false;
  2100. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2101. base_aligned,
  2102. base_aligned,
  2103. size_aligned);
  2104. if (!obj)
  2105. return false;
  2106. obj->tiling_mode = plane_config->tiling;
  2107. if (obj->tiling_mode == I915_TILING_X)
  2108. obj->stride = fb->pitches[0];
  2109. mode_cmd.pixel_format = fb->pixel_format;
  2110. mode_cmd.width = fb->width;
  2111. mode_cmd.height = fb->height;
  2112. mode_cmd.pitches[0] = fb->pitches[0];
  2113. mode_cmd.modifier[0] = fb->modifier[0];
  2114. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2115. mutex_lock(&dev->struct_mutex);
  2116. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2117. &mode_cmd, obj)) {
  2118. DRM_DEBUG_KMS("intel fb init failed\n");
  2119. goto out_unref_obj;
  2120. }
  2121. obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
  2122. mutex_unlock(&dev->struct_mutex);
  2123. DRM_DEBUG_KMS("plane fb obj %p\n", obj);
  2124. return true;
  2125. out_unref_obj:
  2126. drm_gem_object_unreference(&obj->base);
  2127. mutex_unlock(&dev->struct_mutex);
  2128. return false;
  2129. }
  2130. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2131. static void
  2132. update_state_fb(struct drm_plane *plane)
  2133. {
  2134. if (plane->fb == plane->state->fb)
  2135. return;
  2136. if (plane->state->fb)
  2137. drm_framebuffer_unreference(plane->state->fb);
  2138. plane->state->fb = plane->fb;
  2139. if (plane->state->fb)
  2140. drm_framebuffer_reference(plane->state->fb);
  2141. }
  2142. static void
  2143. intel_find_plane_obj(struct intel_crtc *intel_crtc,
  2144. struct intel_initial_plane_config *plane_config)
  2145. {
  2146. struct drm_device *dev = intel_crtc->base.dev;
  2147. struct drm_i915_private *dev_priv = dev->dev_private;
  2148. struct drm_crtc *c;
  2149. struct intel_crtc *i;
  2150. struct drm_i915_gem_object *obj;
  2151. if (!plane_config->fb)
  2152. return;
  2153. if (intel_alloc_plane_obj(intel_crtc, plane_config)) {
  2154. struct drm_plane *primary = intel_crtc->base.primary;
  2155. primary->fb = &plane_config->fb->base;
  2156. primary->state->crtc = &intel_crtc->base;
  2157. update_state_fb(primary);
  2158. return;
  2159. }
  2160. kfree(plane_config->fb);
  2161. /*
  2162. * Failed to alloc the obj, check to see if we should share
  2163. * an fb with another CRTC instead
  2164. */
  2165. for_each_crtc(dev, c) {
  2166. i = to_intel_crtc(c);
  2167. if (c == &intel_crtc->base)
  2168. continue;
  2169. if (!i->active)
  2170. continue;
  2171. obj = intel_fb_obj(c->primary->fb);
  2172. if (obj == NULL)
  2173. continue;
  2174. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2175. struct drm_plane *primary = intel_crtc->base.primary;
  2176. if (obj->tiling_mode != I915_TILING_NONE)
  2177. dev_priv->preserve_bios_swizzle = true;
  2178. drm_framebuffer_reference(c->primary->fb);
  2179. primary->fb = c->primary->fb;
  2180. primary->state->crtc = &intel_crtc->base;
  2181. update_state_fb(intel_crtc->base.primary);
  2182. obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  2183. break;
  2184. }
  2185. }
  2186. }
  2187. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2188. struct drm_framebuffer *fb,
  2189. int x, int y)
  2190. {
  2191. struct drm_device *dev = crtc->dev;
  2192. struct drm_i915_private *dev_priv = dev->dev_private;
  2193. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2194. struct drm_i915_gem_object *obj;
  2195. int plane = intel_crtc->plane;
  2196. unsigned long linear_offset;
  2197. u32 dspcntr;
  2198. u32 reg = DSPCNTR(plane);
  2199. int pixel_size;
  2200. if (!intel_crtc->primary_enabled) {
  2201. I915_WRITE(reg, 0);
  2202. if (INTEL_INFO(dev)->gen >= 4)
  2203. I915_WRITE(DSPSURF(plane), 0);
  2204. else
  2205. I915_WRITE(DSPADDR(plane), 0);
  2206. POSTING_READ(reg);
  2207. return;
  2208. }
  2209. obj = intel_fb_obj(fb);
  2210. if (WARN_ON(obj == NULL))
  2211. return;
  2212. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2213. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2214. dspcntr |= DISPLAY_PLANE_ENABLE;
  2215. if (INTEL_INFO(dev)->gen < 4) {
  2216. if (intel_crtc->pipe == PIPE_B)
  2217. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2218. /* pipesrc and dspsize control the size that is scaled from,
  2219. * which should always be the user's requested size.
  2220. */
  2221. I915_WRITE(DSPSIZE(plane),
  2222. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2223. (intel_crtc->config->pipe_src_w - 1));
  2224. I915_WRITE(DSPPOS(plane), 0);
  2225. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2226. I915_WRITE(PRIMSIZE(plane),
  2227. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2228. (intel_crtc->config->pipe_src_w - 1));
  2229. I915_WRITE(PRIMPOS(plane), 0);
  2230. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2231. }
  2232. switch (fb->pixel_format) {
  2233. case DRM_FORMAT_C8:
  2234. dspcntr |= DISPPLANE_8BPP;
  2235. break;
  2236. case DRM_FORMAT_XRGB1555:
  2237. case DRM_FORMAT_ARGB1555:
  2238. dspcntr |= DISPPLANE_BGRX555;
  2239. break;
  2240. case DRM_FORMAT_RGB565:
  2241. dspcntr |= DISPPLANE_BGRX565;
  2242. break;
  2243. case DRM_FORMAT_XRGB8888:
  2244. case DRM_FORMAT_ARGB8888:
  2245. dspcntr |= DISPPLANE_BGRX888;
  2246. break;
  2247. case DRM_FORMAT_XBGR8888:
  2248. case DRM_FORMAT_ABGR8888:
  2249. dspcntr |= DISPPLANE_RGBX888;
  2250. break;
  2251. case DRM_FORMAT_XRGB2101010:
  2252. case DRM_FORMAT_ARGB2101010:
  2253. dspcntr |= DISPPLANE_BGRX101010;
  2254. break;
  2255. case DRM_FORMAT_XBGR2101010:
  2256. case DRM_FORMAT_ABGR2101010:
  2257. dspcntr |= DISPPLANE_RGBX101010;
  2258. break;
  2259. default:
  2260. BUG();
  2261. }
  2262. if (INTEL_INFO(dev)->gen >= 4 &&
  2263. obj->tiling_mode != I915_TILING_NONE)
  2264. dspcntr |= DISPPLANE_TILED;
  2265. if (IS_G4X(dev))
  2266. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2267. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2268. if (INTEL_INFO(dev)->gen >= 4) {
  2269. intel_crtc->dspaddr_offset =
  2270. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2271. pixel_size,
  2272. fb->pitches[0]);
  2273. linear_offset -= intel_crtc->dspaddr_offset;
  2274. } else {
  2275. intel_crtc->dspaddr_offset = linear_offset;
  2276. }
  2277. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2278. dspcntr |= DISPPLANE_ROTATE_180;
  2279. x += (intel_crtc->config->pipe_src_w - 1);
  2280. y += (intel_crtc->config->pipe_src_h - 1);
  2281. /* Finding the last pixel of the last line of the display
  2282. data and adding to linear_offset*/
  2283. linear_offset +=
  2284. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2285. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2286. }
  2287. I915_WRITE(reg, dspcntr);
  2288. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2289. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2290. fb->pitches[0]);
  2291. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2292. if (INTEL_INFO(dev)->gen >= 4) {
  2293. I915_WRITE(DSPSURF(plane),
  2294. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2295. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2296. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2297. } else
  2298. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2299. POSTING_READ(reg);
  2300. }
  2301. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2302. struct drm_framebuffer *fb,
  2303. int x, int y)
  2304. {
  2305. struct drm_device *dev = crtc->dev;
  2306. struct drm_i915_private *dev_priv = dev->dev_private;
  2307. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2308. struct drm_i915_gem_object *obj;
  2309. int plane = intel_crtc->plane;
  2310. unsigned long linear_offset;
  2311. u32 dspcntr;
  2312. u32 reg = DSPCNTR(plane);
  2313. int pixel_size;
  2314. if (!intel_crtc->primary_enabled) {
  2315. I915_WRITE(reg, 0);
  2316. I915_WRITE(DSPSURF(plane), 0);
  2317. POSTING_READ(reg);
  2318. return;
  2319. }
  2320. obj = intel_fb_obj(fb);
  2321. if (WARN_ON(obj == NULL))
  2322. return;
  2323. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2324. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2325. dspcntr |= DISPLAY_PLANE_ENABLE;
  2326. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2327. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2328. switch (fb->pixel_format) {
  2329. case DRM_FORMAT_C8:
  2330. dspcntr |= DISPPLANE_8BPP;
  2331. break;
  2332. case DRM_FORMAT_RGB565:
  2333. dspcntr |= DISPPLANE_BGRX565;
  2334. break;
  2335. case DRM_FORMAT_XRGB8888:
  2336. case DRM_FORMAT_ARGB8888:
  2337. dspcntr |= DISPPLANE_BGRX888;
  2338. break;
  2339. case DRM_FORMAT_XBGR8888:
  2340. case DRM_FORMAT_ABGR8888:
  2341. dspcntr |= DISPPLANE_RGBX888;
  2342. break;
  2343. case DRM_FORMAT_XRGB2101010:
  2344. case DRM_FORMAT_ARGB2101010:
  2345. dspcntr |= DISPPLANE_BGRX101010;
  2346. break;
  2347. case DRM_FORMAT_XBGR2101010:
  2348. case DRM_FORMAT_ABGR2101010:
  2349. dspcntr |= DISPPLANE_RGBX101010;
  2350. break;
  2351. default:
  2352. BUG();
  2353. }
  2354. if (obj->tiling_mode != I915_TILING_NONE)
  2355. dspcntr |= DISPPLANE_TILED;
  2356. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2357. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2358. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2359. intel_crtc->dspaddr_offset =
  2360. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2361. pixel_size,
  2362. fb->pitches[0]);
  2363. linear_offset -= intel_crtc->dspaddr_offset;
  2364. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2365. dspcntr |= DISPPLANE_ROTATE_180;
  2366. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2367. x += (intel_crtc->config->pipe_src_w - 1);
  2368. y += (intel_crtc->config->pipe_src_h - 1);
  2369. /* Finding the last pixel of the last line of the display
  2370. data and adding to linear_offset*/
  2371. linear_offset +=
  2372. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2373. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2374. }
  2375. }
  2376. I915_WRITE(reg, dspcntr);
  2377. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2378. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2379. fb->pitches[0]);
  2380. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2381. I915_WRITE(DSPSURF(plane),
  2382. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2383. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2384. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2385. } else {
  2386. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2387. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2388. }
  2389. POSTING_READ(reg);
  2390. }
  2391. u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
  2392. uint32_t pixel_format)
  2393. {
  2394. u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
  2395. /*
  2396. * The stride is either expressed as a multiple of 64 bytes
  2397. * chunks for linear buffers or in number of tiles for tiled
  2398. * buffers.
  2399. */
  2400. switch (fb_modifier) {
  2401. case DRM_FORMAT_MOD_NONE:
  2402. return 64;
  2403. case I915_FORMAT_MOD_X_TILED:
  2404. if (INTEL_INFO(dev)->gen == 2)
  2405. return 128;
  2406. return 512;
  2407. case I915_FORMAT_MOD_Y_TILED:
  2408. /* No need to check for old gens and Y tiling since this is
  2409. * about the display engine and those will be blocked before
  2410. * we get here.
  2411. */
  2412. return 128;
  2413. case I915_FORMAT_MOD_Yf_TILED:
  2414. if (bits_per_pixel == 8)
  2415. return 64;
  2416. else
  2417. return 128;
  2418. default:
  2419. MISSING_CASE(fb_modifier);
  2420. return 64;
  2421. }
  2422. }
  2423. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2424. struct drm_framebuffer *fb,
  2425. int x, int y)
  2426. {
  2427. struct drm_device *dev = crtc->dev;
  2428. struct drm_i915_private *dev_priv = dev->dev_private;
  2429. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2430. struct drm_i915_gem_object *obj;
  2431. int pipe = intel_crtc->pipe;
  2432. u32 plane_ctl, stride_div;
  2433. if (!intel_crtc->primary_enabled) {
  2434. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2435. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2436. POSTING_READ(PLANE_CTL(pipe, 0));
  2437. return;
  2438. }
  2439. plane_ctl = PLANE_CTL_ENABLE |
  2440. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2441. PLANE_CTL_PIPE_CSC_ENABLE;
  2442. switch (fb->pixel_format) {
  2443. case DRM_FORMAT_RGB565:
  2444. plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
  2445. break;
  2446. case DRM_FORMAT_XRGB8888:
  2447. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2448. break;
  2449. case DRM_FORMAT_ARGB8888:
  2450. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2451. plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2452. break;
  2453. case DRM_FORMAT_XBGR8888:
  2454. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2455. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2456. break;
  2457. case DRM_FORMAT_ABGR8888:
  2458. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2459. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2460. plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2461. break;
  2462. case DRM_FORMAT_XRGB2101010:
  2463. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2464. break;
  2465. case DRM_FORMAT_XBGR2101010:
  2466. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2467. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2468. break;
  2469. default:
  2470. BUG();
  2471. }
  2472. switch (fb->modifier[0]) {
  2473. case DRM_FORMAT_MOD_NONE:
  2474. break;
  2475. case I915_FORMAT_MOD_X_TILED:
  2476. plane_ctl |= PLANE_CTL_TILED_X;
  2477. break;
  2478. case I915_FORMAT_MOD_Y_TILED:
  2479. plane_ctl |= PLANE_CTL_TILED_Y;
  2480. break;
  2481. case I915_FORMAT_MOD_Yf_TILED:
  2482. plane_ctl |= PLANE_CTL_TILED_YF;
  2483. break;
  2484. default:
  2485. MISSING_CASE(fb->modifier[0]);
  2486. }
  2487. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2488. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180))
  2489. plane_ctl |= PLANE_CTL_ROTATE_180;
  2490. obj = intel_fb_obj(fb);
  2491. stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
  2492. fb->pixel_format);
  2493. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2494. DRM_DEBUG_KMS("Writing base %08lX %d,%d,%d,%d pitch=%d\n",
  2495. i915_gem_obj_ggtt_offset(obj),
  2496. x, y, fb->width, fb->height,
  2497. fb->pitches[0]);
  2498. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2499. I915_WRITE(PLANE_OFFSET(pipe, 0), (y << 16) | x);
  2500. I915_WRITE(PLANE_SIZE(pipe, 0),
  2501. (intel_crtc->config->pipe_src_h - 1) << 16 |
  2502. (intel_crtc->config->pipe_src_w - 1));
  2503. I915_WRITE(PLANE_STRIDE(pipe, 0), fb->pitches[0] / stride_div);
  2504. I915_WRITE(PLANE_SURF(pipe, 0), i915_gem_obj_ggtt_offset(obj));
  2505. POSTING_READ(PLANE_SURF(pipe, 0));
  2506. }
  2507. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2508. static int
  2509. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2510. int x, int y, enum mode_set_atomic state)
  2511. {
  2512. struct drm_device *dev = crtc->dev;
  2513. struct drm_i915_private *dev_priv = dev->dev_private;
  2514. if (dev_priv->display.disable_fbc)
  2515. dev_priv->display.disable_fbc(dev);
  2516. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2517. return 0;
  2518. }
  2519. static void intel_complete_page_flips(struct drm_device *dev)
  2520. {
  2521. struct drm_crtc *crtc;
  2522. for_each_crtc(dev, crtc) {
  2523. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2524. enum plane plane = intel_crtc->plane;
  2525. intel_prepare_page_flip(dev, plane);
  2526. intel_finish_page_flip_plane(dev, plane);
  2527. }
  2528. }
  2529. static void intel_update_primary_planes(struct drm_device *dev)
  2530. {
  2531. struct drm_i915_private *dev_priv = dev->dev_private;
  2532. struct drm_crtc *crtc;
  2533. for_each_crtc(dev, crtc) {
  2534. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2535. drm_modeset_lock(&crtc->mutex, NULL);
  2536. /*
  2537. * FIXME: Once we have proper support for primary planes (and
  2538. * disabling them without disabling the entire crtc) allow again
  2539. * a NULL crtc->primary->fb.
  2540. */
  2541. if (intel_crtc->active && crtc->primary->fb)
  2542. dev_priv->display.update_primary_plane(crtc,
  2543. crtc->primary->fb,
  2544. crtc->x,
  2545. crtc->y);
  2546. drm_modeset_unlock(&crtc->mutex);
  2547. }
  2548. }
  2549. void intel_prepare_reset(struct drm_device *dev)
  2550. {
  2551. struct drm_i915_private *dev_priv = to_i915(dev);
  2552. struct intel_crtc *crtc;
  2553. /* no reset support for gen2 */
  2554. if (IS_GEN2(dev))
  2555. return;
  2556. /* reset doesn't touch the display */
  2557. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2558. return;
  2559. drm_modeset_lock_all(dev);
  2560. /*
  2561. * Disabling the crtcs gracefully seems nicer. Also the
  2562. * g33 docs say we should at least disable all the planes.
  2563. */
  2564. for_each_intel_crtc(dev, crtc) {
  2565. if (crtc->active)
  2566. dev_priv->display.crtc_disable(&crtc->base);
  2567. }
  2568. }
  2569. void intel_finish_reset(struct drm_device *dev)
  2570. {
  2571. struct drm_i915_private *dev_priv = to_i915(dev);
  2572. /*
  2573. * Flips in the rings will be nuked by the reset,
  2574. * so complete all pending flips so that user space
  2575. * will get its events and not get stuck.
  2576. */
  2577. intel_complete_page_flips(dev);
  2578. /* no reset support for gen2 */
  2579. if (IS_GEN2(dev))
  2580. return;
  2581. /* reset doesn't touch the display */
  2582. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2583. /*
  2584. * Flips in the rings have been nuked by the reset,
  2585. * so update the base address of all primary
  2586. * planes to the the last fb to make sure we're
  2587. * showing the correct fb after a reset.
  2588. */
  2589. intel_update_primary_planes(dev);
  2590. return;
  2591. }
  2592. /*
  2593. * The display has been reset as well,
  2594. * so need a full re-initialization.
  2595. */
  2596. intel_runtime_pm_disable_interrupts(dev_priv);
  2597. intel_runtime_pm_enable_interrupts(dev_priv);
  2598. intel_modeset_init_hw(dev);
  2599. spin_lock_irq(&dev_priv->irq_lock);
  2600. if (dev_priv->display.hpd_irq_setup)
  2601. dev_priv->display.hpd_irq_setup(dev);
  2602. spin_unlock_irq(&dev_priv->irq_lock);
  2603. intel_modeset_setup_hw_state(dev, true);
  2604. intel_hpd_init(dev_priv);
  2605. drm_modeset_unlock_all(dev);
  2606. }
  2607. static int
  2608. intel_finish_fb(struct drm_framebuffer *old_fb)
  2609. {
  2610. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2611. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2612. bool was_interruptible = dev_priv->mm.interruptible;
  2613. int ret;
  2614. /* Big Hammer, we also need to ensure that any pending
  2615. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2616. * current scanout is retired before unpinning the old
  2617. * framebuffer.
  2618. *
  2619. * This should only fail upon a hung GPU, in which case we
  2620. * can safely continue.
  2621. */
  2622. dev_priv->mm.interruptible = false;
  2623. ret = i915_gem_object_finish_gpu(obj);
  2624. dev_priv->mm.interruptible = was_interruptible;
  2625. return ret;
  2626. }
  2627. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2628. {
  2629. struct drm_device *dev = crtc->dev;
  2630. struct drm_i915_private *dev_priv = dev->dev_private;
  2631. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2632. bool pending;
  2633. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2634. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2635. return false;
  2636. spin_lock_irq(&dev->event_lock);
  2637. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2638. spin_unlock_irq(&dev->event_lock);
  2639. return pending;
  2640. }
  2641. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2642. {
  2643. struct drm_device *dev = crtc->base.dev;
  2644. struct drm_i915_private *dev_priv = dev->dev_private;
  2645. const struct drm_display_mode *adjusted_mode;
  2646. if (!i915.fastboot)
  2647. return;
  2648. /*
  2649. * Update pipe size and adjust fitter if needed: the reason for this is
  2650. * that in compute_mode_changes we check the native mode (not the pfit
  2651. * mode) to see if we can flip rather than do a full mode set. In the
  2652. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2653. * pfit state, we'll end up with a big fb scanned out into the wrong
  2654. * sized surface.
  2655. *
  2656. * To fix this properly, we need to hoist the checks up into
  2657. * compute_mode_changes (or above), check the actual pfit state and
  2658. * whether the platform allows pfit disable with pipe active, and only
  2659. * then update the pipesrc and pfit state, even on the flip path.
  2660. */
  2661. adjusted_mode = &crtc->config->base.adjusted_mode;
  2662. I915_WRITE(PIPESRC(crtc->pipe),
  2663. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2664. (adjusted_mode->crtc_vdisplay - 1));
  2665. if (!crtc->config->pch_pfit.enabled &&
  2666. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2667. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2668. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2669. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2670. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2671. }
  2672. crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
  2673. crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
  2674. }
  2675. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2676. {
  2677. struct drm_device *dev = crtc->dev;
  2678. struct drm_i915_private *dev_priv = dev->dev_private;
  2679. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2680. int pipe = intel_crtc->pipe;
  2681. u32 reg, temp;
  2682. /* enable normal train */
  2683. reg = FDI_TX_CTL(pipe);
  2684. temp = I915_READ(reg);
  2685. if (IS_IVYBRIDGE(dev)) {
  2686. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2687. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2688. } else {
  2689. temp &= ~FDI_LINK_TRAIN_NONE;
  2690. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2691. }
  2692. I915_WRITE(reg, temp);
  2693. reg = FDI_RX_CTL(pipe);
  2694. temp = I915_READ(reg);
  2695. if (HAS_PCH_CPT(dev)) {
  2696. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2697. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2698. } else {
  2699. temp &= ~FDI_LINK_TRAIN_NONE;
  2700. temp |= FDI_LINK_TRAIN_NONE;
  2701. }
  2702. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2703. /* wait one idle pattern time */
  2704. POSTING_READ(reg);
  2705. udelay(1000);
  2706. /* IVB wants error correction enabled */
  2707. if (IS_IVYBRIDGE(dev))
  2708. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2709. FDI_FE_ERRC_ENABLE);
  2710. }
  2711. static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
  2712. {
  2713. return crtc->base.state->enable && crtc->active &&
  2714. crtc->config->has_pch_encoder;
  2715. }
  2716. static void ivb_modeset_global_resources(struct drm_device *dev)
  2717. {
  2718. struct drm_i915_private *dev_priv = dev->dev_private;
  2719. struct intel_crtc *pipe_B_crtc =
  2720. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  2721. struct intel_crtc *pipe_C_crtc =
  2722. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
  2723. uint32_t temp;
  2724. /*
  2725. * When everything is off disable fdi C so that we could enable fdi B
  2726. * with all lanes. Note that we don't care about enabled pipes without
  2727. * an enabled pch encoder.
  2728. */
  2729. if (!pipe_has_enabled_pch(pipe_B_crtc) &&
  2730. !pipe_has_enabled_pch(pipe_C_crtc)) {
  2731. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2732. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2733. temp = I915_READ(SOUTH_CHICKEN1);
  2734. temp &= ~FDI_BC_BIFURCATION_SELECT;
  2735. DRM_DEBUG_KMS("disabling fdi C rx\n");
  2736. I915_WRITE(SOUTH_CHICKEN1, temp);
  2737. }
  2738. }
  2739. /* The FDI link training functions for ILK/Ibexpeak. */
  2740. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2741. {
  2742. struct drm_device *dev = crtc->dev;
  2743. struct drm_i915_private *dev_priv = dev->dev_private;
  2744. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2745. int pipe = intel_crtc->pipe;
  2746. u32 reg, temp, tries;
  2747. /* FDI needs bits from pipe first */
  2748. assert_pipe_enabled(dev_priv, pipe);
  2749. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2750. for train result */
  2751. reg = FDI_RX_IMR(pipe);
  2752. temp = I915_READ(reg);
  2753. temp &= ~FDI_RX_SYMBOL_LOCK;
  2754. temp &= ~FDI_RX_BIT_LOCK;
  2755. I915_WRITE(reg, temp);
  2756. I915_READ(reg);
  2757. udelay(150);
  2758. /* enable CPU FDI TX and PCH FDI RX */
  2759. reg = FDI_TX_CTL(pipe);
  2760. temp = I915_READ(reg);
  2761. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2762. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2763. temp &= ~FDI_LINK_TRAIN_NONE;
  2764. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2765. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2766. reg = FDI_RX_CTL(pipe);
  2767. temp = I915_READ(reg);
  2768. temp &= ~FDI_LINK_TRAIN_NONE;
  2769. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2770. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2771. POSTING_READ(reg);
  2772. udelay(150);
  2773. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2774. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2775. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2776. FDI_RX_PHASE_SYNC_POINTER_EN);
  2777. reg = FDI_RX_IIR(pipe);
  2778. for (tries = 0; tries < 5; tries++) {
  2779. temp = I915_READ(reg);
  2780. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2781. if ((temp & FDI_RX_BIT_LOCK)) {
  2782. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2783. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2784. break;
  2785. }
  2786. }
  2787. if (tries == 5)
  2788. DRM_ERROR("FDI train 1 fail!\n");
  2789. /* Train 2 */
  2790. reg = FDI_TX_CTL(pipe);
  2791. temp = I915_READ(reg);
  2792. temp &= ~FDI_LINK_TRAIN_NONE;
  2793. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2794. I915_WRITE(reg, temp);
  2795. reg = FDI_RX_CTL(pipe);
  2796. temp = I915_READ(reg);
  2797. temp &= ~FDI_LINK_TRAIN_NONE;
  2798. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2799. I915_WRITE(reg, temp);
  2800. POSTING_READ(reg);
  2801. udelay(150);
  2802. reg = FDI_RX_IIR(pipe);
  2803. for (tries = 0; tries < 5; tries++) {
  2804. temp = I915_READ(reg);
  2805. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2806. if (temp & FDI_RX_SYMBOL_LOCK) {
  2807. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2808. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2809. break;
  2810. }
  2811. }
  2812. if (tries == 5)
  2813. DRM_ERROR("FDI train 2 fail!\n");
  2814. DRM_DEBUG_KMS("FDI train done\n");
  2815. }
  2816. static const int snb_b_fdi_train_param[] = {
  2817. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2818. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2819. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2820. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2821. };
  2822. /* The FDI link training functions for SNB/Cougarpoint. */
  2823. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2824. {
  2825. struct drm_device *dev = crtc->dev;
  2826. struct drm_i915_private *dev_priv = dev->dev_private;
  2827. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2828. int pipe = intel_crtc->pipe;
  2829. u32 reg, temp, i, retry;
  2830. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2831. for train result */
  2832. reg = FDI_RX_IMR(pipe);
  2833. temp = I915_READ(reg);
  2834. temp &= ~FDI_RX_SYMBOL_LOCK;
  2835. temp &= ~FDI_RX_BIT_LOCK;
  2836. I915_WRITE(reg, temp);
  2837. POSTING_READ(reg);
  2838. udelay(150);
  2839. /* enable CPU FDI TX and PCH FDI RX */
  2840. reg = FDI_TX_CTL(pipe);
  2841. temp = I915_READ(reg);
  2842. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2843. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2844. temp &= ~FDI_LINK_TRAIN_NONE;
  2845. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2846. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2847. /* SNB-B */
  2848. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2849. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2850. I915_WRITE(FDI_RX_MISC(pipe),
  2851. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2852. reg = FDI_RX_CTL(pipe);
  2853. temp = I915_READ(reg);
  2854. if (HAS_PCH_CPT(dev)) {
  2855. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2856. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2857. } else {
  2858. temp &= ~FDI_LINK_TRAIN_NONE;
  2859. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2860. }
  2861. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2862. POSTING_READ(reg);
  2863. udelay(150);
  2864. for (i = 0; i < 4; i++) {
  2865. reg = FDI_TX_CTL(pipe);
  2866. temp = I915_READ(reg);
  2867. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2868. temp |= snb_b_fdi_train_param[i];
  2869. I915_WRITE(reg, temp);
  2870. POSTING_READ(reg);
  2871. udelay(500);
  2872. for (retry = 0; retry < 5; retry++) {
  2873. reg = FDI_RX_IIR(pipe);
  2874. temp = I915_READ(reg);
  2875. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2876. if (temp & FDI_RX_BIT_LOCK) {
  2877. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2878. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2879. break;
  2880. }
  2881. udelay(50);
  2882. }
  2883. if (retry < 5)
  2884. break;
  2885. }
  2886. if (i == 4)
  2887. DRM_ERROR("FDI train 1 fail!\n");
  2888. /* Train 2 */
  2889. reg = FDI_TX_CTL(pipe);
  2890. temp = I915_READ(reg);
  2891. temp &= ~FDI_LINK_TRAIN_NONE;
  2892. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2893. if (IS_GEN6(dev)) {
  2894. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2895. /* SNB-B */
  2896. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2897. }
  2898. I915_WRITE(reg, temp);
  2899. reg = FDI_RX_CTL(pipe);
  2900. temp = I915_READ(reg);
  2901. if (HAS_PCH_CPT(dev)) {
  2902. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2903. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2904. } else {
  2905. temp &= ~FDI_LINK_TRAIN_NONE;
  2906. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2907. }
  2908. I915_WRITE(reg, temp);
  2909. POSTING_READ(reg);
  2910. udelay(150);
  2911. for (i = 0; i < 4; i++) {
  2912. reg = FDI_TX_CTL(pipe);
  2913. temp = I915_READ(reg);
  2914. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2915. temp |= snb_b_fdi_train_param[i];
  2916. I915_WRITE(reg, temp);
  2917. POSTING_READ(reg);
  2918. udelay(500);
  2919. for (retry = 0; retry < 5; retry++) {
  2920. reg = FDI_RX_IIR(pipe);
  2921. temp = I915_READ(reg);
  2922. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2923. if (temp & FDI_RX_SYMBOL_LOCK) {
  2924. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2925. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2926. break;
  2927. }
  2928. udelay(50);
  2929. }
  2930. if (retry < 5)
  2931. break;
  2932. }
  2933. if (i == 4)
  2934. DRM_ERROR("FDI train 2 fail!\n");
  2935. DRM_DEBUG_KMS("FDI train done.\n");
  2936. }
  2937. /* Manual link training for Ivy Bridge A0 parts */
  2938. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2939. {
  2940. struct drm_device *dev = crtc->dev;
  2941. struct drm_i915_private *dev_priv = dev->dev_private;
  2942. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2943. int pipe = intel_crtc->pipe;
  2944. u32 reg, temp, i, j;
  2945. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2946. for train result */
  2947. reg = FDI_RX_IMR(pipe);
  2948. temp = I915_READ(reg);
  2949. temp &= ~FDI_RX_SYMBOL_LOCK;
  2950. temp &= ~FDI_RX_BIT_LOCK;
  2951. I915_WRITE(reg, temp);
  2952. POSTING_READ(reg);
  2953. udelay(150);
  2954. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  2955. I915_READ(FDI_RX_IIR(pipe)));
  2956. /* Try each vswing and preemphasis setting twice before moving on */
  2957. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  2958. /* disable first in case we need to retry */
  2959. reg = FDI_TX_CTL(pipe);
  2960. temp = I915_READ(reg);
  2961. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2962. temp &= ~FDI_TX_ENABLE;
  2963. I915_WRITE(reg, temp);
  2964. reg = FDI_RX_CTL(pipe);
  2965. temp = I915_READ(reg);
  2966. temp &= ~FDI_LINK_TRAIN_AUTO;
  2967. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2968. temp &= ~FDI_RX_ENABLE;
  2969. I915_WRITE(reg, temp);
  2970. /* enable CPU FDI TX and PCH FDI RX */
  2971. reg = FDI_TX_CTL(pipe);
  2972. temp = I915_READ(reg);
  2973. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2974. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2975. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2976. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2977. temp |= snb_b_fdi_train_param[j/2];
  2978. temp |= FDI_COMPOSITE_SYNC;
  2979. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2980. I915_WRITE(FDI_RX_MISC(pipe),
  2981. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2982. reg = FDI_RX_CTL(pipe);
  2983. temp = I915_READ(reg);
  2984. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2985. temp |= FDI_COMPOSITE_SYNC;
  2986. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2987. POSTING_READ(reg);
  2988. udelay(1); /* should be 0.5us */
  2989. for (i = 0; i < 4; i++) {
  2990. reg = FDI_RX_IIR(pipe);
  2991. temp = I915_READ(reg);
  2992. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2993. if (temp & FDI_RX_BIT_LOCK ||
  2994. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2995. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2996. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  2997. i);
  2998. break;
  2999. }
  3000. udelay(1); /* should be 0.5us */
  3001. }
  3002. if (i == 4) {
  3003. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3004. continue;
  3005. }
  3006. /* Train 2 */
  3007. reg = FDI_TX_CTL(pipe);
  3008. temp = I915_READ(reg);
  3009. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3010. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3011. I915_WRITE(reg, temp);
  3012. reg = FDI_RX_CTL(pipe);
  3013. temp = I915_READ(reg);
  3014. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3015. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3016. I915_WRITE(reg, temp);
  3017. POSTING_READ(reg);
  3018. udelay(2); /* should be 1.5us */
  3019. for (i = 0; i < 4; i++) {
  3020. reg = FDI_RX_IIR(pipe);
  3021. temp = I915_READ(reg);
  3022. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3023. if (temp & FDI_RX_SYMBOL_LOCK ||
  3024. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3025. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3026. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3027. i);
  3028. goto train_done;
  3029. }
  3030. udelay(2); /* should be 1.5us */
  3031. }
  3032. if (i == 4)
  3033. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3034. }
  3035. train_done:
  3036. DRM_DEBUG_KMS("FDI train done.\n");
  3037. }
  3038. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3039. {
  3040. struct drm_device *dev = intel_crtc->base.dev;
  3041. struct drm_i915_private *dev_priv = dev->dev_private;
  3042. int pipe = intel_crtc->pipe;
  3043. u32 reg, temp;
  3044. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3045. reg = FDI_RX_CTL(pipe);
  3046. temp = I915_READ(reg);
  3047. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3048. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3049. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3050. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3051. POSTING_READ(reg);
  3052. udelay(200);
  3053. /* Switch from Rawclk to PCDclk */
  3054. temp = I915_READ(reg);
  3055. I915_WRITE(reg, temp | FDI_PCDCLK);
  3056. POSTING_READ(reg);
  3057. udelay(200);
  3058. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3059. reg = FDI_TX_CTL(pipe);
  3060. temp = I915_READ(reg);
  3061. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3062. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3063. POSTING_READ(reg);
  3064. udelay(100);
  3065. }
  3066. }
  3067. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3068. {
  3069. struct drm_device *dev = intel_crtc->base.dev;
  3070. struct drm_i915_private *dev_priv = dev->dev_private;
  3071. int pipe = intel_crtc->pipe;
  3072. u32 reg, temp;
  3073. /* Switch from PCDclk to Rawclk */
  3074. reg = FDI_RX_CTL(pipe);
  3075. temp = I915_READ(reg);
  3076. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3077. /* Disable CPU FDI TX PLL */
  3078. reg = FDI_TX_CTL(pipe);
  3079. temp = I915_READ(reg);
  3080. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3081. POSTING_READ(reg);
  3082. udelay(100);
  3083. reg = FDI_RX_CTL(pipe);
  3084. temp = I915_READ(reg);
  3085. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3086. /* Wait for the clocks to turn off. */
  3087. POSTING_READ(reg);
  3088. udelay(100);
  3089. }
  3090. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3091. {
  3092. struct drm_device *dev = crtc->dev;
  3093. struct drm_i915_private *dev_priv = dev->dev_private;
  3094. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3095. int pipe = intel_crtc->pipe;
  3096. u32 reg, temp;
  3097. /* disable CPU FDI tx and PCH FDI rx */
  3098. reg = FDI_TX_CTL(pipe);
  3099. temp = I915_READ(reg);
  3100. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3101. POSTING_READ(reg);
  3102. reg = FDI_RX_CTL(pipe);
  3103. temp = I915_READ(reg);
  3104. temp &= ~(0x7 << 16);
  3105. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3106. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3107. POSTING_READ(reg);
  3108. udelay(100);
  3109. /* Ironlake workaround, disable clock pointer after downing FDI */
  3110. if (HAS_PCH_IBX(dev))
  3111. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3112. /* still set train pattern 1 */
  3113. reg = FDI_TX_CTL(pipe);
  3114. temp = I915_READ(reg);
  3115. temp &= ~FDI_LINK_TRAIN_NONE;
  3116. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3117. I915_WRITE(reg, temp);
  3118. reg = FDI_RX_CTL(pipe);
  3119. temp = I915_READ(reg);
  3120. if (HAS_PCH_CPT(dev)) {
  3121. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3122. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3123. } else {
  3124. temp &= ~FDI_LINK_TRAIN_NONE;
  3125. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3126. }
  3127. /* BPC in FDI rx is consistent with that in PIPECONF */
  3128. temp &= ~(0x07 << 16);
  3129. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3130. I915_WRITE(reg, temp);
  3131. POSTING_READ(reg);
  3132. udelay(100);
  3133. }
  3134. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3135. {
  3136. struct intel_crtc *crtc;
  3137. /* Note that we don't need to be called with mode_config.lock here
  3138. * as our list of CRTC objects is static for the lifetime of the
  3139. * device and so cannot disappear as we iterate. Similarly, we can
  3140. * happily treat the predicates as racy, atomic checks as userspace
  3141. * cannot claim and pin a new fb without at least acquring the
  3142. * struct_mutex and so serialising with us.
  3143. */
  3144. for_each_intel_crtc(dev, crtc) {
  3145. if (atomic_read(&crtc->unpin_work_count) == 0)
  3146. continue;
  3147. if (crtc->unpin_work)
  3148. intel_wait_for_vblank(dev, crtc->pipe);
  3149. return true;
  3150. }
  3151. return false;
  3152. }
  3153. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3154. {
  3155. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3156. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3157. /* ensure that the unpin work is consistent wrt ->pending. */
  3158. smp_rmb();
  3159. intel_crtc->unpin_work = NULL;
  3160. if (work->event)
  3161. drm_send_vblank_event(intel_crtc->base.dev,
  3162. intel_crtc->pipe,
  3163. work->event);
  3164. drm_crtc_vblank_put(&intel_crtc->base);
  3165. wake_up_all(&dev_priv->pending_flip_queue);
  3166. queue_work(dev_priv->wq, &work->work);
  3167. trace_i915_flip_complete(intel_crtc->plane,
  3168. work->pending_flip_obj);
  3169. }
  3170. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3171. {
  3172. struct drm_device *dev = crtc->dev;
  3173. struct drm_i915_private *dev_priv = dev->dev_private;
  3174. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3175. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3176. !intel_crtc_has_pending_flip(crtc),
  3177. 60*HZ) == 0)) {
  3178. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3179. spin_lock_irq(&dev->event_lock);
  3180. if (intel_crtc->unpin_work) {
  3181. WARN_ONCE(1, "Removing stuck page flip\n");
  3182. page_flip_completed(intel_crtc);
  3183. }
  3184. spin_unlock_irq(&dev->event_lock);
  3185. }
  3186. if (crtc->primary->fb) {
  3187. mutex_lock(&dev->struct_mutex);
  3188. intel_finish_fb(crtc->primary->fb);
  3189. mutex_unlock(&dev->struct_mutex);
  3190. }
  3191. }
  3192. /* Program iCLKIP clock to the desired frequency */
  3193. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3194. {
  3195. struct drm_device *dev = crtc->dev;
  3196. struct drm_i915_private *dev_priv = dev->dev_private;
  3197. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3198. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3199. u32 temp;
  3200. mutex_lock(&dev_priv->dpio_lock);
  3201. /* It is necessary to ungate the pixclk gate prior to programming
  3202. * the divisors, and gate it back when it is done.
  3203. */
  3204. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3205. /* Disable SSCCTL */
  3206. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3207. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3208. SBI_SSCCTL_DISABLE,
  3209. SBI_ICLK);
  3210. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3211. if (clock == 20000) {
  3212. auxdiv = 1;
  3213. divsel = 0x41;
  3214. phaseinc = 0x20;
  3215. } else {
  3216. /* The iCLK virtual clock root frequency is in MHz,
  3217. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3218. * divisors, it is necessary to divide one by another, so we
  3219. * convert the virtual clock precision to KHz here for higher
  3220. * precision.
  3221. */
  3222. u32 iclk_virtual_root_freq = 172800 * 1000;
  3223. u32 iclk_pi_range = 64;
  3224. u32 desired_divisor, msb_divisor_value, pi_value;
  3225. desired_divisor = (iclk_virtual_root_freq / clock);
  3226. msb_divisor_value = desired_divisor / iclk_pi_range;
  3227. pi_value = desired_divisor % iclk_pi_range;
  3228. auxdiv = 0;
  3229. divsel = msb_divisor_value - 2;
  3230. phaseinc = pi_value;
  3231. }
  3232. /* This should not happen with any sane values */
  3233. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3234. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3235. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3236. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3237. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3238. clock,
  3239. auxdiv,
  3240. divsel,
  3241. phasedir,
  3242. phaseinc);
  3243. /* Program SSCDIVINTPHASE6 */
  3244. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3245. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3246. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3247. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3248. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3249. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3250. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3251. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3252. /* Program SSCAUXDIV */
  3253. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3254. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3255. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3256. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3257. /* Enable modulator and associated divider */
  3258. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3259. temp &= ~SBI_SSCCTL_DISABLE;
  3260. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3261. /* Wait for initialization time */
  3262. udelay(24);
  3263. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3264. mutex_unlock(&dev_priv->dpio_lock);
  3265. }
  3266. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3267. enum pipe pch_transcoder)
  3268. {
  3269. struct drm_device *dev = crtc->base.dev;
  3270. struct drm_i915_private *dev_priv = dev->dev_private;
  3271. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3272. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3273. I915_READ(HTOTAL(cpu_transcoder)));
  3274. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3275. I915_READ(HBLANK(cpu_transcoder)));
  3276. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3277. I915_READ(HSYNC(cpu_transcoder)));
  3278. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3279. I915_READ(VTOTAL(cpu_transcoder)));
  3280. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3281. I915_READ(VBLANK(cpu_transcoder)));
  3282. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3283. I915_READ(VSYNC(cpu_transcoder)));
  3284. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3285. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3286. }
  3287. static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
  3288. {
  3289. struct drm_i915_private *dev_priv = dev->dev_private;
  3290. uint32_t temp;
  3291. temp = I915_READ(SOUTH_CHICKEN1);
  3292. if (temp & FDI_BC_BIFURCATION_SELECT)
  3293. return;
  3294. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3295. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3296. temp |= FDI_BC_BIFURCATION_SELECT;
  3297. DRM_DEBUG_KMS("enabling fdi C rx\n");
  3298. I915_WRITE(SOUTH_CHICKEN1, temp);
  3299. POSTING_READ(SOUTH_CHICKEN1);
  3300. }
  3301. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3302. {
  3303. struct drm_device *dev = intel_crtc->base.dev;
  3304. struct drm_i915_private *dev_priv = dev->dev_private;
  3305. switch (intel_crtc->pipe) {
  3306. case PIPE_A:
  3307. break;
  3308. case PIPE_B:
  3309. if (intel_crtc->config->fdi_lanes > 2)
  3310. WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
  3311. else
  3312. cpt_enable_fdi_bc_bifurcation(dev);
  3313. break;
  3314. case PIPE_C:
  3315. cpt_enable_fdi_bc_bifurcation(dev);
  3316. break;
  3317. default:
  3318. BUG();
  3319. }
  3320. }
  3321. /*
  3322. * Enable PCH resources required for PCH ports:
  3323. * - PCH PLLs
  3324. * - FDI training & RX/TX
  3325. * - update transcoder timings
  3326. * - DP transcoding bits
  3327. * - transcoder
  3328. */
  3329. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3330. {
  3331. struct drm_device *dev = crtc->dev;
  3332. struct drm_i915_private *dev_priv = dev->dev_private;
  3333. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3334. int pipe = intel_crtc->pipe;
  3335. u32 reg, temp;
  3336. assert_pch_transcoder_disabled(dev_priv, pipe);
  3337. if (IS_IVYBRIDGE(dev))
  3338. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3339. /* Write the TU size bits before fdi link training, so that error
  3340. * detection works. */
  3341. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3342. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3343. /* For PCH output, training FDI link */
  3344. dev_priv->display.fdi_link_train(crtc);
  3345. /* We need to program the right clock selection before writing the pixel
  3346. * mutliplier into the DPLL. */
  3347. if (HAS_PCH_CPT(dev)) {
  3348. u32 sel;
  3349. temp = I915_READ(PCH_DPLL_SEL);
  3350. temp |= TRANS_DPLL_ENABLE(pipe);
  3351. sel = TRANS_DPLLB_SEL(pipe);
  3352. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3353. temp |= sel;
  3354. else
  3355. temp &= ~sel;
  3356. I915_WRITE(PCH_DPLL_SEL, temp);
  3357. }
  3358. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3359. * transcoder, and we actually should do this to not upset any PCH
  3360. * transcoder that already use the clock when we share it.
  3361. *
  3362. * Note that enable_shared_dpll tries to do the right thing, but
  3363. * get_shared_dpll unconditionally resets the pll - we need that to have
  3364. * the right LVDS enable sequence. */
  3365. intel_enable_shared_dpll(intel_crtc);
  3366. /* set transcoder timing, panel must allow it */
  3367. assert_panel_unlocked(dev_priv, pipe);
  3368. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3369. intel_fdi_normal_train(crtc);
  3370. /* For PCH DP, enable TRANS_DP_CTL */
  3371. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3372. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3373. reg = TRANS_DP_CTL(pipe);
  3374. temp = I915_READ(reg);
  3375. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3376. TRANS_DP_SYNC_MASK |
  3377. TRANS_DP_BPC_MASK);
  3378. temp |= (TRANS_DP_OUTPUT_ENABLE |
  3379. TRANS_DP_ENH_FRAMING);
  3380. temp |= bpc << 9; /* same format but at 11:9 */
  3381. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3382. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3383. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3384. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3385. switch (intel_trans_dp_port_sel(crtc)) {
  3386. case PCH_DP_B:
  3387. temp |= TRANS_DP_PORT_SEL_B;
  3388. break;
  3389. case PCH_DP_C:
  3390. temp |= TRANS_DP_PORT_SEL_C;
  3391. break;
  3392. case PCH_DP_D:
  3393. temp |= TRANS_DP_PORT_SEL_D;
  3394. break;
  3395. default:
  3396. BUG();
  3397. }
  3398. I915_WRITE(reg, temp);
  3399. }
  3400. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3401. }
  3402. static void lpt_pch_enable(struct drm_crtc *crtc)
  3403. {
  3404. struct drm_device *dev = crtc->dev;
  3405. struct drm_i915_private *dev_priv = dev->dev_private;
  3406. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3407. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3408. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3409. lpt_program_iclkip(crtc);
  3410. /* Set transcoder timing. */
  3411. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3412. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3413. }
  3414. void intel_put_shared_dpll(struct intel_crtc *crtc)
  3415. {
  3416. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3417. if (pll == NULL)
  3418. return;
  3419. if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
  3420. WARN(1, "bad %s crtc mask\n", pll->name);
  3421. return;
  3422. }
  3423. pll->config.crtc_mask &= ~(1 << crtc->pipe);
  3424. if (pll->config.crtc_mask == 0) {
  3425. WARN_ON(pll->on);
  3426. WARN_ON(pll->active);
  3427. }
  3428. crtc->config->shared_dpll = DPLL_ID_PRIVATE;
  3429. }
  3430. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3431. struct intel_crtc_state *crtc_state)
  3432. {
  3433. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3434. struct intel_shared_dpll *pll;
  3435. enum intel_dpll_id i;
  3436. if (HAS_PCH_IBX(dev_priv->dev)) {
  3437. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3438. i = (enum intel_dpll_id) crtc->pipe;
  3439. pll = &dev_priv->shared_dplls[i];
  3440. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3441. crtc->base.base.id, pll->name);
  3442. WARN_ON(pll->new_config->crtc_mask);
  3443. goto found;
  3444. }
  3445. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3446. pll = &dev_priv->shared_dplls[i];
  3447. /* Only want to check enabled timings first */
  3448. if (pll->new_config->crtc_mask == 0)
  3449. continue;
  3450. if (memcmp(&crtc_state->dpll_hw_state,
  3451. &pll->new_config->hw_state,
  3452. sizeof(pll->new_config->hw_state)) == 0) {
  3453. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3454. crtc->base.base.id, pll->name,
  3455. pll->new_config->crtc_mask,
  3456. pll->active);
  3457. goto found;
  3458. }
  3459. }
  3460. /* Ok no matching timings, maybe there's a free one? */
  3461. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3462. pll = &dev_priv->shared_dplls[i];
  3463. if (pll->new_config->crtc_mask == 0) {
  3464. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3465. crtc->base.base.id, pll->name);
  3466. goto found;
  3467. }
  3468. }
  3469. return NULL;
  3470. found:
  3471. if (pll->new_config->crtc_mask == 0)
  3472. pll->new_config->hw_state = crtc_state->dpll_hw_state;
  3473. crtc_state->shared_dpll = i;
  3474. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3475. pipe_name(crtc->pipe));
  3476. pll->new_config->crtc_mask |= 1 << crtc->pipe;
  3477. return pll;
  3478. }
  3479. /**
  3480. * intel_shared_dpll_start_config - start a new PLL staged config
  3481. * @dev_priv: DRM device
  3482. * @clear_pipes: mask of pipes that will have their PLLs freed
  3483. *
  3484. * Starts a new PLL staged config, copying the current config but
  3485. * releasing the references of pipes specified in clear_pipes.
  3486. */
  3487. static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
  3488. unsigned clear_pipes)
  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. pll->new_config = kmemdup(&pll->config, sizeof pll->config,
  3495. GFP_KERNEL);
  3496. if (!pll->new_config)
  3497. goto cleanup;
  3498. pll->new_config->crtc_mask &= ~clear_pipes;
  3499. }
  3500. return 0;
  3501. cleanup:
  3502. while (--i >= 0) {
  3503. pll = &dev_priv->shared_dplls[i];
  3504. kfree(pll->new_config);
  3505. pll->new_config = NULL;
  3506. }
  3507. return -ENOMEM;
  3508. }
  3509. static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
  3510. {
  3511. struct intel_shared_dpll *pll;
  3512. enum intel_dpll_id i;
  3513. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3514. pll = &dev_priv->shared_dplls[i];
  3515. WARN_ON(pll->new_config == &pll->config);
  3516. pll->config = *pll->new_config;
  3517. kfree(pll->new_config);
  3518. pll->new_config = NULL;
  3519. }
  3520. }
  3521. static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
  3522. {
  3523. struct intel_shared_dpll *pll;
  3524. enum intel_dpll_id i;
  3525. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3526. pll = &dev_priv->shared_dplls[i];
  3527. WARN_ON(pll->new_config == &pll->config);
  3528. kfree(pll->new_config);
  3529. pll->new_config = NULL;
  3530. }
  3531. }
  3532. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3533. {
  3534. struct drm_i915_private *dev_priv = dev->dev_private;
  3535. int dslreg = PIPEDSL(pipe);
  3536. u32 temp;
  3537. temp = I915_READ(dslreg);
  3538. udelay(500);
  3539. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3540. if (wait_for(I915_READ(dslreg) != temp, 5))
  3541. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3542. }
  3543. }
  3544. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3545. {
  3546. struct drm_device *dev = crtc->base.dev;
  3547. struct drm_i915_private *dev_priv = dev->dev_private;
  3548. int pipe = crtc->pipe;
  3549. if (crtc->config->pch_pfit.enabled) {
  3550. I915_WRITE(PS_CTL(pipe), PS_ENABLE);
  3551. I915_WRITE(PS_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3552. I915_WRITE(PS_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3553. }
  3554. }
  3555. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3556. {
  3557. struct drm_device *dev = crtc->base.dev;
  3558. struct drm_i915_private *dev_priv = dev->dev_private;
  3559. int pipe = crtc->pipe;
  3560. if (crtc->config->pch_pfit.enabled) {
  3561. /* Force use of hard-coded filter coefficients
  3562. * as some pre-programmed values are broken,
  3563. * e.g. x201.
  3564. */
  3565. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3566. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3567. PF_PIPE_SEL_IVB(pipe));
  3568. else
  3569. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3570. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3571. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3572. }
  3573. }
  3574. static void intel_enable_sprite_planes(struct drm_crtc *crtc)
  3575. {
  3576. struct drm_device *dev = crtc->dev;
  3577. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3578. struct drm_plane *plane;
  3579. struct intel_plane *intel_plane;
  3580. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3581. intel_plane = to_intel_plane(plane);
  3582. if (intel_plane->pipe == pipe)
  3583. intel_plane_restore(&intel_plane->base);
  3584. }
  3585. }
  3586. static void intel_disable_sprite_planes(struct drm_crtc *crtc)
  3587. {
  3588. struct drm_device *dev = crtc->dev;
  3589. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3590. struct drm_plane *plane;
  3591. struct intel_plane *intel_plane;
  3592. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3593. intel_plane = to_intel_plane(plane);
  3594. if (intel_plane->pipe == pipe)
  3595. plane->funcs->disable_plane(plane);
  3596. }
  3597. }
  3598. void hsw_enable_ips(struct intel_crtc *crtc)
  3599. {
  3600. struct drm_device *dev = crtc->base.dev;
  3601. struct drm_i915_private *dev_priv = dev->dev_private;
  3602. if (!crtc->config->ips_enabled)
  3603. return;
  3604. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3605. intel_wait_for_vblank(dev, crtc->pipe);
  3606. assert_plane_enabled(dev_priv, crtc->plane);
  3607. if (IS_BROADWELL(dev)) {
  3608. mutex_lock(&dev_priv->rps.hw_lock);
  3609. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3610. mutex_unlock(&dev_priv->rps.hw_lock);
  3611. /* Quoting Art Runyan: "its not safe to expect any particular
  3612. * value in IPS_CTL bit 31 after enabling IPS through the
  3613. * mailbox." Moreover, the mailbox may return a bogus state,
  3614. * so we need to just enable it and continue on.
  3615. */
  3616. } else {
  3617. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3618. /* The bit only becomes 1 in the next vblank, so this wait here
  3619. * is essentially intel_wait_for_vblank. If we don't have this
  3620. * and don't wait for vblanks until the end of crtc_enable, then
  3621. * the HW state readout code will complain that the expected
  3622. * IPS_CTL value is not the one we read. */
  3623. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3624. DRM_ERROR("Timed out waiting for IPS enable\n");
  3625. }
  3626. }
  3627. void hsw_disable_ips(struct intel_crtc *crtc)
  3628. {
  3629. struct drm_device *dev = crtc->base.dev;
  3630. struct drm_i915_private *dev_priv = dev->dev_private;
  3631. if (!crtc->config->ips_enabled)
  3632. return;
  3633. assert_plane_enabled(dev_priv, crtc->plane);
  3634. if (IS_BROADWELL(dev)) {
  3635. mutex_lock(&dev_priv->rps.hw_lock);
  3636. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3637. mutex_unlock(&dev_priv->rps.hw_lock);
  3638. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3639. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3640. DRM_ERROR("Timed out waiting for IPS disable\n");
  3641. } else {
  3642. I915_WRITE(IPS_CTL, 0);
  3643. POSTING_READ(IPS_CTL);
  3644. }
  3645. /* We need to wait for a vblank before we can disable the plane. */
  3646. intel_wait_for_vblank(dev, crtc->pipe);
  3647. }
  3648. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3649. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3650. {
  3651. struct drm_device *dev = crtc->dev;
  3652. struct drm_i915_private *dev_priv = dev->dev_private;
  3653. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3654. enum pipe pipe = intel_crtc->pipe;
  3655. int palreg = PALETTE(pipe);
  3656. int i;
  3657. bool reenable_ips = false;
  3658. /* The clocks have to be on to load the palette. */
  3659. if (!crtc->state->enable || !intel_crtc->active)
  3660. return;
  3661. if (!HAS_PCH_SPLIT(dev_priv->dev)) {
  3662. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3663. assert_dsi_pll_enabled(dev_priv);
  3664. else
  3665. assert_pll_enabled(dev_priv, pipe);
  3666. }
  3667. /* use legacy palette for Ironlake */
  3668. if (!HAS_GMCH_DISPLAY(dev))
  3669. palreg = LGC_PALETTE(pipe);
  3670. /* Workaround : Do not read or write the pipe palette/gamma data while
  3671. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3672. */
  3673. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  3674. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3675. GAMMA_MODE_MODE_SPLIT)) {
  3676. hsw_disable_ips(intel_crtc);
  3677. reenable_ips = true;
  3678. }
  3679. for (i = 0; i < 256; i++) {
  3680. I915_WRITE(palreg + 4 * i,
  3681. (intel_crtc->lut_r[i] << 16) |
  3682. (intel_crtc->lut_g[i] << 8) |
  3683. intel_crtc->lut_b[i]);
  3684. }
  3685. if (reenable_ips)
  3686. hsw_enable_ips(intel_crtc);
  3687. }
  3688. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  3689. {
  3690. if (!enable && intel_crtc->overlay) {
  3691. struct drm_device *dev = intel_crtc->base.dev;
  3692. struct drm_i915_private *dev_priv = dev->dev_private;
  3693. mutex_lock(&dev->struct_mutex);
  3694. dev_priv->mm.interruptible = false;
  3695. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3696. dev_priv->mm.interruptible = true;
  3697. mutex_unlock(&dev->struct_mutex);
  3698. }
  3699. /* Let userspace switch the overlay on again. In most cases userspace
  3700. * has to recompute where to put it anyway.
  3701. */
  3702. }
  3703. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  3704. {
  3705. struct drm_device *dev = crtc->dev;
  3706. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3707. int pipe = intel_crtc->pipe;
  3708. intel_enable_primary_hw_plane(crtc->primary, crtc);
  3709. intel_enable_sprite_planes(crtc);
  3710. intel_crtc_update_cursor(crtc, true);
  3711. intel_crtc_dpms_overlay(intel_crtc, true);
  3712. hsw_enable_ips(intel_crtc);
  3713. mutex_lock(&dev->struct_mutex);
  3714. intel_fbc_update(dev);
  3715. mutex_unlock(&dev->struct_mutex);
  3716. /*
  3717. * FIXME: Once we grow proper nuclear flip support out of this we need
  3718. * to compute the mask of flip planes precisely. For the time being
  3719. * consider this a flip from a NULL plane.
  3720. */
  3721. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3722. }
  3723. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  3724. {
  3725. struct drm_device *dev = crtc->dev;
  3726. struct drm_i915_private *dev_priv = dev->dev_private;
  3727. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3728. int pipe = intel_crtc->pipe;
  3729. intel_crtc_wait_for_pending_flips(crtc);
  3730. if (dev_priv->fbc.crtc == intel_crtc)
  3731. intel_fbc_disable(dev);
  3732. hsw_disable_ips(intel_crtc);
  3733. intel_crtc_dpms_overlay(intel_crtc, false);
  3734. intel_crtc_update_cursor(crtc, false);
  3735. intel_disable_sprite_planes(crtc);
  3736. intel_disable_primary_hw_plane(crtc->primary, crtc);
  3737. /*
  3738. * FIXME: Once we grow proper nuclear flip support out of this we need
  3739. * to compute the mask of flip planes precisely. For the time being
  3740. * consider this a flip to a NULL plane.
  3741. */
  3742. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3743. }
  3744. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  3745. {
  3746. struct drm_device *dev = crtc->dev;
  3747. struct drm_i915_private *dev_priv = dev->dev_private;
  3748. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3749. struct intel_encoder *encoder;
  3750. int pipe = intel_crtc->pipe;
  3751. WARN_ON(!crtc->state->enable);
  3752. if (intel_crtc->active)
  3753. return;
  3754. if (intel_crtc->config->has_pch_encoder)
  3755. intel_prepare_shared_dpll(intel_crtc);
  3756. if (intel_crtc->config->has_dp_encoder)
  3757. intel_dp_set_m_n(intel_crtc, M1_N1);
  3758. intel_set_pipe_timings(intel_crtc);
  3759. if (intel_crtc->config->has_pch_encoder) {
  3760. intel_cpu_transcoder_set_m_n(intel_crtc,
  3761. &intel_crtc->config->fdi_m_n, NULL);
  3762. }
  3763. ironlake_set_pipeconf(crtc);
  3764. intel_crtc->active = true;
  3765. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3766. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  3767. for_each_encoder_on_crtc(dev, crtc, encoder)
  3768. if (encoder->pre_enable)
  3769. encoder->pre_enable(encoder);
  3770. if (intel_crtc->config->has_pch_encoder) {
  3771. /* Note: FDI PLL enabling _must_ be done before we enable the
  3772. * cpu pipes, hence this is separate from all the other fdi/pch
  3773. * enabling. */
  3774. ironlake_fdi_pll_enable(intel_crtc);
  3775. } else {
  3776. assert_fdi_tx_disabled(dev_priv, pipe);
  3777. assert_fdi_rx_disabled(dev_priv, pipe);
  3778. }
  3779. ironlake_pfit_enable(intel_crtc);
  3780. /*
  3781. * On ILK+ LUT must be loaded before the pipe is running but with
  3782. * clocks enabled
  3783. */
  3784. intel_crtc_load_lut(crtc);
  3785. intel_update_watermarks(crtc);
  3786. intel_enable_pipe(intel_crtc);
  3787. if (intel_crtc->config->has_pch_encoder)
  3788. ironlake_pch_enable(crtc);
  3789. assert_vblank_disabled(crtc);
  3790. drm_crtc_vblank_on(crtc);
  3791. for_each_encoder_on_crtc(dev, crtc, encoder)
  3792. encoder->enable(encoder);
  3793. if (HAS_PCH_CPT(dev))
  3794. cpt_verify_modeset(dev, intel_crtc->pipe);
  3795. intel_crtc_enable_planes(crtc);
  3796. }
  3797. /* IPS only exists on ULT machines and is tied to pipe A. */
  3798. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  3799. {
  3800. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  3801. }
  3802. /*
  3803. * This implements the workaround described in the "notes" section of the mode
  3804. * set sequence documentation. When going from no pipes or single pipe to
  3805. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  3806. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  3807. */
  3808. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  3809. {
  3810. struct drm_device *dev = crtc->base.dev;
  3811. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  3812. /* We want to get the other_active_crtc only if there's only 1 other
  3813. * active crtc. */
  3814. for_each_intel_crtc(dev, crtc_it) {
  3815. if (!crtc_it->active || crtc_it == crtc)
  3816. continue;
  3817. if (other_active_crtc)
  3818. return;
  3819. other_active_crtc = crtc_it;
  3820. }
  3821. if (!other_active_crtc)
  3822. return;
  3823. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3824. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3825. }
  3826. static void haswell_crtc_enable(struct drm_crtc *crtc)
  3827. {
  3828. struct drm_device *dev = crtc->dev;
  3829. struct drm_i915_private *dev_priv = dev->dev_private;
  3830. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3831. struct intel_encoder *encoder;
  3832. int pipe = intel_crtc->pipe;
  3833. WARN_ON(!crtc->state->enable);
  3834. if (intel_crtc->active)
  3835. return;
  3836. if (intel_crtc_to_shared_dpll(intel_crtc))
  3837. intel_enable_shared_dpll(intel_crtc);
  3838. if (intel_crtc->config->has_dp_encoder)
  3839. intel_dp_set_m_n(intel_crtc, M1_N1);
  3840. intel_set_pipe_timings(intel_crtc);
  3841. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  3842. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  3843. intel_crtc->config->pixel_multiplier - 1);
  3844. }
  3845. if (intel_crtc->config->has_pch_encoder) {
  3846. intel_cpu_transcoder_set_m_n(intel_crtc,
  3847. &intel_crtc->config->fdi_m_n, NULL);
  3848. }
  3849. haswell_set_pipeconf(crtc);
  3850. intel_set_pipe_csc(crtc);
  3851. intel_crtc->active = true;
  3852. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3853. for_each_encoder_on_crtc(dev, crtc, encoder)
  3854. if (encoder->pre_enable)
  3855. encoder->pre_enable(encoder);
  3856. if (intel_crtc->config->has_pch_encoder) {
  3857. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  3858. true);
  3859. dev_priv->display.fdi_link_train(crtc);
  3860. }
  3861. intel_ddi_enable_pipe_clock(intel_crtc);
  3862. if (IS_SKYLAKE(dev))
  3863. skylake_pfit_enable(intel_crtc);
  3864. else
  3865. ironlake_pfit_enable(intel_crtc);
  3866. /*
  3867. * On ILK+ LUT must be loaded before the pipe is running but with
  3868. * clocks enabled
  3869. */
  3870. intel_crtc_load_lut(crtc);
  3871. intel_ddi_set_pipe_settings(crtc);
  3872. intel_ddi_enable_transcoder_func(crtc);
  3873. intel_update_watermarks(crtc);
  3874. intel_enable_pipe(intel_crtc);
  3875. if (intel_crtc->config->has_pch_encoder)
  3876. lpt_pch_enable(crtc);
  3877. if (intel_crtc->config->dp_encoder_is_mst)
  3878. intel_ddi_set_vc_payload_alloc(crtc, true);
  3879. assert_vblank_disabled(crtc);
  3880. drm_crtc_vblank_on(crtc);
  3881. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3882. encoder->enable(encoder);
  3883. intel_opregion_notify_encoder(encoder, true);
  3884. }
  3885. /* If we change the relative order between pipe/planes enabling, we need
  3886. * to change the workaround. */
  3887. haswell_mode_set_planes_workaround(intel_crtc);
  3888. intel_crtc_enable_planes(crtc);
  3889. }
  3890. static void skylake_pfit_disable(struct intel_crtc *crtc)
  3891. {
  3892. struct drm_device *dev = crtc->base.dev;
  3893. struct drm_i915_private *dev_priv = dev->dev_private;
  3894. int pipe = crtc->pipe;
  3895. /* To avoid upsetting the power well on haswell only disable the pfit if
  3896. * it's in use. The hw state code will make sure we get this right. */
  3897. if (crtc->config->pch_pfit.enabled) {
  3898. I915_WRITE(PS_CTL(pipe), 0);
  3899. I915_WRITE(PS_WIN_POS(pipe), 0);
  3900. I915_WRITE(PS_WIN_SZ(pipe), 0);
  3901. }
  3902. }
  3903. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  3904. {
  3905. struct drm_device *dev = crtc->base.dev;
  3906. struct drm_i915_private *dev_priv = dev->dev_private;
  3907. int pipe = crtc->pipe;
  3908. /* To avoid upsetting the power well on haswell only disable the pfit if
  3909. * it's in use. The hw state code will make sure we get this right. */
  3910. if (crtc->config->pch_pfit.enabled) {
  3911. I915_WRITE(PF_CTL(pipe), 0);
  3912. I915_WRITE(PF_WIN_POS(pipe), 0);
  3913. I915_WRITE(PF_WIN_SZ(pipe), 0);
  3914. }
  3915. }
  3916. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  3917. {
  3918. struct drm_device *dev = crtc->dev;
  3919. struct drm_i915_private *dev_priv = dev->dev_private;
  3920. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3921. struct intel_encoder *encoder;
  3922. int pipe = intel_crtc->pipe;
  3923. u32 reg, temp;
  3924. if (!intel_crtc->active)
  3925. return;
  3926. intel_crtc_disable_planes(crtc);
  3927. for_each_encoder_on_crtc(dev, crtc, encoder)
  3928. encoder->disable(encoder);
  3929. drm_crtc_vblank_off(crtc);
  3930. assert_vblank_disabled(crtc);
  3931. if (intel_crtc->config->has_pch_encoder)
  3932. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  3933. intel_disable_pipe(intel_crtc);
  3934. ironlake_pfit_disable(intel_crtc);
  3935. for_each_encoder_on_crtc(dev, crtc, encoder)
  3936. if (encoder->post_disable)
  3937. encoder->post_disable(encoder);
  3938. if (intel_crtc->config->has_pch_encoder) {
  3939. ironlake_fdi_disable(crtc);
  3940. ironlake_disable_pch_transcoder(dev_priv, pipe);
  3941. if (HAS_PCH_CPT(dev)) {
  3942. /* disable TRANS_DP_CTL */
  3943. reg = TRANS_DP_CTL(pipe);
  3944. temp = I915_READ(reg);
  3945. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  3946. TRANS_DP_PORT_SEL_MASK);
  3947. temp |= TRANS_DP_PORT_SEL_NONE;
  3948. I915_WRITE(reg, temp);
  3949. /* disable DPLL_SEL */
  3950. temp = I915_READ(PCH_DPLL_SEL);
  3951. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  3952. I915_WRITE(PCH_DPLL_SEL, temp);
  3953. }
  3954. /* disable PCH DPLL */
  3955. intel_disable_shared_dpll(intel_crtc);
  3956. ironlake_fdi_pll_disable(intel_crtc);
  3957. }
  3958. intel_crtc->active = false;
  3959. intel_update_watermarks(crtc);
  3960. mutex_lock(&dev->struct_mutex);
  3961. intel_fbc_update(dev);
  3962. mutex_unlock(&dev->struct_mutex);
  3963. }
  3964. static void haswell_crtc_disable(struct drm_crtc *crtc)
  3965. {
  3966. struct drm_device *dev = crtc->dev;
  3967. struct drm_i915_private *dev_priv = dev->dev_private;
  3968. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3969. struct intel_encoder *encoder;
  3970. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3971. if (!intel_crtc->active)
  3972. return;
  3973. intel_crtc_disable_planes(crtc);
  3974. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3975. intel_opregion_notify_encoder(encoder, false);
  3976. encoder->disable(encoder);
  3977. }
  3978. drm_crtc_vblank_off(crtc);
  3979. assert_vblank_disabled(crtc);
  3980. if (intel_crtc->config->has_pch_encoder)
  3981. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  3982. false);
  3983. intel_disable_pipe(intel_crtc);
  3984. if (intel_crtc->config->dp_encoder_is_mst)
  3985. intel_ddi_set_vc_payload_alloc(crtc, false);
  3986. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  3987. if (IS_SKYLAKE(dev))
  3988. skylake_pfit_disable(intel_crtc);
  3989. else
  3990. ironlake_pfit_disable(intel_crtc);
  3991. intel_ddi_disable_pipe_clock(intel_crtc);
  3992. if (intel_crtc->config->has_pch_encoder) {
  3993. lpt_disable_pch_transcoder(dev_priv);
  3994. intel_ddi_fdi_disable(crtc);
  3995. }
  3996. for_each_encoder_on_crtc(dev, crtc, encoder)
  3997. if (encoder->post_disable)
  3998. encoder->post_disable(encoder);
  3999. intel_crtc->active = false;
  4000. intel_update_watermarks(crtc);
  4001. mutex_lock(&dev->struct_mutex);
  4002. intel_fbc_update(dev);
  4003. mutex_unlock(&dev->struct_mutex);
  4004. if (intel_crtc_to_shared_dpll(intel_crtc))
  4005. intel_disable_shared_dpll(intel_crtc);
  4006. }
  4007. static void ironlake_crtc_off(struct drm_crtc *crtc)
  4008. {
  4009. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4010. intel_put_shared_dpll(intel_crtc);
  4011. }
  4012. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4013. {
  4014. struct drm_device *dev = crtc->base.dev;
  4015. struct drm_i915_private *dev_priv = dev->dev_private;
  4016. struct intel_crtc_state *pipe_config = crtc->config;
  4017. if (!pipe_config->gmch_pfit.control)
  4018. return;
  4019. /*
  4020. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4021. * according to register description and PRM.
  4022. */
  4023. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4024. assert_pipe_disabled(dev_priv, crtc->pipe);
  4025. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4026. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4027. /* Border color in case we don't scale up to the full screen. Black by
  4028. * default, change to something else for debugging. */
  4029. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4030. }
  4031. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4032. {
  4033. switch (port) {
  4034. case PORT_A:
  4035. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  4036. case PORT_B:
  4037. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  4038. case PORT_C:
  4039. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  4040. case PORT_D:
  4041. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  4042. default:
  4043. WARN_ON_ONCE(1);
  4044. return POWER_DOMAIN_PORT_OTHER;
  4045. }
  4046. }
  4047. #define for_each_power_domain(domain, mask) \
  4048. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  4049. if ((1 << (domain)) & (mask))
  4050. enum intel_display_power_domain
  4051. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4052. {
  4053. struct drm_device *dev = intel_encoder->base.dev;
  4054. struct intel_digital_port *intel_dig_port;
  4055. switch (intel_encoder->type) {
  4056. case INTEL_OUTPUT_UNKNOWN:
  4057. /* Only DDI platforms should ever use this output type */
  4058. WARN_ON_ONCE(!HAS_DDI(dev));
  4059. case INTEL_OUTPUT_DISPLAYPORT:
  4060. case INTEL_OUTPUT_HDMI:
  4061. case INTEL_OUTPUT_EDP:
  4062. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4063. return port_to_power_domain(intel_dig_port->port);
  4064. case INTEL_OUTPUT_DP_MST:
  4065. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4066. return port_to_power_domain(intel_dig_port->port);
  4067. case INTEL_OUTPUT_ANALOG:
  4068. return POWER_DOMAIN_PORT_CRT;
  4069. case INTEL_OUTPUT_DSI:
  4070. return POWER_DOMAIN_PORT_DSI;
  4071. default:
  4072. return POWER_DOMAIN_PORT_OTHER;
  4073. }
  4074. }
  4075. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  4076. {
  4077. struct drm_device *dev = crtc->dev;
  4078. struct intel_encoder *intel_encoder;
  4079. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4080. enum pipe pipe = intel_crtc->pipe;
  4081. unsigned long mask;
  4082. enum transcoder transcoder;
  4083. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  4084. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4085. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4086. if (intel_crtc->config->pch_pfit.enabled ||
  4087. intel_crtc->config->pch_pfit.force_thru)
  4088. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4089. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4090. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4091. return mask;
  4092. }
  4093. static void modeset_update_crtc_power_domains(struct drm_device *dev)
  4094. {
  4095. struct drm_i915_private *dev_priv = dev->dev_private;
  4096. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  4097. struct intel_crtc *crtc;
  4098. /*
  4099. * First get all needed power domains, then put all unneeded, to avoid
  4100. * any unnecessary toggling of the power wells.
  4101. */
  4102. for_each_intel_crtc(dev, crtc) {
  4103. enum intel_display_power_domain domain;
  4104. if (!crtc->base.state->enable)
  4105. continue;
  4106. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  4107. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  4108. intel_display_power_get(dev_priv, domain);
  4109. }
  4110. if (dev_priv->display.modeset_global_resources)
  4111. dev_priv->display.modeset_global_resources(dev);
  4112. for_each_intel_crtc(dev, crtc) {
  4113. enum intel_display_power_domain domain;
  4114. for_each_power_domain(domain, crtc->enabled_power_domains)
  4115. intel_display_power_put(dev_priv, domain);
  4116. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  4117. }
  4118. intel_display_set_init_power(dev_priv, false);
  4119. }
  4120. /* returns HPLL frequency in kHz */
  4121. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  4122. {
  4123. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  4124. /* Obtain SKU information */
  4125. mutex_lock(&dev_priv->dpio_lock);
  4126. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  4127. CCK_FUSE_HPLL_FREQ_MASK;
  4128. mutex_unlock(&dev_priv->dpio_lock);
  4129. return vco_freq[hpll_freq] * 1000;
  4130. }
  4131. static void vlv_update_cdclk(struct drm_device *dev)
  4132. {
  4133. struct drm_i915_private *dev_priv = dev->dev_private;
  4134. dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4135. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4136. dev_priv->vlv_cdclk_freq);
  4137. /*
  4138. * Program the gmbus_freq based on the cdclk frequency.
  4139. * BSpec erroneously claims we should aim for 4MHz, but
  4140. * in fact 1MHz is the correct frequency.
  4141. */
  4142. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->vlv_cdclk_freq, 1000));
  4143. }
  4144. /* Adjust CDclk dividers to allow high res or save power if possible */
  4145. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4146. {
  4147. struct drm_i915_private *dev_priv = dev->dev_private;
  4148. u32 val, cmd;
  4149. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  4150. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4151. cmd = 2;
  4152. else if (cdclk == 266667)
  4153. cmd = 1;
  4154. else
  4155. cmd = 0;
  4156. mutex_lock(&dev_priv->rps.hw_lock);
  4157. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4158. val &= ~DSPFREQGUAR_MASK;
  4159. val |= (cmd << DSPFREQGUAR_SHIFT);
  4160. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4161. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4162. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4163. 50)) {
  4164. DRM_ERROR("timed out waiting for CDclk change\n");
  4165. }
  4166. mutex_unlock(&dev_priv->rps.hw_lock);
  4167. if (cdclk == 400000) {
  4168. u32 divider;
  4169. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4170. mutex_lock(&dev_priv->dpio_lock);
  4171. /* adjust cdclk divider */
  4172. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4173. val &= ~DISPLAY_FREQUENCY_VALUES;
  4174. val |= divider;
  4175. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4176. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4177. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4178. 50))
  4179. DRM_ERROR("timed out waiting for CDclk change\n");
  4180. mutex_unlock(&dev_priv->dpio_lock);
  4181. }
  4182. mutex_lock(&dev_priv->dpio_lock);
  4183. /* adjust self-refresh exit latency value */
  4184. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4185. val &= ~0x7f;
  4186. /*
  4187. * For high bandwidth configs, we set a higher latency in the bunit
  4188. * so that the core display fetch happens in time to avoid underruns.
  4189. */
  4190. if (cdclk == 400000)
  4191. val |= 4500 / 250; /* 4.5 usec */
  4192. else
  4193. val |= 3000 / 250; /* 3.0 usec */
  4194. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4195. mutex_unlock(&dev_priv->dpio_lock);
  4196. vlv_update_cdclk(dev);
  4197. }
  4198. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4199. {
  4200. struct drm_i915_private *dev_priv = dev->dev_private;
  4201. u32 val, cmd;
  4202. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  4203. switch (cdclk) {
  4204. case 400000:
  4205. cmd = 3;
  4206. break;
  4207. case 333333:
  4208. case 320000:
  4209. cmd = 2;
  4210. break;
  4211. case 266667:
  4212. cmd = 1;
  4213. break;
  4214. case 200000:
  4215. cmd = 0;
  4216. break;
  4217. default:
  4218. MISSING_CASE(cdclk);
  4219. return;
  4220. }
  4221. mutex_lock(&dev_priv->rps.hw_lock);
  4222. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4223. val &= ~DSPFREQGUAR_MASK_CHV;
  4224. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4225. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4226. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4227. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4228. 50)) {
  4229. DRM_ERROR("timed out waiting for CDclk change\n");
  4230. }
  4231. mutex_unlock(&dev_priv->rps.hw_lock);
  4232. vlv_update_cdclk(dev);
  4233. }
  4234. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4235. int max_pixclk)
  4236. {
  4237. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4238. /* FIXME: Punit isn't quite ready yet */
  4239. if (IS_CHERRYVIEW(dev_priv->dev))
  4240. return 400000;
  4241. /*
  4242. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4243. * 200MHz
  4244. * 267MHz
  4245. * 320/333MHz (depends on HPLL freq)
  4246. * 400MHz
  4247. * So we check to see whether we're above 90% of the lower bin and
  4248. * adjust if needed.
  4249. *
  4250. * We seem to get an unstable or solid color picture at 200MHz.
  4251. * Not sure what's wrong. For now use 200MHz only when all pipes
  4252. * are off.
  4253. */
  4254. if (max_pixclk > freq_320*9/10)
  4255. return 400000;
  4256. else if (max_pixclk > 266667*9/10)
  4257. return freq_320;
  4258. else if (max_pixclk > 0)
  4259. return 266667;
  4260. else
  4261. return 200000;
  4262. }
  4263. /* compute the max pixel clock for new configuration */
  4264. static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
  4265. {
  4266. struct drm_device *dev = dev_priv->dev;
  4267. struct intel_crtc *intel_crtc;
  4268. int max_pixclk = 0;
  4269. for_each_intel_crtc(dev, intel_crtc) {
  4270. if (intel_crtc->new_enabled)
  4271. max_pixclk = max(max_pixclk,
  4272. intel_crtc->new_config->base.adjusted_mode.crtc_clock);
  4273. }
  4274. return max_pixclk;
  4275. }
  4276. static void valleyview_modeset_global_pipes(struct drm_device *dev,
  4277. unsigned *prepare_pipes)
  4278. {
  4279. struct drm_i915_private *dev_priv = dev->dev_private;
  4280. struct intel_crtc *intel_crtc;
  4281. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4282. if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
  4283. dev_priv->vlv_cdclk_freq)
  4284. return;
  4285. /* disable/enable all currently active pipes while we change cdclk */
  4286. for_each_intel_crtc(dev, intel_crtc)
  4287. if (intel_crtc->base.state->enable)
  4288. *prepare_pipes |= (1 << intel_crtc->pipe);
  4289. }
  4290. static void valleyview_modeset_global_resources(struct drm_device *dev)
  4291. {
  4292. struct drm_i915_private *dev_priv = dev->dev_private;
  4293. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4294. int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  4295. if (req_cdclk != dev_priv->vlv_cdclk_freq) {
  4296. /*
  4297. * FIXME: We can end up here with all power domains off, yet
  4298. * with a CDCLK frequency other than the minimum. To account
  4299. * for this take the PIPE-A power domain, which covers the HW
  4300. * blocks needed for the following programming. This can be
  4301. * removed once it's guaranteed that we get here either with
  4302. * the minimum CDCLK set, or the required power domains
  4303. * enabled.
  4304. */
  4305. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  4306. if (IS_CHERRYVIEW(dev))
  4307. cherryview_set_cdclk(dev, req_cdclk);
  4308. else
  4309. valleyview_set_cdclk(dev, req_cdclk);
  4310. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  4311. }
  4312. }
  4313. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  4314. {
  4315. struct drm_device *dev = crtc->dev;
  4316. struct drm_i915_private *dev_priv = to_i915(dev);
  4317. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4318. struct intel_encoder *encoder;
  4319. int pipe = intel_crtc->pipe;
  4320. bool is_dsi;
  4321. WARN_ON(!crtc->state->enable);
  4322. if (intel_crtc->active)
  4323. return;
  4324. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  4325. if (!is_dsi) {
  4326. if (IS_CHERRYVIEW(dev))
  4327. chv_prepare_pll(intel_crtc, intel_crtc->config);
  4328. else
  4329. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  4330. }
  4331. if (intel_crtc->config->has_dp_encoder)
  4332. intel_dp_set_m_n(intel_crtc, M1_N1);
  4333. intel_set_pipe_timings(intel_crtc);
  4334. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  4335. struct drm_i915_private *dev_priv = dev->dev_private;
  4336. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  4337. I915_WRITE(CHV_CANVAS(pipe), 0);
  4338. }
  4339. i9xx_set_pipeconf(intel_crtc);
  4340. intel_crtc->active = true;
  4341. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4342. for_each_encoder_on_crtc(dev, crtc, encoder)
  4343. if (encoder->pre_pll_enable)
  4344. encoder->pre_pll_enable(encoder);
  4345. if (!is_dsi) {
  4346. if (IS_CHERRYVIEW(dev))
  4347. chv_enable_pll(intel_crtc, intel_crtc->config);
  4348. else
  4349. vlv_enable_pll(intel_crtc, intel_crtc->config);
  4350. }
  4351. for_each_encoder_on_crtc(dev, crtc, encoder)
  4352. if (encoder->pre_enable)
  4353. encoder->pre_enable(encoder);
  4354. i9xx_pfit_enable(intel_crtc);
  4355. intel_crtc_load_lut(crtc);
  4356. intel_update_watermarks(crtc);
  4357. intel_enable_pipe(intel_crtc);
  4358. assert_vblank_disabled(crtc);
  4359. drm_crtc_vblank_on(crtc);
  4360. for_each_encoder_on_crtc(dev, crtc, encoder)
  4361. encoder->enable(encoder);
  4362. intel_crtc_enable_planes(crtc);
  4363. /* Underruns don't raise interrupts, so check manually. */
  4364. i9xx_check_fifo_underruns(dev_priv);
  4365. }
  4366. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  4367. {
  4368. struct drm_device *dev = crtc->base.dev;
  4369. struct drm_i915_private *dev_priv = dev->dev_private;
  4370. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  4371. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  4372. }
  4373. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  4374. {
  4375. struct drm_device *dev = crtc->dev;
  4376. struct drm_i915_private *dev_priv = to_i915(dev);
  4377. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4378. struct intel_encoder *encoder;
  4379. int pipe = intel_crtc->pipe;
  4380. WARN_ON(!crtc->state->enable);
  4381. if (intel_crtc->active)
  4382. return;
  4383. i9xx_set_pll_dividers(intel_crtc);
  4384. if (intel_crtc->config->has_dp_encoder)
  4385. intel_dp_set_m_n(intel_crtc, M1_N1);
  4386. intel_set_pipe_timings(intel_crtc);
  4387. i9xx_set_pipeconf(intel_crtc);
  4388. intel_crtc->active = true;
  4389. if (!IS_GEN2(dev))
  4390. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4391. for_each_encoder_on_crtc(dev, crtc, encoder)
  4392. if (encoder->pre_enable)
  4393. encoder->pre_enable(encoder);
  4394. i9xx_enable_pll(intel_crtc);
  4395. i9xx_pfit_enable(intel_crtc);
  4396. intel_crtc_load_lut(crtc);
  4397. intel_update_watermarks(crtc);
  4398. intel_enable_pipe(intel_crtc);
  4399. assert_vblank_disabled(crtc);
  4400. drm_crtc_vblank_on(crtc);
  4401. for_each_encoder_on_crtc(dev, crtc, encoder)
  4402. encoder->enable(encoder);
  4403. intel_crtc_enable_planes(crtc);
  4404. /*
  4405. * Gen2 reports pipe underruns whenever all planes are disabled.
  4406. * So don't enable underrun reporting before at least some planes
  4407. * are enabled.
  4408. * FIXME: Need to fix the logic to work when we turn off all planes
  4409. * but leave the pipe running.
  4410. */
  4411. if (IS_GEN2(dev))
  4412. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4413. /* Underruns don't raise interrupts, so check manually. */
  4414. i9xx_check_fifo_underruns(dev_priv);
  4415. }
  4416. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4417. {
  4418. struct drm_device *dev = crtc->base.dev;
  4419. struct drm_i915_private *dev_priv = dev->dev_private;
  4420. if (!crtc->config->gmch_pfit.control)
  4421. return;
  4422. assert_pipe_disabled(dev_priv, crtc->pipe);
  4423. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4424. I915_READ(PFIT_CONTROL));
  4425. I915_WRITE(PFIT_CONTROL, 0);
  4426. }
  4427. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  4428. {
  4429. struct drm_device *dev = crtc->dev;
  4430. struct drm_i915_private *dev_priv = dev->dev_private;
  4431. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4432. struct intel_encoder *encoder;
  4433. int pipe = intel_crtc->pipe;
  4434. if (!intel_crtc->active)
  4435. return;
  4436. /*
  4437. * Gen2 reports pipe underruns whenever all planes are disabled.
  4438. * So diasble underrun reporting before all the planes get disabled.
  4439. * FIXME: Need to fix the logic to work when we turn off all planes
  4440. * but leave the pipe running.
  4441. */
  4442. if (IS_GEN2(dev))
  4443. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4444. /*
  4445. * Vblank time updates from the shadow to live plane control register
  4446. * are blocked if the memory self-refresh mode is active at that
  4447. * moment. So to make sure the plane gets truly disabled, disable
  4448. * first the self-refresh mode. The self-refresh enable bit in turn
  4449. * will be checked/applied by the HW only at the next frame start
  4450. * event which is after the vblank start event, so we need to have a
  4451. * wait-for-vblank between disabling the plane and the pipe.
  4452. */
  4453. intel_set_memory_cxsr(dev_priv, false);
  4454. intel_crtc_disable_planes(crtc);
  4455. /*
  4456. * On gen2 planes are double buffered but the pipe isn't, so we must
  4457. * wait for planes to fully turn off before disabling the pipe.
  4458. * We also need to wait on all gmch platforms because of the
  4459. * self-refresh mode constraint explained above.
  4460. */
  4461. intel_wait_for_vblank(dev, pipe);
  4462. for_each_encoder_on_crtc(dev, crtc, encoder)
  4463. encoder->disable(encoder);
  4464. drm_crtc_vblank_off(crtc);
  4465. assert_vblank_disabled(crtc);
  4466. intel_disable_pipe(intel_crtc);
  4467. i9xx_pfit_disable(intel_crtc);
  4468. for_each_encoder_on_crtc(dev, crtc, encoder)
  4469. if (encoder->post_disable)
  4470. encoder->post_disable(encoder);
  4471. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  4472. if (IS_CHERRYVIEW(dev))
  4473. chv_disable_pll(dev_priv, pipe);
  4474. else if (IS_VALLEYVIEW(dev))
  4475. vlv_disable_pll(dev_priv, pipe);
  4476. else
  4477. i9xx_disable_pll(intel_crtc);
  4478. }
  4479. if (!IS_GEN2(dev))
  4480. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4481. intel_crtc->active = false;
  4482. intel_update_watermarks(crtc);
  4483. mutex_lock(&dev->struct_mutex);
  4484. intel_fbc_update(dev);
  4485. mutex_unlock(&dev->struct_mutex);
  4486. }
  4487. static void i9xx_crtc_off(struct drm_crtc *crtc)
  4488. {
  4489. }
  4490. /* Master function to enable/disable CRTC and corresponding power wells */
  4491. void intel_crtc_control(struct drm_crtc *crtc, bool enable)
  4492. {
  4493. struct drm_device *dev = crtc->dev;
  4494. struct drm_i915_private *dev_priv = dev->dev_private;
  4495. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4496. enum intel_display_power_domain domain;
  4497. unsigned long domains;
  4498. if (enable) {
  4499. if (!intel_crtc->active) {
  4500. domains = get_crtc_power_domains(crtc);
  4501. for_each_power_domain(domain, domains)
  4502. intel_display_power_get(dev_priv, domain);
  4503. intel_crtc->enabled_power_domains = domains;
  4504. dev_priv->display.crtc_enable(crtc);
  4505. }
  4506. } else {
  4507. if (intel_crtc->active) {
  4508. dev_priv->display.crtc_disable(crtc);
  4509. domains = intel_crtc->enabled_power_domains;
  4510. for_each_power_domain(domain, domains)
  4511. intel_display_power_put(dev_priv, domain);
  4512. intel_crtc->enabled_power_domains = 0;
  4513. }
  4514. }
  4515. }
  4516. /**
  4517. * Sets the power management mode of the pipe and plane.
  4518. */
  4519. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  4520. {
  4521. struct drm_device *dev = crtc->dev;
  4522. struct intel_encoder *intel_encoder;
  4523. bool enable = false;
  4524. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4525. enable |= intel_encoder->connectors_active;
  4526. intel_crtc_control(crtc, enable);
  4527. }
  4528. static void intel_crtc_disable(struct drm_crtc *crtc)
  4529. {
  4530. struct drm_device *dev = crtc->dev;
  4531. struct drm_connector *connector;
  4532. struct drm_i915_private *dev_priv = dev->dev_private;
  4533. /* crtc should still be enabled when we disable it. */
  4534. WARN_ON(!crtc->state->enable);
  4535. dev_priv->display.crtc_disable(crtc);
  4536. dev_priv->display.off(crtc);
  4537. crtc->primary->funcs->disable_plane(crtc->primary);
  4538. /* Update computed state. */
  4539. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  4540. if (!connector->encoder || !connector->encoder->crtc)
  4541. continue;
  4542. if (connector->encoder->crtc != crtc)
  4543. continue;
  4544. connector->dpms = DRM_MODE_DPMS_OFF;
  4545. to_intel_encoder(connector->encoder)->connectors_active = false;
  4546. }
  4547. }
  4548. void intel_encoder_destroy(struct drm_encoder *encoder)
  4549. {
  4550. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4551. drm_encoder_cleanup(encoder);
  4552. kfree(intel_encoder);
  4553. }
  4554. /* Simple dpms helper for encoders with just one connector, no cloning and only
  4555. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  4556. * state of the entire output pipe. */
  4557. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  4558. {
  4559. if (mode == DRM_MODE_DPMS_ON) {
  4560. encoder->connectors_active = true;
  4561. intel_crtc_update_dpms(encoder->base.crtc);
  4562. } else {
  4563. encoder->connectors_active = false;
  4564. intel_crtc_update_dpms(encoder->base.crtc);
  4565. }
  4566. }
  4567. /* Cross check the actual hw state with our own modeset state tracking (and it's
  4568. * internal consistency). */
  4569. static void intel_connector_check_state(struct intel_connector *connector)
  4570. {
  4571. if (connector->get_hw_state(connector)) {
  4572. struct intel_encoder *encoder = connector->encoder;
  4573. struct drm_crtc *crtc;
  4574. bool encoder_enabled;
  4575. enum pipe pipe;
  4576. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4577. connector->base.base.id,
  4578. connector->base.name);
  4579. /* there is no real hw state for MST connectors */
  4580. if (connector->mst_port)
  4581. return;
  4582. I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  4583. "wrong connector dpms state\n");
  4584. I915_STATE_WARN(connector->base.encoder != &encoder->base,
  4585. "active connector not linked to encoder\n");
  4586. if (encoder) {
  4587. I915_STATE_WARN(!encoder->connectors_active,
  4588. "encoder->connectors_active not set\n");
  4589. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  4590. I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
  4591. if (I915_STATE_WARN_ON(!encoder->base.crtc))
  4592. return;
  4593. crtc = encoder->base.crtc;
  4594. I915_STATE_WARN(!crtc->state->enable,
  4595. "crtc not enabled\n");
  4596. I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  4597. I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
  4598. "encoder active on the wrong pipe\n");
  4599. }
  4600. }
  4601. }
  4602. /* Even simpler default implementation, if there's really no special case to
  4603. * consider. */
  4604. void intel_connector_dpms(struct drm_connector *connector, int mode)
  4605. {
  4606. /* All the simple cases only support two dpms states. */
  4607. if (mode != DRM_MODE_DPMS_ON)
  4608. mode = DRM_MODE_DPMS_OFF;
  4609. if (mode == connector->dpms)
  4610. return;
  4611. connector->dpms = mode;
  4612. /* Only need to change hw state when actually enabled */
  4613. if (connector->encoder)
  4614. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  4615. intel_modeset_check_state(connector->dev);
  4616. }
  4617. /* Simple connector->get_hw_state implementation for encoders that support only
  4618. * one connector and no cloning and hence the encoder state determines the state
  4619. * of the connector. */
  4620. bool intel_connector_get_hw_state(struct intel_connector *connector)
  4621. {
  4622. enum pipe pipe = 0;
  4623. struct intel_encoder *encoder = connector->encoder;
  4624. return encoder->get_hw_state(encoder, &pipe);
  4625. }
  4626. static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  4627. struct intel_crtc_state *pipe_config)
  4628. {
  4629. struct drm_i915_private *dev_priv = dev->dev_private;
  4630. struct intel_crtc *pipe_B_crtc =
  4631. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  4632. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  4633. pipe_name(pipe), pipe_config->fdi_lanes);
  4634. if (pipe_config->fdi_lanes > 4) {
  4635. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  4636. pipe_name(pipe), pipe_config->fdi_lanes);
  4637. return false;
  4638. }
  4639. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  4640. if (pipe_config->fdi_lanes > 2) {
  4641. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  4642. pipe_config->fdi_lanes);
  4643. return false;
  4644. } else {
  4645. return true;
  4646. }
  4647. }
  4648. if (INTEL_INFO(dev)->num_pipes == 2)
  4649. return true;
  4650. /* Ivybridge 3 pipe is really complicated */
  4651. switch (pipe) {
  4652. case PIPE_A:
  4653. return true;
  4654. case PIPE_B:
  4655. if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
  4656. pipe_config->fdi_lanes > 2) {
  4657. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4658. pipe_name(pipe), pipe_config->fdi_lanes);
  4659. return false;
  4660. }
  4661. return true;
  4662. case PIPE_C:
  4663. if (!pipe_has_enabled_pch(pipe_B_crtc) ||
  4664. pipe_B_crtc->config->fdi_lanes <= 2) {
  4665. if (pipe_config->fdi_lanes > 2) {
  4666. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4667. pipe_name(pipe), pipe_config->fdi_lanes);
  4668. return false;
  4669. }
  4670. } else {
  4671. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  4672. return false;
  4673. }
  4674. return true;
  4675. default:
  4676. BUG();
  4677. }
  4678. }
  4679. #define RETRY 1
  4680. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  4681. struct intel_crtc_state *pipe_config)
  4682. {
  4683. struct drm_device *dev = intel_crtc->base.dev;
  4684. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  4685. int lane, link_bw, fdi_dotclock;
  4686. bool setup_ok, needs_recompute = false;
  4687. retry:
  4688. /* FDI is a binary signal running at ~2.7GHz, encoding
  4689. * each output octet as 10 bits. The actual frequency
  4690. * is stored as a divider into a 100MHz clock, and the
  4691. * mode pixel clock is stored in units of 1KHz.
  4692. * Hence the bw of each lane in terms of the mode signal
  4693. * is:
  4694. */
  4695. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4696. fdi_dotclock = adjusted_mode->crtc_clock;
  4697. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  4698. pipe_config->pipe_bpp);
  4699. pipe_config->fdi_lanes = lane;
  4700. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  4701. link_bw, &pipe_config->fdi_m_n);
  4702. setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  4703. intel_crtc->pipe, pipe_config);
  4704. if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
  4705. pipe_config->pipe_bpp -= 2*3;
  4706. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  4707. pipe_config->pipe_bpp);
  4708. needs_recompute = true;
  4709. pipe_config->bw_constrained = true;
  4710. goto retry;
  4711. }
  4712. if (needs_recompute)
  4713. return RETRY;
  4714. return setup_ok ? 0 : -EINVAL;
  4715. }
  4716. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  4717. struct intel_crtc_state *pipe_config)
  4718. {
  4719. pipe_config->ips_enabled = i915.enable_ips &&
  4720. hsw_crtc_supports_ips(crtc) &&
  4721. pipe_config->pipe_bpp <= 24;
  4722. }
  4723. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  4724. struct intel_crtc_state *pipe_config)
  4725. {
  4726. struct drm_device *dev = crtc->base.dev;
  4727. struct drm_i915_private *dev_priv = dev->dev_private;
  4728. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  4729. /* FIXME should check pixel clock limits on all platforms */
  4730. if (INTEL_INFO(dev)->gen < 4) {
  4731. int clock_limit =
  4732. dev_priv->display.get_display_clock_speed(dev);
  4733. /*
  4734. * Enable pixel doubling when the dot clock
  4735. * is > 90% of the (display) core speed.
  4736. *
  4737. * GDG double wide on either pipe,
  4738. * otherwise pipe A only.
  4739. */
  4740. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  4741. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  4742. clock_limit *= 2;
  4743. pipe_config->double_wide = true;
  4744. }
  4745. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  4746. return -EINVAL;
  4747. }
  4748. /*
  4749. * Pipe horizontal size must be even in:
  4750. * - DVO ganged mode
  4751. * - LVDS dual channel mode
  4752. * - Double wide pipe
  4753. */
  4754. if ((intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  4755. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  4756. pipe_config->pipe_src_w &= ~1;
  4757. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  4758. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  4759. */
  4760. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  4761. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  4762. return -EINVAL;
  4763. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
  4764. pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
  4765. } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
  4766. /* only a 8bpc pipe, with 6bpc dither through the panel fitter
  4767. * for lvds. */
  4768. pipe_config->pipe_bpp = 8*3;
  4769. }
  4770. if (HAS_IPS(dev))
  4771. hsw_compute_ips_config(crtc, pipe_config);
  4772. if (pipe_config->has_pch_encoder)
  4773. return ironlake_fdi_compute_config(crtc, pipe_config);
  4774. return 0;
  4775. }
  4776. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  4777. {
  4778. struct drm_i915_private *dev_priv = dev->dev_private;
  4779. u32 val;
  4780. int divider;
  4781. /* FIXME: Punit isn't quite ready yet */
  4782. if (IS_CHERRYVIEW(dev))
  4783. return 400000;
  4784. if (dev_priv->hpll_freq == 0)
  4785. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  4786. mutex_lock(&dev_priv->dpio_lock);
  4787. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4788. mutex_unlock(&dev_priv->dpio_lock);
  4789. divider = val & DISPLAY_FREQUENCY_VALUES;
  4790. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  4791. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4792. "cdclk change in progress\n");
  4793. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  4794. }
  4795. static int i945_get_display_clock_speed(struct drm_device *dev)
  4796. {
  4797. return 400000;
  4798. }
  4799. static int i915_get_display_clock_speed(struct drm_device *dev)
  4800. {
  4801. return 333000;
  4802. }
  4803. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  4804. {
  4805. return 200000;
  4806. }
  4807. static int pnv_get_display_clock_speed(struct drm_device *dev)
  4808. {
  4809. u16 gcfgc = 0;
  4810. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4811. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4812. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  4813. return 267000;
  4814. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  4815. return 333000;
  4816. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  4817. return 444000;
  4818. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  4819. return 200000;
  4820. default:
  4821. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  4822. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  4823. return 133000;
  4824. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  4825. return 167000;
  4826. }
  4827. }
  4828. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  4829. {
  4830. u16 gcfgc = 0;
  4831. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4832. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  4833. return 133000;
  4834. else {
  4835. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4836. case GC_DISPLAY_CLOCK_333_MHZ:
  4837. return 333000;
  4838. default:
  4839. case GC_DISPLAY_CLOCK_190_200_MHZ:
  4840. return 190000;
  4841. }
  4842. }
  4843. }
  4844. static int i865_get_display_clock_speed(struct drm_device *dev)
  4845. {
  4846. return 266000;
  4847. }
  4848. static int i855_get_display_clock_speed(struct drm_device *dev)
  4849. {
  4850. u16 hpllcc = 0;
  4851. /* Assume that the hardware is in the high speed state. This
  4852. * should be the default.
  4853. */
  4854. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  4855. case GC_CLOCK_133_200:
  4856. case GC_CLOCK_100_200:
  4857. return 200000;
  4858. case GC_CLOCK_166_250:
  4859. return 250000;
  4860. case GC_CLOCK_100_133:
  4861. return 133000;
  4862. }
  4863. /* Shouldn't happen */
  4864. return 0;
  4865. }
  4866. static int i830_get_display_clock_speed(struct drm_device *dev)
  4867. {
  4868. return 133000;
  4869. }
  4870. static void
  4871. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  4872. {
  4873. while (*num > DATA_LINK_M_N_MASK ||
  4874. *den > DATA_LINK_M_N_MASK) {
  4875. *num >>= 1;
  4876. *den >>= 1;
  4877. }
  4878. }
  4879. static void compute_m_n(unsigned int m, unsigned int n,
  4880. uint32_t *ret_m, uint32_t *ret_n)
  4881. {
  4882. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  4883. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  4884. intel_reduce_m_n_ratio(ret_m, ret_n);
  4885. }
  4886. void
  4887. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  4888. int pixel_clock, int link_clock,
  4889. struct intel_link_m_n *m_n)
  4890. {
  4891. m_n->tu = 64;
  4892. compute_m_n(bits_per_pixel * pixel_clock,
  4893. link_clock * nlanes * 8,
  4894. &m_n->gmch_m, &m_n->gmch_n);
  4895. compute_m_n(pixel_clock, link_clock,
  4896. &m_n->link_m, &m_n->link_n);
  4897. }
  4898. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  4899. {
  4900. if (i915.panel_use_ssc >= 0)
  4901. return i915.panel_use_ssc != 0;
  4902. return dev_priv->vbt.lvds_use_ssc
  4903. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  4904. }
  4905. static int i9xx_get_refclk(struct intel_crtc *crtc, int num_connectors)
  4906. {
  4907. struct drm_device *dev = crtc->base.dev;
  4908. struct drm_i915_private *dev_priv = dev->dev_private;
  4909. int refclk;
  4910. if (IS_VALLEYVIEW(dev)) {
  4911. refclk = 100000;
  4912. } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  4913. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4914. refclk = dev_priv->vbt.lvds_ssc_freq;
  4915. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  4916. } else if (!IS_GEN2(dev)) {
  4917. refclk = 96000;
  4918. } else {
  4919. refclk = 48000;
  4920. }
  4921. return refclk;
  4922. }
  4923. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  4924. {
  4925. return (1 << dpll->n) << 16 | dpll->m2;
  4926. }
  4927. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  4928. {
  4929. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  4930. }
  4931. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  4932. struct intel_crtc_state *crtc_state,
  4933. intel_clock_t *reduced_clock)
  4934. {
  4935. struct drm_device *dev = crtc->base.dev;
  4936. u32 fp, fp2 = 0;
  4937. if (IS_PINEVIEW(dev)) {
  4938. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  4939. if (reduced_clock)
  4940. fp2 = pnv_dpll_compute_fp(reduced_clock);
  4941. } else {
  4942. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  4943. if (reduced_clock)
  4944. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  4945. }
  4946. crtc_state->dpll_hw_state.fp0 = fp;
  4947. crtc->lowfreq_avail = false;
  4948. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  4949. reduced_clock && i915.powersave) {
  4950. crtc_state->dpll_hw_state.fp1 = fp2;
  4951. crtc->lowfreq_avail = true;
  4952. } else {
  4953. crtc_state->dpll_hw_state.fp1 = fp;
  4954. }
  4955. }
  4956. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  4957. pipe)
  4958. {
  4959. u32 reg_val;
  4960. /*
  4961. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  4962. * and set it to a reasonable value instead.
  4963. */
  4964. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4965. reg_val &= 0xffffff00;
  4966. reg_val |= 0x00000030;
  4967. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4968. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4969. reg_val &= 0x8cffffff;
  4970. reg_val = 0x8c000000;
  4971. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4972. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4973. reg_val &= 0xffffff00;
  4974. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4975. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4976. reg_val &= 0x00ffffff;
  4977. reg_val |= 0xb0000000;
  4978. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4979. }
  4980. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  4981. struct intel_link_m_n *m_n)
  4982. {
  4983. struct drm_device *dev = crtc->base.dev;
  4984. struct drm_i915_private *dev_priv = dev->dev_private;
  4985. int pipe = crtc->pipe;
  4986. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4987. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  4988. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  4989. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  4990. }
  4991. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  4992. struct intel_link_m_n *m_n,
  4993. struct intel_link_m_n *m2_n2)
  4994. {
  4995. struct drm_device *dev = crtc->base.dev;
  4996. struct drm_i915_private *dev_priv = dev->dev_private;
  4997. int pipe = crtc->pipe;
  4998. enum transcoder transcoder = crtc->config->cpu_transcoder;
  4999. if (INTEL_INFO(dev)->gen >= 5) {
  5000. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5001. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5002. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5003. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5004. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5005. * for gen < 8) and if DRRS is supported (to make sure the
  5006. * registers are not unnecessarily accessed).
  5007. */
  5008. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  5009. crtc->config->has_drrs) {
  5010. I915_WRITE(PIPE_DATA_M2(transcoder),
  5011. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  5012. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  5013. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  5014. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  5015. }
  5016. } else {
  5017. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5018. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  5019. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  5020. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  5021. }
  5022. }
  5023. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  5024. {
  5025. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  5026. if (m_n == M1_N1) {
  5027. dp_m_n = &crtc->config->dp_m_n;
  5028. dp_m2_n2 = &crtc->config->dp_m2_n2;
  5029. } else if (m_n == M2_N2) {
  5030. /*
  5031. * M2_N2 registers are not supported. Hence m2_n2 divider value
  5032. * needs to be programmed into M1_N1.
  5033. */
  5034. dp_m_n = &crtc->config->dp_m2_n2;
  5035. } else {
  5036. DRM_ERROR("Unsupported divider value\n");
  5037. return;
  5038. }
  5039. if (crtc->config->has_pch_encoder)
  5040. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  5041. else
  5042. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  5043. }
  5044. static void vlv_update_pll(struct intel_crtc *crtc,
  5045. struct intel_crtc_state *pipe_config)
  5046. {
  5047. u32 dpll, dpll_md;
  5048. /*
  5049. * Enable DPIO clock input. We should never disable the reference
  5050. * clock for pipe B, since VGA hotplug / manual detection depends
  5051. * on it.
  5052. */
  5053. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  5054. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  5055. /* We should never disable this, set it here for state tracking */
  5056. if (crtc->pipe == PIPE_B)
  5057. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5058. dpll |= DPLL_VCO_ENABLE;
  5059. pipe_config->dpll_hw_state.dpll = dpll;
  5060. dpll_md = (pipe_config->pixel_multiplier - 1)
  5061. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5062. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  5063. }
  5064. static void vlv_prepare_pll(struct intel_crtc *crtc,
  5065. const struct intel_crtc_state *pipe_config)
  5066. {
  5067. struct drm_device *dev = crtc->base.dev;
  5068. struct drm_i915_private *dev_priv = dev->dev_private;
  5069. int pipe = crtc->pipe;
  5070. u32 mdiv;
  5071. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  5072. u32 coreclk, reg_val;
  5073. mutex_lock(&dev_priv->dpio_lock);
  5074. bestn = pipe_config->dpll.n;
  5075. bestm1 = pipe_config->dpll.m1;
  5076. bestm2 = pipe_config->dpll.m2;
  5077. bestp1 = pipe_config->dpll.p1;
  5078. bestp2 = pipe_config->dpll.p2;
  5079. /* See eDP HDMI DPIO driver vbios notes doc */
  5080. /* PLL B needs special handling */
  5081. if (pipe == PIPE_B)
  5082. vlv_pllb_recal_opamp(dev_priv, pipe);
  5083. /* Set up Tx target for periodic Rcomp update */
  5084. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  5085. /* Disable target IRef on PLL */
  5086. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  5087. reg_val &= 0x00ffffff;
  5088. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  5089. /* Disable fast lock */
  5090. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  5091. /* Set idtafcrecal before PLL is enabled */
  5092. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  5093. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  5094. mdiv |= ((bestn << DPIO_N_SHIFT));
  5095. mdiv |= (1 << DPIO_K_SHIFT);
  5096. /*
  5097. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  5098. * but we don't support that).
  5099. * Note: don't use the DAC post divider as it seems unstable.
  5100. */
  5101. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5102. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5103. mdiv |= DPIO_ENABLE_CALIBRATION;
  5104. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5105. /* Set HBR and RBR LPF coefficients */
  5106. if (pipe_config->port_clock == 162000 ||
  5107. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  5108. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  5109. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5110. 0x009f0003);
  5111. else
  5112. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5113. 0x00d0000f);
  5114. if (pipe_config->has_dp_encoder) {
  5115. /* Use SSC source */
  5116. if (pipe == PIPE_A)
  5117. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5118. 0x0df40000);
  5119. else
  5120. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5121. 0x0df70000);
  5122. } else { /* HDMI or VGA */
  5123. /* Use bend source */
  5124. if (pipe == PIPE_A)
  5125. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5126. 0x0df70000);
  5127. else
  5128. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5129. 0x0df40000);
  5130. }
  5131. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5132. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5133. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  5134. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  5135. coreclk |= 0x01000000;
  5136. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5137. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5138. mutex_unlock(&dev_priv->dpio_lock);
  5139. }
  5140. static void chv_update_pll(struct intel_crtc *crtc,
  5141. struct intel_crtc_state *pipe_config)
  5142. {
  5143. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  5144. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  5145. DPLL_VCO_ENABLE;
  5146. if (crtc->pipe != PIPE_A)
  5147. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5148. pipe_config->dpll_hw_state.dpll_md =
  5149. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5150. }
  5151. static void chv_prepare_pll(struct intel_crtc *crtc,
  5152. const struct intel_crtc_state *pipe_config)
  5153. {
  5154. struct drm_device *dev = crtc->base.dev;
  5155. struct drm_i915_private *dev_priv = dev->dev_private;
  5156. int pipe = crtc->pipe;
  5157. int dpll_reg = DPLL(crtc->pipe);
  5158. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5159. u32 loopfilter, intcoeff;
  5160. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5161. int refclk;
  5162. bestn = pipe_config->dpll.n;
  5163. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5164. bestm1 = pipe_config->dpll.m1;
  5165. bestm2 = pipe_config->dpll.m2 >> 22;
  5166. bestp1 = pipe_config->dpll.p1;
  5167. bestp2 = pipe_config->dpll.p2;
  5168. /*
  5169. * Enable Refclk and SSC
  5170. */
  5171. I915_WRITE(dpll_reg,
  5172. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5173. mutex_lock(&dev_priv->dpio_lock);
  5174. /* p1 and p2 divider */
  5175. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5176. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5177. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5178. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5179. 1 << DPIO_CHV_K_DIV_SHIFT);
  5180. /* Feedback post-divider - m2 */
  5181. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5182. /* Feedback refclk divider - n and m1 */
  5183. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5184. DPIO_CHV_M1_DIV_BY_2 |
  5185. 1 << DPIO_CHV_N_DIV_SHIFT);
  5186. /* M2 fraction division */
  5187. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5188. /* M2 fraction division enable */
  5189. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
  5190. DPIO_CHV_FRAC_DIV_EN |
  5191. (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
  5192. /* Loop filter */
  5193. refclk = i9xx_get_refclk(crtc, 0);
  5194. loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
  5195. 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
  5196. if (refclk == 100000)
  5197. intcoeff = 11;
  5198. else if (refclk == 38400)
  5199. intcoeff = 10;
  5200. else
  5201. intcoeff = 9;
  5202. loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
  5203. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5204. /* AFC Recal */
  5205. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5206. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5207. DPIO_AFC_RECAL);
  5208. mutex_unlock(&dev_priv->dpio_lock);
  5209. }
  5210. /**
  5211. * vlv_force_pll_on - forcibly enable just the PLL
  5212. * @dev_priv: i915 private structure
  5213. * @pipe: pipe PLL to enable
  5214. * @dpll: PLL configuration
  5215. *
  5216. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5217. * in cases where we need the PLL enabled even when @pipe is not going to
  5218. * be enabled.
  5219. */
  5220. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  5221. const struct dpll *dpll)
  5222. {
  5223. struct intel_crtc *crtc =
  5224. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  5225. struct intel_crtc_state pipe_config = {
  5226. .pixel_multiplier = 1,
  5227. .dpll = *dpll,
  5228. };
  5229. if (IS_CHERRYVIEW(dev)) {
  5230. chv_update_pll(crtc, &pipe_config);
  5231. chv_prepare_pll(crtc, &pipe_config);
  5232. chv_enable_pll(crtc, &pipe_config);
  5233. } else {
  5234. vlv_update_pll(crtc, &pipe_config);
  5235. vlv_prepare_pll(crtc, &pipe_config);
  5236. vlv_enable_pll(crtc, &pipe_config);
  5237. }
  5238. }
  5239. /**
  5240. * vlv_force_pll_off - forcibly disable just the PLL
  5241. * @dev_priv: i915 private structure
  5242. * @pipe: pipe PLL to disable
  5243. *
  5244. * Disable the PLL for @pipe. To be used in cases where we need
  5245. * the PLL enabled even when @pipe is not going to be enabled.
  5246. */
  5247. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  5248. {
  5249. if (IS_CHERRYVIEW(dev))
  5250. chv_disable_pll(to_i915(dev), pipe);
  5251. else
  5252. vlv_disable_pll(to_i915(dev), pipe);
  5253. }
  5254. static void i9xx_update_pll(struct intel_crtc *crtc,
  5255. struct intel_crtc_state *crtc_state,
  5256. intel_clock_t *reduced_clock,
  5257. int num_connectors)
  5258. {
  5259. struct drm_device *dev = crtc->base.dev;
  5260. struct drm_i915_private *dev_priv = dev->dev_private;
  5261. u32 dpll;
  5262. bool is_sdvo;
  5263. struct dpll *clock = &crtc_state->dpll;
  5264. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5265. is_sdvo = intel_pipe_will_have_type(crtc, INTEL_OUTPUT_SDVO) ||
  5266. intel_pipe_will_have_type(crtc, INTEL_OUTPUT_HDMI);
  5267. dpll = DPLL_VGA_MODE_DIS;
  5268. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  5269. dpll |= DPLLB_MODE_LVDS;
  5270. else
  5271. dpll |= DPLLB_MODE_DAC_SERIAL;
  5272. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5273. dpll |= (crtc_state->pixel_multiplier - 1)
  5274. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5275. }
  5276. if (is_sdvo)
  5277. dpll |= DPLL_SDVO_HIGH_SPEED;
  5278. if (crtc_state->has_dp_encoder)
  5279. dpll |= DPLL_SDVO_HIGH_SPEED;
  5280. /* compute bitmask from p1 value */
  5281. if (IS_PINEVIEW(dev))
  5282. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5283. else {
  5284. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5285. if (IS_G4X(dev) && reduced_clock)
  5286. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5287. }
  5288. switch (clock->p2) {
  5289. case 5:
  5290. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5291. break;
  5292. case 7:
  5293. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5294. break;
  5295. case 10:
  5296. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5297. break;
  5298. case 14:
  5299. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5300. break;
  5301. }
  5302. if (INTEL_INFO(dev)->gen >= 4)
  5303. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5304. if (crtc_state->sdvo_tv_clock)
  5305. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5306. else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  5307. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5308. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5309. else
  5310. dpll |= PLL_REF_INPUT_DREFCLK;
  5311. dpll |= DPLL_VCO_ENABLE;
  5312. crtc_state->dpll_hw_state.dpll = dpll;
  5313. if (INTEL_INFO(dev)->gen >= 4) {
  5314. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  5315. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5316. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  5317. }
  5318. }
  5319. static void i8xx_update_pll(struct intel_crtc *crtc,
  5320. struct intel_crtc_state *crtc_state,
  5321. intel_clock_t *reduced_clock,
  5322. int num_connectors)
  5323. {
  5324. struct drm_device *dev = crtc->base.dev;
  5325. struct drm_i915_private *dev_priv = dev->dev_private;
  5326. u32 dpll;
  5327. struct dpll *clock = &crtc_state->dpll;
  5328. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5329. dpll = DPLL_VGA_MODE_DIS;
  5330. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  5331. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5332. } else {
  5333. if (clock->p1 == 2)
  5334. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5335. else
  5336. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5337. if (clock->p2 == 4)
  5338. dpll |= PLL_P2_DIVIDE_BY_4;
  5339. }
  5340. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc, INTEL_OUTPUT_DVO))
  5341. dpll |= DPLL_DVO_2X_MODE;
  5342. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  5343. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5344. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5345. else
  5346. dpll |= PLL_REF_INPUT_DREFCLK;
  5347. dpll |= DPLL_VCO_ENABLE;
  5348. crtc_state->dpll_hw_state.dpll = dpll;
  5349. }
  5350. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5351. {
  5352. struct drm_device *dev = intel_crtc->base.dev;
  5353. struct drm_i915_private *dev_priv = dev->dev_private;
  5354. enum pipe pipe = intel_crtc->pipe;
  5355. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  5356. struct drm_display_mode *adjusted_mode =
  5357. &intel_crtc->config->base.adjusted_mode;
  5358. uint32_t crtc_vtotal, crtc_vblank_end;
  5359. int vsyncshift = 0;
  5360. /* We need to be careful not to changed the adjusted mode, for otherwise
  5361. * the hw state checker will get angry at the mismatch. */
  5362. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5363. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5364. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5365. /* the chip adds 2 halflines automatically */
  5366. crtc_vtotal -= 1;
  5367. crtc_vblank_end -= 1;
  5368. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  5369. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5370. else
  5371. vsyncshift = adjusted_mode->crtc_hsync_start -
  5372. adjusted_mode->crtc_htotal / 2;
  5373. if (vsyncshift < 0)
  5374. vsyncshift += adjusted_mode->crtc_htotal;
  5375. }
  5376. if (INTEL_INFO(dev)->gen > 3)
  5377. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5378. I915_WRITE(HTOTAL(cpu_transcoder),
  5379. (adjusted_mode->crtc_hdisplay - 1) |
  5380. ((adjusted_mode->crtc_htotal - 1) << 16));
  5381. I915_WRITE(HBLANK(cpu_transcoder),
  5382. (adjusted_mode->crtc_hblank_start - 1) |
  5383. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5384. I915_WRITE(HSYNC(cpu_transcoder),
  5385. (adjusted_mode->crtc_hsync_start - 1) |
  5386. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5387. I915_WRITE(VTOTAL(cpu_transcoder),
  5388. (adjusted_mode->crtc_vdisplay - 1) |
  5389. ((crtc_vtotal - 1) << 16));
  5390. I915_WRITE(VBLANK(cpu_transcoder),
  5391. (adjusted_mode->crtc_vblank_start - 1) |
  5392. ((crtc_vblank_end - 1) << 16));
  5393. I915_WRITE(VSYNC(cpu_transcoder),
  5394. (adjusted_mode->crtc_vsync_start - 1) |
  5395. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5396. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5397. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5398. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5399. * bits. */
  5400. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  5401. (pipe == PIPE_B || pipe == PIPE_C))
  5402. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5403. /* pipesrc controls the size that is scaled from, which should
  5404. * always be the user's requested size.
  5405. */
  5406. I915_WRITE(PIPESRC(pipe),
  5407. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  5408. (intel_crtc->config->pipe_src_h - 1));
  5409. }
  5410. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5411. struct intel_crtc_state *pipe_config)
  5412. {
  5413. struct drm_device *dev = crtc->base.dev;
  5414. struct drm_i915_private *dev_priv = dev->dev_private;
  5415. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5416. uint32_t tmp;
  5417. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5418. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5419. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5420. tmp = I915_READ(HBLANK(cpu_transcoder));
  5421. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5422. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5423. tmp = I915_READ(HSYNC(cpu_transcoder));
  5424. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5425. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5426. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5427. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5428. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  5429. tmp = I915_READ(VBLANK(cpu_transcoder));
  5430. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  5431. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  5432. tmp = I915_READ(VSYNC(cpu_transcoder));
  5433. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  5434. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  5435. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  5436. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  5437. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  5438. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  5439. }
  5440. tmp = I915_READ(PIPESRC(crtc->pipe));
  5441. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  5442. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  5443. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  5444. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  5445. }
  5446. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  5447. struct intel_crtc_state *pipe_config)
  5448. {
  5449. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  5450. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  5451. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  5452. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  5453. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  5454. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  5455. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  5456. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  5457. mode->flags = pipe_config->base.adjusted_mode.flags;
  5458. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  5459. mode->flags |= pipe_config->base.adjusted_mode.flags;
  5460. }
  5461. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  5462. {
  5463. struct drm_device *dev = intel_crtc->base.dev;
  5464. struct drm_i915_private *dev_priv = dev->dev_private;
  5465. uint32_t pipeconf;
  5466. pipeconf = 0;
  5467. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  5468. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  5469. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  5470. if (intel_crtc->config->double_wide)
  5471. pipeconf |= PIPECONF_DOUBLE_WIDE;
  5472. /* only g4x and later have fancy bpc/dither controls */
  5473. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5474. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  5475. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  5476. pipeconf |= PIPECONF_DITHER_EN |
  5477. PIPECONF_DITHER_TYPE_SP;
  5478. switch (intel_crtc->config->pipe_bpp) {
  5479. case 18:
  5480. pipeconf |= PIPECONF_6BPC;
  5481. break;
  5482. case 24:
  5483. pipeconf |= PIPECONF_8BPC;
  5484. break;
  5485. case 30:
  5486. pipeconf |= PIPECONF_10BPC;
  5487. break;
  5488. default:
  5489. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5490. BUG();
  5491. }
  5492. }
  5493. if (HAS_PIPE_CXSR(dev)) {
  5494. if (intel_crtc->lowfreq_avail) {
  5495. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  5496. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  5497. } else {
  5498. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  5499. }
  5500. }
  5501. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  5502. if (INTEL_INFO(dev)->gen < 4 ||
  5503. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  5504. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  5505. else
  5506. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  5507. } else
  5508. pipeconf |= PIPECONF_PROGRESSIVE;
  5509. if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
  5510. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  5511. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  5512. POSTING_READ(PIPECONF(intel_crtc->pipe));
  5513. }
  5514. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  5515. struct intel_crtc_state *crtc_state)
  5516. {
  5517. struct drm_device *dev = crtc->base.dev;
  5518. struct drm_i915_private *dev_priv = dev->dev_private;
  5519. int refclk, num_connectors = 0;
  5520. intel_clock_t clock, reduced_clock;
  5521. bool ok, has_reduced_clock = false;
  5522. bool is_lvds = false, is_dsi = false;
  5523. struct intel_encoder *encoder;
  5524. const intel_limit_t *limit;
  5525. for_each_intel_encoder(dev, encoder) {
  5526. if (encoder->new_crtc != crtc)
  5527. continue;
  5528. switch (encoder->type) {
  5529. case INTEL_OUTPUT_LVDS:
  5530. is_lvds = true;
  5531. break;
  5532. case INTEL_OUTPUT_DSI:
  5533. is_dsi = true;
  5534. break;
  5535. default:
  5536. break;
  5537. }
  5538. num_connectors++;
  5539. }
  5540. if (is_dsi)
  5541. return 0;
  5542. if (!crtc_state->clock_set) {
  5543. refclk = i9xx_get_refclk(crtc, num_connectors);
  5544. /*
  5545. * Returns a set of divisors for the desired target clock with
  5546. * the given refclk, or FALSE. The returned values represent
  5547. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  5548. * 2) / p1 / p2.
  5549. */
  5550. limit = intel_limit(crtc, refclk);
  5551. ok = dev_priv->display.find_dpll(limit, crtc,
  5552. crtc_state->port_clock,
  5553. refclk, NULL, &clock);
  5554. if (!ok) {
  5555. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5556. return -EINVAL;
  5557. }
  5558. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5559. /*
  5560. * Ensure we match the reduced clock's P to the target
  5561. * clock. If the clocks don't match, we can't switch
  5562. * the display clock by using the FP0/FP1. In such case
  5563. * we will disable the LVDS downclock feature.
  5564. */
  5565. has_reduced_clock =
  5566. dev_priv->display.find_dpll(limit, crtc,
  5567. dev_priv->lvds_downclock,
  5568. refclk, &clock,
  5569. &reduced_clock);
  5570. }
  5571. /* Compat-code for transition, will disappear. */
  5572. crtc_state->dpll.n = clock.n;
  5573. crtc_state->dpll.m1 = clock.m1;
  5574. crtc_state->dpll.m2 = clock.m2;
  5575. crtc_state->dpll.p1 = clock.p1;
  5576. crtc_state->dpll.p2 = clock.p2;
  5577. }
  5578. if (IS_GEN2(dev)) {
  5579. i8xx_update_pll(crtc, crtc_state,
  5580. has_reduced_clock ? &reduced_clock : NULL,
  5581. num_connectors);
  5582. } else if (IS_CHERRYVIEW(dev)) {
  5583. chv_update_pll(crtc, crtc_state);
  5584. } else if (IS_VALLEYVIEW(dev)) {
  5585. vlv_update_pll(crtc, crtc_state);
  5586. } else {
  5587. i9xx_update_pll(crtc, crtc_state,
  5588. has_reduced_clock ? &reduced_clock : NULL,
  5589. num_connectors);
  5590. }
  5591. return 0;
  5592. }
  5593. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  5594. struct intel_crtc_state *pipe_config)
  5595. {
  5596. struct drm_device *dev = crtc->base.dev;
  5597. struct drm_i915_private *dev_priv = dev->dev_private;
  5598. uint32_t tmp;
  5599. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  5600. return;
  5601. tmp = I915_READ(PFIT_CONTROL);
  5602. if (!(tmp & PFIT_ENABLE))
  5603. return;
  5604. /* Check whether the pfit is attached to our pipe. */
  5605. if (INTEL_INFO(dev)->gen < 4) {
  5606. if (crtc->pipe != PIPE_B)
  5607. return;
  5608. } else {
  5609. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  5610. return;
  5611. }
  5612. pipe_config->gmch_pfit.control = tmp;
  5613. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  5614. if (INTEL_INFO(dev)->gen < 5)
  5615. pipe_config->gmch_pfit.lvds_border_bits =
  5616. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  5617. }
  5618. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  5619. struct intel_crtc_state *pipe_config)
  5620. {
  5621. struct drm_device *dev = crtc->base.dev;
  5622. struct drm_i915_private *dev_priv = dev->dev_private;
  5623. int pipe = pipe_config->cpu_transcoder;
  5624. intel_clock_t clock;
  5625. u32 mdiv;
  5626. int refclk = 100000;
  5627. /* In case of MIPI DPLL will not even be used */
  5628. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  5629. return;
  5630. mutex_lock(&dev_priv->dpio_lock);
  5631. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  5632. mutex_unlock(&dev_priv->dpio_lock);
  5633. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  5634. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  5635. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  5636. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  5637. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  5638. vlv_clock(refclk, &clock);
  5639. /* clock.dot is the fast clock */
  5640. pipe_config->port_clock = clock.dot / 5;
  5641. }
  5642. static void
  5643. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  5644. struct intel_initial_plane_config *plane_config)
  5645. {
  5646. struct drm_device *dev = crtc->base.dev;
  5647. struct drm_i915_private *dev_priv = dev->dev_private;
  5648. u32 val, base, offset;
  5649. int pipe = crtc->pipe, plane = crtc->plane;
  5650. int fourcc, pixel_format;
  5651. int aligned_height;
  5652. struct drm_framebuffer *fb;
  5653. struct intel_framebuffer *intel_fb;
  5654. val = I915_READ(DSPCNTR(plane));
  5655. if (!(val & DISPLAY_PLANE_ENABLE))
  5656. return;
  5657. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  5658. if (!intel_fb) {
  5659. DRM_DEBUG_KMS("failed to alloc fb\n");
  5660. return;
  5661. }
  5662. fb = &intel_fb->base;
  5663. if (INTEL_INFO(dev)->gen >= 4) {
  5664. if (val & DISPPLANE_TILED) {
  5665. plane_config->tiling = I915_TILING_X;
  5666. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  5667. }
  5668. }
  5669. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  5670. fourcc = i9xx_format_to_fourcc(pixel_format);
  5671. fb->pixel_format = fourcc;
  5672. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  5673. if (INTEL_INFO(dev)->gen >= 4) {
  5674. if (plane_config->tiling)
  5675. offset = I915_READ(DSPTILEOFF(plane));
  5676. else
  5677. offset = I915_READ(DSPLINOFF(plane));
  5678. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  5679. } else {
  5680. base = I915_READ(DSPADDR(plane));
  5681. }
  5682. plane_config->base = base;
  5683. val = I915_READ(PIPESRC(pipe));
  5684. fb->width = ((val >> 16) & 0xfff) + 1;
  5685. fb->height = ((val >> 0) & 0xfff) + 1;
  5686. val = I915_READ(DSPSTRIDE(pipe));
  5687. fb->pitches[0] = val & 0xffffffc0;
  5688. aligned_height = intel_fb_align_height(dev, fb->height,
  5689. fb->pixel_format,
  5690. fb->modifier[0]);
  5691. plane_config->size = fb->pitches[0] * aligned_height;
  5692. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  5693. pipe_name(pipe), plane, fb->width, fb->height,
  5694. fb->bits_per_pixel, base, fb->pitches[0],
  5695. plane_config->size);
  5696. plane_config->fb = intel_fb;
  5697. }
  5698. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  5699. struct intel_crtc_state *pipe_config)
  5700. {
  5701. struct drm_device *dev = crtc->base.dev;
  5702. struct drm_i915_private *dev_priv = dev->dev_private;
  5703. int pipe = pipe_config->cpu_transcoder;
  5704. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5705. intel_clock_t clock;
  5706. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  5707. int refclk = 100000;
  5708. mutex_lock(&dev_priv->dpio_lock);
  5709. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  5710. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  5711. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  5712. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  5713. mutex_unlock(&dev_priv->dpio_lock);
  5714. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  5715. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  5716. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  5717. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  5718. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  5719. chv_clock(refclk, &clock);
  5720. /* clock.dot is the fast clock */
  5721. pipe_config->port_clock = clock.dot / 5;
  5722. }
  5723. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  5724. struct intel_crtc_state *pipe_config)
  5725. {
  5726. struct drm_device *dev = crtc->base.dev;
  5727. struct drm_i915_private *dev_priv = dev->dev_private;
  5728. uint32_t tmp;
  5729. if (!intel_display_power_is_enabled(dev_priv,
  5730. POWER_DOMAIN_PIPE(crtc->pipe)))
  5731. return false;
  5732. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5733. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5734. tmp = I915_READ(PIPECONF(crtc->pipe));
  5735. if (!(tmp & PIPECONF_ENABLE))
  5736. return false;
  5737. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5738. switch (tmp & PIPECONF_BPC_MASK) {
  5739. case PIPECONF_6BPC:
  5740. pipe_config->pipe_bpp = 18;
  5741. break;
  5742. case PIPECONF_8BPC:
  5743. pipe_config->pipe_bpp = 24;
  5744. break;
  5745. case PIPECONF_10BPC:
  5746. pipe_config->pipe_bpp = 30;
  5747. break;
  5748. default:
  5749. break;
  5750. }
  5751. }
  5752. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  5753. pipe_config->limited_color_range = true;
  5754. if (INTEL_INFO(dev)->gen < 4)
  5755. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  5756. intel_get_pipe_timings(crtc, pipe_config);
  5757. i9xx_get_pfit_config(crtc, pipe_config);
  5758. if (INTEL_INFO(dev)->gen >= 4) {
  5759. tmp = I915_READ(DPLL_MD(crtc->pipe));
  5760. pipe_config->pixel_multiplier =
  5761. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  5762. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  5763. pipe_config->dpll_hw_state.dpll_md = tmp;
  5764. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5765. tmp = I915_READ(DPLL(crtc->pipe));
  5766. pipe_config->pixel_multiplier =
  5767. ((tmp & SDVO_MULTIPLIER_MASK)
  5768. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  5769. } else {
  5770. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  5771. * port and will be fixed up in the encoder->get_config
  5772. * function. */
  5773. pipe_config->pixel_multiplier = 1;
  5774. }
  5775. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  5776. if (!IS_VALLEYVIEW(dev)) {
  5777. /*
  5778. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  5779. * on 830. Filter it out here so that we don't
  5780. * report errors due to that.
  5781. */
  5782. if (IS_I830(dev))
  5783. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  5784. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  5785. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  5786. } else {
  5787. /* Mask out read-only status bits. */
  5788. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  5789. DPLL_PORTC_READY_MASK |
  5790. DPLL_PORTB_READY_MASK);
  5791. }
  5792. if (IS_CHERRYVIEW(dev))
  5793. chv_crtc_clock_get(crtc, pipe_config);
  5794. else if (IS_VALLEYVIEW(dev))
  5795. vlv_crtc_clock_get(crtc, pipe_config);
  5796. else
  5797. i9xx_crtc_clock_get(crtc, pipe_config);
  5798. return true;
  5799. }
  5800. static void ironlake_init_pch_refclk(struct drm_device *dev)
  5801. {
  5802. struct drm_i915_private *dev_priv = dev->dev_private;
  5803. struct intel_encoder *encoder;
  5804. u32 val, final;
  5805. bool has_lvds = false;
  5806. bool has_cpu_edp = false;
  5807. bool has_panel = false;
  5808. bool has_ck505 = false;
  5809. bool can_ssc = false;
  5810. /* We need to take the global config into account */
  5811. for_each_intel_encoder(dev, encoder) {
  5812. switch (encoder->type) {
  5813. case INTEL_OUTPUT_LVDS:
  5814. has_panel = true;
  5815. has_lvds = true;
  5816. break;
  5817. case INTEL_OUTPUT_EDP:
  5818. has_panel = true;
  5819. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  5820. has_cpu_edp = true;
  5821. break;
  5822. default:
  5823. break;
  5824. }
  5825. }
  5826. if (HAS_PCH_IBX(dev)) {
  5827. has_ck505 = dev_priv->vbt.display_clock_mode;
  5828. can_ssc = has_ck505;
  5829. } else {
  5830. has_ck505 = false;
  5831. can_ssc = true;
  5832. }
  5833. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  5834. has_panel, has_lvds, has_ck505);
  5835. /* Ironlake: try to setup display ref clock before DPLL
  5836. * enabling. This is only under driver's control after
  5837. * PCH B stepping, previous chipset stepping should be
  5838. * ignoring this setting.
  5839. */
  5840. val = I915_READ(PCH_DREF_CONTROL);
  5841. /* As we must carefully and slowly disable/enable each source in turn,
  5842. * compute the final state we want first and check if we need to
  5843. * make any changes at all.
  5844. */
  5845. final = val;
  5846. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  5847. if (has_ck505)
  5848. final |= DREF_NONSPREAD_CK505_ENABLE;
  5849. else
  5850. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  5851. final &= ~DREF_SSC_SOURCE_MASK;
  5852. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5853. final &= ~DREF_SSC1_ENABLE;
  5854. if (has_panel) {
  5855. final |= DREF_SSC_SOURCE_ENABLE;
  5856. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5857. final |= DREF_SSC1_ENABLE;
  5858. if (has_cpu_edp) {
  5859. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5860. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5861. else
  5862. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5863. } else
  5864. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5865. } else {
  5866. final |= DREF_SSC_SOURCE_DISABLE;
  5867. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5868. }
  5869. if (final == val)
  5870. return;
  5871. /* Always enable nonspread source */
  5872. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  5873. if (has_ck505)
  5874. val |= DREF_NONSPREAD_CK505_ENABLE;
  5875. else
  5876. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  5877. if (has_panel) {
  5878. val &= ~DREF_SSC_SOURCE_MASK;
  5879. val |= DREF_SSC_SOURCE_ENABLE;
  5880. /* SSC must be turned on before enabling the CPU output */
  5881. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5882. DRM_DEBUG_KMS("Using SSC on panel\n");
  5883. val |= DREF_SSC1_ENABLE;
  5884. } else
  5885. val &= ~DREF_SSC1_ENABLE;
  5886. /* Get SSC going before enabling the outputs */
  5887. I915_WRITE(PCH_DREF_CONTROL, val);
  5888. POSTING_READ(PCH_DREF_CONTROL);
  5889. udelay(200);
  5890. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5891. /* Enable CPU source on CPU attached eDP */
  5892. if (has_cpu_edp) {
  5893. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5894. DRM_DEBUG_KMS("Using SSC on eDP\n");
  5895. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5896. } else
  5897. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5898. } else
  5899. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5900. I915_WRITE(PCH_DREF_CONTROL, val);
  5901. POSTING_READ(PCH_DREF_CONTROL);
  5902. udelay(200);
  5903. } else {
  5904. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  5905. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5906. /* Turn off CPU output */
  5907. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5908. I915_WRITE(PCH_DREF_CONTROL, val);
  5909. POSTING_READ(PCH_DREF_CONTROL);
  5910. udelay(200);
  5911. /* Turn off the SSC source */
  5912. val &= ~DREF_SSC_SOURCE_MASK;
  5913. val |= DREF_SSC_SOURCE_DISABLE;
  5914. /* Turn off SSC1 */
  5915. val &= ~DREF_SSC1_ENABLE;
  5916. I915_WRITE(PCH_DREF_CONTROL, val);
  5917. POSTING_READ(PCH_DREF_CONTROL);
  5918. udelay(200);
  5919. }
  5920. BUG_ON(val != final);
  5921. }
  5922. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  5923. {
  5924. uint32_t tmp;
  5925. tmp = I915_READ(SOUTH_CHICKEN2);
  5926. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  5927. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5928. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  5929. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  5930. DRM_ERROR("FDI mPHY reset assert timeout\n");
  5931. tmp = I915_READ(SOUTH_CHICKEN2);
  5932. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  5933. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5934. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  5935. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  5936. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  5937. }
  5938. /* WaMPhyProgramming:hsw */
  5939. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  5940. {
  5941. uint32_t tmp;
  5942. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  5943. tmp &= ~(0xFF << 24);
  5944. tmp |= (0x12 << 24);
  5945. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  5946. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  5947. tmp |= (1 << 11);
  5948. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  5949. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  5950. tmp |= (1 << 11);
  5951. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  5952. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  5953. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5954. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  5955. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  5956. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5957. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  5958. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  5959. tmp &= ~(7 << 13);
  5960. tmp |= (5 << 13);
  5961. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  5962. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  5963. tmp &= ~(7 << 13);
  5964. tmp |= (5 << 13);
  5965. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  5966. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  5967. tmp &= ~0xFF;
  5968. tmp |= 0x1C;
  5969. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  5970. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  5971. tmp &= ~0xFF;
  5972. tmp |= 0x1C;
  5973. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  5974. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  5975. tmp &= ~(0xFF << 16);
  5976. tmp |= (0x1C << 16);
  5977. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  5978. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  5979. tmp &= ~(0xFF << 16);
  5980. tmp |= (0x1C << 16);
  5981. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  5982. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  5983. tmp |= (1 << 27);
  5984. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  5985. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  5986. tmp |= (1 << 27);
  5987. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  5988. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  5989. tmp &= ~(0xF << 28);
  5990. tmp |= (4 << 28);
  5991. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  5992. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  5993. tmp &= ~(0xF << 28);
  5994. tmp |= (4 << 28);
  5995. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  5996. }
  5997. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  5998. * Programming" based on the parameters passed:
  5999. * - Sequence to enable CLKOUT_DP
  6000. * - Sequence to enable CLKOUT_DP without spread
  6001. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  6002. */
  6003. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  6004. bool with_fdi)
  6005. {
  6006. struct drm_i915_private *dev_priv = dev->dev_private;
  6007. uint32_t reg, tmp;
  6008. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  6009. with_spread = true;
  6010. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  6011. with_fdi, "LP PCH doesn't have FDI\n"))
  6012. with_fdi = false;
  6013. mutex_lock(&dev_priv->dpio_lock);
  6014. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6015. tmp &= ~SBI_SSCCTL_DISABLE;
  6016. tmp |= SBI_SSCCTL_PATHALT;
  6017. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6018. udelay(24);
  6019. if (with_spread) {
  6020. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6021. tmp &= ~SBI_SSCCTL_PATHALT;
  6022. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6023. if (with_fdi) {
  6024. lpt_reset_fdi_mphy(dev_priv);
  6025. lpt_program_fdi_mphy(dev_priv);
  6026. }
  6027. }
  6028. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  6029. SBI_GEN0 : SBI_DBUFF0;
  6030. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6031. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6032. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6033. mutex_unlock(&dev_priv->dpio_lock);
  6034. }
  6035. /* Sequence to disable CLKOUT_DP */
  6036. static void lpt_disable_clkout_dp(struct drm_device *dev)
  6037. {
  6038. struct drm_i915_private *dev_priv = dev->dev_private;
  6039. uint32_t reg, tmp;
  6040. mutex_lock(&dev_priv->dpio_lock);
  6041. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  6042. SBI_GEN0 : SBI_DBUFF0;
  6043. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6044. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6045. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6046. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6047. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  6048. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  6049. tmp |= SBI_SSCCTL_PATHALT;
  6050. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6051. udelay(32);
  6052. }
  6053. tmp |= SBI_SSCCTL_DISABLE;
  6054. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6055. }
  6056. mutex_unlock(&dev_priv->dpio_lock);
  6057. }
  6058. static void lpt_init_pch_refclk(struct drm_device *dev)
  6059. {
  6060. struct intel_encoder *encoder;
  6061. bool has_vga = false;
  6062. for_each_intel_encoder(dev, encoder) {
  6063. switch (encoder->type) {
  6064. case INTEL_OUTPUT_ANALOG:
  6065. has_vga = true;
  6066. break;
  6067. default:
  6068. break;
  6069. }
  6070. }
  6071. if (has_vga)
  6072. lpt_enable_clkout_dp(dev, true, true);
  6073. else
  6074. lpt_disable_clkout_dp(dev);
  6075. }
  6076. /*
  6077. * Initialize reference clocks when the driver loads
  6078. */
  6079. void intel_init_pch_refclk(struct drm_device *dev)
  6080. {
  6081. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  6082. ironlake_init_pch_refclk(dev);
  6083. else if (HAS_PCH_LPT(dev))
  6084. lpt_init_pch_refclk(dev);
  6085. }
  6086. static int ironlake_get_refclk(struct drm_crtc *crtc)
  6087. {
  6088. struct drm_device *dev = crtc->dev;
  6089. struct drm_i915_private *dev_priv = dev->dev_private;
  6090. struct intel_encoder *encoder;
  6091. int num_connectors = 0;
  6092. bool is_lvds = false;
  6093. for_each_intel_encoder(dev, encoder) {
  6094. if (encoder->new_crtc != to_intel_crtc(crtc))
  6095. continue;
  6096. switch (encoder->type) {
  6097. case INTEL_OUTPUT_LVDS:
  6098. is_lvds = true;
  6099. break;
  6100. default:
  6101. break;
  6102. }
  6103. num_connectors++;
  6104. }
  6105. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  6106. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  6107. dev_priv->vbt.lvds_ssc_freq);
  6108. return dev_priv->vbt.lvds_ssc_freq;
  6109. }
  6110. return 120000;
  6111. }
  6112. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  6113. {
  6114. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  6115. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6116. int pipe = intel_crtc->pipe;
  6117. uint32_t val;
  6118. val = 0;
  6119. switch (intel_crtc->config->pipe_bpp) {
  6120. case 18:
  6121. val |= PIPECONF_6BPC;
  6122. break;
  6123. case 24:
  6124. val |= PIPECONF_8BPC;
  6125. break;
  6126. case 30:
  6127. val |= PIPECONF_10BPC;
  6128. break;
  6129. case 36:
  6130. val |= PIPECONF_12BPC;
  6131. break;
  6132. default:
  6133. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6134. BUG();
  6135. }
  6136. if (intel_crtc->config->dither)
  6137. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6138. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6139. val |= PIPECONF_INTERLACED_ILK;
  6140. else
  6141. val |= PIPECONF_PROGRESSIVE;
  6142. if (intel_crtc->config->limited_color_range)
  6143. val |= PIPECONF_COLOR_RANGE_SELECT;
  6144. I915_WRITE(PIPECONF(pipe), val);
  6145. POSTING_READ(PIPECONF(pipe));
  6146. }
  6147. /*
  6148. * Set up the pipe CSC unit.
  6149. *
  6150. * Currently only full range RGB to limited range RGB conversion
  6151. * is supported, but eventually this should handle various
  6152. * RGB<->YCbCr scenarios as well.
  6153. */
  6154. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  6155. {
  6156. struct drm_device *dev = crtc->dev;
  6157. struct drm_i915_private *dev_priv = dev->dev_private;
  6158. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6159. int pipe = intel_crtc->pipe;
  6160. uint16_t coeff = 0x7800; /* 1.0 */
  6161. /*
  6162. * TODO: Check what kind of values actually come out of the pipe
  6163. * with these coeff/postoff values and adjust to get the best
  6164. * accuracy. Perhaps we even need to take the bpc value into
  6165. * consideration.
  6166. */
  6167. if (intel_crtc->config->limited_color_range)
  6168. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  6169. /*
  6170. * GY/GU and RY/RU should be the other way around according
  6171. * to BSpec, but reality doesn't agree. Just set them up in
  6172. * a way that results in the correct picture.
  6173. */
  6174. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  6175. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  6176. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  6177. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  6178. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  6179. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  6180. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  6181. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  6182. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  6183. if (INTEL_INFO(dev)->gen > 6) {
  6184. uint16_t postoff = 0;
  6185. if (intel_crtc->config->limited_color_range)
  6186. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  6187. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  6188. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  6189. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  6190. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  6191. } else {
  6192. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  6193. if (intel_crtc->config->limited_color_range)
  6194. mode |= CSC_BLACK_SCREEN_OFFSET;
  6195. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  6196. }
  6197. }
  6198. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6199. {
  6200. struct drm_device *dev = crtc->dev;
  6201. struct drm_i915_private *dev_priv = dev->dev_private;
  6202. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6203. enum pipe pipe = intel_crtc->pipe;
  6204. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6205. uint32_t val;
  6206. val = 0;
  6207. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  6208. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6209. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6210. val |= PIPECONF_INTERLACED_ILK;
  6211. else
  6212. val |= PIPECONF_PROGRESSIVE;
  6213. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6214. POSTING_READ(PIPECONF(cpu_transcoder));
  6215. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  6216. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  6217. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  6218. val = 0;
  6219. switch (intel_crtc->config->pipe_bpp) {
  6220. case 18:
  6221. val |= PIPEMISC_DITHER_6_BPC;
  6222. break;
  6223. case 24:
  6224. val |= PIPEMISC_DITHER_8_BPC;
  6225. break;
  6226. case 30:
  6227. val |= PIPEMISC_DITHER_10_BPC;
  6228. break;
  6229. case 36:
  6230. val |= PIPEMISC_DITHER_12_BPC;
  6231. break;
  6232. default:
  6233. /* Case prevented by pipe_config_set_bpp. */
  6234. BUG();
  6235. }
  6236. if (intel_crtc->config->dither)
  6237. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  6238. I915_WRITE(PIPEMISC(pipe), val);
  6239. }
  6240. }
  6241. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  6242. struct intel_crtc_state *crtc_state,
  6243. intel_clock_t *clock,
  6244. bool *has_reduced_clock,
  6245. intel_clock_t *reduced_clock)
  6246. {
  6247. struct drm_device *dev = crtc->dev;
  6248. struct drm_i915_private *dev_priv = dev->dev_private;
  6249. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6250. int refclk;
  6251. const intel_limit_t *limit;
  6252. bool ret, is_lvds = false;
  6253. is_lvds = intel_pipe_will_have_type(intel_crtc, INTEL_OUTPUT_LVDS);
  6254. refclk = ironlake_get_refclk(crtc);
  6255. /*
  6256. * Returns a set of divisors for the desired target clock with the given
  6257. * refclk, or FALSE. The returned values represent the clock equation:
  6258. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  6259. */
  6260. limit = intel_limit(intel_crtc, refclk);
  6261. ret = dev_priv->display.find_dpll(limit, intel_crtc,
  6262. crtc_state->port_clock,
  6263. refclk, NULL, clock);
  6264. if (!ret)
  6265. return false;
  6266. if (is_lvds && dev_priv->lvds_downclock_avail) {
  6267. /*
  6268. * Ensure we match the reduced clock's P to the target clock.
  6269. * If the clocks don't match, we can't switch the display clock
  6270. * by using the FP0/FP1. In such case we will disable the LVDS
  6271. * downclock feature.
  6272. */
  6273. *has_reduced_clock =
  6274. dev_priv->display.find_dpll(limit, intel_crtc,
  6275. dev_priv->lvds_downclock,
  6276. refclk, clock,
  6277. reduced_clock);
  6278. }
  6279. return true;
  6280. }
  6281. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  6282. {
  6283. /*
  6284. * Account for spread spectrum to avoid
  6285. * oversubscribing the link. Max center spread
  6286. * is 2.5%; use 5% for safety's sake.
  6287. */
  6288. u32 bps = target_clock * bpp * 21 / 20;
  6289. return DIV_ROUND_UP(bps, link_bw * 8);
  6290. }
  6291. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  6292. {
  6293. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  6294. }
  6295. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  6296. struct intel_crtc_state *crtc_state,
  6297. u32 *fp,
  6298. intel_clock_t *reduced_clock, u32 *fp2)
  6299. {
  6300. struct drm_crtc *crtc = &intel_crtc->base;
  6301. struct drm_device *dev = crtc->dev;
  6302. struct drm_i915_private *dev_priv = dev->dev_private;
  6303. struct intel_encoder *intel_encoder;
  6304. uint32_t dpll;
  6305. int factor, num_connectors = 0;
  6306. bool is_lvds = false, is_sdvo = false;
  6307. for_each_intel_encoder(dev, intel_encoder) {
  6308. if (intel_encoder->new_crtc != to_intel_crtc(crtc))
  6309. continue;
  6310. switch (intel_encoder->type) {
  6311. case INTEL_OUTPUT_LVDS:
  6312. is_lvds = true;
  6313. break;
  6314. case INTEL_OUTPUT_SDVO:
  6315. case INTEL_OUTPUT_HDMI:
  6316. is_sdvo = true;
  6317. break;
  6318. default:
  6319. break;
  6320. }
  6321. num_connectors++;
  6322. }
  6323. /* Enable autotuning of the PLL clock (if permissible) */
  6324. factor = 21;
  6325. if (is_lvds) {
  6326. if ((intel_panel_use_ssc(dev_priv) &&
  6327. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6328. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  6329. factor = 25;
  6330. } else if (crtc_state->sdvo_tv_clock)
  6331. factor = 20;
  6332. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  6333. *fp |= FP_CB_TUNE;
  6334. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  6335. *fp2 |= FP_CB_TUNE;
  6336. dpll = 0;
  6337. if (is_lvds)
  6338. dpll |= DPLLB_MODE_LVDS;
  6339. else
  6340. dpll |= DPLLB_MODE_DAC_SERIAL;
  6341. dpll |= (crtc_state->pixel_multiplier - 1)
  6342. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6343. if (is_sdvo)
  6344. dpll |= DPLL_SDVO_HIGH_SPEED;
  6345. if (crtc_state->has_dp_encoder)
  6346. dpll |= DPLL_SDVO_HIGH_SPEED;
  6347. /* compute bitmask from p1 value */
  6348. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6349. /* also FPA1 */
  6350. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6351. switch (crtc_state->dpll.p2) {
  6352. case 5:
  6353. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6354. break;
  6355. case 7:
  6356. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6357. break;
  6358. case 10:
  6359. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6360. break;
  6361. case 14:
  6362. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6363. break;
  6364. }
  6365. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6366. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6367. else
  6368. dpll |= PLL_REF_INPUT_DREFCLK;
  6369. return dpll | DPLL_VCO_ENABLE;
  6370. }
  6371. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  6372. struct intel_crtc_state *crtc_state)
  6373. {
  6374. struct drm_device *dev = crtc->base.dev;
  6375. intel_clock_t clock, reduced_clock;
  6376. u32 dpll = 0, fp = 0, fp2 = 0;
  6377. bool ok, has_reduced_clock = false;
  6378. bool is_lvds = false;
  6379. struct intel_shared_dpll *pll;
  6380. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  6381. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  6382. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  6383. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  6384. &has_reduced_clock, &reduced_clock);
  6385. if (!ok && !crtc_state->clock_set) {
  6386. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6387. return -EINVAL;
  6388. }
  6389. /* Compat-code for transition, will disappear. */
  6390. if (!crtc_state->clock_set) {
  6391. crtc_state->dpll.n = clock.n;
  6392. crtc_state->dpll.m1 = clock.m1;
  6393. crtc_state->dpll.m2 = clock.m2;
  6394. crtc_state->dpll.p1 = clock.p1;
  6395. crtc_state->dpll.p2 = clock.p2;
  6396. }
  6397. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  6398. if (crtc_state->has_pch_encoder) {
  6399. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6400. if (has_reduced_clock)
  6401. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  6402. dpll = ironlake_compute_dpll(crtc, crtc_state,
  6403. &fp, &reduced_clock,
  6404. has_reduced_clock ? &fp2 : NULL);
  6405. crtc_state->dpll_hw_state.dpll = dpll;
  6406. crtc_state->dpll_hw_state.fp0 = fp;
  6407. if (has_reduced_clock)
  6408. crtc_state->dpll_hw_state.fp1 = fp2;
  6409. else
  6410. crtc_state->dpll_hw_state.fp1 = fp;
  6411. pll = intel_get_shared_dpll(crtc, crtc_state);
  6412. if (pll == NULL) {
  6413. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  6414. pipe_name(crtc->pipe));
  6415. return -EINVAL;
  6416. }
  6417. }
  6418. if (is_lvds && has_reduced_clock && i915.powersave)
  6419. crtc->lowfreq_avail = true;
  6420. else
  6421. crtc->lowfreq_avail = false;
  6422. return 0;
  6423. }
  6424. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  6425. struct intel_link_m_n *m_n)
  6426. {
  6427. struct drm_device *dev = crtc->base.dev;
  6428. struct drm_i915_private *dev_priv = dev->dev_private;
  6429. enum pipe pipe = crtc->pipe;
  6430. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  6431. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  6432. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  6433. & ~TU_SIZE_MASK;
  6434. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  6435. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  6436. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6437. }
  6438. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  6439. enum transcoder transcoder,
  6440. struct intel_link_m_n *m_n,
  6441. struct intel_link_m_n *m2_n2)
  6442. {
  6443. struct drm_device *dev = crtc->base.dev;
  6444. struct drm_i915_private *dev_priv = dev->dev_private;
  6445. enum pipe pipe = crtc->pipe;
  6446. if (INTEL_INFO(dev)->gen >= 5) {
  6447. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  6448. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  6449. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  6450. & ~TU_SIZE_MASK;
  6451. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  6452. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  6453. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6454. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  6455. * gen < 8) and if DRRS is supported (to make sure the
  6456. * registers are not unnecessarily read).
  6457. */
  6458. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  6459. crtc->config->has_drrs) {
  6460. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  6461. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  6462. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  6463. & ~TU_SIZE_MASK;
  6464. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  6465. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  6466. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6467. }
  6468. } else {
  6469. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  6470. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  6471. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  6472. & ~TU_SIZE_MASK;
  6473. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  6474. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  6475. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6476. }
  6477. }
  6478. void intel_dp_get_m_n(struct intel_crtc *crtc,
  6479. struct intel_crtc_state *pipe_config)
  6480. {
  6481. if (pipe_config->has_pch_encoder)
  6482. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  6483. else
  6484. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6485. &pipe_config->dp_m_n,
  6486. &pipe_config->dp_m2_n2);
  6487. }
  6488. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  6489. struct intel_crtc_state *pipe_config)
  6490. {
  6491. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6492. &pipe_config->fdi_m_n, NULL);
  6493. }
  6494. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  6495. struct intel_crtc_state *pipe_config)
  6496. {
  6497. struct drm_device *dev = crtc->base.dev;
  6498. struct drm_i915_private *dev_priv = dev->dev_private;
  6499. uint32_t tmp;
  6500. tmp = I915_READ(PS_CTL(crtc->pipe));
  6501. if (tmp & PS_ENABLE) {
  6502. pipe_config->pch_pfit.enabled = true;
  6503. pipe_config->pch_pfit.pos = I915_READ(PS_WIN_POS(crtc->pipe));
  6504. pipe_config->pch_pfit.size = I915_READ(PS_WIN_SZ(crtc->pipe));
  6505. }
  6506. }
  6507. static void
  6508. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  6509. struct intel_initial_plane_config *plane_config)
  6510. {
  6511. struct drm_device *dev = crtc->base.dev;
  6512. struct drm_i915_private *dev_priv = dev->dev_private;
  6513. u32 val, base, offset, stride_mult, tiling;
  6514. int pipe = crtc->pipe;
  6515. int fourcc, pixel_format;
  6516. int aligned_height;
  6517. struct drm_framebuffer *fb;
  6518. struct intel_framebuffer *intel_fb;
  6519. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6520. if (!intel_fb) {
  6521. DRM_DEBUG_KMS("failed to alloc fb\n");
  6522. return;
  6523. }
  6524. fb = &intel_fb->base;
  6525. val = I915_READ(PLANE_CTL(pipe, 0));
  6526. if (!(val & PLANE_CTL_ENABLE))
  6527. goto error;
  6528. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  6529. fourcc = skl_format_to_fourcc(pixel_format,
  6530. val & PLANE_CTL_ORDER_RGBX,
  6531. val & PLANE_CTL_ALPHA_MASK);
  6532. fb->pixel_format = fourcc;
  6533. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6534. tiling = val & PLANE_CTL_TILED_MASK;
  6535. switch (tiling) {
  6536. case PLANE_CTL_TILED_LINEAR:
  6537. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  6538. break;
  6539. case PLANE_CTL_TILED_X:
  6540. plane_config->tiling = I915_TILING_X;
  6541. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6542. break;
  6543. case PLANE_CTL_TILED_Y:
  6544. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  6545. break;
  6546. case PLANE_CTL_TILED_YF:
  6547. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  6548. break;
  6549. default:
  6550. MISSING_CASE(tiling);
  6551. goto error;
  6552. }
  6553. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  6554. plane_config->base = base;
  6555. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  6556. val = I915_READ(PLANE_SIZE(pipe, 0));
  6557. fb->height = ((val >> 16) & 0xfff) + 1;
  6558. fb->width = ((val >> 0) & 0x1fff) + 1;
  6559. val = I915_READ(PLANE_STRIDE(pipe, 0));
  6560. stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
  6561. fb->pixel_format);
  6562. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  6563. aligned_height = intel_fb_align_height(dev, fb->height,
  6564. fb->pixel_format,
  6565. fb->modifier[0]);
  6566. plane_config->size = fb->pitches[0] * aligned_height;
  6567. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6568. pipe_name(pipe), fb->width, fb->height,
  6569. fb->bits_per_pixel, base, fb->pitches[0],
  6570. plane_config->size);
  6571. plane_config->fb = intel_fb;
  6572. return;
  6573. error:
  6574. kfree(fb);
  6575. }
  6576. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  6577. struct intel_crtc_state *pipe_config)
  6578. {
  6579. struct drm_device *dev = crtc->base.dev;
  6580. struct drm_i915_private *dev_priv = dev->dev_private;
  6581. uint32_t tmp;
  6582. tmp = I915_READ(PF_CTL(crtc->pipe));
  6583. if (tmp & PF_ENABLE) {
  6584. pipe_config->pch_pfit.enabled = true;
  6585. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  6586. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  6587. /* We currently do not free assignements of panel fitters on
  6588. * ivb/hsw (since we don't use the higher upscaling modes which
  6589. * differentiates them) so just WARN about this case for now. */
  6590. if (IS_GEN7(dev)) {
  6591. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  6592. PF_PIPE_SEL_IVB(crtc->pipe));
  6593. }
  6594. }
  6595. }
  6596. static void
  6597. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  6598. struct intel_initial_plane_config *plane_config)
  6599. {
  6600. struct drm_device *dev = crtc->base.dev;
  6601. struct drm_i915_private *dev_priv = dev->dev_private;
  6602. u32 val, base, offset;
  6603. int pipe = crtc->pipe;
  6604. int fourcc, pixel_format;
  6605. int aligned_height;
  6606. struct drm_framebuffer *fb;
  6607. struct intel_framebuffer *intel_fb;
  6608. val = I915_READ(DSPCNTR(pipe));
  6609. if (!(val & DISPLAY_PLANE_ENABLE))
  6610. return;
  6611. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6612. if (!intel_fb) {
  6613. DRM_DEBUG_KMS("failed to alloc fb\n");
  6614. return;
  6615. }
  6616. fb = &intel_fb->base;
  6617. if (INTEL_INFO(dev)->gen >= 4) {
  6618. if (val & DISPPLANE_TILED) {
  6619. plane_config->tiling = I915_TILING_X;
  6620. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6621. }
  6622. }
  6623. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6624. fourcc = i9xx_format_to_fourcc(pixel_format);
  6625. fb->pixel_format = fourcc;
  6626. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6627. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  6628. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  6629. offset = I915_READ(DSPOFFSET(pipe));
  6630. } else {
  6631. if (plane_config->tiling)
  6632. offset = I915_READ(DSPTILEOFF(pipe));
  6633. else
  6634. offset = I915_READ(DSPLINOFF(pipe));
  6635. }
  6636. plane_config->base = base;
  6637. val = I915_READ(PIPESRC(pipe));
  6638. fb->width = ((val >> 16) & 0xfff) + 1;
  6639. fb->height = ((val >> 0) & 0xfff) + 1;
  6640. val = I915_READ(DSPSTRIDE(pipe));
  6641. fb->pitches[0] = val & 0xffffffc0;
  6642. aligned_height = intel_fb_align_height(dev, fb->height,
  6643. fb->pixel_format,
  6644. fb->modifier[0]);
  6645. plane_config->size = fb->pitches[0] * aligned_height;
  6646. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6647. pipe_name(pipe), fb->width, fb->height,
  6648. fb->bits_per_pixel, base, fb->pitches[0],
  6649. plane_config->size);
  6650. plane_config->fb = intel_fb;
  6651. }
  6652. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  6653. struct intel_crtc_state *pipe_config)
  6654. {
  6655. struct drm_device *dev = crtc->base.dev;
  6656. struct drm_i915_private *dev_priv = dev->dev_private;
  6657. uint32_t tmp;
  6658. if (!intel_display_power_is_enabled(dev_priv,
  6659. POWER_DOMAIN_PIPE(crtc->pipe)))
  6660. return false;
  6661. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6662. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6663. tmp = I915_READ(PIPECONF(crtc->pipe));
  6664. if (!(tmp & PIPECONF_ENABLE))
  6665. return false;
  6666. switch (tmp & PIPECONF_BPC_MASK) {
  6667. case PIPECONF_6BPC:
  6668. pipe_config->pipe_bpp = 18;
  6669. break;
  6670. case PIPECONF_8BPC:
  6671. pipe_config->pipe_bpp = 24;
  6672. break;
  6673. case PIPECONF_10BPC:
  6674. pipe_config->pipe_bpp = 30;
  6675. break;
  6676. case PIPECONF_12BPC:
  6677. pipe_config->pipe_bpp = 36;
  6678. break;
  6679. default:
  6680. break;
  6681. }
  6682. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  6683. pipe_config->limited_color_range = true;
  6684. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  6685. struct intel_shared_dpll *pll;
  6686. pipe_config->has_pch_encoder = true;
  6687. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  6688. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6689. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6690. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6691. if (HAS_PCH_IBX(dev_priv->dev)) {
  6692. pipe_config->shared_dpll =
  6693. (enum intel_dpll_id) crtc->pipe;
  6694. } else {
  6695. tmp = I915_READ(PCH_DPLL_SEL);
  6696. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  6697. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  6698. else
  6699. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  6700. }
  6701. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6702. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6703. &pipe_config->dpll_hw_state));
  6704. tmp = pipe_config->dpll_hw_state.dpll;
  6705. pipe_config->pixel_multiplier =
  6706. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  6707. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  6708. ironlake_pch_clock_get(crtc, pipe_config);
  6709. } else {
  6710. pipe_config->pixel_multiplier = 1;
  6711. }
  6712. intel_get_pipe_timings(crtc, pipe_config);
  6713. ironlake_get_pfit_config(crtc, pipe_config);
  6714. return true;
  6715. }
  6716. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  6717. {
  6718. struct drm_device *dev = dev_priv->dev;
  6719. struct intel_crtc *crtc;
  6720. for_each_intel_crtc(dev, crtc)
  6721. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  6722. pipe_name(crtc->pipe));
  6723. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  6724. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  6725. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  6726. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  6727. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  6728. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  6729. "CPU PWM1 enabled\n");
  6730. if (IS_HASWELL(dev))
  6731. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  6732. "CPU PWM2 enabled\n");
  6733. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  6734. "PCH PWM1 enabled\n");
  6735. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  6736. "Utility pin enabled\n");
  6737. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  6738. /*
  6739. * In theory we can still leave IRQs enabled, as long as only the HPD
  6740. * interrupts remain enabled. We used to check for that, but since it's
  6741. * gen-specific and since we only disable LCPLL after we fully disable
  6742. * the interrupts, the check below should be enough.
  6743. */
  6744. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  6745. }
  6746. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  6747. {
  6748. struct drm_device *dev = dev_priv->dev;
  6749. if (IS_HASWELL(dev))
  6750. return I915_READ(D_COMP_HSW);
  6751. else
  6752. return I915_READ(D_COMP_BDW);
  6753. }
  6754. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  6755. {
  6756. struct drm_device *dev = dev_priv->dev;
  6757. if (IS_HASWELL(dev)) {
  6758. mutex_lock(&dev_priv->rps.hw_lock);
  6759. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  6760. val))
  6761. DRM_ERROR("Failed to write to D_COMP\n");
  6762. mutex_unlock(&dev_priv->rps.hw_lock);
  6763. } else {
  6764. I915_WRITE(D_COMP_BDW, val);
  6765. POSTING_READ(D_COMP_BDW);
  6766. }
  6767. }
  6768. /*
  6769. * This function implements pieces of two sequences from BSpec:
  6770. * - Sequence for display software to disable LCPLL
  6771. * - Sequence for display software to allow package C8+
  6772. * The steps implemented here are just the steps that actually touch the LCPLL
  6773. * register. Callers should take care of disabling all the display engine
  6774. * functions, doing the mode unset, fixing interrupts, etc.
  6775. */
  6776. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  6777. bool switch_to_fclk, bool allow_power_down)
  6778. {
  6779. uint32_t val;
  6780. assert_can_disable_lcpll(dev_priv);
  6781. val = I915_READ(LCPLL_CTL);
  6782. if (switch_to_fclk) {
  6783. val |= LCPLL_CD_SOURCE_FCLK;
  6784. I915_WRITE(LCPLL_CTL, val);
  6785. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  6786. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  6787. DRM_ERROR("Switching to FCLK failed\n");
  6788. val = I915_READ(LCPLL_CTL);
  6789. }
  6790. val |= LCPLL_PLL_DISABLE;
  6791. I915_WRITE(LCPLL_CTL, val);
  6792. POSTING_READ(LCPLL_CTL);
  6793. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  6794. DRM_ERROR("LCPLL still locked\n");
  6795. val = hsw_read_dcomp(dev_priv);
  6796. val |= D_COMP_COMP_DISABLE;
  6797. hsw_write_dcomp(dev_priv, val);
  6798. ndelay(100);
  6799. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  6800. 1))
  6801. DRM_ERROR("D_COMP RCOMP still in progress\n");
  6802. if (allow_power_down) {
  6803. val = I915_READ(LCPLL_CTL);
  6804. val |= LCPLL_POWER_DOWN_ALLOW;
  6805. I915_WRITE(LCPLL_CTL, val);
  6806. POSTING_READ(LCPLL_CTL);
  6807. }
  6808. }
  6809. /*
  6810. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  6811. * source.
  6812. */
  6813. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  6814. {
  6815. uint32_t val;
  6816. val = I915_READ(LCPLL_CTL);
  6817. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  6818. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  6819. return;
  6820. /*
  6821. * Make sure we're not on PC8 state before disabling PC8, otherwise
  6822. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  6823. */
  6824. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  6825. if (val & LCPLL_POWER_DOWN_ALLOW) {
  6826. val &= ~LCPLL_POWER_DOWN_ALLOW;
  6827. I915_WRITE(LCPLL_CTL, val);
  6828. POSTING_READ(LCPLL_CTL);
  6829. }
  6830. val = hsw_read_dcomp(dev_priv);
  6831. val |= D_COMP_COMP_FORCE;
  6832. val &= ~D_COMP_COMP_DISABLE;
  6833. hsw_write_dcomp(dev_priv, val);
  6834. val = I915_READ(LCPLL_CTL);
  6835. val &= ~LCPLL_PLL_DISABLE;
  6836. I915_WRITE(LCPLL_CTL, val);
  6837. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  6838. DRM_ERROR("LCPLL not locked yet\n");
  6839. if (val & LCPLL_CD_SOURCE_FCLK) {
  6840. val = I915_READ(LCPLL_CTL);
  6841. val &= ~LCPLL_CD_SOURCE_FCLK;
  6842. I915_WRITE(LCPLL_CTL, val);
  6843. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  6844. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  6845. DRM_ERROR("Switching back to LCPLL failed\n");
  6846. }
  6847. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  6848. }
  6849. /*
  6850. * Package states C8 and deeper are really deep PC states that can only be
  6851. * reached when all the devices on the system allow it, so even if the graphics
  6852. * device allows PC8+, it doesn't mean the system will actually get to these
  6853. * states. Our driver only allows PC8+ when going into runtime PM.
  6854. *
  6855. * The requirements for PC8+ are that all the outputs are disabled, the power
  6856. * well is disabled and most interrupts are disabled, and these are also
  6857. * requirements for runtime PM. When these conditions are met, we manually do
  6858. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  6859. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  6860. * hang the machine.
  6861. *
  6862. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  6863. * the state of some registers, so when we come back from PC8+ we need to
  6864. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  6865. * need to take care of the registers kept by RC6. Notice that this happens even
  6866. * if we don't put the device in PCI D3 state (which is what currently happens
  6867. * because of the runtime PM support).
  6868. *
  6869. * For more, read "Display Sequences for Package C8" on the hardware
  6870. * documentation.
  6871. */
  6872. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  6873. {
  6874. struct drm_device *dev = dev_priv->dev;
  6875. uint32_t val;
  6876. DRM_DEBUG_KMS("Enabling package C8+\n");
  6877. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6878. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6879. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  6880. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6881. }
  6882. lpt_disable_clkout_dp(dev);
  6883. hsw_disable_lcpll(dev_priv, true, true);
  6884. }
  6885. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  6886. {
  6887. struct drm_device *dev = dev_priv->dev;
  6888. uint32_t val;
  6889. DRM_DEBUG_KMS("Disabling package C8+\n");
  6890. hsw_restore_lcpll(dev_priv);
  6891. lpt_init_pch_refclk(dev);
  6892. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6893. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6894. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  6895. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6896. }
  6897. intel_prepare_ddi(dev);
  6898. }
  6899. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  6900. struct intel_crtc_state *crtc_state)
  6901. {
  6902. if (!intel_ddi_pll_select(crtc, crtc_state))
  6903. return -EINVAL;
  6904. crtc->lowfreq_avail = false;
  6905. return 0;
  6906. }
  6907. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  6908. enum port port,
  6909. struct intel_crtc_state *pipe_config)
  6910. {
  6911. u32 temp, dpll_ctl1;
  6912. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  6913. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  6914. switch (pipe_config->ddi_pll_sel) {
  6915. case SKL_DPLL0:
  6916. /*
  6917. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  6918. * of the shared DPLL framework and thus needs to be read out
  6919. * separately
  6920. */
  6921. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  6922. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  6923. break;
  6924. case SKL_DPLL1:
  6925. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  6926. break;
  6927. case SKL_DPLL2:
  6928. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  6929. break;
  6930. case SKL_DPLL3:
  6931. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  6932. break;
  6933. }
  6934. }
  6935. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  6936. enum port port,
  6937. struct intel_crtc_state *pipe_config)
  6938. {
  6939. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  6940. switch (pipe_config->ddi_pll_sel) {
  6941. case PORT_CLK_SEL_WRPLL1:
  6942. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  6943. break;
  6944. case PORT_CLK_SEL_WRPLL2:
  6945. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  6946. break;
  6947. }
  6948. }
  6949. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  6950. struct intel_crtc_state *pipe_config)
  6951. {
  6952. struct drm_device *dev = crtc->base.dev;
  6953. struct drm_i915_private *dev_priv = dev->dev_private;
  6954. struct intel_shared_dpll *pll;
  6955. enum port port;
  6956. uint32_t tmp;
  6957. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  6958. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  6959. if (IS_SKYLAKE(dev))
  6960. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  6961. else
  6962. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  6963. if (pipe_config->shared_dpll >= 0) {
  6964. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6965. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6966. &pipe_config->dpll_hw_state));
  6967. }
  6968. /*
  6969. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  6970. * DDI E. So just check whether this pipe is wired to DDI E and whether
  6971. * the PCH transcoder is on.
  6972. */
  6973. if (INTEL_INFO(dev)->gen < 9 &&
  6974. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  6975. pipe_config->has_pch_encoder = true;
  6976. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  6977. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6978. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6979. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6980. }
  6981. }
  6982. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  6983. struct intel_crtc_state *pipe_config)
  6984. {
  6985. struct drm_device *dev = crtc->base.dev;
  6986. struct drm_i915_private *dev_priv = dev->dev_private;
  6987. enum intel_display_power_domain pfit_domain;
  6988. uint32_t tmp;
  6989. if (!intel_display_power_is_enabled(dev_priv,
  6990. POWER_DOMAIN_PIPE(crtc->pipe)))
  6991. return false;
  6992. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6993. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6994. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  6995. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  6996. enum pipe trans_edp_pipe;
  6997. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  6998. default:
  6999. WARN(1, "unknown pipe linked to edp transcoder\n");
  7000. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7001. case TRANS_DDI_EDP_INPUT_A_ON:
  7002. trans_edp_pipe = PIPE_A;
  7003. break;
  7004. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  7005. trans_edp_pipe = PIPE_B;
  7006. break;
  7007. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  7008. trans_edp_pipe = PIPE_C;
  7009. break;
  7010. }
  7011. if (trans_edp_pipe == crtc->pipe)
  7012. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  7013. }
  7014. if (!intel_display_power_is_enabled(dev_priv,
  7015. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  7016. return false;
  7017. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  7018. if (!(tmp & PIPECONF_ENABLE))
  7019. return false;
  7020. haswell_get_ddi_port_state(crtc, pipe_config);
  7021. intel_get_pipe_timings(crtc, pipe_config);
  7022. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  7023. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  7024. if (IS_SKYLAKE(dev))
  7025. skylake_get_pfit_config(crtc, pipe_config);
  7026. else
  7027. ironlake_get_pfit_config(crtc, pipe_config);
  7028. }
  7029. if (IS_HASWELL(dev))
  7030. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  7031. (I915_READ(IPS_CTL) & IPS_ENABLE);
  7032. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  7033. pipe_config->pixel_multiplier =
  7034. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  7035. } else {
  7036. pipe_config->pixel_multiplier = 1;
  7037. }
  7038. return true;
  7039. }
  7040. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  7041. {
  7042. struct drm_device *dev = crtc->dev;
  7043. struct drm_i915_private *dev_priv = dev->dev_private;
  7044. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7045. uint32_t cntl = 0, size = 0;
  7046. if (base) {
  7047. unsigned int width = intel_crtc->base.cursor->state->crtc_w;
  7048. unsigned int height = intel_crtc->base.cursor->state->crtc_h;
  7049. unsigned int stride = roundup_pow_of_two(width) * 4;
  7050. switch (stride) {
  7051. default:
  7052. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  7053. width, stride);
  7054. stride = 256;
  7055. /* fallthrough */
  7056. case 256:
  7057. case 512:
  7058. case 1024:
  7059. case 2048:
  7060. break;
  7061. }
  7062. cntl |= CURSOR_ENABLE |
  7063. CURSOR_GAMMA_ENABLE |
  7064. CURSOR_FORMAT_ARGB |
  7065. CURSOR_STRIDE(stride);
  7066. size = (height << 12) | width;
  7067. }
  7068. if (intel_crtc->cursor_cntl != 0 &&
  7069. (intel_crtc->cursor_base != base ||
  7070. intel_crtc->cursor_size != size ||
  7071. intel_crtc->cursor_cntl != cntl)) {
  7072. /* On these chipsets we can only modify the base/size/stride
  7073. * whilst the cursor is disabled.
  7074. */
  7075. I915_WRITE(_CURACNTR, 0);
  7076. POSTING_READ(_CURACNTR);
  7077. intel_crtc->cursor_cntl = 0;
  7078. }
  7079. if (intel_crtc->cursor_base != base) {
  7080. I915_WRITE(_CURABASE, base);
  7081. intel_crtc->cursor_base = base;
  7082. }
  7083. if (intel_crtc->cursor_size != size) {
  7084. I915_WRITE(CURSIZE, size);
  7085. intel_crtc->cursor_size = size;
  7086. }
  7087. if (intel_crtc->cursor_cntl != cntl) {
  7088. I915_WRITE(_CURACNTR, cntl);
  7089. POSTING_READ(_CURACNTR);
  7090. intel_crtc->cursor_cntl = cntl;
  7091. }
  7092. }
  7093. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  7094. {
  7095. struct drm_device *dev = crtc->dev;
  7096. struct drm_i915_private *dev_priv = dev->dev_private;
  7097. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7098. int pipe = intel_crtc->pipe;
  7099. uint32_t cntl;
  7100. cntl = 0;
  7101. if (base) {
  7102. cntl = MCURSOR_GAMMA_ENABLE;
  7103. switch (intel_crtc->base.cursor->state->crtc_w) {
  7104. case 64:
  7105. cntl |= CURSOR_MODE_64_ARGB_AX;
  7106. break;
  7107. case 128:
  7108. cntl |= CURSOR_MODE_128_ARGB_AX;
  7109. break;
  7110. case 256:
  7111. cntl |= CURSOR_MODE_256_ARGB_AX;
  7112. break;
  7113. default:
  7114. MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
  7115. return;
  7116. }
  7117. cntl |= pipe << 28; /* Connect to correct pipe */
  7118. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  7119. cntl |= CURSOR_PIPE_CSC_ENABLE;
  7120. }
  7121. if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
  7122. cntl |= CURSOR_ROTATE_180;
  7123. if (intel_crtc->cursor_cntl != cntl) {
  7124. I915_WRITE(CURCNTR(pipe), cntl);
  7125. POSTING_READ(CURCNTR(pipe));
  7126. intel_crtc->cursor_cntl = cntl;
  7127. }
  7128. /* and commit changes on next vblank */
  7129. I915_WRITE(CURBASE(pipe), base);
  7130. POSTING_READ(CURBASE(pipe));
  7131. intel_crtc->cursor_base = base;
  7132. }
  7133. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  7134. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  7135. bool on)
  7136. {
  7137. struct drm_device *dev = crtc->dev;
  7138. struct drm_i915_private *dev_priv = dev->dev_private;
  7139. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7140. int pipe = intel_crtc->pipe;
  7141. int x = crtc->cursor_x;
  7142. int y = crtc->cursor_y;
  7143. u32 base = 0, pos = 0;
  7144. if (on)
  7145. base = intel_crtc->cursor_addr;
  7146. if (x >= intel_crtc->config->pipe_src_w)
  7147. base = 0;
  7148. if (y >= intel_crtc->config->pipe_src_h)
  7149. base = 0;
  7150. if (x < 0) {
  7151. if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
  7152. base = 0;
  7153. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  7154. x = -x;
  7155. }
  7156. pos |= x << CURSOR_X_SHIFT;
  7157. if (y < 0) {
  7158. if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
  7159. base = 0;
  7160. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  7161. y = -y;
  7162. }
  7163. pos |= y << CURSOR_Y_SHIFT;
  7164. if (base == 0 && intel_crtc->cursor_base == 0)
  7165. return;
  7166. I915_WRITE(CURPOS(pipe), pos);
  7167. /* ILK+ do this automagically */
  7168. if (HAS_GMCH_DISPLAY(dev) &&
  7169. crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
  7170. base += (intel_crtc->base.cursor->state->crtc_h *
  7171. intel_crtc->base.cursor->state->crtc_w - 1) * 4;
  7172. }
  7173. if (IS_845G(dev) || IS_I865G(dev))
  7174. i845_update_cursor(crtc, base);
  7175. else
  7176. i9xx_update_cursor(crtc, base);
  7177. }
  7178. static bool cursor_size_ok(struct drm_device *dev,
  7179. uint32_t width, uint32_t height)
  7180. {
  7181. if (width == 0 || height == 0)
  7182. return false;
  7183. /*
  7184. * 845g/865g are special in that they are only limited by
  7185. * the width of their cursors, the height is arbitrary up to
  7186. * the precision of the register. Everything else requires
  7187. * square cursors, limited to a few power-of-two sizes.
  7188. */
  7189. if (IS_845G(dev) || IS_I865G(dev)) {
  7190. if ((width & 63) != 0)
  7191. return false;
  7192. if (width > (IS_845G(dev) ? 64 : 512))
  7193. return false;
  7194. if (height > 1023)
  7195. return false;
  7196. } else {
  7197. switch (width | height) {
  7198. case 256:
  7199. case 128:
  7200. if (IS_GEN2(dev))
  7201. return false;
  7202. case 64:
  7203. break;
  7204. default:
  7205. return false;
  7206. }
  7207. }
  7208. return true;
  7209. }
  7210. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  7211. u16 *blue, uint32_t start, uint32_t size)
  7212. {
  7213. int end = (start + size > 256) ? 256 : start + size, i;
  7214. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7215. for (i = start; i < end; i++) {
  7216. intel_crtc->lut_r[i] = red[i] >> 8;
  7217. intel_crtc->lut_g[i] = green[i] >> 8;
  7218. intel_crtc->lut_b[i] = blue[i] >> 8;
  7219. }
  7220. intel_crtc_load_lut(crtc);
  7221. }
  7222. /* VESA 640x480x72Hz mode to set on the pipe */
  7223. static struct drm_display_mode load_detect_mode = {
  7224. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  7225. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  7226. };
  7227. struct drm_framebuffer *
  7228. __intel_framebuffer_create(struct drm_device *dev,
  7229. struct drm_mode_fb_cmd2 *mode_cmd,
  7230. struct drm_i915_gem_object *obj)
  7231. {
  7232. struct intel_framebuffer *intel_fb;
  7233. int ret;
  7234. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7235. if (!intel_fb) {
  7236. drm_gem_object_unreference(&obj->base);
  7237. return ERR_PTR(-ENOMEM);
  7238. }
  7239. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  7240. if (ret)
  7241. goto err;
  7242. return &intel_fb->base;
  7243. err:
  7244. drm_gem_object_unreference(&obj->base);
  7245. kfree(intel_fb);
  7246. return ERR_PTR(ret);
  7247. }
  7248. static struct drm_framebuffer *
  7249. intel_framebuffer_create(struct drm_device *dev,
  7250. struct drm_mode_fb_cmd2 *mode_cmd,
  7251. struct drm_i915_gem_object *obj)
  7252. {
  7253. struct drm_framebuffer *fb;
  7254. int ret;
  7255. ret = i915_mutex_lock_interruptible(dev);
  7256. if (ret)
  7257. return ERR_PTR(ret);
  7258. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  7259. mutex_unlock(&dev->struct_mutex);
  7260. return fb;
  7261. }
  7262. static u32
  7263. intel_framebuffer_pitch_for_width(int width, int bpp)
  7264. {
  7265. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  7266. return ALIGN(pitch, 64);
  7267. }
  7268. static u32
  7269. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  7270. {
  7271. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  7272. return PAGE_ALIGN(pitch * mode->vdisplay);
  7273. }
  7274. static struct drm_framebuffer *
  7275. intel_framebuffer_create_for_mode(struct drm_device *dev,
  7276. struct drm_display_mode *mode,
  7277. int depth, int bpp)
  7278. {
  7279. struct drm_i915_gem_object *obj;
  7280. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  7281. obj = i915_gem_alloc_object(dev,
  7282. intel_framebuffer_size_for_mode(mode, bpp));
  7283. if (obj == NULL)
  7284. return ERR_PTR(-ENOMEM);
  7285. mode_cmd.width = mode->hdisplay;
  7286. mode_cmd.height = mode->vdisplay;
  7287. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  7288. bpp);
  7289. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  7290. return intel_framebuffer_create(dev, &mode_cmd, obj);
  7291. }
  7292. static struct drm_framebuffer *
  7293. mode_fits_in_fbdev(struct drm_device *dev,
  7294. struct drm_display_mode *mode)
  7295. {
  7296. #ifdef CONFIG_DRM_I915_FBDEV
  7297. struct drm_i915_private *dev_priv = dev->dev_private;
  7298. struct drm_i915_gem_object *obj;
  7299. struct drm_framebuffer *fb;
  7300. if (!dev_priv->fbdev)
  7301. return NULL;
  7302. if (!dev_priv->fbdev->fb)
  7303. return NULL;
  7304. obj = dev_priv->fbdev->fb->obj;
  7305. BUG_ON(!obj);
  7306. fb = &dev_priv->fbdev->fb->base;
  7307. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  7308. fb->bits_per_pixel))
  7309. return NULL;
  7310. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  7311. return NULL;
  7312. return fb;
  7313. #else
  7314. return NULL;
  7315. #endif
  7316. }
  7317. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  7318. struct drm_display_mode *mode,
  7319. struct intel_load_detect_pipe *old,
  7320. struct drm_modeset_acquire_ctx *ctx)
  7321. {
  7322. struct intel_crtc *intel_crtc;
  7323. struct intel_encoder *intel_encoder =
  7324. intel_attached_encoder(connector);
  7325. struct drm_crtc *possible_crtc;
  7326. struct drm_encoder *encoder = &intel_encoder->base;
  7327. struct drm_crtc *crtc = NULL;
  7328. struct drm_device *dev = encoder->dev;
  7329. struct drm_framebuffer *fb;
  7330. struct drm_mode_config *config = &dev->mode_config;
  7331. int ret, i = -1;
  7332. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7333. connector->base.id, connector->name,
  7334. encoder->base.id, encoder->name);
  7335. retry:
  7336. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  7337. if (ret)
  7338. goto fail_unlock;
  7339. /*
  7340. * Algorithm gets a little messy:
  7341. *
  7342. * - if the connector already has an assigned crtc, use it (but make
  7343. * sure it's on first)
  7344. *
  7345. * - try to find the first unused crtc that can drive this connector,
  7346. * and use that if we find one
  7347. */
  7348. /* See if we already have a CRTC for this connector */
  7349. if (encoder->crtc) {
  7350. crtc = encoder->crtc;
  7351. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7352. if (ret)
  7353. goto fail_unlock;
  7354. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  7355. if (ret)
  7356. goto fail_unlock;
  7357. old->dpms_mode = connector->dpms;
  7358. old->load_detect_temp = false;
  7359. /* Make sure the crtc and connector are running */
  7360. if (connector->dpms != DRM_MODE_DPMS_ON)
  7361. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  7362. return true;
  7363. }
  7364. /* Find an unused one (if possible) */
  7365. for_each_crtc(dev, possible_crtc) {
  7366. i++;
  7367. if (!(encoder->possible_crtcs & (1 << i)))
  7368. continue;
  7369. if (possible_crtc->state->enable)
  7370. continue;
  7371. /* This can occur when applying the pipe A quirk on resume. */
  7372. if (to_intel_crtc(possible_crtc)->new_enabled)
  7373. continue;
  7374. crtc = possible_crtc;
  7375. break;
  7376. }
  7377. /*
  7378. * If we didn't find an unused CRTC, don't use any.
  7379. */
  7380. if (!crtc) {
  7381. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  7382. goto fail_unlock;
  7383. }
  7384. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7385. if (ret)
  7386. goto fail_unlock;
  7387. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  7388. if (ret)
  7389. goto fail_unlock;
  7390. intel_encoder->new_crtc = to_intel_crtc(crtc);
  7391. to_intel_connector(connector)->new_encoder = intel_encoder;
  7392. intel_crtc = to_intel_crtc(crtc);
  7393. intel_crtc->new_enabled = true;
  7394. intel_crtc->new_config = intel_crtc->config;
  7395. old->dpms_mode = connector->dpms;
  7396. old->load_detect_temp = true;
  7397. old->release_fb = NULL;
  7398. if (!mode)
  7399. mode = &load_detect_mode;
  7400. /* We need a framebuffer large enough to accommodate all accesses
  7401. * that the plane may generate whilst we perform load detection.
  7402. * We can not rely on the fbcon either being present (we get called
  7403. * during its initialisation to detect all boot displays, or it may
  7404. * not even exist) or that it is large enough to satisfy the
  7405. * requested mode.
  7406. */
  7407. fb = mode_fits_in_fbdev(dev, mode);
  7408. if (fb == NULL) {
  7409. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  7410. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  7411. old->release_fb = fb;
  7412. } else
  7413. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  7414. if (IS_ERR(fb)) {
  7415. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  7416. goto fail;
  7417. }
  7418. if (intel_set_mode(crtc, mode, 0, 0, fb)) {
  7419. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  7420. if (old->release_fb)
  7421. old->release_fb->funcs->destroy(old->release_fb);
  7422. goto fail;
  7423. }
  7424. crtc->primary->crtc = crtc;
  7425. /* let the connector get through one full cycle before testing */
  7426. intel_wait_for_vblank(dev, intel_crtc->pipe);
  7427. return true;
  7428. fail:
  7429. intel_crtc->new_enabled = crtc->state->enable;
  7430. if (intel_crtc->new_enabled)
  7431. intel_crtc->new_config = intel_crtc->config;
  7432. else
  7433. intel_crtc->new_config = NULL;
  7434. fail_unlock:
  7435. if (ret == -EDEADLK) {
  7436. drm_modeset_backoff(ctx);
  7437. goto retry;
  7438. }
  7439. return false;
  7440. }
  7441. void intel_release_load_detect_pipe(struct drm_connector *connector,
  7442. struct intel_load_detect_pipe *old)
  7443. {
  7444. struct intel_encoder *intel_encoder =
  7445. intel_attached_encoder(connector);
  7446. struct drm_encoder *encoder = &intel_encoder->base;
  7447. struct drm_crtc *crtc = encoder->crtc;
  7448. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7449. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7450. connector->base.id, connector->name,
  7451. encoder->base.id, encoder->name);
  7452. if (old->load_detect_temp) {
  7453. to_intel_connector(connector)->new_encoder = NULL;
  7454. intel_encoder->new_crtc = NULL;
  7455. intel_crtc->new_enabled = false;
  7456. intel_crtc->new_config = NULL;
  7457. intel_set_mode(crtc, NULL, 0, 0, NULL);
  7458. if (old->release_fb) {
  7459. drm_framebuffer_unregister_private(old->release_fb);
  7460. drm_framebuffer_unreference(old->release_fb);
  7461. }
  7462. return;
  7463. }
  7464. /* Switch crtc and encoder back off if necessary */
  7465. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  7466. connector->funcs->dpms(connector, old->dpms_mode);
  7467. }
  7468. static int i9xx_pll_refclk(struct drm_device *dev,
  7469. const struct intel_crtc_state *pipe_config)
  7470. {
  7471. struct drm_i915_private *dev_priv = dev->dev_private;
  7472. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7473. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  7474. return dev_priv->vbt.lvds_ssc_freq;
  7475. else if (HAS_PCH_SPLIT(dev))
  7476. return 120000;
  7477. else if (!IS_GEN2(dev))
  7478. return 96000;
  7479. else
  7480. return 48000;
  7481. }
  7482. /* Returns the clock of the currently programmed mode of the given pipe. */
  7483. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  7484. struct intel_crtc_state *pipe_config)
  7485. {
  7486. struct drm_device *dev = crtc->base.dev;
  7487. struct drm_i915_private *dev_priv = dev->dev_private;
  7488. int pipe = pipe_config->cpu_transcoder;
  7489. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7490. u32 fp;
  7491. intel_clock_t clock;
  7492. int refclk = i9xx_pll_refclk(dev, pipe_config);
  7493. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  7494. fp = pipe_config->dpll_hw_state.fp0;
  7495. else
  7496. fp = pipe_config->dpll_hw_state.fp1;
  7497. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  7498. if (IS_PINEVIEW(dev)) {
  7499. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  7500. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7501. } else {
  7502. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  7503. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7504. }
  7505. if (!IS_GEN2(dev)) {
  7506. if (IS_PINEVIEW(dev))
  7507. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  7508. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  7509. else
  7510. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  7511. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7512. switch (dpll & DPLL_MODE_MASK) {
  7513. case DPLLB_MODE_DAC_SERIAL:
  7514. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  7515. 5 : 10;
  7516. break;
  7517. case DPLLB_MODE_LVDS:
  7518. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  7519. 7 : 14;
  7520. break;
  7521. default:
  7522. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  7523. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  7524. return;
  7525. }
  7526. if (IS_PINEVIEW(dev))
  7527. pineview_clock(refclk, &clock);
  7528. else
  7529. i9xx_clock(refclk, &clock);
  7530. } else {
  7531. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  7532. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  7533. if (is_lvds) {
  7534. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  7535. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7536. if (lvds & LVDS_CLKB_POWER_UP)
  7537. clock.p2 = 7;
  7538. else
  7539. clock.p2 = 14;
  7540. } else {
  7541. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  7542. clock.p1 = 2;
  7543. else {
  7544. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  7545. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  7546. }
  7547. if (dpll & PLL_P2_DIVIDE_BY_4)
  7548. clock.p2 = 4;
  7549. else
  7550. clock.p2 = 2;
  7551. }
  7552. i9xx_clock(refclk, &clock);
  7553. }
  7554. /*
  7555. * This value includes pixel_multiplier. We will use
  7556. * port_clock to compute adjusted_mode.crtc_clock in the
  7557. * encoder's get_config() function.
  7558. */
  7559. pipe_config->port_clock = clock.dot;
  7560. }
  7561. int intel_dotclock_calculate(int link_freq,
  7562. const struct intel_link_m_n *m_n)
  7563. {
  7564. /*
  7565. * The calculation for the data clock is:
  7566. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  7567. * But we want to avoid losing precison if possible, so:
  7568. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  7569. *
  7570. * and the link clock is simpler:
  7571. * link_clock = (m * link_clock) / n
  7572. */
  7573. if (!m_n->link_n)
  7574. return 0;
  7575. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  7576. }
  7577. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  7578. struct intel_crtc_state *pipe_config)
  7579. {
  7580. struct drm_device *dev = crtc->base.dev;
  7581. /* read out port_clock from the DPLL */
  7582. i9xx_crtc_clock_get(crtc, pipe_config);
  7583. /*
  7584. * This value does not include pixel_multiplier.
  7585. * We will check that port_clock and adjusted_mode.crtc_clock
  7586. * agree once we know their relationship in the encoder's
  7587. * get_config() function.
  7588. */
  7589. pipe_config->base.adjusted_mode.crtc_clock =
  7590. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  7591. &pipe_config->fdi_m_n);
  7592. }
  7593. /** Returns the currently programmed mode of the given pipe. */
  7594. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  7595. struct drm_crtc *crtc)
  7596. {
  7597. struct drm_i915_private *dev_priv = dev->dev_private;
  7598. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7599. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7600. struct drm_display_mode *mode;
  7601. struct intel_crtc_state pipe_config;
  7602. int htot = I915_READ(HTOTAL(cpu_transcoder));
  7603. int hsync = I915_READ(HSYNC(cpu_transcoder));
  7604. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  7605. int vsync = I915_READ(VSYNC(cpu_transcoder));
  7606. enum pipe pipe = intel_crtc->pipe;
  7607. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  7608. if (!mode)
  7609. return NULL;
  7610. /*
  7611. * Construct a pipe_config sufficient for getting the clock info
  7612. * back out of crtc_clock_get.
  7613. *
  7614. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  7615. * to use a real value here instead.
  7616. */
  7617. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  7618. pipe_config.pixel_multiplier = 1;
  7619. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  7620. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  7621. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  7622. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  7623. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  7624. mode->hdisplay = (htot & 0xffff) + 1;
  7625. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  7626. mode->hsync_start = (hsync & 0xffff) + 1;
  7627. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  7628. mode->vdisplay = (vtot & 0xffff) + 1;
  7629. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  7630. mode->vsync_start = (vsync & 0xffff) + 1;
  7631. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  7632. drm_mode_set_name(mode);
  7633. return mode;
  7634. }
  7635. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  7636. {
  7637. struct drm_device *dev = crtc->dev;
  7638. struct drm_i915_private *dev_priv = dev->dev_private;
  7639. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7640. if (!HAS_GMCH_DISPLAY(dev))
  7641. return;
  7642. if (!dev_priv->lvds_downclock_avail)
  7643. return;
  7644. /*
  7645. * Since this is called by a timer, we should never get here in
  7646. * the manual case.
  7647. */
  7648. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  7649. int pipe = intel_crtc->pipe;
  7650. int dpll_reg = DPLL(pipe);
  7651. int dpll;
  7652. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  7653. assert_panel_unlocked(dev_priv, pipe);
  7654. dpll = I915_READ(dpll_reg);
  7655. dpll |= DISPLAY_RATE_SELECT_FPA1;
  7656. I915_WRITE(dpll_reg, dpll);
  7657. intel_wait_for_vblank(dev, pipe);
  7658. dpll = I915_READ(dpll_reg);
  7659. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  7660. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  7661. }
  7662. }
  7663. void intel_mark_busy(struct drm_device *dev)
  7664. {
  7665. struct drm_i915_private *dev_priv = dev->dev_private;
  7666. if (dev_priv->mm.busy)
  7667. return;
  7668. intel_runtime_pm_get(dev_priv);
  7669. i915_update_gfx_val(dev_priv);
  7670. dev_priv->mm.busy = true;
  7671. }
  7672. void intel_mark_idle(struct drm_device *dev)
  7673. {
  7674. struct drm_i915_private *dev_priv = dev->dev_private;
  7675. struct drm_crtc *crtc;
  7676. if (!dev_priv->mm.busy)
  7677. return;
  7678. dev_priv->mm.busy = false;
  7679. if (!i915.powersave)
  7680. goto out;
  7681. for_each_crtc(dev, crtc) {
  7682. if (!crtc->primary->fb)
  7683. continue;
  7684. intel_decrease_pllclock(crtc);
  7685. }
  7686. if (INTEL_INFO(dev)->gen >= 6)
  7687. gen6_rps_idle(dev->dev_private);
  7688. out:
  7689. intel_runtime_pm_put(dev_priv);
  7690. }
  7691. static void intel_crtc_set_state(struct intel_crtc *crtc,
  7692. struct intel_crtc_state *crtc_state)
  7693. {
  7694. kfree(crtc->config);
  7695. crtc->config = crtc_state;
  7696. crtc->base.state = &crtc_state->base;
  7697. }
  7698. static void intel_crtc_destroy(struct drm_crtc *crtc)
  7699. {
  7700. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7701. struct drm_device *dev = crtc->dev;
  7702. struct intel_unpin_work *work;
  7703. spin_lock_irq(&dev->event_lock);
  7704. work = intel_crtc->unpin_work;
  7705. intel_crtc->unpin_work = NULL;
  7706. spin_unlock_irq(&dev->event_lock);
  7707. if (work) {
  7708. cancel_work_sync(&work->work);
  7709. kfree(work);
  7710. }
  7711. intel_crtc_set_state(intel_crtc, NULL);
  7712. drm_crtc_cleanup(crtc);
  7713. kfree(intel_crtc);
  7714. }
  7715. static void intel_unpin_work_fn(struct work_struct *__work)
  7716. {
  7717. struct intel_unpin_work *work =
  7718. container_of(__work, struct intel_unpin_work, work);
  7719. struct drm_device *dev = work->crtc->dev;
  7720. enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
  7721. mutex_lock(&dev->struct_mutex);
  7722. intel_unpin_fb_obj(intel_fb_obj(work->old_fb));
  7723. drm_gem_object_unreference(&work->pending_flip_obj->base);
  7724. drm_framebuffer_unreference(work->old_fb);
  7725. intel_fbc_update(dev);
  7726. if (work->flip_queued_req)
  7727. i915_gem_request_assign(&work->flip_queued_req, NULL);
  7728. mutex_unlock(&dev->struct_mutex);
  7729. intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  7730. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  7731. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  7732. kfree(work);
  7733. }
  7734. static void do_intel_finish_page_flip(struct drm_device *dev,
  7735. struct drm_crtc *crtc)
  7736. {
  7737. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7738. struct intel_unpin_work *work;
  7739. unsigned long flags;
  7740. /* Ignore early vblank irqs */
  7741. if (intel_crtc == NULL)
  7742. return;
  7743. /*
  7744. * This is called both by irq handlers and the reset code (to complete
  7745. * lost pageflips) so needs the full irqsave spinlocks.
  7746. */
  7747. spin_lock_irqsave(&dev->event_lock, flags);
  7748. work = intel_crtc->unpin_work;
  7749. /* Ensure we don't miss a work->pending update ... */
  7750. smp_rmb();
  7751. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  7752. spin_unlock_irqrestore(&dev->event_lock, flags);
  7753. return;
  7754. }
  7755. page_flip_completed(intel_crtc);
  7756. spin_unlock_irqrestore(&dev->event_lock, flags);
  7757. }
  7758. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  7759. {
  7760. struct drm_i915_private *dev_priv = dev->dev_private;
  7761. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  7762. do_intel_finish_page_flip(dev, crtc);
  7763. }
  7764. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  7765. {
  7766. struct drm_i915_private *dev_priv = dev->dev_private;
  7767. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  7768. do_intel_finish_page_flip(dev, crtc);
  7769. }
  7770. /* Is 'a' after or equal to 'b'? */
  7771. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  7772. {
  7773. return !((a - b) & 0x80000000);
  7774. }
  7775. static bool page_flip_finished(struct intel_crtc *crtc)
  7776. {
  7777. struct drm_device *dev = crtc->base.dev;
  7778. struct drm_i915_private *dev_priv = dev->dev_private;
  7779. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  7780. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  7781. return true;
  7782. /*
  7783. * The relevant registers doen't exist on pre-ctg.
  7784. * As the flip done interrupt doesn't trigger for mmio
  7785. * flips on gmch platforms, a flip count check isn't
  7786. * really needed there. But since ctg has the registers,
  7787. * include it in the check anyway.
  7788. */
  7789. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  7790. return true;
  7791. /*
  7792. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  7793. * used the same base address. In that case the mmio flip might
  7794. * have completed, but the CS hasn't even executed the flip yet.
  7795. *
  7796. * A flip count check isn't enough as the CS might have updated
  7797. * the base address just after start of vblank, but before we
  7798. * managed to process the interrupt. This means we'd complete the
  7799. * CS flip too soon.
  7800. *
  7801. * Combining both checks should get us a good enough result. It may
  7802. * still happen that the CS flip has been executed, but has not
  7803. * yet actually completed. But in case the base address is the same
  7804. * anyway, we don't really care.
  7805. */
  7806. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  7807. crtc->unpin_work->gtt_offset &&
  7808. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  7809. crtc->unpin_work->flip_count);
  7810. }
  7811. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  7812. {
  7813. struct drm_i915_private *dev_priv = dev->dev_private;
  7814. struct intel_crtc *intel_crtc =
  7815. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  7816. unsigned long flags;
  7817. /*
  7818. * This is called both by irq handlers and the reset code (to complete
  7819. * lost pageflips) so needs the full irqsave spinlocks.
  7820. *
  7821. * NB: An MMIO update of the plane base pointer will also
  7822. * generate a page-flip completion irq, i.e. every modeset
  7823. * is also accompanied by a spurious intel_prepare_page_flip().
  7824. */
  7825. spin_lock_irqsave(&dev->event_lock, flags);
  7826. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  7827. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  7828. spin_unlock_irqrestore(&dev->event_lock, flags);
  7829. }
  7830. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  7831. {
  7832. /* Ensure that the work item is consistent when activating it ... */
  7833. smp_wmb();
  7834. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  7835. /* and that it is marked active as soon as the irq could fire. */
  7836. smp_wmb();
  7837. }
  7838. static int intel_gen2_queue_flip(struct drm_device *dev,
  7839. struct drm_crtc *crtc,
  7840. struct drm_framebuffer *fb,
  7841. struct drm_i915_gem_object *obj,
  7842. struct intel_engine_cs *ring,
  7843. uint32_t flags)
  7844. {
  7845. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7846. u32 flip_mask;
  7847. int ret;
  7848. ret = intel_ring_begin(ring, 6);
  7849. if (ret)
  7850. return ret;
  7851. /* Can't queue multiple flips, so wait for the previous
  7852. * one to finish before executing the next.
  7853. */
  7854. if (intel_crtc->plane)
  7855. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7856. else
  7857. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7858. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7859. intel_ring_emit(ring, MI_NOOP);
  7860. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7861. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7862. intel_ring_emit(ring, fb->pitches[0]);
  7863. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7864. intel_ring_emit(ring, 0); /* aux display base address, unused */
  7865. intel_mark_page_flip_active(intel_crtc);
  7866. __intel_ring_advance(ring);
  7867. return 0;
  7868. }
  7869. static int intel_gen3_queue_flip(struct drm_device *dev,
  7870. struct drm_crtc *crtc,
  7871. struct drm_framebuffer *fb,
  7872. struct drm_i915_gem_object *obj,
  7873. struct intel_engine_cs *ring,
  7874. uint32_t flags)
  7875. {
  7876. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7877. u32 flip_mask;
  7878. int ret;
  7879. ret = intel_ring_begin(ring, 6);
  7880. if (ret)
  7881. return ret;
  7882. if (intel_crtc->plane)
  7883. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7884. else
  7885. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7886. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7887. intel_ring_emit(ring, MI_NOOP);
  7888. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  7889. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7890. intel_ring_emit(ring, fb->pitches[0]);
  7891. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7892. intel_ring_emit(ring, MI_NOOP);
  7893. intel_mark_page_flip_active(intel_crtc);
  7894. __intel_ring_advance(ring);
  7895. return 0;
  7896. }
  7897. static int intel_gen4_queue_flip(struct drm_device *dev,
  7898. struct drm_crtc *crtc,
  7899. struct drm_framebuffer *fb,
  7900. struct drm_i915_gem_object *obj,
  7901. struct intel_engine_cs *ring,
  7902. uint32_t flags)
  7903. {
  7904. struct drm_i915_private *dev_priv = dev->dev_private;
  7905. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7906. uint32_t pf, pipesrc;
  7907. int ret;
  7908. ret = intel_ring_begin(ring, 4);
  7909. if (ret)
  7910. return ret;
  7911. /* i965+ uses the linear or tiled offsets from the
  7912. * Display Registers (which do not change across a page-flip)
  7913. * so we need only reprogram the base address.
  7914. */
  7915. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7916. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7917. intel_ring_emit(ring, fb->pitches[0]);
  7918. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  7919. obj->tiling_mode);
  7920. /* XXX Enabling the panel-fitter across page-flip is so far
  7921. * untested on non-native modes, so ignore it for now.
  7922. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  7923. */
  7924. pf = 0;
  7925. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7926. intel_ring_emit(ring, pf | pipesrc);
  7927. intel_mark_page_flip_active(intel_crtc);
  7928. __intel_ring_advance(ring);
  7929. return 0;
  7930. }
  7931. static int intel_gen6_queue_flip(struct drm_device *dev,
  7932. struct drm_crtc *crtc,
  7933. struct drm_framebuffer *fb,
  7934. struct drm_i915_gem_object *obj,
  7935. struct intel_engine_cs *ring,
  7936. uint32_t flags)
  7937. {
  7938. struct drm_i915_private *dev_priv = dev->dev_private;
  7939. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7940. uint32_t pf, pipesrc;
  7941. int ret;
  7942. ret = intel_ring_begin(ring, 4);
  7943. if (ret)
  7944. return ret;
  7945. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7946. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7947. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  7948. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7949. /* Contrary to the suggestions in the documentation,
  7950. * "Enable Panel Fitter" does not seem to be required when page
  7951. * flipping with a non-native mode, and worse causes a normal
  7952. * modeset to fail.
  7953. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  7954. */
  7955. pf = 0;
  7956. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7957. intel_ring_emit(ring, pf | pipesrc);
  7958. intel_mark_page_flip_active(intel_crtc);
  7959. __intel_ring_advance(ring);
  7960. return 0;
  7961. }
  7962. static int intel_gen7_queue_flip(struct drm_device *dev,
  7963. struct drm_crtc *crtc,
  7964. struct drm_framebuffer *fb,
  7965. struct drm_i915_gem_object *obj,
  7966. struct intel_engine_cs *ring,
  7967. uint32_t flags)
  7968. {
  7969. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7970. uint32_t plane_bit = 0;
  7971. int len, ret;
  7972. switch (intel_crtc->plane) {
  7973. case PLANE_A:
  7974. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  7975. break;
  7976. case PLANE_B:
  7977. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  7978. break;
  7979. case PLANE_C:
  7980. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  7981. break;
  7982. default:
  7983. WARN_ONCE(1, "unknown plane in flip command\n");
  7984. return -ENODEV;
  7985. }
  7986. len = 4;
  7987. if (ring->id == RCS) {
  7988. len += 6;
  7989. /*
  7990. * On Gen 8, SRM is now taking an extra dword to accommodate
  7991. * 48bits addresses, and we need a NOOP for the batch size to
  7992. * stay even.
  7993. */
  7994. if (IS_GEN8(dev))
  7995. len += 2;
  7996. }
  7997. /*
  7998. * BSpec MI_DISPLAY_FLIP for IVB:
  7999. * "The full packet must be contained within the same cache line."
  8000. *
  8001. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  8002. * cacheline, if we ever start emitting more commands before
  8003. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  8004. * then do the cacheline alignment, and finally emit the
  8005. * MI_DISPLAY_FLIP.
  8006. */
  8007. ret = intel_ring_cacheline_align(ring);
  8008. if (ret)
  8009. return ret;
  8010. ret = intel_ring_begin(ring, len);
  8011. if (ret)
  8012. return ret;
  8013. /* Unmask the flip-done completion message. Note that the bspec says that
  8014. * we should do this for both the BCS and RCS, and that we must not unmask
  8015. * more than one flip event at any time (or ensure that one flip message
  8016. * can be sent by waiting for flip-done prior to queueing new flips).
  8017. * Experimentation says that BCS works despite DERRMR masking all
  8018. * flip-done completion events and that unmasking all planes at once
  8019. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  8020. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  8021. */
  8022. if (ring->id == RCS) {
  8023. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  8024. intel_ring_emit(ring, DERRMR);
  8025. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  8026. DERRMR_PIPEB_PRI_FLIP_DONE |
  8027. DERRMR_PIPEC_PRI_FLIP_DONE));
  8028. if (IS_GEN8(dev))
  8029. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  8030. MI_SRM_LRM_GLOBAL_GTT);
  8031. else
  8032. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  8033. MI_SRM_LRM_GLOBAL_GTT);
  8034. intel_ring_emit(ring, DERRMR);
  8035. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  8036. if (IS_GEN8(dev)) {
  8037. intel_ring_emit(ring, 0);
  8038. intel_ring_emit(ring, MI_NOOP);
  8039. }
  8040. }
  8041. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  8042. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  8043. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8044. intel_ring_emit(ring, (MI_NOOP));
  8045. intel_mark_page_flip_active(intel_crtc);
  8046. __intel_ring_advance(ring);
  8047. return 0;
  8048. }
  8049. static bool use_mmio_flip(struct intel_engine_cs *ring,
  8050. struct drm_i915_gem_object *obj)
  8051. {
  8052. /*
  8053. * This is not being used for older platforms, because
  8054. * non-availability of flip done interrupt forces us to use
  8055. * CS flips. Older platforms derive flip done using some clever
  8056. * tricks involving the flip_pending status bits and vblank irqs.
  8057. * So using MMIO flips there would disrupt this mechanism.
  8058. */
  8059. if (ring == NULL)
  8060. return true;
  8061. if (INTEL_INFO(ring->dev)->gen < 5)
  8062. return false;
  8063. if (i915.use_mmio_flip < 0)
  8064. return false;
  8065. else if (i915.use_mmio_flip > 0)
  8066. return true;
  8067. else if (i915.enable_execlists)
  8068. return true;
  8069. else
  8070. return ring != i915_gem_request_get_ring(obj->last_read_req);
  8071. }
  8072. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
  8073. {
  8074. struct drm_device *dev = intel_crtc->base.dev;
  8075. struct drm_i915_private *dev_priv = dev->dev_private;
  8076. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  8077. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  8078. struct drm_i915_gem_object *obj = intel_fb->obj;
  8079. const enum pipe pipe = intel_crtc->pipe;
  8080. u32 ctl, stride;
  8081. ctl = I915_READ(PLANE_CTL(pipe, 0));
  8082. ctl &= ~PLANE_CTL_TILED_MASK;
  8083. if (obj->tiling_mode == I915_TILING_X)
  8084. ctl |= PLANE_CTL_TILED_X;
  8085. /*
  8086. * The stride is either expressed as a multiple of 64 bytes chunks for
  8087. * linear buffers or in number of tiles for tiled buffers.
  8088. */
  8089. stride = fb->pitches[0] >> 6;
  8090. if (obj->tiling_mode == I915_TILING_X)
  8091. stride = fb->pitches[0] >> 9; /* X tiles are 512 bytes wide */
  8092. /*
  8093. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  8094. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  8095. */
  8096. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  8097. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  8098. I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
  8099. POSTING_READ(PLANE_SURF(pipe, 0));
  8100. }
  8101. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
  8102. {
  8103. struct drm_device *dev = intel_crtc->base.dev;
  8104. struct drm_i915_private *dev_priv = dev->dev_private;
  8105. struct intel_framebuffer *intel_fb =
  8106. to_intel_framebuffer(intel_crtc->base.primary->fb);
  8107. struct drm_i915_gem_object *obj = intel_fb->obj;
  8108. u32 dspcntr;
  8109. u32 reg;
  8110. reg = DSPCNTR(intel_crtc->plane);
  8111. dspcntr = I915_READ(reg);
  8112. if (obj->tiling_mode != I915_TILING_NONE)
  8113. dspcntr |= DISPPLANE_TILED;
  8114. else
  8115. dspcntr &= ~DISPPLANE_TILED;
  8116. I915_WRITE(reg, dspcntr);
  8117. I915_WRITE(DSPSURF(intel_crtc->plane),
  8118. intel_crtc->unpin_work->gtt_offset);
  8119. POSTING_READ(DSPSURF(intel_crtc->plane));
  8120. }
  8121. /*
  8122. * XXX: This is the temporary way to update the plane registers until we get
  8123. * around to using the usual plane update functions for MMIO flips
  8124. */
  8125. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  8126. {
  8127. struct drm_device *dev = intel_crtc->base.dev;
  8128. bool atomic_update;
  8129. u32 start_vbl_count;
  8130. intel_mark_page_flip_active(intel_crtc);
  8131. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  8132. if (INTEL_INFO(dev)->gen >= 9)
  8133. skl_do_mmio_flip(intel_crtc);
  8134. else
  8135. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  8136. ilk_do_mmio_flip(intel_crtc);
  8137. if (atomic_update)
  8138. intel_pipe_update_end(intel_crtc, start_vbl_count);
  8139. }
  8140. static void intel_mmio_flip_work_func(struct work_struct *work)
  8141. {
  8142. struct intel_crtc *crtc =
  8143. container_of(work, struct intel_crtc, mmio_flip.work);
  8144. struct intel_mmio_flip *mmio_flip;
  8145. mmio_flip = &crtc->mmio_flip;
  8146. if (mmio_flip->req)
  8147. WARN_ON(__i915_wait_request(mmio_flip->req,
  8148. crtc->reset_counter,
  8149. false, NULL, NULL) != 0);
  8150. intel_do_mmio_flip(crtc);
  8151. if (mmio_flip->req) {
  8152. mutex_lock(&crtc->base.dev->struct_mutex);
  8153. i915_gem_request_assign(&mmio_flip->req, NULL);
  8154. mutex_unlock(&crtc->base.dev->struct_mutex);
  8155. }
  8156. }
  8157. static int intel_queue_mmio_flip(struct drm_device *dev,
  8158. struct drm_crtc *crtc,
  8159. struct drm_framebuffer *fb,
  8160. struct drm_i915_gem_object *obj,
  8161. struct intel_engine_cs *ring,
  8162. uint32_t flags)
  8163. {
  8164. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8165. i915_gem_request_assign(&intel_crtc->mmio_flip.req,
  8166. obj->last_write_req);
  8167. schedule_work(&intel_crtc->mmio_flip.work);
  8168. return 0;
  8169. }
  8170. static int intel_default_queue_flip(struct drm_device *dev,
  8171. struct drm_crtc *crtc,
  8172. struct drm_framebuffer *fb,
  8173. struct drm_i915_gem_object *obj,
  8174. struct intel_engine_cs *ring,
  8175. uint32_t flags)
  8176. {
  8177. return -ENODEV;
  8178. }
  8179. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  8180. struct drm_crtc *crtc)
  8181. {
  8182. struct drm_i915_private *dev_priv = dev->dev_private;
  8183. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8184. struct intel_unpin_work *work = intel_crtc->unpin_work;
  8185. u32 addr;
  8186. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  8187. return true;
  8188. if (!work->enable_stall_check)
  8189. return false;
  8190. if (work->flip_ready_vblank == 0) {
  8191. if (work->flip_queued_req &&
  8192. !i915_gem_request_completed(work->flip_queued_req, true))
  8193. return false;
  8194. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  8195. }
  8196. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  8197. return false;
  8198. /* Potential stall - if we see that the flip has happened,
  8199. * assume a missed interrupt. */
  8200. if (INTEL_INFO(dev)->gen >= 4)
  8201. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  8202. else
  8203. addr = I915_READ(DSPADDR(intel_crtc->plane));
  8204. /* There is a potential issue here with a false positive after a flip
  8205. * to the same address. We could address this by checking for a
  8206. * non-incrementing frame counter.
  8207. */
  8208. return addr == work->gtt_offset;
  8209. }
  8210. void intel_check_page_flip(struct drm_device *dev, int pipe)
  8211. {
  8212. struct drm_i915_private *dev_priv = dev->dev_private;
  8213. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  8214. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8215. WARN_ON(!in_irq());
  8216. if (crtc == NULL)
  8217. return;
  8218. spin_lock(&dev->event_lock);
  8219. if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
  8220. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  8221. intel_crtc->unpin_work->flip_queued_vblank,
  8222. drm_vblank_count(dev, pipe));
  8223. page_flip_completed(intel_crtc);
  8224. }
  8225. spin_unlock(&dev->event_lock);
  8226. }
  8227. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  8228. struct drm_framebuffer *fb,
  8229. struct drm_pending_vblank_event *event,
  8230. uint32_t page_flip_flags)
  8231. {
  8232. struct drm_device *dev = crtc->dev;
  8233. struct drm_i915_private *dev_priv = dev->dev_private;
  8234. struct drm_framebuffer *old_fb = crtc->primary->fb;
  8235. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  8236. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8237. struct drm_plane *primary = crtc->primary;
  8238. enum pipe pipe = intel_crtc->pipe;
  8239. struct intel_unpin_work *work;
  8240. struct intel_engine_cs *ring;
  8241. int ret;
  8242. /*
  8243. * drm_mode_page_flip_ioctl() should already catch this, but double
  8244. * check to be safe. In the future we may enable pageflipping from
  8245. * a disabled primary plane.
  8246. */
  8247. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  8248. return -EBUSY;
  8249. /* Can't change pixel format via MI display flips. */
  8250. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  8251. return -EINVAL;
  8252. /*
  8253. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  8254. * Note that pitch changes could also affect these register.
  8255. */
  8256. if (INTEL_INFO(dev)->gen > 3 &&
  8257. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  8258. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  8259. return -EINVAL;
  8260. if (i915_terminally_wedged(&dev_priv->gpu_error))
  8261. goto out_hang;
  8262. work = kzalloc(sizeof(*work), GFP_KERNEL);
  8263. if (work == NULL)
  8264. return -ENOMEM;
  8265. work->event = event;
  8266. work->crtc = crtc;
  8267. work->old_fb = old_fb;
  8268. INIT_WORK(&work->work, intel_unpin_work_fn);
  8269. ret = drm_crtc_vblank_get(crtc);
  8270. if (ret)
  8271. goto free_work;
  8272. /* We borrow the event spin lock for protecting unpin_work */
  8273. spin_lock_irq(&dev->event_lock);
  8274. if (intel_crtc->unpin_work) {
  8275. /* Before declaring the flip queue wedged, check if
  8276. * the hardware completed the operation behind our backs.
  8277. */
  8278. if (__intel_pageflip_stall_check(dev, crtc)) {
  8279. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  8280. page_flip_completed(intel_crtc);
  8281. } else {
  8282. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  8283. spin_unlock_irq(&dev->event_lock);
  8284. drm_crtc_vblank_put(crtc);
  8285. kfree(work);
  8286. return -EBUSY;
  8287. }
  8288. }
  8289. intel_crtc->unpin_work = work;
  8290. spin_unlock_irq(&dev->event_lock);
  8291. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  8292. flush_workqueue(dev_priv->wq);
  8293. ret = i915_mutex_lock_interruptible(dev);
  8294. if (ret)
  8295. goto cleanup;
  8296. /* Reference the objects for the scheduled work. */
  8297. drm_framebuffer_reference(work->old_fb);
  8298. drm_gem_object_reference(&obj->base);
  8299. crtc->primary->fb = fb;
  8300. update_state_fb(crtc->primary);
  8301. work->pending_flip_obj = obj;
  8302. atomic_inc(&intel_crtc->unpin_work_count);
  8303. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  8304. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  8305. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  8306. if (IS_VALLEYVIEW(dev)) {
  8307. ring = &dev_priv->ring[BCS];
  8308. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  8309. /* vlv: DISPLAY_FLIP fails to change tiling */
  8310. ring = NULL;
  8311. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  8312. ring = &dev_priv->ring[BCS];
  8313. } else if (INTEL_INFO(dev)->gen >= 7) {
  8314. ring = i915_gem_request_get_ring(obj->last_read_req);
  8315. if (ring == NULL || ring->id != RCS)
  8316. ring = &dev_priv->ring[BCS];
  8317. } else {
  8318. ring = &dev_priv->ring[RCS];
  8319. }
  8320. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb, ring);
  8321. if (ret)
  8322. goto cleanup_pending;
  8323. work->gtt_offset =
  8324. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
  8325. if (use_mmio_flip(ring, obj)) {
  8326. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  8327. page_flip_flags);
  8328. if (ret)
  8329. goto cleanup_unpin;
  8330. i915_gem_request_assign(&work->flip_queued_req,
  8331. obj->last_write_req);
  8332. } else {
  8333. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
  8334. page_flip_flags);
  8335. if (ret)
  8336. goto cleanup_unpin;
  8337. i915_gem_request_assign(&work->flip_queued_req,
  8338. intel_ring_get_request(ring));
  8339. }
  8340. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  8341. work->enable_stall_check = true;
  8342. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  8343. INTEL_FRONTBUFFER_PRIMARY(pipe));
  8344. intel_fbc_disable(dev);
  8345. intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  8346. mutex_unlock(&dev->struct_mutex);
  8347. trace_i915_flip_request(intel_crtc->plane, obj);
  8348. return 0;
  8349. cleanup_unpin:
  8350. intel_unpin_fb_obj(obj);
  8351. cleanup_pending:
  8352. atomic_dec(&intel_crtc->unpin_work_count);
  8353. crtc->primary->fb = old_fb;
  8354. update_state_fb(crtc->primary);
  8355. drm_framebuffer_unreference(work->old_fb);
  8356. drm_gem_object_unreference(&obj->base);
  8357. mutex_unlock(&dev->struct_mutex);
  8358. cleanup:
  8359. spin_lock_irq(&dev->event_lock);
  8360. intel_crtc->unpin_work = NULL;
  8361. spin_unlock_irq(&dev->event_lock);
  8362. drm_crtc_vblank_put(crtc);
  8363. free_work:
  8364. kfree(work);
  8365. if (ret == -EIO) {
  8366. out_hang:
  8367. ret = intel_plane_restore(primary);
  8368. if (ret == 0 && event) {
  8369. spin_lock_irq(&dev->event_lock);
  8370. drm_send_vblank_event(dev, pipe, event);
  8371. spin_unlock_irq(&dev->event_lock);
  8372. }
  8373. }
  8374. return ret;
  8375. }
  8376. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  8377. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  8378. .load_lut = intel_crtc_load_lut,
  8379. .atomic_begin = intel_begin_crtc_commit,
  8380. .atomic_flush = intel_finish_crtc_commit,
  8381. };
  8382. /**
  8383. * intel_modeset_update_staged_output_state
  8384. *
  8385. * Updates the staged output configuration state, e.g. after we've read out the
  8386. * current hw state.
  8387. */
  8388. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  8389. {
  8390. struct intel_crtc *crtc;
  8391. struct intel_encoder *encoder;
  8392. struct intel_connector *connector;
  8393. for_each_intel_connector(dev, connector) {
  8394. connector->new_encoder =
  8395. to_intel_encoder(connector->base.encoder);
  8396. }
  8397. for_each_intel_encoder(dev, encoder) {
  8398. encoder->new_crtc =
  8399. to_intel_crtc(encoder->base.crtc);
  8400. }
  8401. for_each_intel_crtc(dev, crtc) {
  8402. crtc->new_enabled = crtc->base.state->enable;
  8403. if (crtc->new_enabled)
  8404. crtc->new_config = crtc->config;
  8405. else
  8406. crtc->new_config = NULL;
  8407. }
  8408. }
  8409. /**
  8410. * intel_modeset_commit_output_state
  8411. *
  8412. * This function copies the stage display pipe configuration to the real one.
  8413. */
  8414. static void intel_modeset_commit_output_state(struct drm_device *dev)
  8415. {
  8416. struct intel_crtc *crtc;
  8417. struct intel_encoder *encoder;
  8418. struct intel_connector *connector;
  8419. for_each_intel_connector(dev, connector) {
  8420. connector->base.encoder = &connector->new_encoder->base;
  8421. }
  8422. for_each_intel_encoder(dev, encoder) {
  8423. encoder->base.crtc = &encoder->new_crtc->base;
  8424. }
  8425. for_each_intel_crtc(dev, crtc) {
  8426. crtc->base.state->enable = crtc->new_enabled;
  8427. crtc->base.enabled = crtc->new_enabled;
  8428. }
  8429. }
  8430. static void
  8431. connected_sink_compute_bpp(struct intel_connector *connector,
  8432. struct intel_crtc_state *pipe_config)
  8433. {
  8434. int bpp = pipe_config->pipe_bpp;
  8435. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8436. connector->base.base.id,
  8437. connector->base.name);
  8438. /* Don't use an invalid EDID bpc value */
  8439. if (connector->base.display_info.bpc &&
  8440. connector->base.display_info.bpc * 3 < bpp) {
  8441. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8442. bpp, connector->base.display_info.bpc*3);
  8443. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  8444. }
  8445. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8446. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  8447. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8448. bpp);
  8449. pipe_config->pipe_bpp = 24;
  8450. }
  8451. }
  8452. static int
  8453. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8454. struct drm_framebuffer *fb,
  8455. struct intel_crtc_state *pipe_config)
  8456. {
  8457. struct drm_device *dev = crtc->base.dev;
  8458. struct intel_connector *connector;
  8459. int bpp;
  8460. switch (fb->pixel_format) {
  8461. case DRM_FORMAT_C8:
  8462. bpp = 8*3; /* since we go through a colormap */
  8463. break;
  8464. case DRM_FORMAT_XRGB1555:
  8465. case DRM_FORMAT_ARGB1555:
  8466. /* checked in intel_framebuffer_init already */
  8467. if (WARN_ON(INTEL_INFO(dev)->gen > 3))
  8468. return -EINVAL;
  8469. case DRM_FORMAT_RGB565:
  8470. bpp = 6*3; /* min is 18bpp */
  8471. break;
  8472. case DRM_FORMAT_XBGR8888:
  8473. case DRM_FORMAT_ABGR8888:
  8474. /* checked in intel_framebuffer_init already */
  8475. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8476. return -EINVAL;
  8477. case DRM_FORMAT_XRGB8888:
  8478. case DRM_FORMAT_ARGB8888:
  8479. bpp = 8*3;
  8480. break;
  8481. case DRM_FORMAT_XRGB2101010:
  8482. case DRM_FORMAT_ARGB2101010:
  8483. case DRM_FORMAT_XBGR2101010:
  8484. case DRM_FORMAT_ABGR2101010:
  8485. /* checked in intel_framebuffer_init already */
  8486. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8487. return -EINVAL;
  8488. bpp = 10*3;
  8489. break;
  8490. /* TODO: gen4+ supports 16 bpc floating point, too. */
  8491. default:
  8492. DRM_DEBUG_KMS("unsupported depth\n");
  8493. return -EINVAL;
  8494. }
  8495. pipe_config->pipe_bpp = bpp;
  8496. /* Clamp display bpp to EDID value */
  8497. for_each_intel_connector(dev, connector) {
  8498. if (!connector->new_encoder ||
  8499. connector->new_encoder->new_crtc != crtc)
  8500. continue;
  8501. connected_sink_compute_bpp(connector, pipe_config);
  8502. }
  8503. return bpp;
  8504. }
  8505. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  8506. {
  8507. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  8508. "type: 0x%x flags: 0x%x\n",
  8509. mode->crtc_clock,
  8510. mode->crtc_hdisplay, mode->crtc_hsync_start,
  8511. mode->crtc_hsync_end, mode->crtc_htotal,
  8512. mode->crtc_vdisplay, mode->crtc_vsync_start,
  8513. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  8514. }
  8515. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  8516. struct intel_crtc_state *pipe_config,
  8517. const char *context)
  8518. {
  8519. DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
  8520. context, pipe_name(crtc->pipe));
  8521. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  8522. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  8523. pipe_config->pipe_bpp, pipe_config->dither);
  8524. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8525. pipe_config->has_pch_encoder,
  8526. pipe_config->fdi_lanes,
  8527. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  8528. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  8529. pipe_config->fdi_m_n.tu);
  8530. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8531. pipe_config->has_dp_encoder,
  8532. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  8533. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  8534. pipe_config->dp_m_n.tu);
  8535. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  8536. pipe_config->has_dp_encoder,
  8537. pipe_config->dp_m2_n2.gmch_m,
  8538. pipe_config->dp_m2_n2.gmch_n,
  8539. pipe_config->dp_m2_n2.link_m,
  8540. pipe_config->dp_m2_n2.link_n,
  8541. pipe_config->dp_m2_n2.tu);
  8542. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  8543. pipe_config->has_audio,
  8544. pipe_config->has_infoframe);
  8545. DRM_DEBUG_KMS("requested mode:\n");
  8546. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  8547. DRM_DEBUG_KMS("adjusted mode:\n");
  8548. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  8549. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  8550. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  8551. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  8552. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  8553. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  8554. pipe_config->gmch_pfit.control,
  8555. pipe_config->gmch_pfit.pgm_ratios,
  8556. pipe_config->gmch_pfit.lvds_border_bits);
  8557. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  8558. pipe_config->pch_pfit.pos,
  8559. pipe_config->pch_pfit.size,
  8560. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  8561. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  8562. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  8563. }
  8564. static bool encoders_cloneable(const struct intel_encoder *a,
  8565. const struct intel_encoder *b)
  8566. {
  8567. /* masks could be asymmetric, so check both ways */
  8568. return a == b || (a->cloneable & (1 << b->type) &&
  8569. b->cloneable & (1 << a->type));
  8570. }
  8571. static bool check_single_encoder_cloning(struct intel_crtc *crtc,
  8572. struct intel_encoder *encoder)
  8573. {
  8574. struct drm_device *dev = crtc->base.dev;
  8575. struct intel_encoder *source_encoder;
  8576. for_each_intel_encoder(dev, source_encoder) {
  8577. if (source_encoder->new_crtc != crtc)
  8578. continue;
  8579. if (!encoders_cloneable(encoder, source_encoder))
  8580. return false;
  8581. }
  8582. return true;
  8583. }
  8584. static bool check_encoder_cloning(struct intel_crtc *crtc)
  8585. {
  8586. struct drm_device *dev = crtc->base.dev;
  8587. struct intel_encoder *encoder;
  8588. for_each_intel_encoder(dev, encoder) {
  8589. if (encoder->new_crtc != crtc)
  8590. continue;
  8591. if (!check_single_encoder_cloning(crtc, encoder))
  8592. return false;
  8593. }
  8594. return true;
  8595. }
  8596. static bool check_digital_port_conflicts(struct drm_device *dev)
  8597. {
  8598. struct intel_connector *connector;
  8599. unsigned int used_ports = 0;
  8600. /*
  8601. * Walk the connector list instead of the encoder
  8602. * list to detect the problem on ddi platforms
  8603. * where there's just one encoder per digital port.
  8604. */
  8605. for_each_intel_connector(dev, connector) {
  8606. struct intel_encoder *encoder = connector->new_encoder;
  8607. if (!encoder)
  8608. continue;
  8609. WARN_ON(!encoder->new_crtc);
  8610. switch (encoder->type) {
  8611. unsigned int port_mask;
  8612. case INTEL_OUTPUT_UNKNOWN:
  8613. if (WARN_ON(!HAS_DDI(dev)))
  8614. break;
  8615. case INTEL_OUTPUT_DISPLAYPORT:
  8616. case INTEL_OUTPUT_HDMI:
  8617. case INTEL_OUTPUT_EDP:
  8618. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  8619. /* the same port mustn't appear more than once */
  8620. if (used_ports & port_mask)
  8621. return false;
  8622. used_ports |= port_mask;
  8623. default:
  8624. break;
  8625. }
  8626. }
  8627. return true;
  8628. }
  8629. static struct intel_crtc_state *
  8630. intel_modeset_pipe_config(struct drm_crtc *crtc,
  8631. struct drm_framebuffer *fb,
  8632. struct drm_display_mode *mode)
  8633. {
  8634. struct drm_device *dev = crtc->dev;
  8635. struct intel_encoder *encoder;
  8636. struct intel_crtc_state *pipe_config;
  8637. int plane_bpp, ret = -EINVAL;
  8638. bool retry = true;
  8639. if (!check_encoder_cloning(to_intel_crtc(crtc))) {
  8640. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  8641. return ERR_PTR(-EINVAL);
  8642. }
  8643. if (!check_digital_port_conflicts(dev)) {
  8644. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  8645. return ERR_PTR(-EINVAL);
  8646. }
  8647. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8648. if (!pipe_config)
  8649. return ERR_PTR(-ENOMEM);
  8650. pipe_config->base.crtc = crtc;
  8651. drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
  8652. drm_mode_copy(&pipe_config->base.mode, mode);
  8653. pipe_config->cpu_transcoder =
  8654. (enum transcoder) to_intel_crtc(crtc)->pipe;
  8655. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8656. /*
  8657. * Sanitize sync polarity flags based on requested ones. If neither
  8658. * positive or negative polarity is requested, treat this as meaning
  8659. * negative polarity.
  8660. */
  8661. if (!(pipe_config->base.adjusted_mode.flags &
  8662. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  8663. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  8664. if (!(pipe_config->base.adjusted_mode.flags &
  8665. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  8666. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  8667. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  8668. * plane pixel format and any sink constraints into account. Returns the
  8669. * source plane bpp so that dithering can be selected on mismatches
  8670. * after encoders and crtc also have had their say. */
  8671. plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  8672. fb, pipe_config);
  8673. if (plane_bpp < 0)
  8674. goto fail;
  8675. /*
  8676. * Determine the real pipe dimensions. Note that stereo modes can
  8677. * increase the actual pipe size due to the frame doubling and
  8678. * insertion of additional space for blanks between the frame. This
  8679. * is stored in the crtc timings. We use the requested mode to do this
  8680. * computation to clearly distinguish it from the adjusted mode, which
  8681. * can be changed by the connectors in the below retry loop.
  8682. */
  8683. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  8684. &pipe_config->pipe_src_w,
  8685. &pipe_config->pipe_src_h);
  8686. encoder_retry:
  8687. /* Ensure the port clock defaults are reset when retrying. */
  8688. pipe_config->port_clock = 0;
  8689. pipe_config->pixel_multiplier = 1;
  8690. /* Fill in default crtc timings, allow encoders to overwrite them. */
  8691. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  8692. CRTC_STEREO_DOUBLE);
  8693. /* Pass our mode to the connectors and the CRTC to give them a chance to
  8694. * adjust it according to limitations or connector properties, and also
  8695. * a chance to reject the mode entirely.
  8696. */
  8697. for_each_intel_encoder(dev, encoder) {
  8698. if (&encoder->new_crtc->base != crtc)
  8699. continue;
  8700. if (!(encoder->compute_config(encoder, pipe_config))) {
  8701. DRM_DEBUG_KMS("Encoder config failure\n");
  8702. goto fail;
  8703. }
  8704. }
  8705. /* Set default port clock if not overwritten by the encoder. Needs to be
  8706. * done afterwards in case the encoder adjusts the mode. */
  8707. if (!pipe_config->port_clock)
  8708. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  8709. * pipe_config->pixel_multiplier;
  8710. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  8711. if (ret < 0) {
  8712. DRM_DEBUG_KMS("CRTC fixup failed\n");
  8713. goto fail;
  8714. }
  8715. if (ret == RETRY) {
  8716. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  8717. ret = -EINVAL;
  8718. goto fail;
  8719. }
  8720. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  8721. retry = false;
  8722. goto encoder_retry;
  8723. }
  8724. pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
  8725. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  8726. plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  8727. return pipe_config;
  8728. fail:
  8729. kfree(pipe_config);
  8730. return ERR_PTR(ret);
  8731. }
  8732. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  8733. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  8734. static void
  8735. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  8736. unsigned *prepare_pipes, unsigned *disable_pipes)
  8737. {
  8738. struct intel_crtc *intel_crtc;
  8739. struct drm_device *dev = crtc->dev;
  8740. struct intel_encoder *encoder;
  8741. struct intel_connector *connector;
  8742. struct drm_crtc *tmp_crtc;
  8743. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  8744. /* Check which crtcs have changed outputs connected to them, these need
  8745. * to be part of the prepare_pipes mask. We don't (yet) support global
  8746. * modeset across multiple crtcs, so modeset_pipes will only have one
  8747. * bit set at most. */
  8748. for_each_intel_connector(dev, connector) {
  8749. if (connector->base.encoder == &connector->new_encoder->base)
  8750. continue;
  8751. if (connector->base.encoder) {
  8752. tmp_crtc = connector->base.encoder->crtc;
  8753. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8754. }
  8755. if (connector->new_encoder)
  8756. *prepare_pipes |=
  8757. 1 << connector->new_encoder->new_crtc->pipe;
  8758. }
  8759. for_each_intel_encoder(dev, encoder) {
  8760. if (encoder->base.crtc == &encoder->new_crtc->base)
  8761. continue;
  8762. if (encoder->base.crtc) {
  8763. tmp_crtc = encoder->base.crtc;
  8764. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8765. }
  8766. if (encoder->new_crtc)
  8767. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  8768. }
  8769. /* Check for pipes that will be enabled/disabled ... */
  8770. for_each_intel_crtc(dev, intel_crtc) {
  8771. if (intel_crtc->base.state->enable == intel_crtc->new_enabled)
  8772. continue;
  8773. if (!intel_crtc->new_enabled)
  8774. *disable_pipes |= 1 << intel_crtc->pipe;
  8775. else
  8776. *prepare_pipes |= 1 << intel_crtc->pipe;
  8777. }
  8778. /* set_mode is also used to update properties on life display pipes. */
  8779. intel_crtc = to_intel_crtc(crtc);
  8780. if (intel_crtc->new_enabled)
  8781. *prepare_pipes |= 1 << intel_crtc->pipe;
  8782. /*
  8783. * For simplicity do a full modeset on any pipe where the output routing
  8784. * changed. We could be more clever, but that would require us to be
  8785. * more careful with calling the relevant encoder->mode_set functions.
  8786. */
  8787. if (*prepare_pipes)
  8788. *modeset_pipes = *prepare_pipes;
  8789. /* ... and mask these out. */
  8790. *modeset_pipes &= ~(*disable_pipes);
  8791. *prepare_pipes &= ~(*disable_pipes);
  8792. /*
  8793. * HACK: We don't (yet) fully support global modesets. intel_set_config
  8794. * obies this rule, but the modeset restore mode of
  8795. * intel_modeset_setup_hw_state does not.
  8796. */
  8797. *modeset_pipes &= 1 << intel_crtc->pipe;
  8798. *prepare_pipes &= 1 << intel_crtc->pipe;
  8799. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  8800. *modeset_pipes, *prepare_pipes, *disable_pipes);
  8801. }
  8802. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  8803. {
  8804. struct drm_encoder *encoder;
  8805. struct drm_device *dev = crtc->dev;
  8806. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  8807. if (encoder->crtc == crtc)
  8808. return true;
  8809. return false;
  8810. }
  8811. static void
  8812. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  8813. {
  8814. struct drm_i915_private *dev_priv = dev->dev_private;
  8815. struct intel_encoder *intel_encoder;
  8816. struct intel_crtc *intel_crtc;
  8817. struct drm_connector *connector;
  8818. intel_shared_dpll_commit(dev_priv);
  8819. for_each_intel_encoder(dev, intel_encoder) {
  8820. if (!intel_encoder->base.crtc)
  8821. continue;
  8822. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  8823. if (prepare_pipes & (1 << intel_crtc->pipe))
  8824. intel_encoder->connectors_active = false;
  8825. }
  8826. intel_modeset_commit_output_state(dev);
  8827. /* Double check state. */
  8828. for_each_intel_crtc(dev, intel_crtc) {
  8829. WARN_ON(intel_crtc->base.state->enable != intel_crtc_in_use(&intel_crtc->base));
  8830. WARN_ON(intel_crtc->new_config &&
  8831. intel_crtc->new_config != intel_crtc->config);
  8832. WARN_ON(intel_crtc->base.state->enable != !!intel_crtc->new_config);
  8833. }
  8834. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  8835. if (!connector->encoder || !connector->encoder->crtc)
  8836. continue;
  8837. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  8838. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  8839. struct drm_property *dpms_property =
  8840. dev->mode_config.dpms_property;
  8841. connector->dpms = DRM_MODE_DPMS_ON;
  8842. drm_object_property_set_value(&connector->base,
  8843. dpms_property,
  8844. DRM_MODE_DPMS_ON);
  8845. intel_encoder = to_intel_encoder(connector->encoder);
  8846. intel_encoder->connectors_active = true;
  8847. }
  8848. }
  8849. }
  8850. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  8851. {
  8852. int diff;
  8853. if (clock1 == clock2)
  8854. return true;
  8855. if (!clock1 || !clock2)
  8856. return false;
  8857. diff = abs(clock1 - clock2);
  8858. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  8859. return true;
  8860. return false;
  8861. }
  8862. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  8863. list_for_each_entry((intel_crtc), \
  8864. &(dev)->mode_config.crtc_list, \
  8865. base.head) \
  8866. if (mask & (1 <<(intel_crtc)->pipe))
  8867. static bool
  8868. intel_pipe_config_compare(struct drm_device *dev,
  8869. struct intel_crtc_state *current_config,
  8870. struct intel_crtc_state *pipe_config)
  8871. {
  8872. #define PIPE_CONF_CHECK_X(name) \
  8873. if (current_config->name != pipe_config->name) { \
  8874. DRM_ERROR("mismatch in " #name " " \
  8875. "(expected 0x%08x, found 0x%08x)\n", \
  8876. current_config->name, \
  8877. pipe_config->name); \
  8878. return false; \
  8879. }
  8880. #define PIPE_CONF_CHECK_I(name) \
  8881. if (current_config->name != pipe_config->name) { \
  8882. DRM_ERROR("mismatch in " #name " " \
  8883. "(expected %i, found %i)\n", \
  8884. current_config->name, \
  8885. pipe_config->name); \
  8886. return false; \
  8887. }
  8888. /* This is required for BDW+ where there is only one set of registers for
  8889. * switching between high and low RR.
  8890. * This macro can be used whenever a comparison has to be made between one
  8891. * hw state and multiple sw state variables.
  8892. */
  8893. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  8894. if ((current_config->name != pipe_config->name) && \
  8895. (current_config->alt_name != pipe_config->name)) { \
  8896. DRM_ERROR("mismatch in " #name " " \
  8897. "(expected %i or %i, found %i)\n", \
  8898. current_config->name, \
  8899. current_config->alt_name, \
  8900. pipe_config->name); \
  8901. return false; \
  8902. }
  8903. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  8904. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  8905. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  8906. "(expected %i, found %i)\n", \
  8907. current_config->name & (mask), \
  8908. pipe_config->name & (mask)); \
  8909. return false; \
  8910. }
  8911. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  8912. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  8913. DRM_ERROR("mismatch in " #name " " \
  8914. "(expected %i, found %i)\n", \
  8915. current_config->name, \
  8916. pipe_config->name); \
  8917. return false; \
  8918. }
  8919. #define PIPE_CONF_QUIRK(quirk) \
  8920. ((current_config->quirks | pipe_config->quirks) & (quirk))
  8921. PIPE_CONF_CHECK_I(cpu_transcoder);
  8922. PIPE_CONF_CHECK_I(has_pch_encoder);
  8923. PIPE_CONF_CHECK_I(fdi_lanes);
  8924. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  8925. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  8926. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  8927. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  8928. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  8929. PIPE_CONF_CHECK_I(has_dp_encoder);
  8930. if (INTEL_INFO(dev)->gen < 8) {
  8931. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  8932. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  8933. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  8934. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  8935. PIPE_CONF_CHECK_I(dp_m_n.tu);
  8936. if (current_config->has_drrs) {
  8937. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
  8938. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
  8939. PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
  8940. PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
  8941. PIPE_CONF_CHECK_I(dp_m2_n2.tu);
  8942. }
  8943. } else {
  8944. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
  8945. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
  8946. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
  8947. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
  8948. PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
  8949. }
  8950. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  8951. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  8952. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  8953. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  8954. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  8955. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  8956. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  8957. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  8958. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  8959. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  8960. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  8961. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  8962. PIPE_CONF_CHECK_I(pixel_multiplier);
  8963. PIPE_CONF_CHECK_I(has_hdmi_sink);
  8964. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  8965. IS_VALLEYVIEW(dev))
  8966. PIPE_CONF_CHECK_I(limited_color_range);
  8967. PIPE_CONF_CHECK_I(has_infoframe);
  8968. PIPE_CONF_CHECK_I(has_audio);
  8969. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  8970. DRM_MODE_FLAG_INTERLACE);
  8971. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  8972. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  8973. DRM_MODE_FLAG_PHSYNC);
  8974. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  8975. DRM_MODE_FLAG_NHSYNC);
  8976. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  8977. DRM_MODE_FLAG_PVSYNC);
  8978. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  8979. DRM_MODE_FLAG_NVSYNC);
  8980. }
  8981. PIPE_CONF_CHECK_I(pipe_src_w);
  8982. PIPE_CONF_CHECK_I(pipe_src_h);
  8983. /*
  8984. * FIXME: BIOS likes to set up a cloned config with lvds+external
  8985. * screen. Since we don't yet re-compute the pipe config when moving
  8986. * just the lvds port away to another pipe the sw tracking won't match.
  8987. *
  8988. * Proper atomic modesets with recomputed global state will fix this.
  8989. * Until then just don't check gmch state for inherited modes.
  8990. */
  8991. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  8992. PIPE_CONF_CHECK_I(gmch_pfit.control);
  8993. /* pfit ratios are autocomputed by the hw on gen4+ */
  8994. if (INTEL_INFO(dev)->gen < 4)
  8995. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  8996. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  8997. }
  8998. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  8999. if (current_config->pch_pfit.enabled) {
  9000. PIPE_CONF_CHECK_I(pch_pfit.pos);
  9001. PIPE_CONF_CHECK_I(pch_pfit.size);
  9002. }
  9003. /* BDW+ don't expose a synchronous way to read the state */
  9004. if (IS_HASWELL(dev))
  9005. PIPE_CONF_CHECK_I(ips_enabled);
  9006. PIPE_CONF_CHECK_I(double_wide);
  9007. PIPE_CONF_CHECK_X(ddi_pll_sel);
  9008. PIPE_CONF_CHECK_I(shared_dpll);
  9009. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  9010. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  9011. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  9012. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  9013. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  9014. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  9015. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  9016. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  9017. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  9018. PIPE_CONF_CHECK_I(pipe_bpp);
  9019. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  9020. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  9021. #undef PIPE_CONF_CHECK_X
  9022. #undef PIPE_CONF_CHECK_I
  9023. #undef PIPE_CONF_CHECK_I_ALT
  9024. #undef PIPE_CONF_CHECK_FLAGS
  9025. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  9026. #undef PIPE_CONF_QUIRK
  9027. return true;
  9028. }
  9029. static void check_wm_state(struct drm_device *dev)
  9030. {
  9031. struct drm_i915_private *dev_priv = dev->dev_private;
  9032. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  9033. struct intel_crtc *intel_crtc;
  9034. int plane;
  9035. if (INTEL_INFO(dev)->gen < 9)
  9036. return;
  9037. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  9038. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  9039. for_each_intel_crtc(dev, intel_crtc) {
  9040. struct skl_ddb_entry *hw_entry, *sw_entry;
  9041. const enum pipe pipe = intel_crtc->pipe;
  9042. if (!intel_crtc->active)
  9043. continue;
  9044. /* planes */
  9045. for_each_plane(dev_priv, pipe, plane) {
  9046. hw_entry = &hw_ddb.plane[pipe][plane];
  9047. sw_entry = &sw_ddb->plane[pipe][plane];
  9048. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  9049. continue;
  9050. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  9051. "(expected (%u,%u), found (%u,%u))\n",
  9052. pipe_name(pipe), plane + 1,
  9053. sw_entry->start, sw_entry->end,
  9054. hw_entry->start, hw_entry->end);
  9055. }
  9056. /* cursor */
  9057. hw_entry = &hw_ddb.cursor[pipe];
  9058. sw_entry = &sw_ddb->cursor[pipe];
  9059. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  9060. continue;
  9061. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  9062. "(expected (%u,%u), found (%u,%u))\n",
  9063. pipe_name(pipe),
  9064. sw_entry->start, sw_entry->end,
  9065. hw_entry->start, hw_entry->end);
  9066. }
  9067. }
  9068. static void
  9069. check_connector_state(struct drm_device *dev)
  9070. {
  9071. struct intel_connector *connector;
  9072. for_each_intel_connector(dev, connector) {
  9073. /* This also checks the encoder/connector hw state with the
  9074. * ->get_hw_state callbacks. */
  9075. intel_connector_check_state(connector);
  9076. I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
  9077. "connector's staged encoder doesn't match current encoder\n");
  9078. }
  9079. }
  9080. static void
  9081. check_encoder_state(struct drm_device *dev)
  9082. {
  9083. struct intel_encoder *encoder;
  9084. struct intel_connector *connector;
  9085. for_each_intel_encoder(dev, encoder) {
  9086. bool enabled = false;
  9087. bool active = false;
  9088. enum pipe pipe, tracked_pipe;
  9089. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  9090. encoder->base.base.id,
  9091. encoder->base.name);
  9092. I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
  9093. "encoder's stage crtc doesn't match current crtc\n");
  9094. I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
  9095. "encoder's active_connectors set, but no crtc\n");
  9096. for_each_intel_connector(dev, connector) {
  9097. if (connector->base.encoder != &encoder->base)
  9098. continue;
  9099. enabled = true;
  9100. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  9101. active = true;
  9102. }
  9103. /*
  9104. * for MST connectors if we unplug the connector is gone
  9105. * away but the encoder is still connected to a crtc
  9106. * until a modeset happens in response to the hotplug.
  9107. */
  9108. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  9109. continue;
  9110. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  9111. "encoder's enabled state mismatch "
  9112. "(expected %i, found %i)\n",
  9113. !!encoder->base.crtc, enabled);
  9114. I915_STATE_WARN(active && !encoder->base.crtc,
  9115. "active encoder with no crtc\n");
  9116. I915_STATE_WARN(encoder->connectors_active != active,
  9117. "encoder's computed active state doesn't match tracked active state "
  9118. "(expected %i, found %i)\n", active, encoder->connectors_active);
  9119. active = encoder->get_hw_state(encoder, &pipe);
  9120. I915_STATE_WARN(active != encoder->connectors_active,
  9121. "encoder's hw state doesn't match sw tracking "
  9122. "(expected %i, found %i)\n",
  9123. encoder->connectors_active, active);
  9124. if (!encoder->base.crtc)
  9125. continue;
  9126. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  9127. I915_STATE_WARN(active && pipe != tracked_pipe,
  9128. "active encoder's pipe doesn't match"
  9129. "(expected %i, found %i)\n",
  9130. tracked_pipe, pipe);
  9131. }
  9132. }
  9133. static void
  9134. check_crtc_state(struct drm_device *dev)
  9135. {
  9136. struct drm_i915_private *dev_priv = dev->dev_private;
  9137. struct intel_crtc *crtc;
  9138. struct intel_encoder *encoder;
  9139. struct intel_crtc_state pipe_config;
  9140. for_each_intel_crtc(dev, crtc) {
  9141. bool enabled = false;
  9142. bool active = false;
  9143. memset(&pipe_config, 0, sizeof(pipe_config));
  9144. DRM_DEBUG_KMS("[CRTC:%d]\n",
  9145. crtc->base.base.id);
  9146. I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
  9147. "active crtc, but not enabled in sw tracking\n");
  9148. for_each_intel_encoder(dev, encoder) {
  9149. if (encoder->base.crtc != &crtc->base)
  9150. continue;
  9151. enabled = true;
  9152. if (encoder->connectors_active)
  9153. active = true;
  9154. }
  9155. I915_STATE_WARN(active != crtc->active,
  9156. "crtc's computed active state doesn't match tracked active state "
  9157. "(expected %i, found %i)\n", active, crtc->active);
  9158. I915_STATE_WARN(enabled != crtc->base.state->enable,
  9159. "crtc's computed enabled state doesn't match tracked enabled state "
  9160. "(expected %i, found %i)\n", enabled,
  9161. crtc->base.state->enable);
  9162. active = dev_priv->display.get_pipe_config(crtc,
  9163. &pipe_config);
  9164. /* hw state is inconsistent with the pipe quirk */
  9165. if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  9166. (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  9167. active = crtc->active;
  9168. for_each_intel_encoder(dev, encoder) {
  9169. enum pipe pipe;
  9170. if (encoder->base.crtc != &crtc->base)
  9171. continue;
  9172. if (encoder->get_hw_state(encoder, &pipe))
  9173. encoder->get_config(encoder, &pipe_config);
  9174. }
  9175. I915_STATE_WARN(crtc->active != active,
  9176. "crtc active state doesn't match with hw state "
  9177. "(expected %i, found %i)\n", crtc->active, active);
  9178. if (active &&
  9179. !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
  9180. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  9181. intel_dump_pipe_config(crtc, &pipe_config,
  9182. "[hw state]");
  9183. intel_dump_pipe_config(crtc, crtc->config,
  9184. "[sw state]");
  9185. }
  9186. }
  9187. }
  9188. static void
  9189. check_shared_dpll_state(struct drm_device *dev)
  9190. {
  9191. struct drm_i915_private *dev_priv = dev->dev_private;
  9192. struct intel_crtc *crtc;
  9193. struct intel_dpll_hw_state dpll_hw_state;
  9194. int i;
  9195. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9196. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  9197. int enabled_crtcs = 0, active_crtcs = 0;
  9198. bool active;
  9199. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  9200. DRM_DEBUG_KMS("%s\n", pll->name);
  9201. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  9202. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  9203. "more active pll users than references: %i vs %i\n",
  9204. pll->active, hweight32(pll->config.crtc_mask));
  9205. I915_STATE_WARN(pll->active && !pll->on,
  9206. "pll in active use but not on in sw tracking\n");
  9207. I915_STATE_WARN(pll->on && !pll->active,
  9208. "pll in on but not on in use in sw tracking\n");
  9209. I915_STATE_WARN(pll->on != active,
  9210. "pll on state mismatch (expected %i, found %i)\n",
  9211. pll->on, active);
  9212. for_each_intel_crtc(dev, crtc) {
  9213. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  9214. enabled_crtcs++;
  9215. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  9216. active_crtcs++;
  9217. }
  9218. I915_STATE_WARN(pll->active != active_crtcs,
  9219. "pll active crtcs mismatch (expected %i, found %i)\n",
  9220. pll->active, active_crtcs);
  9221. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  9222. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  9223. hweight32(pll->config.crtc_mask), enabled_crtcs);
  9224. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  9225. sizeof(dpll_hw_state)),
  9226. "pll hw state mismatch\n");
  9227. }
  9228. }
  9229. void
  9230. intel_modeset_check_state(struct drm_device *dev)
  9231. {
  9232. check_wm_state(dev);
  9233. check_connector_state(dev);
  9234. check_encoder_state(dev);
  9235. check_crtc_state(dev);
  9236. check_shared_dpll_state(dev);
  9237. }
  9238. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  9239. int dotclock)
  9240. {
  9241. /*
  9242. * FDI already provided one idea for the dotclock.
  9243. * Yell if the encoder disagrees.
  9244. */
  9245. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  9246. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9247. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  9248. }
  9249. static void update_scanline_offset(struct intel_crtc *crtc)
  9250. {
  9251. struct drm_device *dev = crtc->base.dev;
  9252. /*
  9253. * The scanline counter increments at the leading edge of hsync.
  9254. *
  9255. * On most platforms it starts counting from vtotal-1 on the
  9256. * first active line. That means the scanline counter value is
  9257. * always one less than what we would expect. Ie. just after
  9258. * start of vblank, which also occurs at start of hsync (on the
  9259. * last active line), the scanline counter will read vblank_start-1.
  9260. *
  9261. * On gen2 the scanline counter starts counting from 1 instead
  9262. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  9263. * to keep the value positive), instead of adding one.
  9264. *
  9265. * On HSW+ the behaviour of the scanline counter depends on the output
  9266. * type. For DP ports it behaves like most other platforms, but on HDMI
  9267. * there's an extra 1 line difference. So we need to add two instead of
  9268. * one to the value.
  9269. */
  9270. if (IS_GEN2(dev)) {
  9271. const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
  9272. int vtotal;
  9273. vtotal = mode->crtc_vtotal;
  9274. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  9275. vtotal /= 2;
  9276. crtc->scanline_offset = vtotal - 1;
  9277. } else if (HAS_DDI(dev) &&
  9278. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  9279. crtc->scanline_offset = 2;
  9280. } else
  9281. crtc->scanline_offset = 1;
  9282. }
  9283. static struct intel_crtc_state *
  9284. intel_modeset_compute_config(struct drm_crtc *crtc,
  9285. struct drm_display_mode *mode,
  9286. struct drm_framebuffer *fb,
  9287. unsigned *modeset_pipes,
  9288. unsigned *prepare_pipes,
  9289. unsigned *disable_pipes)
  9290. {
  9291. struct intel_crtc_state *pipe_config = NULL;
  9292. intel_modeset_affected_pipes(crtc, modeset_pipes,
  9293. prepare_pipes, disable_pipes);
  9294. if ((*modeset_pipes) == 0)
  9295. goto out;
  9296. /*
  9297. * Note this needs changes when we start tracking multiple modes
  9298. * and crtcs. At that point we'll need to compute the whole config
  9299. * (i.e. one pipe_config for each crtc) rather than just the one
  9300. * for this crtc.
  9301. */
  9302. pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
  9303. if (IS_ERR(pipe_config)) {
  9304. goto out;
  9305. }
  9306. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  9307. "[modeset]");
  9308. out:
  9309. return pipe_config;
  9310. }
  9311. static int __intel_set_mode_setup_plls(struct drm_device *dev,
  9312. unsigned modeset_pipes,
  9313. unsigned disable_pipes)
  9314. {
  9315. struct drm_i915_private *dev_priv = to_i915(dev);
  9316. unsigned clear_pipes = modeset_pipes | disable_pipes;
  9317. struct intel_crtc *intel_crtc;
  9318. int ret = 0;
  9319. if (!dev_priv->display.crtc_compute_clock)
  9320. return 0;
  9321. ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
  9322. if (ret)
  9323. goto done;
  9324. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9325. struct intel_crtc_state *state = intel_crtc->new_config;
  9326. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  9327. state);
  9328. if (ret) {
  9329. intel_shared_dpll_abort_config(dev_priv);
  9330. goto done;
  9331. }
  9332. }
  9333. done:
  9334. return ret;
  9335. }
  9336. static int __intel_set_mode(struct drm_crtc *crtc,
  9337. struct drm_display_mode *mode,
  9338. int x, int y, struct drm_framebuffer *fb,
  9339. struct intel_crtc_state *pipe_config,
  9340. unsigned modeset_pipes,
  9341. unsigned prepare_pipes,
  9342. unsigned disable_pipes)
  9343. {
  9344. struct drm_device *dev = crtc->dev;
  9345. struct drm_i915_private *dev_priv = dev->dev_private;
  9346. struct drm_display_mode *saved_mode;
  9347. struct intel_crtc *intel_crtc;
  9348. int ret = 0;
  9349. saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
  9350. if (!saved_mode)
  9351. return -ENOMEM;
  9352. *saved_mode = crtc->mode;
  9353. if (modeset_pipes)
  9354. to_intel_crtc(crtc)->new_config = pipe_config;
  9355. /*
  9356. * See if the config requires any additional preparation, e.g.
  9357. * to adjust global state with pipes off. We need to do this
  9358. * here so we can get the modeset_pipe updated config for the new
  9359. * mode set on this crtc. For other crtcs we need to use the
  9360. * adjusted_mode bits in the crtc directly.
  9361. */
  9362. if (IS_VALLEYVIEW(dev)) {
  9363. valleyview_modeset_global_pipes(dev, &prepare_pipes);
  9364. /* may have added more to prepare_pipes than we should */
  9365. prepare_pipes &= ~disable_pipes;
  9366. }
  9367. ret = __intel_set_mode_setup_plls(dev, modeset_pipes, disable_pipes);
  9368. if (ret)
  9369. goto done;
  9370. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  9371. intel_crtc_disable(&intel_crtc->base);
  9372. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9373. if (intel_crtc->base.state->enable)
  9374. dev_priv->display.crtc_disable(&intel_crtc->base);
  9375. }
  9376. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  9377. * to set it here already despite that we pass it down the callchain.
  9378. *
  9379. * Note we'll need to fix this up when we start tracking multiple
  9380. * pipes; here we assume a single modeset_pipe and only track the
  9381. * single crtc and mode.
  9382. */
  9383. if (modeset_pipes) {
  9384. crtc->mode = *mode;
  9385. /* mode_set/enable/disable functions rely on a correct pipe
  9386. * config. */
  9387. intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
  9388. /*
  9389. * Calculate and store various constants which
  9390. * are later needed by vblank and swap-completion
  9391. * timestamping. They are derived from true hwmode.
  9392. */
  9393. drm_calc_timestamping_constants(crtc,
  9394. &pipe_config->base.adjusted_mode);
  9395. }
  9396. /* Only after disabling all output pipelines that will be changed can we
  9397. * update the the output configuration. */
  9398. intel_modeset_update_state(dev, prepare_pipes);
  9399. modeset_update_crtc_power_domains(dev);
  9400. /* Set up the DPLL and any encoders state that needs to adjust or depend
  9401. * on the DPLL.
  9402. */
  9403. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9404. struct drm_plane *primary = intel_crtc->base.primary;
  9405. int vdisplay, hdisplay;
  9406. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  9407. ret = primary->funcs->update_plane(primary, &intel_crtc->base,
  9408. fb, 0, 0,
  9409. hdisplay, vdisplay,
  9410. x << 16, y << 16,
  9411. hdisplay << 16, vdisplay << 16);
  9412. }
  9413. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  9414. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9415. update_scanline_offset(intel_crtc);
  9416. dev_priv->display.crtc_enable(&intel_crtc->base);
  9417. }
  9418. /* FIXME: add subpixel order */
  9419. done:
  9420. if (ret && crtc->state->enable)
  9421. crtc->mode = *saved_mode;
  9422. kfree(saved_mode);
  9423. return ret;
  9424. }
  9425. static int intel_set_mode_pipes(struct drm_crtc *crtc,
  9426. struct drm_display_mode *mode,
  9427. int x, int y, struct drm_framebuffer *fb,
  9428. struct intel_crtc_state *pipe_config,
  9429. unsigned modeset_pipes,
  9430. unsigned prepare_pipes,
  9431. unsigned disable_pipes)
  9432. {
  9433. int ret;
  9434. ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
  9435. prepare_pipes, disable_pipes);
  9436. if (ret == 0)
  9437. intel_modeset_check_state(crtc->dev);
  9438. return ret;
  9439. }
  9440. static int intel_set_mode(struct drm_crtc *crtc,
  9441. struct drm_display_mode *mode,
  9442. int x, int y, struct drm_framebuffer *fb)
  9443. {
  9444. struct intel_crtc_state *pipe_config;
  9445. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  9446. pipe_config = intel_modeset_compute_config(crtc, mode, fb,
  9447. &modeset_pipes,
  9448. &prepare_pipes,
  9449. &disable_pipes);
  9450. if (IS_ERR(pipe_config))
  9451. return PTR_ERR(pipe_config);
  9452. return intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
  9453. modeset_pipes, prepare_pipes,
  9454. disable_pipes);
  9455. }
  9456. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  9457. {
  9458. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
  9459. }
  9460. #undef for_each_intel_crtc_masked
  9461. static void intel_set_config_free(struct intel_set_config *config)
  9462. {
  9463. if (!config)
  9464. return;
  9465. kfree(config->save_connector_encoders);
  9466. kfree(config->save_encoder_crtcs);
  9467. kfree(config->save_crtc_enabled);
  9468. kfree(config);
  9469. }
  9470. static int intel_set_config_save_state(struct drm_device *dev,
  9471. struct intel_set_config *config)
  9472. {
  9473. struct drm_crtc *crtc;
  9474. struct drm_encoder *encoder;
  9475. struct drm_connector *connector;
  9476. int count;
  9477. config->save_crtc_enabled =
  9478. kcalloc(dev->mode_config.num_crtc,
  9479. sizeof(bool), GFP_KERNEL);
  9480. if (!config->save_crtc_enabled)
  9481. return -ENOMEM;
  9482. config->save_encoder_crtcs =
  9483. kcalloc(dev->mode_config.num_encoder,
  9484. sizeof(struct drm_crtc *), GFP_KERNEL);
  9485. if (!config->save_encoder_crtcs)
  9486. return -ENOMEM;
  9487. config->save_connector_encoders =
  9488. kcalloc(dev->mode_config.num_connector,
  9489. sizeof(struct drm_encoder *), GFP_KERNEL);
  9490. if (!config->save_connector_encoders)
  9491. return -ENOMEM;
  9492. /* Copy data. Note that driver private data is not affected.
  9493. * Should anything bad happen only the expected state is
  9494. * restored, not the drivers personal bookkeeping.
  9495. */
  9496. count = 0;
  9497. for_each_crtc(dev, crtc) {
  9498. config->save_crtc_enabled[count++] = crtc->state->enable;
  9499. }
  9500. count = 0;
  9501. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  9502. config->save_encoder_crtcs[count++] = encoder->crtc;
  9503. }
  9504. count = 0;
  9505. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  9506. config->save_connector_encoders[count++] = connector->encoder;
  9507. }
  9508. return 0;
  9509. }
  9510. static void intel_set_config_restore_state(struct drm_device *dev,
  9511. struct intel_set_config *config)
  9512. {
  9513. struct intel_crtc *crtc;
  9514. struct intel_encoder *encoder;
  9515. struct intel_connector *connector;
  9516. int count;
  9517. count = 0;
  9518. for_each_intel_crtc(dev, crtc) {
  9519. crtc->new_enabled = config->save_crtc_enabled[count++];
  9520. if (crtc->new_enabled)
  9521. crtc->new_config = crtc->config;
  9522. else
  9523. crtc->new_config = NULL;
  9524. }
  9525. count = 0;
  9526. for_each_intel_encoder(dev, encoder) {
  9527. encoder->new_crtc =
  9528. to_intel_crtc(config->save_encoder_crtcs[count++]);
  9529. }
  9530. count = 0;
  9531. for_each_intel_connector(dev, connector) {
  9532. connector->new_encoder =
  9533. to_intel_encoder(config->save_connector_encoders[count++]);
  9534. }
  9535. }
  9536. static bool
  9537. is_crtc_connector_off(struct drm_mode_set *set)
  9538. {
  9539. int i;
  9540. if (set->num_connectors == 0)
  9541. return false;
  9542. if (WARN_ON(set->connectors == NULL))
  9543. return false;
  9544. for (i = 0; i < set->num_connectors; i++)
  9545. if (set->connectors[i]->encoder &&
  9546. set->connectors[i]->encoder->crtc == set->crtc &&
  9547. set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
  9548. return true;
  9549. return false;
  9550. }
  9551. static void
  9552. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  9553. struct intel_set_config *config)
  9554. {
  9555. /* We should be able to check here if the fb has the same properties
  9556. * and then just flip_or_move it */
  9557. if (is_crtc_connector_off(set)) {
  9558. config->mode_changed = true;
  9559. } else if (set->crtc->primary->fb != set->fb) {
  9560. /*
  9561. * If we have no fb, we can only flip as long as the crtc is
  9562. * active, otherwise we need a full mode set. The crtc may
  9563. * be active if we've only disabled the primary plane, or
  9564. * in fastboot situations.
  9565. */
  9566. if (set->crtc->primary->fb == NULL) {
  9567. struct intel_crtc *intel_crtc =
  9568. to_intel_crtc(set->crtc);
  9569. if (intel_crtc->active) {
  9570. DRM_DEBUG_KMS("crtc has no fb, will flip\n");
  9571. config->fb_changed = true;
  9572. } else {
  9573. DRM_DEBUG_KMS("inactive crtc, full mode set\n");
  9574. config->mode_changed = true;
  9575. }
  9576. } else if (set->fb == NULL) {
  9577. config->mode_changed = true;
  9578. } else if (set->fb->pixel_format !=
  9579. set->crtc->primary->fb->pixel_format) {
  9580. config->mode_changed = true;
  9581. } else {
  9582. config->fb_changed = true;
  9583. }
  9584. }
  9585. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  9586. config->fb_changed = true;
  9587. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  9588. DRM_DEBUG_KMS("modes are different, full mode set\n");
  9589. drm_mode_debug_printmodeline(&set->crtc->mode);
  9590. drm_mode_debug_printmodeline(set->mode);
  9591. config->mode_changed = true;
  9592. }
  9593. DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
  9594. set->crtc->base.id, config->mode_changed, config->fb_changed);
  9595. }
  9596. static int
  9597. intel_modeset_stage_output_state(struct drm_device *dev,
  9598. struct drm_mode_set *set,
  9599. struct intel_set_config *config)
  9600. {
  9601. struct intel_connector *connector;
  9602. struct intel_encoder *encoder;
  9603. struct intel_crtc *crtc;
  9604. int ro;
  9605. /* The upper layers ensure that we either disable a crtc or have a list
  9606. * of connectors. For paranoia, double-check this. */
  9607. WARN_ON(!set->fb && (set->num_connectors != 0));
  9608. WARN_ON(set->fb && (set->num_connectors == 0));
  9609. for_each_intel_connector(dev, connector) {
  9610. /* Otherwise traverse passed in connector list and get encoders
  9611. * for them. */
  9612. for (ro = 0; ro < set->num_connectors; ro++) {
  9613. if (set->connectors[ro] == &connector->base) {
  9614. connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
  9615. break;
  9616. }
  9617. }
  9618. /* If we disable the crtc, disable all its connectors. Also, if
  9619. * the connector is on the changing crtc but not on the new
  9620. * connector list, disable it. */
  9621. if ((!set->fb || ro == set->num_connectors) &&
  9622. connector->base.encoder &&
  9623. connector->base.encoder->crtc == set->crtc) {
  9624. connector->new_encoder = NULL;
  9625. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  9626. connector->base.base.id,
  9627. connector->base.name);
  9628. }
  9629. if (&connector->new_encoder->base != connector->base.encoder) {
  9630. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] encoder changed, full mode switch\n",
  9631. connector->base.base.id,
  9632. connector->base.name);
  9633. config->mode_changed = true;
  9634. }
  9635. }
  9636. /* connector->new_encoder is now updated for all connectors. */
  9637. /* Update crtc of enabled connectors. */
  9638. for_each_intel_connector(dev, connector) {
  9639. struct drm_crtc *new_crtc;
  9640. if (!connector->new_encoder)
  9641. continue;
  9642. new_crtc = connector->new_encoder->base.crtc;
  9643. for (ro = 0; ro < set->num_connectors; ro++) {
  9644. if (set->connectors[ro] == &connector->base)
  9645. new_crtc = set->crtc;
  9646. }
  9647. /* Make sure the new CRTC will work with the encoder */
  9648. if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
  9649. new_crtc)) {
  9650. return -EINVAL;
  9651. }
  9652. connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
  9653. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  9654. connector->base.base.id,
  9655. connector->base.name,
  9656. new_crtc->base.id);
  9657. }
  9658. /* Check for any encoders that needs to be disabled. */
  9659. for_each_intel_encoder(dev, encoder) {
  9660. int num_connectors = 0;
  9661. for_each_intel_connector(dev, connector) {
  9662. if (connector->new_encoder == encoder) {
  9663. WARN_ON(!connector->new_encoder->new_crtc);
  9664. num_connectors++;
  9665. }
  9666. }
  9667. if (num_connectors == 0)
  9668. encoder->new_crtc = NULL;
  9669. else if (num_connectors > 1)
  9670. return -EINVAL;
  9671. /* Only now check for crtc changes so we don't miss encoders
  9672. * that will be disabled. */
  9673. if (&encoder->new_crtc->base != encoder->base.crtc) {
  9674. DRM_DEBUG_KMS("[ENCODER:%d:%s] crtc changed, full mode switch\n",
  9675. encoder->base.base.id,
  9676. encoder->base.name);
  9677. config->mode_changed = true;
  9678. }
  9679. }
  9680. /* Now we've also updated encoder->new_crtc for all encoders. */
  9681. for_each_intel_connector(dev, connector) {
  9682. if (connector->new_encoder)
  9683. if (connector->new_encoder != connector->encoder)
  9684. connector->encoder = connector->new_encoder;
  9685. }
  9686. for_each_intel_crtc(dev, crtc) {
  9687. crtc->new_enabled = false;
  9688. for_each_intel_encoder(dev, encoder) {
  9689. if (encoder->new_crtc == crtc) {
  9690. crtc->new_enabled = true;
  9691. break;
  9692. }
  9693. }
  9694. if (crtc->new_enabled != crtc->base.state->enable) {
  9695. DRM_DEBUG_KMS("[CRTC:%d] %sabled, full mode switch\n",
  9696. crtc->base.base.id,
  9697. crtc->new_enabled ? "en" : "dis");
  9698. config->mode_changed = true;
  9699. }
  9700. if (crtc->new_enabled)
  9701. crtc->new_config = crtc->config;
  9702. else
  9703. crtc->new_config = NULL;
  9704. }
  9705. return 0;
  9706. }
  9707. static void disable_crtc_nofb(struct intel_crtc *crtc)
  9708. {
  9709. struct drm_device *dev = crtc->base.dev;
  9710. struct intel_encoder *encoder;
  9711. struct intel_connector *connector;
  9712. DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
  9713. pipe_name(crtc->pipe));
  9714. for_each_intel_connector(dev, connector) {
  9715. if (connector->new_encoder &&
  9716. connector->new_encoder->new_crtc == crtc)
  9717. connector->new_encoder = NULL;
  9718. }
  9719. for_each_intel_encoder(dev, encoder) {
  9720. if (encoder->new_crtc == crtc)
  9721. encoder->new_crtc = NULL;
  9722. }
  9723. crtc->new_enabled = false;
  9724. crtc->new_config = NULL;
  9725. }
  9726. static int intel_crtc_set_config(struct drm_mode_set *set)
  9727. {
  9728. struct drm_device *dev;
  9729. struct drm_mode_set save_set;
  9730. struct intel_set_config *config;
  9731. struct intel_crtc_state *pipe_config;
  9732. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  9733. int ret;
  9734. BUG_ON(!set);
  9735. BUG_ON(!set->crtc);
  9736. BUG_ON(!set->crtc->helper_private);
  9737. /* Enforce sane interface api - has been abused by the fb helper. */
  9738. BUG_ON(!set->mode && set->fb);
  9739. BUG_ON(set->fb && set->num_connectors == 0);
  9740. if (set->fb) {
  9741. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  9742. set->crtc->base.id, set->fb->base.id,
  9743. (int)set->num_connectors, set->x, set->y);
  9744. } else {
  9745. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  9746. }
  9747. dev = set->crtc->dev;
  9748. ret = -ENOMEM;
  9749. config = kzalloc(sizeof(*config), GFP_KERNEL);
  9750. if (!config)
  9751. goto out_config;
  9752. ret = intel_set_config_save_state(dev, config);
  9753. if (ret)
  9754. goto out_config;
  9755. save_set.crtc = set->crtc;
  9756. save_set.mode = &set->crtc->mode;
  9757. save_set.x = set->crtc->x;
  9758. save_set.y = set->crtc->y;
  9759. save_set.fb = set->crtc->primary->fb;
  9760. /* Compute whether we need a full modeset, only an fb base update or no
  9761. * change at all. In the future we might also check whether only the
  9762. * mode changed, e.g. for LVDS where we only change the panel fitter in
  9763. * such cases. */
  9764. intel_set_config_compute_mode_changes(set, config);
  9765. ret = intel_modeset_stage_output_state(dev, set, config);
  9766. if (ret)
  9767. goto fail;
  9768. pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
  9769. set->fb,
  9770. &modeset_pipes,
  9771. &prepare_pipes,
  9772. &disable_pipes);
  9773. if (IS_ERR(pipe_config)) {
  9774. ret = PTR_ERR(pipe_config);
  9775. goto fail;
  9776. } else if (pipe_config) {
  9777. if (pipe_config->has_audio !=
  9778. to_intel_crtc(set->crtc)->config->has_audio)
  9779. config->mode_changed = true;
  9780. /*
  9781. * Note we have an issue here with infoframes: current code
  9782. * only updates them on the full mode set path per hw
  9783. * requirements. So here we should be checking for any
  9784. * required changes and forcing a mode set.
  9785. */
  9786. }
  9787. /* set_mode will free it in the mode_changed case */
  9788. if (!config->mode_changed)
  9789. kfree(pipe_config);
  9790. intel_update_pipe_size(to_intel_crtc(set->crtc));
  9791. if (config->mode_changed) {
  9792. ret = intel_set_mode_pipes(set->crtc, set->mode,
  9793. set->x, set->y, set->fb, pipe_config,
  9794. modeset_pipes, prepare_pipes,
  9795. disable_pipes);
  9796. } else if (config->fb_changed) {
  9797. struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
  9798. struct drm_plane *primary = set->crtc->primary;
  9799. int vdisplay, hdisplay;
  9800. drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
  9801. ret = primary->funcs->update_plane(primary, set->crtc, set->fb,
  9802. 0, 0, hdisplay, vdisplay,
  9803. set->x << 16, set->y << 16,
  9804. hdisplay << 16, vdisplay << 16);
  9805. /*
  9806. * We need to make sure the primary plane is re-enabled if it
  9807. * has previously been turned off.
  9808. */
  9809. if (!intel_crtc->primary_enabled && ret == 0) {
  9810. WARN_ON(!intel_crtc->active);
  9811. intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
  9812. }
  9813. /*
  9814. * In the fastboot case this may be our only check of the
  9815. * state after boot. It would be better to only do it on
  9816. * the first update, but we don't have a nice way of doing that
  9817. * (and really, set_config isn't used much for high freq page
  9818. * flipping, so increasing its cost here shouldn't be a big
  9819. * deal).
  9820. */
  9821. if (i915.fastboot && ret == 0)
  9822. intel_modeset_check_state(set->crtc->dev);
  9823. }
  9824. if (ret) {
  9825. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  9826. set->crtc->base.id, ret);
  9827. fail:
  9828. intel_set_config_restore_state(dev, config);
  9829. /*
  9830. * HACK: if the pipe was on, but we didn't have a framebuffer,
  9831. * force the pipe off to avoid oopsing in the modeset code
  9832. * due to fb==NULL. This should only happen during boot since
  9833. * we don't yet reconstruct the FB from the hardware state.
  9834. */
  9835. if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
  9836. disable_crtc_nofb(to_intel_crtc(save_set.crtc));
  9837. /* Try to restore the config */
  9838. if (config->mode_changed &&
  9839. intel_set_mode(save_set.crtc, save_set.mode,
  9840. save_set.x, save_set.y, save_set.fb))
  9841. DRM_ERROR("failed to restore config after modeset failure\n");
  9842. }
  9843. out_config:
  9844. intel_set_config_free(config);
  9845. return ret;
  9846. }
  9847. static const struct drm_crtc_funcs intel_crtc_funcs = {
  9848. .gamma_set = intel_crtc_gamma_set,
  9849. .set_config = intel_crtc_set_config,
  9850. .destroy = intel_crtc_destroy,
  9851. .page_flip = intel_crtc_page_flip,
  9852. .atomic_duplicate_state = intel_crtc_duplicate_state,
  9853. .atomic_destroy_state = intel_crtc_destroy_state,
  9854. };
  9855. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  9856. struct intel_shared_dpll *pll,
  9857. struct intel_dpll_hw_state *hw_state)
  9858. {
  9859. uint32_t val;
  9860. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  9861. return false;
  9862. val = I915_READ(PCH_DPLL(pll->id));
  9863. hw_state->dpll = val;
  9864. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  9865. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  9866. return val & DPLL_VCO_ENABLE;
  9867. }
  9868. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  9869. struct intel_shared_dpll *pll)
  9870. {
  9871. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  9872. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  9873. }
  9874. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  9875. struct intel_shared_dpll *pll)
  9876. {
  9877. /* PCH refclock must be enabled first */
  9878. ibx_assert_pch_refclk_enabled(dev_priv);
  9879. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  9880. /* Wait for the clocks to stabilize. */
  9881. POSTING_READ(PCH_DPLL(pll->id));
  9882. udelay(150);
  9883. /* The pixel multiplier can only be updated once the
  9884. * DPLL is enabled and the clocks are stable.
  9885. *
  9886. * So write it again.
  9887. */
  9888. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  9889. POSTING_READ(PCH_DPLL(pll->id));
  9890. udelay(200);
  9891. }
  9892. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  9893. struct intel_shared_dpll *pll)
  9894. {
  9895. struct drm_device *dev = dev_priv->dev;
  9896. struct intel_crtc *crtc;
  9897. /* Make sure no transcoder isn't still depending on us. */
  9898. for_each_intel_crtc(dev, crtc) {
  9899. if (intel_crtc_to_shared_dpll(crtc) == pll)
  9900. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  9901. }
  9902. I915_WRITE(PCH_DPLL(pll->id), 0);
  9903. POSTING_READ(PCH_DPLL(pll->id));
  9904. udelay(200);
  9905. }
  9906. static char *ibx_pch_dpll_names[] = {
  9907. "PCH DPLL A",
  9908. "PCH DPLL B",
  9909. };
  9910. static void ibx_pch_dpll_init(struct drm_device *dev)
  9911. {
  9912. struct drm_i915_private *dev_priv = dev->dev_private;
  9913. int i;
  9914. dev_priv->num_shared_dpll = 2;
  9915. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9916. dev_priv->shared_dplls[i].id = i;
  9917. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  9918. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  9919. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  9920. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  9921. dev_priv->shared_dplls[i].get_hw_state =
  9922. ibx_pch_dpll_get_hw_state;
  9923. }
  9924. }
  9925. static void intel_shared_dpll_init(struct drm_device *dev)
  9926. {
  9927. struct drm_i915_private *dev_priv = dev->dev_private;
  9928. if (HAS_DDI(dev))
  9929. intel_ddi_pll_init(dev);
  9930. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  9931. ibx_pch_dpll_init(dev);
  9932. else
  9933. dev_priv->num_shared_dpll = 0;
  9934. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  9935. }
  9936. /**
  9937. * intel_prepare_plane_fb - Prepare fb for usage on plane
  9938. * @plane: drm plane to prepare for
  9939. * @fb: framebuffer to prepare for presentation
  9940. *
  9941. * Prepares a framebuffer for usage on a display plane. Generally this
  9942. * involves pinning the underlying object and updating the frontbuffer tracking
  9943. * bits. Some older platforms need special physical address handling for
  9944. * cursor planes.
  9945. *
  9946. * Returns 0 on success, negative error code on failure.
  9947. */
  9948. int
  9949. intel_prepare_plane_fb(struct drm_plane *plane,
  9950. struct drm_framebuffer *fb,
  9951. const struct drm_plane_state *new_state)
  9952. {
  9953. struct drm_device *dev = plane->dev;
  9954. struct intel_plane *intel_plane = to_intel_plane(plane);
  9955. enum pipe pipe = intel_plane->pipe;
  9956. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9957. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  9958. unsigned frontbuffer_bits = 0;
  9959. int ret = 0;
  9960. if (!obj)
  9961. return 0;
  9962. switch (plane->type) {
  9963. case DRM_PLANE_TYPE_PRIMARY:
  9964. frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
  9965. break;
  9966. case DRM_PLANE_TYPE_CURSOR:
  9967. frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
  9968. break;
  9969. case DRM_PLANE_TYPE_OVERLAY:
  9970. frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
  9971. break;
  9972. }
  9973. mutex_lock(&dev->struct_mutex);
  9974. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  9975. INTEL_INFO(dev)->cursor_needs_physical) {
  9976. int align = IS_I830(dev) ? 16 * 1024 : 256;
  9977. ret = i915_gem_object_attach_phys(obj, align);
  9978. if (ret)
  9979. DRM_DEBUG_KMS("failed to attach phys object\n");
  9980. } else {
  9981. ret = intel_pin_and_fence_fb_obj(plane, fb, NULL);
  9982. }
  9983. if (ret == 0)
  9984. i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
  9985. mutex_unlock(&dev->struct_mutex);
  9986. return ret;
  9987. }
  9988. /**
  9989. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  9990. * @plane: drm plane to clean up for
  9991. * @fb: old framebuffer that was on plane
  9992. *
  9993. * Cleans up a framebuffer that has just been removed from a plane.
  9994. */
  9995. void
  9996. intel_cleanup_plane_fb(struct drm_plane *plane,
  9997. struct drm_framebuffer *fb,
  9998. const struct drm_plane_state *old_state)
  9999. {
  10000. struct drm_device *dev = plane->dev;
  10001. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10002. if (WARN_ON(!obj))
  10003. return;
  10004. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  10005. !INTEL_INFO(dev)->cursor_needs_physical) {
  10006. mutex_lock(&dev->struct_mutex);
  10007. intel_unpin_fb_obj(obj);
  10008. mutex_unlock(&dev->struct_mutex);
  10009. }
  10010. }
  10011. static int
  10012. intel_check_primary_plane(struct drm_plane *plane,
  10013. struct intel_plane_state *state)
  10014. {
  10015. struct drm_device *dev = plane->dev;
  10016. struct drm_i915_private *dev_priv = dev->dev_private;
  10017. struct drm_crtc *crtc = state->base.crtc;
  10018. struct intel_crtc *intel_crtc;
  10019. struct drm_framebuffer *fb = state->base.fb;
  10020. struct drm_rect *dest = &state->dst;
  10021. struct drm_rect *src = &state->src;
  10022. const struct drm_rect *clip = &state->clip;
  10023. int ret;
  10024. crtc = crtc ? crtc : plane->crtc;
  10025. intel_crtc = to_intel_crtc(crtc);
  10026. ret = drm_plane_helper_check_update(plane, crtc, fb,
  10027. src, dest, clip,
  10028. DRM_PLANE_HELPER_NO_SCALING,
  10029. DRM_PLANE_HELPER_NO_SCALING,
  10030. false, true, &state->visible);
  10031. if (ret)
  10032. return ret;
  10033. if (intel_crtc->active) {
  10034. intel_crtc->atomic.wait_for_flips = true;
  10035. /*
  10036. * FBC does not work on some platforms for rotated
  10037. * planes, so disable it when rotation is not 0 and
  10038. * update it when rotation is set back to 0.
  10039. *
  10040. * FIXME: This is redundant with the fbc update done in
  10041. * the primary plane enable function except that that
  10042. * one is done too late. We eventually need to unify
  10043. * this.
  10044. */
  10045. if (intel_crtc->primary_enabled &&
  10046. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  10047. dev_priv->fbc.crtc == intel_crtc &&
  10048. state->base.rotation != BIT(DRM_ROTATE_0)) {
  10049. intel_crtc->atomic.disable_fbc = true;
  10050. }
  10051. if (state->visible) {
  10052. /*
  10053. * BDW signals flip done immediately if the plane
  10054. * is disabled, even if the plane enable is already
  10055. * armed to occur at the next vblank :(
  10056. */
  10057. if (IS_BROADWELL(dev) && !intel_crtc->primary_enabled)
  10058. intel_crtc->atomic.wait_vblank = true;
  10059. }
  10060. intel_crtc->atomic.fb_bits |=
  10061. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  10062. intel_crtc->atomic.update_fbc = true;
  10063. /* Update watermarks on tiling changes. */
  10064. if (!plane->state->fb || !state->base.fb ||
  10065. plane->state->fb->modifier[0] !=
  10066. state->base.fb->modifier[0])
  10067. intel_crtc->atomic.update_wm = true;
  10068. }
  10069. return 0;
  10070. }
  10071. static void
  10072. intel_commit_primary_plane(struct drm_plane *plane,
  10073. struct intel_plane_state *state)
  10074. {
  10075. struct drm_crtc *crtc = state->base.crtc;
  10076. struct drm_framebuffer *fb = state->base.fb;
  10077. struct drm_device *dev = plane->dev;
  10078. struct drm_i915_private *dev_priv = dev->dev_private;
  10079. struct intel_crtc *intel_crtc;
  10080. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10081. struct intel_plane *intel_plane = to_intel_plane(plane);
  10082. struct drm_rect *src = &state->src;
  10083. crtc = crtc ? crtc : plane->crtc;
  10084. intel_crtc = to_intel_crtc(crtc);
  10085. plane->fb = fb;
  10086. crtc->x = src->x1 >> 16;
  10087. crtc->y = src->y1 >> 16;
  10088. intel_plane->obj = obj;
  10089. if (intel_crtc->active) {
  10090. if (state->visible) {
  10091. /* FIXME: kill this fastboot hack */
  10092. intel_update_pipe_size(intel_crtc);
  10093. intel_crtc->primary_enabled = true;
  10094. dev_priv->display.update_primary_plane(crtc, plane->fb,
  10095. crtc->x, crtc->y);
  10096. } else {
  10097. /*
  10098. * If clipping results in a non-visible primary plane,
  10099. * we'll disable the primary plane. Note that this is
  10100. * a bit different than what happens if userspace
  10101. * explicitly disables the plane by passing fb=0
  10102. * because plane->fb still gets set and pinned.
  10103. */
  10104. intel_disable_primary_hw_plane(plane, crtc);
  10105. }
  10106. }
  10107. }
  10108. static void intel_begin_crtc_commit(struct drm_crtc *crtc)
  10109. {
  10110. struct drm_device *dev = crtc->dev;
  10111. struct drm_i915_private *dev_priv = dev->dev_private;
  10112. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10113. struct intel_plane *intel_plane;
  10114. struct drm_plane *p;
  10115. unsigned fb_bits = 0;
  10116. /* Track fb's for any planes being disabled */
  10117. list_for_each_entry(p, &dev->mode_config.plane_list, head) {
  10118. intel_plane = to_intel_plane(p);
  10119. if (intel_crtc->atomic.disabled_planes &
  10120. (1 << drm_plane_index(p))) {
  10121. switch (p->type) {
  10122. case DRM_PLANE_TYPE_PRIMARY:
  10123. fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
  10124. break;
  10125. case DRM_PLANE_TYPE_CURSOR:
  10126. fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
  10127. break;
  10128. case DRM_PLANE_TYPE_OVERLAY:
  10129. fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
  10130. break;
  10131. }
  10132. mutex_lock(&dev->struct_mutex);
  10133. i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
  10134. mutex_unlock(&dev->struct_mutex);
  10135. }
  10136. }
  10137. if (intel_crtc->atomic.wait_for_flips)
  10138. intel_crtc_wait_for_pending_flips(crtc);
  10139. if (intel_crtc->atomic.disable_fbc)
  10140. intel_fbc_disable(dev);
  10141. if (intel_crtc->atomic.pre_disable_primary)
  10142. intel_pre_disable_primary(crtc);
  10143. if (intel_crtc->atomic.update_wm)
  10144. intel_update_watermarks(crtc);
  10145. intel_runtime_pm_get(dev_priv);
  10146. /* Perform vblank evasion around commit operation */
  10147. if (intel_crtc->active)
  10148. intel_crtc->atomic.evade =
  10149. intel_pipe_update_start(intel_crtc,
  10150. &intel_crtc->atomic.start_vbl_count);
  10151. }
  10152. static void intel_finish_crtc_commit(struct drm_crtc *crtc)
  10153. {
  10154. struct drm_device *dev = crtc->dev;
  10155. struct drm_i915_private *dev_priv = dev->dev_private;
  10156. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10157. struct drm_plane *p;
  10158. if (intel_crtc->atomic.evade)
  10159. intel_pipe_update_end(intel_crtc,
  10160. intel_crtc->atomic.start_vbl_count);
  10161. intel_runtime_pm_put(dev_priv);
  10162. if (intel_crtc->atomic.wait_vblank)
  10163. intel_wait_for_vblank(dev, intel_crtc->pipe);
  10164. intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
  10165. if (intel_crtc->atomic.update_fbc) {
  10166. mutex_lock(&dev->struct_mutex);
  10167. intel_fbc_update(dev);
  10168. mutex_unlock(&dev->struct_mutex);
  10169. }
  10170. if (intel_crtc->atomic.post_enable_primary)
  10171. intel_post_enable_primary(crtc);
  10172. drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
  10173. if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
  10174. intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
  10175. false, false);
  10176. memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
  10177. }
  10178. /**
  10179. * intel_plane_destroy - destroy a plane
  10180. * @plane: plane to destroy
  10181. *
  10182. * Common destruction function for all types of planes (primary, cursor,
  10183. * sprite).
  10184. */
  10185. void intel_plane_destroy(struct drm_plane *plane)
  10186. {
  10187. struct intel_plane *intel_plane = to_intel_plane(plane);
  10188. drm_plane_cleanup(plane);
  10189. kfree(intel_plane);
  10190. }
  10191. const struct drm_plane_funcs intel_plane_funcs = {
  10192. .update_plane = drm_plane_helper_update,
  10193. .disable_plane = drm_plane_helper_disable,
  10194. .destroy = intel_plane_destroy,
  10195. .set_property = drm_atomic_helper_plane_set_property,
  10196. .atomic_get_property = intel_plane_atomic_get_property,
  10197. .atomic_set_property = intel_plane_atomic_set_property,
  10198. .atomic_duplicate_state = intel_plane_duplicate_state,
  10199. .atomic_destroy_state = intel_plane_destroy_state,
  10200. };
  10201. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  10202. int pipe)
  10203. {
  10204. struct intel_plane *primary;
  10205. struct intel_plane_state *state;
  10206. const uint32_t *intel_primary_formats;
  10207. int num_formats;
  10208. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  10209. if (primary == NULL)
  10210. return NULL;
  10211. state = intel_create_plane_state(&primary->base);
  10212. if (!state) {
  10213. kfree(primary);
  10214. return NULL;
  10215. }
  10216. primary->base.state = &state->base;
  10217. primary->can_scale = false;
  10218. primary->max_downscale = 1;
  10219. primary->pipe = pipe;
  10220. primary->plane = pipe;
  10221. primary->check_plane = intel_check_primary_plane;
  10222. primary->commit_plane = intel_commit_primary_plane;
  10223. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  10224. primary->plane = !pipe;
  10225. if (INTEL_INFO(dev)->gen <= 3) {
  10226. intel_primary_formats = intel_primary_formats_gen2;
  10227. num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
  10228. } else {
  10229. intel_primary_formats = intel_primary_formats_gen4;
  10230. num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
  10231. }
  10232. drm_universal_plane_init(dev, &primary->base, 0,
  10233. &intel_plane_funcs,
  10234. intel_primary_formats, num_formats,
  10235. DRM_PLANE_TYPE_PRIMARY);
  10236. if (INTEL_INFO(dev)->gen >= 4) {
  10237. if (!dev->mode_config.rotation_property)
  10238. dev->mode_config.rotation_property =
  10239. drm_mode_create_rotation_property(dev,
  10240. BIT(DRM_ROTATE_0) |
  10241. BIT(DRM_ROTATE_180));
  10242. if (dev->mode_config.rotation_property)
  10243. drm_object_attach_property(&primary->base.base,
  10244. dev->mode_config.rotation_property,
  10245. state->base.rotation);
  10246. }
  10247. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  10248. return &primary->base;
  10249. }
  10250. static int
  10251. intel_check_cursor_plane(struct drm_plane *plane,
  10252. struct intel_plane_state *state)
  10253. {
  10254. struct drm_crtc *crtc = state->base.crtc;
  10255. struct drm_device *dev = plane->dev;
  10256. struct drm_framebuffer *fb = state->base.fb;
  10257. struct drm_rect *dest = &state->dst;
  10258. struct drm_rect *src = &state->src;
  10259. const struct drm_rect *clip = &state->clip;
  10260. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10261. struct intel_crtc *intel_crtc;
  10262. unsigned stride;
  10263. int ret;
  10264. crtc = crtc ? crtc : plane->crtc;
  10265. intel_crtc = to_intel_crtc(crtc);
  10266. ret = drm_plane_helper_check_update(plane, crtc, fb,
  10267. src, dest, clip,
  10268. DRM_PLANE_HELPER_NO_SCALING,
  10269. DRM_PLANE_HELPER_NO_SCALING,
  10270. true, true, &state->visible);
  10271. if (ret)
  10272. return ret;
  10273. /* if we want to turn off the cursor ignore width and height */
  10274. if (!obj)
  10275. goto finish;
  10276. /* Check for which cursor types we support */
  10277. if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
  10278. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  10279. state->base.crtc_w, state->base.crtc_h);
  10280. return -EINVAL;
  10281. }
  10282. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  10283. if (obj->base.size < stride * state->base.crtc_h) {
  10284. DRM_DEBUG_KMS("buffer is too small\n");
  10285. return -ENOMEM;
  10286. }
  10287. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  10288. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  10289. ret = -EINVAL;
  10290. }
  10291. finish:
  10292. if (intel_crtc->active) {
  10293. if (intel_crtc->base.cursor->state->crtc_w != state->base.crtc_w)
  10294. intel_crtc->atomic.update_wm = true;
  10295. intel_crtc->atomic.fb_bits |=
  10296. INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
  10297. }
  10298. return ret;
  10299. }
  10300. static void
  10301. intel_commit_cursor_plane(struct drm_plane *plane,
  10302. struct intel_plane_state *state)
  10303. {
  10304. struct drm_crtc *crtc = state->base.crtc;
  10305. struct drm_device *dev = plane->dev;
  10306. struct intel_crtc *intel_crtc;
  10307. struct intel_plane *intel_plane = to_intel_plane(plane);
  10308. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  10309. uint32_t addr;
  10310. crtc = crtc ? crtc : plane->crtc;
  10311. intel_crtc = to_intel_crtc(crtc);
  10312. plane->fb = state->base.fb;
  10313. crtc->cursor_x = state->base.crtc_x;
  10314. crtc->cursor_y = state->base.crtc_y;
  10315. intel_plane->obj = obj;
  10316. if (intel_crtc->cursor_bo == obj)
  10317. goto update;
  10318. if (!obj)
  10319. addr = 0;
  10320. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  10321. addr = i915_gem_obj_ggtt_offset(obj);
  10322. else
  10323. addr = obj->phys_handle->busaddr;
  10324. intel_crtc->cursor_addr = addr;
  10325. intel_crtc->cursor_bo = obj;
  10326. update:
  10327. if (intel_crtc->active)
  10328. intel_crtc_update_cursor(crtc, state->visible);
  10329. }
  10330. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  10331. int pipe)
  10332. {
  10333. struct intel_plane *cursor;
  10334. struct intel_plane_state *state;
  10335. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  10336. if (cursor == NULL)
  10337. return NULL;
  10338. state = intel_create_plane_state(&cursor->base);
  10339. if (!state) {
  10340. kfree(cursor);
  10341. return NULL;
  10342. }
  10343. cursor->base.state = &state->base;
  10344. cursor->can_scale = false;
  10345. cursor->max_downscale = 1;
  10346. cursor->pipe = pipe;
  10347. cursor->plane = pipe;
  10348. cursor->check_plane = intel_check_cursor_plane;
  10349. cursor->commit_plane = intel_commit_cursor_plane;
  10350. drm_universal_plane_init(dev, &cursor->base, 0,
  10351. &intel_plane_funcs,
  10352. intel_cursor_formats,
  10353. ARRAY_SIZE(intel_cursor_formats),
  10354. DRM_PLANE_TYPE_CURSOR);
  10355. if (INTEL_INFO(dev)->gen >= 4) {
  10356. if (!dev->mode_config.rotation_property)
  10357. dev->mode_config.rotation_property =
  10358. drm_mode_create_rotation_property(dev,
  10359. BIT(DRM_ROTATE_0) |
  10360. BIT(DRM_ROTATE_180));
  10361. if (dev->mode_config.rotation_property)
  10362. drm_object_attach_property(&cursor->base.base,
  10363. dev->mode_config.rotation_property,
  10364. state->base.rotation);
  10365. }
  10366. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  10367. return &cursor->base;
  10368. }
  10369. static void intel_crtc_init(struct drm_device *dev, int pipe)
  10370. {
  10371. struct drm_i915_private *dev_priv = dev->dev_private;
  10372. struct intel_crtc *intel_crtc;
  10373. struct intel_crtc_state *crtc_state = NULL;
  10374. struct drm_plane *primary = NULL;
  10375. struct drm_plane *cursor = NULL;
  10376. int i, ret;
  10377. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  10378. if (intel_crtc == NULL)
  10379. return;
  10380. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  10381. if (!crtc_state)
  10382. goto fail;
  10383. intel_crtc_set_state(intel_crtc, crtc_state);
  10384. crtc_state->base.crtc = &intel_crtc->base;
  10385. primary = intel_primary_plane_create(dev, pipe);
  10386. if (!primary)
  10387. goto fail;
  10388. cursor = intel_cursor_plane_create(dev, pipe);
  10389. if (!cursor)
  10390. goto fail;
  10391. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  10392. cursor, &intel_crtc_funcs);
  10393. if (ret)
  10394. goto fail;
  10395. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  10396. for (i = 0; i < 256; i++) {
  10397. intel_crtc->lut_r[i] = i;
  10398. intel_crtc->lut_g[i] = i;
  10399. intel_crtc->lut_b[i] = i;
  10400. }
  10401. /*
  10402. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  10403. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  10404. */
  10405. intel_crtc->pipe = pipe;
  10406. intel_crtc->plane = pipe;
  10407. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  10408. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  10409. intel_crtc->plane = !pipe;
  10410. }
  10411. intel_crtc->cursor_base = ~0;
  10412. intel_crtc->cursor_cntl = ~0;
  10413. intel_crtc->cursor_size = ~0;
  10414. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  10415. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  10416. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  10417. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  10418. INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
  10419. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  10420. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  10421. return;
  10422. fail:
  10423. if (primary)
  10424. drm_plane_cleanup(primary);
  10425. if (cursor)
  10426. drm_plane_cleanup(cursor);
  10427. kfree(crtc_state);
  10428. kfree(intel_crtc);
  10429. }
  10430. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  10431. {
  10432. struct drm_encoder *encoder = connector->base.encoder;
  10433. struct drm_device *dev = connector->base.dev;
  10434. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  10435. if (!encoder || WARN_ON(!encoder->crtc))
  10436. return INVALID_PIPE;
  10437. return to_intel_crtc(encoder->crtc)->pipe;
  10438. }
  10439. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  10440. struct drm_file *file)
  10441. {
  10442. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  10443. struct drm_crtc *drmmode_crtc;
  10444. struct intel_crtc *crtc;
  10445. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  10446. if (!drmmode_crtc) {
  10447. DRM_ERROR("no such CRTC id\n");
  10448. return -ENOENT;
  10449. }
  10450. crtc = to_intel_crtc(drmmode_crtc);
  10451. pipe_from_crtc_id->pipe = crtc->pipe;
  10452. return 0;
  10453. }
  10454. static int intel_encoder_clones(struct intel_encoder *encoder)
  10455. {
  10456. struct drm_device *dev = encoder->base.dev;
  10457. struct intel_encoder *source_encoder;
  10458. int index_mask = 0;
  10459. int entry = 0;
  10460. for_each_intel_encoder(dev, source_encoder) {
  10461. if (encoders_cloneable(encoder, source_encoder))
  10462. index_mask |= (1 << entry);
  10463. entry++;
  10464. }
  10465. return index_mask;
  10466. }
  10467. static bool has_edp_a(struct drm_device *dev)
  10468. {
  10469. struct drm_i915_private *dev_priv = dev->dev_private;
  10470. if (!IS_MOBILE(dev))
  10471. return false;
  10472. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  10473. return false;
  10474. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  10475. return false;
  10476. return true;
  10477. }
  10478. static bool intel_crt_present(struct drm_device *dev)
  10479. {
  10480. struct drm_i915_private *dev_priv = dev->dev_private;
  10481. if (INTEL_INFO(dev)->gen >= 9)
  10482. return false;
  10483. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  10484. return false;
  10485. if (IS_CHERRYVIEW(dev))
  10486. return false;
  10487. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  10488. return false;
  10489. return true;
  10490. }
  10491. static void intel_setup_outputs(struct drm_device *dev)
  10492. {
  10493. struct drm_i915_private *dev_priv = dev->dev_private;
  10494. struct intel_encoder *encoder;
  10495. struct drm_connector *connector;
  10496. bool dpd_is_edp = false;
  10497. intel_lvds_init(dev);
  10498. if (intel_crt_present(dev))
  10499. intel_crt_init(dev);
  10500. if (HAS_DDI(dev)) {
  10501. int found;
  10502. /* Haswell uses DDI functions to detect digital outputs */
  10503. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  10504. /* DDI A only supports eDP */
  10505. if (found)
  10506. intel_ddi_init(dev, PORT_A);
  10507. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  10508. * register */
  10509. found = I915_READ(SFUSE_STRAP);
  10510. if (found & SFUSE_STRAP_DDIB_DETECTED)
  10511. intel_ddi_init(dev, PORT_B);
  10512. if (found & SFUSE_STRAP_DDIC_DETECTED)
  10513. intel_ddi_init(dev, PORT_C);
  10514. if (found & SFUSE_STRAP_DDID_DETECTED)
  10515. intel_ddi_init(dev, PORT_D);
  10516. } else if (HAS_PCH_SPLIT(dev)) {
  10517. int found;
  10518. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  10519. if (has_edp_a(dev))
  10520. intel_dp_init(dev, DP_A, PORT_A);
  10521. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  10522. /* PCH SDVOB multiplex with HDMIB */
  10523. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  10524. if (!found)
  10525. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  10526. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  10527. intel_dp_init(dev, PCH_DP_B, PORT_B);
  10528. }
  10529. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  10530. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  10531. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  10532. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  10533. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  10534. intel_dp_init(dev, PCH_DP_C, PORT_C);
  10535. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  10536. intel_dp_init(dev, PCH_DP_D, PORT_D);
  10537. } else if (IS_VALLEYVIEW(dev)) {
  10538. /*
  10539. * The DP_DETECTED bit is the latched state of the DDC
  10540. * SDA pin at boot. However since eDP doesn't require DDC
  10541. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  10542. * eDP ports may have been muxed to an alternate function.
  10543. * Thus we can't rely on the DP_DETECTED bit alone to detect
  10544. * eDP ports. Consult the VBT as well as DP_DETECTED to
  10545. * detect eDP ports.
  10546. */
  10547. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
  10548. !intel_dp_is_edp(dev, PORT_B))
  10549. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  10550. PORT_B);
  10551. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  10552. intel_dp_is_edp(dev, PORT_B))
  10553. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  10554. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
  10555. !intel_dp_is_edp(dev, PORT_C))
  10556. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  10557. PORT_C);
  10558. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  10559. intel_dp_is_edp(dev, PORT_C))
  10560. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  10561. if (IS_CHERRYVIEW(dev)) {
  10562. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  10563. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  10564. PORT_D);
  10565. /* eDP not supported on port D, so don't check VBT */
  10566. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  10567. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  10568. }
  10569. intel_dsi_init(dev);
  10570. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  10571. bool found = false;
  10572. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10573. DRM_DEBUG_KMS("probing SDVOB\n");
  10574. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  10575. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  10576. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  10577. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  10578. }
  10579. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  10580. intel_dp_init(dev, DP_B, PORT_B);
  10581. }
  10582. /* Before G4X SDVOC doesn't have its own detect register */
  10583. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10584. DRM_DEBUG_KMS("probing SDVOC\n");
  10585. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  10586. }
  10587. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  10588. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  10589. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  10590. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  10591. }
  10592. if (SUPPORTS_INTEGRATED_DP(dev))
  10593. intel_dp_init(dev, DP_C, PORT_C);
  10594. }
  10595. if (SUPPORTS_INTEGRATED_DP(dev) &&
  10596. (I915_READ(DP_D) & DP_DETECTED))
  10597. intel_dp_init(dev, DP_D, PORT_D);
  10598. } else if (IS_GEN2(dev))
  10599. intel_dvo_init(dev);
  10600. if (SUPPORTS_TV(dev))
  10601. intel_tv_init(dev);
  10602. /*
  10603. * FIXME: We don't have full atomic support yet, but we want to be
  10604. * able to enable/test plane updates via the atomic interface in the
  10605. * meantime. However as soon as we flip DRIVER_ATOMIC on, the DRM core
  10606. * will take some atomic codepaths to lookup properties during
  10607. * drmModeGetConnector() that unconditionally dereference
  10608. * connector->state.
  10609. *
  10610. * We create a dummy connector state here for each connector to ensure
  10611. * the DRM core doesn't try to dereference a NULL connector->state.
  10612. * The actual connector properties will never be updated or contain
  10613. * useful information, but since we're doing this specifically for
  10614. * testing/debug of the plane operations (and only when a specific
  10615. * kernel module option is given), that shouldn't really matter.
  10616. *
  10617. * Once atomic support for crtc's + connectors lands, this loop should
  10618. * be removed since we'll be setting up real connector state, which
  10619. * will contain Intel-specific properties.
  10620. */
  10621. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  10622. list_for_each_entry(connector,
  10623. &dev->mode_config.connector_list,
  10624. head) {
  10625. if (!WARN_ON(connector->state)) {
  10626. connector->state =
  10627. kzalloc(sizeof(*connector->state),
  10628. GFP_KERNEL);
  10629. }
  10630. }
  10631. }
  10632. intel_psr_init(dev);
  10633. for_each_intel_encoder(dev, encoder) {
  10634. encoder->base.possible_crtcs = encoder->crtc_mask;
  10635. encoder->base.possible_clones =
  10636. intel_encoder_clones(encoder);
  10637. }
  10638. intel_init_pch_refclk(dev);
  10639. drm_helper_move_panel_connectors_to_head(dev);
  10640. }
  10641. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  10642. {
  10643. struct drm_device *dev = fb->dev;
  10644. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10645. drm_framebuffer_cleanup(fb);
  10646. mutex_lock(&dev->struct_mutex);
  10647. WARN_ON(!intel_fb->obj->framebuffer_references--);
  10648. drm_gem_object_unreference(&intel_fb->obj->base);
  10649. mutex_unlock(&dev->struct_mutex);
  10650. kfree(intel_fb);
  10651. }
  10652. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  10653. struct drm_file *file,
  10654. unsigned int *handle)
  10655. {
  10656. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10657. struct drm_i915_gem_object *obj = intel_fb->obj;
  10658. return drm_gem_handle_create(file, &obj->base, handle);
  10659. }
  10660. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  10661. .destroy = intel_user_framebuffer_destroy,
  10662. .create_handle = intel_user_framebuffer_create_handle,
  10663. };
  10664. static
  10665. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  10666. uint32_t pixel_format)
  10667. {
  10668. u32 gen = INTEL_INFO(dev)->gen;
  10669. if (gen >= 9) {
  10670. /* "The stride in bytes must not exceed the of the size of 8K
  10671. * pixels and 32K bytes."
  10672. */
  10673. return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
  10674. } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
  10675. return 32*1024;
  10676. } else if (gen >= 4) {
  10677. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  10678. return 16*1024;
  10679. else
  10680. return 32*1024;
  10681. } else if (gen >= 3) {
  10682. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  10683. return 8*1024;
  10684. else
  10685. return 16*1024;
  10686. } else {
  10687. /* XXX DSPC is limited to 4k tiled */
  10688. return 8*1024;
  10689. }
  10690. }
  10691. static int intel_framebuffer_init(struct drm_device *dev,
  10692. struct intel_framebuffer *intel_fb,
  10693. struct drm_mode_fb_cmd2 *mode_cmd,
  10694. struct drm_i915_gem_object *obj)
  10695. {
  10696. int aligned_height;
  10697. int ret;
  10698. u32 pitch_limit, stride_alignment;
  10699. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  10700. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  10701. /* Enforce that fb modifier and tiling mode match, but only for
  10702. * X-tiled. This is needed for FBC. */
  10703. if (!!(obj->tiling_mode == I915_TILING_X) !=
  10704. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  10705. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  10706. return -EINVAL;
  10707. }
  10708. } else {
  10709. if (obj->tiling_mode == I915_TILING_X)
  10710. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  10711. else if (obj->tiling_mode == I915_TILING_Y) {
  10712. DRM_DEBUG("No Y tiling for legacy addfb\n");
  10713. return -EINVAL;
  10714. }
  10715. }
  10716. /* Passed in modifier sanity checking. */
  10717. switch (mode_cmd->modifier[0]) {
  10718. case I915_FORMAT_MOD_Y_TILED:
  10719. case I915_FORMAT_MOD_Yf_TILED:
  10720. if (INTEL_INFO(dev)->gen < 9) {
  10721. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  10722. mode_cmd->modifier[0]);
  10723. return -EINVAL;
  10724. }
  10725. case DRM_FORMAT_MOD_NONE:
  10726. case I915_FORMAT_MOD_X_TILED:
  10727. break;
  10728. default:
  10729. DRM_ERROR("Unsupported fb modifier 0x%llx!\n",
  10730. mode_cmd->modifier[0]);
  10731. return -EINVAL;
  10732. }
  10733. stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
  10734. mode_cmd->pixel_format);
  10735. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  10736. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  10737. mode_cmd->pitches[0], stride_alignment);
  10738. return -EINVAL;
  10739. }
  10740. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  10741. mode_cmd->pixel_format);
  10742. if (mode_cmd->pitches[0] > pitch_limit) {
  10743. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  10744. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  10745. "tiled" : "linear",
  10746. mode_cmd->pitches[0], pitch_limit);
  10747. return -EINVAL;
  10748. }
  10749. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  10750. mode_cmd->pitches[0] != obj->stride) {
  10751. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  10752. mode_cmd->pitches[0], obj->stride);
  10753. return -EINVAL;
  10754. }
  10755. /* Reject formats not supported by any plane early. */
  10756. switch (mode_cmd->pixel_format) {
  10757. case DRM_FORMAT_C8:
  10758. case DRM_FORMAT_RGB565:
  10759. case DRM_FORMAT_XRGB8888:
  10760. case DRM_FORMAT_ARGB8888:
  10761. break;
  10762. case DRM_FORMAT_XRGB1555:
  10763. case DRM_FORMAT_ARGB1555:
  10764. if (INTEL_INFO(dev)->gen > 3) {
  10765. DRM_DEBUG("unsupported pixel format: %s\n",
  10766. drm_get_format_name(mode_cmd->pixel_format));
  10767. return -EINVAL;
  10768. }
  10769. break;
  10770. case DRM_FORMAT_XBGR8888:
  10771. case DRM_FORMAT_ABGR8888:
  10772. case DRM_FORMAT_XRGB2101010:
  10773. case DRM_FORMAT_ARGB2101010:
  10774. case DRM_FORMAT_XBGR2101010:
  10775. case DRM_FORMAT_ABGR2101010:
  10776. if (INTEL_INFO(dev)->gen < 4) {
  10777. DRM_DEBUG("unsupported pixel format: %s\n",
  10778. drm_get_format_name(mode_cmd->pixel_format));
  10779. return -EINVAL;
  10780. }
  10781. break;
  10782. case DRM_FORMAT_YUYV:
  10783. case DRM_FORMAT_UYVY:
  10784. case DRM_FORMAT_YVYU:
  10785. case DRM_FORMAT_VYUY:
  10786. if (INTEL_INFO(dev)->gen < 5) {
  10787. DRM_DEBUG("unsupported pixel format: %s\n",
  10788. drm_get_format_name(mode_cmd->pixel_format));
  10789. return -EINVAL;
  10790. }
  10791. break;
  10792. default:
  10793. DRM_DEBUG("unsupported pixel format: %s\n",
  10794. drm_get_format_name(mode_cmd->pixel_format));
  10795. return -EINVAL;
  10796. }
  10797. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  10798. if (mode_cmd->offsets[0] != 0)
  10799. return -EINVAL;
  10800. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  10801. mode_cmd->pixel_format,
  10802. mode_cmd->modifier[0]);
  10803. /* FIXME drm helper for size checks (especially planar formats)? */
  10804. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  10805. return -EINVAL;
  10806. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  10807. intel_fb->obj = obj;
  10808. intel_fb->obj->framebuffer_references++;
  10809. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  10810. if (ret) {
  10811. DRM_ERROR("framebuffer init failed %d\n", ret);
  10812. return ret;
  10813. }
  10814. return 0;
  10815. }
  10816. static struct drm_framebuffer *
  10817. intel_user_framebuffer_create(struct drm_device *dev,
  10818. struct drm_file *filp,
  10819. struct drm_mode_fb_cmd2 *mode_cmd)
  10820. {
  10821. struct drm_i915_gem_object *obj;
  10822. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  10823. mode_cmd->handles[0]));
  10824. if (&obj->base == NULL)
  10825. return ERR_PTR(-ENOENT);
  10826. return intel_framebuffer_create(dev, mode_cmd, obj);
  10827. }
  10828. #ifndef CONFIG_DRM_I915_FBDEV
  10829. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  10830. {
  10831. }
  10832. #endif
  10833. static const struct drm_mode_config_funcs intel_mode_funcs = {
  10834. .fb_create = intel_user_framebuffer_create,
  10835. .output_poll_changed = intel_fbdev_output_poll_changed,
  10836. .atomic_check = intel_atomic_check,
  10837. .atomic_commit = intel_atomic_commit,
  10838. };
  10839. /* Set up chip specific display functions */
  10840. static void intel_init_display(struct drm_device *dev)
  10841. {
  10842. struct drm_i915_private *dev_priv = dev->dev_private;
  10843. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  10844. dev_priv->display.find_dpll = g4x_find_best_dpll;
  10845. else if (IS_CHERRYVIEW(dev))
  10846. dev_priv->display.find_dpll = chv_find_best_dpll;
  10847. else if (IS_VALLEYVIEW(dev))
  10848. dev_priv->display.find_dpll = vlv_find_best_dpll;
  10849. else if (IS_PINEVIEW(dev))
  10850. dev_priv->display.find_dpll = pnv_find_best_dpll;
  10851. else
  10852. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  10853. if (INTEL_INFO(dev)->gen >= 9) {
  10854. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  10855. dev_priv->display.get_initial_plane_config =
  10856. skylake_get_initial_plane_config;
  10857. dev_priv->display.crtc_compute_clock =
  10858. haswell_crtc_compute_clock;
  10859. dev_priv->display.crtc_enable = haswell_crtc_enable;
  10860. dev_priv->display.crtc_disable = haswell_crtc_disable;
  10861. dev_priv->display.off = ironlake_crtc_off;
  10862. dev_priv->display.update_primary_plane =
  10863. skylake_update_primary_plane;
  10864. } else if (HAS_DDI(dev)) {
  10865. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  10866. dev_priv->display.get_initial_plane_config =
  10867. ironlake_get_initial_plane_config;
  10868. dev_priv->display.crtc_compute_clock =
  10869. haswell_crtc_compute_clock;
  10870. dev_priv->display.crtc_enable = haswell_crtc_enable;
  10871. dev_priv->display.crtc_disable = haswell_crtc_disable;
  10872. dev_priv->display.off = ironlake_crtc_off;
  10873. dev_priv->display.update_primary_plane =
  10874. ironlake_update_primary_plane;
  10875. } else if (HAS_PCH_SPLIT(dev)) {
  10876. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  10877. dev_priv->display.get_initial_plane_config =
  10878. ironlake_get_initial_plane_config;
  10879. dev_priv->display.crtc_compute_clock =
  10880. ironlake_crtc_compute_clock;
  10881. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  10882. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  10883. dev_priv->display.off = ironlake_crtc_off;
  10884. dev_priv->display.update_primary_plane =
  10885. ironlake_update_primary_plane;
  10886. } else if (IS_VALLEYVIEW(dev)) {
  10887. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10888. dev_priv->display.get_initial_plane_config =
  10889. i9xx_get_initial_plane_config;
  10890. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  10891. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  10892. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10893. dev_priv->display.off = i9xx_crtc_off;
  10894. dev_priv->display.update_primary_plane =
  10895. i9xx_update_primary_plane;
  10896. } else {
  10897. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10898. dev_priv->display.get_initial_plane_config =
  10899. i9xx_get_initial_plane_config;
  10900. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  10901. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  10902. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10903. dev_priv->display.off = i9xx_crtc_off;
  10904. dev_priv->display.update_primary_plane =
  10905. i9xx_update_primary_plane;
  10906. }
  10907. /* Returns the core display clock speed */
  10908. if (IS_VALLEYVIEW(dev))
  10909. dev_priv->display.get_display_clock_speed =
  10910. valleyview_get_display_clock_speed;
  10911. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  10912. dev_priv->display.get_display_clock_speed =
  10913. i945_get_display_clock_speed;
  10914. else if (IS_I915G(dev))
  10915. dev_priv->display.get_display_clock_speed =
  10916. i915_get_display_clock_speed;
  10917. else if (IS_I945GM(dev) || IS_845G(dev))
  10918. dev_priv->display.get_display_clock_speed =
  10919. i9xx_misc_get_display_clock_speed;
  10920. else if (IS_PINEVIEW(dev))
  10921. dev_priv->display.get_display_clock_speed =
  10922. pnv_get_display_clock_speed;
  10923. else if (IS_I915GM(dev))
  10924. dev_priv->display.get_display_clock_speed =
  10925. i915gm_get_display_clock_speed;
  10926. else if (IS_I865G(dev))
  10927. dev_priv->display.get_display_clock_speed =
  10928. i865_get_display_clock_speed;
  10929. else if (IS_I85X(dev))
  10930. dev_priv->display.get_display_clock_speed =
  10931. i855_get_display_clock_speed;
  10932. else /* 852, 830 */
  10933. dev_priv->display.get_display_clock_speed =
  10934. i830_get_display_clock_speed;
  10935. if (IS_GEN5(dev)) {
  10936. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  10937. } else if (IS_GEN6(dev)) {
  10938. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  10939. } else if (IS_IVYBRIDGE(dev)) {
  10940. /* FIXME: detect B0+ stepping and use auto training */
  10941. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  10942. dev_priv->display.modeset_global_resources =
  10943. ivb_modeset_global_resources;
  10944. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  10945. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  10946. } else if (IS_VALLEYVIEW(dev)) {
  10947. dev_priv->display.modeset_global_resources =
  10948. valleyview_modeset_global_resources;
  10949. }
  10950. switch (INTEL_INFO(dev)->gen) {
  10951. case 2:
  10952. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  10953. break;
  10954. case 3:
  10955. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  10956. break;
  10957. case 4:
  10958. case 5:
  10959. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  10960. break;
  10961. case 6:
  10962. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  10963. break;
  10964. case 7:
  10965. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  10966. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  10967. break;
  10968. case 9:
  10969. /* Drop through - unsupported since execlist only. */
  10970. default:
  10971. /* Default just returns -ENODEV to indicate unsupported */
  10972. dev_priv->display.queue_flip = intel_default_queue_flip;
  10973. }
  10974. intel_panel_init_backlight_funcs(dev);
  10975. mutex_init(&dev_priv->pps_mutex);
  10976. }
  10977. /*
  10978. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  10979. * resume, or other times. This quirk makes sure that's the case for
  10980. * affected systems.
  10981. */
  10982. static void quirk_pipea_force(struct drm_device *dev)
  10983. {
  10984. struct drm_i915_private *dev_priv = dev->dev_private;
  10985. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  10986. DRM_INFO("applying pipe a force quirk\n");
  10987. }
  10988. static void quirk_pipeb_force(struct drm_device *dev)
  10989. {
  10990. struct drm_i915_private *dev_priv = dev->dev_private;
  10991. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  10992. DRM_INFO("applying pipe b force quirk\n");
  10993. }
  10994. /*
  10995. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  10996. */
  10997. static void quirk_ssc_force_disable(struct drm_device *dev)
  10998. {
  10999. struct drm_i915_private *dev_priv = dev->dev_private;
  11000. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  11001. DRM_INFO("applying lvds SSC disable quirk\n");
  11002. }
  11003. /*
  11004. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  11005. * brightness value
  11006. */
  11007. static void quirk_invert_brightness(struct drm_device *dev)
  11008. {
  11009. struct drm_i915_private *dev_priv = dev->dev_private;
  11010. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  11011. DRM_INFO("applying inverted panel brightness quirk\n");
  11012. }
  11013. /* Some VBT's incorrectly indicate no backlight is present */
  11014. static void quirk_backlight_present(struct drm_device *dev)
  11015. {
  11016. struct drm_i915_private *dev_priv = dev->dev_private;
  11017. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  11018. DRM_INFO("applying backlight present quirk\n");
  11019. }
  11020. struct intel_quirk {
  11021. int device;
  11022. int subsystem_vendor;
  11023. int subsystem_device;
  11024. void (*hook)(struct drm_device *dev);
  11025. };
  11026. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  11027. struct intel_dmi_quirk {
  11028. void (*hook)(struct drm_device *dev);
  11029. const struct dmi_system_id (*dmi_id_list)[];
  11030. };
  11031. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  11032. {
  11033. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  11034. return 1;
  11035. }
  11036. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  11037. {
  11038. .dmi_id_list = &(const struct dmi_system_id[]) {
  11039. {
  11040. .callback = intel_dmi_reverse_brightness,
  11041. .ident = "NCR Corporation",
  11042. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  11043. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  11044. },
  11045. },
  11046. { } /* terminating entry */
  11047. },
  11048. .hook = quirk_invert_brightness,
  11049. },
  11050. };
  11051. static struct intel_quirk intel_quirks[] = {
  11052. /* HP Mini needs pipe A force quirk (LP: #322104) */
  11053. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  11054. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  11055. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  11056. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  11057. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  11058. /* 830 needs to leave pipe A & dpll A up */
  11059. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  11060. /* 830 needs to leave pipe B & dpll B up */
  11061. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  11062. /* Lenovo U160 cannot use SSC on LVDS */
  11063. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  11064. /* Sony Vaio Y cannot use SSC on LVDS */
  11065. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  11066. /* Acer Aspire 5734Z must invert backlight brightness */
  11067. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  11068. /* Acer/eMachines G725 */
  11069. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  11070. /* Acer/eMachines e725 */
  11071. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  11072. /* Acer/Packard Bell NCL20 */
  11073. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  11074. /* Acer Aspire 4736Z */
  11075. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  11076. /* Acer Aspire 5336 */
  11077. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  11078. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  11079. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  11080. /* Acer C720 Chromebook (Core i3 4005U) */
  11081. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  11082. /* Apple Macbook 2,1 (Core 2 T7400) */
  11083. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  11084. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  11085. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  11086. /* HP Chromebook 14 (Celeron 2955U) */
  11087. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  11088. /* Dell Chromebook 11 */
  11089. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  11090. };
  11091. static void intel_init_quirks(struct drm_device *dev)
  11092. {
  11093. struct pci_dev *d = dev->pdev;
  11094. int i;
  11095. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  11096. struct intel_quirk *q = &intel_quirks[i];
  11097. if (d->device == q->device &&
  11098. (d->subsystem_vendor == q->subsystem_vendor ||
  11099. q->subsystem_vendor == PCI_ANY_ID) &&
  11100. (d->subsystem_device == q->subsystem_device ||
  11101. q->subsystem_device == PCI_ANY_ID))
  11102. q->hook(dev);
  11103. }
  11104. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  11105. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  11106. intel_dmi_quirks[i].hook(dev);
  11107. }
  11108. }
  11109. /* Disable the VGA plane that we never use */
  11110. static void i915_disable_vga(struct drm_device *dev)
  11111. {
  11112. struct drm_i915_private *dev_priv = dev->dev_private;
  11113. u8 sr1;
  11114. u32 vga_reg = i915_vgacntrl_reg(dev);
  11115. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  11116. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  11117. outb(SR01, VGA_SR_INDEX);
  11118. sr1 = inb(VGA_SR_DATA);
  11119. outb(sr1 | 1<<5, VGA_SR_DATA);
  11120. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  11121. udelay(300);
  11122. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  11123. POSTING_READ(vga_reg);
  11124. }
  11125. void intel_modeset_init_hw(struct drm_device *dev)
  11126. {
  11127. intel_prepare_ddi(dev);
  11128. if (IS_VALLEYVIEW(dev))
  11129. vlv_update_cdclk(dev);
  11130. intel_init_clock_gating(dev);
  11131. intel_enable_gt_powersave(dev);
  11132. }
  11133. void intel_modeset_init(struct drm_device *dev)
  11134. {
  11135. struct drm_i915_private *dev_priv = dev->dev_private;
  11136. int sprite, ret;
  11137. enum pipe pipe;
  11138. struct intel_crtc *crtc;
  11139. drm_mode_config_init(dev);
  11140. dev->mode_config.min_width = 0;
  11141. dev->mode_config.min_height = 0;
  11142. dev->mode_config.preferred_depth = 24;
  11143. dev->mode_config.prefer_shadow = 1;
  11144. dev->mode_config.allow_fb_modifiers = true;
  11145. dev->mode_config.funcs = &intel_mode_funcs;
  11146. intel_init_quirks(dev);
  11147. intel_init_pm(dev);
  11148. if (INTEL_INFO(dev)->num_pipes == 0)
  11149. return;
  11150. intel_init_display(dev);
  11151. intel_init_audio(dev);
  11152. if (IS_GEN2(dev)) {
  11153. dev->mode_config.max_width = 2048;
  11154. dev->mode_config.max_height = 2048;
  11155. } else if (IS_GEN3(dev)) {
  11156. dev->mode_config.max_width = 4096;
  11157. dev->mode_config.max_height = 4096;
  11158. } else {
  11159. dev->mode_config.max_width = 8192;
  11160. dev->mode_config.max_height = 8192;
  11161. }
  11162. if (IS_845G(dev) || IS_I865G(dev)) {
  11163. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  11164. dev->mode_config.cursor_height = 1023;
  11165. } else if (IS_GEN2(dev)) {
  11166. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  11167. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  11168. } else {
  11169. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  11170. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  11171. }
  11172. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  11173. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  11174. INTEL_INFO(dev)->num_pipes,
  11175. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  11176. for_each_pipe(dev_priv, pipe) {
  11177. intel_crtc_init(dev, pipe);
  11178. for_each_sprite(dev_priv, pipe, sprite) {
  11179. ret = intel_plane_init(dev, pipe, sprite);
  11180. if (ret)
  11181. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  11182. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  11183. }
  11184. }
  11185. intel_init_dpio(dev);
  11186. intel_shared_dpll_init(dev);
  11187. /* Just disable it once at startup */
  11188. i915_disable_vga(dev);
  11189. intel_setup_outputs(dev);
  11190. /* Just in case the BIOS is doing something questionable. */
  11191. intel_fbc_disable(dev);
  11192. drm_modeset_lock_all(dev);
  11193. intel_modeset_setup_hw_state(dev, false);
  11194. drm_modeset_unlock_all(dev);
  11195. for_each_intel_crtc(dev, crtc) {
  11196. if (!crtc->active)
  11197. continue;
  11198. /*
  11199. * Note that reserving the BIOS fb up front prevents us
  11200. * from stuffing other stolen allocations like the ring
  11201. * on top. This prevents some ugliness at boot time, and
  11202. * can even allow for smooth boot transitions if the BIOS
  11203. * fb is large enough for the active pipe configuration.
  11204. */
  11205. if (dev_priv->display.get_initial_plane_config) {
  11206. dev_priv->display.get_initial_plane_config(crtc,
  11207. &crtc->plane_config);
  11208. /*
  11209. * If the fb is shared between multiple heads, we'll
  11210. * just get the first one.
  11211. */
  11212. intel_find_plane_obj(crtc, &crtc->plane_config);
  11213. }
  11214. }
  11215. }
  11216. static void intel_enable_pipe_a(struct drm_device *dev)
  11217. {
  11218. struct intel_connector *connector;
  11219. struct drm_connector *crt = NULL;
  11220. struct intel_load_detect_pipe load_detect_temp;
  11221. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  11222. /* We can't just switch on the pipe A, we need to set things up with a
  11223. * proper mode and output configuration. As a gross hack, enable pipe A
  11224. * by enabling the load detect pipe once. */
  11225. for_each_intel_connector(dev, connector) {
  11226. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  11227. crt = &connector->base;
  11228. break;
  11229. }
  11230. }
  11231. if (!crt)
  11232. return;
  11233. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  11234. intel_release_load_detect_pipe(crt, &load_detect_temp);
  11235. }
  11236. static bool
  11237. intel_check_plane_mapping(struct intel_crtc *crtc)
  11238. {
  11239. struct drm_device *dev = crtc->base.dev;
  11240. struct drm_i915_private *dev_priv = dev->dev_private;
  11241. u32 reg, val;
  11242. if (INTEL_INFO(dev)->num_pipes == 1)
  11243. return true;
  11244. reg = DSPCNTR(!crtc->plane);
  11245. val = I915_READ(reg);
  11246. if ((val & DISPLAY_PLANE_ENABLE) &&
  11247. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  11248. return false;
  11249. return true;
  11250. }
  11251. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  11252. {
  11253. struct drm_device *dev = crtc->base.dev;
  11254. struct drm_i915_private *dev_priv = dev->dev_private;
  11255. u32 reg;
  11256. /* Clear any frame start delays used for debugging left by the BIOS */
  11257. reg = PIPECONF(crtc->config->cpu_transcoder);
  11258. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  11259. /* restore vblank interrupts to correct state */
  11260. drm_crtc_vblank_reset(&crtc->base);
  11261. if (crtc->active) {
  11262. update_scanline_offset(crtc);
  11263. drm_crtc_vblank_on(&crtc->base);
  11264. }
  11265. /* We need to sanitize the plane -> pipe mapping first because this will
  11266. * disable the crtc (and hence change the state) if it is wrong. Note
  11267. * that gen4+ has a fixed plane -> pipe mapping. */
  11268. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  11269. struct intel_connector *connector;
  11270. bool plane;
  11271. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  11272. crtc->base.base.id);
  11273. /* Pipe has the wrong plane attached and the plane is active.
  11274. * Temporarily change the plane mapping and disable everything
  11275. * ... */
  11276. plane = crtc->plane;
  11277. crtc->plane = !plane;
  11278. crtc->primary_enabled = true;
  11279. dev_priv->display.crtc_disable(&crtc->base);
  11280. crtc->plane = plane;
  11281. /* ... and break all links. */
  11282. for_each_intel_connector(dev, connector) {
  11283. if (connector->encoder->base.crtc != &crtc->base)
  11284. continue;
  11285. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11286. connector->base.encoder = NULL;
  11287. }
  11288. /* multiple connectors may have the same encoder:
  11289. * handle them and break crtc link separately */
  11290. for_each_intel_connector(dev, connector)
  11291. if (connector->encoder->base.crtc == &crtc->base) {
  11292. connector->encoder->base.crtc = NULL;
  11293. connector->encoder->connectors_active = false;
  11294. }
  11295. WARN_ON(crtc->active);
  11296. crtc->base.state->enable = false;
  11297. crtc->base.enabled = false;
  11298. }
  11299. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  11300. crtc->pipe == PIPE_A && !crtc->active) {
  11301. /* BIOS forgot to enable pipe A, this mostly happens after
  11302. * resume. Force-enable the pipe to fix this, the update_dpms
  11303. * call below we restore the pipe to the right state, but leave
  11304. * the required bits on. */
  11305. intel_enable_pipe_a(dev);
  11306. }
  11307. /* Adjust the state of the output pipe according to whether we
  11308. * have active connectors/encoders. */
  11309. intel_crtc_update_dpms(&crtc->base);
  11310. if (crtc->active != crtc->base.state->enable) {
  11311. struct intel_encoder *encoder;
  11312. /* This can happen either due to bugs in the get_hw_state
  11313. * functions or because the pipe is force-enabled due to the
  11314. * pipe A quirk. */
  11315. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  11316. crtc->base.base.id,
  11317. crtc->base.state->enable ? "enabled" : "disabled",
  11318. crtc->active ? "enabled" : "disabled");
  11319. crtc->base.state->enable = crtc->active;
  11320. crtc->base.enabled = crtc->active;
  11321. /* Because we only establish the connector -> encoder ->
  11322. * crtc links if something is active, this means the
  11323. * crtc is now deactivated. Break the links. connector
  11324. * -> encoder links are only establish when things are
  11325. * actually up, hence no need to break them. */
  11326. WARN_ON(crtc->active);
  11327. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  11328. WARN_ON(encoder->connectors_active);
  11329. encoder->base.crtc = NULL;
  11330. }
  11331. }
  11332. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  11333. /*
  11334. * We start out with underrun reporting disabled to avoid races.
  11335. * For correct bookkeeping mark this on active crtcs.
  11336. *
  11337. * Also on gmch platforms we dont have any hardware bits to
  11338. * disable the underrun reporting. Which means we need to start
  11339. * out with underrun reporting disabled also on inactive pipes,
  11340. * since otherwise we'll complain about the garbage we read when
  11341. * e.g. coming up after runtime pm.
  11342. *
  11343. * No protection against concurrent access is required - at
  11344. * worst a fifo underrun happens which also sets this to false.
  11345. */
  11346. crtc->cpu_fifo_underrun_disabled = true;
  11347. crtc->pch_fifo_underrun_disabled = true;
  11348. }
  11349. }
  11350. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  11351. {
  11352. struct intel_connector *connector;
  11353. struct drm_device *dev = encoder->base.dev;
  11354. /* We need to check both for a crtc link (meaning that the
  11355. * encoder is active and trying to read from a pipe) and the
  11356. * pipe itself being active. */
  11357. bool has_active_crtc = encoder->base.crtc &&
  11358. to_intel_crtc(encoder->base.crtc)->active;
  11359. if (encoder->connectors_active && !has_active_crtc) {
  11360. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  11361. encoder->base.base.id,
  11362. encoder->base.name);
  11363. /* Connector is active, but has no active pipe. This is
  11364. * fallout from our resume register restoring. Disable
  11365. * the encoder manually again. */
  11366. if (encoder->base.crtc) {
  11367. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  11368. encoder->base.base.id,
  11369. encoder->base.name);
  11370. encoder->disable(encoder);
  11371. if (encoder->post_disable)
  11372. encoder->post_disable(encoder);
  11373. }
  11374. encoder->base.crtc = NULL;
  11375. encoder->connectors_active = false;
  11376. /* Inconsistent output/port/pipe state happens presumably due to
  11377. * a bug in one of the get_hw_state functions. Or someplace else
  11378. * in our code, like the register restore mess on resume. Clamp
  11379. * things to off as a safer default. */
  11380. for_each_intel_connector(dev, connector) {
  11381. if (connector->encoder != encoder)
  11382. continue;
  11383. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11384. connector->base.encoder = NULL;
  11385. }
  11386. }
  11387. /* Enabled encoders without active connectors will be fixed in
  11388. * the crtc fixup. */
  11389. }
  11390. void i915_redisable_vga_power_on(struct drm_device *dev)
  11391. {
  11392. struct drm_i915_private *dev_priv = dev->dev_private;
  11393. u32 vga_reg = i915_vgacntrl_reg(dev);
  11394. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  11395. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  11396. i915_disable_vga(dev);
  11397. }
  11398. }
  11399. void i915_redisable_vga(struct drm_device *dev)
  11400. {
  11401. struct drm_i915_private *dev_priv = dev->dev_private;
  11402. /* This function can be called both from intel_modeset_setup_hw_state or
  11403. * at a very early point in our resume sequence, where the power well
  11404. * structures are not yet restored. Since this function is at a very
  11405. * paranoid "someone might have enabled VGA while we were not looking"
  11406. * level, just check if the power well is enabled instead of trying to
  11407. * follow the "don't touch the power well if we don't need it" policy
  11408. * the rest of the driver uses. */
  11409. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  11410. return;
  11411. i915_redisable_vga_power_on(dev);
  11412. }
  11413. static bool primary_get_hw_state(struct intel_crtc *crtc)
  11414. {
  11415. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  11416. if (!crtc->active)
  11417. return false;
  11418. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  11419. }
  11420. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  11421. {
  11422. struct drm_i915_private *dev_priv = dev->dev_private;
  11423. enum pipe pipe;
  11424. struct intel_crtc *crtc;
  11425. struct intel_encoder *encoder;
  11426. struct intel_connector *connector;
  11427. int i;
  11428. for_each_intel_crtc(dev, crtc) {
  11429. memset(crtc->config, 0, sizeof(*crtc->config));
  11430. crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  11431. crtc->active = dev_priv->display.get_pipe_config(crtc,
  11432. crtc->config);
  11433. crtc->base.state->enable = crtc->active;
  11434. crtc->base.enabled = crtc->active;
  11435. crtc->primary_enabled = primary_get_hw_state(crtc);
  11436. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  11437. crtc->base.base.id,
  11438. crtc->active ? "enabled" : "disabled");
  11439. }
  11440. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11441. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11442. pll->on = pll->get_hw_state(dev_priv, pll,
  11443. &pll->config.hw_state);
  11444. pll->active = 0;
  11445. pll->config.crtc_mask = 0;
  11446. for_each_intel_crtc(dev, crtc) {
  11447. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  11448. pll->active++;
  11449. pll->config.crtc_mask |= 1 << crtc->pipe;
  11450. }
  11451. }
  11452. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  11453. pll->name, pll->config.crtc_mask, pll->on);
  11454. if (pll->config.crtc_mask)
  11455. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  11456. }
  11457. for_each_intel_encoder(dev, encoder) {
  11458. pipe = 0;
  11459. if (encoder->get_hw_state(encoder, &pipe)) {
  11460. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11461. encoder->base.crtc = &crtc->base;
  11462. encoder->get_config(encoder, crtc->config);
  11463. } else {
  11464. encoder->base.crtc = NULL;
  11465. }
  11466. encoder->connectors_active = false;
  11467. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  11468. encoder->base.base.id,
  11469. encoder->base.name,
  11470. encoder->base.crtc ? "enabled" : "disabled",
  11471. pipe_name(pipe));
  11472. }
  11473. for_each_intel_connector(dev, connector) {
  11474. if (connector->get_hw_state(connector)) {
  11475. connector->base.dpms = DRM_MODE_DPMS_ON;
  11476. connector->encoder->connectors_active = true;
  11477. connector->base.encoder = &connector->encoder->base;
  11478. } else {
  11479. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11480. connector->base.encoder = NULL;
  11481. }
  11482. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  11483. connector->base.base.id,
  11484. connector->base.name,
  11485. connector->base.encoder ? "enabled" : "disabled");
  11486. }
  11487. }
  11488. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  11489. * and i915 state tracking structures. */
  11490. void intel_modeset_setup_hw_state(struct drm_device *dev,
  11491. bool force_restore)
  11492. {
  11493. struct drm_i915_private *dev_priv = dev->dev_private;
  11494. enum pipe pipe;
  11495. struct intel_crtc *crtc;
  11496. struct intel_encoder *encoder;
  11497. int i;
  11498. intel_modeset_readout_hw_state(dev);
  11499. /*
  11500. * Now that we have the config, copy it to each CRTC struct
  11501. * Note that this could go away if we move to using crtc_config
  11502. * checking everywhere.
  11503. */
  11504. for_each_intel_crtc(dev, crtc) {
  11505. if (crtc->active && i915.fastboot) {
  11506. intel_mode_from_pipe_config(&crtc->base.mode,
  11507. crtc->config);
  11508. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  11509. crtc->base.base.id);
  11510. drm_mode_debug_printmodeline(&crtc->base.mode);
  11511. }
  11512. }
  11513. /* HW state is read out, now we need to sanitize this mess. */
  11514. for_each_intel_encoder(dev, encoder) {
  11515. intel_sanitize_encoder(encoder);
  11516. }
  11517. for_each_pipe(dev_priv, pipe) {
  11518. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11519. intel_sanitize_crtc(crtc);
  11520. intel_dump_pipe_config(crtc, crtc->config,
  11521. "[setup_hw_state]");
  11522. }
  11523. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11524. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11525. if (!pll->on || pll->active)
  11526. continue;
  11527. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  11528. pll->disable(dev_priv, pll);
  11529. pll->on = false;
  11530. }
  11531. if (IS_GEN9(dev))
  11532. skl_wm_get_hw_state(dev);
  11533. else if (HAS_PCH_SPLIT(dev))
  11534. ilk_wm_get_hw_state(dev);
  11535. if (force_restore) {
  11536. i915_redisable_vga(dev);
  11537. /*
  11538. * We need to use raw interfaces for restoring state to avoid
  11539. * checking (bogus) intermediate states.
  11540. */
  11541. for_each_pipe(dev_priv, pipe) {
  11542. struct drm_crtc *crtc =
  11543. dev_priv->pipe_to_crtc_mapping[pipe];
  11544. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
  11545. crtc->primary->fb);
  11546. }
  11547. } else {
  11548. intel_modeset_update_staged_output_state(dev);
  11549. }
  11550. intel_modeset_check_state(dev);
  11551. }
  11552. void intel_modeset_gem_init(struct drm_device *dev)
  11553. {
  11554. struct drm_i915_private *dev_priv = dev->dev_private;
  11555. struct drm_crtc *c;
  11556. struct drm_i915_gem_object *obj;
  11557. mutex_lock(&dev->struct_mutex);
  11558. intel_init_gt_powersave(dev);
  11559. mutex_unlock(&dev->struct_mutex);
  11560. /*
  11561. * There may be no VBT; and if the BIOS enabled SSC we can
  11562. * just keep using it to avoid unnecessary flicker. Whereas if the
  11563. * BIOS isn't using it, don't assume it will work even if the VBT
  11564. * indicates as much.
  11565. */
  11566. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11567. dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  11568. DREF_SSC1_ENABLE);
  11569. intel_modeset_init_hw(dev);
  11570. intel_setup_overlay(dev);
  11571. /*
  11572. * Make sure any fbs we allocated at startup are properly
  11573. * pinned & fenced. When we do the allocation it's too early
  11574. * for this.
  11575. */
  11576. mutex_lock(&dev->struct_mutex);
  11577. for_each_crtc(dev, c) {
  11578. obj = intel_fb_obj(c->primary->fb);
  11579. if (obj == NULL)
  11580. continue;
  11581. if (intel_pin_and_fence_fb_obj(c->primary,
  11582. c->primary->fb,
  11583. NULL)) {
  11584. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  11585. to_intel_crtc(c)->pipe);
  11586. drm_framebuffer_unreference(c->primary->fb);
  11587. c->primary->fb = NULL;
  11588. update_state_fb(c->primary);
  11589. }
  11590. }
  11591. mutex_unlock(&dev->struct_mutex);
  11592. intel_backlight_register(dev);
  11593. }
  11594. void intel_connector_unregister(struct intel_connector *intel_connector)
  11595. {
  11596. struct drm_connector *connector = &intel_connector->base;
  11597. intel_panel_destroy_backlight(connector);
  11598. drm_connector_unregister(connector);
  11599. }
  11600. void intel_modeset_cleanup(struct drm_device *dev)
  11601. {
  11602. struct drm_i915_private *dev_priv = dev->dev_private;
  11603. struct drm_connector *connector;
  11604. intel_disable_gt_powersave(dev);
  11605. intel_backlight_unregister(dev);
  11606. /*
  11607. * Interrupts and polling as the first thing to avoid creating havoc.
  11608. * Too much stuff here (turning of connectors, ...) would
  11609. * experience fancy races otherwise.
  11610. */
  11611. intel_irq_uninstall(dev_priv);
  11612. /*
  11613. * Due to the hpd irq storm handling the hotplug work can re-arm the
  11614. * poll handlers. Hence disable polling after hpd handling is shut down.
  11615. */
  11616. drm_kms_helper_poll_fini(dev);
  11617. mutex_lock(&dev->struct_mutex);
  11618. intel_unregister_dsm_handler();
  11619. intel_fbc_disable(dev);
  11620. ironlake_teardown_rc6(dev);
  11621. mutex_unlock(&dev->struct_mutex);
  11622. /* flush any delayed tasks or pending work */
  11623. flush_scheduled_work();
  11624. /* destroy the backlight and sysfs files before encoders/connectors */
  11625. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  11626. struct intel_connector *intel_connector;
  11627. intel_connector = to_intel_connector(connector);
  11628. intel_connector->unregister(intel_connector);
  11629. }
  11630. drm_mode_config_cleanup(dev);
  11631. intel_cleanup_overlay(dev);
  11632. mutex_lock(&dev->struct_mutex);
  11633. intel_cleanup_gt_powersave(dev);
  11634. mutex_unlock(&dev->struct_mutex);
  11635. }
  11636. /*
  11637. * Return which encoder is currently attached for connector.
  11638. */
  11639. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  11640. {
  11641. return &intel_attached_encoder(connector)->base;
  11642. }
  11643. void intel_connector_attach_encoder(struct intel_connector *connector,
  11644. struct intel_encoder *encoder)
  11645. {
  11646. connector->encoder = encoder;
  11647. drm_mode_connector_attach_encoder(&connector->base,
  11648. &encoder->base);
  11649. }
  11650. /*
  11651. * set vga decode state - true == enable VGA decode
  11652. */
  11653. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  11654. {
  11655. struct drm_i915_private *dev_priv = dev->dev_private;
  11656. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  11657. u16 gmch_ctrl;
  11658. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  11659. DRM_ERROR("failed to read control word\n");
  11660. return -EIO;
  11661. }
  11662. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  11663. return 0;
  11664. if (state)
  11665. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  11666. else
  11667. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  11668. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  11669. DRM_ERROR("failed to write control word\n");
  11670. return -EIO;
  11671. }
  11672. return 0;
  11673. }
  11674. struct intel_display_error_state {
  11675. u32 power_well_driver;
  11676. int num_transcoders;
  11677. struct intel_cursor_error_state {
  11678. u32 control;
  11679. u32 position;
  11680. u32 base;
  11681. u32 size;
  11682. } cursor[I915_MAX_PIPES];
  11683. struct intel_pipe_error_state {
  11684. bool power_domain_on;
  11685. u32 source;
  11686. u32 stat;
  11687. } pipe[I915_MAX_PIPES];
  11688. struct intel_plane_error_state {
  11689. u32 control;
  11690. u32 stride;
  11691. u32 size;
  11692. u32 pos;
  11693. u32 addr;
  11694. u32 surface;
  11695. u32 tile_offset;
  11696. } plane[I915_MAX_PIPES];
  11697. struct intel_transcoder_error_state {
  11698. bool power_domain_on;
  11699. enum transcoder cpu_transcoder;
  11700. u32 conf;
  11701. u32 htotal;
  11702. u32 hblank;
  11703. u32 hsync;
  11704. u32 vtotal;
  11705. u32 vblank;
  11706. u32 vsync;
  11707. } transcoder[4];
  11708. };
  11709. struct intel_display_error_state *
  11710. intel_display_capture_error_state(struct drm_device *dev)
  11711. {
  11712. struct drm_i915_private *dev_priv = dev->dev_private;
  11713. struct intel_display_error_state *error;
  11714. int transcoders[] = {
  11715. TRANSCODER_A,
  11716. TRANSCODER_B,
  11717. TRANSCODER_C,
  11718. TRANSCODER_EDP,
  11719. };
  11720. int i;
  11721. if (INTEL_INFO(dev)->num_pipes == 0)
  11722. return NULL;
  11723. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  11724. if (error == NULL)
  11725. return NULL;
  11726. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11727. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  11728. for_each_pipe(dev_priv, i) {
  11729. error->pipe[i].power_domain_on =
  11730. __intel_display_power_is_enabled(dev_priv,
  11731. POWER_DOMAIN_PIPE(i));
  11732. if (!error->pipe[i].power_domain_on)
  11733. continue;
  11734. error->cursor[i].control = I915_READ(CURCNTR(i));
  11735. error->cursor[i].position = I915_READ(CURPOS(i));
  11736. error->cursor[i].base = I915_READ(CURBASE(i));
  11737. error->plane[i].control = I915_READ(DSPCNTR(i));
  11738. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  11739. if (INTEL_INFO(dev)->gen <= 3) {
  11740. error->plane[i].size = I915_READ(DSPSIZE(i));
  11741. error->plane[i].pos = I915_READ(DSPPOS(i));
  11742. }
  11743. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11744. error->plane[i].addr = I915_READ(DSPADDR(i));
  11745. if (INTEL_INFO(dev)->gen >= 4) {
  11746. error->plane[i].surface = I915_READ(DSPSURF(i));
  11747. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  11748. }
  11749. error->pipe[i].source = I915_READ(PIPESRC(i));
  11750. if (HAS_GMCH_DISPLAY(dev))
  11751. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  11752. }
  11753. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  11754. if (HAS_DDI(dev_priv->dev))
  11755. error->num_transcoders++; /* Account for eDP. */
  11756. for (i = 0; i < error->num_transcoders; i++) {
  11757. enum transcoder cpu_transcoder = transcoders[i];
  11758. error->transcoder[i].power_domain_on =
  11759. __intel_display_power_is_enabled(dev_priv,
  11760. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  11761. if (!error->transcoder[i].power_domain_on)
  11762. continue;
  11763. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  11764. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  11765. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  11766. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  11767. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  11768. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  11769. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  11770. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  11771. }
  11772. return error;
  11773. }
  11774. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  11775. void
  11776. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  11777. struct drm_device *dev,
  11778. struct intel_display_error_state *error)
  11779. {
  11780. struct drm_i915_private *dev_priv = dev->dev_private;
  11781. int i;
  11782. if (!error)
  11783. return;
  11784. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  11785. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11786. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  11787. error->power_well_driver);
  11788. for_each_pipe(dev_priv, i) {
  11789. err_printf(m, "Pipe [%d]:\n", i);
  11790. err_printf(m, " Power: %s\n",
  11791. error->pipe[i].power_domain_on ? "on" : "off");
  11792. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  11793. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  11794. err_printf(m, "Plane [%d]:\n", i);
  11795. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  11796. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  11797. if (INTEL_INFO(dev)->gen <= 3) {
  11798. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  11799. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  11800. }
  11801. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11802. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  11803. if (INTEL_INFO(dev)->gen >= 4) {
  11804. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  11805. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  11806. }
  11807. err_printf(m, "Cursor [%d]:\n", i);
  11808. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  11809. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  11810. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  11811. }
  11812. for (i = 0; i < error->num_transcoders; i++) {
  11813. err_printf(m, "CPU transcoder: %c\n",
  11814. transcoder_name(error->transcoder[i].cpu_transcoder));
  11815. err_printf(m, " Power: %s\n",
  11816. error->transcoder[i].power_domain_on ? "on" : "off");
  11817. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  11818. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  11819. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  11820. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  11821. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  11822. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  11823. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  11824. }
  11825. }
  11826. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  11827. {
  11828. struct intel_crtc *crtc;
  11829. for_each_intel_crtc(dev, crtc) {
  11830. struct intel_unpin_work *work;
  11831. spin_lock_irq(&dev->event_lock);
  11832. work = crtc->unpin_work;
  11833. if (work && work->event &&
  11834. work->event->base.file_priv == file) {
  11835. kfree(work->event);
  11836. work->event = NULL;
  11837. }
  11838. spin_unlock_irq(&dev->event_lock);
  11839. }
  11840. }