intel_display.c 304 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include <drm/i915_drm.h>
  37. #include "i915_drv.h"
  38. #include "i915_trace.h"
  39. #include <drm/drm_dp_helper.h>
  40. #include <drm/drm_crtc_helper.h>
  41. #include <linux/dma_remapping.h>
  42. static void intel_increase_pllclock(struct drm_crtc *crtc);
  43. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  44. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  45. struct intel_crtc_config *pipe_config);
  46. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  47. struct intel_crtc_config *pipe_config);
  48. static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
  49. int x, int y, struct drm_framebuffer *old_fb);
  50. typedef struct {
  51. int min, max;
  52. } intel_range_t;
  53. typedef struct {
  54. int dot_limit;
  55. int p2_slow, p2_fast;
  56. } intel_p2_t;
  57. typedef struct intel_limit intel_limit_t;
  58. struct intel_limit {
  59. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  60. intel_p2_t p2;
  61. };
  62. int
  63. intel_pch_rawclk(struct drm_device *dev)
  64. {
  65. struct drm_i915_private *dev_priv = dev->dev_private;
  66. WARN_ON(!HAS_PCH_SPLIT(dev));
  67. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  68. }
  69. static inline u32 /* units of 100MHz */
  70. intel_fdi_link_freq(struct drm_device *dev)
  71. {
  72. if (IS_GEN5(dev)) {
  73. struct drm_i915_private *dev_priv = dev->dev_private;
  74. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  75. } else
  76. return 27;
  77. }
  78. static const intel_limit_t intel_limits_i8xx_dac = {
  79. .dot = { .min = 25000, .max = 350000 },
  80. .vco = { .min = 930000, .max = 1400000 },
  81. .n = { .min = 3, .max = 16 },
  82. .m = { .min = 96, .max = 140 },
  83. .m1 = { .min = 18, .max = 26 },
  84. .m2 = { .min = 6, .max = 16 },
  85. .p = { .min = 4, .max = 128 },
  86. .p1 = { .min = 2, .max = 33 },
  87. .p2 = { .dot_limit = 165000,
  88. .p2_slow = 4, .p2_fast = 2 },
  89. };
  90. static const intel_limit_t intel_limits_i8xx_dvo = {
  91. .dot = { .min = 25000, .max = 350000 },
  92. .vco = { .min = 930000, .max = 1400000 },
  93. .n = { .min = 3, .max = 16 },
  94. .m = { .min = 96, .max = 140 },
  95. .m1 = { .min = 18, .max = 26 },
  96. .m2 = { .min = 6, .max = 16 },
  97. .p = { .min = 4, .max = 128 },
  98. .p1 = { .min = 2, .max = 33 },
  99. .p2 = { .dot_limit = 165000,
  100. .p2_slow = 4, .p2_fast = 4 },
  101. };
  102. static const intel_limit_t intel_limits_i8xx_lvds = {
  103. .dot = { .min = 25000, .max = 350000 },
  104. .vco = { .min = 930000, .max = 1400000 },
  105. .n = { .min = 3, .max = 16 },
  106. .m = { .min = 96, .max = 140 },
  107. .m1 = { .min = 18, .max = 26 },
  108. .m2 = { .min = 6, .max = 16 },
  109. .p = { .min = 4, .max = 128 },
  110. .p1 = { .min = 1, .max = 6 },
  111. .p2 = { .dot_limit = 165000,
  112. .p2_slow = 14, .p2_fast = 7 },
  113. };
  114. static const intel_limit_t intel_limits_i9xx_sdvo = {
  115. .dot = { .min = 20000, .max = 400000 },
  116. .vco = { .min = 1400000, .max = 2800000 },
  117. .n = { .min = 1, .max = 6 },
  118. .m = { .min = 70, .max = 120 },
  119. .m1 = { .min = 8, .max = 18 },
  120. .m2 = { .min = 3, .max = 7 },
  121. .p = { .min = 5, .max = 80 },
  122. .p1 = { .min = 1, .max = 8 },
  123. .p2 = { .dot_limit = 200000,
  124. .p2_slow = 10, .p2_fast = 5 },
  125. };
  126. static const intel_limit_t intel_limits_i9xx_lvds = {
  127. .dot = { .min = 20000, .max = 400000 },
  128. .vco = { .min = 1400000, .max = 2800000 },
  129. .n = { .min = 1, .max = 6 },
  130. .m = { .min = 70, .max = 120 },
  131. .m1 = { .min = 8, .max = 18 },
  132. .m2 = { .min = 3, .max = 7 },
  133. .p = { .min = 7, .max = 98 },
  134. .p1 = { .min = 1, .max = 8 },
  135. .p2 = { .dot_limit = 112000,
  136. .p2_slow = 14, .p2_fast = 7 },
  137. };
  138. static const intel_limit_t intel_limits_g4x_sdvo = {
  139. .dot = { .min = 25000, .max = 270000 },
  140. .vco = { .min = 1750000, .max = 3500000},
  141. .n = { .min = 1, .max = 4 },
  142. .m = { .min = 104, .max = 138 },
  143. .m1 = { .min = 17, .max = 23 },
  144. .m2 = { .min = 5, .max = 11 },
  145. .p = { .min = 10, .max = 30 },
  146. .p1 = { .min = 1, .max = 3},
  147. .p2 = { .dot_limit = 270000,
  148. .p2_slow = 10,
  149. .p2_fast = 10
  150. },
  151. };
  152. static const intel_limit_t intel_limits_g4x_hdmi = {
  153. .dot = { .min = 22000, .max = 400000 },
  154. .vco = { .min = 1750000, .max = 3500000},
  155. .n = { .min = 1, .max = 4 },
  156. .m = { .min = 104, .max = 138 },
  157. .m1 = { .min = 16, .max = 23 },
  158. .m2 = { .min = 5, .max = 11 },
  159. .p = { .min = 5, .max = 80 },
  160. .p1 = { .min = 1, .max = 8},
  161. .p2 = { .dot_limit = 165000,
  162. .p2_slow = 10, .p2_fast = 5 },
  163. };
  164. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  165. .dot = { .min = 20000, .max = 115000 },
  166. .vco = { .min = 1750000, .max = 3500000 },
  167. .n = { .min = 1, .max = 3 },
  168. .m = { .min = 104, .max = 138 },
  169. .m1 = { .min = 17, .max = 23 },
  170. .m2 = { .min = 5, .max = 11 },
  171. .p = { .min = 28, .max = 112 },
  172. .p1 = { .min = 2, .max = 8 },
  173. .p2 = { .dot_limit = 0,
  174. .p2_slow = 14, .p2_fast = 14
  175. },
  176. };
  177. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  178. .dot = { .min = 80000, .max = 224000 },
  179. .vco = { .min = 1750000, .max = 3500000 },
  180. .n = { .min = 1, .max = 3 },
  181. .m = { .min = 104, .max = 138 },
  182. .m1 = { .min = 17, .max = 23 },
  183. .m2 = { .min = 5, .max = 11 },
  184. .p = { .min = 14, .max = 42 },
  185. .p1 = { .min = 2, .max = 6 },
  186. .p2 = { .dot_limit = 0,
  187. .p2_slow = 7, .p2_fast = 7
  188. },
  189. };
  190. static const intel_limit_t intel_limits_pineview_sdvo = {
  191. .dot = { .min = 20000, .max = 400000},
  192. .vco = { .min = 1700000, .max = 3500000 },
  193. /* Pineview's Ncounter is a ring counter */
  194. .n = { .min = 3, .max = 6 },
  195. .m = { .min = 2, .max = 256 },
  196. /* Pineview only has one combined m divider, which we treat as m2. */
  197. .m1 = { .min = 0, .max = 0 },
  198. .m2 = { .min = 0, .max = 254 },
  199. .p = { .min = 5, .max = 80 },
  200. .p1 = { .min = 1, .max = 8 },
  201. .p2 = { .dot_limit = 200000,
  202. .p2_slow = 10, .p2_fast = 5 },
  203. };
  204. static const intel_limit_t intel_limits_pineview_lvds = {
  205. .dot = { .min = 20000, .max = 400000 },
  206. .vco = { .min = 1700000, .max = 3500000 },
  207. .n = { .min = 3, .max = 6 },
  208. .m = { .min = 2, .max = 256 },
  209. .m1 = { .min = 0, .max = 0 },
  210. .m2 = { .min = 0, .max = 254 },
  211. .p = { .min = 7, .max = 112 },
  212. .p1 = { .min = 1, .max = 8 },
  213. .p2 = { .dot_limit = 112000,
  214. .p2_slow = 14, .p2_fast = 14 },
  215. };
  216. /* Ironlake / Sandybridge
  217. *
  218. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  219. * the range value for them is (actual_value - 2).
  220. */
  221. static const intel_limit_t intel_limits_ironlake_dac = {
  222. .dot = { .min = 25000, .max = 350000 },
  223. .vco = { .min = 1760000, .max = 3510000 },
  224. .n = { .min = 1, .max = 5 },
  225. .m = { .min = 79, .max = 127 },
  226. .m1 = { .min = 12, .max = 22 },
  227. .m2 = { .min = 5, .max = 9 },
  228. .p = { .min = 5, .max = 80 },
  229. .p1 = { .min = 1, .max = 8 },
  230. .p2 = { .dot_limit = 225000,
  231. .p2_slow = 10, .p2_fast = 5 },
  232. };
  233. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  234. .dot = { .min = 25000, .max = 350000 },
  235. .vco = { .min = 1760000, .max = 3510000 },
  236. .n = { .min = 1, .max = 3 },
  237. .m = { .min = 79, .max = 118 },
  238. .m1 = { .min = 12, .max = 22 },
  239. .m2 = { .min = 5, .max = 9 },
  240. .p = { .min = 28, .max = 112 },
  241. .p1 = { .min = 2, .max = 8 },
  242. .p2 = { .dot_limit = 225000,
  243. .p2_slow = 14, .p2_fast = 14 },
  244. };
  245. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  246. .dot = { .min = 25000, .max = 350000 },
  247. .vco = { .min = 1760000, .max = 3510000 },
  248. .n = { .min = 1, .max = 3 },
  249. .m = { .min = 79, .max = 127 },
  250. .m1 = { .min = 12, .max = 22 },
  251. .m2 = { .min = 5, .max = 9 },
  252. .p = { .min = 14, .max = 56 },
  253. .p1 = { .min = 2, .max = 8 },
  254. .p2 = { .dot_limit = 225000,
  255. .p2_slow = 7, .p2_fast = 7 },
  256. };
  257. /* LVDS 100mhz refclk limits. */
  258. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  259. .dot = { .min = 25000, .max = 350000 },
  260. .vco = { .min = 1760000, .max = 3510000 },
  261. .n = { .min = 1, .max = 2 },
  262. .m = { .min = 79, .max = 126 },
  263. .m1 = { .min = 12, .max = 22 },
  264. .m2 = { .min = 5, .max = 9 },
  265. .p = { .min = 28, .max = 112 },
  266. .p1 = { .min = 2, .max = 8 },
  267. .p2 = { .dot_limit = 225000,
  268. .p2_slow = 14, .p2_fast = 14 },
  269. };
  270. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  271. .dot = { .min = 25000, .max = 350000 },
  272. .vco = { .min = 1760000, .max = 3510000 },
  273. .n = { .min = 1, .max = 3 },
  274. .m = { .min = 79, .max = 126 },
  275. .m1 = { .min = 12, .max = 22 },
  276. .m2 = { .min = 5, .max = 9 },
  277. .p = { .min = 14, .max = 42 },
  278. .p1 = { .min = 2, .max = 6 },
  279. .p2 = { .dot_limit = 225000,
  280. .p2_slow = 7, .p2_fast = 7 },
  281. };
  282. static const intel_limit_t intel_limits_vlv = {
  283. /*
  284. * These are the data rate limits (measured in fast clocks)
  285. * since those are the strictest limits we have. The fast
  286. * clock and actual rate limits are more relaxed, so checking
  287. * them would make no difference.
  288. */
  289. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  290. .vco = { .min = 4000000, .max = 6000000 },
  291. .n = { .min = 1, .max = 7 },
  292. .m1 = { .min = 2, .max = 3 },
  293. .m2 = { .min = 11, .max = 156 },
  294. .p1 = { .min = 2, .max = 3 },
  295. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  296. };
  297. static void vlv_clock(int refclk, intel_clock_t *clock)
  298. {
  299. clock->m = clock->m1 * clock->m2;
  300. clock->p = clock->p1 * clock->p2;
  301. clock->vco = refclk * clock->m / clock->n;
  302. clock->dot = clock->vco / clock->p;
  303. }
  304. /**
  305. * Returns whether any output on the specified pipe is of the specified type
  306. */
  307. static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
  308. {
  309. struct drm_device *dev = crtc->dev;
  310. struct intel_encoder *encoder;
  311. for_each_encoder_on_crtc(dev, crtc, encoder)
  312. if (encoder->type == type)
  313. return true;
  314. return false;
  315. }
  316. static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
  317. int refclk)
  318. {
  319. struct drm_device *dev = crtc->dev;
  320. const intel_limit_t *limit;
  321. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  322. if (intel_is_dual_link_lvds(dev)) {
  323. if (refclk == 100000)
  324. limit = &intel_limits_ironlake_dual_lvds_100m;
  325. else
  326. limit = &intel_limits_ironlake_dual_lvds;
  327. } else {
  328. if (refclk == 100000)
  329. limit = &intel_limits_ironlake_single_lvds_100m;
  330. else
  331. limit = &intel_limits_ironlake_single_lvds;
  332. }
  333. } else
  334. limit = &intel_limits_ironlake_dac;
  335. return limit;
  336. }
  337. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  338. {
  339. struct drm_device *dev = crtc->dev;
  340. const intel_limit_t *limit;
  341. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  342. if (intel_is_dual_link_lvds(dev))
  343. limit = &intel_limits_g4x_dual_channel_lvds;
  344. else
  345. limit = &intel_limits_g4x_single_channel_lvds;
  346. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  347. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  348. limit = &intel_limits_g4x_hdmi;
  349. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  350. limit = &intel_limits_g4x_sdvo;
  351. } else /* The option is for other outputs */
  352. limit = &intel_limits_i9xx_sdvo;
  353. return limit;
  354. }
  355. static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
  356. {
  357. struct drm_device *dev = crtc->dev;
  358. const intel_limit_t *limit;
  359. if (HAS_PCH_SPLIT(dev))
  360. limit = intel_ironlake_limit(crtc, refclk);
  361. else if (IS_G4X(dev)) {
  362. limit = intel_g4x_limit(crtc);
  363. } else if (IS_PINEVIEW(dev)) {
  364. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  365. limit = &intel_limits_pineview_lvds;
  366. else
  367. limit = &intel_limits_pineview_sdvo;
  368. } else if (IS_VALLEYVIEW(dev)) {
  369. limit = &intel_limits_vlv;
  370. } else if (!IS_GEN2(dev)) {
  371. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  372. limit = &intel_limits_i9xx_lvds;
  373. else
  374. limit = &intel_limits_i9xx_sdvo;
  375. } else {
  376. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  377. limit = &intel_limits_i8xx_lvds;
  378. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
  379. limit = &intel_limits_i8xx_dvo;
  380. else
  381. limit = &intel_limits_i8xx_dac;
  382. }
  383. return limit;
  384. }
  385. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  386. static void pineview_clock(int refclk, intel_clock_t *clock)
  387. {
  388. clock->m = clock->m2 + 2;
  389. clock->p = clock->p1 * clock->p2;
  390. clock->vco = refclk * clock->m / clock->n;
  391. clock->dot = clock->vco / clock->p;
  392. }
  393. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  394. {
  395. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  396. }
  397. static void i9xx_clock(int refclk, intel_clock_t *clock)
  398. {
  399. clock->m = i9xx_dpll_compute_m(clock);
  400. clock->p = clock->p1 * clock->p2;
  401. clock->vco = refclk * clock->m / (clock->n + 2);
  402. clock->dot = clock->vco / clock->p;
  403. }
  404. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  405. /**
  406. * Returns whether the given set of divisors are valid for a given refclk with
  407. * the given connectors.
  408. */
  409. static bool intel_PLL_is_valid(struct drm_device *dev,
  410. const intel_limit_t *limit,
  411. const intel_clock_t *clock)
  412. {
  413. if (clock->n < limit->n.min || limit->n.max < clock->n)
  414. INTELPllInvalid("n out of range\n");
  415. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  416. INTELPllInvalid("p1 out of range\n");
  417. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  418. INTELPllInvalid("m2 out of range\n");
  419. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  420. INTELPllInvalid("m1 out of range\n");
  421. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
  422. if (clock->m1 <= clock->m2)
  423. INTELPllInvalid("m1 <= m2\n");
  424. if (!IS_VALLEYVIEW(dev)) {
  425. if (clock->p < limit->p.min || limit->p.max < clock->p)
  426. INTELPllInvalid("p out of range\n");
  427. if (clock->m < limit->m.min || limit->m.max < clock->m)
  428. INTELPllInvalid("m out of range\n");
  429. }
  430. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  431. INTELPllInvalid("vco out of range\n");
  432. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  433. * connector, etc., rather than just a single range.
  434. */
  435. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  436. INTELPllInvalid("dot out of range\n");
  437. return true;
  438. }
  439. static bool
  440. i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  441. int target, int refclk, intel_clock_t *match_clock,
  442. intel_clock_t *best_clock)
  443. {
  444. struct drm_device *dev = crtc->dev;
  445. intel_clock_t clock;
  446. int err = target;
  447. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  448. /*
  449. * For LVDS just rely on its current settings for dual-channel.
  450. * We haven't figured out how to reliably set up different
  451. * single/dual channel state, if we even can.
  452. */
  453. if (intel_is_dual_link_lvds(dev))
  454. clock.p2 = limit->p2.p2_fast;
  455. else
  456. clock.p2 = limit->p2.p2_slow;
  457. } else {
  458. if (target < limit->p2.dot_limit)
  459. clock.p2 = limit->p2.p2_slow;
  460. else
  461. clock.p2 = limit->p2.p2_fast;
  462. }
  463. memset(best_clock, 0, sizeof(*best_clock));
  464. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  465. clock.m1++) {
  466. for (clock.m2 = limit->m2.min;
  467. clock.m2 <= limit->m2.max; clock.m2++) {
  468. if (clock.m2 >= clock.m1)
  469. break;
  470. for (clock.n = limit->n.min;
  471. clock.n <= limit->n.max; clock.n++) {
  472. for (clock.p1 = limit->p1.min;
  473. clock.p1 <= limit->p1.max; clock.p1++) {
  474. int this_err;
  475. i9xx_clock(refclk, &clock);
  476. if (!intel_PLL_is_valid(dev, limit,
  477. &clock))
  478. continue;
  479. if (match_clock &&
  480. clock.p != match_clock->p)
  481. continue;
  482. this_err = abs(clock.dot - target);
  483. if (this_err < err) {
  484. *best_clock = clock;
  485. err = this_err;
  486. }
  487. }
  488. }
  489. }
  490. }
  491. return (err != target);
  492. }
  493. static bool
  494. pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  495. int target, int refclk, intel_clock_t *match_clock,
  496. intel_clock_t *best_clock)
  497. {
  498. struct drm_device *dev = crtc->dev;
  499. intel_clock_t clock;
  500. int err = target;
  501. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  502. /*
  503. * For LVDS just rely on its current settings for dual-channel.
  504. * We haven't figured out how to reliably set up different
  505. * single/dual channel state, if we even can.
  506. */
  507. if (intel_is_dual_link_lvds(dev))
  508. clock.p2 = limit->p2.p2_fast;
  509. else
  510. clock.p2 = limit->p2.p2_slow;
  511. } else {
  512. if (target < limit->p2.dot_limit)
  513. clock.p2 = limit->p2.p2_slow;
  514. else
  515. clock.p2 = limit->p2.p2_fast;
  516. }
  517. memset(best_clock, 0, sizeof(*best_clock));
  518. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  519. clock.m1++) {
  520. for (clock.m2 = limit->m2.min;
  521. clock.m2 <= limit->m2.max; clock.m2++) {
  522. for (clock.n = limit->n.min;
  523. clock.n <= limit->n.max; clock.n++) {
  524. for (clock.p1 = limit->p1.min;
  525. clock.p1 <= limit->p1.max; clock.p1++) {
  526. int this_err;
  527. pineview_clock(refclk, &clock);
  528. if (!intel_PLL_is_valid(dev, limit,
  529. &clock))
  530. continue;
  531. if (match_clock &&
  532. clock.p != match_clock->p)
  533. continue;
  534. this_err = abs(clock.dot - target);
  535. if (this_err < err) {
  536. *best_clock = clock;
  537. err = this_err;
  538. }
  539. }
  540. }
  541. }
  542. }
  543. return (err != target);
  544. }
  545. static bool
  546. g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  547. int target, int refclk, intel_clock_t *match_clock,
  548. intel_clock_t *best_clock)
  549. {
  550. struct drm_device *dev = crtc->dev;
  551. intel_clock_t clock;
  552. int max_n;
  553. bool found;
  554. /* approximately equals target * 0.00585 */
  555. int err_most = (target >> 8) + (target >> 9);
  556. found = false;
  557. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  558. if (intel_is_dual_link_lvds(dev))
  559. clock.p2 = limit->p2.p2_fast;
  560. else
  561. clock.p2 = limit->p2.p2_slow;
  562. } else {
  563. if (target < limit->p2.dot_limit)
  564. clock.p2 = limit->p2.p2_slow;
  565. else
  566. clock.p2 = limit->p2.p2_fast;
  567. }
  568. memset(best_clock, 0, sizeof(*best_clock));
  569. max_n = limit->n.max;
  570. /* based on hardware requirement, prefer smaller n to precision */
  571. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  572. /* based on hardware requirement, prefere larger m1,m2 */
  573. for (clock.m1 = limit->m1.max;
  574. clock.m1 >= limit->m1.min; clock.m1--) {
  575. for (clock.m2 = limit->m2.max;
  576. clock.m2 >= limit->m2.min; clock.m2--) {
  577. for (clock.p1 = limit->p1.max;
  578. clock.p1 >= limit->p1.min; clock.p1--) {
  579. int this_err;
  580. i9xx_clock(refclk, &clock);
  581. if (!intel_PLL_is_valid(dev, limit,
  582. &clock))
  583. continue;
  584. this_err = abs(clock.dot - target);
  585. if (this_err < err_most) {
  586. *best_clock = clock;
  587. err_most = this_err;
  588. max_n = clock.n;
  589. found = true;
  590. }
  591. }
  592. }
  593. }
  594. }
  595. return found;
  596. }
  597. static bool
  598. vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  599. int target, int refclk, intel_clock_t *match_clock,
  600. intel_clock_t *best_clock)
  601. {
  602. struct drm_device *dev = crtc->dev;
  603. intel_clock_t clock;
  604. unsigned int bestppm = 1000000;
  605. /* min update 19.2 MHz */
  606. int max_n = min(limit->n.max, refclk / 19200);
  607. bool found = false;
  608. target *= 5; /* fast clock */
  609. memset(best_clock, 0, sizeof(*best_clock));
  610. /* based on hardware requirement, prefer smaller n to precision */
  611. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  612. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  613. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  614. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  615. clock.p = clock.p1 * clock.p2;
  616. /* based on hardware requirement, prefer bigger m1,m2 values */
  617. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  618. unsigned int ppm, diff;
  619. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  620. refclk * clock.m1);
  621. vlv_clock(refclk, &clock);
  622. if (!intel_PLL_is_valid(dev, limit,
  623. &clock))
  624. continue;
  625. diff = abs(clock.dot - target);
  626. ppm = div_u64(1000000ULL * diff, target);
  627. if (ppm < 100 && clock.p > best_clock->p) {
  628. bestppm = 0;
  629. *best_clock = clock;
  630. found = true;
  631. }
  632. if (bestppm >= 10 && ppm < bestppm - 10) {
  633. bestppm = ppm;
  634. *best_clock = clock;
  635. found = true;
  636. }
  637. }
  638. }
  639. }
  640. }
  641. return found;
  642. }
  643. bool intel_crtc_active(struct drm_crtc *crtc)
  644. {
  645. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  646. /* Be paranoid as we can arrive here with only partial
  647. * state retrieved from the hardware during setup.
  648. *
  649. * We can ditch the adjusted_mode.crtc_clock check as soon
  650. * as Haswell has gained clock readout/fastboot support.
  651. *
  652. * We can ditch the crtc->fb check as soon as we can
  653. * properly reconstruct framebuffers.
  654. */
  655. return intel_crtc->active && crtc->fb &&
  656. intel_crtc->config.adjusted_mode.crtc_clock;
  657. }
  658. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  659. enum pipe pipe)
  660. {
  661. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  662. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  663. return intel_crtc->config.cpu_transcoder;
  664. }
  665. static void ironlake_wait_for_vblank(struct drm_device *dev, int pipe)
  666. {
  667. struct drm_i915_private *dev_priv = dev->dev_private;
  668. u32 frame, frame_reg = PIPEFRAME(pipe);
  669. frame = I915_READ(frame_reg);
  670. if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
  671. DRM_DEBUG_KMS("vblank wait timed out\n");
  672. }
  673. /**
  674. * intel_wait_for_vblank - wait for vblank on a given pipe
  675. * @dev: drm device
  676. * @pipe: pipe to wait for
  677. *
  678. * Wait for vblank to occur on a given pipe. Needed for various bits of
  679. * mode setting code.
  680. */
  681. void intel_wait_for_vblank(struct drm_device *dev, int pipe)
  682. {
  683. struct drm_i915_private *dev_priv = dev->dev_private;
  684. int pipestat_reg = PIPESTAT(pipe);
  685. if (INTEL_INFO(dev)->gen >= 5) {
  686. ironlake_wait_for_vblank(dev, pipe);
  687. return;
  688. }
  689. /* Clear existing vblank status. Note this will clear any other
  690. * sticky status fields as well.
  691. *
  692. * This races with i915_driver_irq_handler() with the result
  693. * that either function could miss a vblank event. Here it is not
  694. * fatal, as we will either wait upon the next vblank interrupt or
  695. * timeout. Generally speaking intel_wait_for_vblank() is only
  696. * called during modeset at which time the GPU should be idle and
  697. * should *not* be performing page flips and thus not waiting on
  698. * vblanks...
  699. * Currently, the result of us stealing a vblank from the irq
  700. * handler is that a single frame will be skipped during swapbuffers.
  701. */
  702. I915_WRITE(pipestat_reg,
  703. I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
  704. /* Wait for vblank interrupt bit to set */
  705. if (wait_for(I915_READ(pipestat_reg) &
  706. PIPE_VBLANK_INTERRUPT_STATUS,
  707. 50))
  708. DRM_DEBUG_KMS("vblank wait timed out\n");
  709. }
  710. /*
  711. * intel_wait_for_pipe_off - wait for pipe to turn off
  712. * @dev: drm device
  713. * @pipe: pipe to wait for
  714. *
  715. * After disabling a pipe, we can't wait for vblank in the usual way,
  716. * spinning on the vblank interrupt status bit, since we won't actually
  717. * see an interrupt when the pipe is disabled.
  718. *
  719. * On Gen4 and above:
  720. * wait for the pipe register state bit to turn off
  721. *
  722. * Otherwise:
  723. * wait for the display line value to settle (it usually
  724. * ends up stopping at the start of the next frame).
  725. *
  726. */
  727. void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
  728. {
  729. struct drm_i915_private *dev_priv = dev->dev_private;
  730. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  731. pipe);
  732. if (INTEL_INFO(dev)->gen >= 4) {
  733. int reg = PIPECONF(cpu_transcoder);
  734. /* Wait for the Pipe State to go off */
  735. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  736. 100))
  737. WARN(1, "pipe_off wait timed out\n");
  738. } else {
  739. u32 last_line, line_mask;
  740. int reg = PIPEDSL(pipe);
  741. unsigned long timeout = jiffies + msecs_to_jiffies(100);
  742. if (IS_GEN2(dev))
  743. line_mask = DSL_LINEMASK_GEN2;
  744. else
  745. line_mask = DSL_LINEMASK_GEN3;
  746. /* Wait for the display line to settle */
  747. do {
  748. last_line = I915_READ(reg) & line_mask;
  749. mdelay(5);
  750. } while (((I915_READ(reg) & line_mask) != last_line) &&
  751. time_after(timeout, jiffies));
  752. if (time_after(jiffies, timeout))
  753. WARN(1, "pipe_off wait timed out\n");
  754. }
  755. }
  756. /*
  757. * ibx_digital_port_connected - is the specified port connected?
  758. * @dev_priv: i915 private structure
  759. * @port: the port to test
  760. *
  761. * Returns true if @port is connected, false otherwise.
  762. */
  763. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  764. struct intel_digital_port *port)
  765. {
  766. u32 bit;
  767. if (HAS_PCH_IBX(dev_priv->dev)) {
  768. switch(port->port) {
  769. case PORT_B:
  770. bit = SDE_PORTB_HOTPLUG;
  771. break;
  772. case PORT_C:
  773. bit = SDE_PORTC_HOTPLUG;
  774. break;
  775. case PORT_D:
  776. bit = SDE_PORTD_HOTPLUG;
  777. break;
  778. default:
  779. return true;
  780. }
  781. } else {
  782. switch(port->port) {
  783. case PORT_B:
  784. bit = SDE_PORTB_HOTPLUG_CPT;
  785. break;
  786. case PORT_C:
  787. bit = SDE_PORTC_HOTPLUG_CPT;
  788. break;
  789. case PORT_D:
  790. bit = SDE_PORTD_HOTPLUG_CPT;
  791. break;
  792. default:
  793. return true;
  794. }
  795. }
  796. return I915_READ(SDEISR) & bit;
  797. }
  798. static const char *state_string(bool enabled)
  799. {
  800. return enabled ? "on" : "off";
  801. }
  802. /* Only for pre-ILK configs */
  803. void assert_pll(struct drm_i915_private *dev_priv,
  804. enum pipe pipe, bool state)
  805. {
  806. int reg;
  807. u32 val;
  808. bool cur_state;
  809. reg = DPLL(pipe);
  810. val = I915_READ(reg);
  811. cur_state = !!(val & DPLL_VCO_ENABLE);
  812. WARN(cur_state != state,
  813. "PLL state assertion failure (expected %s, current %s)\n",
  814. state_string(state), state_string(cur_state));
  815. }
  816. /* XXX: the dsi pll is shared between MIPI DSI ports */
  817. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  818. {
  819. u32 val;
  820. bool cur_state;
  821. mutex_lock(&dev_priv->dpio_lock);
  822. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  823. mutex_unlock(&dev_priv->dpio_lock);
  824. cur_state = val & DSI_PLL_VCO_EN;
  825. WARN(cur_state != state,
  826. "DSI PLL state assertion failure (expected %s, current %s)\n",
  827. state_string(state), state_string(cur_state));
  828. }
  829. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  830. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  831. struct intel_shared_dpll *
  832. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  833. {
  834. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  835. if (crtc->config.shared_dpll < 0)
  836. return NULL;
  837. return &dev_priv->shared_dplls[crtc->config.shared_dpll];
  838. }
  839. /* For ILK+ */
  840. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  841. struct intel_shared_dpll *pll,
  842. bool state)
  843. {
  844. bool cur_state;
  845. struct intel_dpll_hw_state hw_state;
  846. if (HAS_PCH_LPT(dev_priv->dev)) {
  847. DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
  848. return;
  849. }
  850. if (WARN (!pll,
  851. "asserting DPLL %s with no DPLL\n", state_string(state)))
  852. return;
  853. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  854. WARN(cur_state != state,
  855. "%s assertion failure (expected %s, current %s)\n",
  856. pll->name, state_string(state), state_string(cur_state));
  857. }
  858. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  859. enum pipe pipe, bool state)
  860. {
  861. int reg;
  862. u32 val;
  863. bool cur_state;
  864. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  865. pipe);
  866. if (HAS_DDI(dev_priv->dev)) {
  867. /* DDI does not have a specific FDI_TX register */
  868. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  869. val = I915_READ(reg);
  870. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  871. } else {
  872. reg = FDI_TX_CTL(pipe);
  873. val = I915_READ(reg);
  874. cur_state = !!(val & FDI_TX_ENABLE);
  875. }
  876. WARN(cur_state != state,
  877. "FDI TX state assertion failure (expected %s, current %s)\n",
  878. state_string(state), state_string(cur_state));
  879. }
  880. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  881. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  882. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  883. enum pipe pipe, bool state)
  884. {
  885. int reg;
  886. u32 val;
  887. bool cur_state;
  888. reg = FDI_RX_CTL(pipe);
  889. val = I915_READ(reg);
  890. cur_state = !!(val & FDI_RX_ENABLE);
  891. WARN(cur_state != state,
  892. "FDI RX state assertion failure (expected %s, current %s)\n",
  893. state_string(state), state_string(cur_state));
  894. }
  895. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  896. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  897. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  898. enum pipe pipe)
  899. {
  900. int reg;
  901. u32 val;
  902. /* ILK FDI PLL is always enabled */
  903. if (dev_priv->info->gen == 5)
  904. return;
  905. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  906. if (HAS_DDI(dev_priv->dev))
  907. return;
  908. reg = FDI_TX_CTL(pipe);
  909. val = I915_READ(reg);
  910. WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  911. }
  912. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  913. enum pipe pipe, bool state)
  914. {
  915. int reg;
  916. u32 val;
  917. bool cur_state;
  918. reg = FDI_RX_CTL(pipe);
  919. val = I915_READ(reg);
  920. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  921. WARN(cur_state != state,
  922. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  923. state_string(state), state_string(cur_state));
  924. }
  925. static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  926. enum pipe pipe)
  927. {
  928. int pp_reg, lvds_reg;
  929. u32 val;
  930. enum pipe panel_pipe = PIPE_A;
  931. bool locked = true;
  932. if (HAS_PCH_SPLIT(dev_priv->dev)) {
  933. pp_reg = PCH_PP_CONTROL;
  934. lvds_reg = PCH_LVDS;
  935. } else {
  936. pp_reg = PP_CONTROL;
  937. lvds_reg = LVDS;
  938. }
  939. val = I915_READ(pp_reg);
  940. if (!(val & PANEL_POWER_ON) ||
  941. ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
  942. locked = false;
  943. if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
  944. panel_pipe = PIPE_B;
  945. WARN(panel_pipe == pipe && locked,
  946. "panel assertion failure, pipe %c regs locked\n",
  947. pipe_name(pipe));
  948. }
  949. static void assert_cursor(struct drm_i915_private *dev_priv,
  950. enum pipe pipe, bool state)
  951. {
  952. struct drm_device *dev = dev_priv->dev;
  953. bool cur_state;
  954. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  955. cur_state = I915_READ(CURCNTR_IVB(pipe)) & CURSOR_MODE;
  956. else if (IS_845G(dev) || IS_I865G(dev))
  957. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  958. else
  959. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  960. WARN(cur_state != state,
  961. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  962. pipe_name(pipe), state_string(state), state_string(cur_state));
  963. }
  964. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  965. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  966. void assert_pipe(struct drm_i915_private *dev_priv,
  967. enum pipe pipe, bool state)
  968. {
  969. int reg;
  970. u32 val;
  971. bool cur_state;
  972. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  973. pipe);
  974. /* if we need the pipe A quirk it must be always on */
  975. if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
  976. state = true;
  977. if (!intel_display_power_enabled(dev_priv->dev,
  978. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  979. cur_state = false;
  980. } else {
  981. reg = PIPECONF(cpu_transcoder);
  982. val = I915_READ(reg);
  983. cur_state = !!(val & PIPECONF_ENABLE);
  984. }
  985. WARN(cur_state != state,
  986. "pipe %c assertion failure (expected %s, current %s)\n",
  987. pipe_name(pipe), state_string(state), state_string(cur_state));
  988. }
  989. static void assert_plane(struct drm_i915_private *dev_priv,
  990. enum plane plane, bool state)
  991. {
  992. int reg;
  993. u32 val;
  994. bool cur_state;
  995. reg = DSPCNTR(plane);
  996. val = I915_READ(reg);
  997. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  998. WARN(cur_state != state,
  999. "plane %c assertion failure (expected %s, current %s)\n",
  1000. plane_name(plane), state_string(state), state_string(cur_state));
  1001. }
  1002. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1003. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1004. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1005. enum pipe pipe)
  1006. {
  1007. struct drm_device *dev = dev_priv->dev;
  1008. int reg, i;
  1009. u32 val;
  1010. int cur_pipe;
  1011. /* Primary planes are fixed to pipes on gen4+ */
  1012. if (INTEL_INFO(dev)->gen >= 4) {
  1013. reg = DSPCNTR(pipe);
  1014. val = I915_READ(reg);
  1015. WARN((val & DISPLAY_PLANE_ENABLE),
  1016. "plane %c assertion failure, should be disabled but not\n",
  1017. plane_name(pipe));
  1018. return;
  1019. }
  1020. /* Need to check both planes against the pipe */
  1021. for_each_pipe(i) {
  1022. reg = DSPCNTR(i);
  1023. val = I915_READ(reg);
  1024. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1025. DISPPLANE_SEL_PIPE_SHIFT;
  1026. WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1027. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1028. plane_name(i), pipe_name(pipe));
  1029. }
  1030. }
  1031. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1032. enum pipe pipe)
  1033. {
  1034. struct drm_device *dev = dev_priv->dev;
  1035. int reg, i;
  1036. u32 val;
  1037. if (IS_VALLEYVIEW(dev)) {
  1038. for (i = 0; i < dev_priv->num_plane; i++) {
  1039. reg = SPCNTR(pipe, i);
  1040. val = I915_READ(reg);
  1041. WARN((val & SP_ENABLE),
  1042. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1043. sprite_name(pipe, i), pipe_name(pipe));
  1044. }
  1045. } else if (INTEL_INFO(dev)->gen >= 7) {
  1046. reg = SPRCTL(pipe);
  1047. val = I915_READ(reg);
  1048. WARN((val & SPRITE_ENABLE),
  1049. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1050. plane_name(pipe), pipe_name(pipe));
  1051. } else if (INTEL_INFO(dev)->gen >= 5) {
  1052. reg = DVSCNTR(pipe);
  1053. val = I915_READ(reg);
  1054. WARN((val & DVS_ENABLE),
  1055. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1056. plane_name(pipe), pipe_name(pipe));
  1057. }
  1058. }
  1059. static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1060. {
  1061. u32 val;
  1062. bool enabled;
  1063. if (HAS_PCH_LPT(dev_priv->dev)) {
  1064. DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
  1065. return;
  1066. }
  1067. val = I915_READ(PCH_DREF_CONTROL);
  1068. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1069. DREF_SUPERSPREAD_SOURCE_MASK));
  1070. WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1071. }
  1072. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1073. enum pipe pipe)
  1074. {
  1075. int reg;
  1076. u32 val;
  1077. bool enabled;
  1078. reg = PCH_TRANSCONF(pipe);
  1079. val = I915_READ(reg);
  1080. enabled = !!(val & TRANS_ENABLE);
  1081. WARN(enabled,
  1082. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1083. pipe_name(pipe));
  1084. }
  1085. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1086. enum pipe pipe, u32 port_sel, u32 val)
  1087. {
  1088. if ((val & DP_PORT_EN) == 0)
  1089. return false;
  1090. if (HAS_PCH_CPT(dev_priv->dev)) {
  1091. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1092. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1093. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1094. return false;
  1095. } else {
  1096. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1097. return false;
  1098. }
  1099. return true;
  1100. }
  1101. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1102. enum pipe pipe, u32 val)
  1103. {
  1104. if ((val & SDVO_ENABLE) == 0)
  1105. return false;
  1106. if (HAS_PCH_CPT(dev_priv->dev)) {
  1107. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1108. return false;
  1109. } else {
  1110. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1111. return false;
  1112. }
  1113. return true;
  1114. }
  1115. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1116. enum pipe pipe, u32 val)
  1117. {
  1118. if ((val & LVDS_PORT_EN) == 0)
  1119. return false;
  1120. if (HAS_PCH_CPT(dev_priv->dev)) {
  1121. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1122. return false;
  1123. } else {
  1124. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1125. return false;
  1126. }
  1127. return true;
  1128. }
  1129. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1130. enum pipe pipe, u32 val)
  1131. {
  1132. if ((val & ADPA_DAC_ENABLE) == 0)
  1133. return false;
  1134. if (HAS_PCH_CPT(dev_priv->dev)) {
  1135. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1136. return false;
  1137. } else {
  1138. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1139. return false;
  1140. }
  1141. return true;
  1142. }
  1143. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1144. enum pipe pipe, int reg, u32 port_sel)
  1145. {
  1146. u32 val = I915_READ(reg);
  1147. WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1148. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1149. reg, pipe_name(pipe));
  1150. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1151. && (val & DP_PIPEB_SELECT),
  1152. "IBX PCH dp port still using transcoder B\n");
  1153. }
  1154. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1155. enum pipe pipe, int reg)
  1156. {
  1157. u32 val = I915_READ(reg);
  1158. WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1159. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1160. reg, pipe_name(pipe));
  1161. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1162. && (val & SDVO_PIPE_B_SELECT),
  1163. "IBX PCH hdmi port still using transcoder B\n");
  1164. }
  1165. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1166. enum pipe pipe)
  1167. {
  1168. int reg;
  1169. u32 val;
  1170. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1171. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1172. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1173. reg = PCH_ADPA;
  1174. val = I915_READ(reg);
  1175. WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1176. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1177. pipe_name(pipe));
  1178. reg = PCH_LVDS;
  1179. val = I915_READ(reg);
  1180. WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1181. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1182. pipe_name(pipe));
  1183. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1184. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1185. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1186. }
  1187. static void intel_init_dpio(struct drm_device *dev)
  1188. {
  1189. struct drm_i915_private *dev_priv = dev->dev_private;
  1190. if (!IS_VALLEYVIEW(dev))
  1191. return;
  1192. /*
  1193. * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
  1194. * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
  1195. * a. GUnit 0x2110 bit[0] set to 1 (def 0)
  1196. * b. The other bits such as sfr settings / modesel may all be set
  1197. * to 0.
  1198. *
  1199. * This should only be done on init and resume from S3 with both
  1200. * PLLs disabled, or we risk losing DPIO and PLL synchronization.
  1201. */
  1202. I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
  1203. }
  1204. static void vlv_enable_pll(struct intel_crtc *crtc)
  1205. {
  1206. struct drm_device *dev = crtc->base.dev;
  1207. struct drm_i915_private *dev_priv = dev->dev_private;
  1208. int reg = DPLL(crtc->pipe);
  1209. u32 dpll = crtc->config.dpll_hw_state.dpll;
  1210. assert_pipe_disabled(dev_priv, crtc->pipe);
  1211. /* No really, not for ILK+ */
  1212. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1213. /* PLL is protected by panel, make sure we can write it */
  1214. if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
  1215. assert_panel_unlocked(dev_priv, crtc->pipe);
  1216. I915_WRITE(reg, dpll);
  1217. POSTING_READ(reg);
  1218. udelay(150);
  1219. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1220. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1221. I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
  1222. POSTING_READ(DPLL_MD(crtc->pipe));
  1223. /* We do this three times for luck */
  1224. I915_WRITE(reg, dpll);
  1225. POSTING_READ(reg);
  1226. udelay(150); /* wait for warmup */
  1227. I915_WRITE(reg, dpll);
  1228. POSTING_READ(reg);
  1229. udelay(150); /* wait for warmup */
  1230. I915_WRITE(reg, dpll);
  1231. POSTING_READ(reg);
  1232. udelay(150); /* wait for warmup */
  1233. }
  1234. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1235. {
  1236. struct drm_device *dev = crtc->base.dev;
  1237. struct drm_i915_private *dev_priv = dev->dev_private;
  1238. int reg = DPLL(crtc->pipe);
  1239. u32 dpll = crtc->config.dpll_hw_state.dpll;
  1240. assert_pipe_disabled(dev_priv, crtc->pipe);
  1241. /* No really, not for ILK+ */
  1242. BUG_ON(dev_priv->info->gen >= 5);
  1243. /* PLL is protected by panel, make sure we can write it */
  1244. if (IS_MOBILE(dev) && !IS_I830(dev))
  1245. assert_panel_unlocked(dev_priv, crtc->pipe);
  1246. I915_WRITE(reg, dpll);
  1247. /* Wait for the clocks to stabilize. */
  1248. POSTING_READ(reg);
  1249. udelay(150);
  1250. if (INTEL_INFO(dev)->gen >= 4) {
  1251. I915_WRITE(DPLL_MD(crtc->pipe),
  1252. crtc->config.dpll_hw_state.dpll_md);
  1253. } else {
  1254. /* The pixel multiplier can only be updated once the
  1255. * DPLL is enabled and the clocks are stable.
  1256. *
  1257. * So write it again.
  1258. */
  1259. I915_WRITE(reg, dpll);
  1260. }
  1261. /* We do this three times for luck */
  1262. I915_WRITE(reg, dpll);
  1263. POSTING_READ(reg);
  1264. udelay(150); /* wait for warmup */
  1265. I915_WRITE(reg, dpll);
  1266. POSTING_READ(reg);
  1267. udelay(150); /* wait for warmup */
  1268. I915_WRITE(reg, dpll);
  1269. POSTING_READ(reg);
  1270. udelay(150); /* wait for warmup */
  1271. }
  1272. /**
  1273. * i9xx_disable_pll - disable a PLL
  1274. * @dev_priv: i915 private structure
  1275. * @pipe: pipe PLL to disable
  1276. *
  1277. * Disable the PLL for @pipe, making sure the pipe is off first.
  1278. *
  1279. * Note! This is for pre-ILK only.
  1280. */
  1281. static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1282. {
  1283. /* Don't disable pipe A or pipe A PLLs if needed */
  1284. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1285. return;
  1286. /* Make sure the pipe isn't still relying on us */
  1287. assert_pipe_disabled(dev_priv, pipe);
  1288. I915_WRITE(DPLL(pipe), 0);
  1289. POSTING_READ(DPLL(pipe));
  1290. }
  1291. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1292. {
  1293. u32 val = 0;
  1294. /* Make sure the pipe isn't still relying on us */
  1295. assert_pipe_disabled(dev_priv, pipe);
  1296. /* Leave integrated clock source enabled */
  1297. if (pipe == PIPE_B)
  1298. val = DPLL_INTEGRATED_CRI_CLK_VLV;
  1299. I915_WRITE(DPLL(pipe), val);
  1300. POSTING_READ(DPLL(pipe));
  1301. }
  1302. void vlv_wait_port_ready(struct drm_i915_private *dev_priv, int port)
  1303. {
  1304. u32 port_mask;
  1305. if (!port)
  1306. port_mask = DPLL_PORTB_READY_MASK;
  1307. else
  1308. port_mask = DPLL_PORTC_READY_MASK;
  1309. if (wait_for((I915_READ(DPLL(0)) & port_mask) == 0, 1000))
  1310. WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
  1311. 'B' + port, I915_READ(DPLL(0)));
  1312. }
  1313. /**
  1314. * ironlake_enable_shared_dpll - enable PCH PLL
  1315. * @dev_priv: i915 private structure
  1316. * @pipe: pipe PLL to enable
  1317. *
  1318. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1319. * drives the transcoder clock.
  1320. */
  1321. static void ironlake_enable_shared_dpll(struct intel_crtc *crtc)
  1322. {
  1323. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1324. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1325. /* PCH PLLs only available on ILK, SNB and IVB */
  1326. BUG_ON(dev_priv->info->gen < 5);
  1327. if (WARN_ON(pll == NULL))
  1328. return;
  1329. if (WARN_ON(pll->refcount == 0))
  1330. return;
  1331. DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
  1332. pll->name, pll->active, pll->on,
  1333. crtc->base.base.id);
  1334. if (pll->active++) {
  1335. WARN_ON(!pll->on);
  1336. assert_shared_dpll_enabled(dev_priv, pll);
  1337. return;
  1338. }
  1339. WARN_ON(pll->on);
  1340. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1341. pll->enable(dev_priv, pll);
  1342. pll->on = true;
  1343. }
  1344. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1345. {
  1346. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1347. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1348. /* PCH only available on ILK+ */
  1349. BUG_ON(dev_priv->info->gen < 5);
  1350. if (WARN_ON(pll == NULL))
  1351. return;
  1352. if (WARN_ON(pll->refcount == 0))
  1353. return;
  1354. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1355. pll->name, pll->active, pll->on,
  1356. crtc->base.base.id);
  1357. if (WARN_ON(pll->active == 0)) {
  1358. assert_shared_dpll_disabled(dev_priv, pll);
  1359. return;
  1360. }
  1361. assert_shared_dpll_enabled(dev_priv, pll);
  1362. WARN_ON(!pll->on);
  1363. if (--pll->active)
  1364. return;
  1365. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1366. pll->disable(dev_priv, pll);
  1367. pll->on = false;
  1368. }
  1369. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1370. enum pipe pipe)
  1371. {
  1372. struct drm_device *dev = dev_priv->dev;
  1373. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1374. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1375. uint32_t reg, val, pipeconf_val;
  1376. /* PCH only available on ILK+ */
  1377. BUG_ON(dev_priv->info->gen < 5);
  1378. /* Make sure PCH DPLL is enabled */
  1379. assert_shared_dpll_enabled(dev_priv,
  1380. intel_crtc_to_shared_dpll(intel_crtc));
  1381. /* FDI must be feeding us bits for PCH ports */
  1382. assert_fdi_tx_enabled(dev_priv, pipe);
  1383. assert_fdi_rx_enabled(dev_priv, pipe);
  1384. if (HAS_PCH_CPT(dev)) {
  1385. /* Workaround: Set the timing override bit before enabling the
  1386. * pch transcoder. */
  1387. reg = TRANS_CHICKEN2(pipe);
  1388. val = I915_READ(reg);
  1389. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1390. I915_WRITE(reg, val);
  1391. }
  1392. reg = PCH_TRANSCONF(pipe);
  1393. val = I915_READ(reg);
  1394. pipeconf_val = I915_READ(PIPECONF(pipe));
  1395. if (HAS_PCH_IBX(dev_priv->dev)) {
  1396. /*
  1397. * make the BPC in transcoder be consistent with
  1398. * that in pipeconf reg.
  1399. */
  1400. val &= ~PIPECONF_BPC_MASK;
  1401. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1402. }
  1403. val &= ~TRANS_INTERLACE_MASK;
  1404. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1405. if (HAS_PCH_IBX(dev_priv->dev) &&
  1406. intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
  1407. val |= TRANS_LEGACY_INTERLACED_ILK;
  1408. else
  1409. val |= TRANS_INTERLACED;
  1410. else
  1411. val |= TRANS_PROGRESSIVE;
  1412. I915_WRITE(reg, val | TRANS_ENABLE);
  1413. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1414. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1415. }
  1416. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1417. enum transcoder cpu_transcoder)
  1418. {
  1419. u32 val, pipeconf_val;
  1420. /* PCH only available on ILK+ */
  1421. BUG_ON(dev_priv->info->gen < 5);
  1422. /* FDI must be feeding us bits for PCH ports */
  1423. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1424. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1425. /* Workaround: set timing override bit. */
  1426. val = I915_READ(_TRANSA_CHICKEN2);
  1427. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1428. I915_WRITE(_TRANSA_CHICKEN2, val);
  1429. val = TRANS_ENABLE;
  1430. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1431. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1432. PIPECONF_INTERLACED_ILK)
  1433. val |= TRANS_INTERLACED;
  1434. else
  1435. val |= TRANS_PROGRESSIVE;
  1436. I915_WRITE(LPT_TRANSCONF, val);
  1437. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1438. DRM_ERROR("Failed to enable PCH transcoder\n");
  1439. }
  1440. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1441. enum pipe pipe)
  1442. {
  1443. struct drm_device *dev = dev_priv->dev;
  1444. uint32_t reg, val;
  1445. /* FDI relies on the transcoder */
  1446. assert_fdi_tx_disabled(dev_priv, pipe);
  1447. assert_fdi_rx_disabled(dev_priv, pipe);
  1448. /* Ports must be off as well */
  1449. assert_pch_ports_disabled(dev_priv, pipe);
  1450. reg = PCH_TRANSCONF(pipe);
  1451. val = I915_READ(reg);
  1452. val &= ~TRANS_ENABLE;
  1453. I915_WRITE(reg, val);
  1454. /* wait for PCH transcoder off, transcoder state */
  1455. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1456. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1457. if (!HAS_PCH_IBX(dev)) {
  1458. /* Workaround: Clear the timing override chicken bit again. */
  1459. reg = TRANS_CHICKEN2(pipe);
  1460. val = I915_READ(reg);
  1461. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1462. I915_WRITE(reg, val);
  1463. }
  1464. }
  1465. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1466. {
  1467. u32 val;
  1468. val = I915_READ(LPT_TRANSCONF);
  1469. val &= ~TRANS_ENABLE;
  1470. I915_WRITE(LPT_TRANSCONF, val);
  1471. /* wait for PCH transcoder off, transcoder state */
  1472. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1473. DRM_ERROR("Failed to disable PCH transcoder\n");
  1474. /* Workaround: clear timing override bit. */
  1475. val = I915_READ(_TRANSA_CHICKEN2);
  1476. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1477. I915_WRITE(_TRANSA_CHICKEN2, val);
  1478. }
  1479. /**
  1480. * intel_enable_pipe - enable a pipe, asserting requirements
  1481. * @dev_priv: i915 private structure
  1482. * @pipe: pipe to enable
  1483. * @pch_port: on ILK+, is this pipe driving a PCH port or not
  1484. *
  1485. * Enable @pipe, making sure that various hardware specific requirements
  1486. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1487. *
  1488. * @pipe should be %PIPE_A or %PIPE_B.
  1489. *
  1490. * Will wait until the pipe is actually running (i.e. first vblank) before
  1491. * returning.
  1492. */
  1493. static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
  1494. bool pch_port, bool dsi)
  1495. {
  1496. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1497. pipe);
  1498. enum pipe pch_transcoder;
  1499. int reg;
  1500. u32 val;
  1501. assert_planes_disabled(dev_priv, pipe);
  1502. assert_cursor_disabled(dev_priv, pipe);
  1503. assert_sprites_disabled(dev_priv, pipe);
  1504. if (HAS_PCH_LPT(dev_priv->dev))
  1505. pch_transcoder = TRANSCODER_A;
  1506. else
  1507. pch_transcoder = pipe;
  1508. /*
  1509. * A pipe without a PLL won't actually be able to drive bits from
  1510. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1511. * need the check.
  1512. */
  1513. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1514. if (dsi)
  1515. assert_dsi_pll_enabled(dev_priv);
  1516. else
  1517. assert_pll_enabled(dev_priv, pipe);
  1518. else {
  1519. if (pch_port) {
  1520. /* if driving the PCH, we need FDI enabled */
  1521. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1522. assert_fdi_tx_pll_enabled(dev_priv,
  1523. (enum pipe) cpu_transcoder);
  1524. }
  1525. /* FIXME: assert CPU port conditions for SNB+ */
  1526. }
  1527. reg = PIPECONF(cpu_transcoder);
  1528. val = I915_READ(reg);
  1529. if (val & PIPECONF_ENABLE)
  1530. return;
  1531. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1532. intel_wait_for_vblank(dev_priv->dev, pipe);
  1533. }
  1534. /**
  1535. * intel_disable_pipe - disable a pipe, asserting requirements
  1536. * @dev_priv: i915 private structure
  1537. * @pipe: pipe to disable
  1538. *
  1539. * Disable @pipe, making sure that various hardware specific requirements
  1540. * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
  1541. *
  1542. * @pipe should be %PIPE_A or %PIPE_B.
  1543. *
  1544. * Will wait until the pipe has shut down before returning.
  1545. */
  1546. static void intel_disable_pipe(struct drm_i915_private *dev_priv,
  1547. enum pipe pipe)
  1548. {
  1549. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1550. pipe);
  1551. int reg;
  1552. u32 val;
  1553. /*
  1554. * Make sure planes won't keep trying to pump pixels to us,
  1555. * or we might hang the display.
  1556. */
  1557. assert_planes_disabled(dev_priv, pipe);
  1558. assert_cursor_disabled(dev_priv, pipe);
  1559. assert_sprites_disabled(dev_priv, pipe);
  1560. /* Don't disable pipe A or pipe A PLLs if needed */
  1561. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1562. return;
  1563. reg = PIPECONF(cpu_transcoder);
  1564. val = I915_READ(reg);
  1565. if ((val & PIPECONF_ENABLE) == 0)
  1566. return;
  1567. I915_WRITE(reg, val & ~PIPECONF_ENABLE);
  1568. intel_wait_for_pipe_off(dev_priv->dev, pipe);
  1569. }
  1570. /*
  1571. * Plane regs are double buffered, going from enabled->disabled needs a
  1572. * trigger in order to latch. The display address reg provides this.
  1573. */
  1574. void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
  1575. enum plane plane)
  1576. {
  1577. u32 reg = dev_priv->info->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
  1578. I915_WRITE(reg, I915_READ(reg));
  1579. POSTING_READ(reg);
  1580. }
  1581. /**
  1582. * intel_enable_primary_plane - enable the primary plane on a given pipe
  1583. * @dev_priv: i915 private structure
  1584. * @plane: plane to enable
  1585. * @pipe: pipe being fed
  1586. *
  1587. * Enable @plane on @pipe, making sure that @pipe is running first.
  1588. */
  1589. static void intel_enable_primary_plane(struct drm_i915_private *dev_priv,
  1590. enum plane plane, enum pipe pipe)
  1591. {
  1592. struct intel_crtc *intel_crtc =
  1593. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  1594. int reg;
  1595. u32 val;
  1596. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1597. assert_pipe_enabled(dev_priv, pipe);
  1598. WARN(!intel_crtc->primary_disabled, "Primary plane already enabled\n");
  1599. intel_crtc->primary_disabled = false;
  1600. reg = DSPCNTR(plane);
  1601. val = I915_READ(reg);
  1602. if (val & DISPLAY_PLANE_ENABLE)
  1603. return;
  1604. I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
  1605. intel_flush_primary_plane(dev_priv, plane);
  1606. intel_wait_for_vblank(dev_priv->dev, pipe);
  1607. }
  1608. /**
  1609. * intel_disable_primary_plane - disable the primary plane
  1610. * @dev_priv: i915 private structure
  1611. * @plane: plane to disable
  1612. * @pipe: pipe consuming the data
  1613. *
  1614. * Disable @plane; should be an independent operation.
  1615. */
  1616. static void intel_disable_primary_plane(struct drm_i915_private *dev_priv,
  1617. enum plane plane, enum pipe pipe)
  1618. {
  1619. struct intel_crtc *intel_crtc =
  1620. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  1621. int reg;
  1622. u32 val;
  1623. WARN(intel_crtc->primary_disabled, "Primary plane already disabled\n");
  1624. intel_crtc->primary_disabled = true;
  1625. reg = DSPCNTR(plane);
  1626. val = I915_READ(reg);
  1627. if ((val & DISPLAY_PLANE_ENABLE) == 0)
  1628. return;
  1629. I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
  1630. intel_flush_primary_plane(dev_priv, plane);
  1631. intel_wait_for_vblank(dev_priv->dev, pipe);
  1632. }
  1633. static bool need_vtd_wa(struct drm_device *dev)
  1634. {
  1635. #ifdef CONFIG_INTEL_IOMMU
  1636. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1637. return true;
  1638. #endif
  1639. return false;
  1640. }
  1641. int
  1642. intel_pin_and_fence_fb_obj(struct drm_device *dev,
  1643. struct drm_i915_gem_object *obj,
  1644. struct intel_ring_buffer *pipelined)
  1645. {
  1646. struct drm_i915_private *dev_priv = dev->dev_private;
  1647. u32 alignment;
  1648. int ret;
  1649. switch (obj->tiling_mode) {
  1650. case I915_TILING_NONE:
  1651. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1652. alignment = 128 * 1024;
  1653. else if (INTEL_INFO(dev)->gen >= 4)
  1654. alignment = 4 * 1024;
  1655. else
  1656. alignment = 64 * 1024;
  1657. break;
  1658. case I915_TILING_X:
  1659. /* pin() will align the object as required by fence */
  1660. alignment = 0;
  1661. break;
  1662. case I915_TILING_Y:
  1663. /* Despite that we check this in framebuffer_init userspace can
  1664. * screw us over and change the tiling after the fact. Only
  1665. * pinned buffers can't change their tiling. */
  1666. DRM_DEBUG_DRIVER("Y tiled not allowed for scan out buffers\n");
  1667. return -EINVAL;
  1668. default:
  1669. BUG();
  1670. }
  1671. /* Note that the w/a also requires 64 PTE of padding following the
  1672. * bo. We currently fill all unused PTE with the shadow page and so
  1673. * we should always have valid PTE following the scanout preventing
  1674. * the VT-d warning.
  1675. */
  1676. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1677. alignment = 256 * 1024;
  1678. dev_priv->mm.interruptible = false;
  1679. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1680. if (ret)
  1681. goto err_interruptible;
  1682. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1683. * fence, whereas 965+ only requires a fence if using
  1684. * framebuffer compression. For simplicity, we always install
  1685. * a fence as the cost is not that onerous.
  1686. */
  1687. ret = i915_gem_object_get_fence(obj);
  1688. if (ret)
  1689. goto err_unpin;
  1690. i915_gem_object_pin_fence(obj);
  1691. dev_priv->mm.interruptible = true;
  1692. return 0;
  1693. err_unpin:
  1694. i915_gem_object_unpin_from_display_plane(obj);
  1695. err_interruptible:
  1696. dev_priv->mm.interruptible = true;
  1697. return ret;
  1698. }
  1699. void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
  1700. {
  1701. i915_gem_object_unpin_fence(obj);
  1702. i915_gem_object_unpin_from_display_plane(obj);
  1703. }
  1704. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  1705. * is assumed to be a power-of-two. */
  1706. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  1707. unsigned int tiling_mode,
  1708. unsigned int cpp,
  1709. unsigned int pitch)
  1710. {
  1711. if (tiling_mode != I915_TILING_NONE) {
  1712. unsigned int tile_rows, tiles;
  1713. tile_rows = *y / 8;
  1714. *y %= 8;
  1715. tiles = *x / (512/cpp);
  1716. *x %= 512/cpp;
  1717. return tile_rows * pitch * 8 + tiles * 4096;
  1718. } else {
  1719. unsigned int offset;
  1720. offset = *y * pitch + *x * cpp;
  1721. *y = 0;
  1722. *x = (offset & 4095) / cpp;
  1723. return offset & -4096;
  1724. }
  1725. }
  1726. static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1727. int x, int y)
  1728. {
  1729. struct drm_device *dev = crtc->dev;
  1730. struct drm_i915_private *dev_priv = dev->dev_private;
  1731. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1732. struct intel_framebuffer *intel_fb;
  1733. struct drm_i915_gem_object *obj;
  1734. int plane = intel_crtc->plane;
  1735. unsigned long linear_offset;
  1736. u32 dspcntr;
  1737. u32 reg;
  1738. switch (plane) {
  1739. case 0:
  1740. case 1:
  1741. break;
  1742. default:
  1743. DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
  1744. return -EINVAL;
  1745. }
  1746. intel_fb = to_intel_framebuffer(fb);
  1747. obj = intel_fb->obj;
  1748. reg = DSPCNTR(plane);
  1749. dspcntr = I915_READ(reg);
  1750. /* Mask out pixel format bits in case we change it */
  1751. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1752. switch (fb->pixel_format) {
  1753. case DRM_FORMAT_C8:
  1754. dspcntr |= DISPPLANE_8BPP;
  1755. break;
  1756. case DRM_FORMAT_XRGB1555:
  1757. case DRM_FORMAT_ARGB1555:
  1758. dspcntr |= DISPPLANE_BGRX555;
  1759. break;
  1760. case DRM_FORMAT_RGB565:
  1761. dspcntr |= DISPPLANE_BGRX565;
  1762. break;
  1763. case DRM_FORMAT_XRGB8888:
  1764. case DRM_FORMAT_ARGB8888:
  1765. dspcntr |= DISPPLANE_BGRX888;
  1766. break;
  1767. case DRM_FORMAT_XBGR8888:
  1768. case DRM_FORMAT_ABGR8888:
  1769. dspcntr |= DISPPLANE_RGBX888;
  1770. break;
  1771. case DRM_FORMAT_XRGB2101010:
  1772. case DRM_FORMAT_ARGB2101010:
  1773. dspcntr |= DISPPLANE_BGRX101010;
  1774. break;
  1775. case DRM_FORMAT_XBGR2101010:
  1776. case DRM_FORMAT_ABGR2101010:
  1777. dspcntr |= DISPPLANE_RGBX101010;
  1778. break;
  1779. default:
  1780. BUG();
  1781. }
  1782. if (INTEL_INFO(dev)->gen >= 4) {
  1783. if (obj->tiling_mode != I915_TILING_NONE)
  1784. dspcntr |= DISPPLANE_TILED;
  1785. else
  1786. dspcntr &= ~DISPPLANE_TILED;
  1787. }
  1788. if (IS_G4X(dev))
  1789. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  1790. I915_WRITE(reg, dspcntr);
  1791. linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  1792. if (INTEL_INFO(dev)->gen >= 4) {
  1793. intel_crtc->dspaddr_offset =
  1794. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  1795. fb->bits_per_pixel / 8,
  1796. fb->pitches[0]);
  1797. linear_offset -= intel_crtc->dspaddr_offset;
  1798. } else {
  1799. intel_crtc->dspaddr_offset = linear_offset;
  1800. }
  1801. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  1802. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  1803. fb->pitches[0]);
  1804. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  1805. if (INTEL_INFO(dev)->gen >= 4) {
  1806. I915_MODIFY_DISPBASE(DSPSURF(plane),
  1807. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  1808. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  1809. I915_WRITE(DSPLINOFF(plane), linear_offset);
  1810. } else
  1811. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  1812. POSTING_READ(reg);
  1813. return 0;
  1814. }
  1815. static int ironlake_update_plane(struct drm_crtc *crtc,
  1816. struct drm_framebuffer *fb, int x, int y)
  1817. {
  1818. struct drm_device *dev = crtc->dev;
  1819. struct drm_i915_private *dev_priv = dev->dev_private;
  1820. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1821. struct intel_framebuffer *intel_fb;
  1822. struct drm_i915_gem_object *obj;
  1823. int plane = intel_crtc->plane;
  1824. unsigned long linear_offset;
  1825. u32 dspcntr;
  1826. u32 reg;
  1827. switch (plane) {
  1828. case 0:
  1829. case 1:
  1830. case 2:
  1831. break;
  1832. default:
  1833. DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
  1834. return -EINVAL;
  1835. }
  1836. intel_fb = to_intel_framebuffer(fb);
  1837. obj = intel_fb->obj;
  1838. reg = DSPCNTR(plane);
  1839. dspcntr = I915_READ(reg);
  1840. /* Mask out pixel format bits in case we change it */
  1841. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1842. switch (fb->pixel_format) {
  1843. case DRM_FORMAT_C8:
  1844. dspcntr |= DISPPLANE_8BPP;
  1845. break;
  1846. case DRM_FORMAT_RGB565:
  1847. dspcntr |= DISPPLANE_BGRX565;
  1848. break;
  1849. case DRM_FORMAT_XRGB8888:
  1850. case DRM_FORMAT_ARGB8888:
  1851. dspcntr |= DISPPLANE_BGRX888;
  1852. break;
  1853. case DRM_FORMAT_XBGR8888:
  1854. case DRM_FORMAT_ABGR8888:
  1855. dspcntr |= DISPPLANE_RGBX888;
  1856. break;
  1857. case DRM_FORMAT_XRGB2101010:
  1858. case DRM_FORMAT_ARGB2101010:
  1859. dspcntr |= DISPPLANE_BGRX101010;
  1860. break;
  1861. case DRM_FORMAT_XBGR2101010:
  1862. case DRM_FORMAT_ABGR2101010:
  1863. dspcntr |= DISPPLANE_RGBX101010;
  1864. break;
  1865. default:
  1866. BUG();
  1867. }
  1868. if (obj->tiling_mode != I915_TILING_NONE)
  1869. dspcntr |= DISPPLANE_TILED;
  1870. else
  1871. dspcntr &= ~DISPPLANE_TILED;
  1872. if (IS_HASWELL(dev))
  1873. dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
  1874. else
  1875. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  1876. I915_WRITE(reg, dspcntr);
  1877. linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  1878. intel_crtc->dspaddr_offset =
  1879. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  1880. fb->bits_per_pixel / 8,
  1881. fb->pitches[0]);
  1882. linear_offset -= intel_crtc->dspaddr_offset;
  1883. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  1884. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  1885. fb->pitches[0]);
  1886. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  1887. I915_MODIFY_DISPBASE(DSPSURF(plane),
  1888. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  1889. if (IS_HASWELL(dev)) {
  1890. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  1891. } else {
  1892. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  1893. I915_WRITE(DSPLINOFF(plane), linear_offset);
  1894. }
  1895. POSTING_READ(reg);
  1896. return 0;
  1897. }
  1898. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  1899. static int
  1900. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1901. int x, int y, enum mode_set_atomic state)
  1902. {
  1903. struct drm_device *dev = crtc->dev;
  1904. struct drm_i915_private *dev_priv = dev->dev_private;
  1905. if (dev_priv->display.disable_fbc)
  1906. dev_priv->display.disable_fbc(dev);
  1907. intel_increase_pllclock(crtc);
  1908. return dev_priv->display.update_plane(crtc, fb, x, y);
  1909. }
  1910. void intel_display_handle_reset(struct drm_device *dev)
  1911. {
  1912. struct drm_i915_private *dev_priv = dev->dev_private;
  1913. struct drm_crtc *crtc;
  1914. /*
  1915. * Flips in the rings have been nuked by the reset,
  1916. * so complete all pending flips so that user space
  1917. * will get its events and not get stuck.
  1918. *
  1919. * Also update the base address of all primary
  1920. * planes to the the last fb to make sure we're
  1921. * showing the correct fb after a reset.
  1922. *
  1923. * Need to make two loops over the crtcs so that we
  1924. * don't try to grab a crtc mutex before the
  1925. * pending_flip_queue really got woken up.
  1926. */
  1927. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1928. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1929. enum plane plane = intel_crtc->plane;
  1930. intel_prepare_page_flip(dev, plane);
  1931. intel_finish_page_flip_plane(dev, plane);
  1932. }
  1933. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1934. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1935. mutex_lock(&crtc->mutex);
  1936. if (intel_crtc->active)
  1937. dev_priv->display.update_plane(crtc, crtc->fb,
  1938. crtc->x, crtc->y);
  1939. mutex_unlock(&crtc->mutex);
  1940. }
  1941. }
  1942. static int
  1943. intel_finish_fb(struct drm_framebuffer *old_fb)
  1944. {
  1945. struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
  1946. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1947. bool was_interruptible = dev_priv->mm.interruptible;
  1948. int ret;
  1949. /* Big Hammer, we also need to ensure that any pending
  1950. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  1951. * current scanout is retired before unpinning the old
  1952. * framebuffer.
  1953. *
  1954. * This should only fail upon a hung GPU, in which case we
  1955. * can safely continue.
  1956. */
  1957. dev_priv->mm.interruptible = false;
  1958. ret = i915_gem_object_finish_gpu(obj);
  1959. dev_priv->mm.interruptible = was_interruptible;
  1960. return ret;
  1961. }
  1962. static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
  1963. {
  1964. struct drm_device *dev = crtc->dev;
  1965. struct drm_i915_master_private *master_priv;
  1966. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1967. if (!dev->primary->master)
  1968. return;
  1969. master_priv = dev->primary->master->driver_priv;
  1970. if (!master_priv->sarea_priv)
  1971. return;
  1972. switch (intel_crtc->pipe) {
  1973. case 0:
  1974. master_priv->sarea_priv->pipeA_x = x;
  1975. master_priv->sarea_priv->pipeA_y = y;
  1976. break;
  1977. case 1:
  1978. master_priv->sarea_priv->pipeB_x = x;
  1979. master_priv->sarea_priv->pipeB_y = y;
  1980. break;
  1981. default:
  1982. break;
  1983. }
  1984. }
  1985. static int
  1986. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  1987. struct drm_framebuffer *fb)
  1988. {
  1989. struct drm_device *dev = crtc->dev;
  1990. struct drm_i915_private *dev_priv = dev->dev_private;
  1991. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1992. struct drm_framebuffer *old_fb;
  1993. int ret;
  1994. /* no fb bound */
  1995. if (!fb) {
  1996. DRM_ERROR("No FB bound\n");
  1997. return 0;
  1998. }
  1999. if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
  2000. DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
  2001. plane_name(intel_crtc->plane),
  2002. INTEL_INFO(dev)->num_pipes);
  2003. return -EINVAL;
  2004. }
  2005. mutex_lock(&dev->struct_mutex);
  2006. ret = intel_pin_and_fence_fb_obj(dev,
  2007. to_intel_framebuffer(fb)->obj,
  2008. NULL);
  2009. if (ret != 0) {
  2010. mutex_unlock(&dev->struct_mutex);
  2011. DRM_ERROR("pin & fence failed\n");
  2012. return ret;
  2013. }
  2014. /*
  2015. * Update pipe size and adjust fitter if needed: the reason for this is
  2016. * that in compute_mode_changes we check the native mode (not the pfit
  2017. * mode) to see if we can flip rather than do a full mode set. In the
  2018. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2019. * pfit state, we'll end up with a big fb scanned out into the wrong
  2020. * sized surface.
  2021. *
  2022. * To fix this properly, we need to hoist the checks up into
  2023. * compute_mode_changes (or above), check the actual pfit state and
  2024. * whether the platform allows pfit disable with pipe active, and only
  2025. * then update the pipesrc and pfit state, even on the flip path.
  2026. */
  2027. if (i915_fastboot) {
  2028. const struct drm_display_mode *adjusted_mode =
  2029. &intel_crtc->config.adjusted_mode;
  2030. I915_WRITE(PIPESRC(intel_crtc->pipe),
  2031. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2032. (adjusted_mode->crtc_vdisplay - 1));
  2033. if (!intel_crtc->config.pch_pfit.enabled &&
  2034. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2035. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2036. I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
  2037. I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
  2038. I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
  2039. }
  2040. }
  2041. ret = dev_priv->display.update_plane(crtc, fb, x, y);
  2042. if (ret) {
  2043. intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
  2044. mutex_unlock(&dev->struct_mutex);
  2045. DRM_ERROR("failed to update base address\n");
  2046. return ret;
  2047. }
  2048. old_fb = crtc->fb;
  2049. crtc->fb = fb;
  2050. crtc->x = x;
  2051. crtc->y = y;
  2052. if (old_fb) {
  2053. if (intel_crtc->active && old_fb != fb)
  2054. intel_wait_for_vblank(dev, intel_crtc->pipe);
  2055. intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
  2056. }
  2057. intel_update_fbc(dev);
  2058. intel_edp_psr_update(dev);
  2059. mutex_unlock(&dev->struct_mutex);
  2060. intel_crtc_update_sarea_pos(crtc, x, y);
  2061. return 0;
  2062. }
  2063. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2064. {
  2065. struct drm_device *dev = crtc->dev;
  2066. struct drm_i915_private *dev_priv = dev->dev_private;
  2067. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2068. int pipe = intel_crtc->pipe;
  2069. u32 reg, temp;
  2070. /* enable normal train */
  2071. reg = FDI_TX_CTL(pipe);
  2072. temp = I915_READ(reg);
  2073. if (IS_IVYBRIDGE(dev)) {
  2074. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2075. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2076. } else {
  2077. temp &= ~FDI_LINK_TRAIN_NONE;
  2078. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2079. }
  2080. I915_WRITE(reg, temp);
  2081. reg = FDI_RX_CTL(pipe);
  2082. temp = I915_READ(reg);
  2083. if (HAS_PCH_CPT(dev)) {
  2084. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2085. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2086. } else {
  2087. temp &= ~FDI_LINK_TRAIN_NONE;
  2088. temp |= FDI_LINK_TRAIN_NONE;
  2089. }
  2090. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2091. /* wait one idle pattern time */
  2092. POSTING_READ(reg);
  2093. udelay(1000);
  2094. /* IVB wants error correction enabled */
  2095. if (IS_IVYBRIDGE(dev))
  2096. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2097. FDI_FE_ERRC_ENABLE);
  2098. }
  2099. static bool pipe_has_enabled_pch(struct intel_crtc *intel_crtc)
  2100. {
  2101. return intel_crtc->base.enabled && intel_crtc->config.has_pch_encoder;
  2102. }
  2103. static void ivb_modeset_global_resources(struct drm_device *dev)
  2104. {
  2105. struct drm_i915_private *dev_priv = dev->dev_private;
  2106. struct intel_crtc *pipe_B_crtc =
  2107. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  2108. struct intel_crtc *pipe_C_crtc =
  2109. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
  2110. uint32_t temp;
  2111. /*
  2112. * When everything is off disable fdi C so that we could enable fdi B
  2113. * with all lanes. Note that we don't care about enabled pipes without
  2114. * an enabled pch encoder.
  2115. */
  2116. if (!pipe_has_enabled_pch(pipe_B_crtc) &&
  2117. !pipe_has_enabled_pch(pipe_C_crtc)) {
  2118. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2119. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2120. temp = I915_READ(SOUTH_CHICKEN1);
  2121. temp &= ~FDI_BC_BIFURCATION_SELECT;
  2122. DRM_DEBUG_KMS("disabling fdi C rx\n");
  2123. I915_WRITE(SOUTH_CHICKEN1, temp);
  2124. }
  2125. }
  2126. /* The FDI link training functions for ILK/Ibexpeak. */
  2127. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2128. {
  2129. struct drm_device *dev = crtc->dev;
  2130. struct drm_i915_private *dev_priv = dev->dev_private;
  2131. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2132. int pipe = intel_crtc->pipe;
  2133. int plane = intel_crtc->plane;
  2134. u32 reg, temp, tries;
  2135. /* FDI needs bits from pipe & plane first */
  2136. assert_pipe_enabled(dev_priv, pipe);
  2137. assert_plane_enabled(dev_priv, plane);
  2138. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2139. for train result */
  2140. reg = FDI_RX_IMR(pipe);
  2141. temp = I915_READ(reg);
  2142. temp &= ~FDI_RX_SYMBOL_LOCK;
  2143. temp &= ~FDI_RX_BIT_LOCK;
  2144. I915_WRITE(reg, temp);
  2145. I915_READ(reg);
  2146. udelay(150);
  2147. /* enable CPU FDI TX and PCH FDI RX */
  2148. reg = FDI_TX_CTL(pipe);
  2149. temp = I915_READ(reg);
  2150. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2151. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2152. temp &= ~FDI_LINK_TRAIN_NONE;
  2153. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2154. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2155. reg = FDI_RX_CTL(pipe);
  2156. temp = I915_READ(reg);
  2157. temp &= ~FDI_LINK_TRAIN_NONE;
  2158. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2159. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2160. POSTING_READ(reg);
  2161. udelay(150);
  2162. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2163. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2164. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2165. FDI_RX_PHASE_SYNC_POINTER_EN);
  2166. reg = FDI_RX_IIR(pipe);
  2167. for (tries = 0; tries < 5; tries++) {
  2168. temp = I915_READ(reg);
  2169. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2170. if ((temp & FDI_RX_BIT_LOCK)) {
  2171. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2172. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2173. break;
  2174. }
  2175. }
  2176. if (tries == 5)
  2177. DRM_ERROR("FDI train 1 fail!\n");
  2178. /* Train 2 */
  2179. reg = FDI_TX_CTL(pipe);
  2180. temp = I915_READ(reg);
  2181. temp &= ~FDI_LINK_TRAIN_NONE;
  2182. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2183. I915_WRITE(reg, temp);
  2184. reg = FDI_RX_CTL(pipe);
  2185. temp = I915_READ(reg);
  2186. temp &= ~FDI_LINK_TRAIN_NONE;
  2187. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2188. I915_WRITE(reg, temp);
  2189. POSTING_READ(reg);
  2190. udelay(150);
  2191. reg = FDI_RX_IIR(pipe);
  2192. for (tries = 0; tries < 5; tries++) {
  2193. temp = I915_READ(reg);
  2194. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2195. if (temp & FDI_RX_SYMBOL_LOCK) {
  2196. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2197. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2198. break;
  2199. }
  2200. }
  2201. if (tries == 5)
  2202. DRM_ERROR("FDI train 2 fail!\n");
  2203. DRM_DEBUG_KMS("FDI train done\n");
  2204. }
  2205. static const int snb_b_fdi_train_param[] = {
  2206. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2207. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2208. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2209. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2210. };
  2211. /* The FDI link training functions for SNB/Cougarpoint. */
  2212. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2213. {
  2214. struct drm_device *dev = crtc->dev;
  2215. struct drm_i915_private *dev_priv = dev->dev_private;
  2216. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2217. int pipe = intel_crtc->pipe;
  2218. u32 reg, temp, i, retry;
  2219. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2220. for train result */
  2221. reg = FDI_RX_IMR(pipe);
  2222. temp = I915_READ(reg);
  2223. temp &= ~FDI_RX_SYMBOL_LOCK;
  2224. temp &= ~FDI_RX_BIT_LOCK;
  2225. I915_WRITE(reg, temp);
  2226. POSTING_READ(reg);
  2227. udelay(150);
  2228. /* enable CPU FDI TX and PCH FDI RX */
  2229. reg = FDI_TX_CTL(pipe);
  2230. temp = I915_READ(reg);
  2231. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2232. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2233. temp &= ~FDI_LINK_TRAIN_NONE;
  2234. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2235. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2236. /* SNB-B */
  2237. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2238. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2239. I915_WRITE(FDI_RX_MISC(pipe),
  2240. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2241. reg = FDI_RX_CTL(pipe);
  2242. temp = I915_READ(reg);
  2243. if (HAS_PCH_CPT(dev)) {
  2244. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2245. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2246. } else {
  2247. temp &= ~FDI_LINK_TRAIN_NONE;
  2248. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2249. }
  2250. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2251. POSTING_READ(reg);
  2252. udelay(150);
  2253. for (i = 0; i < 4; i++) {
  2254. reg = FDI_TX_CTL(pipe);
  2255. temp = I915_READ(reg);
  2256. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2257. temp |= snb_b_fdi_train_param[i];
  2258. I915_WRITE(reg, temp);
  2259. POSTING_READ(reg);
  2260. udelay(500);
  2261. for (retry = 0; retry < 5; retry++) {
  2262. reg = FDI_RX_IIR(pipe);
  2263. temp = I915_READ(reg);
  2264. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2265. if (temp & FDI_RX_BIT_LOCK) {
  2266. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2267. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2268. break;
  2269. }
  2270. udelay(50);
  2271. }
  2272. if (retry < 5)
  2273. break;
  2274. }
  2275. if (i == 4)
  2276. DRM_ERROR("FDI train 1 fail!\n");
  2277. /* Train 2 */
  2278. reg = FDI_TX_CTL(pipe);
  2279. temp = I915_READ(reg);
  2280. temp &= ~FDI_LINK_TRAIN_NONE;
  2281. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2282. if (IS_GEN6(dev)) {
  2283. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2284. /* SNB-B */
  2285. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2286. }
  2287. I915_WRITE(reg, temp);
  2288. reg = FDI_RX_CTL(pipe);
  2289. temp = I915_READ(reg);
  2290. if (HAS_PCH_CPT(dev)) {
  2291. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2292. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2293. } else {
  2294. temp &= ~FDI_LINK_TRAIN_NONE;
  2295. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2296. }
  2297. I915_WRITE(reg, temp);
  2298. POSTING_READ(reg);
  2299. udelay(150);
  2300. for (i = 0; i < 4; i++) {
  2301. reg = FDI_TX_CTL(pipe);
  2302. temp = I915_READ(reg);
  2303. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2304. temp |= snb_b_fdi_train_param[i];
  2305. I915_WRITE(reg, temp);
  2306. POSTING_READ(reg);
  2307. udelay(500);
  2308. for (retry = 0; retry < 5; retry++) {
  2309. reg = FDI_RX_IIR(pipe);
  2310. temp = I915_READ(reg);
  2311. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2312. if (temp & FDI_RX_SYMBOL_LOCK) {
  2313. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2314. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2315. break;
  2316. }
  2317. udelay(50);
  2318. }
  2319. if (retry < 5)
  2320. break;
  2321. }
  2322. if (i == 4)
  2323. DRM_ERROR("FDI train 2 fail!\n");
  2324. DRM_DEBUG_KMS("FDI train done.\n");
  2325. }
  2326. /* Manual link training for Ivy Bridge A0 parts */
  2327. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2328. {
  2329. struct drm_device *dev = crtc->dev;
  2330. struct drm_i915_private *dev_priv = dev->dev_private;
  2331. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2332. int pipe = intel_crtc->pipe;
  2333. u32 reg, temp, i, j;
  2334. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2335. for train result */
  2336. reg = FDI_RX_IMR(pipe);
  2337. temp = I915_READ(reg);
  2338. temp &= ~FDI_RX_SYMBOL_LOCK;
  2339. temp &= ~FDI_RX_BIT_LOCK;
  2340. I915_WRITE(reg, temp);
  2341. POSTING_READ(reg);
  2342. udelay(150);
  2343. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  2344. I915_READ(FDI_RX_IIR(pipe)));
  2345. /* Try each vswing and preemphasis setting twice before moving on */
  2346. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  2347. /* disable first in case we need to retry */
  2348. reg = FDI_TX_CTL(pipe);
  2349. temp = I915_READ(reg);
  2350. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2351. temp &= ~FDI_TX_ENABLE;
  2352. I915_WRITE(reg, temp);
  2353. reg = FDI_RX_CTL(pipe);
  2354. temp = I915_READ(reg);
  2355. temp &= ~FDI_LINK_TRAIN_AUTO;
  2356. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2357. temp &= ~FDI_RX_ENABLE;
  2358. I915_WRITE(reg, temp);
  2359. /* enable CPU FDI TX and PCH FDI RX */
  2360. reg = FDI_TX_CTL(pipe);
  2361. temp = I915_READ(reg);
  2362. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2363. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2364. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2365. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2366. temp |= snb_b_fdi_train_param[j/2];
  2367. temp |= FDI_COMPOSITE_SYNC;
  2368. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2369. I915_WRITE(FDI_RX_MISC(pipe),
  2370. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2371. reg = FDI_RX_CTL(pipe);
  2372. temp = I915_READ(reg);
  2373. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2374. temp |= FDI_COMPOSITE_SYNC;
  2375. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2376. POSTING_READ(reg);
  2377. udelay(1); /* should be 0.5us */
  2378. for (i = 0; i < 4; i++) {
  2379. reg = FDI_RX_IIR(pipe);
  2380. temp = I915_READ(reg);
  2381. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2382. if (temp & FDI_RX_BIT_LOCK ||
  2383. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2384. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2385. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  2386. i);
  2387. break;
  2388. }
  2389. udelay(1); /* should be 0.5us */
  2390. }
  2391. if (i == 4) {
  2392. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  2393. continue;
  2394. }
  2395. /* Train 2 */
  2396. reg = FDI_TX_CTL(pipe);
  2397. temp = I915_READ(reg);
  2398. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2399. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2400. I915_WRITE(reg, temp);
  2401. reg = FDI_RX_CTL(pipe);
  2402. temp = I915_READ(reg);
  2403. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2404. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2405. I915_WRITE(reg, temp);
  2406. POSTING_READ(reg);
  2407. udelay(2); /* should be 1.5us */
  2408. for (i = 0; i < 4; i++) {
  2409. reg = FDI_RX_IIR(pipe);
  2410. temp = I915_READ(reg);
  2411. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2412. if (temp & FDI_RX_SYMBOL_LOCK ||
  2413. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  2414. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2415. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  2416. i);
  2417. goto train_done;
  2418. }
  2419. udelay(2); /* should be 1.5us */
  2420. }
  2421. if (i == 4)
  2422. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  2423. }
  2424. train_done:
  2425. DRM_DEBUG_KMS("FDI train done.\n");
  2426. }
  2427. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  2428. {
  2429. struct drm_device *dev = intel_crtc->base.dev;
  2430. struct drm_i915_private *dev_priv = dev->dev_private;
  2431. int pipe = intel_crtc->pipe;
  2432. u32 reg, temp;
  2433. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2434. reg = FDI_RX_CTL(pipe);
  2435. temp = I915_READ(reg);
  2436. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  2437. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2438. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2439. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2440. POSTING_READ(reg);
  2441. udelay(200);
  2442. /* Switch from Rawclk to PCDclk */
  2443. temp = I915_READ(reg);
  2444. I915_WRITE(reg, temp | FDI_PCDCLK);
  2445. POSTING_READ(reg);
  2446. udelay(200);
  2447. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2448. reg = FDI_TX_CTL(pipe);
  2449. temp = I915_READ(reg);
  2450. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2451. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2452. POSTING_READ(reg);
  2453. udelay(100);
  2454. }
  2455. }
  2456. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  2457. {
  2458. struct drm_device *dev = intel_crtc->base.dev;
  2459. struct drm_i915_private *dev_priv = dev->dev_private;
  2460. int pipe = intel_crtc->pipe;
  2461. u32 reg, temp;
  2462. /* Switch from PCDclk to Rawclk */
  2463. reg = FDI_RX_CTL(pipe);
  2464. temp = I915_READ(reg);
  2465. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2466. /* Disable CPU FDI TX PLL */
  2467. reg = FDI_TX_CTL(pipe);
  2468. temp = I915_READ(reg);
  2469. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2470. POSTING_READ(reg);
  2471. udelay(100);
  2472. reg = FDI_RX_CTL(pipe);
  2473. temp = I915_READ(reg);
  2474. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2475. /* Wait for the clocks to turn off. */
  2476. POSTING_READ(reg);
  2477. udelay(100);
  2478. }
  2479. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2480. {
  2481. struct drm_device *dev = crtc->dev;
  2482. struct drm_i915_private *dev_priv = dev->dev_private;
  2483. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2484. int pipe = intel_crtc->pipe;
  2485. u32 reg, temp;
  2486. /* disable CPU FDI tx and PCH FDI rx */
  2487. reg = FDI_TX_CTL(pipe);
  2488. temp = I915_READ(reg);
  2489. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2490. POSTING_READ(reg);
  2491. reg = FDI_RX_CTL(pipe);
  2492. temp = I915_READ(reg);
  2493. temp &= ~(0x7 << 16);
  2494. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2495. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2496. POSTING_READ(reg);
  2497. udelay(100);
  2498. /* Ironlake workaround, disable clock pointer after downing FDI */
  2499. if (HAS_PCH_IBX(dev)) {
  2500. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2501. }
  2502. /* still set train pattern 1 */
  2503. reg = FDI_TX_CTL(pipe);
  2504. temp = I915_READ(reg);
  2505. temp &= ~FDI_LINK_TRAIN_NONE;
  2506. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2507. I915_WRITE(reg, temp);
  2508. reg = FDI_RX_CTL(pipe);
  2509. temp = I915_READ(reg);
  2510. if (HAS_PCH_CPT(dev)) {
  2511. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2512. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2513. } else {
  2514. temp &= ~FDI_LINK_TRAIN_NONE;
  2515. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2516. }
  2517. /* BPC in FDI rx is consistent with that in PIPECONF */
  2518. temp &= ~(0x07 << 16);
  2519. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2520. I915_WRITE(reg, temp);
  2521. POSTING_READ(reg);
  2522. udelay(100);
  2523. }
  2524. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2525. {
  2526. struct drm_device *dev = crtc->dev;
  2527. struct drm_i915_private *dev_priv = dev->dev_private;
  2528. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2529. unsigned long flags;
  2530. bool pending;
  2531. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2532. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2533. return false;
  2534. spin_lock_irqsave(&dev->event_lock, flags);
  2535. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2536. spin_unlock_irqrestore(&dev->event_lock, flags);
  2537. return pending;
  2538. }
  2539. static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  2540. {
  2541. struct drm_device *dev = crtc->dev;
  2542. struct drm_i915_private *dev_priv = dev->dev_private;
  2543. if (crtc->fb == NULL)
  2544. return;
  2545. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  2546. wait_event(dev_priv->pending_flip_queue,
  2547. !intel_crtc_has_pending_flip(crtc));
  2548. mutex_lock(&dev->struct_mutex);
  2549. intel_finish_fb(crtc->fb);
  2550. mutex_unlock(&dev->struct_mutex);
  2551. }
  2552. /* Program iCLKIP clock to the desired frequency */
  2553. static void lpt_program_iclkip(struct drm_crtc *crtc)
  2554. {
  2555. struct drm_device *dev = crtc->dev;
  2556. struct drm_i915_private *dev_priv = dev->dev_private;
  2557. int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
  2558. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  2559. u32 temp;
  2560. mutex_lock(&dev_priv->dpio_lock);
  2561. /* It is necessary to ungate the pixclk gate prior to programming
  2562. * the divisors, and gate it back when it is done.
  2563. */
  2564. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  2565. /* Disable SSCCTL */
  2566. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  2567. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  2568. SBI_SSCCTL_DISABLE,
  2569. SBI_ICLK);
  2570. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  2571. if (clock == 20000) {
  2572. auxdiv = 1;
  2573. divsel = 0x41;
  2574. phaseinc = 0x20;
  2575. } else {
  2576. /* The iCLK virtual clock root frequency is in MHz,
  2577. * but the adjusted_mode->crtc_clock in in KHz. To get the
  2578. * divisors, it is necessary to divide one by another, so we
  2579. * convert the virtual clock precision to KHz here for higher
  2580. * precision.
  2581. */
  2582. u32 iclk_virtual_root_freq = 172800 * 1000;
  2583. u32 iclk_pi_range = 64;
  2584. u32 desired_divisor, msb_divisor_value, pi_value;
  2585. desired_divisor = (iclk_virtual_root_freq / clock);
  2586. msb_divisor_value = desired_divisor / iclk_pi_range;
  2587. pi_value = desired_divisor % iclk_pi_range;
  2588. auxdiv = 0;
  2589. divsel = msb_divisor_value - 2;
  2590. phaseinc = pi_value;
  2591. }
  2592. /* This should not happen with any sane values */
  2593. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  2594. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  2595. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  2596. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  2597. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  2598. clock,
  2599. auxdiv,
  2600. divsel,
  2601. phasedir,
  2602. phaseinc);
  2603. /* Program SSCDIVINTPHASE6 */
  2604. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  2605. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  2606. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  2607. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  2608. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  2609. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  2610. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  2611. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  2612. /* Program SSCAUXDIV */
  2613. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  2614. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  2615. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  2616. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  2617. /* Enable modulator and associated divider */
  2618. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  2619. temp &= ~SBI_SSCCTL_DISABLE;
  2620. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  2621. /* Wait for initialization time */
  2622. udelay(24);
  2623. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  2624. mutex_unlock(&dev_priv->dpio_lock);
  2625. }
  2626. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  2627. enum pipe pch_transcoder)
  2628. {
  2629. struct drm_device *dev = crtc->base.dev;
  2630. struct drm_i915_private *dev_priv = dev->dev_private;
  2631. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  2632. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  2633. I915_READ(HTOTAL(cpu_transcoder)));
  2634. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  2635. I915_READ(HBLANK(cpu_transcoder)));
  2636. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  2637. I915_READ(HSYNC(cpu_transcoder)));
  2638. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  2639. I915_READ(VTOTAL(cpu_transcoder)));
  2640. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  2641. I915_READ(VBLANK(cpu_transcoder)));
  2642. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  2643. I915_READ(VSYNC(cpu_transcoder)));
  2644. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  2645. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  2646. }
  2647. /*
  2648. * Enable PCH resources required for PCH ports:
  2649. * - PCH PLLs
  2650. * - FDI training & RX/TX
  2651. * - update transcoder timings
  2652. * - DP transcoding bits
  2653. * - transcoder
  2654. */
  2655. static void ironlake_pch_enable(struct drm_crtc *crtc)
  2656. {
  2657. struct drm_device *dev = crtc->dev;
  2658. struct drm_i915_private *dev_priv = dev->dev_private;
  2659. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2660. int pipe = intel_crtc->pipe;
  2661. u32 reg, temp;
  2662. assert_pch_transcoder_disabled(dev_priv, pipe);
  2663. /* Write the TU size bits before fdi link training, so that error
  2664. * detection works. */
  2665. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  2666. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  2667. /* For PCH output, training FDI link */
  2668. dev_priv->display.fdi_link_train(crtc);
  2669. /* We need to program the right clock selection before writing the pixel
  2670. * mutliplier into the DPLL. */
  2671. if (HAS_PCH_CPT(dev)) {
  2672. u32 sel;
  2673. temp = I915_READ(PCH_DPLL_SEL);
  2674. temp |= TRANS_DPLL_ENABLE(pipe);
  2675. sel = TRANS_DPLLB_SEL(pipe);
  2676. if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
  2677. temp |= sel;
  2678. else
  2679. temp &= ~sel;
  2680. I915_WRITE(PCH_DPLL_SEL, temp);
  2681. }
  2682. /* XXX: pch pll's can be enabled any time before we enable the PCH
  2683. * transcoder, and we actually should do this to not upset any PCH
  2684. * transcoder that already use the clock when we share it.
  2685. *
  2686. * Note that enable_shared_dpll tries to do the right thing, but
  2687. * get_shared_dpll unconditionally resets the pll - we need that to have
  2688. * the right LVDS enable sequence. */
  2689. ironlake_enable_shared_dpll(intel_crtc);
  2690. /* set transcoder timing, panel must allow it */
  2691. assert_panel_unlocked(dev_priv, pipe);
  2692. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  2693. intel_fdi_normal_train(crtc);
  2694. /* For PCH DP, enable TRANS_DP_CTL */
  2695. if (HAS_PCH_CPT(dev) &&
  2696. (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  2697. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2698. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  2699. reg = TRANS_DP_CTL(pipe);
  2700. temp = I915_READ(reg);
  2701. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  2702. TRANS_DP_SYNC_MASK |
  2703. TRANS_DP_BPC_MASK);
  2704. temp |= (TRANS_DP_OUTPUT_ENABLE |
  2705. TRANS_DP_ENH_FRAMING);
  2706. temp |= bpc << 9; /* same format but at 11:9 */
  2707. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  2708. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  2709. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  2710. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  2711. switch (intel_trans_dp_port_sel(crtc)) {
  2712. case PCH_DP_B:
  2713. temp |= TRANS_DP_PORT_SEL_B;
  2714. break;
  2715. case PCH_DP_C:
  2716. temp |= TRANS_DP_PORT_SEL_C;
  2717. break;
  2718. case PCH_DP_D:
  2719. temp |= TRANS_DP_PORT_SEL_D;
  2720. break;
  2721. default:
  2722. BUG();
  2723. }
  2724. I915_WRITE(reg, temp);
  2725. }
  2726. ironlake_enable_pch_transcoder(dev_priv, pipe);
  2727. }
  2728. static void lpt_pch_enable(struct drm_crtc *crtc)
  2729. {
  2730. struct drm_device *dev = crtc->dev;
  2731. struct drm_i915_private *dev_priv = dev->dev_private;
  2732. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2733. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  2734. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  2735. lpt_program_iclkip(crtc);
  2736. /* Set transcoder timing. */
  2737. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  2738. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  2739. }
  2740. static void intel_put_shared_dpll(struct intel_crtc *crtc)
  2741. {
  2742. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  2743. if (pll == NULL)
  2744. return;
  2745. if (pll->refcount == 0) {
  2746. WARN(1, "bad %s refcount\n", pll->name);
  2747. return;
  2748. }
  2749. if (--pll->refcount == 0) {
  2750. WARN_ON(pll->on);
  2751. WARN_ON(pll->active);
  2752. }
  2753. crtc->config.shared_dpll = DPLL_ID_PRIVATE;
  2754. }
  2755. static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
  2756. {
  2757. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  2758. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  2759. enum intel_dpll_id i;
  2760. if (pll) {
  2761. DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
  2762. crtc->base.base.id, pll->name);
  2763. intel_put_shared_dpll(crtc);
  2764. }
  2765. if (HAS_PCH_IBX(dev_priv->dev)) {
  2766. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  2767. i = (enum intel_dpll_id) crtc->pipe;
  2768. pll = &dev_priv->shared_dplls[i];
  2769. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  2770. crtc->base.base.id, pll->name);
  2771. goto found;
  2772. }
  2773. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  2774. pll = &dev_priv->shared_dplls[i];
  2775. /* Only want to check enabled timings first */
  2776. if (pll->refcount == 0)
  2777. continue;
  2778. if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
  2779. sizeof(pll->hw_state)) == 0) {
  2780. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
  2781. crtc->base.base.id,
  2782. pll->name, pll->refcount, pll->active);
  2783. goto found;
  2784. }
  2785. }
  2786. /* Ok no matching timings, maybe there's a free one? */
  2787. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  2788. pll = &dev_priv->shared_dplls[i];
  2789. if (pll->refcount == 0) {
  2790. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  2791. crtc->base.base.id, pll->name);
  2792. goto found;
  2793. }
  2794. }
  2795. return NULL;
  2796. found:
  2797. crtc->config.shared_dpll = i;
  2798. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  2799. pipe_name(crtc->pipe));
  2800. if (pll->active == 0) {
  2801. memcpy(&pll->hw_state, &crtc->config.dpll_hw_state,
  2802. sizeof(pll->hw_state));
  2803. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  2804. WARN_ON(pll->on);
  2805. assert_shared_dpll_disabled(dev_priv, pll);
  2806. pll->mode_set(dev_priv, pll);
  2807. }
  2808. pll->refcount++;
  2809. return pll;
  2810. }
  2811. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  2812. {
  2813. struct drm_i915_private *dev_priv = dev->dev_private;
  2814. int dslreg = PIPEDSL(pipe);
  2815. u32 temp;
  2816. temp = I915_READ(dslreg);
  2817. udelay(500);
  2818. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  2819. if (wait_for(I915_READ(dslreg) != temp, 5))
  2820. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  2821. }
  2822. }
  2823. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  2824. {
  2825. struct drm_device *dev = crtc->base.dev;
  2826. struct drm_i915_private *dev_priv = dev->dev_private;
  2827. int pipe = crtc->pipe;
  2828. if (crtc->config.pch_pfit.enabled) {
  2829. /* Force use of hard-coded filter coefficients
  2830. * as some pre-programmed values are broken,
  2831. * e.g. x201.
  2832. */
  2833. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  2834. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  2835. PF_PIPE_SEL_IVB(pipe));
  2836. else
  2837. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  2838. I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
  2839. I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
  2840. }
  2841. }
  2842. static void intel_enable_planes(struct drm_crtc *crtc)
  2843. {
  2844. struct drm_device *dev = crtc->dev;
  2845. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  2846. struct intel_plane *intel_plane;
  2847. list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
  2848. if (intel_plane->pipe == pipe)
  2849. intel_plane_restore(&intel_plane->base);
  2850. }
  2851. static void intel_disable_planes(struct drm_crtc *crtc)
  2852. {
  2853. struct drm_device *dev = crtc->dev;
  2854. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  2855. struct intel_plane *intel_plane;
  2856. list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
  2857. if (intel_plane->pipe == pipe)
  2858. intel_plane_disable(&intel_plane->base);
  2859. }
  2860. void hsw_enable_ips(struct intel_crtc *crtc)
  2861. {
  2862. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  2863. if (!crtc->config.ips_enabled)
  2864. return;
  2865. /* We can only enable IPS after we enable a plane and wait for a vblank.
  2866. * We guarantee that the plane is enabled by calling intel_enable_ips
  2867. * only after intel_enable_plane. And intel_enable_plane already waits
  2868. * for a vblank, so all we need to do here is to enable the IPS bit. */
  2869. assert_plane_enabled(dev_priv, crtc->plane);
  2870. I915_WRITE(IPS_CTL, IPS_ENABLE);
  2871. }
  2872. void hsw_disable_ips(struct intel_crtc *crtc)
  2873. {
  2874. struct drm_device *dev = crtc->base.dev;
  2875. struct drm_i915_private *dev_priv = dev->dev_private;
  2876. if (!crtc->config.ips_enabled)
  2877. return;
  2878. assert_plane_enabled(dev_priv, crtc->plane);
  2879. I915_WRITE(IPS_CTL, 0);
  2880. POSTING_READ(IPS_CTL);
  2881. /* We need to wait for a vblank before we can disable the plane. */
  2882. intel_wait_for_vblank(dev, crtc->pipe);
  2883. }
  2884. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  2885. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  2886. {
  2887. struct drm_device *dev = crtc->dev;
  2888. struct drm_i915_private *dev_priv = dev->dev_private;
  2889. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2890. enum pipe pipe = intel_crtc->pipe;
  2891. int palreg = PALETTE(pipe);
  2892. int i;
  2893. bool reenable_ips = false;
  2894. /* The clocks have to be on to load the palette. */
  2895. if (!crtc->enabled || !intel_crtc->active)
  2896. return;
  2897. if (!HAS_PCH_SPLIT(dev_priv->dev)) {
  2898. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  2899. assert_dsi_pll_enabled(dev_priv);
  2900. else
  2901. assert_pll_enabled(dev_priv, pipe);
  2902. }
  2903. /* use legacy palette for Ironlake */
  2904. if (HAS_PCH_SPLIT(dev))
  2905. palreg = LGC_PALETTE(pipe);
  2906. /* Workaround : Do not read or write the pipe palette/gamma data while
  2907. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  2908. */
  2909. if (intel_crtc->config.ips_enabled &&
  2910. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  2911. GAMMA_MODE_MODE_SPLIT)) {
  2912. hsw_disable_ips(intel_crtc);
  2913. reenable_ips = true;
  2914. }
  2915. for (i = 0; i < 256; i++) {
  2916. I915_WRITE(palreg + 4 * i,
  2917. (intel_crtc->lut_r[i] << 16) |
  2918. (intel_crtc->lut_g[i] << 8) |
  2919. intel_crtc->lut_b[i]);
  2920. }
  2921. if (reenable_ips)
  2922. hsw_enable_ips(intel_crtc);
  2923. }
  2924. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  2925. {
  2926. struct drm_device *dev = crtc->dev;
  2927. struct drm_i915_private *dev_priv = dev->dev_private;
  2928. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2929. struct intel_encoder *encoder;
  2930. int pipe = intel_crtc->pipe;
  2931. int plane = intel_crtc->plane;
  2932. WARN_ON(!crtc->enabled);
  2933. if (intel_crtc->active)
  2934. return;
  2935. intel_crtc->active = true;
  2936. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  2937. intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
  2938. for_each_encoder_on_crtc(dev, crtc, encoder)
  2939. if (encoder->pre_enable)
  2940. encoder->pre_enable(encoder);
  2941. if (intel_crtc->config.has_pch_encoder) {
  2942. /* Note: FDI PLL enabling _must_ be done before we enable the
  2943. * cpu pipes, hence this is separate from all the other fdi/pch
  2944. * enabling. */
  2945. ironlake_fdi_pll_enable(intel_crtc);
  2946. } else {
  2947. assert_fdi_tx_disabled(dev_priv, pipe);
  2948. assert_fdi_rx_disabled(dev_priv, pipe);
  2949. }
  2950. ironlake_pfit_enable(intel_crtc);
  2951. /*
  2952. * On ILK+ LUT must be loaded before the pipe is running but with
  2953. * clocks enabled
  2954. */
  2955. intel_crtc_load_lut(crtc);
  2956. intel_update_watermarks(crtc);
  2957. intel_enable_pipe(dev_priv, pipe,
  2958. intel_crtc->config.has_pch_encoder, false);
  2959. intel_enable_primary_plane(dev_priv, plane, pipe);
  2960. intel_enable_planes(crtc);
  2961. intel_crtc_update_cursor(crtc, true);
  2962. if (intel_crtc->config.has_pch_encoder)
  2963. ironlake_pch_enable(crtc);
  2964. mutex_lock(&dev->struct_mutex);
  2965. intel_update_fbc(dev);
  2966. mutex_unlock(&dev->struct_mutex);
  2967. for_each_encoder_on_crtc(dev, crtc, encoder)
  2968. encoder->enable(encoder);
  2969. if (HAS_PCH_CPT(dev))
  2970. cpt_verify_modeset(dev, intel_crtc->pipe);
  2971. /*
  2972. * There seems to be a race in PCH platform hw (at least on some
  2973. * outputs) where an enabled pipe still completes any pageflip right
  2974. * away (as if the pipe is off) instead of waiting for vblank. As soon
  2975. * as the first vblank happend, everything works as expected. Hence just
  2976. * wait for one vblank before returning to avoid strange things
  2977. * happening.
  2978. */
  2979. intel_wait_for_vblank(dev, intel_crtc->pipe);
  2980. }
  2981. /* IPS only exists on ULT machines and is tied to pipe A. */
  2982. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  2983. {
  2984. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  2985. }
  2986. static void haswell_crtc_enable_planes(struct drm_crtc *crtc)
  2987. {
  2988. struct drm_device *dev = crtc->dev;
  2989. struct drm_i915_private *dev_priv = dev->dev_private;
  2990. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2991. int pipe = intel_crtc->pipe;
  2992. int plane = intel_crtc->plane;
  2993. intel_enable_primary_plane(dev_priv, plane, pipe);
  2994. intel_enable_planes(crtc);
  2995. intel_crtc_update_cursor(crtc, true);
  2996. hsw_enable_ips(intel_crtc);
  2997. mutex_lock(&dev->struct_mutex);
  2998. intel_update_fbc(dev);
  2999. mutex_unlock(&dev->struct_mutex);
  3000. }
  3001. static void haswell_crtc_disable_planes(struct drm_crtc *crtc)
  3002. {
  3003. struct drm_device *dev = crtc->dev;
  3004. struct drm_i915_private *dev_priv = dev->dev_private;
  3005. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3006. int pipe = intel_crtc->pipe;
  3007. int plane = intel_crtc->plane;
  3008. intel_crtc_wait_for_pending_flips(crtc);
  3009. drm_vblank_off(dev, pipe);
  3010. /* FBC must be disabled before disabling the plane on HSW. */
  3011. if (dev_priv->fbc.plane == plane)
  3012. intel_disable_fbc(dev);
  3013. hsw_disable_ips(intel_crtc);
  3014. intel_crtc_update_cursor(crtc, false);
  3015. intel_disable_planes(crtc);
  3016. intel_disable_primary_plane(dev_priv, plane, pipe);
  3017. }
  3018. /*
  3019. * This implements the workaround described in the "notes" section of the mode
  3020. * set sequence documentation. When going from no pipes or single pipe to
  3021. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  3022. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  3023. */
  3024. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  3025. {
  3026. struct drm_device *dev = crtc->base.dev;
  3027. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  3028. /* We want to get the other_active_crtc only if there's only 1 other
  3029. * active crtc. */
  3030. list_for_each_entry(crtc_it, &dev->mode_config.crtc_list, base.head) {
  3031. if (!crtc_it->active || crtc_it == crtc)
  3032. continue;
  3033. if (other_active_crtc)
  3034. return;
  3035. other_active_crtc = crtc_it;
  3036. }
  3037. if (!other_active_crtc)
  3038. return;
  3039. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3040. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3041. }
  3042. static void haswell_crtc_enable(struct drm_crtc *crtc)
  3043. {
  3044. struct drm_device *dev = crtc->dev;
  3045. struct drm_i915_private *dev_priv = dev->dev_private;
  3046. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3047. struct intel_encoder *encoder;
  3048. int pipe = intel_crtc->pipe;
  3049. WARN_ON(!crtc->enabled);
  3050. if (intel_crtc->active)
  3051. return;
  3052. intel_crtc->active = true;
  3053. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3054. if (intel_crtc->config.has_pch_encoder)
  3055. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
  3056. if (intel_crtc->config.has_pch_encoder)
  3057. dev_priv->display.fdi_link_train(crtc);
  3058. for_each_encoder_on_crtc(dev, crtc, encoder)
  3059. if (encoder->pre_enable)
  3060. encoder->pre_enable(encoder);
  3061. intel_ddi_enable_pipe_clock(intel_crtc);
  3062. ironlake_pfit_enable(intel_crtc);
  3063. /*
  3064. * On ILK+ LUT must be loaded before the pipe is running but with
  3065. * clocks enabled
  3066. */
  3067. intel_crtc_load_lut(crtc);
  3068. intel_ddi_set_pipe_settings(crtc);
  3069. intel_ddi_enable_transcoder_func(crtc);
  3070. intel_update_watermarks(crtc);
  3071. intel_enable_pipe(dev_priv, pipe,
  3072. intel_crtc->config.has_pch_encoder, false);
  3073. if (intel_crtc->config.has_pch_encoder)
  3074. lpt_pch_enable(crtc);
  3075. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3076. encoder->enable(encoder);
  3077. intel_opregion_notify_encoder(encoder, true);
  3078. }
  3079. /* If we change the relative order between pipe/planes enabling, we need
  3080. * to change the workaround. */
  3081. haswell_mode_set_planes_workaround(intel_crtc);
  3082. haswell_crtc_enable_planes(crtc);
  3083. /*
  3084. * There seems to be a race in PCH platform hw (at least on some
  3085. * outputs) where an enabled pipe still completes any pageflip right
  3086. * away (as if the pipe is off) instead of waiting for vblank. As soon
  3087. * as the first vblank happend, everything works as expected. Hence just
  3088. * wait for one vblank before returning to avoid strange things
  3089. * happening.
  3090. */
  3091. intel_wait_for_vblank(dev, intel_crtc->pipe);
  3092. }
  3093. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  3094. {
  3095. struct drm_device *dev = crtc->base.dev;
  3096. struct drm_i915_private *dev_priv = dev->dev_private;
  3097. int pipe = crtc->pipe;
  3098. /* To avoid upsetting the power well on haswell only disable the pfit if
  3099. * it's in use. The hw state code will make sure we get this right. */
  3100. if (crtc->config.pch_pfit.enabled) {
  3101. I915_WRITE(PF_CTL(pipe), 0);
  3102. I915_WRITE(PF_WIN_POS(pipe), 0);
  3103. I915_WRITE(PF_WIN_SZ(pipe), 0);
  3104. }
  3105. }
  3106. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  3107. {
  3108. struct drm_device *dev = crtc->dev;
  3109. struct drm_i915_private *dev_priv = dev->dev_private;
  3110. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3111. struct intel_encoder *encoder;
  3112. int pipe = intel_crtc->pipe;
  3113. int plane = intel_crtc->plane;
  3114. u32 reg, temp;
  3115. if (!intel_crtc->active)
  3116. return;
  3117. for_each_encoder_on_crtc(dev, crtc, encoder)
  3118. encoder->disable(encoder);
  3119. intel_crtc_wait_for_pending_flips(crtc);
  3120. drm_vblank_off(dev, pipe);
  3121. if (dev_priv->fbc.plane == plane)
  3122. intel_disable_fbc(dev);
  3123. intel_crtc_update_cursor(crtc, false);
  3124. intel_disable_planes(crtc);
  3125. intel_disable_primary_plane(dev_priv, plane, pipe);
  3126. if (intel_crtc->config.has_pch_encoder)
  3127. intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
  3128. intel_disable_pipe(dev_priv, pipe);
  3129. ironlake_pfit_disable(intel_crtc);
  3130. for_each_encoder_on_crtc(dev, crtc, encoder)
  3131. if (encoder->post_disable)
  3132. encoder->post_disable(encoder);
  3133. if (intel_crtc->config.has_pch_encoder) {
  3134. ironlake_fdi_disable(crtc);
  3135. ironlake_disable_pch_transcoder(dev_priv, pipe);
  3136. intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
  3137. if (HAS_PCH_CPT(dev)) {
  3138. /* disable TRANS_DP_CTL */
  3139. reg = TRANS_DP_CTL(pipe);
  3140. temp = I915_READ(reg);
  3141. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  3142. TRANS_DP_PORT_SEL_MASK);
  3143. temp |= TRANS_DP_PORT_SEL_NONE;
  3144. I915_WRITE(reg, temp);
  3145. /* disable DPLL_SEL */
  3146. temp = I915_READ(PCH_DPLL_SEL);
  3147. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  3148. I915_WRITE(PCH_DPLL_SEL, temp);
  3149. }
  3150. /* disable PCH DPLL */
  3151. intel_disable_shared_dpll(intel_crtc);
  3152. ironlake_fdi_pll_disable(intel_crtc);
  3153. }
  3154. intel_crtc->active = false;
  3155. intel_update_watermarks(crtc);
  3156. mutex_lock(&dev->struct_mutex);
  3157. intel_update_fbc(dev);
  3158. mutex_unlock(&dev->struct_mutex);
  3159. }
  3160. static void haswell_crtc_disable(struct drm_crtc *crtc)
  3161. {
  3162. struct drm_device *dev = crtc->dev;
  3163. struct drm_i915_private *dev_priv = dev->dev_private;
  3164. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3165. struct intel_encoder *encoder;
  3166. int pipe = intel_crtc->pipe;
  3167. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3168. if (!intel_crtc->active)
  3169. return;
  3170. haswell_crtc_disable_planes(crtc);
  3171. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3172. intel_opregion_notify_encoder(encoder, false);
  3173. encoder->disable(encoder);
  3174. }
  3175. if (intel_crtc->config.has_pch_encoder)
  3176. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
  3177. intel_disable_pipe(dev_priv, pipe);
  3178. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  3179. ironlake_pfit_disable(intel_crtc);
  3180. intel_ddi_disable_pipe_clock(intel_crtc);
  3181. for_each_encoder_on_crtc(dev, crtc, encoder)
  3182. if (encoder->post_disable)
  3183. encoder->post_disable(encoder);
  3184. if (intel_crtc->config.has_pch_encoder) {
  3185. lpt_disable_pch_transcoder(dev_priv);
  3186. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
  3187. intel_ddi_fdi_disable(crtc);
  3188. }
  3189. intel_crtc->active = false;
  3190. intel_update_watermarks(crtc);
  3191. mutex_lock(&dev->struct_mutex);
  3192. intel_update_fbc(dev);
  3193. mutex_unlock(&dev->struct_mutex);
  3194. }
  3195. static void ironlake_crtc_off(struct drm_crtc *crtc)
  3196. {
  3197. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3198. intel_put_shared_dpll(intel_crtc);
  3199. }
  3200. static void haswell_crtc_off(struct drm_crtc *crtc)
  3201. {
  3202. intel_ddi_put_crtc_pll(crtc);
  3203. }
  3204. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  3205. {
  3206. if (!enable && intel_crtc->overlay) {
  3207. struct drm_device *dev = intel_crtc->base.dev;
  3208. struct drm_i915_private *dev_priv = dev->dev_private;
  3209. mutex_lock(&dev->struct_mutex);
  3210. dev_priv->mm.interruptible = false;
  3211. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3212. dev_priv->mm.interruptible = true;
  3213. mutex_unlock(&dev->struct_mutex);
  3214. }
  3215. /* Let userspace switch the overlay on again. In most cases userspace
  3216. * has to recompute where to put it anyway.
  3217. */
  3218. }
  3219. /**
  3220. * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
  3221. * cursor plane briefly if not already running after enabling the display
  3222. * plane.
  3223. * This workaround avoids occasional blank screens when self refresh is
  3224. * enabled.
  3225. */
  3226. static void
  3227. g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
  3228. {
  3229. u32 cntl = I915_READ(CURCNTR(pipe));
  3230. if ((cntl & CURSOR_MODE) == 0) {
  3231. u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
  3232. I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
  3233. I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
  3234. intel_wait_for_vblank(dev_priv->dev, pipe);
  3235. I915_WRITE(CURCNTR(pipe), cntl);
  3236. I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
  3237. I915_WRITE(FW_BLC_SELF, fw_bcl_self);
  3238. }
  3239. }
  3240. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  3241. {
  3242. struct drm_device *dev = crtc->base.dev;
  3243. struct drm_i915_private *dev_priv = dev->dev_private;
  3244. struct intel_crtc_config *pipe_config = &crtc->config;
  3245. if (!crtc->config.gmch_pfit.control)
  3246. return;
  3247. /*
  3248. * The panel fitter should only be adjusted whilst the pipe is disabled,
  3249. * according to register description and PRM.
  3250. */
  3251. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  3252. assert_pipe_disabled(dev_priv, crtc->pipe);
  3253. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  3254. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  3255. /* Border color in case we don't scale up to the full screen. Black by
  3256. * default, change to something else for debugging. */
  3257. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  3258. }
  3259. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  3260. {
  3261. struct drm_device *dev = crtc->dev;
  3262. struct drm_i915_private *dev_priv = dev->dev_private;
  3263. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3264. struct intel_encoder *encoder;
  3265. int pipe = intel_crtc->pipe;
  3266. int plane = intel_crtc->plane;
  3267. bool is_dsi;
  3268. WARN_ON(!crtc->enabled);
  3269. if (intel_crtc->active)
  3270. return;
  3271. intel_crtc->active = true;
  3272. for_each_encoder_on_crtc(dev, crtc, encoder)
  3273. if (encoder->pre_pll_enable)
  3274. encoder->pre_pll_enable(encoder);
  3275. is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
  3276. if (!is_dsi)
  3277. vlv_enable_pll(intel_crtc);
  3278. for_each_encoder_on_crtc(dev, crtc, encoder)
  3279. if (encoder->pre_enable)
  3280. encoder->pre_enable(encoder);
  3281. i9xx_pfit_enable(intel_crtc);
  3282. intel_crtc_load_lut(crtc);
  3283. intel_update_watermarks(crtc);
  3284. intel_enable_pipe(dev_priv, pipe, false, is_dsi);
  3285. intel_enable_primary_plane(dev_priv, plane, pipe);
  3286. intel_enable_planes(crtc);
  3287. intel_crtc_update_cursor(crtc, true);
  3288. intel_update_fbc(dev);
  3289. for_each_encoder_on_crtc(dev, crtc, encoder)
  3290. encoder->enable(encoder);
  3291. }
  3292. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  3293. {
  3294. struct drm_device *dev = crtc->dev;
  3295. struct drm_i915_private *dev_priv = dev->dev_private;
  3296. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3297. struct intel_encoder *encoder;
  3298. int pipe = intel_crtc->pipe;
  3299. int plane = intel_crtc->plane;
  3300. WARN_ON(!crtc->enabled);
  3301. if (intel_crtc->active)
  3302. return;
  3303. intel_crtc->active = true;
  3304. for_each_encoder_on_crtc(dev, crtc, encoder)
  3305. if (encoder->pre_enable)
  3306. encoder->pre_enable(encoder);
  3307. i9xx_enable_pll(intel_crtc);
  3308. i9xx_pfit_enable(intel_crtc);
  3309. intel_crtc_load_lut(crtc);
  3310. intel_update_watermarks(crtc);
  3311. intel_enable_pipe(dev_priv, pipe, false, false);
  3312. intel_enable_primary_plane(dev_priv, plane, pipe);
  3313. intel_enable_planes(crtc);
  3314. /* The fixup needs to happen before cursor is enabled */
  3315. if (IS_G4X(dev))
  3316. g4x_fixup_plane(dev_priv, pipe);
  3317. intel_crtc_update_cursor(crtc, true);
  3318. /* Give the overlay scaler a chance to enable if it's on this pipe */
  3319. intel_crtc_dpms_overlay(intel_crtc, true);
  3320. intel_update_fbc(dev);
  3321. for_each_encoder_on_crtc(dev, crtc, encoder)
  3322. encoder->enable(encoder);
  3323. }
  3324. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  3325. {
  3326. struct drm_device *dev = crtc->base.dev;
  3327. struct drm_i915_private *dev_priv = dev->dev_private;
  3328. if (!crtc->config.gmch_pfit.control)
  3329. return;
  3330. assert_pipe_disabled(dev_priv, crtc->pipe);
  3331. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  3332. I915_READ(PFIT_CONTROL));
  3333. I915_WRITE(PFIT_CONTROL, 0);
  3334. }
  3335. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  3336. {
  3337. struct drm_device *dev = crtc->dev;
  3338. struct drm_i915_private *dev_priv = dev->dev_private;
  3339. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3340. struct intel_encoder *encoder;
  3341. int pipe = intel_crtc->pipe;
  3342. int plane = intel_crtc->plane;
  3343. if (!intel_crtc->active)
  3344. return;
  3345. for_each_encoder_on_crtc(dev, crtc, encoder)
  3346. encoder->disable(encoder);
  3347. /* Give the overlay scaler a chance to disable if it's on this pipe */
  3348. intel_crtc_wait_for_pending_flips(crtc);
  3349. drm_vblank_off(dev, pipe);
  3350. if (dev_priv->fbc.plane == plane)
  3351. intel_disable_fbc(dev);
  3352. intel_crtc_dpms_overlay(intel_crtc, false);
  3353. intel_crtc_update_cursor(crtc, false);
  3354. intel_disable_planes(crtc);
  3355. intel_disable_primary_plane(dev_priv, plane, pipe);
  3356. intel_disable_pipe(dev_priv, pipe);
  3357. i9xx_pfit_disable(intel_crtc);
  3358. for_each_encoder_on_crtc(dev, crtc, encoder)
  3359. if (encoder->post_disable)
  3360. encoder->post_disable(encoder);
  3361. if (IS_VALLEYVIEW(dev) && !intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  3362. vlv_disable_pll(dev_priv, pipe);
  3363. else if (!IS_VALLEYVIEW(dev))
  3364. i9xx_disable_pll(dev_priv, pipe);
  3365. intel_crtc->active = false;
  3366. intel_update_watermarks(crtc);
  3367. intel_update_fbc(dev);
  3368. }
  3369. static void i9xx_crtc_off(struct drm_crtc *crtc)
  3370. {
  3371. }
  3372. static void intel_crtc_update_sarea(struct drm_crtc *crtc,
  3373. bool enabled)
  3374. {
  3375. struct drm_device *dev = crtc->dev;
  3376. struct drm_i915_master_private *master_priv;
  3377. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3378. int pipe = intel_crtc->pipe;
  3379. if (!dev->primary->master)
  3380. return;
  3381. master_priv = dev->primary->master->driver_priv;
  3382. if (!master_priv->sarea_priv)
  3383. return;
  3384. switch (pipe) {
  3385. case 0:
  3386. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  3387. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  3388. break;
  3389. case 1:
  3390. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  3391. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  3392. break;
  3393. default:
  3394. DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
  3395. break;
  3396. }
  3397. }
  3398. /**
  3399. * Sets the power management mode of the pipe and plane.
  3400. */
  3401. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  3402. {
  3403. struct drm_device *dev = crtc->dev;
  3404. struct drm_i915_private *dev_priv = dev->dev_private;
  3405. struct intel_encoder *intel_encoder;
  3406. bool enable = false;
  3407. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  3408. enable |= intel_encoder->connectors_active;
  3409. if (enable)
  3410. dev_priv->display.crtc_enable(crtc);
  3411. else
  3412. dev_priv->display.crtc_disable(crtc);
  3413. intel_crtc_update_sarea(crtc, enable);
  3414. }
  3415. static void intel_crtc_disable(struct drm_crtc *crtc)
  3416. {
  3417. struct drm_device *dev = crtc->dev;
  3418. struct drm_connector *connector;
  3419. struct drm_i915_private *dev_priv = dev->dev_private;
  3420. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3421. /* crtc should still be enabled when we disable it. */
  3422. WARN_ON(!crtc->enabled);
  3423. dev_priv->display.crtc_disable(crtc);
  3424. intel_crtc->eld_vld = false;
  3425. intel_crtc_update_sarea(crtc, false);
  3426. dev_priv->display.off(crtc);
  3427. assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
  3428. assert_cursor_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
  3429. assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
  3430. if (crtc->fb) {
  3431. mutex_lock(&dev->struct_mutex);
  3432. intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
  3433. mutex_unlock(&dev->struct_mutex);
  3434. crtc->fb = NULL;
  3435. }
  3436. /* Update computed state. */
  3437. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  3438. if (!connector->encoder || !connector->encoder->crtc)
  3439. continue;
  3440. if (connector->encoder->crtc != crtc)
  3441. continue;
  3442. connector->dpms = DRM_MODE_DPMS_OFF;
  3443. to_intel_encoder(connector->encoder)->connectors_active = false;
  3444. }
  3445. }
  3446. void intel_encoder_destroy(struct drm_encoder *encoder)
  3447. {
  3448. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  3449. drm_encoder_cleanup(encoder);
  3450. kfree(intel_encoder);
  3451. }
  3452. /* Simple dpms helper for encoders with just one connector, no cloning and only
  3453. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  3454. * state of the entire output pipe. */
  3455. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  3456. {
  3457. if (mode == DRM_MODE_DPMS_ON) {
  3458. encoder->connectors_active = true;
  3459. intel_crtc_update_dpms(encoder->base.crtc);
  3460. } else {
  3461. encoder->connectors_active = false;
  3462. intel_crtc_update_dpms(encoder->base.crtc);
  3463. }
  3464. }
  3465. /* Cross check the actual hw state with our own modeset state tracking (and it's
  3466. * internal consistency). */
  3467. static void intel_connector_check_state(struct intel_connector *connector)
  3468. {
  3469. if (connector->get_hw_state(connector)) {
  3470. struct intel_encoder *encoder = connector->encoder;
  3471. struct drm_crtc *crtc;
  3472. bool encoder_enabled;
  3473. enum pipe pipe;
  3474. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  3475. connector->base.base.id,
  3476. drm_get_connector_name(&connector->base));
  3477. WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  3478. "wrong connector dpms state\n");
  3479. WARN(connector->base.encoder != &encoder->base,
  3480. "active connector not linked to encoder\n");
  3481. WARN(!encoder->connectors_active,
  3482. "encoder->connectors_active not set\n");
  3483. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  3484. WARN(!encoder_enabled, "encoder not enabled\n");
  3485. if (WARN_ON(!encoder->base.crtc))
  3486. return;
  3487. crtc = encoder->base.crtc;
  3488. WARN(!crtc->enabled, "crtc not enabled\n");
  3489. WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  3490. WARN(pipe != to_intel_crtc(crtc)->pipe,
  3491. "encoder active on the wrong pipe\n");
  3492. }
  3493. }
  3494. /* Even simpler default implementation, if there's really no special case to
  3495. * consider. */
  3496. void intel_connector_dpms(struct drm_connector *connector, int mode)
  3497. {
  3498. struct intel_encoder *encoder = intel_attached_encoder(connector);
  3499. /* All the simple cases only support two dpms states. */
  3500. if (mode != DRM_MODE_DPMS_ON)
  3501. mode = DRM_MODE_DPMS_OFF;
  3502. if (mode == connector->dpms)
  3503. return;
  3504. connector->dpms = mode;
  3505. /* Only need to change hw state when actually enabled */
  3506. if (encoder->base.crtc)
  3507. intel_encoder_dpms(encoder, mode);
  3508. else
  3509. WARN_ON(encoder->connectors_active != false);
  3510. intel_modeset_check_state(connector->dev);
  3511. }
  3512. /* Simple connector->get_hw_state implementation for encoders that support only
  3513. * one connector and no cloning and hence the encoder state determines the state
  3514. * of the connector. */
  3515. bool intel_connector_get_hw_state(struct intel_connector *connector)
  3516. {
  3517. enum pipe pipe = 0;
  3518. struct intel_encoder *encoder = connector->encoder;
  3519. return encoder->get_hw_state(encoder, &pipe);
  3520. }
  3521. static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  3522. struct intel_crtc_config *pipe_config)
  3523. {
  3524. struct drm_i915_private *dev_priv = dev->dev_private;
  3525. struct intel_crtc *pipe_B_crtc =
  3526. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  3527. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  3528. pipe_name(pipe), pipe_config->fdi_lanes);
  3529. if (pipe_config->fdi_lanes > 4) {
  3530. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  3531. pipe_name(pipe), pipe_config->fdi_lanes);
  3532. return false;
  3533. }
  3534. if (IS_HASWELL(dev)) {
  3535. if (pipe_config->fdi_lanes > 2) {
  3536. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  3537. pipe_config->fdi_lanes);
  3538. return false;
  3539. } else {
  3540. return true;
  3541. }
  3542. }
  3543. if (INTEL_INFO(dev)->num_pipes == 2)
  3544. return true;
  3545. /* Ivybridge 3 pipe is really complicated */
  3546. switch (pipe) {
  3547. case PIPE_A:
  3548. return true;
  3549. case PIPE_B:
  3550. if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
  3551. pipe_config->fdi_lanes > 2) {
  3552. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  3553. pipe_name(pipe), pipe_config->fdi_lanes);
  3554. return false;
  3555. }
  3556. return true;
  3557. case PIPE_C:
  3558. if (!pipe_has_enabled_pch(pipe_B_crtc) ||
  3559. pipe_B_crtc->config.fdi_lanes <= 2) {
  3560. if (pipe_config->fdi_lanes > 2) {
  3561. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  3562. pipe_name(pipe), pipe_config->fdi_lanes);
  3563. return false;
  3564. }
  3565. } else {
  3566. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  3567. return false;
  3568. }
  3569. return true;
  3570. default:
  3571. BUG();
  3572. }
  3573. }
  3574. #define RETRY 1
  3575. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  3576. struct intel_crtc_config *pipe_config)
  3577. {
  3578. struct drm_device *dev = intel_crtc->base.dev;
  3579. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  3580. int lane, link_bw, fdi_dotclock;
  3581. bool setup_ok, needs_recompute = false;
  3582. retry:
  3583. /* FDI is a binary signal running at ~2.7GHz, encoding
  3584. * each output octet as 10 bits. The actual frequency
  3585. * is stored as a divider into a 100MHz clock, and the
  3586. * mode pixel clock is stored in units of 1KHz.
  3587. * Hence the bw of each lane in terms of the mode signal
  3588. * is:
  3589. */
  3590. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  3591. fdi_dotclock = adjusted_mode->crtc_clock;
  3592. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  3593. pipe_config->pipe_bpp);
  3594. pipe_config->fdi_lanes = lane;
  3595. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  3596. link_bw, &pipe_config->fdi_m_n);
  3597. setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  3598. intel_crtc->pipe, pipe_config);
  3599. if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
  3600. pipe_config->pipe_bpp -= 2*3;
  3601. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  3602. pipe_config->pipe_bpp);
  3603. needs_recompute = true;
  3604. pipe_config->bw_constrained = true;
  3605. goto retry;
  3606. }
  3607. if (needs_recompute)
  3608. return RETRY;
  3609. return setup_ok ? 0 : -EINVAL;
  3610. }
  3611. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  3612. struct intel_crtc_config *pipe_config)
  3613. {
  3614. pipe_config->ips_enabled = i915_enable_ips &&
  3615. hsw_crtc_supports_ips(crtc) &&
  3616. pipe_config->pipe_bpp <= 24;
  3617. }
  3618. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  3619. struct intel_crtc_config *pipe_config)
  3620. {
  3621. struct drm_device *dev = crtc->base.dev;
  3622. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  3623. /* FIXME should check pixel clock limits on all platforms */
  3624. if (INTEL_INFO(dev)->gen < 4) {
  3625. struct drm_i915_private *dev_priv = dev->dev_private;
  3626. int clock_limit =
  3627. dev_priv->display.get_display_clock_speed(dev);
  3628. /*
  3629. * Enable pixel doubling when the dot clock
  3630. * is > 90% of the (display) core speed.
  3631. *
  3632. * GDG double wide on either pipe,
  3633. * otherwise pipe A only.
  3634. */
  3635. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  3636. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  3637. clock_limit *= 2;
  3638. pipe_config->double_wide = true;
  3639. }
  3640. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  3641. return -EINVAL;
  3642. }
  3643. /*
  3644. * Pipe horizontal size must be even in:
  3645. * - DVO ganged mode
  3646. * - LVDS dual channel mode
  3647. * - Double wide pipe
  3648. */
  3649. if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  3650. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  3651. pipe_config->pipe_src_w &= ~1;
  3652. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  3653. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  3654. */
  3655. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  3656. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  3657. return -EINVAL;
  3658. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
  3659. pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
  3660. } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
  3661. /* only a 8bpc pipe, with 6bpc dither through the panel fitter
  3662. * for lvds. */
  3663. pipe_config->pipe_bpp = 8*3;
  3664. }
  3665. if (HAS_IPS(dev))
  3666. hsw_compute_ips_config(crtc, pipe_config);
  3667. /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
  3668. * clock survives for now. */
  3669. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  3670. pipe_config->shared_dpll = crtc->config.shared_dpll;
  3671. if (pipe_config->has_pch_encoder)
  3672. return ironlake_fdi_compute_config(crtc, pipe_config);
  3673. return 0;
  3674. }
  3675. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  3676. {
  3677. return 400000; /* FIXME */
  3678. }
  3679. static int i945_get_display_clock_speed(struct drm_device *dev)
  3680. {
  3681. return 400000;
  3682. }
  3683. static int i915_get_display_clock_speed(struct drm_device *dev)
  3684. {
  3685. return 333000;
  3686. }
  3687. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  3688. {
  3689. return 200000;
  3690. }
  3691. static int pnv_get_display_clock_speed(struct drm_device *dev)
  3692. {
  3693. u16 gcfgc = 0;
  3694. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  3695. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  3696. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  3697. return 267000;
  3698. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  3699. return 333000;
  3700. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  3701. return 444000;
  3702. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  3703. return 200000;
  3704. default:
  3705. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  3706. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  3707. return 133000;
  3708. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  3709. return 167000;
  3710. }
  3711. }
  3712. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  3713. {
  3714. u16 gcfgc = 0;
  3715. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  3716. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  3717. return 133000;
  3718. else {
  3719. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  3720. case GC_DISPLAY_CLOCK_333_MHZ:
  3721. return 333000;
  3722. default:
  3723. case GC_DISPLAY_CLOCK_190_200_MHZ:
  3724. return 190000;
  3725. }
  3726. }
  3727. }
  3728. static int i865_get_display_clock_speed(struct drm_device *dev)
  3729. {
  3730. return 266000;
  3731. }
  3732. static int i855_get_display_clock_speed(struct drm_device *dev)
  3733. {
  3734. u16 hpllcc = 0;
  3735. /* Assume that the hardware is in the high speed state. This
  3736. * should be the default.
  3737. */
  3738. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  3739. case GC_CLOCK_133_200:
  3740. case GC_CLOCK_100_200:
  3741. return 200000;
  3742. case GC_CLOCK_166_250:
  3743. return 250000;
  3744. case GC_CLOCK_100_133:
  3745. return 133000;
  3746. }
  3747. /* Shouldn't happen */
  3748. return 0;
  3749. }
  3750. static int i830_get_display_clock_speed(struct drm_device *dev)
  3751. {
  3752. return 133000;
  3753. }
  3754. static void
  3755. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  3756. {
  3757. while (*num > DATA_LINK_M_N_MASK ||
  3758. *den > DATA_LINK_M_N_MASK) {
  3759. *num >>= 1;
  3760. *den >>= 1;
  3761. }
  3762. }
  3763. static void compute_m_n(unsigned int m, unsigned int n,
  3764. uint32_t *ret_m, uint32_t *ret_n)
  3765. {
  3766. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  3767. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  3768. intel_reduce_m_n_ratio(ret_m, ret_n);
  3769. }
  3770. void
  3771. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  3772. int pixel_clock, int link_clock,
  3773. struct intel_link_m_n *m_n)
  3774. {
  3775. m_n->tu = 64;
  3776. compute_m_n(bits_per_pixel * pixel_clock,
  3777. link_clock * nlanes * 8,
  3778. &m_n->gmch_m, &m_n->gmch_n);
  3779. compute_m_n(pixel_clock, link_clock,
  3780. &m_n->link_m, &m_n->link_n);
  3781. }
  3782. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  3783. {
  3784. if (i915_panel_use_ssc >= 0)
  3785. return i915_panel_use_ssc != 0;
  3786. return dev_priv->vbt.lvds_use_ssc
  3787. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  3788. }
  3789. static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
  3790. {
  3791. struct drm_device *dev = crtc->dev;
  3792. struct drm_i915_private *dev_priv = dev->dev_private;
  3793. int refclk;
  3794. if (IS_VALLEYVIEW(dev)) {
  3795. refclk = 100000;
  3796. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  3797. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  3798. refclk = dev_priv->vbt.lvds_ssc_freq * 1000;
  3799. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  3800. refclk / 1000);
  3801. } else if (!IS_GEN2(dev)) {
  3802. refclk = 96000;
  3803. } else {
  3804. refclk = 48000;
  3805. }
  3806. return refclk;
  3807. }
  3808. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  3809. {
  3810. return (1 << dpll->n) << 16 | dpll->m2;
  3811. }
  3812. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  3813. {
  3814. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  3815. }
  3816. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  3817. intel_clock_t *reduced_clock)
  3818. {
  3819. struct drm_device *dev = crtc->base.dev;
  3820. struct drm_i915_private *dev_priv = dev->dev_private;
  3821. int pipe = crtc->pipe;
  3822. u32 fp, fp2 = 0;
  3823. if (IS_PINEVIEW(dev)) {
  3824. fp = pnv_dpll_compute_fp(&crtc->config.dpll);
  3825. if (reduced_clock)
  3826. fp2 = pnv_dpll_compute_fp(reduced_clock);
  3827. } else {
  3828. fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
  3829. if (reduced_clock)
  3830. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  3831. }
  3832. I915_WRITE(FP0(pipe), fp);
  3833. crtc->config.dpll_hw_state.fp0 = fp;
  3834. crtc->lowfreq_avail = false;
  3835. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  3836. reduced_clock && i915_powersave) {
  3837. I915_WRITE(FP1(pipe), fp2);
  3838. crtc->config.dpll_hw_state.fp1 = fp2;
  3839. crtc->lowfreq_avail = true;
  3840. } else {
  3841. I915_WRITE(FP1(pipe), fp);
  3842. crtc->config.dpll_hw_state.fp1 = fp;
  3843. }
  3844. }
  3845. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  3846. pipe)
  3847. {
  3848. u32 reg_val;
  3849. /*
  3850. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  3851. * and set it to a reasonable value instead.
  3852. */
  3853. reg_val = vlv_dpio_read(dev_priv, pipe, DPIO_IREF(1));
  3854. reg_val &= 0xffffff00;
  3855. reg_val |= 0x00000030;
  3856. vlv_dpio_write(dev_priv, pipe, DPIO_IREF(1), reg_val);
  3857. reg_val = vlv_dpio_read(dev_priv, pipe, DPIO_CALIBRATION);
  3858. reg_val &= 0x8cffffff;
  3859. reg_val = 0x8c000000;
  3860. vlv_dpio_write(dev_priv, pipe, DPIO_CALIBRATION, reg_val);
  3861. reg_val = vlv_dpio_read(dev_priv, pipe, DPIO_IREF(1));
  3862. reg_val &= 0xffffff00;
  3863. vlv_dpio_write(dev_priv, pipe, DPIO_IREF(1), reg_val);
  3864. reg_val = vlv_dpio_read(dev_priv, pipe, DPIO_CALIBRATION);
  3865. reg_val &= 0x00ffffff;
  3866. reg_val |= 0xb0000000;
  3867. vlv_dpio_write(dev_priv, pipe, DPIO_CALIBRATION, reg_val);
  3868. }
  3869. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  3870. struct intel_link_m_n *m_n)
  3871. {
  3872. struct drm_device *dev = crtc->base.dev;
  3873. struct drm_i915_private *dev_priv = dev->dev_private;
  3874. int pipe = crtc->pipe;
  3875. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  3876. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  3877. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  3878. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  3879. }
  3880. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  3881. struct intel_link_m_n *m_n)
  3882. {
  3883. struct drm_device *dev = crtc->base.dev;
  3884. struct drm_i915_private *dev_priv = dev->dev_private;
  3885. int pipe = crtc->pipe;
  3886. enum transcoder transcoder = crtc->config.cpu_transcoder;
  3887. if (INTEL_INFO(dev)->gen >= 5) {
  3888. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  3889. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  3890. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  3891. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  3892. } else {
  3893. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  3894. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  3895. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  3896. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  3897. }
  3898. }
  3899. static void intel_dp_set_m_n(struct intel_crtc *crtc)
  3900. {
  3901. if (crtc->config.has_pch_encoder)
  3902. intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
  3903. else
  3904. intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
  3905. }
  3906. static void vlv_update_pll(struct intel_crtc *crtc)
  3907. {
  3908. struct drm_device *dev = crtc->base.dev;
  3909. struct drm_i915_private *dev_priv = dev->dev_private;
  3910. int pipe = crtc->pipe;
  3911. u32 dpll, mdiv;
  3912. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  3913. u32 coreclk, reg_val, dpll_md;
  3914. mutex_lock(&dev_priv->dpio_lock);
  3915. bestn = crtc->config.dpll.n;
  3916. bestm1 = crtc->config.dpll.m1;
  3917. bestm2 = crtc->config.dpll.m2;
  3918. bestp1 = crtc->config.dpll.p1;
  3919. bestp2 = crtc->config.dpll.p2;
  3920. /* See eDP HDMI DPIO driver vbios notes doc */
  3921. /* PLL B needs special handling */
  3922. if (pipe)
  3923. vlv_pllb_recal_opamp(dev_priv, pipe);
  3924. /* Set up Tx target for periodic Rcomp update */
  3925. vlv_dpio_write(dev_priv, pipe, DPIO_IREF_BCAST, 0x0100000f);
  3926. /* Disable target IRef on PLL */
  3927. reg_val = vlv_dpio_read(dev_priv, pipe, DPIO_IREF_CTL(pipe));
  3928. reg_val &= 0x00ffffff;
  3929. vlv_dpio_write(dev_priv, pipe, DPIO_IREF_CTL(pipe), reg_val);
  3930. /* Disable fast lock */
  3931. vlv_dpio_write(dev_priv, pipe, DPIO_FASTCLK_DISABLE, 0x610);
  3932. /* Set idtafcrecal before PLL is enabled */
  3933. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  3934. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  3935. mdiv |= ((bestn << DPIO_N_SHIFT));
  3936. mdiv |= (1 << DPIO_K_SHIFT);
  3937. /*
  3938. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  3939. * but we don't support that).
  3940. * Note: don't use the DAC post divider as it seems unstable.
  3941. */
  3942. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  3943. vlv_dpio_write(dev_priv, pipe, DPIO_DIV(pipe), mdiv);
  3944. mdiv |= DPIO_ENABLE_CALIBRATION;
  3945. vlv_dpio_write(dev_priv, pipe, DPIO_DIV(pipe), mdiv);
  3946. /* Set HBR and RBR LPF coefficients */
  3947. if (crtc->config.port_clock == 162000 ||
  3948. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
  3949. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
  3950. vlv_dpio_write(dev_priv, pipe, DPIO_LPF_COEFF(pipe),
  3951. 0x009f0003);
  3952. else
  3953. vlv_dpio_write(dev_priv, pipe, DPIO_LPF_COEFF(pipe),
  3954. 0x00d0000f);
  3955. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
  3956. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
  3957. /* Use SSC source */
  3958. if (!pipe)
  3959. vlv_dpio_write(dev_priv, pipe, DPIO_REFSFR(pipe),
  3960. 0x0df40000);
  3961. else
  3962. vlv_dpio_write(dev_priv, pipe, DPIO_REFSFR(pipe),
  3963. 0x0df70000);
  3964. } else { /* HDMI or VGA */
  3965. /* Use bend source */
  3966. if (!pipe)
  3967. vlv_dpio_write(dev_priv, pipe, DPIO_REFSFR(pipe),
  3968. 0x0df70000);
  3969. else
  3970. vlv_dpio_write(dev_priv, pipe, DPIO_REFSFR(pipe),
  3971. 0x0df40000);
  3972. }
  3973. coreclk = vlv_dpio_read(dev_priv, pipe, DPIO_CORE_CLK(pipe));
  3974. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  3975. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
  3976. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
  3977. coreclk |= 0x01000000;
  3978. vlv_dpio_write(dev_priv, pipe, DPIO_CORE_CLK(pipe), coreclk);
  3979. vlv_dpio_write(dev_priv, pipe, DPIO_PLL_CML(pipe), 0x87871000);
  3980. /* Enable DPIO clock input */
  3981. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  3982. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  3983. /* We should never disable this, set it here for state tracking */
  3984. if (pipe == PIPE_B)
  3985. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  3986. dpll |= DPLL_VCO_ENABLE;
  3987. crtc->config.dpll_hw_state.dpll = dpll;
  3988. dpll_md = (crtc->config.pixel_multiplier - 1)
  3989. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  3990. crtc->config.dpll_hw_state.dpll_md = dpll_md;
  3991. if (crtc->config.has_dp_encoder)
  3992. intel_dp_set_m_n(crtc);
  3993. mutex_unlock(&dev_priv->dpio_lock);
  3994. }
  3995. static void i9xx_update_pll(struct intel_crtc *crtc,
  3996. intel_clock_t *reduced_clock,
  3997. int num_connectors)
  3998. {
  3999. struct drm_device *dev = crtc->base.dev;
  4000. struct drm_i915_private *dev_priv = dev->dev_private;
  4001. u32 dpll;
  4002. bool is_sdvo;
  4003. struct dpll *clock = &crtc->config.dpll;
  4004. i9xx_update_pll_dividers(crtc, reduced_clock);
  4005. is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
  4006. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
  4007. dpll = DPLL_VGA_MODE_DIS;
  4008. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
  4009. dpll |= DPLLB_MODE_LVDS;
  4010. else
  4011. dpll |= DPLLB_MODE_DAC_SERIAL;
  4012. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  4013. dpll |= (crtc->config.pixel_multiplier - 1)
  4014. << SDVO_MULTIPLIER_SHIFT_HIRES;
  4015. }
  4016. if (is_sdvo)
  4017. dpll |= DPLL_SDVO_HIGH_SPEED;
  4018. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
  4019. dpll |= DPLL_SDVO_HIGH_SPEED;
  4020. /* compute bitmask from p1 value */
  4021. if (IS_PINEVIEW(dev))
  4022. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  4023. else {
  4024. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4025. if (IS_G4X(dev) && reduced_clock)
  4026. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  4027. }
  4028. switch (clock->p2) {
  4029. case 5:
  4030. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  4031. break;
  4032. case 7:
  4033. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  4034. break;
  4035. case 10:
  4036. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  4037. break;
  4038. case 14:
  4039. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  4040. break;
  4041. }
  4042. if (INTEL_INFO(dev)->gen >= 4)
  4043. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  4044. if (crtc->config.sdvo_tv_clock)
  4045. dpll |= PLL_REF_INPUT_TVCLKINBC;
  4046. else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4047. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4048. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4049. else
  4050. dpll |= PLL_REF_INPUT_DREFCLK;
  4051. dpll |= DPLL_VCO_ENABLE;
  4052. crtc->config.dpll_hw_state.dpll = dpll;
  4053. if (INTEL_INFO(dev)->gen >= 4) {
  4054. u32 dpll_md = (crtc->config.pixel_multiplier - 1)
  4055. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4056. crtc->config.dpll_hw_state.dpll_md = dpll_md;
  4057. }
  4058. if (crtc->config.has_dp_encoder)
  4059. intel_dp_set_m_n(crtc);
  4060. }
  4061. static void i8xx_update_pll(struct intel_crtc *crtc,
  4062. intel_clock_t *reduced_clock,
  4063. int num_connectors)
  4064. {
  4065. struct drm_device *dev = crtc->base.dev;
  4066. struct drm_i915_private *dev_priv = dev->dev_private;
  4067. u32 dpll;
  4068. struct dpll *clock = &crtc->config.dpll;
  4069. i9xx_update_pll_dividers(crtc, reduced_clock);
  4070. dpll = DPLL_VGA_MODE_DIS;
  4071. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
  4072. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4073. } else {
  4074. if (clock->p1 == 2)
  4075. dpll |= PLL_P1_DIVIDE_BY_TWO;
  4076. else
  4077. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4078. if (clock->p2 == 4)
  4079. dpll |= PLL_P2_DIVIDE_BY_4;
  4080. }
  4081. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
  4082. dpll |= DPLL_DVO_2X_MODE;
  4083. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4084. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4085. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4086. else
  4087. dpll |= PLL_REF_INPUT_DREFCLK;
  4088. dpll |= DPLL_VCO_ENABLE;
  4089. crtc->config.dpll_hw_state.dpll = dpll;
  4090. }
  4091. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  4092. {
  4093. struct drm_device *dev = intel_crtc->base.dev;
  4094. struct drm_i915_private *dev_priv = dev->dev_private;
  4095. enum pipe pipe = intel_crtc->pipe;
  4096. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  4097. struct drm_display_mode *adjusted_mode =
  4098. &intel_crtc->config.adjusted_mode;
  4099. uint32_t vsyncshift, crtc_vtotal, crtc_vblank_end;
  4100. /* We need to be careful not to changed the adjusted mode, for otherwise
  4101. * the hw state checker will get angry at the mismatch. */
  4102. crtc_vtotal = adjusted_mode->crtc_vtotal;
  4103. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  4104. if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4105. /* the chip adds 2 halflines automatically */
  4106. crtc_vtotal -= 1;
  4107. crtc_vblank_end -= 1;
  4108. vsyncshift = adjusted_mode->crtc_hsync_start
  4109. - adjusted_mode->crtc_htotal / 2;
  4110. } else {
  4111. vsyncshift = 0;
  4112. }
  4113. if (INTEL_INFO(dev)->gen > 3)
  4114. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  4115. I915_WRITE(HTOTAL(cpu_transcoder),
  4116. (adjusted_mode->crtc_hdisplay - 1) |
  4117. ((adjusted_mode->crtc_htotal - 1) << 16));
  4118. I915_WRITE(HBLANK(cpu_transcoder),
  4119. (adjusted_mode->crtc_hblank_start - 1) |
  4120. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  4121. I915_WRITE(HSYNC(cpu_transcoder),
  4122. (adjusted_mode->crtc_hsync_start - 1) |
  4123. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  4124. I915_WRITE(VTOTAL(cpu_transcoder),
  4125. (adjusted_mode->crtc_vdisplay - 1) |
  4126. ((crtc_vtotal - 1) << 16));
  4127. I915_WRITE(VBLANK(cpu_transcoder),
  4128. (adjusted_mode->crtc_vblank_start - 1) |
  4129. ((crtc_vblank_end - 1) << 16));
  4130. I915_WRITE(VSYNC(cpu_transcoder),
  4131. (adjusted_mode->crtc_vsync_start - 1) |
  4132. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  4133. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  4134. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  4135. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  4136. * bits. */
  4137. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  4138. (pipe == PIPE_B || pipe == PIPE_C))
  4139. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  4140. /* pipesrc controls the size that is scaled from, which should
  4141. * always be the user's requested size.
  4142. */
  4143. I915_WRITE(PIPESRC(pipe),
  4144. ((intel_crtc->config.pipe_src_w - 1) << 16) |
  4145. (intel_crtc->config.pipe_src_h - 1));
  4146. }
  4147. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  4148. struct intel_crtc_config *pipe_config)
  4149. {
  4150. struct drm_device *dev = crtc->base.dev;
  4151. struct drm_i915_private *dev_priv = dev->dev_private;
  4152. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  4153. uint32_t tmp;
  4154. tmp = I915_READ(HTOTAL(cpu_transcoder));
  4155. pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  4156. pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  4157. tmp = I915_READ(HBLANK(cpu_transcoder));
  4158. pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  4159. pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  4160. tmp = I915_READ(HSYNC(cpu_transcoder));
  4161. pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  4162. pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  4163. tmp = I915_READ(VTOTAL(cpu_transcoder));
  4164. pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  4165. pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  4166. tmp = I915_READ(VBLANK(cpu_transcoder));
  4167. pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  4168. pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  4169. tmp = I915_READ(VSYNC(cpu_transcoder));
  4170. pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  4171. pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  4172. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  4173. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  4174. pipe_config->adjusted_mode.crtc_vtotal += 1;
  4175. pipe_config->adjusted_mode.crtc_vblank_end += 1;
  4176. }
  4177. tmp = I915_READ(PIPESRC(crtc->pipe));
  4178. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  4179. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  4180. pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
  4181. pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
  4182. }
  4183. static void intel_crtc_mode_from_pipe_config(struct intel_crtc *intel_crtc,
  4184. struct intel_crtc_config *pipe_config)
  4185. {
  4186. struct drm_crtc *crtc = &intel_crtc->base;
  4187. crtc->mode.hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
  4188. crtc->mode.htotal = pipe_config->adjusted_mode.crtc_htotal;
  4189. crtc->mode.hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
  4190. crtc->mode.hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
  4191. crtc->mode.vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
  4192. crtc->mode.vtotal = pipe_config->adjusted_mode.crtc_vtotal;
  4193. crtc->mode.vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
  4194. crtc->mode.vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
  4195. crtc->mode.flags = pipe_config->adjusted_mode.flags;
  4196. crtc->mode.clock = pipe_config->adjusted_mode.crtc_clock;
  4197. crtc->mode.flags |= pipe_config->adjusted_mode.flags;
  4198. }
  4199. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  4200. {
  4201. struct drm_device *dev = intel_crtc->base.dev;
  4202. struct drm_i915_private *dev_priv = dev->dev_private;
  4203. uint32_t pipeconf;
  4204. pipeconf = 0;
  4205. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  4206. I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
  4207. pipeconf |= PIPECONF_ENABLE;
  4208. if (intel_crtc->config.double_wide)
  4209. pipeconf |= PIPECONF_DOUBLE_WIDE;
  4210. /* only g4x and later have fancy bpc/dither controls */
  4211. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  4212. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  4213. if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
  4214. pipeconf |= PIPECONF_DITHER_EN |
  4215. PIPECONF_DITHER_TYPE_SP;
  4216. switch (intel_crtc->config.pipe_bpp) {
  4217. case 18:
  4218. pipeconf |= PIPECONF_6BPC;
  4219. break;
  4220. case 24:
  4221. pipeconf |= PIPECONF_8BPC;
  4222. break;
  4223. case 30:
  4224. pipeconf |= PIPECONF_10BPC;
  4225. break;
  4226. default:
  4227. /* Case prevented by intel_choose_pipe_bpp_dither. */
  4228. BUG();
  4229. }
  4230. }
  4231. if (HAS_PIPE_CXSR(dev)) {
  4232. if (intel_crtc->lowfreq_avail) {
  4233. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  4234. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  4235. } else {
  4236. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  4237. }
  4238. }
  4239. if (!IS_GEN2(dev) &&
  4240. intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  4241. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  4242. else
  4243. pipeconf |= PIPECONF_PROGRESSIVE;
  4244. if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
  4245. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  4246. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  4247. POSTING_READ(PIPECONF(intel_crtc->pipe));
  4248. }
  4249. static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
  4250. int x, int y,
  4251. struct drm_framebuffer *fb)
  4252. {
  4253. struct drm_device *dev = crtc->dev;
  4254. struct drm_i915_private *dev_priv = dev->dev_private;
  4255. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4256. int pipe = intel_crtc->pipe;
  4257. int plane = intel_crtc->plane;
  4258. int refclk, num_connectors = 0;
  4259. intel_clock_t clock, reduced_clock;
  4260. u32 dspcntr;
  4261. bool ok, has_reduced_clock = false;
  4262. bool is_lvds = false, is_dsi = false;
  4263. struct intel_encoder *encoder;
  4264. const intel_limit_t *limit;
  4265. int ret;
  4266. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4267. switch (encoder->type) {
  4268. case INTEL_OUTPUT_LVDS:
  4269. is_lvds = true;
  4270. break;
  4271. case INTEL_OUTPUT_DSI:
  4272. is_dsi = true;
  4273. break;
  4274. }
  4275. num_connectors++;
  4276. }
  4277. if (is_dsi)
  4278. goto skip_dpll;
  4279. if (!intel_crtc->config.clock_set) {
  4280. refclk = i9xx_get_refclk(crtc, num_connectors);
  4281. /*
  4282. * Returns a set of divisors for the desired target clock with
  4283. * the given refclk, or FALSE. The returned values represent
  4284. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  4285. * 2) / p1 / p2.
  4286. */
  4287. limit = intel_limit(crtc, refclk);
  4288. ok = dev_priv->display.find_dpll(limit, crtc,
  4289. intel_crtc->config.port_clock,
  4290. refclk, NULL, &clock);
  4291. if (!ok) {
  4292. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  4293. return -EINVAL;
  4294. }
  4295. if (is_lvds && dev_priv->lvds_downclock_avail) {
  4296. /*
  4297. * Ensure we match the reduced clock's P to the target
  4298. * clock. If the clocks don't match, we can't switch
  4299. * the display clock by using the FP0/FP1. In such case
  4300. * we will disable the LVDS downclock feature.
  4301. */
  4302. has_reduced_clock =
  4303. dev_priv->display.find_dpll(limit, crtc,
  4304. dev_priv->lvds_downclock,
  4305. refclk, &clock,
  4306. &reduced_clock);
  4307. }
  4308. /* Compat-code for transition, will disappear. */
  4309. intel_crtc->config.dpll.n = clock.n;
  4310. intel_crtc->config.dpll.m1 = clock.m1;
  4311. intel_crtc->config.dpll.m2 = clock.m2;
  4312. intel_crtc->config.dpll.p1 = clock.p1;
  4313. intel_crtc->config.dpll.p2 = clock.p2;
  4314. }
  4315. if (IS_GEN2(dev)) {
  4316. i8xx_update_pll(intel_crtc,
  4317. has_reduced_clock ? &reduced_clock : NULL,
  4318. num_connectors);
  4319. } else if (IS_VALLEYVIEW(dev)) {
  4320. vlv_update_pll(intel_crtc);
  4321. } else {
  4322. i9xx_update_pll(intel_crtc,
  4323. has_reduced_clock ? &reduced_clock : NULL,
  4324. num_connectors);
  4325. }
  4326. skip_dpll:
  4327. /* Set up the display plane register */
  4328. dspcntr = DISPPLANE_GAMMA_ENABLE;
  4329. if (!IS_VALLEYVIEW(dev)) {
  4330. if (pipe == 0)
  4331. dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
  4332. else
  4333. dspcntr |= DISPPLANE_SEL_PIPE_B;
  4334. }
  4335. intel_set_pipe_timings(intel_crtc);
  4336. /* pipesrc and dspsize control the size that is scaled from,
  4337. * which should always be the user's requested size.
  4338. */
  4339. I915_WRITE(DSPSIZE(plane),
  4340. ((intel_crtc->config.pipe_src_h - 1) << 16) |
  4341. (intel_crtc->config.pipe_src_w - 1));
  4342. I915_WRITE(DSPPOS(plane), 0);
  4343. i9xx_set_pipeconf(intel_crtc);
  4344. I915_WRITE(DSPCNTR(plane), dspcntr);
  4345. POSTING_READ(DSPCNTR(plane));
  4346. ret = intel_pipe_set_base(crtc, x, y, fb);
  4347. return ret;
  4348. }
  4349. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  4350. struct intel_crtc_config *pipe_config)
  4351. {
  4352. struct drm_device *dev = crtc->base.dev;
  4353. struct drm_i915_private *dev_priv = dev->dev_private;
  4354. uint32_t tmp;
  4355. tmp = I915_READ(PFIT_CONTROL);
  4356. if (!(tmp & PFIT_ENABLE))
  4357. return;
  4358. /* Check whether the pfit is attached to our pipe. */
  4359. if (INTEL_INFO(dev)->gen < 4) {
  4360. if (crtc->pipe != PIPE_B)
  4361. return;
  4362. } else {
  4363. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  4364. return;
  4365. }
  4366. pipe_config->gmch_pfit.control = tmp;
  4367. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  4368. if (INTEL_INFO(dev)->gen < 5)
  4369. pipe_config->gmch_pfit.lvds_border_bits =
  4370. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  4371. }
  4372. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  4373. struct intel_crtc_config *pipe_config)
  4374. {
  4375. struct drm_device *dev = crtc->base.dev;
  4376. struct drm_i915_private *dev_priv = dev->dev_private;
  4377. int pipe = pipe_config->cpu_transcoder;
  4378. intel_clock_t clock;
  4379. u32 mdiv;
  4380. int refclk = 100000;
  4381. mutex_lock(&dev_priv->dpio_lock);
  4382. mdiv = vlv_dpio_read(dev_priv, pipe, DPIO_DIV(pipe));
  4383. mutex_unlock(&dev_priv->dpio_lock);
  4384. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  4385. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  4386. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  4387. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  4388. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  4389. clock.vco = refclk * clock.m1 * clock.m2 / clock.n;
  4390. clock.dot = 2 * clock.vco / (clock.p1 * clock.p2);
  4391. pipe_config->port_clock = clock.dot / 10;
  4392. }
  4393. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  4394. struct intel_crtc_config *pipe_config)
  4395. {
  4396. struct drm_device *dev = crtc->base.dev;
  4397. struct drm_i915_private *dev_priv = dev->dev_private;
  4398. uint32_t tmp;
  4399. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  4400. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  4401. tmp = I915_READ(PIPECONF(crtc->pipe));
  4402. if (!(tmp & PIPECONF_ENABLE))
  4403. return false;
  4404. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  4405. switch (tmp & PIPECONF_BPC_MASK) {
  4406. case PIPECONF_6BPC:
  4407. pipe_config->pipe_bpp = 18;
  4408. break;
  4409. case PIPECONF_8BPC:
  4410. pipe_config->pipe_bpp = 24;
  4411. break;
  4412. case PIPECONF_10BPC:
  4413. pipe_config->pipe_bpp = 30;
  4414. break;
  4415. default:
  4416. break;
  4417. }
  4418. }
  4419. if (INTEL_INFO(dev)->gen < 4)
  4420. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  4421. intel_get_pipe_timings(crtc, pipe_config);
  4422. i9xx_get_pfit_config(crtc, pipe_config);
  4423. if (INTEL_INFO(dev)->gen >= 4) {
  4424. tmp = I915_READ(DPLL_MD(crtc->pipe));
  4425. pipe_config->pixel_multiplier =
  4426. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  4427. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  4428. pipe_config->dpll_hw_state.dpll_md = tmp;
  4429. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  4430. tmp = I915_READ(DPLL(crtc->pipe));
  4431. pipe_config->pixel_multiplier =
  4432. ((tmp & SDVO_MULTIPLIER_MASK)
  4433. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  4434. } else {
  4435. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  4436. * port and will be fixed up in the encoder->get_config
  4437. * function. */
  4438. pipe_config->pixel_multiplier = 1;
  4439. }
  4440. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  4441. if (!IS_VALLEYVIEW(dev)) {
  4442. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  4443. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  4444. } else {
  4445. /* Mask out read-only status bits. */
  4446. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  4447. DPLL_PORTC_READY_MASK |
  4448. DPLL_PORTB_READY_MASK);
  4449. }
  4450. if (IS_VALLEYVIEW(dev))
  4451. vlv_crtc_clock_get(crtc, pipe_config);
  4452. else
  4453. i9xx_crtc_clock_get(crtc, pipe_config);
  4454. return true;
  4455. }
  4456. static void ironlake_init_pch_refclk(struct drm_device *dev)
  4457. {
  4458. struct drm_i915_private *dev_priv = dev->dev_private;
  4459. struct drm_mode_config *mode_config = &dev->mode_config;
  4460. struct intel_encoder *encoder;
  4461. u32 val, final;
  4462. bool has_lvds = false;
  4463. bool has_cpu_edp = false;
  4464. bool has_panel = false;
  4465. bool has_ck505 = false;
  4466. bool can_ssc = false;
  4467. /* We need to take the global config into account */
  4468. list_for_each_entry(encoder, &mode_config->encoder_list,
  4469. base.head) {
  4470. switch (encoder->type) {
  4471. case INTEL_OUTPUT_LVDS:
  4472. has_panel = true;
  4473. has_lvds = true;
  4474. break;
  4475. case INTEL_OUTPUT_EDP:
  4476. has_panel = true;
  4477. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  4478. has_cpu_edp = true;
  4479. break;
  4480. }
  4481. }
  4482. if (HAS_PCH_IBX(dev)) {
  4483. has_ck505 = dev_priv->vbt.display_clock_mode;
  4484. can_ssc = has_ck505;
  4485. } else {
  4486. has_ck505 = false;
  4487. can_ssc = true;
  4488. }
  4489. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  4490. has_panel, has_lvds, has_ck505);
  4491. /* Ironlake: try to setup display ref clock before DPLL
  4492. * enabling. This is only under driver's control after
  4493. * PCH B stepping, previous chipset stepping should be
  4494. * ignoring this setting.
  4495. */
  4496. val = I915_READ(PCH_DREF_CONTROL);
  4497. /* As we must carefully and slowly disable/enable each source in turn,
  4498. * compute the final state we want first and check if we need to
  4499. * make any changes at all.
  4500. */
  4501. final = val;
  4502. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  4503. if (has_ck505)
  4504. final |= DREF_NONSPREAD_CK505_ENABLE;
  4505. else
  4506. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  4507. final &= ~DREF_SSC_SOURCE_MASK;
  4508. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  4509. final &= ~DREF_SSC1_ENABLE;
  4510. if (has_panel) {
  4511. final |= DREF_SSC_SOURCE_ENABLE;
  4512. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  4513. final |= DREF_SSC1_ENABLE;
  4514. if (has_cpu_edp) {
  4515. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  4516. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  4517. else
  4518. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  4519. } else
  4520. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  4521. } else {
  4522. final |= DREF_SSC_SOURCE_DISABLE;
  4523. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  4524. }
  4525. if (final == val)
  4526. return;
  4527. /* Always enable nonspread source */
  4528. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  4529. if (has_ck505)
  4530. val |= DREF_NONSPREAD_CK505_ENABLE;
  4531. else
  4532. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  4533. if (has_panel) {
  4534. val &= ~DREF_SSC_SOURCE_MASK;
  4535. val |= DREF_SSC_SOURCE_ENABLE;
  4536. /* SSC must be turned on before enabling the CPU output */
  4537. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  4538. DRM_DEBUG_KMS("Using SSC on panel\n");
  4539. val |= DREF_SSC1_ENABLE;
  4540. } else
  4541. val &= ~DREF_SSC1_ENABLE;
  4542. /* Get SSC going before enabling the outputs */
  4543. I915_WRITE(PCH_DREF_CONTROL, val);
  4544. POSTING_READ(PCH_DREF_CONTROL);
  4545. udelay(200);
  4546. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  4547. /* Enable CPU source on CPU attached eDP */
  4548. if (has_cpu_edp) {
  4549. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  4550. DRM_DEBUG_KMS("Using SSC on eDP\n");
  4551. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  4552. }
  4553. else
  4554. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  4555. } else
  4556. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  4557. I915_WRITE(PCH_DREF_CONTROL, val);
  4558. POSTING_READ(PCH_DREF_CONTROL);
  4559. udelay(200);
  4560. } else {
  4561. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  4562. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  4563. /* Turn off CPU output */
  4564. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  4565. I915_WRITE(PCH_DREF_CONTROL, val);
  4566. POSTING_READ(PCH_DREF_CONTROL);
  4567. udelay(200);
  4568. /* Turn off the SSC source */
  4569. val &= ~DREF_SSC_SOURCE_MASK;
  4570. val |= DREF_SSC_SOURCE_DISABLE;
  4571. /* Turn off SSC1 */
  4572. val &= ~DREF_SSC1_ENABLE;
  4573. I915_WRITE(PCH_DREF_CONTROL, val);
  4574. POSTING_READ(PCH_DREF_CONTROL);
  4575. udelay(200);
  4576. }
  4577. BUG_ON(val != final);
  4578. }
  4579. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  4580. {
  4581. uint32_t tmp;
  4582. tmp = I915_READ(SOUTH_CHICKEN2);
  4583. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  4584. I915_WRITE(SOUTH_CHICKEN2, tmp);
  4585. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  4586. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  4587. DRM_ERROR("FDI mPHY reset assert timeout\n");
  4588. tmp = I915_READ(SOUTH_CHICKEN2);
  4589. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  4590. I915_WRITE(SOUTH_CHICKEN2, tmp);
  4591. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  4592. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  4593. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  4594. }
  4595. /* WaMPhyProgramming:hsw */
  4596. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  4597. {
  4598. uint32_t tmp;
  4599. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  4600. tmp &= ~(0xFF << 24);
  4601. tmp |= (0x12 << 24);
  4602. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  4603. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  4604. tmp |= (1 << 11);
  4605. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  4606. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  4607. tmp |= (1 << 11);
  4608. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  4609. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  4610. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  4611. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  4612. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  4613. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  4614. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  4615. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  4616. tmp &= ~(7 << 13);
  4617. tmp |= (5 << 13);
  4618. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  4619. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  4620. tmp &= ~(7 << 13);
  4621. tmp |= (5 << 13);
  4622. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  4623. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  4624. tmp &= ~0xFF;
  4625. tmp |= 0x1C;
  4626. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  4627. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  4628. tmp &= ~0xFF;
  4629. tmp |= 0x1C;
  4630. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  4631. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  4632. tmp &= ~(0xFF << 16);
  4633. tmp |= (0x1C << 16);
  4634. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  4635. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  4636. tmp &= ~(0xFF << 16);
  4637. tmp |= (0x1C << 16);
  4638. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  4639. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  4640. tmp |= (1 << 27);
  4641. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  4642. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  4643. tmp |= (1 << 27);
  4644. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  4645. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  4646. tmp &= ~(0xF << 28);
  4647. tmp |= (4 << 28);
  4648. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  4649. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  4650. tmp &= ~(0xF << 28);
  4651. tmp |= (4 << 28);
  4652. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  4653. }
  4654. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  4655. * Programming" based on the parameters passed:
  4656. * - Sequence to enable CLKOUT_DP
  4657. * - Sequence to enable CLKOUT_DP without spread
  4658. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  4659. */
  4660. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  4661. bool with_fdi)
  4662. {
  4663. struct drm_i915_private *dev_priv = dev->dev_private;
  4664. uint32_t reg, tmp;
  4665. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  4666. with_spread = true;
  4667. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  4668. with_fdi, "LP PCH doesn't have FDI\n"))
  4669. with_fdi = false;
  4670. mutex_lock(&dev_priv->dpio_lock);
  4671. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  4672. tmp &= ~SBI_SSCCTL_DISABLE;
  4673. tmp |= SBI_SSCCTL_PATHALT;
  4674. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  4675. udelay(24);
  4676. if (with_spread) {
  4677. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  4678. tmp &= ~SBI_SSCCTL_PATHALT;
  4679. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  4680. if (with_fdi) {
  4681. lpt_reset_fdi_mphy(dev_priv);
  4682. lpt_program_fdi_mphy(dev_priv);
  4683. }
  4684. }
  4685. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  4686. SBI_GEN0 : SBI_DBUFF0;
  4687. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  4688. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  4689. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  4690. mutex_unlock(&dev_priv->dpio_lock);
  4691. }
  4692. /* Sequence to disable CLKOUT_DP */
  4693. static void lpt_disable_clkout_dp(struct drm_device *dev)
  4694. {
  4695. struct drm_i915_private *dev_priv = dev->dev_private;
  4696. uint32_t reg, tmp;
  4697. mutex_lock(&dev_priv->dpio_lock);
  4698. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  4699. SBI_GEN0 : SBI_DBUFF0;
  4700. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  4701. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  4702. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  4703. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  4704. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  4705. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  4706. tmp |= SBI_SSCCTL_PATHALT;
  4707. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  4708. udelay(32);
  4709. }
  4710. tmp |= SBI_SSCCTL_DISABLE;
  4711. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  4712. }
  4713. mutex_unlock(&dev_priv->dpio_lock);
  4714. }
  4715. static void lpt_init_pch_refclk(struct drm_device *dev)
  4716. {
  4717. struct drm_mode_config *mode_config = &dev->mode_config;
  4718. struct intel_encoder *encoder;
  4719. bool has_vga = false;
  4720. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  4721. switch (encoder->type) {
  4722. case INTEL_OUTPUT_ANALOG:
  4723. has_vga = true;
  4724. break;
  4725. }
  4726. }
  4727. if (has_vga)
  4728. lpt_enable_clkout_dp(dev, true, true);
  4729. else
  4730. lpt_disable_clkout_dp(dev);
  4731. }
  4732. /*
  4733. * Initialize reference clocks when the driver loads
  4734. */
  4735. void intel_init_pch_refclk(struct drm_device *dev)
  4736. {
  4737. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  4738. ironlake_init_pch_refclk(dev);
  4739. else if (HAS_PCH_LPT(dev))
  4740. lpt_init_pch_refclk(dev);
  4741. }
  4742. static int ironlake_get_refclk(struct drm_crtc *crtc)
  4743. {
  4744. struct drm_device *dev = crtc->dev;
  4745. struct drm_i915_private *dev_priv = dev->dev_private;
  4746. struct intel_encoder *encoder;
  4747. int num_connectors = 0;
  4748. bool is_lvds = false;
  4749. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4750. switch (encoder->type) {
  4751. case INTEL_OUTPUT_LVDS:
  4752. is_lvds = true;
  4753. break;
  4754. }
  4755. num_connectors++;
  4756. }
  4757. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4758. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  4759. dev_priv->vbt.lvds_ssc_freq);
  4760. return dev_priv->vbt.lvds_ssc_freq * 1000;
  4761. }
  4762. return 120000;
  4763. }
  4764. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  4765. {
  4766. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  4767. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4768. int pipe = intel_crtc->pipe;
  4769. uint32_t val;
  4770. val = 0;
  4771. switch (intel_crtc->config.pipe_bpp) {
  4772. case 18:
  4773. val |= PIPECONF_6BPC;
  4774. break;
  4775. case 24:
  4776. val |= PIPECONF_8BPC;
  4777. break;
  4778. case 30:
  4779. val |= PIPECONF_10BPC;
  4780. break;
  4781. case 36:
  4782. val |= PIPECONF_12BPC;
  4783. break;
  4784. default:
  4785. /* Case prevented by intel_choose_pipe_bpp_dither. */
  4786. BUG();
  4787. }
  4788. if (intel_crtc->config.dither)
  4789. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  4790. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  4791. val |= PIPECONF_INTERLACED_ILK;
  4792. else
  4793. val |= PIPECONF_PROGRESSIVE;
  4794. if (intel_crtc->config.limited_color_range)
  4795. val |= PIPECONF_COLOR_RANGE_SELECT;
  4796. I915_WRITE(PIPECONF(pipe), val);
  4797. POSTING_READ(PIPECONF(pipe));
  4798. }
  4799. /*
  4800. * Set up the pipe CSC unit.
  4801. *
  4802. * Currently only full range RGB to limited range RGB conversion
  4803. * is supported, but eventually this should handle various
  4804. * RGB<->YCbCr scenarios as well.
  4805. */
  4806. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  4807. {
  4808. struct drm_device *dev = crtc->dev;
  4809. struct drm_i915_private *dev_priv = dev->dev_private;
  4810. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4811. int pipe = intel_crtc->pipe;
  4812. uint16_t coeff = 0x7800; /* 1.0 */
  4813. /*
  4814. * TODO: Check what kind of values actually come out of the pipe
  4815. * with these coeff/postoff values and adjust to get the best
  4816. * accuracy. Perhaps we even need to take the bpc value into
  4817. * consideration.
  4818. */
  4819. if (intel_crtc->config.limited_color_range)
  4820. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  4821. /*
  4822. * GY/GU and RY/RU should be the other way around according
  4823. * to BSpec, but reality doesn't agree. Just set them up in
  4824. * a way that results in the correct picture.
  4825. */
  4826. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  4827. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  4828. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  4829. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  4830. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  4831. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  4832. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  4833. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  4834. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  4835. if (INTEL_INFO(dev)->gen > 6) {
  4836. uint16_t postoff = 0;
  4837. if (intel_crtc->config.limited_color_range)
  4838. postoff = (16 * (1 << 13) / 255) & 0x1fff;
  4839. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  4840. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  4841. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  4842. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  4843. } else {
  4844. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  4845. if (intel_crtc->config.limited_color_range)
  4846. mode |= CSC_BLACK_SCREEN_OFFSET;
  4847. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  4848. }
  4849. }
  4850. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  4851. {
  4852. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  4853. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4854. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  4855. uint32_t val;
  4856. val = 0;
  4857. if (intel_crtc->config.dither)
  4858. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  4859. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  4860. val |= PIPECONF_INTERLACED_ILK;
  4861. else
  4862. val |= PIPECONF_PROGRESSIVE;
  4863. I915_WRITE(PIPECONF(cpu_transcoder), val);
  4864. POSTING_READ(PIPECONF(cpu_transcoder));
  4865. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  4866. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  4867. }
  4868. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  4869. intel_clock_t *clock,
  4870. bool *has_reduced_clock,
  4871. intel_clock_t *reduced_clock)
  4872. {
  4873. struct drm_device *dev = crtc->dev;
  4874. struct drm_i915_private *dev_priv = dev->dev_private;
  4875. struct intel_encoder *intel_encoder;
  4876. int refclk;
  4877. const intel_limit_t *limit;
  4878. bool ret, is_lvds = false;
  4879. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  4880. switch (intel_encoder->type) {
  4881. case INTEL_OUTPUT_LVDS:
  4882. is_lvds = true;
  4883. break;
  4884. }
  4885. }
  4886. refclk = ironlake_get_refclk(crtc);
  4887. /*
  4888. * Returns a set of divisors for the desired target clock with the given
  4889. * refclk, or FALSE. The returned values represent the clock equation:
  4890. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  4891. */
  4892. limit = intel_limit(crtc, refclk);
  4893. ret = dev_priv->display.find_dpll(limit, crtc,
  4894. to_intel_crtc(crtc)->config.port_clock,
  4895. refclk, NULL, clock);
  4896. if (!ret)
  4897. return false;
  4898. if (is_lvds && dev_priv->lvds_downclock_avail) {
  4899. /*
  4900. * Ensure we match the reduced clock's P to the target clock.
  4901. * If the clocks don't match, we can't switch the display clock
  4902. * by using the FP0/FP1. In such case we will disable the LVDS
  4903. * downclock feature.
  4904. */
  4905. *has_reduced_clock =
  4906. dev_priv->display.find_dpll(limit, crtc,
  4907. dev_priv->lvds_downclock,
  4908. refclk, clock,
  4909. reduced_clock);
  4910. }
  4911. return true;
  4912. }
  4913. static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
  4914. {
  4915. struct drm_i915_private *dev_priv = dev->dev_private;
  4916. uint32_t temp;
  4917. temp = I915_READ(SOUTH_CHICKEN1);
  4918. if (temp & FDI_BC_BIFURCATION_SELECT)
  4919. return;
  4920. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  4921. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  4922. temp |= FDI_BC_BIFURCATION_SELECT;
  4923. DRM_DEBUG_KMS("enabling fdi C rx\n");
  4924. I915_WRITE(SOUTH_CHICKEN1, temp);
  4925. POSTING_READ(SOUTH_CHICKEN1);
  4926. }
  4927. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  4928. {
  4929. struct drm_device *dev = intel_crtc->base.dev;
  4930. struct drm_i915_private *dev_priv = dev->dev_private;
  4931. switch (intel_crtc->pipe) {
  4932. case PIPE_A:
  4933. break;
  4934. case PIPE_B:
  4935. if (intel_crtc->config.fdi_lanes > 2)
  4936. WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
  4937. else
  4938. cpt_enable_fdi_bc_bifurcation(dev);
  4939. break;
  4940. case PIPE_C:
  4941. cpt_enable_fdi_bc_bifurcation(dev);
  4942. break;
  4943. default:
  4944. BUG();
  4945. }
  4946. }
  4947. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  4948. {
  4949. /*
  4950. * Account for spread spectrum to avoid
  4951. * oversubscribing the link. Max center spread
  4952. * is 2.5%; use 5% for safety's sake.
  4953. */
  4954. u32 bps = target_clock * bpp * 21 / 20;
  4955. return bps / (link_bw * 8) + 1;
  4956. }
  4957. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  4958. {
  4959. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  4960. }
  4961. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  4962. u32 *fp,
  4963. intel_clock_t *reduced_clock, u32 *fp2)
  4964. {
  4965. struct drm_crtc *crtc = &intel_crtc->base;
  4966. struct drm_device *dev = crtc->dev;
  4967. struct drm_i915_private *dev_priv = dev->dev_private;
  4968. struct intel_encoder *intel_encoder;
  4969. uint32_t dpll;
  4970. int factor, num_connectors = 0;
  4971. bool is_lvds = false, is_sdvo = false;
  4972. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  4973. switch (intel_encoder->type) {
  4974. case INTEL_OUTPUT_LVDS:
  4975. is_lvds = true;
  4976. break;
  4977. case INTEL_OUTPUT_SDVO:
  4978. case INTEL_OUTPUT_HDMI:
  4979. is_sdvo = true;
  4980. break;
  4981. }
  4982. num_connectors++;
  4983. }
  4984. /* Enable autotuning of the PLL clock (if permissible) */
  4985. factor = 21;
  4986. if (is_lvds) {
  4987. if ((intel_panel_use_ssc(dev_priv) &&
  4988. dev_priv->vbt.lvds_ssc_freq == 100) ||
  4989. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  4990. factor = 25;
  4991. } else if (intel_crtc->config.sdvo_tv_clock)
  4992. factor = 20;
  4993. if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
  4994. *fp |= FP_CB_TUNE;
  4995. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  4996. *fp2 |= FP_CB_TUNE;
  4997. dpll = 0;
  4998. if (is_lvds)
  4999. dpll |= DPLLB_MODE_LVDS;
  5000. else
  5001. dpll |= DPLLB_MODE_DAC_SERIAL;
  5002. dpll |= (intel_crtc->config.pixel_multiplier - 1)
  5003. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  5004. if (is_sdvo)
  5005. dpll |= DPLL_SDVO_HIGH_SPEED;
  5006. if (intel_crtc->config.has_dp_encoder)
  5007. dpll |= DPLL_SDVO_HIGH_SPEED;
  5008. /* compute bitmask from p1 value */
  5009. dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5010. /* also FPA1 */
  5011. dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5012. switch (intel_crtc->config.dpll.p2) {
  5013. case 5:
  5014. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5015. break;
  5016. case 7:
  5017. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5018. break;
  5019. case 10:
  5020. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5021. break;
  5022. case 14:
  5023. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5024. break;
  5025. }
  5026. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5027. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5028. else
  5029. dpll |= PLL_REF_INPUT_DREFCLK;
  5030. return dpll | DPLL_VCO_ENABLE;
  5031. }
  5032. static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
  5033. int x, int y,
  5034. struct drm_framebuffer *fb)
  5035. {
  5036. struct drm_device *dev = crtc->dev;
  5037. struct drm_i915_private *dev_priv = dev->dev_private;
  5038. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5039. int pipe = intel_crtc->pipe;
  5040. int plane = intel_crtc->plane;
  5041. int num_connectors = 0;
  5042. intel_clock_t clock, reduced_clock;
  5043. u32 dpll = 0, fp = 0, fp2 = 0;
  5044. bool ok, has_reduced_clock = false;
  5045. bool is_lvds = false;
  5046. struct intel_encoder *encoder;
  5047. struct intel_shared_dpll *pll;
  5048. int ret;
  5049. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5050. switch (encoder->type) {
  5051. case INTEL_OUTPUT_LVDS:
  5052. is_lvds = true;
  5053. break;
  5054. }
  5055. num_connectors++;
  5056. }
  5057. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  5058. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  5059. ok = ironlake_compute_clocks(crtc, &clock,
  5060. &has_reduced_clock, &reduced_clock);
  5061. if (!ok && !intel_crtc->config.clock_set) {
  5062. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5063. return -EINVAL;
  5064. }
  5065. /* Compat-code for transition, will disappear. */
  5066. if (!intel_crtc->config.clock_set) {
  5067. intel_crtc->config.dpll.n = clock.n;
  5068. intel_crtc->config.dpll.m1 = clock.m1;
  5069. intel_crtc->config.dpll.m2 = clock.m2;
  5070. intel_crtc->config.dpll.p1 = clock.p1;
  5071. intel_crtc->config.dpll.p2 = clock.p2;
  5072. }
  5073. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  5074. if (intel_crtc->config.has_pch_encoder) {
  5075. fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
  5076. if (has_reduced_clock)
  5077. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  5078. dpll = ironlake_compute_dpll(intel_crtc,
  5079. &fp, &reduced_clock,
  5080. has_reduced_clock ? &fp2 : NULL);
  5081. intel_crtc->config.dpll_hw_state.dpll = dpll;
  5082. intel_crtc->config.dpll_hw_state.fp0 = fp;
  5083. if (has_reduced_clock)
  5084. intel_crtc->config.dpll_hw_state.fp1 = fp2;
  5085. else
  5086. intel_crtc->config.dpll_hw_state.fp1 = fp;
  5087. pll = intel_get_shared_dpll(intel_crtc);
  5088. if (pll == NULL) {
  5089. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  5090. pipe_name(pipe));
  5091. return -EINVAL;
  5092. }
  5093. } else
  5094. intel_put_shared_dpll(intel_crtc);
  5095. if (intel_crtc->config.has_dp_encoder)
  5096. intel_dp_set_m_n(intel_crtc);
  5097. if (is_lvds && has_reduced_clock && i915_powersave)
  5098. intel_crtc->lowfreq_avail = true;
  5099. else
  5100. intel_crtc->lowfreq_avail = false;
  5101. if (intel_crtc->config.has_pch_encoder) {
  5102. pll = intel_crtc_to_shared_dpll(intel_crtc);
  5103. }
  5104. intel_set_pipe_timings(intel_crtc);
  5105. if (intel_crtc->config.has_pch_encoder) {
  5106. intel_cpu_transcoder_set_m_n(intel_crtc,
  5107. &intel_crtc->config.fdi_m_n);
  5108. }
  5109. if (IS_IVYBRIDGE(dev))
  5110. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  5111. ironlake_set_pipeconf(crtc);
  5112. /* Set up the display plane register */
  5113. I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
  5114. POSTING_READ(DSPCNTR(plane));
  5115. ret = intel_pipe_set_base(crtc, x, y, fb);
  5116. return ret;
  5117. }
  5118. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  5119. struct intel_link_m_n *m_n)
  5120. {
  5121. struct drm_device *dev = crtc->base.dev;
  5122. struct drm_i915_private *dev_priv = dev->dev_private;
  5123. enum pipe pipe = crtc->pipe;
  5124. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  5125. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  5126. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  5127. & ~TU_SIZE_MASK;
  5128. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  5129. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  5130. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  5131. }
  5132. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  5133. enum transcoder transcoder,
  5134. struct intel_link_m_n *m_n)
  5135. {
  5136. struct drm_device *dev = crtc->base.dev;
  5137. struct drm_i915_private *dev_priv = dev->dev_private;
  5138. enum pipe pipe = crtc->pipe;
  5139. if (INTEL_INFO(dev)->gen >= 5) {
  5140. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  5141. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  5142. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  5143. & ~TU_SIZE_MASK;
  5144. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  5145. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  5146. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  5147. } else {
  5148. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  5149. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  5150. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  5151. & ~TU_SIZE_MASK;
  5152. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  5153. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  5154. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  5155. }
  5156. }
  5157. void intel_dp_get_m_n(struct intel_crtc *crtc,
  5158. struct intel_crtc_config *pipe_config)
  5159. {
  5160. if (crtc->config.has_pch_encoder)
  5161. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  5162. else
  5163. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  5164. &pipe_config->dp_m_n);
  5165. }
  5166. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  5167. struct intel_crtc_config *pipe_config)
  5168. {
  5169. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  5170. &pipe_config->fdi_m_n);
  5171. }
  5172. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  5173. struct intel_crtc_config *pipe_config)
  5174. {
  5175. struct drm_device *dev = crtc->base.dev;
  5176. struct drm_i915_private *dev_priv = dev->dev_private;
  5177. uint32_t tmp;
  5178. tmp = I915_READ(PF_CTL(crtc->pipe));
  5179. if (tmp & PF_ENABLE) {
  5180. pipe_config->pch_pfit.enabled = true;
  5181. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  5182. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  5183. /* We currently do not free assignements of panel fitters on
  5184. * ivb/hsw (since we don't use the higher upscaling modes which
  5185. * differentiates them) so just WARN about this case for now. */
  5186. if (IS_GEN7(dev)) {
  5187. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  5188. PF_PIPE_SEL_IVB(crtc->pipe));
  5189. }
  5190. }
  5191. }
  5192. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  5193. struct intel_crtc_config *pipe_config)
  5194. {
  5195. struct drm_device *dev = crtc->base.dev;
  5196. struct drm_i915_private *dev_priv = dev->dev_private;
  5197. uint32_t tmp;
  5198. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5199. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5200. tmp = I915_READ(PIPECONF(crtc->pipe));
  5201. if (!(tmp & PIPECONF_ENABLE))
  5202. return false;
  5203. switch (tmp & PIPECONF_BPC_MASK) {
  5204. case PIPECONF_6BPC:
  5205. pipe_config->pipe_bpp = 18;
  5206. break;
  5207. case PIPECONF_8BPC:
  5208. pipe_config->pipe_bpp = 24;
  5209. break;
  5210. case PIPECONF_10BPC:
  5211. pipe_config->pipe_bpp = 30;
  5212. break;
  5213. case PIPECONF_12BPC:
  5214. pipe_config->pipe_bpp = 36;
  5215. break;
  5216. default:
  5217. break;
  5218. }
  5219. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  5220. struct intel_shared_dpll *pll;
  5221. pipe_config->has_pch_encoder = true;
  5222. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  5223. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  5224. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  5225. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  5226. if (HAS_PCH_IBX(dev_priv->dev)) {
  5227. pipe_config->shared_dpll =
  5228. (enum intel_dpll_id) crtc->pipe;
  5229. } else {
  5230. tmp = I915_READ(PCH_DPLL_SEL);
  5231. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  5232. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  5233. else
  5234. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  5235. }
  5236. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  5237. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  5238. &pipe_config->dpll_hw_state));
  5239. tmp = pipe_config->dpll_hw_state.dpll;
  5240. pipe_config->pixel_multiplier =
  5241. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  5242. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  5243. ironlake_pch_clock_get(crtc, pipe_config);
  5244. } else {
  5245. pipe_config->pixel_multiplier = 1;
  5246. }
  5247. intel_get_pipe_timings(crtc, pipe_config);
  5248. ironlake_get_pfit_config(crtc, pipe_config);
  5249. return true;
  5250. }
  5251. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  5252. {
  5253. struct drm_device *dev = dev_priv->dev;
  5254. struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
  5255. struct intel_crtc *crtc;
  5256. unsigned long irqflags;
  5257. uint32_t val;
  5258. list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
  5259. WARN(crtc->base.enabled, "CRTC for pipe %c enabled\n",
  5260. pipe_name(crtc->pipe));
  5261. WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  5262. WARN(plls->spll_refcount, "SPLL enabled\n");
  5263. WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
  5264. WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
  5265. WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  5266. WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  5267. "CPU PWM1 enabled\n");
  5268. WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  5269. "CPU PWM2 enabled\n");
  5270. WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  5271. "PCH PWM1 enabled\n");
  5272. WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  5273. "Utility pin enabled\n");
  5274. WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  5275. spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
  5276. val = I915_READ(DEIMR);
  5277. WARN((val & ~DE_PCH_EVENT_IVB) != val,
  5278. "Unexpected DEIMR bits enabled: 0x%x\n", val);
  5279. val = I915_READ(SDEIMR);
  5280. WARN((val | SDE_HOTPLUG_MASK_CPT) != 0xffffffff,
  5281. "Unexpected SDEIMR bits enabled: 0x%x\n", val);
  5282. spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
  5283. }
  5284. /*
  5285. * This function implements pieces of two sequences from BSpec:
  5286. * - Sequence for display software to disable LCPLL
  5287. * - Sequence for display software to allow package C8+
  5288. * The steps implemented here are just the steps that actually touch the LCPLL
  5289. * register. Callers should take care of disabling all the display engine
  5290. * functions, doing the mode unset, fixing interrupts, etc.
  5291. */
  5292. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  5293. bool switch_to_fclk, bool allow_power_down)
  5294. {
  5295. uint32_t val;
  5296. assert_can_disable_lcpll(dev_priv);
  5297. val = I915_READ(LCPLL_CTL);
  5298. if (switch_to_fclk) {
  5299. val |= LCPLL_CD_SOURCE_FCLK;
  5300. I915_WRITE(LCPLL_CTL, val);
  5301. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  5302. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  5303. DRM_ERROR("Switching to FCLK failed\n");
  5304. val = I915_READ(LCPLL_CTL);
  5305. }
  5306. val |= LCPLL_PLL_DISABLE;
  5307. I915_WRITE(LCPLL_CTL, val);
  5308. POSTING_READ(LCPLL_CTL);
  5309. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  5310. DRM_ERROR("LCPLL still locked\n");
  5311. val = I915_READ(D_COMP);
  5312. val |= D_COMP_COMP_DISABLE;
  5313. mutex_lock(&dev_priv->rps.hw_lock);
  5314. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP, val))
  5315. DRM_ERROR("Failed to disable D_COMP\n");
  5316. mutex_unlock(&dev_priv->rps.hw_lock);
  5317. POSTING_READ(D_COMP);
  5318. ndelay(100);
  5319. if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
  5320. DRM_ERROR("D_COMP RCOMP still in progress\n");
  5321. if (allow_power_down) {
  5322. val = I915_READ(LCPLL_CTL);
  5323. val |= LCPLL_POWER_DOWN_ALLOW;
  5324. I915_WRITE(LCPLL_CTL, val);
  5325. POSTING_READ(LCPLL_CTL);
  5326. }
  5327. }
  5328. /*
  5329. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  5330. * source.
  5331. */
  5332. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  5333. {
  5334. uint32_t val;
  5335. val = I915_READ(LCPLL_CTL);
  5336. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  5337. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  5338. return;
  5339. /* Make sure we're not on PC8 state before disabling PC8, otherwise
  5340. * we'll hang the machine! */
  5341. dev_priv->uncore.funcs.force_wake_get(dev_priv);
  5342. if (val & LCPLL_POWER_DOWN_ALLOW) {
  5343. val &= ~LCPLL_POWER_DOWN_ALLOW;
  5344. I915_WRITE(LCPLL_CTL, val);
  5345. POSTING_READ(LCPLL_CTL);
  5346. }
  5347. val = I915_READ(D_COMP);
  5348. val |= D_COMP_COMP_FORCE;
  5349. val &= ~D_COMP_COMP_DISABLE;
  5350. mutex_lock(&dev_priv->rps.hw_lock);
  5351. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP, val))
  5352. DRM_ERROR("Failed to enable D_COMP\n");
  5353. mutex_unlock(&dev_priv->rps.hw_lock);
  5354. POSTING_READ(D_COMP);
  5355. val = I915_READ(LCPLL_CTL);
  5356. val &= ~LCPLL_PLL_DISABLE;
  5357. I915_WRITE(LCPLL_CTL, val);
  5358. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  5359. DRM_ERROR("LCPLL not locked yet\n");
  5360. if (val & LCPLL_CD_SOURCE_FCLK) {
  5361. val = I915_READ(LCPLL_CTL);
  5362. val &= ~LCPLL_CD_SOURCE_FCLK;
  5363. I915_WRITE(LCPLL_CTL, val);
  5364. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  5365. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  5366. DRM_ERROR("Switching back to LCPLL failed\n");
  5367. }
  5368. dev_priv->uncore.funcs.force_wake_put(dev_priv);
  5369. }
  5370. void hsw_enable_pc8_work(struct work_struct *__work)
  5371. {
  5372. struct drm_i915_private *dev_priv =
  5373. container_of(to_delayed_work(__work), struct drm_i915_private,
  5374. pc8.enable_work);
  5375. struct drm_device *dev = dev_priv->dev;
  5376. uint32_t val;
  5377. if (dev_priv->pc8.enabled)
  5378. return;
  5379. DRM_DEBUG_KMS("Enabling package C8+\n");
  5380. dev_priv->pc8.enabled = true;
  5381. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  5382. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  5383. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  5384. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  5385. }
  5386. lpt_disable_clkout_dp(dev);
  5387. hsw_pc8_disable_interrupts(dev);
  5388. hsw_disable_lcpll(dev_priv, true, true);
  5389. }
  5390. static void __hsw_enable_package_c8(struct drm_i915_private *dev_priv)
  5391. {
  5392. WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
  5393. WARN(dev_priv->pc8.disable_count < 1,
  5394. "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
  5395. dev_priv->pc8.disable_count--;
  5396. if (dev_priv->pc8.disable_count != 0)
  5397. return;
  5398. schedule_delayed_work(&dev_priv->pc8.enable_work,
  5399. msecs_to_jiffies(i915_pc8_timeout));
  5400. }
  5401. static void __hsw_disable_package_c8(struct drm_i915_private *dev_priv)
  5402. {
  5403. struct drm_device *dev = dev_priv->dev;
  5404. uint32_t val;
  5405. WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
  5406. WARN(dev_priv->pc8.disable_count < 0,
  5407. "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
  5408. dev_priv->pc8.disable_count++;
  5409. if (dev_priv->pc8.disable_count != 1)
  5410. return;
  5411. cancel_delayed_work_sync(&dev_priv->pc8.enable_work);
  5412. if (!dev_priv->pc8.enabled)
  5413. return;
  5414. DRM_DEBUG_KMS("Disabling package C8+\n");
  5415. hsw_restore_lcpll(dev_priv);
  5416. hsw_pc8_restore_interrupts(dev);
  5417. lpt_init_pch_refclk(dev);
  5418. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  5419. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  5420. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  5421. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  5422. }
  5423. intel_prepare_ddi(dev);
  5424. i915_gem_init_swizzling(dev);
  5425. mutex_lock(&dev_priv->rps.hw_lock);
  5426. gen6_update_ring_freq(dev);
  5427. mutex_unlock(&dev_priv->rps.hw_lock);
  5428. dev_priv->pc8.enabled = false;
  5429. }
  5430. void hsw_enable_package_c8(struct drm_i915_private *dev_priv)
  5431. {
  5432. mutex_lock(&dev_priv->pc8.lock);
  5433. __hsw_enable_package_c8(dev_priv);
  5434. mutex_unlock(&dev_priv->pc8.lock);
  5435. }
  5436. void hsw_disable_package_c8(struct drm_i915_private *dev_priv)
  5437. {
  5438. mutex_lock(&dev_priv->pc8.lock);
  5439. __hsw_disable_package_c8(dev_priv);
  5440. mutex_unlock(&dev_priv->pc8.lock);
  5441. }
  5442. static bool hsw_can_enable_package_c8(struct drm_i915_private *dev_priv)
  5443. {
  5444. struct drm_device *dev = dev_priv->dev;
  5445. struct intel_crtc *crtc;
  5446. uint32_t val;
  5447. list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
  5448. if (crtc->base.enabled)
  5449. return false;
  5450. /* This case is still possible since we have the i915.disable_power_well
  5451. * parameter and also the KVMr or something else might be requesting the
  5452. * power well. */
  5453. val = I915_READ(HSW_PWR_WELL_DRIVER);
  5454. if (val != 0) {
  5455. DRM_DEBUG_KMS("Not enabling PC8: power well on\n");
  5456. return false;
  5457. }
  5458. return true;
  5459. }
  5460. /* Since we're called from modeset_global_resources there's no way to
  5461. * symmetrically increase and decrease the refcount, so we use
  5462. * dev_priv->pc8.requirements_met to track whether we already have the refcount
  5463. * or not.
  5464. */
  5465. static void hsw_update_package_c8(struct drm_device *dev)
  5466. {
  5467. struct drm_i915_private *dev_priv = dev->dev_private;
  5468. bool allow;
  5469. if (!i915_enable_pc8)
  5470. return;
  5471. mutex_lock(&dev_priv->pc8.lock);
  5472. allow = hsw_can_enable_package_c8(dev_priv);
  5473. if (allow == dev_priv->pc8.requirements_met)
  5474. goto done;
  5475. dev_priv->pc8.requirements_met = allow;
  5476. if (allow)
  5477. __hsw_enable_package_c8(dev_priv);
  5478. else
  5479. __hsw_disable_package_c8(dev_priv);
  5480. done:
  5481. mutex_unlock(&dev_priv->pc8.lock);
  5482. }
  5483. static void hsw_package_c8_gpu_idle(struct drm_i915_private *dev_priv)
  5484. {
  5485. if (!dev_priv->pc8.gpu_idle) {
  5486. dev_priv->pc8.gpu_idle = true;
  5487. hsw_enable_package_c8(dev_priv);
  5488. }
  5489. }
  5490. static void hsw_package_c8_gpu_busy(struct drm_i915_private *dev_priv)
  5491. {
  5492. if (dev_priv->pc8.gpu_idle) {
  5493. dev_priv->pc8.gpu_idle = false;
  5494. hsw_disable_package_c8(dev_priv);
  5495. }
  5496. }
  5497. static void haswell_modeset_global_resources(struct drm_device *dev)
  5498. {
  5499. bool enable = false;
  5500. struct intel_crtc *crtc;
  5501. list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
  5502. if (!crtc->base.enabled)
  5503. continue;
  5504. if (crtc->pipe != PIPE_A || crtc->config.pch_pfit.enabled ||
  5505. crtc->config.cpu_transcoder != TRANSCODER_EDP)
  5506. enable = true;
  5507. }
  5508. intel_set_power_well(dev, enable);
  5509. hsw_update_package_c8(dev);
  5510. }
  5511. static int haswell_crtc_mode_set(struct drm_crtc *crtc,
  5512. int x, int y,
  5513. struct drm_framebuffer *fb)
  5514. {
  5515. struct drm_device *dev = crtc->dev;
  5516. struct drm_i915_private *dev_priv = dev->dev_private;
  5517. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5518. int plane = intel_crtc->plane;
  5519. int ret;
  5520. if (!intel_ddi_pll_mode_set(crtc))
  5521. return -EINVAL;
  5522. if (intel_crtc->config.has_dp_encoder)
  5523. intel_dp_set_m_n(intel_crtc);
  5524. intel_crtc->lowfreq_avail = false;
  5525. intel_set_pipe_timings(intel_crtc);
  5526. if (intel_crtc->config.has_pch_encoder) {
  5527. intel_cpu_transcoder_set_m_n(intel_crtc,
  5528. &intel_crtc->config.fdi_m_n);
  5529. }
  5530. haswell_set_pipeconf(crtc);
  5531. intel_set_pipe_csc(crtc);
  5532. /* Set up the display plane register */
  5533. I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
  5534. POSTING_READ(DSPCNTR(plane));
  5535. ret = intel_pipe_set_base(crtc, x, y, fb);
  5536. return ret;
  5537. }
  5538. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  5539. struct intel_crtc_config *pipe_config)
  5540. {
  5541. struct drm_device *dev = crtc->base.dev;
  5542. struct drm_i915_private *dev_priv = dev->dev_private;
  5543. enum intel_display_power_domain pfit_domain;
  5544. uint32_t tmp;
  5545. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5546. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5547. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  5548. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  5549. enum pipe trans_edp_pipe;
  5550. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  5551. default:
  5552. WARN(1, "unknown pipe linked to edp transcoder\n");
  5553. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  5554. case TRANS_DDI_EDP_INPUT_A_ON:
  5555. trans_edp_pipe = PIPE_A;
  5556. break;
  5557. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  5558. trans_edp_pipe = PIPE_B;
  5559. break;
  5560. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  5561. trans_edp_pipe = PIPE_C;
  5562. break;
  5563. }
  5564. if (trans_edp_pipe == crtc->pipe)
  5565. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  5566. }
  5567. if (!intel_display_power_enabled(dev,
  5568. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  5569. return false;
  5570. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  5571. if (!(tmp & PIPECONF_ENABLE))
  5572. return false;
  5573. /*
  5574. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  5575. * DDI E. So just check whether this pipe is wired to DDI E and whether
  5576. * the PCH transcoder is on.
  5577. */
  5578. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  5579. if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
  5580. I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  5581. pipe_config->has_pch_encoder = true;
  5582. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  5583. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  5584. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  5585. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  5586. }
  5587. intel_get_pipe_timings(crtc, pipe_config);
  5588. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  5589. if (intel_display_power_enabled(dev, pfit_domain))
  5590. ironlake_get_pfit_config(crtc, pipe_config);
  5591. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  5592. (I915_READ(IPS_CTL) & IPS_ENABLE);
  5593. pipe_config->pixel_multiplier = 1;
  5594. return true;
  5595. }
  5596. static int intel_crtc_mode_set(struct drm_crtc *crtc,
  5597. int x, int y,
  5598. struct drm_framebuffer *fb)
  5599. {
  5600. struct drm_device *dev = crtc->dev;
  5601. struct drm_i915_private *dev_priv = dev->dev_private;
  5602. struct intel_encoder *encoder;
  5603. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5604. struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
  5605. int pipe = intel_crtc->pipe;
  5606. int ret;
  5607. drm_vblank_pre_modeset(dev, pipe);
  5608. ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
  5609. drm_vblank_post_modeset(dev, pipe);
  5610. if (ret != 0)
  5611. return ret;
  5612. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5613. DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
  5614. encoder->base.base.id,
  5615. drm_get_encoder_name(&encoder->base),
  5616. mode->base.id, mode->name);
  5617. encoder->mode_set(encoder);
  5618. }
  5619. return 0;
  5620. }
  5621. static bool intel_eld_uptodate(struct drm_connector *connector,
  5622. int reg_eldv, uint32_t bits_eldv,
  5623. int reg_elda, uint32_t bits_elda,
  5624. int reg_edid)
  5625. {
  5626. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  5627. uint8_t *eld = connector->eld;
  5628. uint32_t i;
  5629. i = I915_READ(reg_eldv);
  5630. i &= bits_eldv;
  5631. if (!eld[0])
  5632. return !i;
  5633. if (!i)
  5634. return false;
  5635. i = I915_READ(reg_elda);
  5636. i &= ~bits_elda;
  5637. I915_WRITE(reg_elda, i);
  5638. for (i = 0; i < eld[2]; i++)
  5639. if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
  5640. return false;
  5641. return true;
  5642. }
  5643. static void g4x_write_eld(struct drm_connector *connector,
  5644. struct drm_crtc *crtc)
  5645. {
  5646. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  5647. uint8_t *eld = connector->eld;
  5648. uint32_t eldv;
  5649. uint32_t len;
  5650. uint32_t i;
  5651. i = I915_READ(G4X_AUD_VID_DID);
  5652. if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
  5653. eldv = G4X_ELDV_DEVCL_DEVBLC;
  5654. else
  5655. eldv = G4X_ELDV_DEVCTG;
  5656. if (intel_eld_uptodate(connector,
  5657. G4X_AUD_CNTL_ST, eldv,
  5658. G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
  5659. G4X_HDMIW_HDMIEDID))
  5660. return;
  5661. i = I915_READ(G4X_AUD_CNTL_ST);
  5662. i &= ~(eldv | G4X_ELD_ADDR);
  5663. len = (i >> 9) & 0x1f; /* ELD buffer size */
  5664. I915_WRITE(G4X_AUD_CNTL_ST, i);
  5665. if (!eld[0])
  5666. return;
  5667. len = min_t(uint8_t, eld[2], len);
  5668. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  5669. for (i = 0; i < len; i++)
  5670. I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
  5671. i = I915_READ(G4X_AUD_CNTL_ST);
  5672. i |= eldv;
  5673. I915_WRITE(G4X_AUD_CNTL_ST, i);
  5674. }
  5675. static void haswell_write_eld(struct drm_connector *connector,
  5676. struct drm_crtc *crtc)
  5677. {
  5678. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  5679. uint8_t *eld = connector->eld;
  5680. struct drm_device *dev = crtc->dev;
  5681. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5682. uint32_t eldv;
  5683. uint32_t i;
  5684. int len;
  5685. int pipe = to_intel_crtc(crtc)->pipe;
  5686. int tmp;
  5687. int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
  5688. int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
  5689. int aud_config = HSW_AUD_CFG(pipe);
  5690. int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
  5691. DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
  5692. /* Audio output enable */
  5693. DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
  5694. tmp = I915_READ(aud_cntrl_st2);
  5695. tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
  5696. I915_WRITE(aud_cntrl_st2, tmp);
  5697. /* Wait for 1 vertical blank */
  5698. intel_wait_for_vblank(dev, pipe);
  5699. /* Set ELD valid state */
  5700. tmp = I915_READ(aud_cntrl_st2);
  5701. DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
  5702. tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
  5703. I915_WRITE(aud_cntrl_st2, tmp);
  5704. tmp = I915_READ(aud_cntrl_st2);
  5705. DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
  5706. /* Enable HDMI mode */
  5707. tmp = I915_READ(aud_config);
  5708. DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
  5709. /* clear N_programing_enable and N_value_index */
  5710. tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
  5711. I915_WRITE(aud_config, tmp);
  5712. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  5713. eldv = AUDIO_ELD_VALID_A << (pipe * 4);
  5714. intel_crtc->eld_vld = true;
  5715. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  5716. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  5717. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  5718. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  5719. } else
  5720. I915_WRITE(aud_config, 0);
  5721. if (intel_eld_uptodate(connector,
  5722. aud_cntrl_st2, eldv,
  5723. aud_cntl_st, IBX_ELD_ADDRESS,
  5724. hdmiw_hdmiedid))
  5725. return;
  5726. i = I915_READ(aud_cntrl_st2);
  5727. i &= ~eldv;
  5728. I915_WRITE(aud_cntrl_st2, i);
  5729. if (!eld[0])
  5730. return;
  5731. i = I915_READ(aud_cntl_st);
  5732. i &= ~IBX_ELD_ADDRESS;
  5733. I915_WRITE(aud_cntl_st, i);
  5734. i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
  5735. DRM_DEBUG_DRIVER("port num:%d\n", i);
  5736. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  5737. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  5738. for (i = 0; i < len; i++)
  5739. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  5740. i = I915_READ(aud_cntrl_st2);
  5741. i |= eldv;
  5742. I915_WRITE(aud_cntrl_st2, i);
  5743. }
  5744. static void ironlake_write_eld(struct drm_connector *connector,
  5745. struct drm_crtc *crtc)
  5746. {
  5747. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  5748. uint8_t *eld = connector->eld;
  5749. uint32_t eldv;
  5750. uint32_t i;
  5751. int len;
  5752. int hdmiw_hdmiedid;
  5753. int aud_config;
  5754. int aud_cntl_st;
  5755. int aud_cntrl_st2;
  5756. int pipe = to_intel_crtc(crtc)->pipe;
  5757. if (HAS_PCH_IBX(connector->dev)) {
  5758. hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
  5759. aud_config = IBX_AUD_CFG(pipe);
  5760. aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
  5761. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  5762. } else {
  5763. hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
  5764. aud_config = CPT_AUD_CFG(pipe);
  5765. aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
  5766. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  5767. }
  5768. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  5769. i = I915_READ(aud_cntl_st);
  5770. i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
  5771. if (!i) {
  5772. DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
  5773. /* operate blindly on all ports */
  5774. eldv = IBX_ELD_VALIDB;
  5775. eldv |= IBX_ELD_VALIDB << 4;
  5776. eldv |= IBX_ELD_VALIDB << 8;
  5777. } else {
  5778. DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
  5779. eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
  5780. }
  5781. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  5782. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  5783. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  5784. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  5785. } else
  5786. I915_WRITE(aud_config, 0);
  5787. if (intel_eld_uptodate(connector,
  5788. aud_cntrl_st2, eldv,
  5789. aud_cntl_st, IBX_ELD_ADDRESS,
  5790. hdmiw_hdmiedid))
  5791. return;
  5792. i = I915_READ(aud_cntrl_st2);
  5793. i &= ~eldv;
  5794. I915_WRITE(aud_cntrl_st2, i);
  5795. if (!eld[0])
  5796. return;
  5797. i = I915_READ(aud_cntl_st);
  5798. i &= ~IBX_ELD_ADDRESS;
  5799. I915_WRITE(aud_cntl_st, i);
  5800. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  5801. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  5802. for (i = 0; i < len; i++)
  5803. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  5804. i = I915_READ(aud_cntrl_st2);
  5805. i |= eldv;
  5806. I915_WRITE(aud_cntrl_st2, i);
  5807. }
  5808. void intel_write_eld(struct drm_encoder *encoder,
  5809. struct drm_display_mode *mode)
  5810. {
  5811. struct drm_crtc *crtc = encoder->crtc;
  5812. struct drm_connector *connector;
  5813. struct drm_device *dev = encoder->dev;
  5814. struct drm_i915_private *dev_priv = dev->dev_private;
  5815. connector = drm_select_eld(encoder, mode);
  5816. if (!connector)
  5817. return;
  5818. DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  5819. connector->base.id,
  5820. drm_get_connector_name(connector),
  5821. connector->encoder->base.id,
  5822. drm_get_encoder_name(connector->encoder));
  5823. connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
  5824. if (dev_priv->display.write_eld)
  5825. dev_priv->display.write_eld(connector, crtc);
  5826. }
  5827. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  5828. {
  5829. struct drm_device *dev = crtc->dev;
  5830. struct drm_i915_private *dev_priv = dev->dev_private;
  5831. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5832. bool visible = base != 0;
  5833. u32 cntl;
  5834. if (intel_crtc->cursor_visible == visible)
  5835. return;
  5836. cntl = I915_READ(_CURACNTR);
  5837. if (visible) {
  5838. /* On these chipsets we can only modify the base whilst
  5839. * the cursor is disabled.
  5840. */
  5841. I915_WRITE(_CURABASE, base);
  5842. cntl &= ~(CURSOR_FORMAT_MASK);
  5843. /* XXX width must be 64, stride 256 => 0x00 << 28 */
  5844. cntl |= CURSOR_ENABLE |
  5845. CURSOR_GAMMA_ENABLE |
  5846. CURSOR_FORMAT_ARGB;
  5847. } else
  5848. cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
  5849. I915_WRITE(_CURACNTR, cntl);
  5850. intel_crtc->cursor_visible = visible;
  5851. }
  5852. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  5853. {
  5854. struct drm_device *dev = crtc->dev;
  5855. struct drm_i915_private *dev_priv = dev->dev_private;
  5856. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5857. int pipe = intel_crtc->pipe;
  5858. bool visible = base != 0;
  5859. if (intel_crtc->cursor_visible != visible) {
  5860. uint32_t cntl = I915_READ(CURCNTR(pipe));
  5861. if (base) {
  5862. cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
  5863. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  5864. cntl |= pipe << 28; /* Connect to correct pipe */
  5865. } else {
  5866. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  5867. cntl |= CURSOR_MODE_DISABLE;
  5868. }
  5869. I915_WRITE(CURCNTR(pipe), cntl);
  5870. intel_crtc->cursor_visible = visible;
  5871. }
  5872. /* and commit changes on next vblank */
  5873. I915_WRITE(CURBASE(pipe), base);
  5874. }
  5875. static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
  5876. {
  5877. struct drm_device *dev = crtc->dev;
  5878. struct drm_i915_private *dev_priv = dev->dev_private;
  5879. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5880. int pipe = intel_crtc->pipe;
  5881. bool visible = base != 0;
  5882. if (intel_crtc->cursor_visible != visible) {
  5883. uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
  5884. if (base) {
  5885. cntl &= ~CURSOR_MODE;
  5886. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  5887. } else {
  5888. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  5889. cntl |= CURSOR_MODE_DISABLE;
  5890. }
  5891. if (IS_HASWELL(dev)) {
  5892. cntl |= CURSOR_PIPE_CSC_ENABLE;
  5893. cntl &= ~CURSOR_TRICKLE_FEED_DISABLE;
  5894. }
  5895. I915_WRITE(CURCNTR_IVB(pipe), cntl);
  5896. intel_crtc->cursor_visible = visible;
  5897. }
  5898. /* and commit changes on next vblank */
  5899. I915_WRITE(CURBASE_IVB(pipe), base);
  5900. }
  5901. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  5902. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  5903. bool on)
  5904. {
  5905. struct drm_device *dev = crtc->dev;
  5906. struct drm_i915_private *dev_priv = dev->dev_private;
  5907. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5908. int pipe = intel_crtc->pipe;
  5909. int x = intel_crtc->cursor_x;
  5910. int y = intel_crtc->cursor_y;
  5911. u32 base = 0, pos = 0;
  5912. bool visible;
  5913. if (on)
  5914. base = intel_crtc->cursor_addr;
  5915. if (x >= intel_crtc->config.pipe_src_w)
  5916. base = 0;
  5917. if (y >= intel_crtc->config.pipe_src_h)
  5918. base = 0;
  5919. if (x < 0) {
  5920. if (x + intel_crtc->cursor_width <= 0)
  5921. base = 0;
  5922. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  5923. x = -x;
  5924. }
  5925. pos |= x << CURSOR_X_SHIFT;
  5926. if (y < 0) {
  5927. if (y + intel_crtc->cursor_height <= 0)
  5928. base = 0;
  5929. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  5930. y = -y;
  5931. }
  5932. pos |= y << CURSOR_Y_SHIFT;
  5933. visible = base != 0;
  5934. if (!visible && !intel_crtc->cursor_visible)
  5935. return;
  5936. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  5937. I915_WRITE(CURPOS_IVB(pipe), pos);
  5938. ivb_update_cursor(crtc, base);
  5939. } else {
  5940. I915_WRITE(CURPOS(pipe), pos);
  5941. if (IS_845G(dev) || IS_I865G(dev))
  5942. i845_update_cursor(crtc, base);
  5943. else
  5944. i9xx_update_cursor(crtc, base);
  5945. }
  5946. }
  5947. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  5948. struct drm_file *file,
  5949. uint32_t handle,
  5950. uint32_t width, uint32_t height)
  5951. {
  5952. struct drm_device *dev = crtc->dev;
  5953. struct drm_i915_private *dev_priv = dev->dev_private;
  5954. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5955. struct drm_i915_gem_object *obj;
  5956. uint32_t addr;
  5957. int ret;
  5958. /* if we want to turn off the cursor ignore width and height */
  5959. if (!handle) {
  5960. DRM_DEBUG_KMS("cursor off\n");
  5961. addr = 0;
  5962. obj = NULL;
  5963. mutex_lock(&dev->struct_mutex);
  5964. goto finish;
  5965. }
  5966. /* Currently we only support 64x64 cursors */
  5967. if (width != 64 || height != 64) {
  5968. DRM_ERROR("we currently only support 64x64 cursors\n");
  5969. return -EINVAL;
  5970. }
  5971. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  5972. if (&obj->base == NULL)
  5973. return -ENOENT;
  5974. if (obj->base.size < width * height * 4) {
  5975. DRM_ERROR("buffer is to small\n");
  5976. ret = -ENOMEM;
  5977. goto fail;
  5978. }
  5979. /* we only need to pin inside GTT if cursor is non-phy */
  5980. mutex_lock(&dev->struct_mutex);
  5981. if (!dev_priv->info->cursor_needs_physical) {
  5982. unsigned alignment;
  5983. if (obj->tiling_mode) {
  5984. DRM_ERROR("cursor cannot be tiled\n");
  5985. ret = -EINVAL;
  5986. goto fail_locked;
  5987. }
  5988. /* Note that the w/a also requires 2 PTE of padding following
  5989. * the bo. We currently fill all unused PTE with the shadow
  5990. * page and so we should always have valid PTE following the
  5991. * cursor preventing the VT-d warning.
  5992. */
  5993. alignment = 0;
  5994. if (need_vtd_wa(dev))
  5995. alignment = 64*1024;
  5996. ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
  5997. if (ret) {
  5998. DRM_ERROR("failed to move cursor bo into the GTT\n");
  5999. goto fail_locked;
  6000. }
  6001. ret = i915_gem_object_put_fence(obj);
  6002. if (ret) {
  6003. DRM_ERROR("failed to release fence for cursor");
  6004. goto fail_unpin;
  6005. }
  6006. addr = i915_gem_obj_ggtt_offset(obj);
  6007. } else {
  6008. int align = IS_I830(dev) ? 16 * 1024 : 256;
  6009. ret = i915_gem_attach_phys_object(dev, obj,
  6010. (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
  6011. align);
  6012. if (ret) {
  6013. DRM_ERROR("failed to attach phys object\n");
  6014. goto fail_locked;
  6015. }
  6016. addr = obj->phys_obj->handle->busaddr;
  6017. }
  6018. if (IS_GEN2(dev))
  6019. I915_WRITE(CURSIZE, (height << 12) | width);
  6020. finish:
  6021. if (intel_crtc->cursor_bo) {
  6022. if (dev_priv->info->cursor_needs_physical) {
  6023. if (intel_crtc->cursor_bo != obj)
  6024. i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
  6025. } else
  6026. i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
  6027. drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
  6028. }
  6029. mutex_unlock(&dev->struct_mutex);
  6030. intel_crtc->cursor_addr = addr;
  6031. intel_crtc->cursor_bo = obj;
  6032. intel_crtc->cursor_width = width;
  6033. intel_crtc->cursor_height = height;
  6034. if (intel_crtc->active)
  6035. intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
  6036. return 0;
  6037. fail_unpin:
  6038. i915_gem_object_unpin_from_display_plane(obj);
  6039. fail_locked:
  6040. mutex_unlock(&dev->struct_mutex);
  6041. fail:
  6042. drm_gem_object_unreference_unlocked(&obj->base);
  6043. return ret;
  6044. }
  6045. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  6046. {
  6047. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6048. intel_crtc->cursor_x = x;
  6049. intel_crtc->cursor_y = y;
  6050. if (intel_crtc->active)
  6051. intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
  6052. return 0;
  6053. }
  6054. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  6055. u16 *blue, uint32_t start, uint32_t size)
  6056. {
  6057. int end = (start + size > 256) ? 256 : start + size, i;
  6058. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6059. for (i = start; i < end; i++) {
  6060. intel_crtc->lut_r[i] = red[i] >> 8;
  6061. intel_crtc->lut_g[i] = green[i] >> 8;
  6062. intel_crtc->lut_b[i] = blue[i] >> 8;
  6063. }
  6064. intel_crtc_load_lut(crtc);
  6065. }
  6066. /* VESA 640x480x72Hz mode to set on the pipe */
  6067. static struct drm_display_mode load_detect_mode = {
  6068. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  6069. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  6070. };
  6071. static struct drm_framebuffer *
  6072. intel_framebuffer_create(struct drm_device *dev,
  6073. struct drm_mode_fb_cmd2 *mode_cmd,
  6074. struct drm_i915_gem_object *obj)
  6075. {
  6076. struct intel_framebuffer *intel_fb;
  6077. int ret;
  6078. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6079. if (!intel_fb) {
  6080. drm_gem_object_unreference_unlocked(&obj->base);
  6081. return ERR_PTR(-ENOMEM);
  6082. }
  6083. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  6084. if (ret) {
  6085. drm_gem_object_unreference_unlocked(&obj->base);
  6086. kfree(intel_fb);
  6087. return ERR_PTR(ret);
  6088. }
  6089. return &intel_fb->base;
  6090. }
  6091. static u32
  6092. intel_framebuffer_pitch_for_width(int width, int bpp)
  6093. {
  6094. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  6095. return ALIGN(pitch, 64);
  6096. }
  6097. static u32
  6098. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  6099. {
  6100. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  6101. return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
  6102. }
  6103. static struct drm_framebuffer *
  6104. intel_framebuffer_create_for_mode(struct drm_device *dev,
  6105. struct drm_display_mode *mode,
  6106. int depth, int bpp)
  6107. {
  6108. struct drm_i915_gem_object *obj;
  6109. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  6110. obj = i915_gem_alloc_object(dev,
  6111. intel_framebuffer_size_for_mode(mode, bpp));
  6112. if (obj == NULL)
  6113. return ERR_PTR(-ENOMEM);
  6114. mode_cmd.width = mode->hdisplay;
  6115. mode_cmd.height = mode->vdisplay;
  6116. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  6117. bpp);
  6118. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  6119. return intel_framebuffer_create(dev, &mode_cmd, obj);
  6120. }
  6121. static struct drm_framebuffer *
  6122. mode_fits_in_fbdev(struct drm_device *dev,
  6123. struct drm_display_mode *mode)
  6124. {
  6125. struct drm_i915_private *dev_priv = dev->dev_private;
  6126. struct drm_i915_gem_object *obj;
  6127. struct drm_framebuffer *fb;
  6128. if (dev_priv->fbdev == NULL)
  6129. return NULL;
  6130. obj = dev_priv->fbdev->ifb.obj;
  6131. if (obj == NULL)
  6132. return NULL;
  6133. fb = &dev_priv->fbdev->ifb.base;
  6134. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  6135. fb->bits_per_pixel))
  6136. return NULL;
  6137. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  6138. return NULL;
  6139. return fb;
  6140. }
  6141. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  6142. struct drm_display_mode *mode,
  6143. struct intel_load_detect_pipe *old)
  6144. {
  6145. struct intel_crtc *intel_crtc;
  6146. struct intel_encoder *intel_encoder =
  6147. intel_attached_encoder(connector);
  6148. struct drm_crtc *possible_crtc;
  6149. struct drm_encoder *encoder = &intel_encoder->base;
  6150. struct drm_crtc *crtc = NULL;
  6151. struct drm_device *dev = encoder->dev;
  6152. struct drm_framebuffer *fb;
  6153. int i = -1;
  6154. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  6155. connector->base.id, drm_get_connector_name(connector),
  6156. encoder->base.id, drm_get_encoder_name(encoder));
  6157. /*
  6158. * Algorithm gets a little messy:
  6159. *
  6160. * - if the connector already has an assigned crtc, use it (but make
  6161. * sure it's on first)
  6162. *
  6163. * - try to find the first unused crtc that can drive this connector,
  6164. * and use that if we find one
  6165. */
  6166. /* See if we already have a CRTC for this connector */
  6167. if (encoder->crtc) {
  6168. crtc = encoder->crtc;
  6169. mutex_lock(&crtc->mutex);
  6170. old->dpms_mode = connector->dpms;
  6171. old->load_detect_temp = false;
  6172. /* Make sure the crtc and connector are running */
  6173. if (connector->dpms != DRM_MODE_DPMS_ON)
  6174. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  6175. return true;
  6176. }
  6177. /* Find an unused one (if possible) */
  6178. list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
  6179. i++;
  6180. if (!(encoder->possible_crtcs & (1 << i)))
  6181. continue;
  6182. if (!possible_crtc->enabled) {
  6183. crtc = possible_crtc;
  6184. break;
  6185. }
  6186. }
  6187. /*
  6188. * If we didn't find an unused CRTC, don't use any.
  6189. */
  6190. if (!crtc) {
  6191. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  6192. return false;
  6193. }
  6194. mutex_lock(&crtc->mutex);
  6195. intel_encoder->new_crtc = to_intel_crtc(crtc);
  6196. to_intel_connector(connector)->new_encoder = intel_encoder;
  6197. intel_crtc = to_intel_crtc(crtc);
  6198. old->dpms_mode = connector->dpms;
  6199. old->load_detect_temp = true;
  6200. old->release_fb = NULL;
  6201. if (!mode)
  6202. mode = &load_detect_mode;
  6203. /* We need a framebuffer large enough to accommodate all accesses
  6204. * that the plane may generate whilst we perform load detection.
  6205. * We can not rely on the fbcon either being present (we get called
  6206. * during its initialisation to detect all boot displays, or it may
  6207. * not even exist) or that it is large enough to satisfy the
  6208. * requested mode.
  6209. */
  6210. fb = mode_fits_in_fbdev(dev, mode);
  6211. if (fb == NULL) {
  6212. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  6213. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  6214. old->release_fb = fb;
  6215. } else
  6216. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  6217. if (IS_ERR(fb)) {
  6218. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  6219. mutex_unlock(&crtc->mutex);
  6220. return false;
  6221. }
  6222. if (intel_set_mode(crtc, mode, 0, 0, fb)) {
  6223. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  6224. if (old->release_fb)
  6225. old->release_fb->funcs->destroy(old->release_fb);
  6226. mutex_unlock(&crtc->mutex);
  6227. return false;
  6228. }
  6229. /* let the connector get through one full cycle before testing */
  6230. intel_wait_for_vblank(dev, intel_crtc->pipe);
  6231. return true;
  6232. }
  6233. void intel_release_load_detect_pipe(struct drm_connector *connector,
  6234. struct intel_load_detect_pipe *old)
  6235. {
  6236. struct intel_encoder *intel_encoder =
  6237. intel_attached_encoder(connector);
  6238. struct drm_encoder *encoder = &intel_encoder->base;
  6239. struct drm_crtc *crtc = encoder->crtc;
  6240. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  6241. connector->base.id, drm_get_connector_name(connector),
  6242. encoder->base.id, drm_get_encoder_name(encoder));
  6243. if (old->load_detect_temp) {
  6244. to_intel_connector(connector)->new_encoder = NULL;
  6245. intel_encoder->new_crtc = NULL;
  6246. intel_set_mode(crtc, NULL, 0, 0, NULL);
  6247. if (old->release_fb) {
  6248. drm_framebuffer_unregister_private(old->release_fb);
  6249. drm_framebuffer_unreference(old->release_fb);
  6250. }
  6251. mutex_unlock(&crtc->mutex);
  6252. return;
  6253. }
  6254. /* Switch crtc and encoder back off if necessary */
  6255. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  6256. connector->funcs->dpms(connector, old->dpms_mode);
  6257. mutex_unlock(&crtc->mutex);
  6258. }
  6259. static int i9xx_pll_refclk(struct drm_device *dev,
  6260. const struct intel_crtc_config *pipe_config)
  6261. {
  6262. struct drm_i915_private *dev_priv = dev->dev_private;
  6263. u32 dpll = pipe_config->dpll_hw_state.dpll;
  6264. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  6265. return dev_priv->vbt.lvds_ssc_freq * 1000;
  6266. else if (HAS_PCH_SPLIT(dev))
  6267. return 120000;
  6268. else if (!IS_GEN2(dev))
  6269. return 96000;
  6270. else
  6271. return 48000;
  6272. }
  6273. /* Returns the clock of the currently programmed mode of the given pipe. */
  6274. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  6275. struct intel_crtc_config *pipe_config)
  6276. {
  6277. struct drm_device *dev = crtc->base.dev;
  6278. struct drm_i915_private *dev_priv = dev->dev_private;
  6279. int pipe = pipe_config->cpu_transcoder;
  6280. u32 dpll = pipe_config->dpll_hw_state.dpll;
  6281. u32 fp;
  6282. intel_clock_t clock;
  6283. int refclk = i9xx_pll_refclk(dev, pipe_config);
  6284. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  6285. fp = pipe_config->dpll_hw_state.fp0;
  6286. else
  6287. fp = pipe_config->dpll_hw_state.fp1;
  6288. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  6289. if (IS_PINEVIEW(dev)) {
  6290. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  6291. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  6292. } else {
  6293. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  6294. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  6295. }
  6296. if (!IS_GEN2(dev)) {
  6297. if (IS_PINEVIEW(dev))
  6298. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  6299. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  6300. else
  6301. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  6302. DPLL_FPA01_P1_POST_DIV_SHIFT);
  6303. switch (dpll & DPLL_MODE_MASK) {
  6304. case DPLLB_MODE_DAC_SERIAL:
  6305. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  6306. 5 : 10;
  6307. break;
  6308. case DPLLB_MODE_LVDS:
  6309. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  6310. 7 : 14;
  6311. break;
  6312. default:
  6313. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  6314. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  6315. return;
  6316. }
  6317. if (IS_PINEVIEW(dev))
  6318. pineview_clock(refclk, &clock);
  6319. else
  6320. i9xx_clock(refclk, &clock);
  6321. } else {
  6322. bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
  6323. if (is_lvds) {
  6324. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  6325. DPLL_FPA01_P1_POST_DIV_SHIFT);
  6326. clock.p2 = 14;
  6327. } else {
  6328. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  6329. clock.p1 = 2;
  6330. else {
  6331. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  6332. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  6333. }
  6334. if (dpll & PLL_P2_DIVIDE_BY_4)
  6335. clock.p2 = 4;
  6336. else
  6337. clock.p2 = 2;
  6338. }
  6339. i9xx_clock(refclk, &clock);
  6340. }
  6341. /*
  6342. * This value includes pixel_multiplier. We will use
  6343. * port_clock to compute adjusted_mode.crtc_clock in the
  6344. * encoder's get_config() function.
  6345. */
  6346. pipe_config->port_clock = clock.dot;
  6347. }
  6348. int intel_dotclock_calculate(int link_freq,
  6349. const struct intel_link_m_n *m_n)
  6350. {
  6351. /*
  6352. * The calculation for the data clock is:
  6353. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  6354. * But we want to avoid losing precison if possible, so:
  6355. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  6356. *
  6357. * and the link clock is simpler:
  6358. * link_clock = (m * link_clock) / n
  6359. */
  6360. if (!m_n->link_n)
  6361. return 0;
  6362. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  6363. }
  6364. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  6365. struct intel_crtc_config *pipe_config)
  6366. {
  6367. struct drm_device *dev = crtc->base.dev;
  6368. /* read out port_clock from the DPLL */
  6369. i9xx_crtc_clock_get(crtc, pipe_config);
  6370. /*
  6371. * This value does not include pixel_multiplier.
  6372. * We will check that port_clock and adjusted_mode.crtc_clock
  6373. * agree once we know their relationship in the encoder's
  6374. * get_config() function.
  6375. */
  6376. pipe_config->adjusted_mode.crtc_clock =
  6377. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  6378. &pipe_config->fdi_m_n);
  6379. }
  6380. /** Returns the currently programmed mode of the given pipe. */
  6381. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  6382. struct drm_crtc *crtc)
  6383. {
  6384. struct drm_i915_private *dev_priv = dev->dev_private;
  6385. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6386. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  6387. struct drm_display_mode *mode;
  6388. struct intel_crtc_config pipe_config;
  6389. int htot = I915_READ(HTOTAL(cpu_transcoder));
  6390. int hsync = I915_READ(HSYNC(cpu_transcoder));
  6391. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  6392. int vsync = I915_READ(VSYNC(cpu_transcoder));
  6393. enum pipe pipe = intel_crtc->pipe;
  6394. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  6395. if (!mode)
  6396. return NULL;
  6397. /*
  6398. * Construct a pipe_config sufficient for getting the clock info
  6399. * back out of crtc_clock_get.
  6400. *
  6401. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  6402. * to use a real value here instead.
  6403. */
  6404. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  6405. pipe_config.pixel_multiplier = 1;
  6406. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  6407. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  6408. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  6409. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  6410. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  6411. mode->hdisplay = (htot & 0xffff) + 1;
  6412. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  6413. mode->hsync_start = (hsync & 0xffff) + 1;
  6414. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  6415. mode->vdisplay = (vtot & 0xffff) + 1;
  6416. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  6417. mode->vsync_start = (vsync & 0xffff) + 1;
  6418. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  6419. drm_mode_set_name(mode);
  6420. return mode;
  6421. }
  6422. static void intel_increase_pllclock(struct drm_crtc *crtc)
  6423. {
  6424. struct drm_device *dev = crtc->dev;
  6425. drm_i915_private_t *dev_priv = dev->dev_private;
  6426. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6427. int pipe = intel_crtc->pipe;
  6428. int dpll_reg = DPLL(pipe);
  6429. int dpll;
  6430. if (HAS_PCH_SPLIT(dev))
  6431. return;
  6432. if (!dev_priv->lvds_downclock_avail)
  6433. return;
  6434. dpll = I915_READ(dpll_reg);
  6435. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  6436. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  6437. assert_panel_unlocked(dev_priv, pipe);
  6438. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  6439. I915_WRITE(dpll_reg, dpll);
  6440. intel_wait_for_vblank(dev, pipe);
  6441. dpll = I915_READ(dpll_reg);
  6442. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  6443. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  6444. }
  6445. }
  6446. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  6447. {
  6448. struct drm_device *dev = crtc->dev;
  6449. drm_i915_private_t *dev_priv = dev->dev_private;
  6450. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6451. if (HAS_PCH_SPLIT(dev))
  6452. return;
  6453. if (!dev_priv->lvds_downclock_avail)
  6454. return;
  6455. /*
  6456. * Since this is called by a timer, we should never get here in
  6457. * the manual case.
  6458. */
  6459. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  6460. int pipe = intel_crtc->pipe;
  6461. int dpll_reg = DPLL(pipe);
  6462. int dpll;
  6463. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  6464. assert_panel_unlocked(dev_priv, pipe);
  6465. dpll = I915_READ(dpll_reg);
  6466. dpll |= DISPLAY_RATE_SELECT_FPA1;
  6467. I915_WRITE(dpll_reg, dpll);
  6468. intel_wait_for_vblank(dev, pipe);
  6469. dpll = I915_READ(dpll_reg);
  6470. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  6471. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  6472. }
  6473. }
  6474. void intel_mark_busy(struct drm_device *dev)
  6475. {
  6476. struct drm_i915_private *dev_priv = dev->dev_private;
  6477. hsw_package_c8_gpu_busy(dev_priv);
  6478. i915_update_gfx_val(dev_priv);
  6479. }
  6480. void intel_mark_idle(struct drm_device *dev)
  6481. {
  6482. struct drm_i915_private *dev_priv = dev->dev_private;
  6483. struct drm_crtc *crtc;
  6484. hsw_package_c8_gpu_idle(dev_priv);
  6485. if (!i915_powersave)
  6486. return;
  6487. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  6488. if (!crtc->fb)
  6489. continue;
  6490. intel_decrease_pllclock(crtc);
  6491. }
  6492. if (dev_priv->info->gen >= 6)
  6493. gen6_rps_idle(dev->dev_private);
  6494. }
  6495. void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
  6496. struct intel_ring_buffer *ring)
  6497. {
  6498. struct drm_device *dev = obj->base.dev;
  6499. struct drm_crtc *crtc;
  6500. if (!i915_powersave)
  6501. return;
  6502. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  6503. if (!crtc->fb)
  6504. continue;
  6505. if (to_intel_framebuffer(crtc->fb)->obj != obj)
  6506. continue;
  6507. intel_increase_pllclock(crtc);
  6508. if (ring && intel_fbc_enabled(dev))
  6509. ring->fbc_dirty = true;
  6510. }
  6511. }
  6512. static void intel_crtc_destroy(struct drm_crtc *crtc)
  6513. {
  6514. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6515. struct drm_device *dev = crtc->dev;
  6516. struct intel_unpin_work *work;
  6517. unsigned long flags;
  6518. spin_lock_irqsave(&dev->event_lock, flags);
  6519. work = intel_crtc->unpin_work;
  6520. intel_crtc->unpin_work = NULL;
  6521. spin_unlock_irqrestore(&dev->event_lock, flags);
  6522. if (work) {
  6523. cancel_work_sync(&work->work);
  6524. kfree(work);
  6525. }
  6526. intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
  6527. drm_crtc_cleanup(crtc);
  6528. kfree(intel_crtc);
  6529. }
  6530. static void intel_unpin_work_fn(struct work_struct *__work)
  6531. {
  6532. struct intel_unpin_work *work =
  6533. container_of(__work, struct intel_unpin_work, work);
  6534. struct drm_device *dev = work->crtc->dev;
  6535. mutex_lock(&dev->struct_mutex);
  6536. intel_unpin_fb_obj(work->old_fb_obj);
  6537. drm_gem_object_unreference(&work->pending_flip_obj->base);
  6538. drm_gem_object_unreference(&work->old_fb_obj->base);
  6539. intel_update_fbc(dev);
  6540. mutex_unlock(&dev->struct_mutex);
  6541. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  6542. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  6543. kfree(work);
  6544. }
  6545. static void do_intel_finish_page_flip(struct drm_device *dev,
  6546. struct drm_crtc *crtc)
  6547. {
  6548. drm_i915_private_t *dev_priv = dev->dev_private;
  6549. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6550. struct intel_unpin_work *work;
  6551. unsigned long flags;
  6552. /* Ignore early vblank irqs */
  6553. if (intel_crtc == NULL)
  6554. return;
  6555. spin_lock_irqsave(&dev->event_lock, flags);
  6556. work = intel_crtc->unpin_work;
  6557. /* Ensure we don't miss a work->pending update ... */
  6558. smp_rmb();
  6559. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  6560. spin_unlock_irqrestore(&dev->event_lock, flags);
  6561. return;
  6562. }
  6563. /* and that the unpin work is consistent wrt ->pending. */
  6564. smp_rmb();
  6565. intel_crtc->unpin_work = NULL;
  6566. if (work->event)
  6567. drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
  6568. drm_vblank_put(dev, intel_crtc->pipe);
  6569. spin_unlock_irqrestore(&dev->event_lock, flags);
  6570. wake_up_all(&dev_priv->pending_flip_queue);
  6571. queue_work(dev_priv->wq, &work->work);
  6572. trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
  6573. }
  6574. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  6575. {
  6576. drm_i915_private_t *dev_priv = dev->dev_private;
  6577. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  6578. do_intel_finish_page_flip(dev, crtc);
  6579. }
  6580. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  6581. {
  6582. drm_i915_private_t *dev_priv = dev->dev_private;
  6583. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  6584. do_intel_finish_page_flip(dev, crtc);
  6585. }
  6586. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  6587. {
  6588. drm_i915_private_t *dev_priv = dev->dev_private;
  6589. struct intel_crtc *intel_crtc =
  6590. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  6591. unsigned long flags;
  6592. /* NB: An MMIO update of the plane base pointer will also
  6593. * generate a page-flip completion irq, i.e. every modeset
  6594. * is also accompanied by a spurious intel_prepare_page_flip().
  6595. */
  6596. spin_lock_irqsave(&dev->event_lock, flags);
  6597. if (intel_crtc->unpin_work)
  6598. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  6599. spin_unlock_irqrestore(&dev->event_lock, flags);
  6600. }
  6601. inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  6602. {
  6603. /* Ensure that the work item is consistent when activating it ... */
  6604. smp_wmb();
  6605. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  6606. /* and that it is marked active as soon as the irq could fire. */
  6607. smp_wmb();
  6608. }
  6609. static int intel_gen2_queue_flip(struct drm_device *dev,
  6610. struct drm_crtc *crtc,
  6611. struct drm_framebuffer *fb,
  6612. struct drm_i915_gem_object *obj,
  6613. uint32_t flags)
  6614. {
  6615. struct drm_i915_private *dev_priv = dev->dev_private;
  6616. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6617. u32 flip_mask;
  6618. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  6619. int ret;
  6620. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  6621. if (ret)
  6622. goto err;
  6623. ret = intel_ring_begin(ring, 6);
  6624. if (ret)
  6625. goto err_unpin;
  6626. /* Can't queue multiple flips, so wait for the previous
  6627. * one to finish before executing the next.
  6628. */
  6629. if (intel_crtc->plane)
  6630. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  6631. else
  6632. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  6633. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  6634. intel_ring_emit(ring, MI_NOOP);
  6635. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  6636. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  6637. intel_ring_emit(ring, fb->pitches[0]);
  6638. intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  6639. intel_ring_emit(ring, 0); /* aux display base address, unused */
  6640. intel_mark_page_flip_active(intel_crtc);
  6641. __intel_ring_advance(ring);
  6642. return 0;
  6643. err_unpin:
  6644. intel_unpin_fb_obj(obj);
  6645. err:
  6646. return ret;
  6647. }
  6648. static int intel_gen3_queue_flip(struct drm_device *dev,
  6649. struct drm_crtc *crtc,
  6650. struct drm_framebuffer *fb,
  6651. struct drm_i915_gem_object *obj,
  6652. uint32_t flags)
  6653. {
  6654. struct drm_i915_private *dev_priv = dev->dev_private;
  6655. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6656. u32 flip_mask;
  6657. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  6658. int ret;
  6659. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  6660. if (ret)
  6661. goto err;
  6662. ret = intel_ring_begin(ring, 6);
  6663. if (ret)
  6664. goto err_unpin;
  6665. if (intel_crtc->plane)
  6666. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  6667. else
  6668. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  6669. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  6670. intel_ring_emit(ring, MI_NOOP);
  6671. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  6672. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  6673. intel_ring_emit(ring, fb->pitches[0]);
  6674. intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  6675. intel_ring_emit(ring, MI_NOOP);
  6676. intel_mark_page_flip_active(intel_crtc);
  6677. __intel_ring_advance(ring);
  6678. return 0;
  6679. err_unpin:
  6680. intel_unpin_fb_obj(obj);
  6681. err:
  6682. return ret;
  6683. }
  6684. static int intel_gen4_queue_flip(struct drm_device *dev,
  6685. struct drm_crtc *crtc,
  6686. struct drm_framebuffer *fb,
  6687. struct drm_i915_gem_object *obj,
  6688. uint32_t flags)
  6689. {
  6690. struct drm_i915_private *dev_priv = dev->dev_private;
  6691. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6692. uint32_t pf, pipesrc;
  6693. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  6694. int ret;
  6695. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  6696. if (ret)
  6697. goto err;
  6698. ret = intel_ring_begin(ring, 4);
  6699. if (ret)
  6700. goto err_unpin;
  6701. /* i965+ uses the linear or tiled offsets from the
  6702. * Display Registers (which do not change across a page-flip)
  6703. * so we need only reprogram the base address.
  6704. */
  6705. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  6706. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  6707. intel_ring_emit(ring, fb->pitches[0]);
  6708. intel_ring_emit(ring,
  6709. (i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset) |
  6710. obj->tiling_mode);
  6711. /* XXX Enabling the panel-fitter across page-flip is so far
  6712. * untested on non-native modes, so ignore it for now.
  6713. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  6714. */
  6715. pf = 0;
  6716. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  6717. intel_ring_emit(ring, pf | pipesrc);
  6718. intel_mark_page_flip_active(intel_crtc);
  6719. __intel_ring_advance(ring);
  6720. return 0;
  6721. err_unpin:
  6722. intel_unpin_fb_obj(obj);
  6723. err:
  6724. return ret;
  6725. }
  6726. static int intel_gen6_queue_flip(struct drm_device *dev,
  6727. struct drm_crtc *crtc,
  6728. struct drm_framebuffer *fb,
  6729. struct drm_i915_gem_object *obj,
  6730. uint32_t flags)
  6731. {
  6732. struct drm_i915_private *dev_priv = dev->dev_private;
  6733. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6734. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  6735. uint32_t pf, pipesrc;
  6736. int ret;
  6737. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  6738. if (ret)
  6739. goto err;
  6740. ret = intel_ring_begin(ring, 4);
  6741. if (ret)
  6742. goto err_unpin;
  6743. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  6744. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  6745. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  6746. intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  6747. /* Contrary to the suggestions in the documentation,
  6748. * "Enable Panel Fitter" does not seem to be required when page
  6749. * flipping with a non-native mode, and worse causes a normal
  6750. * modeset to fail.
  6751. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  6752. */
  6753. pf = 0;
  6754. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  6755. intel_ring_emit(ring, pf | pipesrc);
  6756. intel_mark_page_flip_active(intel_crtc);
  6757. __intel_ring_advance(ring);
  6758. return 0;
  6759. err_unpin:
  6760. intel_unpin_fb_obj(obj);
  6761. err:
  6762. return ret;
  6763. }
  6764. static int intel_gen7_queue_flip(struct drm_device *dev,
  6765. struct drm_crtc *crtc,
  6766. struct drm_framebuffer *fb,
  6767. struct drm_i915_gem_object *obj,
  6768. uint32_t flags)
  6769. {
  6770. struct drm_i915_private *dev_priv = dev->dev_private;
  6771. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6772. struct intel_ring_buffer *ring;
  6773. uint32_t plane_bit = 0;
  6774. int len, ret;
  6775. ring = obj->ring;
  6776. if (IS_VALLEYVIEW(dev) || ring == NULL || ring->id != RCS)
  6777. ring = &dev_priv->ring[BCS];
  6778. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  6779. if (ret)
  6780. goto err;
  6781. switch(intel_crtc->plane) {
  6782. case PLANE_A:
  6783. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  6784. break;
  6785. case PLANE_B:
  6786. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  6787. break;
  6788. case PLANE_C:
  6789. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  6790. break;
  6791. default:
  6792. WARN_ONCE(1, "unknown plane in flip command\n");
  6793. ret = -ENODEV;
  6794. goto err_unpin;
  6795. }
  6796. len = 4;
  6797. if (ring->id == RCS)
  6798. len += 6;
  6799. ret = intel_ring_begin(ring, len);
  6800. if (ret)
  6801. goto err_unpin;
  6802. /* Unmask the flip-done completion message. Note that the bspec says that
  6803. * we should do this for both the BCS and RCS, and that we must not unmask
  6804. * more than one flip event at any time (or ensure that one flip message
  6805. * can be sent by waiting for flip-done prior to queueing new flips).
  6806. * Experimentation says that BCS works despite DERRMR masking all
  6807. * flip-done completion events and that unmasking all planes at once
  6808. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  6809. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  6810. */
  6811. if (ring->id == RCS) {
  6812. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  6813. intel_ring_emit(ring, DERRMR);
  6814. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  6815. DERRMR_PIPEB_PRI_FLIP_DONE |
  6816. DERRMR_PIPEC_PRI_FLIP_DONE));
  6817. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1));
  6818. intel_ring_emit(ring, DERRMR);
  6819. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  6820. }
  6821. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  6822. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  6823. intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  6824. intel_ring_emit(ring, (MI_NOOP));
  6825. intel_mark_page_flip_active(intel_crtc);
  6826. __intel_ring_advance(ring);
  6827. return 0;
  6828. err_unpin:
  6829. intel_unpin_fb_obj(obj);
  6830. err:
  6831. return ret;
  6832. }
  6833. static int intel_default_queue_flip(struct drm_device *dev,
  6834. struct drm_crtc *crtc,
  6835. struct drm_framebuffer *fb,
  6836. struct drm_i915_gem_object *obj,
  6837. uint32_t flags)
  6838. {
  6839. return -ENODEV;
  6840. }
  6841. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  6842. struct drm_framebuffer *fb,
  6843. struct drm_pending_vblank_event *event,
  6844. uint32_t page_flip_flags)
  6845. {
  6846. struct drm_device *dev = crtc->dev;
  6847. struct drm_i915_private *dev_priv = dev->dev_private;
  6848. struct drm_framebuffer *old_fb = crtc->fb;
  6849. struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
  6850. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6851. struct intel_unpin_work *work;
  6852. unsigned long flags;
  6853. int ret;
  6854. /* Can't change pixel format via MI display flips. */
  6855. if (fb->pixel_format != crtc->fb->pixel_format)
  6856. return -EINVAL;
  6857. /*
  6858. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  6859. * Note that pitch changes could also affect these register.
  6860. */
  6861. if (INTEL_INFO(dev)->gen > 3 &&
  6862. (fb->offsets[0] != crtc->fb->offsets[0] ||
  6863. fb->pitches[0] != crtc->fb->pitches[0]))
  6864. return -EINVAL;
  6865. work = kzalloc(sizeof(*work), GFP_KERNEL);
  6866. if (work == NULL)
  6867. return -ENOMEM;
  6868. work->event = event;
  6869. work->crtc = crtc;
  6870. work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
  6871. INIT_WORK(&work->work, intel_unpin_work_fn);
  6872. ret = drm_vblank_get(dev, intel_crtc->pipe);
  6873. if (ret)
  6874. goto free_work;
  6875. /* We borrow the event spin lock for protecting unpin_work */
  6876. spin_lock_irqsave(&dev->event_lock, flags);
  6877. if (intel_crtc->unpin_work) {
  6878. spin_unlock_irqrestore(&dev->event_lock, flags);
  6879. kfree(work);
  6880. drm_vblank_put(dev, intel_crtc->pipe);
  6881. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  6882. return -EBUSY;
  6883. }
  6884. intel_crtc->unpin_work = work;
  6885. spin_unlock_irqrestore(&dev->event_lock, flags);
  6886. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  6887. flush_workqueue(dev_priv->wq);
  6888. ret = i915_mutex_lock_interruptible(dev);
  6889. if (ret)
  6890. goto cleanup;
  6891. /* Reference the objects for the scheduled work. */
  6892. drm_gem_object_reference(&work->old_fb_obj->base);
  6893. drm_gem_object_reference(&obj->base);
  6894. crtc->fb = fb;
  6895. work->pending_flip_obj = obj;
  6896. work->enable_stall_check = true;
  6897. atomic_inc(&intel_crtc->unpin_work_count);
  6898. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  6899. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, page_flip_flags);
  6900. if (ret)
  6901. goto cleanup_pending;
  6902. intel_disable_fbc(dev);
  6903. intel_mark_fb_busy(obj, NULL);
  6904. mutex_unlock(&dev->struct_mutex);
  6905. trace_i915_flip_request(intel_crtc->plane, obj);
  6906. return 0;
  6907. cleanup_pending:
  6908. atomic_dec(&intel_crtc->unpin_work_count);
  6909. crtc->fb = old_fb;
  6910. drm_gem_object_unreference(&work->old_fb_obj->base);
  6911. drm_gem_object_unreference(&obj->base);
  6912. mutex_unlock(&dev->struct_mutex);
  6913. cleanup:
  6914. spin_lock_irqsave(&dev->event_lock, flags);
  6915. intel_crtc->unpin_work = NULL;
  6916. spin_unlock_irqrestore(&dev->event_lock, flags);
  6917. drm_vblank_put(dev, intel_crtc->pipe);
  6918. free_work:
  6919. kfree(work);
  6920. return ret;
  6921. }
  6922. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  6923. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  6924. .load_lut = intel_crtc_load_lut,
  6925. };
  6926. static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
  6927. struct drm_crtc *crtc)
  6928. {
  6929. struct drm_device *dev;
  6930. struct drm_crtc *tmp;
  6931. int crtc_mask = 1;
  6932. WARN(!crtc, "checking null crtc?\n");
  6933. dev = crtc->dev;
  6934. list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
  6935. if (tmp == crtc)
  6936. break;
  6937. crtc_mask <<= 1;
  6938. }
  6939. if (encoder->possible_crtcs & crtc_mask)
  6940. return true;
  6941. return false;
  6942. }
  6943. /**
  6944. * intel_modeset_update_staged_output_state
  6945. *
  6946. * Updates the staged output configuration state, e.g. after we've read out the
  6947. * current hw state.
  6948. */
  6949. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  6950. {
  6951. struct intel_encoder *encoder;
  6952. struct intel_connector *connector;
  6953. list_for_each_entry(connector, &dev->mode_config.connector_list,
  6954. base.head) {
  6955. connector->new_encoder =
  6956. to_intel_encoder(connector->base.encoder);
  6957. }
  6958. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  6959. base.head) {
  6960. encoder->new_crtc =
  6961. to_intel_crtc(encoder->base.crtc);
  6962. }
  6963. }
  6964. /**
  6965. * intel_modeset_commit_output_state
  6966. *
  6967. * This function copies the stage display pipe configuration to the real one.
  6968. */
  6969. static void intel_modeset_commit_output_state(struct drm_device *dev)
  6970. {
  6971. struct intel_encoder *encoder;
  6972. struct intel_connector *connector;
  6973. list_for_each_entry(connector, &dev->mode_config.connector_list,
  6974. base.head) {
  6975. connector->base.encoder = &connector->new_encoder->base;
  6976. }
  6977. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  6978. base.head) {
  6979. encoder->base.crtc = &encoder->new_crtc->base;
  6980. }
  6981. }
  6982. static void
  6983. connected_sink_compute_bpp(struct intel_connector * connector,
  6984. struct intel_crtc_config *pipe_config)
  6985. {
  6986. int bpp = pipe_config->pipe_bpp;
  6987. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  6988. connector->base.base.id,
  6989. drm_get_connector_name(&connector->base));
  6990. /* Don't use an invalid EDID bpc value */
  6991. if (connector->base.display_info.bpc &&
  6992. connector->base.display_info.bpc * 3 < bpp) {
  6993. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  6994. bpp, connector->base.display_info.bpc*3);
  6995. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  6996. }
  6997. /* Clamp bpp to 8 on screens without EDID 1.4 */
  6998. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  6999. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  7000. bpp);
  7001. pipe_config->pipe_bpp = 24;
  7002. }
  7003. }
  7004. static int
  7005. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  7006. struct drm_framebuffer *fb,
  7007. struct intel_crtc_config *pipe_config)
  7008. {
  7009. struct drm_device *dev = crtc->base.dev;
  7010. struct intel_connector *connector;
  7011. int bpp;
  7012. switch (fb->pixel_format) {
  7013. case DRM_FORMAT_C8:
  7014. bpp = 8*3; /* since we go through a colormap */
  7015. break;
  7016. case DRM_FORMAT_XRGB1555:
  7017. case DRM_FORMAT_ARGB1555:
  7018. /* checked in intel_framebuffer_init already */
  7019. if (WARN_ON(INTEL_INFO(dev)->gen > 3))
  7020. return -EINVAL;
  7021. case DRM_FORMAT_RGB565:
  7022. bpp = 6*3; /* min is 18bpp */
  7023. break;
  7024. case DRM_FORMAT_XBGR8888:
  7025. case DRM_FORMAT_ABGR8888:
  7026. /* checked in intel_framebuffer_init already */
  7027. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  7028. return -EINVAL;
  7029. case DRM_FORMAT_XRGB8888:
  7030. case DRM_FORMAT_ARGB8888:
  7031. bpp = 8*3;
  7032. break;
  7033. case DRM_FORMAT_XRGB2101010:
  7034. case DRM_FORMAT_ARGB2101010:
  7035. case DRM_FORMAT_XBGR2101010:
  7036. case DRM_FORMAT_ABGR2101010:
  7037. /* checked in intel_framebuffer_init already */
  7038. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  7039. return -EINVAL;
  7040. bpp = 10*3;
  7041. break;
  7042. /* TODO: gen4+ supports 16 bpc floating point, too. */
  7043. default:
  7044. DRM_DEBUG_KMS("unsupported depth\n");
  7045. return -EINVAL;
  7046. }
  7047. pipe_config->pipe_bpp = bpp;
  7048. /* Clamp display bpp to EDID value */
  7049. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7050. base.head) {
  7051. if (!connector->new_encoder ||
  7052. connector->new_encoder->new_crtc != crtc)
  7053. continue;
  7054. connected_sink_compute_bpp(connector, pipe_config);
  7055. }
  7056. return bpp;
  7057. }
  7058. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  7059. {
  7060. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  7061. "type: 0x%x flags: 0x%x\n",
  7062. mode->crtc_clock,
  7063. mode->crtc_hdisplay, mode->crtc_hsync_start,
  7064. mode->crtc_hsync_end, mode->crtc_htotal,
  7065. mode->crtc_vdisplay, mode->crtc_vsync_start,
  7066. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  7067. }
  7068. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  7069. struct intel_crtc_config *pipe_config,
  7070. const char *context)
  7071. {
  7072. DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
  7073. context, pipe_name(crtc->pipe));
  7074. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  7075. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  7076. pipe_config->pipe_bpp, pipe_config->dither);
  7077. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  7078. pipe_config->has_pch_encoder,
  7079. pipe_config->fdi_lanes,
  7080. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  7081. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  7082. pipe_config->fdi_m_n.tu);
  7083. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  7084. pipe_config->has_dp_encoder,
  7085. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  7086. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  7087. pipe_config->dp_m_n.tu);
  7088. DRM_DEBUG_KMS("requested mode:\n");
  7089. drm_mode_debug_printmodeline(&pipe_config->requested_mode);
  7090. DRM_DEBUG_KMS("adjusted mode:\n");
  7091. drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
  7092. intel_dump_crtc_timings(&pipe_config->adjusted_mode);
  7093. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  7094. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  7095. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  7096. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  7097. pipe_config->gmch_pfit.control,
  7098. pipe_config->gmch_pfit.pgm_ratios,
  7099. pipe_config->gmch_pfit.lvds_border_bits);
  7100. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  7101. pipe_config->pch_pfit.pos,
  7102. pipe_config->pch_pfit.size,
  7103. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  7104. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  7105. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  7106. }
  7107. static bool check_encoder_cloning(struct drm_crtc *crtc)
  7108. {
  7109. int num_encoders = 0;
  7110. bool uncloneable_encoders = false;
  7111. struct intel_encoder *encoder;
  7112. list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list,
  7113. base.head) {
  7114. if (&encoder->new_crtc->base != crtc)
  7115. continue;
  7116. num_encoders++;
  7117. if (!encoder->cloneable)
  7118. uncloneable_encoders = true;
  7119. }
  7120. return !(num_encoders > 1 && uncloneable_encoders);
  7121. }
  7122. static struct intel_crtc_config *
  7123. intel_modeset_pipe_config(struct drm_crtc *crtc,
  7124. struct drm_framebuffer *fb,
  7125. struct drm_display_mode *mode)
  7126. {
  7127. struct drm_device *dev = crtc->dev;
  7128. struct intel_encoder *encoder;
  7129. struct intel_crtc_config *pipe_config;
  7130. int plane_bpp, ret = -EINVAL;
  7131. bool retry = true;
  7132. if (!check_encoder_cloning(crtc)) {
  7133. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  7134. return ERR_PTR(-EINVAL);
  7135. }
  7136. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  7137. if (!pipe_config)
  7138. return ERR_PTR(-ENOMEM);
  7139. drm_mode_copy(&pipe_config->adjusted_mode, mode);
  7140. drm_mode_copy(&pipe_config->requested_mode, mode);
  7141. pipe_config->cpu_transcoder =
  7142. (enum transcoder) to_intel_crtc(crtc)->pipe;
  7143. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7144. /*
  7145. * Sanitize sync polarity flags based on requested ones. If neither
  7146. * positive or negative polarity is requested, treat this as meaning
  7147. * negative polarity.
  7148. */
  7149. if (!(pipe_config->adjusted_mode.flags &
  7150. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  7151. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  7152. if (!(pipe_config->adjusted_mode.flags &
  7153. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  7154. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  7155. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  7156. * plane pixel format and any sink constraints into account. Returns the
  7157. * source plane bpp so that dithering can be selected on mismatches
  7158. * after encoders and crtc also have had their say. */
  7159. plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  7160. fb, pipe_config);
  7161. if (plane_bpp < 0)
  7162. goto fail;
  7163. /*
  7164. * Determine the real pipe dimensions. Note that stereo modes can
  7165. * increase the actual pipe size due to the frame doubling and
  7166. * insertion of additional space for blanks between the frame. This
  7167. * is stored in the crtc timings. We use the requested mode to do this
  7168. * computation to clearly distinguish it from the adjusted mode, which
  7169. * can be changed by the connectors in the below retry loop.
  7170. */
  7171. drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
  7172. pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
  7173. pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
  7174. encoder_retry:
  7175. /* Ensure the port clock defaults are reset when retrying. */
  7176. pipe_config->port_clock = 0;
  7177. pipe_config->pixel_multiplier = 1;
  7178. /* Fill in default crtc timings, allow encoders to overwrite them. */
  7179. drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
  7180. /* Pass our mode to the connectors and the CRTC to give them a chance to
  7181. * adjust it according to limitations or connector properties, and also
  7182. * a chance to reject the mode entirely.
  7183. */
  7184. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  7185. base.head) {
  7186. if (&encoder->new_crtc->base != crtc)
  7187. continue;
  7188. if (!(encoder->compute_config(encoder, pipe_config))) {
  7189. DRM_DEBUG_KMS("Encoder config failure\n");
  7190. goto fail;
  7191. }
  7192. }
  7193. /* Set default port clock if not overwritten by the encoder. Needs to be
  7194. * done afterwards in case the encoder adjusts the mode. */
  7195. if (!pipe_config->port_clock)
  7196. pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
  7197. * pipe_config->pixel_multiplier;
  7198. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  7199. if (ret < 0) {
  7200. DRM_DEBUG_KMS("CRTC fixup failed\n");
  7201. goto fail;
  7202. }
  7203. if (ret == RETRY) {
  7204. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  7205. ret = -EINVAL;
  7206. goto fail;
  7207. }
  7208. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  7209. retry = false;
  7210. goto encoder_retry;
  7211. }
  7212. pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
  7213. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  7214. plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  7215. return pipe_config;
  7216. fail:
  7217. kfree(pipe_config);
  7218. return ERR_PTR(ret);
  7219. }
  7220. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  7221. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  7222. static void
  7223. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  7224. unsigned *prepare_pipes, unsigned *disable_pipes)
  7225. {
  7226. struct intel_crtc *intel_crtc;
  7227. struct drm_device *dev = crtc->dev;
  7228. struct intel_encoder *encoder;
  7229. struct intel_connector *connector;
  7230. struct drm_crtc *tmp_crtc;
  7231. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  7232. /* Check which crtcs have changed outputs connected to them, these need
  7233. * to be part of the prepare_pipes mask. We don't (yet) support global
  7234. * modeset across multiple crtcs, so modeset_pipes will only have one
  7235. * bit set at most. */
  7236. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7237. base.head) {
  7238. if (connector->base.encoder == &connector->new_encoder->base)
  7239. continue;
  7240. if (connector->base.encoder) {
  7241. tmp_crtc = connector->base.encoder->crtc;
  7242. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  7243. }
  7244. if (connector->new_encoder)
  7245. *prepare_pipes |=
  7246. 1 << connector->new_encoder->new_crtc->pipe;
  7247. }
  7248. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  7249. base.head) {
  7250. if (encoder->base.crtc == &encoder->new_crtc->base)
  7251. continue;
  7252. if (encoder->base.crtc) {
  7253. tmp_crtc = encoder->base.crtc;
  7254. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  7255. }
  7256. if (encoder->new_crtc)
  7257. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  7258. }
  7259. /* Check for any pipes that will be fully disabled ... */
  7260. list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
  7261. base.head) {
  7262. bool used = false;
  7263. /* Don't try to disable disabled crtcs. */
  7264. if (!intel_crtc->base.enabled)
  7265. continue;
  7266. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  7267. base.head) {
  7268. if (encoder->new_crtc == intel_crtc)
  7269. used = true;
  7270. }
  7271. if (!used)
  7272. *disable_pipes |= 1 << intel_crtc->pipe;
  7273. }
  7274. /* set_mode is also used to update properties on life display pipes. */
  7275. intel_crtc = to_intel_crtc(crtc);
  7276. if (crtc->enabled)
  7277. *prepare_pipes |= 1 << intel_crtc->pipe;
  7278. /*
  7279. * For simplicity do a full modeset on any pipe where the output routing
  7280. * changed. We could be more clever, but that would require us to be
  7281. * more careful with calling the relevant encoder->mode_set functions.
  7282. */
  7283. if (*prepare_pipes)
  7284. *modeset_pipes = *prepare_pipes;
  7285. /* ... and mask these out. */
  7286. *modeset_pipes &= ~(*disable_pipes);
  7287. *prepare_pipes &= ~(*disable_pipes);
  7288. /*
  7289. * HACK: We don't (yet) fully support global modesets. intel_set_config
  7290. * obies this rule, but the modeset restore mode of
  7291. * intel_modeset_setup_hw_state does not.
  7292. */
  7293. *modeset_pipes &= 1 << intel_crtc->pipe;
  7294. *prepare_pipes &= 1 << intel_crtc->pipe;
  7295. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  7296. *modeset_pipes, *prepare_pipes, *disable_pipes);
  7297. }
  7298. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  7299. {
  7300. struct drm_encoder *encoder;
  7301. struct drm_device *dev = crtc->dev;
  7302. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  7303. if (encoder->crtc == crtc)
  7304. return true;
  7305. return false;
  7306. }
  7307. static void
  7308. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  7309. {
  7310. struct intel_encoder *intel_encoder;
  7311. struct intel_crtc *intel_crtc;
  7312. struct drm_connector *connector;
  7313. list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
  7314. base.head) {
  7315. if (!intel_encoder->base.crtc)
  7316. continue;
  7317. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  7318. if (prepare_pipes & (1 << intel_crtc->pipe))
  7319. intel_encoder->connectors_active = false;
  7320. }
  7321. intel_modeset_commit_output_state(dev);
  7322. /* Update computed state. */
  7323. list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
  7324. base.head) {
  7325. intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
  7326. }
  7327. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  7328. if (!connector->encoder || !connector->encoder->crtc)
  7329. continue;
  7330. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  7331. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  7332. struct drm_property *dpms_property =
  7333. dev->mode_config.dpms_property;
  7334. connector->dpms = DRM_MODE_DPMS_ON;
  7335. drm_object_property_set_value(&connector->base,
  7336. dpms_property,
  7337. DRM_MODE_DPMS_ON);
  7338. intel_encoder = to_intel_encoder(connector->encoder);
  7339. intel_encoder->connectors_active = true;
  7340. }
  7341. }
  7342. }
  7343. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  7344. {
  7345. int diff;
  7346. if (clock1 == clock2)
  7347. return true;
  7348. if (!clock1 || !clock2)
  7349. return false;
  7350. diff = abs(clock1 - clock2);
  7351. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  7352. return true;
  7353. return false;
  7354. }
  7355. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  7356. list_for_each_entry((intel_crtc), \
  7357. &(dev)->mode_config.crtc_list, \
  7358. base.head) \
  7359. if (mask & (1 <<(intel_crtc)->pipe))
  7360. static bool
  7361. intel_pipe_config_compare(struct drm_device *dev,
  7362. struct intel_crtc_config *current_config,
  7363. struct intel_crtc_config *pipe_config)
  7364. {
  7365. #define PIPE_CONF_CHECK_X(name) \
  7366. if (current_config->name != pipe_config->name) { \
  7367. DRM_ERROR("mismatch in " #name " " \
  7368. "(expected 0x%08x, found 0x%08x)\n", \
  7369. current_config->name, \
  7370. pipe_config->name); \
  7371. return false; \
  7372. }
  7373. #define PIPE_CONF_CHECK_I(name) \
  7374. if (current_config->name != pipe_config->name) { \
  7375. DRM_ERROR("mismatch in " #name " " \
  7376. "(expected %i, found %i)\n", \
  7377. current_config->name, \
  7378. pipe_config->name); \
  7379. return false; \
  7380. }
  7381. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  7382. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  7383. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  7384. "(expected %i, found %i)\n", \
  7385. current_config->name & (mask), \
  7386. pipe_config->name & (mask)); \
  7387. return false; \
  7388. }
  7389. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  7390. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  7391. DRM_ERROR("mismatch in " #name " " \
  7392. "(expected %i, found %i)\n", \
  7393. current_config->name, \
  7394. pipe_config->name); \
  7395. return false; \
  7396. }
  7397. #define PIPE_CONF_QUIRK(quirk) \
  7398. ((current_config->quirks | pipe_config->quirks) & (quirk))
  7399. PIPE_CONF_CHECK_I(cpu_transcoder);
  7400. PIPE_CONF_CHECK_I(has_pch_encoder);
  7401. PIPE_CONF_CHECK_I(fdi_lanes);
  7402. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  7403. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  7404. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  7405. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  7406. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  7407. PIPE_CONF_CHECK_I(has_dp_encoder);
  7408. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  7409. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  7410. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  7411. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  7412. PIPE_CONF_CHECK_I(dp_m_n.tu);
  7413. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
  7414. PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
  7415. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
  7416. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
  7417. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
  7418. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
  7419. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
  7420. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
  7421. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
  7422. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
  7423. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
  7424. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
  7425. PIPE_CONF_CHECK_I(pixel_multiplier);
  7426. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  7427. DRM_MODE_FLAG_INTERLACE);
  7428. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  7429. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  7430. DRM_MODE_FLAG_PHSYNC);
  7431. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  7432. DRM_MODE_FLAG_NHSYNC);
  7433. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  7434. DRM_MODE_FLAG_PVSYNC);
  7435. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  7436. DRM_MODE_FLAG_NVSYNC);
  7437. }
  7438. PIPE_CONF_CHECK_I(pipe_src_w);
  7439. PIPE_CONF_CHECK_I(pipe_src_h);
  7440. PIPE_CONF_CHECK_I(gmch_pfit.control);
  7441. /* pfit ratios are autocomputed by the hw on gen4+ */
  7442. if (INTEL_INFO(dev)->gen < 4)
  7443. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  7444. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  7445. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  7446. if (current_config->pch_pfit.enabled) {
  7447. PIPE_CONF_CHECK_I(pch_pfit.pos);
  7448. PIPE_CONF_CHECK_I(pch_pfit.size);
  7449. }
  7450. PIPE_CONF_CHECK_I(ips_enabled);
  7451. PIPE_CONF_CHECK_I(double_wide);
  7452. PIPE_CONF_CHECK_I(shared_dpll);
  7453. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  7454. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  7455. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  7456. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  7457. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  7458. PIPE_CONF_CHECK_I(pipe_bpp);
  7459. if (!IS_HASWELL(dev)) {
  7460. PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
  7461. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  7462. }
  7463. #undef PIPE_CONF_CHECK_X
  7464. #undef PIPE_CONF_CHECK_I
  7465. #undef PIPE_CONF_CHECK_FLAGS
  7466. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  7467. #undef PIPE_CONF_QUIRK
  7468. return true;
  7469. }
  7470. static void
  7471. check_connector_state(struct drm_device *dev)
  7472. {
  7473. struct intel_connector *connector;
  7474. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7475. base.head) {
  7476. /* This also checks the encoder/connector hw state with the
  7477. * ->get_hw_state callbacks. */
  7478. intel_connector_check_state(connector);
  7479. WARN(&connector->new_encoder->base != connector->base.encoder,
  7480. "connector's staged encoder doesn't match current encoder\n");
  7481. }
  7482. }
  7483. static void
  7484. check_encoder_state(struct drm_device *dev)
  7485. {
  7486. struct intel_encoder *encoder;
  7487. struct intel_connector *connector;
  7488. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  7489. base.head) {
  7490. bool enabled = false;
  7491. bool active = false;
  7492. enum pipe pipe, tracked_pipe;
  7493. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  7494. encoder->base.base.id,
  7495. drm_get_encoder_name(&encoder->base));
  7496. WARN(&encoder->new_crtc->base != encoder->base.crtc,
  7497. "encoder's stage crtc doesn't match current crtc\n");
  7498. WARN(encoder->connectors_active && !encoder->base.crtc,
  7499. "encoder's active_connectors set, but no crtc\n");
  7500. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7501. base.head) {
  7502. if (connector->base.encoder != &encoder->base)
  7503. continue;
  7504. enabled = true;
  7505. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  7506. active = true;
  7507. }
  7508. WARN(!!encoder->base.crtc != enabled,
  7509. "encoder's enabled state mismatch "
  7510. "(expected %i, found %i)\n",
  7511. !!encoder->base.crtc, enabled);
  7512. WARN(active && !encoder->base.crtc,
  7513. "active encoder with no crtc\n");
  7514. WARN(encoder->connectors_active != active,
  7515. "encoder's computed active state doesn't match tracked active state "
  7516. "(expected %i, found %i)\n", active, encoder->connectors_active);
  7517. active = encoder->get_hw_state(encoder, &pipe);
  7518. WARN(active != encoder->connectors_active,
  7519. "encoder's hw state doesn't match sw tracking "
  7520. "(expected %i, found %i)\n",
  7521. encoder->connectors_active, active);
  7522. if (!encoder->base.crtc)
  7523. continue;
  7524. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  7525. WARN(active && pipe != tracked_pipe,
  7526. "active encoder's pipe doesn't match"
  7527. "(expected %i, found %i)\n",
  7528. tracked_pipe, pipe);
  7529. }
  7530. }
  7531. static void
  7532. check_crtc_state(struct drm_device *dev)
  7533. {
  7534. drm_i915_private_t *dev_priv = dev->dev_private;
  7535. struct intel_crtc *crtc;
  7536. struct intel_encoder *encoder;
  7537. struct intel_crtc_config pipe_config;
  7538. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  7539. base.head) {
  7540. bool enabled = false;
  7541. bool active = false;
  7542. memset(&pipe_config, 0, sizeof(pipe_config));
  7543. DRM_DEBUG_KMS("[CRTC:%d]\n",
  7544. crtc->base.base.id);
  7545. WARN(crtc->active && !crtc->base.enabled,
  7546. "active crtc, but not enabled in sw tracking\n");
  7547. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  7548. base.head) {
  7549. if (encoder->base.crtc != &crtc->base)
  7550. continue;
  7551. enabled = true;
  7552. if (encoder->connectors_active)
  7553. active = true;
  7554. }
  7555. WARN(active != crtc->active,
  7556. "crtc's computed active state doesn't match tracked active state "
  7557. "(expected %i, found %i)\n", active, crtc->active);
  7558. WARN(enabled != crtc->base.enabled,
  7559. "crtc's computed enabled state doesn't match tracked enabled state "
  7560. "(expected %i, found %i)\n", enabled, crtc->base.enabled);
  7561. active = dev_priv->display.get_pipe_config(crtc,
  7562. &pipe_config);
  7563. /* hw state is inconsistent with the pipe A quirk */
  7564. if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
  7565. active = crtc->active;
  7566. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  7567. base.head) {
  7568. enum pipe pipe;
  7569. if (encoder->base.crtc != &crtc->base)
  7570. continue;
  7571. if (encoder->get_config &&
  7572. encoder->get_hw_state(encoder, &pipe))
  7573. encoder->get_config(encoder, &pipe_config);
  7574. }
  7575. WARN(crtc->active != active,
  7576. "crtc active state doesn't match with hw state "
  7577. "(expected %i, found %i)\n", crtc->active, active);
  7578. if (active &&
  7579. !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
  7580. WARN(1, "pipe state doesn't match!\n");
  7581. intel_dump_pipe_config(crtc, &pipe_config,
  7582. "[hw state]");
  7583. intel_dump_pipe_config(crtc, &crtc->config,
  7584. "[sw state]");
  7585. }
  7586. }
  7587. }
  7588. static void
  7589. check_shared_dpll_state(struct drm_device *dev)
  7590. {
  7591. drm_i915_private_t *dev_priv = dev->dev_private;
  7592. struct intel_crtc *crtc;
  7593. struct intel_dpll_hw_state dpll_hw_state;
  7594. int i;
  7595. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  7596. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  7597. int enabled_crtcs = 0, active_crtcs = 0;
  7598. bool active;
  7599. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  7600. DRM_DEBUG_KMS("%s\n", pll->name);
  7601. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  7602. WARN(pll->active > pll->refcount,
  7603. "more active pll users than references: %i vs %i\n",
  7604. pll->active, pll->refcount);
  7605. WARN(pll->active && !pll->on,
  7606. "pll in active use but not on in sw tracking\n");
  7607. WARN(pll->on && !pll->active,
  7608. "pll in on but not on in use in sw tracking\n");
  7609. WARN(pll->on != active,
  7610. "pll on state mismatch (expected %i, found %i)\n",
  7611. pll->on, active);
  7612. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  7613. base.head) {
  7614. if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
  7615. enabled_crtcs++;
  7616. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  7617. active_crtcs++;
  7618. }
  7619. WARN(pll->active != active_crtcs,
  7620. "pll active crtcs mismatch (expected %i, found %i)\n",
  7621. pll->active, active_crtcs);
  7622. WARN(pll->refcount != enabled_crtcs,
  7623. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  7624. pll->refcount, enabled_crtcs);
  7625. WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
  7626. sizeof(dpll_hw_state)),
  7627. "pll hw state mismatch\n");
  7628. }
  7629. }
  7630. void
  7631. intel_modeset_check_state(struct drm_device *dev)
  7632. {
  7633. check_connector_state(dev);
  7634. check_encoder_state(dev);
  7635. check_crtc_state(dev);
  7636. check_shared_dpll_state(dev);
  7637. }
  7638. void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
  7639. int dotclock)
  7640. {
  7641. /*
  7642. * FDI already provided one idea for the dotclock.
  7643. * Yell if the encoder disagrees.
  7644. */
  7645. WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
  7646. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  7647. pipe_config->adjusted_mode.crtc_clock, dotclock);
  7648. }
  7649. static int __intel_set_mode(struct drm_crtc *crtc,
  7650. struct drm_display_mode *mode,
  7651. int x, int y, struct drm_framebuffer *fb)
  7652. {
  7653. struct drm_device *dev = crtc->dev;
  7654. drm_i915_private_t *dev_priv = dev->dev_private;
  7655. struct drm_display_mode *saved_mode, *saved_hwmode;
  7656. struct intel_crtc_config *pipe_config = NULL;
  7657. struct intel_crtc *intel_crtc;
  7658. unsigned disable_pipes, prepare_pipes, modeset_pipes;
  7659. int ret = 0;
  7660. saved_mode = kcalloc(2, sizeof(*saved_mode), GFP_KERNEL);
  7661. if (!saved_mode)
  7662. return -ENOMEM;
  7663. saved_hwmode = saved_mode + 1;
  7664. intel_modeset_affected_pipes(crtc, &modeset_pipes,
  7665. &prepare_pipes, &disable_pipes);
  7666. *saved_hwmode = crtc->hwmode;
  7667. *saved_mode = crtc->mode;
  7668. /* Hack: Because we don't (yet) support global modeset on multiple
  7669. * crtcs, we don't keep track of the new mode for more than one crtc.
  7670. * Hence simply check whether any bit is set in modeset_pipes in all the
  7671. * pieces of code that are not yet converted to deal with mutliple crtcs
  7672. * changing their mode at the same time. */
  7673. if (modeset_pipes) {
  7674. pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
  7675. if (IS_ERR(pipe_config)) {
  7676. ret = PTR_ERR(pipe_config);
  7677. pipe_config = NULL;
  7678. goto out;
  7679. }
  7680. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  7681. "[modeset]");
  7682. }
  7683. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  7684. intel_crtc_disable(&intel_crtc->base);
  7685. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  7686. if (intel_crtc->base.enabled)
  7687. dev_priv->display.crtc_disable(&intel_crtc->base);
  7688. }
  7689. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  7690. * to set it here already despite that we pass it down the callchain.
  7691. */
  7692. if (modeset_pipes) {
  7693. crtc->mode = *mode;
  7694. /* mode_set/enable/disable functions rely on a correct pipe
  7695. * config. */
  7696. to_intel_crtc(crtc)->config = *pipe_config;
  7697. }
  7698. /* Only after disabling all output pipelines that will be changed can we
  7699. * update the the output configuration. */
  7700. intel_modeset_update_state(dev, prepare_pipes);
  7701. if (dev_priv->display.modeset_global_resources)
  7702. dev_priv->display.modeset_global_resources(dev);
  7703. /* Set up the DPLL and any encoders state that needs to adjust or depend
  7704. * on the DPLL.
  7705. */
  7706. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  7707. ret = intel_crtc_mode_set(&intel_crtc->base,
  7708. x, y, fb);
  7709. if (ret)
  7710. goto done;
  7711. }
  7712. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  7713. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
  7714. dev_priv->display.crtc_enable(&intel_crtc->base);
  7715. if (modeset_pipes) {
  7716. /* Store real post-adjustment hardware mode. */
  7717. crtc->hwmode = pipe_config->adjusted_mode;
  7718. /* Calculate and store various constants which
  7719. * are later needed by vblank and swap-completion
  7720. * timestamping. They are derived from true hwmode.
  7721. */
  7722. drm_calc_timestamping_constants(crtc);
  7723. }
  7724. /* FIXME: add subpixel order */
  7725. done:
  7726. if (ret && crtc->enabled) {
  7727. crtc->hwmode = *saved_hwmode;
  7728. crtc->mode = *saved_mode;
  7729. }
  7730. out:
  7731. kfree(pipe_config);
  7732. kfree(saved_mode);
  7733. return ret;
  7734. }
  7735. static int intel_set_mode(struct drm_crtc *crtc,
  7736. struct drm_display_mode *mode,
  7737. int x, int y, struct drm_framebuffer *fb)
  7738. {
  7739. int ret;
  7740. ret = __intel_set_mode(crtc, mode, x, y, fb);
  7741. if (ret == 0)
  7742. intel_modeset_check_state(crtc->dev);
  7743. return ret;
  7744. }
  7745. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  7746. {
  7747. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
  7748. }
  7749. #undef for_each_intel_crtc_masked
  7750. static void intel_set_config_free(struct intel_set_config *config)
  7751. {
  7752. if (!config)
  7753. return;
  7754. kfree(config->save_connector_encoders);
  7755. kfree(config->save_encoder_crtcs);
  7756. kfree(config);
  7757. }
  7758. static int intel_set_config_save_state(struct drm_device *dev,
  7759. struct intel_set_config *config)
  7760. {
  7761. struct drm_encoder *encoder;
  7762. struct drm_connector *connector;
  7763. int count;
  7764. config->save_encoder_crtcs =
  7765. kcalloc(dev->mode_config.num_encoder,
  7766. sizeof(struct drm_crtc *), GFP_KERNEL);
  7767. if (!config->save_encoder_crtcs)
  7768. return -ENOMEM;
  7769. config->save_connector_encoders =
  7770. kcalloc(dev->mode_config.num_connector,
  7771. sizeof(struct drm_encoder *), GFP_KERNEL);
  7772. if (!config->save_connector_encoders)
  7773. return -ENOMEM;
  7774. /* Copy data. Note that driver private data is not affected.
  7775. * Should anything bad happen only the expected state is
  7776. * restored, not the drivers personal bookkeeping.
  7777. */
  7778. count = 0;
  7779. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  7780. config->save_encoder_crtcs[count++] = encoder->crtc;
  7781. }
  7782. count = 0;
  7783. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  7784. config->save_connector_encoders[count++] = connector->encoder;
  7785. }
  7786. return 0;
  7787. }
  7788. static void intel_set_config_restore_state(struct drm_device *dev,
  7789. struct intel_set_config *config)
  7790. {
  7791. struct intel_encoder *encoder;
  7792. struct intel_connector *connector;
  7793. int count;
  7794. count = 0;
  7795. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  7796. encoder->new_crtc =
  7797. to_intel_crtc(config->save_encoder_crtcs[count++]);
  7798. }
  7799. count = 0;
  7800. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  7801. connector->new_encoder =
  7802. to_intel_encoder(config->save_connector_encoders[count++]);
  7803. }
  7804. }
  7805. static bool
  7806. is_crtc_connector_off(struct drm_mode_set *set)
  7807. {
  7808. int i;
  7809. if (set->num_connectors == 0)
  7810. return false;
  7811. if (WARN_ON(set->connectors == NULL))
  7812. return false;
  7813. for (i = 0; i < set->num_connectors; i++)
  7814. if (set->connectors[i]->encoder &&
  7815. set->connectors[i]->encoder->crtc == set->crtc &&
  7816. set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
  7817. return true;
  7818. return false;
  7819. }
  7820. static void
  7821. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  7822. struct intel_set_config *config)
  7823. {
  7824. /* We should be able to check here if the fb has the same properties
  7825. * and then just flip_or_move it */
  7826. if (is_crtc_connector_off(set)) {
  7827. config->mode_changed = true;
  7828. } else if (set->crtc->fb != set->fb) {
  7829. /* If we have no fb then treat it as a full mode set */
  7830. if (set->crtc->fb == NULL) {
  7831. struct intel_crtc *intel_crtc =
  7832. to_intel_crtc(set->crtc);
  7833. if (intel_crtc->active && i915_fastboot) {
  7834. DRM_DEBUG_KMS("crtc has no fb, will flip\n");
  7835. config->fb_changed = true;
  7836. } else {
  7837. DRM_DEBUG_KMS("inactive crtc, full mode set\n");
  7838. config->mode_changed = true;
  7839. }
  7840. } else if (set->fb == NULL) {
  7841. config->mode_changed = true;
  7842. } else if (set->fb->pixel_format !=
  7843. set->crtc->fb->pixel_format) {
  7844. config->mode_changed = true;
  7845. } else {
  7846. config->fb_changed = true;
  7847. }
  7848. }
  7849. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  7850. config->fb_changed = true;
  7851. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  7852. DRM_DEBUG_KMS("modes are different, full mode set\n");
  7853. drm_mode_debug_printmodeline(&set->crtc->mode);
  7854. drm_mode_debug_printmodeline(set->mode);
  7855. config->mode_changed = true;
  7856. }
  7857. DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
  7858. set->crtc->base.id, config->mode_changed, config->fb_changed);
  7859. }
  7860. static int
  7861. intel_modeset_stage_output_state(struct drm_device *dev,
  7862. struct drm_mode_set *set,
  7863. struct intel_set_config *config)
  7864. {
  7865. struct drm_crtc *new_crtc;
  7866. struct intel_connector *connector;
  7867. struct intel_encoder *encoder;
  7868. int ro;
  7869. /* The upper layers ensure that we either disable a crtc or have a list
  7870. * of connectors. For paranoia, double-check this. */
  7871. WARN_ON(!set->fb && (set->num_connectors != 0));
  7872. WARN_ON(set->fb && (set->num_connectors == 0));
  7873. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7874. base.head) {
  7875. /* Otherwise traverse passed in connector list and get encoders
  7876. * for them. */
  7877. for (ro = 0; ro < set->num_connectors; ro++) {
  7878. if (set->connectors[ro] == &connector->base) {
  7879. connector->new_encoder = connector->encoder;
  7880. break;
  7881. }
  7882. }
  7883. /* If we disable the crtc, disable all its connectors. Also, if
  7884. * the connector is on the changing crtc but not on the new
  7885. * connector list, disable it. */
  7886. if ((!set->fb || ro == set->num_connectors) &&
  7887. connector->base.encoder &&
  7888. connector->base.encoder->crtc == set->crtc) {
  7889. connector->new_encoder = NULL;
  7890. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  7891. connector->base.base.id,
  7892. drm_get_connector_name(&connector->base));
  7893. }
  7894. if (&connector->new_encoder->base != connector->base.encoder) {
  7895. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  7896. config->mode_changed = true;
  7897. }
  7898. }
  7899. /* connector->new_encoder is now updated for all connectors. */
  7900. /* Update crtc of enabled connectors. */
  7901. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7902. base.head) {
  7903. if (!connector->new_encoder)
  7904. continue;
  7905. new_crtc = connector->new_encoder->base.crtc;
  7906. for (ro = 0; ro < set->num_connectors; ro++) {
  7907. if (set->connectors[ro] == &connector->base)
  7908. new_crtc = set->crtc;
  7909. }
  7910. /* Make sure the new CRTC will work with the encoder */
  7911. if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
  7912. new_crtc)) {
  7913. return -EINVAL;
  7914. }
  7915. connector->encoder->new_crtc = to_intel_crtc(new_crtc);
  7916. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  7917. connector->base.base.id,
  7918. drm_get_connector_name(&connector->base),
  7919. new_crtc->base.id);
  7920. }
  7921. /* Check for any encoders that needs to be disabled. */
  7922. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  7923. base.head) {
  7924. list_for_each_entry(connector,
  7925. &dev->mode_config.connector_list,
  7926. base.head) {
  7927. if (connector->new_encoder == encoder) {
  7928. WARN_ON(!connector->new_encoder->new_crtc);
  7929. goto next_encoder;
  7930. }
  7931. }
  7932. encoder->new_crtc = NULL;
  7933. next_encoder:
  7934. /* Only now check for crtc changes so we don't miss encoders
  7935. * that will be disabled. */
  7936. if (&encoder->new_crtc->base != encoder->base.crtc) {
  7937. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  7938. config->mode_changed = true;
  7939. }
  7940. }
  7941. /* Now we've also updated encoder->new_crtc for all encoders. */
  7942. return 0;
  7943. }
  7944. static int intel_crtc_set_config(struct drm_mode_set *set)
  7945. {
  7946. struct drm_device *dev;
  7947. struct drm_mode_set save_set;
  7948. struct intel_set_config *config;
  7949. int ret;
  7950. BUG_ON(!set);
  7951. BUG_ON(!set->crtc);
  7952. BUG_ON(!set->crtc->helper_private);
  7953. /* Enforce sane interface api - has been abused by the fb helper. */
  7954. BUG_ON(!set->mode && set->fb);
  7955. BUG_ON(set->fb && set->num_connectors == 0);
  7956. if (set->fb) {
  7957. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  7958. set->crtc->base.id, set->fb->base.id,
  7959. (int)set->num_connectors, set->x, set->y);
  7960. } else {
  7961. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  7962. }
  7963. dev = set->crtc->dev;
  7964. ret = -ENOMEM;
  7965. config = kzalloc(sizeof(*config), GFP_KERNEL);
  7966. if (!config)
  7967. goto out_config;
  7968. ret = intel_set_config_save_state(dev, config);
  7969. if (ret)
  7970. goto out_config;
  7971. save_set.crtc = set->crtc;
  7972. save_set.mode = &set->crtc->mode;
  7973. save_set.x = set->crtc->x;
  7974. save_set.y = set->crtc->y;
  7975. save_set.fb = set->crtc->fb;
  7976. /* Compute whether we need a full modeset, only an fb base update or no
  7977. * change at all. In the future we might also check whether only the
  7978. * mode changed, e.g. for LVDS where we only change the panel fitter in
  7979. * such cases. */
  7980. intel_set_config_compute_mode_changes(set, config);
  7981. ret = intel_modeset_stage_output_state(dev, set, config);
  7982. if (ret)
  7983. goto fail;
  7984. if (config->mode_changed) {
  7985. ret = intel_set_mode(set->crtc, set->mode,
  7986. set->x, set->y, set->fb);
  7987. } else if (config->fb_changed) {
  7988. intel_crtc_wait_for_pending_flips(set->crtc);
  7989. ret = intel_pipe_set_base(set->crtc,
  7990. set->x, set->y, set->fb);
  7991. }
  7992. if (ret) {
  7993. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  7994. set->crtc->base.id, ret);
  7995. fail:
  7996. intel_set_config_restore_state(dev, config);
  7997. /* Try to restore the config */
  7998. if (config->mode_changed &&
  7999. intel_set_mode(save_set.crtc, save_set.mode,
  8000. save_set.x, save_set.y, save_set.fb))
  8001. DRM_ERROR("failed to restore config after modeset failure\n");
  8002. }
  8003. out_config:
  8004. intel_set_config_free(config);
  8005. return ret;
  8006. }
  8007. static const struct drm_crtc_funcs intel_crtc_funcs = {
  8008. .cursor_set = intel_crtc_cursor_set,
  8009. .cursor_move = intel_crtc_cursor_move,
  8010. .gamma_set = intel_crtc_gamma_set,
  8011. .set_config = intel_crtc_set_config,
  8012. .destroy = intel_crtc_destroy,
  8013. .page_flip = intel_crtc_page_flip,
  8014. };
  8015. static void intel_cpu_pll_init(struct drm_device *dev)
  8016. {
  8017. if (HAS_DDI(dev))
  8018. intel_ddi_pll_init(dev);
  8019. }
  8020. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  8021. struct intel_shared_dpll *pll,
  8022. struct intel_dpll_hw_state *hw_state)
  8023. {
  8024. uint32_t val;
  8025. val = I915_READ(PCH_DPLL(pll->id));
  8026. hw_state->dpll = val;
  8027. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  8028. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  8029. return val & DPLL_VCO_ENABLE;
  8030. }
  8031. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  8032. struct intel_shared_dpll *pll)
  8033. {
  8034. I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
  8035. I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
  8036. }
  8037. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  8038. struct intel_shared_dpll *pll)
  8039. {
  8040. /* PCH refclock must be enabled first */
  8041. assert_pch_refclk_enabled(dev_priv);
  8042. I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
  8043. /* Wait for the clocks to stabilize. */
  8044. POSTING_READ(PCH_DPLL(pll->id));
  8045. udelay(150);
  8046. /* The pixel multiplier can only be updated once the
  8047. * DPLL is enabled and the clocks are stable.
  8048. *
  8049. * So write it again.
  8050. */
  8051. I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
  8052. POSTING_READ(PCH_DPLL(pll->id));
  8053. udelay(200);
  8054. }
  8055. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  8056. struct intel_shared_dpll *pll)
  8057. {
  8058. struct drm_device *dev = dev_priv->dev;
  8059. struct intel_crtc *crtc;
  8060. /* Make sure no transcoder isn't still depending on us. */
  8061. list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
  8062. if (intel_crtc_to_shared_dpll(crtc) == pll)
  8063. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  8064. }
  8065. I915_WRITE(PCH_DPLL(pll->id), 0);
  8066. POSTING_READ(PCH_DPLL(pll->id));
  8067. udelay(200);
  8068. }
  8069. static char *ibx_pch_dpll_names[] = {
  8070. "PCH DPLL A",
  8071. "PCH DPLL B",
  8072. };
  8073. static void ibx_pch_dpll_init(struct drm_device *dev)
  8074. {
  8075. struct drm_i915_private *dev_priv = dev->dev_private;
  8076. int i;
  8077. dev_priv->num_shared_dpll = 2;
  8078. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  8079. dev_priv->shared_dplls[i].id = i;
  8080. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  8081. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  8082. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  8083. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  8084. dev_priv->shared_dplls[i].get_hw_state =
  8085. ibx_pch_dpll_get_hw_state;
  8086. }
  8087. }
  8088. static void intel_shared_dpll_init(struct drm_device *dev)
  8089. {
  8090. struct drm_i915_private *dev_priv = dev->dev_private;
  8091. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  8092. ibx_pch_dpll_init(dev);
  8093. else
  8094. dev_priv->num_shared_dpll = 0;
  8095. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  8096. DRM_DEBUG_KMS("%i shared PLLs initialized\n",
  8097. dev_priv->num_shared_dpll);
  8098. }
  8099. static void intel_crtc_init(struct drm_device *dev, int pipe)
  8100. {
  8101. drm_i915_private_t *dev_priv = dev->dev_private;
  8102. struct intel_crtc *intel_crtc;
  8103. int i;
  8104. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  8105. if (intel_crtc == NULL)
  8106. return;
  8107. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  8108. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  8109. for (i = 0; i < 256; i++) {
  8110. intel_crtc->lut_r[i] = i;
  8111. intel_crtc->lut_g[i] = i;
  8112. intel_crtc->lut_b[i] = i;
  8113. }
  8114. /* Swap pipes & planes for FBC on pre-965 */
  8115. intel_crtc->pipe = pipe;
  8116. intel_crtc->plane = pipe;
  8117. if (IS_MOBILE(dev) && IS_GEN3(dev)) {
  8118. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  8119. intel_crtc->plane = !pipe;
  8120. }
  8121. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  8122. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  8123. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  8124. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  8125. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  8126. }
  8127. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  8128. struct drm_file *file)
  8129. {
  8130. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  8131. struct drm_mode_object *drmmode_obj;
  8132. struct intel_crtc *crtc;
  8133. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  8134. return -ENODEV;
  8135. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  8136. DRM_MODE_OBJECT_CRTC);
  8137. if (!drmmode_obj) {
  8138. DRM_ERROR("no such CRTC id\n");
  8139. return -EINVAL;
  8140. }
  8141. crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
  8142. pipe_from_crtc_id->pipe = crtc->pipe;
  8143. return 0;
  8144. }
  8145. static int intel_encoder_clones(struct intel_encoder *encoder)
  8146. {
  8147. struct drm_device *dev = encoder->base.dev;
  8148. struct intel_encoder *source_encoder;
  8149. int index_mask = 0;
  8150. int entry = 0;
  8151. list_for_each_entry(source_encoder,
  8152. &dev->mode_config.encoder_list, base.head) {
  8153. if (encoder == source_encoder)
  8154. index_mask |= (1 << entry);
  8155. /* Intel hw has only one MUX where enocoders could be cloned. */
  8156. if (encoder->cloneable && source_encoder->cloneable)
  8157. index_mask |= (1 << entry);
  8158. entry++;
  8159. }
  8160. return index_mask;
  8161. }
  8162. static bool has_edp_a(struct drm_device *dev)
  8163. {
  8164. struct drm_i915_private *dev_priv = dev->dev_private;
  8165. if (!IS_MOBILE(dev))
  8166. return false;
  8167. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  8168. return false;
  8169. if (IS_GEN5(dev) &&
  8170. (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
  8171. return false;
  8172. return true;
  8173. }
  8174. static void intel_setup_outputs(struct drm_device *dev)
  8175. {
  8176. struct drm_i915_private *dev_priv = dev->dev_private;
  8177. struct intel_encoder *encoder;
  8178. bool dpd_is_edp = false;
  8179. intel_lvds_init(dev);
  8180. if (!IS_ULT(dev))
  8181. intel_crt_init(dev);
  8182. if (HAS_DDI(dev)) {
  8183. int found;
  8184. /* Haswell uses DDI functions to detect digital outputs */
  8185. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  8186. /* DDI A only supports eDP */
  8187. if (found)
  8188. intel_ddi_init(dev, PORT_A);
  8189. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  8190. * register */
  8191. found = I915_READ(SFUSE_STRAP);
  8192. if (found & SFUSE_STRAP_DDIB_DETECTED)
  8193. intel_ddi_init(dev, PORT_B);
  8194. if (found & SFUSE_STRAP_DDIC_DETECTED)
  8195. intel_ddi_init(dev, PORT_C);
  8196. if (found & SFUSE_STRAP_DDID_DETECTED)
  8197. intel_ddi_init(dev, PORT_D);
  8198. } else if (HAS_PCH_SPLIT(dev)) {
  8199. int found;
  8200. dpd_is_edp = intel_dpd_is_edp(dev);
  8201. if (has_edp_a(dev))
  8202. intel_dp_init(dev, DP_A, PORT_A);
  8203. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  8204. /* PCH SDVOB multiplex with HDMIB */
  8205. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  8206. if (!found)
  8207. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  8208. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  8209. intel_dp_init(dev, PCH_DP_B, PORT_B);
  8210. }
  8211. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  8212. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  8213. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  8214. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  8215. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  8216. intel_dp_init(dev, PCH_DP_C, PORT_C);
  8217. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  8218. intel_dp_init(dev, PCH_DP_D, PORT_D);
  8219. } else if (IS_VALLEYVIEW(dev)) {
  8220. /* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
  8221. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
  8222. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  8223. PORT_C);
  8224. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
  8225. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C,
  8226. PORT_C);
  8227. }
  8228. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
  8229. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  8230. PORT_B);
  8231. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
  8232. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  8233. }
  8234. intel_dsi_init(dev);
  8235. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  8236. bool found = false;
  8237. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  8238. DRM_DEBUG_KMS("probing SDVOB\n");
  8239. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  8240. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  8241. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  8242. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  8243. }
  8244. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  8245. intel_dp_init(dev, DP_B, PORT_B);
  8246. }
  8247. /* Before G4X SDVOC doesn't have its own detect register */
  8248. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  8249. DRM_DEBUG_KMS("probing SDVOC\n");
  8250. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  8251. }
  8252. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  8253. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  8254. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  8255. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  8256. }
  8257. if (SUPPORTS_INTEGRATED_DP(dev))
  8258. intel_dp_init(dev, DP_C, PORT_C);
  8259. }
  8260. if (SUPPORTS_INTEGRATED_DP(dev) &&
  8261. (I915_READ(DP_D) & DP_DETECTED))
  8262. intel_dp_init(dev, DP_D, PORT_D);
  8263. } else if (IS_GEN2(dev))
  8264. intel_dvo_init(dev);
  8265. if (SUPPORTS_TV(dev))
  8266. intel_tv_init(dev);
  8267. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  8268. encoder->base.possible_crtcs = encoder->crtc_mask;
  8269. encoder->base.possible_clones =
  8270. intel_encoder_clones(encoder);
  8271. }
  8272. intel_init_pch_refclk(dev);
  8273. drm_helper_move_panel_connectors_to_head(dev);
  8274. }
  8275. void intel_framebuffer_fini(struct intel_framebuffer *fb)
  8276. {
  8277. drm_framebuffer_cleanup(&fb->base);
  8278. drm_gem_object_unreference_unlocked(&fb->obj->base);
  8279. }
  8280. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  8281. {
  8282. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  8283. intel_framebuffer_fini(intel_fb);
  8284. kfree(intel_fb);
  8285. }
  8286. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  8287. struct drm_file *file,
  8288. unsigned int *handle)
  8289. {
  8290. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  8291. struct drm_i915_gem_object *obj = intel_fb->obj;
  8292. return drm_gem_handle_create(file, &obj->base, handle);
  8293. }
  8294. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  8295. .destroy = intel_user_framebuffer_destroy,
  8296. .create_handle = intel_user_framebuffer_create_handle,
  8297. };
  8298. int intel_framebuffer_init(struct drm_device *dev,
  8299. struct intel_framebuffer *intel_fb,
  8300. struct drm_mode_fb_cmd2 *mode_cmd,
  8301. struct drm_i915_gem_object *obj)
  8302. {
  8303. int pitch_limit;
  8304. int ret;
  8305. if (obj->tiling_mode == I915_TILING_Y) {
  8306. DRM_DEBUG("hardware does not support tiling Y\n");
  8307. return -EINVAL;
  8308. }
  8309. if (mode_cmd->pitches[0] & 63) {
  8310. DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
  8311. mode_cmd->pitches[0]);
  8312. return -EINVAL;
  8313. }
  8314. if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
  8315. pitch_limit = 32*1024;
  8316. } else if (INTEL_INFO(dev)->gen >= 4) {
  8317. if (obj->tiling_mode)
  8318. pitch_limit = 16*1024;
  8319. else
  8320. pitch_limit = 32*1024;
  8321. } else if (INTEL_INFO(dev)->gen >= 3) {
  8322. if (obj->tiling_mode)
  8323. pitch_limit = 8*1024;
  8324. else
  8325. pitch_limit = 16*1024;
  8326. } else
  8327. /* XXX DSPC is limited to 4k tiled */
  8328. pitch_limit = 8*1024;
  8329. if (mode_cmd->pitches[0] > pitch_limit) {
  8330. DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
  8331. obj->tiling_mode ? "tiled" : "linear",
  8332. mode_cmd->pitches[0], pitch_limit);
  8333. return -EINVAL;
  8334. }
  8335. if (obj->tiling_mode != I915_TILING_NONE &&
  8336. mode_cmd->pitches[0] != obj->stride) {
  8337. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  8338. mode_cmd->pitches[0], obj->stride);
  8339. return -EINVAL;
  8340. }
  8341. /* Reject formats not supported by any plane early. */
  8342. switch (mode_cmd->pixel_format) {
  8343. case DRM_FORMAT_C8:
  8344. case DRM_FORMAT_RGB565:
  8345. case DRM_FORMAT_XRGB8888:
  8346. case DRM_FORMAT_ARGB8888:
  8347. break;
  8348. case DRM_FORMAT_XRGB1555:
  8349. case DRM_FORMAT_ARGB1555:
  8350. if (INTEL_INFO(dev)->gen > 3) {
  8351. DRM_DEBUG("unsupported pixel format: %s\n",
  8352. drm_get_format_name(mode_cmd->pixel_format));
  8353. return -EINVAL;
  8354. }
  8355. break;
  8356. case DRM_FORMAT_XBGR8888:
  8357. case DRM_FORMAT_ABGR8888:
  8358. case DRM_FORMAT_XRGB2101010:
  8359. case DRM_FORMAT_ARGB2101010:
  8360. case DRM_FORMAT_XBGR2101010:
  8361. case DRM_FORMAT_ABGR2101010:
  8362. if (INTEL_INFO(dev)->gen < 4) {
  8363. DRM_DEBUG("unsupported pixel format: %s\n",
  8364. drm_get_format_name(mode_cmd->pixel_format));
  8365. return -EINVAL;
  8366. }
  8367. break;
  8368. case DRM_FORMAT_YUYV:
  8369. case DRM_FORMAT_UYVY:
  8370. case DRM_FORMAT_YVYU:
  8371. case DRM_FORMAT_VYUY:
  8372. if (INTEL_INFO(dev)->gen < 5) {
  8373. DRM_DEBUG("unsupported pixel format: %s\n",
  8374. drm_get_format_name(mode_cmd->pixel_format));
  8375. return -EINVAL;
  8376. }
  8377. break;
  8378. default:
  8379. DRM_DEBUG("unsupported pixel format: %s\n",
  8380. drm_get_format_name(mode_cmd->pixel_format));
  8381. return -EINVAL;
  8382. }
  8383. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  8384. if (mode_cmd->offsets[0] != 0)
  8385. return -EINVAL;
  8386. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  8387. intel_fb->obj = obj;
  8388. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  8389. if (ret) {
  8390. DRM_ERROR("framebuffer init failed %d\n", ret);
  8391. return ret;
  8392. }
  8393. return 0;
  8394. }
  8395. static struct drm_framebuffer *
  8396. intel_user_framebuffer_create(struct drm_device *dev,
  8397. struct drm_file *filp,
  8398. struct drm_mode_fb_cmd2 *mode_cmd)
  8399. {
  8400. struct drm_i915_gem_object *obj;
  8401. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  8402. mode_cmd->handles[0]));
  8403. if (&obj->base == NULL)
  8404. return ERR_PTR(-ENOENT);
  8405. return intel_framebuffer_create(dev, mode_cmd, obj);
  8406. }
  8407. static const struct drm_mode_config_funcs intel_mode_funcs = {
  8408. .fb_create = intel_user_framebuffer_create,
  8409. .output_poll_changed = intel_fb_output_poll_changed,
  8410. };
  8411. /* Set up chip specific display functions */
  8412. static void intel_init_display(struct drm_device *dev)
  8413. {
  8414. struct drm_i915_private *dev_priv = dev->dev_private;
  8415. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  8416. dev_priv->display.find_dpll = g4x_find_best_dpll;
  8417. else if (IS_VALLEYVIEW(dev))
  8418. dev_priv->display.find_dpll = vlv_find_best_dpll;
  8419. else if (IS_PINEVIEW(dev))
  8420. dev_priv->display.find_dpll = pnv_find_best_dpll;
  8421. else
  8422. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  8423. if (HAS_DDI(dev)) {
  8424. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  8425. dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
  8426. dev_priv->display.crtc_enable = haswell_crtc_enable;
  8427. dev_priv->display.crtc_disable = haswell_crtc_disable;
  8428. dev_priv->display.off = haswell_crtc_off;
  8429. dev_priv->display.update_plane = ironlake_update_plane;
  8430. } else if (HAS_PCH_SPLIT(dev)) {
  8431. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  8432. dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
  8433. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  8434. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  8435. dev_priv->display.off = ironlake_crtc_off;
  8436. dev_priv->display.update_plane = ironlake_update_plane;
  8437. } else if (IS_VALLEYVIEW(dev)) {
  8438. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  8439. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  8440. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  8441. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  8442. dev_priv->display.off = i9xx_crtc_off;
  8443. dev_priv->display.update_plane = i9xx_update_plane;
  8444. } else {
  8445. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  8446. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  8447. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  8448. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  8449. dev_priv->display.off = i9xx_crtc_off;
  8450. dev_priv->display.update_plane = i9xx_update_plane;
  8451. }
  8452. /* Returns the core display clock speed */
  8453. if (IS_VALLEYVIEW(dev))
  8454. dev_priv->display.get_display_clock_speed =
  8455. valleyview_get_display_clock_speed;
  8456. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  8457. dev_priv->display.get_display_clock_speed =
  8458. i945_get_display_clock_speed;
  8459. else if (IS_I915G(dev))
  8460. dev_priv->display.get_display_clock_speed =
  8461. i915_get_display_clock_speed;
  8462. else if (IS_I945GM(dev) || IS_845G(dev))
  8463. dev_priv->display.get_display_clock_speed =
  8464. i9xx_misc_get_display_clock_speed;
  8465. else if (IS_PINEVIEW(dev))
  8466. dev_priv->display.get_display_clock_speed =
  8467. pnv_get_display_clock_speed;
  8468. else if (IS_I915GM(dev))
  8469. dev_priv->display.get_display_clock_speed =
  8470. i915gm_get_display_clock_speed;
  8471. else if (IS_I865G(dev))
  8472. dev_priv->display.get_display_clock_speed =
  8473. i865_get_display_clock_speed;
  8474. else if (IS_I85X(dev))
  8475. dev_priv->display.get_display_clock_speed =
  8476. i855_get_display_clock_speed;
  8477. else /* 852, 830 */
  8478. dev_priv->display.get_display_clock_speed =
  8479. i830_get_display_clock_speed;
  8480. if (HAS_PCH_SPLIT(dev)) {
  8481. if (IS_GEN5(dev)) {
  8482. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  8483. dev_priv->display.write_eld = ironlake_write_eld;
  8484. } else if (IS_GEN6(dev)) {
  8485. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  8486. dev_priv->display.write_eld = ironlake_write_eld;
  8487. } else if (IS_IVYBRIDGE(dev)) {
  8488. /* FIXME: detect B0+ stepping and use auto training */
  8489. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  8490. dev_priv->display.write_eld = ironlake_write_eld;
  8491. dev_priv->display.modeset_global_resources =
  8492. ivb_modeset_global_resources;
  8493. } else if (IS_HASWELL(dev)) {
  8494. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  8495. dev_priv->display.write_eld = haswell_write_eld;
  8496. dev_priv->display.modeset_global_resources =
  8497. haswell_modeset_global_resources;
  8498. }
  8499. } else if (IS_G4X(dev)) {
  8500. dev_priv->display.write_eld = g4x_write_eld;
  8501. }
  8502. /* Default just returns -ENODEV to indicate unsupported */
  8503. dev_priv->display.queue_flip = intel_default_queue_flip;
  8504. switch (INTEL_INFO(dev)->gen) {
  8505. case 2:
  8506. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  8507. break;
  8508. case 3:
  8509. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  8510. break;
  8511. case 4:
  8512. case 5:
  8513. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  8514. break;
  8515. case 6:
  8516. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  8517. break;
  8518. case 7:
  8519. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  8520. break;
  8521. }
  8522. }
  8523. /*
  8524. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  8525. * resume, or other times. This quirk makes sure that's the case for
  8526. * affected systems.
  8527. */
  8528. static void quirk_pipea_force(struct drm_device *dev)
  8529. {
  8530. struct drm_i915_private *dev_priv = dev->dev_private;
  8531. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  8532. DRM_INFO("applying pipe a force quirk\n");
  8533. }
  8534. /*
  8535. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  8536. */
  8537. static void quirk_ssc_force_disable(struct drm_device *dev)
  8538. {
  8539. struct drm_i915_private *dev_priv = dev->dev_private;
  8540. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  8541. DRM_INFO("applying lvds SSC disable quirk\n");
  8542. }
  8543. /*
  8544. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  8545. * brightness value
  8546. */
  8547. static void quirk_invert_brightness(struct drm_device *dev)
  8548. {
  8549. struct drm_i915_private *dev_priv = dev->dev_private;
  8550. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  8551. DRM_INFO("applying inverted panel brightness quirk\n");
  8552. }
  8553. /*
  8554. * Some machines (Dell XPS13) suffer broken backlight controls if
  8555. * BLM_PCH_PWM_ENABLE is set.
  8556. */
  8557. static void quirk_no_pcm_pwm_enable(struct drm_device *dev)
  8558. {
  8559. struct drm_i915_private *dev_priv = dev->dev_private;
  8560. dev_priv->quirks |= QUIRK_NO_PCH_PWM_ENABLE;
  8561. DRM_INFO("applying no-PCH_PWM_ENABLE quirk\n");
  8562. }
  8563. struct intel_quirk {
  8564. int device;
  8565. int subsystem_vendor;
  8566. int subsystem_device;
  8567. void (*hook)(struct drm_device *dev);
  8568. };
  8569. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  8570. struct intel_dmi_quirk {
  8571. void (*hook)(struct drm_device *dev);
  8572. const struct dmi_system_id (*dmi_id_list)[];
  8573. };
  8574. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  8575. {
  8576. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  8577. return 1;
  8578. }
  8579. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  8580. {
  8581. .dmi_id_list = &(const struct dmi_system_id[]) {
  8582. {
  8583. .callback = intel_dmi_reverse_brightness,
  8584. .ident = "NCR Corporation",
  8585. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  8586. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  8587. },
  8588. },
  8589. { } /* terminating entry */
  8590. },
  8591. .hook = quirk_invert_brightness,
  8592. },
  8593. };
  8594. static struct intel_quirk intel_quirks[] = {
  8595. /* HP Mini needs pipe A force quirk (LP: #322104) */
  8596. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  8597. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  8598. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  8599. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  8600. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  8601. /* 830/845 need to leave pipe A & dpll A up */
  8602. { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  8603. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  8604. /* Lenovo U160 cannot use SSC on LVDS */
  8605. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  8606. /* Sony Vaio Y cannot use SSC on LVDS */
  8607. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  8608. /*
  8609. * All GM45 Acer (and its brands eMachines and Packard Bell) laptops
  8610. * seem to use inverted backlight PWM.
  8611. */
  8612. { 0x2a42, 0x1025, PCI_ANY_ID, quirk_invert_brightness },
  8613. /* Dell XPS13 HD Sandy Bridge */
  8614. { 0x0116, 0x1028, 0x052e, quirk_no_pcm_pwm_enable },
  8615. /* Dell XPS13 HD and XPS13 FHD Ivy Bridge */
  8616. { 0x0166, 0x1028, 0x058b, quirk_no_pcm_pwm_enable },
  8617. };
  8618. static void intel_init_quirks(struct drm_device *dev)
  8619. {
  8620. struct pci_dev *d = dev->pdev;
  8621. int i;
  8622. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  8623. struct intel_quirk *q = &intel_quirks[i];
  8624. if (d->device == q->device &&
  8625. (d->subsystem_vendor == q->subsystem_vendor ||
  8626. q->subsystem_vendor == PCI_ANY_ID) &&
  8627. (d->subsystem_device == q->subsystem_device ||
  8628. q->subsystem_device == PCI_ANY_ID))
  8629. q->hook(dev);
  8630. }
  8631. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  8632. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  8633. intel_dmi_quirks[i].hook(dev);
  8634. }
  8635. }
  8636. /* Disable the VGA plane that we never use */
  8637. static void i915_disable_vga(struct drm_device *dev)
  8638. {
  8639. struct drm_i915_private *dev_priv = dev->dev_private;
  8640. u8 sr1;
  8641. u32 vga_reg = i915_vgacntrl_reg(dev);
  8642. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  8643. outb(SR01, VGA_SR_INDEX);
  8644. sr1 = inb(VGA_SR_DATA);
  8645. outb(sr1 | 1<<5, VGA_SR_DATA);
  8646. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  8647. udelay(300);
  8648. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  8649. POSTING_READ(vga_reg);
  8650. }
  8651. static void i915_enable_vga_mem(struct drm_device *dev)
  8652. {
  8653. /* Enable VGA memory on Intel HD */
  8654. if (HAS_PCH_SPLIT(dev)) {
  8655. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  8656. outb(inb(VGA_MSR_READ) | VGA_MSR_MEM_EN, VGA_MSR_WRITE);
  8657. vga_set_legacy_decoding(dev->pdev, VGA_RSRC_LEGACY_IO |
  8658. VGA_RSRC_LEGACY_MEM |
  8659. VGA_RSRC_NORMAL_IO |
  8660. VGA_RSRC_NORMAL_MEM);
  8661. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  8662. }
  8663. }
  8664. void i915_disable_vga_mem(struct drm_device *dev)
  8665. {
  8666. /* Disable VGA memory on Intel HD */
  8667. if (HAS_PCH_SPLIT(dev)) {
  8668. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  8669. outb(inb(VGA_MSR_READ) & ~VGA_MSR_MEM_EN, VGA_MSR_WRITE);
  8670. vga_set_legacy_decoding(dev->pdev, VGA_RSRC_LEGACY_IO |
  8671. VGA_RSRC_NORMAL_IO |
  8672. VGA_RSRC_NORMAL_MEM);
  8673. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  8674. }
  8675. }
  8676. void intel_modeset_init_hw(struct drm_device *dev)
  8677. {
  8678. struct drm_i915_private *dev_priv = dev->dev_private;
  8679. intel_prepare_ddi(dev);
  8680. intel_init_clock_gating(dev);
  8681. /* Enable the CRI clock source so we can get at the display */
  8682. if (IS_VALLEYVIEW(dev))
  8683. I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
  8684. DPLL_INTEGRATED_CRI_CLK_VLV);
  8685. intel_init_dpio(dev);
  8686. mutex_lock(&dev->struct_mutex);
  8687. intel_enable_gt_powersave(dev);
  8688. mutex_unlock(&dev->struct_mutex);
  8689. }
  8690. void intel_modeset_suspend_hw(struct drm_device *dev)
  8691. {
  8692. intel_suspend_hw(dev);
  8693. }
  8694. void intel_modeset_init(struct drm_device *dev)
  8695. {
  8696. struct drm_i915_private *dev_priv = dev->dev_private;
  8697. int i, j, ret;
  8698. drm_mode_config_init(dev);
  8699. dev->mode_config.min_width = 0;
  8700. dev->mode_config.min_height = 0;
  8701. dev->mode_config.preferred_depth = 24;
  8702. dev->mode_config.prefer_shadow = 1;
  8703. dev->mode_config.funcs = &intel_mode_funcs;
  8704. intel_init_quirks(dev);
  8705. intel_init_pm(dev);
  8706. if (INTEL_INFO(dev)->num_pipes == 0)
  8707. return;
  8708. intel_init_display(dev);
  8709. if (IS_GEN2(dev)) {
  8710. dev->mode_config.max_width = 2048;
  8711. dev->mode_config.max_height = 2048;
  8712. } else if (IS_GEN3(dev)) {
  8713. dev->mode_config.max_width = 4096;
  8714. dev->mode_config.max_height = 4096;
  8715. } else {
  8716. dev->mode_config.max_width = 8192;
  8717. dev->mode_config.max_height = 8192;
  8718. }
  8719. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  8720. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  8721. INTEL_INFO(dev)->num_pipes,
  8722. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  8723. for_each_pipe(i) {
  8724. intel_crtc_init(dev, i);
  8725. for (j = 0; j < dev_priv->num_plane; j++) {
  8726. ret = intel_plane_init(dev, i, j);
  8727. if (ret)
  8728. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  8729. pipe_name(i), sprite_name(i, j), ret);
  8730. }
  8731. }
  8732. intel_cpu_pll_init(dev);
  8733. intel_shared_dpll_init(dev);
  8734. /* Just disable it once at startup */
  8735. i915_disable_vga(dev);
  8736. intel_setup_outputs(dev);
  8737. /* Just in case the BIOS is doing something questionable. */
  8738. intel_disable_fbc(dev);
  8739. }
  8740. static void
  8741. intel_connector_break_all_links(struct intel_connector *connector)
  8742. {
  8743. connector->base.dpms = DRM_MODE_DPMS_OFF;
  8744. connector->base.encoder = NULL;
  8745. connector->encoder->connectors_active = false;
  8746. connector->encoder->base.crtc = NULL;
  8747. }
  8748. static void intel_enable_pipe_a(struct drm_device *dev)
  8749. {
  8750. struct intel_connector *connector;
  8751. struct drm_connector *crt = NULL;
  8752. struct intel_load_detect_pipe load_detect_temp;
  8753. /* We can't just switch on the pipe A, we need to set things up with a
  8754. * proper mode and output configuration. As a gross hack, enable pipe A
  8755. * by enabling the load detect pipe once. */
  8756. list_for_each_entry(connector,
  8757. &dev->mode_config.connector_list,
  8758. base.head) {
  8759. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  8760. crt = &connector->base;
  8761. break;
  8762. }
  8763. }
  8764. if (!crt)
  8765. return;
  8766. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
  8767. intel_release_load_detect_pipe(crt, &load_detect_temp);
  8768. }
  8769. static bool
  8770. intel_check_plane_mapping(struct intel_crtc *crtc)
  8771. {
  8772. struct drm_device *dev = crtc->base.dev;
  8773. struct drm_i915_private *dev_priv = dev->dev_private;
  8774. u32 reg, val;
  8775. if (INTEL_INFO(dev)->num_pipes == 1)
  8776. return true;
  8777. reg = DSPCNTR(!crtc->plane);
  8778. val = I915_READ(reg);
  8779. if ((val & DISPLAY_PLANE_ENABLE) &&
  8780. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  8781. return false;
  8782. return true;
  8783. }
  8784. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  8785. {
  8786. struct drm_device *dev = crtc->base.dev;
  8787. struct drm_i915_private *dev_priv = dev->dev_private;
  8788. u32 reg;
  8789. /* Clear any frame start delays used for debugging left by the BIOS */
  8790. reg = PIPECONF(crtc->config.cpu_transcoder);
  8791. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  8792. /* We need to sanitize the plane -> pipe mapping first because this will
  8793. * disable the crtc (and hence change the state) if it is wrong. Note
  8794. * that gen4+ has a fixed plane -> pipe mapping. */
  8795. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  8796. struct intel_connector *connector;
  8797. bool plane;
  8798. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  8799. crtc->base.base.id);
  8800. /* Pipe has the wrong plane attached and the plane is active.
  8801. * Temporarily change the plane mapping and disable everything
  8802. * ... */
  8803. plane = crtc->plane;
  8804. crtc->plane = !plane;
  8805. dev_priv->display.crtc_disable(&crtc->base);
  8806. crtc->plane = plane;
  8807. /* ... and break all links. */
  8808. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8809. base.head) {
  8810. if (connector->encoder->base.crtc != &crtc->base)
  8811. continue;
  8812. intel_connector_break_all_links(connector);
  8813. }
  8814. WARN_ON(crtc->active);
  8815. crtc->base.enabled = false;
  8816. }
  8817. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  8818. crtc->pipe == PIPE_A && !crtc->active) {
  8819. /* BIOS forgot to enable pipe A, this mostly happens after
  8820. * resume. Force-enable the pipe to fix this, the update_dpms
  8821. * call below we restore the pipe to the right state, but leave
  8822. * the required bits on. */
  8823. intel_enable_pipe_a(dev);
  8824. }
  8825. /* Adjust the state of the output pipe according to whether we
  8826. * have active connectors/encoders. */
  8827. intel_crtc_update_dpms(&crtc->base);
  8828. if (crtc->active != crtc->base.enabled) {
  8829. struct intel_encoder *encoder;
  8830. /* This can happen either due to bugs in the get_hw_state
  8831. * functions or because the pipe is force-enabled due to the
  8832. * pipe A quirk. */
  8833. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  8834. crtc->base.base.id,
  8835. crtc->base.enabled ? "enabled" : "disabled",
  8836. crtc->active ? "enabled" : "disabled");
  8837. crtc->base.enabled = crtc->active;
  8838. /* Because we only establish the connector -> encoder ->
  8839. * crtc links if something is active, this means the
  8840. * crtc is now deactivated. Break the links. connector
  8841. * -> encoder links are only establish when things are
  8842. * actually up, hence no need to break them. */
  8843. WARN_ON(crtc->active);
  8844. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  8845. WARN_ON(encoder->connectors_active);
  8846. encoder->base.crtc = NULL;
  8847. }
  8848. }
  8849. }
  8850. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  8851. {
  8852. struct intel_connector *connector;
  8853. struct drm_device *dev = encoder->base.dev;
  8854. /* We need to check both for a crtc link (meaning that the
  8855. * encoder is active and trying to read from a pipe) and the
  8856. * pipe itself being active. */
  8857. bool has_active_crtc = encoder->base.crtc &&
  8858. to_intel_crtc(encoder->base.crtc)->active;
  8859. if (encoder->connectors_active && !has_active_crtc) {
  8860. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  8861. encoder->base.base.id,
  8862. drm_get_encoder_name(&encoder->base));
  8863. /* Connector is active, but has no active pipe. This is
  8864. * fallout from our resume register restoring. Disable
  8865. * the encoder manually again. */
  8866. if (encoder->base.crtc) {
  8867. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  8868. encoder->base.base.id,
  8869. drm_get_encoder_name(&encoder->base));
  8870. encoder->disable(encoder);
  8871. }
  8872. /* Inconsistent output/port/pipe state happens presumably due to
  8873. * a bug in one of the get_hw_state functions. Or someplace else
  8874. * in our code, like the register restore mess on resume. Clamp
  8875. * things to off as a safer default. */
  8876. list_for_each_entry(connector,
  8877. &dev->mode_config.connector_list,
  8878. base.head) {
  8879. if (connector->encoder != encoder)
  8880. continue;
  8881. intel_connector_break_all_links(connector);
  8882. }
  8883. }
  8884. /* Enabled encoders without active connectors will be fixed in
  8885. * the crtc fixup. */
  8886. }
  8887. void i915_redisable_vga(struct drm_device *dev)
  8888. {
  8889. struct drm_i915_private *dev_priv = dev->dev_private;
  8890. u32 vga_reg = i915_vgacntrl_reg(dev);
  8891. /* This function can be called both from intel_modeset_setup_hw_state or
  8892. * at a very early point in our resume sequence, where the power well
  8893. * structures are not yet restored. Since this function is at a very
  8894. * paranoid "someone might have enabled VGA while we were not looking"
  8895. * level, just check if the power well is enabled instead of trying to
  8896. * follow the "don't touch the power well if we don't need it" policy
  8897. * the rest of the driver uses. */
  8898. if (HAS_POWER_WELL(dev) &&
  8899. (I915_READ(HSW_PWR_WELL_DRIVER) & HSW_PWR_WELL_STATE_ENABLED) == 0)
  8900. return;
  8901. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  8902. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  8903. i915_disable_vga(dev);
  8904. i915_disable_vga_mem(dev);
  8905. }
  8906. }
  8907. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  8908. {
  8909. struct drm_i915_private *dev_priv = dev->dev_private;
  8910. enum pipe pipe;
  8911. struct intel_crtc *crtc;
  8912. struct intel_encoder *encoder;
  8913. struct intel_connector *connector;
  8914. int i;
  8915. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  8916. base.head) {
  8917. memset(&crtc->config, 0, sizeof(crtc->config));
  8918. crtc->active = dev_priv->display.get_pipe_config(crtc,
  8919. &crtc->config);
  8920. crtc->base.enabled = crtc->active;
  8921. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  8922. crtc->base.base.id,
  8923. crtc->active ? "enabled" : "disabled");
  8924. }
  8925. /* FIXME: Smash this into the new shared dpll infrastructure. */
  8926. if (HAS_DDI(dev))
  8927. intel_ddi_setup_hw_pll_state(dev);
  8928. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  8929. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  8930. pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
  8931. pll->active = 0;
  8932. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  8933. base.head) {
  8934. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  8935. pll->active++;
  8936. }
  8937. pll->refcount = pll->active;
  8938. DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
  8939. pll->name, pll->refcount, pll->on);
  8940. }
  8941. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8942. base.head) {
  8943. pipe = 0;
  8944. if (encoder->get_hw_state(encoder, &pipe)) {
  8945. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  8946. encoder->base.crtc = &crtc->base;
  8947. if (encoder->get_config)
  8948. encoder->get_config(encoder, &crtc->config);
  8949. } else {
  8950. encoder->base.crtc = NULL;
  8951. }
  8952. encoder->connectors_active = false;
  8953. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
  8954. encoder->base.base.id,
  8955. drm_get_encoder_name(&encoder->base),
  8956. encoder->base.crtc ? "enabled" : "disabled",
  8957. pipe);
  8958. }
  8959. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8960. base.head) {
  8961. if (connector->get_hw_state(connector)) {
  8962. connector->base.dpms = DRM_MODE_DPMS_ON;
  8963. connector->encoder->connectors_active = true;
  8964. connector->base.encoder = &connector->encoder->base;
  8965. } else {
  8966. connector->base.dpms = DRM_MODE_DPMS_OFF;
  8967. connector->base.encoder = NULL;
  8968. }
  8969. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  8970. connector->base.base.id,
  8971. drm_get_connector_name(&connector->base),
  8972. connector->base.encoder ? "enabled" : "disabled");
  8973. }
  8974. }
  8975. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  8976. * and i915 state tracking structures. */
  8977. void intel_modeset_setup_hw_state(struct drm_device *dev,
  8978. bool force_restore)
  8979. {
  8980. struct drm_i915_private *dev_priv = dev->dev_private;
  8981. enum pipe pipe;
  8982. struct intel_crtc *crtc;
  8983. struct intel_encoder *encoder;
  8984. int i;
  8985. intel_modeset_readout_hw_state(dev);
  8986. /*
  8987. * Now that we have the config, copy it to each CRTC struct
  8988. * Note that this could go away if we move to using crtc_config
  8989. * checking everywhere.
  8990. */
  8991. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  8992. base.head) {
  8993. if (crtc->active && i915_fastboot) {
  8994. intel_crtc_mode_from_pipe_config(crtc, &crtc->config);
  8995. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  8996. crtc->base.base.id);
  8997. drm_mode_debug_printmodeline(&crtc->base.mode);
  8998. }
  8999. }
  9000. /* HW state is read out, now we need to sanitize this mess. */
  9001. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  9002. base.head) {
  9003. intel_sanitize_encoder(encoder);
  9004. }
  9005. for_each_pipe(pipe) {
  9006. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  9007. intel_sanitize_crtc(crtc);
  9008. intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
  9009. }
  9010. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9011. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  9012. if (!pll->on || pll->active)
  9013. continue;
  9014. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  9015. pll->disable(dev_priv, pll);
  9016. pll->on = false;
  9017. }
  9018. if (force_restore) {
  9019. i915_redisable_vga(dev);
  9020. /*
  9021. * We need to use raw interfaces for restoring state to avoid
  9022. * checking (bogus) intermediate states.
  9023. */
  9024. for_each_pipe(pipe) {
  9025. struct drm_crtc *crtc =
  9026. dev_priv->pipe_to_crtc_mapping[pipe];
  9027. __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
  9028. crtc->fb);
  9029. }
  9030. } else {
  9031. intel_modeset_update_staged_output_state(dev);
  9032. }
  9033. intel_modeset_check_state(dev);
  9034. drm_mode_config_reset(dev);
  9035. }
  9036. void intel_modeset_gem_init(struct drm_device *dev)
  9037. {
  9038. intel_modeset_init_hw(dev);
  9039. intel_setup_overlay(dev);
  9040. intel_modeset_setup_hw_state(dev, false);
  9041. }
  9042. void intel_modeset_cleanup(struct drm_device *dev)
  9043. {
  9044. struct drm_i915_private *dev_priv = dev->dev_private;
  9045. struct drm_crtc *crtc;
  9046. struct drm_connector *connector;
  9047. /*
  9048. * Interrupts and polling as the first thing to avoid creating havoc.
  9049. * Too much stuff here (turning of rps, connectors, ...) would
  9050. * experience fancy races otherwise.
  9051. */
  9052. drm_irq_uninstall(dev);
  9053. cancel_work_sync(&dev_priv->hotplug_work);
  9054. /*
  9055. * Due to the hpd irq storm handling the hotplug work can re-arm the
  9056. * poll handlers. Hence disable polling after hpd handling is shut down.
  9057. */
  9058. drm_kms_helper_poll_fini(dev);
  9059. mutex_lock(&dev->struct_mutex);
  9060. intel_unregister_dsm_handler();
  9061. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  9062. /* Skip inactive CRTCs */
  9063. if (!crtc->fb)
  9064. continue;
  9065. intel_increase_pllclock(crtc);
  9066. }
  9067. intel_disable_fbc(dev);
  9068. i915_enable_vga_mem(dev);
  9069. intel_disable_gt_powersave(dev);
  9070. ironlake_teardown_rc6(dev);
  9071. mutex_unlock(&dev->struct_mutex);
  9072. /* flush any delayed tasks or pending work */
  9073. flush_scheduled_work();
  9074. /* destroy backlight, if any, before the connectors */
  9075. intel_panel_destroy_backlight(dev);
  9076. /* destroy the sysfs files before encoders/connectors */
  9077. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  9078. drm_sysfs_connector_remove(connector);
  9079. drm_mode_config_cleanup(dev);
  9080. intel_cleanup_overlay(dev);
  9081. }
  9082. /*
  9083. * Return which encoder is currently attached for connector.
  9084. */
  9085. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  9086. {
  9087. return &intel_attached_encoder(connector)->base;
  9088. }
  9089. void intel_connector_attach_encoder(struct intel_connector *connector,
  9090. struct intel_encoder *encoder)
  9091. {
  9092. connector->encoder = encoder;
  9093. drm_mode_connector_attach_encoder(&connector->base,
  9094. &encoder->base);
  9095. }
  9096. /*
  9097. * set vga decode state - true == enable VGA decode
  9098. */
  9099. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  9100. {
  9101. struct drm_i915_private *dev_priv = dev->dev_private;
  9102. u16 gmch_ctrl;
  9103. pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
  9104. if (state)
  9105. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  9106. else
  9107. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  9108. pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
  9109. return 0;
  9110. }
  9111. struct intel_display_error_state {
  9112. u32 power_well_driver;
  9113. int num_transcoders;
  9114. struct intel_cursor_error_state {
  9115. u32 control;
  9116. u32 position;
  9117. u32 base;
  9118. u32 size;
  9119. } cursor[I915_MAX_PIPES];
  9120. struct intel_pipe_error_state {
  9121. u32 source;
  9122. } pipe[I915_MAX_PIPES];
  9123. struct intel_plane_error_state {
  9124. u32 control;
  9125. u32 stride;
  9126. u32 size;
  9127. u32 pos;
  9128. u32 addr;
  9129. u32 surface;
  9130. u32 tile_offset;
  9131. } plane[I915_MAX_PIPES];
  9132. struct intel_transcoder_error_state {
  9133. enum transcoder cpu_transcoder;
  9134. u32 conf;
  9135. u32 htotal;
  9136. u32 hblank;
  9137. u32 hsync;
  9138. u32 vtotal;
  9139. u32 vblank;
  9140. u32 vsync;
  9141. } transcoder[4];
  9142. };
  9143. struct intel_display_error_state *
  9144. intel_display_capture_error_state(struct drm_device *dev)
  9145. {
  9146. drm_i915_private_t *dev_priv = dev->dev_private;
  9147. struct intel_display_error_state *error;
  9148. int transcoders[] = {
  9149. TRANSCODER_A,
  9150. TRANSCODER_B,
  9151. TRANSCODER_C,
  9152. TRANSCODER_EDP,
  9153. };
  9154. int i;
  9155. if (INTEL_INFO(dev)->num_pipes == 0)
  9156. return NULL;
  9157. error = kmalloc(sizeof(*error), GFP_ATOMIC);
  9158. if (error == NULL)
  9159. return NULL;
  9160. if (HAS_POWER_WELL(dev))
  9161. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  9162. for_each_pipe(i) {
  9163. if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
  9164. error->cursor[i].control = I915_READ(CURCNTR(i));
  9165. error->cursor[i].position = I915_READ(CURPOS(i));
  9166. error->cursor[i].base = I915_READ(CURBASE(i));
  9167. } else {
  9168. error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
  9169. error->cursor[i].position = I915_READ(CURPOS_IVB(i));
  9170. error->cursor[i].base = I915_READ(CURBASE_IVB(i));
  9171. }
  9172. error->plane[i].control = I915_READ(DSPCNTR(i));
  9173. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  9174. if (INTEL_INFO(dev)->gen <= 3) {
  9175. error->plane[i].size = I915_READ(DSPSIZE(i));
  9176. error->plane[i].pos = I915_READ(DSPPOS(i));
  9177. }
  9178. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  9179. error->plane[i].addr = I915_READ(DSPADDR(i));
  9180. if (INTEL_INFO(dev)->gen >= 4) {
  9181. error->plane[i].surface = I915_READ(DSPSURF(i));
  9182. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  9183. }
  9184. error->pipe[i].source = I915_READ(PIPESRC(i));
  9185. }
  9186. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  9187. if (HAS_DDI(dev_priv->dev))
  9188. error->num_transcoders++; /* Account for eDP. */
  9189. for (i = 0; i < error->num_transcoders; i++) {
  9190. enum transcoder cpu_transcoder = transcoders[i];
  9191. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  9192. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  9193. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  9194. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  9195. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  9196. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  9197. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  9198. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  9199. }
  9200. /* In the code above we read the registers without checking if the power
  9201. * well was on, so here we have to clear the FPGA_DBG_RM_NOCLAIM bit to
  9202. * prevent the next I915_WRITE from detecting it and printing an error
  9203. * message. */
  9204. intel_uncore_clear_errors(dev);
  9205. return error;
  9206. }
  9207. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  9208. void
  9209. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  9210. struct drm_device *dev,
  9211. struct intel_display_error_state *error)
  9212. {
  9213. int i;
  9214. if (!error)
  9215. return;
  9216. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  9217. if (HAS_POWER_WELL(dev))
  9218. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  9219. error->power_well_driver);
  9220. for_each_pipe(i) {
  9221. err_printf(m, "Pipe [%d]:\n", i);
  9222. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  9223. err_printf(m, "Plane [%d]:\n", i);
  9224. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  9225. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  9226. if (INTEL_INFO(dev)->gen <= 3) {
  9227. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  9228. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  9229. }
  9230. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  9231. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  9232. if (INTEL_INFO(dev)->gen >= 4) {
  9233. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  9234. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  9235. }
  9236. err_printf(m, "Cursor [%d]:\n", i);
  9237. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  9238. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  9239. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  9240. }
  9241. for (i = 0; i < error->num_transcoders; i++) {
  9242. err_printf(m, " CPU transcoder: %c\n",
  9243. transcoder_name(error->transcoder[i].cpu_transcoder));
  9244. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  9245. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  9246. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  9247. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  9248. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  9249. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  9250. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  9251. }
  9252. }