intel_display.c 141 KB

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