intel_display.c 98 KB

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