intel_display.c 185 KB

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