intel_display.c 182 KB

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