intel_display.c 151 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259
  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/module.h>
  27. #include <linux/input.h>
  28. #include <linux/i2c.h>
  29. #include <linux/kernel.h>
  30. #include "drmP.h"
  31. #include "intel_drv.h"
  32. #include "i915_drm.h"
  33. #include "i915_drv.h"
  34. #include "drm_dp_helper.h"
  35. #include "drm_crtc_helper.h"
  36. #define HAS_eDP (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  37. bool intel_pipe_has_type (struct drm_crtc *crtc, int type);
  38. static void intel_update_watermarks(struct drm_device *dev);
  39. static void intel_increase_pllclock(struct drm_crtc *crtc, bool schedule);
  40. typedef struct {
  41. /* given values */
  42. int n;
  43. int m1, m2;
  44. int p1, p2;
  45. /* derived values */
  46. int dot;
  47. int vco;
  48. int m;
  49. int p;
  50. } intel_clock_t;
  51. typedef struct {
  52. int min, max;
  53. } intel_range_t;
  54. typedef struct {
  55. int dot_limit;
  56. int p2_slow, p2_fast;
  57. } intel_p2_t;
  58. #define INTEL_P2_NUM 2
  59. typedef struct intel_limit intel_limit_t;
  60. struct intel_limit {
  61. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  62. intel_p2_t p2;
  63. bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
  64. int, int, intel_clock_t *);
  65. };
  66. #define I8XX_DOT_MIN 25000
  67. #define I8XX_DOT_MAX 350000
  68. #define I8XX_VCO_MIN 930000
  69. #define I8XX_VCO_MAX 1400000
  70. #define I8XX_N_MIN 3
  71. #define I8XX_N_MAX 16
  72. #define I8XX_M_MIN 96
  73. #define I8XX_M_MAX 140
  74. #define I8XX_M1_MIN 18
  75. #define I8XX_M1_MAX 26
  76. #define I8XX_M2_MIN 6
  77. #define I8XX_M2_MAX 16
  78. #define I8XX_P_MIN 4
  79. #define I8XX_P_MAX 128
  80. #define I8XX_P1_MIN 2
  81. #define I8XX_P1_MAX 33
  82. #define I8XX_P1_LVDS_MIN 1
  83. #define I8XX_P1_LVDS_MAX 6
  84. #define I8XX_P2_SLOW 4
  85. #define I8XX_P2_FAST 2
  86. #define I8XX_P2_LVDS_SLOW 14
  87. #define I8XX_P2_LVDS_FAST 7
  88. #define I8XX_P2_SLOW_LIMIT 165000
  89. #define I9XX_DOT_MIN 20000
  90. #define I9XX_DOT_MAX 400000
  91. #define I9XX_VCO_MIN 1400000
  92. #define I9XX_VCO_MAX 2800000
  93. #define PINEVIEW_VCO_MIN 1700000
  94. #define PINEVIEW_VCO_MAX 3500000
  95. #define I9XX_N_MIN 1
  96. #define I9XX_N_MAX 6
  97. /* Pineview's Ncounter is a ring counter */
  98. #define PINEVIEW_N_MIN 3
  99. #define PINEVIEW_N_MAX 6
  100. #define I9XX_M_MIN 70
  101. #define I9XX_M_MAX 120
  102. #define PINEVIEW_M_MIN 2
  103. #define PINEVIEW_M_MAX 256
  104. #define I9XX_M1_MIN 10
  105. #define I9XX_M1_MAX 22
  106. #define I9XX_M2_MIN 5
  107. #define I9XX_M2_MAX 9
  108. /* Pineview M1 is reserved, and must be 0 */
  109. #define PINEVIEW_M1_MIN 0
  110. #define PINEVIEW_M1_MAX 0
  111. #define PINEVIEW_M2_MIN 0
  112. #define PINEVIEW_M2_MAX 254
  113. #define I9XX_P_SDVO_DAC_MIN 5
  114. #define I9XX_P_SDVO_DAC_MAX 80
  115. #define I9XX_P_LVDS_MIN 7
  116. #define I9XX_P_LVDS_MAX 98
  117. #define PINEVIEW_P_LVDS_MIN 7
  118. #define PINEVIEW_P_LVDS_MAX 112
  119. #define I9XX_P1_MIN 1
  120. #define I9XX_P1_MAX 8
  121. #define I9XX_P2_SDVO_DAC_SLOW 10
  122. #define I9XX_P2_SDVO_DAC_FAST 5
  123. #define I9XX_P2_SDVO_DAC_SLOW_LIMIT 200000
  124. #define I9XX_P2_LVDS_SLOW 14
  125. #define I9XX_P2_LVDS_FAST 7
  126. #define I9XX_P2_LVDS_SLOW_LIMIT 112000
  127. /*The parameter is for SDVO on G4x platform*/
  128. #define G4X_DOT_SDVO_MIN 25000
  129. #define G4X_DOT_SDVO_MAX 270000
  130. #define G4X_VCO_MIN 1750000
  131. #define G4X_VCO_MAX 3500000
  132. #define G4X_N_SDVO_MIN 1
  133. #define G4X_N_SDVO_MAX 4
  134. #define G4X_M_SDVO_MIN 104
  135. #define G4X_M_SDVO_MAX 138
  136. #define G4X_M1_SDVO_MIN 17
  137. #define G4X_M1_SDVO_MAX 23
  138. #define G4X_M2_SDVO_MIN 5
  139. #define G4X_M2_SDVO_MAX 11
  140. #define G4X_P_SDVO_MIN 10
  141. #define G4X_P_SDVO_MAX 30
  142. #define G4X_P1_SDVO_MIN 1
  143. #define G4X_P1_SDVO_MAX 3
  144. #define G4X_P2_SDVO_SLOW 10
  145. #define G4X_P2_SDVO_FAST 10
  146. #define G4X_P2_SDVO_LIMIT 270000
  147. /*The parameter is for HDMI_DAC on G4x platform*/
  148. #define G4X_DOT_HDMI_DAC_MIN 22000
  149. #define G4X_DOT_HDMI_DAC_MAX 400000
  150. #define G4X_N_HDMI_DAC_MIN 1
  151. #define G4X_N_HDMI_DAC_MAX 4
  152. #define G4X_M_HDMI_DAC_MIN 104
  153. #define G4X_M_HDMI_DAC_MAX 138
  154. #define G4X_M1_HDMI_DAC_MIN 16
  155. #define G4X_M1_HDMI_DAC_MAX 23
  156. #define G4X_M2_HDMI_DAC_MIN 5
  157. #define G4X_M2_HDMI_DAC_MAX 11
  158. #define G4X_P_HDMI_DAC_MIN 5
  159. #define G4X_P_HDMI_DAC_MAX 80
  160. #define G4X_P1_HDMI_DAC_MIN 1
  161. #define G4X_P1_HDMI_DAC_MAX 8
  162. #define G4X_P2_HDMI_DAC_SLOW 10
  163. #define G4X_P2_HDMI_DAC_FAST 5
  164. #define G4X_P2_HDMI_DAC_LIMIT 165000
  165. /*The parameter is for SINGLE_CHANNEL_LVDS on G4x platform*/
  166. #define G4X_DOT_SINGLE_CHANNEL_LVDS_MIN 20000
  167. #define G4X_DOT_SINGLE_CHANNEL_LVDS_MAX 115000
  168. #define G4X_N_SINGLE_CHANNEL_LVDS_MIN 1
  169. #define G4X_N_SINGLE_CHANNEL_LVDS_MAX 3
  170. #define G4X_M_SINGLE_CHANNEL_LVDS_MIN 104
  171. #define G4X_M_SINGLE_CHANNEL_LVDS_MAX 138
  172. #define G4X_M1_SINGLE_CHANNEL_LVDS_MIN 17
  173. #define G4X_M1_SINGLE_CHANNEL_LVDS_MAX 23
  174. #define G4X_M2_SINGLE_CHANNEL_LVDS_MIN 5
  175. #define G4X_M2_SINGLE_CHANNEL_LVDS_MAX 11
  176. #define G4X_P_SINGLE_CHANNEL_LVDS_MIN 28
  177. #define G4X_P_SINGLE_CHANNEL_LVDS_MAX 112
  178. #define G4X_P1_SINGLE_CHANNEL_LVDS_MIN 2
  179. #define G4X_P1_SINGLE_CHANNEL_LVDS_MAX 8
  180. #define G4X_P2_SINGLE_CHANNEL_LVDS_SLOW 14
  181. #define G4X_P2_SINGLE_CHANNEL_LVDS_FAST 14
  182. #define G4X_P2_SINGLE_CHANNEL_LVDS_LIMIT 0
  183. /*The parameter is for DUAL_CHANNEL_LVDS on G4x platform*/
  184. #define G4X_DOT_DUAL_CHANNEL_LVDS_MIN 80000
  185. #define G4X_DOT_DUAL_CHANNEL_LVDS_MAX 224000
  186. #define G4X_N_DUAL_CHANNEL_LVDS_MIN 1
  187. #define G4X_N_DUAL_CHANNEL_LVDS_MAX 3
  188. #define G4X_M_DUAL_CHANNEL_LVDS_MIN 104
  189. #define G4X_M_DUAL_CHANNEL_LVDS_MAX 138
  190. #define G4X_M1_DUAL_CHANNEL_LVDS_MIN 17
  191. #define G4X_M1_DUAL_CHANNEL_LVDS_MAX 23
  192. #define G4X_M2_DUAL_CHANNEL_LVDS_MIN 5
  193. #define G4X_M2_DUAL_CHANNEL_LVDS_MAX 11
  194. #define G4X_P_DUAL_CHANNEL_LVDS_MIN 14
  195. #define G4X_P_DUAL_CHANNEL_LVDS_MAX 42
  196. #define G4X_P1_DUAL_CHANNEL_LVDS_MIN 2
  197. #define G4X_P1_DUAL_CHANNEL_LVDS_MAX 6
  198. #define G4X_P2_DUAL_CHANNEL_LVDS_SLOW 7
  199. #define G4X_P2_DUAL_CHANNEL_LVDS_FAST 7
  200. #define G4X_P2_DUAL_CHANNEL_LVDS_LIMIT 0
  201. /*The parameter is for DISPLAY PORT on G4x platform*/
  202. #define G4X_DOT_DISPLAY_PORT_MIN 161670
  203. #define G4X_DOT_DISPLAY_PORT_MAX 227000
  204. #define G4X_N_DISPLAY_PORT_MIN 1
  205. #define G4X_N_DISPLAY_PORT_MAX 2
  206. #define G4X_M_DISPLAY_PORT_MIN 97
  207. #define G4X_M_DISPLAY_PORT_MAX 108
  208. #define G4X_M1_DISPLAY_PORT_MIN 0x10
  209. #define G4X_M1_DISPLAY_PORT_MAX 0x12
  210. #define G4X_M2_DISPLAY_PORT_MIN 0x05
  211. #define G4X_M2_DISPLAY_PORT_MAX 0x06
  212. #define G4X_P_DISPLAY_PORT_MIN 10
  213. #define G4X_P_DISPLAY_PORT_MAX 20
  214. #define G4X_P1_DISPLAY_PORT_MIN 1
  215. #define G4X_P1_DISPLAY_PORT_MAX 2
  216. #define G4X_P2_DISPLAY_PORT_SLOW 10
  217. #define G4X_P2_DISPLAY_PORT_FAST 10
  218. #define G4X_P2_DISPLAY_PORT_LIMIT 0
  219. /* Ironlake / Sandybridge */
  220. /* as we calculate clock using (register_value + 2) for
  221. N/M1/M2, so here the range value for them is (actual_value-2).
  222. */
  223. #define IRONLAKE_DOT_MIN 25000
  224. #define IRONLAKE_DOT_MAX 350000
  225. #define IRONLAKE_VCO_MIN 1760000
  226. #define IRONLAKE_VCO_MAX 3510000
  227. #define IRONLAKE_M1_MIN 12
  228. #define IRONLAKE_M1_MAX 22
  229. #define IRONLAKE_M2_MIN 5
  230. #define IRONLAKE_M2_MAX 9
  231. #define IRONLAKE_P2_DOT_LIMIT 225000 /* 225Mhz */
  232. /* We have parameter ranges for different type of outputs. */
  233. /* DAC & HDMI Refclk 120Mhz */
  234. #define IRONLAKE_DAC_N_MIN 1
  235. #define IRONLAKE_DAC_N_MAX 5
  236. #define IRONLAKE_DAC_M_MIN 79
  237. #define IRONLAKE_DAC_M_MAX 127
  238. #define IRONLAKE_DAC_P_MIN 5
  239. #define IRONLAKE_DAC_P_MAX 80
  240. #define IRONLAKE_DAC_P1_MIN 1
  241. #define IRONLAKE_DAC_P1_MAX 8
  242. #define IRONLAKE_DAC_P2_SLOW 10
  243. #define IRONLAKE_DAC_P2_FAST 5
  244. /* LVDS single-channel 120Mhz refclk */
  245. #define IRONLAKE_LVDS_S_N_MIN 1
  246. #define IRONLAKE_LVDS_S_N_MAX 3
  247. #define IRONLAKE_LVDS_S_M_MIN 79
  248. #define IRONLAKE_LVDS_S_M_MAX 118
  249. #define IRONLAKE_LVDS_S_P_MIN 28
  250. #define IRONLAKE_LVDS_S_P_MAX 112
  251. #define IRONLAKE_LVDS_S_P1_MIN 2
  252. #define IRONLAKE_LVDS_S_P1_MAX 8
  253. #define IRONLAKE_LVDS_S_P2_SLOW 14
  254. #define IRONLAKE_LVDS_S_P2_FAST 14
  255. /* LVDS dual-channel 120Mhz refclk */
  256. #define IRONLAKE_LVDS_D_N_MIN 1
  257. #define IRONLAKE_LVDS_D_N_MAX 3
  258. #define IRONLAKE_LVDS_D_M_MIN 79
  259. #define IRONLAKE_LVDS_D_M_MAX 127
  260. #define IRONLAKE_LVDS_D_P_MIN 14
  261. #define IRONLAKE_LVDS_D_P_MAX 56
  262. #define IRONLAKE_LVDS_D_P1_MIN 2
  263. #define IRONLAKE_LVDS_D_P1_MAX 8
  264. #define IRONLAKE_LVDS_D_P2_SLOW 7
  265. #define IRONLAKE_LVDS_D_P2_FAST 7
  266. /* LVDS single-channel 100Mhz refclk */
  267. #define IRONLAKE_LVDS_S_SSC_N_MIN 1
  268. #define IRONLAKE_LVDS_S_SSC_N_MAX 2
  269. #define IRONLAKE_LVDS_S_SSC_M_MIN 79
  270. #define IRONLAKE_LVDS_S_SSC_M_MAX 126
  271. #define IRONLAKE_LVDS_S_SSC_P_MIN 28
  272. #define IRONLAKE_LVDS_S_SSC_P_MAX 112
  273. #define IRONLAKE_LVDS_S_SSC_P1_MIN 2
  274. #define IRONLAKE_LVDS_S_SSC_P1_MAX 8
  275. #define IRONLAKE_LVDS_S_SSC_P2_SLOW 14
  276. #define IRONLAKE_LVDS_S_SSC_P2_FAST 14
  277. /* LVDS dual-channel 100Mhz refclk */
  278. #define IRONLAKE_LVDS_D_SSC_N_MIN 1
  279. #define IRONLAKE_LVDS_D_SSC_N_MAX 3
  280. #define IRONLAKE_LVDS_D_SSC_M_MIN 79
  281. #define IRONLAKE_LVDS_D_SSC_M_MAX 126
  282. #define IRONLAKE_LVDS_D_SSC_P_MIN 14
  283. #define IRONLAKE_LVDS_D_SSC_P_MAX 42
  284. #define IRONLAKE_LVDS_D_SSC_P1_MIN 2
  285. #define IRONLAKE_LVDS_D_SSC_P1_MAX 6
  286. #define IRONLAKE_LVDS_D_SSC_P2_SLOW 7
  287. #define IRONLAKE_LVDS_D_SSC_P2_FAST 7
  288. /* DisplayPort */
  289. #define IRONLAKE_DP_N_MIN 1
  290. #define IRONLAKE_DP_N_MAX 2
  291. #define IRONLAKE_DP_M_MIN 81
  292. #define IRONLAKE_DP_M_MAX 90
  293. #define IRONLAKE_DP_P_MIN 10
  294. #define IRONLAKE_DP_P_MAX 20
  295. #define IRONLAKE_DP_P2_FAST 10
  296. #define IRONLAKE_DP_P2_SLOW 10
  297. #define IRONLAKE_DP_P2_LIMIT 0
  298. #define IRONLAKE_DP_P1_MIN 1
  299. #define IRONLAKE_DP_P1_MAX 2
  300. static bool
  301. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  302. int target, int refclk, intel_clock_t *best_clock);
  303. static bool
  304. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  305. int target, int refclk, intel_clock_t *best_clock);
  306. static bool
  307. intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
  308. int target, int refclk, intel_clock_t *best_clock);
  309. static bool
  310. intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
  311. int target, int refclk, intel_clock_t *best_clock);
  312. static const intel_limit_t intel_limits_i8xx_dvo = {
  313. .dot = { .min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX },
  314. .vco = { .min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX },
  315. .n = { .min = I8XX_N_MIN, .max = I8XX_N_MAX },
  316. .m = { .min = I8XX_M_MIN, .max = I8XX_M_MAX },
  317. .m1 = { .min = I8XX_M1_MIN, .max = I8XX_M1_MAX },
  318. .m2 = { .min = I8XX_M2_MIN, .max = I8XX_M2_MAX },
  319. .p = { .min = I8XX_P_MIN, .max = I8XX_P_MAX },
  320. .p1 = { .min = I8XX_P1_MIN, .max = I8XX_P1_MAX },
  321. .p2 = { .dot_limit = I8XX_P2_SLOW_LIMIT,
  322. .p2_slow = I8XX_P2_SLOW, .p2_fast = I8XX_P2_FAST },
  323. .find_pll = intel_find_best_PLL,
  324. };
  325. static const intel_limit_t intel_limits_i8xx_lvds = {
  326. .dot = { .min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX },
  327. .vco = { .min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX },
  328. .n = { .min = I8XX_N_MIN, .max = I8XX_N_MAX },
  329. .m = { .min = I8XX_M_MIN, .max = I8XX_M_MAX },
  330. .m1 = { .min = I8XX_M1_MIN, .max = I8XX_M1_MAX },
  331. .m2 = { .min = I8XX_M2_MIN, .max = I8XX_M2_MAX },
  332. .p = { .min = I8XX_P_MIN, .max = I8XX_P_MAX },
  333. .p1 = { .min = I8XX_P1_LVDS_MIN, .max = I8XX_P1_LVDS_MAX },
  334. .p2 = { .dot_limit = I8XX_P2_SLOW_LIMIT,
  335. .p2_slow = I8XX_P2_LVDS_SLOW, .p2_fast = I8XX_P2_LVDS_FAST },
  336. .find_pll = intel_find_best_PLL,
  337. };
  338. static const intel_limit_t intel_limits_i9xx_sdvo = {
  339. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
  340. .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
  341. .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
  342. .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
  343. .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
  344. .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
  345. .p = { .min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX },
  346. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  347. .p2 = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
  348. .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast = I9XX_P2_SDVO_DAC_FAST },
  349. .find_pll = intel_find_best_PLL,
  350. };
  351. static const intel_limit_t intel_limits_i9xx_lvds = {
  352. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
  353. .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
  354. .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
  355. .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
  356. .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
  357. .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
  358. .p = { .min = I9XX_P_LVDS_MIN, .max = I9XX_P_LVDS_MAX },
  359. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  360. /* The single-channel range is 25-112Mhz, and dual-channel
  361. * is 80-224Mhz. Prefer single channel as much as possible.
  362. */
  363. .p2 = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
  364. .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_FAST },
  365. .find_pll = intel_find_best_PLL,
  366. };
  367. /* below parameter and function is for G4X Chipset Family*/
  368. static const intel_limit_t intel_limits_g4x_sdvo = {
  369. .dot = { .min = G4X_DOT_SDVO_MIN, .max = G4X_DOT_SDVO_MAX },
  370. .vco = { .min = G4X_VCO_MIN, .max = G4X_VCO_MAX},
  371. .n = { .min = G4X_N_SDVO_MIN, .max = G4X_N_SDVO_MAX },
  372. .m = { .min = G4X_M_SDVO_MIN, .max = G4X_M_SDVO_MAX },
  373. .m1 = { .min = G4X_M1_SDVO_MIN, .max = G4X_M1_SDVO_MAX },
  374. .m2 = { .min = G4X_M2_SDVO_MIN, .max = G4X_M2_SDVO_MAX },
  375. .p = { .min = G4X_P_SDVO_MIN, .max = G4X_P_SDVO_MAX },
  376. .p1 = { .min = G4X_P1_SDVO_MIN, .max = G4X_P1_SDVO_MAX},
  377. .p2 = { .dot_limit = G4X_P2_SDVO_LIMIT,
  378. .p2_slow = G4X_P2_SDVO_SLOW,
  379. .p2_fast = G4X_P2_SDVO_FAST
  380. },
  381. .find_pll = intel_g4x_find_best_PLL,
  382. };
  383. static const intel_limit_t intel_limits_g4x_hdmi = {
  384. .dot = { .min = G4X_DOT_HDMI_DAC_MIN, .max = G4X_DOT_HDMI_DAC_MAX },
  385. .vco = { .min = G4X_VCO_MIN, .max = G4X_VCO_MAX},
  386. .n = { .min = G4X_N_HDMI_DAC_MIN, .max = G4X_N_HDMI_DAC_MAX },
  387. .m = { .min = G4X_M_HDMI_DAC_MIN, .max = G4X_M_HDMI_DAC_MAX },
  388. .m1 = { .min = G4X_M1_HDMI_DAC_MIN, .max = G4X_M1_HDMI_DAC_MAX },
  389. .m2 = { .min = G4X_M2_HDMI_DAC_MIN, .max = G4X_M2_HDMI_DAC_MAX },
  390. .p = { .min = G4X_P_HDMI_DAC_MIN, .max = G4X_P_HDMI_DAC_MAX },
  391. .p1 = { .min = G4X_P1_HDMI_DAC_MIN, .max = G4X_P1_HDMI_DAC_MAX},
  392. .p2 = { .dot_limit = G4X_P2_HDMI_DAC_LIMIT,
  393. .p2_slow = G4X_P2_HDMI_DAC_SLOW,
  394. .p2_fast = G4X_P2_HDMI_DAC_FAST
  395. },
  396. .find_pll = intel_g4x_find_best_PLL,
  397. };
  398. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  399. .dot = { .min = G4X_DOT_SINGLE_CHANNEL_LVDS_MIN,
  400. .max = G4X_DOT_SINGLE_CHANNEL_LVDS_MAX },
  401. .vco = { .min = G4X_VCO_MIN,
  402. .max = G4X_VCO_MAX },
  403. .n = { .min = G4X_N_SINGLE_CHANNEL_LVDS_MIN,
  404. .max = G4X_N_SINGLE_CHANNEL_LVDS_MAX },
  405. .m = { .min = G4X_M_SINGLE_CHANNEL_LVDS_MIN,
  406. .max = G4X_M_SINGLE_CHANNEL_LVDS_MAX },
  407. .m1 = { .min = G4X_M1_SINGLE_CHANNEL_LVDS_MIN,
  408. .max = G4X_M1_SINGLE_CHANNEL_LVDS_MAX },
  409. .m2 = { .min = G4X_M2_SINGLE_CHANNEL_LVDS_MIN,
  410. .max = G4X_M2_SINGLE_CHANNEL_LVDS_MAX },
  411. .p = { .min = G4X_P_SINGLE_CHANNEL_LVDS_MIN,
  412. .max = G4X_P_SINGLE_CHANNEL_LVDS_MAX },
  413. .p1 = { .min = G4X_P1_SINGLE_CHANNEL_LVDS_MIN,
  414. .max = G4X_P1_SINGLE_CHANNEL_LVDS_MAX },
  415. .p2 = { .dot_limit = G4X_P2_SINGLE_CHANNEL_LVDS_LIMIT,
  416. .p2_slow = G4X_P2_SINGLE_CHANNEL_LVDS_SLOW,
  417. .p2_fast = G4X_P2_SINGLE_CHANNEL_LVDS_FAST
  418. },
  419. .find_pll = intel_g4x_find_best_PLL,
  420. };
  421. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  422. .dot = { .min = G4X_DOT_DUAL_CHANNEL_LVDS_MIN,
  423. .max = G4X_DOT_DUAL_CHANNEL_LVDS_MAX },
  424. .vco = { .min = G4X_VCO_MIN,
  425. .max = G4X_VCO_MAX },
  426. .n = { .min = G4X_N_DUAL_CHANNEL_LVDS_MIN,
  427. .max = G4X_N_DUAL_CHANNEL_LVDS_MAX },
  428. .m = { .min = G4X_M_DUAL_CHANNEL_LVDS_MIN,
  429. .max = G4X_M_DUAL_CHANNEL_LVDS_MAX },
  430. .m1 = { .min = G4X_M1_DUAL_CHANNEL_LVDS_MIN,
  431. .max = G4X_M1_DUAL_CHANNEL_LVDS_MAX },
  432. .m2 = { .min = G4X_M2_DUAL_CHANNEL_LVDS_MIN,
  433. .max = G4X_M2_DUAL_CHANNEL_LVDS_MAX },
  434. .p = { .min = G4X_P_DUAL_CHANNEL_LVDS_MIN,
  435. .max = G4X_P_DUAL_CHANNEL_LVDS_MAX },
  436. .p1 = { .min = G4X_P1_DUAL_CHANNEL_LVDS_MIN,
  437. .max = G4X_P1_DUAL_CHANNEL_LVDS_MAX },
  438. .p2 = { .dot_limit = G4X_P2_DUAL_CHANNEL_LVDS_LIMIT,
  439. .p2_slow = G4X_P2_DUAL_CHANNEL_LVDS_SLOW,
  440. .p2_fast = G4X_P2_DUAL_CHANNEL_LVDS_FAST
  441. },
  442. .find_pll = intel_g4x_find_best_PLL,
  443. };
  444. static const intel_limit_t intel_limits_g4x_display_port = {
  445. .dot = { .min = G4X_DOT_DISPLAY_PORT_MIN,
  446. .max = G4X_DOT_DISPLAY_PORT_MAX },
  447. .vco = { .min = G4X_VCO_MIN,
  448. .max = G4X_VCO_MAX},
  449. .n = { .min = G4X_N_DISPLAY_PORT_MIN,
  450. .max = G4X_N_DISPLAY_PORT_MAX },
  451. .m = { .min = G4X_M_DISPLAY_PORT_MIN,
  452. .max = G4X_M_DISPLAY_PORT_MAX },
  453. .m1 = { .min = G4X_M1_DISPLAY_PORT_MIN,
  454. .max = G4X_M1_DISPLAY_PORT_MAX },
  455. .m2 = { .min = G4X_M2_DISPLAY_PORT_MIN,
  456. .max = G4X_M2_DISPLAY_PORT_MAX },
  457. .p = { .min = G4X_P_DISPLAY_PORT_MIN,
  458. .max = G4X_P_DISPLAY_PORT_MAX },
  459. .p1 = { .min = G4X_P1_DISPLAY_PORT_MIN,
  460. .max = G4X_P1_DISPLAY_PORT_MAX},
  461. .p2 = { .dot_limit = G4X_P2_DISPLAY_PORT_LIMIT,
  462. .p2_slow = G4X_P2_DISPLAY_PORT_SLOW,
  463. .p2_fast = G4X_P2_DISPLAY_PORT_FAST },
  464. .find_pll = intel_find_pll_g4x_dp,
  465. };
  466. static const intel_limit_t intel_limits_pineview_sdvo = {
  467. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX},
  468. .vco = { .min = PINEVIEW_VCO_MIN, .max = PINEVIEW_VCO_MAX },
  469. .n = { .min = PINEVIEW_N_MIN, .max = PINEVIEW_N_MAX },
  470. .m = { .min = PINEVIEW_M_MIN, .max = PINEVIEW_M_MAX },
  471. .m1 = { .min = PINEVIEW_M1_MIN, .max = PINEVIEW_M1_MAX },
  472. .m2 = { .min = PINEVIEW_M2_MIN, .max = PINEVIEW_M2_MAX },
  473. .p = { .min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX },
  474. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  475. .p2 = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
  476. .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast = I9XX_P2_SDVO_DAC_FAST },
  477. .find_pll = intel_find_best_PLL,
  478. };
  479. static const intel_limit_t intel_limits_pineview_lvds = {
  480. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
  481. .vco = { .min = PINEVIEW_VCO_MIN, .max = PINEVIEW_VCO_MAX },
  482. .n = { .min = PINEVIEW_N_MIN, .max = PINEVIEW_N_MAX },
  483. .m = { .min = PINEVIEW_M_MIN, .max = PINEVIEW_M_MAX },
  484. .m1 = { .min = PINEVIEW_M1_MIN, .max = PINEVIEW_M1_MAX },
  485. .m2 = { .min = PINEVIEW_M2_MIN, .max = PINEVIEW_M2_MAX },
  486. .p = { .min = PINEVIEW_P_LVDS_MIN, .max = PINEVIEW_P_LVDS_MAX },
  487. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  488. /* Pineview only supports single-channel mode. */
  489. .p2 = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
  490. .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_SLOW },
  491. .find_pll = intel_find_best_PLL,
  492. };
  493. static const intel_limit_t intel_limits_ironlake_dac = {
  494. .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
  495. .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
  496. .n = { .min = IRONLAKE_DAC_N_MIN, .max = IRONLAKE_DAC_N_MAX },
  497. .m = { .min = IRONLAKE_DAC_M_MIN, .max = IRONLAKE_DAC_M_MAX },
  498. .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
  499. .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
  500. .p = { .min = IRONLAKE_DAC_P_MIN, .max = IRONLAKE_DAC_P_MAX },
  501. .p1 = { .min = IRONLAKE_DAC_P1_MIN, .max = IRONLAKE_DAC_P1_MAX },
  502. .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
  503. .p2_slow = IRONLAKE_DAC_P2_SLOW,
  504. .p2_fast = IRONLAKE_DAC_P2_FAST },
  505. .find_pll = intel_g4x_find_best_PLL,
  506. };
  507. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  508. .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
  509. .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
  510. .n = { .min = IRONLAKE_LVDS_S_N_MIN, .max = IRONLAKE_LVDS_S_N_MAX },
  511. .m = { .min = IRONLAKE_LVDS_S_M_MIN, .max = IRONLAKE_LVDS_S_M_MAX },
  512. .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
  513. .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
  514. .p = { .min = IRONLAKE_LVDS_S_P_MIN, .max = IRONLAKE_LVDS_S_P_MAX },
  515. .p1 = { .min = IRONLAKE_LVDS_S_P1_MIN, .max = IRONLAKE_LVDS_S_P1_MAX },
  516. .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
  517. .p2_slow = IRONLAKE_LVDS_S_P2_SLOW,
  518. .p2_fast = IRONLAKE_LVDS_S_P2_FAST },
  519. .find_pll = intel_g4x_find_best_PLL,
  520. };
  521. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  522. .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
  523. .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
  524. .n = { .min = IRONLAKE_LVDS_D_N_MIN, .max = IRONLAKE_LVDS_D_N_MAX },
  525. .m = { .min = IRONLAKE_LVDS_D_M_MIN, .max = IRONLAKE_LVDS_D_M_MAX },
  526. .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
  527. .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
  528. .p = { .min = IRONLAKE_LVDS_D_P_MIN, .max = IRONLAKE_LVDS_D_P_MAX },
  529. .p1 = { .min = IRONLAKE_LVDS_D_P1_MIN, .max = IRONLAKE_LVDS_D_P1_MAX },
  530. .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
  531. .p2_slow = IRONLAKE_LVDS_D_P2_SLOW,
  532. .p2_fast = IRONLAKE_LVDS_D_P2_FAST },
  533. .find_pll = intel_g4x_find_best_PLL,
  534. };
  535. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  536. .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
  537. .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
  538. .n = { .min = IRONLAKE_LVDS_S_SSC_N_MIN, .max = IRONLAKE_LVDS_S_SSC_N_MAX },
  539. .m = { .min = IRONLAKE_LVDS_S_SSC_M_MIN, .max = IRONLAKE_LVDS_S_SSC_M_MAX },
  540. .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
  541. .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
  542. .p = { .min = IRONLAKE_LVDS_S_SSC_P_MIN, .max = IRONLAKE_LVDS_S_SSC_P_MAX },
  543. .p1 = { .min = IRONLAKE_LVDS_S_SSC_P1_MIN,.max = IRONLAKE_LVDS_S_SSC_P1_MAX },
  544. .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
  545. .p2_slow = IRONLAKE_LVDS_S_SSC_P2_SLOW,
  546. .p2_fast = IRONLAKE_LVDS_S_SSC_P2_FAST },
  547. .find_pll = intel_g4x_find_best_PLL,
  548. };
  549. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  550. .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
  551. .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
  552. .n = { .min = IRONLAKE_LVDS_D_SSC_N_MIN, .max = IRONLAKE_LVDS_D_SSC_N_MAX },
  553. .m = { .min = IRONLAKE_LVDS_D_SSC_M_MIN, .max = IRONLAKE_LVDS_D_SSC_M_MAX },
  554. .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
  555. .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
  556. .p = { .min = IRONLAKE_LVDS_D_SSC_P_MIN, .max = IRONLAKE_LVDS_D_SSC_P_MAX },
  557. .p1 = { .min = IRONLAKE_LVDS_D_SSC_P1_MIN,.max = IRONLAKE_LVDS_D_SSC_P1_MAX },
  558. .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
  559. .p2_slow = IRONLAKE_LVDS_D_SSC_P2_SLOW,
  560. .p2_fast = IRONLAKE_LVDS_D_SSC_P2_FAST },
  561. .find_pll = intel_g4x_find_best_PLL,
  562. };
  563. static const intel_limit_t intel_limits_ironlake_display_port = {
  564. .dot = { .min = IRONLAKE_DOT_MIN,
  565. .max = IRONLAKE_DOT_MAX },
  566. .vco = { .min = IRONLAKE_VCO_MIN,
  567. .max = IRONLAKE_VCO_MAX},
  568. .n = { .min = IRONLAKE_DP_N_MIN,
  569. .max = IRONLAKE_DP_N_MAX },
  570. .m = { .min = IRONLAKE_DP_M_MIN,
  571. .max = IRONLAKE_DP_M_MAX },
  572. .m1 = { .min = IRONLAKE_M1_MIN,
  573. .max = IRONLAKE_M1_MAX },
  574. .m2 = { .min = IRONLAKE_M2_MIN,
  575. .max = IRONLAKE_M2_MAX },
  576. .p = { .min = IRONLAKE_DP_P_MIN,
  577. .max = IRONLAKE_DP_P_MAX },
  578. .p1 = { .min = IRONLAKE_DP_P1_MIN,
  579. .max = IRONLAKE_DP_P1_MAX},
  580. .p2 = { .dot_limit = IRONLAKE_DP_P2_LIMIT,
  581. .p2_slow = IRONLAKE_DP_P2_SLOW,
  582. .p2_fast = IRONLAKE_DP_P2_FAST },
  583. .find_pll = intel_find_pll_ironlake_dp,
  584. };
  585. static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc)
  586. {
  587. struct drm_device *dev = crtc->dev;
  588. struct drm_i915_private *dev_priv = dev->dev_private;
  589. const intel_limit_t *limit;
  590. int refclk = 120;
  591. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  592. if (dev_priv->lvds_use_ssc && dev_priv->lvds_ssc_freq == 100)
  593. refclk = 100;
  594. if ((I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) ==
  595. LVDS_CLKB_POWER_UP) {
  596. /* LVDS dual channel */
  597. if (refclk == 100)
  598. limit = &intel_limits_ironlake_dual_lvds_100m;
  599. else
  600. limit = &intel_limits_ironlake_dual_lvds;
  601. } else {
  602. if (refclk == 100)
  603. limit = &intel_limits_ironlake_single_lvds_100m;
  604. else
  605. limit = &intel_limits_ironlake_single_lvds;
  606. }
  607. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  608. HAS_eDP)
  609. limit = &intel_limits_ironlake_display_port;
  610. else
  611. limit = &intel_limits_ironlake_dac;
  612. return limit;
  613. }
  614. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  615. {
  616. struct drm_device *dev = crtc->dev;
  617. struct drm_i915_private *dev_priv = dev->dev_private;
  618. const intel_limit_t *limit;
  619. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  620. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  621. LVDS_CLKB_POWER_UP)
  622. /* LVDS with dual channel */
  623. limit = &intel_limits_g4x_dual_channel_lvds;
  624. else
  625. /* LVDS with dual channel */
  626. limit = &intel_limits_g4x_single_channel_lvds;
  627. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  628. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  629. limit = &intel_limits_g4x_hdmi;
  630. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  631. limit = &intel_limits_g4x_sdvo;
  632. } else if (intel_pipe_has_type (crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  633. limit = &intel_limits_g4x_display_port;
  634. } else /* The option is for other outputs */
  635. limit = &intel_limits_i9xx_sdvo;
  636. return limit;
  637. }
  638. static const intel_limit_t *intel_limit(struct drm_crtc *crtc)
  639. {
  640. struct drm_device *dev = crtc->dev;
  641. const intel_limit_t *limit;
  642. if (HAS_PCH_SPLIT(dev))
  643. limit = intel_ironlake_limit(crtc);
  644. else if (IS_G4X(dev)) {
  645. limit = intel_g4x_limit(crtc);
  646. } else if (IS_I9XX(dev) && !IS_PINEVIEW(dev)) {
  647. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  648. limit = &intel_limits_i9xx_lvds;
  649. else
  650. limit = &intel_limits_i9xx_sdvo;
  651. } else if (IS_PINEVIEW(dev)) {
  652. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  653. limit = &intel_limits_pineview_lvds;
  654. else
  655. limit = &intel_limits_pineview_sdvo;
  656. } else {
  657. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  658. limit = &intel_limits_i8xx_lvds;
  659. else
  660. limit = &intel_limits_i8xx_dvo;
  661. }
  662. return limit;
  663. }
  664. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  665. static void pineview_clock(int refclk, intel_clock_t *clock)
  666. {
  667. clock->m = clock->m2 + 2;
  668. clock->p = clock->p1 * clock->p2;
  669. clock->vco = refclk * clock->m / clock->n;
  670. clock->dot = clock->vco / clock->p;
  671. }
  672. static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
  673. {
  674. if (IS_PINEVIEW(dev)) {
  675. pineview_clock(refclk, clock);
  676. return;
  677. }
  678. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  679. clock->p = clock->p1 * clock->p2;
  680. clock->vco = refclk * clock->m / (clock->n + 2);
  681. clock->dot = clock->vco / clock->p;
  682. }
  683. /**
  684. * Returns whether any output on the specified pipe is of the specified type
  685. */
  686. bool intel_pipe_has_type (struct drm_crtc *crtc, int type)
  687. {
  688. struct drm_device *dev = crtc->dev;
  689. struct drm_mode_config *mode_config = &dev->mode_config;
  690. struct drm_encoder *l_entry;
  691. list_for_each_entry(l_entry, &mode_config->encoder_list, head) {
  692. if (l_entry && l_entry->crtc == crtc) {
  693. struct intel_encoder *intel_encoder = enc_to_intel_encoder(l_entry);
  694. if (intel_encoder->type == type)
  695. return true;
  696. }
  697. }
  698. return false;
  699. }
  700. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  701. /**
  702. * Returns whether the given set of divisors are valid for a given refclk with
  703. * the given connectors.
  704. */
  705. static bool intel_PLL_is_valid(struct drm_crtc *crtc, intel_clock_t *clock)
  706. {
  707. const intel_limit_t *limit = intel_limit (crtc);
  708. struct drm_device *dev = crtc->dev;
  709. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  710. INTELPllInvalid ("p1 out of range\n");
  711. if (clock->p < limit->p.min || limit->p.max < clock->p)
  712. INTELPllInvalid ("p out of range\n");
  713. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  714. INTELPllInvalid ("m2 out of range\n");
  715. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  716. INTELPllInvalid ("m1 out of range\n");
  717. if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
  718. INTELPllInvalid ("m1 <= m2\n");
  719. if (clock->m < limit->m.min || limit->m.max < clock->m)
  720. INTELPllInvalid ("m out of range\n");
  721. if (clock->n < limit->n.min || limit->n.max < clock->n)
  722. INTELPllInvalid ("n out of range\n");
  723. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  724. INTELPllInvalid ("vco out of range\n");
  725. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  726. * connector, etc., rather than just a single range.
  727. */
  728. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  729. INTELPllInvalid ("dot out of range\n");
  730. return true;
  731. }
  732. static bool
  733. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  734. int target, int refclk, intel_clock_t *best_clock)
  735. {
  736. struct drm_device *dev = crtc->dev;
  737. struct drm_i915_private *dev_priv = dev->dev_private;
  738. intel_clock_t clock;
  739. int err = target;
  740. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  741. (I915_READ(LVDS)) != 0) {
  742. /*
  743. * For LVDS, if the panel is on, just rely on its current
  744. * settings for dual-channel. We haven't figured out how to
  745. * reliably set up different single/dual channel state, if we
  746. * even can.
  747. */
  748. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  749. LVDS_CLKB_POWER_UP)
  750. clock.p2 = limit->p2.p2_fast;
  751. else
  752. clock.p2 = limit->p2.p2_slow;
  753. } else {
  754. if (target < limit->p2.dot_limit)
  755. clock.p2 = limit->p2.p2_slow;
  756. else
  757. clock.p2 = limit->p2.p2_fast;
  758. }
  759. memset (best_clock, 0, sizeof (*best_clock));
  760. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  761. clock.m1++) {
  762. for (clock.m2 = limit->m2.min;
  763. clock.m2 <= limit->m2.max; clock.m2++) {
  764. /* m1 is always 0 in Pineview */
  765. if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
  766. break;
  767. for (clock.n = limit->n.min;
  768. clock.n <= limit->n.max; clock.n++) {
  769. for (clock.p1 = limit->p1.min;
  770. clock.p1 <= limit->p1.max; clock.p1++) {
  771. int this_err;
  772. intel_clock(dev, refclk, &clock);
  773. if (!intel_PLL_is_valid(crtc, &clock))
  774. continue;
  775. this_err = abs(clock.dot - target);
  776. if (this_err < err) {
  777. *best_clock = clock;
  778. err = this_err;
  779. }
  780. }
  781. }
  782. }
  783. }
  784. return (err != target);
  785. }
  786. static bool
  787. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  788. int target, int refclk, intel_clock_t *best_clock)
  789. {
  790. struct drm_device *dev = crtc->dev;
  791. struct drm_i915_private *dev_priv = dev->dev_private;
  792. intel_clock_t clock;
  793. int max_n;
  794. bool found;
  795. /* approximately equals target * 0.00488 */
  796. int err_most = (target >> 8) + (target >> 10);
  797. found = false;
  798. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  799. int lvds_reg;
  800. if (HAS_PCH_SPLIT(dev))
  801. lvds_reg = PCH_LVDS;
  802. else
  803. lvds_reg = LVDS;
  804. if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==
  805. LVDS_CLKB_POWER_UP)
  806. clock.p2 = limit->p2.p2_fast;
  807. else
  808. clock.p2 = limit->p2.p2_slow;
  809. } else {
  810. if (target < limit->p2.dot_limit)
  811. clock.p2 = limit->p2.p2_slow;
  812. else
  813. clock.p2 = limit->p2.p2_fast;
  814. }
  815. memset(best_clock, 0, sizeof(*best_clock));
  816. max_n = limit->n.max;
  817. /* based on hardware requriment prefer smaller n to precision */
  818. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  819. /* based on hardware requirment prefere larger m1,m2 */
  820. for (clock.m1 = limit->m1.max;
  821. clock.m1 >= limit->m1.min; clock.m1--) {
  822. for (clock.m2 = limit->m2.max;
  823. clock.m2 >= limit->m2.min; clock.m2--) {
  824. for (clock.p1 = limit->p1.max;
  825. clock.p1 >= limit->p1.min; clock.p1--) {
  826. int this_err;
  827. intel_clock(dev, refclk, &clock);
  828. if (!intel_PLL_is_valid(crtc, &clock))
  829. continue;
  830. this_err = abs(clock.dot - target) ;
  831. if (this_err < err_most) {
  832. *best_clock = clock;
  833. err_most = this_err;
  834. max_n = clock.n;
  835. found = true;
  836. }
  837. }
  838. }
  839. }
  840. }
  841. return found;
  842. }
  843. static bool
  844. intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  845. int target, int refclk, intel_clock_t *best_clock)
  846. {
  847. struct drm_device *dev = crtc->dev;
  848. intel_clock_t clock;
  849. /* return directly when it is eDP */
  850. if (HAS_eDP)
  851. return true;
  852. if (target < 200000) {
  853. clock.n = 1;
  854. clock.p1 = 2;
  855. clock.p2 = 10;
  856. clock.m1 = 12;
  857. clock.m2 = 9;
  858. } else {
  859. clock.n = 2;
  860. clock.p1 = 1;
  861. clock.p2 = 10;
  862. clock.m1 = 14;
  863. clock.m2 = 8;
  864. }
  865. intel_clock(dev, refclk, &clock);
  866. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  867. return true;
  868. }
  869. /* DisplayPort has only two frequencies, 162MHz and 270MHz */
  870. static bool
  871. intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  872. int target, int refclk, intel_clock_t *best_clock)
  873. {
  874. intel_clock_t clock;
  875. if (target < 200000) {
  876. clock.p1 = 2;
  877. clock.p2 = 10;
  878. clock.n = 2;
  879. clock.m1 = 23;
  880. clock.m2 = 8;
  881. } else {
  882. clock.p1 = 1;
  883. clock.p2 = 10;
  884. clock.n = 1;
  885. clock.m1 = 14;
  886. clock.m2 = 2;
  887. }
  888. clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
  889. clock.p = (clock.p1 * clock.p2);
  890. clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
  891. clock.vco = 0;
  892. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  893. return true;
  894. }
  895. void
  896. intel_wait_for_vblank(struct drm_device *dev)
  897. {
  898. /* Wait for 20ms, i.e. one cycle at 50hz. */
  899. msleep(20);
  900. }
  901. /* Parameters have changed, update FBC info */
  902. static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  903. {
  904. struct drm_device *dev = crtc->dev;
  905. struct drm_i915_private *dev_priv = dev->dev_private;
  906. struct drm_framebuffer *fb = crtc->fb;
  907. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  908. struct drm_i915_gem_object *obj_priv = to_intel_bo(intel_fb->obj);
  909. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  910. int plane, i;
  911. u32 fbc_ctl, fbc_ctl2;
  912. dev_priv->cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
  913. if (fb->pitch < dev_priv->cfb_pitch)
  914. dev_priv->cfb_pitch = fb->pitch;
  915. /* FBC_CTL wants 64B units */
  916. dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
  917. dev_priv->cfb_fence = obj_priv->fence_reg;
  918. dev_priv->cfb_plane = intel_crtc->plane;
  919. plane = dev_priv->cfb_plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
  920. /* Clear old tags */
  921. for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
  922. I915_WRITE(FBC_TAG + (i * 4), 0);
  923. /* Set it up... */
  924. fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | plane;
  925. if (obj_priv->tiling_mode != I915_TILING_NONE)
  926. fbc_ctl2 |= FBC_CTL_CPU_FENCE;
  927. I915_WRITE(FBC_CONTROL2, fbc_ctl2);
  928. I915_WRITE(FBC_FENCE_OFF, crtc->y);
  929. /* enable it... */
  930. fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
  931. if (IS_I945GM(dev))
  932. fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
  933. fbc_ctl |= (dev_priv->cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
  934. fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
  935. if (obj_priv->tiling_mode != I915_TILING_NONE)
  936. fbc_ctl |= dev_priv->cfb_fence;
  937. I915_WRITE(FBC_CONTROL, fbc_ctl);
  938. DRM_DEBUG_KMS("enabled FBC, pitch %ld, yoff %d, plane %d, ",
  939. dev_priv->cfb_pitch, crtc->y, dev_priv->cfb_plane);
  940. }
  941. void i8xx_disable_fbc(struct drm_device *dev)
  942. {
  943. struct drm_i915_private *dev_priv = dev->dev_private;
  944. u32 fbc_ctl;
  945. if (!I915_HAS_FBC(dev))
  946. return;
  947. /* Disable compression */
  948. fbc_ctl = I915_READ(FBC_CONTROL);
  949. fbc_ctl &= ~FBC_CTL_EN;
  950. I915_WRITE(FBC_CONTROL, fbc_ctl);
  951. /* Wait for compressing bit to clear */
  952. while (I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING)
  953. ; /* nothing */
  954. intel_wait_for_vblank(dev);
  955. DRM_DEBUG_KMS("disabled FBC\n");
  956. }
  957. static bool i8xx_fbc_enabled(struct drm_crtc *crtc)
  958. {
  959. struct drm_device *dev = crtc->dev;
  960. struct drm_i915_private *dev_priv = dev->dev_private;
  961. return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
  962. }
  963. static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  964. {
  965. struct drm_device *dev = crtc->dev;
  966. struct drm_i915_private *dev_priv = dev->dev_private;
  967. struct drm_framebuffer *fb = crtc->fb;
  968. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  969. struct drm_i915_gem_object *obj_priv = to_intel_bo(intel_fb->obj);
  970. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  971. int plane = (intel_crtc->plane == 0 ? DPFC_CTL_PLANEA :
  972. DPFC_CTL_PLANEB);
  973. unsigned long stall_watermark = 200;
  974. u32 dpfc_ctl;
  975. dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
  976. dev_priv->cfb_fence = obj_priv->fence_reg;
  977. dev_priv->cfb_plane = intel_crtc->plane;
  978. dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
  979. if (obj_priv->tiling_mode != I915_TILING_NONE) {
  980. dpfc_ctl |= DPFC_CTL_FENCE_EN | dev_priv->cfb_fence;
  981. I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
  982. } else {
  983. I915_WRITE(DPFC_CHICKEN, ~DPFC_HT_MODIFY);
  984. }
  985. I915_WRITE(DPFC_CONTROL, dpfc_ctl);
  986. I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  987. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  988. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  989. I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
  990. /* enable it... */
  991. I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
  992. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  993. }
  994. void g4x_disable_fbc(struct drm_device *dev)
  995. {
  996. struct drm_i915_private *dev_priv = dev->dev_private;
  997. u32 dpfc_ctl;
  998. /* Disable compression */
  999. dpfc_ctl = I915_READ(DPFC_CONTROL);
  1000. dpfc_ctl &= ~DPFC_CTL_EN;
  1001. I915_WRITE(DPFC_CONTROL, dpfc_ctl);
  1002. intel_wait_for_vblank(dev);
  1003. DRM_DEBUG_KMS("disabled FBC\n");
  1004. }
  1005. static bool g4x_fbc_enabled(struct drm_crtc *crtc)
  1006. {
  1007. struct drm_device *dev = crtc->dev;
  1008. struct drm_i915_private *dev_priv = dev->dev_private;
  1009. return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
  1010. }
  1011. /**
  1012. * intel_update_fbc - enable/disable FBC as needed
  1013. * @crtc: CRTC to point the compressor at
  1014. * @mode: mode in use
  1015. *
  1016. * Set up the framebuffer compression hardware at mode set time. We
  1017. * enable it if possible:
  1018. * - plane A only (on pre-965)
  1019. * - no pixel mulitply/line duplication
  1020. * - no alpha buffer discard
  1021. * - no dual wide
  1022. * - framebuffer <= 2048 in width, 1536 in height
  1023. *
  1024. * We can't assume that any compression will take place (worst case),
  1025. * so the compressed buffer has to be the same size as the uncompressed
  1026. * one. It also must reside (along with the line length buffer) in
  1027. * stolen memory.
  1028. *
  1029. * We need to enable/disable FBC on a global basis.
  1030. */
  1031. static void intel_update_fbc(struct drm_crtc *crtc,
  1032. struct drm_display_mode *mode)
  1033. {
  1034. struct drm_device *dev = crtc->dev;
  1035. struct drm_i915_private *dev_priv = dev->dev_private;
  1036. struct drm_framebuffer *fb = crtc->fb;
  1037. struct intel_framebuffer *intel_fb;
  1038. struct drm_i915_gem_object *obj_priv;
  1039. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1040. int plane = intel_crtc->plane;
  1041. if (!i915_powersave)
  1042. return;
  1043. if (!dev_priv->display.fbc_enabled ||
  1044. !dev_priv->display.enable_fbc ||
  1045. !dev_priv->display.disable_fbc)
  1046. return;
  1047. if (!crtc->fb)
  1048. return;
  1049. intel_fb = to_intel_framebuffer(fb);
  1050. obj_priv = to_intel_bo(intel_fb->obj);
  1051. /*
  1052. * If FBC is already on, we just have to verify that we can
  1053. * keep it that way...
  1054. * Need to disable if:
  1055. * - changing FBC params (stride, fence, mode)
  1056. * - new fb is too large to fit in compressed buffer
  1057. * - going to an unsupported config (interlace, pixel multiply, etc.)
  1058. */
  1059. if (intel_fb->obj->size > dev_priv->cfb_size) {
  1060. DRM_DEBUG_KMS("framebuffer too large, disabling "
  1061. "compression\n");
  1062. dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
  1063. goto out_disable;
  1064. }
  1065. if ((mode->flags & DRM_MODE_FLAG_INTERLACE) ||
  1066. (mode->flags & DRM_MODE_FLAG_DBLSCAN)) {
  1067. DRM_DEBUG_KMS("mode incompatible with compression, "
  1068. "disabling\n");
  1069. dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;
  1070. goto out_disable;
  1071. }
  1072. if ((mode->hdisplay > 2048) ||
  1073. (mode->vdisplay > 1536)) {
  1074. DRM_DEBUG_KMS("mode too large for compression, disabling\n");
  1075. dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;
  1076. goto out_disable;
  1077. }
  1078. if ((IS_I915GM(dev) || IS_I945GM(dev)) && plane != 0) {
  1079. DRM_DEBUG_KMS("plane not 0, disabling compression\n");
  1080. dev_priv->no_fbc_reason = FBC_BAD_PLANE;
  1081. goto out_disable;
  1082. }
  1083. if (obj_priv->tiling_mode != I915_TILING_X) {
  1084. DRM_DEBUG_KMS("framebuffer not tiled, disabling compression\n");
  1085. dev_priv->no_fbc_reason = FBC_NOT_TILED;
  1086. goto out_disable;
  1087. }
  1088. if (dev_priv->display.fbc_enabled(crtc)) {
  1089. /* We can re-enable it in this case, but need to update pitch */
  1090. if (fb->pitch > dev_priv->cfb_pitch)
  1091. dev_priv->display.disable_fbc(dev);
  1092. if (obj_priv->fence_reg != dev_priv->cfb_fence)
  1093. dev_priv->display.disable_fbc(dev);
  1094. if (plane != dev_priv->cfb_plane)
  1095. dev_priv->display.disable_fbc(dev);
  1096. }
  1097. if (!dev_priv->display.fbc_enabled(crtc)) {
  1098. /* Now try to turn it back on if possible */
  1099. dev_priv->display.enable_fbc(crtc, 500);
  1100. }
  1101. return;
  1102. out_disable:
  1103. DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
  1104. /* Multiple disables should be harmless */
  1105. if (dev_priv->display.fbc_enabled(crtc))
  1106. dev_priv->display.disable_fbc(dev);
  1107. }
  1108. static int
  1109. intel_pin_and_fence_fb_obj(struct drm_device *dev, struct drm_gem_object *obj)
  1110. {
  1111. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1112. u32 alignment;
  1113. int ret;
  1114. switch (obj_priv->tiling_mode) {
  1115. case I915_TILING_NONE:
  1116. alignment = 64 * 1024;
  1117. break;
  1118. case I915_TILING_X:
  1119. /* pin() will align the object as required by fence */
  1120. alignment = 0;
  1121. break;
  1122. case I915_TILING_Y:
  1123. /* FIXME: Is this true? */
  1124. DRM_ERROR("Y tiled not allowed for scan out buffers\n");
  1125. return -EINVAL;
  1126. default:
  1127. BUG();
  1128. }
  1129. ret = i915_gem_object_pin(obj, alignment);
  1130. if (ret != 0)
  1131. return ret;
  1132. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1133. * fence, whereas 965+ only requires a fence if using
  1134. * framebuffer compression. For simplicity, we always install
  1135. * a fence as the cost is not that onerous.
  1136. */
  1137. if (obj_priv->fence_reg == I915_FENCE_REG_NONE &&
  1138. obj_priv->tiling_mode != I915_TILING_NONE) {
  1139. ret = i915_gem_object_get_fence_reg(obj);
  1140. if (ret != 0) {
  1141. i915_gem_object_unpin(obj);
  1142. return ret;
  1143. }
  1144. }
  1145. return 0;
  1146. }
  1147. static int
  1148. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  1149. struct drm_framebuffer *old_fb)
  1150. {
  1151. struct drm_device *dev = crtc->dev;
  1152. struct drm_i915_private *dev_priv = dev->dev_private;
  1153. struct drm_i915_master_private *master_priv;
  1154. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1155. struct intel_framebuffer *intel_fb;
  1156. struct drm_i915_gem_object *obj_priv;
  1157. struct drm_gem_object *obj;
  1158. int pipe = intel_crtc->pipe;
  1159. int plane = intel_crtc->plane;
  1160. unsigned long Start, Offset;
  1161. int dspbase = (plane == 0 ? DSPAADDR : DSPBADDR);
  1162. int dspsurf = (plane == 0 ? DSPASURF : DSPBSURF);
  1163. int dspstride = (plane == 0) ? DSPASTRIDE : DSPBSTRIDE;
  1164. int dsptileoff = (plane == 0 ? DSPATILEOFF : DSPBTILEOFF);
  1165. int dspcntr_reg = (plane == 0) ? DSPACNTR : DSPBCNTR;
  1166. u32 dspcntr;
  1167. int ret;
  1168. /* no fb bound */
  1169. if (!crtc->fb) {
  1170. DRM_DEBUG_KMS("No FB bound\n");
  1171. return 0;
  1172. }
  1173. switch (plane) {
  1174. case 0:
  1175. case 1:
  1176. break;
  1177. default:
  1178. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1179. return -EINVAL;
  1180. }
  1181. intel_fb = to_intel_framebuffer(crtc->fb);
  1182. obj = intel_fb->obj;
  1183. obj_priv = to_intel_bo(obj);
  1184. mutex_lock(&dev->struct_mutex);
  1185. ret = intel_pin_and_fence_fb_obj(dev, obj);
  1186. if (ret != 0) {
  1187. mutex_unlock(&dev->struct_mutex);
  1188. return ret;
  1189. }
  1190. ret = i915_gem_object_set_to_display_plane(obj);
  1191. if (ret != 0) {
  1192. i915_gem_object_unpin(obj);
  1193. mutex_unlock(&dev->struct_mutex);
  1194. return ret;
  1195. }
  1196. dspcntr = I915_READ(dspcntr_reg);
  1197. /* Mask out pixel format bits in case we change it */
  1198. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1199. switch (crtc->fb->bits_per_pixel) {
  1200. case 8:
  1201. dspcntr |= DISPPLANE_8BPP;
  1202. break;
  1203. case 16:
  1204. if (crtc->fb->depth == 15)
  1205. dspcntr |= DISPPLANE_15_16BPP;
  1206. else
  1207. dspcntr |= DISPPLANE_16BPP;
  1208. break;
  1209. case 24:
  1210. case 32:
  1211. if (crtc->fb->depth == 30)
  1212. dspcntr |= DISPPLANE_32BPP_30BIT_NO_ALPHA;
  1213. else
  1214. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  1215. break;
  1216. default:
  1217. DRM_ERROR("Unknown color depth\n");
  1218. i915_gem_object_unpin(obj);
  1219. mutex_unlock(&dev->struct_mutex);
  1220. return -EINVAL;
  1221. }
  1222. if (IS_I965G(dev)) {
  1223. if (obj_priv->tiling_mode != I915_TILING_NONE)
  1224. dspcntr |= DISPPLANE_TILED;
  1225. else
  1226. dspcntr &= ~DISPPLANE_TILED;
  1227. }
  1228. if (HAS_PCH_SPLIT(dev))
  1229. /* must disable */
  1230. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  1231. I915_WRITE(dspcntr_reg, dspcntr);
  1232. Start = obj_priv->gtt_offset;
  1233. Offset = y * crtc->fb->pitch + x * (crtc->fb->bits_per_pixel / 8);
  1234. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d\n", Start, Offset, x, y);
  1235. I915_WRITE(dspstride, crtc->fb->pitch);
  1236. if (IS_I965G(dev)) {
  1237. I915_WRITE(dspbase, Offset);
  1238. I915_READ(dspbase);
  1239. I915_WRITE(dspsurf, Start);
  1240. I915_READ(dspsurf);
  1241. I915_WRITE(dsptileoff, (y << 16) | x);
  1242. } else {
  1243. I915_WRITE(dspbase, Start + Offset);
  1244. I915_READ(dspbase);
  1245. }
  1246. if ((IS_I965G(dev) || plane == 0))
  1247. intel_update_fbc(crtc, &crtc->mode);
  1248. intel_wait_for_vblank(dev);
  1249. if (old_fb) {
  1250. intel_fb = to_intel_framebuffer(old_fb);
  1251. obj_priv = to_intel_bo(intel_fb->obj);
  1252. i915_gem_object_unpin(intel_fb->obj);
  1253. }
  1254. intel_increase_pllclock(crtc, true);
  1255. mutex_unlock(&dev->struct_mutex);
  1256. if (!dev->primary->master)
  1257. return 0;
  1258. master_priv = dev->primary->master->driver_priv;
  1259. if (!master_priv->sarea_priv)
  1260. return 0;
  1261. if (pipe) {
  1262. master_priv->sarea_priv->pipeB_x = x;
  1263. master_priv->sarea_priv->pipeB_y = y;
  1264. } else {
  1265. master_priv->sarea_priv->pipeA_x = x;
  1266. master_priv->sarea_priv->pipeA_y = y;
  1267. }
  1268. return 0;
  1269. }
  1270. /* Disable the VGA plane that we never use */
  1271. static void i915_disable_vga (struct drm_device *dev)
  1272. {
  1273. struct drm_i915_private *dev_priv = dev->dev_private;
  1274. u8 sr1;
  1275. u32 vga_reg;
  1276. if (HAS_PCH_SPLIT(dev))
  1277. vga_reg = CPU_VGACNTRL;
  1278. else
  1279. vga_reg = VGACNTRL;
  1280. if (I915_READ(vga_reg) & VGA_DISP_DISABLE)
  1281. return;
  1282. I915_WRITE8(VGA_SR_INDEX, 1);
  1283. sr1 = I915_READ8(VGA_SR_DATA);
  1284. I915_WRITE8(VGA_SR_DATA, sr1 | (1 << 5));
  1285. udelay(100);
  1286. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  1287. }
  1288. static void ironlake_disable_pll_edp (struct drm_crtc *crtc)
  1289. {
  1290. struct drm_device *dev = crtc->dev;
  1291. struct drm_i915_private *dev_priv = dev->dev_private;
  1292. u32 dpa_ctl;
  1293. DRM_DEBUG_KMS("\n");
  1294. dpa_ctl = I915_READ(DP_A);
  1295. dpa_ctl &= ~DP_PLL_ENABLE;
  1296. I915_WRITE(DP_A, dpa_ctl);
  1297. }
  1298. static void ironlake_enable_pll_edp (struct drm_crtc *crtc)
  1299. {
  1300. struct drm_device *dev = crtc->dev;
  1301. struct drm_i915_private *dev_priv = dev->dev_private;
  1302. u32 dpa_ctl;
  1303. dpa_ctl = I915_READ(DP_A);
  1304. dpa_ctl |= DP_PLL_ENABLE;
  1305. I915_WRITE(DP_A, dpa_ctl);
  1306. udelay(200);
  1307. }
  1308. static void ironlake_set_pll_edp (struct drm_crtc *crtc, int clock)
  1309. {
  1310. struct drm_device *dev = crtc->dev;
  1311. struct drm_i915_private *dev_priv = dev->dev_private;
  1312. u32 dpa_ctl;
  1313. DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
  1314. dpa_ctl = I915_READ(DP_A);
  1315. dpa_ctl &= ~DP_PLL_FREQ_MASK;
  1316. if (clock < 200000) {
  1317. u32 temp;
  1318. dpa_ctl |= DP_PLL_FREQ_160MHZ;
  1319. /* workaround for 160Mhz:
  1320. 1) program 0x4600c bits 15:0 = 0x8124
  1321. 2) program 0x46010 bit 0 = 1
  1322. 3) program 0x46034 bit 24 = 1
  1323. 4) program 0x64000 bit 14 = 1
  1324. */
  1325. temp = I915_READ(0x4600c);
  1326. temp &= 0xffff0000;
  1327. I915_WRITE(0x4600c, temp | 0x8124);
  1328. temp = I915_READ(0x46010);
  1329. I915_WRITE(0x46010, temp | 1);
  1330. temp = I915_READ(0x46034);
  1331. I915_WRITE(0x46034, temp | (1 << 24));
  1332. } else {
  1333. dpa_ctl |= DP_PLL_FREQ_270MHZ;
  1334. }
  1335. I915_WRITE(DP_A, dpa_ctl);
  1336. udelay(500);
  1337. }
  1338. /* The FDI link training functions for ILK/Ibexpeak. */
  1339. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  1340. {
  1341. struct drm_device *dev = crtc->dev;
  1342. struct drm_i915_private *dev_priv = dev->dev_private;
  1343. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1344. int pipe = intel_crtc->pipe;
  1345. int fdi_tx_reg = (pipe == 0) ? FDI_TXA_CTL : FDI_TXB_CTL;
  1346. int fdi_rx_reg = (pipe == 0) ? FDI_RXA_CTL : FDI_RXB_CTL;
  1347. int fdi_rx_iir_reg = (pipe == 0) ? FDI_RXA_IIR : FDI_RXB_IIR;
  1348. int fdi_rx_imr_reg = (pipe == 0) ? FDI_RXA_IMR : FDI_RXB_IMR;
  1349. u32 temp, tries = 0;
  1350. /* enable CPU FDI TX and PCH FDI RX */
  1351. temp = I915_READ(fdi_tx_reg);
  1352. temp |= FDI_TX_ENABLE;
  1353. temp |= FDI_DP_PORT_WIDTH_X4; /* default */
  1354. temp &= ~FDI_LINK_TRAIN_NONE;
  1355. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1356. I915_WRITE(fdi_tx_reg, temp);
  1357. I915_READ(fdi_tx_reg);
  1358. temp = I915_READ(fdi_rx_reg);
  1359. temp &= ~FDI_LINK_TRAIN_NONE;
  1360. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1361. I915_WRITE(fdi_rx_reg, temp | FDI_RX_ENABLE);
  1362. I915_READ(fdi_rx_reg);
  1363. udelay(150);
  1364. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  1365. for train result */
  1366. temp = I915_READ(fdi_rx_imr_reg);
  1367. temp &= ~FDI_RX_SYMBOL_LOCK;
  1368. temp &= ~FDI_RX_BIT_LOCK;
  1369. I915_WRITE(fdi_rx_imr_reg, temp);
  1370. I915_READ(fdi_rx_imr_reg);
  1371. udelay(150);
  1372. for (;;) {
  1373. temp = I915_READ(fdi_rx_iir_reg);
  1374. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1375. if ((temp & FDI_RX_BIT_LOCK)) {
  1376. DRM_DEBUG_KMS("FDI train 1 done.\n");
  1377. I915_WRITE(fdi_rx_iir_reg,
  1378. temp | FDI_RX_BIT_LOCK);
  1379. break;
  1380. }
  1381. tries++;
  1382. if (tries > 5) {
  1383. DRM_DEBUG_KMS("FDI train 1 fail!\n");
  1384. break;
  1385. }
  1386. }
  1387. /* Train 2 */
  1388. temp = I915_READ(fdi_tx_reg);
  1389. temp &= ~FDI_LINK_TRAIN_NONE;
  1390. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1391. I915_WRITE(fdi_tx_reg, temp);
  1392. temp = I915_READ(fdi_rx_reg);
  1393. temp &= ~FDI_LINK_TRAIN_NONE;
  1394. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1395. I915_WRITE(fdi_rx_reg, temp);
  1396. udelay(150);
  1397. tries = 0;
  1398. for (;;) {
  1399. temp = I915_READ(fdi_rx_iir_reg);
  1400. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1401. if (temp & FDI_RX_SYMBOL_LOCK) {
  1402. I915_WRITE(fdi_rx_iir_reg,
  1403. temp | FDI_RX_SYMBOL_LOCK);
  1404. DRM_DEBUG_KMS("FDI train 2 done.\n");
  1405. break;
  1406. }
  1407. tries++;
  1408. if (tries > 5) {
  1409. DRM_DEBUG_KMS("FDI train 2 fail!\n");
  1410. break;
  1411. }
  1412. }
  1413. DRM_DEBUG_KMS("FDI train done\n");
  1414. }
  1415. static int snb_b_fdi_train_param [] = {
  1416. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  1417. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  1418. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  1419. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  1420. };
  1421. /* The FDI link training functions for SNB/Cougarpoint. */
  1422. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  1423. {
  1424. struct drm_device *dev = crtc->dev;
  1425. struct drm_i915_private *dev_priv = dev->dev_private;
  1426. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1427. int pipe = intel_crtc->pipe;
  1428. int fdi_tx_reg = (pipe == 0) ? FDI_TXA_CTL : FDI_TXB_CTL;
  1429. int fdi_rx_reg = (pipe == 0) ? FDI_RXA_CTL : FDI_RXB_CTL;
  1430. int fdi_rx_iir_reg = (pipe == 0) ? FDI_RXA_IIR : FDI_RXB_IIR;
  1431. int fdi_rx_imr_reg = (pipe == 0) ? FDI_RXA_IMR : FDI_RXB_IMR;
  1432. u32 temp, i;
  1433. /* enable CPU FDI TX and PCH FDI RX */
  1434. temp = I915_READ(fdi_tx_reg);
  1435. temp |= FDI_TX_ENABLE;
  1436. temp |= FDI_DP_PORT_WIDTH_X4; /* default */
  1437. temp &= ~FDI_LINK_TRAIN_NONE;
  1438. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1439. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1440. /* SNB-B */
  1441. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  1442. I915_WRITE(fdi_tx_reg, temp);
  1443. I915_READ(fdi_tx_reg);
  1444. temp = I915_READ(fdi_rx_reg);
  1445. if (HAS_PCH_CPT(dev)) {
  1446. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1447. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  1448. } else {
  1449. temp &= ~FDI_LINK_TRAIN_NONE;
  1450. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1451. }
  1452. I915_WRITE(fdi_rx_reg, temp | FDI_RX_ENABLE);
  1453. I915_READ(fdi_rx_reg);
  1454. udelay(150);
  1455. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  1456. for train result */
  1457. temp = I915_READ(fdi_rx_imr_reg);
  1458. temp &= ~FDI_RX_SYMBOL_LOCK;
  1459. temp &= ~FDI_RX_BIT_LOCK;
  1460. I915_WRITE(fdi_rx_imr_reg, temp);
  1461. I915_READ(fdi_rx_imr_reg);
  1462. udelay(150);
  1463. for (i = 0; i < 4; i++ ) {
  1464. temp = I915_READ(fdi_tx_reg);
  1465. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1466. temp |= snb_b_fdi_train_param[i];
  1467. I915_WRITE(fdi_tx_reg, temp);
  1468. udelay(500);
  1469. temp = I915_READ(fdi_rx_iir_reg);
  1470. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1471. if (temp & FDI_RX_BIT_LOCK) {
  1472. I915_WRITE(fdi_rx_iir_reg,
  1473. temp | FDI_RX_BIT_LOCK);
  1474. DRM_DEBUG_KMS("FDI train 1 done.\n");
  1475. break;
  1476. }
  1477. }
  1478. if (i == 4)
  1479. DRM_DEBUG_KMS("FDI train 1 fail!\n");
  1480. /* Train 2 */
  1481. temp = I915_READ(fdi_tx_reg);
  1482. temp &= ~FDI_LINK_TRAIN_NONE;
  1483. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1484. if (IS_GEN6(dev)) {
  1485. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1486. /* SNB-B */
  1487. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  1488. }
  1489. I915_WRITE(fdi_tx_reg, temp);
  1490. temp = I915_READ(fdi_rx_reg);
  1491. if (HAS_PCH_CPT(dev)) {
  1492. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1493. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  1494. } else {
  1495. temp &= ~FDI_LINK_TRAIN_NONE;
  1496. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1497. }
  1498. I915_WRITE(fdi_rx_reg, temp);
  1499. udelay(150);
  1500. for (i = 0; i < 4; i++ ) {
  1501. temp = I915_READ(fdi_tx_reg);
  1502. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1503. temp |= snb_b_fdi_train_param[i];
  1504. I915_WRITE(fdi_tx_reg, temp);
  1505. udelay(500);
  1506. temp = I915_READ(fdi_rx_iir_reg);
  1507. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1508. if (temp & FDI_RX_SYMBOL_LOCK) {
  1509. I915_WRITE(fdi_rx_iir_reg,
  1510. temp | FDI_RX_SYMBOL_LOCK);
  1511. DRM_DEBUG_KMS("FDI train 2 done.\n");
  1512. break;
  1513. }
  1514. }
  1515. if (i == 4)
  1516. DRM_DEBUG_KMS("FDI train 2 fail!\n");
  1517. DRM_DEBUG_KMS("FDI train done.\n");
  1518. }
  1519. static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
  1520. {
  1521. struct drm_device *dev = crtc->dev;
  1522. struct drm_i915_private *dev_priv = dev->dev_private;
  1523. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1524. int pipe = intel_crtc->pipe;
  1525. int plane = intel_crtc->plane;
  1526. int pch_dpll_reg = (pipe == 0) ? PCH_DPLL_A : PCH_DPLL_B;
  1527. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  1528. int dspcntr_reg = (plane == 0) ? DSPACNTR : DSPBCNTR;
  1529. int dspbase_reg = (plane == 0) ? DSPAADDR : DSPBADDR;
  1530. int fdi_tx_reg = (pipe == 0) ? FDI_TXA_CTL : FDI_TXB_CTL;
  1531. int fdi_rx_reg = (pipe == 0) ? FDI_RXA_CTL : FDI_RXB_CTL;
  1532. int transconf_reg = (pipe == 0) ? TRANSACONF : TRANSBCONF;
  1533. int pf_ctl_reg = (pipe == 0) ? PFA_CTL_1 : PFB_CTL_1;
  1534. int pf_win_size = (pipe == 0) ? PFA_WIN_SZ : PFB_WIN_SZ;
  1535. int pf_win_pos = (pipe == 0) ? PFA_WIN_POS : PFB_WIN_POS;
  1536. int cpu_htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
  1537. int cpu_hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
  1538. int cpu_hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
  1539. int cpu_vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
  1540. int cpu_vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
  1541. int cpu_vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
  1542. int trans_htot_reg = (pipe == 0) ? TRANS_HTOTAL_A : TRANS_HTOTAL_B;
  1543. int trans_hblank_reg = (pipe == 0) ? TRANS_HBLANK_A : TRANS_HBLANK_B;
  1544. int trans_hsync_reg = (pipe == 0) ? TRANS_HSYNC_A : TRANS_HSYNC_B;
  1545. int trans_vtot_reg = (pipe == 0) ? TRANS_VTOTAL_A : TRANS_VTOTAL_B;
  1546. int trans_vblank_reg = (pipe == 0) ? TRANS_VBLANK_A : TRANS_VBLANK_B;
  1547. int trans_vsync_reg = (pipe == 0) ? TRANS_VSYNC_A : TRANS_VSYNC_B;
  1548. int trans_dpll_sel = (pipe == 0) ? 0 : 1;
  1549. u32 temp;
  1550. int n;
  1551. u32 pipe_bpc;
  1552. temp = I915_READ(pipeconf_reg);
  1553. pipe_bpc = temp & PIPE_BPC_MASK;
  1554. /* XXX: When our outputs are all unaware of DPMS modes other than off
  1555. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  1556. */
  1557. switch (mode) {
  1558. case DRM_MODE_DPMS_ON:
  1559. case DRM_MODE_DPMS_STANDBY:
  1560. case DRM_MODE_DPMS_SUSPEND:
  1561. DRM_DEBUG_KMS("crtc %d dpms on\n", pipe);
  1562. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  1563. temp = I915_READ(PCH_LVDS);
  1564. if ((temp & LVDS_PORT_EN) == 0) {
  1565. I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
  1566. POSTING_READ(PCH_LVDS);
  1567. }
  1568. }
  1569. if (HAS_eDP) {
  1570. /* enable eDP PLL */
  1571. ironlake_enable_pll_edp(crtc);
  1572. } else {
  1573. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  1574. temp = I915_READ(fdi_rx_reg);
  1575. /*
  1576. * make the BPC in FDI Rx be consistent with that in
  1577. * pipeconf reg.
  1578. */
  1579. temp &= ~(0x7 << 16);
  1580. temp |= (pipe_bpc << 11);
  1581. I915_WRITE(fdi_rx_reg, temp | FDI_RX_PLL_ENABLE |
  1582. FDI_DP_PORT_WIDTH_X4); /* default 4 lanes */
  1583. I915_READ(fdi_rx_reg);
  1584. udelay(200);
  1585. /* Switch from Rawclk to PCDclk */
  1586. temp = I915_READ(fdi_rx_reg);
  1587. I915_WRITE(fdi_rx_reg, temp | FDI_SEL_PCDCLK);
  1588. I915_READ(fdi_rx_reg);
  1589. udelay(200);
  1590. /* Enable CPU FDI TX PLL, always on for Ironlake */
  1591. temp = I915_READ(fdi_tx_reg);
  1592. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  1593. I915_WRITE(fdi_tx_reg, temp | FDI_TX_PLL_ENABLE);
  1594. I915_READ(fdi_tx_reg);
  1595. udelay(100);
  1596. }
  1597. }
  1598. /* Enable panel fitting for LVDS */
  1599. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  1600. temp = I915_READ(pf_ctl_reg);
  1601. I915_WRITE(pf_ctl_reg, temp | PF_ENABLE | PF_FILTER_MED_3x3);
  1602. /* currently full aspect */
  1603. I915_WRITE(pf_win_pos, 0);
  1604. I915_WRITE(pf_win_size,
  1605. (dev_priv->panel_fixed_mode->hdisplay << 16) |
  1606. (dev_priv->panel_fixed_mode->vdisplay));
  1607. }
  1608. /* Enable CPU pipe */
  1609. temp = I915_READ(pipeconf_reg);
  1610. if ((temp & PIPEACONF_ENABLE) == 0) {
  1611. I915_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
  1612. I915_READ(pipeconf_reg);
  1613. udelay(100);
  1614. }
  1615. /* configure and enable CPU plane */
  1616. temp = I915_READ(dspcntr_reg);
  1617. if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
  1618. I915_WRITE(dspcntr_reg, temp | DISPLAY_PLANE_ENABLE);
  1619. /* Flush the plane changes */
  1620. I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
  1621. }
  1622. if (!HAS_eDP) {
  1623. /* For PCH output, training FDI link */
  1624. if (IS_GEN6(dev))
  1625. gen6_fdi_link_train(crtc);
  1626. else
  1627. ironlake_fdi_link_train(crtc);
  1628. /* enable PCH DPLL */
  1629. temp = I915_READ(pch_dpll_reg);
  1630. if ((temp & DPLL_VCO_ENABLE) == 0) {
  1631. I915_WRITE(pch_dpll_reg, temp | DPLL_VCO_ENABLE);
  1632. I915_READ(pch_dpll_reg);
  1633. }
  1634. udelay(200);
  1635. if (HAS_PCH_CPT(dev)) {
  1636. /* Be sure PCH DPLL SEL is set */
  1637. temp = I915_READ(PCH_DPLL_SEL);
  1638. if (trans_dpll_sel == 0 &&
  1639. (temp & TRANSA_DPLL_ENABLE) == 0)
  1640. temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
  1641. else if (trans_dpll_sel == 1 &&
  1642. (temp & TRANSB_DPLL_ENABLE) == 0)
  1643. temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  1644. I915_WRITE(PCH_DPLL_SEL, temp);
  1645. I915_READ(PCH_DPLL_SEL);
  1646. }
  1647. /* set transcoder timing */
  1648. I915_WRITE(trans_htot_reg, I915_READ(cpu_htot_reg));
  1649. I915_WRITE(trans_hblank_reg, I915_READ(cpu_hblank_reg));
  1650. I915_WRITE(trans_hsync_reg, I915_READ(cpu_hsync_reg));
  1651. I915_WRITE(trans_vtot_reg, I915_READ(cpu_vtot_reg));
  1652. I915_WRITE(trans_vblank_reg, I915_READ(cpu_vblank_reg));
  1653. I915_WRITE(trans_vsync_reg, I915_READ(cpu_vsync_reg));
  1654. /* enable normal train */
  1655. temp = I915_READ(fdi_tx_reg);
  1656. temp &= ~FDI_LINK_TRAIN_NONE;
  1657. I915_WRITE(fdi_tx_reg, temp | FDI_LINK_TRAIN_NONE |
  1658. FDI_TX_ENHANCE_FRAME_ENABLE);
  1659. I915_READ(fdi_tx_reg);
  1660. temp = I915_READ(fdi_rx_reg);
  1661. if (HAS_PCH_CPT(dev)) {
  1662. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1663. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  1664. } else {
  1665. temp &= ~FDI_LINK_TRAIN_NONE;
  1666. temp |= FDI_LINK_TRAIN_NONE;
  1667. }
  1668. I915_WRITE(fdi_rx_reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  1669. I915_READ(fdi_rx_reg);
  1670. /* wait one idle pattern time */
  1671. udelay(100);
  1672. /* For PCH DP, enable TRANS_DP_CTL */
  1673. if (HAS_PCH_CPT(dev) &&
  1674. intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  1675. int trans_dp_ctl = (pipe == 0) ? TRANS_DP_CTL_A : TRANS_DP_CTL_B;
  1676. int reg;
  1677. reg = I915_READ(trans_dp_ctl);
  1678. reg &= ~TRANS_DP_PORT_SEL_MASK;
  1679. reg = TRANS_DP_OUTPUT_ENABLE |
  1680. TRANS_DP_ENH_FRAMING |
  1681. TRANS_DP_VSYNC_ACTIVE_HIGH |
  1682. TRANS_DP_HSYNC_ACTIVE_HIGH;
  1683. switch (intel_trans_dp_port_sel(crtc)) {
  1684. case PCH_DP_B:
  1685. reg |= TRANS_DP_PORT_SEL_B;
  1686. break;
  1687. case PCH_DP_C:
  1688. reg |= TRANS_DP_PORT_SEL_C;
  1689. break;
  1690. case PCH_DP_D:
  1691. reg |= TRANS_DP_PORT_SEL_D;
  1692. break;
  1693. default:
  1694. DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
  1695. reg |= TRANS_DP_PORT_SEL_B;
  1696. break;
  1697. }
  1698. I915_WRITE(trans_dp_ctl, reg);
  1699. POSTING_READ(trans_dp_ctl);
  1700. }
  1701. /* enable PCH transcoder */
  1702. temp = I915_READ(transconf_reg);
  1703. /*
  1704. * make the BPC in transcoder be consistent with
  1705. * that in pipeconf reg.
  1706. */
  1707. temp &= ~PIPE_BPC_MASK;
  1708. temp |= pipe_bpc;
  1709. I915_WRITE(transconf_reg, temp | TRANS_ENABLE);
  1710. I915_READ(transconf_reg);
  1711. while ((I915_READ(transconf_reg) & TRANS_STATE_ENABLE) == 0)
  1712. ;
  1713. }
  1714. intel_crtc_load_lut(crtc);
  1715. break;
  1716. case DRM_MODE_DPMS_OFF:
  1717. DRM_DEBUG_KMS("crtc %d dpms off\n", pipe);
  1718. drm_vblank_off(dev, pipe);
  1719. /* Disable display plane */
  1720. temp = I915_READ(dspcntr_reg);
  1721. if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
  1722. I915_WRITE(dspcntr_reg, temp & ~DISPLAY_PLANE_ENABLE);
  1723. /* Flush the plane changes */
  1724. I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
  1725. I915_READ(dspbase_reg);
  1726. }
  1727. i915_disable_vga(dev);
  1728. /* disable cpu pipe, disable after all planes disabled */
  1729. temp = I915_READ(pipeconf_reg);
  1730. if ((temp & PIPEACONF_ENABLE) != 0) {
  1731. I915_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
  1732. I915_READ(pipeconf_reg);
  1733. n = 0;
  1734. /* wait for cpu pipe off, pipe state */
  1735. while ((I915_READ(pipeconf_reg) & I965_PIPECONF_ACTIVE) != 0) {
  1736. n++;
  1737. if (n < 60) {
  1738. udelay(500);
  1739. continue;
  1740. } else {
  1741. DRM_DEBUG_KMS("pipe %d off delay\n",
  1742. pipe);
  1743. break;
  1744. }
  1745. }
  1746. } else
  1747. DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
  1748. udelay(100);
  1749. /* Disable PF */
  1750. temp = I915_READ(pf_ctl_reg);
  1751. if ((temp & PF_ENABLE) != 0) {
  1752. I915_WRITE(pf_ctl_reg, temp & ~PF_ENABLE);
  1753. I915_READ(pf_ctl_reg);
  1754. }
  1755. I915_WRITE(pf_win_size, 0);
  1756. POSTING_READ(pf_win_size);
  1757. /* disable CPU FDI tx and PCH FDI rx */
  1758. temp = I915_READ(fdi_tx_reg);
  1759. I915_WRITE(fdi_tx_reg, temp & ~FDI_TX_ENABLE);
  1760. I915_READ(fdi_tx_reg);
  1761. temp = I915_READ(fdi_rx_reg);
  1762. /* BPC in FDI rx is consistent with that in pipeconf */
  1763. temp &= ~(0x07 << 16);
  1764. temp |= (pipe_bpc << 11);
  1765. I915_WRITE(fdi_rx_reg, temp & ~FDI_RX_ENABLE);
  1766. I915_READ(fdi_rx_reg);
  1767. udelay(100);
  1768. /* still set train pattern 1 */
  1769. temp = I915_READ(fdi_tx_reg);
  1770. temp &= ~FDI_LINK_TRAIN_NONE;
  1771. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1772. I915_WRITE(fdi_tx_reg, temp);
  1773. POSTING_READ(fdi_tx_reg);
  1774. temp = I915_READ(fdi_rx_reg);
  1775. if (HAS_PCH_CPT(dev)) {
  1776. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1777. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  1778. } else {
  1779. temp &= ~FDI_LINK_TRAIN_NONE;
  1780. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1781. }
  1782. I915_WRITE(fdi_rx_reg, temp);
  1783. POSTING_READ(fdi_rx_reg);
  1784. udelay(100);
  1785. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  1786. temp = I915_READ(PCH_LVDS);
  1787. I915_WRITE(PCH_LVDS, temp & ~LVDS_PORT_EN);
  1788. I915_READ(PCH_LVDS);
  1789. udelay(100);
  1790. }
  1791. /* disable PCH transcoder */
  1792. temp = I915_READ(transconf_reg);
  1793. if ((temp & TRANS_ENABLE) != 0) {
  1794. I915_WRITE(transconf_reg, temp & ~TRANS_ENABLE);
  1795. I915_READ(transconf_reg);
  1796. n = 0;
  1797. /* wait for PCH transcoder off, transcoder state */
  1798. while ((I915_READ(transconf_reg) & TRANS_STATE_ENABLE) != 0) {
  1799. n++;
  1800. if (n < 60) {
  1801. udelay(500);
  1802. continue;
  1803. } else {
  1804. DRM_DEBUG_KMS("transcoder %d off "
  1805. "delay\n", pipe);
  1806. break;
  1807. }
  1808. }
  1809. }
  1810. temp = I915_READ(transconf_reg);
  1811. /* BPC in transcoder is consistent with that in pipeconf */
  1812. temp &= ~PIPE_BPC_MASK;
  1813. temp |= pipe_bpc;
  1814. I915_WRITE(transconf_reg, temp);
  1815. I915_READ(transconf_reg);
  1816. udelay(100);
  1817. if (HAS_PCH_CPT(dev)) {
  1818. /* disable TRANS_DP_CTL */
  1819. int trans_dp_ctl = (pipe == 0) ? TRANS_DP_CTL_A : TRANS_DP_CTL_B;
  1820. int reg;
  1821. reg = I915_READ(trans_dp_ctl);
  1822. reg &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
  1823. I915_WRITE(trans_dp_ctl, reg);
  1824. POSTING_READ(trans_dp_ctl);
  1825. /* disable DPLL_SEL */
  1826. temp = I915_READ(PCH_DPLL_SEL);
  1827. if (trans_dpll_sel == 0)
  1828. temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
  1829. else
  1830. temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  1831. I915_WRITE(PCH_DPLL_SEL, temp);
  1832. I915_READ(PCH_DPLL_SEL);
  1833. }
  1834. /* disable PCH DPLL */
  1835. temp = I915_READ(pch_dpll_reg);
  1836. I915_WRITE(pch_dpll_reg, temp & ~DPLL_VCO_ENABLE);
  1837. I915_READ(pch_dpll_reg);
  1838. if (HAS_eDP) {
  1839. ironlake_disable_pll_edp(crtc);
  1840. }
  1841. /* Switch from PCDclk to Rawclk */
  1842. temp = I915_READ(fdi_rx_reg);
  1843. temp &= ~FDI_SEL_PCDCLK;
  1844. I915_WRITE(fdi_rx_reg, temp);
  1845. I915_READ(fdi_rx_reg);
  1846. /* Disable CPU FDI TX PLL */
  1847. temp = I915_READ(fdi_tx_reg);
  1848. I915_WRITE(fdi_tx_reg, temp & ~FDI_TX_PLL_ENABLE);
  1849. I915_READ(fdi_tx_reg);
  1850. udelay(100);
  1851. temp = I915_READ(fdi_rx_reg);
  1852. temp &= ~FDI_RX_PLL_ENABLE;
  1853. I915_WRITE(fdi_rx_reg, temp);
  1854. I915_READ(fdi_rx_reg);
  1855. /* Wait for the clocks to turn off. */
  1856. udelay(100);
  1857. break;
  1858. }
  1859. }
  1860. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  1861. {
  1862. struct intel_overlay *overlay;
  1863. int ret;
  1864. if (!enable && intel_crtc->overlay) {
  1865. overlay = intel_crtc->overlay;
  1866. mutex_lock(&overlay->dev->struct_mutex);
  1867. for (;;) {
  1868. ret = intel_overlay_switch_off(overlay);
  1869. if (ret == 0)
  1870. break;
  1871. ret = intel_overlay_recover_from_interrupt(overlay, 0);
  1872. if (ret != 0) {
  1873. /* overlay doesn't react anymore. Usually
  1874. * results in a black screen and an unkillable
  1875. * X server. */
  1876. BUG();
  1877. overlay->hw_wedged = HW_WEDGED;
  1878. break;
  1879. }
  1880. }
  1881. mutex_unlock(&overlay->dev->struct_mutex);
  1882. }
  1883. /* Let userspace switch the overlay on again. In most cases userspace
  1884. * has to recompute where to put it anyway. */
  1885. return;
  1886. }
  1887. static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
  1888. {
  1889. struct drm_device *dev = crtc->dev;
  1890. struct drm_i915_private *dev_priv = dev->dev_private;
  1891. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1892. int pipe = intel_crtc->pipe;
  1893. int plane = intel_crtc->plane;
  1894. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  1895. int dspcntr_reg = (plane == 0) ? DSPACNTR : DSPBCNTR;
  1896. int dspbase_reg = (plane == 0) ? DSPAADDR : DSPBADDR;
  1897. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  1898. u32 temp;
  1899. /* XXX: When our outputs are all unaware of DPMS modes other than off
  1900. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  1901. */
  1902. switch (mode) {
  1903. case DRM_MODE_DPMS_ON:
  1904. case DRM_MODE_DPMS_STANDBY:
  1905. case DRM_MODE_DPMS_SUSPEND:
  1906. intel_update_watermarks(dev);
  1907. /* Enable the DPLL */
  1908. temp = I915_READ(dpll_reg);
  1909. if ((temp & DPLL_VCO_ENABLE) == 0) {
  1910. I915_WRITE(dpll_reg, temp);
  1911. I915_READ(dpll_reg);
  1912. /* Wait for the clocks to stabilize. */
  1913. udelay(150);
  1914. I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
  1915. I915_READ(dpll_reg);
  1916. /* Wait for the clocks to stabilize. */
  1917. udelay(150);
  1918. I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
  1919. I915_READ(dpll_reg);
  1920. /* Wait for the clocks to stabilize. */
  1921. udelay(150);
  1922. }
  1923. /* Enable the pipe */
  1924. temp = I915_READ(pipeconf_reg);
  1925. if ((temp & PIPEACONF_ENABLE) == 0)
  1926. I915_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
  1927. /* Enable the plane */
  1928. temp = I915_READ(dspcntr_reg);
  1929. if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
  1930. I915_WRITE(dspcntr_reg, temp | DISPLAY_PLANE_ENABLE);
  1931. /* Flush the plane changes */
  1932. I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
  1933. }
  1934. intel_crtc_load_lut(crtc);
  1935. if ((IS_I965G(dev) || plane == 0))
  1936. intel_update_fbc(crtc, &crtc->mode);
  1937. /* Give the overlay scaler a chance to enable if it's on this pipe */
  1938. intel_crtc_dpms_overlay(intel_crtc, true);
  1939. break;
  1940. case DRM_MODE_DPMS_OFF:
  1941. intel_update_watermarks(dev);
  1942. /* Give the overlay scaler a chance to disable if it's on this pipe */
  1943. intel_crtc_dpms_overlay(intel_crtc, false);
  1944. drm_vblank_off(dev, pipe);
  1945. if (dev_priv->cfb_plane == plane &&
  1946. dev_priv->display.disable_fbc)
  1947. dev_priv->display.disable_fbc(dev);
  1948. /* Disable the VGA plane that we never use */
  1949. i915_disable_vga(dev);
  1950. /* Disable display plane */
  1951. temp = I915_READ(dspcntr_reg);
  1952. if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
  1953. I915_WRITE(dspcntr_reg, temp & ~DISPLAY_PLANE_ENABLE);
  1954. /* Flush the plane changes */
  1955. I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
  1956. I915_READ(dspbase_reg);
  1957. }
  1958. if (!IS_I9XX(dev)) {
  1959. /* Wait for vblank for the disable to take effect */
  1960. intel_wait_for_vblank(dev);
  1961. }
  1962. /* Next, disable display pipes */
  1963. temp = I915_READ(pipeconf_reg);
  1964. if ((temp & PIPEACONF_ENABLE) != 0) {
  1965. I915_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
  1966. I915_READ(pipeconf_reg);
  1967. }
  1968. /* Wait for vblank for the disable to take effect. */
  1969. intel_wait_for_vblank(dev);
  1970. temp = I915_READ(dpll_reg);
  1971. if ((temp & DPLL_VCO_ENABLE) != 0) {
  1972. I915_WRITE(dpll_reg, temp & ~DPLL_VCO_ENABLE);
  1973. I915_READ(dpll_reg);
  1974. }
  1975. /* Wait for the clocks to turn off. */
  1976. udelay(150);
  1977. break;
  1978. }
  1979. }
  1980. /**
  1981. * Sets the power management mode of the pipe and plane.
  1982. *
  1983. * This code should probably grow support for turning the cursor off and back
  1984. * on appropriately at the same time as we're turning the pipe off/on.
  1985. */
  1986. static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
  1987. {
  1988. struct drm_device *dev = crtc->dev;
  1989. struct drm_i915_private *dev_priv = dev->dev_private;
  1990. struct drm_i915_master_private *master_priv;
  1991. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1992. int pipe = intel_crtc->pipe;
  1993. bool enabled;
  1994. dev_priv->display.dpms(crtc, mode);
  1995. intel_crtc->dpms_mode = mode;
  1996. if (!dev->primary->master)
  1997. return;
  1998. master_priv = dev->primary->master->driver_priv;
  1999. if (!master_priv->sarea_priv)
  2000. return;
  2001. enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
  2002. switch (pipe) {
  2003. case 0:
  2004. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  2005. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  2006. break;
  2007. case 1:
  2008. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  2009. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  2010. break;
  2011. default:
  2012. DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
  2013. break;
  2014. }
  2015. }
  2016. static void intel_crtc_prepare (struct drm_crtc *crtc)
  2017. {
  2018. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  2019. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  2020. }
  2021. static void intel_crtc_commit (struct drm_crtc *crtc)
  2022. {
  2023. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  2024. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  2025. }
  2026. void intel_encoder_prepare (struct drm_encoder *encoder)
  2027. {
  2028. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2029. /* lvds has its own version of prepare see intel_lvds_prepare */
  2030. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  2031. }
  2032. void intel_encoder_commit (struct drm_encoder *encoder)
  2033. {
  2034. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2035. /* lvds has its own version of commit see intel_lvds_commit */
  2036. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  2037. }
  2038. static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
  2039. struct drm_display_mode *mode,
  2040. struct drm_display_mode *adjusted_mode)
  2041. {
  2042. struct drm_device *dev = crtc->dev;
  2043. if (HAS_PCH_SPLIT(dev)) {
  2044. /* FDI link clock is fixed at 2.7G */
  2045. if (mode->clock * 3 > 27000 * 4)
  2046. return MODE_CLOCK_HIGH;
  2047. }
  2048. return true;
  2049. }
  2050. static int i945_get_display_clock_speed(struct drm_device *dev)
  2051. {
  2052. return 400000;
  2053. }
  2054. static int i915_get_display_clock_speed(struct drm_device *dev)
  2055. {
  2056. return 333000;
  2057. }
  2058. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  2059. {
  2060. return 200000;
  2061. }
  2062. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  2063. {
  2064. u16 gcfgc = 0;
  2065. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  2066. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  2067. return 133000;
  2068. else {
  2069. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  2070. case GC_DISPLAY_CLOCK_333_MHZ:
  2071. return 333000;
  2072. default:
  2073. case GC_DISPLAY_CLOCK_190_200_MHZ:
  2074. return 190000;
  2075. }
  2076. }
  2077. }
  2078. static int i865_get_display_clock_speed(struct drm_device *dev)
  2079. {
  2080. return 266000;
  2081. }
  2082. static int i855_get_display_clock_speed(struct drm_device *dev)
  2083. {
  2084. u16 hpllcc = 0;
  2085. /* Assume that the hardware is in the high speed state. This
  2086. * should be the default.
  2087. */
  2088. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  2089. case GC_CLOCK_133_200:
  2090. case GC_CLOCK_100_200:
  2091. return 200000;
  2092. case GC_CLOCK_166_250:
  2093. return 250000;
  2094. case GC_CLOCK_100_133:
  2095. return 133000;
  2096. }
  2097. /* Shouldn't happen */
  2098. return 0;
  2099. }
  2100. static int i830_get_display_clock_speed(struct drm_device *dev)
  2101. {
  2102. return 133000;
  2103. }
  2104. /**
  2105. * Return the pipe currently connected to the panel fitter,
  2106. * or -1 if the panel fitter is not present or not in use
  2107. */
  2108. int intel_panel_fitter_pipe (struct drm_device *dev)
  2109. {
  2110. struct drm_i915_private *dev_priv = dev->dev_private;
  2111. u32 pfit_control;
  2112. /* i830 doesn't have a panel fitter */
  2113. if (IS_I830(dev))
  2114. return -1;
  2115. pfit_control = I915_READ(PFIT_CONTROL);
  2116. /* See if the panel fitter is in use */
  2117. if ((pfit_control & PFIT_ENABLE) == 0)
  2118. return -1;
  2119. /* 965 can place panel fitter on either pipe */
  2120. if (IS_I965G(dev))
  2121. return (pfit_control >> 29) & 0x3;
  2122. /* older chips can only use pipe 1 */
  2123. return 1;
  2124. }
  2125. struct fdi_m_n {
  2126. u32 tu;
  2127. u32 gmch_m;
  2128. u32 gmch_n;
  2129. u32 link_m;
  2130. u32 link_n;
  2131. };
  2132. static void
  2133. fdi_reduce_ratio(u32 *num, u32 *den)
  2134. {
  2135. while (*num > 0xffffff || *den > 0xffffff) {
  2136. *num >>= 1;
  2137. *den >>= 1;
  2138. }
  2139. }
  2140. #define DATA_N 0x800000
  2141. #define LINK_N 0x80000
  2142. static void
  2143. ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
  2144. int link_clock, struct fdi_m_n *m_n)
  2145. {
  2146. u64 temp;
  2147. m_n->tu = 64; /* default size */
  2148. temp = (u64) DATA_N * pixel_clock;
  2149. temp = div_u64(temp, link_clock);
  2150. m_n->gmch_m = div_u64(temp * bits_per_pixel, nlanes);
  2151. m_n->gmch_m >>= 3; /* convert to bytes_per_pixel */
  2152. m_n->gmch_n = DATA_N;
  2153. fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
  2154. temp = (u64) LINK_N * pixel_clock;
  2155. m_n->link_m = div_u64(temp, link_clock);
  2156. m_n->link_n = LINK_N;
  2157. fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
  2158. }
  2159. struct intel_watermark_params {
  2160. unsigned long fifo_size;
  2161. unsigned long max_wm;
  2162. unsigned long default_wm;
  2163. unsigned long guard_size;
  2164. unsigned long cacheline_size;
  2165. };
  2166. /* Pineview has different values for various configs */
  2167. static struct intel_watermark_params pineview_display_wm = {
  2168. PINEVIEW_DISPLAY_FIFO,
  2169. PINEVIEW_MAX_WM,
  2170. PINEVIEW_DFT_WM,
  2171. PINEVIEW_GUARD_WM,
  2172. PINEVIEW_FIFO_LINE_SIZE
  2173. };
  2174. static struct intel_watermark_params pineview_display_hplloff_wm = {
  2175. PINEVIEW_DISPLAY_FIFO,
  2176. PINEVIEW_MAX_WM,
  2177. PINEVIEW_DFT_HPLLOFF_WM,
  2178. PINEVIEW_GUARD_WM,
  2179. PINEVIEW_FIFO_LINE_SIZE
  2180. };
  2181. static struct intel_watermark_params pineview_cursor_wm = {
  2182. PINEVIEW_CURSOR_FIFO,
  2183. PINEVIEW_CURSOR_MAX_WM,
  2184. PINEVIEW_CURSOR_DFT_WM,
  2185. PINEVIEW_CURSOR_GUARD_WM,
  2186. PINEVIEW_FIFO_LINE_SIZE,
  2187. };
  2188. static struct intel_watermark_params pineview_cursor_hplloff_wm = {
  2189. PINEVIEW_CURSOR_FIFO,
  2190. PINEVIEW_CURSOR_MAX_WM,
  2191. PINEVIEW_CURSOR_DFT_WM,
  2192. PINEVIEW_CURSOR_GUARD_WM,
  2193. PINEVIEW_FIFO_LINE_SIZE
  2194. };
  2195. static struct intel_watermark_params g4x_wm_info = {
  2196. G4X_FIFO_SIZE,
  2197. G4X_MAX_WM,
  2198. G4X_MAX_WM,
  2199. 2,
  2200. G4X_FIFO_LINE_SIZE,
  2201. };
  2202. static struct intel_watermark_params i945_wm_info = {
  2203. I945_FIFO_SIZE,
  2204. I915_MAX_WM,
  2205. 1,
  2206. 2,
  2207. I915_FIFO_LINE_SIZE
  2208. };
  2209. static struct intel_watermark_params i915_wm_info = {
  2210. I915_FIFO_SIZE,
  2211. I915_MAX_WM,
  2212. 1,
  2213. 2,
  2214. I915_FIFO_LINE_SIZE
  2215. };
  2216. static struct intel_watermark_params i855_wm_info = {
  2217. I855GM_FIFO_SIZE,
  2218. I915_MAX_WM,
  2219. 1,
  2220. 2,
  2221. I830_FIFO_LINE_SIZE
  2222. };
  2223. static struct intel_watermark_params i830_wm_info = {
  2224. I830_FIFO_SIZE,
  2225. I915_MAX_WM,
  2226. 1,
  2227. 2,
  2228. I830_FIFO_LINE_SIZE
  2229. };
  2230. /**
  2231. * intel_calculate_wm - calculate watermark level
  2232. * @clock_in_khz: pixel clock
  2233. * @wm: chip FIFO params
  2234. * @pixel_size: display pixel size
  2235. * @latency_ns: memory latency for the platform
  2236. *
  2237. * Calculate the watermark level (the level at which the display plane will
  2238. * start fetching from memory again). Each chip has a different display
  2239. * FIFO size and allocation, so the caller needs to figure that out and pass
  2240. * in the correct intel_watermark_params structure.
  2241. *
  2242. * As the pixel clock runs, the FIFO will be drained at a rate that depends
  2243. * on the pixel size. When it reaches the watermark level, it'll start
  2244. * fetching FIFO line sized based chunks from memory until the FIFO fills
  2245. * past the watermark point. If the FIFO drains completely, a FIFO underrun
  2246. * will occur, and a display engine hang could result.
  2247. */
  2248. static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
  2249. struct intel_watermark_params *wm,
  2250. int pixel_size,
  2251. unsigned long latency_ns)
  2252. {
  2253. long entries_required, wm_size;
  2254. /*
  2255. * Note: we need to make sure we don't overflow for various clock &
  2256. * latency values.
  2257. * clocks go from a few thousand to several hundred thousand.
  2258. * latency is usually a few thousand
  2259. */
  2260. entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
  2261. 1000;
  2262. entries_required /= wm->cacheline_size;
  2263. DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries_required);
  2264. wm_size = wm->fifo_size - (entries_required + wm->guard_size);
  2265. DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
  2266. /* Don't promote wm_size to unsigned... */
  2267. if (wm_size > (long)wm->max_wm)
  2268. wm_size = wm->max_wm;
  2269. if (wm_size <= 0)
  2270. wm_size = wm->default_wm;
  2271. return wm_size;
  2272. }
  2273. struct cxsr_latency {
  2274. int is_desktop;
  2275. unsigned long fsb_freq;
  2276. unsigned long mem_freq;
  2277. unsigned long display_sr;
  2278. unsigned long display_hpll_disable;
  2279. unsigned long cursor_sr;
  2280. unsigned long cursor_hpll_disable;
  2281. };
  2282. static struct cxsr_latency cxsr_latency_table[] = {
  2283. {1, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
  2284. {1, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
  2285. {1, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
  2286. {1, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
  2287. {1, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
  2288. {1, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
  2289. {1, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
  2290. {1, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
  2291. {1, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
  2292. {0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
  2293. {0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
  2294. {0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
  2295. {0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
  2296. {0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
  2297. {0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
  2298. {0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
  2299. {0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
  2300. {0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
  2301. };
  2302. static struct cxsr_latency *intel_get_cxsr_latency(int is_desktop, int fsb,
  2303. int mem)
  2304. {
  2305. int i;
  2306. struct cxsr_latency *latency;
  2307. if (fsb == 0 || mem == 0)
  2308. return NULL;
  2309. for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
  2310. latency = &cxsr_latency_table[i];
  2311. if (is_desktop == latency->is_desktop &&
  2312. fsb == latency->fsb_freq && mem == latency->mem_freq)
  2313. return latency;
  2314. }
  2315. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  2316. return NULL;
  2317. }
  2318. static void pineview_disable_cxsr(struct drm_device *dev)
  2319. {
  2320. struct drm_i915_private *dev_priv = dev->dev_private;
  2321. u32 reg;
  2322. /* deactivate cxsr */
  2323. reg = I915_READ(DSPFW3);
  2324. reg &= ~(PINEVIEW_SELF_REFRESH_EN);
  2325. I915_WRITE(DSPFW3, reg);
  2326. DRM_INFO("Big FIFO is disabled\n");
  2327. }
  2328. static void pineview_enable_cxsr(struct drm_device *dev, unsigned long clock,
  2329. int pixel_size)
  2330. {
  2331. struct drm_i915_private *dev_priv = dev->dev_private;
  2332. u32 reg;
  2333. unsigned long wm;
  2334. struct cxsr_latency *latency;
  2335. latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->fsb_freq,
  2336. dev_priv->mem_freq);
  2337. if (!latency) {
  2338. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  2339. pineview_disable_cxsr(dev);
  2340. return;
  2341. }
  2342. /* Display SR */
  2343. wm = intel_calculate_wm(clock, &pineview_display_wm, pixel_size,
  2344. latency->display_sr);
  2345. reg = I915_READ(DSPFW1);
  2346. reg &= 0x7fffff;
  2347. reg |= wm << 23;
  2348. I915_WRITE(DSPFW1, reg);
  2349. DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
  2350. /* cursor SR */
  2351. wm = intel_calculate_wm(clock, &pineview_cursor_wm, pixel_size,
  2352. latency->cursor_sr);
  2353. reg = I915_READ(DSPFW3);
  2354. reg &= ~(0x3f << 24);
  2355. reg |= (wm & 0x3f) << 24;
  2356. I915_WRITE(DSPFW3, reg);
  2357. /* Display HPLL off SR */
  2358. wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
  2359. latency->display_hpll_disable, I915_FIFO_LINE_SIZE);
  2360. reg = I915_READ(DSPFW3);
  2361. reg &= 0xfffffe00;
  2362. reg |= wm & 0x1ff;
  2363. I915_WRITE(DSPFW3, reg);
  2364. /* cursor HPLL off SR */
  2365. wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm, pixel_size,
  2366. latency->cursor_hpll_disable);
  2367. reg = I915_READ(DSPFW3);
  2368. reg &= ~(0x3f << 16);
  2369. reg |= (wm & 0x3f) << 16;
  2370. I915_WRITE(DSPFW3, reg);
  2371. DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
  2372. /* activate cxsr */
  2373. reg = I915_READ(DSPFW3);
  2374. reg |= PINEVIEW_SELF_REFRESH_EN;
  2375. I915_WRITE(DSPFW3, reg);
  2376. DRM_INFO("Big FIFO is enabled\n");
  2377. return;
  2378. }
  2379. /*
  2380. * Latency for FIFO fetches is dependent on several factors:
  2381. * - memory configuration (speed, channels)
  2382. * - chipset
  2383. * - current MCH state
  2384. * It can be fairly high in some situations, so here we assume a fairly
  2385. * pessimal value. It's a tradeoff between extra memory fetches (if we
  2386. * set this value too high, the FIFO will fetch frequently to stay full)
  2387. * and power consumption (set it too low to save power and we might see
  2388. * FIFO underruns and display "flicker").
  2389. *
  2390. * A value of 5us seems to be a good balance; safe for very low end
  2391. * platforms but not overly aggressive on lower latency configs.
  2392. */
  2393. static const int latency_ns = 5000;
  2394. static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
  2395. {
  2396. struct drm_i915_private *dev_priv = dev->dev_private;
  2397. uint32_t dsparb = I915_READ(DSPARB);
  2398. int size;
  2399. if (plane == 0)
  2400. size = dsparb & 0x7f;
  2401. else
  2402. size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) -
  2403. (dsparb & 0x7f);
  2404. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2405. plane ? "B" : "A", size);
  2406. return size;
  2407. }
  2408. static int i85x_get_fifo_size(struct drm_device *dev, int plane)
  2409. {
  2410. struct drm_i915_private *dev_priv = dev->dev_private;
  2411. uint32_t dsparb = I915_READ(DSPARB);
  2412. int size;
  2413. if (plane == 0)
  2414. size = dsparb & 0x1ff;
  2415. else
  2416. size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) -
  2417. (dsparb & 0x1ff);
  2418. size >>= 1; /* Convert to cachelines */
  2419. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2420. plane ? "B" : "A", size);
  2421. return size;
  2422. }
  2423. static int i845_get_fifo_size(struct drm_device *dev, int plane)
  2424. {
  2425. struct drm_i915_private *dev_priv = dev->dev_private;
  2426. uint32_t dsparb = I915_READ(DSPARB);
  2427. int size;
  2428. size = dsparb & 0x7f;
  2429. size >>= 2; /* Convert to cachelines */
  2430. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2431. plane ? "B" : "A",
  2432. size);
  2433. return size;
  2434. }
  2435. static int i830_get_fifo_size(struct drm_device *dev, int plane)
  2436. {
  2437. struct drm_i915_private *dev_priv = dev->dev_private;
  2438. uint32_t dsparb = I915_READ(DSPARB);
  2439. int size;
  2440. size = dsparb & 0x7f;
  2441. size >>= 1; /* Convert to cachelines */
  2442. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2443. plane ? "B" : "A", size);
  2444. return size;
  2445. }
  2446. static void g4x_update_wm(struct drm_device *dev, int planea_clock,
  2447. int planeb_clock, int sr_hdisplay, int pixel_size)
  2448. {
  2449. struct drm_i915_private *dev_priv = dev->dev_private;
  2450. int total_size, cacheline_size;
  2451. int planea_wm, planeb_wm, cursora_wm, cursorb_wm, cursor_sr;
  2452. struct intel_watermark_params planea_params, planeb_params;
  2453. unsigned long line_time_us;
  2454. int sr_clock, sr_entries = 0, entries_required;
  2455. /* Create copies of the base settings for each pipe */
  2456. planea_params = planeb_params = g4x_wm_info;
  2457. /* Grab a couple of global values before we overwrite them */
  2458. total_size = planea_params.fifo_size;
  2459. cacheline_size = planea_params.cacheline_size;
  2460. /*
  2461. * Note: we need to make sure we don't overflow for various clock &
  2462. * latency values.
  2463. * clocks go from a few thousand to several hundred thousand.
  2464. * latency is usually a few thousand
  2465. */
  2466. entries_required = ((planea_clock / 1000) * pixel_size * latency_ns) /
  2467. 1000;
  2468. entries_required /= G4X_FIFO_LINE_SIZE;
  2469. planea_wm = entries_required + planea_params.guard_size;
  2470. entries_required = ((planeb_clock / 1000) * pixel_size * latency_ns) /
  2471. 1000;
  2472. entries_required /= G4X_FIFO_LINE_SIZE;
  2473. planeb_wm = entries_required + planeb_params.guard_size;
  2474. cursora_wm = cursorb_wm = 16;
  2475. cursor_sr = 32;
  2476. DRM_DEBUG("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  2477. /* Calc sr entries for one plane configs */
  2478. if (sr_hdisplay && (!planea_clock || !planeb_clock)) {
  2479. /* self-refresh has much higher latency */
  2480. static const int sr_latency_ns = 12000;
  2481. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2482. line_time_us = ((sr_hdisplay * 1000) / sr_clock);
  2483. /* Use ns/us then divide to preserve precision */
  2484. sr_entries = (((sr_latency_ns / line_time_us) + 1) *
  2485. pixel_size * sr_hdisplay) / 1000;
  2486. sr_entries = roundup(sr_entries / cacheline_size, 1);
  2487. DRM_DEBUG("self-refresh entries: %d\n", sr_entries);
  2488. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  2489. } else {
  2490. /* Turn off self refresh if both pipes are enabled */
  2491. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  2492. & ~FW_BLC_SELF_EN);
  2493. }
  2494. DRM_DEBUG("Setting FIFO watermarks - A: %d, B: %d, SR %d\n",
  2495. planea_wm, planeb_wm, sr_entries);
  2496. planea_wm &= 0x3f;
  2497. planeb_wm &= 0x3f;
  2498. I915_WRITE(DSPFW1, (sr_entries << DSPFW_SR_SHIFT) |
  2499. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  2500. (planeb_wm << DSPFW_PLANEB_SHIFT) | planea_wm);
  2501. I915_WRITE(DSPFW2, (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
  2502. (cursora_wm << DSPFW_CURSORA_SHIFT));
  2503. /* HPLL off in SR has some issues on G4x... disable it */
  2504. I915_WRITE(DSPFW3, (I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |
  2505. (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  2506. }
  2507. static void i965_update_wm(struct drm_device *dev, int planea_clock,
  2508. int planeb_clock, int sr_hdisplay, int pixel_size)
  2509. {
  2510. struct drm_i915_private *dev_priv = dev->dev_private;
  2511. unsigned long line_time_us;
  2512. int sr_clock, sr_entries, srwm = 1;
  2513. /* Calc sr entries for one plane configs */
  2514. if (sr_hdisplay && (!planea_clock || !planeb_clock)) {
  2515. /* self-refresh has much higher latency */
  2516. static const int sr_latency_ns = 12000;
  2517. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2518. line_time_us = ((sr_hdisplay * 1000) / sr_clock);
  2519. /* Use ns/us then divide to preserve precision */
  2520. sr_entries = (((sr_latency_ns / line_time_us) + 1) *
  2521. pixel_size * sr_hdisplay) / 1000;
  2522. sr_entries = roundup(sr_entries / I915_FIFO_LINE_SIZE, 1);
  2523. DRM_DEBUG("self-refresh entries: %d\n", sr_entries);
  2524. srwm = I945_FIFO_SIZE - sr_entries;
  2525. if (srwm < 0)
  2526. srwm = 1;
  2527. srwm &= 0x3f;
  2528. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  2529. } else {
  2530. /* Turn off self refresh if both pipes are enabled */
  2531. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  2532. & ~FW_BLC_SELF_EN);
  2533. }
  2534. DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
  2535. srwm);
  2536. /* 965 has limitations... */
  2537. I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) | (8 << 16) | (8 << 8) |
  2538. (8 << 0));
  2539. I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
  2540. }
  2541. static void i9xx_update_wm(struct drm_device *dev, int planea_clock,
  2542. int planeb_clock, int sr_hdisplay, int pixel_size)
  2543. {
  2544. struct drm_i915_private *dev_priv = dev->dev_private;
  2545. uint32_t fwater_lo;
  2546. uint32_t fwater_hi;
  2547. int total_size, cacheline_size, cwm, srwm = 1;
  2548. int planea_wm, planeb_wm;
  2549. struct intel_watermark_params planea_params, planeb_params;
  2550. unsigned long line_time_us;
  2551. int sr_clock, sr_entries = 0;
  2552. /* Create copies of the base settings for each pipe */
  2553. if (IS_I965GM(dev) || IS_I945GM(dev))
  2554. planea_params = planeb_params = i945_wm_info;
  2555. else if (IS_I9XX(dev))
  2556. planea_params = planeb_params = i915_wm_info;
  2557. else
  2558. planea_params = planeb_params = i855_wm_info;
  2559. /* Grab a couple of global values before we overwrite them */
  2560. total_size = planea_params.fifo_size;
  2561. cacheline_size = planea_params.cacheline_size;
  2562. /* Update per-plane FIFO sizes */
  2563. planea_params.fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  2564. planeb_params.fifo_size = dev_priv->display.get_fifo_size(dev, 1);
  2565. planea_wm = intel_calculate_wm(planea_clock, &planea_params,
  2566. pixel_size, latency_ns);
  2567. planeb_wm = intel_calculate_wm(planeb_clock, &planeb_params,
  2568. pixel_size, latency_ns);
  2569. DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  2570. /*
  2571. * Overlay gets an aggressive default since video jitter is bad.
  2572. */
  2573. cwm = 2;
  2574. /* Calc sr entries for one plane configs */
  2575. if (HAS_FW_BLC(dev) && sr_hdisplay &&
  2576. (!planea_clock || !planeb_clock)) {
  2577. /* self-refresh has much higher latency */
  2578. static const int sr_latency_ns = 6000;
  2579. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2580. line_time_us = ((sr_hdisplay * 1000) / sr_clock);
  2581. /* Use ns/us then divide to preserve precision */
  2582. sr_entries = (((sr_latency_ns / line_time_us) + 1) *
  2583. pixel_size * sr_hdisplay) / 1000;
  2584. sr_entries = roundup(sr_entries / cacheline_size, 1);
  2585. DRM_DEBUG_KMS("self-refresh entries: %d\n", sr_entries);
  2586. srwm = total_size - sr_entries;
  2587. if (srwm < 0)
  2588. srwm = 1;
  2589. if (IS_I945G(dev) || IS_I945GM(dev))
  2590. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
  2591. else if (IS_I915GM(dev)) {
  2592. /* 915M has a smaller SRWM field */
  2593. I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
  2594. I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
  2595. }
  2596. } else {
  2597. /* Turn off self refresh if both pipes are enabled */
  2598. if (IS_I945G(dev) || IS_I945GM(dev)) {
  2599. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  2600. & ~FW_BLC_SELF_EN);
  2601. } else if (IS_I915GM(dev)) {
  2602. I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
  2603. }
  2604. }
  2605. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
  2606. planea_wm, planeb_wm, cwm, srwm);
  2607. fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
  2608. fwater_hi = (cwm & 0x1f);
  2609. /* Set request length to 8 cachelines per fetch */
  2610. fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
  2611. fwater_hi = fwater_hi | (1 << 8);
  2612. I915_WRITE(FW_BLC, fwater_lo);
  2613. I915_WRITE(FW_BLC2, fwater_hi);
  2614. }
  2615. static void i830_update_wm(struct drm_device *dev, int planea_clock, int unused,
  2616. int unused2, int pixel_size)
  2617. {
  2618. struct drm_i915_private *dev_priv = dev->dev_private;
  2619. uint32_t fwater_lo = I915_READ(FW_BLC) & ~0xfff;
  2620. int planea_wm;
  2621. i830_wm_info.fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  2622. planea_wm = intel_calculate_wm(planea_clock, &i830_wm_info,
  2623. pixel_size, latency_ns);
  2624. fwater_lo |= (3<<8) | planea_wm;
  2625. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
  2626. I915_WRITE(FW_BLC, fwater_lo);
  2627. }
  2628. /**
  2629. * intel_update_watermarks - update FIFO watermark values based on current modes
  2630. *
  2631. * Calculate watermark values for the various WM regs based on current mode
  2632. * and plane configuration.
  2633. *
  2634. * There are several cases to deal with here:
  2635. * - normal (i.e. non-self-refresh)
  2636. * - self-refresh (SR) mode
  2637. * - lines are large relative to FIFO size (buffer can hold up to 2)
  2638. * - lines are small relative to FIFO size (buffer can hold more than 2
  2639. * lines), so need to account for TLB latency
  2640. *
  2641. * The normal calculation is:
  2642. * watermark = dotclock * bytes per pixel * latency
  2643. * where latency is platform & configuration dependent (we assume pessimal
  2644. * values here).
  2645. *
  2646. * The SR calculation is:
  2647. * watermark = (trunc(latency/line time)+1) * surface width *
  2648. * bytes per pixel
  2649. * where
  2650. * line time = htotal / dotclock
  2651. * and latency is assumed to be high, as above.
  2652. *
  2653. * The final value programmed to the register should always be rounded up,
  2654. * and include an extra 2 entries to account for clock crossings.
  2655. *
  2656. * We don't use the sprite, so we can ignore that. And on Crestline we have
  2657. * to set the non-SR watermarks to 8.
  2658. */
  2659. static void intel_update_watermarks(struct drm_device *dev)
  2660. {
  2661. struct drm_i915_private *dev_priv = dev->dev_private;
  2662. struct drm_crtc *crtc;
  2663. struct intel_crtc *intel_crtc;
  2664. int sr_hdisplay = 0;
  2665. unsigned long planea_clock = 0, planeb_clock = 0, sr_clock = 0;
  2666. int enabled = 0, pixel_size = 0;
  2667. if (!dev_priv->display.update_wm)
  2668. return;
  2669. /* Get the clock config from both planes */
  2670. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  2671. intel_crtc = to_intel_crtc(crtc);
  2672. if (crtc->enabled) {
  2673. enabled++;
  2674. if (intel_crtc->plane == 0) {
  2675. DRM_DEBUG_KMS("plane A (pipe %d) clock: %d\n",
  2676. intel_crtc->pipe, crtc->mode.clock);
  2677. planea_clock = crtc->mode.clock;
  2678. } else {
  2679. DRM_DEBUG_KMS("plane B (pipe %d) clock: %d\n",
  2680. intel_crtc->pipe, crtc->mode.clock);
  2681. planeb_clock = crtc->mode.clock;
  2682. }
  2683. sr_hdisplay = crtc->mode.hdisplay;
  2684. sr_clock = crtc->mode.clock;
  2685. if (crtc->fb)
  2686. pixel_size = crtc->fb->bits_per_pixel / 8;
  2687. else
  2688. pixel_size = 4; /* by default */
  2689. }
  2690. }
  2691. if (enabled <= 0)
  2692. return;
  2693. /* Single plane configs can enable self refresh */
  2694. if (enabled == 1 && IS_PINEVIEW(dev))
  2695. pineview_enable_cxsr(dev, sr_clock, pixel_size);
  2696. else if (IS_PINEVIEW(dev))
  2697. pineview_disable_cxsr(dev);
  2698. dev_priv->display.update_wm(dev, planea_clock, planeb_clock,
  2699. sr_hdisplay, pixel_size);
  2700. }
  2701. static int intel_crtc_mode_set(struct drm_crtc *crtc,
  2702. struct drm_display_mode *mode,
  2703. struct drm_display_mode *adjusted_mode,
  2704. int x, int y,
  2705. struct drm_framebuffer *old_fb)
  2706. {
  2707. struct drm_device *dev = crtc->dev;
  2708. struct drm_i915_private *dev_priv = dev->dev_private;
  2709. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2710. int pipe = intel_crtc->pipe;
  2711. int plane = intel_crtc->plane;
  2712. int fp_reg = (pipe == 0) ? FPA0 : FPB0;
  2713. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  2714. int dpll_md_reg = (intel_crtc->pipe == 0) ? DPLL_A_MD : DPLL_B_MD;
  2715. int dspcntr_reg = (plane == 0) ? DSPACNTR : DSPBCNTR;
  2716. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  2717. int htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
  2718. int hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
  2719. int hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
  2720. int vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
  2721. int vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
  2722. int vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
  2723. int dspsize_reg = (plane == 0) ? DSPASIZE : DSPBSIZE;
  2724. int dsppos_reg = (plane == 0) ? DSPAPOS : DSPBPOS;
  2725. int pipesrc_reg = (pipe == 0) ? PIPEASRC : PIPEBSRC;
  2726. int refclk, num_connectors = 0;
  2727. intel_clock_t clock, reduced_clock;
  2728. u32 dpll = 0, fp = 0, fp2 = 0, dspcntr, pipeconf;
  2729. bool ok, has_reduced_clock = false, is_sdvo = false, is_dvo = false;
  2730. bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
  2731. bool is_edp = false;
  2732. struct drm_mode_config *mode_config = &dev->mode_config;
  2733. struct drm_encoder *encoder;
  2734. struct intel_encoder *intel_encoder = NULL;
  2735. const intel_limit_t *limit;
  2736. int ret;
  2737. struct fdi_m_n m_n = {0};
  2738. int data_m1_reg = (pipe == 0) ? PIPEA_DATA_M1 : PIPEB_DATA_M1;
  2739. int data_n1_reg = (pipe == 0) ? PIPEA_DATA_N1 : PIPEB_DATA_N1;
  2740. int link_m1_reg = (pipe == 0) ? PIPEA_LINK_M1 : PIPEB_LINK_M1;
  2741. int link_n1_reg = (pipe == 0) ? PIPEA_LINK_N1 : PIPEB_LINK_N1;
  2742. int pch_fp_reg = (pipe == 0) ? PCH_FPA0 : PCH_FPB0;
  2743. int pch_dpll_reg = (pipe == 0) ? PCH_DPLL_A : PCH_DPLL_B;
  2744. int fdi_rx_reg = (pipe == 0) ? FDI_RXA_CTL : FDI_RXB_CTL;
  2745. int fdi_tx_reg = (pipe == 0) ? FDI_TXA_CTL : FDI_TXB_CTL;
  2746. int trans_dpll_sel = (pipe == 0) ? 0 : 1;
  2747. int lvds_reg = LVDS;
  2748. u32 temp;
  2749. int sdvo_pixel_multiply;
  2750. int target_clock;
  2751. drm_vblank_pre_modeset(dev, pipe);
  2752. list_for_each_entry(encoder, &mode_config->encoder_list, head) {
  2753. if (!encoder || encoder->crtc != crtc)
  2754. continue;
  2755. intel_encoder = enc_to_intel_encoder(encoder);
  2756. switch (intel_encoder->type) {
  2757. case INTEL_OUTPUT_LVDS:
  2758. is_lvds = true;
  2759. break;
  2760. case INTEL_OUTPUT_SDVO:
  2761. case INTEL_OUTPUT_HDMI:
  2762. is_sdvo = true;
  2763. if (intel_encoder->needs_tv_clock)
  2764. is_tv = true;
  2765. break;
  2766. case INTEL_OUTPUT_DVO:
  2767. is_dvo = true;
  2768. break;
  2769. case INTEL_OUTPUT_TVOUT:
  2770. is_tv = true;
  2771. break;
  2772. case INTEL_OUTPUT_ANALOG:
  2773. is_crt = true;
  2774. break;
  2775. case INTEL_OUTPUT_DISPLAYPORT:
  2776. is_dp = true;
  2777. break;
  2778. case INTEL_OUTPUT_EDP:
  2779. is_edp = true;
  2780. break;
  2781. }
  2782. num_connectors++;
  2783. }
  2784. if (is_lvds && dev_priv->lvds_use_ssc && num_connectors < 2) {
  2785. refclk = dev_priv->lvds_ssc_freq * 1000;
  2786. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  2787. refclk / 1000);
  2788. } else if (IS_I9XX(dev)) {
  2789. refclk = 96000;
  2790. if (HAS_PCH_SPLIT(dev))
  2791. refclk = 120000; /* 120Mhz refclk */
  2792. } else {
  2793. refclk = 48000;
  2794. }
  2795. /*
  2796. * Returns a set of divisors for the desired target clock with the given
  2797. * refclk, or FALSE. The returned values represent the clock equation:
  2798. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  2799. */
  2800. limit = intel_limit(crtc);
  2801. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
  2802. if (!ok) {
  2803. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  2804. drm_vblank_post_modeset(dev, pipe);
  2805. return -EINVAL;
  2806. }
  2807. if (is_lvds && dev_priv->lvds_downclock_avail) {
  2808. has_reduced_clock = limit->find_pll(limit, crtc,
  2809. dev_priv->lvds_downclock,
  2810. refclk,
  2811. &reduced_clock);
  2812. if (has_reduced_clock && (clock.p != reduced_clock.p)) {
  2813. /*
  2814. * If the different P is found, it means that we can't
  2815. * switch the display clock by using the FP0/FP1.
  2816. * In such case we will disable the LVDS downclock
  2817. * feature.
  2818. */
  2819. DRM_DEBUG_KMS("Different P is found for "
  2820. "LVDS clock/downclock\n");
  2821. has_reduced_clock = 0;
  2822. }
  2823. }
  2824. /* SDVO TV has fixed PLL values depend on its clock range,
  2825. this mirrors vbios setting. */
  2826. if (is_sdvo && is_tv) {
  2827. if (adjusted_mode->clock >= 100000
  2828. && adjusted_mode->clock < 140500) {
  2829. clock.p1 = 2;
  2830. clock.p2 = 10;
  2831. clock.n = 3;
  2832. clock.m1 = 16;
  2833. clock.m2 = 8;
  2834. } else if (adjusted_mode->clock >= 140500
  2835. && adjusted_mode->clock <= 200000) {
  2836. clock.p1 = 1;
  2837. clock.p2 = 10;
  2838. clock.n = 6;
  2839. clock.m1 = 12;
  2840. clock.m2 = 8;
  2841. }
  2842. }
  2843. /* FDI link */
  2844. if (HAS_PCH_SPLIT(dev)) {
  2845. int lane, link_bw, bpp;
  2846. /* eDP doesn't require FDI link, so just set DP M/N
  2847. according to current link config */
  2848. if (is_edp) {
  2849. target_clock = mode->clock;
  2850. intel_edp_link_config(intel_encoder,
  2851. &lane, &link_bw);
  2852. } else {
  2853. /* DP over FDI requires target mode clock
  2854. instead of link clock */
  2855. if (is_dp)
  2856. target_clock = mode->clock;
  2857. else
  2858. target_clock = adjusted_mode->clock;
  2859. lane = 4;
  2860. link_bw = 270000;
  2861. }
  2862. /* determine panel color depth */
  2863. temp = I915_READ(pipeconf_reg);
  2864. temp &= ~PIPE_BPC_MASK;
  2865. if (is_lvds) {
  2866. int lvds_reg = I915_READ(PCH_LVDS);
  2867. /* the BPC will be 6 if it is 18-bit LVDS panel */
  2868. if ((lvds_reg & LVDS_A3_POWER_MASK) == LVDS_A3_POWER_UP)
  2869. temp |= PIPE_8BPC;
  2870. else
  2871. temp |= PIPE_6BPC;
  2872. } else if (is_edp) {
  2873. switch (dev_priv->edp_bpp/3) {
  2874. case 8:
  2875. temp |= PIPE_8BPC;
  2876. break;
  2877. case 10:
  2878. temp |= PIPE_10BPC;
  2879. break;
  2880. case 6:
  2881. temp |= PIPE_6BPC;
  2882. break;
  2883. case 12:
  2884. temp |= PIPE_12BPC;
  2885. break;
  2886. }
  2887. } else
  2888. temp |= PIPE_8BPC;
  2889. I915_WRITE(pipeconf_reg, temp);
  2890. I915_READ(pipeconf_reg);
  2891. switch (temp & PIPE_BPC_MASK) {
  2892. case PIPE_8BPC:
  2893. bpp = 24;
  2894. break;
  2895. case PIPE_10BPC:
  2896. bpp = 30;
  2897. break;
  2898. case PIPE_6BPC:
  2899. bpp = 18;
  2900. break;
  2901. case PIPE_12BPC:
  2902. bpp = 36;
  2903. break;
  2904. default:
  2905. DRM_ERROR("unknown pipe bpc value\n");
  2906. bpp = 24;
  2907. }
  2908. ironlake_compute_m_n(bpp, lane, target_clock, link_bw, &m_n);
  2909. }
  2910. /* Ironlake: try to setup display ref clock before DPLL
  2911. * enabling. This is only under driver's control after
  2912. * PCH B stepping, previous chipset stepping should be
  2913. * ignoring this setting.
  2914. */
  2915. if (HAS_PCH_SPLIT(dev)) {
  2916. temp = I915_READ(PCH_DREF_CONTROL);
  2917. /* Always enable nonspread source */
  2918. temp &= ~DREF_NONSPREAD_SOURCE_MASK;
  2919. temp |= DREF_NONSPREAD_SOURCE_ENABLE;
  2920. I915_WRITE(PCH_DREF_CONTROL, temp);
  2921. POSTING_READ(PCH_DREF_CONTROL);
  2922. temp &= ~DREF_SSC_SOURCE_MASK;
  2923. temp |= DREF_SSC_SOURCE_ENABLE;
  2924. I915_WRITE(PCH_DREF_CONTROL, temp);
  2925. POSTING_READ(PCH_DREF_CONTROL);
  2926. udelay(200);
  2927. if (is_edp) {
  2928. if (dev_priv->lvds_use_ssc) {
  2929. temp |= DREF_SSC1_ENABLE;
  2930. I915_WRITE(PCH_DREF_CONTROL, temp);
  2931. POSTING_READ(PCH_DREF_CONTROL);
  2932. udelay(200);
  2933. temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  2934. temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  2935. I915_WRITE(PCH_DREF_CONTROL, temp);
  2936. POSTING_READ(PCH_DREF_CONTROL);
  2937. } else {
  2938. temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  2939. I915_WRITE(PCH_DREF_CONTROL, temp);
  2940. POSTING_READ(PCH_DREF_CONTROL);
  2941. }
  2942. }
  2943. }
  2944. if (IS_PINEVIEW(dev)) {
  2945. fp = (1 << clock.n) << 16 | clock.m1 << 8 | clock.m2;
  2946. if (has_reduced_clock)
  2947. fp2 = (1 << reduced_clock.n) << 16 |
  2948. reduced_clock.m1 << 8 | reduced_clock.m2;
  2949. } else {
  2950. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  2951. if (has_reduced_clock)
  2952. fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
  2953. reduced_clock.m2;
  2954. }
  2955. if (!HAS_PCH_SPLIT(dev))
  2956. dpll = DPLL_VGA_MODE_DIS;
  2957. if (IS_I9XX(dev)) {
  2958. if (is_lvds)
  2959. dpll |= DPLLB_MODE_LVDS;
  2960. else
  2961. dpll |= DPLLB_MODE_DAC_SERIAL;
  2962. if (is_sdvo) {
  2963. dpll |= DPLL_DVO_HIGH_SPEED;
  2964. sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
  2965. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  2966. dpll |= (sdvo_pixel_multiply - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
  2967. else if (HAS_PCH_SPLIT(dev))
  2968. dpll |= (sdvo_pixel_multiply - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  2969. }
  2970. if (is_dp)
  2971. dpll |= DPLL_DVO_HIGH_SPEED;
  2972. /* compute bitmask from p1 value */
  2973. if (IS_PINEVIEW(dev))
  2974. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  2975. else {
  2976. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  2977. /* also FPA1 */
  2978. if (HAS_PCH_SPLIT(dev))
  2979. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  2980. if (IS_G4X(dev) && has_reduced_clock)
  2981. dpll |= (1 << (reduced_clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  2982. }
  2983. switch (clock.p2) {
  2984. case 5:
  2985. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  2986. break;
  2987. case 7:
  2988. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  2989. break;
  2990. case 10:
  2991. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  2992. break;
  2993. case 14:
  2994. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  2995. break;
  2996. }
  2997. if (IS_I965G(dev) && !HAS_PCH_SPLIT(dev))
  2998. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  2999. } else {
  3000. if (is_lvds) {
  3001. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3002. } else {
  3003. if (clock.p1 == 2)
  3004. dpll |= PLL_P1_DIVIDE_BY_TWO;
  3005. else
  3006. dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3007. if (clock.p2 == 4)
  3008. dpll |= PLL_P2_DIVIDE_BY_4;
  3009. }
  3010. }
  3011. if (is_sdvo && is_tv)
  3012. dpll |= PLL_REF_INPUT_TVCLKINBC;
  3013. else if (is_tv)
  3014. /* XXX: just matching BIOS for now */
  3015. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  3016. dpll |= 3;
  3017. else if (is_lvds && dev_priv->lvds_use_ssc && num_connectors < 2)
  3018. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  3019. else
  3020. dpll |= PLL_REF_INPUT_DREFCLK;
  3021. /* setup pipeconf */
  3022. pipeconf = I915_READ(pipeconf_reg);
  3023. /* Set up the display plane register */
  3024. dspcntr = DISPPLANE_GAMMA_ENABLE;
  3025. /* Ironlake's plane is forced to pipe, bit 24 is to
  3026. enable color space conversion */
  3027. if (!HAS_PCH_SPLIT(dev)) {
  3028. if (pipe == 0)
  3029. dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
  3030. else
  3031. dspcntr |= DISPPLANE_SEL_PIPE_B;
  3032. }
  3033. if (pipe == 0 && !IS_I965G(dev)) {
  3034. /* Enable pixel doubling when the dot clock is > 90% of the (display)
  3035. * core speed.
  3036. *
  3037. * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
  3038. * pipe == 0 check?
  3039. */
  3040. if (mode->clock >
  3041. dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
  3042. pipeconf |= PIPEACONF_DOUBLE_WIDE;
  3043. else
  3044. pipeconf &= ~PIPEACONF_DOUBLE_WIDE;
  3045. }
  3046. /* Disable the panel fitter if it was on our pipe */
  3047. if (!HAS_PCH_SPLIT(dev) && intel_panel_fitter_pipe(dev) == pipe)
  3048. I915_WRITE(PFIT_CONTROL, 0);
  3049. DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  3050. drm_mode_debug_printmodeline(mode);
  3051. /* assign to Ironlake registers */
  3052. if (HAS_PCH_SPLIT(dev)) {
  3053. fp_reg = pch_fp_reg;
  3054. dpll_reg = pch_dpll_reg;
  3055. }
  3056. if (is_edp) {
  3057. ironlake_disable_pll_edp(crtc);
  3058. } else if ((dpll & DPLL_VCO_ENABLE)) {
  3059. I915_WRITE(fp_reg, fp);
  3060. I915_WRITE(dpll_reg, dpll & ~DPLL_VCO_ENABLE);
  3061. I915_READ(dpll_reg);
  3062. udelay(150);
  3063. }
  3064. /* enable transcoder DPLL */
  3065. if (HAS_PCH_CPT(dev)) {
  3066. temp = I915_READ(PCH_DPLL_SEL);
  3067. if (trans_dpll_sel == 0)
  3068. temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
  3069. else
  3070. temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  3071. I915_WRITE(PCH_DPLL_SEL, temp);
  3072. I915_READ(PCH_DPLL_SEL);
  3073. udelay(150);
  3074. }
  3075. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  3076. * This is an exception to the general rule that mode_set doesn't turn
  3077. * things on.
  3078. */
  3079. if (is_lvds) {
  3080. u32 lvds;
  3081. if (HAS_PCH_SPLIT(dev))
  3082. lvds_reg = PCH_LVDS;
  3083. lvds = I915_READ(lvds_reg);
  3084. lvds |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  3085. if (pipe == 1) {
  3086. if (HAS_PCH_CPT(dev))
  3087. lvds |= PORT_TRANS_B_SEL_CPT;
  3088. else
  3089. lvds |= LVDS_PIPEB_SELECT;
  3090. } else {
  3091. if (HAS_PCH_CPT(dev))
  3092. lvds &= ~PORT_TRANS_SEL_MASK;
  3093. else
  3094. lvds &= ~LVDS_PIPEB_SELECT;
  3095. }
  3096. /* set the corresponsding LVDS_BORDER bit */
  3097. lvds |= dev_priv->lvds_border_bits;
  3098. /* Set the B0-B3 data pairs corresponding to whether we're going to
  3099. * set the DPLLs for dual-channel mode or not.
  3100. */
  3101. if (clock.p2 == 7)
  3102. lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  3103. else
  3104. lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  3105. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  3106. * appropriately here, but we need to look more thoroughly into how
  3107. * panels behave in the two modes.
  3108. */
  3109. /* set the dithering flag */
  3110. if (IS_I965G(dev)) {
  3111. if (dev_priv->lvds_dither) {
  3112. if (HAS_PCH_SPLIT(dev))
  3113. pipeconf |= PIPE_ENABLE_DITHER;
  3114. else
  3115. lvds |= LVDS_ENABLE_DITHER;
  3116. } else {
  3117. if (HAS_PCH_SPLIT(dev))
  3118. pipeconf &= ~PIPE_ENABLE_DITHER;
  3119. else
  3120. lvds &= ~LVDS_ENABLE_DITHER;
  3121. }
  3122. }
  3123. I915_WRITE(lvds_reg, lvds);
  3124. I915_READ(lvds_reg);
  3125. }
  3126. if (is_dp)
  3127. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  3128. else if (HAS_PCH_SPLIT(dev)) {
  3129. /* For non-DP output, clear any trans DP clock recovery setting.*/
  3130. if (pipe == 0) {
  3131. I915_WRITE(TRANSA_DATA_M1, 0);
  3132. I915_WRITE(TRANSA_DATA_N1, 0);
  3133. I915_WRITE(TRANSA_DP_LINK_M1, 0);
  3134. I915_WRITE(TRANSA_DP_LINK_N1, 0);
  3135. } else {
  3136. I915_WRITE(TRANSB_DATA_M1, 0);
  3137. I915_WRITE(TRANSB_DATA_N1, 0);
  3138. I915_WRITE(TRANSB_DP_LINK_M1, 0);
  3139. I915_WRITE(TRANSB_DP_LINK_N1, 0);
  3140. }
  3141. }
  3142. if (!is_edp) {
  3143. I915_WRITE(fp_reg, fp);
  3144. I915_WRITE(dpll_reg, dpll);
  3145. I915_READ(dpll_reg);
  3146. /* Wait for the clocks to stabilize. */
  3147. udelay(150);
  3148. if (IS_I965G(dev) && !HAS_PCH_SPLIT(dev)) {
  3149. if (is_sdvo) {
  3150. sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
  3151. I915_WRITE(dpll_md_reg, (0 << DPLL_MD_UDI_DIVIDER_SHIFT) |
  3152. ((sdvo_pixel_multiply - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT));
  3153. } else
  3154. I915_WRITE(dpll_md_reg, 0);
  3155. } else {
  3156. /* write it again -- the BIOS does, after all */
  3157. I915_WRITE(dpll_reg, dpll);
  3158. }
  3159. I915_READ(dpll_reg);
  3160. /* Wait for the clocks to stabilize. */
  3161. udelay(150);
  3162. }
  3163. if (is_lvds && has_reduced_clock && i915_powersave) {
  3164. I915_WRITE(fp_reg + 4, fp2);
  3165. intel_crtc->lowfreq_avail = true;
  3166. if (HAS_PIPE_CXSR(dev)) {
  3167. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  3168. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  3169. }
  3170. } else {
  3171. I915_WRITE(fp_reg + 4, fp);
  3172. intel_crtc->lowfreq_avail = false;
  3173. if (HAS_PIPE_CXSR(dev)) {
  3174. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  3175. pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
  3176. }
  3177. }
  3178. I915_WRITE(htot_reg, (adjusted_mode->crtc_hdisplay - 1) |
  3179. ((adjusted_mode->crtc_htotal - 1) << 16));
  3180. I915_WRITE(hblank_reg, (adjusted_mode->crtc_hblank_start - 1) |
  3181. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  3182. I915_WRITE(hsync_reg, (adjusted_mode->crtc_hsync_start - 1) |
  3183. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  3184. I915_WRITE(vtot_reg, (adjusted_mode->crtc_vdisplay - 1) |
  3185. ((adjusted_mode->crtc_vtotal - 1) << 16));
  3186. I915_WRITE(vblank_reg, (adjusted_mode->crtc_vblank_start - 1) |
  3187. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  3188. I915_WRITE(vsync_reg, (adjusted_mode->crtc_vsync_start - 1) |
  3189. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  3190. /* pipesrc and dspsize control the size that is scaled from, which should
  3191. * always be the user's requested size.
  3192. */
  3193. if (!HAS_PCH_SPLIT(dev)) {
  3194. I915_WRITE(dspsize_reg, ((mode->vdisplay - 1) << 16) |
  3195. (mode->hdisplay - 1));
  3196. I915_WRITE(dsppos_reg, 0);
  3197. }
  3198. I915_WRITE(pipesrc_reg, ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  3199. if (HAS_PCH_SPLIT(dev)) {
  3200. I915_WRITE(data_m1_reg, TU_SIZE(m_n.tu) | m_n.gmch_m);
  3201. I915_WRITE(data_n1_reg, TU_SIZE(m_n.tu) | m_n.gmch_n);
  3202. I915_WRITE(link_m1_reg, m_n.link_m);
  3203. I915_WRITE(link_n1_reg, m_n.link_n);
  3204. if (is_edp) {
  3205. ironlake_set_pll_edp(crtc, adjusted_mode->clock);
  3206. } else {
  3207. /* enable FDI RX PLL too */
  3208. temp = I915_READ(fdi_rx_reg);
  3209. I915_WRITE(fdi_rx_reg, temp | FDI_RX_PLL_ENABLE);
  3210. I915_READ(fdi_rx_reg);
  3211. udelay(200);
  3212. /* enable FDI TX PLL too */
  3213. temp = I915_READ(fdi_tx_reg);
  3214. I915_WRITE(fdi_tx_reg, temp | FDI_TX_PLL_ENABLE);
  3215. I915_READ(fdi_tx_reg);
  3216. /* enable FDI RX PCDCLK */
  3217. temp = I915_READ(fdi_rx_reg);
  3218. I915_WRITE(fdi_rx_reg, temp | FDI_SEL_PCDCLK);
  3219. I915_READ(fdi_rx_reg);
  3220. udelay(200);
  3221. }
  3222. }
  3223. I915_WRITE(pipeconf_reg, pipeconf);
  3224. I915_READ(pipeconf_reg);
  3225. intel_wait_for_vblank(dev);
  3226. if (IS_IRONLAKE(dev)) {
  3227. /* enable address swizzle for tiling buffer */
  3228. temp = I915_READ(DISP_ARB_CTL);
  3229. I915_WRITE(DISP_ARB_CTL, temp | DISP_TILE_SURFACE_SWIZZLING);
  3230. }
  3231. I915_WRITE(dspcntr_reg, dspcntr);
  3232. /* Flush the plane changes */
  3233. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  3234. if ((IS_I965G(dev) || plane == 0))
  3235. intel_update_fbc(crtc, &crtc->mode);
  3236. intel_update_watermarks(dev);
  3237. drm_vblank_post_modeset(dev, pipe);
  3238. return ret;
  3239. }
  3240. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3241. void intel_crtc_load_lut(struct drm_crtc *crtc)
  3242. {
  3243. struct drm_device *dev = crtc->dev;
  3244. struct drm_i915_private *dev_priv = dev->dev_private;
  3245. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3246. int palreg = (intel_crtc->pipe == 0) ? PALETTE_A : PALETTE_B;
  3247. int i;
  3248. /* The clocks have to be on to load the palette. */
  3249. if (!crtc->enabled)
  3250. return;
  3251. /* use legacy palette for Ironlake */
  3252. if (HAS_PCH_SPLIT(dev))
  3253. palreg = (intel_crtc->pipe == 0) ? LGC_PALETTE_A :
  3254. LGC_PALETTE_B;
  3255. for (i = 0; i < 256; i++) {
  3256. I915_WRITE(palreg + 4 * i,
  3257. (intel_crtc->lut_r[i] << 16) |
  3258. (intel_crtc->lut_g[i] << 8) |
  3259. intel_crtc->lut_b[i]);
  3260. }
  3261. }
  3262. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  3263. struct drm_file *file_priv,
  3264. uint32_t handle,
  3265. uint32_t width, uint32_t height)
  3266. {
  3267. struct drm_device *dev = crtc->dev;
  3268. struct drm_i915_private *dev_priv = dev->dev_private;
  3269. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3270. struct drm_gem_object *bo;
  3271. struct drm_i915_gem_object *obj_priv;
  3272. int pipe = intel_crtc->pipe;
  3273. uint32_t control = (pipe == 0) ? CURACNTR : CURBCNTR;
  3274. uint32_t base = (pipe == 0) ? CURABASE : CURBBASE;
  3275. uint32_t temp = I915_READ(control);
  3276. size_t addr;
  3277. int ret;
  3278. DRM_DEBUG_KMS("\n");
  3279. /* if we want to turn off the cursor ignore width and height */
  3280. if (!handle) {
  3281. DRM_DEBUG_KMS("cursor off\n");
  3282. if (IS_MOBILE(dev) || IS_I9XX(dev)) {
  3283. temp &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  3284. temp |= CURSOR_MODE_DISABLE;
  3285. } else {
  3286. temp &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
  3287. }
  3288. addr = 0;
  3289. bo = NULL;
  3290. mutex_lock(&dev->struct_mutex);
  3291. goto finish;
  3292. }
  3293. /* Currently we only support 64x64 cursors */
  3294. if (width != 64 || height != 64) {
  3295. DRM_ERROR("we currently only support 64x64 cursors\n");
  3296. return -EINVAL;
  3297. }
  3298. bo = drm_gem_object_lookup(dev, file_priv, handle);
  3299. if (!bo)
  3300. return -ENOENT;
  3301. obj_priv = to_intel_bo(bo);
  3302. if (bo->size < width * height * 4) {
  3303. DRM_ERROR("buffer is to small\n");
  3304. ret = -ENOMEM;
  3305. goto fail;
  3306. }
  3307. /* we only need to pin inside GTT if cursor is non-phy */
  3308. mutex_lock(&dev->struct_mutex);
  3309. if (!dev_priv->info->cursor_needs_physical) {
  3310. ret = i915_gem_object_pin(bo, PAGE_SIZE);
  3311. if (ret) {
  3312. DRM_ERROR("failed to pin cursor bo\n");
  3313. goto fail_locked;
  3314. }
  3315. addr = obj_priv->gtt_offset;
  3316. } else {
  3317. ret = i915_gem_attach_phys_object(dev, bo, (pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1);
  3318. if (ret) {
  3319. DRM_ERROR("failed to attach phys object\n");
  3320. goto fail_locked;
  3321. }
  3322. addr = obj_priv->phys_obj->handle->busaddr;
  3323. }
  3324. if (!IS_I9XX(dev))
  3325. I915_WRITE(CURSIZE, (height << 12) | width);
  3326. /* Hooray for CUR*CNTR differences */
  3327. if (IS_MOBILE(dev) || IS_I9XX(dev)) {
  3328. temp &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
  3329. temp |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  3330. temp |= (pipe << 28); /* Connect to correct pipe */
  3331. } else {
  3332. temp &= ~(CURSOR_FORMAT_MASK);
  3333. temp |= CURSOR_ENABLE;
  3334. temp |= CURSOR_FORMAT_ARGB | CURSOR_GAMMA_ENABLE;
  3335. }
  3336. finish:
  3337. I915_WRITE(control, temp);
  3338. I915_WRITE(base, addr);
  3339. if (intel_crtc->cursor_bo) {
  3340. if (dev_priv->info->cursor_needs_physical) {
  3341. if (intel_crtc->cursor_bo != bo)
  3342. i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
  3343. } else
  3344. i915_gem_object_unpin(intel_crtc->cursor_bo);
  3345. drm_gem_object_unreference(intel_crtc->cursor_bo);
  3346. }
  3347. mutex_unlock(&dev->struct_mutex);
  3348. intel_crtc->cursor_addr = addr;
  3349. intel_crtc->cursor_bo = bo;
  3350. return 0;
  3351. fail_locked:
  3352. mutex_unlock(&dev->struct_mutex);
  3353. fail:
  3354. drm_gem_object_unreference_unlocked(bo);
  3355. return ret;
  3356. }
  3357. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  3358. {
  3359. struct drm_device *dev = crtc->dev;
  3360. struct drm_i915_private *dev_priv = dev->dev_private;
  3361. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3362. struct intel_framebuffer *intel_fb;
  3363. int pipe = intel_crtc->pipe;
  3364. uint32_t temp = 0;
  3365. uint32_t adder;
  3366. if (crtc->fb) {
  3367. intel_fb = to_intel_framebuffer(crtc->fb);
  3368. intel_mark_busy(dev, intel_fb->obj);
  3369. }
  3370. if (x < 0) {
  3371. temp |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  3372. x = -x;
  3373. }
  3374. if (y < 0) {
  3375. temp |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  3376. y = -y;
  3377. }
  3378. temp |= x << CURSOR_X_SHIFT;
  3379. temp |= y << CURSOR_Y_SHIFT;
  3380. adder = intel_crtc->cursor_addr;
  3381. I915_WRITE((pipe == 0) ? CURAPOS : CURBPOS, temp);
  3382. I915_WRITE((pipe == 0) ? CURABASE : CURBBASE, adder);
  3383. return 0;
  3384. }
  3385. /** Sets the color ramps on behalf of RandR */
  3386. void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  3387. u16 blue, int regno)
  3388. {
  3389. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3390. intel_crtc->lut_r[regno] = red >> 8;
  3391. intel_crtc->lut_g[regno] = green >> 8;
  3392. intel_crtc->lut_b[regno] = blue >> 8;
  3393. }
  3394. void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  3395. u16 *blue, int regno)
  3396. {
  3397. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3398. *red = intel_crtc->lut_r[regno] << 8;
  3399. *green = intel_crtc->lut_g[regno] << 8;
  3400. *blue = intel_crtc->lut_b[regno] << 8;
  3401. }
  3402. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  3403. u16 *blue, uint32_t size)
  3404. {
  3405. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3406. int i;
  3407. if (size != 256)
  3408. return;
  3409. for (i = 0; i < 256; i++) {
  3410. intel_crtc->lut_r[i] = red[i] >> 8;
  3411. intel_crtc->lut_g[i] = green[i] >> 8;
  3412. intel_crtc->lut_b[i] = blue[i] >> 8;
  3413. }
  3414. intel_crtc_load_lut(crtc);
  3415. }
  3416. /**
  3417. * Get a pipe with a simple mode set on it for doing load-based monitor
  3418. * detection.
  3419. *
  3420. * It will be up to the load-detect code to adjust the pipe as appropriate for
  3421. * its requirements. The pipe will be connected to no other encoders.
  3422. *
  3423. * Currently this code will only succeed if there is a pipe with no encoders
  3424. * configured for it. In the future, it could choose to temporarily disable
  3425. * some outputs to free up a pipe for its use.
  3426. *
  3427. * \return crtc, or NULL if no pipes are available.
  3428. */
  3429. /* VESA 640x480x72Hz mode to set on the pipe */
  3430. static struct drm_display_mode load_detect_mode = {
  3431. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  3432. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  3433. };
  3434. struct drm_crtc *intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,
  3435. struct drm_connector *connector,
  3436. struct drm_display_mode *mode,
  3437. int *dpms_mode)
  3438. {
  3439. struct intel_crtc *intel_crtc;
  3440. struct drm_crtc *possible_crtc;
  3441. struct drm_crtc *supported_crtc =NULL;
  3442. struct drm_encoder *encoder = &intel_encoder->enc;
  3443. struct drm_crtc *crtc = NULL;
  3444. struct drm_device *dev = encoder->dev;
  3445. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  3446. struct drm_crtc_helper_funcs *crtc_funcs;
  3447. int i = -1;
  3448. /*
  3449. * Algorithm gets a little messy:
  3450. * - if the connector already has an assigned crtc, use it (but make
  3451. * sure it's on first)
  3452. * - try to find the first unused crtc that can drive this connector,
  3453. * and use that if we find one
  3454. * - if there are no unused crtcs available, try to use the first
  3455. * one we found that supports the connector
  3456. */
  3457. /* See if we already have a CRTC for this connector */
  3458. if (encoder->crtc) {
  3459. crtc = encoder->crtc;
  3460. /* Make sure the crtc and connector are running */
  3461. intel_crtc = to_intel_crtc(crtc);
  3462. *dpms_mode = intel_crtc->dpms_mode;
  3463. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  3464. crtc_funcs = crtc->helper_private;
  3465. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  3466. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  3467. }
  3468. return crtc;
  3469. }
  3470. /* Find an unused one (if possible) */
  3471. list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
  3472. i++;
  3473. if (!(encoder->possible_crtcs & (1 << i)))
  3474. continue;
  3475. if (!possible_crtc->enabled) {
  3476. crtc = possible_crtc;
  3477. break;
  3478. }
  3479. if (!supported_crtc)
  3480. supported_crtc = possible_crtc;
  3481. }
  3482. /*
  3483. * If we didn't find an unused CRTC, don't use any.
  3484. */
  3485. if (!crtc) {
  3486. return NULL;
  3487. }
  3488. encoder->crtc = crtc;
  3489. connector->encoder = encoder;
  3490. intel_encoder->load_detect_temp = true;
  3491. intel_crtc = to_intel_crtc(crtc);
  3492. *dpms_mode = intel_crtc->dpms_mode;
  3493. if (!crtc->enabled) {
  3494. if (!mode)
  3495. mode = &load_detect_mode;
  3496. drm_crtc_helper_set_mode(crtc, mode, 0, 0, crtc->fb);
  3497. } else {
  3498. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  3499. crtc_funcs = crtc->helper_private;
  3500. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  3501. }
  3502. /* Add this connector to the crtc */
  3503. encoder_funcs->mode_set(encoder, &crtc->mode, &crtc->mode);
  3504. encoder_funcs->commit(encoder);
  3505. }
  3506. /* let the connector get through one full cycle before testing */
  3507. intel_wait_for_vblank(dev);
  3508. return crtc;
  3509. }
  3510. void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,
  3511. struct drm_connector *connector, int dpms_mode)
  3512. {
  3513. struct drm_encoder *encoder = &intel_encoder->enc;
  3514. struct drm_device *dev = encoder->dev;
  3515. struct drm_crtc *crtc = encoder->crtc;
  3516. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  3517. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  3518. if (intel_encoder->load_detect_temp) {
  3519. encoder->crtc = NULL;
  3520. connector->encoder = NULL;
  3521. intel_encoder->load_detect_temp = false;
  3522. crtc->enabled = drm_helper_crtc_in_use(crtc);
  3523. drm_helper_disable_unused_functions(dev);
  3524. }
  3525. /* Switch crtc and encoder back off if necessary */
  3526. if (crtc->enabled && dpms_mode != DRM_MODE_DPMS_ON) {
  3527. if (encoder->crtc == crtc)
  3528. encoder_funcs->dpms(encoder, dpms_mode);
  3529. crtc_funcs->dpms(crtc, dpms_mode);
  3530. }
  3531. }
  3532. /* Returns the clock of the currently programmed mode of the given pipe. */
  3533. static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
  3534. {
  3535. struct drm_i915_private *dev_priv = dev->dev_private;
  3536. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3537. int pipe = intel_crtc->pipe;
  3538. u32 dpll = I915_READ((pipe == 0) ? DPLL_A : DPLL_B);
  3539. u32 fp;
  3540. intel_clock_t clock;
  3541. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  3542. fp = I915_READ((pipe == 0) ? FPA0 : FPB0);
  3543. else
  3544. fp = I915_READ((pipe == 0) ? FPA1 : FPB1);
  3545. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  3546. if (IS_PINEVIEW(dev)) {
  3547. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  3548. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  3549. } else {
  3550. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  3551. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  3552. }
  3553. if (IS_I9XX(dev)) {
  3554. if (IS_PINEVIEW(dev))
  3555. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  3556. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  3557. else
  3558. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  3559. DPLL_FPA01_P1_POST_DIV_SHIFT);
  3560. switch (dpll & DPLL_MODE_MASK) {
  3561. case DPLLB_MODE_DAC_SERIAL:
  3562. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  3563. 5 : 10;
  3564. break;
  3565. case DPLLB_MODE_LVDS:
  3566. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  3567. 7 : 14;
  3568. break;
  3569. default:
  3570. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  3571. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  3572. return 0;
  3573. }
  3574. /* XXX: Handle the 100Mhz refclk */
  3575. intel_clock(dev, 96000, &clock);
  3576. } else {
  3577. bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
  3578. if (is_lvds) {
  3579. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  3580. DPLL_FPA01_P1_POST_DIV_SHIFT);
  3581. clock.p2 = 14;
  3582. if ((dpll & PLL_REF_INPUT_MASK) ==
  3583. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  3584. /* XXX: might not be 66MHz */
  3585. intel_clock(dev, 66000, &clock);
  3586. } else
  3587. intel_clock(dev, 48000, &clock);
  3588. } else {
  3589. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  3590. clock.p1 = 2;
  3591. else {
  3592. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  3593. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  3594. }
  3595. if (dpll & PLL_P2_DIVIDE_BY_4)
  3596. clock.p2 = 4;
  3597. else
  3598. clock.p2 = 2;
  3599. intel_clock(dev, 48000, &clock);
  3600. }
  3601. }
  3602. /* XXX: It would be nice to validate the clocks, but we can't reuse
  3603. * i830PllIsValid() because it relies on the xf86_config connector
  3604. * configuration being accurate, which it isn't necessarily.
  3605. */
  3606. return clock.dot;
  3607. }
  3608. /** Returns the currently programmed mode of the given pipe. */
  3609. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  3610. struct drm_crtc *crtc)
  3611. {
  3612. struct drm_i915_private *dev_priv = dev->dev_private;
  3613. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3614. int pipe = intel_crtc->pipe;
  3615. struct drm_display_mode *mode;
  3616. int htot = I915_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
  3617. int hsync = I915_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
  3618. int vtot = I915_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
  3619. int vsync = I915_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
  3620. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  3621. if (!mode)
  3622. return NULL;
  3623. mode->clock = intel_crtc_clock_get(dev, crtc);
  3624. mode->hdisplay = (htot & 0xffff) + 1;
  3625. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  3626. mode->hsync_start = (hsync & 0xffff) + 1;
  3627. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  3628. mode->vdisplay = (vtot & 0xffff) + 1;
  3629. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  3630. mode->vsync_start = (vsync & 0xffff) + 1;
  3631. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  3632. drm_mode_set_name(mode);
  3633. drm_mode_set_crtcinfo(mode, 0);
  3634. return mode;
  3635. }
  3636. #define GPU_IDLE_TIMEOUT 500 /* ms */
  3637. /* When this timer fires, we've been idle for awhile */
  3638. static void intel_gpu_idle_timer(unsigned long arg)
  3639. {
  3640. struct drm_device *dev = (struct drm_device *)arg;
  3641. drm_i915_private_t *dev_priv = dev->dev_private;
  3642. DRM_DEBUG_DRIVER("idle timer fired, downclocking\n");
  3643. dev_priv->busy = false;
  3644. queue_work(dev_priv->wq, &dev_priv->idle_work);
  3645. }
  3646. #define CRTC_IDLE_TIMEOUT 1000 /* ms */
  3647. static void intel_crtc_idle_timer(unsigned long arg)
  3648. {
  3649. struct intel_crtc *intel_crtc = (struct intel_crtc *)arg;
  3650. struct drm_crtc *crtc = &intel_crtc->base;
  3651. drm_i915_private_t *dev_priv = crtc->dev->dev_private;
  3652. DRM_DEBUG_DRIVER("idle timer fired, downclocking\n");
  3653. intel_crtc->busy = false;
  3654. queue_work(dev_priv->wq, &dev_priv->idle_work);
  3655. }
  3656. static void intel_increase_pllclock(struct drm_crtc *crtc, bool schedule)
  3657. {
  3658. struct drm_device *dev = crtc->dev;
  3659. drm_i915_private_t *dev_priv = dev->dev_private;
  3660. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3661. int pipe = intel_crtc->pipe;
  3662. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  3663. int dpll = I915_READ(dpll_reg);
  3664. if (HAS_PCH_SPLIT(dev))
  3665. return;
  3666. if (!dev_priv->lvds_downclock_avail)
  3667. return;
  3668. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  3669. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  3670. /* Unlock panel regs */
  3671. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) | (0xabcd << 16));
  3672. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  3673. I915_WRITE(dpll_reg, dpll);
  3674. dpll = I915_READ(dpll_reg);
  3675. intel_wait_for_vblank(dev);
  3676. dpll = I915_READ(dpll_reg);
  3677. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  3678. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  3679. /* ...and lock them again */
  3680. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
  3681. }
  3682. /* Schedule downclock */
  3683. if (schedule)
  3684. mod_timer(&intel_crtc->idle_timer, jiffies +
  3685. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  3686. }
  3687. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  3688. {
  3689. struct drm_device *dev = crtc->dev;
  3690. drm_i915_private_t *dev_priv = dev->dev_private;
  3691. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3692. int pipe = intel_crtc->pipe;
  3693. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  3694. int dpll = I915_READ(dpll_reg);
  3695. if (HAS_PCH_SPLIT(dev))
  3696. return;
  3697. if (!dev_priv->lvds_downclock_avail)
  3698. return;
  3699. /*
  3700. * Since this is called by a timer, we should never get here in
  3701. * the manual case.
  3702. */
  3703. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  3704. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  3705. /* Unlock panel regs */
  3706. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) | (0xabcd << 16));
  3707. dpll |= DISPLAY_RATE_SELECT_FPA1;
  3708. I915_WRITE(dpll_reg, dpll);
  3709. dpll = I915_READ(dpll_reg);
  3710. intel_wait_for_vblank(dev);
  3711. dpll = I915_READ(dpll_reg);
  3712. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  3713. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  3714. /* ...and lock them again */
  3715. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
  3716. }
  3717. }
  3718. /**
  3719. * intel_idle_update - adjust clocks for idleness
  3720. * @work: work struct
  3721. *
  3722. * Either the GPU or display (or both) went idle. Check the busy status
  3723. * here and adjust the CRTC and GPU clocks as necessary.
  3724. */
  3725. static void intel_idle_update(struct work_struct *work)
  3726. {
  3727. drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
  3728. idle_work);
  3729. struct drm_device *dev = dev_priv->dev;
  3730. struct drm_crtc *crtc;
  3731. struct intel_crtc *intel_crtc;
  3732. if (!i915_powersave)
  3733. return;
  3734. mutex_lock(&dev->struct_mutex);
  3735. if (IS_I945G(dev) || IS_I945GM(dev)) {
  3736. DRM_DEBUG_DRIVER("enable memory self refresh on 945\n");
  3737. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
  3738. }
  3739. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  3740. /* Skip inactive CRTCs */
  3741. if (!crtc->fb)
  3742. continue;
  3743. intel_crtc = to_intel_crtc(crtc);
  3744. if (!intel_crtc->busy)
  3745. intel_decrease_pllclock(crtc);
  3746. }
  3747. mutex_unlock(&dev->struct_mutex);
  3748. }
  3749. /**
  3750. * intel_mark_busy - mark the GPU and possibly the display busy
  3751. * @dev: drm device
  3752. * @obj: object we're operating on
  3753. *
  3754. * Callers can use this function to indicate that the GPU is busy processing
  3755. * commands. If @obj matches one of the CRTC objects (i.e. it's a scanout
  3756. * buffer), we'll also mark the display as busy, so we know to increase its
  3757. * clock frequency.
  3758. */
  3759. void intel_mark_busy(struct drm_device *dev, struct drm_gem_object *obj)
  3760. {
  3761. drm_i915_private_t *dev_priv = dev->dev_private;
  3762. struct drm_crtc *crtc = NULL;
  3763. struct intel_framebuffer *intel_fb;
  3764. struct intel_crtc *intel_crtc;
  3765. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3766. return;
  3767. if (!dev_priv->busy) {
  3768. if (IS_I945G(dev) || IS_I945GM(dev)) {
  3769. u32 fw_blc_self;
  3770. DRM_DEBUG_DRIVER("disable memory self refresh on 945\n");
  3771. fw_blc_self = I915_READ(FW_BLC_SELF);
  3772. fw_blc_self &= ~FW_BLC_SELF_EN;
  3773. I915_WRITE(FW_BLC_SELF, fw_blc_self | FW_BLC_SELF_EN_MASK);
  3774. }
  3775. dev_priv->busy = true;
  3776. } else
  3777. mod_timer(&dev_priv->idle_timer, jiffies +
  3778. msecs_to_jiffies(GPU_IDLE_TIMEOUT));
  3779. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  3780. if (!crtc->fb)
  3781. continue;
  3782. intel_crtc = to_intel_crtc(crtc);
  3783. intel_fb = to_intel_framebuffer(crtc->fb);
  3784. if (intel_fb->obj == obj) {
  3785. if (!intel_crtc->busy) {
  3786. if (IS_I945G(dev) || IS_I945GM(dev)) {
  3787. u32 fw_blc_self;
  3788. DRM_DEBUG_DRIVER("disable memory self refresh on 945\n");
  3789. fw_blc_self = I915_READ(FW_BLC_SELF);
  3790. fw_blc_self &= ~FW_BLC_SELF_EN;
  3791. I915_WRITE(FW_BLC_SELF, fw_blc_self | FW_BLC_SELF_EN_MASK);
  3792. }
  3793. /* Non-busy -> busy, upclock */
  3794. intel_increase_pllclock(crtc, true);
  3795. intel_crtc->busy = true;
  3796. } else {
  3797. /* Busy -> busy, put off timer */
  3798. mod_timer(&intel_crtc->idle_timer, jiffies +
  3799. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  3800. }
  3801. }
  3802. }
  3803. }
  3804. static void intel_crtc_destroy(struct drm_crtc *crtc)
  3805. {
  3806. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3807. drm_crtc_cleanup(crtc);
  3808. kfree(intel_crtc);
  3809. }
  3810. struct intel_unpin_work {
  3811. struct work_struct work;
  3812. struct drm_device *dev;
  3813. struct drm_gem_object *old_fb_obj;
  3814. struct drm_gem_object *pending_flip_obj;
  3815. struct drm_pending_vblank_event *event;
  3816. int pending;
  3817. };
  3818. static void intel_unpin_work_fn(struct work_struct *__work)
  3819. {
  3820. struct intel_unpin_work *work =
  3821. container_of(__work, struct intel_unpin_work, work);
  3822. mutex_lock(&work->dev->struct_mutex);
  3823. i915_gem_object_unpin(work->old_fb_obj);
  3824. drm_gem_object_unreference(work->pending_flip_obj);
  3825. drm_gem_object_unreference(work->old_fb_obj);
  3826. mutex_unlock(&work->dev->struct_mutex);
  3827. kfree(work);
  3828. }
  3829. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  3830. {
  3831. drm_i915_private_t *dev_priv = dev->dev_private;
  3832. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  3833. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3834. struct intel_unpin_work *work;
  3835. struct drm_i915_gem_object *obj_priv;
  3836. struct drm_pending_vblank_event *e;
  3837. struct timeval now;
  3838. unsigned long flags;
  3839. /* Ignore early vblank irqs */
  3840. if (intel_crtc == NULL)
  3841. return;
  3842. spin_lock_irqsave(&dev->event_lock, flags);
  3843. work = intel_crtc->unpin_work;
  3844. if (work == NULL || !work->pending) {
  3845. if (work && !work->pending) {
  3846. obj_priv = to_intel_bo(work->pending_flip_obj);
  3847. DRM_DEBUG_DRIVER("flip finish: %p (%d) not pending?\n",
  3848. obj_priv,
  3849. atomic_read(&obj_priv->pending_flip));
  3850. }
  3851. spin_unlock_irqrestore(&dev->event_lock, flags);
  3852. return;
  3853. }
  3854. intel_crtc->unpin_work = NULL;
  3855. drm_vblank_put(dev, intel_crtc->pipe);
  3856. if (work->event) {
  3857. e = work->event;
  3858. do_gettimeofday(&now);
  3859. e->event.sequence = drm_vblank_count(dev, intel_crtc->pipe);
  3860. e->event.tv_sec = now.tv_sec;
  3861. e->event.tv_usec = now.tv_usec;
  3862. list_add_tail(&e->base.link,
  3863. &e->base.file_priv->event_list);
  3864. wake_up_interruptible(&e->base.file_priv->event_wait);
  3865. }
  3866. spin_unlock_irqrestore(&dev->event_lock, flags);
  3867. obj_priv = to_intel_bo(work->pending_flip_obj);
  3868. /* Initial scanout buffer will have a 0 pending flip count */
  3869. if ((atomic_read(&obj_priv->pending_flip) == 0) ||
  3870. atomic_dec_and_test(&obj_priv->pending_flip))
  3871. DRM_WAKEUP(&dev_priv->pending_flip_queue);
  3872. schedule_work(&work->work);
  3873. }
  3874. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  3875. {
  3876. drm_i915_private_t *dev_priv = dev->dev_private;
  3877. struct intel_crtc *intel_crtc =
  3878. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  3879. unsigned long flags;
  3880. spin_lock_irqsave(&dev->event_lock, flags);
  3881. if (intel_crtc->unpin_work) {
  3882. intel_crtc->unpin_work->pending = 1;
  3883. } else {
  3884. DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
  3885. }
  3886. spin_unlock_irqrestore(&dev->event_lock, flags);
  3887. }
  3888. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  3889. struct drm_framebuffer *fb,
  3890. struct drm_pending_vblank_event *event)
  3891. {
  3892. struct drm_device *dev = crtc->dev;
  3893. struct drm_i915_private *dev_priv = dev->dev_private;
  3894. struct intel_framebuffer *intel_fb;
  3895. struct drm_i915_gem_object *obj_priv;
  3896. struct drm_gem_object *obj;
  3897. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3898. struct intel_unpin_work *work;
  3899. unsigned long flags;
  3900. int pipesrc_reg = (intel_crtc->pipe == 0) ? PIPEASRC : PIPEBSRC;
  3901. int ret, pipesrc;
  3902. RING_LOCALS;
  3903. work = kzalloc(sizeof *work, GFP_KERNEL);
  3904. if (work == NULL)
  3905. return -ENOMEM;
  3906. mutex_lock(&dev->struct_mutex);
  3907. work->event = event;
  3908. work->dev = crtc->dev;
  3909. intel_fb = to_intel_framebuffer(crtc->fb);
  3910. work->old_fb_obj = intel_fb->obj;
  3911. INIT_WORK(&work->work, intel_unpin_work_fn);
  3912. /* We borrow the event spin lock for protecting unpin_work */
  3913. spin_lock_irqsave(&dev->event_lock, flags);
  3914. if (intel_crtc->unpin_work) {
  3915. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  3916. spin_unlock_irqrestore(&dev->event_lock, flags);
  3917. kfree(work);
  3918. mutex_unlock(&dev->struct_mutex);
  3919. return -EBUSY;
  3920. }
  3921. intel_crtc->unpin_work = work;
  3922. spin_unlock_irqrestore(&dev->event_lock, flags);
  3923. intel_fb = to_intel_framebuffer(fb);
  3924. obj = intel_fb->obj;
  3925. ret = intel_pin_and_fence_fb_obj(dev, obj);
  3926. if (ret != 0) {
  3927. DRM_DEBUG_DRIVER("flip queue: %p pin & fence failed\n",
  3928. to_intel_bo(obj));
  3929. kfree(work);
  3930. intel_crtc->unpin_work = NULL;
  3931. mutex_unlock(&dev->struct_mutex);
  3932. return ret;
  3933. }
  3934. /* Reference the objects for the scheduled work. */
  3935. drm_gem_object_reference(work->old_fb_obj);
  3936. drm_gem_object_reference(obj);
  3937. crtc->fb = fb;
  3938. i915_gem_object_flush_write_domain(obj);
  3939. drm_vblank_get(dev, intel_crtc->pipe);
  3940. obj_priv = to_intel_bo(obj);
  3941. atomic_inc(&obj_priv->pending_flip);
  3942. work->pending_flip_obj = obj;
  3943. BEGIN_LP_RING(4);
  3944. OUT_RING(MI_DISPLAY_FLIP |
  3945. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  3946. OUT_RING(fb->pitch);
  3947. if (IS_I965G(dev)) {
  3948. OUT_RING(obj_priv->gtt_offset | obj_priv->tiling_mode);
  3949. pipesrc = I915_READ(pipesrc_reg);
  3950. OUT_RING(pipesrc & 0x0fff0fff);
  3951. } else {
  3952. OUT_RING(obj_priv->gtt_offset);
  3953. OUT_RING(MI_NOOP);
  3954. }
  3955. ADVANCE_LP_RING();
  3956. mutex_unlock(&dev->struct_mutex);
  3957. return 0;
  3958. }
  3959. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  3960. .dpms = intel_crtc_dpms,
  3961. .mode_fixup = intel_crtc_mode_fixup,
  3962. .mode_set = intel_crtc_mode_set,
  3963. .mode_set_base = intel_pipe_set_base,
  3964. .prepare = intel_crtc_prepare,
  3965. .commit = intel_crtc_commit,
  3966. .load_lut = intel_crtc_load_lut,
  3967. };
  3968. static const struct drm_crtc_funcs intel_crtc_funcs = {
  3969. .cursor_set = intel_crtc_cursor_set,
  3970. .cursor_move = intel_crtc_cursor_move,
  3971. .gamma_set = intel_crtc_gamma_set,
  3972. .set_config = drm_crtc_helper_set_config,
  3973. .destroy = intel_crtc_destroy,
  3974. .page_flip = intel_crtc_page_flip,
  3975. };
  3976. static void intel_crtc_init(struct drm_device *dev, int pipe)
  3977. {
  3978. drm_i915_private_t *dev_priv = dev->dev_private;
  3979. struct intel_crtc *intel_crtc;
  3980. int i;
  3981. intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  3982. if (intel_crtc == NULL)
  3983. return;
  3984. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  3985. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  3986. intel_crtc->pipe = pipe;
  3987. intel_crtc->plane = pipe;
  3988. for (i = 0; i < 256; i++) {
  3989. intel_crtc->lut_r[i] = i;
  3990. intel_crtc->lut_g[i] = i;
  3991. intel_crtc->lut_b[i] = i;
  3992. }
  3993. /* Swap pipes & planes for FBC on pre-965 */
  3994. intel_crtc->pipe = pipe;
  3995. intel_crtc->plane = pipe;
  3996. if (IS_MOBILE(dev) && (IS_I9XX(dev) && !IS_I965G(dev))) {
  3997. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  3998. intel_crtc->plane = ((pipe == 0) ? 1 : 0);
  3999. }
  4000. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  4001. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  4002. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  4003. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  4004. intel_crtc->cursor_addr = 0;
  4005. intel_crtc->dpms_mode = DRM_MODE_DPMS_OFF;
  4006. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  4007. intel_crtc->busy = false;
  4008. setup_timer(&intel_crtc->idle_timer, intel_crtc_idle_timer,
  4009. (unsigned long)intel_crtc);
  4010. }
  4011. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  4012. struct drm_file *file_priv)
  4013. {
  4014. drm_i915_private_t *dev_priv = dev->dev_private;
  4015. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  4016. struct drm_mode_object *drmmode_obj;
  4017. struct intel_crtc *crtc;
  4018. if (!dev_priv) {
  4019. DRM_ERROR("called with no initialization\n");
  4020. return -EINVAL;
  4021. }
  4022. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  4023. DRM_MODE_OBJECT_CRTC);
  4024. if (!drmmode_obj) {
  4025. DRM_ERROR("no such CRTC id\n");
  4026. return -EINVAL;
  4027. }
  4028. crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
  4029. pipe_from_crtc_id->pipe = crtc->pipe;
  4030. return 0;
  4031. }
  4032. struct drm_crtc *intel_get_crtc_from_pipe(struct drm_device *dev, int pipe)
  4033. {
  4034. struct drm_crtc *crtc = NULL;
  4035. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  4036. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4037. if (intel_crtc->pipe == pipe)
  4038. break;
  4039. }
  4040. return crtc;
  4041. }
  4042. static int intel_encoder_clones(struct drm_device *dev, int type_mask)
  4043. {
  4044. int index_mask = 0;
  4045. struct drm_encoder *encoder;
  4046. int entry = 0;
  4047. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  4048. struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
  4049. if (type_mask & intel_encoder->clone_mask)
  4050. index_mask |= (1 << entry);
  4051. entry++;
  4052. }
  4053. return index_mask;
  4054. }
  4055. static void intel_setup_outputs(struct drm_device *dev)
  4056. {
  4057. struct drm_i915_private *dev_priv = dev->dev_private;
  4058. struct drm_encoder *encoder;
  4059. intel_crt_init(dev);
  4060. /* Set up integrated LVDS */
  4061. if (IS_MOBILE(dev) && !IS_I830(dev))
  4062. intel_lvds_init(dev);
  4063. if (HAS_PCH_SPLIT(dev)) {
  4064. int found;
  4065. if (IS_MOBILE(dev) && (I915_READ(DP_A) & DP_DETECTED))
  4066. intel_dp_init(dev, DP_A);
  4067. if (I915_READ(HDMIB) & PORT_DETECTED) {
  4068. /* check SDVOB */
  4069. /* found = intel_sdvo_init(dev, HDMIB); */
  4070. found = 0;
  4071. if (!found)
  4072. intel_hdmi_init(dev, HDMIB);
  4073. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  4074. intel_dp_init(dev, PCH_DP_B);
  4075. }
  4076. if (I915_READ(HDMIC) & PORT_DETECTED)
  4077. intel_hdmi_init(dev, HDMIC);
  4078. if (I915_READ(HDMID) & PORT_DETECTED)
  4079. intel_hdmi_init(dev, HDMID);
  4080. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  4081. intel_dp_init(dev, PCH_DP_C);
  4082. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  4083. intel_dp_init(dev, PCH_DP_D);
  4084. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  4085. bool found = false;
  4086. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  4087. DRM_DEBUG_KMS("probing SDVOB\n");
  4088. found = intel_sdvo_init(dev, SDVOB);
  4089. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  4090. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  4091. intel_hdmi_init(dev, SDVOB);
  4092. }
  4093. if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
  4094. DRM_DEBUG_KMS("probing DP_B\n");
  4095. intel_dp_init(dev, DP_B);
  4096. }
  4097. }
  4098. /* Before G4X SDVOC doesn't have its own detect register */
  4099. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  4100. DRM_DEBUG_KMS("probing SDVOC\n");
  4101. found = intel_sdvo_init(dev, SDVOC);
  4102. }
  4103. if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
  4104. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  4105. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  4106. intel_hdmi_init(dev, SDVOC);
  4107. }
  4108. if (SUPPORTS_INTEGRATED_DP(dev)) {
  4109. DRM_DEBUG_KMS("probing DP_C\n");
  4110. intel_dp_init(dev, DP_C);
  4111. }
  4112. }
  4113. if (SUPPORTS_INTEGRATED_DP(dev) &&
  4114. (I915_READ(DP_D) & DP_DETECTED)) {
  4115. DRM_DEBUG_KMS("probing DP_D\n");
  4116. intel_dp_init(dev, DP_D);
  4117. }
  4118. } else if (IS_GEN2(dev))
  4119. intel_dvo_init(dev);
  4120. if (SUPPORTS_TV(dev))
  4121. intel_tv_init(dev);
  4122. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  4123. struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
  4124. encoder->possible_crtcs = intel_encoder->crtc_mask;
  4125. encoder->possible_clones = intel_encoder_clones(dev,
  4126. intel_encoder->clone_mask);
  4127. }
  4128. }
  4129. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  4130. {
  4131. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  4132. struct drm_device *dev = fb->dev;
  4133. if (fb->fbdev)
  4134. intelfb_remove(dev, fb);
  4135. drm_framebuffer_cleanup(fb);
  4136. drm_gem_object_unreference_unlocked(intel_fb->obj);
  4137. kfree(intel_fb);
  4138. }
  4139. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  4140. struct drm_file *file_priv,
  4141. unsigned int *handle)
  4142. {
  4143. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  4144. struct drm_gem_object *object = intel_fb->obj;
  4145. return drm_gem_handle_create(file_priv, object, handle);
  4146. }
  4147. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  4148. .destroy = intel_user_framebuffer_destroy,
  4149. .create_handle = intel_user_framebuffer_create_handle,
  4150. };
  4151. int intel_framebuffer_create(struct drm_device *dev,
  4152. struct drm_mode_fb_cmd *mode_cmd,
  4153. struct drm_framebuffer **fb,
  4154. struct drm_gem_object *obj)
  4155. {
  4156. struct intel_framebuffer *intel_fb;
  4157. int ret;
  4158. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  4159. if (!intel_fb)
  4160. return -ENOMEM;
  4161. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  4162. if (ret) {
  4163. DRM_ERROR("framebuffer init failed %d\n", ret);
  4164. return ret;
  4165. }
  4166. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  4167. intel_fb->obj = obj;
  4168. *fb = &intel_fb->base;
  4169. return 0;
  4170. }
  4171. static struct drm_framebuffer *
  4172. intel_user_framebuffer_create(struct drm_device *dev,
  4173. struct drm_file *filp,
  4174. struct drm_mode_fb_cmd *mode_cmd)
  4175. {
  4176. struct drm_gem_object *obj;
  4177. struct drm_framebuffer *fb;
  4178. int ret;
  4179. obj = drm_gem_object_lookup(dev, filp, mode_cmd->handle);
  4180. if (!obj)
  4181. return NULL;
  4182. ret = intel_framebuffer_create(dev, mode_cmd, &fb, obj);
  4183. if (ret) {
  4184. drm_gem_object_unreference_unlocked(obj);
  4185. return NULL;
  4186. }
  4187. return fb;
  4188. }
  4189. static const struct drm_mode_config_funcs intel_mode_funcs = {
  4190. .fb_create = intel_user_framebuffer_create,
  4191. .fb_changed = intelfb_probe,
  4192. };
  4193. static struct drm_gem_object *
  4194. intel_alloc_power_context(struct drm_device *dev)
  4195. {
  4196. struct drm_gem_object *pwrctx;
  4197. int ret;
  4198. pwrctx = drm_gem_object_alloc(dev, 4096);
  4199. if (!pwrctx) {
  4200. DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
  4201. return NULL;
  4202. }
  4203. mutex_lock(&dev->struct_mutex);
  4204. ret = i915_gem_object_pin(pwrctx, 4096);
  4205. if (ret) {
  4206. DRM_ERROR("failed to pin power context: %d\n", ret);
  4207. goto err_unref;
  4208. }
  4209. ret = i915_gem_object_set_to_gtt_domain(pwrctx, 1);
  4210. if (ret) {
  4211. DRM_ERROR("failed to set-domain on power context: %d\n", ret);
  4212. goto err_unpin;
  4213. }
  4214. mutex_unlock(&dev->struct_mutex);
  4215. return pwrctx;
  4216. err_unpin:
  4217. i915_gem_object_unpin(pwrctx);
  4218. err_unref:
  4219. drm_gem_object_unreference(pwrctx);
  4220. mutex_unlock(&dev->struct_mutex);
  4221. return NULL;
  4222. }
  4223. void ironlake_enable_drps(struct drm_device *dev)
  4224. {
  4225. struct drm_i915_private *dev_priv = dev->dev_private;
  4226. u32 rgvmodectl = I915_READ(MEMMODECTL), rgvswctl;
  4227. u8 fmax, fmin, fstart, vstart;
  4228. int i = 0;
  4229. /* 100ms RC evaluation intervals */
  4230. I915_WRITE(RCUPEI, 100000);
  4231. I915_WRITE(RCDNEI, 100000);
  4232. /* Set max/min thresholds to 90ms and 80ms respectively */
  4233. I915_WRITE(RCBMAXAVG, 90000);
  4234. I915_WRITE(RCBMINAVG, 80000);
  4235. I915_WRITE(MEMIHYST, 1);
  4236. /* Set up min, max, and cur for interrupt handling */
  4237. fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
  4238. fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
  4239. fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
  4240. MEMMODE_FSTART_SHIFT;
  4241. vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
  4242. PXVFREQ_PX_SHIFT;
  4243. dev_priv->max_delay = fstart; /* can't go to fmax w/o IPS */
  4244. dev_priv->min_delay = fmin;
  4245. dev_priv->cur_delay = fstart;
  4246. I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
  4247. /*
  4248. * Interrupts will be enabled in ironlake_irq_postinstall
  4249. */
  4250. I915_WRITE(VIDSTART, vstart);
  4251. POSTING_READ(VIDSTART);
  4252. rgvmodectl |= MEMMODE_SWMODE_EN;
  4253. I915_WRITE(MEMMODECTL, rgvmodectl);
  4254. while (I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) {
  4255. if (i++ > 100) {
  4256. DRM_ERROR("stuck trying to change perf mode\n");
  4257. break;
  4258. }
  4259. msleep(1);
  4260. }
  4261. msleep(1);
  4262. rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
  4263. (fstart << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
  4264. I915_WRITE(MEMSWCTL, rgvswctl);
  4265. POSTING_READ(MEMSWCTL);
  4266. rgvswctl |= MEMCTL_CMD_STS;
  4267. I915_WRITE(MEMSWCTL, rgvswctl);
  4268. }
  4269. void ironlake_disable_drps(struct drm_device *dev)
  4270. {
  4271. struct drm_i915_private *dev_priv = dev->dev_private;
  4272. u32 rgvswctl;
  4273. u8 fstart;
  4274. /* Ack interrupts, disable EFC interrupt */
  4275. I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
  4276. I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
  4277. I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
  4278. I915_WRITE(DEIIR, DE_PCU_EVENT);
  4279. I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
  4280. /* Go back to the starting frequency */
  4281. fstart = (I915_READ(MEMMODECTL) & MEMMODE_FSTART_MASK) >>
  4282. MEMMODE_FSTART_SHIFT;
  4283. rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
  4284. (fstart << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
  4285. I915_WRITE(MEMSWCTL, rgvswctl);
  4286. msleep(1);
  4287. rgvswctl |= MEMCTL_CMD_STS;
  4288. I915_WRITE(MEMSWCTL, rgvswctl);
  4289. msleep(1);
  4290. }
  4291. void intel_init_clock_gating(struct drm_device *dev)
  4292. {
  4293. struct drm_i915_private *dev_priv = dev->dev_private;
  4294. /*
  4295. * Disable clock gating reported to work incorrectly according to the
  4296. * specs, but enable as much else as we can.
  4297. */
  4298. if (HAS_PCH_SPLIT(dev)) {
  4299. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  4300. if (IS_IRONLAKE(dev)) {
  4301. /* Required for FBC */
  4302. dspclk_gate |= DPFDUNIT_CLOCK_GATE_DISABLE;
  4303. /* Required for CxSR */
  4304. dspclk_gate |= DPARBUNIT_CLOCK_GATE_DISABLE;
  4305. I915_WRITE(PCH_3DCGDIS0,
  4306. MARIUNIT_CLOCK_GATE_DISABLE |
  4307. SVSMUNIT_CLOCK_GATE_DISABLE);
  4308. }
  4309. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  4310. return;
  4311. } else if (IS_G4X(dev)) {
  4312. uint32_t dspclk_gate;
  4313. I915_WRITE(RENCLK_GATE_D1, 0);
  4314. I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
  4315. GS_UNIT_CLOCK_GATE_DISABLE |
  4316. CL_UNIT_CLOCK_GATE_DISABLE);
  4317. I915_WRITE(RAMCLK_GATE_D, 0);
  4318. dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
  4319. OVRUNIT_CLOCK_GATE_DISABLE |
  4320. OVCUNIT_CLOCK_GATE_DISABLE;
  4321. if (IS_GM45(dev))
  4322. dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
  4323. I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
  4324. } else if (IS_I965GM(dev)) {
  4325. I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
  4326. I915_WRITE(RENCLK_GATE_D2, 0);
  4327. I915_WRITE(DSPCLK_GATE_D, 0);
  4328. I915_WRITE(RAMCLK_GATE_D, 0);
  4329. I915_WRITE16(DEUC, 0);
  4330. } else if (IS_I965G(dev)) {
  4331. I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
  4332. I965_RCC_CLOCK_GATE_DISABLE |
  4333. I965_RCPB_CLOCK_GATE_DISABLE |
  4334. I965_ISC_CLOCK_GATE_DISABLE |
  4335. I965_FBC_CLOCK_GATE_DISABLE);
  4336. I915_WRITE(RENCLK_GATE_D2, 0);
  4337. } else if (IS_I9XX(dev)) {
  4338. u32 dstate = I915_READ(D_STATE);
  4339. dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
  4340. DSTATE_DOT_CLOCK_GATING;
  4341. I915_WRITE(D_STATE, dstate);
  4342. } else if (IS_I85X(dev) || IS_I865G(dev)) {
  4343. I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
  4344. } else if (IS_I830(dev)) {
  4345. I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
  4346. }
  4347. /*
  4348. * GPU can automatically power down the render unit if given a page
  4349. * to save state.
  4350. */
  4351. if (I915_HAS_RC6(dev) && drm_core_check_feature(dev, DRIVER_MODESET)) {
  4352. struct drm_i915_gem_object *obj_priv = NULL;
  4353. if (dev_priv->pwrctx) {
  4354. obj_priv = to_intel_bo(dev_priv->pwrctx);
  4355. } else {
  4356. struct drm_gem_object *pwrctx;
  4357. pwrctx = intel_alloc_power_context(dev);
  4358. if (pwrctx) {
  4359. dev_priv->pwrctx = pwrctx;
  4360. obj_priv = to_intel_bo(pwrctx);
  4361. }
  4362. }
  4363. if (obj_priv) {
  4364. I915_WRITE(PWRCTXA, obj_priv->gtt_offset | PWRCTX_EN);
  4365. I915_WRITE(MCHBAR_RENDER_STANDBY,
  4366. I915_READ(MCHBAR_RENDER_STANDBY) & ~RCX_SW_EXIT);
  4367. }
  4368. }
  4369. }
  4370. /* Set up chip specific display functions */
  4371. static void intel_init_display(struct drm_device *dev)
  4372. {
  4373. struct drm_i915_private *dev_priv = dev->dev_private;
  4374. /* We always want a DPMS function */
  4375. if (HAS_PCH_SPLIT(dev))
  4376. dev_priv->display.dpms = ironlake_crtc_dpms;
  4377. else
  4378. dev_priv->display.dpms = i9xx_crtc_dpms;
  4379. /* Only mobile has FBC, leave pointers NULL for other chips */
  4380. if (IS_MOBILE(dev)) {
  4381. if (IS_GM45(dev)) {
  4382. dev_priv->display.fbc_enabled = g4x_fbc_enabled;
  4383. dev_priv->display.enable_fbc = g4x_enable_fbc;
  4384. dev_priv->display.disable_fbc = g4x_disable_fbc;
  4385. } else if (IS_I965GM(dev)) {
  4386. dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
  4387. dev_priv->display.enable_fbc = i8xx_enable_fbc;
  4388. dev_priv->display.disable_fbc = i8xx_disable_fbc;
  4389. }
  4390. /* 855GM needs testing */
  4391. }
  4392. /* Returns the core display clock speed */
  4393. if (IS_I945G(dev) || (IS_G33(dev) && ! IS_PINEVIEW_M(dev)))
  4394. dev_priv->display.get_display_clock_speed =
  4395. i945_get_display_clock_speed;
  4396. else if (IS_I915G(dev))
  4397. dev_priv->display.get_display_clock_speed =
  4398. i915_get_display_clock_speed;
  4399. else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
  4400. dev_priv->display.get_display_clock_speed =
  4401. i9xx_misc_get_display_clock_speed;
  4402. else if (IS_I915GM(dev))
  4403. dev_priv->display.get_display_clock_speed =
  4404. i915gm_get_display_clock_speed;
  4405. else if (IS_I865G(dev))
  4406. dev_priv->display.get_display_clock_speed =
  4407. i865_get_display_clock_speed;
  4408. else if (IS_I85X(dev))
  4409. dev_priv->display.get_display_clock_speed =
  4410. i855_get_display_clock_speed;
  4411. else /* 852, 830 */
  4412. dev_priv->display.get_display_clock_speed =
  4413. i830_get_display_clock_speed;
  4414. /* For FIFO watermark updates */
  4415. if (HAS_PCH_SPLIT(dev))
  4416. dev_priv->display.update_wm = NULL;
  4417. else if (IS_G4X(dev))
  4418. dev_priv->display.update_wm = g4x_update_wm;
  4419. else if (IS_I965G(dev))
  4420. dev_priv->display.update_wm = i965_update_wm;
  4421. else if (IS_I9XX(dev) || IS_MOBILE(dev)) {
  4422. dev_priv->display.update_wm = i9xx_update_wm;
  4423. dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
  4424. } else {
  4425. if (IS_I85X(dev))
  4426. dev_priv->display.get_fifo_size = i85x_get_fifo_size;
  4427. else if (IS_845G(dev))
  4428. dev_priv->display.get_fifo_size = i845_get_fifo_size;
  4429. else
  4430. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  4431. dev_priv->display.update_wm = i830_update_wm;
  4432. }
  4433. }
  4434. void intel_modeset_init(struct drm_device *dev)
  4435. {
  4436. struct drm_i915_private *dev_priv = dev->dev_private;
  4437. int num_pipe;
  4438. int i;
  4439. drm_mode_config_init(dev);
  4440. dev->mode_config.min_width = 0;
  4441. dev->mode_config.min_height = 0;
  4442. dev->mode_config.funcs = (void *)&intel_mode_funcs;
  4443. intel_init_display(dev);
  4444. if (IS_I965G(dev)) {
  4445. dev->mode_config.max_width = 8192;
  4446. dev->mode_config.max_height = 8192;
  4447. } else if (IS_I9XX(dev)) {
  4448. dev->mode_config.max_width = 4096;
  4449. dev->mode_config.max_height = 4096;
  4450. } else {
  4451. dev->mode_config.max_width = 2048;
  4452. dev->mode_config.max_height = 2048;
  4453. }
  4454. /* set memory base */
  4455. if (IS_I9XX(dev))
  4456. dev->mode_config.fb_base = pci_resource_start(dev->pdev, 2);
  4457. else
  4458. dev->mode_config.fb_base = pci_resource_start(dev->pdev, 0);
  4459. if (IS_MOBILE(dev) || IS_I9XX(dev))
  4460. num_pipe = 2;
  4461. else
  4462. num_pipe = 1;
  4463. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  4464. num_pipe, num_pipe > 1 ? "s" : "");
  4465. for (i = 0; i < num_pipe; i++) {
  4466. intel_crtc_init(dev, i);
  4467. }
  4468. intel_setup_outputs(dev);
  4469. intel_init_clock_gating(dev);
  4470. if (IS_IRONLAKE_M(dev))
  4471. ironlake_enable_drps(dev);
  4472. INIT_WORK(&dev_priv->idle_work, intel_idle_update);
  4473. setup_timer(&dev_priv->idle_timer, intel_gpu_idle_timer,
  4474. (unsigned long)dev);
  4475. intel_setup_overlay(dev);
  4476. if (IS_PINEVIEW(dev) && !intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
  4477. dev_priv->fsb_freq,
  4478. dev_priv->mem_freq))
  4479. DRM_INFO("failed to find known CxSR latency "
  4480. "(found fsb freq %d, mem freq %d), disabling CxSR\n",
  4481. dev_priv->fsb_freq, dev_priv->mem_freq);
  4482. }
  4483. void intel_modeset_cleanup(struct drm_device *dev)
  4484. {
  4485. struct drm_i915_private *dev_priv = dev->dev_private;
  4486. struct drm_crtc *crtc;
  4487. struct intel_crtc *intel_crtc;
  4488. mutex_lock(&dev->struct_mutex);
  4489. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  4490. /* Skip inactive CRTCs */
  4491. if (!crtc->fb)
  4492. continue;
  4493. intel_crtc = to_intel_crtc(crtc);
  4494. intel_increase_pllclock(crtc, false);
  4495. del_timer_sync(&intel_crtc->idle_timer);
  4496. }
  4497. del_timer_sync(&dev_priv->idle_timer);
  4498. if (dev_priv->display.disable_fbc)
  4499. dev_priv->display.disable_fbc(dev);
  4500. if (dev_priv->pwrctx) {
  4501. struct drm_i915_gem_object *obj_priv;
  4502. obj_priv = to_intel_bo(dev_priv->pwrctx);
  4503. I915_WRITE(PWRCTXA, obj_priv->gtt_offset &~ PWRCTX_EN);
  4504. I915_READ(PWRCTXA);
  4505. i915_gem_object_unpin(dev_priv->pwrctx);
  4506. drm_gem_object_unreference(dev_priv->pwrctx);
  4507. }
  4508. if (IS_IRONLAKE_M(dev))
  4509. ironlake_disable_drps(dev);
  4510. mutex_unlock(&dev->struct_mutex);
  4511. drm_mode_config_cleanup(dev);
  4512. }
  4513. /*
  4514. * Return which encoder is currently attached for connector.
  4515. */
  4516. struct drm_encoder *intel_attached_encoder (struct drm_connector *connector)
  4517. {
  4518. struct drm_mode_object *obj;
  4519. struct drm_encoder *encoder;
  4520. int i;
  4521. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  4522. if (connector->encoder_ids[i] == 0)
  4523. break;
  4524. obj = drm_mode_object_find(connector->dev,
  4525. connector->encoder_ids[i],
  4526. DRM_MODE_OBJECT_ENCODER);
  4527. if (!obj)
  4528. continue;
  4529. encoder = obj_to_encoder(obj);
  4530. return encoder;
  4531. }
  4532. return NULL;
  4533. }
  4534. /*
  4535. * set vga decode state - true == enable VGA decode
  4536. */
  4537. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  4538. {
  4539. struct drm_i915_private *dev_priv = dev->dev_private;
  4540. u16 gmch_ctrl;
  4541. pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
  4542. if (state)
  4543. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  4544. else
  4545. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  4546. pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
  4547. return 0;
  4548. }