intel_display.c 118 KB

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