intel_display.c 268 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897
  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/cpufreq.h>
  28. #include <linux/module.h>
  29. #include <linux/input.h>
  30. #include <linux/i2c.h>
  31. #include <linux/kernel.h>
  32. #include <linux/slab.h>
  33. #include <linux/vgaarb.h>
  34. #include <drm/drm_edid.h>
  35. #include "drmP.h"
  36. #include "intel_drv.h"
  37. #include "i915_drm.h"
  38. #include "i915_drv.h"
  39. #include "i915_trace.h"
  40. #include "drm_dp_helper.h"
  41. #include "drm_crtc_helper.h"
  42. #include <linux/dma_remapping.h>
  43. #define HAS_eDP (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  44. bool intel_pipe_has_type(struct drm_crtc *crtc, int type);
  45. static void intel_increase_pllclock(struct drm_crtc *crtc);
  46. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  47. typedef struct {
  48. /* given values */
  49. int n;
  50. int m1, m2;
  51. int p1, p2;
  52. /* derived values */
  53. int dot;
  54. int vco;
  55. int m;
  56. int p;
  57. } intel_clock_t;
  58. typedef struct {
  59. int min, max;
  60. } intel_range_t;
  61. typedef struct {
  62. int dot_limit;
  63. int p2_slow, p2_fast;
  64. } intel_p2_t;
  65. #define INTEL_P2_NUM 2
  66. typedef struct intel_limit intel_limit_t;
  67. struct intel_limit {
  68. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  69. intel_p2_t p2;
  70. bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
  71. int, int, intel_clock_t *, intel_clock_t *);
  72. };
  73. /* FDI */
  74. #define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
  75. static bool
  76. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  77. int target, int refclk, intel_clock_t *match_clock,
  78. intel_clock_t *best_clock);
  79. static bool
  80. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  81. int target, int refclk, intel_clock_t *match_clock,
  82. intel_clock_t *best_clock);
  83. static bool
  84. intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
  85. int target, int refclk, intel_clock_t *match_clock,
  86. intel_clock_t *best_clock);
  87. static bool
  88. intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
  89. int target, int refclk, intel_clock_t *match_clock,
  90. intel_clock_t *best_clock);
  91. static inline u32 /* units of 100MHz */
  92. intel_fdi_link_freq(struct drm_device *dev)
  93. {
  94. if (IS_GEN5(dev)) {
  95. struct drm_i915_private *dev_priv = dev->dev_private;
  96. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  97. } else
  98. return 27;
  99. }
  100. static const intel_limit_t intel_limits_i8xx_dvo = {
  101. .dot = { .min = 25000, .max = 350000 },
  102. .vco = { .min = 930000, .max = 1400000 },
  103. .n = { .min = 3, .max = 16 },
  104. .m = { .min = 96, .max = 140 },
  105. .m1 = { .min = 18, .max = 26 },
  106. .m2 = { .min = 6, .max = 16 },
  107. .p = { .min = 4, .max = 128 },
  108. .p1 = { .min = 2, .max = 33 },
  109. .p2 = { .dot_limit = 165000,
  110. .p2_slow = 4, .p2_fast = 2 },
  111. .find_pll = intel_find_best_PLL,
  112. };
  113. static const intel_limit_t intel_limits_i8xx_lvds = {
  114. .dot = { .min = 25000, .max = 350000 },
  115. .vco = { .min = 930000, .max = 1400000 },
  116. .n = { .min = 3, .max = 16 },
  117. .m = { .min = 96, .max = 140 },
  118. .m1 = { .min = 18, .max = 26 },
  119. .m2 = { .min = 6, .max = 16 },
  120. .p = { .min = 4, .max = 128 },
  121. .p1 = { .min = 1, .max = 6 },
  122. .p2 = { .dot_limit = 165000,
  123. .p2_slow = 14, .p2_fast = 7 },
  124. .find_pll = intel_find_best_PLL,
  125. };
  126. static const intel_limit_t intel_limits_i9xx_sdvo = {
  127. .dot = { .min = 20000, .max = 400000 },
  128. .vco = { .min = 1400000, .max = 2800000 },
  129. .n = { .min = 1, .max = 6 },
  130. .m = { .min = 70, .max = 120 },
  131. .m1 = { .min = 10, .max = 22 },
  132. .m2 = { .min = 5, .max = 9 },
  133. .p = { .min = 5, .max = 80 },
  134. .p1 = { .min = 1, .max = 8 },
  135. .p2 = { .dot_limit = 200000,
  136. .p2_slow = 10, .p2_fast = 5 },
  137. .find_pll = intel_find_best_PLL,
  138. };
  139. static const intel_limit_t intel_limits_i9xx_lvds = {
  140. .dot = { .min = 20000, .max = 400000 },
  141. .vco = { .min = 1400000, .max = 2800000 },
  142. .n = { .min = 1, .max = 6 },
  143. .m = { .min = 70, .max = 120 },
  144. .m1 = { .min = 10, .max = 22 },
  145. .m2 = { .min = 5, .max = 9 },
  146. .p = { .min = 7, .max = 98 },
  147. .p1 = { .min = 1, .max = 8 },
  148. .p2 = { .dot_limit = 112000,
  149. .p2_slow = 14, .p2_fast = 7 },
  150. .find_pll = intel_find_best_PLL,
  151. };
  152. static const intel_limit_t intel_limits_g4x_sdvo = {
  153. .dot = { .min = 25000, .max = 270000 },
  154. .vco = { .min = 1750000, .max = 3500000},
  155. .n = { .min = 1, .max = 4 },
  156. .m = { .min = 104, .max = 138 },
  157. .m1 = { .min = 17, .max = 23 },
  158. .m2 = { .min = 5, .max = 11 },
  159. .p = { .min = 10, .max = 30 },
  160. .p1 = { .min = 1, .max = 3},
  161. .p2 = { .dot_limit = 270000,
  162. .p2_slow = 10,
  163. .p2_fast = 10
  164. },
  165. .find_pll = intel_g4x_find_best_PLL,
  166. };
  167. static const intel_limit_t intel_limits_g4x_hdmi = {
  168. .dot = { .min = 22000, .max = 400000 },
  169. .vco = { .min = 1750000, .max = 3500000},
  170. .n = { .min = 1, .max = 4 },
  171. .m = { .min = 104, .max = 138 },
  172. .m1 = { .min = 16, .max = 23 },
  173. .m2 = { .min = 5, .max = 11 },
  174. .p = { .min = 5, .max = 80 },
  175. .p1 = { .min = 1, .max = 8},
  176. .p2 = { .dot_limit = 165000,
  177. .p2_slow = 10, .p2_fast = 5 },
  178. .find_pll = intel_g4x_find_best_PLL,
  179. };
  180. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  181. .dot = { .min = 20000, .max = 115000 },
  182. .vco = { .min = 1750000, .max = 3500000 },
  183. .n = { .min = 1, .max = 3 },
  184. .m = { .min = 104, .max = 138 },
  185. .m1 = { .min = 17, .max = 23 },
  186. .m2 = { .min = 5, .max = 11 },
  187. .p = { .min = 28, .max = 112 },
  188. .p1 = { .min = 2, .max = 8 },
  189. .p2 = { .dot_limit = 0,
  190. .p2_slow = 14, .p2_fast = 14
  191. },
  192. .find_pll = intel_g4x_find_best_PLL,
  193. };
  194. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  195. .dot = { .min = 80000, .max = 224000 },
  196. .vco = { .min = 1750000, .max = 3500000 },
  197. .n = { .min = 1, .max = 3 },
  198. .m = { .min = 104, .max = 138 },
  199. .m1 = { .min = 17, .max = 23 },
  200. .m2 = { .min = 5, .max = 11 },
  201. .p = { .min = 14, .max = 42 },
  202. .p1 = { .min = 2, .max = 6 },
  203. .p2 = { .dot_limit = 0,
  204. .p2_slow = 7, .p2_fast = 7
  205. },
  206. .find_pll = intel_g4x_find_best_PLL,
  207. };
  208. static const intel_limit_t intel_limits_g4x_display_port = {
  209. .dot = { .min = 161670, .max = 227000 },
  210. .vco = { .min = 1750000, .max = 3500000},
  211. .n = { .min = 1, .max = 2 },
  212. .m = { .min = 97, .max = 108 },
  213. .m1 = { .min = 0x10, .max = 0x12 },
  214. .m2 = { .min = 0x05, .max = 0x06 },
  215. .p = { .min = 10, .max = 20 },
  216. .p1 = { .min = 1, .max = 2},
  217. .p2 = { .dot_limit = 0,
  218. .p2_slow = 10, .p2_fast = 10 },
  219. .find_pll = intel_find_pll_g4x_dp,
  220. };
  221. static const intel_limit_t intel_limits_pineview_sdvo = {
  222. .dot = { .min = 20000, .max = 400000},
  223. .vco = { .min = 1700000, .max = 3500000 },
  224. /* Pineview's Ncounter is a ring counter */
  225. .n = { .min = 3, .max = 6 },
  226. .m = { .min = 2, .max = 256 },
  227. /* Pineview only has one combined m divider, which we treat as m2. */
  228. .m1 = { .min = 0, .max = 0 },
  229. .m2 = { .min = 0, .max = 254 },
  230. .p = { .min = 5, .max = 80 },
  231. .p1 = { .min = 1, .max = 8 },
  232. .p2 = { .dot_limit = 200000,
  233. .p2_slow = 10, .p2_fast = 5 },
  234. .find_pll = intel_find_best_PLL,
  235. };
  236. static const intel_limit_t intel_limits_pineview_lvds = {
  237. .dot = { .min = 20000, .max = 400000 },
  238. .vco = { .min = 1700000, .max = 3500000 },
  239. .n = { .min = 3, .max = 6 },
  240. .m = { .min = 2, .max = 256 },
  241. .m1 = { .min = 0, .max = 0 },
  242. .m2 = { .min = 0, .max = 254 },
  243. .p = { .min = 7, .max = 112 },
  244. .p1 = { .min = 1, .max = 8 },
  245. .p2 = { .dot_limit = 112000,
  246. .p2_slow = 14, .p2_fast = 14 },
  247. .find_pll = intel_find_best_PLL,
  248. };
  249. /* Ironlake / Sandybridge
  250. *
  251. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  252. * the range value for them is (actual_value - 2).
  253. */
  254. static const intel_limit_t intel_limits_ironlake_dac = {
  255. .dot = { .min = 25000, .max = 350000 },
  256. .vco = { .min = 1760000, .max = 3510000 },
  257. .n = { .min = 1, .max = 5 },
  258. .m = { .min = 79, .max = 127 },
  259. .m1 = { .min = 12, .max = 22 },
  260. .m2 = { .min = 5, .max = 9 },
  261. .p = { .min = 5, .max = 80 },
  262. .p1 = { .min = 1, .max = 8 },
  263. .p2 = { .dot_limit = 225000,
  264. .p2_slow = 10, .p2_fast = 5 },
  265. .find_pll = intel_g4x_find_best_PLL,
  266. };
  267. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  268. .dot = { .min = 25000, .max = 350000 },
  269. .vco = { .min = 1760000, .max = 3510000 },
  270. .n = { .min = 1, .max = 3 },
  271. .m = { .min = 79, .max = 118 },
  272. .m1 = { .min = 12, .max = 22 },
  273. .m2 = { .min = 5, .max = 9 },
  274. .p = { .min = 28, .max = 112 },
  275. .p1 = { .min = 2, .max = 8 },
  276. .p2 = { .dot_limit = 225000,
  277. .p2_slow = 14, .p2_fast = 14 },
  278. .find_pll = intel_g4x_find_best_PLL,
  279. };
  280. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  281. .dot = { .min = 25000, .max = 350000 },
  282. .vco = { .min = 1760000, .max = 3510000 },
  283. .n = { .min = 1, .max = 3 },
  284. .m = { .min = 79, .max = 127 },
  285. .m1 = { .min = 12, .max = 22 },
  286. .m2 = { .min = 5, .max = 9 },
  287. .p = { .min = 14, .max = 56 },
  288. .p1 = { .min = 2, .max = 8 },
  289. .p2 = { .dot_limit = 225000,
  290. .p2_slow = 7, .p2_fast = 7 },
  291. .find_pll = intel_g4x_find_best_PLL,
  292. };
  293. /* LVDS 100mhz refclk limits. */
  294. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  295. .dot = { .min = 25000, .max = 350000 },
  296. .vco = { .min = 1760000, .max = 3510000 },
  297. .n = { .min = 1, .max = 2 },
  298. .m = { .min = 79, .max = 126 },
  299. .m1 = { .min = 12, .max = 22 },
  300. .m2 = { .min = 5, .max = 9 },
  301. .p = { .min = 28, .max = 112 },
  302. .p1 = { .min = 2, .max = 8 },
  303. .p2 = { .dot_limit = 225000,
  304. .p2_slow = 14, .p2_fast = 14 },
  305. .find_pll = intel_g4x_find_best_PLL,
  306. };
  307. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  308. .dot = { .min = 25000, .max = 350000 },
  309. .vco = { .min = 1760000, .max = 3510000 },
  310. .n = { .min = 1, .max = 3 },
  311. .m = { .min = 79, .max = 126 },
  312. .m1 = { .min = 12, .max = 22 },
  313. .m2 = { .min = 5, .max = 9 },
  314. .p = { .min = 14, .max = 42 },
  315. .p1 = { .min = 2, .max = 6 },
  316. .p2 = { .dot_limit = 225000,
  317. .p2_slow = 7, .p2_fast = 7 },
  318. .find_pll = intel_g4x_find_best_PLL,
  319. };
  320. static const intel_limit_t intel_limits_ironlake_display_port = {
  321. .dot = { .min = 25000, .max = 350000 },
  322. .vco = { .min = 1760000, .max = 3510000},
  323. .n = { .min = 1, .max = 2 },
  324. .m = { .min = 81, .max = 90 },
  325. .m1 = { .min = 12, .max = 22 },
  326. .m2 = { .min = 5, .max = 9 },
  327. .p = { .min = 10, .max = 20 },
  328. .p1 = { .min = 1, .max = 2},
  329. .p2 = { .dot_limit = 0,
  330. .p2_slow = 10, .p2_fast = 10 },
  331. .find_pll = intel_find_pll_ironlake_dp,
  332. };
  333. u32 intel_dpio_read(struct drm_i915_private *dev_priv, int reg)
  334. {
  335. unsigned long flags;
  336. u32 val = 0;
  337. spin_lock_irqsave(&dev_priv->dpio_lock, flags);
  338. if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
  339. DRM_ERROR("DPIO idle wait timed out\n");
  340. goto out_unlock;
  341. }
  342. I915_WRITE(DPIO_REG, reg);
  343. I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_READ | DPIO_PORTID |
  344. DPIO_BYTE);
  345. if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
  346. DRM_ERROR("DPIO read wait timed out\n");
  347. goto out_unlock;
  348. }
  349. val = I915_READ(DPIO_DATA);
  350. out_unlock:
  351. spin_unlock_irqrestore(&dev_priv->dpio_lock, flags);
  352. return val;
  353. }
  354. static void intel_dpio_write(struct drm_i915_private *dev_priv, int reg,
  355. u32 val)
  356. {
  357. unsigned long flags;
  358. spin_lock_irqsave(&dev_priv->dpio_lock, flags);
  359. if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
  360. DRM_ERROR("DPIO idle wait timed out\n");
  361. goto out_unlock;
  362. }
  363. I915_WRITE(DPIO_DATA, val);
  364. I915_WRITE(DPIO_REG, reg);
  365. I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_WRITE | DPIO_PORTID |
  366. DPIO_BYTE);
  367. if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100))
  368. DRM_ERROR("DPIO write wait timed out\n");
  369. out_unlock:
  370. spin_unlock_irqrestore(&dev_priv->dpio_lock, flags);
  371. }
  372. static void vlv_init_dpio(struct drm_device *dev)
  373. {
  374. struct drm_i915_private *dev_priv = dev->dev_private;
  375. /* Reset the DPIO config */
  376. I915_WRITE(DPIO_CTL, 0);
  377. POSTING_READ(DPIO_CTL);
  378. I915_WRITE(DPIO_CTL, 1);
  379. POSTING_READ(DPIO_CTL);
  380. }
  381. static int intel_dual_link_lvds_callback(const struct dmi_system_id *id)
  382. {
  383. DRM_INFO("Forcing lvds to dual link mode on %s\n", id->ident);
  384. return 1;
  385. }
  386. static const struct dmi_system_id intel_dual_link_lvds[] = {
  387. {
  388. .callback = intel_dual_link_lvds_callback,
  389. .ident = "Apple MacBook Pro (Core i5/i7 Series)",
  390. .matches = {
  391. DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
  392. DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro8,2"),
  393. },
  394. },
  395. { } /* terminating entry */
  396. };
  397. static bool is_dual_link_lvds(struct drm_i915_private *dev_priv,
  398. unsigned int reg)
  399. {
  400. unsigned int val;
  401. /* use the module option value if specified */
  402. if (i915_lvds_channel_mode > 0)
  403. return i915_lvds_channel_mode == 2;
  404. if (dmi_check_system(intel_dual_link_lvds))
  405. return true;
  406. if (dev_priv->lvds_val)
  407. val = dev_priv->lvds_val;
  408. else {
  409. /* BIOS should set the proper LVDS register value at boot, but
  410. * in reality, it doesn't set the value when the lid is closed;
  411. * we need to check "the value to be set" in VBT when LVDS
  412. * register is uninitialized.
  413. */
  414. val = I915_READ(reg);
  415. if (!(val & ~LVDS_DETECTED))
  416. val = dev_priv->bios_lvds_val;
  417. dev_priv->lvds_val = val;
  418. }
  419. return (val & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP;
  420. }
  421. static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
  422. int refclk)
  423. {
  424. struct drm_device *dev = crtc->dev;
  425. struct drm_i915_private *dev_priv = dev->dev_private;
  426. const intel_limit_t *limit;
  427. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  428. if (is_dual_link_lvds(dev_priv, PCH_LVDS)) {
  429. /* LVDS dual channel */
  430. if (refclk == 100000)
  431. limit = &intel_limits_ironlake_dual_lvds_100m;
  432. else
  433. limit = &intel_limits_ironlake_dual_lvds;
  434. } else {
  435. if (refclk == 100000)
  436. limit = &intel_limits_ironlake_single_lvds_100m;
  437. else
  438. limit = &intel_limits_ironlake_single_lvds;
  439. }
  440. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  441. HAS_eDP)
  442. limit = &intel_limits_ironlake_display_port;
  443. else
  444. limit = &intel_limits_ironlake_dac;
  445. return limit;
  446. }
  447. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  448. {
  449. struct drm_device *dev = crtc->dev;
  450. struct drm_i915_private *dev_priv = dev->dev_private;
  451. const intel_limit_t *limit;
  452. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  453. if (is_dual_link_lvds(dev_priv, LVDS))
  454. /* LVDS with dual channel */
  455. limit = &intel_limits_g4x_dual_channel_lvds;
  456. else
  457. /* LVDS with dual channel */
  458. limit = &intel_limits_g4x_single_channel_lvds;
  459. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  460. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  461. limit = &intel_limits_g4x_hdmi;
  462. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  463. limit = &intel_limits_g4x_sdvo;
  464. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  465. limit = &intel_limits_g4x_display_port;
  466. } else /* The option is for other outputs */
  467. limit = &intel_limits_i9xx_sdvo;
  468. return limit;
  469. }
  470. static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
  471. {
  472. struct drm_device *dev = crtc->dev;
  473. const intel_limit_t *limit;
  474. if (HAS_PCH_SPLIT(dev))
  475. limit = intel_ironlake_limit(crtc, refclk);
  476. else if (IS_G4X(dev)) {
  477. limit = intel_g4x_limit(crtc);
  478. } else if (IS_PINEVIEW(dev)) {
  479. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  480. limit = &intel_limits_pineview_lvds;
  481. else
  482. limit = &intel_limits_pineview_sdvo;
  483. } else if (!IS_GEN2(dev)) {
  484. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  485. limit = &intel_limits_i9xx_lvds;
  486. else
  487. limit = &intel_limits_i9xx_sdvo;
  488. } else {
  489. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  490. limit = &intel_limits_i8xx_lvds;
  491. else
  492. limit = &intel_limits_i8xx_dvo;
  493. }
  494. return limit;
  495. }
  496. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  497. static void pineview_clock(int refclk, intel_clock_t *clock)
  498. {
  499. clock->m = clock->m2 + 2;
  500. clock->p = clock->p1 * clock->p2;
  501. clock->vco = refclk * clock->m / clock->n;
  502. clock->dot = clock->vco / clock->p;
  503. }
  504. static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
  505. {
  506. if (IS_PINEVIEW(dev)) {
  507. pineview_clock(refclk, clock);
  508. return;
  509. }
  510. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  511. clock->p = clock->p1 * clock->p2;
  512. clock->vco = refclk * clock->m / (clock->n + 2);
  513. clock->dot = clock->vco / clock->p;
  514. }
  515. /**
  516. * Returns whether any output on the specified pipe is of the specified type
  517. */
  518. bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
  519. {
  520. struct drm_device *dev = crtc->dev;
  521. struct drm_mode_config *mode_config = &dev->mode_config;
  522. struct intel_encoder *encoder;
  523. list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
  524. if (encoder->base.crtc == crtc && encoder->type == type)
  525. return true;
  526. return false;
  527. }
  528. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  529. /**
  530. * Returns whether the given set of divisors are valid for a given refclk with
  531. * the given connectors.
  532. */
  533. static bool intel_PLL_is_valid(struct drm_device *dev,
  534. const intel_limit_t *limit,
  535. const intel_clock_t *clock)
  536. {
  537. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  538. INTELPllInvalid("p1 out of range\n");
  539. if (clock->p < limit->p.min || limit->p.max < clock->p)
  540. INTELPllInvalid("p out of range\n");
  541. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  542. INTELPllInvalid("m2 out of range\n");
  543. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  544. INTELPllInvalid("m1 out of range\n");
  545. if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
  546. INTELPllInvalid("m1 <= m2\n");
  547. if (clock->m < limit->m.min || limit->m.max < clock->m)
  548. INTELPllInvalid("m out of range\n");
  549. if (clock->n < limit->n.min || limit->n.max < clock->n)
  550. INTELPllInvalid("n out of range\n");
  551. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  552. INTELPllInvalid("vco out of range\n");
  553. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  554. * connector, etc., rather than just a single range.
  555. */
  556. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  557. INTELPllInvalid("dot out of range\n");
  558. return true;
  559. }
  560. static bool
  561. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  562. int target, int refclk, intel_clock_t *match_clock,
  563. intel_clock_t *best_clock)
  564. {
  565. struct drm_device *dev = crtc->dev;
  566. struct drm_i915_private *dev_priv = dev->dev_private;
  567. intel_clock_t clock;
  568. int err = target;
  569. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  570. (I915_READ(LVDS)) != 0) {
  571. /*
  572. * For LVDS, if the panel is on, just rely on its current
  573. * settings for dual-channel. We haven't figured out how to
  574. * reliably set up different single/dual channel state, if we
  575. * even can.
  576. */
  577. if (is_dual_link_lvds(dev_priv, LVDS))
  578. clock.p2 = limit->p2.p2_fast;
  579. else
  580. clock.p2 = limit->p2.p2_slow;
  581. } else {
  582. if (target < limit->p2.dot_limit)
  583. clock.p2 = limit->p2.p2_slow;
  584. else
  585. clock.p2 = limit->p2.p2_fast;
  586. }
  587. memset(best_clock, 0, sizeof(*best_clock));
  588. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  589. clock.m1++) {
  590. for (clock.m2 = limit->m2.min;
  591. clock.m2 <= limit->m2.max; clock.m2++) {
  592. /* m1 is always 0 in Pineview */
  593. if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
  594. break;
  595. for (clock.n = limit->n.min;
  596. clock.n <= limit->n.max; clock.n++) {
  597. for (clock.p1 = limit->p1.min;
  598. clock.p1 <= limit->p1.max; clock.p1++) {
  599. int this_err;
  600. intel_clock(dev, refclk, &clock);
  601. if (!intel_PLL_is_valid(dev, limit,
  602. &clock))
  603. continue;
  604. if (match_clock &&
  605. clock.p != match_clock->p)
  606. continue;
  607. this_err = abs(clock.dot - target);
  608. if (this_err < err) {
  609. *best_clock = clock;
  610. err = this_err;
  611. }
  612. }
  613. }
  614. }
  615. }
  616. return (err != target);
  617. }
  618. static bool
  619. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  620. int target, int refclk, intel_clock_t *match_clock,
  621. intel_clock_t *best_clock)
  622. {
  623. struct drm_device *dev = crtc->dev;
  624. struct drm_i915_private *dev_priv = dev->dev_private;
  625. intel_clock_t clock;
  626. int max_n;
  627. bool found;
  628. /* approximately equals target * 0.00585 */
  629. int err_most = (target >> 8) + (target >> 9);
  630. found = false;
  631. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  632. int lvds_reg;
  633. if (HAS_PCH_SPLIT(dev))
  634. lvds_reg = PCH_LVDS;
  635. else
  636. lvds_reg = LVDS;
  637. if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==
  638. LVDS_CLKB_POWER_UP)
  639. clock.p2 = limit->p2.p2_fast;
  640. else
  641. clock.p2 = limit->p2.p2_slow;
  642. } else {
  643. if (target < limit->p2.dot_limit)
  644. clock.p2 = limit->p2.p2_slow;
  645. else
  646. clock.p2 = limit->p2.p2_fast;
  647. }
  648. memset(best_clock, 0, sizeof(*best_clock));
  649. max_n = limit->n.max;
  650. /* based on hardware requirement, prefer smaller n to precision */
  651. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  652. /* based on hardware requirement, prefere larger m1,m2 */
  653. for (clock.m1 = limit->m1.max;
  654. clock.m1 >= limit->m1.min; clock.m1--) {
  655. for (clock.m2 = limit->m2.max;
  656. clock.m2 >= limit->m2.min; clock.m2--) {
  657. for (clock.p1 = limit->p1.max;
  658. clock.p1 >= limit->p1.min; clock.p1--) {
  659. int this_err;
  660. intel_clock(dev, refclk, &clock);
  661. if (!intel_PLL_is_valid(dev, limit,
  662. &clock))
  663. continue;
  664. if (match_clock &&
  665. clock.p != match_clock->p)
  666. continue;
  667. this_err = abs(clock.dot - target);
  668. if (this_err < err_most) {
  669. *best_clock = clock;
  670. err_most = this_err;
  671. max_n = clock.n;
  672. found = true;
  673. }
  674. }
  675. }
  676. }
  677. }
  678. return found;
  679. }
  680. static bool
  681. intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  682. int target, int refclk, intel_clock_t *match_clock,
  683. intel_clock_t *best_clock)
  684. {
  685. struct drm_device *dev = crtc->dev;
  686. intel_clock_t clock;
  687. if (target < 200000) {
  688. clock.n = 1;
  689. clock.p1 = 2;
  690. clock.p2 = 10;
  691. clock.m1 = 12;
  692. clock.m2 = 9;
  693. } else {
  694. clock.n = 2;
  695. clock.p1 = 1;
  696. clock.p2 = 10;
  697. clock.m1 = 14;
  698. clock.m2 = 8;
  699. }
  700. intel_clock(dev, refclk, &clock);
  701. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  702. return true;
  703. }
  704. /* DisplayPort has only two frequencies, 162MHz and 270MHz */
  705. static bool
  706. intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  707. int target, int refclk, intel_clock_t *match_clock,
  708. intel_clock_t *best_clock)
  709. {
  710. intel_clock_t clock;
  711. if (target < 200000) {
  712. clock.p1 = 2;
  713. clock.p2 = 10;
  714. clock.n = 2;
  715. clock.m1 = 23;
  716. clock.m2 = 8;
  717. } else {
  718. clock.p1 = 1;
  719. clock.p2 = 10;
  720. clock.n = 1;
  721. clock.m1 = 14;
  722. clock.m2 = 2;
  723. }
  724. clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
  725. clock.p = (clock.p1 * clock.p2);
  726. clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
  727. clock.vco = 0;
  728. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  729. return true;
  730. }
  731. /**
  732. * intel_wait_for_vblank - wait for vblank on a given pipe
  733. * @dev: drm device
  734. * @pipe: pipe to wait for
  735. *
  736. * Wait for vblank to occur on a given pipe. Needed for various bits of
  737. * mode setting code.
  738. */
  739. void intel_wait_for_vblank(struct drm_device *dev, int pipe)
  740. {
  741. struct drm_i915_private *dev_priv = dev->dev_private;
  742. int pipestat_reg = PIPESTAT(pipe);
  743. /* Clear existing vblank status. Note this will clear any other
  744. * sticky status fields as well.
  745. *
  746. * This races with i915_driver_irq_handler() with the result
  747. * that either function could miss a vblank event. Here it is not
  748. * fatal, as we will either wait upon the next vblank interrupt or
  749. * timeout. Generally speaking intel_wait_for_vblank() is only
  750. * called during modeset at which time the GPU should be idle and
  751. * should *not* be performing page flips and thus not waiting on
  752. * vblanks...
  753. * Currently, the result of us stealing a vblank from the irq
  754. * handler is that a single frame will be skipped during swapbuffers.
  755. */
  756. I915_WRITE(pipestat_reg,
  757. I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
  758. /* Wait for vblank interrupt bit to set */
  759. if (wait_for(I915_READ(pipestat_reg) &
  760. PIPE_VBLANK_INTERRUPT_STATUS,
  761. 50))
  762. DRM_DEBUG_KMS("vblank wait timed out\n");
  763. }
  764. /*
  765. * intel_wait_for_pipe_off - wait for pipe to turn off
  766. * @dev: drm device
  767. * @pipe: pipe to wait for
  768. *
  769. * After disabling a pipe, we can't wait for vblank in the usual way,
  770. * spinning on the vblank interrupt status bit, since we won't actually
  771. * see an interrupt when the pipe is disabled.
  772. *
  773. * On Gen4 and above:
  774. * wait for the pipe register state bit to turn off
  775. *
  776. * Otherwise:
  777. * wait for the display line value to settle (it usually
  778. * ends up stopping at the start of the next frame).
  779. *
  780. */
  781. void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
  782. {
  783. struct drm_i915_private *dev_priv = dev->dev_private;
  784. if (INTEL_INFO(dev)->gen >= 4) {
  785. int reg = PIPECONF(pipe);
  786. /* Wait for the Pipe State to go off */
  787. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  788. 100))
  789. DRM_DEBUG_KMS("pipe_off wait timed out\n");
  790. } else {
  791. u32 last_line;
  792. int reg = PIPEDSL(pipe);
  793. unsigned long timeout = jiffies + msecs_to_jiffies(100);
  794. /* Wait for the display line to settle */
  795. do {
  796. last_line = I915_READ(reg) & DSL_LINEMASK;
  797. mdelay(5);
  798. } while (((I915_READ(reg) & DSL_LINEMASK) != last_line) &&
  799. time_after(timeout, jiffies));
  800. if (time_after(jiffies, timeout))
  801. DRM_DEBUG_KMS("pipe_off wait timed out\n");
  802. }
  803. }
  804. static const char *state_string(bool enabled)
  805. {
  806. return enabled ? "on" : "off";
  807. }
  808. /* Only for pre-ILK configs */
  809. static void assert_pll(struct drm_i915_private *dev_priv,
  810. enum pipe pipe, bool state)
  811. {
  812. int reg;
  813. u32 val;
  814. bool cur_state;
  815. reg = DPLL(pipe);
  816. val = I915_READ(reg);
  817. cur_state = !!(val & DPLL_VCO_ENABLE);
  818. WARN(cur_state != state,
  819. "PLL state assertion failure (expected %s, current %s)\n",
  820. state_string(state), state_string(cur_state));
  821. }
  822. #define assert_pll_enabled(d, p) assert_pll(d, p, true)
  823. #define assert_pll_disabled(d, p) assert_pll(d, p, false)
  824. /* For ILK+ */
  825. static void assert_pch_pll(struct drm_i915_private *dev_priv,
  826. enum pipe pipe, bool state)
  827. {
  828. int reg;
  829. u32 val;
  830. bool cur_state;
  831. if (HAS_PCH_CPT(dev_priv->dev)) {
  832. u32 pch_dpll;
  833. pch_dpll = I915_READ(PCH_DPLL_SEL);
  834. /* Make sure the selected PLL is enabled to the transcoder */
  835. WARN(!((pch_dpll >> (4 * pipe)) & 8),
  836. "transcoder %d PLL not enabled\n", pipe);
  837. /* Convert the transcoder pipe number to a pll pipe number */
  838. pipe = (pch_dpll >> (4 * pipe)) & 1;
  839. }
  840. reg = PCH_DPLL(pipe);
  841. val = I915_READ(reg);
  842. cur_state = !!(val & DPLL_VCO_ENABLE);
  843. WARN(cur_state != state,
  844. "PCH PLL state assertion failure (expected %s, current %s)\n",
  845. state_string(state), state_string(cur_state));
  846. }
  847. #define assert_pch_pll_enabled(d, p) assert_pch_pll(d, p, true)
  848. #define assert_pch_pll_disabled(d, p) assert_pch_pll(d, p, false)
  849. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  850. enum pipe pipe, bool state)
  851. {
  852. int reg;
  853. u32 val;
  854. bool cur_state;
  855. reg = FDI_TX_CTL(pipe);
  856. val = I915_READ(reg);
  857. cur_state = !!(val & FDI_TX_ENABLE);
  858. WARN(cur_state != state,
  859. "FDI TX state assertion failure (expected %s, current %s)\n",
  860. state_string(state), state_string(cur_state));
  861. }
  862. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  863. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  864. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  865. enum pipe pipe, bool state)
  866. {
  867. int reg;
  868. u32 val;
  869. bool cur_state;
  870. reg = FDI_RX_CTL(pipe);
  871. val = I915_READ(reg);
  872. cur_state = !!(val & FDI_RX_ENABLE);
  873. WARN(cur_state != state,
  874. "FDI RX state assertion failure (expected %s, current %s)\n",
  875. state_string(state), state_string(cur_state));
  876. }
  877. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  878. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  879. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  880. enum pipe pipe)
  881. {
  882. int reg;
  883. u32 val;
  884. /* ILK FDI PLL is always enabled */
  885. if (dev_priv->info->gen == 5)
  886. return;
  887. reg = FDI_TX_CTL(pipe);
  888. val = I915_READ(reg);
  889. WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  890. }
  891. static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
  892. enum pipe pipe)
  893. {
  894. int reg;
  895. u32 val;
  896. reg = FDI_RX_CTL(pipe);
  897. val = I915_READ(reg);
  898. WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
  899. }
  900. static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  901. enum pipe pipe)
  902. {
  903. int pp_reg, lvds_reg;
  904. u32 val;
  905. enum pipe panel_pipe = PIPE_A;
  906. bool locked = true;
  907. if (HAS_PCH_SPLIT(dev_priv->dev)) {
  908. pp_reg = PCH_PP_CONTROL;
  909. lvds_reg = PCH_LVDS;
  910. } else {
  911. pp_reg = PP_CONTROL;
  912. lvds_reg = LVDS;
  913. }
  914. val = I915_READ(pp_reg);
  915. if (!(val & PANEL_POWER_ON) ||
  916. ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
  917. locked = false;
  918. if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
  919. panel_pipe = PIPE_B;
  920. WARN(panel_pipe == pipe && locked,
  921. "panel assertion failure, pipe %c regs locked\n",
  922. pipe_name(pipe));
  923. }
  924. void assert_pipe(struct drm_i915_private *dev_priv,
  925. enum pipe pipe, bool state)
  926. {
  927. int reg;
  928. u32 val;
  929. bool cur_state;
  930. /* if we need the pipe A quirk it must be always on */
  931. if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
  932. state = true;
  933. reg = PIPECONF(pipe);
  934. val = I915_READ(reg);
  935. cur_state = !!(val & PIPECONF_ENABLE);
  936. WARN(cur_state != state,
  937. "pipe %c assertion failure (expected %s, current %s)\n",
  938. pipe_name(pipe), state_string(state), state_string(cur_state));
  939. }
  940. static void assert_plane(struct drm_i915_private *dev_priv,
  941. enum plane plane, bool state)
  942. {
  943. int reg;
  944. u32 val;
  945. bool cur_state;
  946. reg = DSPCNTR(plane);
  947. val = I915_READ(reg);
  948. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  949. WARN(cur_state != state,
  950. "plane %c assertion failure (expected %s, current %s)\n",
  951. plane_name(plane), state_string(state), state_string(cur_state));
  952. }
  953. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  954. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  955. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  956. enum pipe pipe)
  957. {
  958. int reg, i;
  959. u32 val;
  960. int cur_pipe;
  961. /* Planes are fixed to pipes on ILK+ */
  962. if (HAS_PCH_SPLIT(dev_priv->dev)) {
  963. reg = DSPCNTR(pipe);
  964. val = I915_READ(reg);
  965. WARN((val & DISPLAY_PLANE_ENABLE),
  966. "plane %c assertion failure, should be disabled but not\n",
  967. plane_name(pipe));
  968. return;
  969. }
  970. /* Need to check both planes against the pipe */
  971. for (i = 0; i < 2; i++) {
  972. reg = DSPCNTR(i);
  973. val = I915_READ(reg);
  974. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  975. DISPPLANE_SEL_PIPE_SHIFT;
  976. WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  977. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  978. plane_name(i), pipe_name(pipe));
  979. }
  980. }
  981. static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  982. {
  983. u32 val;
  984. bool enabled;
  985. val = I915_READ(PCH_DREF_CONTROL);
  986. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  987. DREF_SUPERSPREAD_SOURCE_MASK));
  988. WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  989. }
  990. static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
  991. enum pipe pipe)
  992. {
  993. int reg;
  994. u32 val;
  995. bool enabled;
  996. reg = TRANSCONF(pipe);
  997. val = I915_READ(reg);
  998. enabled = !!(val & TRANS_ENABLE);
  999. WARN(enabled,
  1000. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1001. pipe_name(pipe));
  1002. }
  1003. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1004. enum pipe pipe, u32 port_sel, u32 val)
  1005. {
  1006. if ((val & DP_PORT_EN) == 0)
  1007. return false;
  1008. if (HAS_PCH_CPT(dev_priv->dev)) {
  1009. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1010. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1011. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1012. return false;
  1013. } else {
  1014. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1015. return false;
  1016. }
  1017. return true;
  1018. }
  1019. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1020. enum pipe pipe, u32 val)
  1021. {
  1022. if ((val & PORT_ENABLE) == 0)
  1023. return false;
  1024. if (HAS_PCH_CPT(dev_priv->dev)) {
  1025. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1026. return false;
  1027. } else {
  1028. if ((val & TRANSCODER_MASK) != TRANSCODER(pipe))
  1029. return false;
  1030. }
  1031. return true;
  1032. }
  1033. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1034. enum pipe pipe, u32 val)
  1035. {
  1036. if ((val & LVDS_PORT_EN) == 0)
  1037. return false;
  1038. if (HAS_PCH_CPT(dev_priv->dev)) {
  1039. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1040. return false;
  1041. } else {
  1042. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1043. return false;
  1044. }
  1045. return true;
  1046. }
  1047. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1048. enum pipe pipe, u32 val)
  1049. {
  1050. if ((val & ADPA_DAC_ENABLE) == 0)
  1051. return false;
  1052. if (HAS_PCH_CPT(dev_priv->dev)) {
  1053. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1054. return false;
  1055. } else {
  1056. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1057. return false;
  1058. }
  1059. return true;
  1060. }
  1061. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1062. enum pipe pipe, int reg, u32 port_sel)
  1063. {
  1064. u32 val = I915_READ(reg);
  1065. WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1066. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1067. reg, pipe_name(pipe));
  1068. }
  1069. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1070. enum pipe pipe, int reg)
  1071. {
  1072. u32 val = I915_READ(reg);
  1073. WARN(hdmi_pipe_enabled(dev_priv, val, pipe),
  1074. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1075. reg, pipe_name(pipe));
  1076. }
  1077. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1078. enum pipe pipe)
  1079. {
  1080. int reg;
  1081. u32 val;
  1082. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1083. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1084. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1085. reg = PCH_ADPA;
  1086. val = I915_READ(reg);
  1087. WARN(adpa_pipe_enabled(dev_priv, val, pipe),
  1088. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1089. pipe_name(pipe));
  1090. reg = PCH_LVDS;
  1091. val = I915_READ(reg);
  1092. WARN(lvds_pipe_enabled(dev_priv, val, pipe),
  1093. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1094. pipe_name(pipe));
  1095. assert_pch_hdmi_disabled(dev_priv, pipe, HDMIB);
  1096. assert_pch_hdmi_disabled(dev_priv, pipe, HDMIC);
  1097. assert_pch_hdmi_disabled(dev_priv, pipe, HDMID);
  1098. }
  1099. /**
  1100. * intel_enable_pll - enable a PLL
  1101. * @dev_priv: i915 private structure
  1102. * @pipe: pipe PLL to enable
  1103. *
  1104. * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
  1105. * make sure the PLL reg is writable first though, since the panel write
  1106. * protect mechanism may be enabled.
  1107. *
  1108. * Note! This is for pre-ILK only.
  1109. */
  1110. static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1111. {
  1112. int reg;
  1113. u32 val;
  1114. /* No really, not for ILK+ */
  1115. BUG_ON(dev_priv->info->gen >= 5);
  1116. /* PLL is protected by panel, make sure we can write it */
  1117. if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
  1118. assert_panel_unlocked(dev_priv, pipe);
  1119. reg = DPLL(pipe);
  1120. val = I915_READ(reg);
  1121. val |= DPLL_VCO_ENABLE;
  1122. /* We do this three times for luck */
  1123. I915_WRITE(reg, val);
  1124. POSTING_READ(reg);
  1125. udelay(150); /* wait for warmup */
  1126. I915_WRITE(reg, val);
  1127. POSTING_READ(reg);
  1128. udelay(150); /* wait for warmup */
  1129. I915_WRITE(reg, val);
  1130. POSTING_READ(reg);
  1131. udelay(150); /* wait for warmup */
  1132. }
  1133. /**
  1134. * intel_disable_pll - disable a PLL
  1135. * @dev_priv: i915 private structure
  1136. * @pipe: pipe PLL to disable
  1137. *
  1138. * Disable the PLL for @pipe, making sure the pipe is off first.
  1139. *
  1140. * Note! This is for pre-ILK only.
  1141. */
  1142. static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1143. {
  1144. int reg;
  1145. u32 val;
  1146. /* Don't disable pipe A or pipe A PLLs if needed */
  1147. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1148. return;
  1149. /* Make sure the pipe isn't still relying on us */
  1150. assert_pipe_disabled(dev_priv, pipe);
  1151. reg = DPLL(pipe);
  1152. val = I915_READ(reg);
  1153. val &= ~DPLL_VCO_ENABLE;
  1154. I915_WRITE(reg, val);
  1155. POSTING_READ(reg);
  1156. }
  1157. /**
  1158. * intel_enable_pch_pll - enable PCH PLL
  1159. * @dev_priv: i915 private structure
  1160. * @pipe: pipe PLL to enable
  1161. *
  1162. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1163. * drives the transcoder clock.
  1164. */
  1165. static void intel_enable_pch_pll(struct drm_i915_private *dev_priv,
  1166. enum pipe pipe)
  1167. {
  1168. int reg;
  1169. u32 val;
  1170. if (pipe > 1)
  1171. return;
  1172. /* PCH only available on ILK+ */
  1173. BUG_ON(dev_priv->info->gen < 5);
  1174. /* PCH refclock must be enabled first */
  1175. assert_pch_refclk_enabled(dev_priv);
  1176. reg = PCH_DPLL(pipe);
  1177. val = I915_READ(reg);
  1178. val |= DPLL_VCO_ENABLE;
  1179. I915_WRITE(reg, val);
  1180. POSTING_READ(reg);
  1181. udelay(200);
  1182. }
  1183. static void intel_disable_pch_pll(struct drm_i915_private *dev_priv,
  1184. enum pipe pipe)
  1185. {
  1186. int reg;
  1187. u32 val, pll_mask = TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL,
  1188. pll_sel = TRANSC_DPLL_ENABLE;
  1189. if (pipe > 1)
  1190. return;
  1191. /* PCH only available on ILK+ */
  1192. BUG_ON(dev_priv->info->gen < 5);
  1193. /* Make sure transcoder isn't still depending on us */
  1194. assert_transcoder_disabled(dev_priv, pipe);
  1195. if (pipe == 0)
  1196. pll_sel |= TRANSC_DPLLA_SEL;
  1197. else if (pipe == 1)
  1198. pll_sel |= TRANSC_DPLLB_SEL;
  1199. if ((I915_READ(PCH_DPLL_SEL) & pll_mask) == pll_sel)
  1200. return;
  1201. reg = PCH_DPLL(pipe);
  1202. val = I915_READ(reg);
  1203. val &= ~DPLL_VCO_ENABLE;
  1204. I915_WRITE(reg, val);
  1205. POSTING_READ(reg);
  1206. udelay(200);
  1207. }
  1208. static void intel_enable_transcoder(struct drm_i915_private *dev_priv,
  1209. enum pipe pipe)
  1210. {
  1211. int reg;
  1212. u32 val, pipeconf_val;
  1213. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1214. /* PCH only available on ILK+ */
  1215. BUG_ON(dev_priv->info->gen < 5);
  1216. /* Make sure PCH DPLL is enabled */
  1217. assert_pch_pll_enabled(dev_priv, pipe);
  1218. /* FDI must be feeding us bits for PCH ports */
  1219. assert_fdi_tx_enabled(dev_priv, pipe);
  1220. assert_fdi_rx_enabled(dev_priv, pipe);
  1221. reg = TRANSCONF(pipe);
  1222. val = I915_READ(reg);
  1223. pipeconf_val = I915_READ(PIPECONF(pipe));
  1224. if (HAS_PCH_IBX(dev_priv->dev)) {
  1225. /*
  1226. * make the BPC in transcoder be consistent with
  1227. * that in pipeconf reg.
  1228. */
  1229. val &= ~PIPE_BPC_MASK;
  1230. val |= pipeconf_val & PIPE_BPC_MASK;
  1231. }
  1232. val &= ~TRANS_INTERLACE_MASK;
  1233. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1234. if (HAS_PCH_IBX(dev_priv->dev) &&
  1235. intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
  1236. val |= TRANS_LEGACY_INTERLACED_ILK;
  1237. else
  1238. val |= TRANS_INTERLACED;
  1239. else
  1240. val |= TRANS_PROGRESSIVE;
  1241. I915_WRITE(reg, val | TRANS_ENABLE);
  1242. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1243. DRM_ERROR("failed to enable transcoder %d\n", pipe);
  1244. }
  1245. static void intel_disable_transcoder(struct drm_i915_private *dev_priv,
  1246. enum pipe pipe)
  1247. {
  1248. int reg;
  1249. u32 val;
  1250. /* FDI relies on the transcoder */
  1251. assert_fdi_tx_disabled(dev_priv, pipe);
  1252. assert_fdi_rx_disabled(dev_priv, pipe);
  1253. /* Ports must be off as well */
  1254. assert_pch_ports_disabled(dev_priv, pipe);
  1255. reg = TRANSCONF(pipe);
  1256. val = I915_READ(reg);
  1257. val &= ~TRANS_ENABLE;
  1258. I915_WRITE(reg, val);
  1259. /* wait for PCH transcoder off, transcoder state */
  1260. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1261. DRM_ERROR("failed to disable transcoder %d\n", pipe);
  1262. }
  1263. /**
  1264. * intel_enable_pipe - enable a pipe, asserting requirements
  1265. * @dev_priv: i915 private structure
  1266. * @pipe: pipe to enable
  1267. * @pch_port: on ILK+, is this pipe driving a PCH port or not
  1268. *
  1269. * Enable @pipe, making sure that various hardware specific requirements
  1270. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1271. *
  1272. * @pipe should be %PIPE_A or %PIPE_B.
  1273. *
  1274. * Will wait until the pipe is actually running (i.e. first vblank) before
  1275. * returning.
  1276. */
  1277. static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
  1278. bool pch_port)
  1279. {
  1280. int reg;
  1281. u32 val;
  1282. /*
  1283. * A pipe without a PLL won't actually be able to drive bits from
  1284. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1285. * need the check.
  1286. */
  1287. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1288. assert_pll_enabled(dev_priv, pipe);
  1289. else {
  1290. if (pch_port) {
  1291. /* if driving the PCH, we need FDI enabled */
  1292. assert_fdi_rx_pll_enabled(dev_priv, pipe);
  1293. assert_fdi_tx_pll_enabled(dev_priv, pipe);
  1294. }
  1295. /* FIXME: assert CPU port conditions for SNB+ */
  1296. }
  1297. reg = PIPECONF(pipe);
  1298. val = I915_READ(reg);
  1299. if (val & PIPECONF_ENABLE)
  1300. return;
  1301. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1302. intel_wait_for_vblank(dev_priv->dev, pipe);
  1303. }
  1304. /**
  1305. * intel_disable_pipe - disable a pipe, asserting requirements
  1306. * @dev_priv: i915 private structure
  1307. * @pipe: pipe to disable
  1308. *
  1309. * Disable @pipe, making sure that various hardware specific requirements
  1310. * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
  1311. *
  1312. * @pipe should be %PIPE_A or %PIPE_B.
  1313. *
  1314. * Will wait until the pipe has shut down before returning.
  1315. */
  1316. static void intel_disable_pipe(struct drm_i915_private *dev_priv,
  1317. enum pipe pipe)
  1318. {
  1319. int reg;
  1320. u32 val;
  1321. /*
  1322. * Make sure planes won't keep trying to pump pixels to us,
  1323. * or we might hang the display.
  1324. */
  1325. assert_planes_disabled(dev_priv, pipe);
  1326. /* Don't disable pipe A or pipe A PLLs if needed */
  1327. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1328. return;
  1329. reg = PIPECONF(pipe);
  1330. val = I915_READ(reg);
  1331. if ((val & PIPECONF_ENABLE) == 0)
  1332. return;
  1333. I915_WRITE(reg, val & ~PIPECONF_ENABLE);
  1334. intel_wait_for_pipe_off(dev_priv->dev, pipe);
  1335. }
  1336. /*
  1337. * Plane regs are double buffered, going from enabled->disabled needs a
  1338. * trigger in order to latch. The display address reg provides this.
  1339. */
  1340. static void intel_flush_display_plane(struct drm_i915_private *dev_priv,
  1341. enum plane plane)
  1342. {
  1343. I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
  1344. I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
  1345. }
  1346. /**
  1347. * intel_enable_plane - enable a display plane on a given pipe
  1348. * @dev_priv: i915 private structure
  1349. * @plane: plane to enable
  1350. * @pipe: pipe being fed
  1351. *
  1352. * Enable @plane on @pipe, making sure that @pipe is running first.
  1353. */
  1354. static void intel_enable_plane(struct drm_i915_private *dev_priv,
  1355. enum plane plane, enum pipe pipe)
  1356. {
  1357. int reg;
  1358. u32 val;
  1359. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1360. assert_pipe_enabled(dev_priv, pipe);
  1361. reg = DSPCNTR(plane);
  1362. val = I915_READ(reg);
  1363. if (val & DISPLAY_PLANE_ENABLE)
  1364. return;
  1365. I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
  1366. intel_flush_display_plane(dev_priv, plane);
  1367. intel_wait_for_vblank(dev_priv->dev, pipe);
  1368. }
  1369. /**
  1370. * intel_disable_plane - disable a display plane
  1371. * @dev_priv: i915 private structure
  1372. * @plane: plane to disable
  1373. * @pipe: pipe consuming the data
  1374. *
  1375. * Disable @plane; should be an independent operation.
  1376. */
  1377. static void intel_disable_plane(struct drm_i915_private *dev_priv,
  1378. enum plane plane, enum pipe pipe)
  1379. {
  1380. int reg;
  1381. u32 val;
  1382. reg = DSPCNTR(plane);
  1383. val = I915_READ(reg);
  1384. if ((val & DISPLAY_PLANE_ENABLE) == 0)
  1385. return;
  1386. I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
  1387. intel_flush_display_plane(dev_priv, plane);
  1388. intel_wait_for_vblank(dev_priv->dev, pipe);
  1389. }
  1390. static void disable_pch_dp(struct drm_i915_private *dev_priv,
  1391. enum pipe pipe, int reg, u32 port_sel)
  1392. {
  1393. u32 val = I915_READ(reg);
  1394. if (dp_pipe_enabled(dev_priv, pipe, port_sel, val)) {
  1395. DRM_DEBUG_KMS("Disabling pch dp %x on pipe %d\n", reg, pipe);
  1396. I915_WRITE(reg, val & ~DP_PORT_EN);
  1397. }
  1398. }
  1399. static void disable_pch_hdmi(struct drm_i915_private *dev_priv,
  1400. enum pipe pipe, int reg)
  1401. {
  1402. u32 val = I915_READ(reg);
  1403. if (hdmi_pipe_enabled(dev_priv, val, pipe)) {
  1404. DRM_DEBUG_KMS("Disabling pch HDMI %x on pipe %d\n",
  1405. reg, pipe);
  1406. I915_WRITE(reg, val & ~PORT_ENABLE);
  1407. }
  1408. }
  1409. /* Disable any ports connected to this transcoder */
  1410. static void intel_disable_pch_ports(struct drm_i915_private *dev_priv,
  1411. enum pipe pipe)
  1412. {
  1413. u32 reg, val;
  1414. val = I915_READ(PCH_PP_CONTROL);
  1415. I915_WRITE(PCH_PP_CONTROL, val | PANEL_UNLOCK_REGS);
  1416. disable_pch_dp(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1417. disable_pch_dp(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1418. disable_pch_dp(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1419. reg = PCH_ADPA;
  1420. val = I915_READ(reg);
  1421. if (adpa_pipe_enabled(dev_priv, val, pipe))
  1422. I915_WRITE(reg, val & ~ADPA_DAC_ENABLE);
  1423. reg = PCH_LVDS;
  1424. val = I915_READ(reg);
  1425. if (lvds_pipe_enabled(dev_priv, val, pipe)) {
  1426. DRM_DEBUG_KMS("disable lvds on pipe %d val 0x%08x\n", pipe, val);
  1427. I915_WRITE(reg, val & ~LVDS_PORT_EN);
  1428. POSTING_READ(reg);
  1429. udelay(100);
  1430. }
  1431. disable_pch_hdmi(dev_priv, pipe, HDMIB);
  1432. disable_pch_hdmi(dev_priv, pipe, HDMIC);
  1433. disable_pch_hdmi(dev_priv, pipe, HDMID);
  1434. }
  1435. static void i8xx_disable_fbc(struct drm_device *dev)
  1436. {
  1437. struct drm_i915_private *dev_priv = dev->dev_private;
  1438. u32 fbc_ctl;
  1439. /* Disable compression */
  1440. fbc_ctl = I915_READ(FBC_CONTROL);
  1441. if ((fbc_ctl & FBC_CTL_EN) == 0)
  1442. return;
  1443. fbc_ctl &= ~FBC_CTL_EN;
  1444. I915_WRITE(FBC_CONTROL, fbc_ctl);
  1445. /* Wait for compressing bit to clear */
  1446. if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
  1447. DRM_DEBUG_KMS("FBC idle timed out\n");
  1448. return;
  1449. }
  1450. DRM_DEBUG_KMS("disabled FBC\n");
  1451. }
  1452. static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1453. {
  1454. struct drm_device *dev = crtc->dev;
  1455. struct drm_i915_private *dev_priv = dev->dev_private;
  1456. struct drm_framebuffer *fb = crtc->fb;
  1457. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1458. struct drm_i915_gem_object *obj = intel_fb->obj;
  1459. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1460. int cfb_pitch;
  1461. int plane, i;
  1462. u32 fbc_ctl, fbc_ctl2;
  1463. cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
  1464. if (fb->pitches[0] < cfb_pitch)
  1465. cfb_pitch = fb->pitches[0];
  1466. /* FBC_CTL wants 64B units */
  1467. cfb_pitch = (cfb_pitch / 64) - 1;
  1468. plane = intel_crtc->plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
  1469. /* Clear old tags */
  1470. for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
  1471. I915_WRITE(FBC_TAG + (i * 4), 0);
  1472. /* Set it up... */
  1473. fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
  1474. fbc_ctl2 |= plane;
  1475. I915_WRITE(FBC_CONTROL2, fbc_ctl2);
  1476. I915_WRITE(FBC_FENCE_OFF, crtc->y);
  1477. /* enable it... */
  1478. fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
  1479. if (IS_I945GM(dev))
  1480. fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
  1481. fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
  1482. fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
  1483. fbc_ctl |= obj->fence_reg;
  1484. I915_WRITE(FBC_CONTROL, fbc_ctl);
  1485. DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %d, ",
  1486. cfb_pitch, crtc->y, intel_crtc->plane);
  1487. }
  1488. static bool i8xx_fbc_enabled(struct drm_device *dev)
  1489. {
  1490. struct drm_i915_private *dev_priv = dev->dev_private;
  1491. return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
  1492. }
  1493. static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1494. {
  1495. struct drm_device *dev = crtc->dev;
  1496. struct drm_i915_private *dev_priv = dev->dev_private;
  1497. struct drm_framebuffer *fb = crtc->fb;
  1498. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1499. struct drm_i915_gem_object *obj = intel_fb->obj;
  1500. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1501. int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
  1502. unsigned long stall_watermark = 200;
  1503. u32 dpfc_ctl;
  1504. dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
  1505. dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
  1506. I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
  1507. I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  1508. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  1509. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  1510. I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
  1511. /* enable it... */
  1512. I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
  1513. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  1514. }
  1515. static void g4x_disable_fbc(struct drm_device *dev)
  1516. {
  1517. struct drm_i915_private *dev_priv = dev->dev_private;
  1518. u32 dpfc_ctl;
  1519. /* Disable compression */
  1520. dpfc_ctl = I915_READ(DPFC_CONTROL);
  1521. if (dpfc_ctl & DPFC_CTL_EN) {
  1522. dpfc_ctl &= ~DPFC_CTL_EN;
  1523. I915_WRITE(DPFC_CONTROL, dpfc_ctl);
  1524. DRM_DEBUG_KMS("disabled FBC\n");
  1525. }
  1526. }
  1527. static bool g4x_fbc_enabled(struct drm_device *dev)
  1528. {
  1529. struct drm_i915_private *dev_priv = dev->dev_private;
  1530. return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
  1531. }
  1532. static void sandybridge_blit_fbc_update(struct drm_device *dev)
  1533. {
  1534. struct drm_i915_private *dev_priv = dev->dev_private;
  1535. u32 blt_ecoskpd;
  1536. /* Make sure blitter notifies FBC of writes */
  1537. gen6_gt_force_wake_get(dev_priv);
  1538. blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
  1539. blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
  1540. GEN6_BLITTER_LOCK_SHIFT;
  1541. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  1542. blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
  1543. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  1544. blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
  1545. GEN6_BLITTER_LOCK_SHIFT);
  1546. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  1547. POSTING_READ(GEN6_BLITTER_ECOSKPD);
  1548. gen6_gt_force_wake_put(dev_priv);
  1549. }
  1550. static void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1551. {
  1552. struct drm_device *dev = crtc->dev;
  1553. struct drm_i915_private *dev_priv = dev->dev_private;
  1554. struct drm_framebuffer *fb = crtc->fb;
  1555. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1556. struct drm_i915_gem_object *obj = intel_fb->obj;
  1557. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1558. int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
  1559. unsigned long stall_watermark = 200;
  1560. u32 dpfc_ctl;
  1561. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  1562. dpfc_ctl &= DPFC_RESERVED;
  1563. dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
  1564. /* Set persistent mode for front-buffer rendering, ala X. */
  1565. dpfc_ctl |= DPFC_CTL_PERSISTENT_MODE;
  1566. dpfc_ctl |= (DPFC_CTL_FENCE_EN | obj->fence_reg);
  1567. I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
  1568. I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  1569. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  1570. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  1571. I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
  1572. I915_WRITE(ILK_FBC_RT_BASE, obj->gtt_offset | ILK_FBC_RT_VALID);
  1573. /* enable it... */
  1574. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
  1575. if (IS_GEN6(dev)) {
  1576. I915_WRITE(SNB_DPFC_CTL_SA,
  1577. SNB_CPU_FENCE_ENABLE | obj->fence_reg);
  1578. I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
  1579. sandybridge_blit_fbc_update(dev);
  1580. }
  1581. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  1582. }
  1583. static void ironlake_disable_fbc(struct drm_device *dev)
  1584. {
  1585. struct drm_i915_private *dev_priv = dev->dev_private;
  1586. u32 dpfc_ctl;
  1587. /* Disable compression */
  1588. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  1589. if (dpfc_ctl & DPFC_CTL_EN) {
  1590. dpfc_ctl &= ~DPFC_CTL_EN;
  1591. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
  1592. DRM_DEBUG_KMS("disabled FBC\n");
  1593. }
  1594. }
  1595. static bool ironlake_fbc_enabled(struct drm_device *dev)
  1596. {
  1597. struct drm_i915_private *dev_priv = dev->dev_private;
  1598. return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
  1599. }
  1600. bool intel_fbc_enabled(struct drm_device *dev)
  1601. {
  1602. struct drm_i915_private *dev_priv = dev->dev_private;
  1603. if (!dev_priv->display.fbc_enabled)
  1604. return false;
  1605. return dev_priv->display.fbc_enabled(dev);
  1606. }
  1607. static void intel_fbc_work_fn(struct work_struct *__work)
  1608. {
  1609. struct intel_fbc_work *work =
  1610. container_of(to_delayed_work(__work),
  1611. struct intel_fbc_work, work);
  1612. struct drm_device *dev = work->crtc->dev;
  1613. struct drm_i915_private *dev_priv = dev->dev_private;
  1614. mutex_lock(&dev->struct_mutex);
  1615. if (work == dev_priv->fbc_work) {
  1616. /* Double check that we haven't switched fb without cancelling
  1617. * the prior work.
  1618. */
  1619. if (work->crtc->fb == work->fb) {
  1620. dev_priv->display.enable_fbc(work->crtc,
  1621. work->interval);
  1622. dev_priv->cfb_plane = to_intel_crtc(work->crtc)->plane;
  1623. dev_priv->cfb_fb = work->crtc->fb->base.id;
  1624. dev_priv->cfb_y = work->crtc->y;
  1625. }
  1626. dev_priv->fbc_work = NULL;
  1627. }
  1628. mutex_unlock(&dev->struct_mutex);
  1629. kfree(work);
  1630. }
  1631. static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
  1632. {
  1633. if (dev_priv->fbc_work == NULL)
  1634. return;
  1635. DRM_DEBUG_KMS("cancelling pending FBC enable\n");
  1636. /* Synchronisation is provided by struct_mutex and checking of
  1637. * dev_priv->fbc_work, so we can perform the cancellation
  1638. * entirely asynchronously.
  1639. */
  1640. if (cancel_delayed_work(&dev_priv->fbc_work->work))
  1641. /* tasklet was killed before being run, clean up */
  1642. kfree(dev_priv->fbc_work);
  1643. /* Mark the work as no longer wanted so that if it does
  1644. * wake-up (because the work was already running and waiting
  1645. * for our mutex), it will discover that is no longer
  1646. * necessary to run.
  1647. */
  1648. dev_priv->fbc_work = NULL;
  1649. }
  1650. static void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1651. {
  1652. struct intel_fbc_work *work;
  1653. struct drm_device *dev = crtc->dev;
  1654. struct drm_i915_private *dev_priv = dev->dev_private;
  1655. if (!dev_priv->display.enable_fbc)
  1656. return;
  1657. intel_cancel_fbc_work(dev_priv);
  1658. work = kzalloc(sizeof *work, GFP_KERNEL);
  1659. if (work == NULL) {
  1660. dev_priv->display.enable_fbc(crtc, interval);
  1661. return;
  1662. }
  1663. work->crtc = crtc;
  1664. work->fb = crtc->fb;
  1665. work->interval = interval;
  1666. INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
  1667. dev_priv->fbc_work = work;
  1668. DRM_DEBUG_KMS("scheduling delayed FBC enable\n");
  1669. /* Delay the actual enabling to let pageflipping cease and the
  1670. * display to settle before starting the compression. Note that
  1671. * this delay also serves a second purpose: it allows for a
  1672. * vblank to pass after disabling the FBC before we attempt
  1673. * to modify the control registers.
  1674. *
  1675. * A more complicated solution would involve tracking vblanks
  1676. * following the termination of the page-flipping sequence
  1677. * and indeed performing the enable as a co-routine and not
  1678. * waiting synchronously upon the vblank.
  1679. */
  1680. schedule_delayed_work(&work->work, msecs_to_jiffies(50));
  1681. }
  1682. void intel_disable_fbc(struct drm_device *dev)
  1683. {
  1684. struct drm_i915_private *dev_priv = dev->dev_private;
  1685. intel_cancel_fbc_work(dev_priv);
  1686. if (!dev_priv->display.disable_fbc)
  1687. return;
  1688. dev_priv->display.disable_fbc(dev);
  1689. dev_priv->cfb_plane = -1;
  1690. }
  1691. /**
  1692. * intel_update_fbc - enable/disable FBC as needed
  1693. * @dev: the drm_device
  1694. *
  1695. * Set up the framebuffer compression hardware at mode set time. We
  1696. * enable it if possible:
  1697. * - plane A only (on pre-965)
  1698. * - no pixel mulitply/line duplication
  1699. * - no alpha buffer discard
  1700. * - no dual wide
  1701. * - framebuffer <= 2048 in width, 1536 in height
  1702. *
  1703. * We can't assume that any compression will take place (worst case),
  1704. * so the compressed buffer has to be the same size as the uncompressed
  1705. * one. It also must reside (along with the line length buffer) in
  1706. * stolen memory.
  1707. *
  1708. * We need to enable/disable FBC on a global basis.
  1709. */
  1710. static void intel_update_fbc(struct drm_device *dev)
  1711. {
  1712. struct drm_i915_private *dev_priv = dev->dev_private;
  1713. struct drm_crtc *crtc = NULL, *tmp_crtc;
  1714. struct intel_crtc *intel_crtc;
  1715. struct drm_framebuffer *fb;
  1716. struct intel_framebuffer *intel_fb;
  1717. struct drm_i915_gem_object *obj;
  1718. int enable_fbc;
  1719. DRM_DEBUG_KMS("\n");
  1720. if (!i915_powersave)
  1721. return;
  1722. if (!I915_HAS_FBC(dev))
  1723. return;
  1724. /*
  1725. * If FBC is already on, we just have to verify that we can
  1726. * keep it that way...
  1727. * Need to disable if:
  1728. * - more than one pipe is active
  1729. * - changing FBC params (stride, fence, mode)
  1730. * - new fb is too large to fit in compressed buffer
  1731. * - going to an unsupported config (interlace, pixel multiply, etc.)
  1732. */
  1733. list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
  1734. if (tmp_crtc->enabled && tmp_crtc->fb) {
  1735. if (crtc) {
  1736. DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
  1737. dev_priv->no_fbc_reason = FBC_MULTIPLE_PIPES;
  1738. goto out_disable;
  1739. }
  1740. crtc = tmp_crtc;
  1741. }
  1742. }
  1743. if (!crtc || crtc->fb == NULL) {
  1744. DRM_DEBUG_KMS("no output, disabling\n");
  1745. dev_priv->no_fbc_reason = FBC_NO_OUTPUT;
  1746. goto out_disable;
  1747. }
  1748. intel_crtc = to_intel_crtc(crtc);
  1749. fb = crtc->fb;
  1750. intel_fb = to_intel_framebuffer(fb);
  1751. obj = intel_fb->obj;
  1752. enable_fbc = i915_enable_fbc;
  1753. if (enable_fbc < 0) {
  1754. DRM_DEBUG_KMS("fbc set to per-chip default\n");
  1755. enable_fbc = 1;
  1756. if (INTEL_INFO(dev)->gen <= 6)
  1757. enable_fbc = 0;
  1758. }
  1759. if (!enable_fbc) {
  1760. DRM_DEBUG_KMS("fbc disabled per module param\n");
  1761. dev_priv->no_fbc_reason = FBC_MODULE_PARAM;
  1762. goto out_disable;
  1763. }
  1764. if (intel_fb->obj->base.size > dev_priv->cfb_size) {
  1765. DRM_DEBUG_KMS("framebuffer too large, disabling "
  1766. "compression\n");
  1767. dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
  1768. goto out_disable;
  1769. }
  1770. if ((crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) ||
  1771. (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)) {
  1772. DRM_DEBUG_KMS("mode incompatible with compression, "
  1773. "disabling\n");
  1774. dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;
  1775. goto out_disable;
  1776. }
  1777. if ((crtc->mode.hdisplay > 2048) ||
  1778. (crtc->mode.vdisplay > 1536)) {
  1779. DRM_DEBUG_KMS("mode too large for compression, disabling\n");
  1780. dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;
  1781. goto out_disable;
  1782. }
  1783. if ((IS_I915GM(dev) || IS_I945GM(dev)) && intel_crtc->plane != 0) {
  1784. DRM_DEBUG_KMS("plane not 0, disabling compression\n");
  1785. dev_priv->no_fbc_reason = FBC_BAD_PLANE;
  1786. goto out_disable;
  1787. }
  1788. /* The use of a CPU fence is mandatory in order to detect writes
  1789. * by the CPU to the scanout and trigger updates to the FBC.
  1790. */
  1791. if (obj->tiling_mode != I915_TILING_X ||
  1792. obj->fence_reg == I915_FENCE_REG_NONE) {
  1793. DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
  1794. dev_priv->no_fbc_reason = FBC_NOT_TILED;
  1795. goto out_disable;
  1796. }
  1797. /* If the kernel debugger is active, always disable compression */
  1798. if (in_dbg_master())
  1799. goto out_disable;
  1800. /* If the scanout has not changed, don't modify the FBC settings.
  1801. * Note that we make the fundamental assumption that the fb->obj
  1802. * cannot be unpinned (and have its GTT offset and fence revoked)
  1803. * without first being decoupled from the scanout and FBC disabled.
  1804. */
  1805. if (dev_priv->cfb_plane == intel_crtc->plane &&
  1806. dev_priv->cfb_fb == fb->base.id &&
  1807. dev_priv->cfb_y == crtc->y)
  1808. return;
  1809. if (intel_fbc_enabled(dev)) {
  1810. /* We update FBC along two paths, after changing fb/crtc
  1811. * configuration (modeswitching) and after page-flipping
  1812. * finishes. For the latter, we know that not only did
  1813. * we disable the FBC at the start of the page-flip
  1814. * sequence, but also more than one vblank has passed.
  1815. *
  1816. * For the former case of modeswitching, it is possible
  1817. * to switch between two FBC valid configurations
  1818. * instantaneously so we do need to disable the FBC
  1819. * before we can modify its control registers. We also
  1820. * have to wait for the next vblank for that to take
  1821. * effect. However, since we delay enabling FBC we can
  1822. * assume that a vblank has passed since disabling and
  1823. * that we can safely alter the registers in the deferred
  1824. * callback.
  1825. *
  1826. * In the scenario that we go from a valid to invalid
  1827. * and then back to valid FBC configuration we have
  1828. * no strict enforcement that a vblank occurred since
  1829. * disabling the FBC. However, along all current pipe
  1830. * disabling paths we do need to wait for a vblank at
  1831. * some point. And we wait before enabling FBC anyway.
  1832. */
  1833. DRM_DEBUG_KMS("disabling active FBC for update\n");
  1834. intel_disable_fbc(dev);
  1835. }
  1836. intel_enable_fbc(crtc, 500);
  1837. return;
  1838. out_disable:
  1839. /* Multiple disables should be harmless */
  1840. if (intel_fbc_enabled(dev)) {
  1841. DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
  1842. intel_disable_fbc(dev);
  1843. }
  1844. }
  1845. int
  1846. intel_pin_and_fence_fb_obj(struct drm_device *dev,
  1847. struct drm_i915_gem_object *obj,
  1848. struct intel_ring_buffer *pipelined)
  1849. {
  1850. struct drm_i915_private *dev_priv = dev->dev_private;
  1851. u32 alignment;
  1852. int ret;
  1853. switch (obj->tiling_mode) {
  1854. case I915_TILING_NONE:
  1855. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1856. alignment = 128 * 1024;
  1857. else if (INTEL_INFO(dev)->gen >= 4)
  1858. alignment = 4 * 1024;
  1859. else
  1860. alignment = 64 * 1024;
  1861. break;
  1862. case I915_TILING_X:
  1863. /* pin() will align the object as required by fence */
  1864. alignment = 0;
  1865. break;
  1866. case I915_TILING_Y:
  1867. /* FIXME: Is this true? */
  1868. DRM_ERROR("Y tiled not allowed for scan out buffers\n");
  1869. return -EINVAL;
  1870. default:
  1871. BUG();
  1872. }
  1873. dev_priv->mm.interruptible = false;
  1874. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1875. if (ret)
  1876. goto err_interruptible;
  1877. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1878. * fence, whereas 965+ only requires a fence if using
  1879. * framebuffer compression. For simplicity, we always install
  1880. * a fence as the cost is not that onerous.
  1881. */
  1882. ret = i915_gem_object_get_fence(obj, pipelined);
  1883. if (ret)
  1884. goto err_unpin;
  1885. i915_gem_object_pin_fence(obj);
  1886. dev_priv->mm.interruptible = true;
  1887. return 0;
  1888. err_unpin:
  1889. i915_gem_object_unpin(obj);
  1890. err_interruptible:
  1891. dev_priv->mm.interruptible = true;
  1892. return ret;
  1893. }
  1894. void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
  1895. {
  1896. i915_gem_object_unpin_fence(obj);
  1897. i915_gem_object_unpin(obj);
  1898. }
  1899. static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1900. int x, int y)
  1901. {
  1902. struct drm_device *dev = crtc->dev;
  1903. struct drm_i915_private *dev_priv = dev->dev_private;
  1904. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1905. struct intel_framebuffer *intel_fb;
  1906. struct drm_i915_gem_object *obj;
  1907. int plane = intel_crtc->plane;
  1908. unsigned long Start, Offset;
  1909. u32 dspcntr;
  1910. u32 reg;
  1911. switch (plane) {
  1912. case 0:
  1913. case 1:
  1914. break;
  1915. default:
  1916. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1917. return -EINVAL;
  1918. }
  1919. intel_fb = to_intel_framebuffer(fb);
  1920. obj = intel_fb->obj;
  1921. reg = DSPCNTR(plane);
  1922. dspcntr = I915_READ(reg);
  1923. /* Mask out pixel format bits in case we change it */
  1924. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1925. switch (fb->bits_per_pixel) {
  1926. case 8:
  1927. dspcntr |= DISPPLANE_8BPP;
  1928. break;
  1929. case 16:
  1930. if (fb->depth == 15)
  1931. dspcntr |= DISPPLANE_15_16BPP;
  1932. else
  1933. dspcntr |= DISPPLANE_16BPP;
  1934. break;
  1935. case 24:
  1936. case 32:
  1937. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  1938. break;
  1939. default:
  1940. DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
  1941. return -EINVAL;
  1942. }
  1943. if (INTEL_INFO(dev)->gen >= 4) {
  1944. if (obj->tiling_mode != I915_TILING_NONE)
  1945. dspcntr |= DISPPLANE_TILED;
  1946. else
  1947. dspcntr &= ~DISPPLANE_TILED;
  1948. }
  1949. I915_WRITE(reg, dspcntr);
  1950. Start = obj->gtt_offset;
  1951. Offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  1952. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  1953. Start, Offset, x, y, fb->pitches[0]);
  1954. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  1955. if (INTEL_INFO(dev)->gen >= 4) {
  1956. I915_MODIFY_DISPBASE(DSPSURF(plane), Start);
  1957. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  1958. I915_WRITE(DSPADDR(plane), Offset);
  1959. } else
  1960. I915_WRITE(DSPADDR(plane), Start + Offset);
  1961. POSTING_READ(reg);
  1962. return 0;
  1963. }
  1964. static int ironlake_update_plane(struct drm_crtc *crtc,
  1965. struct drm_framebuffer *fb, int x, int y)
  1966. {
  1967. struct drm_device *dev = crtc->dev;
  1968. struct drm_i915_private *dev_priv = dev->dev_private;
  1969. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1970. struct intel_framebuffer *intel_fb;
  1971. struct drm_i915_gem_object *obj;
  1972. int plane = intel_crtc->plane;
  1973. unsigned long Start, Offset;
  1974. u32 dspcntr;
  1975. u32 reg;
  1976. switch (plane) {
  1977. case 0:
  1978. case 1:
  1979. case 2:
  1980. break;
  1981. default:
  1982. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1983. return -EINVAL;
  1984. }
  1985. intel_fb = to_intel_framebuffer(fb);
  1986. obj = intel_fb->obj;
  1987. reg = DSPCNTR(plane);
  1988. dspcntr = I915_READ(reg);
  1989. /* Mask out pixel format bits in case we change it */
  1990. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1991. switch (fb->bits_per_pixel) {
  1992. case 8:
  1993. dspcntr |= DISPPLANE_8BPP;
  1994. break;
  1995. case 16:
  1996. if (fb->depth != 16)
  1997. return -EINVAL;
  1998. dspcntr |= DISPPLANE_16BPP;
  1999. break;
  2000. case 24:
  2001. case 32:
  2002. if (fb->depth == 24)
  2003. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  2004. else if (fb->depth == 30)
  2005. dspcntr |= DISPPLANE_32BPP_30BIT_NO_ALPHA;
  2006. else
  2007. return -EINVAL;
  2008. break;
  2009. default:
  2010. DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
  2011. return -EINVAL;
  2012. }
  2013. if (obj->tiling_mode != I915_TILING_NONE)
  2014. dspcntr |= DISPPLANE_TILED;
  2015. else
  2016. dspcntr &= ~DISPPLANE_TILED;
  2017. /* must disable */
  2018. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2019. I915_WRITE(reg, dspcntr);
  2020. Start = obj->gtt_offset;
  2021. Offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  2022. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2023. Start, Offset, x, y, fb->pitches[0]);
  2024. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2025. I915_MODIFY_DISPBASE(DSPSURF(plane), Start);
  2026. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2027. I915_WRITE(DSPADDR(plane), Offset);
  2028. POSTING_READ(reg);
  2029. return 0;
  2030. }
  2031. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2032. static int
  2033. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2034. int x, int y, enum mode_set_atomic state)
  2035. {
  2036. struct drm_device *dev = crtc->dev;
  2037. struct drm_i915_private *dev_priv = dev->dev_private;
  2038. if (dev_priv->display.disable_fbc)
  2039. dev_priv->display.disable_fbc(dev);
  2040. intel_increase_pllclock(crtc);
  2041. return dev_priv->display.update_plane(crtc, fb, x, y);
  2042. }
  2043. static int
  2044. intel_finish_fb(struct drm_framebuffer *old_fb)
  2045. {
  2046. struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
  2047. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2048. bool was_interruptible = dev_priv->mm.interruptible;
  2049. int ret;
  2050. wait_event(dev_priv->pending_flip_queue,
  2051. atomic_read(&dev_priv->mm.wedged) ||
  2052. atomic_read(&obj->pending_flip) == 0);
  2053. /* Big Hammer, we also need to ensure that any pending
  2054. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2055. * current scanout is retired before unpinning the old
  2056. * framebuffer.
  2057. *
  2058. * This should only fail upon a hung GPU, in which case we
  2059. * can safely continue.
  2060. */
  2061. dev_priv->mm.interruptible = false;
  2062. ret = i915_gem_object_finish_gpu(obj);
  2063. dev_priv->mm.interruptible = was_interruptible;
  2064. return ret;
  2065. }
  2066. static int
  2067. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  2068. struct drm_framebuffer *old_fb)
  2069. {
  2070. struct drm_device *dev = crtc->dev;
  2071. struct drm_i915_private *dev_priv = dev->dev_private;
  2072. struct drm_i915_master_private *master_priv;
  2073. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2074. int ret;
  2075. /* no fb bound */
  2076. if (!crtc->fb) {
  2077. DRM_ERROR("No FB bound\n");
  2078. return 0;
  2079. }
  2080. switch (intel_crtc->plane) {
  2081. case 0:
  2082. case 1:
  2083. break;
  2084. case 2:
  2085. if (IS_IVYBRIDGE(dev))
  2086. break;
  2087. /* fall through otherwise */
  2088. default:
  2089. DRM_ERROR("no plane for crtc\n");
  2090. return -EINVAL;
  2091. }
  2092. mutex_lock(&dev->struct_mutex);
  2093. ret = intel_pin_and_fence_fb_obj(dev,
  2094. to_intel_framebuffer(crtc->fb)->obj,
  2095. NULL);
  2096. if (ret != 0) {
  2097. mutex_unlock(&dev->struct_mutex);
  2098. DRM_ERROR("pin & fence failed\n");
  2099. return ret;
  2100. }
  2101. if (old_fb)
  2102. intel_finish_fb(old_fb);
  2103. ret = dev_priv->display.update_plane(crtc, crtc->fb, x, y);
  2104. if (ret) {
  2105. intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
  2106. mutex_unlock(&dev->struct_mutex);
  2107. DRM_ERROR("failed to update base address\n");
  2108. return ret;
  2109. }
  2110. if (old_fb) {
  2111. intel_wait_for_vblank(dev, intel_crtc->pipe);
  2112. intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
  2113. }
  2114. intel_update_fbc(dev);
  2115. mutex_unlock(&dev->struct_mutex);
  2116. if (!dev->primary->master)
  2117. return 0;
  2118. master_priv = dev->primary->master->driver_priv;
  2119. if (!master_priv->sarea_priv)
  2120. return 0;
  2121. if (intel_crtc->pipe) {
  2122. master_priv->sarea_priv->pipeB_x = x;
  2123. master_priv->sarea_priv->pipeB_y = y;
  2124. } else {
  2125. master_priv->sarea_priv->pipeA_x = x;
  2126. master_priv->sarea_priv->pipeA_y = y;
  2127. }
  2128. return 0;
  2129. }
  2130. static void ironlake_set_pll_edp(struct drm_crtc *crtc, int clock)
  2131. {
  2132. struct drm_device *dev = crtc->dev;
  2133. struct drm_i915_private *dev_priv = dev->dev_private;
  2134. u32 dpa_ctl;
  2135. DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
  2136. dpa_ctl = I915_READ(DP_A);
  2137. dpa_ctl &= ~DP_PLL_FREQ_MASK;
  2138. if (clock < 200000) {
  2139. u32 temp;
  2140. dpa_ctl |= DP_PLL_FREQ_160MHZ;
  2141. /* workaround for 160Mhz:
  2142. 1) program 0x4600c bits 15:0 = 0x8124
  2143. 2) program 0x46010 bit 0 = 1
  2144. 3) program 0x46034 bit 24 = 1
  2145. 4) program 0x64000 bit 14 = 1
  2146. */
  2147. temp = I915_READ(0x4600c);
  2148. temp &= 0xffff0000;
  2149. I915_WRITE(0x4600c, temp | 0x8124);
  2150. temp = I915_READ(0x46010);
  2151. I915_WRITE(0x46010, temp | 1);
  2152. temp = I915_READ(0x46034);
  2153. I915_WRITE(0x46034, temp | (1 << 24));
  2154. } else {
  2155. dpa_ctl |= DP_PLL_FREQ_270MHZ;
  2156. }
  2157. I915_WRITE(DP_A, dpa_ctl);
  2158. POSTING_READ(DP_A);
  2159. udelay(500);
  2160. }
  2161. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2162. {
  2163. struct drm_device *dev = crtc->dev;
  2164. struct drm_i915_private *dev_priv = dev->dev_private;
  2165. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2166. int pipe = intel_crtc->pipe;
  2167. u32 reg, temp;
  2168. /* enable normal train */
  2169. reg = FDI_TX_CTL(pipe);
  2170. temp = I915_READ(reg);
  2171. if (IS_IVYBRIDGE(dev)) {
  2172. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2173. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2174. } else {
  2175. temp &= ~FDI_LINK_TRAIN_NONE;
  2176. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2177. }
  2178. I915_WRITE(reg, temp);
  2179. reg = FDI_RX_CTL(pipe);
  2180. temp = I915_READ(reg);
  2181. if (HAS_PCH_CPT(dev)) {
  2182. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2183. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2184. } else {
  2185. temp &= ~FDI_LINK_TRAIN_NONE;
  2186. temp |= FDI_LINK_TRAIN_NONE;
  2187. }
  2188. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2189. /* wait one idle pattern time */
  2190. POSTING_READ(reg);
  2191. udelay(1000);
  2192. /* IVB wants error correction enabled */
  2193. if (IS_IVYBRIDGE(dev))
  2194. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2195. FDI_FE_ERRC_ENABLE);
  2196. }
  2197. static void cpt_phase_pointer_enable(struct drm_device *dev, int pipe)
  2198. {
  2199. struct drm_i915_private *dev_priv = dev->dev_private;
  2200. u32 flags = I915_READ(SOUTH_CHICKEN1);
  2201. flags |= FDI_PHASE_SYNC_OVR(pipe);
  2202. I915_WRITE(SOUTH_CHICKEN1, flags); /* once to unlock... */
  2203. flags |= FDI_PHASE_SYNC_EN(pipe);
  2204. I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to enable */
  2205. POSTING_READ(SOUTH_CHICKEN1);
  2206. }
  2207. /* The FDI link training functions for ILK/Ibexpeak. */
  2208. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2209. {
  2210. struct drm_device *dev = crtc->dev;
  2211. struct drm_i915_private *dev_priv = dev->dev_private;
  2212. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2213. int pipe = intel_crtc->pipe;
  2214. int plane = intel_crtc->plane;
  2215. u32 reg, temp, tries;
  2216. /* FDI needs bits from pipe & plane first */
  2217. assert_pipe_enabled(dev_priv, pipe);
  2218. assert_plane_enabled(dev_priv, plane);
  2219. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2220. for train result */
  2221. reg = FDI_RX_IMR(pipe);
  2222. temp = I915_READ(reg);
  2223. temp &= ~FDI_RX_SYMBOL_LOCK;
  2224. temp &= ~FDI_RX_BIT_LOCK;
  2225. I915_WRITE(reg, temp);
  2226. I915_READ(reg);
  2227. udelay(150);
  2228. /* enable CPU FDI TX and PCH FDI RX */
  2229. reg = FDI_TX_CTL(pipe);
  2230. temp = I915_READ(reg);
  2231. temp &= ~(7 << 19);
  2232. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2233. temp &= ~FDI_LINK_TRAIN_NONE;
  2234. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2235. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2236. reg = FDI_RX_CTL(pipe);
  2237. temp = I915_READ(reg);
  2238. temp &= ~FDI_LINK_TRAIN_NONE;
  2239. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2240. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2241. POSTING_READ(reg);
  2242. udelay(150);
  2243. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2244. if (HAS_PCH_IBX(dev)) {
  2245. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2246. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2247. FDI_RX_PHASE_SYNC_POINTER_EN);
  2248. }
  2249. reg = FDI_RX_IIR(pipe);
  2250. for (tries = 0; tries < 5; tries++) {
  2251. temp = I915_READ(reg);
  2252. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2253. if ((temp & FDI_RX_BIT_LOCK)) {
  2254. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2255. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2256. break;
  2257. }
  2258. }
  2259. if (tries == 5)
  2260. DRM_ERROR("FDI train 1 fail!\n");
  2261. /* Train 2 */
  2262. reg = FDI_TX_CTL(pipe);
  2263. temp = I915_READ(reg);
  2264. temp &= ~FDI_LINK_TRAIN_NONE;
  2265. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2266. I915_WRITE(reg, temp);
  2267. reg = FDI_RX_CTL(pipe);
  2268. temp = I915_READ(reg);
  2269. temp &= ~FDI_LINK_TRAIN_NONE;
  2270. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2271. I915_WRITE(reg, temp);
  2272. POSTING_READ(reg);
  2273. udelay(150);
  2274. reg = FDI_RX_IIR(pipe);
  2275. for (tries = 0; tries < 5; tries++) {
  2276. temp = I915_READ(reg);
  2277. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2278. if (temp & FDI_RX_SYMBOL_LOCK) {
  2279. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2280. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2281. break;
  2282. }
  2283. }
  2284. if (tries == 5)
  2285. DRM_ERROR("FDI train 2 fail!\n");
  2286. DRM_DEBUG_KMS("FDI train done\n");
  2287. }
  2288. static const int snb_b_fdi_train_param[] = {
  2289. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2290. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2291. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2292. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2293. };
  2294. /* The FDI link training functions for SNB/Cougarpoint. */
  2295. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2296. {
  2297. struct drm_device *dev = crtc->dev;
  2298. struct drm_i915_private *dev_priv = dev->dev_private;
  2299. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2300. int pipe = intel_crtc->pipe;
  2301. u32 reg, temp, i, retry;
  2302. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2303. for train result */
  2304. reg = FDI_RX_IMR(pipe);
  2305. temp = I915_READ(reg);
  2306. temp &= ~FDI_RX_SYMBOL_LOCK;
  2307. temp &= ~FDI_RX_BIT_LOCK;
  2308. I915_WRITE(reg, temp);
  2309. POSTING_READ(reg);
  2310. udelay(150);
  2311. /* enable CPU FDI TX and PCH FDI RX */
  2312. reg = FDI_TX_CTL(pipe);
  2313. temp = I915_READ(reg);
  2314. temp &= ~(7 << 19);
  2315. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2316. temp &= ~FDI_LINK_TRAIN_NONE;
  2317. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2318. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2319. /* SNB-B */
  2320. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2321. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2322. reg = FDI_RX_CTL(pipe);
  2323. temp = I915_READ(reg);
  2324. if (HAS_PCH_CPT(dev)) {
  2325. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2326. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2327. } else {
  2328. temp &= ~FDI_LINK_TRAIN_NONE;
  2329. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2330. }
  2331. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2332. POSTING_READ(reg);
  2333. udelay(150);
  2334. if (HAS_PCH_CPT(dev))
  2335. cpt_phase_pointer_enable(dev, pipe);
  2336. for (i = 0; i < 4; i++) {
  2337. reg = FDI_TX_CTL(pipe);
  2338. temp = I915_READ(reg);
  2339. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2340. temp |= snb_b_fdi_train_param[i];
  2341. I915_WRITE(reg, temp);
  2342. POSTING_READ(reg);
  2343. udelay(500);
  2344. for (retry = 0; retry < 5; retry++) {
  2345. reg = FDI_RX_IIR(pipe);
  2346. temp = I915_READ(reg);
  2347. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2348. if (temp & FDI_RX_BIT_LOCK) {
  2349. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2350. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2351. break;
  2352. }
  2353. udelay(50);
  2354. }
  2355. if (retry < 5)
  2356. break;
  2357. }
  2358. if (i == 4)
  2359. DRM_ERROR("FDI train 1 fail!\n");
  2360. /* Train 2 */
  2361. reg = FDI_TX_CTL(pipe);
  2362. temp = I915_READ(reg);
  2363. temp &= ~FDI_LINK_TRAIN_NONE;
  2364. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2365. if (IS_GEN6(dev)) {
  2366. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2367. /* SNB-B */
  2368. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2369. }
  2370. I915_WRITE(reg, temp);
  2371. reg = FDI_RX_CTL(pipe);
  2372. temp = I915_READ(reg);
  2373. if (HAS_PCH_CPT(dev)) {
  2374. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2375. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2376. } else {
  2377. temp &= ~FDI_LINK_TRAIN_NONE;
  2378. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2379. }
  2380. I915_WRITE(reg, temp);
  2381. POSTING_READ(reg);
  2382. udelay(150);
  2383. for (i = 0; i < 4; i++) {
  2384. reg = FDI_TX_CTL(pipe);
  2385. temp = I915_READ(reg);
  2386. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2387. temp |= snb_b_fdi_train_param[i];
  2388. I915_WRITE(reg, temp);
  2389. POSTING_READ(reg);
  2390. udelay(500);
  2391. for (retry = 0; retry < 5; retry++) {
  2392. reg = FDI_RX_IIR(pipe);
  2393. temp = I915_READ(reg);
  2394. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2395. if (temp & FDI_RX_SYMBOL_LOCK) {
  2396. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2397. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2398. break;
  2399. }
  2400. udelay(50);
  2401. }
  2402. if (retry < 5)
  2403. break;
  2404. }
  2405. if (i == 4)
  2406. DRM_ERROR("FDI train 2 fail!\n");
  2407. DRM_DEBUG_KMS("FDI train done.\n");
  2408. }
  2409. /* Manual link training for Ivy Bridge A0 parts */
  2410. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2411. {
  2412. struct drm_device *dev = crtc->dev;
  2413. struct drm_i915_private *dev_priv = dev->dev_private;
  2414. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2415. int pipe = intel_crtc->pipe;
  2416. u32 reg, temp, i;
  2417. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2418. for train result */
  2419. reg = FDI_RX_IMR(pipe);
  2420. temp = I915_READ(reg);
  2421. temp &= ~FDI_RX_SYMBOL_LOCK;
  2422. temp &= ~FDI_RX_BIT_LOCK;
  2423. I915_WRITE(reg, temp);
  2424. POSTING_READ(reg);
  2425. udelay(150);
  2426. /* enable CPU FDI TX and PCH FDI RX */
  2427. reg = FDI_TX_CTL(pipe);
  2428. temp = I915_READ(reg);
  2429. temp &= ~(7 << 19);
  2430. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2431. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2432. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2433. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2434. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2435. temp |= FDI_COMPOSITE_SYNC;
  2436. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2437. reg = FDI_RX_CTL(pipe);
  2438. temp = I915_READ(reg);
  2439. temp &= ~FDI_LINK_TRAIN_AUTO;
  2440. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2441. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2442. temp |= FDI_COMPOSITE_SYNC;
  2443. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2444. POSTING_READ(reg);
  2445. udelay(150);
  2446. if (HAS_PCH_CPT(dev))
  2447. cpt_phase_pointer_enable(dev, pipe);
  2448. for (i = 0; i < 4; i++) {
  2449. reg = FDI_TX_CTL(pipe);
  2450. temp = I915_READ(reg);
  2451. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2452. temp |= snb_b_fdi_train_param[i];
  2453. I915_WRITE(reg, temp);
  2454. POSTING_READ(reg);
  2455. udelay(500);
  2456. reg = FDI_RX_IIR(pipe);
  2457. temp = I915_READ(reg);
  2458. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2459. if (temp & FDI_RX_BIT_LOCK ||
  2460. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2461. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2462. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2463. break;
  2464. }
  2465. }
  2466. if (i == 4)
  2467. DRM_ERROR("FDI train 1 fail!\n");
  2468. /* Train 2 */
  2469. reg = FDI_TX_CTL(pipe);
  2470. temp = I915_READ(reg);
  2471. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2472. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2473. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2474. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2475. I915_WRITE(reg, temp);
  2476. reg = FDI_RX_CTL(pipe);
  2477. temp = I915_READ(reg);
  2478. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2479. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2480. I915_WRITE(reg, temp);
  2481. POSTING_READ(reg);
  2482. udelay(150);
  2483. for (i = 0; i < 4; i++) {
  2484. reg = FDI_TX_CTL(pipe);
  2485. temp = I915_READ(reg);
  2486. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2487. temp |= snb_b_fdi_train_param[i];
  2488. I915_WRITE(reg, temp);
  2489. POSTING_READ(reg);
  2490. udelay(500);
  2491. reg = FDI_RX_IIR(pipe);
  2492. temp = I915_READ(reg);
  2493. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2494. if (temp & FDI_RX_SYMBOL_LOCK) {
  2495. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2496. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2497. break;
  2498. }
  2499. }
  2500. if (i == 4)
  2501. DRM_ERROR("FDI train 2 fail!\n");
  2502. DRM_DEBUG_KMS("FDI train done.\n");
  2503. }
  2504. static void ironlake_fdi_pll_enable(struct drm_crtc *crtc)
  2505. {
  2506. struct drm_device *dev = crtc->dev;
  2507. struct drm_i915_private *dev_priv = dev->dev_private;
  2508. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2509. int pipe = intel_crtc->pipe;
  2510. u32 reg, temp;
  2511. /* Write the TU size bits so error detection works */
  2512. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  2513. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  2514. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2515. reg = FDI_RX_CTL(pipe);
  2516. temp = I915_READ(reg);
  2517. temp &= ~((0x7 << 19) | (0x7 << 16));
  2518. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2519. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2520. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2521. POSTING_READ(reg);
  2522. udelay(200);
  2523. /* Switch from Rawclk to PCDclk */
  2524. temp = I915_READ(reg);
  2525. I915_WRITE(reg, temp | FDI_PCDCLK);
  2526. POSTING_READ(reg);
  2527. udelay(200);
  2528. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2529. reg = FDI_TX_CTL(pipe);
  2530. temp = I915_READ(reg);
  2531. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2532. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2533. POSTING_READ(reg);
  2534. udelay(100);
  2535. }
  2536. }
  2537. static void cpt_phase_pointer_disable(struct drm_device *dev, int pipe)
  2538. {
  2539. struct drm_i915_private *dev_priv = dev->dev_private;
  2540. u32 flags = I915_READ(SOUTH_CHICKEN1);
  2541. flags &= ~(FDI_PHASE_SYNC_EN(pipe));
  2542. I915_WRITE(SOUTH_CHICKEN1, flags); /* once to disable... */
  2543. flags &= ~(FDI_PHASE_SYNC_OVR(pipe));
  2544. I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to lock */
  2545. POSTING_READ(SOUTH_CHICKEN1);
  2546. }
  2547. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2548. {
  2549. struct drm_device *dev = crtc->dev;
  2550. struct drm_i915_private *dev_priv = dev->dev_private;
  2551. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2552. int pipe = intel_crtc->pipe;
  2553. u32 reg, temp;
  2554. /* disable CPU FDI tx and PCH FDI rx */
  2555. reg = FDI_TX_CTL(pipe);
  2556. temp = I915_READ(reg);
  2557. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2558. POSTING_READ(reg);
  2559. reg = FDI_RX_CTL(pipe);
  2560. temp = I915_READ(reg);
  2561. temp &= ~(0x7 << 16);
  2562. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2563. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2564. POSTING_READ(reg);
  2565. udelay(100);
  2566. /* Ironlake workaround, disable clock pointer after downing FDI */
  2567. if (HAS_PCH_IBX(dev)) {
  2568. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2569. I915_WRITE(FDI_RX_CHICKEN(pipe),
  2570. I915_READ(FDI_RX_CHICKEN(pipe) &
  2571. ~FDI_RX_PHASE_SYNC_POINTER_EN));
  2572. } else if (HAS_PCH_CPT(dev)) {
  2573. cpt_phase_pointer_disable(dev, pipe);
  2574. }
  2575. /* still set train pattern 1 */
  2576. reg = FDI_TX_CTL(pipe);
  2577. temp = I915_READ(reg);
  2578. temp &= ~FDI_LINK_TRAIN_NONE;
  2579. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2580. I915_WRITE(reg, temp);
  2581. reg = FDI_RX_CTL(pipe);
  2582. temp = I915_READ(reg);
  2583. if (HAS_PCH_CPT(dev)) {
  2584. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2585. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2586. } else {
  2587. temp &= ~FDI_LINK_TRAIN_NONE;
  2588. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2589. }
  2590. /* BPC in FDI rx is consistent with that in PIPECONF */
  2591. temp &= ~(0x07 << 16);
  2592. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2593. I915_WRITE(reg, temp);
  2594. POSTING_READ(reg);
  2595. udelay(100);
  2596. }
  2597. /*
  2598. * When we disable a pipe, we need to clear any pending scanline wait events
  2599. * to avoid hanging the ring, which we assume we are waiting on.
  2600. */
  2601. static void intel_clear_scanline_wait(struct drm_device *dev)
  2602. {
  2603. struct drm_i915_private *dev_priv = dev->dev_private;
  2604. struct intel_ring_buffer *ring;
  2605. u32 tmp;
  2606. if (IS_GEN2(dev))
  2607. /* Can't break the hang on i8xx */
  2608. return;
  2609. ring = LP_RING(dev_priv);
  2610. tmp = I915_READ_CTL(ring);
  2611. if (tmp & RING_WAIT)
  2612. I915_WRITE_CTL(ring, tmp);
  2613. }
  2614. static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  2615. {
  2616. struct drm_i915_gem_object *obj;
  2617. struct drm_i915_private *dev_priv;
  2618. if (crtc->fb == NULL)
  2619. return;
  2620. obj = to_intel_framebuffer(crtc->fb)->obj;
  2621. dev_priv = crtc->dev->dev_private;
  2622. wait_event(dev_priv->pending_flip_queue,
  2623. atomic_read(&obj->pending_flip) == 0);
  2624. }
  2625. static bool intel_crtc_driving_pch(struct drm_crtc *crtc)
  2626. {
  2627. struct drm_device *dev = crtc->dev;
  2628. struct drm_mode_config *mode_config = &dev->mode_config;
  2629. struct intel_encoder *encoder;
  2630. /*
  2631. * If there's a non-PCH eDP on this crtc, it must be DP_A, and that
  2632. * must be driven by its own crtc; no sharing is possible.
  2633. */
  2634. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  2635. if (encoder->base.crtc != crtc)
  2636. continue;
  2637. switch (encoder->type) {
  2638. case INTEL_OUTPUT_EDP:
  2639. if (!intel_encoder_is_pch_edp(&encoder->base))
  2640. return false;
  2641. continue;
  2642. }
  2643. }
  2644. return true;
  2645. }
  2646. /*
  2647. * Enable PCH resources required for PCH ports:
  2648. * - PCH PLLs
  2649. * - FDI training & RX/TX
  2650. * - update transcoder timings
  2651. * - DP transcoding bits
  2652. * - transcoder
  2653. */
  2654. static void ironlake_pch_enable(struct drm_crtc *crtc)
  2655. {
  2656. struct drm_device *dev = crtc->dev;
  2657. struct drm_i915_private *dev_priv = dev->dev_private;
  2658. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2659. int pipe = intel_crtc->pipe;
  2660. u32 reg, temp, transc_sel;
  2661. /* For PCH output, training FDI link */
  2662. dev_priv->display.fdi_link_train(crtc);
  2663. intel_enable_pch_pll(dev_priv, pipe);
  2664. if (HAS_PCH_CPT(dev)) {
  2665. transc_sel = intel_crtc->use_pll_a ? TRANSC_DPLLA_SEL :
  2666. TRANSC_DPLLB_SEL;
  2667. /* Be sure PCH DPLL SEL is set */
  2668. temp = I915_READ(PCH_DPLL_SEL);
  2669. if (pipe == 0) {
  2670. temp &= ~(TRANSA_DPLLB_SEL);
  2671. temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
  2672. } else if (pipe == 1) {
  2673. temp &= ~(TRANSB_DPLLB_SEL);
  2674. temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  2675. } else if (pipe == 2) {
  2676. temp &= ~(TRANSC_DPLLB_SEL);
  2677. temp |= (TRANSC_DPLL_ENABLE | transc_sel);
  2678. }
  2679. I915_WRITE(PCH_DPLL_SEL, temp);
  2680. }
  2681. /* set transcoder timing, panel must allow it */
  2682. assert_panel_unlocked(dev_priv, pipe);
  2683. I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
  2684. I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
  2685. I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
  2686. I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
  2687. I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
  2688. I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
  2689. I915_WRITE(TRANS_VSYNCSHIFT(pipe), I915_READ(VSYNCSHIFT(pipe)));
  2690. intel_fdi_normal_train(crtc);
  2691. /* For PCH DP, enable TRANS_DP_CTL */
  2692. if (HAS_PCH_CPT(dev) &&
  2693. (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  2694. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2695. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) >> 5;
  2696. reg = TRANS_DP_CTL(pipe);
  2697. temp = I915_READ(reg);
  2698. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  2699. TRANS_DP_SYNC_MASK |
  2700. TRANS_DP_BPC_MASK);
  2701. temp |= (TRANS_DP_OUTPUT_ENABLE |
  2702. TRANS_DP_ENH_FRAMING);
  2703. temp |= bpc << 9; /* same format but at 11:9 */
  2704. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  2705. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  2706. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  2707. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  2708. switch (intel_trans_dp_port_sel(crtc)) {
  2709. case PCH_DP_B:
  2710. temp |= TRANS_DP_PORT_SEL_B;
  2711. break;
  2712. case PCH_DP_C:
  2713. temp |= TRANS_DP_PORT_SEL_C;
  2714. break;
  2715. case PCH_DP_D:
  2716. temp |= TRANS_DP_PORT_SEL_D;
  2717. break;
  2718. default:
  2719. DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
  2720. temp |= TRANS_DP_PORT_SEL_B;
  2721. break;
  2722. }
  2723. I915_WRITE(reg, temp);
  2724. }
  2725. intel_enable_transcoder(dev_priv, pipe);
  2726. }
  2727. void intel_cpt_verify_modeset(struct drm_device *dev, int pipe)
  2728. {
  2729. struct drm_i915_private *dev_priv = dev->dev_private;
  2730. int dslreg = PIPEDSL(pipe), tc2reg = TRANS_CHICKEN2(pipe);
  2731. u32 temp;
  2732. temp = I915_READ(dslreg);
  2733. udelay(500);
  2734. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  2735. /* Without this, mode sets may fail silently on FDI */
  2736. I915_WRITE(tc2reg, TRANS_AUTOTRAIN_GEN_STALL_DIS);
  2737. udelay(250);
  2738. I915_WRITE(tc2reg, 0);
  2739. if (wait_for(I915_READ(dslreg) != temp, 5))
  2740. DRM_ERROR("mode set failed: pipe %d stuck\n", pipe);
  2741. }
  2742. }
  2743. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  2744. {
  2745. struct drm_device *dev = crtc->dev;
  2746. struct drm_i915_private *dev_priv = dev->dev_private;
  2747. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2748. int pipe = intel_crtc->pipe;
  2749. int plane = intel_crtc->plane;
  2750. u32 temp;
  2751. bool is_pch_port;
  2752. if (intel_crtc->active)
  2753. return;
  2754. intel_crtc->active = true;
  2755. intel_update_watermarks(dev);
  2756. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  2757. temp = I915_READ(PCH_LVDS);
  2758. if ((temp & LVDS_PORT_EN) == 0)
  2759. I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
  2760. }
  2761. is_pch_port = intel_crtc_driving_pch(crtc);
  2762. if (is_pch_port)
  2763. ironlake_fdi_pll_enable(crtc);
  2764. else
  2765. ironlake_fdi_disable(crtc);
  2766. /* Enable panel fitting for LVDS */
  2767. if (dev_priv->pch_pf_size &&
  2768. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) || HAS_eDP)) {
  2769. /* Force use of hard-coded filter coefficients
  2770. * as some pre-programmed values are broken,
  2771. * e.g. x201.
  2772. */
  2773. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  2774. I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
  2775. I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
  2776. }
  2777. /*
  2778. * On ILK+ LUT must be loaded before the pipe is running but with
  2779. * clocks enabled
  2780. */
  2781. intel_crtc_load_lut(crtc);
  2782. intel_enable_pipe(dev_priv, pipe, is_pch_port);
  2783. intel_enable_plane(dev_priv, plane, pipe);
  2784. if (is_pch_port)
  2785. ironlake_pch_enable(crtc);
  2786. mutex_lock(&dev->struct_mutex);
  2787. intel_update_fbc(dev);
  2788. mutex_unlock(&dev->struct_mutex);
  2789. intel_crtc_update_cursor(crtc, true);
  2790. }
  2791. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  2792. {
  2793. struct drm_device *dev = crtc->dev;
  2794. struct drm_i915_private *dev_priv = dev->dev_private;
  2795. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2796. int pipe = intel_crtc->pipe;
  2797. int plane = intel_crtc->plane;
  2798. u32 reg, temp;
  2799. if (!intel_crtc->active)
  2800. return;
  2801. intel_crtc_wait_for_pending_flips(crtc);
  2802. drm_vblank_off(dev, pipe);
  2803. intel_crtc_update_cursor(crtc, false);
  2804. intel_disable_plane(dev_priv, plane, pipe);
  2805. if (dev_priv->cfb_plane == plane)
  2806. intel_disable_fbc(dev);
  2807. intel_disable_pipe(dev_priv, pipe);
  2808. /* Disable PF */
  2809. I915_WRITE(PF_CTL(pipe), 0);
  2810. I915_WRITE(PF_WIN_SZ(pipe), 0);
  2811. ironlake_fdi_disable(crtc);
  2812. /* This is a horrible layering violation; we should be doing this in
  2813. * the connector/encoder ->prepare instead, but we don't always have
  2814. * enough information there about the config to know whether it will
  2815. * actually be necessary or just cause undesired flicker.
  2816. */
  2817. intel_disable_pch_ports(dev_priv, pipe);
  2818. intel_disable_transcoder(dev_priv, pipe);
  2819. if (HAS_PCH_CPT(dev)) {
  2820. /* disable TRANS_DP_CTL */
  2821. reg = TRANS_DP_CTL(pipe);
  2822. temp = I915_READ(reg);
  2823. temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
  2824. temp |= TRANS_DP_PORT_SEL_NONE;
  2825. I915_WRITE(reg, temp);
  2826. /* disable DPLL_SEL */
  2827. temp = I915_READ(PCH_DPLL_SEL);
  2828. switch (pipe) {
  2829. case 0:
  2830. temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
  2831. break;
  2832. case 1:
  2833. temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  2834. break;
  2835. case 2:
  2836. /* C shares PLL A or B */
  2837. temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
  2838. break;
  2839. default:
  2840. BUG(); /* wtf */
  2841. }
  2842. I915_WRITE(PCH_DPLL_SEL, temp);
  2843. }
  2844. /* disable PCH DPLL */
  2845. if (!intel_crtc->no_pll)
  2846. intel_disable_pch_pll(dev_priv, pipe);
  2847. /* Switch from PCDclk to Rawclk */
  2848. reg = FDI_RX_CTL(pipe);
  2849. temp = I915_READ(reg);
  2850. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2851. /* Disable CPU FDI TX PLL */
  2852. reg = FDI_TX_CTL(pipe);
  2853. temp = I915_READ(reg);
  2854. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2855. POSTING_READ(reg);
  2856. udelay(100);
  2857. reg = FDI_RX_CTL(pipe);
  2858. temp = I915_READ(reg);
  2859. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2860. /* Wait for the clocks to turn off. */
  2861. POSTING_READ(reg);
  2862. udelay(100);
  2863. intel_crtc->active = false;
  2864. intel_update_watermarks(dev);
  2865. mutex_lock(&dev->struct_mutex);
  2866. intel_update_fbc(dev);
  2867. intel_clear_scanline_wait(dev);
  2868. mutex_unlock(&dev->struct_mutex);
  2869. }
  2870. static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
  2871. {
  2872. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2873. int pipe = intel_crtc->pipe;
  2874. int plane = intel_crtc->plane;
  2875. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2876. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2877. */
  2878. switch (mode) {
  2879. case DRM_MODE_DPMS_ON:
  2880. case DRM_MODE_DPMS_STANDBY:
  2881. case DRM_MODE_DPMS_SUSPEND:
  2882. DRM_DEBUG_KMS("crtc %d/%d dpms on\n", pipe, plane);
  2883. ironlake_crtc_enable(crtc);
  2884. break;
  2885. case DRM_MODE_DPMS_OFF:
  2886. DRM_DEBUG_KMS("crtc %d/%d dpms off\n", pipe, plane);
  2887. ironlake_crtc_disable(crtc);
  2888. break;
  2889. }
  2890. }
  2891. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  2892. {
  2893. if (!enable && intel_crtc->overlay) {
  2894. struct drm_device *dev = intel_crtc->base.dev;
  2895. struct drm_i915_private *dev_priv = dev->dev_private;
  2896. mutex_lock(&dev->struct_mutex);
  2897. dev_priv->mm.interruptible = false;
  2898. (void) intel_overlay_switch_off(intel_crtc->overlay);
  2899. dev_priv->mm.interruptible = true;
  2900. mutex_unlock(&dev->struct_mutex);
  2901. }
  2902. /* Let userspace switch the overlay on again. In most cases userspace
  2903. * has to recompute where to put it anyway.
  2904. */
  2905. }
  2906. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  2907. {
  2908. struct drm_device *dev = crtc->dev;
  2909. struct drm_i915_private *dev_priv = dev->dev_private;
  2910. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2911. int pipe = intel_crtc->pipe;
  2912. int plane = intel_crtc->plane;
  2913. if (intel_crtc->active)
  2914. return;
  2915. intel_crtc->active = true;
  2916. intel_update_watermarks(dev);
  2917. intel_enable_pll(dev_priv, pipe);
  2918. intel_enable_pipe(dev_priv, pipe, false);
  2919. intel_enable_plane(dev_priv, plane, pipe);
  2920. intel_crtc_load_lut(crtc);
  2921. intel_update_fbc(dev);
  2922. /* Give the overlay scaler a chance to enable if it's on this pipe */
  2923. intel_crtc_dpms_overlay(intel_crtc, true);
  2924. intel_crtc_update_cursor(crtc, true);
  2925. }
  2926. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  2927. {
  2928. struct drm_device *dev = crtc->dev;
  2929. struct drm_i915_private *dev_priv = dev->dev_private;
  2930. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2931. int pipe = intel_crtc->pipe;
  2932. int plane = intel_crtc->plane;
  2933. if (!intel_crtc->active)
  2934. return;
  2935. /* Give the overlay scaler a chance to disable if it's on this pipe */
  2936. intel_crtc_wait_for_pending_flips(crtc);
  2937. drm_vblank_off(dev, pipe);
  2938. intel_crtc_dpms_overlay(intel_crtc, false);
  2939. intel_crtc_update_cursor(crtc, false);
  2940. if (dev_priv->cfb_plane == plane)
  2941. intel_disable_fbc(dev);
  2942. intel_disable_plane(dev_priv, plane, pipe);
  2943. intel_disable_pipe(dev_priv, pipe);
  2944. intel_disable_pll(dev_priv, pipe);
  2945. intel_crtc->active = false;
  2946. intel_update_fbc(dev);
  2947. intel_update_watermarks(dev);
  2948. intel_clear_scanline_wait(dev);
  2949. }
  2950. static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
  2951. {
  2952. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2953. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2954. */
  2955. switch (mode) {
  2956. case DRM_MODE_DPMS_ON:
  2957. case DRM_MODE_DPMS_STANDBY:
  2958. case DRM_MODE_DPMS_SUSPEND:
  2959. i9xx_crtc_enable(crtc);
  2960. break;
  2961. case DRM_MODE_DPMS_OFF:
  2962. i9xx_crtc_disable(crtc);
  2963. break;
  2964. }
  2965. }
  2966. /**
  2967. * Sets the power management mode of the pipe and plane.
  2968. */
  2969. static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
  2970. {
  2971. struct drm_device *dev = crtc->dev;
  2972. struct drm_i915_private *dev_priv = dev->dev_private;
  2973. struct drm_i915_master_private *master_priv;
  2974. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2975. int pipe = intel_crtc->pipe;
  2976. bool enabled;
  2977. if (intel_crtc->dpms_mode == mode)
  2978. return;
  2979. intel_crtc->dpms_mode = mode;
  2980. dev_priv->display.dpms(crtc, mode);
  2981. if (!dev->primary->master)
  2982. return;
  2983. master_priv = dev->primary->master->driver_priv;
  2984. if (!master_priv->sarea_priv)
  2985. return;
  2986. enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
  2987. switch (pipe) {
  2988. case 0:
  2989. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  2990. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  2991. break;
  2992. case 1:
  2993. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  2994. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  2995. break;
  2996. default:
  2997. DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
  2998. break;
  2999. }
  3000. }
  3001. static void intel_crtc_disable(struct drm_crtc *crtc)
  3002. {
  3003. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  3004. struct drm_device *dev = crtc->dev;
  3005. /* Flush any pending WAITs before we disable the pipe. Note that
  3006. * we need to drop the struct_mutex in order to acquire it again
  3007. * during the lowlevel dpms routines around a couple of the
  3008. * operations. It does not look trivial nor desirable to move
  3009. * that locking higher. So instead we leave a window for the
  3010. * submission of further commands on the fb before we can actually
  3011. * disable it. This race with userspace exists anyway, and we can
  3012. * only rely on the pipe being disabled by userspace after it
  3013. * receives the hotplug notification and has flushed any pending
  3014. * batches.
  3015. */
  3016. if (crtc->fb) {
  3017. mutex_lock(&dev->struct_mutex);
  3018. intel_finish_fb(crtc->fb);
  3019. mutex_unlock(&dev->struct_mutex);
  3020. }
  3021. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  3022. assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
  3023. assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
  3024. if (crtc->fb) {
  3025. mutex_lock(&dev->struct_mutex);
  3026. intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
  3027. mutex_unlock(&dev->struct_mutex);
  3028. }
  3029. }
  3030. /* Prepare for a mode set.
  3031. *
  3032. * Note we could be a lot smarter here. We need to figure out which outputs
  3033. * will be enabled, which disabled (in short, how the config will changes)
  3034. * and perform the minimum necessary steps to accomplish that, e.g. updating
  3035. * watermarks, FBC configuration, making sure PLLs are programmed correctly,
  3036. * panel fitting is in the proper state, etc.
  3037. */
  3038. static void i9xx_crtc_prepare(struct drm_crtc *crtc)
  3039. {
  3040. i9xx_crtc_disable(crtc);
  3041. }
  3042. static void i9xx_crtc_commit(struct drm_crtc *crtc)
  3043. {
  3044. i9xx_crtc_enable(crtc);
  3045. }
  3046. static void ironlake_crtc_prepare(struct drm_crtc *crtc)
  3047. {
  3048. ironlake_crtc_disable(crtc);
  3049. }
  3050. static void ironlake_crtc_commit(struct drm_crtc *crtc)
  3051. {
  3052. ironlake_crtc_enable(crtc);
  3053. }
  3054. void intel_encoder_prepare(struct drm_encoder *encoder)
  3055. {
  3056. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  3057. /* lvds has its own version of prepare see intel_lvds_prepare */
  3058. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  3059. }
  3060. void intel_encoder_commit(struct drm_encoder *encoder)
  3061. {
  3062. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  3063. struct drm_device *dev = encoder->dev;
  3064. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  3065. struct intel_crtc *intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  3066. /* lvds has its own version of commit see intel_lvds_commit */
  3067. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  3068. if (HAS_PCH_CPT(dev))
  3069. intel_cpt_verify_modeset(dev, intel_crtc->pipe);
  3070. }
  3071. void intel_encoder_destroy(struct drm_encoder *encoder)
  3072. {
  3073. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  3074. drm_encoder_cleanup(encoder);
  3075. kfree(intel_encoder);
  3076. }
  3077. static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
  3078. struct drm_display_mode *mode,
  3079. struct drm_display_mode *adjusted_mode)
  3080. {
  3081. struct drm_device *dev = crtc->dev;
  3082. if (HAS_PCH_SPLIT(dev)) {
  3083. /* FDI link clock is fixed at 2.7G */
  3084. if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
  3085. return false;
  3086. }
  3087. /* All interlaced capable intel hw wants timings in frames. */
  3088. drm_mode_set_crtcinfo(adjusted_mode, 0);
  3089. return true;
  3090. }
  3091. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  3092. {
  3093. return 400000; /* FIXME */
  3094. }
  3095. static int i945_get_display_clock_speed(struct drm_device *dev)
  3096. {
  3097. return 400000;
  3098. }
  3099. static int i915_get_display_clock_speed(struct drm_device *dev)
  3100. {
  3101. return 333000;
  3102. }
  3103. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  3104. {
  3105. return 200000;
  3106. }
  3107. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  3108. {
  3109. u16 gcfgc = 0;
  3110. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  3111. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  3112. return 133000;
  3113. else {
  3114. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  3115. case GC_DISPLAY_CLOCK_333_MHZ:
  3116. return 333000;
  3117. default:
  3118. case GC_DISPLAY_CLOCK_190_200_MHZ:
  3119. return 190000;
  3120. }
  3121. }
  3122. }
  3123. static int i865_get_display_clock_speed(struct drm_device *dev)
  3124. {
  3125. return 266000;
  3126. }
  3127. static int i855_get_display_clock_speed(struct drm_device *dev)
  3128. {
  3129. u16 hpllcc = 0;
  3130. /* Assume that the hardware is in the high speed state. This
  3131. * should be the default.
  3132. */
  3133. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  3134. case GC_CLOCK_133_200:
  3135. case GC_CLOCK_100_200:
  3136. return 200000;
  3137. case GC_CLOCK_166_250:
  3138. return 250000;
  3139. case GC_CLOCK_100_133:
  3140. return 133000;
  3141. }
  3142. /* Shouldn't happen */
  3143. return 0;
  3144. }
  3145. static int i830_get_display_clock_speed(struct drm_device *dev)
  3146. {
  3147. return 133000;
  3148. }
  3149. struct fdi_m_n {
  3150. u32 tu;
  3151. u32 gmch_m;
  3152. u32 gmch_n;
  3153. u32 link_m;
  3154. u32 link_n;
  3155. };
  3156. static void
  3157. fdi_reduce_ratio(u32 *num, u32 *den)
  3158. {
  3159. while (*num > 0xffffff || *den > 0xffffff) {
  3160. *num >>= 1;
  3161. *den >>= 1;
  3162. }
  3163. }
  3164. static void
  3165. ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
  3166. int link_clock, struct fdi_m_n *m_n)
  3167. {
  3168. m_n->tu = 64; /* default size */
  3169. /* BUG_ON(pixel_clock > INT_MAX / 36); */
  3170. m_n->gmch_m = bits_per_pixel * pixel_clock;
  3171. m_n->gmch_n = link_clock * nlanes * 8;
  3172. fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
  3173. m_n->link_m = pixel_clock;
  3174. m_n->link_n = link_clock;
  3175. fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
  3176. }
  3177. struct intel_watermark_params {
  3178. unsigned long fifo_size;
  3179. unsigned long max_wm;
  3180. unsigned long default_wm;
  3181. unsigned long guard_size;
  3182. unsigned long cacheline_size;
  3183. };
  3184. /* Pineview has different values for various configs */
  3185. static const struct intel_watermark_params pineview_display_wm = {
  3186. PINEVIEW_DISPLAY_FIFO,
  3187. PINEVIEW_MAX_WM,
  3188. PINEVIEW_DFT_WM,
  3189. PINEVIEW_GUARD_WM,
  3190. PINEVIEW_FIFO_LINE_SIZE
  3191. };
  3192. static const struct intel_watermark_params pineview_display_hplloff_wm = {
  3193. PINEVIEW_DISPLAY_FIFO,
  3194. PINEVIEW_MAX_WM,
  3195. PINEVIEW_DFT_HPLLOFF_WM,
  3196. PINEVIEW_GUARD_WM,
  3197. PINEVIEW_FIFO_LINE_SIZE
  3198. };
  3199. static const struct intel_watermark_params pineview_cursor_wm = {
  3200. PINEVIEW_CURSOR_FIFO,
  3201. PINEVIEW_CURSOR_MAX_WM,
  3202. PINEVIEW_CURSOR_DFT_WM,
  3203. PINEVIEW_CURSOR_GUARD_WM,
  3204. PINEVIEW_FIFO_LINE_SIZE,
  3205. };
  3206. static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
  3207. PINEVIEW_CURSOR_FIFO,
  3208. PINEVIEW_CURSOR_MAX_WM,
  3209. PINEVIEW_CURSOR_DFT_WM,
  3210. PINEVIEW_CURSOR_GUARD_WM,
  3211. PINEVIEW_FIFO_LINE_SIZE
  3212. };
  3213. static const struct intel_watermark_params g4x_wm_info = {
  3214. G4X_FIFO_SIZE,
  3215. G4X_MAX_WM,
  3216. G4X_MAX_WM,
  3217. 2,
  3218. G4X_FIFO_LINE_SIZE,
  3219. };
  3220. static const struct intel_watermark_params g4x_cursor_wm_info = {
  3221. I965_CURSOR_FIFO,
  3222. I965_CURSOR_MAX_WM,
  3223. I965_CURSOR_DFT_WM,
  3224. 2,
  3225. G4X_FIFO_LINE_SIZE,
  3226. };
  3227. static const struct intel_watermark_params valleyview_wm_info = {
  3228. VALLEYVIEW_FIFO_SIZE,
  3229. VALLEYVIEW_MAX_WM,
  3230. VALLEYVIEW_MAX_WM,
  3231. 2,
  3232. G4X_FIFO_LINE_SIZE,
  3233. };
  3234. static const struct intel_watermark_params valleyview_cursor_wm_info = {
  3235. I965_CURSOR_FIFO,
  3236. VALLEYVIEW_CURSOR_MAX_WM,
  3237. I965_CURSOR_DFT_WM,
  3238. 2,
  3239. G4X_FIFO_LINE_SIZE,
  3240. };
  3241. static const struct intel_watermark_params i965_cursor_wm_info = {
  3242. I965_CURSOR_FIFO,
  3243. I965_CURSOR_MAX_WM,
  3244. I965_CURSOR_DFT_WM,
  3245. 2,
  3246. I915_FIFO_LINE_SIZE,
  3247. };
  3248. static const struct intel_watermark_params i945_wm_info = {
  3249. I945_FIFO_SIZE,
  3250. I915_MAX_WM,
  3251. 1,
  3252. 2,
  3253. I915_FIFO_LINE_SIZE
  3254. };
  3255. static const struct intel_watermark_params i915_wm_info = {
  3256. I915_FIFO_SIZE,
  3257. I915_MAX_WM,
  3258. 1,
  3259. 2,
  3260. I915_FIFO_LINE_SIZE
  3261. };
  3262. static const struct intel_watermark_params i855_wm_info = {
  3263. I855GM_FIFO_SIZE,
  3264. I915_MAX_WM,
  3265. 1,
  3266. 2,
  3267. I830_FIFO_LINE_SIZE
  3268. };
  3269. static const struct intel_watermark_params i830_wm_info = {
  3270. I830_FIFO_SIZE,
  3271. I915_MAX_WM,
  3272. 1,
  3273. 2,
  3274. I830_FIFO_LINE_SIZE
  3275. };
  3276. static const struct intel_watermark_params ironlake_display_wm_info = {
  3277. ILK_DISPLAY_FIFO,
  3278. ILK_DISPLAY_MAXWM,
  3279. ILK_DISPLAY_DFTWM,
  3280. 2,
  3281. ILK_FIFO_LINE_SIZE
  3282. };
  3283. static const struct intel_watermark_params ironlake_cursor_wm_info = {
  3284. ILK_CURSOR_FIFO,
  3285. ILK_CURSOR_MAXWM,
  3286. ILK_CURSOR_DFTWM,
  3287. 2,
  3288. ILK_FIFO_LINE_SIZE
  3289. };
  3290. static const struct intel_watermark_params ironlake_display_srwm_info = {
  3291. ILK_DISPLAY_SR_FIFO,
  3292. ILK_DISPLAY_MAX_SRWM,
  3293. ILK_DISPLAY_DFT_SRWM,
  3294. 2,
  3295. ILK_FIFO_LINE_SIZE
  3296. };
  3297. static const struct intel_watermark_params ironlake_cursor_srwm_info = {
  3298. ILK_CURSOR_SR_FIFO,
  3299. ILK_CURSOR_MAX_SRWM,
  3300. ILK_CURSOR_DFT_SRWM,
  3301. 2,
  3302. ILK_FIFO_LINE_SIZE
  3303. };
  3304. static const struct intel_watermark_params sandybridge_display_wm_info = {
  3305. SNB_DISPLAY_FIFO,
  3306. SNB_DISPLAY_MAXWM,
  3307. SNB_DISPLAY_DFTWM,
  3308. 2,
  3309. SNB_FIFO_LINE_SIZE
  3310. };
  3311. static const struct intel_watermark_params sandybridge_cursor_wm_info = {
  3312. SNB_CURSOR_FIFO,
  3313. SNB_CURSOR_MAXWM,
  3314. SNB_CURSOR_DFTWM,
  3315. 2,
  3316. SNB_FIFO_LINE_SIZE
  3317. };
  3318. static const struct intel_watermark_params sandybridge_display_srwm_info = {
  3319. SNB_DISPLAY_SR_FIFO,
  3320. SNB_DISPLAY_MAX_SRWM,
  3321. SNB_DISPLAY_DFT_SRWM,
  3322. 2,
  3323. SNB_FIFO_LINE_SIZE
  3324. };
  3325. static const struct intel_watermark_params sandybridge_cursor_srwm_info = {
  3326. SNB_CURSOR_SR_FIFO,
  3327. SNB_CURSOR_MAX_SRWM,
  3328. SNB_CURSOR_DFT_SRWM,
  3329. 2,
  3330. SNB_FIFO_LINE_SIZE
  3331. };
  3332. /**
  3333. * intel_calculate_wm - calculate watermark level
  3334. * @clock_in_khz: pixel clock
  3335. * @wm: chip FIFO params
  3336. * @pixel_size: display pixel size
  3337. * @latency_ns: memory latency for the platform
  3338. *
  3339. * Calculate the watermark level (the level at which the display plane will
  3340. * start fetching from memory again). Each chip has a different display
  3341. * FIFO size and allocation, so the caller needs to figure that out and pass
  3342. * in the correct intel_watermark_params structure.
  3343. *
  3344. * As the pixel clock runs, the FIFO will be drained at a rate that depends
  3345. * on the pixel size. When it reaches the watermark level, it'll start
  3346. * fetching FIFO line sized based chunks from memory until the FIFO fills
  3347. * past the watermark point. If the FIFO drains completely, a FIFO underrun
  3348. * will occur, and a display engine hang could result.
  3349. */
  3350. static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
  3351. const struct intel_watermark_params *wm,
  3352. int fifo_size,
  3353. int pixel_size,
  3354. unsigned long latency_ns)
  3355. {
  3356. long entries_required, wm_size;
  3357. /*
  3358. * Note: we need to make sure we don't overflow for various clock &
  3359. * latency values.
  3360. * clocks go from a few thousand to several hundred thousand.
  3361. * latency is usually a few thousand
  3362. */
  3363. entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
  3364. 1000;
  3365. entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
  3366. DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
  3367. wm_size = fifo_size - (entries_required + wm->guard_size);
  3368. DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
  3369. /* Don't promote wm_size to unsigned... */
  3370. if (wm_size > (long)wm->max_wm)
  3371. wm_size = wm->max_wm;
  3372. if (wm_size <= 0)
  3373. wm_size = wm->default_wm;
  3374. return wm_size;
  3375. }
  3376. struct cxsr_latency {
  3377. int is_desktop;
  3378. int is_ddr3;
  3379. unsigned long fsb_freq;
  3380. unsigned long mem_freq;
  3381. unsigned long display_sr;
  3382. unsigned long display_hpll_disable;
  3383. unsigned long cursor_sr;
  3384. unsigned long cursor_hpll_disable;
  3385. };
  3386. static const struct cxsr_latency cxsr_latency_table[] = {
  3387. {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
  3388. {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
  3389. {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
  3390. {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
  3391. {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
  3392. {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
  3393. {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
  3394. {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
  3395. {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
  3396. {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
  3397. {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
  3398. {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
  3399. {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
  3400. {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
  3401. {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
  3402. {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
  3403. {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
  3404. {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
  3405. {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
  3406. {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
  3407. {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
  3408. {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
  3409. {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
  3410. {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
  3411. {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
  3412. {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
  3413. {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
  3414. {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
  3415. {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
  3416. {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
  3417. };
  3418. static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
  3419. int is_ddr3,
  3420. int fsb,
  3421. int mem)
  3422. {
  3423. const struct cxsr_latency *latency;
  3424. int i;
  3425. if (fsb == 0 || mem == 0)
  3426. return NULL;
  3427. for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
  3428. latency = &cxsr_latency_table[i];
  3429. if (is_desktop == latency->is_desktop &&
  3430. is_ddr3 == latency->is_ddr3 &&
  3431. fsb == latency->fsb_freq && mem == latency->mem_freq)
  3432. return latency;
  3433. }
  3434. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  3435. return NULL;
  3436. }
  3437. static void pineview_disable_cxsr(struct drm_device *dev)
  3438. {
  3439. struct drm_i915_private *dev_priv = dev->dev_private;
  3440. /* deactivate cxsr */
  3441. I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
  3442. }
  3443. /*
  3444. * Latency for FIFO fetches is dependent on several factors:
  3445. * - memory configuration (speed, channels)
  3446. * - chipset
  3447. * - current MCH state
  3448. * It can be fairly high in some situations, so here we assume a fairly
  3449. * pessimal value. It's a tradeoff between extra memory fetches (if we
  3450. * set this value too high, the FIFO will fetch frequently to stay full)
  3451. * and power consumption (set it too low to save power and we might see
  3452. * FIFO underruns and display "flicker").
  3453. *
  3454. * A value of 5us seems to be a good balance; safe for very low end
  3455. * platforms but not overly aggressive on lower latency configs.
  3456. */
  3457. static const int latency_ns = 5000;
  3458. static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
  3459. {
  3460. struct drm_i915_private *dev_priv = dev->dev_private;
  3461. uint32_t dsparb = I915_READ(DSPARB);
  3462. int size;
  3463. size = dsparb & 0x7f;
  3464. if (plane)
  3465. size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
  3466. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  3467. plane ? "B" : "A", size);
  3468. return size;
  3469. }
  3470. static int i85x_get_fifo_size(struct drm_device *dev, int plane)
  3471. {
  3472. struct drm_i915_private *dev_priv = dev->dev_private;
  3473. uint32_t dsparb = I915_READ(DSPARB);
  3474. int size;
  3475. size = dsparb & 0x1ff;
  3476. if (plane)
  3477. size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
  3478. size >>= 1; /* Convert to cachelines */
  3479. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  3480. plane ? "B" : "A", size);
  3481. return size;
  3482. }
  3483. static int i845_get_fifo_size(struct drm_device *dev, int plane)
  3484. {
  3485. struct drm_i915_private *dev_priv = dev->dev_private;
  3486. uint32_t dsparb = I915_READ(DSPARB);
  3487. int size;
  3488. size = dsparb & 0x7f;
  3489. size >>= 2; /* Convert to cachelines */
  3490. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  3491. plane ? "B" : "A",
  3492. size);
  3493. return size;
  3494. }
  3495. static int i830_get_fifo_size(struct drm_device *dev, int plane)
  3496. {
  3497. struct drm_i915_private *dev_priv = dev->dev_private;
  3498. uint32_t dsparb = I915_READ(DSPARB);
  3499. int size;
  3500. size = dsparb & 0x7f;
  3501. size >>= 1; /* Convert to cachelines */
  3502. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  3503. plane ? "B" : "A", size);
  3504. return size;
  3505. }
  3506. static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
  3507. {
  3508. struct drm_crtc *crtc, *enabled = NULL;
  3509. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  3510. if (crtc->enabled && crtc->fb) {
  3511. if (enabled)
  3512. return NULL;
  3513. enabled = crtc;
  3514. }
  3515. }
  3516. return enabled;
  3517. }
  3518. static void pineview_update_wm(struct drm_device *dev)
  3519. {
  3520. struct drm_i915_private *dev_priv = dev->dev_private;
  3521. struct drm_crtc *crtc;
  3522. const struct cxsr_latency *latency;
  3523. u32 reg;
  3524. unsigned long wm;
  3525. latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
  3526. dev_priv->fsb_freq, dev_priv->mem_freq);
  3527. if (!latency) {
  3528. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  3529. pineview_disable_cxsr(dev);
  3530. return;
  3531. }
  3532. crtc = single_enabled_crtc(dev);
  3533. if (crtc) {
  3534. int clock = crtc->mode.clock;
  3535. int pixel_size = crtc->fb->bits_per_pixel / 8;
  3536. /* Display SR */
  3537. wm = intel_calculate_wm(clock, &pineview_display_wm,
  3538. pineview_display_wm.fifo_size,
  3539. pixel_size, latency->display_sr);
  3540. reg = I915_READ(DSPFW1);
  3541. reg &= ~DSPFW_SR_MASK;
  3542. reg |= wm << DSPFW_SR_SHIFT;
  3543. I915_WRITE(DSPFW1, reg);
  3544. DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
  3545. /* cursor SR */
  3546. wm = intel_calculate_wm(clock, &pineview_cursor_wm,
  3547. pineview_display_wm.fifo_size,
  3548. pixel_size, latency->cursor_sr);
  3549. reg = I915_READ(DSPFW3);
  3550. reg &= ~DSPFW_CURSOR_SR_MASK;
  3551. reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
  3552. I915_WRITE(DSPFW3, reg);
  3553. /* Display HPLL off SR */
  3554. wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
  3555. pineview_display_hplloff_wm.fifo_size,
  3556. pixel_size, latency->display_hpll_disable);
  3557. reg = I915_READ(DSPFW3);
  3558. reg &= ~DSPFW_HPLL_SR_MASK;
  3559. reg |= wm & DSPFW_HPLL_SR_MASK;
  3560. I915_WRITE(DSPFW3, reg);
  3561. /* cursor HPLL off SR */
  3562. wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
  3563. pineview_display_hplloff_wm.fifo_size,
  3564. pixel_size, latency->cursor_hpll_disable);
  3565. reg = I915_READ(DSPFW3);
  3566. reg &= ~DSPFW_HPLL_CURSOR_MASK;
  3567. reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
  3568. I915_WRITE(DSPFW3, reg);
  3569. DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
  3570. /* activate cxsr */
  3571. I915_WRITE(DSPFW3,
  3572. I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
  3573. DRM_DEBUG_KMS("Self-refresh is enabled\n");
  3574. } else {
  3575. pineview_disable_cxsr(dev);
  3576. DRM_DEBUG_KMS("Self-refresh is disabled\n");
  3577. }
  3578. }
  3579. static bool g4x_compute_wm0(struct drm_device *dev,
  3580. int plane,
  3581. const struct intel_watermark_params *display,
  3582. int display_latency_ns,
  3583. const struct intel_watermark_params *cursor,
  3584. int cursor_latency_ns,
  3585. int *plane_wm,
  3586. int *cursor_wm)
  3587. {
  3588. struct drm_crtc *crtc;
  3589. int htotal, hdisplay, clock, pixel_size;
  3590. int line_time_us, line_count;
  3591. int entries, tlb_miss;
  3592. crtc = intel_get_crtc_for_plane(dev, plane);
  3593. if (crtc->fb == NULL || !crtc->enabled) {
  3594. *cursor_wm = cursor->guard_size;
  3595. *plane_wm = display->guard_size;
  3596. return false;
  3597. }
  3598. htotal = crtc->mode.htotal;
  3599. hdisplay = crtc->mode.hdisplay;
  3600. clock = crtc->mode.clock;
  3601. pixel_size = crtc->fb->bits_per_pixel / 8;
  3602. /* Use the small buffer method to calculate plane watermark */
  3603. entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
  3604. tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
  3605. if (tlb_miss > 0)
  3606. entries += tlb_miss;
  3607. entries = DIV_ROUND_UP(entries, display->cacheline_size);
  3608. *plane_wm = entries + display->guard_size;
  3609. if (*plane_wm > (int)display->max_wm)
  3610. *plane_wm = display->max_wm;
  3611. /* Use the large buffer method to calculate cursor watermark */
  3612. line_time_us = ((htotal * 1000) / clock);
  3613. line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
  3614. entries = line_count * 64 * pixel_size;
  3615. tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
  3616. if (tlb_miss > 0)
  3617. entries += tlb_miss;
  3618. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  3619. *cursor_wm = entries + cursor->guard_size;
  3620. if (*cursor_wm > (int)cursor->max_wm)
  3621. *cursor_wm = (int)cursor->max_wm;
  3622. return true;
  3623. }
  3624. /*
  3625. * Check the wm result.
  3626. *
  3627. * If any calculated watermark values is larger than the maximum value that
  3628. * can be programmed into the associated watermark register, that watermark
  3629. * must be disabled.
  3630. */
  3631. static bool g4x_check_srwm(struct drm_device *dev,
  3632. int display_wm, int cursor_wm,
  3633. const struct intel_watermark_params *display,
  3634. const struct intel_watermark_params *cursor)
  3635. {
  3636. DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
  3637. display_wm, cursor_wm);
  3638. if (display_wm > display->max_wm) {
  3639. DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
  3640. display_wm, display->max_wm);
  3641. return false;
  3642. }
  3643. if (cursor_wm > cursor->max_wm) {
  3644. DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
  3645. cursor_wm, cursor->max_wm);
  3646. return false;
  3647. }
  3648. if (!(display_wm || cursor_wm)) {
  3649. DRM_DEBUG_KMS("SR latency is 0, disabling\n");
  3650. return false;
  3651. }
  3652. return true;
  3653. }
  3654. static bool g4x_compute_srwm(struct drm_device *dev,
  3655. int plane,
  3656. int latency_ns,
  3657. const struct intel_watermark_params *display,
  3658. const struct intel_watermark_params *cursor,
  3659. int *display_wm, int *cursor_wm)
  3660. {
  3661. struct drm_crtc *crtc;
  3662. int hdisplay, htotal, pixel_size, clock;
  3663. unsigned long line_time_us;
  3664. int line_count, line_size;
  3665. int small, large;
  3666. int entries;
  3667. if (!latency_ns) {
  3668. *display_wm = *cursor_wm = 0;
  3669. return false;
  3670. }
  3671. crtc = intel_get_crtc_for_plane(dev, plane);
  3672. hdisplay = crtc->mode.hdisplay;
  3673. htotal = crtc->mode.htotal;
  3674. clock = crtc->mode.clock;
  3675. pixel_size = crtc->fb->bits_per_pixel / 8;
  3676. line_time_us = (htotal * 1000) / clock;
  3677. line_count = (latency_ns / line_time_us + 1000) / 1000;
  3678. line_size = hdisplay * pixel_size;
  3679. /* Use the minimum of the small and large buffer method for primary */
  3680. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  3681. large = line_count * line_size;
  3682. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  3683. *display_wm = entries + display->guard_size;
  3684. /* calculate the self-refresh watermark for display cursor */
  3685. entries = line_count * pixel_size * 64;
  3686. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  3687. *cursor_wm = entries + cursor->guard_size;
  3688. return g4x_check_srwm(dev,
  3689. *display_wm, *cursor_wm,
  3690. display, cursor);
  3691. }
  3692. static bool vlv_compute_drain_latency(struct drm_device *dev,
  3693. int plane,
  3694. int *plane_prec_mult,
  3695. int *plane_dl,
  3696. int *cursor_prec_mult,
  3697. int *cursor_dl)
  3698. {
  3699. struct drm_crtc *crtc;
  3700. int clock, pixel_size;
  3701. int entries;
  3702. crtc = intel_get_crtc_for_plane(dev, plane);
  3703. if (crtc->fb == NULL || !crtc->enabled)
  3704. return false;
  3705. clock = crtc->mode.clock; /* VESA DOT Clock */
  3706. pixel_size = crtc->fb->bits_per_pixel / 8; /* BPP */
  3707. entries = (clock / 1000) * pixel_size;
  3708. *plane_prec_mult = (entries > 256) ?
  3709. DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
  3710. *plane_dl = (64 * (*plane_prec_mult) * 4) / ((clock / 1000) *
  3711. pixel_size);
  3712. entries = (clock / 1000) * 4; /* BPP is always 4 for cursor */
  3713. *cursor_prec_mult = (entries > 256) ?
  3714. DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_16;
  3715. *cursor_dl = (64 * (*cursor_prec_mult) * 4) / ((clock / 1000) * 4);
  3716. return true;
  3717. }
  3718. /*
  3719. * Update drain latency registers of memory arbiter
  3720. *
  3721. * Valleyview SoC has a new memory arbiter and needs drain latency registers
  3722. * to be programmed. Each plane has a drain latency multiplier and a drain
  3723. * latency value.
  3724. */
  3725. static void vlv_update_drain_latency(struct drm_device *dev)
  3726. {
  3727. struct drm_i915_private *dev_priv = dev->dev_private;
  3728. int planea_prec, planea_dl, planeb_prec, planeb_dl;
  3729. int cursora_prec, cursora_dl, cursorb_prec, cursorb_dl;
  3730. int plane_prec_mult, cursor_prec_mult; /* Precision multiplier is
  3731. either 16 or 32 */
  3732. /* For plane A, Cursor A */
  3733. if (vlv_compute_drain_latency(dev, 0, &plane_prec_mult, &planea_dl,
  3734. &cursor_prec_mult, &cursora_dl)) {
  3735. cursora_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
  3736. DDL_CURSORA_PRECISION_32 : DDL_CURSORA_PRECISION_16;
  3737. planea_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
  3738. DDL_PLANEA_PRECISION_32 : DDL_PLANEA_PRECISION_16;
  3739. I915_WRITE(VLV_DDL1, cursora_prec |
  3740. (cursora_dl << DDL_CURSORA_SHIFT) |
  3741. planea_prec | planea_dl);
  3742. }
  3743. /* For plane B, Cursor B */
  3744. if (vlv_compute_drain_latency(dev, 1, &plane_prec_mult, &planeb_dl,
  3745. &cursor_prec_mult, &cursorb_dl)) {
  3746. cursorb_prec = (cursor_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
  3747. DDL_CURSORB_PRECISION_32 : DDL_CURSORB_PRECISION_16;
  3748. planeb_prec = (plane_prec_mult == DRAIN_LATENCY_PRECISION_32) ?
  3749. DDL_PLANEB_PRECISION_32 : DDL_PLANEB_PRECISION_16;
  3750. I915_WRITE(VLV_DDL2, cursorb_prec |
  3751. (cursorb_dl << DDL_CURSORB_SHIFT) |
  3752. planeb_prec | planeb_dl);
  3753. }
  3754. }
  3755. #define single_plane_enabled(mask) is_power_of_2(mask)
  3756. static void valleyview_update_wm(struct drm_device *dev)
  3757. {
  3758. static const int sr_latency_ns = 12000;
  3759. struct drm_i915_private *dev_priv = dev->dev_private;
  3760. int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
  3761. int plane_sr, cursor_sr;
  3762. unsigned int enabled = 0;
  3763. vlv_update_drain_latency(dev);
  3764. if (g4x_compute_wm0(dev, 0,
  3765. &valleyview_wm_info, latency_ns,
  3766. &valleyview_cursor_wm_info, latency_ns,
  3767. &planea_wm, &cursora_wm))
  3768. enabled |= 1;
  3769. if (g4x_compute_wm0(dev, 1,
  3770. &valleyview_wm_info, latency_ns,
  3771. &valleyview_cursor_wm_info, latency_ns,
  3772. &planeb_wm, &cursorb_wm))
  3773. enabled |= 2;
  3774. plane_sr = cursor_sr = 0;
  3775. if (single_plane_enabled(enabled) &&
  3776. g4x_compute_srwm(dev, ffs(enabled) - 1,
  3777. sr_latency_ns,
  3778. &valleyview_wm_info,
  3779. &valleyview_cursor_wm_info,
  3780. &plane_sr, &cursor_sr))
  3781. I915_WRITE(FW_BLC_SELF_VLV, FW_CSPWRDWNEN);
  3782. else
  3783. I915_WRITE(FW_BLC_SELF_VLV,
  3784. I915_READ(FW_BLC_SELF_VLV) & ~FW_CSPWRDWNEN);
  3785. DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
  3786. planea_wm, cursora_wm,
  3787. planeb_wm, cursorb_wm,
  3788. plane_sr, cursor_sr);
  3789. I915_WRITE(DSPFW1,
  3790. (plane_sr << DSPFW_SR_SHIFT) |
  3791. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  3792. (planeb_wm << DSPFW_PLANEB_SHIFT) |
  3793. planea_wm);
  3794. I915_WRITE(DSPFW2,
  3795. (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
  3796. (cursora_wm << DSPFW_CURSORA_SHIFT));
  3797. I915_WRITE(DSPFW3,
  3798. (I915_READ(DSPFW3) | (cursor_sr << DSPFW_CURSOR_SR_SHIFT)));
  3799. }
  3800. static void g4x_update_wm(struct drm_device *dev)
  3801. {
  3802. static const int sr_latency_ns = 12000;
  3803. struct drm_i915_private *dev_priv = dev->dev_private;
  3804. int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
  3805. int plane_sr, cursor_sr;
  3806. unsigned int enabled = 0;
  3807. if (g4x_compute_wm0(dev, 0,
  3808. &g4x_wm_info, latency_ns,
  3809. &g4x_cursor_wm_info, latency_ns,
  3810. &planea_wm, &cursora_wm))
  3811. enabled |= 1;
  3812. if (g4x_compute_wm0(dev, 1,
  3813. &g4x_wm_info, latency_ns,
  3814. &g4x_cursor_wm_info, latency_ns,
  3815. &planeb_wm, &cursorb_wm))
  3816. enabled |= 2;
  3817. plane_sr = cursor_sr = 0;
  3818. if (single_plane_enabled(enabled) &&
  3819. g4x_compute_srwm(dev, ffs(enabled) - 1,
  3820. sr_latency_ns,
  3821. &g4x_wm_info,
  3822. &g4x_cursor_wm_info,
  3823. &plane_sr, &cursor_sr))
  3824. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  3825. else
  3826. I915_WRITE(FW_BLC_SELF,
  3827. I915_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN);
  3828. DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
  3829. planea_wm, cursora_wm,
  3830. planeb_wm, cursorb_wm,
  3831. plane_sr, cursor_sr);
  3832. I915_WRITE(DSPFW1,
  3833. (plane_sr << DSPFW_SR_SHIFT) |
  3834. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  3835. (planeb_wm << DSPFW_PLANEB_SHIFT) |
  3836. planea_wm);
  3837. I915_WRITE(DSPFW2,
  3838. (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
  3839. (cursora_wm << DSPFW_CURSORA_SHIFT));
  3840. /* HPLL off in SR has some issues on G4x... disable it */
  3841. I915_WRITE(DSPFW3,
  3842. (I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |
  3843. (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  3844. }
  3845. static void i965_update_wm(struct drm_device *dev)
  3846. {
  3847. struct drm_i915_private *dev_priv = dev->dev_private;
  3848. struct drm_crtc *crtc;
  3849. int srwm = 1;
  3850. int cursor_sr = 16;
  3851. /* Calc sr entries for one plane configs */
  3852. crtc = single_enabled_crtc(dev);
  3853. if (crtc) {
  3854. /* self-refresh has much higher latency */
  3855. static const int sr_latency_ns = 12000;
  3856. int clock = crtc->mode.clock;
  3857. int htotal = crtc->mode.htotal;
  3858. int hdisplay = crtc->mode.hdisplay;
  3859. int pixel_size = crtc->fb->bits_per_pixel / 8;
  3860. unsigned long line_time_us;
  3861. int entries;
  3862. line_time_us = ((htotal * 1000) / clock);
  3863. /* Use ns/us then divide to preserve precision */
  3864. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  3865. pixel_size * hdisplay;
  3866. entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
  3867. srwm = I965_FIFO_SIZE - entries;
  3868. if (srwm < 0)
  3869. srwm = 1;
  3870. srwm &= 0x1ff;
  3871. DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
  3872. entries, srwm);
  3873. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  3874. pixel_size * 64;
  3875. entries = DIV_ROUND_UP(entries,
  3876. i965_cursor_wm_info.cacheline_size);
  3877. cursor_sr = i965_cursor_wm_info.fifo_size -
  3878. (entries + i965_cursor_wm_info.guard_size);
  3879. if (cursor_sr > i965_cursor_wm_info.max_wm)
  3880. cursor_sr = i965_cursor_wm_info.max_wm;
  3881. DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
  3882. "cursor %d\n", srwm, cursor_sr);
  3883. if (IS_CRESTLINE(dev))
  3884. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  3885. } else {
  3886. /* Turn off self refresh if both pipes are enabled */
  3887. if (IS_CRESTLINE(dev))
  3888. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  3889. & ~FW_BLC_SELF_EN);
  3890. }
  3891. DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
  3892. srwm);
  3893. /* 965 has limitations... */
  3894. I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
  3895. (8 << 16) | (8 << 8) | (8 << 0));
  3896. I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
  3897. /* update cursor SR watermark */
  3898. I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  3899. }
  3900. static void i9xx_update_wm(struct drm_device *dev)
  3901. {
  3902. struct drm_i915_private *dev_priv = dev->dev_private;
  3903. const struct intel_watermark_params *wm_info;
  3904. uint32_t fwater_lo;
  3905. uint32_t fwater_hi;
  3906. int cwm, srwm = 1;
  3907. int fifo_size;
  3908. int planea_wm, planeb_wm;
  3909. struct drm_crtc *crtc, *enabled = NULL;
  3910. if (IS_I945GM(dev))
  3911. wm_info = &i945_wm_info;
  3912. else if (!IS_GEN2(dev))
  3913. wm_info = &i915_wm_info;
  3914. else
  3915. wm_info = &i855_wm_info;
  3916. fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  3917. crtc = intel_get_crtc_for_plane(dev, 0);
  3918. if (crtc->enabled && crtc->fb) {
  3919. planea_wm = intel_calculate_wm(crtc->mode.clock,
  3920. wm_info, fifo_size,
  3921. crtc->fb->bits_per_pixel / 8,
  3922. latency_ns);
  3923. enabled = crtc;
  3924. } else
  3925. planea_wm = fifo_size - wm_info->guard_size;
  3926. fifo_size = dev_priv->display.get_fifo_size(dev, 1);
  3927. crtc = intel_get_crtc_for_plane(dev, 1);
  3928. if (crtc->enabled && crtc->fb) {
  3929. planeb_wm = intel_calculate_wm(crtc->mode.clock,
  3930. wm_info, fifo_size,
  3931. crtc->fb->bits_per_pixel / 8,
  3932. latency_ns);
  3933. if (enabled == NULL)
  3934. enabled = crtc;
  3935. else
  3936. enabled = NULL;
  3937. } else
  3938. planeb_wm = fifo_size - wm_info->guard_size;
  3939. DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  3940. /*
  3941. * Overlay gets an aggressive default since video jitter is bad.
  3942. */
  3943. cwm = 2;
  3944. /* Play safe and disable self-refresh before adjusting watermarks. */
  3945. if (IS_I945G(dev) || IS_I945GM(dev))
  3946. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | 0);
  3947. else if (IS_I915GM(dev))
  3948. I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
  3949. /* Calc sr entries for one plane configs */
  3950. if (HAS_FW_BLC(dev) && enabled) {
  3951. /* self-refresh has much higher latency */
  3952. static const int sr_latency_ns = 6000;
  3953. int clock = enabled->mode.clock;
  3954. int htotal = enabled->mode.htotal;
  3955. int hdisplay = enabled->mode.hdisplay;
  3956. int pixel_size = enabled->fb->bits_per_pixel / 8;
  3957. unsigned long line_time_us;
  3958. int entries;
  3959. line_time_us = (htotal * 1000) / clock;
  3960. /* Use ns/us then divide to preserve precision */
  3961. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  3962. pixel_size * hdisplay;
  3963. entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
  3964. DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
  3965. srwm = wm_info->fifo_size - entries;
  3966. if (srwm < 0)
  3967. srwm = 1;
  3968. if (IS_I945G(dev) || IS_I945GM(dev))
  3969. I915_WRITE(FW_BLC_SELF,
  3970. FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
  3971. else if (IS_I915GM(dev))
  3972. I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
  3973. }
  3974. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
  3975. planea_wm, planeb_wm, cwm, srwm);
  3976. fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
  3977. fwater_hi = (cwm & 0x1f);
  3978. /* Set request length to 8 cachelines per fetch */
  3979. fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
  3980. fwater_hi = fwater_hi | (1 << 8);
  3981. I915_WRITE(FW_BLC, fwater_lo);
  3982. I915_WRITE(FW_BLC2, fwater_hi);
  3983. if (HAS_FW_BLC(dev)) {
  3984. if (enabled) {
  3985. if (IS_I945G(dev) || IS_I945GM(dev))
  3986. I915_WRITE(FW_BLC_SELF,
  3987. FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
  3988. else if (IS_I915GM(dev))
  3989. I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
  3990. DRM_DEBUG_KMS("memory self refresh enabled\n");
  3991. } else
  3992. DRM_DEBUG_KMS("memory self refresh disabled\n");
  3993. }
  3994. }
  3995. static void i830_update_wm(struct drm_device *dev)
  3996. {
  3997. struct drm_i915_private *dev_priv = dev->dev_private;
  3998. struct drm_crtc *crtc;
  3999. uint32_t fwater_lo;
  4000. int planea_wm;
  4001. crtc = single_enabled_crtc(dev);
  4002. if (crtc == NULL)
  4003. return;
  4004. planea_wm = intel_calculate_wm(crtc->mode.clock, &i830_wm_info,
  4005. dev_priv->display.get_fifo_size(dev, 0),
  4006. crtc->fb->bits_per_pixel / 8,
  4007. latency_ns);
  4008. fwater_lo = I915_READ(FW_BLC) & ~0xfff;
  4009. fwater_lo |= (3<<8) | planea_wm;
  4010. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
  4011. I915_WRITE(FW_BLC, fwater_lo);
  4012. }
  4013. #define ILK_LP0_PLANE_LATENCY 700
  4014. #define ILK_LP0_CURSOR_LATENCY 1300
  4015. /*
  4016. * Check the wm result.
  4017. *
  4018. * If any calculated watermark values is larger than the maximum value that
  4019. * can be programmed into the associated watermark register, that watermark
  4020. * must be disabled.
  4021. */
  4022. static bool ironlake_check_srwm(struct drm_device *dev, int level,
  4023. int fbc_wm, int display_wm, int cursor_wm,
  4024. const struct intel_watermark_params *display,
  4025. const struct intel_watermark_params *cursor)
  4026. {
  4027. struct drm_i915_private *dev_priv = dev->dev_private;
  4028. DRM_DEBUG_KMS("watermark %d: display plane %d, fbc lines %d,"
  4029. " cursor %d\n", level, display_wm, fbc_wm, cursor_wm);
  4030. if (fbc_wm > SNB_FBC_MAX_SRWM) {
  4031. DRM_DEBUG_KMS("fbc watermark(%d) is too large(%d), disabling wm%d+\n",
  4032. fbc_wm, SNB_FBC_MAX_SRWM, level);
  4033. /* fbc has it's own way to disable FBC WM */
  4034. I915_WRITE(DISP_ARB_CTL,
  4035. I915_READ(DISP_ARB_CTL) | DISP_FBC_WM_DIS);
  4036. return false;
  4037. }
  4038. if (display_wm > display->max_wm) {
  4039. DRM_DEBUG_KMS("display watermark(%d) is too large(%d), disabling wm%d+\n",
  4040. display_wm, SNB_DISPLAY_MAX_SRWM, level);
  4041. return false;
  4042. }
  4043. if (cursor_wm > cursor->max_wm) {
  4044. DRM_DEBUG_KMS("cursor watermark(%d) is too large(%d), disabling wm%d+\n",
  4045. cursor_wm, SNB_CURSOR_MAX_SRWM, level);
  4046. return false;
  4047. }
  4048. if (!(fbc_wm || display_wm || cursor_wm)) {
  4049. DRM_DEBUG_KMS("latency %d is 0, disabling wm%d+\n", level, level);
  4050. return false;
  4051. }
  4052. return true;
  4053. }
  4054. /*
  4055. * Compute watermark values of WM[1-3],
  4056. */
  4057. static bool ironlake_compute_srwm(struct drm_device *dev, int level, int plane,
  4058. int latency_ns,
  4059. const struct intel_watermark_params *display,
  4060. const struct intel_watermark_params *cursor,
  4061. int *fbc_wm, int *display_wm, int *cursor_wm)
  4062. {
  4063. struct drm_crtc *crtc;
  4064. unsigned long line_time_us;
  4065. int hdisplay, htotal, pixel_size, clock;
  4066. int line_count, line_size;
  4067. int small, large;
  4068. int entries;
  4069. if (!latency_ns) {
  4070. *fbc_wm = *display_wm = *cursor_wm = 0;
  4071. return false;
  4072. }
  4073. crtc = intel_get_crtc_for_plane(dev, plane);
  4074. hdisplay = crtc->mode.hdisplay;
  4075. htotal = crtc->mode.htotal;
  4076. clock = crtc->mode.clock;
  4077. pixel_size = crtc->fb->bits_per_pixel / 8;
  4078. line_time_us = (htotal * 1000) / clock;
  4079. line_count = (latency_ns / line_time_us + 1000) / 1000;
  4080. line_size = hdisplay * pixel_size;
  4081. /* Use the minimum of the small and large buffer method for primary */
  4082. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  4083. large = line_count * line_size;
  4084. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  4085. *display_wm = entries + display->guard_size;
  4086. /*
  4087. * Spec says:
  4088. * FBC WM = ((Final Primary WM * 64) / number of bytes per line) + 2
  4089. */
  4090. *fbc_wm = DIV_ROUND_UP(*display_wm * 64, line_size) + 2;
  4091. /* calculate the self-refresh watermark for display cursor */
  4092. entries = line_count * pixel_size * 64;
  4093. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  4094. *cursor_wm = entries + cursor->guard_size;
  4095. return ironlake_check_srwm(dev, level,
  4096. *fbc_wm, *display_wm, *cursor_wm,
  4097. display, cursor);
  4098. }
  4099. static void ironlake_update_wm(struct drm_device *dev)
  4100. {
  4101. struct drm_i915_private *dev_priv = dev->dev_private;
  4102. int fbc_wm, plane_wm, cursor_wm;
  4103. unsigned int enabled;
  4104. enabled = 0;
  4105. if (g4x_compute_wm0(dev, 0,
  4106. &ironlake_display_wm_info,
  4107. ILK_LP0_PLANE_LATENCY,
  4108. &ironlake_cursor_wm_info,
  4109. ILK_LP0_CURSOR_LATENCY,
  4110. &plane_wm, &cursor_wm)) {
  4111. I915_WRITE(WM0_PIPEA_ILK,
  4112. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  4113. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  4114. " plane %d, " "cursor: %d\n",
  4115. plane_wm, cursor_wm);
  4116. enabled |= 1;
  4117. }
  4118. if (g4x_compute_wm0(dev, 1,
  4119. &ironlake_display_wm_info,
  4120. ILK_LP0_PLANE_LATENCY,
  4121. &ironlake_cursor_wm_info,
  4122. ILK_LP0_CURSOR_LATENCY,
  4123. &plane_wm, &cursor_wm)) {
  4124. I915_WRITE(WM0_PIPEB_ILK,
  4125. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  4126. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  4127. " plane %d, cursor: %d\n",
  4128. plane_wm, cursor_wm);
  4129. enabled |= 2;
  4130. }
  4131. /*
  4132. * Calculate and update the self-refresh watermark only when one
  4133. * display plane is used.
  4134. */
  4135. I915_WRITE(WM3_LP_ILK, 0);
  4136. I915_WRITE(WM2_LP_ILK, 0);
  4137. I915_WRITE(WM1_LP_ILK, 0);
  4138. if (!single_plane_enabled(enabled))
  4139. return;
  4140. enabled = ffs(enabled) - 1;
  4141. /* WM1 */
  4142. if (!ironlake_compute_srwm(dev, 1, enabled,
  4143. ILK_READ_WM1_LATENCY() * 500,
  4144. &ironlake_display_srwm_info,
  4145. &ironlake_cursor_srwm_info,
  4146. &fbc_wm, &plane_wm, &cursor_wm))
  4147. return;
  4148. I915_WRITE(WM1_LP_ILK,
  4149. WM1_LP_SR_EN |
  4150. (ILK_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  4151. (fbc_wm << WM1_LP_FBC_SHIFT) |
  4152. (plane_wm << WM1_LP_SR_SHIFT) |
  4153. cursor_wm);
  4154. /* WM2 */
  4155. if (!ironlake_compute_srwm(dev, 2, enabled,
  4156. ILK_READ_WM2_LATENCY() * 500,
  4157. &ironlake_display_srwm_info,
  4158. &ironlake_cursor_srwm_info,
  4159. &fbc_wm, &plane_wm, &cursor_wm))
  4160. return;
  4161. I915_WRITE(WM2_LP_ILK,
  4162. WM2_LP_EN |
  4163. (ILK_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  4164. (fbc_wm << WM1_LP_FBC_SHIFT) |
  4165. (plane_wm << WM1_LP_SR_SHIFT) |
  4166. cursor_wm);
  4167. /*
  4168. * WM3 is unsupported on ILK, probably because we don't have latency
  4169. * data for that power state
  4170. */
  4171. }
  4172. static void sandybridge_update_wm(struct drm_device *dev)
  4173. {
  4174. struct drm_i915_private *dev_priv = dev->dev_private;
  4175. int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
  4176. u32 val;
  4177. int fbc_wm, plane_wm, cursor_wm;
  4178. unsigned int enabled;
  4179. enabled = 0;
  4180. if (g4x_compute_wm0(dev, 0,
  4181. &sandybridge_display_wm_info, latency,
  4182. &sandybridge_cursor_wm_info, latency,
  4183. &plane_wm, &cursor_wm)) {
  4184. val = I915_READ(WM0_PIPEA_ILK);
  4185. val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
  4186. I915_WRITE(WM0_PIPEA_ILK, val |
  4187. ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
  4188. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  4189. " plane %d, " "cursor: %d\n",
  4190. plane_wm, cursor_wm);
  4191. enabled |= 1;
  4192. }
  4193. if (g4x_compute_wm0(dev, 1,
  4194. &sandybridge_display_wm_info, latency,
  4195. &sandybridge_cursor_wm_info, latency,
  4196. &plane_wm, &cursor_wm)) {
  4197. val = I915_READ(WM0_PIPEB_ILK);
  4198. val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
  4199. I915_WRITE(WM0_PIPEB_ILK, val |
  4200. ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
  4201. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  4202. " plane %d, cursor: %d\n",
  4203. plane_wm, cursor_wm);
  4204. enabled |= 2;
  4205. }
  4206. /* IVB has 3 pipes */
  4207. if (IS_IVYBRIDGE(dev) &&
  4208. g4x_compute_wm0(dev, 2,
  4209. &sandybridge_display_wm_info, latency,
  4210. &sandybridge_cursor_wm_info, latency,
  4211. &plane_wm, &cursor_wm)) {
  4212. val = I915_READ(WM0_PIPEC_IVB);
  4213. val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
  4214. I915_WRITE(WM0_PIPEC_IVB, val |
  4215. ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
  4216. DRM_DEBUG_KMS("FIFO watermarks For pipe C -"
  4217. " plane %d, cursor: %d\n",
  4218. plane_wm, cursor_wm);
  4219. enabled |= 3;
  4220. }
  4221. /*
  4222. * Calculate and update the self-refresh watermark only when one
  4223. * display plane is used.
  4224. *
  4225. * SNB support 3 levels of watermark.
  4226. *
  4227. * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
  4228. * and disabled in the descending order
  4229. *
  4230. */
  4231. I915_WRITE(WM3_LP_ILK, 0);
  4232. I915_WRITE(WM2_LP_ILK, 0);
  4233. I915_WRITE(WM1_LP_ILK, 0);
  4234. if (!single_plane_enabled(enabled) ||
  4235. dev_priv->sprite_scaling_enabled)
  4236. return;
  4237. enabled = ffs(enabled) - 1;
  4238. /* WM1 */
  4239. if (!ironlake_compute_srwm(dev, 1, enabled,
  4240. SNB_READ_WM1_LATENCY() * 500,
  4241. &sandybridge_display_srwm_info,
  4242. &sandybridge_cursor_srwm_info,
  4243. &fbc_wm, &plane_wm, &cursor_wm))
  4244. return;
  4245. I915_WRITE(WM1_LP_ILK,
  4246. WM1_LP_SR_EN |
  4247. (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  4248. (fbc_wm << WM1_LP_FBC_SHIFT) |
  4249. (plane_wm << WM1_LP_SR_SHIFT) |
  4250. cursor_wm);
  4251. /* WM2 */
  4252. if (!ironlake_compute_srwm(dev, 2, enabled,
  4253. SNB_READ_WM2_LATENCY() * 500,
  4254. &sandybridge_display_srwm_info,
  4255. &sandybridge_cursor_srwm_info,
  4256. &fbc_wm, &plane_wm, &cursor_wm))
  4257. return;
  4258. I915_WRITE(WM2_LP_ILK,
  4259. WM2_LP_EN |
  4260. (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  4261. (fbc_wm << WM1_LP_FBC_SHIFT) |
  4262. (plane_wm << WM1_LP_SR_SHIFT) |
  4263. cursor_wm);
  4264. /* WM3 */
  4265. if (!ironlake_compute_srwm(dev, 3, enabled,
  4266. SNB_READ_WM3_LATENCY() * 500,
  4267. &sandybridge_display_srwm_info,
  4268. &sandybridge_cursor_srwm_info,
  4269. &fbc_wm, &plane_wm, &cursor_wm))
  4270. return;
  4271. I915_WRITE(WM3_LP_ILK,
  4272. WM3_LP_EN |
  4273. (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  4274. (fbc_wm << WM1_LP_FBC_SHIFT) |
  4275. (plane_wm << WM1_LP_SR_SHIFT) |
  4276. cursor_wm);
  4277. }
  4278. static bool
  4279. sandybridge_compute_sprite_wm(struct drm_device *dev, int plane,
  4280. uint32_t sprite_width, int pixel_size,
  4281. const struct intel_watermark_params *display,
  4282. int display_latency_ns, int *sprite_wm)
  4283. {
  4284. struct drm_crtc *crtc;
  4285. int clock;
  4286. int entries, tlb_miss;
  4287. crtc = intel_get_crtc_for_plane(dev, plane);
  4288. if (crtc->fb == NULL || !crtc->enabled) {
  4289. *sprite_wm = display->guard_size;
  4290. return false;
  4291. }
  4292. clock = crtc->mode.clock;
  4293. /* Use the small buffer method to calculate the sprite watermark */
  4294. entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
  4295. tlb_miss = display->fifo_size*display->cacheline_size -
  4296. sprite_width * 8;
  4297. if (tlb_miss > 0)
  4298. entries += tlb_miss;
  4299. entries = DIV_ROUND_UP(entries, display->cacheline_size);
  4300. *sprite_wm = entries + display->guard_size;
  4301. if (*sprite_wm > (int)display->max_wm)
  4302. *sprite_wm = display->max_wm;
  4303. return true;
  4304. }
  4305. static bool
  4306. sandybridge_compute_sprite_srwm(struct drm_device *dev, int plane,
  4307. uint32_t sprite_width, int pixel_size,
  4308. const struct intel_watermark_params *display,
  4309. int latency_ns, int *sprite_wm)
  4310. {
  4311. struct drm_crtc *crtc;
  4312. unsigned long line_time_us;
  4313. int clock;
  4314. int line_count, line_size;
  4315. int small, large;
  4316. int entries;
  4317. if (!latency_ns) {
  4318. *sprite_wm = 0;
  4319. return false;
  4320. }
  4321. crtc = intel_get_crtc_for_plane(dev, plane);
  4322. clock = crtc->mode.clock;
  4323. if (!clock) {
  4324. *sprite_wm = 0;
  4325. return false;
  4326. }
  4327. line_time_us = (sprite_width * 1000) / clock;
  4328. if (!line_time_us) {
  4329. *sprite_wm = 0;
  4330. return false;
  4331. }
  4332. line_count = (latency_ns / line_time_us + 1000) / 1000;
  4333. line_size = sprite_width * pixel_size;
  4334. /* Use the minimum of the small and large buffer method for primary */
  4335. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  4336. large = line_count * line_size;
  4337. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  4338. *sprite_wm = entries + display->guard_size;
  4339. return *sprite_wm > 0x3ff ? false : true;
  4340. }
  4341. static void sandybridge_update_sprite_wm(struct drm_device *dev, int pipe,
  4342. uint32_t sprite_width, int pixel_size)
  4343. {
  4344. struct drm_i915_private *dev_priv = dev->dev_private;
  4345. int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
  4346. u32 val;
  4347. int sprite_wm, reg;
  4348. int ret;
  4349. switch (pipe) {
  4350. case 0:
  4351. reg = WM0_PIPEA_ILK;
  4352. break;
  4353. case 1:
  4354. reg = WM0_PIPEB_ILK;
  4355. break;
  4356. case 2:
  4357. reg = WM0_PIPEC_IVB;
  4358. break;
  4359. default:
  4360. return; /* bad pipe */
  4361. }
  4362. ret = sandybridge_compute_sprite_wm(dev, pipe, sprite_width, pixel_size,
  4363. &sandybridge_display_wm_info,
  4364. latency, &sprite_wm);
  4365. if (!ret) {
  4366. DRM_DEBUG_KMS("failed to compute sprite wm for pipe %d\n",
  4367. pipe);
  4368. return;
  4369. }
  4370. val = I915_READ(reg);
  4371. val &= ~WM0_PIPE_SPRITE_MASK;
  4372. I915_WRITE(reg, val | (sprite_wm << WM0_PIPE_SPRITE_SHIFT));
  4373. DRM_DEBUG_KMS("sprite watermarks For pipe %d - %d\n", pipe, sprite_wm);
  4374. ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
  4375. pixel_size,
  4376. &sandybridge_display_srwm_info,
  4377. SNB_READ_WM1_LATENCY() * 500,
  4378. &sprite_wm);
  4379. if (!ret) {
  4380. DRM_DEBUG_KMS("failed to compute sprite lp1 wm on pipe %d\n",
  4381. pipe);
  4382. return;
  4383. }
  4384. I915_WRITE(WM1S_LP_ILK, sprite_wm);
  4385. /* Only IVB has two more LP watermarks for sprite */
  4386. if (!IS_IVYBRIDGE(dev))
  4387. return;
  4388. ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
  4389. pixel_size,
  4390. &sandybridge_display_srwm_info,
  4391. SNB_READ_WM2_LATENCY() * 500,
  4392. &sprite_wm);
  4393. if (!ret) {
  4394. DRM_DEBUG_KMS("failed to compute sprite lp2 wm on pipe %d\n",
  4395. pipe);
  4396. return;
  4397. }
  4398. I915_WRITE(WM2S_LP_IVB, sprite_wm);
  4399. ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
  4400. pixel_size,
  4401. &sandybridge_display_srwm_info,
  4402. SNB_READ_WM3_LATENCY() * 500,
  4403. &sprite_wm);
  4404. if (!ret) {
  4405. DRM_DEBUG_KMS("failed to compute sprite lp3 wm on pipe %d\n",
  4406. pipe);
  4407. return;
  4408. }
  4409. I915_WRITE(WM3S_LP_IVB, sprite_wm);
  4410. }
  4411. /**
  4412. * intel_update_watermarks - update FIFO watermark values based on current modes
  4413. *
  4414. * Calculate watermark values for the various WM regs based on current mode
  4415. * and plane configuration.
  4416. *
  4417. * There are several cases to deal with here:
  4418. * - normal (i.e. non-self-refresh)
  4419. * - self-refresh (SR) mode
  4420. * - lines are large relative to FIFO size (buffer can hold up to 2)
  4421. * - lines are small relative to FIFO size (buffer can hold more than 2
  4422. * lines), so need to account for TLB latency
  4423. *
  4424. * The normal calculation is:
  4425. * watermark = dotclock * bytes per pixel * latency
  4426. * where latency is platform & configuration dependent (we assume pessimal
  4427. * values here).
  4428. *
  4429. * The SR calculation is:
  4430. * watermark = (trunc(latency/line time)+1) * surface width *
  4431. * bytes per pixel
  4432. * where
  4433. * line time = htotal / dotclock
  4434. * surface width = hdisplay for normal plane and 64 for cursor
  4435. * and latency is assumed to be high, as above.
  4436. *
  4437. * The final value programmed to the register should always be rounded up,
  4438. * and include an extra 2 entries to account for clock crossings.
  4439. *
  4440. * We don't use the sprite, so we can ignore that. And on Crestline we have
  4441. * to set the non-SR watermarks to 8.
  4442. */
  4443. void intel_update_watermarks(struct drm_device *dev)
  4444. {
  4445. struct drm_i915_private *dev_priv = dev->dev_private;
  4446. if (dev_priv->display.update_wm)
  4447. dev_priv->display.update_wm(dev);
  4448. }
  4449. void intel_update_sprite_watermarks(struct drm_device *dev, int pipe,
  4450. uint32_t sprite_width, int pixel_size)
  4451. {
  4452. struct drm_i915_private *dev_priv = dev->dev_private;
  4453. if (dev_priv->display.update_sprite_wm)
  4454. dev_priv->display.update_sprite_wm(dev, pipe, sprite_width,
  4455. pixel_size);
  4456. }
  4457. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  4458. {
  4459. if (i915_panel_use_ssc >= 0)
  4460. return i915_panel_use_ssc != 0;
  4461. return dev_priv->lvds_use_ssc
  4462. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  4463. }
  4464. /**
  4465. * intel_choose_pipe_bpp_dither - figure out what color depth the pipe should send
  4466. * @crtc: CRTC structure
  4467. * @mode: requested mode
  4468. *
  4469. * A pipe may be connected to one or more outputs. Based on the depth of the
  4470. * attached framebuffer, choose a good color depth to use on the pipe.
  4471. *
  4472. * If possible, match the pipe depth to the fb depth. In some cases, this
  4473. * isn't ideal, because the connected output supports a lesser or restricted
  4474. * set of depths. Resolve that here:
  4475. * LVDS typically supports only 6bpc, so clamp down in that case
  4476. * HDMI supports only 8bpc or 12bpc, so clamp to 8bpc with dither for 10bpc
  4477. * Displays may support a restricted set as well, check EDID and clamp as
  4478. * appropriate.
  4479. * DP may want to dither down to 6bpc to fit larger modes
  4480. *
  4481. * RETURNS:
  4482. * Dithering requirement (i.e. false if display bpc and pipe bpc match,
  4483. * true if they don't match).
  4484. */
  4485. static bool intel_choose_pipe_bpp_dither(struct drm_crtc *crtc,
  4486. unsigned int *pipe_bpp,
  4487. struct drm_display_mode *mode)
  4488. {
  4489. struct drm_device *dev = crtc->dev;
  4490. struct drm_i915_private *dev_priv = dev->dev_private;
  4491. struct drm_encoder *encoder;
  4492. struct drm_connector *connector;
  4493. unsigned int display_bpc = UINT_MAX, bpc;
  4494. /* Walk the encoders & connectors on this crtc, get min bpc */
  4495. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  4496. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4497. if (encoder->crtc != crtc)
  4498. continue;
  4499. if (intel_encoder->type == INTEL_OUTPUT_LVDS) {
  4500. unsigned int lvds_bpc;
  4501. if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) ==
  4502. LVDS_A3_POWER_UP)
  4503. lvds_bpc = 8;
  4504. else
  4505. lvds_bpc = 6;
  4506. if (lvds_bpc < display_bpc) {
  4507. DRM_DEBUG_KMS("clamping display bpc (was %d) to LVDS (%d)\n", display_bpc, lvds_bpc);
  4508. display_bpc = lvds_bpc;
  4509. }
  4510. continue;
  4511. }
  4512. if (intel_encoder->type == INTEL_OUTPUT_EDP) {
  4513. /* Use VBT settings if we have an eDP panel */
  4514. unsigned int edp_bpc = dev_priv->edp.bpp / 3;
  4515. if (edp_bpc < display_bpc) {
  4516. DRM_DEBUG_KMS("clamping display bpc (was %d) to eDP (%d)\n", display_bpc, edp_bpc);
  4517. display_bpc = edp_bpc;
  4518. }
  4519. continue;
  4520. }
  4521. /* Not one of the known troublemakers, check the EDID */
  4522. list_for_each_entry(connector, &dev->mode_config.connector_list,
  4523. head) {
  4524. if (connector->encoder != encoder)
  4525. continue;
  4526. /* Don't use an invalid EDID bpc value */
  4527. if (connector->display_info.bpc &&
  4528. connector->display_info.bpc < display_bpc) {
  4529. DRM_DEBUG_KMS("clamping display bpc (was %d) to EDID reported max of %d\n", display_bpc, connector->display_info.bpc);
  4530. display_bpc = connector->display_info.bpc;
  4531. }
  4532. }
  4533. /*
  4534. * HDMI is either 12 or 8, so if the display lets 10bpc sneak
  4535. * through, clamp it down. (Note: >12bpc will be caught below.)
  4536. */
  4537. if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
  4538. if (display_bpc > 8 && display_bpc < 12) {
  4539. DRM_DEBUG_KMS("forcing bpc to 12 for HDMI\n");
  4540. display_bpc = 12;
  4541. } else {
  4542. DRM_DEBUG_KMS("forcing bpc to 8 for HDMI\n");
  4543. display_bpc = 8;
  4544. }
  4545. }
  4546. }
  4547. if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
  4548. DRM_DEBUG_KMS("Dithering DP to 6bpc\n");
  4549. display_bpc = 6;
  4550. }
  4551. /*
  4552. * We could just drive the pipe at the highest bpc all the time and
  4553. * enable dithering as needed, but that costs bandwidth. So choose
  4554. * the minimum value that expresses the full color range of the fb but
  4555. * also stays within the max display bpc discovered above.
  4556. */
  4557. switch (crtc->fb->depth) {
  4558. case 8:
  4559. bpc = 8; /* since we go through a colormap */
  4560. break;
  4561. case 15:
  4562. case 16:
  4563. bpc = 6; /* min is 18bpp */
  4564. break;
  4565. case 24:
  4566. bpc = 8;
  4567. break;
  4568. case 30:
  4569. bpc = 10;
  4570. break;
  4571. case 48:
  4572. bpc = 12;
  4573. break;
  4574. default:
  4575. DRM_DEBUG("unsupported depth, assuming 24 bits\n");
  4576. bpc = min((unsigned int)8, display_bpc);
  4577. break;
  4578. }
  4579. display_bpc = min(display_bpc, bpc);
  4580. DRM_DEBUG_KMS("setting pipe bpc to %d (max display bpc %d)\n",
  4581. bpc, display_bpc);
  4582. *pipe_bpp = display_bpc * 3;
  4583. return display_bpc != bpc;
  4584. }
  4585. static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
  4586. {
  4587. struct drm_device *dev = crtc->dev;
  4588. struct drm_i915_private *dev_priv = dev->dev_private;
  4589. int refclk;
  4590. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  4591. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4592. refclk = dev_priv->lvds_ssc_freq * 1000;
  4593. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  4594. refclk / 1000);
  4595. } else if (!IS_GEN2(dev)) {
  4596. refclk = 96000;
  4597. } else {
  4598. refclk = 48000;
  4599. }
  4600. return refclk;
  4601. }
  4602. static void i9xx_adjust_sdvo_tv_clock(struct drm_display_mode *adjusted_mode,
  4603. intel_clock_t *clock)
  4604. {
  4605. /* SDVO TV has fixed PLL values depend on its clock range,
  4606. this mirrors vbios setting. */
  4607. if (adjusted_mode->clock >= 100000
  4608. && adjusted_mode->clock < 140500) {
  4609. clock->p1 = 2;
  4610. clock->p2 = 10;
  4611. clock->n = 3;
  4612. clock->m1 = 16;
  4613. clock->m2 = 8;
  4614. } else if (adjusted_mode->clock >= 140500
  4615. && adjusted_mode->clock <= 200000) {
  4616. clock->p1 = 1;
  4617. clock->p2 = 10;
  4618. clock->n = 6;
  4619. clock->m1 = 12;
  4620. clock->m2 = 8;
  4621. }
  4622. }
  4623. static void i9xx_update_pll_dividers(struct drm_crtc *crtc,
  4624. intel_clock_t *clock,
  4625. intel_clock_t *reduced_clock)
  4626. {
  4627. struct drm_device *dev = crtc->dev;
  4628. struct drm_i915_private *dev_priv = dev->dev_private;
  4629. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4630. int pipe = intel_crtc->pipe;
  4631. u32 fp, fp2 = 0;
  4632. if (IS_PINEVIEW(dev)) {
  4633. fp = (1 << clock->n) << 16 | clock->m1 << 8 | clock->m2;
  4634. if (reduced_clock)
  4635. fp2 = (1 << reduced_clock->n) << 16 |
  4636. reduced_clock->m1 << 8 | reduced_clock->m2;
  4637. } else {
  4638. fp = clock->n << 16 | clock->m1 << 8 | clock->m2;
  4639. if (reduced_clock)
  4640. fp2 = reduced_clock->n << 16 | reduced_clock->m1 << 8 |
  4641. reduced_clock->m2;
  4642. }
  4643. I915_WRITE(FP0(pipe), fp);
  4644. intel_crtc->lowfreq_avail = false;
  4645. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  4646. reduced_clock && i915_powersave) {
  4647. I915_WRITE(FP1(pipe), fp2);
  4648. intel_crtc->lowfreq_avail = true;
  4649. } else {
  4650. I915_WRITE(FP1(pipe), fp);
  4651. }
  4652. }
  4653. static void intel_update_lvds(struct drm_crtc *crtc, intel_clock_t *clock,
  4654. struct drm_display_mode *adjusted_mode)
  4655. {
  4656. struct drm_device *dev = crtc->dev;
  4657. struct drm_i915_private *dev_priv = dev->dev_private;
  4658. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4659. int pipe = intel_crtc->pipe;
  4660. u32 temp, lvds_sync = 0;
  4661. temp = I915_READ(LVDS);
  4662. temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  4663. if (pipe == 1) {
  4664. temp |= LVDS_PIPEB_SELECT;
  4665. } else {
  4666. temp &= ~LVDS_PIPEB_SELECT;
  4667. }
  4668. /* set the corresponsding LVDS_BORDER bit */
  4669. temp |= dev_priv->lvds_border_bits;
  4670. /* Set the B0-B3 data pairs corresponding to whether we're going to
  4671. * set the DPLLs for dual-channel mode or not.
  4672. */
  4673. if (clock->p2 == 7)
  4674. temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  4675. else
  4676. temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  4677. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  4678. * appropriately here, but we need to look more thoroughly into how
  4679. * panels behave in the two modes.
  4680. */
  4681. /* set the dithering flag on LVDS as needed */
  4682. if (INTEL_INFO(dev)->gen >= 4) {
  4683. if (dev_priv->lvds_dither)
  4684. temp |= LVDS_ENABLE_DITHER;
  4685. else
  4686. temp &= ~LVDS_ENABLE_DITHER;
  4687. }
  4688. if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
  4689. lvds_sync |= LVDS_HSYNC_POLARITY;
  4690. if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
  4691. lvds_sync |= LVDS_VSYNC_POLARITY;
  4692. if ((temp & (LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY))
  4693. != lvds_sync) {
  4694. char flags[2] = "-+";
  4695. DRM_INFO("Changing LVDS panel from "
  4696. "(%chsync, %cvsync) to (%chsync, %cvsync)\n",
  4697. flags[!(temp & LVDS_HSYNC_POLARITY)],
  4698. flags[!(temp & LVDS_VSYNC_POLARITY)],
  4699. flags[!(lvds_sync & LVDS_HSYNC_POLARITY)],
  4700. flags[!(lvds_sync & LVDS_VSYNC_POLARITY)]);
  4701. temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
  4702. temp |= lvds_sync;
  4703. }
  4704. I915_WRITE(LVDS, temp);
  4705. }
  4706. static void i9xx_update_pll(struct drm_crtc *crtc,
  4707. struct drm_display_mode *mode,
  4708. struct drm_display_mode *adjusted_mode,
  4709. intel_clock_t *clock, intel_clock_t *reduced_clock,
  4710. int num_connectors)
  4711. {
  4712. struct drm_device *dev = crtc->dev;
  4713. struct drm_i915_private *dev_priv = dev->dev_private;
  4714. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4715. int pipe = intel_crtc->pipe;
  4716. u32 dpll;
  4717. bool is_sdvo;
  4718. is_sdvo = intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO) ||
  4719. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
  4720. dpll = DPLL_VGA_MODE_DIS;
  4721. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  4722. dpll |= DPLLB_MODE_LVDS;
  4723. else
  4724. dpll |= DPLLB_MODE_DAC_SERIAL;
  4725. if (is_sdvo) {
  4726. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  4727. if (pixel_multiplier > 1) {
  4728. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  4729. dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
  4730. }
  4731. dpll |= DPLL_DVO_HIGH_SPEED;
  4732. }
  4733. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
  4734. dpll |= DPLL_DVO_HIGH_SPEED;
  4735. /* compute bitmask from p1 value */
  4736. if (IS_PINEVIEW(dev))
  4737. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  4738. else {
  4739. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4740. if (IS_G4X(dev) && reduced_clock)
  4741. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  4742. }
  4743. switch (clock->p2) {
  4744. case 5:
  4745. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  4746. break;
  4747. case 7:
  4748. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  4749. break;
  4750. case 10:
  4751. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  4752. break;
  4753. case 14:
  4754. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  4755. break;
  4756. }
  4757. if (INTEL_INFO(dev)->gen >= 4)
  4758. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  4759. if (is_sdvo && intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
  4760. dpll |= PLL_REF_INPUT_TVCLKINBC;
  4761. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
  4762. /* XXX: just matching BIOS for now */
  4763. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  4764. dpll |= 3;
  4765. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  4766. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4767. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4768. else
  4769. dpll |= PLL_REF_INPUT_DREFCLK;
  4770. dpll |= DPLL_VCO_ENABLE;
  4771. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
  4772. POSTING_READ(DPLL(pipe));
  4773. udelay(150);
  4774. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  4775. * This is an exception to the general rule that mode_set doesn't turn
  4776. * things on.
  4777. */
  4778. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  4779. intel_update_lvds(crtc, clock, adjusted_mode);
  4780. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
  4781. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  4782. I915_WRITE(DPLL(pipe), dpll);
  4783. /* Wait for the clocks to stabilize. */
  4784. POSTING_READ(DPLL(pipe));
  4785. udelay(150);
  4786. if (INTEL_INFO(dev)->gen >= 4) {
  4787. u32 temp = 0;
  4788. if (is_sdvo) {
  4789. temp = intel_mode_get_pixel_multiplier(adjusted_mode);
  4790. if (temp > 1)
  4791. temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4792. else
  4793. temp = 0;
  4794. }
  4795. I915_WRITE(DPLL_MD(pipe), temp);
  4796. } else {
  4797. /* The pixel multiplier can only be updated once the
  4798. * DPLL is enabled and the clocks are stable.
  4799. *
  4800. * So write it again.
  4801. */
  4802. I915_WRITE(DPLL(pipe), dpll);
  4803. }
  4804. }
  4805. static void i8xx_update_pll(struct drm_crtc *crtc,
  4806. struct drm_display_mode *adjusted_mode,
  4807. intel_clock_t *clock,
  4808. int num_connectors)
  4809. {
  4810. struct drm_device *dev = crtc->dev;
  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. u32 dpll;
  4815. dpll = DPLL_VGA_MODE_DIS;
  4816. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  4817. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4818. } else {
  4819. if (clock->p1 == 2)
  4820. dpll |= PLL_P1_DIVIDE_BY_TWO;
  4821. else
  4822. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4823. if (clock->p2 == 4)
  4824. dpll |= PLL_P2_DIVIDE_BY_4;
  4825. }
  4826. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
  4827. /* XXX: just matching BIOS for now */
  4828. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  4829. dpll |= 3;
  4830. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  4831. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4832. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4833. else
  4834. dpll |= PLL_REF_INPUT_DREFCLK;
  4835. dpll |= DPLL_VCO_ENABLE;
  4836. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
  4837. POSTING_READ(DPLL(pipe));
  4838. udelay(150);
  4839. I915_WRITE(DPLL(pipe), dpll);
  4840. /* Wait for the clocks to stabilize. */
  4841. POSTING_READ(DPLL(pipe));
  4842. udelay(150);
  4843. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  4844. * This is an exception to the general rule that mode_set doesn't turn
  4845. * things on.
  4846. */
  4847. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  4848. intel_update_lvds(crtc, clock, adjusted_mode);
  4849. /* The pixel multiplier can only be updated once the
  4850. * DPLL is enabled and the clocks are stable.
  4851. *
  4852. * So write it again.
  4853. */
  4854. I915_WRITE(DPLL(pipe), dpll);
  4855. }
  4856. static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
  4857. struct drm_display_mode *mode,
  4858. struct drm_display_mode *adjusted_mode,
  4859. int x, int y,
  4860. struct drm_framebuffer *old_fb)
  4861. {
  4862. struct drm_device *dev = crtc->dev;
  4863. struct drm_i915_private *dev_priv = dev->dev_private;
  4864. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4865. int pipe = intel_crtc->pipe;
  4866. int plane = intel_crtc->plane;
  4867. int refclk, num_connectors = 0;
  4868. intel_clock_t clock, reduced_clock;
  4869. u32 dspcntr, pipeconf, vsyncshift;
  4870. bool ok, has_reduced_clock = false, is_sdvo = false;
  4871. bool is_lvds = false, is_tv = false, is_dp = false;
  4872. struct drm_mode_config *mode_config = &dev->mode_config;
  4873. struct intel_encoder *encoder;
  4874. const intel_limit_t *limit;
  4875. int ret;
  4876. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  4877. if (encoder->base.crtc != crtc)
  4878. continue;
  4879. switch (encoder->type) {
  4880. case INTEL_OUTPUT_LVDS:
  4881. is_lvds = true;
  4882. break;
  4883. case INTEL_OUTPUT_SDVO:
  4884. case INTEL_OUTPUT_HDMI:
  4885. is_sdvo = true;
  4886. if (encoder->needs_tv_clock)
  4887. is_tv = true;
  4888. break;
  4889. case INTEL_OUTPUT_TVOUT:
  4890. is_tv = true;
  4891. break;
  4892. case INTEL_OUTPUT_DISPLAYPORT:
  4893. is_dp = true;
  4894. break;
  4895. }
  4896. num_connectors++;
  4897. }
  4898. refclk = i9xx_get_refclk(crtc, num_connectors);
  4899. /*
  4900. * Returns a set of divisors for the desired target clock with the given
  4901. * refclk, or FALSE. The returned values represent the clock equation:
  4902. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  4903. */
  4904. limit = intel_limit(crtc, refclk);
  4905. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
  4906. &clock);
  4907. if (!ok) {
  4908. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  4909. return -EINVAL;
  4910. }
  4911. /* Ensure that the cursor is valid for the new mode before changing... */
  4912. intel_crtc_update_cursor(crtc, true);
  4913. if (is_lvds && dev_priv->lvds_downclock_avail) {
  4914. /*
  4915. * Ensure we match the reduced clock's P to the target clock.
  4916. * If the clocks don't match, we can't switch the display clock
  4917. * by using the FP0/FP1. In such case we will disable the LVDS
  4918. * downclock feature.
  4919. */
  4920. has_reduced_clock = limit->find_pll(limit, crtc,
  4921. dev_priv->lvds_downclock,
  4922. refclk,
  4923. &clock,
  4924. &reduced_clock);
  4925. }
  4926. if (is_sdvo && is_tv)
  4927. i9xx_adjust_sdvo_tv_clock(adjusted_mode, &clock);
  4928. i9xx_update_pll_dividers(crtc, &clock, has_reduced_clock ?
  4929. &reduced_clock : NULL);
  4930. if (IS_GEN2(dev))
  4931. i8xx_update_pll(crtc, adjusted_mode, &clock, num_connectors);
  4932. else
  4933. i9xx_update_pll(crtc, mode, adjusted_mode, &clock,
  4934. has_reduced_clock ? &reduced_clock : NULL,
  4935. num_connectors);
  4936. /* setup pipeconf */
  4937. pipeconf = I915_READ(PIPECONF(pipe));
  4938. /* Set up the display plane register */
  4939. dspcntr = DISPPLANE_GAMMA_ENABLE;
  4940. if (pipe == 0)
  4941. dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
  4942. else
  4943. dspcntr |= DISPPLANE_SEL_PIPE_B;
  4944. if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
  4945. /* Enable pixel doubling when the dot clock is > 90% of the (display)
  4946. * core speed.
  4947. *
  4948. * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
  4949. * pipe == 0 check?
  4950. */
  4951. if (mode->clock >
  4952. dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
  4953. pipeconf |= PIPECONF_DOUBLE_WIDE;
  4954. else
  4955. pipeconf &= ~PIPECONF_DOUBLE_WIDE;
  4956. }
  4957. /* default to 8bpc */
  4958. pipeconf &= ~(PIPECONF_BPP_MASK | PIPECONF_DITHER_EN);
  4959. if (is_dp) {
  4960. if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
  4961. pipeconf |= PIPECONF_BPP_6 |
  4962. PIPECONF_DITHER_EN |
  4963. PIPECONF_DITHER_TYPE_SP;
  4964. }
  4965. }
  4966. DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  4967. drm_mode_debug_printmodeline(mode);
  4968. if (HAS_PIPE_CXSR(dev)) {
  4969. if (intel_crtc->lowfreq_avail) {
  4970. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  4971. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  4972. } else {
  4973. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  4974. pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
  4975. }
  4976. }
  4977. pipeconf &= ~PIPECONF_INTERLACE_MASK;
  4978. if (!IS_GEN2(dev) &&
  4979. adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4980. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  4981. /* the chip adds 2 halflines automatically */
  4982. adjusted_mode->crtc_vtotal -= 1;
  4983. adjusted_mode->crtc_vblank_end -= 1;
  4984. vsyncshift = adjusted_mode->crtc_hsync_start
  4985. - adjusted_mode->crtc_htotal/2;
  4986. } else {
  4987. pipeconf |= PIPECONF_PROGRESSIVE;
  4988. vsyncshift = 0;
  4989. }
  4990. if (!IS_GEN3(dev))
  4991. I915_WRITE(VSYNCSHIFT(pipe), vsyncshift);
  4992. I915_WRITE(HTOTAL(pipe),
  4993. (adjusted_mode->crtc_hdisplay - 1) |
  4994. ((adjusted_mode->crtc_htotal - 1) << 16));
  4995. I915_WRITE(HBLANK(pipe),
  4996. (adjusted_mode->crtc_hblank_start - 1) |
  4997. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  4998. I915_WRITE(HSYNC(pipe),
  4999. (adjusted_mode->crtc_hsync_start - 1) |
  5000. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5001. I915_WRITE(VTOTAL(pipe),
  5002. (adjusted_mode->crtc_vdisplay - 1) |
  5003. ((adjusted_mode->crtc_vtotal - 1) << 16));
  5004. I915_WRITE(VBLANK(pipe),
  5005. (adjusted_mode->crtc_vblank_start - 1) |
  5006. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  5007. I915_WRITE(VSYNC(pipe),
  5008. (adjusted_mode->crtc_vsync_start - 1) |
  5009. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5010. /* pipesrc and dspsize control the size that is scaled from,
  5011. * which should always be the user's requested size.
  5012. */
  5013. I915_WRITE(DSPSIZE(plane),
  5014. ((mode->vdisplay - 1) << 16) |
  5015. (mode->hdisplay - 1));
  5016. I915_WRITE(DSPPOS(plane), 0);
  5017. I915_WRITE(PIPESRC(pipe),
  5018. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  5019. I915_WRITE(PIPECONF(pipe), pipeconf);
  5020. POSTING_READ(PIPECONF(pipe));
  5021. intel_enable_pipe(dev_priv, pipe, false);
  5022. intel_wait_for_vblank(dev, pipe);
  5023. I915_WRITE(DSPCNTR(plane), dspcntr);
  5024. POSTING_READ(DSPCNTR(plane));
  5025. intel_enable_plane(dev_priv, plane, pipe);
  5026. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  5027. intel_update_watermarks(dev);
  5028. return ret;
  5029. }
  5030. /*
  5031. * Initialize reference clocks when the driver loads
  5032. */
  5033. void ironlake_init_pch_refclk(struct drm_device *dev)
  5034. {
  5035. struct drm_i915_private *dev_priv = dev->dev_private;
  5036. struct drm_mode_config *mode_config = &dev->mode_config;
  5037. struct intel_encoder *encoder;
  5038. u32 temp;
  5039. bool has_lvds = false;
  5040. bool has_cpu_edp = false;
  5041. bool has_pch_edp = false;
  5042. bool has_panel = false;
  5043. bool has_ck505 = false;
  5044. bool can_ssc = false;
  5045. /* We need to take the global config into account */
  5046. list_for_each_entry(encoder, &mode_config->encoder_list,
  5047. base.head) {
  5048. switch (encoder->type) {
  5049. case INTEL_OUTPUT_LVDS:
  5050. has_panel = true;
  5051. has_lvds = true;
  5052. break;
  5053. case INTEL_OUTPUT_EDP:
  5054. has_panel = true;
  5055. if (intel_encoder_is_pch_edp(&encoder->base))
  5056. has_pch_edp = true;
  5057. else
  5058. has_cpu_edp = true;
  5059. break;
  5060. }
  5061. }
  5062. if (HAS_PCH_IBX(dev)) {
  5063. has_ck505 = dev_priv->display_clock_mode;
  5064. can_ssc = has_ck505;
  5065. } else {
  5066. has_ck505 = false;
  5067. can_ssc = true;
  5068. }
  5069. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_pch_edp %d has_cpu_edp %d has_ck505 %d\n",
  5070. has_panel, has_lvds, has_pch_edp, has_cpu_edp,
  5071. has_ck505);
  5072. /* Ironlake: try to setup display ref clock before DPLL
  5073. * enabling. This is only under driver's control after
  5074. * PCH B stepping, previous chipset stepping should be
  5075. * ignoring this setting.
  5076. */
  5077. temp = I915_READ(PCH_DREF_CONTROL);
  5078. /* Always enable nonspread source */
  5079. temp &= ~DREF_NONSPREAD_SOURCE_MASK;
  5080. if (has_ck505)
  5081. temp |= DREF_NONSPREAD_CK505_ENABLE;
  5082. else
  5083. temp |= DREF_NONSPREAD_SOURCE_ENABLE;
  5084. if (has_panel) {
  5085. temp &= ~DREF_SSC_SOURCE_MASK;
  5086. temp |= DREF_SSC_SOURCE_ENABLE;
  5087. /* SSC must be turned on before enabling the CPU output */
  5088. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5089. DRM_DEBUG_KMS("Using SSC on panel\n");
  5090. temp |= DREF_SSC1_ENABLE;
  5091. } else
  5092. temp &= ~DREF_SSC1_ENABLE;
  5093. /* Get SSC going before enabling the outputs */
  5094. I915_WRITE(PCH_DREF_CONTROL, temp);
  5095. POSTING_READ(PCH_DREF_CONTROL);
  5096. udelay(200);
  5097. temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5098. /* Enable CPU source on CPU attached eDP */
  5099. if (has_cpu_edp) {
  5100. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5101. DRM_DEBUG_KMS("Using SSC on eDP\n");
  5102. temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5103. }
  5104. else
  5105. temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5106. } else
  5107. temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5108. I915_WRITE(PCH_DREF_CONTROL, temp);
  5109. POSTING_READ(PCH_DREF_CONTROL);
  5110. udelay(200);
  5111. } else {
  5112. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  5113. temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5114. /* Turn off CPU output */
  5115. temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5116. I915_WRITE(PCH_DREF_CONTROL, temp);
  5117. POSTING_READ(PCH_DREF_CONTROL);
  5118. udelay(200);
  5119. /* Turn off the SSC source */
  5120. temp &= ~DREF_SSC_SOURCE_MASK;
  5121. temp |= DREF_SSC_SOURCE_DISABLE;
  5122. /* Turn off SSC1 */
  5123. temp &= ~ DREF_SSC1_ENABLE;
  5124. I915_WRITE(PCH_DREF_CONTROL, temp);
  5125. POSTING_READ(PCH_DREF_CONTROL);
  5126. udelay(200);
  5127. }
  5128. }
  5129. static int ironlake_get_refclk(struct drm_crtc *crtc)
  5130. {
  5131. struct drm_device *dev = crtc->dev;
  5132. struct drm_i915_private *dev_priv = dev->dev_private;
  5133. struct intel_encoder *encoder;
  5134. struct drm_mode_config *mode_config = &dev->mode_config;
  5135. struct intel_encoder *edp_encoder = NULL;
  5136. int num_connectors = 0;
  5137. bool is_lvds = false;
  5138. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  5139. if (encoder->base.crtc != crtc)
  5140. continue;
  5141. switch (encoder->type) {
  5142. case INTEL_OUTPUT_LVDS:
  5143. is_lvds = true;
  5144. break;
  5145. case INTEL_OUTPUT_EDP:
  5146. edp_encoder = encoder;
  5147. break;
  5148. }
  5149. num_connectors++;
  5150. }
  5151. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5152. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  5153. dev_priv->lvds_ssc_freq);
  5154. return dev_priv->lvds_ssc_freq * 1000;
  5155. }
  5156. return 120000;
  5157. }
  5158. static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
  5159. struct drm_display_mode *mode,
  5160. struct drm_display_mode *adjusted_mode,
  5161. int x, int y,
  5162. struct drm_framebuffer *old_fb)
  5163. {
  5164. struct drm_device *dev = crtc->dev;
  5165. struct drm_i915_private *dev_priv = dev->dev_private;
  5166. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5167. int pipe = intel_crtc->pipe;
  5168. int plane = intel_crtc->plane;
  5169. int refclk, num_connectors = 0;
  5170. intel_clock_t clock, reduced_clock;
  5171. u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
  5172. bool ok, has_reduced_clock = false, is_sdvo = false;
  5173. bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
  5174. struct drm_mode_config *mode_config = &dev->mode_config;
  5175. struct intel_encoder *encoder, *edp_encoder = NULL;
  5176. const intel_limit_t *limit;
  5177. int ret;
  5178. struct fdi_m_n m_n = {0};
  5179. u32 temp;
  5180. u32 lvds_sync = 0;
  5181. int target_clock, pixel_multiplier, lane, link_bw, factor;
  5182. unsigned int pipe_bpp;
  5183. bool dither;
  5184. bool is_cpu_edp = false, is_pch_edp = false;
  5185. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  5186. if (encoder->base.crtc != crtc)
  5187. continue;
  5188. switch (encoder->type) {
  5189. case INTEL_OUTPUT_LVDS:
  5190. is_lvds = true;
  5191. break;
  5192. case INTEL_OUTPUT_SDVO:
  5193. case INTEL_OUTPUT_HDMI:
  5194. is_sdvo = true;
  5195. if (encoder->needs_tv_clock)
  5196. is_tv = true;
  5197. break;
  5198. case INTEL_OUTPUT_TVOUT:
  5199. is_tv = true;
  5200. break;
  5201. case INTEL_OUTPUT_ANALOG:
  5202. is_crt = true;
  5203. break;
  5204. case INTEL_OUTPUT_DISPLAYPORT:
  5205. is_dp = true;
  5206. break;
  5207. case INTEL_OUTPUT_EDP:
  5208. is_dp = true;
  5209. if (intel_encoder_is_pch_edp(&encoder->base))
  5210. is_pch_edp = true;
  5211. else
  5212. is_cpu_edp = true;
  5213. edp_encoder = encoder;
  5214. break;
  5215. }
  5216. num_connectors++;
  5217. }
  5218. refclk = ironlake_get_refclk(crtc);
  5219. /*
  5220. * Returns a set of divisors for the desired target clock with the given
  5221. * refclk, or FALSE. The returned values represent the clock equation:
  5222. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  5223. */
  5224. limit = intel_limit(crtc, refclk);
  5225. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
  5226. &clock);
  5227. if (!ok) {
  5228. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5229. return -EINVAL;
  5230. }
  5231. /* Ensure that the cursor is valid for the new mode before changing... */
  5232. intel_crtc_update_cursor(crtc, true);
  5233. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5234. /*
  5235. * Ensure we match the reduced clock's P to the target clock.
  5236. * If the clocks don't match, we can't switch the display clock
  5237. * by using the FP0/FP1. In such case we will disable the LVDS
  5238. * downclock feature.
  5239. */
  5240. has_reduced_clock = limit->find_pll(limit, crtc,
  5241. dev_priv->lvds_downclock,
  5242. refclk,
  5243. &clock,
  5244. &reduced_clock);
  5245. }
  5246. /* SDVO TV has fixed PLL values depend on its clock range,
  5247. this mirrors vbios setting. */
  5248. if (is_sdvo && is_tv) {
  5249. if (adjusted_mode->clock >= 100000
  5250. && adjusted_mode->clock < 140500) {
  5251. clock.p1 = 2;
  5252. clock.p2 = 10;
  5253. clock.n = 3;
  5254. clock.m1 = 16;
  5255. clock.m2 = 8;
  5256. } else if (adjusted_mode->clock >= 140500
  5257. && adjusted_mode->clock <= 200000) {
  5258. clock.p1 = 1;
  5259. clock.p2 = 10;
  5260. clock.n = 6;
  5261. clock.m1 = 12;
  5262. clock.m2 = 8;
  5263. }
  5264. }
  5265. /* FDI link */
  5266. pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  5267. lane = 0;
  5268. /* CPU eDP doesn't require FDI link, so just set DP M/N
  5269. according to current link config */
  5270. if (is_cpu_edp) {
  5271. target_clock = mode->clock;
  5272. intel_edp_link_config(edp_encoder, &lane, &link_bw);
  5273. } else {
  5274. /* [e]DP over FDI requires target mode clock
  5275. instead of link clock */
  5276. if (is_dp)
  5277. target_clock = mode->clock;
  5278. else
  5279. target_clock = adjusted_mode->clock;
  5280. /* FDI is a binary signal running at ~2.7GHz, encoding
  5281. * each output octet as 10 bits. The actual frequency
  5282. * is stored as a divider into a 100MHz clock, and the
  5283. * mode pixel clock is stored in units of 1KHz.
  5284. * Hence the bw of each lane in terms of the mode signal
  5285. * is:
  5286. */
  5287. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  5288. }
  5289. /* determine panel color depth */
  5290. temp = I915_READ(PIPECONF(pipe));
  5291. temp &= ~PIPE_BPC_MASK;
  5292. dither = intel_choose_pipe_bpp_dither(crtc, &pipe_bpp, mode);
  5293. switch (pipe_bpp) {
  5294. case 18:
  5295. temp |= PIPE_6BPC;
  5296. break;
  5297. case 24:
  5298. temp |= PIPE_8BPC;
  5299. break;
  5300. case 30:
  5301. temp |= PIPE_10BPC;
  5302. break;
  5303. case 36:
  5304. temp |= PIPE_12BPC;
  5305. break;
  5306. default:
  5307. WARN(1, "intel_choose_pipe_bpp returned invalid value %d\n",
  5308. pipe_bpp);
  5309. temp |= PIPE_8BPC;
  5310. pipe_bpp = 24;
  5311. break;
  5312. }
  5313. intel_crtc->bpp = pipe_bpp;
  5314. I915_WRITE(PIPECONF(pipe), temp);
  5315. if (!lane) {
  5316. /*
  5317. * Account for spread spectrum to avoid
  5318. * oversubscribing the link. Max center spread
  5319. * is 2.5%; use 5% for safety's sake.
  5320. */
  5321. u32 bps = target_clock * intel_crtc->bpp * 21 / 20;
  5322. lane = bps / (link_bw * 8) + 1;
  5323. }
  5324. intel_crtc->fdi_lanes = lane;
  5325. if (pixel_multiplier > 1)
  5326. link_bw *= pixel_multiplier;
  5327. ironlake_compute_m_n(intel_crtc->bpp, lane, target_clock, link_bw,
  5328. &m_n);
  5329. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  5330. if (has_reduced_clock)
  5331. fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
  5332. reduced_clock.m2;
  5333. /* Enable autotuning of the PLL clock (if permissible) */
  5334. factor = 21;
  5335. if (is_lvds) {
  5336. if ((intel_panel_use_ssc(dev_priv) &&
  5337. dev_priv->lvds_ssc_freq == 100) ||
  5338. (I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP)
  5339. factor = 25;
  5340. } else if (is_sdvo && is_tv)
  5341. factor = 20;
  5342. if (clock.m < factor * clock.n)
  5343. fp |= FP_CB_TUNE;
  5344. dpll = 0;
  5345. if (is_lvds)
  5346. dpll |= DPLLB_MODE_LVDS;
  5347. else
  5348. dpll |= DPLLB_MODE_DAC_SERIAL;
  5349. if (is_sdvo) {
  5350. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  5351. if (pixel_multiplier > 1) {
  5352. dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  5353. }
  5354. dpll |= DPLL_DVO_HIGH_SPEED;
  5355. }
  5356. if (is_dp && !is_cpu_edp)
  5357. dpll |= DPLL_DVO_HIGH_SPEED;
  5358. /* compute bitmask from p1 value */
  5359. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5360. /* also FPA1 */
  5361. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5362. switch (clock.p2) {
  5363. case 5:
  5364. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5365. break;
  5366. case 7:
  5367. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5368. break;
  5369. case 10:
  5370. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5371. break;
  5372. case 14:
  5373. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5374. break;
  5375. }
  5376. if (is_sdvo && is_tv)
  5377. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5378. else if (is_tv)
  5379. /* XXX: just matching BIOS for now */
  5380. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  5381. dpll |= 3;
  5382. else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5383. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5384. else
  5385. dpll |= PLL_REF_INPUT_DREFCLK;
  5386. /* setup pipeconf */
  5387. pipeconf = I915_READ(PIPECONF(pipe));
  5388. /* Set up the display plane register */
  5389. dspcntr = DISPPLANE_GAMMA_ENABLE;
  5390. DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
  5391. drm_mode_debug_printmodeline(mode);
  5392. /* PCH eDP needs FDI, but CPU eDP does not */
  5393. if (!intel_crtc->no_pll) {
  5394. if (!is_cpu_edp) {
  5395. I915_WRITE(PCH_FP0(pipe), fp);
  5396. I915_WRITE(PCH_DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
  5397. POSTING_READ(PCH_DPLL(pipe));
  5398. udelay(150);
  5399. }
  5400. } else {
  5401. if (dpll == (I915_READ(PCH_DPLL(0)) & 0x7fffffff) &&
  5402. fp == I915_READ(PCH_FP0(0))) {
  5403. intel_crtc->use_pll_a = true;
  5404. DRM_DEBUG_KMS("using pipe a dpll\n");
  5405. } else if (dpll == (I915_READ(PCH_DPLL(1)) & 0x7fffffff) &&
  5406. fp == I915_READ(PCH_FP0(1))) {
  5407. intel_crtc->use_pll_a = false;
  5408. DRM_DEBUG_KMS("using pipe b dpll\n");
  5409. } else {
  5410. DRM_DEBUG_KMS("no matching PLL configuration for pipe 2\n");
  5411. return -EINVAL;
  5412. }
  5413. }
  5414. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  5415. * This is an exception to the general rule that mode_set doesn't turn
  5416. * things on.
  5417. */
  5418. if (is_lvds) {
  5419. temp = I915_READ(PCH_LVDS);
  5420. temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  5421. if (HAS_PCH_CPT(dev)) {
  5422. temp &= ~PORT_TRANS_SEL_MASK;
  5423. temp |= PORT_TRANS_SEL_CPT(pipe);
  5424. } else {
  5425. if (pipe == 1)
  5426. temp |= LVDS_PIPEB_SELECT;
  5427. else
  5428. temp &= ~LVDS_PIPEB_SELECT;
  5429. }
  5430. /* set the corresponsding LVDS_BORDER bit */
  5431. temp |= dev_priv->lvds_border_bits;
  5432. /* Set the B0-B3 data pairs corresponding to whether we're going to
  5433. * set the DPLLs for dual-channel mode or not.
  5434. */
  5435. if (clock.p2 == 7)
  5436. temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  5437. else
  5438. temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  5439. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  5440. * appropriately here, but we need to look more thoroughly into how
  5441. * panels behave in the two modes.
  5442. */
  5443. if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
  5444. lvds_sync |= LVDS_HSYNC_POLARITY;
  5445. if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
  5446. lvds_sync |= LVDS_VSYNC_POLARITY;
  5447. if ((temp & (LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY))
  5448. != lvds_sync) {
  5449. char flags[2] = "-+";
  5450. DRM_INFO("Changing LVDS panel from "
  5451. "(%chsync, %cvsync) to (%chsync, %cvsync)\n",
  5452. flags[!(temp & LVDS_HSYNC_POLARITY)],
  5453. flags[!(temp & LVDS_VSYNC_POLARITY)],
  5454. flags[!(lvds_sync & LVDS_HSYNC_POLARITY)],
  5455. flags[!(lvds_sync & LVDS_VSYNC_POLARITY)]);
  5456. temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
  5457. temp |= lvds_sync;
  5458. }
  5459. I915_WRITE(PCH_LVDS, temp);
  5460. }
  5461. pipeconf &= ~PIPECONF_DITHER_EN;
  5462. pipeconf &= ~PIPECONF_DITHER_TYPE_MASK;
  5463. if ((is_lvds && dev_priv->lvds_dither) || dither) {
  5464. pipeconf |= PIPECONF_DITHER_EN;
  5465. pipeconf |= PIPECONF_DITHER_TYPE_SP;
  5466. }
  5467. if (is_dp && !is_cpu_edp) {
  5468. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  5469. } else {
  5470. /* For non-DP output, clear any trans DP clock recovery setting.*/
  5471. I915_WRITE(TRANSDATA_M1(pipe), 0);
  5472. I915_WRITE(TRANSDATA_N1(pipe), 0);
  5473. I915_WRITE(TRANSDPLINK_M1(pipe), 0);
  5474. I915_WRITE(TRANSDPLINK_N1(pipe), 0);
  5475. }
  5476. if (!intel_crtc->no_pll && (!edp_encoder || is_pch_edp)) {
  5477. I915_WRITE(PCH_DPLL(pipe), dpll);
  5478. /* Wait for the clocks to stabilize. */
  5479. POSTING_READ(PCH_DPLL(pipe));
  5480. udelay(150);
  5481. /* The pixel multiplier can only be updated once the
  5482. * DPLL is enabled and the clocks are stable.
  5483. *
  5484. * So write it again.
  5485. */
  5486. I915_WRITE(PCH_DPLL(pipe), dpll);
  5487. }
  5488. intel_crtc->lowfreq_avail = false;
  5489. if (!intel_crtc->no_pll) {
  5490. if (is_lvds && has_reduced_clock && i915_powersave) {
  5491. I915_WRITE(PCH_FP1(pipe), fp2);
  5492. intel_crtc->lowfreq_avail = true;
  5493. if (HAS_PIPE_CXSR(dev)) {
  5494. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  5495. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  5496. }
  5497. } else {
  5498. I915_WRITE(PCH_FP1(pipe), fp);
  5499. if (HAS_PIPE_CXSR(dev)) {
  5500. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  5501. pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
  5502. }
  5503. }
  5504. }
  5505. pipeconf &= ~PIPECONF_INTERLACE_MASK;
  5506. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5507. pipeconf |= PIPECONF_INTERLACED_ILK;
  5508. /* the chip adds 2 halflines automatically */
  5509. adjusted_mode->crtc_vtotal -= 1;
  5510. adjusted_mode->crtc_vblank_end -= 1;
  5511. I915_WRITE(VSYNCSHIFT(pipe),
  5512. adjusted_mode->crtc_hsync_start
  5513. - adjusted_mode->crtc_htotal/2);
  5514. } else {
  5515. pipeconf |= PIPECONF_PROGRESSIVE;
  5516. I915_WRITE(VSYNCSHIFT(pipe), 0);
  5517. }
  5518. I915_WRITE(HTOTAL(pipe),
  5519. (adjusted_mode->crtc_hdisplay - 1) |
  5520. ((adjusted_mode->crtc_htotal - 1) << 16));
  5521. I915_WRITE(HBLANK(pipe),
  5522. (adjusted_mode->crtc_hblank_start - 1) |
  5523. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5524. I915_WRITE(HSYNC(pipe),
  5525. (adjusted_mode->crtc_hsync_start - 1) |
  5526. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5527. I915_WRITE(VTOTAL(pipe),
  5528. (adjusted_mode->crtc_vdisplay - 1) |
  5529. ((adjusted_mode->crtc_vtotal - 1) << 16));
  5530. I915_WRITE(VBLANK(pipe),
  5531. (adjusted_mode->crtc_vblank_start - 1) |
  5532. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  5533. I915_WRITE(VSYNC(pipe),
  5534. (adjusted_mode->crtc_vsync_start - 1) |
  5535. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5536. /* pipesrc controls the size that is scaled from, which should
  5537. * always be the user's requested size.
  5538. */
  5539. I915_WRITE(PIPESRC(pipe),
  5540. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  5541. I915_WRITE(PIPE_DATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);
  5542. I915_WRITE(PIPE_DATA_N1(pipe), m_n.gmch_n);
  5543. I915_WRITE(PIPE_LINK_M1(pipe), m_n.link_m);
  5544. I915_WRITE(PIPE_LINK_N1(pipe), m_n.link_n);
  5545. if (is_cpu_edp)
  5546. ironlake_set_pll_edp(crtc, adjusted_mode->clock);
  5547. I915_WRITE(PIPECONF(pipe), pipeconf);
  5548. POSTING_READ(PIPECONF(pipe));
  5549. intel_wait_for_vblank(dev, pipe);
  5550. I915_WRITE(DSPCNTR(plane), dspcntr);
  5551. POSTING_READ(DSPCNTR(plane));
  5552. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  5553. intel_update_watermarks(dev);
  5554. return ret;
  5555. }
  5556. static int intel_crtc_mode_set(struct drm_crtc *crtc,
  5557. struct drm_display_mode *mode,
  5558. struct drm_display_mode *adjusted_mode,
  5559. int x, int y,
  5560. struct drm_framebuffer *old_fb)
  5561. {
  5562. struct drm_device *dev = crtc->dev;
  5563. struct drm_i915_private *dev_priv = dev->dev_private;
  5564. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5565. int pipe = intel_crtc->pipe;
  5566. int ret;
  5567. drm_vblank_pre_modeset(dev, pipe);
  5568. ret = dev_priv->display.crtc_mode_set(crtc, mode, adjusted_mode,
  5569. x, y, old_fb);
  5570. drm_vblank_post_modeset(dev, pipe);
  5571. if (ret)
  5572. intel_crtc->dpms_mode = DRM_MODE_DPMS_OFF;
  5573. else
  5574. intel_crtc->dpms_mode = DRM_MODE_DPMS_ON;
  5575. return ret;
  5576. }
  5577. static bool intel_eld_uptodate(struct drm_connector *connector,
  5578. int reg_eldv, uint32_t bits_eldv,
  5579. int reg_elda, uint32_t bits_elda,
  5580. int reg_edid)
  5581. {
  5582. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  5583. uint8_t *eld = connector->eld;
  5584. uint32_t i;
  5585. i = I915_READ(reg_eldv);
  5586. i &= bits_eldv;
  5587. if (!eld[0])
  5588. return !i;
  5589. if (!i)
  5590. return false;
  5591. i = I915_READ(reg_elda);
  5592. i &= ~bits_elda;
  5593. I915_WRITE(reg_elda, i);
  5594. for (i = 0; i < eld[2]; i++)
  5595. if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
  5596. return false;
  5597. return true;
  5598. }
  5599. static void g4x_write_eld(struct drm_connector *connector,
  5600. struct drm_crtc *crtc)
  5601. {
  5602. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  5603. uint8_t *eld = connector->eld;
  5604. uint32_t eldv;
  5605. uint32_t len;
  5606. uint32_t i;
  5607. i = I915_READ(G4X_AUD_VID_DID);
  5608. if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
  5609. eldv = G4X_ELDV_DEVCL_DEVBLC;
  5610. else
  5611. eldv = G4X_ELDV_DEVCTG;
  5612. if (intel_eld_uptodate(connector,
  5613. G4X_AUD_CNTL_ST, eldv,
  5614. G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
  5615. G4X_HDMIW_HDMIEDID))
  5616. return;
  5617. i = I915_READ(G4X_AUD_CNTL_ST);
  5618. i &= ~(eldv | G4X_ELD_ADDR);
  5619. len = (i >> 9) & 0x1f; /* ELD buffer size */
  5620. I915_WRITE(G4X_AUD_CNTL_ST, i);
  5621. if (!eld[0])
  5622. return;
  5623. len = min_t(uint8_t, eld[2], len);
  5624. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  5625. for (i = 0; i < len; i++)
  5626. I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
  5627. i = I915_READ(G4X_AUD_CNTL_ST);
  5628. i |= eldv;
  5629. I915_WRITE(G4X_AUD_CNTL_ST, i);
  5630. }
  5631. static void ironlake_write_eld(struct drm_connector *connector,
  5632. struct drm_crtc *crtc)
  5633. {
  5634. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  5635. uint8_t *eld = connector->eld;
  5636. uint32_t eldv;
  5637. uint32_t i;
  5638. int len;
  5639. int hdmiw_hdmiedid;
  5640. int aud_config;
  5641. int aud_cntl_st;
  5642. int aud_cntrl_st2;
  5643. if (HAS_PCH_IBX(connector->dev)) {
  5644. hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID_A;
  5645. aud_config = IBX_AUD_CONFIG_A;
  5646. aud_cntl_st = IBX_AUD_CNTL_ST_A;
  5647. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  5648. } else {
  5649. hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID_A;
  5650. aud_config = CPT_AUD_CONFIG_A;
  5651. aud_cntl_st = CPT_AUD_CNTL_ST_A;
  5652. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  5653. }
  5654. i = to_intel_crtc(crtc)->pipe;
  5655. hdmiw_hdmiedid += i * 0x100;
  5656. aud_cntl_st += i * 0x100;
  5657. aud_config += i * 0x100;
  5658. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(i));
  5659. i = I915_READ(aud_cntl_st);
  5660. i = (i >> 29) & 0x3; /* DIP_Port_Select, 0x1 = PortB */
  5661. if (!i) {
  5662. DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
  5663. /* operate blindly on all ports */
  5664. eldv = IBX_ELD_VALIDB;
  5665. eldv |= IBX_ELD_VALIDB << 4;
  5666. eldv |= IBX_ELD_VALIDB << 8;
  5667. } else {
  5668. DRM_DEBUG_DRIVER("ELD on port %c\n", 'A' + i);
  5669. eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
  5670. }
  5671. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  5672. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  5673. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  5674. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  5675. } else
  5676. I915_WRITE(aud_config, 0);
  5677. if (intel_eld_uptodate(connector,
  5678. aud_cntrl_st2, eldv,
  5679. aud_cntl_st, IBX_ELD_ADDRESS,
  5680. hdmiw_hdmiedid))
  5681. return;
  5682. i = I915_READ(aud_cntrl_st2);
  5683. i &= ~eldv;
  5684. I915_WRITE(aud_cntrl_st2, i);
  5685. if (!eld[0])
  5686. return;
  5687. i = I915_READ(aud_cntl_st);
  5688. i &= ~IBX_ELD_ADDRESS;
  5689. I915_WRITE(aud_cntl_st, i);
  5690. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  5691. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  5692. for (i = 0; i < len; i++)
  5693. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  5694. i = I915_READ(aud_cntrl_st2);
  5695. i |= eldv;
  5696. I915_WRITE(aud_cntrl_st2, i);
  5697. }
  5698. void intel_write_eld(struct drm_encoder *encoder,
  5699. struct drm_display_mode *mode)
  5700. {
  5701. struct drm_crtc *crtc = encoder->crtc;
  5702. struct drm_connector *connector;
  5703. struct drm_device *dev = encoder->dev;
  5704. struct drm_i915_private *dev_priv = dev->dev_private;
  5705. connector = drm_select_eld(encoder, mode);
  5706. if (!connector)
  5707. return;
  5708. DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  5709. connector->base.id,
  5710. drm_get_connector_name(connector),
  5711. connector->encoder->base.id,
  5712. drm_get_encoder_name(connector->encoder));
  5713. connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
  5714. if (dev_priv->display.write_eld)
  5715. dev_priv->display.write_eld(connector, crtc);
  5716. }
  5717. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  5718. void intel_crtc_load_lut(struct drm_crtc *crtc)
  5719. {
  5720. struct drm_device *dev = crtc->dev;
  5721. struct drm_i915_private *dev_priv = dev->dev_private;
  5722. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5723. int palreg = PALETTE(intel_crtc->pipe);
  5724. int i;
  5725. /* The clocks have to be on to load the palette. */
  5726. if (!crtc->enabled || !intel_crtc->active)
  5727. return;
  5728. /* use legacy palette for Ironlake */
  5729. if (HAS_PCH_SPLIT(dev))
  5730. palreg = LGC_PALETTE(intel_crtc->pipe);
  5731. for (i = 0; i < 256; i++) {
  5732. I915_WRITE(palreg + 4 * i,
  5733. (intel_crtc->lut_r[i] << 16) |
  5734. (intel_crtc->lut_g[i] << 8) |
  5735. intel_crtc->lut_b[i]);
  5736. }
  5737. }
  5738. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  5739. {
  5740. struct drm_device *dev = crtc->dev;
  5741. struct drm_i915_private *dev_priv = dev->dev_private;
  5742. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5743. bool visible = base != 0;
  5744. u32 cntl;
  5745. if (intel_crtc->cursor_visible == visible)
  5746. return;
  5747. cntl = I915_READ(_CURACNTR);
  5748. if (visible) {
  5749. /* On these chipsets we can only modify the base whilst
  5750. * the cursor is disabled.
  5751. */
  5752. I915_WRITE(_CURABASE, base);
  5753. cntl &= ~(CURSOR_FORMAT_MASK);
  5754. /* XXX width must be 64, stride 256 => 0x00 << 28 */
  5755. cntl |= CURSOR_ENABLE |
  5756. CURSOR_GAMMA_ENABLE |
  5757. CURSOR_FORMAT_ARGB;
  5758. } else
  5759. cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
  5760. I915_WRITE(_CURACNTR, cntl);
  5761. intel_crtc->cursor_visible = visible;
  5762. }
  5763. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  5764. {
  5765. struct drm_device *dev = crtc->dev;
  5766. struct drm_i915_private *dev_priv = dev->dev_private;
  5767. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5768. int pipe = intel_crtc->pipe;
  5769. bool visible = base != 0;
  5770. if (intel_crtc->cursor_visible != visible) {
  5771. uint32_t cntl = I915_READ(CURCNTR(pipe));
  5772. if (base) {
  5773. cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
  5774. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  5775. cntl |= pipe << 28; /* Connect to correct pipe */
  5776. } else {
  5777. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  5778. cntl |= CURSOR_MODE_DISABLE;
  5779. }
  5780. I915_WRITE(CURCNTR(pipe), cntl);
  5781. intel_crtc->cursor_visible = visible;
  5782. }
  5783. /* and commit changes on next vblank */
  5784. I915_WRITE(CURBASE(pipe), base);
  5785. }
  5786. static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
  5787. {
  5788. struct drm_device *dev = crtc->dev;
  5789. struct drm_i915_private *dev_priv = dev->dev_private;
  5790. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5791. int pipe = intel_crtc->pipe;
  5792. bool visible = base != 0;
  5793. if (intel_crtc->cursor_visible != visible) {
  5794. uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
  5795. if (base) {
  5796. cntl &= ~CURSOR_MODE;
  5797. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  5798. } else {
  5799. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  5800. cntl |= CURSOR_MODE_DISABLE;
  5801. }
  5802. I915_WRITE(CURCNTR_IVB(pipe), cntl);
  5803. intel_crtc->cursor_visible = visible;
  5804. }
  5805. /* and commit changes on next vblank */
  5806. I915_WRITE(CURBASE_IVB(pipe), base);
  5807. }
  5808. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  5809. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  5810. bool on)
  5811. {
  5812. struct drm_device *dev = crtc->dev;
  5813. struct drm_i915_private *dev_priv = dev->dev_private;
  5814. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5815. int pipe = intel_crtc->pipe;
  5816. int x = intel_crtc->cursor_x;
  5817. int y = intel_crtc->cursor_y;
  5818. u32 base, pos;
  5819. bool visible;
  5820. pos = 0;
  5821. if (on && crtc->enabled && crtc->fb) {
  5822. base = intel_crtc->cursor_addr;
  5823. if (x > (int) crtc->fb->width)
  5824. base = 0;
  5825. if (y > (int) crtc->fb->height)
  5826. base = 0;
  5827. } else
  5828. base = 0;
  5829. if (x < 0) {
  5830. if (x + intel_crtc->cursor_width < 0)
  5831. base = 0;
  5832. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  5833. x = -x;
  5834. }
  5835. pos |= x << CURSOR_X_SHIFT;
  5836. if (y < 0) {
  5837. if (y + intel_crtc->cursor_height < 0)
  5838. base = 0;
  5839. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  5840. y = -y;
  5841. }
  5842. pos |= y << CURSOR_Y_SHIFT;
  5843. visible = base != 0;
  5844. if (!visible && !intel_crtc->cursor_visible)
  5845. return;
  5846. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  5847. I915_WRITE(CURPOS_IVB(pipe), pos);
  5848. ivb_update_cursor(crtc, base);
  5849. } else {
  5850. I915_WRITE(CURPOS(pipe), pos);
  5851. if (IS_845G(dev) || IS_I865G(dev))
  5852. i845_update_cursor(crtc, base);
  5853. else
  5854. i9xx_update_cursor(crtc, base);
  5855. }
  5856. if (visible)
  5857. intel_mark_busy(dev, to_intel_framebuffer(crtc->fb)->obj);
  5858. }
  5859. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  5860. struct drm_file *file,
  5861. uint32_t handle,
  5862. uint32_t width, uint32_t height)
  5863. {
  5864. struct drm_device *dev = crtc->dev;
  5865. struct drm_i915_private *dev_priv = dev->dev_private;
  5866. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5867. struct drm_i915_gem_object *obj;
  5868. uint32_t addr;
  5869. int ret;
  5870. DRM_DEBUG_KMS("\n");
  5871. /* if we want to turn off the cursor ignore width and height */
  5872. if (!handle) {
  5873. DRM_DEBUG_KMS("cursor off\n");
  5874. addr = 0;
  5875. obj = NULL;
  5876. mutex_lock(&dev->struct_mutex);
  5877. goto finish;
  5878. }
  5879. /* Currently we only support 64x64 cursors */
  5880. if (width != 64 || height != 64) {
  5881. DRM_ERROR("we currently only support 64x64 cursors\n");
  5882. return -EINVAL;
  5883. }
  5884. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  5885. if (&obj->base == NULL)
  5886. return -ENOENT;
  5887. if (obj->base.size < width * height * 4) {
  5888. DRM_ERROR("buffer is to small\n");
  5889. ret = -ENOMEM;
  5890. goto fail;
  5891. }
  5892. /* we only need to pin inside GTT if cursor is non-phy */
  5893. mutex_lock(&dev->struct_mutex);
  5894. if (!dev_priv->info->cursor_needs_physical) {
  5895. if (obj->tiling_mode) {
  5896. DRM_ERROR("cursor cannot be tiled\n");
  5897. ret = -EINVAL;
  5898. goto fail_locked;
  5899. }
  5900. ret = i915_gem_object_pin_to_display_plane(obj, 0, NULL);
  5901. if (ret) {
  5902. DRM_ERROR("failed to move cursor bo into the GTT\n");
  5903. goto fail_locked;
  5904. }
  5905. ret = i915_gem_object_put_fence(obj);
  5906. if (ret) {
  5907. DRM_ERROR("failed to release fence for cursor");
  5908. goto fail_unpin;
  5909. }
  5910. addr = obj->gtt_offset;
  5911. } else {
  5912. int align = IS_I830(dev) ? 16 * 1024 : 256;
  5913. ret = i915_gem_attach_phys_object(dev, obj,
  5914. (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
  5915. align);
  5916. if (ret) {
  5917. DRM_ERROR("failed to attach phys object\n");
  5918. goto fail_locked;
  5919. }
  5920. addr = obj->phys_obj->handle->busaddr;
  5921. }
  5922. if (IS_GEN2(dev))
  5923. I915_WRITE(CURSIZE, (height << 12) | width);
  5924. finish:
  5925. if (intel_crtc->cursor_bo) {
  5926. if (dev_priv->info->cursor_needs_physical) {
  5927. if (intel_crtc->cursor_bo != obj)
  5928. i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
  5929. } else
  5930. i915_gem_object_unpin(intel_crtc->cursor_bo);
  5931. drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
  5932. }
  5933. mutex_unlock(&dev->struct_mutex);
  5934. intel_crtc->cursor_addr = addr;
  5935. intel_crtc->cursor_bo = obj;
  5936. intel_crtc->cursor_width = width;
  5937. intel_crtc->cursor_height = height;
  5938. intel_crtc_update_cursor(crtc, true);
  5939. return 0;
  5940. fail_unpin:
  5941. i915_gem_object_unpin(obj);
  5942. fail_locked:
  5943. mutex_unlock(&dev->struct_mutex);
  5944. fail:
  5945. drm_gem_object_unreference_unlocked(&obj->base);
  5946. return ret;
  5947. }
  5948. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  5949. {
  5950. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5951. intel_crtc->cursor_x = x;
  5952. intel_crtc->cursor_y = y;
  5953. intel_crtc_update_cursor(crtc, true);
  5954. return 0;
  5955. }
  5956. /** Sets the color ramps on behalf of RandR */
  5957. void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  5958. u16 blue, int regno)
  5959. {
  5960. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5961. intel_crtc->lut_r[regno] = red >> 8;
  5962. intel_crtc->lut_g[regno] = green >> 8;
  5963. intel_crtc->lut_b[regno] = blue >> 8;
  5964. }
  5965. void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  5966. u16 *blue, int regno)
  5967. {
  5968. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5969. *red = intel_crtc->lut_r[regno] << 8;
  5970. *green = intel_crtc->lut_g[regno] << 8;
  5971. *blue = intel_crtc->lut_b[regno] << 8;
  5972. }
  5973. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  5974. u16 *blue, uint32_t start, uint32_t size)
  5975. {
  5976. int end = (start + size > 256) ? 256 : start + size, i;
  5977. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5978. for (i = start; i < end; i++) {
  5979. intel_crtc->lut_r[i] = red[i] >> 8;
  5980. intel_crtc->lut_g[i] = green[i] >> 8;
  5981. intel_crtc->lut_b[i] = blue[i] >> 8;
  5982. }
  5983. intel_crtc_load_lut(crtc);
  5984. }
  5985. /**
  5986. * Get a pipe with a simple mode set on it for doing load-based monitor
  5987. * detection.
  5988. *
  5989. * It will be up to the load-detect code to adjust the pipe as appropriate for
  5990. * its requirements. The pipe will be connected to no other encoders.
  5991. *
  5992. * Currently this code will only succeed if there is a pipe with no encoders
  5993. * configured for it. In the future, it could choose to temporarily disable
  5994. * some outputs to free up a pipe for its use.
  5995. *
  5996. * \return crtc, or NULL if no pipes are available.
  5997. */
  5998. /* VESA 640x480x72Hz mode to set on the pipe */
  5999. static struct drm_display_mode load_detect_mode = {
  6000. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  6001. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  6002. };
  6003. static struct drm_framebuffer *
  6004. intel_framebuffer_create(struct drm_device *dev,
  6005. struct drm_mode_fb_cmd2 *mode_cmd,
  6006. struct drm_i915_gem_object *obj)
  6007. {
  6008. struct intel_framebuffer *intel_fb;
  6009. int ret;
  6010. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6011. if (!intel_fb) {
  6012. drm_gem_object_unreference_unlocked(&obj->base);
  6013. return ERR_PTR(-ENOMEM);
  6014. }
  6015. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  6016. if (ret) {
  6017. drm_gem_object_unreference_unlocked(&obj->base);
  6018. kfree(intel_fb);
  6019. return ERR_PTR(ret);
  6020. }
  6021. return &intel_fb->base;
  6022. }
  6023. static u32
  6024. intel_framebuffer_pitch_for_width(int width, int bpp)
  6025. {
  6026. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  6027. return ALIGN(pitch, 64);
  6028. }
  6029. static u32
  6030. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  6031. {
  6032. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  6033. return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
  6034. }
  6035. static struct drm_framebuffer *
  6036. intel_framebuffer_create_for_mode(struct drm_device *dev,
  6037. struct drm_display_mode *mode,
  6038. int depth, int bpp)
  6039. {
  6040. struct drm_i915_gem_object *obj;
  6041. struct drm_mode_fb_cmd2 mode_cmd;
  6042. obj = i915_gem_alloc_object(dev,
  6043. intel_framebuffer_size_for_mode(mode, bpp));
  6044. if (obj == NULL)
  6045. return ERR_PTR(-ENOMEM);
  6046. mode_cmd.width = mode->hdisplay;
  6047. mode_cmd.height = mode->vdisplay;
  6048. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  6049. bpp);
  6050. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  6051. return intel_framebuffer_create(dev, &mode_cmd, obj);
  6052. }
  6053. static struct drm_framebuffer *
  6054. mode_fits_in_fbdev(struct drm_device *dev,
  6055. struct drm_display_mode *mode)
  6056. {
  6057. struct drm_i915_private *dev_priv = dev->dev_private;
  6058. struct drm_i915_gem_object *obj;
  6059. struct drm_framebuffer *fb;
  6060. if (dev_priv->fbdev == NULL)
  6061. return NULL;
  6062. obj = dev_priv->fbdev->ifb.obj;
  6063. if (obj == NULL)
  6064. return NULL;
  6065. fb = &dev_priv->fbdev->ifb.base;
  6066. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  6067. fb->bits_per_pixel))
  6068. return NULL;
  6069. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  6070. return NULL;
  6071. return fb;
  6072. }
  6073. bool intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,
  6074. struct drm_connector *connector,
  6075. struct drm_display_mode *mode,
  6076. struct intel_load_detect_pipe *old)
  6077. {
  6078. struct intel_crtc *intel_crtc;
  6079. struct drm_crtc *possible_crtc;
  6080. struct drm_encoder *encoder = &intel_encoder->base;
  6081. struct drm_crtc *crtc = NULL;
  6082. struct drm_device *dev = encoder->dev;
  6083. struct drm_framebuffer *old_fb;
  6084. int i = -1;
  6085. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  6086. connector->base.id, drm_get_connector_name(connector),
  6087. encoder->base.id, drm_get_encoder_name(encoder));
  6088. /*
  6089. * Algorithm gets a little messy:
  6090. *
  6091. * - if the connector already has an assigned crtc, use it (but make
  6092. * sure it's on first)
  6093. *
  6094. * - try to find the first unused crtc that can drive this connector,
  6095. * and use that if we find one
  6096. */
  6097. /* See if we already have a CRTC for this connector */
  6098. if (encoder->crtc) {
  6099. crtc = encoder->crtc;
  6100. intel_crtc = to_intel_crtc(crtc);
  6101. old->dpms_mode = intel_crtc->dpms_mode;
  6102. old->load_detect_temp = false;
  6103. /* Make sure the crtc and connector are running */
  6104. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  6105. struct drm_encoder_helper_funcs *encoder_funcs;
  6106. struct drm_crtc_helper_funcs *crtc_funcs;
  6107. crtc_funcs = crtc->helper_private;
  6108. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  6109. encoder_funcs = encoder->helper_private;
  6110. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  6111. }
  6112. return true;
  6113. }
  6114. /* Find an unused one (if possible) */
  6115. list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
  6116. i++;
  6117. if (!(encoder->possible_crtcs & (1 << i)))
  6118. continue;
  6119. if (!possible_crtc->enabled) {
  6120. crtc = possible_crtc;
  6121. break;
  6122. }
  6123. }
  6124. /*
  6125. * If we didn't find an unused CRTC, don't use any.
  6126. */
  6127. if (!crtc) {
  6128. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  6129. return false;
  6130. }
  6131. encoder->crtc = crtc;
  6132. connector->encoder = encoder;
  6133. intel_crtc = to_intel_crtc(crtc);
  6134. old->dpms_mode = intel_crtc->dpms_mode;
  6135. old->load_detect_temp = true;
  6136. old->release_fb = NULL;
  6137. if (!mode)
  6138. mode = &load_detect_mode;
  6139. old_fb = crtc->fb;
  6140. /* We need a framebuffer large enough to accommodate all accesses
  6141. * that the plane may generate whilst we perform load detection.
  6142. * We can not rely on the fbcon either being present (we get called
  6143. * during its initialisation to detect all boot displays, or it may
  6144. * not even exist) or that it is large enough to satisfy the
  6145. * requested mode.
  6146. */
  6147. crtc->fb = mode_fits_in_fbdev(dev, mode);
  6148. if (crtc->fb == NULL) {
  6149. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  6150. crtc->fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  6151. old->release_fb = crtc->fb;
  6152. } else
  6153. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  6154. if (IS_ERR(crtc->fb)) {
  6155. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  6156. crtc->fb = old_fb;
  6157. return false;
  6158. }
  6159. if (!drm_crtc_helper_set_mode(crtc, mode, 0, 0, old_fb)) {
  6160. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  6161. if (old->release_fb)
  6162. old->release_fb->funcs->destroy(old->release_fb);
  6163. crtc->fb = old_fb;
  6164. return false;
  6165. }
  6166. /* let the connector get through one full cycle before testing */
  6167. intel_wait_for_vblank(dev, intel_crtc->pipe);
  6168. return true;
  6169. }
  6170. void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,
  6171. struct drm_connector *connector,
  6172. struct intel_load_detect_pipe *old)
  6173. {
  6174. struct drm_encoder *encoder = &intel_encoder->base;
  6175. struct drm_device *dev = encoder->dev;
  6176. struct drm_crtc *crtc = encoder->crtc;
  6177. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  6178. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  6179. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  6180. connector->base.id, drm_get_connector_name(connector),
  6181. encoder->base.id, drm_get_encoder_name(encoder));
  6182. if (old->load_detect_temp) {
  6183. connector->encoder = NULL;
  6184. drm_helper_disable_unused_functions(dev);
  6185. if (old->release_fb)
  6186. old->release_fb->funcs->destroy(old->release_fb);
  6187. return;
  6188. }
  6189. /* Switch crtc and encoder back off if necessary */
  6190. if (old->dpms_mode != DRM_MODE_DPMS_ON) {
  6191. encoder_funcs->dpms(encoder, old->dpms_mode);
  6192. crtc_funcs->dpms(crtc, old->dpms_mode);
  6193. }
  6194. }
  6195. /* Returns the clock of the currently programmed mode of the given pipe. */
  6196. static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
  6197. {
  6198. struct drm_i915_private *dev_priv = dev->dev_private;
  6199. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6200. int pipe = intel_crtc->pipe;
  6201. u32 dpll = I915_READ(DPLL(pipe));
  6202. u32 fp;
  6203. intel_clock_t clock;
  6204. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  6205. fp = I915_READ(FP0(pipe));
  6206. else
  6207. fp = I915_READ(FP1(pipe));
  6208. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  6209. if (IS_PINEVIEW(dev)) {
  6210. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  6211. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  6212. } else {
  6213. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  6214. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  6215. }
  6216. if (!IS_GEN2(dev)) {
  6217. if (IS_PINEVIEW(dev))
  6218. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  6219. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  6220. else
  6221. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  6222. DPLL_FPA01_P1_POST_DIV_SHIFT);
  6223. switch (dpll & DPLL_MODE_MASK) {
  6224. case DPLLB_MODE_DAC_SERIAL:
  6225. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  6226. 5 : 10;
  6227. break;
  6228. case DPLLB_MODE_LVDS:
  6229. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  6230. 7 : 14;
  6231. break;
  6232. default:
  6233. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  6234. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  6235. return 0;
  6236. }
  6237. /* XXX: Handle the 100Mhz refclk */
  6238. intel_clock(dev, 96000, &clock);
  6239. } else {
  6240. bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
  6241. if (is_lvds) {
  6242. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  6243. DPLL_FPA01_P1_POST_DIV_SHIFT);
  6244. clock.p2 = 14;
  6245. if ((dpll & PLL_REF_INPUT_MASK) ==
  6246. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  6247. /* XXX: might not be 66MHz */
  6248. intel_clock(dev, 66000, &clock);
  6249. } else
  6250. intel_clock(dev, 48000, &clock);
  6251. } else {
  6252. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  6253. clock.p1 = 2;
  6254. else {
  6255. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  6256. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  6257. }
  6258. if (dpll & PLL_P2_DIVIDE_BY_4)
  6259. clock.p2 = 4;
  6260. else
  6261. clock.p2 = 2;
  6262. intel_clock(dev, 48000, &clock);
  6263. }
  6264. }
  6265. /* XXX: It would be nice to validate the clocks, but we can't reuse
  6266. * i830PllIsValid() because it relies on the xf86_config connector
  6267. * configuration being accurate, which it isn't necessarily.
  6268. */
  6269. return clock.dot;
  6270. }
  6271. /** Returns the currently programmed mode of the given pipe. */
  6272. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  6273. struct drm_crtc *crtc)
  6274. {
  6275. struct drm_i915_private *dev_priv = dev->dev_private;
  6276. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6277. int pipe = intel_crtc->pipe;
  6278. struct drm_display_mode *mode;
  6279. int htot = I915_READ(HTOTAL(pipe));
  6280. int hsync = I915_READ(HSYNC(pipe));
  6281. int vtot = I915_READ(VTOTAL(pipe));
  6282. int vsync = I915_READ(VSYNC(pipe));
  6283. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  6284. if (!mode)
  6285. return NULL;
  6286. mode->clock = intel_crtc_clock_get(dev, crtc);
  6287. mode->hdisplay = (htot & 0xffff) + 1;
  6288. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  6289. mode->hsync_start = (hsync & 0xffff) + 1;
  6290. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  6291. mode->vdisplay = (vtot & 0xffff) + 1;
  6292. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  6293. mode->vsync_start = (vsync & 0xffff) + 1;
  6294. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  6295. drm_mode_set_name(mode);
  6296. drm_mode_set_crtcinfo(mode, 0);
  6297. return mode;
  6298. }
  6299. #define GPU_IDLE_TIMEOUT 500 /* ms */
  6300. /* When this timer fires, we've been idle for awhile */
  6301. static void intel_gpu_idle_timer(unsigned long arg)
  6302. {
  6303. struct drm_device *dev = (struct drm_device *)arg;
  6304. drm_i915_private_t *dev_priv = dev->dev_private;
  6305. if (!list_empty(&dev_priv->mm.active_list)) {
  6306. /* Still processing requests, so just re-arm the timer. */
  6307. mod_timer(&dev_priv->idle_timer, jiffies +
  6308. msecs_to_jiffies(GPU_IDLE_TIMEOUT));
  6309. return;
  6310. }
  6311. dev_priv->busy = false;
  6312. queue_work(dev_priv->wq, &dev_priv->idle_work);
  6313. }
  6314. #define CRTC_IDLE_TIMEOUT 1000 /* ms */
  6315. static void intel_crtc_idle_timer(unsigned long arg)
  6316. {
  6317. struct intel_crtc *intel_crtc = (struct intel_crtc *)arg;
  6318. struct drm_crtc *crtc = &intel_crtc->base;
  6319. drm_i915_private_t *dev_priv = crtc->dev->dev_private;
  6320. struct intel_framebuffer *intel_fb;
  6321. intel_fb = to_intel_framebuffer(crtc->fb);
  6322. if (intel_fb && intel_fb->obj->active) {
  6323. /* The framebuffer is still being accessed by the GPU. */
  6324. mod_timer(&intel_crtc->idle_timer, jiffies +
  6325. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  6326. return;
  6327. }
  6328. intel_crtc->busy = false;
  6329. queue_work(dev_priv->wq, &dev_priv->idle_work);
  6330. }
  6331. static void intel_increase_pllclock(struct drm_crtc *crtc)
  6332. {
  6333. struct drm_device *dev = crtc->dev;
  6334. drm_i915_private_t *dev_priv = dev->dev_private;
  6335. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6336. int pipe = intel_crtc->pipe;
  6337. int dpll_reg = DPLL(pipe);
  6338. int dpll;
  6339. if (HAS_PCH_SPLIT(dev))
  6340. return;
  6341. if (!dev_priv->lvds_downclock_avail)
  6342. return;
  6343. dpll = I915_READ(dpll_reg);
  6344. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  6345. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  6346. assert_panel_unlocked(dev_priv, pipe);
  6347. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  6348. I915_WRITE(dpll_reg, dpll);
  6349. intel_wait_for_vblank(dev, pipe);
  6350. dpll = I915_READ(dpll_reg);
  6351. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  6352. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  6353. }
  6354. /* Schedule downclock */
  6355. mod_timer(&intel_crtc->idle_timer, jiffies +
  6356. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  6357. }
  6358. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  6359. {
  6360. struct drm_device *dev = crtc->dev;
  6361. drm_i915_private_t *dev_priv = dev->dev_private;
  6362. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6363. int pipe = intel_crtc->pipe;
  6364. int dpll_reg = DPLL(pipe);
  6365. int dpll = I915_READ(dpll_reg);
  6366. if (HAS_PCH_SPLIT(dev))
  6367. return;
  6368. if (!dev_priv->lvds_downclock_avail)
  6369. return;
  6370. /*
  6371. * Since this is called by a timer, we should never get here in
  6372. * the manual case.
  6373. */
  6374. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  6375. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  6376. assert_panel_unlocked(dev_priv, pipe);
  6377. dpll |= DISPLAY_RATE_SELECT_FPA1;
  6378. I915_WRITE(dpll_reg, dpll);
  6379. intel_wait_for_vblank(dev, pipe);
  6380. dpll = I915_READ(dpll_reg);
  6381. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  6382. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  6383. }
  6384. }
  6385. /**
  6386. * intel_idle_update - adjust clocks for idleness
  6387. * @work: work struct
  6388. *
  6389. * Either the GPU or display (or both) went idle. Check the busy status
  6390. * here and adjust the CRTC and GPU clocks as necessary.
  6391. */
  6392. static void intel_idle_update(struct work_struct *work)
  6393. {
  6394. drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
  6395. idle_work);
  6396. struct drm_device *dev = dev_priv->dev;
  6397. struct drm_crtc *crtc;
  6398. struct intel_crtc *intel_crtc;
  6399. if (!i915_powersave)
  6400. return;
  6401. mutex_lock(&dev->struct_mutex);
  6402. i915_update_gfx_val(dev_priv);
  6403. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  6404. /* Skip inactive CRTCs */
  6405. if (!crtc->fb)
  6406. continue;
  6407. intel_crtc = to_intel_crtc(crtc);
  6408. if (!intel_crtc->busy)
  6409. intel_decrease_pllclock(crtc);
  6410. }
  6411. mutex_unlock(&dev->struct_mutex);
  6412. }
  6413. /**
  6414. * intel_mark_busy - mark the GPU and possibly the display busy
  6415. * @dev: drm device
  6416. * @obj: object we're operating on
  6417. *
  6418. * Callers can use this function to indicate that the GPU is busy processing
  6419. * commands. If @obj matches one of the CRTC objects (i.e. it's a scanout
  6420. * buffer), we'll also mark the display as busy, so we know to increase its
  6421. * clock frequency.
  6422. */
  6423. void intel_mark_busy(struct drm_device *dev, struct drm_i915_gem_object *obj)
  6424. {
  6425. drm_i915_private_t *dev_priv = dev->dev_private;
  6426. struct drm_crtc *crtc = NULL;
  6427. struct intel_framebuffer *intel_fb;
  6428. struct intel_crtc *intel_crtc;
  6429. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  6430. return;
  6431. if (!dev_priv->busy)
  6432. dev_priv->busy = true;
  6433. else
  6434. mod_timer(&dev_priv->idle_timer, jiffies +
  6435. msecs_to_jiffies(GPU_IDLE_TIMEOUT));
  6436. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  6437. if (!crtc->fb)
  6438. continue;
  6439. intel_crtc = to_intel_crtc(crtc);
  6440. intel_fb = to_intel_framebuffer(crtc->fb);
  6441. if (intel_fb->obj == obj) {
  6442. if (!intel_crtc->busy) {
  6443. /* Non-busy -> busy, upclock */
  6444. intel_increase_pllclock(crtc);
  6445. intel_crtc->busy = true;
  6446. } else {
  6447. /* Busy -> busy, put off timer */
  6448. mod_timer(&intel_crtc->idle_timer, jiffies +
  6449. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  6450. }
  6451. }
  6452. }
  6453. }
  6454. static void intel_crtc_destroy(struct drm_crtc *crtc)
  6455. {
  6456. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6457. struct drm_device *dev = crtc->dev;
  6458. struct intel_unpin_work *work;
  6459. unsigned long flags;
  6460. spin_lock_irqsave(&dev->event_lock, flags);
  6461. work = intel_crtc->unpin_work;
  6462. intel_crtc->unpin_work = NULL;
  6463. spin_unlock_irqrestore(&dev->event_lock, flags);
  6464. if (work) {
  6465. cancel_work_sync(&work->work);
  6466. kfree(work);
  6467. }
  6468. drm_crtc_cleanup(crtc);
  6469. kfree(intel_crtc);
  6470. }
  6471. static void intel_unpin_work_fn(struct work_struct *__work)
  6472. {
  6473. struct intel_unpin_work *work =
  6474. container_of(__work, struct intel_unpin_work, work);
  6475. mutex_lock(&work->dev->struct_mutex);
  6476. intel_unpin_fb_obj(work->old_fb_obj);
  6477. drm_gem_object_unreference(&work->pending_flip_obj->base);
  6478. drm_gem_object_unreference(&work->old_fb_obj->base);
  6479. intel_update_fbc(work->dev);
  6480. mutex_unlock(&work->dev->struct_mutex);
  6481. kfree(work);
  6482. }
  6483. static void do_intel_finish_page_flip(struct drm_device *dev,
  6484. struct drm_crtc *crtc)
  6485. {
  6486. drm_i915_private_t *dev_priv = dev->dev_private;
  6487. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6488. struct intel_unpin_work *work;
  6489. struct drm_i915_gem_object *obj;
  6490. struct drm_pending_vblank_event *e;
  6491. struct timeval tnow, tvbl;
  6492. unsigned long flags;
  6493. /* Ignore early vblank irqs */
  6494. if (intel_crtc == NULL)
  6495. return;
  6496. do_gettimeofday(&tnow);
  6497. spin_lock_irqsave(&dev->event_lock, flags);
  6498. work = intel_crtc->unpin_work;
  6499. if (work == NULL || !work->pending) {
  6500. spin_unlock_irqrestore(&dev->event_lock, flags);
  6501. return;
  6502. }
  6503. intel_crtc->unpin_work = NULL;
  6504. if (work->event) {
  6505. e = work->event;
  6506. e->event.sequence = drm_vblank_count_and_time(dev, intel_crtc->pipe, &tvbl);
  6507. /* Called before vblank count and timestamps have
  6508. * been updated for the vblank interval of flip
  6509. * completion? Need to increment vblank count and
  6510. * add one videorefresh duration to returned timestamp
  6511. * to account for this. We assume this happened if we
  6512. * get called over 0.9 frame durations after the last
  6513. * timestamped vblank.
  6514. *
  6515. * This calculation can not be used with vrefresh rates
  6516. * below 5Hz (10Hz to be on the safe side) without
  6517. * promoting to 64 integers.
  6518. */
  6519. if (10 * (timeval_to_ns(&tnow) - timeval_to_ns(&tvbl)) >
  6520. 9 * crtc->framedur_ns) {
  6521. e->event.sequence++;
  6522. tvbl = ns_to_timeval(timeval_to_ns(&tvbl) +
  6523. crtc->framedur_ns);
  6524. }
  6525. e->event.tv_sec = tvbl.tv_sec;
  6526. e->event.tv_usec = tvbl.tv_usec;
  6527. list_add_tail(&e->base.link,
  6528. &e->base.file_priv->event_list);
  6529. wake_up_interruptible(&e->base.file_priv->event_wait);
  6530. }
  6531. drm_vblank_put(dev, intel_crtc->pipe);
  6532. spin_unlock_irqrestore(&dev->event_lock, flags);
  6533. obj = work->old_fb_obj;
  6534. atomic_clear_mask(1 << intel_crtc->plane,
  6535. &obj->pending_flip.counter);
  6536. if (atomic_read(&obj->pending_flip) == 0)
  6537. wake_up(&dev_priv->pending_flip_queue);
  6538. schedule_work(&work->work);
  6539. trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
  6540. }
  6541. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  6542. {
  6543. drm_i915_private_t *dev_priv = dev->dev_private;
  6544. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  6545. do_intel_finish_page_flip(dev, crtc);
  6546. }
  6547. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  6548. {
  6549. drm_i915_private_t *dev_priv = dev->dev_private;
  6550. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  6551. do_intel_finish_page_flip(dev, crtc);
  6552. }
  6553. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  6554. {
  6555. drm_i915_private_t *dev_priv = dev->dev_private;
  6556. struct intel_crtc *intel_crtc =
  6557. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  6558. unsigned long flags;
  6559. spin_lock_irqsave(&dev->event_lock, flags);
  6560. if (intel_crtc->unpin_work) {
  6561. if ((++intel_crtc->unpin_work->pending) > 1)
  6562. DRM_ERROR("Prepared flip multiple times\n");
  6563. } else {
  6564. DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
  6565. }
  6566. spin_unlock_irqrestore(&dev->event_lock, flags);
  6567. }
  6568. static int intel_gen2_queue_flip(struct drm_device *dev,
  6569. struct drm_crtc *crtc,
  6570. struct drm_framebuffer *fb,
  6571. struct drm_i915_gem_object *obj)
  6572. {
  6573. struct drm_i915_private *dev_priv = dev->dev_private;
  6574. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6575. unsigned long offset;
  6576. u32 flip_mask;
  6577. int ret;
  6578. ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
  6579. if (ret)
  6580. goto err;
  6581. /* Offset into the new buffer for cases of shared fbs between CRTCs */
  6582. offset = crtc->y * fb->pitches[0] + crtc->x * fb->bits_per_pixel/8;
  6583. ret = BEGIN_LP_RING(6);
  6584. if (ret)
  6585. goto err_unpin;
  6586. /* Can't queue multiple flips, so wait for the previous
  6587. * one to finish before executing the next.
  6588. */
  6589. if (intel_crtc->plane)
  6590. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  6591. else
  6592. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  6593. OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
  6594. OUT_RING(MI_NOOP);
  6595. OUT_RING(MI_DISPLAY_FLIP |
  6596. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  6597. OUT_RING(fb->pitches[0]);
  6598. OUT_RING(obj->gtt_offset + offset);
  6599. OUT_RING(0); /* aux display base address, unused */
  6600. ADVANCE_LP_RING();
  6601. return 0;
  6602. err_unpin:
  6603. intel_unpin_fb_obj(obj);
  6604. err:
  6605. return ret;
  6606. }
  6607. static int intel_gen3_queue_flip(struct drm_device *dev,
  6608. struct drm_crtc *crtc,
  6609. struct drm_framebuffer *fb,
  6610. struct drm_i915_gem_object *obj)
  6611. {
  6612. struct drm_i915_private *dev_priv = dev->dev_private;
  6613. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6614. unsigned long offset;
  6615. u32 flip_mask;
  6616. int ret;
  6617. ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
  6618. if (ret)
  6619. goto err;
  6620. /* Offset into the new buffer for cases of shared fbs between CRTCs */
  6621. offset = crtc->y * fb->pitches[0] + crtc->x * fb->bits_per_pixel/8;
  6622. ret = BEGIN_LP_RING(6);
  6623. if (ret)
  6624. goto err_unpin;
  6625. if (intel_crtc->plane)
  6626. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  6627. else
  6628. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  6629. OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
  6630. OUT_RING(MI_NOOP);
  6631. OUT_RING(MI_DISPLAY_FLIP_I915 |
  6632. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  6633. OUT_RING(fb->pitches[0]);
  6634. OUT_RING(obj->gtt_offset + offset);
  6635. OUT_RING(MI_NOOP);
  6636. ADVANCE_LP_RING();
  6637. return 0;
  6638. err_unpin:
  6639. intel_unpin_fb_obj(obj);
  6640. err:
  6641. return ret;
  6642. }
  6643. static int intel_gen4_queue_flip(struct drm_device *dev,
  6644. struct drm_crtc *crtc,
  6645. struct drm_framebuffer *fb,
  6646. struct drm_i915_gem_object *obj)
  6647. {
  6648. struct drm_i915_private *dev_priv = dev->dev_private;
  6649. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6650. uint32_t pf, pipesrc;
  6651. int ret;
  6652. ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
  6653. if (ret)
  6654. goto err;
  6655. ret = BEGIN_LP_RING(4);
  6656. if (ret)
  6657. goto err_unpin;
  6658. /* i965+ uses the linear or tiled offsets from the
  6659. * Display Registers (which do not change across a page-flip)
  6660. * so we need only reprogram the base address.
  6661. */
  6662. OUT_RING(MI_DISPLAY_FLIP |
  6663. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  6664. OUT_RING(fb->pitches[0]);
  6665. OUT_RING(obj->gtt_offset | obj->tiling_mode);
  6666. /* XXX Enabling the panel-fitter across page-flip is so far
  6667. * untested on non-native modes, so ignore it for now.
  6668. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  6669. */
  6670. pf = 0;
  6671. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  6672. OUT_RING(pf | pipesrc);
  6673. ADVANCE_LP_RING();
  6674. return 0;
  6675. err_unpin:
  6676. intel_unpin_fb_obj(obj);
  6677. err:
  6678. return ret;
  6679. }
  6680. static int intel_gen6_queue_flip(struct drm_device *dev,
  6681. struct drm_crtc *crtc,
  6682. struct drm_framebuffer *fb,
  6683. struct drm_i915_gem_object *obj)
  6684. {
  6685. struct drm_i915_private *dev_priv = dev->dev_private;
  6686. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6687. uint32_t pf, pipesrc;
  6688. int ret;
  6689. ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
  6690. if (ret)
  6691. goto err;
  6692. ret = BEGIN_LP_RING(4);
  6693. if (ret)
  6694. goto err_unpin;
  6695. OUT_RING(MI_DISPLAY_FLIP |
  6696. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  6697. OUT_RING(fb->pitches[0] | obj->tiling_mode);
  6698. OUT_RING(obj->gtt_offset);
  6699. pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  6700. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  6701. OUT_RING(pf | pipesrc);
  6702. ADVANCE_LP_RING();
  6703. return 0;
  6704. err_unpin:
  6705. intel_unpin_fb_obj(obj);
  6706. err:
  6707. return ret;
  6708. }
  6709. /*
  6710. * On gen7 we currently use the blit ring because (in early silicon at least)
  6711. * the render ring doesn't give us interrpts for page flip completion, which
  6712. * means clients will hang after the first flip is queued. Fortunately the
  6713. * blit ring generates interrupts properly, so use it instead.
  6714. */
  6715. static int intel_gen7_queue_flip(struct drm_device *dev,
  6716. struct drm_crtc *crtc,
  6717. struct drm_framebuffer *fb,
  6718. struct drm_i915_gem_object *obj)
  6719. {
  6720. struct drm_i915_private *dev_priv = dev->dev_private;
  6721. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6722. struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
  6723. int ret;
  6724. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  6725. if (ret)
  6726. goto err;
  6727. ret = intel_ring_begin(ring, 4);
  6728. if (ret)
  6729. goto err_unpin;
  6730. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | (intel_crtc->plane << 19));
  6731. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  6732. intel_ring_emit(ring, (obj->gtt_offset));
  6733. intel_ring_emit(ring, (MI_NOOP));
  6734. intel_ring_advance(ring);
  6735. return 0;
  6736. err_unpin:
  6737. intel_unpin_fb_obj(obj);
  6738. err:
  6739. return ret;
  6740. }
  6741. static int intel_default_queue_flip(struct drm_device *dev,
  6742. struct drm_crtc *crtc,
  6743. struct drm_framebuffer *fb,
  6744. struct drm_i915_gem_object *obj)
  6745. {
  6746. return -ENODEV;
  6747. }
  6748. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  6749. struct drm_framebuffer *fb,
  6750. struct drm_pending_vblank_event *event)
  6751. {
  6752. struct drm_device *dev = crtc->dev;
  6753. struct drm_i915_private *dev_priv = dev->dev_private;
  6754. struct intel_framebuffer *intel_fb;
  6755. struct drm_i915_gem_object *obj;
  6756. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6757. struct intel_unpin_work *work;
  6758. unsigned long flags;
  6759. int ret;
  6760. work = kzalloc(sizeof *work, GFP_KERNEL);
  6761. if (work == NULL)
  6762. return -ENOMEM;
  6763. work->event = event;
  6764. work->dev = crtc->dev;
  6765. intel_fb = to_intel_framebuffer(crtc->fb);
  6766. work->old_fb_obj = intel_fb->obj;
  6767. INIT_WORK(&work->work, intel_unpin_work_fn);
  6768. ret = drm_vblank_get(dev, intel_crtc->pipe);
  6769. if (ret)
  6770. goto free_work;
  6771. /* We borrow the event spin lock for protecting unpin_work */
  6772. spin_lock_irqsave(&dev->event_lock, flags);
  6773. if (intel_crtc->unpin_work) {
  6774. spin_unlock_irqrestore(&dev->event_lock, flags);
  6775. kfree(work);
  6776. drm_vblank_put(dev, intel_crtc->pipe);
  6777. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  6778. return -EBUSY;
  6779. }
  6780. intel_crtc->unpin_work = work;
  6781. spin_unlock_irqrestore(&dev->event_lock, flags);
  6782. intel_fb = to_intel_framebuffer(fb);
  6783. obj = intel_fb->obj;
  6784. mutex_lock(&dev->struct_mutex);
  6785. /* Reference the objects for the scheduled work. */
  6786. drm_gem_object_reference(&work->old_fb_obj->base);
  6787. drm_gem_object_reference(&obj->base);
  6788. crtc->fb = fb;
  6789. work->pending_flip_obj = obj;
  6790. work->enable_stall_check = true;
  6791. /* Block clients from rendering to the new back buffer until
  6792. * the flip occurs and the object is no longer visible.
  6793. */
  6794. atomic_add(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
  6795. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
  6796. if (ret)
  6797. goto cleanup_pending;
  6798. intel_disable_fbc(dev);
  6799. mutex_unlock(&dev->struct_mutex);
  6800. trace_i915_flip_request(intel_crtc->plane, obj);
  6801. return 0;
  6802. cleanup_pending:
  6803. atomic_sub(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
  6804. drm_gem_object_unreference(&work->old_fb_obj->base);
  6805. drm_gem_object_unreference(&obj->base);
  6806. mutex_unlock(&dev->struct_mutex);
  6807. spin_lock_irqsave(&dev->event_lock, flags);
  6808. intel_crtc->unpin_work = NULL;
  6809. spin_unlock_irqrestore(&dev->event_lock, flags);
  6810. drm_vblank_put(dev, intel_crtc->pipe);
  6811. free_work:
  6812. kfree(work);
  6813. return ret;
  6814. }
  6815. static void intel_sanitize_modesetting(struct drm_device *dev,
  6816. int pipe, int plane)
  6817. {
  6818. struct drm_i915_private *dev_priv = dev->dev_private;
  6819. u32 reg, val;
  6820. /* Clear any frame start delays used for debugging left by the BIOS */
  6821. for_each_pipe(pipe) {
  6822. reg = PIPECONF(pipe);
  6823. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  6824. }
  6825. if (HAS_PCH_SPLIT(dev))
  6826. return;
  6827. /* Who knows what state these registers were left in by the BIOS or
  6828. * grub?
  6829. *
  6830. * If we leave the registers in a conflicting state (e.g. with the
  6831. * display plane reading from the other pipe than the one we intend
  6832. * to use) then when we attempt to teardown the active mode, we will
  6833. * not disable the pipes and planes in the correct order -- leaving
  6834. * a plane reading from a disabled pipe and possibly leading to
  6835. * undefined behaviour.
  6836. */
  6837. reg = DSPCNTR(plane);
  6838. val = I915_READ(reg);
  6839. if ((val & DISPLAY_PLANE_ENABLE) == 0)
  6840. return;
  6841. if (!!(val & DISPPLANE_SEL_PIPE_MASK) == pipe)
  6842. return;
  6843. /* This display plane is active and attached to the other CPU pipe. */
  6844. pipe = !pipe;
  6845. /* Disable the plane and wait for it to stop reading from the pipe. */
  6846. intel_disable_plane(dev_priv, plane, pipe);
  6847. intel_disable_pipe(dev_priv, pipe);
  6848. }
  6849. static void intel_crtc_reset(struct drm_crtc *crtc)
  6850. {
  6851. struct drm_device *dev = crtc->dev;
  6852. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6853. /* Reset flags back to the 'unknown' status so that they
  6854. * will be correctly set on the initial modeset.
  6855. */
  6856. intel_crtc->dpms_mode = -1;
  6857. /* We need to fix up any BIOS configuration that conflicts with
  6858. * our expectations.
  6859. */
  6860. intel_sanitize_modesetting(dev, intel_crtc->pipe, intel_crtc->plane);
  6861. }
  6862. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  6863. .dpms = intel_crtc_dpms,
  6864. .mode_fixup = intel_crtc_mode_fixup,
  6865. .mode_set = intel_crtc_mode_set,
  6866. .mode_set_base = intel_pipe_set_base,
  6867. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  6868. .load_lut = intel_crtc_load_lut,
  6869. .disable = intel_crtc_disable,
  6870. };
  6871. static const struct drm_crtc_funcs intel_crtc_funcs = {
  6872. .reset = intel_crtc_reset,
  6873. .cursor_set = intel_crtc_cursor_set,
  6874. .cursor_move = intel_crtc_cursor_move,
  6875. .gamma_set = intel_crtc_gamma_set,
  6876. .set_config = drm_crtc_helper_set_config,
  6877. .destroy = intel_crtc_destroy,
  6878. .page_flip = intel_crtc_page_flip,
  6879. };
  6880. static void intel_crtc_init(struct drm_device *dev, int pipe)
  6881. {
  6882. drm_i915_private_t *dev_priv = dev->dev_private;
  6883. struct intel_crtc *intel_crtc;
  6884. int i;
  6885. intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  6886. if (intel_crtc == NULL)
  6887. return;
  6888. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  6889. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  6890. for (i = 0; i < 256; i++) {
  6891. intel_crtc->lut_r[i] = i;
  6892. intel_crtc->lut_g[i] = i;
  6893. intel_crtc->lut_b[i] = i;
  6894. }
  6895. /* Swap pipes & planes for FBC on pre-965 */
  6896. intel_crtc->pipe = pipe;
  6897. intel_crtc->plane = pipe;
  6898. if (IS_MOBILE(dev) && IS_GEN3(dev)) {
  6899. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  6900. intel_crtc->plane = !pipe;
  6901. }
  6902. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  6903. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  6904. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  6905. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  6906. intel_crtc_reset(&intel_crtc->base);
  6907. intel_crtc->active = true; /* force the pipe off on setup_init_config */
  6908. intel_crtc->bpp = 24; /* default for pre-Ironlake */
  6909. if (HAS_PCH_SPLIT(dev)) {
  6910. if (pipe == 2 && IS_IVYBRIDGE(dev))
  6911. intel_crtc->no_pll = true;
  6912. intel_helper_funcs.prepare = ironlake_crtc_prepare;
  6913. intel_helper_funcs.commit = ironlake_crtc_commit;
  6914. } else {
  6915. intel_helper_funcs.prepare = i9xx_crtc_prepare;
  6916. intel_helper_funcs.commit = i9xx_crtc_commit;
  6917. }
  6918. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  6919. intel_crtc->busy = false;
  6920. setup_timer(&intel_crtc->idle_timer, intel_crtc_idle_timer,
  6921. (unsigned long)intel_crtc);
  6922. }
  6923. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  6924. struct drm_file *file)
  6925. {
  6926. drm_i915_private_t *dev_priv = dev->dev_private;
  6927. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  6928. struct drm_mode_object *drmmode_obj;
  6929. struct intel_crtc *crtc;
  6930. if (!dev_priv) {
  6931. DRM_ERROR("called with no initialization\n");
  6932. return -EINVAL;
  6933. }
  6934. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  6935. DRM_MODE_OBJECT_CRTC);
  6936. if (!drmmode_obj) {
  6937. DRM_ERROR("no such CRTC id\n");
  6938. return -EINVAL;
  6939. }
  6940. crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
  6941. pipe_from_crtc_id->pipe = crtc->pipe;
  6942. return 0;
  6943. }
  6944. static int intel_encoder_clones(struct drm_device *dev, int type_mask)
  6945. {
  6946. struct intel_encoder *encoder;
  6947. int index_mask = 0;
  6948. int entry = 0;
  6949. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  6950. if (type_mask & encoder->clone_mask)
  6951. index_mask |= (1 << entry);
  6952. entry++;
  6953. }
  6954. return index_mask;
  6955. }
  6956. static bool has_edp_a(struct drm_device *dev)
  6957. {
  6958. struct drm_i915_private *dev_priv = dev->dev_private;
  6959. if (!IS_MOBILE(dev))
  6960. return false;
  6961. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  6962. return false;
  6963. if (IS_GEN5(dev) &&
  6964. (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
  6965. return false;
  6966. return true;
  6967. }
  6968. static void intel_setup_outputs(struct drm_device *dev)
  6969. {
  6970. struct drm_i915_private *dev_priv = dev->dev_private;
  6971. struct intel_encoder *encoder;
  6972. bool dpd_is_edp = false;
  6973. bool has_lvds;
  6974. has_lvds = intel_lvds_init(dev);
  6975. if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
  6976. /* disable the panel fitter on everything but LVDS */
  6977. I915_WRITE(PFIT_CONTROL, 0);
  6978. }
  6979. if (HAS_PCH_SPLIT(dev)) {
  6980. dpd_is_edp = intel_dpd_is_edp(dev);
  6981. if (has_edp_a(dev))
  6982. intel_dp_init(dev, DP_A);
  6983. if (dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  6984. intel_dp_init(dev, PCH_DP_D);
  6985. }
  6986. intel_crt_init(dev);
  6987. if (HAS_PCH_SPLIT(dev)) {
  6988. int found;
  6989. if (I915_READ(HDMIB) & PORT_DETECTED) {
  6990. /* PCH SDVOB multiplex with HDMIB */
  6991. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  6992. if (!found)
  6993. intel_hdmi_init(dev, HDMIB);
  6994. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  6995. intel_dp_init(dev, PCH_DP_B);
  6996. }
  6997. if (I915_READ(HDMIC) & PORT_DETECTED)
  6998. intel_hdmi_init(dev, HDMIC);
  6999. if (I915_READ(HDMID) & PORT_DETECTED)
  7000. intel_hdmi_init(dev, HDMID);
  7001. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  7002. intel_dp_init(dev, PCH_DP_C);
  7003. if (!dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  7004. intel_dp_init(dev, PCH_DP_D);
  7005. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  7006. bool found = false;
  7007. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  7008. DRM_DEBUG_KMS("probing SDVOB\n");
  7009. found = intel_sdvo_init(dev, SDVOB, true);
  7010. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  7011. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  7012. intel_hdmi_init(dev, SDVOB);
  7013. }
  7014. if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
  7015. DRM_DEBUG_KMS("probing DP_B\n");
  7016. intel_dp_init(dev, DP_B);
  7017. }
  7018. }
  7019. /* Before G4X SDVOC doesn't have its own detect register */
  7020. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  7021. DRM_DEBUG_KMS("probing SDVOC\n");
  7022. found = intel_sdvo_init(dev, SDVOC, false);
  7023. }
  7024. if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
  7025. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  7026. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  7027. intel_hdmi_init(dev, SDVOC);
  7028. }
  7029. if (SUPPORTS_INTEGRATED_DP(dev)) {
  7030. DRM_DEBUG_KMS("probing DP_C\n");
  7031. intel_dp_init(dev, DP_C);
  7032. }
  7033. }
  7034. if (SUPPORTS_INTEGRATED_DP(dev) &&
  7035. (I915_READ(DP_D) & DP_DETECTED)) {
  7036. DRM_DEBUG_KMS("probing DP_D\n");
  7037. intel_dp_init(dev, DP_D);
  7038. }
  7039. } else if (IS_GEN2(dev))
  7040. intel_dvo_init(dev);
  7041. if (SUPPORTS_TV(dev))
  7042. intel_tv_init(dev);
  7043. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  7044. encoder->base.possible_crtcs = encoder->crtc_mask;
  7045. encoder->base.possible_clones =
  7046. intel_encoder_clones(dev, encoder->clone_mask);
  7047. }
  7048. /* disable all the possible outputs/crtcs before entering KMS mode */
  7049. drm_helper_disable_unused_functions(dev);
  7050. if (HAS_PCH_SPLIT(dev))
  7051. ironlake_init_pch_refclk(dev);
  7052. }
  7053. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  7054. {
  7055. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  7056. drm_framebuffer_cleanup(fb);
  7057. drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
  7058. kfree(intel_fb);
  7059. }
  7060. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  7061. struct drm_file *file,
  7062. unsigned int *handle)
  7063. {
  7064. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  7065. struct drm_i915_gem_object *obj = intel_fb->obj;
  7066. return drm_gem_handle_create(file, &obj->base, handle);
  7067. }
  7068. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  7069. .destroy = intel_user_framebuffer_destroy,
  7070. .create_handle = intel_user_framebuffer_create_handle,
  7071. };
  7072. int intel_framebuffer_init(struct drm_device *dev,
  7073. struct intel_framebuffer *intel_fb,
  7074. struct drm_mode_fb_cmd2 *mode_cmd,
  7075. struct drm_i915_gem_object *obj)
  7076. {
  7077. int ret;
  7078. if (obj->tiling_mode == I915_TILING_Y)
  7079. return -EINVAL;
  7080. if (mode_cmd->pitches[0] & 63)
  7081. return -EINVAL;
  7082. switch (mode_cmd->pixel_format) {
  7083. case DRM_FORMAT_RGB332:
  7084. case DRM_FORMAT_RGB565:
  7085. case DRM_FORMAT_XRGB8888:
  7086. case DRM_FORMAT_XBGR8888:
  7087. case DRM_FORMAT_ARGB8888:
  7088. case DRM_FORMAT_XRGB2101010:
  7089. case DRM_FORMAT_ARGB2101010:
  7090. /* RGB formats are common across chipsets */
  7091. break;
  7092. case DRM_FORMAT_YUYV:
  7093. case DRM_FORMAT_UYVY:
  7094. case DRM_FORMAT_YVYU:
  7095. case DRM_FORMAT_VYUY:
  7096. break;
  7097. default:
  7098. DRM_DEBUG_KMS("unsupported pixel format %u\n",
  7099. mode_cmd->pixel_format);
  7100. return -EINVAL;
  7101. }
  7102. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  7103. if (ret) {
  7104. DRM_ERROR("framebuffer init failed %d\n", ret);
  7105. return ret;
  7106. }
  7107. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  7108. intel_fb->obj = obj;
  7109. return 0;
  7110. }
  7111. static struct drm_framebuffer *
  7112. intel_user_framebuffer_create(struct drm_device *dev,
  7113. struct drm_file *filp,
  7114. struct drm_mode_fb_cmd2 *mode_cmd)
  7115. {
  7116. struct drm_i915_gem_object *obj;
  7117. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  7118. mode_cmd->handles[0]));
  7119. if (&obj->base == NULL)
  7120. return ERR_PTR(-ENOENT);
  7121. return intel_framebuffer_create(dev, mode_cmd, obj);
  7122. }
  7123. static const struct drm_mode_config_funcs intel_mode_funcs = {
  7124. .fb_create = intel_user_framebuffer_create,
  7125. .output_poll_changed = intel_fb_output_poll_changed,
  7126. };
  7127. static struct drm_i915_gem_object *
  7128. intel_alloc_context_page(struct drm_device *dev)
  7129. {
  7130. struct drm_i915_gem_object *ctx;
  7131. int ret;
  7132. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  7133. ctx = i915_gem_alloc_object(dev, 4096);
  7134. if (!ctx) {
  7135. DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
  7136. return NULL;
  7137. }
  7138. ret = i915_gem_object_pin(ctx, 4096, true);
  7139. if (ret) {
  7140. DRM_ERROR("failed to pin power context: %d\n", ret);
  7141. goto err_unref;
  7142. }
  7143. ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
  7144. if (ret) {
  7145. DRM_ERROR("failed to set-domain on power context: %d\n", ret);
  7146. goto err_unpin;
  7147. }
  7148. return ctx;
  7149. err_unpin:
  7150. i915_gem_object_unpin(ctx);
  7151. err_unref:
  7152. drm_gem_object_unreference(&ctx->base);
  7153. mutex_unlock(&dev->struct_mutex);
  7154. return NULL;
  7155. }
  7156. bool ironlake_set_drps(struct drm_device *dev, u8 val)
  7157. {
  7158. struct drm_i915_private *dev_priv = dev->dev_private;
  7159. u16 rgvswctl;
  7160. rgvswctl = I915_READ16(MEMSWCTL);
  7161. if (rgvswctl & MEMCTL_CMD_STS) {
  7162. DRM_DEBUG("gpu busy, RCS change rejected\n");
  7163. return false; /* still busy with another command */
  7164. }
  7165. rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
  7166. (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
  7167. I915_WRITE16(MEMSWCTL, rgvswctl);
  7168. POSTING_READ16(MEMSWCTL);
  7169. rgvswctl |= MEMCTL_CMD_STS;
  7170. I915_WRITE16(MEMSWCTL, rgvswctl);
  7171. return true;
  7172. }
  7173. void ironlake_enable_drps(struct drm_device *dev)
  7174. {
  7175. struct drm_i915_private *dev_priv = dev->dev_private;
  7176. u32 rgvmodectl = I915_READ(MEMMODECTL);
  7177. u8 fmax, fmin, fstart, vstart;
  7178. /* Enable temp reporting */
  7179. I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
  7180. I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
  7181. /* 100ms RC evaluation intervals */
  7182. I915_WRITE(RCUPEI, 100000);
  7183. I915_WRITE(RCDNEI, 100000);
  7184. /* Set max/min thresholds to 90ms and 80ms respectively */
  7185. I915_WRITE(RCBMAXAVG, 90000);
  7186. I915_WRITE(RCBMINAVG, 80000);
  7187. I915_WRITE(MEMIHYST, 1);
  7188. /* Set up min, max, and cur for interrupt handling */
  7189. fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
  7190. fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
  7191. fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
  7192. MEMMODE_FSTART_SHIFT;
  7193. vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
  7194. PXVFREQ_PX_SHIFT;
  7195. dev_priv->fmax = fmax; /* IPS callback will increase this */
  7196. dev_priv->fstart = fstart;
  7197. dev_priv->max_delay = fstart;
  7198. dev_priv->min_delay = fmin;
  7199. dev_priv->cur_delay = fstart;
  7200. DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
  7201. fmax, fmin, fstart);
  7202. I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
  7203. /*
  7204. * Interrupts will be enabled in ironlake_irq_postinstall
  7205. */
  7206. I915_WRITE(VIDSTART, vstart);
  7207. POSTING_READ(VIDSTART);
  7208. rgvmodectl |= MEMMODE_SWMODE_EN;
  7209. I915_WRITE(MEMMODECTL, rgvmodectl);
  7210. if (wait_for((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
  7211. DRM_ERROR("stuck trying to change perf mode\n");
  7212. msleep(1);
  7213. ironlake_set_drps(dev, fstart);
  7214. dev_priv->last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
  7215. I915_READ(0x112e0);
  7216. dev_priv->last_time1 = jiffies_to_msecs(jiffies);
  7217. dev_priv->last_count2 = I915_READ(0x112f4);
  7218. getrawmonotonic(&dev_priv->last_time2);
  7219. }
  7220. void ironlake_disable_drps(struct drm_device *dev)
  7221. {
  7222. struct drm_i915_private *dev_priv = dev->dev_private;
  7223. u16 rgvswctl = I915_READ16(MEMSWCTL);
  7224. /* Ack interrupts, disable EFC interrupt */
  7225. I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
  7226. I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
  7227. I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
  7228. I915_WRITE(DEIIR, DE_PCU_EVENT);
  7229. I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
  7230. /* Go back to the starting frequency */
  7231. ironlake_set_drps(dev, dev_priv->fstart);
  7232. msleep(1);
  7233. rgvswctl |= MEMCTL_CMD_STS;
  7234. I915_WRITE(MEMSWCTL, rgvswctl);
  7235. msleep(1);
  7236. }
  7237. void gen6_set_rps(struct drm_device *dev, u8 val)
  7238. {
  7239. struct drm_i915_private *dev_priv = dev->dev_private;
  7240. u32 swreq;
  7241. swreq = (val & 0x3ff) << 25;
  7242. I915_WRITE(GEN6_RPNSWREQ, swreq);
  7243. }
  7244. void gen6_disable_rps(struct drm_device *dev)
  7245. {
  7246. struct drm_i915_private *dev_priv = dev->dev_private;
  7247. I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
  7248. I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
  7249. I915_WRITE(GEN6_PMIER, 0);
  7250. /* Complete PM interrupt masking here doesn't race with the rps work
  7251. * item again unmasking PM interrupts because that is using a different
  7252. * register (PMIMR) to mask PM interrupts. The only risk is in leaving
  7253. * stale bits in PMIIR and PMIMR which gen6_enable_rps will clean up. */
  7254. spin_lock_irq(&dev_priv->rps_lock);
  7255. dev_priv->pm_iir = 0;
  7256. spin_unlock_irq(&dev_priv->rps_lock);
  7257. I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
  7258. }
  7259. static unsigned long intel_pxfreq(u32 vidfreq)
  7260. {
  7261. unsigned long freq;
  7262. int div = (vidfreq & 0x3f0000) >> 16;
  7263. int post = (vidfreq & 0x3000) >> 12;
  7264. int pre = (vidfreq & 0x7);
  7265. if (!pre)
  7266. return 0;
  7267. freq = ((div * 133333) / ((1<<post) * pre));
  7268. return freq;
  7269. }
  7270. void intel_init_emon(struct drm_device *dev)
  7271. {
  7272. struct drm_i915_private *dev_priv = dev->dev_private;
  7273. u32 lcfuse;
  7274. u8 pxw[16];
  7275. int i;
  7276. /* Disable to program */
  7277. I915_WRITE(ECR, 0);
  7278. POSTING_READ(ECR);
  7279. /* Program energy weights for various events */
  7280. I915_WRITE(SDEW, 0x15040d00);
  7281. I915_WRITE(CSIEW0, 0x007f0000);
  7282. I915_WRITE(CSIEW1, 0x1e220004);
  7283. I915_WRITE(CSIEW2, 0x04000004);
  7284. for (i = 0; i < 5; i++)
  7285. I915_WRITE(PEW + (i * 4), 0);
  7286. for (i = 0; i < 3; i++)
  7287. I915_WRITE(DEW + (i * 4), 0);
  7288. /* Program P-state weights to account for frequency power adjustment */
  7289. for (i = 0; i < 16; i++) {
  7290. u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
  7291. unsigned long freq = intel_pxfreq(pxvidfreq);
  7292. unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
  7293. PXVFREQ_PX_SHIFT;
  7294. unsigned long val;
  7295. val = vid * vid;
  7296. val *= (freq / 1000);
  7297. val *= 255;
  7298. val /= (127*127*900);
  7299. if (val > 0xff)
  7300. DRM_ERROR("bad pxval: %ld\n", val);
  7301. pxw[i] = val;
  7302. }
  7303. /* Render standby states get 0 weight */
  7304. pxw[14] = 0;
  7305. pxw[15] = 0;
  7306. for (i = 0; i < 4; i++) {
  7307. u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
  7308. (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
  7309. I915_WRITE(PXW + (i * 4), val);
  7310. }
  7311. /* Adjust magic regs to magic values (more experimental results) */
  7312. I915_WRITE(OGW0, 0);
  7313. I915_WRITE(OGW1, 0);
  7314. I915_WRITE(EG0, 0x00007f00);
  7315. I915_WRITE(EG1, 0x0000000e);
  7316. I915_WRITE(EG2, 0x000e0000);
  7317. I915_WRITE(EG3, 0x68000300);
  7318. I915_WRITE(EG4, 0x42000000);
  7319. I915_WRITE(EG5, 0x00140031);
  7320. I915_WRITE(EG6, 0);
  7321. I915_WRITE(EG7, 0);
  7322. for (i = 0; i < 8; i++)
  7323. I915_WRITE(PXWL + (i * 4), 0);
  7324. /* Enable PMON + select events */
  7325. I915_WRITE(ECR, 0x80000019);
  7326. lcfuse = I915_READ(LCFUSE02);
  7327. dev_priv->corr = (lcfuse & LCFUSE_HIV_MASK);
  7328. }
  7329. int intel_enable_rc6(const struct drm_device *dev)
  7330. {
  7331. /*
  7332. * Respect the kernel parameter if it is set
  7333. */
  7334. if (i915_enable_rc6 >= 0)
  7335. return i915_enable_rc6;
  7336. /*
  7337. * Disable RC6 on Ironlake
  7338. */
  7339. if (INTEL_INFO(dev)->gen == 5)
  7340. return 0;
  7341. /* Sorry Haswell, no RC6 for you for now. */
  7342. if (IS_HASWELL(dev))
  7343. return 0;
  7344. /*
  7345. * Disable rc6 on Sandybridge
  7346. */
  7347. if (INTEL_INFO(dev)->gen == 6) {
  7348. DRM_DEBUG_DRIVER("Sandybridge: deep RC6 disabled\n");
  7349. return INTEL_RC6_ENABLE;
  7350. }
  7351. DRM_DEBUG_DRIVER("RC6 and deep RC6 enabled\n");
  7352. return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
  7353. }
  7354. void gen6_enable_rps(struct drm_i915_private *dev_priv)
  7355. {
  7356. u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
  7357. u32 gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
  7358. u32 pcu_mbox, rc6_mask = 0;
  7359. u32 gtfifodbg;
  7360. int cur_freq, min_freq, max_freq;
  7361. int rc6_mode;
  7362. int i;
  7363. /* Here begins a magic sequence of register writes to enable
  7364. * auto-downclocking.
  7365. *
  7366. * Perhaps there might be some value in exposing these to
  7367. * userspace...
  7368. */
  7369. I915_WRITE(GEN6_RC_STATE, 0);
  7370. mutex_lock(&dev_priv->dev->struct_mutex);
  7371. /* Clear the DBG now so we don't confuse earlier errors */
  7372. if ((gtfifodbg = I915_READ(GTFIFODBG))) {
  7373. DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
  7374. I915_WRITE(GTFIFODBG, gtfifodbg);
  7375. }
  7376. gen6_gt_force_wake_get(dev_priv);
  7377. /* disable the counters and set deterministic thresholds */
  7378. I915_WRITE(GEN6_RC_CONTROL, 0);
  7379. I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
  7380. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
  7381. I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
  7382. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
  7383. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
  7384. for (i = 0; i < I915_NUM_RINGS; i++)
  7385. I915_WRITE(RING_MAX_IDLE(dev_priv->ring[i].mmio_base), 10);
  7386. I915_WRITE(GEN6_RC_SLEEP, 0);
  7387. I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
  7388. I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
  7389. I915_WRITE(GEN6_RC6p_THRESHOLD, 100000);
  7390. I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
  7391. rc6_mode = intel_enable_rc6(dev_priv->dev);
  7392. if (rc6_mode & INTEL_RC6_ENABLE)
  7393. rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
  7394. if (rc6_mode & INTEL_RC6p_ENABLE)
  7395. rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
  7396. if (rc6_mode & INTEL_RC6pp_ENABLE)
  7397. rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
  7398. DRM_INFO("Enabling RC6 states: RC6 %s, RC6p %s, RC6pp %s\n",
  7399. (rc6_mode & INTEL_RC6_ENABLE) ? "on" : "off",
  7400. (rc6_mode & INTEL_RC6p_ENABLE) ? "on" : "off",
  7401. (rc6_mode & INTEL_RC6pp_ENABLE) ? "on" : "off");
  7402. I915_WRITE(GEN6_RC_CONTROL,
  7403. rc6_mask |
  7404. GEN6_RC_CTL_EI_MODE(1) |
  7405. GEN6_RC_CTL_HW_ENABLE);
  7406. I915_WRITE(GEN6_RPNSWREQ,
  7407. GEN6_FREQUENCY(10) |
  7408. GEN6_OFFSET(0) |
  7409. GEN6_AGGRESSIVE_TURBO);
  7410. I915_WRITE(GEN6_RC_VIDEO_FREQ,
  7411. GEN6_FREQUENCY(12));
  7412. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
  7413. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
  7414. 18 << 24 |
  7415. 6 << 16);
  7416. I915_WRITE(GEN6_RP_UP_THRESHOLD, 10000);
  7417. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 1000000);
  7418. I915_WRITE(GEN6_RP_UP_EI, 100000);
  7419. I915_WRITE(GEN6_RP_DOWN_EI, 5000000);
  7420. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  7421. I915_WRITE(GEN6_RP_CONTROL,
  7422. GEN6_RP_MEDIA_TURBO |
  7423. GEN6_RP_MEDIA_HW_MODE |
  7424. GEN6_RP_MEDIA_IS_GFX |
  7425. GEN6_RP_ENABLE |
  7426. GEN6_RP_UP_BUSY_AVG |
  7427. GEN6_RP_DOWN_IDLE_CONT);
  7428. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  7429. 500))
  7430. DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
  7431. I915_WRITE(GEN6_PCODE_DATA, 0);
  7432. I915_WRITE(GEN6_PCODE_MAILBOX,
  7433. GEN6_PCODE_READY |
  7434. GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
  7435. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  7436. 500))
  7437. DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
  7438. min_freq = (rp_state_cap & 0xff0000) >> 16;
  7439. max_freq = rp_state_cap & 0xff;
  7440. cur_freq = (gt_perf_status & 0xff00) >> 8;
  7441. /* Check for overclock support */
  7442. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  7443. 500))
  7444. DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
  7445. I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_READ_OC_PARAMS);
  7446. pcu_mbox = I915_READ(GEN6_PCODE_DATA);
  7447. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  7448. 500))
  7449. DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
  7450. if (pcu_mbox & (1<<31)) { /* OC supported */
  7451. max_freq = pcu_mbox & 0xff;
  7452. DRM_DEBUG_DRIVER("overclocking supported, adjusting frequency max to %dMHz\n", pcu_mbox * 50);
  7453. }
  7454. /* In units of 100MHz */
  7455. dev_priv->max_delay = max_freq;
  7456. dev_priv->min_delay = min_freq;
  7457. dev_priv->cur_delay = cur_freq;
  7458. /* requires MSI enabled */
  7459. I915_WRITE(GEN6_PMIER,
  7460. GEN6_PM_MBOX_EVENT |
  7461. GEN6_PM_THERMAL_EVENT |
  7462. GEN6_PM_RP_DOWN_TIMEOUT |
  7463. GEN6_PM_RP_UP_THRESHOLD |
  7464. GEN6_PM_RP_DOWN_THRESHOLD |
  7465. GEN6_PM_RP_UP_EI_EXPIRED |
  7466. GEN6_PM_RP_DOWN_EI_EXPIRED);
  7467. spin_lock_irq(&dev_priv->rps_lock);
  7468. WARN_ON(dev_priv->pm_iir != 0);
  7469. I915_WRITE(GEN6_PMIMR, 0);
  7470. spin_unlock_irq(&dev_priv->rps_lock);
  7471. /* enable all PM interrupts */
  7472. I915_WRITE(GEN6_PMINTRMSK, 0);
  7473. gen6_gt_force_wake_put(dev_priv);
  7474. mutex_unlock(&dev_priv->dev->struct_mutex);
  7475. }
  7476. void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
  7477. {
  7478. int min_freq = 15;
  7479. int gpu_freq, ia_freq, max_ia_freq;
  7480. int scaling_factor = 180;
  7481. max_ia_freq = cpufreq_quick_get_max(0);
  7482. /*
  7483. * Default to measured freq if none found, PCU will ensure we don't go
  7484. * over
  7485. */
  7486. if (!max_ia_freq)
  7487. max_ia_freq = tsc_khz;
  7488. /* Convert from kHz to MHz */
  7489. max_ia_freq /= 1000;
  7490. mutex_lock(&dev_priv->dev->struct_mutex);
  7491. /*
  7492. * For each potential GPU frequency, load a ring frequency we'd like
  7493. * to use for memory access. We do this by specifying the IA frequency
  7494. * the PCU should use as a reference to determine the ring frequency.
  7495. */
  7496. for (gpu_freq = dev_priv->max_delay; gpu_freq >= dev_priv->min_delay;
  7497. gpu_freq--) {
  7498. int diff = dev_priv->max_delay - gpu_freq;
  7499. /*
  7500. * For GPU frequencies less than 750MHz, just use the lowest
  7501. * ring freq.
  7502. */
  7503. if (gpu_freq < min_freq)
  7504. ia_freq = 800;
  7505. else
  7506. ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
  7507. ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
  7508. I915_WRITE(GEN6_PCODE_DATA,
  7509. (ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT) |
  7510. gpu_freq);
  7511. I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY |
  7512. GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
  7513. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) &
  7514. GEN6_PCODE_READY) == 0, 10)) {
  7515. DRM_ERROR("pcode write of freq table timed out\n");
  7516. continue;
  7517. }
  7518. }
  7519. mutex_unlock(&dev_priv->dev->struct_mutex);
  7520. }
  7521. static void ironlake_init_clock_gating(struct drm_device *dev)
  7522. {
  7523. struct drm_i915_private *dev_priv = dev->dev_private;
  7524. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  7525. /* Required for FBC */
  7526. dspclk_gate |= DPFCUNIT_CLOCK_GATE_DISABLE |
  7527. DPFCRUNIT_CLOCK_GATE_DISABLE |
  7528. DPFDUNIT_CLOCK_GATE_DISABLE;
  7529. /* Required for CxSR */
  7530. dspclk_gate |= DPARBUNIT_CLOCK_GATE_DISABLE;
  7531. I915_WRITE(PCH_3DCGDIS0,
  7532. MARIUNIT_CLOCK_GATE_DISABLE |
  7533. SVSMUNIT_CLOCK_GATE_DISABLE);
  7534. I915_WRITE(PCH_3DCGDIS1,
  7535. VFMUNIT_CLOCK_GATE_DISABLE);
  7536. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  7537. /*
  7538. * According to the spec the following bits should be set in
  7539. * order to enable memory self-refresh
  7540. * The bit 22/21 of 0x42004
  7541. * The bit 5 of 0x42020
  7542. * The bit 15 of 0x45000
  7543. */
  7544. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  7545. (I915_READ(ILK_DISPLAY_CHICKEN2) |
  7546. ILK_DPARB_GATE | ILK_VSDPFD_FULL));
  7547. I915_WRITE(ILK_DSPCLK_GATE,
  7548. (I915_READ(ILK_DSPCLK_GATE) |
  7549. ILK_DPARB_CLK_GATE));
  7550. I915_WRITE(DISP_ARB_CTL,
  7551. (I915_READ(DISP_ARB_CTL) |
  7552. DISP_FBC_WM_DIS));
  7553. I915_WRITE(WM3_LP_ILK, 0);
  7554. I915_WRITE(WM2_LP_ILK, 0);
  7555. I915_WRITE(WM1_LP_ILK, 0);
  7556. /*
  7557. * Based on the document from hardware guys the following bits
  7558. * should be set unconditionally in order to enable FBC.
  7559. * The bit 22 of 0x42000
  7560. * The bit 22 of 0x42004
  7561. * The bit 7,8,9 of 0x42020.
  7562. */
  7563. if (IS_IRONLAKE_M(dev)) {
  7564. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  7565. I915_READ(ILK_DISPLAY_CHICKEN1) |
  7566. ILK_FBCQ_DIS);
  7567. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  7568. I915_READ(ILK_DISPLAY_CHICKEN2) |
  7569. ILK_DPARB_GATE);
  7570. I915_WRITE(ILK_DSPCLK_GATE,
  7571. I915_READ(ILK_DSPCLK_GATE) |
  7572. ILK_DPFC_DIS1 |
  7573. ILK_DPFC_DIS2 |
  7574. ILK_CLK_FBC);
  7575. }
  7576. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  7577. I915_READ(ILK_DISPLAY_CHICKEN2) |
  7578. ILK_ELPIN_409_SELECT);
  7579. I915_WRITE(_3D_CHICKEN2,
  7580. _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
  7581. _3D_CHICKEN2_WM_READ_PIPELINED);
  7582. }
  7583. static void gen6_init_clock_gating(struct drm_device *dev)
  7584. {
  7585. struct drm_i915_private *dev_priv = dev->dev_private;
  7586. int pipe;
  7587. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  7588. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  7589. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  7590. I915_READ(ILK_DISPLAY_CHICKEN2) |
  7591. ILK_ELPIN_409_SELECT);
  7592. I915_WRITE(WM3_LP_ILK, 0);
  7593. I915_WRITE(WM2_LP_ILK, 0);
  7594. I915_WRITE(WM1_LP_ILK, 0);
  7595. /* clear masked bit */
  7596. I915_WRITE(CACHE_MODE_0,
  7597. CM0_STC_EVICT_DISABLE_LRA_SNB << CM0_MASK_SHIFT);
  7598. I915_WRITE(GEN6_UCGCTL1,
  7599. I915_READ(GEN6_UCGCTL1) |
  7600. GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
  7601. GEN6_CSUNIT_CLOCK_GATE_DISABLE);
  7602. /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
  7603. * gating disable must be set. Failure to set it results in
  7604. * flickering pixels due to Z write ordering failures after
  7605. * some amount of runtime in the Mesa "fire" demo, and Unigine
  7606. * Sanctuary and Tropics, and apparently anything else with
  7607. * alpha test or pixel discard.
  7608. *
  7609. * According to the spec, bit 11 (RCCUNIT) must also be set,
  7610. * but we didn't debug actual testcases to find it out.
  7611. */
  7612. I915_WRITE(GEN6_UCGCTL2,
  7613. GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
  7614. GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
  7615. /* Bspec says we need to always set all mask bits. */
  7616. I915_WRITE(_3D_CHICKEN, (0xFFFF << 16) |
  7617. _3D_CHICKEN_SF_DISABLE_FASTCLIP_CULL);
  7618. /*
  7619. * According to the spec the following bits should be
  7620. * set in order to enable memory self-refresh and fbc:
  7621. * The bit21 and bit22 of 0x42000
  7622. * The bit21 and bit22 of 0x42004
  7623. * The bit5 and bit7 of 0x42020
  7624. * The bit14 of 0x70180
  7625. * The bit14 of 0x71180
  7626. */
  7627. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  7628. I915_READ(ILK_DISPLAY_CHICKEN1) |
  7629. ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
  7630. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  7631. I915_READ(ILK_DISPLAY_CHICKEN2) |
  7632. ILK_DPARB_GATE | ILK_VSDPFD_FULL);
  7633. I915_WRITE(ILK_DSPCLK_GATE,
  7634. I915_READ(ILK_DSPCLK_GATE) |
  7635. ILK_DPARB_CLK_GATE |
  7636. ILK_DPFD_CLK_GATE);
  7637. for_each_pipe(pipe) {
  7638. I915_WRITE(DSPCNTR(pipe),
  7639. I915_READ(DSPCNTR(pipe)) |
  7640. DISPPLANE_TRICKLE_FEED_DISABLE);
  7641. intel_flush_display_plane(dev_priv, pipe);
  7642. }
  7643. }
  7644. static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
  7645. {
  7646. uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
  7647. reg &= ~GEN7_FF_SCHED_MASK;
  7648. reg |= GEN7_FF_TS_SCHED_HW;
  7649. reg |= GEN7_FF_VS_SCHED_HW;
  7650. reg |= GEN7_FF_DS_SCHED_HW;
  7651. I915_WRITE(GEN7_FF_THREAD_MODE, reg);
  7652. }
  7653. static void ivybridge_init_clock_gating(struct drm_device *dev)
  7654. {
  7655. struct drm_i915_private *dev_priv = dev->dev_private;
  7656. int pipe;
  7657. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  7658. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  7659. I915_WRITE(WM3_LP_ILK, 0);
  7660. I915_WRITE(WM2_LP_ILK, 0);
  7661. I915_WRITE(WM1_LP_ILK, 0);
  7662. /* According to the spec, bit 13 (RCZUNIT) must be set on IVB.
  7663. * This implements the WaDisableRCZUnitClockGating workaround.
  7664. */
  7665. I915_WRITE(GEN6_UCGCTL2, GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
  7666. I915_WRITE(ILK_DSPCLK_GATE, IVB_VRHUNIT_CLK_GATE);
  7667. I915_WRITE(IVB_CHICKEN3,
  7668. CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
  7669. CHICKEN3_DGMG_DONE_FIX_DISABLE);
  7670. /* Apply the WaDisableRHWOOptimizationForRenderHang workaround. */
  7671. I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
  7672. GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
  7673. /* WaApplyL3ControlAndL3ChickenMode requires those two on Ivy Bridge */
  7674. I915_WRITE(GEN7_L3CNTLREG1,
  7675. GEN7_WA_FOR_GEN7_L3_CONTROL);
  7676. I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
  7677. GEN7_WA_L3_CHICKEN_MODE);
  7678. /* This is required by WaCatErrorRejectionIssue */
  7679. I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
  7680. I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
  7681. GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
  7682. for_each_pipe(pipe) {
  7683. I915_WRITE(DSPCNTR(pipe),
  7684. I915_READ(DSPCNTR(pipe)) |
  7685. DISPPLANE_TRICKLE_FEED_DISABLE);
  7686. intel_flush_display_plane(dev_priv, pipe);
  7687. }
  7688. gen7_setup_fixed_func_scheduler(dev_priv);
  7689. }
  7690. static void valleyview_init_clock_gating(struct drm_device *dev)
  7691. {
  7692. struct drm_i915_private *dev_priv = dev->dev_private;
  7693. int pipe;
  7694. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  7695. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  7696. I915_WRITE(WM3_LP_ILK, 0);
  7697. I915_WRITE(WM2_LP_ILK, 0);
  7698. I915_WRITE(WM1_LP_ILK, 0);
  7699. /* According to the spec, bit 13 (RCZUNIT) must be set on IVB.
  7700. * This implements the WaDisableRCZUnitClockGating workaround.
  7701. */
  7702. I915_WRITE(GEN6_UCGCTL2, GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
  7703. I915_WRITE(ILK_DSPCLK_GATE, IVB_VRHUNIT_CLK_GATE);
  7704. I915_WRITE(IVB_CHICKEN3,
  7705. CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
  7706. CHICKEN3_DGMG_DONE_FIX_DISABLE);
  7707. /* Apply the WaDisableRHWOOptimizationForRenderHang workaround. */
  7708. I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
  7709. GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
  7710. /* WaApplyL3ControlAndL3ChickenMode requires those two on Ivy Bridge */
  7711. I915_WRITE(GEN7_L3CNTLREG1, GEN7_WA_FOR_GEN7_L3_CONTROL);
  7712. I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER, GEN7_WA_L3_CHICKEN_MODE);
  7713. /* This is required by WaCatErrorRejectionIssue */
  7714. I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
  7715. I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
  7716. GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
  7717. for_each_pipe(pipe) {
  7718. I915_WRITE(DSPCNTR(pipe),
  7719. I915_READ(DSPCNTR(pipe)) |
  7720. DISPPLANE_TRICKLE_FEED_DISABLE);
  7721. intel_flush_display_plane(dev_priv, pipe);
  7722. }
  7723. I915_WRITE(CACHE_MODE_1, I915_READ(CACHE_MODE_1) |
  7724. (PIXEL_SUBSPAN_COLLECT_OPT_DISABLE << 16) |
  7725. PIXEL_SUBSPAN_COLLECT_OPT_DISABLE);
  7726. }
  7727. static void g4x_init_clock_gating(struct drm_device *dev)
  7728. {
  7729. struct drm_i915_private *dev_priv = dev->dev_private;
  7730. uint32_t dspclk_gate;
  7731. I915_WRITE(RENCLK_GATE_D1, 0);
  7732. I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
  7733. GS_UNIT_CLOCK_GATE_DISABLE |
  7734. CL_UNIT_CLOCK_GATE_DISABLE);
  7735. I915_WRITE(RAMCLK_GATE_D, 0);
  7736. dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
  7737. OVRUNIT_CLOCK_GATE_DISABLE |
  7738. OVCUNIT_CLOCK_GATE_DISABLE;
  7739. if (IS_GM45(dev))
  7740. dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
  7741. I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
  7742. }
  7743. static void crestline_init_clock_gating(struct drm_device *dev)
  7744. {
  7745. struct drm_i915_private *dev_priv = dev->dev_private;
  7746. I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
  7747. I915_WRITE(RENCLK_GATE_D2, 0);
  7748. I915_WRITE(DSPCLK_GATE_D, 0);
  7749. I915_WRITE(RAMCLK_GATE_D, 0);
  7750. I915_WRITE16(DEUC, 0);
  7751. }
  7752. static void broadwater_init_clock_gating(struct drm_device *dev)
  7753. {
  7754. struct drm_i915_private *dev_priv = dev->dev_private;
  7755. I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
  7756. I965_RCC_CLOCK_GATE_DISABLE |
  7757. I965_RCPB_CLOCK_GATE_DISABLE |
  7758. I965_ISC_CLOCK_GATE_DISABLE |
  7759. I965_FBC_CLOCK_GATE_DISABLE);
  7760. I915_WRITE(RENCLK_GATE_D2, 0);
  7761. }
  7762. static void gen3_init_clock_gating(struct drm_device *dev)
  7763. {
  7764. struct drm_i915_private *dev_priv = dev->dev_private;
  7765. u32 dstate = I915_READ(D_STATE);
  7766. dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
  7767. DSTATE_DOT_CLOCK_GATING;
  7768. I915_WRITE(D_STATE, dstate);
  7769. }
  7770. static void i85x_init_clock_gating(struct drm_device *dev)
  7771. {
  7772. struct drm_i915_private *dev_priv = dev->dev_private;
  7773. I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
  7774. }
  7775. static void i830_init_clock_gating(struct drm_device *dev)
  7776. {
  7777. struct drm_i915_private *dev_priv = dev->dev_private;
  7778. I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
  7779. }
  7780. static void ibx_init_clock_gating(struct drm_device *dev)
  7781. {
  7782. struct drm_i915_private *dev_priv = dev->dev_private;
  7783. /*
  7784. * On Ibex Peak and Cougar Point, we need to disable clock
  7785. * gating for the panel power sequencer or it will fail to
  7786. * start up when no ports are active.
  7787. */
  7788. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
  7789. }
  7790. static void cpt_init_clock_gating(struct drm_device *dev)
  7791. {
  7792. struct drm_i915_private *dev_priv = dev->dev_private;
  7793. int pipe;
  7794. /*
  7795. * On Ibex Peak and Cougar Point, we need to disable clock
  7796. * gating for the panel power sequencer or it will fail to
  7797. * start up when no ports are active.
  7798. */
  7799. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
  7800. I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
  7801. DPLS_EDP_PPS_FIX_DIS);
  7802. /* Without this, mode sets may fail silently on FDI */
  7803. for_each_pipe(pipe)
  7804. I915_WRITE(TRANS_CHICKEN2(pipe), TRANS_AUTOTRAIN_GEN_STALL_DIS);
  7805. }
  7806. static void ironlake_teardown_rc6(struct drm_device *dev)
  7807. {
  7808. struct drm_i915_private *dev_priv = dev->dev_private;
  7809. if (dev_priv->renderctx) {
  7810. i915_gem_object_unpin(dev_priv->renderctx);
  7811. drm_gem_object_unreference(&dev_priv->renderctx->base);
  7812. dev_priv->renderctx = NULL;
  7813. }
  7814. if (dev_priv->pwrctx) {
  7815. i915_gem_object_unpin(dev_priv->pwrctx);
  7816. drm_gem_object_unreference(&dev_priv->pwrctx->base);
  7817. dev_priv->pwrctx = NULL;
  7818. }
  7819. }
  7820. static void ironlake_disable_rc6(struct drm_device *dev)
  7821. {
  7822. struct drm_i915_private *dev_priv = dev->dev_private;
  7823. if (I915_READ(PWRCTXA)) {
  7824. /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
  7825. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
  7826. wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
  7827. 50);
  7828. I915_WRITE(PWRCTXA, 0);
  7829. POSTING_READ(PWRCTXA);
  7830. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
  7831. POSTING_READ(RSTDBYCTL);
  7832. }
  7833. ironlake_teardown_rc6(dev);
  7834. }
  7835. static int ironlake_setup_rc6(struct drm_device *dev)
  7836. {
  7837. struct drm_i915_private *dev_priv = dev->dev_private;
  7838. if (dev_priv->renderctx == NULL)
  7839. dev_priv->renderctx = intel_alloc_context_page(dev);
  7840. if (!dev_priv->renderctx)
  7841. return -ENOMEM;
  7842. if (dev_priv->pwrctx == NULL)
  7843. dev_priv->pwrctx = intel_alloc_context_page(dev);
  7844. if (!dev_priv->pwrctx) {
  7845. ironlake_teardown_rc6(dev);
  7846. return -ENOMEM;
  7847. }
  7848. return 0;
  7849. }
  7850. void ironlake_enable_rc6(struct drm_device *dev)
  7851. {
  7852. struct drm_i915_private *dev_priv = dev->dev_private;
  7853. int ret;
  7854. /* rc6 disabled by default due to repeated reports of hanging during
  7855. * boot and resume.
  7856. */
  7857. if (!intel_enable_rc6(dev))
  7858. return;
  7859. mutex_lock(&dev->struct_mutex);
  7860. ret = ironlake_setup_rc6(dev);
  7861. if (ret) {
  7862. mutex_unlock(&dev->struct_mutex);
  7863. return;
  7864. }
  7865. /*
  7866. * GPU can automatically power down the render unit if given a page
  7867. * to save state.
  7868. */
  7869. ret = BEGIN_LP_RING(6);
  7870. if (ret) {
  7871. ironlake_teardown_rc6(dev);
  7872. mutex_unlock(&dev->struct_mutex);
  7873. return;
  7874. }
  7875. OUT_RING(MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
  7876. OUT_RING(MI_SET_CONTEXT);
  7877. OUT_RING(dev_priv->renderctx->gtt_offset |
  7878. MI_MM_SPACE_GTT |
  7879. MI_SAVE_EXT_STATE_EN |
  7880. MI_RESTORE_EXT_STATE_EN |
  7881. MI_RESTORE_INHIBIT);
  7882. OUT_RING(MI_SUSPEND_FLUSH);
  7883. OUT_RING(MI_NOOP);
  7884. OUT_RING(MI_FLUSH);
  7885. ADVANCE_LP_RING();
  7886. /*
  7887. * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
  7888. * does an implicit flush, combined with MI_FLUSH above, it should be
  7889. * safe to assume that renderctx is valid
  7890. */
  7891. ret = intel_wait_ring_idle(LP_RING(dev_priv));
  7892. if (ret) {
  7893. DRM_ERROR("failed to enable ironlake power power savings\n");
  7894. ironlake_teardown_rc6(dev);
  7895. mutex_unlock(&dev->struct_mutex);
  7896. return;
  7897. }
  7898. I915_WRITE(PWRCTXA, dev_priv->pwrctx->gtt_offset | PWRCTX_EN);
  7899. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
  7900. mutex_unlock(&dev->struct_mutex);
  7901. }
  7902. void intel_init_clock_gating(struct drm_device *dev)
  7903. {
  7904. struct drm_i915_private *dev_priv = dev->dev_private;
  7905. dev_priv->display.init_clock_gating(dev);
  7906. if (dev_priv->display.init_pch_clock_gating)
  7907. dev_priv->display.init_pch_clock_gating(dev);
  7908. }
  7909. /* Set up chip specific display functions */
  7910. static void intel_init_display(struct drm_device *dev)
  7911. {
  7912. struct drm_i915_private *dev_priv = dev->dev_private;
  7913. /* We always want a DPMS function */
  7914. if (HAS_PCH_SPLIT(dev)) {
  7915. dev_priv->display.dpms = ironlake_crtc_dpms;
  7916. dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
  7917. dev_priv->display.update_plane = ironlake_update_plane;
  7918. } else {
  7919. dev_priv->display.dpms = i9xx_crtc_dpms;
  7920. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  7921. dev_priv->display.update_plane = i9xx_update_plane;
  7922. }
  7923. if (I915_HAS_FBC(dev)) {
  7924. if (HAS_PCH_SPLIT(dev)) {
  7925. dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
  7926. dev_priv->display.enable_fbc = ironlake_enable_fbc;
  7927. dev_priv->display.disable_fbc = ironlake_disable_fbc;
  7928. } else if (IS_GM45(dev)) {
  7929. dev_priv->display.fbc_enabled = g4x_fbc_enabled;
  7930. dev_priv->display.enable_fbc = g4x_enable_fbc;
  7931. dev_priv->display.disable_fbc = g4x_disable_fbc;
  7932. } else if (IS_CRESTLINE(dev)) {
  7933. dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
  7934. dev_priv->display.enable_fbc = i8xx_enable_fbc;
  7935. dev_priv->display.disable_fbc = i8xx_disable_fbc;
  7936. }
  7937. /* 855GM needs testing */
  7938. }
  7939. /* Returns the core display clock speed */
  7940. if (IS_VALLEYVIEW(dev))
  7941. dev_priv->display.get_display_clock_speed =
  7942. valleyview_get_display_clock_speed;
  7943. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  7944. dev_priv->display.get_display_clock_speed =
  7945. i945_get_display_clock_speed;
  7946. else if (IS_I915G(dev))
  7947. dev_priv->display.get_display_clock_speed =
  7948. i915_get_display_clock_speed;
  7949. else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
  7950. dev_priv->display.get_display_clock_speed =
  7951. i9xx_misc_get_display_clock_speed;
  7952. else if (IS_I915GM(dev))
  7953. dev_priv->display.get_display_clock_speed =
  7954. i915gm_get_display_clock_speed;
  7955. else if (IS_I865G(dev))
  7956. dev_priv->display.get_display_clock_speed =
  7957. i865_get_display_clock_speed;
  7958. else if (IS_I85X(dev))
  7959. dev_priv->display.get_display_clock_speed =
  7960. i855_get_display_clock_speed;
  7961. else /* 852, 830 */
  7962. dev_priv->display.get_display_clock_speed =
  7963. i830_get_display_clock_speed;
  7964. /* For FIFO watermark updates */
  7965. if (HAS_PCH_SPLIT(dev)) {
  7966. dev_priv->display.force_wake_get = __gen6_gt_force_wake_get;
  7967. dev_priv->display.force_wake_put = __gen6_gt_force_wake_put;
  7968. /* IVB configs may use multi-threaded forcewake */
  7969. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  7970. u32 ecobus;
  7971. /* A small trick here - if the bios hasn't configured MT forcewake,
  7972. * and if the device is in RC6, then force_wake_mt_get will not wake
  7973. * the device and the ECOBUS read will return zero. Which will be
  7974. * (correctly) interpreted by the test below as MT forcewake being
  7975. * disabled.
  7976. */
  7977. mutex_lock(&dev->struct_mutex);
  7978. __gen6_gt_force_wake_mt_get(dev_priv);
  7979. ecobus = I915_READ_NOTRACE(ECOBUS);
  7980. __gen6_gt_force_wake_mt_put(dev_priv);
  7981. mutex_unlock(&dev->struct_mutex);
  7982. if (ecobus & FORCEWAKE_MT_ENABLE) {
  7983. DRM_DEBUG_KMS("Using MT version of forcewake\n");
  7984. dev_priv->display.force_wake_get =
  7985. __gen6_gt_force_wake_mt_get;
  7986. dev_priv->display.force_wake_put =
  7987. __gen6_gt_force_wake_mt_put;
  7988. }
  7989. }
  7990. if (HAS_PCH_IBX(dev))
  7991. dev_priv->display.init_pch_clock_gating = ibx_init_clock_gating;
  7992. else if (HAS_PCH_CPT(dev))
  7993. dev_priv->display.init_pch_clock_gating = cpt_init_clock_gating;
  7994. if (IS_GEN5(dev)) {
  7995. if (I915_READ(MLTR_ILK) & ILK_SRLT_MASK)
  7996. dev_priv->display.update_wm = ironlake_update_wm;
  7997. else {
  7998. DRM_DEBUG_KMS("Failed to get proper latency. "
  7999. "Disable CxSR\n");
  8000. dev_priv->display.update_wm = NULL;
  8001. }
  8002. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  8003. dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
  8004. dev_priv->display.write_eld = ironlake_write_eld;
  8005. } else if (IS_GEN6(dev)) {
  8006. if (SNB_READ_WM0_LATENCY()) {
  8007. dev_priv->display.update_wm = sandybridge_update_wm;
  8008. dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
  8009. } else {
  8010. DRM_DEBUG_KMS("Failed to read display plane latency. "
  8011. "Disable CxSR\n");
  8012. dev_priv->display.update_wm = NULL;
  8013. }
  8014. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  8015. dev_priv->display.init_clock_gating = gen6_init_clock_gating;
  8016. dev_priv->display.write_eld = ironlake_write_eld;
  8017. } else if (IS_IVYBRIDGE(dev)) {
  8018. /* FIXME: detect B0+ stepping and use auto training */
  8019. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  8020. if (SNB_READ_WM0_LATENCY()) {
  8021. dev_priv->display.update_wm = sandybridge_update_wm;
  8022. dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
  8023. } else {
  8024. DRM_DEBUG_KMS("Failed to read display plane latency. "
  8025. "Disable CxSR\n");
  8026. dev_priv->display.update_wm = NULL;
  8027. }
  8028. dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
  8029. dev_priv->display.write_eld = ironlake_write_eld;
  8030. } else
  8031. dev_priv->display.update_wm = NULL;
  8032. } else if (IS_VALLEYVIEW(dev)) {
  8033. dev_priv->display.update_wm = valleyview_update_wm;
  8034. dev_priv->display.init_clock_gating =
  8035. valleyview_init_clock_gating;
  8036. dev_priv->display.force_wake_get = vlv_force_wake_get;
  8037. dev_priv->display.force_wake_put = vlv_force_wake_put;
  8038. } else if (IS_PINEVIEW(dev)) {
  8039. if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
  8040. dev_priv->is_ddr3,
  8041. dev_priv->fsb_freq,
  8042. dev_priv->mem_freq)) {
  8043. DRM_INFO("failed to find known CxSR latency "
  8044. "(found ddr%s fsb freq %d, mem freq %d), "
  8045. "disabling CxSR\n",
  8046. (dev_priv->is_ddr3 == 1) ? "3" : "2",
  8047. dev_priv->fsb_freq, dev_priv->mem_freq);
  8048. /* Disable CxSR and never update its watermark again */
  8049. pineview_disable_cxsr(dev);
  8050. dev_priv->display.update_wm = NULL;
  8051. } else
  8052. dev_priv->display.update_wm = pineview_update_wm;
  8053. dev_priv->display.init_clock_gating = gen3_init_clock_gating;
  8054. } else if (IS_G4X(dev)) {
  8055. dev_priv->display.write_eld = g4x_write_eld;
  8056. dev_priv->display.update_wm = g4x_update_wm;
  8057. dev_priv->display.init_clock_gating = g4x_init_clock_gating;
  8058. } else if (IS_GEN4(dev)) {
  8059. dev_priv->display.update_wm = i965_update_wm;
  8060. if (IS_CRESTLINE(dev))
  8061. dev_priv->display.init_clock_gating = crestline_init_clock_gating;
  8062. else if (IS_BROADWATER(dev))
  8063. dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
  8064. } else if (IS_GEN3(dev)) {
  8065. dev_priv->display.update_wm = i9xx_update_wm;
  8066. dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
  8067. dev_priv->display.init_clock_gating = gen3_init_clock_gating;
  8068. } else if (IS_I865G(dev)) {
  8069. dev_priv->display.update_wm = i830_update_wm;
  8070. dev_priv->display.init_clock_gating = i85x_init_clock_gating;
  8071. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  8072. } else if (IS_I85X(dev)) {
  8073. dev_priv->display.update_wm = i9xx_update_wm;
  8074. dev_priv->display.get_fifo_size = i85x_get_fifo_size;
  8075. dev_priv->display.init_clock_gating = i85x_init_clock_gating;
  8076. } else {
  8077. dev_priv->display.update_wm = i830_update_wm;
  8078. dev_priv->display.init_clock_gating = i830_init_clock_gating;
  8079. if (IS_845G(dev))
  8080. dev_priv->display.get_fifo_size = i845_get_fifo_size;
  8081. else
  8082. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  8083. }
  8084. /* Default just returns -ENODEV to indicate unsupported */
  8085. dev_priv->display.queue_flip = intel_default_queue_flip;
  8086. switch (INTEL_INFO(dev)->gen) {
  8087. case 2:
  8088. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  8089. break;
  8090. case 3:
  8091. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  8092. break;
  8093. case 4:
  8094. case 5:
  8095. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  8096. break;
  8097. case 6:
  8098. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  8099. break;
  8100. case 7:
  8101. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  8102. break;
  8103. }
  8104. }
  8105. /*
  8106. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  8107. * resume, or other times. This quirk makes sure that's the case for
  8108. * affected systems.
  8109. */
  8110. static void quirk_pipea_force(struct drm_device *dev)
  8111. {
  8112. struct drm_i915_private *dev_priv = dev->dev_private;
  8113. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  8114. DRM_INFO("applying pipe a force quirk\n");
  8115. }
  8116. /*
  8117. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  8118. */
  8119. static void quirk_ssc_force_disable(struct drm_device *dev)
  8120. {
  8121. struct drm_i915_private *dev_priv = dev->dev_private;
  8122. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  8123. DRM_INFO("applying lvds SSC disable quirk\n");
  8124. }
  8125. /*
  8126. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  8127. * brightness value
  8128. */
  8129. static void quirk_invert_brightness(struct drm_device *dev)
  8130. {
  8131. struct drm_i915_private *dev_priv = dev->dev_private;
  8132. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  8133. DRM_INFO("applying inverted panel brightness quirk\n");
  8134. }
  8135. struct intel_quirk {
  8136. int device;
  8137. int subsystem_vendor;
  8138. int subsystem_device;
  8139. void (*hook)(struct drm_device *dev);
  8140. };
  8141. static struct intel_quirk intel_quirks[] = {
  8142. /* HP Mini needs pipe A force quirk (LP: #322104) */
  8143. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  8144. /* Thinkpad R31 needs pipe A force quirk */
  8145. { 0x3577, 0x1014, 0x0505, quirk_pipea_force },
  8146. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  8147. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  8148. /* ThinkPad X30 needs pipe A force quirk (LP: #304614) */
  8149. { 0x3577, 0x1014, 0x0513, quirk_pipea_force },
  8150. /* ThinkPad X40 needs pipe A force quirk */
  8151. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  8152. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  8153. /* 855 & before need to leave pipe A & dpll A up */
  8154. { 0x3582, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  8155. { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  8156. /* Lenovo U160 cannot use SSC on LVDS */
  8157. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  8158. /* Sony Vaio Y cannot use SSC on LVDS */
  8159. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  8160. /* Acer Aspire 5734Z must invert backlight brightness */
  8161. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  8162. };
  8163. static void intel_init_quirks(struct drm_device *dev)
  8164. {
  8165. struct pci_dev *d = dev->pdev;
  8166. int i;
  8167. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  8168. struct intel_quirk *q = &intel_quirks[i];
  8169. if (d->device == q->device &&
  8170. (d->subsystem_vendor == q->subsystem_vendor ||
  8171. q->subsystem_vendor == PCI_ANY_ID) &&
  8172. (d->subsystem_device == q->subsystem_device ||
  8173. q->subsystem_device == PCI_ANY_ID))
  8174. q->hook(dev);
  8175. }
  8176. }
  8177. /* Disable the VGA plane that we never use */
  8178. static void i915_disable_vga(struct drm_device *dev)
  8179. {
  8180. struct drm_i915_private *dev_priv = dev->dev_private;
  8181. u8 sr1;
  8182. u32 vga_reg;
  8183. if (HAS_PCH_SPLIT(dev))
  8184. vga_reg = CPU_VGACNTRL;
  8185. else
  8186. vga_reg = VGACNTRL;
  8187. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  8188. outb(SR01, VGA_SR_INDEX);
  8189. sr1 = inb(VGA_SR_DATA);
  8190. outb(sr1 | 1<<5, VGA_SR_DATA);
  8191. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  8192. udelay(300);
  8193. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  8194. POSTING_READ(vga_reg);
  8195. }
  8196. static void ivb_pch_pwm_override(struct drm_device *dev)
  8197. {
  8198. struct drm_i915_private *dev_priv = dev->dev_private;
  8199. /*
  8200. * IVB has CPU eDP backlight regs too, set things up to let the
  8201. * PCH regs control the backlight
  8202. */
  8203. I915_WRITE(BLC_PWM_CPU_CTL2, PWM_ENABLE);
  8204. I915_WRITE(BLC_PWM_CPU_CTL, 0);
  8205. I915_WRITE(BLC_PWM_PCH_CTL1, PWM_ENABLE | (1<<30));
  8206. }
  8207. void intel_modeset_init_hw(struct drm_device *dev)
  8208. {
  8209. struct drm_i915_private *dev_priv = dev->dev_private;
  8210. intel_init_clock_gating(dev);
  8211. if (IS_IRONLAKE_M(dev)) {
  8212. ironlake_enable_drps(dev);
  8213. intel_init_emon(dev);
  8214. }
  8215. if ((IS_GEN6(dev) || IS_GEN7(dev)) && !IS_VALLEYVIEW(dev)) {
  8216. gen6_enable_rps(dev_priv);
  8217. gen6_update_ring_freq(dev_priv);
  8218. }
  8219. if (IS_IVYBRIDGE(dev))
  8220. ivb_pch_pwm_override(dev);
  8221. }
  8222. void intel_modeset_init(struct drm_device *dev)
  8223. {
  8224. struct drm_i915_private *dev_priv = dev->dev_private;
  8225. int i, ret;
  8226. drm_mode_config_init(dev);
  8227. dev->mode_config.min_width = 0;
  8228. dev->mode_config.min_height = 0;
  8229. dev->mode_config.preferred_depth = 24;
  8230. dev->mode_config.prefer_shadow = 1;
  8231. dev->mode_config.funcs = (void *)&intel_mode_funcs;
  8232. intel_init_quirks(dev);
  8233. intel_init_display(dev);
  8234. if (IS_GEN2(dev)) {
  8235. dev->mode_config.max_width = 2048;
  8236. dev->mode_config.max_height = 2048;
  8237. } else if (IS_GEN3(dev)) {
  8238. dev->mode_config.max_width = 4096;
  8239. dev->mode_config.max_height = 4096;
  8240. } else {
  8241. dev->mode_config.max_width = 8192;
  8242. dev->mode_config.max_height = 8192;
  8243. }
  8244. dev->mode_config.fb_base = dev->agp->base;
  8245. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  8246. dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
  8247. for (i = 0; i < dev_priv->num_pipe; i++) {
  8248. intel_crtc_init(dev, i);
  8249. ret = intel_plane_init(dev, i);
  8250. if (ret)
  8251. DRM_DEBUG_KMS("plane %d init failed: %d\n", i, ret);
  8252. }
  8253. /* Just disable it once at startup */
  8254. i915_disable_vga(dev);
  8255. intel_setup_outputs(dev);
  8256. intel_modeset_init_hw(dev);
  8257. INIT_WORK(&dev_priv->idle_work, intel_idle_update);
  8258. setup_timer(&dev_priv->idle_timer, intel_gpu_idle_timer,
  8259. (unsigned long)dev);
  8260. }
  8261. void intel_modeset_gem_init(struct drm_device *dev)
  8262. {
  8263. if (IS_IRONLAKE_M(dev))
  8264. ironlake_enable_rc6(dev);
  8265. intel_setup_overlay(dev);
  8266. }
  8267. void intel_modeset_cleanup(struct drm_device *dev)
  8268. {
  8269. struct drm_i915_private *dev_priv = dev->dev_private;
  8270. struct drm_crtc *crtc;
  8271. struct intel_crtc *intel_crtc;
  8272. drm_kms_helper_poll_fini(dev);
  8273. mutex_lock(&dev->struct_mutex);
  8274. intel_unregister_dsm_handler();
  8275. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  8276. /* Skip inactive CRTCs */
  8277. if (!crtc->fb)
  8278. continue;
  8279. intel_crtc = to_intel_crtc(crtc);
  8280. intel_increase_pllclock(crtc);
  8281. }
  8282. intel_disable_fbc(dev);
  8283. if (IS_IRONLAKE_M(dev))
  8284. ironlake_disable_drps(dev);
  8285. if ((IS_GEN6(dev) || IS_GEN7(dev)) && !IS_VALLEYVIEW(dev))
  8286. gen6_disable_rps(dev);
  8287. if (IS_IRONLAKE_M(dev))
  8288. ironlake_disable_rc6(dev);
  8289. if (IS_VALLEYVIEW(dev))
  8290. vlv_init_dpio(dev);
  8291. mutex_unlock(&dev->struct_mutex);
  8292. /* Disable the irq before mode object teardown, for the irq might
  8293. * enqueue unpin/hotplug work. */
  8294. drm_irq_uninstall(dev);
  8295. cancel_work_sync(&dev_priv->hotplug_work);
  8296. cancel_work_sync(&dev_priv->rps_work);
  8297. /* flush any delayed tasks or pending work */
  8298. flush_scheduled_work();
  8299. /* Shut off idle work before the crtcs get freed. */
  8300. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  8301. intel_crtc = to_intel_crtc(crtc);
  8302. del_timer_sync(&intel_crtc->idle_timer);
  8303. }
  8304. del_timer_sync(&dev_priv->idle_timer);
  8305. cancel_work_sync(&dev_priv->idle_work);
  8306. drm_mode_config_cleanup(dev);
  8307. }
  8308. /*
  8309. * Return which encoder is currently attached for connector.
  8310. */
  8311. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  8312. {
  8313. return &intel_attached_encoder(connector)->base;
  8314. }
  8315. void intel_connector_attach_encoder(struct intel_connector *connector,
  8316. struct intel_encoder *encoder)
  8317. {
  8318. connector->encoder = encoder;
  8319. drm_mode_connector_attach_encoder(&connector->base,
  8320. &encoder->base);
  8321. }
  8322. /*
  8323. * set vga decode state - true == enable VGA decode
  8324. */
  8325. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  8326. {
  8327. struct drm_i915_private *dev_priv = dev->dev_private;
  8328. u16 gmch_ctrl;
  8329. pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
  8330. if (state)
  8331. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  8332. else
  8333. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  8334. pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
  8335. return 0;
  8336. }
  8337. #ifdef CONFIG_DEBUG_FS
  8338. #include <linux/seq_file.h>
  8339. struct intel_display_error_state {
  8340. struct intel_cursor_error_state {
  8341. u32 control;
  8342. u32 position;
  8343. u32 base;
  8344. u32 size;
  8345. } cursor[2];
  8346. struct intel_pipe_error_state {
  8347. u32 conf;
  8348. u32 source;
  8349. u32 htotal;
  8350. u32 hblank;
  8351. u32 hsync;
  8352. u32 vtotal;
  8353. u32 vblank;
  8354. u32 vsync;
  8355. } pipe[2];
  8356. struct intel_plane_error_state {
  8357. u32 control;
  8358. u32 stride;
  8359. u32 size;
  8360. u32 pos;
  8361. u32 addr;
  8362. u32 surface;
  8363. u32 tile_offset;
  8364. } plane[2];
  8365. };
  8366. struct intel_display_error_state *
  8367. intel_display_capture_error_state(struct drm_device *dev)
  8368. {
  8369. drm_i915_private_t *dev_priv = dev->dev_private;
  8370. struct intel_display_error_state *error;
  8371. int i;
  8372. error = kmalloc(sizeof(*error), GFP_ATOMIC);
  8373. if (error == NULL)
  8374. return NULL;
  8375. for (i = 0; i < 2; i++) {
  8376. error->cursor[i].control = I915_READ(CURCNTR(i));
  8377. error->cursor[i].position = I915_READ(CURPOS(i));
  8378. error->cursor[i].base = I915_READ(CURBASE(i));
  8379. error->plane[i].control = I915_READ(DSPCNTR(i));
  8380. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  8381. error->plane[i].size = I915_READ(DSPSIZE(i));
  8382. error->plane[i].pos = I915_READ(DSPPOS(i));
  8383. error->plane[i].addr = I915_READ(DSPADDR(i));
  8384. if (INTEL_INFO(dev)->gen >= 4) {
  8385. error->plane[i].surface = I915_READ(DSPSURF(i));
  8386. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  8387. }
  8388. error->pipe[i].conf = I915_READ(PIPECONF(i));
  8389. error->pipe[i].source = I915_READ(PIPESRC(i));
  8390. error->pipe[i].htotal = I915_READ(HTOTAL(i));
  8391. error->pipe[i].hblank = I915_READ(HBLANK(i));
  8392. error->pipe[i].hsync = I915_READ(HSYNC(i));
  8393. error->pipe[i].vtotal = I915_READ(VTOTAL(i));
  8394. error->pipe[i].vblank = I915_READ(VBLANK(i));
  8395. error->pipe[i].vsync = I915_READ(VSYNC(i));
  8396. }
  8397. return error;
  8398. }
  8399. void
  8400. intel_display_print_error_state(struct seq_file *m,
  8401. struct drm_device *dev,
  8402. struct intel_display_error_state *error)
  8403. {
  8404. int i;
  8405. for (i = 0; i < 2; i++) {
  8406. seq_printf(m, "Pipe [%d]:\n", i);
  8407. seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
  8408. seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
  8409. seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
  8410. seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
  8411. seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
  8412. seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
  8413. seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
  8414. seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
  8415. seq_printf(m, "Plane [%d]:\n", i);
  8416. seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
  8417. seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  8418. seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
  8419. seq_printf(m, " POS: %08x\n", error->plane[i].pos);
  8420. seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  8421. if (INTEL_INFO(dev)->gen >= 4) {
  8422. seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
  8423. seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  8424. }
  8425. seq_printf(m, "Cursor [%d]:\n", i);
  8426. seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  8427. seq_printf(m, " POS: %08x\n", error->cursor[i].position);
  8428. seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
  8429. }
  8430. }
  8431. #endif