intel_display.c 189 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191
  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 "drmP.h"
  35. #include "intel_drv.h"
  36. #include "i915_drm.h"
  37. #include "i915_drv.h"
  38. #include "i915_trace.h"
  39. #include "drm_dp_helper.h"
  40. #include "drm_crtc_helper.h"
  41. #include <linux/dma_remapping.h>
  42. #define HAS_eDP (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  43. bool intel_pipe_has_type(struct drm_crtc *crtc, int type);
  44. static void intel_increase_pllclock(struct drm_crtc *crtc);
  45. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  46. typedef struct {
  47. /* given values */
  48. int n;
  49. int m1, m2;
  50. int p1, p2;
  51. /* derived values */
  52. int dot;
  53. int vco;
  54. int m;
  55. int p;
  56. } intel_clock_t;
  57. typedef struct {
  58. int min, max;
  59. } intel_range_t;
  60. typedef struct {
  61. int dot_limit;
  62. int p2_slow, p2_fast;
  63. } intel_p2_t;
  64. #define INTEL_P2_NUM 2
  65. typedef struct intel_limit intel_limit_t;
  66. struct intel_limit {
  67. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  68. intel_p2_t p2;
  69. bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
  70. int, int, intel_clock_t *, intel_clock_t *);
  71. };
  72. /* FDI */
  73. #define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
  74. static bool
  75. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  76. int target, int refclk, intel_clock_t *match_clock,
  77. intel_clock_t *best_clock);
  78. static bool
  79. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  80. int target, int refclk, intel_clock_t *match_clock,
  81. intel_clock_t *best_clock);
  82. static bool
  83. intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
  84. int target, int refclk, intel_clock_t *match_clock,
  85. intel_clock_t *best_clock);
  86. static bool
  87. intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
  88. int target, int refclk, intel_clock_t *match_clock,
  89. intel_clock_t *best_clock);
  90. static inline u32 /* units of 100MHz */
  91. intel_fdi_link_freq(struct drm_device *dev)
  92. {
  93. if (IS_GEN5(dev)) {
  94. struct drm_i915_private *dev_priv = dev->dev_private;
  95. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  96. } else
  97. return 27;
  98. }
  99. static const intel_limit_t intel_limits_i8xx_dvo = {
  100. .dot = { .min = 25000, .max = 350000 },
  101. .vco = { .min = 930000, .max = 1400000 },
  102. .n = { .min = 3, .max = 16 },
  103. .m = { .min = 96, .max = 140 },
  104. .m1 = { .min = 18, .max = 26 },
  105. .m2 = { .min = 6, .max = 16 },
  106. .p = { .min = 4, .max = 128 },
  107. .p1 = { .min = 2, .max = 33 },
  108. .p2 = { .dot_limit = 165000,
  109. .p2_slow = 4, .p2_fast = 2 },
  110. .find_pll = intel_find_best_PLL,
  111. };
  112. static const intel_limit_t intel_limits_i8xx_lvds = {
  113. .dot = { .min = 25000, .max = 350000 },
  114. .vco = { .min = 930000, .max = 1400000 },
  115. .n = { .min = 3, .max = 16 },
  116. .m = { .min = 96, .max = 140 },
  117. .m1 = { .min = 18, .max = 26 },
  118. .m2 = { .min = 6, .max = 16 },
  119. .p = { .min = 4, .max = 128 },
  120. .p1 = { .min = 1, .max = 6 },
  121. .p2 = { .dot_limit = 165000,
  122. .p2_slow = 14, .p2_fast = 7 },
  123. .find_pll = intel_find_best_PLL,
  124. };
  125. static const intel_limit_t intel_limits_i9xx_sdvo = {
  126. .dot = { .min = 20000, .max = 400000 },
  127. .vco = { .min = 1400000, .max = 2800000 },
  128. .n = { .min = 1, .max = 6 },
  129. .m = { .min = 70, .max = 120 },
  130. .m1 = { .min = 10, .max = 22 },
  131. .m2 = { .min = 5, .max = 9 },
  132. .p = { .min = 5, .max = 80 },
  133. .p1 = { .min = 1, .max = 8 },
  134. .p2 = { .dot_limit = 200000,
  135. .p2_slow = 10, .p2_fast = 5 },
  136. .find_pll = intel_find_best_PLL,
  137. };
  138. static const intel_limit_t intel_limits_i9xx_lvds = {
  139. .dot = { .min = 20000, .max = 400000 },
  140. .vco = { .min = 1400000, .max = 2800000 },
  141. .n = { .min = 1, .max = 6 },
  142. .m = { .min = 70, .max = 120 },
  143. .m1 = { .min = 10, .max = 22 },
  144. .m2 = { .min = 5, .max = 9 },
  145. .p = { .min = 7, .max = 98 },
  146. .p1 = { .min = 1, .max = 8 },
  147. .p2 = { .dot_limit = 112000,
  148. .p2_slow = 14, .p2_fast = 7 },
  149. .find_pll = intel_find_best_PLL,
  150. };
  151. static const intel_limit_t intel_limits_g4x_sdvo = {
  152. .dot = { .min = 25000, .max = 270000 },
  153. .vco = { .min = 1750000, .max = 3500000},
  154. .n = { .min = 1, .max = 4 },
  155. .m = { .min = 104, .max = 138 },
  156. .m1 = { .min = 17, .max = 23 },
  157. .m2 = { .min = 5, .max = 11 },
  158. .p = { .min = 10, .max = 30 },
  159. .p1 = { .min = 1, .max = 3},
  160. .p2 = { .dot_limit = 270000,
  161. .p2_slow = 10,
  162. .p2_fast = 10
  163. },
  164. .find_pll = intel_g4x_find_best_PLL,
  165. };
  166. static const intel_limit_t intel_limits_g4x_hdmi = {
  167. .dot = { .min = 22000, .max = 400000 },
  168. .vco = { .min = 1750000, .max = 3500000},
  169. .n = { .min = 1, .max = 4 },
  170. .m = { .min = 104, .max = 138 },
  171. .m1 = { .min = 16, .max = 23 },
  172. .m2 = { .min = 5, .max = 11 },
  173. .p = { .min = 5, .max = 80 },
  174. .p1 = { .min = 1, .max = 8},
  175. .p2 = { .dot_limit = 165000,
  176. .p2_slow = 10, .p2_fast = 5 },
  177. .find_pll = intel_g4x_find_best_PLL,
  178. };
  179. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  180. .dot = { .min = 20000, .max = 115000 },
  181. .vco = { .min = 1750000, .max = 3500000 },
  182. .n = { .min = 1, .max = 3 },
  183. .m = { .min = 104, .max = 138 },
  184. .m1 = { .min = 17, .max = 23 },
  185. .m2 = { .min = 5, .max = 11 },
  186. .p = { .min = 28, .max = 112 },
  187. .p1 = { .min = 2, .max = 8 },
  188. .p2 = { .dot_limit = 0,
  189. .p2_slow = 14, .p2_fast = 14
  190. },
  191. .find_pll = intel_g4x_find_best_PLL,
  192. };
  193. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  194. .dot = { .min = 80000, .max = 224000 },
  195. .vco = { .min = 1750000, .max = 3500000 },
  196. .n = { .min = 1, .max = 3 },
  197. .m = { .min = 104, .max = 138 },
  198. .m1 = { .min = 17, .max = 23 },
  199. .m2 = { .min = 5, .max = 11 },
  200. .p = { .min = 14, .max = 42 },
  201. .p1 = { .min = 2, .max = 6 },
  202. .p2 = { .dot_limit = 0,
  203. .p2_slow = 7, .p2_fast = 7
  204. },
  205. .find_pll = intel_g4x_find_best_PLL,
  206. };
  207. static const intel_limit_t intel_limits_g4x_display_port = {
  208. .dot = { .min = 161670, .max = 227000 },
  209. .vco = { .min = 1750000, .max = 3500000},
  210. .n = { .min = 1, .max = 2 },
  211. .m = { .min = 97, .max = 108 },
  212. .m1 = { .min = 0x10, .max = 0x12 },
  213. .m2 = { .min = 0x05, .max = 0x06 },
  214. .p = { .min = 10, .max = 20 },
  215. .p1 = { .min = 1, .max = 2},
  216. .p2 = { .dot_limit = 0,
  217. .p2_slow = 10, .p2_fast = 10 },
  218. .find_pll = intel_find_pll_g4x_dp,
  219. };
  220. static const intel_limit_t intel_limits_pineview_sdvo = {
  221. .dot = { .min = 20000, .max = 400000},
  222. .vco = { .min = 1700000, .max = 3500000 },
  223. /* Pineview's Ncounter is a ring counter */
  224. .n = { .min = 3, .max = 6 },
  225. .m = { .min = 2, .max = 256 },
  226. /* Pineview only has one combined m divider, which we treat as m2. */
  227. .m1 = { .min = 0, .max = 0 },
  228. .m2 = { .min = 0, .max = 254 },
  229. .p = { .min = 5, .max = 80 },
  230. .p1 = { .min = 1, .max = 8 },
  231. .p2 = { .dot_limit = 200000,
  232. .p2_slow = 10, .p2_fast = 5 },
  233. .find_pll = intel_find_best_PLL,
  234. };
  235. static const intel_limit_t intel_limits_pineview_lvds = {
  236. .dot = { .min = 20000, .max = 400000 },
  237. .vco = { .min = 1700000, .max = 3500000 },
  238. .n = { .min = 3, .max = 6 },
  239. .m = { .min = 2, .max = 256 },
  240. .m1 = { .min = 0, .max = 0 },
  241. .m2 = { .min = 0, .max = 254 },
  242. .p = { .min = 7, .max = 112 },
  243. .p1 = { .min = 1, .max = 8 },
  244. .p2 = { .dot_limit = 112000,
  245. .p2_slow = 14, .p2_fast = 14 },
  246. .find_pll = intel_find_best_PLL,
  247. };
  248. /* Ironlake / Sandybridge
  249. *
  250. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  251. * the range value for them is (actual_value - 2).
  252. */
  253. static const intel_limit_t intel_limits_ironlake_dac = {
  254. .dot = { .min = 25000, .max = 350000 },
  255. .vco = { .min = 1760000, .max = 3510000 },
  256. .n = { .min = 1, .max = 5 },
  257. .m = { .min = 79, .max = 127 },
  258. .m1 = { .min = 12, .max = 22 },
  259. .m2 = { .min = 5, .max = 9 },
  260. .p = { .min = 5, .max = 80 },
  261. .p1 = { .min = 1, .max = 8 },
  262. .p2 = { .dot_limit = 225000,
  263. .p2_slow = 10, .p2_fast = 5 },
  264. .find_pll = intel_g4x_find_best_PLL,
  265. };
  266. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  267. .dot = { .min = 25000, .max = 350000 },
  268. .vco = { .min = 1760000, .max = 3510000 },
  269. .n = { .min = 1, .max = 3 },
  270. .m = { .min = 79, .max = 118 },
  271. .m1 = { .min = 12, .max = 22 },
  272. .m2 = { .min = 5, .max = 9 },
  273. .p = { .min = 28, .max = 112 },
  274. .p1 = { .min = 2, .max = 8 },
  275. .p2 = { .dot_limit = 225000,
  276. .p2_slow = 14, .p2_fast = 14 },
  277. .find_pll = intel_g4x_find_best_PLL,
  278. };
  279. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  280. .dot = { .min = 25000, .max = 350000 },
  281. .vco = { .min = 1760000, .max = 3510000 },
  282. .n = { .min = 1, .max = 3 },
  283. .m = { .min = 79, .max = 127 },
  284. .m1 = { .min = 12, .max = 22 },
  285. .m2 = { .min = 5, .max = 9 },
  286. .p = { .min = 14, .max = 56 },
  287. .p1 = { .min = 2, .max = 8 },
  288. .p2 = { .dot_limit = 225000,
  289. .p2_slow = 7, .p2_fast = 7 },
  290. .find_pll = intel_g4x_find_best_PLL,
  291. };
  292. /* LVDS 100mhz refclk limits. */
  293. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  294. .dot = { .min = 25000, .max = 350000 },
  295. .vco = { .min = 1760000, .max = 3510000 },
  296. .n = { .min = 1, .max = 2 },
  297. .m = { .min = 79, .max = 126 },
  298. .m1 = { .min = 12, .max = 22 },
  299. .m2 = { .min = 5, .max = 9 },
  300. .p = { .min = 28, .max = 112 },
  301. .p1 = { .min = 2, .max = 8 },
  302. .p2 = { .dot_limit = 225000,
  303. .p2_slow = 14, .p2_fast = 14 },
  304. .find_pll = intel_g4x_find_best_PLL,
  305. };
  306. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  307. .dot = { .min = 25000, .max = 350000 },
  308. .vco = { .min = 1760000, .max = 3510000 },
  309. .n = { .min = 1, .max = 3 },
  310. .m = { .min = 79, .max = 126 },
  311. .m1 = { .min = 12, .max = 22 },
  312. .m2 = { .min = 5, .max = 9 },
  313. .p = { .min = 14, .max = 42 },
  314. .p1 = { .min = 2, .max = 6 },
  315. .p2 = { .dot_limit = 225000,
  316. .p2_slow = 7, .p2_fast = 7 },
  317. .find_pll = intel_g4x_find_best_PLL,
  318. };
  319. static const intel_limit_t intel_limits_ironlake_display_port = {
  320. .dot = { .min = 25000, .max = 350000 },
  321. .vco = { .min = 1760000, .max = 3510000},
  322. .n = { .min = 1, .max = 2 },
  323. .m = { .min = 81, .max = 90 },
  324. .m1 = { .min = 12, .max = 22 },
  325. .m2 = { .min = 5, .max = 9 },
  326. .p = { .min = 10, .max = 20 },
  327. .p1 = { .min = 1, .max = 2},
  328. .p2 = { .dot_limit = 0,
  329. .p2_slow = 10, .p2_fast = 10 },
  330. .find_pll = intel_find_pll_ironlake_dp,
  331. };
  332. u32 intel_dpio_read(struct drm_i915_private *dev_priv, int reg)
  333. {
  334. unsigned long flags;
  335. u32 val = 0;
  336. spin_lock_irqsave(&dev_priv->dpio_lock, flags);
  337. if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
  338. DRM_ERROR("DPIO idle wait timed out\n");
  339. goto out_unlock;
  340. }
  341. I915_WRITE(DPIO_REG, reg);
  342. I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_READ | DPIO_PORTID |
  343. DPIO_BYTE);
  344. if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
  345. DRM_ERROR("DPIO read wait timed out\n");
  346. goto out_unlock;
  347. }
  348. val = I915_READ(DPIO_DATA);
  349. out_unlock:
  350. spin_unlock_irqrestore(&dev_priv->dpio_lock, flags);
  351. return val;
  352. }
  353. static void vlv_init_dpio(struct drm_device *dev)
  354. {
  355. struct drm_i915_private *dev_priv = dev->dev_private;
  356. /* Reset the DPIO config */
  357. I915_WRITE(DPIO_CTL, 0);
  358. POSTING_READ(DPIO_CTL);
  359. I915_WRITE(DPIO_CTL, 1);
  360. POSTING_READ(DPIO_CTL);
  361. }
  362. static int intel_dual_link_lvds_callback(const struct dmi_system_id *id)
  363. {
  364. DRM_INFO("Forcing lvds to dual link mode on %s\n", id->ident);
  365. return 1;
  366. }
  367. static const struct dmi_system_id intel_dual_link_lvds[] = {
  368. {
  369. .callback = intel_dual_link_lvds_callback,
  370. .ident = "Apple MacBook Pro (Core i5/i7 Series)",
  371. .matches = {
  372. DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
  373. DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro8,2"),
  374. },
  375. },
  376. { } /* terminating entry */
  377. };
  378. static bool is_dual_link_lvds(struct drm_i915_private *dev_priv,
  379. unsigned int reg)
  380. {
  381. unsigned int val;
  382. /* use the module option value if specified */
  383. if (i915_lvds_channel_mode > 0)
  384. return i915_lvds_channel_mode == 2;
  385. if (dmi_check_system(intel_dual_link_lvds))
  386. return true;
  387. if (dev_priv->lvds_val)
  388. val = dev_priv->lvds_val;
  389. else {
  390. /* BIOS should set the proper LVDS register value at boot, but
  391. * in reality, it doesn't set the value when the lid is closed;
  392. * we need to check "the value to be set" in VBT when LVDS
  393. * register is uninitialized.
  394. */
  395. val = I915_READ(reg);
  396. if (!(val & ~LVDS_DETECTED))
  397. val = dev_priv->bios_lvds_val;
  398. dev_priv->lvds_val = val;
  399. }
  400. return (val & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP;
  401. }
  402. static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
  403. int refclk)
  404. {
  405. struct drm_device *dev = crtc->dev;
  406. struct drm_i915_private *dev_priv = dev->dev_private;
  407. const intel_limit_t *limit;
  408. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  409. if (is_dual_link_lvds(dev_priv, PCH_LVDS)) {
  410. /* LVDS dual channel */
  411. if (refclk == 100000)
  412. limit = &intel_limits_ironlake_dual_lvds_100m;
  413. else
  414. limit = &intel_limits_ironlake_dual_lvds;
  415. } else {
  416. if (refclk == 100000)
  417. limit = &intel_limits_ironlake_single_lvds_100m;
  418. else
  419. limit = &intel_limits_ironlake_single_lvds;
  420. }
  421. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  422. HAS_eDP)
  423. limit = &intel_limits_ironlake_display_port;
  424. else
  425. limit = &intel_limits_ironlake_dac;
  426. return limit;
  427. }
  428. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  429. {
  430. struct drm_device *dev = crtc->dev;
  431. struct drm_i915_private *dev_priv = dev->dev_private;
  432. const intel_limit_t *limit;
  433. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  434. if (is_dual_link_lvds(dev_priv, LVDS))
  435. /* LVDS with dual channel */
  436. limit = &intel_limits_g4x_dual_channel_lvds;
  437. else
  438. /* LVDS with dual channel */
  439. limit = &intel_limits_g4x_single_channel_lvds;
  440. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  441. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  442. limit = &intel_limits_g4x_hdmi;
  443. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  444. limit = &intel_limits_g4x_sdvo;
  445. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  446. limit = &intel_limits_g4x_display_port;
  447. } else /* The option is for other outputs */
  448. limit = &intel_limits_i9xx_sdvo;
  449. return limit;
  450. }
  451. static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
  452. {
  453. struct drm_device *dev = crtc->dev;
  454. const intel_limit_t *limit;
  455. if (HAS_PCH_SPLIT(dev))
  456. limit = intel_ironlake_limit(crtc, refclk);
  457. else if (IS_G4X(dev)) {
  458. limit = intel_g4x_limit(crtc);
  459. } else if (IS_PINEVIEW(dev)) {
  460. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  461. limit = &intel_limits_pineview_lvds;
  462. else
  463. limit = &intel_limits_pineview_sdvo;
  464. } else if (!IS_GEN2(dev)) {
  465. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  466. limit = &intel_limits_i9xx_lvds;
  467. else
  468. limit = &intel_limits_i9xx_sdvo;
  469. } else {
  470. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  471. limit = &intel_limits_i8xx_lvds;
  472. else
  473. limit = &intel_limits_i8xx_dvo;
  474. }
  475. return limit;
  476. }
  477. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  478. static void pineview_clock(int refclk, intel_clock_t *clock)
  479. {
  480. clock->m = clock->m2 + 2;
  481. clock->p = clock->p1 * clock->p2;
  482. clock->vco = refclk * clock->m / clock->n;
  483. clock->dot = clock->vco / clock->p;
  484. }
  485. static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
  486. {
  487. if (IS_PINEVIEW(dev)) {
  488. pineview_clock(refclk, clock);
  489. return;
  490. }
  491. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  492. clock->p = clock->p1 * clock->p2;
  493. clock->vco = refclk * clock->m / (clock->n + 2);
  494. clock->dot = clock->vco / clock->p;
  495. }
  496. /**
  497. * Returns whether any output on the specified pipe is of the specified type
  498. */
  499. bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
  500. {
  501. struct drm_device *dev = crtc->dev;
  502. struct drm_mode_config *mode_config = &dev->mode_config;
  503. struct intel_encoder *encoder;
  504. list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
  505. if (encoder->base.crtc == crtc && encoder->type == type)
  506. return true;
  507. return false;
  508. }
  509. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  510. /**
  511. * Returns whether the given set of divisors are valid for a given refclk with
  512. * the given connectors.
  513. */
  514. static bool intel_PLL_is_valid(struct drm_device *dev,
  515. const intel_limit_t *limit,
  516. const intel_clock_t *clock)
  517. {
  518. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  519. INTELPllInvalid("p1 out of range\n");
  520. if (clock->p < limit->p.min || limit->p.max < clock->p)
  521. INTELPllInvalid("p out of range\n");
  522. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  523. INTELPllInvalid("m2 out of range\n");
  524. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  525. INTELPllInvalid("m1 out of range\n");
  526. if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
  527. INTELPllInvalid("m1 <= m2\n");
  528. if (clock->m < limit->m.min || limit->m.max < clock->m)
  529. INTELPllInvalid("m out of range\n");
  530. if (clock->n < limit->n.min || limit->n.max < clock->n)
  531. INTELPllInvalid("n out of range\n");
  532. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  533. INTELPllInvalid("vco out of range\n");
  534. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  535. * connector, etc., rather than just a single range.
  536. */
  537. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  538. INTELPllInvalid("dot out of range\n");
  539. return true;
  540. }
  541. static bool
  542. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  543. int target, int refclk, intel_clock_t *match_clock,
  544. intel_clock_t *best_clock)
  545. {
  546. struct drm_device *dev = crtc->dev;
  547. struct drm_i915_private *dev_priv = dev->dev_private;
  548. intel_clock_t clock;
  549. int err = target;
  550. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  551. (I915_READ(LVDS)) != 0) {
  552. /*
  553. * For LVDS, if the panel is on, just rely on its current
  554. * settings for dual-channel. We haven't figured out how to
  555. * reliably set up different single/dual channel state, if we
  556. * even can.
  557. */
  558. if (is_dual_link_lvds(dev_priv, LVDS))
  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. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  570. clock.m1++) {
  571. for (clock.m2 = limit->m2.min;
  572. clock.m2 <= limit->m2.max; clock.m2++) {
  573. /* m1 is always 0 in Pineview */
  574. if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
  575. break;
  576. for (clock.n = limit->n.min;
  577. clock.n <= limit->n.max; clock.n++) {
  578. for (clock.p1 = limit->p1.min;
  579. clock.p1 <= limit->p1.max; clock.p1++) {
  580. int this_err;
  581. intel_clock(dev, refclk, &clock);
  582. if (!intel_PLL_is_valid(dev, limit,
  583. &clock))
  584. continue;
  585. if (match_clock &&
  586. clock.p != match_clock->p)
  587. continue;
  588. this_err = abs(clock.dot - target);
  589. if (this_err < err) {
  590. *best_clock = clock;
  591. err = this_err;
  592. }
  593. }
  594. }
  595. }
  596. }
  597. return (err != target);
  598. }
  599. static bool
  600. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  601. int target, int refclk, intel_clock_t *match_clock,
  602. intel_clock_t *best_clock)
  603. {
  604. struct drm_device *dev = crtc->dev;
  605. struct drm_i915_private *dev_priv = dev->dev_private;
  606. intel_clock_t clock;
  607. int max_n;
  608. bool found;
  609. /* approximately equals target * 0.00585 */
  610. int err_most = (target >> 8) + (target >> 9);
  611. found = false;
  612. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  613. int lvds_reg;
  614. if (HAS_PCH_SPLIT(dev))
  615. lvds_reg = PCH_LVDS;
  616. else
  617. lvds_reg = LVDS;
  618. if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==
  619. LVDS_CLKB_POWER_UP)
  620. clock.p2 = limit->p2.p2_fast;
  621. else
  622. clock.p2 = limit->p2.p2_slow;
  623. } else {
  624. if (target < limit->p2.dot_limit)
  625. clock.p2 = limit->p2.p2_slow;
  626. else
  627. clock.p2 = limit->p2.p2_fast;
  628. }
  629. memset(best_clock, 0, sizeof(*best_clock));
  630. max_n = limit->n.max;
  631. /* based on hardware requirement, prefer smaller n to precision */
  632. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  633. /* based on hardware requirement, prefere larger m1,m2 */
  634. for (clock.m1 = limit->m1.max;
  635. clock.m1 >= limit->m1.min; clock.m1--) {
  636. for (clock.m2 = limit->m2.max;
  637. clock.m2 >= limit->m2.min; clock.m2--) {
  638. for (clock.p1 = limit->p1.max;
  639. clock.p1 >= limit->p1.min; clock.p1--) {
  640. int this_err;
  641. intel_clock(dev, refclk, &clock);
  642. if (!intel_PLL_is_valid(dev, limit,
  643. &clock))
  644. continue;
  645. if (match_clock &&
  646. clock.p != match_clock->p)
  647. continue;
  648. this_err = abs(clock.dot - target);
  649. if (this_err < err_most) {
  650. *best_clock = clock;
  651. err_most = this_err;
  652. max_n = clock.n;
  653. found = true;
  654. }
  655. }
  656. }
  657. }
  658. }
  659. return found;
  660. }
  661. static bool
  662. intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  663. int target, int refclk, intel_clock_t *match_clock,
  664. intel_clock_t *best_clock)
  665. {
  666. struct drm_device *dev = crtc->dev;
  667. intel_clock_t clock;
  668. if (target < 200000) {
  669. clock.n = 1;
  670. clock.p1 = 2;
  671. clock.p2 = 10;
  672. clock.m1 = 12;
  673. clock.m2 = 9;
  674. } else {
  675. clock.n = 2;
  676. clock.p1 = 1;
  677. clock.p2 = 10;
  678. clock.m1 = 14;
  679. clock.m2 = 8;
  680. }
  681. intel_clock(dev, refclk, &clock);
  682. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  683. return true;
  684. }
  685. /* DisplayPort has only two frequencies, 162MHz and 270MHz */
  686. static bool
  687. intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  688. int target, int refclk, intel_clock_t *match_clock,
  689. intel_clock_t *best_clock)
  690. {
  691. intel_clock_t clock;
  692. if (target < 200000) {
  693. clock.p1 = 2;
  694. clock.p2 = 10;
  695. clock.n = 2;
  696. clock.m1 = 23;
  697. clock.m2 = 8;
  698. } else {
  699. clock.p1 = 1;
  700. clock.p2 = 10;
  701. clock.n = 1;
  702. clock.m1 = 14;
  703. clock.m2 = 2;
  704. }
  705. clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
  706. clock.p = (clock.p1 * clock.p2);
  707. clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
  708. clock.vco = 0;
  709. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  710. return true;
  711. }
  712. static void ironlake_wait_for_vblank(struct drm_device *dev, int pipe)
  713. {
  714. struct drm_i915_private *dev_priv = dev->dev_private;
  715. u32 frame, frame_reg = PIPEFRAME(pipe);
  716. frame = I915_READ(frame_reg);
  717. if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
  718. DRM_DEBUG_KMS("vblank wait timed out\n");
  719. }
  720. /**
  721. * intel_wait_for_vblank - wait for vblank on a given pipe
  722. * @dev: drm device
  723. * @pipe: pipe to wait for
  724. *
  725. * Wait for vblank to occur on a given pipe. Needed for various bits of
  726. * mode setting code.
  727. */
  728. void intel_wait_for_vblank(struct drm_device *dev, int pipe)
  729. {
  730. struct drm_i915_private *dev_priv = dev->dev_private;
  731. int pipestat_reg = PIPESTAT(pipe);
  732. if (INTEL_INFO(dev)->gen >= 5) {
  733. ironlake_wait_for_vblank(dev, pipe);
  734. return;
  735. }
  736. /* Clear existing vblank status. Note this will clear any other
  737. * sticky status fields as well.
  738. *
  739. * This races with i915_driver_irq_handler() with the result
  740. * that either function could miss a vblank event. Here it is not
  741. * fatal, as we will either wait upon the next vblank interrupt or
  742. * timeout. Generally speaking intel_wait_for_vblank() is only
  743. * called during modeset at which time the GPU should be idle and
  744. * should *not* be performing page flips and thus not waiting on
  745. * vblanks...
  746. * Currently, the result of us stealing a vblank from the irq
  747. * handler is that a single frame will be skipped during swapbuffers.
  748. */
  749. I915_WRITE(pipestat_reg,
  750. I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
  751. /* Wait for vblank interrupt bit to set */
  752. if (wait_for(I915_READ(pipestat_reg) &
  753. PIPE_VBLANK_INTERRUPT_STATUS,
  754. 50))
  755. DRM_DEBUG_KMS("vblank wait timed out\n");
  756. }
  757. /*
  758. * intel_wait_for_pipe_off - wait for pipe to turn off
  759. * @dev: drm device
  760. * @pipe: pipe to wait for
  761. *
  762. * After disabling a pipe, we can't wait for vblank in the usual way,
  763. * spinning on the vblank interrupt status bit, since we won't actually
  764. * see an interrupt when the pipe is disabled.
  765. *
  766. * On Gen4 and above:
  767. * wait for the pipe register state bit to turn off
  768. *
  769. * Otherwise:
  770. * wait for the display line value to settle (it usually
  771. * ends up stopping at the start of the next frame).
  772. *
  773. */
  774. void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
  775. {
  776. struct drm_i915_private *dev_priv = dev->dev_private;
  777. if (INTEL_INFO(dev)->gen >= 4) {
  778. int reg = PIPECONF(pipe);
  779. /* Wait for the Pipe State to go off */
  780. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  781. 100))
  782. DRM_DEBUG_KMS("pipe_off wait timed out\n");
  783. } else {
  784. u32 last_line, line_mask;
  785. int reg = PIPEDSL(pipe);
  786. unsigned long timeout = jiffies + msecs_to_jiffies(100);
  787. if (IS_GEN2(dev))
  788. line_mask = DSL_LINEMASK_GEN2;
  789. else
  790. line_mask = DSL_LINEMASK_GEN3;
  791. /* Wait for the display line to settle */
  792. do {
  793. last_line = I915_READ(reg) & line_mask;
  794. mdelay(5);
  795. } while (((I915_READ(reg) & line_mask) != last_line) &&
  796. time_after(timeout, jiffies));
  797. if (time_after(jiffies, timeout))
  798. DRM_DEBUG_KMS("pipe_off wait timed out\n");
  799. }
  800. }
  801. static const char *state_string(bool enabled)
  802. {
  803. return enabled ? "on" : "off";
  804. }
  805. /* Only for pre-ILK configs */
  806. static void assert_pll(struct drm_i915_private *dev_priv,
  807. enum pipe pipe, bool state)
  808. {
  809. int reg;
  810. u32 val;
  811. bool cur_state;
  812. reg = DPLL(pipe);
  813. val = I915_READ(reg);
  814. cur_state = !!(val & DPLL_VCO_ENABLE);
  815. WARN(cur_state != state,
  816. "PLL state assertion failure (expected %s, current %s)\n",
  817. state_string(state), state_string(cur_state));
  818. }
  819. #define assert_pll_enabled(d, p) assert_pll(d, p, true)
  820. #define assert_pll_disabled(d, p) assert_pll(d, p, false)
  821. /* For ILK+ */
  822. static void assert_pch_pll(struct drm_i915_private *dev_priv,
  823. struct intel_crtc *intel_crtc, bool state)
  824. {
  825. int reg;
  826. u32 val;
  827. bool cur_state;
  828. if (HAS_PCH_LPT(dev_priv->dev)) {
  829. DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
  830. return;
  831. }
  832. if (!intel_crtc->pch_pll) {
  833. WARN(1, "asserting PCH PLL enabled with no PLL\n");
  834. return;
  835. }
  836. if (HAS_PCH_CPT(dev_priv->dev)) {
  837. u32 pch_dpll;
  838. pch_dpll = I915_READ(PCH_DPLL_SEL);
  839. /* Make sure the selected PLL is enabled to the transcoder */
  840. WARN(!((pch_dpll >> (4 * intel_crtc->pipe)) & 8),
  841. "transcoder %d PLL not enabled\n", intel_crtc->pipe);
  842. }
  843. reg = intel_crtc->pch_pll->pll_reg;
  844. val = I915_READ(reg);
  845. cur_state = !!(val & DPLL_VCO_ENABLE);
  846. WARN(cur_state != state,
  847. "PCH PLL state assertion failure (expected %s, current %s)\n",
  848. state_string(state), state_string(cur_state));
  849. }
  850. #define assert_pch_pll_enabled(d, p) assert_pch_pll(d, p, true)
  851. #define assert_pch_pll_disabled(d, p) assert_pch_pll(d, p, false)
  852. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  853. enum pipe pipe, bool state)
  854. {
  855. int reg;
  856. u32 val;
  857. bool cur_state;
  858. if (IS_HASWELL(dev_priv->dev)) {
  859. /* On Haswell, DDI is used instead of FDI_TX_CTL */
  860. reg = DDI_FUNC_CTL(pipe);
  861. val = I915_READ(reg);
  862. cur_state = !!(val & PIPE_DDI_FUNC_ENABLE);
  863. } else {
  864. reg = FDI_TX_CTL(pipe);
  865. val = I915_READ(reg);
  866. cur_state = !!(val & FDI_TX_ENABLE);
  867. }
  868. WARN(cur_state != state,
  869. "FDI TX state assertion failure (expected %s, current %s)\n",
  870. state_string(state), state_string(cur_state));
  871. }
  872. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  873. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  874. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  875. enum pipe pipe, bool state)
  876. {
  877. int reg;
  878. u32 val;
  879. bool cur_state;
  880. if (IS_HASWELL(dev_priv->dev) && pipe > 0) {
  881. DRM_ERROR("Attempting to enable FDI_RX on Haswell pipe > 0\n");
  882. return;
  883. } else {
  884. reg = FDI_RX_CTL(pipe);
  885. val = I915_READ(reg);
  886. cur_state = !!(val & FDI_RX_ENABLE);
  887. }
  888. WARN(cur_state != state,
  889. "FDI RX state assertion failure (expected %s, current %s)\n",
  890. state_string(state), state_string(cur_state));
  891. }
  892. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  893. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  894. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  895. enum pipe pipe)
  896. {
  897. int reg;
  898. u32 val;
  899. /* ILK FDI PLL is always enabled */
  900. if (dev_priv->info->gen == 5)
  901. return;
  902. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  903. if (IS_HASWELL(dev_priv->dev))
  904. return;
  905. reg = FDI_TX_CTL(pipe);
  906. val = I915_READ(reg);
  907. WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  908. }
  909. static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
  910. enum pipe pipe)
  911. {
  912. int reg;
  913. u32 val;
  914. if (IS_HASWELL(dev_priv->dev) && pipe > 0) {
  915. DRM_ERROR("Attempting to enable FDI on Haswell with pipe > 0\n");
  916. return;
  917. }
  918. reg = FDI_RX_CTL(pipe);
  919. val = I915_READ(reg);
  920. WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
  921. }
  922. static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  923. enum pipe pipe)
  924. {
  925. int pp_reg, lvds_reg;
  926. u32 val;
  927. enum pipe panel_pipe = PIPE_A;
  928. bool locked = true;
  929. if (HAS_PCH_SPLIT(dev_priv->dev)) {
  930. pp_reg = PCH_PP_CONTROL;
  931. lvds_reg = PCH_LVDS;
  932. } else {
  933. pp_reg = PP_CONTROL;
  934. lvds_reg = LVDS;
  935. }
  936. val = I915_READ(pp_reg);
  937. if (!(val & PANEL_POWER_ON) ||
  938. ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
  939. locked = false;
  940. if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
  941. panel_pipe = PIPE_B;
  942. WARN(panel_pipe == pipe && locked,
  943. "panel assertion failure, pipe %c regs locked\n",
  944. pipe_name(pipe));
  945. }
  946. void assert_pipe(struct drm_i915_private *dev_priv,
  947. enum pipe pipe, bool state)
  948. {
  949. int reg;
  950. u32 val;
  951. bool cur_state;
  952. /* if we need the pipe A quirk it must be always on */
  953. if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
  954. state = true;
  955. reg = PIPECONF(pipe);
  956. val = I915_READ(reg);
  957. cur_state = !!(val & PIPECONF_ENABLE);
  958. WARN(cur_state != state,
  959. "pipe %c assertion failure (expected %s, current %s)\n",
  960. pipe_name(pipe), state_string(state), state_string(cur_state));
  961. }
  962. static void assert_plane(struct drm_i915_private *dev_priv,
  963. enum plane plane, bool state)
  964. {
  965. int reg;
  966. u32 val;
  967. bool cur_state;
  968. reg = DSPCNTR(plane);
  969. val = I915_READ(reg);
  970. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  971. WARN(cur_state != state,
  972. "plane %c assertion failure (expected %s, current %s)\n",
  973. plane_name(plane), state_string(state), state_string(cur_state));
  974. }
  975. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  976. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  977. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  978. enum pipe pipe)
  979. {
  980. int reg, i;
  981. u32 val;
  982. int cur_pipe;
  983. /* Planes are fixed to pipes on ILK+ */
  984. if (HAS_PCH_SPLIT(dev_priv->dev)) {
  985. reg = DSPCNTR(pipe);
  986. val = I915_READ(reg);
  987. WARN((val & DISPLAY_PLANE_ENABLE),
  988. "plane %c assertion failure, should be disabled but not\n",
  989. plane_name(pipe));
  990. return;
  991. }
  992. /* Need to check both planes against the pipe */
  993. for (i = 0; i < 2; i++) {
  994. reg = DSPCNTR(i);
  995. val = I915_READ(reg);
  996. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  997. DISPPLANE_SEL_PIPE_SHIFT;
  998. WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  999. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1000. plane_name(i), pipe_name(pipe));
  1001. }
  1002. }
  1003. static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1004. {
  1005. u32 val;
  1006. bool enabled;
  1007. if (HAS_PCH_LPT(dev_priv->dev)) {
  1008. DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
  1009. return;
  1010. }
  1011. val = I915_READ(PCH_DREF_CONTROL);
  1012. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1013. DREF_SUPERSPREAD_SOURCE_MASK));
  1014. WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1015. }
  1016. static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
  1017. enum pipe pipe)
  1018. {
  1019. int reg;
  1020. u32 val;
  1021. bool enabled;
  1022. reg = TRANSCONF(pipe);
  1023. val = I915_READ(reg);
  1024. enabled = !!(val & TRANS_ENABLE);
  1025. WARN(enabled,
  1026. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1027. pipe_name(pipe));
  1028. }
  1029. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1030. enum pipe pipe, u32 port_sel, u32 val)
  1031. {
  1032. if ((val & DP_PORT_EN) == 0)
  1033. return false;
  1034. if (HAS_PCH_CPT(dev_priv->dev)) {
  1035. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1036. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1037. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1038. return false;
  1039. } else {
  1040. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1041. return false;
  1042. }
  1043. return true;
  1044. }
  1045. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1046. enum pipe pipe, u32 val)
  1047. {
  1048. if ((val & PORT_ENABLE) == 0)
  1049. return false;
  1050. if (HAS_PCH_CPT(dev_priv->dev)) {
  1051. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1052. return false;
  1053. } else {
  1054. if ((val & TRANSCODER_MASK) != TRANSCODER(pipe))
  1055. return false;
  1056. }
  1057. return true;
  1058. }
  1059. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1060. enum pipe pipe, u32 val)
  1061. {
  1062. if ((val & LVDS_PORT_EN) == 0)
  1063. return false;
  1064. if (HAS_PCH_CPT(dev_priv->dev)) {
  1065. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1066. return false;
  1067. } else {
  1068. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1069. return false;
  1070. }
  1071. return true;
  1072. }
  1073. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1074. enum pipe pipe, u32 val)
  1075. {
  1076. if ((val & ADPA_DAC_ENABLE) == 0)
  1077. return false;
  1078. if (HAS_PCH_CPT(dev_priv->dev)) {
  1079. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1080. return false;
  1081. } else {
  1082. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1083. return false;
  1084. }
  1085. return true;
  1086. }
  1087. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1088. enum pipe pipe, int reg, u32 port_sel)
  1089. {
  1090. u32 val = I915_READ(reg);
  1091. WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1092. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1093. reg, pipe_name(pipe));
  1094. }
  1095. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1096. enum pipe pipe, int reg)
  1097. {
  1098. u32 val = I915_READ(reg);
  1099. WARN(hdmi_pipe_enabled(dev_priv, val, pipe),
  1100. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1101. reg, pipe_name(pipe));
  1102. }
  1103. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1104. enum pipe pipe)
  1105. {
  1106. int reg;
  1107. u32 val;
  1108. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1109. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1110. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1111. reg = PCH_ADPA;
  1112. val = I915_READ(reg);
  1113. WARN(adpa_pipe_enabled(dev_priv, val, pipe),
  1114. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1115. pipe_name(pipe));
  1116. reg = PCH_LVDS;
  1117. val = I915_READ(reg);
  1118. WARN(lvds_pipe_enabled(dev_priv, val, pipe),
  1119. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1120. pipe_name(pipe));
  1121. assert_pch_hdmi_disabled(dev_priv, pipe, HDMIB);
  1122. assert_pch_hdmi_disabled(dev_priv, pipe, HDMIC);
  1123. assert_pch_hdmi_disabled(dev_priv, pipe, HDMID);
  1124. }
  1125. /**
  1126. * intel_enable_pll - enable a PLL
  1127. * @dev_priv: i915 private structure
  1128. * @pipe: pipe PLL to enable
  1129. *
  1130. * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
  1131. * make sure the PLL reg is writable first though, since the panel write
  1132. * protect mechanism may be enabled.
  1133. *
  1134. * Note! This is for pre-ILK only.
  1135. */
  1136. static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1137. {
  1138. int reg;
  1139. u32 val;
  1140. /* No really, not for ILK+ */
  1141. BUG_ON(dev_priv->info->gen >= 5);
  1142. /* PLL is protected by panel, make sure we can write it */
  1143. if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
  1144. assert_panel_unlocked(dev_priv, pipe);
  1145. reg = DPLL(pipe);
  1146. val = I915_READ(reg);
  1147. val |= DPLL_VCO_ENABLE;
  1148. /* We do this three times for luck */
  1149. I915_WRITE(reg, val);
  1150. POSTING_READ(reg);
  1151. udelay(150); /* wait for warmup */
  1152. I915_WRITE(reg, val);
  1153. POSTING_READ(reg);
  1154. udelay(150); /* wait for warmup */
  1155. I915_WRITE(reg, val);
  1156. POSTING_READ(reg);
  1157. udelay(150); /* wait for warmup */
  1158. }
  1159. /**
  1160. * intel_disable_pll - disable a PLL
  1161. * @dev_priv: i915 private structure
  1162. * @pipe: pipe PLL to disable
  1163. *
  1164. * Disable the PLL for @pipe, making sure the pipe is off first.
  1165. *
  1166. * Note! This is for pre-ILK only.
  1167. */
  1168. static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1169. {
  1170. int reg;
  1171. u32 val;
  1172. /* Don't disable pipe A or pipe A PLLs if needed */
  1173. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1174. return;
  1175. /* Make sure the pipe isn't still relying on us */
  1176. assert_pipe_disabled(dev_priv, pipe);
  1177. reg = DPLL(pipe);
  1178. val = I915_READ(reg);
  1179. val &= ~DPLL_VCO_ENABLE;
  1180. I915_WRITE(reg, val);
  1181. POSTING_READ(reg);
  1182. }
  1183. /* SBI access */
  1184. static void
  1185. intel_sbi_write(struct drm_i915_private *dev_priv, u16 reg, u32 value)
  1186. {
  1187. unsigned long flags;
  1188. spin_lock_irqsave(&dev_priv->dpio_lock, flags);
  1189. if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_READY) == 0,
  1190. 100)) {
  1191. DRM_ERROR("timeout waiting for SBI to become ready\n");
  1192. goto out_unlock;
  1193. }
  1194. I915_WRITE(SBI_ADDR,
  1195. (reg << 16));
  1196. I915_WRITE(SBI_DATA,
  1197. value);
  1198. I915_WRITE(SBI_CTL_STAT,
  1199. SBI_BUSY |
  1200. SBI_CTL_OP_CRWR);
  1201. if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_READY | SBI_RESPONSE_SUCCESS)) == 0,
  1202. 100)) {
  1203. DRM_ERROR("timeout waiting for SBI to complete write transaction\n");
  1204. goto out_unlock;
  1205. }
  1206. out_unlock:
  1207. spin_unlock_irqrestore(&dev_priv->dpio_lock, flags);
  1208. }
  1209. static u32
  1210. intel_sbi_read(struct drm_i915_private *dev_priv, u16 reg)
  1211. {
  1212. unsigned long flags;
  1213. u32 value;
  1214. spin_lock_irqsave(&dev_priv->dpio_lock, flags);
  1215. if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_READY) == 0,
  1216. 100)) {
  1217. DRM_ERROR("timeout waiting for SBI to become ready\n");
  1218. goto out_unlock;
  1219. }
  1220. I915_WRITE(SBI_ADDR,
  1221. (reg << 16));
  1222. I915_WRITE(SBI_CTL_STAT,
  1223. SBI_BUSY |
  1224. SBI_CTL_OP_CRRD);
  1225. if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_READY | SBI_RESPONSE_SUCCESS)) == 0,
  1226. 100)) {
  1227. DRM_ERROR("timeout waiting for SBI to complete read transaction\n");
  1228. goto out_unlock;
  1229. }
  1230. value = I915_READ(SBI_DATA);
  1231. out_unlock:
  1232. spin_unlock_irqrestore(&dev_priv->dpio_lock, flags);
  1233. return value;
  1234. }
  1235. /**
  1236. * intel_enable_pch_pll - enable PCH PLL
  1237. * @dev_priv: i915 private structure
  1238. * @pipe: pipe PLL to enable
  1239. *
  1240. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1241. * drives the transcoder clock.
  1242. */
  1243. static void intel_enable_pch_pll(struct intel_crtc *intel_crtc)
  1244. {
  1245. struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
  1246. struct intel_pch_pll *pll;
  1247. int reg;
  1248. u32 val;
  1249. /* PCH PLLs only available on ILK, SNB and IVB */
  1250. BUG_ON(dev_priv->info->gen < 5);
  1251. pll = intel_crtc->pch_pll;
  1252. if (pll == NULL)
  1253. return;
  1254. if (WARN_ON(pll->refcount == 0))
  1255. return;
  1256. DRM_DEBUG_KMS("enable PCH PLL %x (active %d, on? %d)for crtc %d\n",
  1257. pll->pll_reg, pll->active, pll->on,
  1258. intel_crtc->base.base.id);
  1259. /* PCH refclock must be enabled first */
  1260. assert_pch_refclk_enabled(dev_priv);
  1261. if (pll->active++ && pll->on) {
  1262. assert_pch_pll_enabled(dev_priv, intel_crtc);
  1263. return;
  1264. }
  1265. DRM_DEBUG_KMS("enabling PCH PLL %x\n", pll->pll_reg);
  1266. reg = pll->pll_reg;
  1267. val = I915_READ(reg);
  1268. val |= DPLL_VCO_ENABLE;
  1269. I915_WRITE(reg, val);
  1270. POSTING_READ(reg);
  1271. udelay(200);
  1272. pll->on = true;
  1273. }
  1274. static void intel_disable_pch_pll(struct intel_crtc *intel_crtc)
  1275. {
  1276. struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
  1277. struct intel_pch_pll *pll = intel_crtc->pch_pll;
  1278. int reg;
  1279. u32 val;
  1280. /* PCH only available on ILK+ */
  1281. BUG_ON(dev_priv->info->gen < 5);
  1282. if (pll == NULL)
  1283. return;
  1284. if (WARN_ON(pll->refcount == 0))
  1285. return;
  1286. DRM_DEBUG_KMS("disable PCH PLL %x (active %d, on? %d) for crtc %d\n",
  1287. pll->pll_reg, pll->active, pll->on,
  1288. intel_crtc->base.base.id);
  1289. if (WARN_ON(pll->active == 0)) {
  1290. assert_pch_pll_disabled(dev_priv, intel_crtc);
  1291. return;
  1292. }
  1293. if (--pll->active) {
  1294. assert_pch_pll_enabled(dev_priv, intel_crtc);
  1295. return;
  1296. }
  1297. DRM_DEBUG_KMS("disabling PCH PLL %x\n", pll->pll_reg);
  1298. /* Make sure transcoder isn't still depending on us */
  1299. assert_transcoder_disabled(dev_priv, intel_crtc->pipe);
  1300. reg = pll->pll_reg;
  1301. val = I915_READ(reg);
  1302. val &= ~DPLL_VCO_ENABLE;
  1303. I915_WRITE(reg, val);
  1304. POSTING_READ(reg);
  1305. udelay(200);
  1306. pll->on = false;
  1307. }
  1308. static void intel_enable_transcoder(struct drm_i915_private *dev_priv,
  1309. enum pipe pipe)
  1310. {
  1311. int reg;
  1312. u32 val, pipeconf_val;
  1313. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1314. /* PCH only available on ILK+ */
  1315. BUG_ON(dev_priv->info->gen < 5);
  1316. /* Make sure PCH DPLL is enabled */
  1317. assert_pch_pll_enabled(dev_priv, to_intel_crtc(crtc));
  1318. /* FDI must be feeding us bits for PCH ports */
  1319. assert_fdi_tx_enabled(dev_priv, pipe);
  1320. assert_fdi_rx_enabled(dev_priv, pipe);
  1321. if (IS_HASWELL(dev_priv->dev) && pipe > 0) {
  1322. DRM_ERROR("Attempting to enable transcoder on Haswell with pipe > 0\n");
  1323. return;
  1324. }
  1325. reg = TRANSCONF(pipe);
  1326. val = I915_READ(reg);
  1327. pipeconf_val = I915_READ(PIPECONF(pipe));
  1328. if (HAS_PCH_IBX(dev_priv->dev)) {
  1329. /*
  1330. * make the BPC in transcoder be consistent with
  1331. * that in pipeconf reg.
  1332. */
  1333. val &= ~PIPE_BPC_MASK;
  1334. val |= pipeconf_val & PIPE_BPC_MASK;
  1335. }
  1336. val &= ~TRANS_INTERLACE_MASK;
  1337. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1338. if (HAS_PCH_IBX(dev_priv->dev) &&
  1339. intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
  1340. val |= TRANS_LEGACY_INTERLACED_ILK;
  1341. else
  1342. val |= TRANS_INTERLACED;
  1343. else
  1344. val |= TRANS_PROGRESSIVE;
  1345. I915_WRITE(reg, val | TRANS_ENABLE);
  1346. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1347. DRM_ERROR("failed to enable transcoder %d\n", pipe);
  1348. }
  1349. static void intel_disable_transcoder(struct drm_i915_private *dev_priv,
  1350. enum pipe pipe)
  1351. {
  1352. int reg;
  1353. u32 val;
  1354. /* FDI relies on the transcoder */
  1355. assert_fdi_tx_disabled(dev_priv, pipe);
  1356. assert_fdi_rx_disabled(dev_priv, pipe);
  1357. /* Ports must be off as well */
  1358. assert_pch_ports_disabled(dev_priv, pipe);
  1359. reg = TRANSCONF(pipe);
  1360. val = I915_READ(reg);
  1361. val &= ~TRANS_ENABLE;
  1362. I915_WRITE(reg, val);
  1363. /* wait for PCH transcoder off, transcoder state */
  1364. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1365. DRM_ERROR("failed to disable transcoder %d\n", pipe);
  1366. }
  1367. /**
  1368. * intel_enable_pipe - enable a pipe, asserting requirements
  1369. * @dev_priv: i915 private structure
  1370. * @pipe: pipe to enable
  1371. * @pch_port: on ILK+, is this pipe driving a PCH port or not
  1372. *
  1373. * Enable @pipe, making sure that various hardware specific requirements
  1374. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1375. *
  1376. * @pipe should be %PIPE_A or %PIPE_B.
  1377. *
  1378. * Will wait until the pipe is actually running (i.e. first vblank) before
  1379. * returning.
  1380. */
  1381. static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
  1382. bool pch_port)
  1383. {
  1384. int reg;
  1385. u32 val;
  1386. /*
  1387. * A pipe without a PLL won't actually be able to drive bits from
  1388. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1389. * need the check.
  1390. */
  1391. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1392. assert_pll_enabled(dev_priv, pipe);
  1393. else {
  1394. if (pch_port) {
  1395. /* if driving the PCH, we need FDI enabled */
  1396. assert_fdi_rx_pll_enabled(dev_priv, pipe);
  1397. assert_fdi_tx_pll_enabled(dev_priv, pipe);
  1398. }
  1399. /* FIXME: assert CPU port conditions for SNB+ */
  1400. }
  1401. reg = PIPECONF(pipe);
  1402. val = I915_READ(reg);
  1403. if (val & PIPECONF_ENABLE)
  1404. return;
  1405. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1406. intel_wait_for_vblank(dev_priv->dev, pipe);
  1407. }
  1408. /**
  1409. * intel_disable_pipe - disable a pipe, asserting requirements
  1410. * @dev_priv: i915 private structure
  1411. * @pipe: pipe to disable
  1412. *
  1413. * Disable @pipe, making sure that various hardware specific requirements
  1414. * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
  1415. *
  1416. * @pipe should be %PIPE_A or %PIPE_B.
  1417. *
  1418. * Will wait until the pipe has shut down before returning.
  1419. */
  1420. static void intel_disable_pipe(struct drm_i915_private *dev_priv,
  1421. enum pipe pipe)
  1422. {
  1423. int reg;
  1424. u32 val;
  1425. /*
  1426. * Make sure planes won't keep trying to pump pixels to us,
  1427. * or we might hang the display.
  1428. */
  1429. assert_planes_disabled(dev_priv, pipe);
  1430. /* Don't disable pipe A or pipe A PLLs if needed */
  1431. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1432. return;
  1433. reg = PIPECONF(pipe);
  1434. val = I915_READ(reg);
  1435. if ((val & PIPECONF_ENABLE) == 0)
  1436. return;
  1437. I915_WRITE(reg, val & ~PIPECONF_ENABLE);
  1438. intel_wait_for_pipe_off(dev_priv->dev, pipe);
  1439. }
  1440. /*
  1441. * Plane regs are double buffered, going from enabled->disabled needs a
  1442. * trigger in order to latch. The display address reg provides this.
  1443. */
  1444. void intel_flush_display_plane(struct drm_i915_private *dev_priv,
  1445. enum plane plane)
  1446. {
  1447. I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
  1448. I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
  1449. }
  1450. /**
  1451. * intel_enable_plane - enable a display plane on a given pipe
  1452. * @dev_priv: i915 private structure
  1453. * @plane: plane to enable
  1454. * @pipe: pipe being fed
  1455. *
  1456. * Enable @plane on @pipe, making sure that @pipe is running first.
  1457. */
  1458. static void intel_enable_plane(struct drm_i915_private *dev_priv,
  1459. enum plane plane, enum pipe pipe)
  1460. {
  1461. int reg;
  1462. u32 val;
  1463. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1464. assert_pipe_enabled(dev_priv, pipe);
  1465. reg = DSPCNTR(plane);
  1466. val = I915_READ(reg);
  1467. if (val & DISPLAY_PLANE_ENABLE)
  1468. return;
  1469. I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
  1470. intel_flush_display_plane(dev_priv, plane);
  1471. intel_wait_for_vblank(dev_priv->dev, pipe);
  1472. }
  1473. /**
  1474. * intel_disable_plane - disable a display plane
  1475. * @dev_priv: i915 private structure
  1476. * @plane: plane to disable
  1477. * @pipe: pipe consuming the data
  1478. *
  1479. * Disable @plane; should be an independent operation.
  1480. */
  1481. static void intel_disable_plane(struct drm_i915_private *dev_priv,
  1482. enum plane plane, enum pipe pipe)
  1483. {
  1484. int reg;
  1485. u32 val;
  1486. reg = DSPCNTR(plane);
  1487. val = I915_READ(reg);
  1488. if ((val & DISPLAY_PLANE_ENABLE) == 0)
  1489. return;
  1490. I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
  1491. intel_flush_display_plane(dev_priv, plane);
  1492. intel_wait_for_vblank(dev_priv->dev, pipe);
  1493. }
  1494. static void disable_pch_dp(struct drm_i915_private *dev_priv,
  1495. enum pipe pipe, int reg, u32 port_sel)
  1496. {
  1497. u32 val = I915_READ(reg);
  1498. if (dp_pipe_enabled(dev_priv, pipe, port_sel, val)) {
  1499. DRM_DEBUG_KMS("Disabling pch dp %x on pipe %d\n", reg, pipe);
  1500. I915_WRITE(reg, val & ~DP_PORT_EN);
  1501. }
  1502. }
  1503. static void disable_pch_hdmi(struct drm_i915_private *dev_priv,
  1504. enum pipe pipe, int reg)
  1505. {
  1506. u32 val = I915_READ(reg);
  1507. if (hdmi_pipe_enabled(dev_priv, val, pipe)) {
  1508. DRM_DEBUG_KMS("Disabling pch HDMI %x on pipe %d\n",
  1509. reg, pipe);
  1510. I915_WRITE(reg, val & ~PORT_ENABLE);
  1511. }
  1512. }
  1513. /* Disable any ports connected to this transcoder */
  1514. static void intel_disable_pch_ports(struct drm_i915_private *dev_priv,
  1515. enum pipe pipe)
  1516. {
  1517. u32 reg, val;
  1518. val = I915_READ(PCH_PP_CONTROL);
  1519. I915_WRITE(PCH_PP_CONTROL, val | PANEL_UNLOCK_REGS);
  1520. disable_pch_dp(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1521. disable_pch_dp(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1522. disable_pch_dp(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1523. reg = PCH_ADPA;
  1524. val = I915_READ(reg);
  1525. if (adpa_pipe_enabled(dev_priv, val, pipe))
  1526. I915_WRITE(reg, val & ~ADPA_DAC_ENABLE);
  1527. reg = PCH_LVDS;
  1528. val = I915_READ(reg);
  1529. if (lvds_pipe_enabled(dev_priv, val, pipe)) {
  1530. DRM_DEBUG_KMS("disable lvds on pipe %d val 0x%08x\n", pipe, val);
  1531. I915_WRITE(reg, val & ~LVDS_PORT_EN);
  1532. POSTING_READ(reg);
  1533. udelay(100);
  1534. }
  1535. disable_pch_hdmi(dev_priv, pipe, HDMIB);
  1536. disable_pch_hdmi(dev_priv, pipe, HDMIC);
  1537. disable_pch_hdmi(dev_priv, pipe, HDMID);
  1538. }
  1539. int
  1540. intel_pin_and_fence_fb_obj(struct drm_device *dev,
  1541. struct drm_i915_gem_object *obj,
  1542. struct intel_ring_buffer *pipelined)
  1543. {
  1544. struct drm_i915_private *dev_priv = dev->dev_private;
  1545. u32 alignment;
  1546. int ret;
  1547. switch (obj->tiling_mode) {
  1548. case I915_TILING_NONE:
  1549. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1550. alignment = 128 * 1024;
  1551. else if (INTEL_INFO(dev)->gen >= 4)
  1552. alignment = 4 * 1024;
  1553. else
  1554. alignment = 64 * 1024;
  1555. break;
  1556. case I915_TILING_X:
  1557. /* pin() will align the object as required by fence */
  1558. alignment = 0;
  1559. break;
  1560. case I915_TILING_Y:
  1561. /* FIXME: Is this true? */
  1562. DRM_ERROR("Y tiled not allowed for scan out buffers\n");
  1563. return -EINVAL;
  1564. default:
  1565. BUG();
  1566. }
  1567. dev_priv->mm.interruptible = false;
  1568. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1569. if (ret)
  1570. goto err_interruptible;
  1571. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1572. * fence, whereas 965+ only requires a fence if using
  1573. * framebuffer compression. For simplicity, we always install
  1574. * a fence as the cost is not that onerous.
  1575. */
  1576. ret = i915_gem_object_get_fence(obj);
  1577. if (ret)
  1578. goto err_unpin;
  1579. i915_gem_object_pin_fence(obj);
  1580. dev_priv->mm.interruptible = true;
  1581. return 0;
  1582. err_unpin:
  1583. i915_gem_object_unpin(obj);
  1584. err_interruptible:
  1585. dev_priv->mm.interruptible = true;
  1586. return ret;
  1587. }
  1588. void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
  1589. {
  1590. i915_gem_object_unpin_fence(obj);
  1591. i915_gem_object_unpin(obj);
  1592. }
  1593. static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1594. int x, int y)
  1595. {
  1596. struct drm_device *dev = crtc->dev;
  1597. struct drm_i915_private *dev_priv = dev->dev_private;
  1598. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1599. struct intel_framebuffer *intel_fb;
  1600. struct drm_i915_gem_object *obj;
  1601. int plane = intel_crtc->plane;
  1602. unsigned long Start, Offset;
  1603. u32 dspcntr;
  1604. u32 reg;
  1605. switch (plane) {
  1606. case 0:
  1607. case 1:
  1608. break;
  1609. default:
  1610. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1611. return -EINVAL;
  1612. }
  1613. intel_fb = to_intel_framebuffer(fb);
  1614. obj = intel_fb->obj;
  1615. reg = DSPCNTR(plane);
  1616. dspcntr = I915_READ(reg);
  1617. /* Mask out pixel format bits in case we change it */
  1618. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1619. switch (fb->bits_per_pixel) {
  1620. case 8:
  1621. dspcntr |= DISPPLANE_8BPP;
  1622. break;
  1623. case 16:
  1624. if (fb->depth == 15)
  1625. dspcntr |= DISPPLANE_15_16BPP;
  1626. else
  1627. dspcntr |= DISPPLANE_16BPP;
  1628. break;
  1629. case 24:
  1630. case 32:
  1631. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  1632. break;
  1633. default:
  1634. DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
  1635. return -EINVAL;
  1636. }
  1637. if (INTEL_INFO(dev)->gen >= 4) {
  1638. if (obj->tiling_mode != I915_TILING_NONE)
  1639. dspcntr |= DISPPLANE_TILED;
  1640. else
  1641. dspcntr &= ~DISPPLANE_TILED;
  1642. }
  1643. I915_WRITE(reg, dspcntr);
  1644. Start = obj->gtt_offset;
  1645. Offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  1646. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  1647. Start, Offset, x, y, fb->pitches[0]);
  1648. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  1649. if (INTEL_INFO(dev)->gen >= 4) {
  1650. I915_MODIFY_DISPBASE(DSPSURF(plane), Start);
  1651. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  1652. I915_WRITE(DSPADDR(plane), Offset);
  1653. } else
  1654. I915_WRITE(DSPADDR(plane), Start + Offset);
  1655. POSTING_READ(reg);
  1656. return 0;
  1657. }
  1658. static int ironlake_update_plane(struct drm_crtc *crtc,
  1659. struct drm_framebuffer *fb, int x, int y)
  1660. {
  1661. struct drm_device *dev = crtc->dev;
  1662. struct drm_i915_private *dev_priv = dev->dev_private;
  1663. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1664. struct intel_framebuffer *intel_fb;
  1665. struct drm_i915_gem_object *obj;
  1666. int plane = intel_crtc->plane;
  1667. unsigned long Start, Offset;
  1668. u32 dspcntr;
  1669. u32 reg;
  1670. switch (plane) {
  1671. case 0:
  1672. case 1:
  1673. case 2:
  1674. break;
  1675. default:
  1676. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1677. return -EINVAL;
  1678. }
  1679. intel_fb = to_intel_framebuffer(fb);
  1680. obj = intel_fb->obj;
  1681. reg = DSPCNTR(plane);
  1682. dspcntr = I915_READ(reg);
  1683. /* Mask out pixel format bits in case we change it */
  1684. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1685. switch (fb->bits_per_pixel) {
  1686. case 8:
  1687. dspcntr |= DISPPLANE_8BPP;
  1688. break;
  1689. case 16:
  1690. if (fb->depth != 16)
  1691. return -EINVAL;
  1692. dspcntr |= DISPPLANE_16BPP;
  1693. break;
  1694. case 24:
  1695. case 32:
  1696. if (fb->depth == 24)
  1697. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  1698. else if (fb->depth == 30)
  1699. dspcntr |= DISPPLANE_32BPP_30BIT_NO_ALPHA;
  1700. else
  1701. return -EINVAL;
  1702. break;
  1703. default:
  1704. DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
  1705. return -EINVAL;
  1706. }
  1707. if (obj->tiling_mode != I915_TILING_NONE)
  1708. dspcntr |= DISPPLANE_TILED;
  1709. else
  1710. dspcntr &= ~DISPPLANE_TILED;
  1711. /* must disable */
  1712. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  1713. I915_WRITE(reg, dspcntr);
  1714. Start = obj->gtt_offset;
  1715. Offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  1716. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  1717. Start, Offset, x, y, fb->pitches[0]);
  1718. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  1719. I915_MODIFY_DISPBASE(DSPSURF(plane), Start);
  1720. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  1721. I915_WRITE(DSPADDR(plane), Offset);
  1722. POSTING_READ(reg);
  1723. return 0;
  1724. }
  1725. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  1726. static int
  1727. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1728. int x, int y, enum mode_set_atomic state)
  1729. {
  1730. struct drm_device *dev = crtc->dev;
  1731. struct drm_i915_private *dev_priv = dev->dev_private;
  1732. if (dev_priv->display.disable_fbc)
  1733. dev_priv->display.disable_fbc(dev);
  1734. intel_increase_pllclock(crtc);
  1735. return dev_priv->display.update_plane(crtc, fb, x, y);
  1736. }
  1737. static int
  1738. intel_finish_fb(struct drm_framebuffer *old_fb)
  1739. {
  1740. struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
  1741. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1742. bool was_interruptible = dev_priv->mm.interruptible;
  1743. int ret;
  1744. wait_event(dev_priv->pending_flip_queue,
  1745. atomic_read(&dev_priv->mm.wedged) ||
  1746. atomic_read(&obj->pending_flip) == 0);
  1747. /* Big Hammer, we also need to ensure that any pending
  1748. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  1749. * current scanout is retired before unpinning the old
  1750. * framebuffer.
  1751. *
  1752. * This should only fail upon a hung GPU, in which case we
  1753. * can safely continue.
  1754. */
  1755. dev_priv->mm.interruptible = false;
  1756. ret = i915_gem_object_finish_gpu(obj);
  1757. dev_priv->mm.interruptible = was_interruptible;
  1758. return ret;
  1759. }
  1760. static int
  1761. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  1762. struct drm_framebuffer *old_fb)
  1763. {
  1764. struct drm_device *dev = crtc->dev;
  1765. struct drm_i915_private *dev_priv = dev->dev_private;
  1766. struct drm_i915_master_private *master_priv;
  1767. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1768. int ret;
  1769. /* no fb bound */
  1770. if (!crtc->fb) {
  1771. DRM_ERROR("No FB bound\n");
  1772. return 0;
  1773. }
  1774. if(intel_crtc->plane > dev_priv->num_pipe) {
  1775. DRM_ERROR("no plane for crtc: plane %d, num_pipes %d\n",
  1776. intel_crtc->plane,
  1777. dev_priv->num_pipe);
  1778. return -EINVAL;
  1779. }
  1780. mutex_lock(&dev->struct_mutex);
  1781. ret = intel_pin_and_fence_fb_obj(dev,
  1782. to_intel_framebuffer(crtc->fb)->obj,
  1783. NULL);
  1784. if (ret != 0) {
  1785. mutex_unlock(&dev->struct_mutex);
  1786. DRM_ERROR("pin & fence failed\n");
  1787. return ret;
  1788. }
  1789. if (old_fb)
  1790. intel_finish_fb(old_fb);
  1791. ret = dev_priv->display.update_plane(crtc, crtc->fb, x, y);
  1792. if (ret) {
  1793. intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
  1794. mutex_unlock(&dev->struct_mutex);
  1795. DRM_ERROR("failed to update base address\n");
  1796. return ret;
  1797. }
  1798. if (old_fb) {
  1799. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1800. intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
  1801. }
  1802. intel_update_fbc(dev);
  1803. mutex_unlock(&dev->struct_mutex);
  1804. if (!dev->primary->master)
  1805. return 0;
  1806. master_priv = dev->primary->master->driver_priv;
  1807. if (!master_priv->sarea_priv)
  1808. return 0;
  1809. if (intel_crtc->pipe) {
  1810. master_priv->sarea_priv->pipeB_x = x;
  1811. master_priv->sarea_priv->pipeB_y = y;
  1812. } else {
  1813. master_priv->sarea_priv->pipeA_x = x;
  1814. master_priv->sarea_priv->pipeA_y = y;
  1815. }
  1816. return 0;
  1817. }
  1818. static void ironlake_set_pll_edp(struct drm_crtc *crtc, int clock)
  1819. {
  1820. struct drm_device *dev = crtc->dev;
  1821. struct drm_i915_private *dev_priv = dev->dev_private;
  1822. u32 dpa_ctl;
  1823. DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
  1824. dpa_ctl = I915_READ(DP_A);
  1825. dpa_ctl &= ~DP_PLL_FREQ_MASK;
  1826. if (clock < 200000) {
  1827. u32 temp;
  1828. dpa_ctl |= DP_PLL_FREQ_160MHZ;
  1829. /* workaround for 160Mhz:
  1830. 1) program 0x4600c bits 15:0 = 0x8124
  1831. 2) program 0x46010 bit 0 = 1
  1832. 3) program 0x46034 bit 24 = 1
  1833. 4) program 0x64000 bit 14 = 1
  1834. */
  1835. temp = I915_READ(0x4600c);
  1836. temp &= 0xffff0000;
  1837. I915_WRITE(0x4600c, temp | 0x8124);
  1838. temp = I915_READ(0x46010);
  1839. I915_WRITE(0x46010, temp | 1);
  1840. temp = I915_READ(0x46034);
  1841. I915_WRITE(0x46034, temp | (1 << 24));
  1842. } else {
  1843. dpa_ctl |= DP_PLL_FREQ_270MHZ;
  1844. }
  1845. I915_WRITE(DP_A, dpa_ctl);
  1846. POSTING_READ(DP_A);
  1847. udelay(500);
  1848. }
  1849. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  1850. {
  1851. struct drm_device *dev = crtc->dev;
  1852. struct drm_i915_private *dev_priv = dev->dev_private;
  1853. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1854. int pipe = intel_crtc->pipe;
  1855. u32 reg, temp;
  1856. /* enable normal train */
  1857. reg = FDI_TX_CTL(pipe);
  1858. temp = I915_READ(reg);
  1859. if (IS_IVYBRIDGE(dev)) {
  1860. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  1861. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  1862. } else {
  1863. temp &= ~FDI_LINK_TRAIN_NONE;
  1864. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  1865. }
  1866. I915_WRITE(reg, temp);
  1867. reg = FDI_RX_CTL(pipe);
  1868. temp = I915_READ(reg);
  1869. if (HAS_PCH_CPT(dev)) {
  1870. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1871. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  1872. } else {
  1873. temp &= ~FDI_LINK_TRAIN_NONE;
  1874. temp |= FDI_LINK_TRAIN_NONE;
  1875. }
  1876. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  1877. /* wait one idle pattern time */
  1878. POSTING_READ(reg);
  1879. udelay(1000);
  1880. /* IVB wants error correction enabled */
  1881. if (IS_IVYBRIDGE(dev))
  1882. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  1883. FDI_FE_ERRC_ENABLE);
  1884. }
  1885. static void cpt_phase_pointer_enable(struct drm_device *dev, int pipe)
  1886. {
  1887. struct drm_i915_private *dev_priv = dev->dev_private;
  1888. u32 flags = I915_READ(SOUTH_CHICKEN1);
  1889. flags |= FDI_PHASE_SYNC_OVR(pipe);
  1890. I915_WRITE(SOUTH_CHICKEN1, flags); /* once to unlock... */
  1891. flags |= FDI_PHASE_SYNC_EN(pipe);
  1892. I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to enable */
  1893. POSTING_READ(SOUTH_CHICKEN1);
  1894. }
  1895. /* The FDI link training functions for ILK/Ibexpeak. */
  1896. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  1897. {
  1898. struct drm_device *dev = crtc->dev;
  1899. struct drm_i915_private *dev_priv = dev->dev_private;
  1900. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1901. int pipe = intel_crtc->pipe;
  1902. int plane = intel_crtc->plane;
  1903. u32 reg, temp, tries;
  1904. /* FDI needs bits from pipe & plane first */
  1905. assert_pipe_enabled(dev_priv, pipe);
  1906. assert_plane_enabled(dev_priv, plane);
  1907. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  1908. for train result */
  1909. reg = FDI_RX_IMR(pipe);
  1910. temp = I915_READ(reg);
  1911. temp &= ~FDI_RX_SYMBOL_LOCK;
  1912. temp &= ~FDI_RX_BIT_LOCK;
  1913. I915_WRITE(reg, temp);
  1914. I915_READ(reg);
  1915. udelay(150);
  1916. /* enable CPU FDI TX and PCH FDI RX */
  1917. reg = FDI_TX_CTL(pipe);
  1918. temp = I915_READ(reg);
  1919. temp &= ~(7 << 19);
  1920. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  1921. temp &= ~FDI_LINK_TRAIN_NONE;
  1922. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1923. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  1924. reg = FDI_RX_CTL(pipe);
  1925. temp = I915_READ(reg);
  1926. temp &= ~FDI_LINK_TRAIN_NONE;
  1927. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1928. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  1929. POSTING_READ(reg);
  1930. udelay(150);
  1931. /* Ironlake workaround, enable clock pointer after FDI enable*/
  1932. if (HAS_PCH_IBX(dev)) {
  1933. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  1934. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  1935. FDI_RX_PHASE_SYNC_POINTER_EN);
  1936. }
  1937. reg = FDI_RX_IIR(pipe);
  1938. for (tries = 0; tries < 5; tries++) {
  1939. temp = I915_READ(reg);
  1940. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1941. if ((temp & FDI_RX_BIT_LOCK)) {
  1942. DRM_DEBUG_KMS("FDI train 1 done.\n");
  1943. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  1944. break;
  1945. }
  1946. }
  1947. if (tries == 5)
  1948. DRM_ERROR("FDI train 1 fail!\n");
  1949. /* Train 2 */
  1950. reg = FDI_TX_CTL(pipe);
  1951. temp = I915_READ(reg);
  1952. temp &= ~FDI_LINK_TRAIN_NONE;
  1953. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1954. I915_WRITE(reg, temp);
  1955. reg = FDI_RX_CTL(pipe);
  1956. temp = I915_READ(reg);
  1957. temp &= ~FDI_LINK_TRAIN_NONE;
  1958. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1959. I915_WRITE(reg, temp);
  1960. POSTING_READ(reg);
  1961. udelay(150);
  1962. reg = FDI_RX_IIR(pipe);
  1963. for (tries = 0; tries < 5; tries++) {
  1964. temp = I915_READ(reg);
  1965. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1966. if (temp & FDI_RX_SYMBOL_LOCK) {
  1967. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  1968. DRM_DEBUG_KMS("FDI train 2 done.\n");
  1969. break;
  1970. }
  1971. }
  1972. if (tries == 5)
  1973. DRM_ERROR("FDI train 2 fail!\n");
  1974. DRM_DEBUG_KMS("FDI train done\n");
  1975. }
  1976. static const int snb_b_fdi_train_param[] = {
  1977. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  1978. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  1979. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  1980. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  1981. };
  1982. /* The FDI link training functions for SNB/Cougarpoint. */
  1983. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  1984. {
  1985. struct drm_device *dev = crtc->dev;
  1986. struct drm_i915_private *dev_priv = dev->dev_private;
  1987. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1988. int pipe = intel_crtc->pipe;
  1989. u32 reg, temp, i, retry;
  1990. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  1991. for train result */
  1992. reg = FDI_RX_IMR(pipe);
  1993. temp = I915_READ(reg);
  1994. temp &= ~FDI_RX_SYMBOL_LOCK;
  1995. temp &= ~FDI_RX_BIT_LOCK;
  1996. I915_WRITE(reg, temp);
  1997. POSTING_READ(reg);
  1998. udelay(150);
  1999. /* enable CPU FDI TX and PCH FDI RX */
  2000. reg = FDI_TX_CTL(pipe);
  2001. temp = I915_READ(reg);
  2002. temp &= ~(7 << 19);
  2003. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2004. temp &= ~FDI_LINK_TRAIN_NONE;
  2005. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2006. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2007. /* SNB-B */
  2008. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2009. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2010. reg = FDI_RX_CTL(pipe);
  2011. temp = I915_READ(reg);
  2012. if (HAS_PCH_CPT(dev)) {
  2013. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2014. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2015. } else {
  2016. temp &= ~FDI_LINK_TRAIN_NONE;
  2017. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2018. }
  2019. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2020. POSTING_READ(reg);
  2021. udelay(150);
  2022. if (HAS_PCH_CPT(dev))
  2023. cpt_phase_pointer_enable(dev, pipe);
  2024. for (i = 0; i < 4; i++) {
  2025. reg = FDI_TX_CTL(pipe);
  2026. temp = I915_READ(reg);
  2027. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2028. temp |= snb_b_fdi_train_param[i];
  2029. I915_WRITE(reg, temp);
  2030. POSTING_READ(reg);
  2031. udelay(500);
  2032. for (retry = 0; retry < 5; retry++) {
  2033. reg = FDI_RX_IIR(pipe);
  2034. temp = I915_READ(reg);
  2035. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2036. if (temp & FDI_RX_BIT_LOCK) {
  2037. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2038. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2039. break;
  2040. }
  2041. udelay(50);
  2042. }
  2043. if (retry < 5)
  2044. break;
  2045. }
  2046. if (i == 4)
  2047. DRM_ERROR("FDI train 1 fail!\n");
  2048. /* Train 2 */
  2049. reg = FDI_TX_CTL(pipe);
  2050. temp = I915_READ(reg);
  2051. temp &= ~FDI_LINK_TRAIN_NONE;
  2052. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2053. if (IS_GEN6(dev)) {
  2054. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2055. /* SNB-B */
  2056. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2057. }
  2058. I915_WRITE(reg, temp);
  2059. reg = FDI_RX_CTL(pipe);
  2060. temp = I915_READ(reg);
  2061. if (HAS_PCH_CPT(dev)) {
  2062. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2063. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2064. } else {
  2065. temp &= ~FDI_LINK_TRAIN_NONE;
  2066. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2067. }
  2068. I915_WRITE(reg, temp);
  2069. POSTING_READ(reg);
  2070. udelay(150);
  2071. for (i = 0; i < 4; i++) {
  2072. reg = FDI_TX_CTL(pipe);
  2073. temp = I915_READ(reg);
  2074. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2075. temp |= snb_b_fdi_train_param[i];
  2076. I915_WRITE(reg, temp);
  2077. POSTING_READ(reg);
  2078. udelay(500);
  2079. for (retry = 0; retry < 5; retry++) {
  2080. reg = FDI_RX_IIR(pipe);
  2081. temp = I915_READ(reg);
  2082. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2083. if (temp & FDI_RX_SYMBOL_LOCK) {
  2084. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2085. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2086. break;
  2087. }
  2088. udelay(50);
  2089. }
  2090. if (retry < 5)
  2091. break;
  2092. }
  2093. if (i == 4)
  2094. DRM_ERROR("FDI train 2 fail!\n");
  2095. DRM_DEBUG_KMS("FDI train done.\n");
  2096. }
  2097. /* Manual link training for Ivy Bridge A0 parts */
  2098. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2099. {
  2100. struct drm_device *dev = crtc->dev;
  2101. struct drm_i915_private *dev_priv = dev->dev_private;
  2102. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2103. int pipe = intel_crtc->pipe;
  2104. u32 reg, temp, i;
  2105. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2106. for train result */
  2107. reg = FDI_RX_IMR(pipe);
  2108. temp = I915_READ(reg);
  2109. temp &= ~FDI_RX_SYMBOL_LOCK;
  2110. temp &= ~FDI_RX_BIT_LOCK;
  2111. I915_WRITE(reg, temp);
  2112. POSTING_READ(reg);
  2113. udelay(150);
  2114. /* enable CPU FDI TX and PCH FDI RX */
  2115. reg = FDI_TX_CTL(pipe);
  2116. temp = I915_READ(reg);
  2117. temp &= ~(7 << 19);
  2118. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2119. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2120. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2121. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2122. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2123. temp |= FDI_COMPOSITE_SYNC;
  2124. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2125. reg = FDI_RX_CTL(pipe);
  2126. temp = I915_READ(reg);
  2127. temp &= ~FDI_LINK_TRAIN_AUTO;
  2128. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2129. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2130. temp |= FDI_COMPOSITE_SYNC;
  2131. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2132. POSTING_READ(reg);
  2133. udelay(150);
  2134. if (HAS_PCH_CPT(dev))
  2135. cpt_phase_pointer_enable(dev, pipe);
  2136. for (i = 0; i < 4; i++) {
  2137. reg = FDI_TX_CTL(pipe);
  2138. temp = I915_READ(reg);
  2139. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2140. temp |= snb_b_fdi_train_param[i];
  2141. I915_WRITE(reg, temp);
  2142. POSTING_READ(reg);
  2143. udelay(500);
  2144. reg = FDI_RX_IIR(pipe);
  2145. temp = I915_READ(reg);
  2146. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2147. if (temp & FDI_RX_BIT_LOCK ||
  2148. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2149. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2150. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2151. break;
  2152. }
  2153. }
  2154. if (i == 4)
  2155. DRM_ERROR("FDI train 1 fail!\n");
  2156. /* Train 2 */
  2157. reg = FDI_TX_CTL(pipe);
  2158. temp = I915_READ(reg);
  2159. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2160. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2161. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2162. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2163. I915_WRITE(reg, temp);
  2164. reg = FDI_RX_CTL(pipe);
  2165. temp = I915_READ(reg);
  2166. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2167. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2168. I915_WRITE(reg, temp);
  2169. POSTING_READ(reg);
  2170. udelay(150);
  2171. for (i = 0; i < 4; i++) {
  2172. reg = FDI_TX_CTL(pipe);
  2173. temp = I915_READ(reg);
  2174. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2175. temp |= snb_b_fdi_train_param[i];
  2176. I915_WRITE(reg, temp);
  2177. POSTING_READ(reg);
  2178. udelay(500);
  2179. reg = FDI_RX_IIR(pipe);
  2180. temp = I915_READ(reg);
  2181. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2182. if (temp & FDI_RX_SYMBOL_LOCK) {
  2183. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2184. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2185. break;
  2186. }
  2187. }
  2188. if (i == 4)
  2189. DRM_ERROR("FDI train 2 fail!\n");
  2190. DRM_DEBUG_KMS("FDI train done.\n");
  2191. }
  2192. static void ironlake_fdi_pll_enable(struct drm_crtc *crtc)
  2193. {
  2194. struct drm_device *dev = crtc->dev;
  2195. struct drm_i915_private *dev_priv = dev->dev_private;
  2196. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2197. int pipe = intel_crtc->pipe;
  2198. u32 reg, temp;
  2199. /* Write the TU size bits so error detection works */
  2200. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  2201. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  2202. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2203. reg = FDI_RX_CTL(pipe);
  2204. temp = I915_READ(reg);
  2205. temp &= ~((0x7 << 19) | (0x7 << 16));
  2206. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2207. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2208. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2209. POSTING_READ(reg);
  2210. udelay(200);
  2211. /* Switch from Rawclk to PCDclk */
  2212. temp = I915_READ(reg);
  2213. I915_WRITE(reg, temp | FDI_PCDCLK);
  2214. POSTING_READ(reg);
  2215. udelay(200);
  2216. /* On Haswell, the PLL configuration for ports and pipes is handled
  2217. * separately, as part of DDI setup */
  2218. if (!IS_HASWELL(dev)) {
  2219. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2220. reg = FDI_TX_CTL(pipe);
  2221. temp = I915_READ(reg);
  2222. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2223. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2224. POSTING_READ(reg);
  2225. udelay(100);
  2226. }
  2227. }
  2228. }
  2229. static void cpt_phase_pointer_disable(struct drm_device *dev, int pipe)
  2230. {
  2231. struct drm_i915_private *dev_priv = dev->dev_private;
  2232. u32 flags = I915_READ(SOUTH_CHICKEN1);
  2233. flags &= ~(FDI_PHASE_SYNC_EN(pipe));
  2234. I915_WRITE(SOUTH_CHICKEN1, flags); /* once to disable... */
  2235. flags &= ~(FDI_PHASE_SYNC_OVR(pipe));
  2236. I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to lock */
  2237. POSTING_READ(SOUTH_CHICKEN1);
  2238. }
  2239. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2240. {
  2241. struct drm_device *dev = crtc->dev;
  2242. struct drm_i915_private *dev_priv = dev->dev_private;
  2243. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2244. int pipe = intel_crtc->pipe;
  2245. u32 reg, temp;
  2246. /* disable CPU FDI tx and PCH FDI rx */
  2247. reg = FDI_TX_CTL(pipe);
  2248. temp = I915_READ(reg);
  2249. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2250. POSTING_READ(reg);
  2251. reg = FDI_RX_CTL(pipe);
  2252. temp = I915_READ(reg);
  2253. temp &= ~(0x7 << 16);
  2254. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2255. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2256. POSTING_READ(reg);
  2257. udelay(100);
  2258. /* Ironlake workaround, disable clock pointer after downing FDI */
  2259. if (HAS_PCH_IBX(dev)) {
  2260. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2261. I915_WRITE(FDI_RX_CHICKEN(pipe),
  2262. I915_READ(FDI_RX_CHICKEN(pipe) &
  2263. ~FDI_RX_PHASE_SYNC_POINTER_EN));
  2264. } else if (HAS_PCH_CPT(dev)) {
  2265. cpt_phase_pointer_disable(dev, pipe);
  2266. }
  2267. /* still set train pattern 1 */
  2268. reg = FDI_TX_CTL(pipe);
  2269. temp = I915_READ(reg);
  2270. temp &= ~FDI_LINK_TRAIN_NONE;
  2271. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2272. I915_WRITE(reg, temp);
  2273. reg = FDI_RX_CTL(pipe);
  2274. temp = I915_READ(reg);
  2275. if (HAS_PCH_CPT(dev)) {
  2276. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2277. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2278. } else {
  2279. temp &= ~FDI_LINK_TRAIN_NONE;
  2280. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2281. }
  2282. /* BPC in FDI rx is consistent with that in PIPECONF */
  2283. temp &= ~(0x07 << 16);
  2284. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2285. I915_WRITE(reg, temp);
  2286. POSTING_READ(reg);
  2287. udelay(100);
  2288. }
  2289. static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  2290. {
  2291. struct drm_device *dev = crtc->dev;
  2292. if (crtc->fb == NULL)
  2293. return;
  2294. mutex_lock(&dev->struct_mutex);
  2295. intel_finish_fb(crtc->fb);
  2296. mutex_unlock(&dev->struct_mutex);
  2297. }
  2298. static bool intel_crtc_driving_pch(struct drm_crtc *crtc)
  2299. {
  2300. struct drm_device *dev = crtc->dev;
  2301. struct drm_mode_config *mode_config = &dev->mode_config;
  2302. struct intel_encoder *encoder;
  2303. /*
  2304. * If there's a non-PCH eDP on this crtc, it must be DP_A, and that
  2305. * must be driven by its own crtc; no sharing is possible.
  2306. */
  2307. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  2308. if (encoder->base.crtc != crtc)
  2309. continue;
  2310. /* On Haswell, LPT PCH handles the VGA connection via FDI, and Haswell
  2311. * CPU handles all others */
  2312. if (IS_HASWELL(dev)) {
  2313. /* It is still unclear how this will work on PPT, so throw up a warning */
  2314. WARN_ON(!HAS_PCH_LPT(dev));
  2315. if (encoder->type == DRM_MODE_ENCODER_DAC) {
  2316. DRM_DEBUG_KMS("Haswell detected DAC encoder, assuming is PCH\n");
  2317. return true;
  2318. } else {
  2319. DRM_DEBUG_KMS("Haswell detected encoder %d, assuming is CPU\n",
  2320. encoder->type);
  2321. return false;
  2322. }
  2323. }
  2324. switch (encoder->type) {
  2325. case INTEL_OUTPUT_EDP:
  2326. if (!intel_encoder_is_pch_edp(&encoder->base))
  2327. return false;
  2328. continue;
  2329. }
  2330. }
  2331. return true;
  2332. }
  2333. /* Program iCLKIP clock to the desired frequency */
  2334. static void lpt_program_iclkip(struct drm_crtc *crtc)
  2335. {
  2336. struct drm_device *dev = crtc->dev;
  2337. struct drm_i915_private *dev_priv = dev->dev_private;
  2338. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  2339. u32 temp;
  2340. /* It is necessary to ungate the pixclk gate prior to programming
  2341. * the divisors, and gate it back when it is done.
  2342. */
  2343. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  2344. /* Disable SSCCTL */
  2345. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  2346. intel_sbi_read(dev_priv, SBI_SSCCTL6) |
  2347. SBI_SSCCTL_DISABLE);
  2348. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  2349. if (crtc->mode.clock == 20000) {
  2350. auxdiv = 1;
  2351. divsel = 0x41;
  2352. phaseinc = 0x20;
  2353. } else {
  2354. /* The iCLK virtual clock root frequency is in MHz,
  2355. * but the crtc->mode.clock in in KHz. To get the divisors,
  2356. * it is necessary to divide one by another, so we
  2357. * convert the virtual clock precision to KHz here for higher
  2358. * precision.
  2359. */
  2360. u32 iclk_virtual_root_freq = 172800 * 1000;
  2361. u32 iclk_pi_range = 64;
  2362. u32 desired_divisor, msb_divisor_value, pi_value;
  2363. desired_divisor = (iclk_virtual_root_freq / crtc->mode.clock);
  2364. msb_divisor_value = desired_divisor / iclk_pi_range;
  2365. pi_value = desired_divisor % iclk_pi_range;
  2366. auxdiv = 0;
  2367. divsel = msb_divisor_value - 2;
  2368. phaseinc = pi_value;
  2369. }
  2370. /* This should not happen with any sane values */
  2371. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  2372. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  2373. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  2374. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  2375. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  2376. crtc->mode.clock,
  2377. auxdiv,
  2378. divsel,
  2379. phasedir,
  2380. phaseinc);
  2381. /* Program SSCDIVINTPHASE6 */
  2382. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6);
  2383. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  2384. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  2385. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  2386. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  2387. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  2388. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  2389. intel_sbi_write(dev_priv,
  2390. SBI_SSCDIVINTPHASE6,
  2391. temp);
  2392. /* Program SSCAUXDIV */
  2393. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6);
  2394. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  2395. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  2396. intel_sbi_write(dev_priv,
  2397. SBI_SSCAUXDIV6,
  2398. temp);
  2399. /* Enable modulator and associated divider */
  2400. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6);
  2401. temp &= ~SBI_SSCCTL_DISABLE;
  2402. intel_sbi_write(dev_priv,
  2403. SBI_SSCCTL6,
  2404. temp);
  2405. /* Wait for initialization time */
  2406. udelay(24);
  2407. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  2408. }
  2409. /*
  2410. * Enable PCH resources required for PCH ports:
  2411. * - PCH PLLs
  2412. * - FDI training & RX/TX
  2413. * - update transcoder timings
  2414. * - DP transcoding bits
  2415. * - transcoder
  2416. */
  2417. static void ironlake_pch_enable(struct drm_crtc *crtc)
  2418. {
  2419. struct drm_device *dev = crtc->dev;
  2420. struct drm_i915_private *dev_priv = dev->dev_private;
  2421. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2422. int pipe = intel_crtc->pipe;
  2423. u32 reg, temp;
  2424. assert_transcoder_disabled(dev_priv, pipe);
  2425. /* For PCH output, training FDI link */
  2426. dev_priv->display.fdi_link_train(crtc);
  2427. intel_enable_pch_pll(intel_crtc);
  2428. if (HAS_PCH_LPT(dev)) {
  2429. DRM_DEBUG_KMS("LPT detected: programming iCLKIP\n");
  2430. lpt_program_iclkip(crtc);
  2431. } else if (HAS_PCH_CPT(dev)) {
  2432. u32 sel;
  2433. temp = I915_READ(PCH_DPLL_SEL);
  2434. switch (pipe) {
  2435. default:
  2436. case 0:
  2437. temp |= TRANSA_DPLL_ENABLE;
  2438. sel = TRANSA_DPLLB_SEL;
  2439. break;
  2440. case 1:
  2441. temp |= TRANSB_DPLL_ENABLE;
  2442. sel = TRANSB_DPLLB_SEL;
  2443. break;
  2444. case 2:
  2445. temp |= TRANSC_DPLL_ENABLE;
  2446. sel = TRANSC_DPLLB_SEL;
  2447. break;
  2448. }
  2449. if (intel_crtc->pch_pll->pll_reg == _PCH_DPLL_B)
  2450. temp |= sel;
  2451. else
  2452. temp &= ~sel;
  2453. I915_WRITE(PCH_DPLL_SEL, temp);
  2454. }
  2455. /* set transcoder timing, panel must allow it */
  2456. assert_panel_unlocked(dev_priv, pipe);
  2457. I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
  2458. I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
  2459. I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
  2460. I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
  2461. I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
  2462. I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
  2463. I915_WRITE(TRANS_VSYNCSHIFT(pipe), I915_READ(VSYNCSHIFT(pipe)));
  2464. if (!IS_HASWELL(dev))
  2465. intel_fdi_normal_train(crtc);
  2466. /* For PCH DP, enable TRANS_DP_CTL */
  2467. if (HAS_PCH_CPT(dev) &&
  2468. (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  2469. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2470. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) >> 5;
  2471. reg = TRANS_DP_CTL(pipe);
  2472. temp = I915_READ(reg);
  2473. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  2474. TRANS_DP_SYNC_MASK |
  2475. TRANS_DP_BPC_MASK);
  2476. temp |= (TRANS_DP_OUTPUT_ENABLE |
  2477. TRANS_DP_ENH_FRAMING);
  2478. temp |= bpc << 9; /* same format but at 11:9 */
  2479. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  2480. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  2481. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  2482. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  2483. switch (intel_trans_dp_port_sel(crtc)) {
  2484. case PCH_DP_B:
  2485. temp |= TRANS_DP_PORT_SEL_B;
  2486. break;
  2487. case PCH_DP_C:
  2488. temp |= TRANS_DP_PORT_SEL_C;
  2489. break;
  2490. case PCH_DP_D:
  2491. temp |= TRANS_DP_PORT_SEL_D;
  2492. break;
  2493. default:
  2494. DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
  2495. temp |= TRANS_DP_PORT_SEL_B;
  2496. break;
  2497. }
  2498. I915_WRITE(reg, temp);
  2499. }
  2500. intel_enable_transcoder(dev_priv, pipe);
  2501. }
  2502. static void intel_put_pch_pll(struct intel_crtc *intel_crtc)
  2503. {
  2504. struct intel_pch_pll *pll = intel_crtc->pch_pll;
  2505. if (pll == NULL)
  2506. return;
  2507. if (pll->refcount == 0) {
  2508. WARN(1, "bad PCH PLL refcount\n");
  2509. return;
  2510. }
  2511. --pll->refcount;
  2512. intel_crtc->pch_pll = NULL;
  2513. }
  2514. static struct intel_pch_pll *intel_get_pch_pll(struct intel_crtc *intel_crtc, u32 dpll, u32 fp)
  2515. {
  2516. struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
  2517. struct intel_pch_pll *pll;
  2518. int i;
  2519. pll = intel_crtc->pch_pll;
  2520. if (pll) {
  2521. DRM_DEBUG_KMS("CRTC:%d reusing existing PCH PLL %x\n",
  2522. intel_crtc->base.base.id, pll->pll_reg);
  2523. goto prepare;
  2524. }
  2525. if (HAS_PCH_IBX(dev_priv->dev)) {
  2526. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  2527. i = intel_crtc->pipe;
  2528. pll = &dev_priv->pch_plls[i];
  2529. DRM_DEBUG_KMS("CRTC:%d using pre-allocated PCH PLL %x\n",
  2530. intel_crtc->base.base.id, pll->pll_reg);
  2531. goto found;
  2532. }
  2533. for (i = 0; i < dev_priv->num_pch_pll; i++) {
  2534. pll = &dev_priv->pch_plls[i];
  2535. /* Only want to check enabled timings first */
  2536. if (pll->refcount == 0)
  2537. continue;
  2538. if (dpll == (I915_READ(pll->pll_reg) & 0x7fffffff) &&
  2539. fp == I915_READ(pll->fp0_reg)) {
  2540. DRM_DEBUG_KMS("CRTC:%d sharing existing PCH PLL %x (refcount %d, ative %d)\n",
  2541. intel_crtc->base.base.id,
  2542. pll->pll_reg, pll->refcount, pll->active);
  2543. goto found;
  2544. }
  2545. }
  2546. /* Ok no matching timings, maybe there's a free one? */
  2547. for (i = 0; i < dev_priv->num_pch_pll; i++) {
  2548. pll = &dev_priv->pch_plls[i];
  2549. if (pll->refcount == 0) {
  2550. DRM_DEBUG_KMS("CRTC:%d allocated PCH PLL %x\n",
  2551. intel_crtc->base.base.id, pll->pll_reg);
  2552. goto found;
  2553. }
  2554. }
  2555. return NULL;
  2556. found:
  2557. intel_crtc->pch_pll = pll;
  2558. pll->refcount++;
  2559. DRM_DEBUG_DRIVER("using pll %d for pipe %d\n", i, intel_crtc->pipe);
  2560. prepare: /* separate function? */
  2561. DRM_DEBUG_DRIVER("switching PLL %x off\n", pll->pll_reg);
  2562. /* Wait for the clocks to stabilize before rewriting the regs */
  2563. I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
  2564. POSTING_READ(pll->pll_reg);
  2565. udelay(150);
  2566. I915_WRITE(pll->fp0_reg, fp);
  2567. I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
  2568. pll->on = false;
  2569. return pll;
  2570. }
  2571. void intel_cpt_verify_modeset(struct drm_device *dev, int pipe)
  2572. {
  2573. struct drm_i915_private *dev_priv = dev->dev_private;
  2574. int dslreg = PIPEDSL(pipe), tc2reg = TRANS_CHICKEN2(pipe);
  2575. u32 temp;
  2576. temp = I915_READ(dslreg);
  2577. udelay(500);
  2578. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  2579. /* Without this, mode sets may fail silently on FDI */
  2580. I915_WRITE(tc2reg, TRANS_AUTOTRAIN_GEN_STALL_DIS);
  2581. udelay(250);
  2582. I915_WRITE(tc2reg, 0);
  2583. if (wait_for(I915_READ(dslreg) != temp, 5))
  2584. DRM_ERROR("mode set failed: pipe %d stuck\n", pipe);
  2585. }
  2586. }
  2587. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  2588. {
  2589. struct drm_device *dev = crtc->dev;
  2590. struct drm_i915_private *dev_priv = dev->dev_private;
  2591. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2592. int pipe = intel_crtc->pipe;
  2593. int plane = intel_crtc->plane;
  2594. u32 temp;
  2595. bool is_pch_port;
  2596. if (intel_crtc->active)
  2597. return;
  2598. intel_crtc->active = true;
  2599. intel_update_watermarks(dev);
  2600. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  2601. temp = I915_READ(PCH_LVDS);
  2602. if ((temp & LVDS_PORT_EN) == 0)
  2603. I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
  2604. }
  2605. is_pch_port = intel_crtc_driving_pch(crtc);
  2606. if (is_pch_port)
  2607. ironlake_fdi_pll_enable(crtc);
  2608. else
  2609. ironlake_fdi_disable(crtc);
  2610. /* Enable panel fitting for LVDS */
  2611. if (dev_priv->pch_pf_size &&
  2612. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) || HAS_eDP)) {
  2613. /* Force use of hard-coded filter coefficients
  2614. * as some pre-programmed values are broken,
  2615. * e.g. x201.
  2616. */
  2617. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  2618. I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
  2619. I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
  2620. }
  2621. /*
  2622. * On ILK+ LUT must be loaded before the pipe is running but with
  2623. * clocks enabled
  2624. */
  2625. intel_crtc_load_lut(crtc);
  2626. intel_enable_pipe(dev_priv, pipe, is_pch_port);
  2627. intel_enable_plane(dev_priv, plane, pipe);
  2628. if (is_pch_port)
  2629. ironlake_pch_enable(crtc);
  2630. mutex_lock(&dev->struct_mutex);
  2631. intel_update_fbc(dev);
  2632. mutex_unlock(&dev->struct_mutex);
  2633. intel_crtc_update_cursor(crtc, true);
  2634. }
  2635. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  2636. {
  2637. struct drm_device *dev = crtc->dev;
  2638. struct drm_i915_private *dev_priv = dev->dev_private;
  2639. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2640. int pipe = intel_crtc->pipe;
  2641. int plane = intel_crtc->plane;
  2642. u32 reg, temp;
  2643. if (!intel_crtc->active)
  2644. return;
  2645. intel_crtc_wait_for_pending_flips(crtc);
  2646. drm_vblank_off(dev, pipe);
  2647. intel_crtc_update_cursor(crtc, false);
  2648. intel_disable_plane(dev_priv, plane, pipe);
  2649. if (dev_priv->cfb_plane == plane)
  2650. intel_disable_fbc(dev);
  2651. intel_disable_pipe(dev_priv, pipe);
  2652. /* Disable PF */
  2653. I915_WRITE(PF_CTL(pipe), 0);
  2654. I915_WRITE(PF_WIN_SZ(pipe), 0);
  2655. ironlake_fdi_disable(crtc);
  2656. /* This is a horrible layering violation; we should be doing this in
  2657. * the connector/encoder ->prepare instead, but we don't always have
  2658. * enough information there about the config to know whether it will
  2659. * actually be necessary or just cause undesired flicker.
  2660. */
  2661. intel_disable_pch_ports(dev_priv, pipe);
  2662. intel_disable_transcoder(dev_priv, pipe);
  2663. if (HAS_PCH_CPT(dev)) {
  2664. /* disable TRANS_DP_CTL */
  2665. reg = TRANS_DP_CTL(pipe);
  2666. temp = I915_READ(reg);
  2667. temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
  2668. temp |= TRANS_DP_PORT_SEL_NONE;
  2669. I915_WRITE(reg, temp);
  2670. /* disable DPLL_SEL */
  2671. temp = I915_READ(PCH_DPLL_SEL);
  2672. switch (pipe) {
  2673. case 0:
  2674. temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
  2675. break;
  2676. case 1:
  2677. temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  2678. break;
  2679. case 2:
  2680. /* C shares PLL A or B */
  2681. temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
  2682. break;
  2683. default:
  2684. BUG(); /* wtf */
  2685. }
  2686. I915_WRITE(PCH_DPLL_SEL, temp);
  2687. }
  2688. /* disable PCH DPLL */
  2689. intel_disable_pch_pll(intel_crtc);
  2690. /* Switch from PCDclk to Rawclk */
  2691. reg = FDI_RX_CTL(pipe);
  2692. temp = I915_READ(reg);
  2693. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2694. /* Disable CPU FDI TX PLL */
  2695. reg = FDI_TX_CTL(pipe);
  2696. temp = I915_READ(reg);
  2697. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2698. POSTING_READ(reg);
  2699. udelay(100);
  2700. reg = FDI_RX_CTL(pipe);
  2701. temp = I915_READ(reg);
  2702. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2703. /* Wait for the clocks to turn off. */
  2704. POSTING_READ(reg);
  2705. udelay(100);
  2706. intel_crtc->active = false;
  2707. intel_update_watermarks(dev);
  2708. mutex_lock(&dev->struct_mutex);
  2709. intel_update_fbc(dev);
  2710. mutex_unlock(&dev->struct_mutex);
  2711. }
  2712. static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
  2713. {
  2714. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2715. int pipe = intel_crtc->pipe;
  2716. int plane = intel_crtc->plane;
  2717. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2718. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2719. */
  2720. switch (mode) {
  2721. case DRM_MODE_DPMS_ON:
  2722. case DRM_MODE_DPMS_STANDBY:
  2723. case DRM_MODE_DPMS_SUSPEND:
  2724. DRM_DEBUG_KMS("crtc %d/%d dpms on\n", pipe, plane);
  2725. ironlake_crtc_enable(crtc);
  2726. break;
  2727. case DRM_MODE_DPMS_OFF:
  2728. DRM_DEBUG_KMS("crtc %d/%d dpms off\n", pipe, plane);
  2729. ironlake_crtc_disable(crtc);
  2730. break;
  2731. }
  2732. }
  2733. static void ironlake_crtc_off(struct drm_crtc *crtc)
  2734. {
  2735. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2736. intel_put_pch_pll(intel_crtc);
  2737. }
  2738. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  2739. {
  2740. if (!enable && intel_crtc->overlay) {
  2741. struct drm_device *dev = intel_crtc->base.dev;
  2742. struct drm_i915_private *dev_priv = dev->dev_private;
  2743. mutex_lock(&dev->struct_mutex);
  2744. dev_priv->mm.interruptible = false;
  2745. (void) intel_overlay_switch_off(intel_crtc->overlay);
  2746. dev_priv->mm.interruptible = true;
  2747. mutex_unlock(&dev->struct_mutex);
  2748. }
  2749. /* Let userspace switch the overlay on again. In most cases userspace
  2750. * has to recompute where to put it anyway.
  2751. */
  2752. }
  2753. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  2754. {
  2755. struct drm_device *dev = crtc->dev;
  2756. struct drm_i915_private *dev_priv = dev->dev_private;
  2757. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2758. int pipe = intel_crtc->pipe;
  2759. int plane = intel_crtc->plane;
  2760. if (intel_crtc->active)
  2761. return;
  2762. intel_crtc->active = true;
  2763. intel_update_watermarks(dev);
  2764. intel_enable_pll(dev_priv, pipe);
  2765. intel_enable_pipe(dev_priv, pipe, false);
  2766. intel_enable_plane(dev_priv, plane, pipe);
  2767. intel_crtc_load_lut(crtc);
  2768. intel_update_fbc(dev);
  2769. /* Give the overlay scaler a chance to enable if it's on this pipe */
  2770. intel_crtc_dpms_overlay(intel_crtc, true);
  2771. intel_crtc_update_cursor(crtc, true);
  2772. }
  2773. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  2774. {
  2775. struct drm_device *dev = crtc->dev;
  2776. struct drm_i915_private *dev_priv = dev->dev_private;
  2777. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2778. int pipe = intel_crtc->pipe;
  2779. int plane = intel_crtc->plane;
  2780. if (!intel_crtc->active)
  2781. return;
  2782. /* Give the overlay scaler a chance to disable if it's on this pipe */
  2783. intel_crtc_wait_for_pending_flips(crtc);
  2784. drm_vblank_off(dev, pipe);
  2785. intel_crtc_dpms_overlay(intel_crtc, false);
  2786. intel_crtc_update_cursor(crtc, false);
  2787. if (dev_priv->cfb_plane == plane)
  2788. intel_disable_fbc(dev);
  2789. intel_disable_plane(dev_priv, plane, pipe);
  2790. intel_disable_pipe(dev_priv, pipe);
  2791. intel_disable_pll(dev_priv, pipe);
  2792. intel_crtc->active = false;
  2793. intel_update_fbc(dev);
  2794. intel_update_watermarks(dev);
  2795. }
  2796. static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
  2797. {
  2798. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2799. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2800. */
  2801. switch (mode) {
  2802. case DRM_MODE_DPMS_ON:
  2803. case DRM_MODE_DPMS_STANDBY:
  2804. case DRM_MODE_DPMS_SUSPEND:
  2805. i9xx_crtc_enable(crtc);
  2806. break;
  2807. case DRM_MODE_DPMS_OFF:
  2808. i9xx_crtc_disable(crtc);
  2809. break;
  2810. }
  2811. }
  2812. static void i9xx_crtc_off(struct drm_crtc *crtc)
  2813. {
  2814. }
  2815. /**
  2816. * Sets the power management mode of the pipe and plane.
  2817. */
  2818. static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
  2819. {
  2820. struct drm_device *dev = crtc->dev;
  2821. struct drm_i915_private *dev_priv = dev->dev_private;
  2822. struct drm_i915_master_private *master_priv;
  2823. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2824. int pipe = intel_crtc->pipe;
  2825. bool enabled;
  2826. if (intel_crtc->dpms_mode == mode)
  2827. return;
  2828. intel_crtc->dpms_mode = mode;
  2829. dev_priv->display.dpms(crtc, mode);
  2830. if (!dev->primary->master)
  2831. return;
  2832. master_priv = dev->primary->master->driver_priv;
  2833. if (!master_priv->sarea_priv)
  2834. return;
  2835. enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
  2836. switch (pipe) {
  2837. case 0:
  2838. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  2839. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  2840. break;
  2841. case 1:
  2842. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  2843. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  2844. break;
  2845. default:
  2846. DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
  2847. break;
  2848. }
  2849. }
  2850. static void intel_crtc_disable(struct drm_crtc *crtc)
  2851. {
  2852. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  2853. struct drm_device *dev = crtc->dev;
  2854. struct drm_i915_private *dev_priv = dev->dev_private;
  2855. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  2856. dev_priv->display.off(crtc);
  2857. assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
  2858. assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
  2859. if (crtc->fb) {
  2860. mutex_lock(&dev->struct_mutex);
  2861. intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
  2862. mutex_unlock(&dev->struct_mutex);
  2863. }
  2864. }
  2865. /* Prepare for a mode set.
  2866. *
  2867. * Note we could be a lot smarter here. We need to figure out which outputs
  2868. * will be enabled, which disabled (in short, how the config will changes)
  2869. * and perform the minimum necessary steps to accomplish that, e.g. updating
  2870. * watermarks, FBC configuration, making sure PLLs are programmed correctly,
  2871. * panel fitting is in the proper state, etc.
  2872. */
  2873. static void i9xx_crtc_prepare(struct drm_crtc *crtc)
  2874. {
  2875. i9xx_crtc_disable(crtc);
  2876. }
  2877. static void i9xx_crtc_commit(struct drm_crtc *crtc)
  2878. {
  2879. i9xx_crtc_enable(crtc);
  2880. }
  2881. static void ironlake_crtc_prepare(struct drm_crtc *crtc)
  2882. {
  2883. ironlake_crtc_disable(crtc);
  2884. }
  2885. static void ironlake_crtc_commit(struct drm_crtc *crtc)
  2886. {
  2887. ironlake_crtc_enable(crtc);
  2888. }
  2889. void intel_encoder_prepare(struct drm_encoder *encoder)
  2890. {
  2891. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2892. /* lvds has its own version of prepare see intel_lvds_prepare */
  2893. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  2894. }
  2895. void intel_encoder_commit(struct drm_encoder *encoder)
  2896. {
  2897. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2898. struct drm_device *dev = encoder->dev;
  2899. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
  2900. /* lvds has its own version of commit see intel_lvds_commit */
  2901. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  2902. if (HAS_PCH_CPT(dev))
  2903. intel_cpt_verify_modeset(dev, intel_crtc->pipe);
  2904. }
  2905. void intel_encoder_destroy(struct drm_encoder *encoder)
  2906. {
  2907. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  2908. drm_encoder_cleanup(encoder);
  2909. kfree(intel_encoder);
  2910. }
  2911. static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
  2912. struct drm_display_mode *mode,
  2913. struct drm_display_mode *adjusted_mode)
  2914. {
  2915. struct drm_device *dev = crtc->dev;
  2916. if (HAS_PCH_SPLIT(dev)) {
  2917. /* FDI link clock is fixed at 2.7G */
  2918. if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
  2919. return false;
  2920. }
  2921. /* All interlaced capable intel hw wants timings in frames. Note though
  2922. * that intel_lvds_mode_fixup does some funny tricks with the crtc
  2923. * timings, so we need to be careful not to clobber these.*/
  2924. if (!(adjusted_mode->private_flags & INTEL_MODE_CRTC_TIMINGS_SET))
  2925. drm_mode_set_crtcinfo(adjusted_mode, 0);
  2926. return true;
  2927. }
  2928. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  2929. {
  2930. return 400000; /* FIXME */
  2931. }
  2932. static int i945_get_display_clock_speed(struct drm_device *dev)
  2933. {
  2934. return 400000;
  2935. }
  2936. static int i915_get_display_clock_speed(struct drm_device *dev)
  2937. {
  2938. return 333000;
  2939. }
  2940. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  2941. {
  2942. return 200000;
  2943. }
  2944. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  2945. {
  2946. u16 gcfgc = 0;
  2947. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  2948. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  2949. return 133000;
  2950. else {
  2951. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  2952. case GC_DISPLAY_CLOCK_333_MHZ:
  2953. return 333000;
  2954. default:
  2955. case GC_DISPLAY_CLOCK_190_200_MHZ:
  2956. return 190000;
  2957. }
  2958. }
  2959. }
  2960. static int i865_get_display_clock_speed(struct drm_device *dev)
  2961. {
  2962. return 266000;
  2963. }
  2964. static int i855_get_display_clock_speed(struct drm_device *dev)
  2965. {
  2966. u16 hpllcc = 0;
  2967. /* Assume that the hardware is in the high speed state. This
  2968. * should be the default.
  2969. */
  2970. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  2971. case GC_CLOCK_133_200:
  2972. case GC_CLOCK_100_200:
  2973. return 200000;
  2974. case GC_CLOCK_166_250:
  2975. return 250000;
  2976. case GC_CLOCK_100_133:
  2977. return 133000;
  2978. }
  2979. /* Shouldn't happen */
  2980. return 0;
  2981. }
  2982. static int i830_get_display_clock_speed(struct drm_device *dev)
  2983. {
  2984. return 133000;
  2985. }
  2986. struct fdi_m_n {
  2987. u32 tu;
  2988. u32 gmch_m;
  2989. u32 gmch_n;
  2990. u32 link_m;
  2991. u32 link_n;
  2992. };
  2993. static void
  2994. fdi_reduce_ratio(u32 *num, u32 *den)
  2995. {
  2996. while (*num > 0xffffff || *den > 0xffffff) {
  2997. *num >>= 1;
  2998. *den >>= 1;
  2999. }
  3000. }
  3001. static void
  3002. ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
  3003. int link_clock, struct fdi_m_n *m_n)
  3004. {
  3005. m_n->tu = 64; /* default size */
  3006. /* BUG_ON(pixel_clock > INT_MAX / 36); */
  3007. m_n->gmch_m = bits_per_pixel * pixel_clock;
  3008. m_n->gmch_n = link_clock * nlanes * 8;
  3009. fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
  3010. m_n->link_m = pixel_clock;
  3011. m_n->link_n = link_clock;
  3012. fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
  3013. }
  3014. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  3015. {
  3016. if (i915_panel_use_ssc >= 0)
  3017. return i915_panel_use_ssc != 0;
  3018. return dev_priv->lvds_use_ssc
  3019. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  3020. }
  3021. /**
  3022. * intel_choose_pipe_bpp_dither - figure out what color depth the pipe should send
  3023. * @crtc: CRTC structure
  3024. * @mode: requested mode
  3025. *
  3026. * A pipe may be connected to one or more outputs. Based on the depth of the
  3027. * attached framebuffer, choose a good color depth to use on the pipe.
  3028. *
  3029. * If possible, match the pipe depth to the fb depth. In some cases, this
  3030. * isn't ideal, because the connected output supports a lesser or restricted
  3031. * set of depths. Resolve that here:
  3032. * LVDS typically supports only 6bpc, so clamp down in that case
  3033. * HDMI supports only 8bpc or 12bpc, so clamp to 8bpc with dither for 10bpc
  3034. * Displays may support a restricted set as well, check EDID and clamp as
  3035. * appropriate.
  3036. * DP may want to dither down to 6bpc to fit larger modes
  3037. *
  3038. * RETURNS:
  3039. * Dithering requirement (i.e. false if display bpc and pipe bpc match,
  3040. * true if they don't match).
  3041. */
  3042. static bool intel_choose_pipe_bpp_dither(struct drm_crtc *crtc,
  3043. unsigned int *pipe_bpp,
  3044. struct drm_display_mode *mode)
  3045. {
  3046. struct drm_device *dev = crtc->dev;
  3047. struct drm_i915_private *dev_priv = dev->dev_private;
  3048. struct drm_encoder *encoder;
  3049. struct drm_connector *connector;
  3050. unsigned int display_bpc = UINT_MAX, bpc;
  3051. /* Walk the encoders & connectors on this crtc, get min bpc */
  3052. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  3053. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  3054. if (encoder->crtc != crtc)
  3055. continue;
  3056. if (intel_encoder->type == INTEL_OUTPUT_LVDS) {
  3057. unsigned int lvds_bpc;
  3058. if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) ==
  3059. LVDS_A3_POWER_UP)
  3060. lvds_bpc = 8;
  3061. else
  3062. lvds_bpc = 6;
  3063. if (lvds_bpc < display_bpc) {
  3064. DRM_DEBUG_KMS("clamping display bpc (was %d) to LVDS (%d)\n", display_bpc, lvds_bpc);
  3065. display_bpc = lvds_bpc;
  3066. }
  3067. continue;
  3068. }
  3069. if (intel_encoder->type == INTEL_OUTPUT_EDP) {
  3070. /* Use VBT settings if we have an eDP panel */
  3071. unsigned int edp_bpc = dev_priv->edp.bpp / 3;
  3072. if (edp_bpc < display_bpc) {
  3073. DRM_DEBUG_KMS("clamping display bpc (was %d) to eDP (%d)\n", display_bpc, edp_bpc);
  3074. display_bpc = edp_bpc;
  3075. }
  3076. continue;
  3077. }
  3078. /* Not one of the known troublemakers, check the EDID */
  3079. list_for_each_entry(connector, &dev->mode_config.connector_list,
  3080. head) {
  3081. if (connector->encoder != encoder)
  3082. continue;
  3083. /* Don't use an invalid EDID bpc value */
  3084. if (connector->display_info.bpc &&
  3085. connector->display_info.bpc < display_bpc) {
  3086. DRM_DEBUG_KMS("clamping display bpc (was %d) to EDID reported max of %d\n", display_bpc, connector->display_info.bpc);
  3087. display_bpc = connector->display_info.bpc;
  3088. }
  3089. }
  3090. /*
  3091. * HDMI is either 12 or 8, so if the display lets 10bpc sneak
  3092. * through, clamp it down. (Note: >12bpc will be caught below.)
  3093. */
  3094. if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
  3095. if (display_bpc > 8 && display_bpc < 12) {
  3096. DRM_DEBUG_KMS("forcing bpc to 12 for HDMI\n");
  3097. display_bpc = 12;
  3098. } else {
  3099. DRM_DEBUG_KMS("forcing bpc to 8 for HDMI\n");
  3100. display_bpc = 8;
  3101. }
  3102. }
  3103. }
  3104. if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
  3105. DRM_DEBUG_KMS("Dithering DP to 6bpc\n");
  3106. display_bpc = 6;
  3107. }
  3108. /*
  3109. * We could just drive the pipe at the highest bpc all the time and
  3110. * enable dithering as needed, but that costs bandwidth. So choose
  3111. * the minimum value that expresses the full color range of the fb but
  3112. * also stays within the max display bpc discovered above.
  3113. */
  3114. switch (crtc->fb->depth) {
  3115. case 8:
  3116. bpc = 8; /* since we go through a colormap */
  3117. break;
  3118. case 15:
  3119. case 16:
  3120. bpc = 6; /* min is 18bpp */
  3121. break;
  3122. case 24:
  3123. bpc = 8;
  3124. break;
  3125. case 30:
  3126. bpc = 10;
  3127. break;
  3128. case 48:
  3129. bpc = 12;
  3130. break;
  3131. default:
  3132. DRM_DEBUG("unsupported depth, assuming 24 bits\n");
  3133. bpc = min((unsigned int)8, display_bpc);
  3134. break;
  3135. }
  3136. display_bpc = min(display_bpc, bpc);
  3137. DRM_DEBUG_KMS("setting pipe bpc to %d (max display bpc %d)\n",
  3138. bpc, display_bpc);
  3139. *pipe_bpp = display_bpc * 3;
  3140. return display_bpc != bpc;
  3141. }
  3142. static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
  3143. {
  3144. struct drm_device *dev = crtc->dev;
  3145. struct drm_i915_private *dev_priv = dev->dev_private;
  3146. int refclk;
  3147. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  3148. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  3149. refclk = dev_priv->lvds_ssc_freq * 1000;
  3150. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  3151. refclk / 1000);
  3152. } else if (!IS_GEN2(dev)) {
  3153. refclk = 96000;
  3154. } else {
  3155. refclk = 48000;
  3156. }
  3157. return refclk;
  3158. }
  3159. static void i9xx_adjust_sdvo_tv_clock(struct drm_display_mode *adjusted_mode,
  3160. intel_clock_t *clock)
  3161. {
  3162. /* SDVO TV has fixed PLL values depend on its clock range,
  3163. this mirrors vbios setting. */
  3164. if (adjusted_mode->clock >= 100000
  3165. && adjusted_mode->clock < 140500) {
  3166. clock->p1 = 2;
  3167. clock->p2 = 10;
  3168. clock->n = 3;
  3169. clock->m1 = 16;
  3170. clock->m2 = 8;
  3171. } else if (adjusted_mode->clock >= 140500
  3172. && adjusted_mode->clock <= 200000) {
  3173. clock->p1 = 1;
  3174. clock->p2 = 10;
  3175. clock->n = 6;
  3176. clock->m1 = 12;
  3177. clock->m2 = 8;
  3178. }
  3179. }
  3180. static void i9xx_update_pll_dividers(struct drm_crtc *crtc,
  3181. intel_clock_t *clock,
  3182. intel_clock_t *reduced_clock)
  3183. {
  3184. struct drm_device *dev = crtc->dev;
  3185. struct drm_i915_private *dev_priv = dev->dev_private;
  3186. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3187. int pipe = intel_crtc->pipe;
  3188. u32 fp, fp2 = 0;
  3189. if (IS_PINEVIEW(dev)) {
  3190. fp = (1 << clock->n) << 16 | clock->m1 << 8 | clock->m2;
  3191. if (reduced_clock)
  3192. fp2 = (1 << reduced_clock->n) << 16 |
  3193. reduced_clock->m1 << 8 | reduced_clock->m2;
  3194. } else {
  3195. fp = clock->n << 16 | clock->m1 << 8 | clock->m2;
  3196. if (reduced_clock)
  3197. fp2 = reduced_clock->n << 16 | reduced_clock->m1 << 8 |
  3198. reduced_clock->m2;
  3199. }
  3200. I915_WRITE(FP0(pipe), fp);
  3201. intel_crtc->lowfreq_avail = false;
  3202. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  3203. reduced_clock && i915_powersave) {
  3204. I915_WRITE(FP1(pipe), fp2);
  3205. intel_crtc->lowfreq_avail = true;
  3206. } else {
  3207. I915_WRITE(FP1(pipe), fp);
  3208. }
  3209. }
  3210. static void intel_update_lvds(struct drm_crtc *crtc, intel_clock_t *clock,
  3211. struct drm_display_mode *adjusted_mode)
  3212. {
  3213. struct drm_device *dev = crtc->dev;
  3214. struct drm_i915_private *dev_priv = dev->dev_private;
  3215. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3216. int pipe = intel_crtc->pipe;
  3217. u32 temp;
  3218. temp = I915_READ(LVDS);
  3219. temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  3220. if (pipe == 1) {
  3221. temp |= LVDS_PIPEB_SELECT;
  3222. } else {
  3223. temp &= ~LVDS_PIPEB_SELECT;
  3224. }
  3225. /* set the corresponsding LVDS_BORDER bit */
  3226. temp |= dev_priv->lvds_border_bits;
  3227. /* Set the B0-B3 data pairs corresponding to whether we're going to
  3228. * set the DPLLs for dual-channel mode or not.
  3229. */
  3230. if (clock->p2 == 7)
  3231. temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  3232. else
  3233. temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  3234. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  3235. * appropriately here, but we need to look more thoroughly into how
  3236. * panels behave in the two modes.
  3237. */
  3238. /* set the dithering flag on LVDS as needed */
  3239. if (INTEL_INFO(dev)->gen >= 4) {
  3240. if (dev_priv->lvds_dither)
  3241. temp |= LVDS_ENABLE_DITHER;
  3242. else
  3243. temp &= ~LVDS_ENABLE_DITHER;
  3244. }
  3245. temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
  3246. if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
  3247. temp |= LVDS_HSYNC_POLARITY;
  3248. if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
  3249. temp |= LVDS_VSYNC_POLARITY;
  3250. I915_WRITE(LVDS, temp);
  3251. }
  3252. static void i9xx_update_pll(struct drm_crtc *crtc,
  3253. struct drm_display_mode *mode,
  3254. struct drm_display_mode *adjusted_mode,
  3255. intel_clock_t *clock, intel_clock_t *reduced_clock,
  3256. int num_connectors)
  3257. {
  3258. struct drm_device *dev = crtc->dev;
  3259. struct drm_i915_private *dev_priv = dev->dev_private;
  3260. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3261. int pipe = intel_crtc->pipe;
  3262. u32 dpll;
  3263. bool is_sdvo;
  3264. is_sdvo = intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO) ||
  3265. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
  3266. dpll = DPLL_VGA_MODE_DIS;
  3267. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  3268. dpll |= DPLLB_MODE_LVDS;
  3269. else
  3270. dpll |= DPLLB_MODE_DAC_SERIAL;
  3271. if (is_sdvo) {
  3272. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  3273. if (pixel_multiplier > 1) {
  3274. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  3275. dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
  3276. }
  3277. dpll |= DPLL_DVO_HIGH_SPEED;
  3278. }
  3279. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
  3280. dpll |= DPLL_DVO_HIGH_SPEED;
  3281. /* compute bitmask from p1 value */
  3282. if (IS_PINEVIEW(dev))
  3283. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  3284. else {
  3285. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3286. if (IS_G4X(dev) && reduced_clock)
  3287. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  3288. }
  3289. switch (clock->p2) {
  3290. case 5:
  3291. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  3292. break;
  3293. case 7:
  3294. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  3295. break;
  3296. case 10:
  3297. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  3298. break;
  3299. case 14:
  3300. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  3301. break;
  3302. }
  3303. if (INTEL_INFO(dev)->gen >= 4)
  3304. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  3305. if (is_sdvo && intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
  3306. dpll |= PLL_REF_INPUT_TVCLKINBC;
  3307. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
  3308. /* XXX: just matching BIOS for now */
  3309. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  3310. dpll |= 3;
  3311. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  3312. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  3313. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  3314. else
  3315. dpll |= PLL_REF_INPUT_DREFCLK;
  3316. dpll |= DPLL_VCO_ENABLE;
  3317. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
  3318. POSTING_READ(DPLL(pipe));
  3319. udelay(150);
  3320. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  3321. * This is an exception to the general rule that mode_set doesn't turn
  3322. * things on.
  3323. */
  3324. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  3325. intel_update_lvds(crtc, clock, adjusted_mode);
  3326. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
  3327. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  3328. I915_WRITE(DPLL(pipe), dpll);
  3329. /* Wait for the clocks to stabilize. */
  3330. POSTING_READ(DPLL(pipe));
  3331. udelay(150);
  3332. if (INTEL_INFO(dev)->gen >= 4) {
  3333. u32 temp = 0;
  3334. if (is_sdvo) {
  3335. temp = intel_mode_get_pixel_multiplier(adjusted_mode);
  3336. if (temp > 1)
  3337. temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  3338. else
  3339. temp = 0;
  3340. }
  3341. I915_WRITE(DPLL_MD(pipe), temp);
  3342. } else {
  3343. /* The pixel multiplier can only be updated once the
  3344. * DPLL is enabled and the clocks are stable.
  3345. *
  3346. * So write it again.
  3347. */
  3348. I915_WRITE(DPLL(pipe), dpll);
  3349. }
  3350. }
  3351. static void i8xx_update_pll(struct drm_crtc *crtc,
  3352. struct drm_display_mode *adjusted_mode,
  3353. intel_clock_t *clock,
  3354. int num_connectors)
  3355. {
  3356. struct drm_device *dev = crtc->dev;
  3357. struct drm_i915_private *dev_priv = dev->dev_private;
  3358. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3359. int pipe = intel_crtc->pipe;
  3360. u32 dpll;
  3361. dpll = DPLL_VGA_MODE_DIS;
  3362. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  3363. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3364. } else {
  3365. if (clock->p1 == 2)
  3366. dpll |= PLL_P1_DIVIDE_BY_TWO;
  3367. else
  3368. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3369. if (clock->p2 == 4)
  3370. dpll |= PLL_P2_DIVIDE_BY_4;
  3371. }
  3372. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
  3373. /* XXX: just matching BIOS for now */
  3374. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  3375. dpll |= 3;
  3376. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  3377. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  3378. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  3379. else
  3380. dpll |= PLL_REF_INPUT_DREFCLK;
  3381. dpll |= DPLL_VCO_ENABLE;
  3382. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
  3383. POSTING_READ(DPLL(pipe));
  3384. udelay(150);
  3385. I915_WRITE(DPLL(pipe), dpll);
  3386. /* Wait for the clocks to stabilize. */
  3387. POSTING_READ(DPLL(pipe));
  3388. udelay(150);
  3389. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  3390. * This is an exception to the general rule that mode_set doesn't turn
  3391. * things on.
  3392. */
  3393. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  3394. intel_update_lvds(crtc, clock, adjusted_mode);
  3395. /* The pixel multiplier can only be updated once the
  3396. * DPLL is enabled and the clocks are stable.
  3397. *
  3398. * So write it again.
  3399. */
  3400. I915_WRITE(DPLL(pipe), dpll);
  3401. }
  3402. static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
  3403. struct drm_display_mode *mode,
  3404. struct drm_display_mode *adjusted_mode,
  3405. int x, int y,
  3406. struct drm_framebuffer *old_fb)
  3407. {
  3408. struct drm_device *dev = crtc->dev;
  3409. struct drm_i915_private *dev_priv = dev->dev_private;
  3410. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3411. int pipe = intel_crtc->pipe;
  3412. int plane = intel_crtc->plane;
  3413. int refclk, num_connectors = 0;
  3414. intel_clock_t clock, reduced_clock;
  3415. u32 dspcntr, pipeconf, vsyncshift;
  3416. bool ok, has_reduced_clock = false, is_sdvo = false;
  3417. bool is_lvds = false, is_tv = false, is_dp = false;
  3418. struct drm_mode_config *mode_config = &dev->mode_config;
  3419. struct intel_encoder *encoder;
  3420. const intel_limit_t *limit;
  3421. int ret;
  3422. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  3423. if (encoder->base.crtc != crtc)
  3424. continue;
  3425. switch (encoder->type) {
  3426. case INTEL_OUTPUT_LVDS:
  3427. is_lvds = true;
  3428. break;
  3429. case INTEL_OUTPUT_SDVO:
  3430. case INTEL_OUTPUT_HDMI:
  3431. is_sdvo = true;
  3432. if (encoder->needs_tv_clock)
  3433. is_tv = true;
  3434. break;
  3435. case INTEL_OUTPUT_TVOUT:
  3436. is_tv = true;
  3437. break;
  3438. case INTEL_OUTPUT_DISPLAYPORT:
  3439. is_dp = true;
  3440. break;
  3441. }
  3442. num_connectors++;
  3443. }
  3444. refclk = i9xx_get_refclk(crtc, num_connectors);
  3445. /*
  3446. * Returns a set of divisors for the desired target clock with the given
  3447. * refclk, or FALSE. The returned values represent the clock equation:
  3448. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  3449. */
  3450. limit = intel_limit(crtc, refclk);
  3451. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
  3452. &clock);
  3453. if (!ok) {
  3454. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  3455. return -EINVAL;
  3456. }
  3457. /* Ensure that the cursor is valid for the new mode before changing... */
  3458. intel_crtc_update_cursor(crtc, true);
  3459. if (is_lvds && dev_priv->lvds_downclock_avail) {
  3460. /*
  3461. * Ensure we match the reduced clock's P to the target clock.
  3462. * If the clocks don't match, we can't switch the display clock
  3463. * by using the FP0/FP1. In such case we will disable the LVDS
  3464. * downclock feature.
  3465. */
  3466. has_reduced_clock = limit->find_pll(limit, crtc,
  3467. dev_priv->lvds_downclock,
  3468. refclk,
  3469. &clock,
  3470. &reduced_clock);
  3471. }
  3472. if (is_sdvo && is_tv)
  3473. i9xx_adjust_sdvo_tv_clock(adjusted_mode, &clock);
  3474. i9xx_update_pll_dividers(crtc, &clock, has_reduced_clock ?
  3475. &reduced_clock : NULL);
  3476. if (IS_GEN2(dev))
  3477. i8xx_update_pll(crtc, adjusted_mode, &clock, num_connectors);
  3478. else
  3479. i9xx_update_pll(crtc, mode, adjusted_mode, &clock,
  3480. has_reduced_clock ? &reduced_clock : NULL,
  3481. num_connectors);
  3482. /* setup pipeconf */
  3483. pipeconf = I915_READ(PIPECONF(pipe));
  3484. /* Set up the display plane register */
  3485. dspcntr = DISPPLANE_GAMMA_ENABLE;
  3486. if (pipe == 0)
  3487. dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
  3488. else
  3489. dspcntr |= DISPPLANE_SEL_PIPE_B;
  3490. if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
  3491. /* Enable pixel doubling when the dot clock is > 90% of the (display)
  3492. * core speed.
  3493. *
  3494. * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
  3495. * pipe == 0 check?
  3496. */
  3497. if (mode->clock >
  3498. dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
  3499. pipeconf |= PIPECONF_DOUBLE_WIDE;
  3500. else
  3501. pipeconf &= ~PIPECONF_DOUBLE_WIDE;
  3502. }
  3503. /* default to 8bpc */
  3504. pipeconf &= ~(PIPECONF_BPP_MASK | PIPECONF_DITHER_EN);
  3505. if (is_dp) {
  3506. if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
  3507. pipeconf |= PIPECONF_BPP_6 |
  3508. PIPECONF_DITHER_EN |
  3509. PIPECONF_DITHER_TYPE_SP;
  3510. }
  3511. }
  3512. DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  3513. drm_mode_debug_printmodeline(mode);
  3514. if (HAS_PIPE_CXSR(dev)) {
  3515. if (intel_crtc->lowfreq_avail) {
  3516. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  3517. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  3518. } else {
  3519. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  3520. pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
  3521. }
  3522. }
  3523. pipeconf &= ~PIPECONF_INTERLACE_MASK;
  3524. if (!IS_GEN2(dev) &&
  3525. adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  3526. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  3527. /* the chip adds 2 halflines automatically */
  3528. adjusted_mode->crtc_vtotal -= 1;
  3529. adjusted_mode->crtc_vblank_end -= 1;
  3530. vsyncshift = adjusted_mode->crtc_hsync_start
  3531. - adjusted_mode->crtc_htotal/2;
  3532. } else {
  3533. pipeconf |= PIPECONF_PROGRESSIVE;
  3534. vsyncshift = 0;
  3535. }
  3536. if (!IS_GEN3(dev))
  3537. I915_WRITE(VSYNCSHIFT(pipe), vsyncshift);
  3538. I915_WRITE(HTOTAL(pipe),
  3539. (adjusted_mode->crtc_hdisplay - 1) |
  3540. ((adjusted_mode->crtc_htotal - 1) << 16));
  3541. I915_WRITE(HBLANK(pipe),
  3542. (adjusted_mode->crtc_hblank_start - 1) |
  3543. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  3544. I915_WRITE(HSYNC(pipe),
  3545. (adjusted_mode->crtc_hsync_start - 1) |
  3546. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  3547. I915_WRITE(VTOTAL(pipe),
  3548. (adjusted_mode->crtc_vdisplay - 1) |
  3549. ((adjusted_mode->crtc_vtotal - 1) << 16));
  3550. I915_WRITE(VBLANK(pipe),
  3551. (adjusted_mode->crtc_vblank_start - 1) |
  3552. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  3553. I915_WRITE(VSYNC(pipe),
  3554. (adjusted_mode->crtc_vsync_start - 1) |
  3555. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  3556. /* pipesrc and dspsize control the size that is scaled from,
  3557. * which should always be the user's requested size.
  3558. */
  3559. I915_WRITE(DSPSIZE(plane),
  3560. ((mode->vdisplay - 1) << 16) |
  3561. (mode->hdisplay - 1));
  3562. I915_WRITE(DSPPOS(plane), 0);
  3563. I915_WRITE(PIPESRC(pipe),
  3564. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  3565. I915_WRITE(PIPECONF(pipe), pipeconf);
  3566. POSTING_READ(PIPECONF(pipe));
  3567. intel_enable_pipe(dev_priv, pipe, false);
  3568. intel_wait_for_vblank(dev, pipe);
  3569. I915_WRITE(DSPCNTR(plane), dspcntr);
  3570. POSTING_READ(DSPCNTR(plane));
  3571. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  3572. intel_update_watermarks(dev);
  3573. return ret;
  3574. }
  3575. /*
  3576. * Initialize reference clocks when the driver loads
  3577. */
  3578. void ironlake_init_pch_refclk(struct drm_device *dev)
  3579. {
  3580. struct drm_i915_private *dev_priv = dev->dev_private;
  3581. struct drm_mode_config *mode_config = &dev->mode_config;
  3582. struct intel_encoder *encoder;
  3583. u32 temp;
  3584. bool has_lvds = false;
  3585. bool has_cpu_edp = false;
  3586. bool has_pch_edp = false;
  3587. bool has_panel = false;
  3588. bool has_ck505 = false;
  3589. bool can_ssc = false;
  3590. /* We need to take the global config into account */
  3591. list_for_each_entry(encoder, &mode_config->encoder_list,
  3592. base.head) {
  3593. switch (encoder->type) {
  3594. case INTEL_OUTPUT_LVDS:
  3595. has_panel = true;
  3596. has_lvds = true;
  3597. break;
  3598. case INTEL_OUTPUT_EDP:
  3599. has_panel = true;
  3600. if (intel_encoder_is_pch_edp(&encoder->base))
  3601. has_pch_edp = true;
  3602. else
  3603. has_cpu_edp = true;
  3604. break;
  3605. }
  3606. }
  3607. if (HAS_PCH_IBX(dev)) {
  3608. has_ck505 = dev_priv->display_clock_mode;
  3609. can_ssc = has_ck505;
  3610. } else {
  3611. has_ck505 = false;
  3612. can_ssc = true;
  3613. }
  3614. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_pch_edp %d has_cpu_edp %d has_ck505 %d\n",
  3615. has_panel, has_lvds, has_pch_edp, has_cpu_edp,
  3616. has_ck505);
  3617. /* Ironlake: try to setup display ref clock before DPLL
  3618. * enabling. This is only under driver's control after
  3619. * PCH B stepping, previous chipset stepping should be
  3620. * ignoring this setting.
  3621. */
  3622. temp = I915_READ(PCH_DREF_CONTROL);
  3623. /* Always enable nonspread source */
  3624. temp &= ~DREF_NONSPREAD_SOURCE_MASK;
  3625. if (has_ck505)
  3626. temp |= DREF_NONSPREAD_CK505_ENABLE;
  3627. else
  3628. temp |= DREF_NONSPREAD_SOURCE_ENABLE;
  3629. if (has_panel) {
  3630. temp &= ~DREF_SSC_SOURCE_MASK;
  3631. temp |= DREF_SSC_SOURCE_ENABLE;
  3632. /* SSC must be turned on before enabling the CPU output */
  3633. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  3634. DRM_DEBUG_KMS("Using SSC on panel\n");
  3635. temp |= DREF_SSC1_ENABLE;
  3636. } else
  3637. temp &= ~DREF_SSC1_ENABLE;
  3638. /* Get SSC going before enabling the outputs */
  3639. I915_WRITE(PCH_DREF_CONTROL, temp);
  3640. POSTING_READ(PCH_DREF_CONTROL);
  3641. udelay(200);
  3642. temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  3643. /* Enable CPU source on CPU attached eDP */
  3644. if (has_cpu_edp) {
  3645. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  3646. DRM_DEBUG_KMS("Using SSC on eDP\n");
  3647. temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  3648. }
  3649. else
  3650. temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  3651. } else
  3652. temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  3653. I915_WRITE(PCH_DREF_CONTROL, temp);
  3654. POSTING_READ(PCH_DREF_CONTROL);
  3655. udelay(200);
  3656. } else {
  3657. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  3658. temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  3659. /* Turn off CPU output */
  3660. temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  3661. I915_WRITE(PCH_DREF_CONTROL, temp);
  3662. POSTING_READ(PCH_DREF_CONTROL);
  3663. udelay(200);
  3664. /* Turn off the SSC source */
  3665. temp &= ~DREF_SSC_SOURCE_MASK;
  3666. temp |= DREF_SSC_SOURCE_DISABLE;
  3667. /* Turn off SSC1 */
  3668. temp &= ~ DREF_SSC1_ENABLE;
  3669. I915_WRITE(PCH_DREF_CONTROL, temp);
  3670. POSTING_READ(PCH_DREF_CONTROL);
  3671. udelay(200);
  3672. }
  3673. }
  3674. static int ironlake_get_refclk(struct drm_crtc *crtc)
  3675. {
  3676. struct drm_device *dev = crtc->dev;
  3677. struct drm_i915_private *dev_priv = dev->dev_private;
  3678. struct intel_encoder *encoder;
  3679. struct drm_mode_config *mode_config = &dev->mode_config;
  3680. struct intel_encoder *edp_encoder = NULL;
  3681. int num_connectors = 0;
  3682. bool is_lvds = false;
  3683. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  3684. if (encoder->base.crtc != crtc)
  3685. continue;
  3686. switch (encoder->type) {
  3687. case INTEL_OUTPUT_LVDS:
  3688. is_lvds = true;
  3689. break;
  3690. case INTEL_OUTPUT_EDP:
  3691. edp_encoder = encoder;
  3692. break;
  3693. }
  3694. num_connectors++;
  3695. }
  3696. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  3697. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  3698. dev_priv->lvds_ssc_freq);
  3699. return dev_priv->lvds_ssc_freq * 1000;
  3700. }
  3701. return 120000;
  3702. }
  3703. static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
  3704. struct drm_display_mode *mode,
  3705. struct drm_display_mode *adjusted_mode,
  3706. int x, int y,
  3707. struct drm_framebuffer *old_fb)
  3708. {
  3709. struct drm_device *dev = crtc->dev;
  3710. struct drm_i915_private *dev_priv = dev->dev_private;
  3711. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3712. int pipe = intel_crtc->pipe;
  3713. int plane = intel_crtc->plane;
  3714. int refclk, num_connectors = 0;
  3715. intel_clock_t clock, reduced_clock;
  3716. u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
  3717. bool ok, has_reduced_clock = false, is_sdvo = false;
  3718. bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
  3719. struct drm_mode_config *mode_config = &dev->mode_config;
  3720. struct intel_encoder *encoder, *edp_encoder = NULL;
  3721. const intel_limit_t *limit;
  3722. int ret;
  3723. struct fdi_m_n m_n = {0};
  3724. u32 temp;
  3725. int target_clock, pixel_multiplier, lane, link_bw, factor;
  3726. unsigned int pipe_bpp;
  3727. bool dither;
  3728. bool is_cpu_edp = false, is_pch_edp = false;
  3729. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  3730. if (encoder->base.crtc != crtc)
  3731. continue;
  3732. switch (encoder->type) {
  3733. case INTEL_OUTPUT_LVDS:
  3734. is_lvds = true;
  3735. break;
  3736. case INTEL_OUTPUT_SDVO:
  3737. case INTEL_OUTPUT_HDMI:
  3738. is_sdvo = true;
  3739. if (encoder->needs_tv_clock)
  3740. is_tv = true;
  3741. break;
  3742. case INTEL_OUTPUT_TVOUT:
  3743. is_tv = true;
  3744. break;
  3745. case INTEL_OUTPUT_ANALOG:
  3746. is_crt = true;
  3747. break;
  3748. case INTEL_OUTPUT_DISPLAYPORT:
  3749. is_dp = true;
  3750. break;
  3751. case INTEL_OUTPUT_EDP:
  3752. is_dp = true;
  3753. if (intel_encoder_is_pch_edp(&encoder->base))
  3754. is_pch_edp = true;
  3755. else
  3756. is_cpu_edp = true;
  3757. edp_encoder = encoder;
  3758. break;
  3759. }
  3760. num_connectors++;
  3761. }
  3762. refclk = ironlake_get_refclk(crtc);
  3763. /*
  3764. * Returns a set of divisors for the desired target clock with the given
  3765. * refclk, or FALSE. The returned values represent the clock equation:
  3766. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  3767. */
  3768. limit = intel_limit(crtc, refclk);
  3769. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
  3770. &clock);
  3771. if (!ok) {
  3772. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  3773. return -EINVAL;
  3774. }
  3775. /* Ensure that the cursor is valid for the new mode before changing... */
  3776. intel_crtc_update_cursor(crtc, true);
  3777. if (is_lvds && dev_priv->lvds_downclock_avail) {
  3778. /*
  3779. * Ensure we match the reduced clock's P to the target clock.
  3780. * If the clocks don't match, we can't switch the display clock
  3781. * by using the FP0/FP1. In such case we will disable the LVDS
  3782. * downclock feature.
  3783. */
  3784. has_reduced_clock = limit->find_pll(limit, crtc,
  3785. dev_priv->lvds_downclock,
  3786. refclk,
  3787. &clock,
  3788. &reduced_clock);
  3789. }
  3790. /* SDVO TV has fixed PLL values depend on its clock range,
  3791. this mirrors vbios setting. */
  3792. if (is_sdvo && is_tv) {
  3793. if (adjusted_mode->clock >= 100000
  3794. && adjusted_mode->clock < 140500) {
  3795. clock.p1 = 2;
  3796. clock.p2 = 10;
  3797. clock.n = 3;
  3798. clock.m1 = 16;
  3799. clock.m2 = 8;
  3800. } else if (adjusted_mode->clock >= 140500
  3801. && adjusted_mode->clock <= 200000) {
  3802. clock.p1 = 1;
  3803. clock.p2 = 10;
  3804. clock.n = 6;
  3805. clock.m1 = 12;
  3806. clock.m2 = 8;
  3807. }
  3808. }
  3809. /* FDI link */
  3810. pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  3811. lane = 0;
  3812. /* CPU eDP doesn't require FDI link, so just set DP M/N
  3813. according to current link config */
  3814. if (is_cpu_edp) {
  3815. target_clock = mode->clock;
  3816. intel_edp_link_config(edp_encoder, &lane, &link_bw);
  3817. } else {
  3818. /* [e]DP over FDI requires target mode clock
  3819. instead of link clock */
  3820. if (is_dp)
  3821. target_clock = mode->clock;
  3822. else
  3823. target_clock = adjusted_mode->clock;
  3824. /* FDI is a binary signal running at ~2.7GHz, encoding
  3825. * each output octet as 10 bits. The actual frequency
  3826. * is stored as a divider into a 100MHz clock, and the
  3827. * mode pixel clock is stored in units of 1KHz.
  3828. * Hence the bw of each lane in terms of the mode signal
  3829. * is:
  3830. */
  3831. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  3832. }
  3833. /* determine panel color depth */
  3834. temp = I915_READ(PIPECONF(pipe));
  3835. temp &= ~PIPE_BPC_MASK;
  3836. dither = intel_choose_pipe_bpp_dither(crtc, &pipe_bpp, mode);
  3837. switch (pipe_bpp) {
  3838. case 18:
  3839. temp |= PIPE_6BPC;
  3840. break;
  3841. case 24:
  3842. temp |= PIPE_8BPC;
  3843. break;
  3844. case 30:
  3845. temp |= PIPE_10BPC;
  3846. break;
  3847. case 36:
  3848. temp |= PIPE_12BPC;
  3849. break;
  3850. default:
  3851. WARN(1, "intel_choose_pipe_bpp returned invalid value %d\n",
  3852. pipe_bpp);
  3853. temp |= PIPE_8BPC;
  3854. pipe_bpp = 24;
  3855. break;
  3856. }
  3857. intel_crtc->bpp = pipe_bpp;
  3858. I915_WRITE(PIPECONF(pipe), temp);
  3859. if (!lane) {
  3860. /*
  3861. * Account for spread spectrum to avoid
  3862. * oversubscribing the link. Max center spread
  3863. * is 2.5%; use 5% for safety's sake.
  3864. */
  3865. u32 bps = target_clock * intel_crtc->bpp * 21 / 20;
  3866. lane = bps / (link_bw * 8) + 1;
  3867. }
  3868. intel_crtc->fdi_lanes = lane;
  3869. if (pixel_multiplier > 1)
  3870. link_bw *= pixel_multiplier;
  3871. ironlake_compute_m_n(intel_crtc->bpp, lane, target_clock, link_bw,
  3872. &m_n);
  3873. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  3874. if (has_reduced_clock)
  3875. fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
  3876. reduced_clock.m2;
  3877. /* Enable autotuning of the PLL clock (if permissible) */
  3878. factor = 21;
  3879. if (is_lvds) {
  3880. if ((intel_panel_use_ssc(dev_priv) &&
  3881. dev_priv->lvds_ssc_freq == 100) ||
  3882. (I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP)
  3883. factor = 25;
  3884. } else if (is_sdvo && is_tv)
  3885. factor = 20;
  3886. if (clock.m < factor * clock.n)
  3887. fp |= FP_CB_TUNE;
  3888. dpll = 0;
  3889. if (is_lvds)
  3890. dpll |= DPLLB_MODE_LVDS;
  3891. else
  3892. dpll |= DPLLB_MODE_DAC_SERIAL;
  3893. if (is_sdvo) {
  3894. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  3895. if (pixel_multiplier > 1) {
  3896. dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  3897. }
  3898. dpll |= DPLL_DVO_HIGH_SPEED;
  3899. }
  3900. if (is_dp && !is_cpu_edp)
  3901. dpll |= DPLL_DVO_HIGH_SPEED;
  3902. /* compute bitmask from p1 value */
  3903. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3904. /* also FPA1 */
  3905. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  3906. switch (clock.p2) {
  3907. case 5:
  3908. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  3909. break;
  3910. case 7:
  3911. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  3912. break;
  3913. case 10:
  3914. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  3915. break;
  3916. case 14:
  3917. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  3918. break;
  3919. }
  3920. if (is_sdvo && is_tv)
  3921. dpll |= PLL_REF_INPUT_TVCLKINBC;
  3922. else if (is_tv)
  3923. /* XXX: just matching BIOS for now */
  3924. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  3925. dpll |= 3;
  3926. else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  3927. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  3928. else
  3929. dpll |= PLL_REF_INPUT_DREFCLK;
  3930. /* setup pipeconf */
  3931. pipeconf = I915_READ(PIPECONF(pipe));
  3932. /* Set up the display plane register */
  3933. dspcntr = DISPPLANE_GAMMA_ENABLE;
  3934. DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
  3935. drm_mode_debug_printmodeline(mode);
  3936. /* CPU eDP is the only output that doesn't need a PCH PLL of its own on
  3937. * pre-Haswell/LPT generation */
  3938. if (HAS_PCH_LPT(dev)) {
  3939. DRM_DEBUG_KMS("LPT detected: no PLL for pipe %d necessary\n",
  3940. pipe);
  3941. } else if (!is_cpu_edp) {
  3942. struct intel_pch_pll *pll;
  3943. pll = intel_get_pch_pll(intel_crtc, dpll, fp);
  3944. if (pll == NULL) {
  3945. DRM_DEBUG_DRIVER("failed to find PLL for pipe %d\n",
  3946. pipe);
  3947. return -EINVAL;
  3948. }
  3949. } else
  3950. intel_put_pch_pll(intel_crtc);
  3951. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  3952. * This is an exception to the general rule that mode_set doesn't turn
  3953. * things on.
  3954. */
  3955. if (is_lvds) {
  3956. temp = I915_READ(PCH_LVDS);
  3957. temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  3958. if (HAS_PCH_CPT(dev)) {
  3959. temp &= ~PORT_TRANS_SEL_MASK;
  3960. temp |= PORT_TRANS_SEL_CPT(pipe);
  3961. } else {
  3962. if (pipe == 1)
  3963. temp |= LVDS_PIPEB_SELECT;
  3964. else
  3965. temp &= ~LVDS_PIPEB_SELECT;
  3966. }
  3967. /* set the corresponsding LVDS_BORDER bit */
  3968. temp |= dev_priv->lvds_border_bits;
  3969. /* Set the B0-B3 data pairs corresponding to whether we're going to
  3970. * set the DPLLs for dual-channel mode or not.
  3971. */
  3972. if (clock.p2 == 7)
  3973. temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  3974. else
  3975. temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  3976. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  3977. * appropriately here, but we need to look more thoroughly into how
  3978. * panels behave in the two modes.
  3979. */
  3980. temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
  3981. if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
  3982. temp |= LVDS_HSYNC_POLARITY;
  3983. if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
  3984. temp |= LVDS_VSYNC_POLARITY;
  3985. I915_WRITE(PCH_LVDS, temp);
  3986. }
  3987. pipeconf &= ~PIPECONF_DITHER_EN;
  3988. pipeconf &= ~PIPECONF_DITHER_TYPE_MASK;
  3989. if ((is_lvds && dev_priv->lvds_dither) || dither) {
  3990. pipeconf |= PIPECONF_DITHER_EN;
  3991. pipeconf |= PIPECONF_DITHER_TYPE_SP;
  3992. }
  3993. if (is_dp && !is_cpu_edp) {
  3994. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  3995. } else {
  3996. /* For non-DP output, clear any trans DP clock recovery setting.*/
  3997. I915_WRITE(TRANSDATA_M1(pipe), 0);
  3998. I915_WRITE(TRANSDATA_N1(pipe), 0);
  3999. I915_WRITE(TRANSDPLINK_M1(pipe), 0);
  4000. I915_WRITE(TRANSDPLINK_N1(pipe), 0);
  4001. }
  4002. if (intel_crtc->pch_pll) {
  4003. I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
  4004. /* Wait for the clocks to stabilize. */
  4005. POSTING_READ(intel_crtc->pch_pll->pll_reg);
  4006. udelay(150);
  4007. /* The pixel multiplier can only be updated once the
  4008. * DPLL is enabled and the clocks are stable.
  4009. *
  4010. * So write it again.
  4011. */
  4012. I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
  4013. }
  4014. intel_crtc->lowfreq_avail = false;
  4015. if (intel_crtc->pch_pll) {
  4016. if (is_lvds && has_reduced_clock && i915_powersave) {
  4017. I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp2);
  4018. intel_crtc->lowfreq_avail = true;
  4019. if (HAS_PIPE_CXSR(dev)) {
  4020. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  4021. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  4022. }
  4023. } else {
  4024. I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp);
  4025. if (HAS_PIPE_CXSR(dev)) {
  4026. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  4027. pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
  4028. }
  4029. }
  4030. }
  4031. pipeconf &= ~PIPECONF_INTERLACE_MASK;
  4032. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4033. pipeconf |= PIPECONF_INTERLACED_ILK;
  4034. /* the chip adds 2 halflines automatically */
  4035. adjusted_mode->crtc_vtotal -= 1;
  4036. adjusted_mode->crtc_vblank_end -= 1;
  4037. I915_WRITE(VSYNCSHIFT(pipe),
  4038. adjusted_mode->crtc_hsync_start
  4039. - adjusted_mode->crtc_htotal/2);
  4040. } else {
  4041. pipeconf |= PIPECONF_PROGRESSIVE;
  4042. I915_WRITE(VSYNCSHIFT(pipe), 0);
  4043. }
  4044. I915_WRITE(HTOTAL(pipe),
  4045. (adjusted_mode->crtc_hdisplay - 1) |
  4046. ((adjusted_mode->crtc_htotal - 1) << 16));
  4047. I915_WRITE(HBLANK(pipe),
  4048. (adjusted_mode->crtc_hblank_start - 1) |
  4049. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  4050. I915_WRITE(HSYNC(pipe),
  4051. (adjusted_mode->crtc_hsync_start - 1) |
  4052. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  4053. I915_WRITE(VTOTAL(pipe),
  4054. (adjusted_mode->crtc_vdisplay - 1) |
  4055. ((adjusted_mode->crtc_vtotal - 1) << 16));
  4056. I915_WRITE(VBLANK(pipe),
  4057. (adjusted_mode->crtc_vblank_start - 1) |
  4058. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  4059. I915_WRITE(VSYNC(pipe),
  4060. (adjusted_mode->crtc_vsync_start - 1) |
  4061. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  4062. /* pipesrc controls the size that is scaled from, which should
  4063. * always be the user's requested size.
  4064. */
  4065. I915_WRITE(PIPESRC(pipe),
  4066. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  4067. I915_WRITE(PIPE_DATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);
  4068. I915_WRITE(PIPE_DATA_N1(pipe), m_n.gmch_n);
  4069. I915_WRITE(PIPE_LINK_M1(pipe), m_n.link_m);
  4070. I915_WRITE(PIPE_LINK_N1(pipe), m_n.link_n);
  4071. if (is_cpu_edp)
  4072. ironlake_set_pll_edp(crtc, adjusted_mode->clock);
  4073. I915_WRITE(PIPECONF(pipe), pipeconf);
  4074. POSTING_READ(PIPECONF(pipe));
  4075. intel_wait_for_vblank(dev, pipe);
  4076. I915_WRITE(DSPCNTR(plane), dspcntr);
  4077. POSTING_READ(DSPCNTR(plane));
  4078. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  4079. intel_update_watermarks(dev);
  4080. intel_update_linetime_watermarks(dev, pipe, adjusted_mode);
  4081. return ret;
  4082. }
  4083. static int intel_crtc_mode_set(struct drm_crtc *crtc,
  4084. struct drm_display_mode *mode,
  4085. struct drm_display_mode *adjusted_mode,
  4086. int x, int y,
  4087. struct drm_framebuffer *old_fb)
  4088. {
  4089. struct drm_device *dev = crtc->dev;
  4090. struct drm_i915_private *dev_priv = dev->dev_private;
  4091. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4092. int pipe = intel_crtc->pipe;
  4093. int ret;
  4094. drm_vblank_pre_modeset(dev, pipe);
  4095. ret = dev_priv->display.crtc_mode_set(crtc, mode, adjusted_mode,
  4096. x, y, old_fb);
  4097. drm_vblank_post_modeset(dev, pipe);
  4098. if (ret)
  4099. intel_crtc->dpms_mode = DRM_MODE_DPMS_OFF;
  4100. else
  4101. intel_crtc->dpms_mode = DRM_MODE_DPMS_ON;
  4102. return ret;
  4103. }
  4104. static bool intel_eld_uptodate(struct drm_connector *connector,
  4105. int reg_eldv, uint32_t bits_eldv,
  4106. int reg_elda, uint32_t bits_elda,
  4107. int reg_edid)
  4108. {
  4109. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  4110. uint8_t *eld = connector->eld;
  4111. uint32_t i;
  4112. i = I915_READ(reg_eldv);
  4113. i &= bits_eldv;
  4114. if (!eld[0])
  4115. return !i;
  4116. if (!i)
  4117. return false;
  4118. i = I915_READ(reg_elda);
  4119. i &= ~bits_elda;
  4120. I915_WRITE(reg_elda, i);
  4121. for (i = 0; i < eld[2]; i++)
  4122. if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
  4123. return false;
  4124. return true;
  4125. }
  4126. static void g4x_write_eld(struct drm_connector *connector,
  4127. struct drm_crtc *crtc)
  4128. {
  4129. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  4130. uint8_t *eld = connector->eld;
  4131. uint32_t eldv;
  4132. uint32_t len;
  4133. uint32_t i;
  4134. i = I915_READ(G4X_AUD_VID_DID);
  4135. if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
  4136. eldv = G4X_ELDV_DEVCL_DEVBLC;
  4137. else
  4138. eldv = G4X_ELDV_DEVCTG;
  4139. if (intel_eld_uptodate(connector,
  4140. G4X_AUD_CNTL_ST, eldv,
  4141. G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
  4142. G4X_HDMIW_HDMIEDID))
  4143. return;
  4144. i = I915_READ(G4X_AUD_CNTL_ST);
  4145. i &= ~(eldv | G4X_ELD_ADDR);
  4146. len = (i >> 9) & 0x1f; /* ELD buffer size */
  4147. I915_WRITE(G4X_AUD_CNTL_ST, i);
  4148. if (!eld[0])
  4149. return;
  4150. len = min_t(uint8_t, eld[2], len);
  4151. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  4152. for (i = 0; i < len; i++)
  4153. I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
  4154. i = I915_READ(G4X_AUD_CNTL_ST);
  4155. i |= eldv;
  4156. I915_WRITE(G4X_AUD_CNTL_ST, i);
  4157. }
  4158. static void ironlake_write_eld(struct drm_connector *connector,
  4159. struct drm_crtc *crtc)
  4160. {
  4161. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  4162. uint8_t *eld = connector->eld;
  4163. uint32_t eldv;
  4164. uint32_t i;
  4165. int len;
  4166. int hdmiw_hdmiedid;
  4167. int aud_config;
  4168. int aud_cntl_st;
  4169. int aud_cntrl_st2;
  4170. if (HAS_PCH_IBX(connector->dev)) {
  4171. hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID_A;
  4172. aud_config = IBX_AUD_CONFIG_A;
  4173. aud_cntl_st = IBX_AUD_CNTL_ST_A;
  4174. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  4175. } else {
  4176. hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID_A;
  4177. aud_config = CPT_AUD_CONFIG_A;
  4178. aud_cntl_st = CPT_AUD_CNTL_ST_A;
  4179. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  4180. }
  4181. i = to_intel_crtc(crtc)->pipe;
  4182. hdmiw_hdmiedid += i * 0x100;
  4183. aud_cntl_st += i * 0x100;
  4184. aud_config += i * 0x100;
  4185. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(i));
  4186. i = I915_READ(aud_cntl_st);
  4187. i = (i >> 29) & 0x3; /* DIP_Port_Select, 0x1 = PortB */
  4188. if (!i) {
  4189. DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
  4190. /* operate blindly on all ports */
  4191. eldv = IBX_ELD_VALIDB;
  4192. eldv |= IBX_ELD_VALIDB << 4;
  4193. eldv |= IBX_ELD_VALIDB << 8;
  4194. } else {
  4195. DRM_DEBUG_DRIVER("ELD on port %c\n", 'A' + i);
  4196. eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
  4197. }
  4198. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  4199. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  4200. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  4201. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  4202. } else
  4203. I915_WRITE(aud_config, 0);
  4204. if (intel_eld_uptodate(connector,
  4205. aud_cntrl_st2, eldv,
  4206. aud_cntl_st, IBX_ELD_ADDRESS,
  4207. hdmiw_hdmiedid))
  4208. return;
  4209. i = I915_READ(aud_cntrl_st2);
  4210. i &= ~eldv;
  4211. I915_WRITE(aud_cntrl_st2, i);
  4212. if (!eld[0])
  4213. return;
  4214. i = I915_READ(aud_cntl_st);
  4215. i &= ~IBX_ELD_ADDRESS;
  4216. I915_WRITE(aud_cntl_st, i);
  4217. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  4218. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  4219. for (i = 0; i < len; i++)
  4220. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  4221. i = I915_READ(aud_cntrl_st2);
  4222. i |= eldv;
  4223. I915_WRITE(aud_cntrl_st2, i);
  4224. }
  4225. void intel_write_eld(struct drm_encoder *encoder,
  4226. struct drm_display_mode *mode)
  4227. {
  4228. struct drm_crtc *crtc = encoder->crtc;
  4229. struct drm_connector *connector;
  4230. struct drm_device *dev = encoder->dev;
  4231. struct drm_i915_private *dev_priv = dev->dev_private;
  4232. connector = drm_select_eld(encoder, mode);
  4233. if (!connector)
  4234. return;
  4235. DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  4236. connector->base.id,
  4237. drm_get_connector_name(connector),
  4238. connector->encoder->base.id,
  4239. drm_get_encoder_name(connector->encoder));
  4240. connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
  4241. if (dev_priv->display.write_eld)
  4242. dev_priv->display.write_eld(connector, crtc);
  4243. }
  4244. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  4245. void intel_crtc_load_lut(struct drm_crtc *crtc)
  4246. {
  4247. struct drm_device *dev = crtc->dev;
  4248. struct drm_i915_private *dev_priv = dev->dev_private;
  4249. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4250. int palreg = PALETTE(intel_crtc->pipe);
  4251. int i;
  4252. /* The clocks have to be on to load the palette. */
  4253. if (!crtc->enabled || !intel_crtc->active)
  4254. return;
  4255. /* use legacy palette for Ironlake */
  4256. if (HAS_PCH_SPLIT(dev))
  4257. palreg = LGC_PALETTE(intel_crtc->pipe);
  4258. for (i = 0; i < 256; i++) {
  4259. I915_WRITE(palreg + 4 * i,
  4260. (intel_crtc->lut_r[i] << 16) |
  4261. (intel_crtc->lut_g[i] << 8) |
  4262. intel_crtc->lut_b[i]);
  4263. }
  4264. }
  4265. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  4266. {
  4267. struct drm_device *dev = crtc->dev;
  4268. struct drm_i915_private *dev_priv = dev->dev_private;
  4269. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4270. bool visible = base != 0;
  4271. u32 cntl;
  4272. if (intel_crtc->cursor_visible == visible)
  4273. return;
  4274. cntl = I915_READ(_CURACNTR);
  4275. if (visible) {
  4276. /* On these chipsets we can only modify the base whilst
  4277. * the cursor is disabled.
  4278. */
  4279. I915_WRITE(_CURABASE, base);
  4280. cntl &= ~(CURSOR_FORMAT_MASK);
  4281. /* XXX width must be 64, stride 256 => 0x00 << 28 */
  4282. cntl |= CURSOR_ENABLE |
  4283. CURSOR_GAMMA_ENABLE |
  4284. CURSOR_FORMAT_ARGB;
  4285. } else
  4286. cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
  4287. I915_WRITE(_CURACNTR, cntl);
  4288. intel_crtc->cursor_visible = visible;
  4289. }
  4290. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  4291. {
  4292. struct drm_device *dev = crtc->dev;
  4293. struct drm_i915_private *dev_priv = dev->dev_private;
  4294. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4295. int pipe = intel_crtc->pipe;
  4296. bool visible = base != 0;
  4297. if (intel_crtc->cursor_visible != visible) {
  4298. uint32_t cntl = I915_READ(CURCNTR(pipe));
  4299. if (base) {
  4300. cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
  4301. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  4302. cntl |= pipe << 28; /* Connect to correct pipe */
  4303. } else {
  4304. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  4305. cntl |= CURSOR_MODE_DISABLE;
  4306. }
  4307. I915_WRITE(CURCNTR(pipe), cntl);
  4308. intel_crtc->cursor_visible = visible;
  4309. }
  4310. /* and commit changes on next vblank */
  4311. I915_WRITE(CURBASE(pipe), base);
  4312. }
  4313. static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
  4314. {
  4315. struct drm_device *dev = crtc->dev;
  4316. struct drm_i915_private *dev_priv = dev->dev_private;
  4317. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4318. int pipe = intel_crtc->pipe;
  4319. bool visible = base != 0;
  4320. if (intel_crtc->cursor_visible != visible) {
  4321. uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
  4322. if (base) {
  4323. cntl &= ~CURSOR_MODE;
  4324. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  4325. } else {
  4326. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  4327. cntl |= CURSOR_MODE_DISABLE;
  4328. }
  4329. I915_WRITE(CURCNTR_IVB(pipe), cntl);
  4330. intel_crtc->cursor_visible = visible;
  4331. }
  4332. /* and commit changes on next vblank */
  4333. I915_WRITE(CURBASE_IVB(pipe), base);
  4334. }
  4335. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  4336. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  4337. bool on)
  4338. {
  4339. struct drm_device *dev = crtc->dev;
  4340. struct drm_i915_private *dev_priv = dev->dev_private;
  4341. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4342. int pipe = intel_crtc->pipe;
  4343. int x = intel_crtc->cursor_x;
  4344. int y = intel_crtc->cursor_y;
  4345. u32 base, pos;
  4346. bool visible;
  4347. pos = 0;
  4348. if (on && crtc->enabled && crtc->fb) {
  4349. base = intel_crtc->cursor_addr;
  4350. if (x > (int) crtc->fb->width)
  4351. base = 0;
  4352. if (y > (int) crtc->fb->height)
  4353. base = 0;
  4354. } else
  4355. base = 0;
  4356. if (x < 0) {
  4357. if (x + intel_crtc->cursor_width < 0)
  4358. base = 0;
  4359. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  4360. x = -x;
  4361. }
  4362. pos |= x << CURSOR_X_SHIFT;
  4363. if (y < 0) {
  4364. if (y + intel_crtc->cursor_height < 0)
  4365. base = 0;
  4366. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  4367. y = -y;
  4368. }
  4369. pos |= y << CURSOR_Y_SHIFT;
  4370. visible = base != 0;
  4371. if (!visible && !intel_crtc->cursor_visible)
  4372. return;
  4373. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  4374. I915_WRITE(CURPOS_IVB(pipe), pos);
  4375. ivb_update_cursor(crtc, base);
  4376. } else {
  4377. I915_WRITE(CURPOS(pipe), pos);
  4378. if (IS_845G(dev) || IS_I865G(dev))
  4379. i845_update_cursor(crtc, base);
  4380. else
  4381. i9xx_update_cursor(crtc, base);
  4382. }
  4383. }
  4384. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  4385. struct drm_file *file,
  4386. uint32_t handle,
  4387. uint32_t width, uint32_t height)
  4388. {
  4389. struct drm_device *dev = crtc->dev;
  4390. struct drm_i915_private *dev_priv = dev->dev_private;
  4391. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4392. struct drm_i915_gem_object *obj;
  4393. uint32_t addr;
  4394. int ret;
  4395. DRM_DEBUG_KMS("\n");
  4396. /* if we want to turn off the cursor ignore width and height */
  4397. if (!handle) {
  4398. DRM_DEBUG_KMS("cursor off\n");
  4399. addr = 0;
  4400. obj = NULL;
  4401. mutex_lock(&dev->struct_mutex);
  4402. goto finish;
  4403. }
  4404. /* Currently we only support 64x64 cursors */
  4405. if (width != 64 || height != 64) {
  4406. DRM_ERROR("we currently only support 64x64 cursors\n");
  4407. return -EINVAL;
  4408. }
  4409. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  4410. if (&obj->base == NULL)
  4411. return -ENOENT;
  4412. if (obj->base.size < width * height * 4) {
  4413. DRM_ERROR("buffer is to small\n");
  4414. ret = -ENOMEM;
  4415. goto fail;
  4416. }
  4417. /* we only need to pin inside GTT if cursor is non-phy */
  4418. mutex_lock(&dev->struct_mutex);
  4419. if (!dev_priv->info->cursor_needs_physical) {
  4420. if (obj->tiling_mode) {
  4421. DRM_ERROR("cursor cannot be tiled\n");
  4422. ret = -EINVAL;
  4423. goto fail_locked;
  4424. }
  4425. ret = i915_gem_object_pin_to_display_plane(obj, 0, NULL);
  4426. if (ret) {
  4427. DRM_ERROR("failed to move cursor bo into the GTT\n");
  4428. goto fail_locked;
  4429. }
  4430. ret = i915_gem_object_put_fence(obj);
  4431. if (ret) {
  4432. DRM_ERROR("failed to release fence for cursor");
  4433. goto fail_unpin;
  4434. }
  4435. addr = obj->gtt_offset;
  4436. } else {
  4437. int align = IS_I830(dev) ? 16 * 1024 : 256;
  4438. ret = i915_gem_attach_phys_object(dev, obj,
  4439. (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
  4440. align);
  4441. if (ret) {
  4442. DRM_ERROR("failed to attach phys object\n");
  4443. goto fail_locked;
  4444. }
  4445. addr = obj->phys_obj->handle->busaddr;
  4446. }
  4447. if (IS_GEN2(dev))
  4448. I915_WRITE(CURSIZE, (height << 12) | width);
  4449. finish:
  4450. if (intel_crtc->cursor_bo) {
  4451. if (dev_priv->info->cursor_needs_physical) {
  4452. if (intel_crtc->cursor_bo != obj)
  4453. i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
  4454. } else
  4455. i915_gem_object_unpin(intel_crtc->cursor_bo);
  4456. drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
  4457. }
  4458. mutex_unlock(&dev->struct_mutex);
  4459. intel_crtc->cursor_addr = addr;
  4460. intel_crtc->cursor_bo = obj;
  4461. intel_crtc->cursor_width = width;
  4462. intel_crtc->cursor_height = height;
  4463. intel_crtc_update_cursor(crtc, true);
  4464. return 0;
  4465. fail_unpin:
  4466. i915_gem_object_unpin(obj);
  4467. fail_locked:
  4468. mutex_unlock(&dev->struct_mutex);
  4469. fail:
  4470. drm_gem_object_unreference_unlocked(&obj->base);
  4471. return ret;
  4472. }
  4473. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  4474. {
  4475. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4476. intel_crtc->cursor_x = x;
  4477. intel_crtc->cursor_y = y;
  4478. intel_crtc_update_cursor(crtc, true);
  4479. return 0;
  4480. }
  4481. /** Sets the color ramps on behalf of RandR */
  4482. void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  4483. u16 blue, int regno)
  4484. {
  4485. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4486. intel_crtc->lut_r[regno] = red >> 8;
  4487. intel_crtc->lut_g[regno] = green >> 8;
  4488. intel_crtc->lut_b[regno] = blue >> 8;
  4489. }
  4490. void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  4491. u16 *blue, int regno)
  4492. {
  4493. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4494. *red = intel_crtc->lut_r[regno] << 8;
  4495. *green = intel_crtc->lut_g[regno] << 8;
  4496. *blue = intel_crtc->lut_b[regno] << 8;
  4497. }
  4498. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  4499. u16 *blue, uint32_t start, uint32_t size)
  4500. {
  4501. int end = (start + size > 256) ? 256 : start + size, i;
  4502. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4503. for (i = start; i < end; i++) {
  4504. intel_crtc->lut_r[i] = red[i] >> 8;
  4505. intel_crtc->lut_g[i] = green[i] >> 8;
  4506. intel_crtc->lut_b[i] = blue[i] >> 8;
  4507. }
  4508. intel_crtc_load_lut(crtc);
  4509. }
  4510. /**
  4511. * Get a pipe with a simple mode set on it for doing load-based monitor
  4512. * detection.
  4513. *
  4514. * It will be up to the load-detect code to adjust the pipe as appropriate for
  4515. * its requirements. The pipe will be connected to no other encoders.
  4516. *
  4517. * Currently this code will only succeed if there is a pipe with no encoders
  4518. * configured for it. In the future, it could choose to temporarily disable
  4519. * some outputs to free up a pipe for its use.
  4520. *
  4521. * \return crtc, or NULL if no pipes are available.
  4522. */
  4523. /* VESA 640x480x72Hz mode to set on the pipe */
  4524. static struct drm_display_mode load_detect_mode = {
  4525. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  4526. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  4527. };
  4528. static struct drm_framebuffer *
  4529. intel_framebuffer_create(struct drm_device *dev,
  4530. struct drm_mode_fb_cmd2 *mode_cmd,
  4531. struct drm_i915_gem_object *obj)
  4532. {
  4533. struct intel_framebuffer *intel_fb;
  4534. int ret;
  4535. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  4536. if (!intel_fb) {
  4537. drm_gem_object_unreference_unlocked(&obj->base);
  4538. return ERR_PTR(-ENOMEM);
  4539. }
  4540. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  4541. if (ret) {
  4542. drm_gem_object_unreference_unlocked(&obj->base);
  4543. kfree(intel_fb);
  4544. return ERR_PTR(ret);
  4545. }
  4546. return &intel_fb->base;
  4547. }
  4548. static u32
  4549. intel_framebuffer_pitch_for_width(int width, int bpp)
  4550. {
  4551. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  4552. return ALIGN(pitch, 64);
  4553. }
  4554. static u32
  4555. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  4556. {
  4557. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  4558. return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
  4559. }
  4560. static struct drm_framebuffer *
  4561. intel_framebuffer_create_for_mode(struct drm_device *dev,
  4562. struct drm_display_mode *mode,
  4563. int depth, int bpp)
  4564. {
  4565. struct drm_i915_gem_object *obj;
  4566. struct drm_mode_fb_cmd2 mode_cmd;
  4567. obj = i915_gem_alloc_object(dev,
  4568. intel_framebuffer_size_for_mode(mode, bpp));
  4569. if (obj == NULL)
  4570. return ERR_PTR(-ENOMEM);
  4571. mode_cmd.width = mode->hdisplay;
  4572. mode_cmd.height = mode->vdisplay;
  4573. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  4574. bpp);
  4575. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  4576. return intel_framebuffer_create(dev, &mode_cmd, obj);
  4577. }
  4578. static struct drm_framebuffer *
  4579. mode_fits_in_fbdev(struct drm_device *dev,
  4580. struct drm_display_mode *mode)
  4581. {
  4582. struct drm_i915_private *dev_priv = dev->dev_private;
  4583. struct drm_i915_gem_object *obj;
  4584. struct drm_framebuffer *fb;
  4585. if (dev_priv->fbdev == NULL)
  4586. return NULL;
  4587. obj = dev_priv->fbdev->ifb.obj;
  4588. if (obj == NULL)
  4589. return NULL;
  4590. fb = &dev_priv->fbdev->ifb.base;
  4591. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  4592. fb->bits_per_pixel))
  4593. return NULL;
  4594. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  4595. return NULL;
  4596. return fb;
  4597. }
  4598. bool intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,
  4599. struct drm_connector *connector,
  4600. struct drm_display_mode *mode,
  4601. struct intel_load_detect_pipe *old)
  4602. {
  4603. struct intel_crtc *intel_crtc;
  4604. struct drm_crtc *possible_crtc;
  4605. struct drm_encoder *encoder = &intel_encoder->base;
  4606. struct drm_crtc *crtc = NULL;
  4607. struct drm_device *dev = encoder->dev;
  4608. struct drm_framebuffer *old_fb;
  4609. int i = -1;
  4610. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  4611. connector->base.id, drm_get_connector_name(connector),
  4612. encoder->base.id, drm_get_encoder_name(encoder));
  4613. /*
  4614. * Algorithm gets a little messy:
  4615. *
  4616. * - if the connector already has an assigned crtc, use it (but make
  4617. * sure it's on first)
  4618. *
  4619. * - try to find the first unused crtc that can drive this connector,
  4620. * and use that if we find one
  4621. */
  4622. /* See if we already have a CRTC for this connector */
  4623. if (encoder->crtc) {
  4624. crtc = encoder->crtc;
  4625. intel_crtc = to_intel_crtc(crtc);
  4626. old->dpms_mode = intel_crtc->dpms_mode;
  4627. old->load_detect_temp = false;
  4628. /* Make sure the crtc and connector are running */
  4629. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  4630. struct drm_encoder_helper_funcs *encoder_funcs;
  4631. struct drm_crtc_helper_funcs *crtc_funcs;
  4632. crtc_funcs = crtc->helper_private;
  4633. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  4634. encoder_funcs = encoder->helper_private;
  4635. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  4636. }
  4637. return true;
  4638. }
  4639. /* Find an unused one (if possible) */
  4640. list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
  4641. i++;
  4642. if (!(encoder->possible_crtcs & (1 << i)))
  4643. continue;
  4644. if (!possible_crtc->enabled) {
  4645. crtc = possible_crtc;
  4646. break;
  4647. }
  4648. }
  4649. /*
  4650. * If we didn't find an unused CRTC, don't use any.
  4651. */
  4652. if (!crtc) {
  4653. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  4654. return false;
  4655. }
  4656. encoder->crtc = crtc;
  4657. connector->encoder = encoder;
  4658. intel_crtc = to_intel_crtc(crtc);
  4659. old->dpms_mode = intel_crtc->dpms_mode;
  4660. old->load_detect_temp = true;
  4661. old->release_fb = NULL;
  4662. if (!mode)
  4663. mode = &load_detect_mode;
  4664. old_fb = crtc->fb;
  4665. /* We need a framebuffer large enough to accommodate all accesses
  4666. * that the plane may generate whilst we perform load detection.
  4667. * We can not rely on the fbcon either being present (we get called
  4668. * during its initialisation to detect all boot displays, or it may
  4669. * not even exist) or that it is large enough to satisfy the
  4670. * requested mode.
  4671. */
  4672. crtc->fb = mode_fits_in_fbdev(dev, mode);
  4673. if (crtc->fb == NULL) {
  4674. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  4675. crtc->fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  4676. old->release_fb = crtc->fb;
  4677. } else
  4678. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  4679. if (IS_ERR(crtc->fb)) {
  4680. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  4681. crtc->fb = old_fb;
  4682. return false;
  4683. }
  4684. if (!drm_crtc_helper_set_mode(crtc, mode, 0, 0, old_fb)) {
  4685. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  4686. if (old->release_fb)
  4687. old->release_fb->funcs->destroy(old->release_fb);
  4688. crtc->fb = old_fb;
  4689. return false;
  4690. }
  4691. /* let the connector get through one full cycle before testing */
  4692. intel_wait_for_vblank(dev, intel_crtc->pipe);
  4693. return true;
  4694. }
  4695. void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,
  4696. struct drm_connector *connector,
  4697. struct intel_load_detect_pipe *old)
  4698. {
  4699. struct drm_encoder *encoder = &intel_encoder->base;
  4700. struct drm_device *dev = encoder->dev;
  4701. struct drm_crtc *crtc = encoder->crtc;
  4702. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  4703. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  4704. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  4705. connector->base.id, drm_get_connector_name(connector),
  4706. encoder->base.id, drm_get_encoder_name(encoder));
  4707. if (old->load_detect_temp) {
  4708. connector->encoder = NULL;
  4709. drm_helper_disable_unused_functions(dev);
  4710. if (old->release_fb)
  4711. old->release_fb->funcs->destroy(old->release_fb);
  4712. return;
  4713. }
  4714. /* Switch crtc and encoder back off if necessary */
  4715. if (old->dpms_mode != DRM_MODE_DPMS_ON) {
  4716. encoder_funcs->dpms(encoder, old->dpms_mode);
  4717. crtc_funcs->dpms(crtc, old->dpms_mode);
  4718. }
  4719. }
  4720. /* Returns the clock of the currently programmed mode of the given pipe. */
  4721. static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
  4722. {
  4723. struct drm_i915_private *dev_priv = dev->dev_private;
  4724. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4725. int pipe = intel_crtc->pipe;
  4726. u32 dpll = I915_READ(DPLL(pipe));
  4727. u32 fp;
  4728. intel_clock_t clock;
  4729. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  4730. fp = I915_READ(FP0(pipe));
  4731. else
  4732. fp = I915_READ(FP1(pipe));
  4733. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  4734. if (IS_PINEVIEW(dev)) {
  4735. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  4736. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  4737. } else {
  4738. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  4739. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  4740. }
  4741. if (!IS_GEN2(dev)) {
  4742. if (IS_PINEVIEW(dev))
  4743. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  4744. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  4745. else
  4746. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  4747. DPLL_FPA01_P1_POST_DIV_SHIFT);
  4748. switch (dpll & DPLL_MODE_MASK) {
  4749. case DPLLB_MODE_DAC_SERIAL:
  4750. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  4751. 5 : 10;
  4752. break;
  4753. case DPLLB_MODE_LVDS:
  4754. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  4755. 7 : 14;
  4756. break;
  4757. default:
  4758. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  4759. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  4760. return 0;
  4761. }
  4762. /* XXX: Handle the 100Mhz refclk */
  4763. intel_clock(dev, 96000, &clock);
  4764. } else {
  4765. bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
  4766. if (is_lvds) {
  4767. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  4768. DPLL_FPA01_P1_POST_DIV_SHIFT);
  4769. clock.p2 = 14;
  4770. if ((dpll & PLL_REF_INPUT_MASK) ==
  4771. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  4772. /* XXX: might not be 66MHz */
  4773. intel_clock(dev, 66000, &clock);
  4774. } else
  4775. intel_clock(dev, 48000, &clock);
  4776. } else {
  4777. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  4778. clock.p1 = 2;
  4779. else {
  4780. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  4781. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  4782. }
  4783. if (dpll & PLL_P2_DIVIDE_BY_4)
  4784. clock.p2 = 4;
  4785. else
  4786. clock.p2 = 2;
  4787. intel_clock(dev, 48000, &clock);
  4788. }
  4789. }
  4790. /* XXX: It would be nice to validate the clocks, but we can't reuse
  4791. * i830PllIsValid() because it relies on the xf86_config connector
  4792. * configuration being accurate, which it isn't necessarily.
  4793. */
  4794. return clock.dot;
  4795. }
  4796. /** Returns the currently programmed mode of the given pipe. */
  4797. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  4798. struct drm_crtc *crtc)
  4799. {
  4800. struct drm_i915_private *dev_priv = dev->dev_private;
  4801. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4802. int pipe = intel_crtc->pipe;
  4803. struct drm_display_mode *mode;
  4804. int htot = I915_READ(HTOTAL(pipe));
  4805. int hsync = I915_READ(HSYNC(pipe));
  4806. int vtot = I915_READ(VTOTAL(pipe));
  4807. int vsync = I915_READ(VSYNC(pipe));
  4808. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  4809. if (!mode)
  4810. return NULL;
  4811. mode->clock = intel_crtc_clock_get(dev, crtc);
  4812. mode->hdisplay = (htot & 0xffff) + 1;
  4813. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  4814. mode->hsync_start = (hsync & 0xffff) + 1;
  4815. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  4816. mode->vdisplay = (vtot & 0xffff) + 1;
  4817. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  4818. mode->vsync_start = (vsync & 0xffff) + 1;
  4819. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  4820. drm_mode_set_name(mode);
  4821. return mode;
  4822. }
  4823. #define GPU_IDLE_TIMEOUT 500 /* ms */
  4824. /* When this timer fires, we've been idle for awhile */
  4825. static void intel_gpu_idle_timer(unsigned long arg)
  4826. {
  4827. struct drm_device *dev = (struct drm_device *)arg;
  4828. drm_i915_private_t *dev_priv = dev->dev_private;
  4829. if (!list_empty(&dev_priv->mm.active_list)) {
  4830. /* Still processing requests, so just re-arm the timer. */
  4831. mod_timer(&dev_priv->idle_timer, jiffies +
  4832. msecs_to_jiffies(GPU_IDLE_TIMEOUT));
  4833. return;
  4834. }
  4835. dev_priv->busy = false;
  4836. queue_work(dev_priv->wq, &dev_priv->idle_work);
  4837. }
  4838. #define CRTC_IDLE_TIMEOUT 1000 /* ms */
  4839. static void intel_crtc_idle_timer(unsigned long arg)
  4840. {
  4841. struct intel_crtc *intel_crtc = (struct intel_crtc *)arg;
  4842. struct drm_crtc *crtc = &intel_crtc->base;
  4843. drm_i915_private_t *dev_priv = crtc->dev->dev_private;
  4844. struct intel_framebuffer *intel_fb;
  4845. intel_fb = to_intel_framebuffer(crtc->fb);
  4846. if (intel_fb && intel_fb->obj->active) {
  4847. /* The framebuffer is still being accessed by the GPU. */
  4848. mod_timer(&intel_crtc->idle_timer, jiffies +
  4849. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  4850. return;
  4851. }
  4852. intel_crtc->busy = false;
  4853. queue_work(dev_priv->wq, &dev_priv->idle_work);
  4854. }
  4855. static void intel_increase_pllclock(struct drm_crtc *crtc)
  4856. {
  4857. struct drm_device *dev = crtc->dev;
  4858. drm_i915_private_t *dev_priv = dev->dev_private;
  4859. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4860. int pipe = intel_crtc->pipe;
  4861. int dpll_reg = DPLL(pipe);
  4862. int dpll;
  4863. if (HAS_PCH_SPLIT(dev))
  4864. return;
  4865. if (!dev_priv->lvds_downclock_avail)
  4866. return;
  4867. dpll = I915_READ(dpll_reg);
  4868. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  4869. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  4870. assert_panel_unlocked(dev_priv, pipe);
  4871. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  4872. I915_WRITE(dpll_reg, dpll);
  4873. intel_wait_for_vblank(dev, pipe);
  4874. dpll = I915_READ(dpll_reg);
  4875. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  4876. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  4877. }
  4878. /* Schedule downclock */
  4879. mod_timer(&intel_crtc->idle_timer, jiffies +
  4880. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  4881. }
  4882. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  4883. {
  4884. struct drm_device *dev = crtc->dev;
  4885. drm_i915_private_t *dev_priv = dev->dev_private;
  4886. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4887. if (HAS_PCH_SPLIT(dev))
  4888. return;
  4889. if (!dev_priv->lvds_downclock_avail)
  4890. return;
  4891. /*
  4892. * Since this is called by a timer, we should never get here in
  4893. * the manual case.
  4894. */
  4895. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  4896. int pipe = intel_crtc->pipe;
  4897. int dpll_reg = DPLL(pipe);
  4898. int dpll;
  4899. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  4900. assert_panel_unlocked(dev_priv, pipe);
  4901. dpll = I915_READ(dpll_reg);
  4902. dpll |= DISPLAY_RATE_SELECT_FPA1;
  4903. I915_WRITE(dpll_reg, dpll);
  4904. intel_wait_for_vblank(dev, pipe);
  4905. dpll = I915_READ(dpll_reg);
  4906. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  4907. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  4908. }
  4909. }
  4910. /**
  4911. * intel_idle_update - adjust clocks for idleness
  4912. * @work: work struct
  4913. *
  4914. * Either the GPU or display (or both) went idle. Check the busy status
  4915. * here and adjust the CRTC and GPU clocks as necessary.
  4916. */
  4917. static void intel_idle_update(struct work_struct *work)
  4918. {
  4919. drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
  4920. idle_work);
  4921. struct drm_device *dev = dev_priv->dev;
  4922. struct drm_crtc *crtc;
  4923. struct intel_crtc *intel_crtc;
  4924. if (!i915_powersave)
  4925. return;
  4926. mutex_lock(&dev->struct_mutex);
  4927. i915_update_gfx_val(dev_priv);
  4928. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  4929. /* Skip inactive CRTCs */
  4930. if (!crtc->fb)
  4931. continue;
  4932. intel_crtc = to_intel_crtc(crtc);
  4933. if (!intel_crtc->busy)
  4934. intel_decrease_pllclock(crtc);
  4935. }
  4936. mutex_unlock(&dev->struct_mutex);
  4937. }
  4938. /**
  4939. * intel_mark_busy - mark the GPU and possibly the display busy
  4940. * @dev: drm device
  4941. * @obj: object we're operating on
  4942. *
  4943. * Callers can use this function to indicate that the GPU is busy processing
  4944. * commands. If @obj matches one of the CRTC objects (i.e. it's a scanout
  4945. * buffer), we'll also mark the display as busy, so we know to increase its
  4946. * clock frequency.
  4947. */
  4948. void intel_mark_busy(struct drm_device *dev, struct drm_i915_gem_object *obj)
  4949. {
  4950. drm_i915_private_t *dev_priv = dev->dev_private;
  4951. struct drm_crtc *crtc = NULL;
  4952. struct intel_framebuffer *intel_fb;
  4953. struct intel_crtc *intel_crtc;
  4954. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4955. return;
  4956. if (!dev_priv->busy) {
  4957. intel_sanitize_pm(dev);
  4958. dev_priv->busy = true;
  4959. } else
  4960. mod_timer(&dev_priv->idle_timer, jiffies +
  4961. msecs_to_jiffies(GPU_IDLE_TIMEOUT));
  4962. if (obj == NULL)
  4963. return;
  4964. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  4965. if (!crtc->fb)
  4966. continue;
  4967. intel_crtc = to_intel_crtc(crtc);
  4968. intel_fb = to_intel_framebuffer(crtc->fb);
  4969. if (intel_fb->obj == obj) {
  4970. if (!intel_crtc->busy) {
  4971. /* Non-busy -> busy, upclock */
  4972. intel_increase_pllclock(crtc);
  4973. intel_crtc->busy = true;
  4974. } else {
  4975. /* Busy -> busy, put off timer */
  4976. mod_timer(&intel_crtc->idle_timer, jiffies +
  4977. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  4978. }
  4979. }
  4980. }
  4981. }
  4982. static void intel_crtc_destroy(struct drm_crtc *crtc)
  4983. {
  4984. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4985. struct drm_device *dev = crtc->dev;
  4986. struct intel_unpin_work *work;
  4987. unsigned long flags;
  4988. spin_lock_irqsave(&dev->event_lock, flags);
  4989. work = intel_crtc->unpin_work;
  4990. intel_crtc->unpin_work = NULL;
  4991. spin_unlock_irqrestore(&dev->event_lock, flags);
  4992. if (work) {
  4993. cancel_work_sync(&work->work);
  4994. kfree(work);
  4995. }
  4996. drm_crtc_cleanup(crtc);
  4997. kfree(intel_crtc);
  4998. }
  4999. static void intel_unpin_work_fn(struct work_struct *__work)
  5000. {
  5001. struct intel_unpin_work *work =
  5002. container_of(__work, struct intel_unpin_work, work);
  5003. mutex_lock(&work->dev->struct_mutex);
  5004. intel_unpin_fb_obj(work->old_fb_obj);
  5005. drm_gem_object_unreference(&work->pending_flip_obj->base);
  5006. drm_gem_object_unreference(&work->old_fb_obj->base);
  5007. intel_update_fbc(work->dev);
  5008. mutex_unlock(&work->dev->struct_mutex);
  5009. kfree(work);
  5010. }
  5011. static void do_intel_finish_page_flip(struct drm_device *dev,
  5012. struct drm_crtc *crtc)
  5013. {
  5014. drm_i915_private_t *dev_priv = dev->dev_private;
  5015. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5016. struct intel_unpin_work *work;
  5017. struct drm_i915_gem_object *obj;
  5018. struct drm_pending_vblank_event *e;
  5019. struct timeval tnow, tvbl;
  5020. unsigned long flags;
  5021. /* Ignore early vblank irqs */
  5022. if (intel_crtc == NULL)
  5023. return;
  5024. do_gettimeofday(&tnow);
  5025. spin_lock_irqsave(&dev->event_lock, flags);
  5026. work = intel_crtc->unpin_work;
  5027. if (work == NULL || !work->pending) {
  5028. spin_unlock_irqrestore(&dev->event_lock, flags);
  5029. return;
  5030. }
  5031. intel_crtc->unpin_work = NULL;
  5032. if (work->event) {
  5033. e = work->event;
  5034. e->event.sequence = drm_vblank_count_and_time(dev, intel_crtc->pipe, &tvbl);
  5035. /* Called before vblank count and timestamps have
  5036. * been updated for the vblank interval of flip
  5037. * completion? Need to increment vblank count and
  5038. * add one videorefresh duration to returned timestamp
  5039. * to account for this. We assume this happened if we
  5040. * get called over 0.9 frame durations after the last
  5041. * timestamped vblank.
  5042. *
  5043. * This calculation can not be used with vrefresh rates
  5044. * below 5Hz (10Hz to be on the safe side) without
  5045. * promoting to 64 integers.
  5046. */
  5047. if (10 * (timeval_to_ns(&tnow) - timeval_to_ns(&tvbl)) >
  5048. 9 * crtc->framedur_ns) {
  5049. e->event.sequence++;
  5050. tvbl = ns_to_timeval(timeval_to_ns(&tvbl) +
  5051. crtc->framedur_ns);
  5052. }
  5053. e->event.tv_sec = tvbl.tv_sec;
  5054. e->event.tv_usec = tvbl.tv_usec;
  5055. list_add_tail(&e->base.link,
  5056. &e->base.file_priv->event_list);
  5057. wake_up_interruptible(&e->base.file_priv->event_wait);
  5058. }
  5059. drm_vblank_put(dev, intel_crtc->pipe);
  5060. spin_unlock_irqrestore(&dev->event_lock, flags);
  5061. obj = work->old_fb_obj;
  5062. atomic_clear_mask(1 << intel_crtc->plane,
  5063. &obj->pending_flip.counter);
  5064. if (atomic_read(&obj->pending_flip) == 0)
  5065. wake_up(&dev_priv->pending_flip_queue);
  5066. schedule_work(&work->work);
  5067. trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
  5068. }
  5069. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  5070. {
  5071. drm_i915_private_t *dev_priv = dev->dev_private;
  5072. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  5073. do_intel_finish_page_flip(dev, crtc);
  5074. }
  5075. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  5076. {
  5077. drm_i915_private_t *dev_priv = dev->dev_private;
  5078. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  5079. do_intel_finish_page_flip(dev, crtc);
  5080. }
  5081. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  5082. {
  5083. drm_i915_private_t *dev_priv = dev->dev_private;
  5084. struct intel_crtc *intel_crtc =
  5085. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  5086. unsigned long flags;
  5087. spin_lock_irqsave(&dev->event_lock, flags);
  5088. if (intel_crtc->unpin_work) {
  5089. if ((++intel_crtc->unpin_work->pending) > 1)
  5090. DRM_ERROR("Prepared flip multiple times\n");
  5091. } else {
  5092. DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
  5093. }
  5094. spin_unlock_irqrestore(&dev->event_lock, flags);
  5095. }
  5096. static int intel_gen2_queue_flip(struct drm_device *dev,
  5097. struct drm_crtc *crtc,
  5098. struct drm_framebuffer *fb,
  5099. struct drm_i915_gem_object *obj)
  5100. {
  5101. struct drm_i915_private *dev_priv = dev->dev_private;
  5102. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5103. unsigned long offset;
  5104. u32 flip_mask;
  5105. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  5106. int ret;
  5107. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  5108. if (ret)
  5109. goto err;
  5110. /* Offset into the new buffer for cases of shared fbs between CRTCs */
  5111. offset = crtc->y * fb->pitches[0] + crtc->x * fb->bits_per_pixel/8;
  5112. ret = intel_ring_begin(ring, 6);
  5113. if (ret)
  5114. goto err_unpin;
  5115. /* Can't queue multiple flips, so wait for the previous
  5116. * one to finish before executing the next.
  5117. */
  5118. if (intel_crtc->plane)
  5119. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  5120. else
  5121. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  5122. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  5123. intel_ring_emit(ring, MI_NOOP);
  5124. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  5125. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5126. intel_ring_emit(ring, fb->pitches[0]);
  5127. intel_ring_emit(ring, obj->gtt_offset + offset);
  5128. intel_ring_emit(ring, 0); /* aux display base address, unused */
  5129. intel_ring_advance(ring);
  5130. return 0;
  5131. err_unpin:
  5132. intel_unpin_fb_obj(obj);
  5133. err:
  5134. return ret;
  5135. }
  5136. static int intel_gen3_queue_flip(struct drm_device *dev,
  5137. struct drm_crtc *crtc,
  5138. struct drm_framebuffer *fb,
  5139. struct drm_i915_gem_object *obj)
  5140. {
  5141. struct drm_i915_private *dev_priv = dev->dev_private;
  5142. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5143. unsigned long offset;
  5144. u32 flip_mask;
  5145. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  5146. int ret;
  5147. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  5148. if (ret)
  5149. goto err;
  5150. /* Offset into the new buffer for cases of shared fbs between CRTCs */
  5151. offset = crtc->y * fb->pitches[0] + crtc->x * fb->bits_per_pixel/8;
  5152. ret = intel_ring_begin(ring, 6);
  5153. if (ret)
  5154. goto err_unpin;
  5155. if (intel_crtc->plane)
  5156. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  5157. else
  5158. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  5159. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  5160. intel_ring_emit(ring, MI_NOOP);
  5161. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  5162. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5163. intel_ring_emit(ring, fb->pitches[0]);
  5164. intel_ring_emit(ring, obj->gtt_offset + offset);
  5165. intel_ring_emit(ring, MI_NOOP);
  5166. intel_ring_advance(ring);
  5167. return 0;
  5168. err_unpin:
  5169. intel_unpin_fb_obj(obj);
  5170. err:
  5171. return ret;
  5172. }
  5173. static int intel_gen4_queue_flip(struct drm_device *dev,
  5174. struct drm_crtc *crtc,
  5175. struct drm_framebuffer *fb,
  5176. struct drm_i915_gem_object *obj)
  5177. {
  5178. struct drm_i915_private *dev_priv = dev->dev_private;
  5179. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5180. uint32_t pf, pipesrc;
  5181. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  5182. int ret;
  5183. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  5184. if (ret)
  5185. goto err;
  5186. ret = intel_ring_begin(ring, 4);
  5187. if (ret)
  5188. goto err_unpin;
  5189. /* i965+ uses the linear or tiled offsets from the
  5190. * Display Registers (which do not change across a page-flip)
  5191. * so we need only reprogram the base address.
  5192. */
  5193. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  5194. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5195. intel_ring_emit(ring, fb->pitches[0]);
  5196. intel_ring_emit(ring, obj->gtt_offset | obj->tiling_mode);
  5197. /* XXX Enabling the panel-fitter across page-flip is so far
  5198. * untested on non-native modes, so ignore it for now.
  5199. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  5200. */
  5201. pf = 0;
  5202. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  5203. intel_ring_emit(ring, pf | pipesrc);
  5204. intel_ring_advance(ring);
  5205. return 0;
  5206. err_unpin:
  5207. intel_unpin_fb_obj(obj);
  5208. err:
  5209. return ret;
  5210. }
  5211. static int intel_gen6_queue_flip(struct drm_device *dev,
  5212. struct drm_crtc *crtc,
  5213. struct drm_framebuffer *fb,
  5214. struct drm_i915_gem_object *obj)
  5215. {
  5216. struct drm_i915_private *dev_priv = dev->dev_private;
  5217. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5218. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  5219. uint32_t pf, pipesrc;
  5220. int ret;
  5221. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  5222. if (ret)
  5223. goto err;
  5224. ret = intel_ring_begin(ring, 4);
  5225. if (ret)
  5226. goto err_unpin;
  5227. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  5228. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5229. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  5230. intel_ring_emit(ring, obj->gtt_offset);
  5231. /* Contrary to the suggestions in the documentation,
  5232. * "Enable Panel Fitter" does not seem to be required when page
  5233. * flipping with a non-native mode, and worse causes a normal
  5234. * modeset to fail.
  5235. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  5236. */
  5237. pf = 0;
  5238. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  5239. intel_ring_emit(ring, pf | pipesrc);
  5240. intel_ring_advance(ring);
  5241. return 0;
  5242. err_unpin:
  5243. intel_unpin_fb_obj(obj);
  5244. err:
  5245. return ret;
  5246. }
  5247. /*
  5248. * On gen7 we currently use the blit ring because (in early silicon at least)
  5249. * the render ring doesn't give us interrpts for page flip completion, which
  5250. * means clients will hang after the first flip is queued. Fortunately the
  5251. * blit ring generates interrupts properly, so use it instead.
  5252. */
  5253. static int intel_gen7_queue_flip(struct drm_device *dev,
  5254. struct drm_crtc *crtc,
  5255. struct drm_framebuffer *fb,
  5256. struct drm_i915_gem_object *obj)
  5257. {
  5258. struct drm_i915_private *dev_priv = dev->dev_private;
  5259. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5260. struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
  5261. int ret;
  5262. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  5263. if (ret)
  5264. goto err;
  5265. ret = intel_ring_begin(ring, 4);
  5266. if (ret)
  5267. goto err_unpin;
  5268. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | (intel_crtc->plane << 19));
  5269. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  5270. intel_ring_emit(ring, (obj->gtt_offset));
  5271. intel_ring_emit(ring, (MI_NOOP));
  5272. intel_ring_advance(ring);
  5273. return 0;
  5274. err_unpin:
  5275. intel_unpin_fb_obj(obj);
  5276. err:
  5277. return ret;
  5278. }
  5279. static int intel_default_queue_flip(struct drm_device *dev,
  5280. struct drm_crtc *crtc,
  5281. struct drm_framebuffer *fb,
  5282. struct drm_i915_gem_object *obj)
  5283. {
  5284. return -ENODEV;
  5285. }
  5286. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  5287. struct drm_framebuffer *fb,
  5288. struct drm_pending_vblank_event *event)
  5289. {
  5290. struct drm_device *dev = crtc->dev;
  5291. struct drm_i915_private *dev_priv = dev->dev_private;
  5292. struct intel_framebuffer *intel_fb;
  5293. struct drm_i915_gem_object *obj;
  5294. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5295. struct intel_unpin_work *work;
  5296. unsigned long flags;
  5297. int ret;
  5298. work = kzalloc(sizeof *work, GFP_KERNEL);
  5299. if (work == NULL)
  5300. return -ENOMEM;
  5301. work->event = event;
  5302. work->dev = crtc->dev;
  5303. intel_fb = to_intel_framebuffer(crtc->fb);
  5304. work->old_fb_obj = intel_fb->obj;
  5305. INIT_WORK(&work->work, intel_unpin_work_fn);
  5306. ret = drm_vblank_get(dev, intel_crtc->pipe);
  5307. if (ret)
  5308. goto free_work;
  5309. /* We borrow the event spin lock for protecting unpin_work */
  5310. spin_lock_irqsave(&dev->event_lock, flags);
  5311. if (intel_crtc->unpin_work) {
  5312. spin_unlock_irqrestore(&dev->event_lock, flags);
  5313. kfree(work);
  5314. drm_vblank_put(dev, intel_crtc->pipe);
  5315. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  5316. return -EBUSY;
  5317. }
  5318. intel_crtc->unpin_work = work;
  5319. spin_unlock_irqrestore(&dev->event_lock, flags);
  5320. intel_fb = to_intel_framebuffer(fb);
  5321. obj = intel_fb->obj;
  5322. mutex_lock(&dev->struct_mutex);
  5323. /* Reference the objects for the scheduled work. */
  5324. drm_gem_object_reference(&work->old_fb_obj->base);
  5325. drm_gem_object_reference(&obj->base);
  5326. crtc->fb = fb;
  5327. work->pending_flip_obj = obj;
  5328. work->enable_stall_check = true;
  5329. /* Block clients from rendering to the new back buffer until
  5330. * the flip occurs and the object is no longer visible.
  5331. */
  5332. atomic_add(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
  5333. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
  5334. if (ret)
  5335. goto cleanup_pending;
  5336. intel_disable_fbc(dev);
  5337. intel_mark_busy(dev, obj);
  5338. mutex_unlock(&dev->struct_mutex);
  5339. trace_i915_flip_request(intel_crtc->plane, obj);
  5340. return 0;
  5341. cleanup_pending:
  5342. atomic_sub(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
  5343. drm_gem_object_unreference(&work->old_fb_obj->base);
  5344. drm_gem_object_unreference(&obj->base);
  5345. mutex_unlock(&dev->struct_mutex);
  5346. spin_lock_irqsave(&dev->event_lock, flags);
  5347. intel_crtc->unpin_work = NULL;
  5348. spin_unlock_irqrestore(&dev->event_lock, flags);
  5349. drm_vblank_put(dev, intel_crtc->pipe);
  5350. free_work:
  5351. kfree(work);
  5352. return ret;
  5353. }
  5354. static void intel_sanitize_modesetting(struct drm_device *dev,
  5355. int pipe, int plane)
  5356. {
  5357. struct drm_i915_private *dev_priv = dev->dev_private;
  5358. u32 reg, val;
  5359. int i;
  5360. /* Clear any frame start delays used for debugging left by the BIOS */
  5361. for_each_pipe(i) {
  5362. reg = PIPECONF(i);
  5363. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  5364. }
  5365. if (HAS_PCH_SPLIT(dev))
  5366. return;
  5367. /* Who knows what state these registers were left in by the BIOS or
  5368. * grub?
  5369. *
  5370. * If we leave the registers in a conflicting state (e.g. with the
  5371. * display plane reading from the other pipe than the one we intend
  5372. * to use) then when we attempt to teardown the active mode, we will
  5373. * not disable the pipes and planes in the correct order -- leaving
  5374. * a plane reading from a disabled pipe and possibly leading to
  5375. * undefined behaviour.
  5376. */
  5377. reg = DSPCNTR(plane);
  5378. val = I915_READ(reg);
  5379. if ((val & DISPLAY_PLANE_ENABLE) == 0)
  5380. return;
  5381. if (!!(val & DISPPLANE_SEL_PIPE_MASK) == pipe)
  5382. return;
  5383. /* This display plane is active and attached to the other CPU pipe. */
  5384. pipe = !pipe;
  5385. /* Disable the plane and wait for it to stop reading from the pipe. */
  5386. intel_disable_plane(dev_priv, plane, pipe);
  5387. intel_disable_pipe(dev_priv, pipe);
  5388. }
  5389. static void intel_crtc_reset(struct drm_crtc *crtc)
  5390. {
  5391. struct drm_device *dev = crtc->dev;
  5392. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5393. /* Reset flags back to the 'unknown' status so that they
  5394. * will be correctly set on the initial modeset.
  5395. */
  5396. intel_crtc->dpms_mode = -1;
  5397. /* We need to fix up any BIOS configuration that conflicts with
  5398. * our expectations.
  5399. */
  5400. intel_sanitize_modesetting(dev, intel_crtc->pipe, intel_crtc->plane);
  5401. }
  5402. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  5403. .dpms = intel_crtc_dpms,
  5404. .mode_fixup = intel_crtc_mode_fixup,
  5405. .mode_set = intel_crtc_mode_set,
  5406. .mode_set_base = intel_pipe_set_base,
  5407. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  5408. .load_lut = intel_crtc_load_lut,
  5409. .disable = intel_crtc_disable,
  5410. };
  5411. static const struct drm_crtc_funcs intel_crtc_funcs = {
  5412. .reset = intel_crtc_reset,
  5413. .cursor_set = intel_crtc_cursor_set,
  5414. .cursor_move = intel_crtc_cursor_move,
  5415. .gamma_set = intel_crtc_gamma_set,
  5416. .set_config = drm_crtc_helper_set_config,
  5417. .destroy = intel_crtc_destroy,
  5418. .page_flip = intel_crtc_page_flip,
  5419. };
  5420. static void intel_pch_pll_init(struct drm_device *dev)
  5421. {
  5422. drm_i915_private_t *dev_priv = dev->dev_private;
  5423. int i;
  5424. if (dev_priv->num_pch_pll == 0) {
  5425. DRM_DEBUG_KMS("No PCH PLLs on this hardware, skipping initialisation\n");
  5426. return;
  5427. }
  5428. for (i = 0; i < dev_priv->num_pch_pll; i++) {
  5429. dev_priv->pch_plls[i].pll_reg = _PCH_DPLL(i);
  5430. dev_priv->pch_plls[i].fp0_reg = _PCH_FP0(i);
  5431. dev_priv->pch_plls[i].fp1_reg = _PCH_FP1(i);
  5432. }
  5433. }
  5434. static void intel_crtc_init(struct drm_device *dev, int pipe)
  5435. {
  5436. drm_i915_private_t *dev_priv = dev->dev_private;
  5437. struct intel_crtc *intel_crtc;
  5438. int i;
  5439. intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  5440. if (intel_crtc == NULL)
  5441. return;
  5442. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  5443. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  5444. for (i = 0; i < 256; i++) {
  5445. intel_crtc->lut_r[i] = i;
  5446. intel_crtc->lut_g[i] = i;
  5447. intel_crtc->lut_b[i] = i;
  5448. }
  5449. /* Swap pipes & planes for FBC on pre-965 */
  5450. intel_crtc->pipe = pipe;
  5451. intel_crtc->plane = pipe;
  5452. if (IS_MOBILE(dev) && IS_GEN3(dev)) {
  5453. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  5454. intel_crtc->plane = !pipe;
  5455. }
  5456. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  5457. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  5458. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  5459. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  5460. intel_crtc_reset(&intel_crtc->base);
  5461. intel_crtc->active = true; /* force the pipe off on setup_init_config */
  5462. intel_crtc->bpp = 24; /* default for pre-Ironlake */
  5463. if (HAS_PCH_SPLIT(dev)) {
  5464. intel_helper_funcs.prepare = ironlake_crtc_prepare;
  5465. intel_helper_funcs.commit = ironlake_crtc_commit;
  5466. } else {
  5467. intel_helper_funcs.prepare = i9xx_crtc_prepare;
  5468. intel_helper_funcs.commit = i9xx_crtc_commit;
  5469. }
  5470. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  5471. intel_crtc->busy = false;
  5472. setup_timer(&intel_crtc->idle_timer, intel_crtc_idle_timer,
  5473. (unsigned long)intel_crtc);
  5474. }
  5475. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  5476. struct drm_file *file)
  5477. {
  5478. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  5479. struct drm_mode_object *drmmode_obj;
  5480. struct intel_crtc *crtc;
  5481. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  5482. return -ENODEV;
  5483. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  5484. DRM_MODE_OBJECT_CRTC);
  5485. if (!drmmode_obj) {
  5486. DRM_ERROR("no such CRTC id\n");
  5487. return -EINVAL;
  5488. }
  5489. crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
  5490. pipe_from_crtc_id->pipe = crtc->pipe;
  5491. return 0;
  5492. }
  5493. static int intel_encoder_clones(struct drm_device *dev, int type_mask)
  5494. {
  5495. struct intel_encoder *encoder;
  5496. int index_mask = 0;
  5497. int entry = 0;
  5498. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  5499. if (type_mask & encoder->clone_mask)
  5500. index_mask |= (1 << entry);
  5501. entry++;
  5502. }
  5503. return index_mask;
  5504. }
  5505. static bool has_edp_a(struct drm_device *dev)
  5506. {
  5507. struct drm_i915_private *dev_priv = dev->dev_private;
  5508. if (!IS_MOBILE(dev))
  5509. return false;
  5510. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  5511. return false;
  5512. if (IS_GEN5(dev) &&
  5513. (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
  5514. return false;
  5515. return true;
  5516. }
  5517. static void intel_setup_outputs(struct drm_device *dev)
  5518. {
  5519. struct drm_i915_private *dev_priv = dev->dev_private;
  5520. struct intel_encoder *encoder;
  5521. bool dpd_is_edp = false;
  5522. bool has_lvds;
  5523. has_lvds = intel_lvds_init(dev);
  5524. if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
  5525. /* disable the panel fitter on everything but LVDS */
  5526. I915_WRITE(PFIT_CONTROL, 0);
  5527. }
  5528. if (HAS_PCH_SPLIT(dev)) {
  5529. dpd_is_edp = intel_dpd_is_edp(dev);
  5530. if (has_edp_a(dev))
  5531. intel_dp_init(dev, DP_A);
  5532. if (dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  5533. intel_dp_init(dev, PCH_DP_D);
  5534. }
  5535. intel_crt_init(dev);
  5536. if (IS_HASWELL(dev)) {
  5537. int found;
  5538. /* Haswell uses DDI functions to detect digital outputs */
  5539. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  5540. /* DDI A only supports eDP */
  5541. if (found)
  5542. intel_ddi_init(dev, PORT_A);
  5543. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  5544. * register */
  5545. found = I915_READ(SFUSE_STRAP);
  5546. if (found & SFUSE_STRAP_DDIB_DETECTED)
  5547. intel_ddi_init(dev, PORT_B);
  5548. if (found & SFUSE_STRAP_DDIC_DETECTED)
  5549. intel_ddi_init(dev, PORT_C);
  5550. if (found & SFUSE_STRAP_DDID_DETECTED)
  5551. intel_ddi_init(dev, PORT_D);
  5552. } else if (HAS_PCH_SPLIT(dev)) {
  5553. int found;
  5554. if (I915_READ(HDMIB) & PORT_DETECTED) {
  5555. /* PCH SDVOB multiplex with HDMIB */
  5556. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  5557. if (!found)
  5558. intel_hdmi_init(dev, HDMIB);
  5559. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  5560. intel_dp_init(dev, PCH_DP_B);
  5561. }
  5562. if (I915_READ(HDMIC) & PORT_DETECTED)
  5563. intel_hdmi_init(dev, HDMIC);
  5564. if (I915_READ(HDMID) & PORT_DETECTED)
  5565. intel_hdmi_init(dev, HDMID);
  5566. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  5567. intel_dp_init(dev, PCH_DP_C);
  5568. if (!dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  5569. intel_dp_init(dev, PCH_DP_D);
  5570. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  5571. bool found = false;
  5572. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  5573. DRM_DEBUG_KMS("probing SDVOB\n");
  5574. found = intel_sdvo_init(dev, SDVOB, true);
  5575. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  5576. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  5577. intel_hdmi_init(dev, SDVOB);
  5578. }
  5579. if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
  5580. DRM_DEBUG_KMS("probing DP_B\n");
  5581. intel_dp_init(dev, DP_B);
  5582. }
  5583. }
  5584. /* Before G4X SDVOC doesn't have its own detect register */
  5585. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  5586. DRM_DEBUG_KMS("probing SDVOC\n");
  5587. found = intel_sdvo_init(dev, SDVOC, false);
  5588. }
  5589. if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
  5590. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  5591. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  5592. intel_hdmi_init(dev, SDVOC);
  5593. }
  5594. if (SUPPORTS_INTEGRATED_DP(dev)) {
  5595. DRM_DEBUG_KMS("probing DP_C\n");
  5596. intel_dp_init(dev, DP_C);
  5597. }
  5598. }
  5599. if (SUPPORTS_INTEGRATED_DP(dev) &&
  5600. (I915_READ(DP_D) & DP_DETECTED)) {
  5601. DRM_DEBUG_KMS("probing DP_D\n");
  5602. intel_dp_init(dev, DP_D);
  5603. }
  5604. } else if (IS_GEN2(dev))
  5605. intel_dvo_init(dev);
  5606. if (SUPPORTS_TV(dev))
  5607. intel_tv_init(dev);
  5608. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  5609. encoder->base.possible_crtcs = encoder->crtc_mask;
  5610. encoder->base.possible_clones =
  5611. intel_encoder_clones(dev, encoder->clone_mask);
  5612. }
  5613. /* disable all the possible outputs/crtcs before entering KMS mode */
  5614. drm_helper_disable_unused_functions(dev);
  5615. if (HAS_PCH_SPLIT(dev))
  5616. ironlake_init_pch_refclk(dev);
  5617. }
  5618. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  5619. {
  5620. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  5621. drm_framebuffer_cleanup(fb);
  5622. drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
  5623. kfree(intel_fb);
  5624. }
  5625. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  5626. struct drm_file *file,
  5627. unsigned int *handle)
  5628. {
  5629. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  5630. struct drm_i915_gem_object *obj = intel_fb->obj;
  5631. return drm_gem_handle_create(file, &obj->base, handle);
  5632. }
  5633. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  5634. .destroy = intel_user_framebuffer_destroy,
  5635. .create_handle = intel_user_framebuffer_create_handle,
  5636. };
  5637. int intel_framebuffer_init(struct drm_device *dev,
  5638. struct intel_framebuffer *intel_fb,
  5639. struct drm_mode_fb_cmd2 *mode_cmd,
  5640. struct drm_i915_gem_object *obj)
  5641. {
  5642. int ret;
  5643. if (obj->tiling_mode == I915_TILING_Y)
  5644. return -EINVAL;
  5645. if (mode_cmd->pitches[0] & 63)
  5646. return -EINVAL;
  5647. switch (mode_cmd->pixel_format) {
  5648. case DRM_FORMAT_RGB332:
  5649. case DRM_FORMAT_RGB565:
  5650. case DRM_FORMAT_XRGB8888:
  5651. case DRM_FORMAT_XBGR8888:
  5652. case DRM_FORMAT_ARGB8888:
  5653. case DRM_FORMAT_XRGB2101010:
  5654. case DRM_FORMAT_ARGB2101010:
  5655. /* RGB formats are common across chipsets */
  5656. break;
  5657. case DRM_FORMAT_YUYV:
  5658. case DRM_FORMAT_UYVY:
  5659. case DRM_FORMAT_YVYU:
  5660. case DRM_FORMAT_VYUY:
  5661. break;
  5662. default:
  5663. DRM_DEBUG_KMS("unsupported pixel format %u\n",
  5664. mode_cmd->pixel_format);
  5665. return -EINVAL;
  5666. }
  5667. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  5668. if (ret) {
  5669. DRM_ERROR("framebuffer init failed %d\n", ret);
  5670. return ret;
  5671. }
  5672. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  5673. intel_fb->obj = obj;
  5674. return 0;
  5675. }
  5676. static struct drm_framebuffer *
  5677. intel_user_framebuffer_create(struct drm_device *dev,
  5678. struct drm_file *filp,
  5679. struct drm_mode_fb_cmd2 *mode_cmd)
  5680. {
  5681. struct drm_i915_gem_object *obj;
  5682. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  5683. mode_cmd->handles[0]));
  5684. if (&obj->base == NULL)
  5685. return ERR_PTR(-ENOENT);
  5686. return intel_framebuffer_create(dev, mode_cmd, obj);
  5687. }
  5688. static const struct drm_mode_config_funcs intel_mode_funcs = {
  5689. .fb_create = intel_user_framebuffer_create,
  5690. .output_poll_changed = intel_fb_output_poll_changed,
  5691. };
  5692. /* Set up chip specific display functions */
  5693. static void intel_init_display(struct drm_device *dev)
  5694. {
  5695. struct drm_i915_private *dev_priv = dev->dev_private;
  5696. /* We always want a DPMS function */
  5697. if (HAS_PCH_SPLIT(dev)) {
  5698. dev_priv->display.dpms = ironlake_crtc_dpms;
  5699. dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
  5700. dev_priv->display.off = ironlake_crtc_off;
  5701. dev_priv->display.update_plane = ironlake_update_plane;
  5702. } else {
  5703. dev_priv->display.dpms = i9xx_crtc_dpms;
  5704. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  5705. dev_priv->display.off = i9xx_crtc_off;
  5706. dev_priv->display.update_plane = i9xx_update_plane;
  5707. }
  5708. /* Returns the core display clock speed */
  5709. if (IS_VALLEYVIEW(dev))
  5710. dev_priv->display.get_display_clock_speed =
  5711. valleyview_get_display_clock_speed;
  5712. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  5713. dev_priv->display.get_display_clock_speed =
  5714. i945_get_display_clock_speed;
  5715. else if (IS_I915G(dev))
  5716. dev_priv->display.get_display_clock_speed =
  5717. i915_get_display_clock_speed;
  5718. else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
  5719. dev_priv->display.get_display_clock_speed =
  5720. i9xx_misc_get_display_clock_speed;
  5721. else if (IS_I915GM(dev))
  5722. dev_priv->display.get_display_clock_speed =
  5723. i915gm_get_display_clock_speed;
  5724. else if (IS_I865G(dev))
  5725. dev_priv->display.get_display_clock_speed =
  5726. i865_get_display_clock_speed;
  5727. else if (IS_I85X(dev))
  5728. dev_priv->display.get_display_clock_speed =
  5729. i855_get_display_clock_speed;
  5730. else /* 852, 830 */
  5731. dev_priv->display.get_display_clock_speed =
  5732. i830_get_display_clock_speed;
  5733. if (HAS_PCH_SPLIT(dev)) {
  5734. if (IS_GEN5(dev)) {
  5735. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  5736. dev_priv->display.write_eld = ironlake_write_eld;
  5737. } else if (IS_GEN6(dev)) {
  5738. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  5739. dev_priv->display.write_eld = ironlake_write_eld;
  5740. } else if (IS_IVYBRIDGE(dev)) {
  5741. /* FIXME: detect B0+ stepping and use auto training */
  5742. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  5743. dev_priv->display.write_eld = ironlake_write_eld;
  5744. } else if (IS_HASWELL(dev)) {
  5745. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  5746. dev_priv->display.write_eld = ironlake_write_eld;
  5747. } else
  5748. dev_priv->display.update_wm = NULL;
  5749. } else if (IS_VALLEYVIEW(dev)) {
  5750. dev_priv->display.force_wake_get = vlv_force_wake_get;
  5751. dev_priv->display.force_wake_put = vlv_force_wake_put;
  5752. } else if (IS_G4X(dev)) {
  5753. dev_priv->display.write_eld = g4x_write_eld;
  5754. }
  5755. /* Default just returns -ENODEV to indicate unsupported */
  5756. dev_priv->display.queue_flip = intel_default_queue_flip;
  5757. switch (INTEL_INFO(dev)->gen) {
  5758. case 2:
  5759. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  5760. break;
  5761. case 3:
  5762. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  5763. break;
  5764. case 4:
  5765. case 5:
  5766. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  5767. break;
  5768. case 6:
  5769. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  5770. break;
  5771. case 7:
  5772. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  5773. break;
  5774. }
  5775. }
  5776. /*
  5777. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  5778. * resume, or other times. This quirk makes sure that's the case for
  5779. * affected systems.
  5780. */
  5781. static void quirk_pipea_force(struct drm_device *dev)
  5782. {
  5783. struct drm_i915_private *dev_priv = dev->dev_private;
  5784. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  5785. DRM_INFO("applying pipe a force quirk\n");
  5786. }
  5787. /*
  5788. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  5789. */
  5790. static void quirk_ssc_force_disable(struct drm_device *dev)
  5791. {
  5792. struct drm_i915_private *dev_priv = dev->dev_private;
  5793. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  5794. DRM_INFO("applying lvds SSC disable quirk\n");
  5795. }
  5796. /*
  5797. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  5798. * brightness value
  5799. */
  5800. static void quirk_invert_brightness(struct drm_device *dev)
  5801. {
  5802. struct drm_i915_private *dev_priv = dev->dev_private;
  5803. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  5804. DRM_INFO("applying inverted panel brightness quirk\n");
  5805. }
  5806. struct intel_quirk {
  5807. int device;
  5808. int subsystem_vendor;
  5809. int subsystem_device;
  5810. void (*hook)(struct drm_device *dev);
  5811. };
  5812. static struct intel_quirk intel_quirks[] = {
  5813. /* HP Mini needs pipe A force quirk (LP: #322104) */
  5814. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  5815. /* Thinkpad R31 needs pipe A force quirk */
  5816. { 0x3577, 0x1014, 0x0505, quirk_pipea_force },
  5817. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  5818. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  5819. /* ThinkPad X30 needs pipe A force quirk (LP: #304614) */
  5820. { 0x3577, 0x1014, 0x0513, quirk_pipea_force },
  5821. /* ThinkPad X40 needs pipe A force quirk */
  5822. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  5823. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  5824. /* 855 & before need to leave pipe A & dpll A up */
  5825. { 0x3582, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  5826. { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  5827. /* Lenovo U160 cannot use SSC on LVDS */
  5828. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  5829. /* Sony Vaio Y cannot use SSC on LVDS */
  5830. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  5831. /* Acer Aspire 5734Z must invert backlight brightness */
  5832. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  5833. };
  5834. static void intel_init_quirks(struct drm_device *dev)
  5835. {
  5836. struct pci_dev *d = dev->pdev;
  5837. int i;
  5838. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  5839. struct intel_quirk *q = &intel_quirks[i];
  5840. if (d->device == q->device &&
  5841. (d->subsystem_vendor == q->subsystem_vendor ||
  5842. q->subsystem_vendor == PCI_ANY_ID) &&
  5843. (d->subsystem_device == q->subsystem_device ||
  5844. q->subsystem_device == PCI_ANY_ID))
  5845. q->hook(dev);
  5846. }
  5847. }
  5848. /* Disable the VGA plane that we never use */
  5849. static void i915_disable_vga(struct drm_device *dev)
  5850. {
  5851. struct drm_i915_private *dev_priv = dev->dev_private;
  5852. u8 sr1;
  5853. u32 vga_reg;
  5854. if (HAS_PCH_SPLIT(dev))
  5855. vga_reg = CPU_VGACNTRL;
  5856. else
  5857. vga_reg = VGACNTRL;
  5858. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  5859. outb(SR01, VGA_SR_INDEX);
  5860. sr1 = inb(VGA_SR_DATA);
  5861. outb(sr1 | 1<<5, VGA_SR_DATA);
  5862. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  5863. udelay(300);
  5864. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  5865. POSTING_READ(vga_reg);
  5866. }
  5867. static void ivb_pch_pwm_override(struct drm_device *dev)
  5868. {
  5869. struct drm_i915_private *dev_priv = dev->dev_private;
  5870. /*
  5871. * IVB has CPU eDP backlight regs too, set things up to let the
  5872. * PCH regs control the backlight
  5873. */
  5874. I915_WRITE(BLC_PWM_CPU_CTL2, PWM_ENABLE);
  5875. I915_WRITE(BLC_PWM_CPU_CTL, 0);
  5876. I915_WRITE(BLC_PWM_PCH_CTL1, PWM_ENABLE | (1<<30));
  5877. }
  5878. void intel_modeset_init_hw(struct drm_device *dev)
  5879. {
  5880. struct drm_i915_private *dev_priv = dev->dev_private;
  5881. intel_init_clock_gating(dev);
  5882. if (IS_IRONLAKE_M(dev)) {
  5883. ironlake_enable_drps(dev);
  5884. ironlake_enable_rc6(dev);
  5885. intel_init_emon(dev);
  5886. }
  5887. if ((IS_GEN6(dev) || IS_GEN7(dev)) && !IS_VALLEYVIEW(dev)) {
  5888. gen6_enable_rps(dev_priv);
  5889. gen6_update_ring_freq(dev_priv);
  5890. }
  5891. if (IS_IVYBRIDGE(dev))
  5892. ivb_pch_pwm_override(dev);
  5893. }
  5894. void intel_modeset_init(struct drm_device *dev)
  5895. {
  5896. struct drm_i915_private *dev_priv = dev->dev_private;
  5897. int i, ret;
  5898. drm_mode_config_init(dev);
  5899. dev->mode_config.min_width = 0;
  5900. dev->mode_config.min_height = 0;
  5901. dev->mode_config.preferred_depth = 24;
  5902. dev->mode_config.prefer_shadow = 1;
  5903. dev->mode_config.funcs = &intel_mode_funcs;
  5904. intel_init_quirks(dev);
  5905. intel_init_pm(dev);
  5906. intel_prepare_ddi(dev);
  5907. intel_init_display(dev);
  5908. if (IS_GEN2(dev)) {
  5909. dev->mode_config.max_width = 2048;
  5910. dev->mode_config.max_height = 2048;
  5911. } else if (IS_GEN3(dev)) {
  5912. dev->mode_config.max_width = 4096;
  5913. dev->mode_config.max_height = 4096;
  5914. } else {
  5915. dev->mode_config.max_width = 8192;
  5916. dev->mode_config.max_height = 8192;
  5917. }
  5918. dev->mode_config.fb_base = dev->agp->base;
  5919. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  5920. dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
  5921. for (i = 0; i < dev_priv->num_pipe; i++) {
  5922. intel_crtc_init(dev, i);
  5923. ret = intel_plane_init(dev, i);
  5924. if (ret)
  5925. DRM_DEBUG_KMS("plane %d init failed: %d\n", i, ret);
  5926. }
  5927. intel_pch_pll_init(dev);
  5928. /* Just disable it once at startup */
  5929. i915_disable_vga(dev);
  5930. intel_setup_outputs(dev);
  5931. INIT_WORK(&dev_priv->idle_work, intel_idle_update);
  5932. setup_timer(&dev_priv->idle_timer, intel_gpu_idle_timer,
  5933. (unsigned long)dev);
  5934. }
  5935. void intel_modeset_gem_init(struct drm_device *dev)
  5936. {
  5937. intel_modeset_init_hw(dev);
  5938. intel_setup_overlay(dev);
  5939. }
  5940. void intel_modeset_cleanup(struct drm_device *dev)
  5941. {
  5942. struct drm_i915_private *dev_priv = dev->dev_private;
  5943. struct drm_crtc *crtc;
  5944. struct intel_crtc *intel_crtc;
  5945. drm_kms_helper_poll_fini(dev);
  5946. mutex_lock(&dev->struct_mutex);
  5947. intel_unregister_dsm_handler();
  5948. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5949. /* Skip inactive CRTCs */
  5950. if (!crtc->fb)
  5951. continue;
  5952. intel_crtc = to_intel_crtc(crtc);
  5953. intel_increase_pllclock(crtc);
  5954. }
  5955. intel_disable_fbc(dev);
  5956. if (IS_IRONLAKE_M(dev))
  5957. ironlake_disable_drps(dev);
  5958. if ((IS_GEN6(dev) || IS_GEN7(dev)) && !IS_VALLEYVIEW(dev))
  5959. gen6_disable_rps(dev);
  5960. if (IS_IRONLAKE_M(dev))
  5961. ironlake_disable_rc6(dev);
  5962. if (IS_VALLEYVIEW(dev))
  5963. vlv_init_dpio(dev);
  5964. mutex_unlock(&dev->struct_mutex);
  5965. /* Disable the irq before mode object teardown, for the irq might
  5966. * enqueue unpin/hotplug work. */
  5967. drm_irq_uninstall(dev);
  5968. cancel_work_sync(&dev_priv->hotplug_work);
  5969. cancel_work_sync(&dev_priv->rps_work);
  5970. /* flush any delayed tasks or pending work */
  5971. flush_scheduled_work();
  5972. /* Shut off idle work before the crtcs get freed. */
  5973. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5974. intel_crtc = to_intel_crtc(crtc);
  5975. del_timer_sync(&intel_crtc->idle_timer);
  5976. }
  5977. del_timer_sync(&dev_priv->idle_timer);
  5978. cancel_work_sync(&dev_priv->idle_work);
  5979. drm_mode_config_cleanup(dev);
  5980. }
  5981. /*
  5982. * Return which encoder is currently attached for connector.
  5983. */
  5984. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  5985. {
  5986. return &intel_attached_encoder(connector)->base;
  5987. }
  5988. void intel_connector_attach_encoder(struct intel_connector *connector,
  5989. struct intel_encoder *encoder)
  5990. {
  5991. connector->encoder = encoder;
  5992. drm_mode_connector_attach_encoder(&connector->base,
  5993. &encoder->base);
  5994. }
  5995. /*
  5996. * set vga decode state - true == enable VGA decode
  5997. */
  5998. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  5999. {
  6000. struct drm_i915_private *dev_priv = dev->dev_private;
  6001. u16 gmch_ctrl;
  6002. pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
  6003. if (state)
  6004. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  6005. else
  6006. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  6007. pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
  6008. return 0;
  6009. }
  6010. #ifdef CONFIG_DEBUG_FS
  6011. #include <linux/seq_file.h>
  6012. struct intel_display_error_state {
  6013. struct intel_cursor_error_state {
  6014. u32 control;
  6015. u32 position;
  6016. u32 base;
  6017. u32 size;
  6018. } cursor[2];
  6019. struct intel_pipe_error_state {
  6020. u32 conf;
  6021. u32 source;
  6022. u32 htotal;
  6023. u32 hblank;
  6024. u32 hsync;
  6025. u32 vtotal;
  6026. u32 vblank;
  6027. u32 vsync;
  6028. } pipe[2];
  6029. struct intel_plane_error_state {
  6030. u32 control;
  6031. u32 stride;
  6032. u32 size;
  6033. u32 pos;
  6034. u32 addr;
  6035. u32 surface;
  6036. u32 tile_offset;
  6037. } plane[2];
  6038. };
  6039. struct intel_display_error_state *
  6040. intel_display_capture_error_state(struct drm_device *dev)
  6041. {
  6042. drm_i915_private_t *dev_priv = dev->dev_private;
  6043. struct intel_display_error_state *error;
  6044. int i;
  6045. error = kmalloc(sizeof(*error), GFP_ATOMIC);
  6046. if (error == NULL)
  6047. return NULL;
  6048. for (i = 0; i < 2; i++) {
  6049. error->cursor[i].control = I915_READ(CURCNTR(i));
  6050. error->cursor[i].position = I915_READ(CURPOS(i));
  6051. error->cursor[i].base = I915_READ(CURBASE(i));
  6052. error->plane[i].control = I915_READ(DSPCNTR(i));
  6053. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  6054. error->plane[i].size = I915_READ(DSPSIZE(i));
  6055. error->plane[i].pos = I915_READ(DSPPOS(i));
  6056. error->plane[i].addr = I915_READ(DSPADDR(i));
  6057. if (INTEL_INFO(dev)->gen >= 4) {
  6058. error->plane[i].surface = I915_READ(DSPSURF(i));
  6059. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  6060. }
  6061. error->pipe[i].conf = I915_READ(PIPECONF(i));
  6062. error->pipe[i].source = I915_READ(PIPESRC(i));
  6063. error->pipe[i].htotal = I915_READ(HTOTAL(i));
  6064. error->pipe[i].hblank = I915_READ(HBLANK(i));
  6065. error->pipe[i].hsync = I915_READ(HSYNC(i));
  6066. error->pipe[i].vtotal = I915_READ(VTOTAL(i));
  6067. error->pipe[i].vblank = I915_READ(VBLANK(i));
  6068. error->pipe[i].vsync = I915_READ(VSYNC(i));
  6069. }
  6070. return error;
  6071. }
  6072. void
  6073. intel_display_print_error_state(struct seq_file *m,
  6074. struct drm_device *dev,
  6075. struct intel_display_error_state *error)
  6076. {
  6077. int i;
  6078. for (i = 0; i < 2; i++) {
  6079. seq_printf(m, "Pipe [%d]:\n", i);
  6080. seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
  6081. seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
  6082. seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
  6083. seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
  6084. seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
  6085. seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
  6086. seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
  6087. seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
  6088. seq_printf(m, "Plane [%d]:\n", i);
  6089. seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
  6090. seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  6091. seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
  6092. seq_printf(m, " POS: %08x\n", error->plane[i].pos);
  6093. seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  6094. if (INTEL_INFO(dev)->gen >= 4) {
  6095. seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
  6096. seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  6097. }
  6098. seq_printf(m, "Cursor [%d]:\n", i);
  6099. seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  6100. seq_printf(m, " POS: %08x\n", error->cursor[i].position);
  6101. seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
  6102. }
  6103. }
  6104. #endif