intel_display.c 265 KB

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