intel_display.c 318 KB

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