intel_display.c 304 KB

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