intel_display.c 190 KB

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