intel_display.c 81 KB

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