intel_display.c 138 KB

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