intel_display.c 303 KB

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