intel_display.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046
  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 "drm_crtc_helper.h"
  32. bool intel_pipe_has_type (struct drm_crtc *crtc, int type);
  33. typedef struct {
  34. /* given values */
  35. int n;
  36. int m1, m2;
  37. int p1, p2;
  38. /* derived values */
  39. int dot;
  40. int vco;
  41. int m;
  42. int p;
  43. } intel_clock_t;
  44. typedef struct {
  45. int min, max;
  46. } intel_range_t;
  47. typedef struct {
  48. int dot_limit;
  49. int p2_slow, p2_fast;
  50. } intel_p2_t;
  51. #define INTEL_P2_NUM 2
  52. typedef struct intel_limit intel_limit_t;
  53. struct intel_limit {
  54. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  55. intel_p2_t p2;
  56. bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
  57. int, int, intel_clock_t *);
  58. };
  59. #define I8XX_DOT_MIN 25000
  60. #define I8XX_DOT_MAX 350000
  61. #define I8XX_VCO_MIN 930000
  62. #define I8XX_VCO_MAX 1400000
  63. #define I8XX_N_MIN 3
  64. #define I8XX_N_MAX 16
  65. #define I8XX_M_MIN 96
  66. #define I8XX_M_MAX 140
  67. #define I8XX_M1_MIN 18
  68. #define I8XX_M1_MAX 26
  69. #define I8XX_M2_MIN 6
  70. #define I8XX_M2_MAX 16
  71. #define I8XX_P_MIN 4
  72. #define I8XX_P_MAX 128
  73. #define I8XX_P1_MIN 2
  74. #define I8XX_P1_MAX 33
  75. #define I8XX_P1_LVDS_MIN 1
  76. #define I8XX_P1_LVDS_MAX 6
  77. #define I8XX_P2_SLOW 4
  78. #define I8XX_P2_FAST 2
  79. #define I8XX_P2_LVDS_SLOW 14
  80. #define I8XX_P2_LVDS_FAST 14 /* No fast option */
  81. #define I8XX_P2_SLOW_LIMIT 165000
  82. #define I9XX_DOT_MIN 20000
  83. #define I9XX_DOT_MAX 400000
  84. #define I9XX_VCO_MIN 1400000
  85. #define I9XX_VCO_MAX 2800000
  86. #define IGD_VCO_MIN 1700000
  87. #define IGD_VCO_MAX 3500000
  88. #define I9XX_N_MIN 1
  89. #define I9XX_N_MAX 6
  90. /* IGD's Ncounter is a ring counter */
  91. #define IGD_N_MIN 3
  92. #define IGD_N_MAX 6
  93. #define I9XX_M_MIN 70
  94. #define I9XX_M_MAX 120
  95. #define IGD_M_MIN 2
  96. #define IGD_M_MAX 256
  97. #define I9XX_M1_MIN 10
  98. #define I9XX_M1_MAX 22
  99. #define I9XX_M2_MIN 5
  100. #define I9XX_M2_MAX 9
  101. /* IGD M1 is reserved, and must be 0 */
  102. #define IGD_M1_MIN 0
  103. #define IGD_M1_MAX 0
  104. #define IGD_M2_MIN 0
  105. #define IGD_M2_MAX 254
  106. #define I9XX_P_SDVO_DAC_MIN 5
  107. #define I9XX_P_SDVO_DAC_MAX 80
  108. #define I9XX_P_LVDS_MIN 7
  109. #define I9XX_P_LVDS_MAX 98
  110. #define IGD_P_LVDS_MIN 7
  111. #define IGD_P_LVDS_MAX 112
  112. #define I9XX_P1_MIN 1
  113. #define I9XX_P1_MAX 8
  114. #define I9XX_P2_SDVO_DAC_SLOW 10
  115. #define I9XX_P2_SDVO_DAC_FAST 5
  116. #define I9XX_P2_SDVO_DAC_SLOW_LIMIT 200000
  117. #define I9XX_P2_LVDS_SLOW 14
  118. #define I9XX_P2_LVDS_FAST 7
  119. #define I9XX_P2_LVDS_SLOW_LIMIT 112000
  120. #define INTEL_LIMIT_I8XX_DVO_DAC 0
  121. #define INTEL_LIMIT_I8XX_LVDS 1
  122. #define INTEL_LIMIT_I9XX_SDVO_DAC 2
  123. #define INTEL_LIMIT_I9XX_LVDS 3
  124. #define INTEL_LIMIT_G4X_SDVO 4
  125. #define INTEL_LIMIT_G4X_HDMI_DAC 5
  126. #define INTEL_LIMIT_G4X_SINGLE_CHANNEL_LVDS 6
  127. #define INTEL_LIMIT_G4X_DUAL_CHANNEL_LVDS 7
  128. #define INTEL_LIMIT_IGD_SDVO_DAC 8
  129. #define INTEL_LIMIT_IGD_LVDS 9
  130. /*The parameter is for SDVO on G4x platform*/
  131. #define G4X_DOT_SDVO_MIN 25000
  132. #define G4X_DOT_SDVO_MAX 270000
  133. #define G4X_VCO_MIN 1750000
  134. #define G4X_VCO_MAX 3500000
  135. #define G4X_N_SDVO_MIN 1
  136. #define G4X_N_SDVO_MAX 4
  137. #define G4X_M_SDVO_MIN 104
  138. #define G4X_M_SDVO_MAX 138
  139. #define G4X_M1_SDVO_MIN 17
  140. #define G4X_M1_SDVO_MAX 23
  141. #define G4X_M2_SDVO_MIN 5
  142. #define G4X_M2_SDVO_MAX 11
  143. #define G4X_P_SDVO_MIN 10
  144. #define G4X_P_SDVO_MAX 30
  145. #define G4X_P1_SDVO_MIN 1
  146. #define G4X_P1_SDVO_MAX 3
  147. #define G4X_P2_SDVO_SLOW 10
  148. #define G4X_P2_SDVO_FAST 10
  149. #define G4X_P2_SDVO_LIMIT 270000
  150. /*The parameter is for HDMI_DAC on G4x platform*/
  151. #define G4X_DOT_HDMI_DAC_MIN 22000
  152. #define G4X_DOT_HDMI_DAC_MAX 400000
  153. #define G4X_N_HDMI_DAC_MIN 1
  154. #define G4X_N_HDMI_DAC_MAX 4
  155. #define G4X_M_HDMI_DAC_MIN 104
  156. #define G4X_M_HDMI_DAC_MAX 138
  157. #define G4X_M1_HDMI_DAC_MIN 16
  158. #define G4X_M1_HDMI_DAC_MAX 23
  159. #define G4X_M2_HDMI_DAC_MIN 5
  160. #define G4X_M2_HDMI_DAC_MAX 11
  161. #define G4X_P_HDMI_DAC_MIN 5
  162. #define G4X_P_HDMI_DAC_MAX 80
  163. #define G4X_P1_HDMI_DAC_MIN 1
  164. #define G4X_P1_HDMI_DAC_MAX 8
  165. #define G4X_P2_HDMI_DAC_SLOW 10
  166. #define G4X_P2_HDMI_DAC_FAST 5
  167. #define G4X_P2_HDMI_DAC_LIMIT 165000
  168. /*The parameter is for SINGLE_CHANNEL_LVDS on G4x platform*/
  169. #define G4X_DOT_SINGLE_CHANNEL_LVDS_MIN 20000
  170. #define G4X_DOT_SINGLE_CHANNEL_LVDS_MAX 115000
  171. #define G4X_N_SINGLE_CHANNEL_LVDS_MIN 1
  172. #define G4X_N_SINGLE_CHANNEL_LVDS_MAX 3
  173. #define G4X_M_SINGLE_CHANNEL_LVDS_MIN 104
  174. #define G4X_M_SINGLE_CHANNEL_LVDS_MAX 138
  175. #define G4X_M1_SINGLE_CHANNEL_LVDS_MIN 17
  176. #define G4X_M1_SINGLE_CHANNEL_LVDS_MAX 23
  177. #define G4X_M2_SINGLE_CHANNEL_LVDS_MIN 5
  178. #define G4X_M2_SINGLE_CHANNEL_LVDS_MAX 11
  179. #define G4X_P_SINGLE_CHANNEL_LVDS_MIN 28
  180. #define G4X_P_SINGLE_CHANNEL_LVDS_MAX 112
  181. #define G4X_P1_SINGLE_CHANNEL_LVDS_MIN 2
  182. #define G4X_P1_SINGLE_CHANNEL_LVDS_MAX 8
  183. #define G4X_P2_SINGLE_CHANNEL_LVDS_SLOW 14
  184. #define G4X_P2_SINGLE_CHANNEL_LVDS_FAST 14
  185. #define G4X_P2_SINGLE_CHANNEL_LVDS_LIMIT 0
  186. /*The parameter is for DUAL_CHANNEL_LVDS on G4x platform*/
  187. #define G4X_DOT_DUAL_CHANNEL_LVDS_MIN 80000
  188. #define G4X_DOT_DUAL_CHANNEL_LVDS_MAX 224000
  189. #define G4X_N_DUAL_CHANNEL_LVDS_MIN 1
  190. #define G4X_N_DUAL_CHANNEL_LVDS_MAX 3
  191. #define G4X_M_DUAL_CHANNEL_LVDS_MIN 104
  192. #define G4X_M_DUAL_CHANNEL_LVDS_MAX 138
  193. #define G4X_M1_DUAL_CHANNEL_LVDS_MIN 17
  194. #define G4X_M1_DUAL_CHANNEL_LVDS_MAX 23
  195. #define G4X_M2_DUAL_CHANNEL_LVDS_MIN 5
  196. #define G4X_M2_DUAL_CHANNEL_LVDS_MAX 11
  197. #define G4X_P_DUAL_CHANNEL_LVDS_MIN 14
  198. #define G4X_P_DUAL_CHANNEL_LVDS_MAX 42
  199. #define G4X_P1_DUAL_CHANNEL_LVDS_MIN 2
  200. #define G4X_P1_DUAL_CHANNEL_LVDS_MAX 6
  201. #define G4X_P2_DUAL_CHANNEL_LVDS_SLOW 7
  202. #define G4X_P2_DUAL_CHANNEL_LVDS_FAST 7
  203. #define G4X_P2_DUAL_CHANNEL_LVDS_LIMIT 0
  204. static bool
  205. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  206. int target, int refclk, intel_clock_t *best_clock);
  207. static bool
  208. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  209. int target, int refclk, intel_clock_t *best_clock);
  210. static const intel_limit_t intel_limits[] = {
  211. { /* INTEL_LIMIT_I8XX_DVO_DAC */
  212. .dot = { .min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX },
  213. .vco = { .min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX },
  214. .n = { .min = I8XX_N_MIN, .max = I8XX_N_MAX },
  215. .m = { .min = I8XX_M_MIN, .max = I8XX_M_MAX },
  216. .m1 = { .min = I8XX_M1_MIN, .max = I8XX_M1_MAX },
  217. .m2 = { .min = I8XX_M2_MIN, .max = I8XX_M2_MAX },
  218. .p = { .min = I8XX_P_MIN, .max = I8XX_P_MAX },
  219. .p1 = { .min = I8XX_P1_MIN, .max = I8XX_P1_MAX },
  220. .p2 = { .dot_limit = I8XX_P2_SLOW_LIMIT,
  221. .p2_slow = I8XX_P2_SLOW, .p2_fast = I8XX_P2_FAST },
  222. .find_pll = intel_find_best_PLL,
  223. },
  224. { /* INTEL_LIMIT_I8XX_LVDS */
  225. .dot = { .min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX },
  226. .vco = { .min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX },
  227. .n = { .min = I8XX_N_MIN, .max = I8XX_N_MAX },
  228. .m = { .min = I8XX_M_MIN, .max = I8XX_M_MAX },
  229. .m1 = { .min = I8XX_M1_MIN, .max = I8XX_M1_MAX },
  230. .m2 = { .min = I8XX_M2_MIN, .max = I8XX_M2_MAX },
  231. .p = { .min = I8XX_P_MIN, .max = I8XX_P_MAX },
  232. .p1 = { .min = I8XX_P1_LVDS_MIN, .max = I8XX_P1_LVDS_MAX },
  233. .p2 = { .dot_limit = I8XX_P2_SLOW_LIMIT,
  234. .p2_slow = I8XX_P2_LVDS_SLOW, .p2_fast = I8XX_P2_LVDS_FAST },
  235. .find_pll = intel_find_best_PLL,
  236. },
  237. { /* INTEL_LIMIT_I9XX_SDVO_DAC */
  238. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
  239. .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
  240. .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
  241. .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
  242. .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
  243. .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
  244. .p = { .min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX },
  245. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  246. .p2 = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
  247. .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast = I9XX_P2_SDVO_DAC_FAST },
  248. .find_pll = intel_find_best_PLL,
  249. },
  250. { /* INTEL_LIMIT_I9XX_LVDS */
  251. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
  252. .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
  253. .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
  254. .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
  255. .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
  256. .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
  257. .p = { .min = I9XX_P_LVDS_MIN, .max = I9XX_P_LVDS_MAX },
  258. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  259. /* The single-channel range is 25-112Mhz, and dual-channel
  260. * is 80-224Mhz. Prefer single channel as much as possible.
  261. */
  262. .p2 = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
  263. .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_FAST },
  264. .find_pll = intel_find_best_PLL,
  265. },
  266. /* below parameter and function is for G4X Chipset Family*/
  267. { /* INTEL_LIMIT_G4X_SDVO */
  268. .dot = { .min = G4X_DOT_SDVO_MIN, .max = G4X_DOT_SDVO_MAX },
  269. .vco = { .min = G4X_VCO_MIN, .max = G4X_VCO_MAX},
  270. .n = { .min = G4X_N_SDVO_MIN, .max = G4X_N_SDVO_MAX },
  271. .m = { .min = G4X_M_SDVO_MIN, .max = G4X_M_SDVO_MAX },
  272. .m1 = { .min = G4X_M1_SDVO_MIN, .max = G4X_M1_SDVO_MAX },
  273. .m2 = { .min = G4X_M2_SDVO_MIN, .max = G4X_M2_SDVO_MAX },
  274. .p = { .min = G4X_P_SDVO_MIN, .max = G4X_P_SDVO_MAX },
  275. .p1 = { .min = G4X_P1_SDVO_MIN, .max = G4X_P1_SDVO_MAX},
  276. .p2 = { .dot_limit = G4X_P2_SDVO_LIMIT,
  277. .p2_slow = G4X_P2_SDVO_SLOW,
  278. .p2_fast = G4X_P2_SDVO_FAST
  279. },
  280. .find_pll = intel_g4x_find_best_PLL,
  281. },
  282. { /* INTEL_LIMIT_G4X_HDMI_DAC */
  283. .dot = { .min = G4X_DOT_HDMI_DAC_MIN, .max = G4X_DOT_HDMI_DAC_MAX },
  284. .vco = { .min = G4X_VCO_MIN, .max = G4X_VCO_MAX},
  285. .n = { .min = G4X_N_HDMI_DAC_MIN, .max = G4X_N_HDMI_DAC_MAX },
  286. .m = { .min = G4X_M_HDMI_DAC_MIN, .max = G4X_M_HDMI_DAC_MAX },
  287. .m1 = { .min = G4X_M1_HDMI_DAC_MIN, .max = G4X_M1_HDMI_DAC_MAX },
  288. .m2 = { .min = G4X_M2_HDMI_DAC_MIN, .max = G4X_M2_HDMI_DAC_MAX },
  289. .p = { .min = G4X_P_HDMI_DAC_MIN, .max = G4X_P_HDMI_DAC_MAX },
  290. .p1 = { .min = G4X_P1_HDMI_DAC_MIN, .max = G4X_P1_HDMI_DAC_MAX},
  291. .p2 = { .dot_limit = G4X_P2_HDMI_DAC_LIMIT,
  292. .p2_slow = G4X_P2_HDMI_DAC_SLOW,
  293. .p2_fast = G4X_P2_HDMI_DAC_FAST
  294. },
  295. .find_pll = intel_g4x_find_best_PLL,
  296. },
  297. { /* INTEL_LIMIT_G4X_SINGLE_CHANNEL_LVDS */
  298. .dot = { .min = G4X_DOT_SINGLE_CHANNEL_LVDS_MIN,
  299. .max = G4X_DOT_SINGLE_CHANNEL_LVDS_MAX },
  300. .vco = { .min = G4X_VCO_MIN,
  301. .max = G4X_VCO_MAX },
  302. .n = { .min = G4X_N_SINGLE_CHANNEL_LVDS_MIN,
  303. .max = G4X_N_SINGLE_CHANNEL_LVDS_MAX },
  304. .m = { .min = G4X_M_SINGLE_CHANNEL_LVDS_MIN,
  305. .max = G4X_M_SINGLE_CHANNEL_LVDS_MAX },
  306. .m1 = { .min = G4X_M1_SINGLE_CHANNEL_LVDS_MIN,
  307. .max = G4X_M1_SINGLE_CHANNEL_LVDS_MAX },
  308. .m2 = { .min = G4X_M2_SINGLE_CHANNEL_LVDS_MIN,
  309. .max = G4X_M2_SINGLE_CHANNEL_LVDS_MAX },
  310. .p = { .min = G4X_P_SINGLE_CHANNEL_LVDS_MIN,
  311. .max = G4X_P_SINGLE_CHANNEL_LVDS_MAX },
  312. .p1 = { .min = G4X_P1_SINGLE_CHANNEL_LVDS_MIN,
  313. .max = G4X_P1_SINGLE_CHANNEL_LVDS_MAX },
  314. .p2 = { .dot_limit = G4X_P2_SINGLE_CHANNEL_LVDS_LIMIT,
  315. .p2_slow = G4X_P2_SINGLE_CHANNEL_LVDS_SLOW,
  316. .p2_fast = G4X_P2_SINGLE_CHANNEL_LVDS_FAST
  317. },
  318. .find_pll = intel_g4x_find_best_PLL,
  319. },
  320. { /* INTEL_LIMIT_G4X_DUAL_CHANNEL_LVDS */
  321. .dot = { .min = G4X_DOT_DUAL_CHANNEL_LVDS_MIN,
  322. .max = G4X_DOT_DUAL_CHANNEL_LVDS_MAX },
  323. .vco = { .min = G4X_VCO_MIN,
  324. .max = G4X_VCO_MAX },
  325. .n = { .min = G4X_N_DUAL_CHANNEL_LVDS_MIN,
  326. .max = G4X_N_DUAL_CHANNEL_LVDS_MAX },
  327. .m = { .min = G4X_M_DUAL_CHANNEL_LVDS_MIN,
  328. .max = G4X_M_DUAL_CHANNEL_LVDS_MAX },
  329. .m1 = { .min = G4X_M1_DUAL_CHANNEL_LVDS_MIN,
  330. .max = G4X_M1_DUAL_CHANNEL_LVDS_MAX },
  331. .m2 = { .min = G4X_M2_DUAL_CHANNEL_LVDS_MIN,
  332. .max = G4X_M2_DUAL_CHANNEL_LVDS_MAX },
  333. .p = { .min = G4X_P_DUAL_CHANNEL_LVDS_MIN,
  334. .max = G4X_P_DUAL_CHANNEL_LVDS_MAX },
  335. .p1 = { .min = G4X_P1_DUAL_CHANNEL_LVDS_MIN,
  336. .max = G4X_P1_DUAL_CHANNEL_LVDS_MAX },
  337. .p2 = { .dot_limit = G4X_P2_DUAL_CHANNEL_LVDS_LIMIT,
  338. .p2_slow = G4X_P2_DUAL_CHANNEL_LVDS_SLOW,
  339. .p2_fast = G4X_P2_DUAL_CHANNEL_LVDS_FAST
  340. },
  341. .find_pll = intel_g4x_find_best_PLL,
  342. },
  343. { /* INTEL_LIMIT_IGD_SDVO */
  344. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX},
  345. .vco = { .min = IGD_VCO_MIN, .max = IGD_VCO_MAX },
  346. .n = { .min = IGD_N_MIN, .max = IGD_N_MAX },
  347. .m = { .min = IGD_M_MIN, .max = IGD_M_MAX },
  348. .m1 = { .min = IGD_M1_MIN, .max = IGD_M1_MAX },
  349. .m2 = { .min = IGD_M2_MIN, .max = IGD_M2_MAX },
  350. .p = { .min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX },
  351. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  352. .p2 = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
  353. .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast = I9XX_P2_SDVO_DAC_FAST },
  354. },
  355. { /* INTEL_LIMIT_IGD_LVDS */
  356. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
  357. .vco = { .min = IGD_VCO_MIN, .max = IGD_VCO_MAX },
  358. .n = { .min = IGD_N_MIN, .max = IGD_N_MAX },
  359. .m = { .min = IGD_M_MIN, .max = IGD_M_MAX },
  360. .m1 = { .min = IGD_M1_MIN, .max = IGD_M1_MAX },
  361. .m2 = { .min = IGD_M2_MIN, .max = IGD_M2_MAX },
  362. .p = { .min = IGD_P_LVDS_MIN, .max = IGD_P_LVDS_MAX },
  363. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  364. /* IGD only supports single-channel mode. */
  365. .p2 = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
  366. .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_SLOW },
  367. },
  368. };
  369. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  370. {
  371. struct drm_device *dev = crtc->dev;
  372. struct drm_i915_private *dev_priv = dev->dev_private;
  373. const intel_limit_t *limit;
  374. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  375. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  376. LVDS_CLKB_POWER_UP)
  377. /* LVDS with dual channel */
  378. limit = &intel_limits
  379. [INTEL_LIMIT_G4X_DUAL_CHANNEL_LVDS];
  380. else
  381. /* LVDS with dual channel */
  382. limit = &intel_limits
  383. [INTEL_LIMIT_G4X_SINGLE_CHANNEL_LVDS];
  384. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  385. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  386. limit = &intel_limits[INTEL_LIMIT_G4X_HDMI_DAC];
  387. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  388. limit = &intel_limits[INTEL_LIMIT_G4X_SDVO];
  389. } else /* The option is for other outputs */
  390. limit = &intel_limits[INTEL_LIMIT_I9XX_SDVO_DAC];
  391. return limit;
  392. }
  393. static const intel_limit_t *intel_limit(struct drm_crtc *crtc)
  394. {
  395. struct drm_device *dev = crtc->dev;
  396. const intel_limit_t *limit;
  397. if (IS_G4X(dev)) {
  398. limit = intel_g4x_limit(crtc);
  399. } else if (IS_I9XX(dev) && !IS_IGD(dev)) {
  400. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  401. limit = &intel_limits[INTEL_LIMIT_I9XX_LVDS];
  402. else
  403. limit = &intel_limits[INTEL_LIMIT_I9XX_SDVO_DAC];
  404. } else if (IS_IGD(dev)) {
  405. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  406. limit = &intel_limits[INTEL_LIMIT_IGD_LVDS];
  407. else
  408. limit = &intel_limits[INTEL_LIMIT_IGD_SDVO_DAC];
  409. } else {
  410. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  411. limit = &intel_limits[INTEL_LIMIT_I8XX_LVDS];
  412. else
  413. limit = &intel_limits[INTEL_LIMIT_I8XX_DVO_DAC];
  414. }
  415. return limit;
  416. }
  417. /* m1 is reserved as 0 in IGD, n is a ring counter */
  418. static void igd_clock(int refclk, intel_clock_t *clock)
  419. {
  420. clock->m = clock->m2 + 2;
  421. clock->p = clock->p1 * clock->p2;
  422. clock->vco = refclk * clock->m / clock->n;
  423. clock->dot = clock->vco / clock->p;
  424. }
  425. static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
  426. {
  427. if (IS_IGD(dev)) {
  428. igd_clock(refclk, clock);
  429. return;
  430. }
  431. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  432. clock->p = clock->p1 * clock->p2;
  433. clock->vco = refclk * clock->m / (clock->n + 2);
  434. clock->dot = clock->vco / clock->p;
  435. }
  436. /**
  437. * Returns whether any output on the specified pipe is of the specified type
  438. */
  439. bool intel_pipe_has_type (struct drm_crtc *crtc, int type)
  440. {
  441. struct drm_device *dev = crtc->dev;
  442. struct drm_mode_config *mode_config = &dev->mode_config;
  443. struct drm_connector *l_entry;
  444. list_for_each_entry(l_entry, &mode_config->connector_list, head) {
  445. if (l_entry->encoder &&
  446. l_entry->encoder->crtc == crtc) {
  447. struct intel_output *intel_output = to_intel_output(l_entry);
  448. if (intel_output->type == type)
  449. return true;
  450. }
  451. }
  452. return false;
  453. }
  454. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  455. /**
  456. * Returns whether the given set of divisors are valid for a given refclk with
  457. * the given connectors.
  458. */
  459. static bool intel_PLL_is_valid(struct drm_crtc *crtc, intel_clock_t *clock)
  460. {
  461. const intel_limit_t *limit = intel_limit (crtc);
  462. struct drm_device *dev = crtc->dev;
  463. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  464. INTELPllInvalid ("p1 out of range\n");
  465. if (clock->p < limit->p.min || limit->p.max < clock->p)
  466. INTELPllInvalid ("p out of range\n");
  467. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  468. INTELPllInvalid ("m2 out of range\n");
  469. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  470. INTELPllInvalid ("m1 out of range\n");
  471. if (clock->m1 <= clock->m2 && !IS_IGD(dev))
  472. INTELPllInvalid ("m1 <= m2\n");
  473. if (clock->m < limit->m.min || limit->m.max < clock->m)
  474. INTELPllInvalid ("m out of range\n");
  475. if (clock->n < limit->n.min || limit->n.max < clock->n)
  476. INTELPllInvalid ("n out of range\n");
  477. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  478. INTELPllInvalid ("vco out of range\n");
  479. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  480. * connector, etc., rather than just a single range.
  481. */
  482. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  483. INTELPllInvalid ("dot out of range\n");
  484. return true;
  485. }
  486. static bool
  487. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  488. int target, int refclk, intel_clock_t *best_clock)
  489. {
  490. struct drm_device *dev = crtc->dev;
  491. struct drm_i915_private *dev_priv = dev->dev_private;
  492. intel_clock_t clock;
  493. int err = target;
  494. if (IS_I9XX(dev) && intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  495. (I915_READ(LVDS) & LVDS_PORT_EN) != 0) {
  496. /*
  497. * For LVDS, if the panel is on, just rely on its current
  498. * settings for dual-channel. We haven't figured out how to
  499. * reliably set up different single/dual channel state, if we
  500. * even can.
  501. */
  502. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  503. LVDS_CLKB_POWER_UP)
  504. clock.p2 = limit->p2.p2_fast;
  505. else
  506. clock.p2 = limit->p2.p2_slow;
  507. } else {
  508. if (target < limit->p2.dot_limit)
  509. clock.p2 = limit->p2.p2_slow;
  510. else
  511. clock.p2 = limit->p2.p2_fast;
  512. }
  513. memset (best_clock, 0, sizeof (*best_clock));
  514. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  515. for (clock.m2 = limit->m2.min; clock.m2 <= limit->m2.max; clock.m2++) {
  516. /* m1 is always 0 in IGD */
  517. if (clock.m2 >= clock.m1 && !IS_IGD(dev))
  518. break;
  519. for (clock.n = limit->n.min; clock.n <= limit->n.max;
  520. clock.n++) {
  521. for (clock.p1 = limit->p1.min;
  522. clock.p1 <= limit->p1.max; clock.p1++) {
  523. int this_err;
  524. intel_clock(dev, refclk, &clock);
  525. if (!intel_PLL_is_valid(crtc, &clock))
  526. continue;
  527. this_err = abs(clock.dot - target);
  528. if (this_err < err) {
  529. *best_clock = clock;
  530. err = this_err;
  531. }
  532. }
  533. }
  534. }
  535. }
  536. return (err != target);
  537. }
  538. static bool
  539. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  540. int target, int refclk, intel_clock_t *best_clock)
  541. {
  542. struct drm_device *dev = crtc->dev;
  543. struct drm_i915_private *dev_priv = dev->dev_private;
  544. intel_clock_t clock;
  545. int max_n;
  546. bool found;
  547. /* approximately equals target * 0.00488 */
  548. int err_most = (target >> 8) + (target >> 10);
  549. found = false;
  550. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  551. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  552. LVDS_CLKB_POWER_UP)
  553. clock.p2 = limit->p2.p2_fast;
  554. else
  555. clock.p2 = limit->p2.p2_slow;
  556. } else {
  557. if (target < limit->p2.dot_limit)
  558. clock.p2 = limit->p2.p2_slow;
  559. else
  560. clock.p2 = limit->p2.p2_fast;
  561. }
  562. memset(best_clock, 0, sizeof(*best_clock));
  563. max_n = limit->n.max;
  564. /* based on hardware requriment prefer smaller n to precision */
  565. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  566. /* based on hardware requirment prefere larger m1,m2, p1 */
  567. for (clock.m1 = limit->m1.max;
  568. clock.m1 >= limit->m1.min; clock.m1--) {
  569. for (clock.m2 = limit->m2.max;
  570. clock.m2 >= limit->m2.min; clock.m2--) {
  571. for (clock.p1 = limit->p1.max;
  572. clock.p1 >= limit->p1.min; clock.p1--) {
  573. int this_err;
  574. intel_clock(dev, refclk, &clock);
  575. if (!intel_PLL_is_valid(crtc, &clock))
  576. continue;
  577. this_err = abs(clock.dot - target) ;
  578. if (this_err < err_most) {
  579. *best_clock = clock;
  580. err_most = this_err;
  581. max_n = clock.n;
  582. found = true;
  583. }
  584. }
  585. }
  586. }
  587. }
  588. return found;
  589. }
  590. void
  591. intel_wait_for_vblank(struct drm_device *dev)
  592. {
  593. /* Wait for 20ms, i.e. one cycle at 50hz. */
  594. mdelay(20);
  595. }
  596. static int
  597. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  598. struct drm_framebuffer *old_fb)
  599. {
  600. struct drm_device *dev = crtc->dev;
  601. struct drm_i915_private *dev_priv = dev->dev_private;
  602. struct drm_i915_master_private *master_priv;
  603. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  604. struct intel_framebuffer *intel_fb;
  605. struct drm_i915_gem_object *obj_priv;
  606. struct drm_gem_object *obj;
  607. int pipe = intel_crtc->pipe;
  608. unsigned long Start, Offset;
  609. int dspbase = (pipe == 0 ? DSPAADDR : DSPBADDR);
  610. int dspsurf = (pipe == 0 ? DSPASURF : DSPBSURF);
  611. int dspstride = (pipe == 0) ? DSPASTRIDE : DSPBSTRIDE;
  612. int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
  613. u32 dspcntr, alignment;
  614. int ret;
  615. /* no fb bound */
  616. if (!crtc->fb) {
  617. DRM_DEBUG("No FB bound\n");
  618. return 0;
  619. }
  620. switch (pipe) {
  621. case 0:
  622. case 1:
  623. break;
  624. default:
  625. DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
  626. return -EINVAL;
  627. }
  628. intel_fb = to_intel_framebuffer(crtc->fb);
  629. obj = intel_fb->obj;
  630. obj_priv = obj->driver_private;
  631. switch (obj_priv->tiling_mode) {
  632. case I915_TILING_NONE:
  633. alignment = 64 * 1024;
  634. break;
  635. case I915_TILING_X:
  636. /* pin() will align the object as required by fence */
  637. alignment = 0;
  638. break;
  639. case I915_TILING_Y:
  640. /* FIXME: Is this true? */
  641. DRM_ERROR("Y tiled not allowed for scan out buffers\n");
  642. return -EINVAL;
  643. default:
  644. BUG();
  645. }
  646. mutex_lock(&dev->struct_mutex);
  647. ret = i915_gem_object_pin(intel_fb->obj, alignment);
  648. if (ret != 0) {
  649. mutex_unlock(&dev->struct_mutex);
  650. return ret;
  651. }
  652. ret = i915_gem_object_set_to_gtt_domain(intel_fb->obj, 1);
  653. if (ret != 0) {
  654. i915_gem_object_unpin(intel_fb->obj);
  655. mutex_unlock(&dev->struct_mutex);
  656. return ret;
  657. }
  658. dspcntr = I915_READ(dspcntr_reg);
  659. /* Mask out pixel format bits in case we change it */
  660. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  661. switch (crtc->fb->bits_per_pixel) {
  662. case 8:
  663. dspcntr |= DISPPLANE_8BPP;
  664. break;
  665. case 16:
  666. if (crtc->fb->depth == 15)
  667. dspcntr |= DISPPLANE_15_16BPP;
  668. else
  669. dspcntr |= DISPPLANE_16BPP;
  670. break;
  671. case 24:
  672. case 32:
  673. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  674. break;
  675. default:
  676. DRM_ERROR("Unknown color depth\n");
  677. i915_gem_object_unpin(intel_fb->obj);
  678. mutex_unlock(&dev->struct_mutex);
  679. return -EINVAL;
  680. }
  681. I915_WRITE(dspcntr_reg, dspcntr);
  682. Start = obj_priv->gtt_offset;
  683. Offset = y * crtc->fb->pitch + x * (crtc->fb->bits_per_pixel / 8);
  684. DRM_DEBUG("Writing base %08lX %08lX %d %d\n", Start, Offset, x, y);
  685. I915_WRITE(dspstride, crtc->fb->pitch);
  686. if (IS_I965G(dev)) {
  687. I915_WRITE(dspbase, Offset);
  688. I915_READ(dspbase);
  689. I915_WRITE(dspsurf, Start);
  690. I915_READ(dspsurf);
  691. } else {
  692. I915_WRITE(dspbase, Start + Offset);
  693. I915_READ(dspbase);
  694. }
  695. intel_wait_for_vblank(dev);
  696. if (old_fb) {
  697. intel_fb = to_intel_framebuffer(old_fb);
  698. i915_gem_object_unpin(intel_fb->obj);
  699. }
  700. mutex_unlock(&dev->struct_mutex);
  701. if (!dev->primary->master)
  702. return 0;
  703. master_priv = dev->primary->master->driver_priv;
  704. if (!master_priv->sarea_priv)
  705. return 0;
  706. if (pipe) {
  707. master_priv->sarea_priv->pipeB_x = x;
  708. master_priv->sarea_priv->pipeB_y = y;
  709. } else {
  710. master_priv->sarea_priv->pipeA_x = x;
  711. master_priv->sarea_priv->pipeA_y = y;
  712. }
  713. return 0;
  714. }
  715. /**
  716. * Sets the power management mode of the pipe and plane.
  717. *
  718. * This code should probably grow support for turning the cursor off and back
  719. * on appropriately at the same time as we're turning the pipe off/on.
  720. */
  721. static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
  722. {
  723. struct drm_device *dev = crtc->dev;
  724. struct drm_i915_master_private *master_priv;
  725. struct drm_i915_private *dev_priv = dev->dev_private;
  726. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  727. int pipe = intel_crtc->pipe;
  728. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  729. int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
  730. int dspbase_reg = (pipe == 0) ? DSPAADDR : DSPBADDR;
  731. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  732. u32 temp;
  733. bool enabled;
  734. /* XXX: When our outputs are all unaware of DPMS modes other than off
  735. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  736. */
  737. switch (mode) {
  738. case DRM_MODE_DPMS_ON:
  739. case DRM_MODE_DPMS_STANDBY:
  740. case DRM_MODE_DPMS_SUSPEND:
  741. /* Enable the DPLL */
  742. temp = I915_READ(dpll_reg);
  743. if ((temp & DPLL_VCO_ENABLE) == 0) {
  744. I915_WRITE(dpll_reg, temp);
  745. I915_READ(dpll_reg);
  746. /* Wait for the clocks to stabilize. */
  747. udelay(150);
  748. I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
  749. I915_READ(dpll_reg);
  750. /* Wait for the clocks to stabilize. */
  751. udelay(150);
  752. I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
  753. I915_READ(dpll_reg);
  754. /* Wait for the clocks to stabilize. */
  755. udelay(150);
  756. }
  757. /* Enable the pipe */
  758. temp = I915_READ(pipeconf_reg);
  759. if ((temp & PIPEACONF_ENABLE) == 0)
  760. I915_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
  761. /* Enable the plane */
  762. temp = I915_READ(dspcntr_reg);
  763. if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
  764. I915_WRITE(dspcntr_reg, temp | DISPLAY_PLANE_ENABLE);
  765. /* Flush the plane changes */
  766. I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
  767. }
  768. intel_crtc_load_lut(crtc);
  769. /* Give the overlay scaler a chance to enable if it's on this pipe */
  770. //intel_crtc_dpms_video(crtc, true); TODO
  771. break;
  772. case DRM_MODE_DPMS_OFF:
  773. /* Give the overlay scaler a chance to disable if it's on this pipe */
  774. //intel_crtc_dpms_video(crtc, FALSE); TODO
  775. /* Disable the VGA plane that we never use */
  776. I915_WRITE(VGACNTRL, VGA_DISP_DISABLE);
  777. /* Disable display plane */
  778. temp = I915_READ(dspcntr_reg);
  779. if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
  780. I915_WRITE(dspcntr_reg, temp & ~DISPLAY_PLANE_ENABLE);
  781. /* Flush the plane changes */
  782. I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
  783. I915_READ(dspbase_reg);
  784. }
  785. if (!IS_I9XX(dev)) {
  786. /* Wait for vblank for the disable to take effect */
  787. intel_wait_for_vblank(dev);
  788. }
  789. /* Next, disable display pipes */
  790. temp = I915_READ(pipeconf_reg);
  791. if ((temp & PIPEACONF_ENABLE) != 0) {
  792. I915_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
  793. I915_READ(pipeconf_reg);
  794. }
  795. /* Wait for vblank for the disable to take effect. */
  796. intel_wait_for_vblank(dev);
  797. temp = I915_READ(dpll_reg);
  798. if ((temp & DPLL_VCO_ENABLE) != 0) {
  799. I915_WRITE(dpll_reg, temp & ~DPLL_VCO_ENABLE);
  800. I915_READ(dpll_reg);
  801. }
  802. /* Wait for the clocks to turn off. */
  803. udelay(150);
  804. break;
  805. }
  806. if (!dev->primary->master)
  807. return;
  808. master_priv = dev->primary->master->driver_priv;
  809. if (!master_priv->sarea_priv)
  810. return;
  811. enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
  812. switch (pipe) {
  813. case 0:
  814. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  815. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  816. break;
  817. case 1:
  818. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  819. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  820. break;
  821. default:
  822. DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
  823. break;
  824. }
  825. intel_crtc->dpms_mode = mode;
  826. }
  827. static void intel_crtc_prepare (struct drm_crtc *crtc)
  828. {
  829. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  830. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  831. }
  832. static void intel_crtc_commit (struct drm_crtc *crtc)
  833. {
  834. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  835. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  836. }
  837. void intel_encoder_prepare (struct drm_encoder *encoder)
  838. {
  839. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  840. /* lvds has its own version of prepare see intel_lvds_prepare */
  841. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  842. }
  843. void intel_encoder_commit (struct drm_encoder *encoder)
  844. {
  845. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  846. /* lvds has its own version of commit see intel_lvds_commit */
  847. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  848. }
  849. static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
  850. struct drm_display_mode *mode,
  851. struct drm_display_mode *adjusted_mode)
  852. {
  853. return true;
  854. }
  855. /** Returns the core display clock speed for i830 - i945 */
  856. static int intel_get_core_clock_speed(struct drm_device *dev)
  857. {
  858. /* Core clock values taken from the published datasheets.
  859. * The 830 may go up to 166 Mhz, which we should check.
  860. */
  861. if (IS_I945G(dev))
  862. return 400000;
  863. else if (IS_I915G(dev))
  864. return 333000;
  865. else if (IS_I945GM(dev) || IS_845G(dev) || IS_IGDGM(dev))
  866. return 200000;
  867. else if (IS_I915GM(dev)) {
  868. u16 gcfgc = 0;
  869. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  870. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  871. return 133000;
  872. else {
  873. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  874. case GC_DISPLAY_CLOCK_333_MHZ:
  875. return 333000;
  876. default:
  877. case GC_DISPLAY_CLOCK_190_200_MHZ:
  878. return 190000;
  879. }
  880. }
  881. } else if (IS_I865G(dev))
  882. return 266000;
  883. else if (IS_I855(dev)) {
  884. u16 hpllcc = 0;
  885. /* Assume that the hardware is in the high speed state. This
  886. * should be the default.
  887. */
  888. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  889. case GC_CLOCK_133_200:
  890. case GC_CLOCK_100_200:
  891. return 200000;
  892. case GC_CLOCK_166_250:
  893. return 250000;
  894. case GC_CLOCK_100_133:
  895. return 133000;
  896. }
  897. } else /* 852, 830 */
  898. return 133000;
  899. return 0; /* Silence gcc warning */
  900. }
  901. /**
  902. * Return the pipe currently connected to the panel fitter,
  903. * or -1 if the panel fitter is not present or not in use
  904. */
  905. static int intel_panel_fitter_pipe (struct drm_device *dev)
  906. {
  907. struct drm_i915_private *dev_priv = dev->dev_private;
  908. u32 pfit_control;
  909. /* i830 doesn't have a panel fitter */
  910. if (IS_I830(dev))
  911. return -1;
  912. pfit_control = I915_READ(PFIT_CONTROL);
  913. /* See if the panel fitter is in use */
  914. if ((pfit_control & PFIT_ENABLE) == 0)
  915. return -1;
  916. /* 965 can place panel fitter on either pipe */
  917. if (IS_I965G(dev))
  918. return (pfit_control >> 29) & 0x3;
  919. /* older chips can only use pipe 1 */
  920. return 1;
  921. }
  922. static int intel_crtc_mode_set(struct drm_crtc *crtc,
  923. struct drm_display_mode *mode,
  924. struct drm_display_mode *adjusted_mode,
  925. int x, int y,
  926. struct drm_framebuffer *old_fb)
  927. {
  928. struct drm_device *dev = crtc->dev;
  929. struct drm_i915_private *dev_priv = dev->dev_private;
  930. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  931. int pipe = intel_crtc->pipe;
  932. int fp_reg = (pipe == 0) ? FPA0 : FPB0;
  933. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  934. int dpll_md_reg = (intel_crtc->pipe == 0) ? DPLL_A_MD : DPLL_B_MD;
  935. int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
  936. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  937. int htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
  938. int hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
  939. int hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
  940. int vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
  941. int vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
  942. int vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
  943. int dspsize_reg = (pipe == 0) ? DSPASIZE : DSPBSIZE;
  944. int dsppos_reg = (pipe == 0) ? DSPAPOS : DSPBPOS;
  945. int pipesrc_reg = (pipe == 0) ? PIPEASRC : PIPEBSRC;
  946. int refclk, num_outputs = 0;
  947. intel_clock_t clock;
  948. u32 dpll = 0, fp = 0, dspcntr, pipeconf;
  949. bool ok, is_sdvo = false, is_dvo = false;
  950. bool is_crt = false, is_lvds = false, is_tv = false;
  951. struct drm_mode_config *mode_config = &dev->mode_config;
  952. struct drm_connector *connector;
  953. const intel_limit_t *limit;
  954. int ret;
  955. drm_vblank_pre_modeset(dev, pipe);
  956. list_for_each_entry(connector, &mode_config->connector_list, head) {
  957. struct intel_output *intel_output = to_intel_output(connector);
  958. if (!connector->encoder || connector->encoder->crtc != crtc)
  959. continue;
  960. switch (intel_output->type) {
  961. case INTEL_OUTPUT_LVDS:
  962. is_lvds = true;
  963. break;
  964. case INTEL_OUTPUT_SDVO:
  965. case INTEL_OUTPUT_HDMI:
  966. is_sdvo = true;
  967. if (intel_output->needs_tv_clock)
  968. is_tv = true;
  969. break;
  970. case INTEL_OUTPUT_DVO:
  971. is_dvo = true;
  972. break;
  973. case INTEL_OUTPUT_TVOUT:
  974. is_tv = true;
  975. break;
  976. case INTEL_OUTPUT_ANALOG:
  977. is_crt = true;
  978. break;
  979. }
  980. num_outputs++;
  981. }
  982. if (is_lvds && dev_priv->lvds_use_ssc && num_outputs < 2) {
  983. refclk = dev_priv->lvds_ssc_freq * 1000;
  984. DRM_DEBUG("using SSC reference clock of %d MHz\n", refclk / 1000);
  985. } else if (IS_I9XX(dev)) {
  986. refclk = 96000;
  987. } else {
  988. refclk = 48000;
  989. }
  990. /*
  991. * Returns a set of divisors for the desired target clock with the given
  992. * refclk, or FALSE. The returned values represent the clock equation:
  993. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  994. */
  995. limit = intel_limit(crtc);
  996. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
  997. if (!ok) {
  998. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  999. return -EINVAL;
  1000. }
  1001. /* SDVO TV has fixed PLL values depend on its clock range,
  1002. this mirrors vbios setting. */
  1003. if (is_sdvo && is_tv) {
  1004. if (adjusted_mode->clock >= 100000
  1005. && adjusted_mode->clock < 140500) {
  1006. clock.p1 = 2;
  1007. clock.p2 = 10;
  1008. clock.n = 3;
  1009. clock.m1 = 16;
  1010. clock.m2 = 8;
  1011. } else if (adjusted_mode->clock >= 140500
  1012. && adjusted_mode->clock <= 200000) {
  1013. clock.p1 = 1;
  1014. clock.p2 = 10;
  1015. clock.n = 6;
  1016. clock.m1 = 12;
  1017. clock.m2 = 8;
  1018. }
  1019. }
  1020. if (IS_IGD(dev))
  1021. fp = (1 << clock.n) << 16 | clock.m1 << 8 | clock.m2;
  1022. else
  1023. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  1024. dpll = DPLL_VGA_MODE_DIS;
  1025. if (IS_I9XX(dev)) {
  1026. if (is_lvds)
  1027. dpll |= DPLLB_MODE_LVDS;
  1028. else
  1029. dpll |= DPLLB_MODE_DAC_SERIAL;
  1030. if (is_sdvo) {
  1031. dpll |= DPLL_DVO_HIGH_SPEED;
  1032. if (IS_I945G(dev) || IS_I945GM(dev)) {
  1033. int sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
  1034. dpll |= (sdvo_pixel_multiply - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
  1035. }
  1036. }
  1037. /* compute bitmask from p1 value */
  1038. if (IS_IGD(dev))
  1039. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_IGD;
  1040. else
  1041. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  1042. switch (clock.p2) {
  1043. case 5:
  1044. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  1045. break;
  1046. case 7:
  1047. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  1048. break;
  1049. case 10:
  1050. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  1051. break;
  1052. case 14:
  1053. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  1054. break;
  1055. }
  1056. if (IS_I965G(dev))
  1057. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  1058. } else {
  1059. if (is_lvds) {
  1060. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  1061. } else {
  1062. if (clock.p1 == 2)
  1063. dpll |= PLL_P1_DIVIDE_BY_TWO;
  1064. else
  1065. dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  1066. if (clock.p2 == 4)
  1067. dpll |= PLL_P2_DIVIDE_BY_4;
  1068. }
  1069. }
  1070. if (is_sdvo && is_tv)
  1071. dpll |= PLL_REF_INPUT_TVCLKINBC;
  1072. else if (is_tv)
  1073. /* XXX: just matching BIOS for now */
  1074. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  1075. dpll |= 3;
  1076. else if (is_lvds && dev_priv->lvds_use_ssc && num_outputs < 2)
  1077. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  1078. else
  1079. dpll |= PLL_REF_INPUT_DREFCLK;
  1080. /* setup pipeconf */
  1081. pipeconf = I915_READ(pipeconf_reg);
  1082. /* Set up the display plane register */
  1083. dspcntr = DISPPLANE_GAMMA_ENABLE;
  1084. if (pipe == 0)
  1085. dspcntr |= DISPPLANE_SEL_PIPE_A;
  1086. else
  1087. dspcntr |= DISPPLANE_SEL_PIPE_B;
  1088. if (pipe == 0 && !IS_I965G(dev)) {
  1089. /* Enable pixel doubling when the dot clock is > 90% of the (display)
  1090. * core speed.
  1091. *
  1092. * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
  1093. * pipe == 0 check?
  1094. */
  1095. if (mode->clock > intel_get_core_clock_speed(dev) * 9 / 10)
  1096. pipeconf |= PIPEACONF_DOUBLE_WIDE;
  1097. else
  1098. pipeconf &= ~PIPEACONF_DOUBLE_WIDE;
  1099. }
  1100. dspcntr |= DISPLAY_PLANE_ENABLE;
  1101. pipeconf |= PIPEACONF_ENABLE;
  1102. dpll |= DPLL_VCO_ENABLE;
  1103. /* Disable the panel fitter if it was on our pipe */
  1104. if (intel_panel_fitter_pipe(dev) == pipe)
  1105. I915_WRITE(PFIT_CONTROL, 0);
  1106. DRM_DEBUG("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  1107. drm_mode_debug_printmodeline(mode);
  1108. if (dpll & DPLL_VCO_ENABLE) {
  1109. I915_WRITE(fp_reg, fp);
  1110. I915_WRITE(dpll_reg, dpll & ~DPLL_VCO_ENABLE);
  1111. I915_READ(dpll_reg);
  1112. udelay(150);
  1113. }
  1114. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  1115. * This is an exception to the general rule that mode_set doesn't turn
  1116. * things on.
  1117. */
  1118. if (is_lvds) {
  1119. u32 lvds = I915_READ(LVDS);
  1120. lvds |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP | LVDS_PIPEB_SELECT;
  1121. /* Set the B0-B3 data pairs corresponding to whether we're going to
  1122. * set the DPLLs for dual-channel mode or not.
  1123. */
  1124. if (clock.p2 == 7)
  1125. lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  1126. else
  1127. lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  1128. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  1129. * appropriately here, but we need to look more thoroughly into how
  1130. * panels behave in the two modes.
  1131. */
  1132. I915_WRITE(LVDS, lvds);
  1133. I915_READ(LVDS);
  1134. }
  1135. I915_WRITE(fp_reg, fp);
  1136. I915_WRITE(dpll_reg, dpll);
  1137. I915_READ(dpll_reg);
  1138. /* Wait for the clocks to stabilize. */
  1139. udelay(150);
  1140. if (IS_I965G(dev)) {
  1141. int sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
  1142. I915_WRITE(dpll_md_reg, (0 << DPLL_MD_UDI_DIVIDER_SHIFT) |
  1143. ((sdvo_pixel_multiply - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT));
  1144. } else {
  1145. /* write it again -- the BIOS does, after all */
  1146. I915_WRITE(dpll_reg, dpll);
  1147. }
  1148. I915_READ(dpll_reg);
  1149. /* Wait for the clocks to stabilize. */
  1150. udelay(150);
  1151. I915_WRITE(htot_reg, (adjusted_mode->crtc_hdisplay - 1) |
  1152. ((adjusted_mode->crtc_htotal - 1) << 16));
  1153. I915_WRITE(hblank_reg, (adjusted_mode->crtc_hblank_start - 1) |
  1154. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  1155. I915_WRITE(hsync_reg, (adjusted_mode->crtc_hsync_start - 1) |
  1156. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  1157. I915_WRITE(vtot_reg, (adjusted_mode->crtc_vdisplay - 1) |
  1158. ((adjusted_mode->crtc_vtotal - 1) << 16));
  1159. I915_WRITE(vblank_reg, (adjusted_mode->crtc_vblank_start - 1) |
  1160. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  1161. I915_WRITE(vsync_reg, (adjusted_mode->crtc_vsync_start - 1) |
  1162. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  1163. /* pipesrc and dspsize control the size that is scaled from, which should
  1164. * always be the user's requested size.
  1165. */
  1166. I915_WRITE(dspsize_reg, ((mode->vdisplay - 1) << 16) | (mode->hdisplay - 1));
  1167. I915_WRITE(dsppos_reg, 0);
  1168. I915_WRITE(pipesrc_reg, ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  1169. I915_WRITE(pipeconf_reg, pipeconf);
  1170. I915_READ(pipeconf_reg);
  1171. intel_wait_for_vblank(dev);
  1172. I915_WRITE(dspcntr_reg, dspcntr);
  1173. /* Flush the plane changes */
  1174. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  1175. if (ret != 0)
  1176. return ret;
  1177. drm_vblank_post_modeset(dev, pipe);
  1178. return 0;
  1179. }
  1180. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  1181. void intel_crtc_load_lut(struct drm_crtc *crtc)
  1182. {
  1183. struct drm_device *dev = crtc->dev;
  1184. struct drm_i915_private *dev_priv = dev->dev_private;
  1185. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1186. int palreg = (intel_crtc->pipe == 0) ? PALETTE_A : PALETTE_B;
  1187. int i;
  1188. /* The clocks have to be on to load the palette. */
  1189. if (!crtc->enabled)
  1190. return;
  1191. for (i = 0; i < 256; i++) {
  1192. I915_WRITE(palreg + 4 * i,
  1193. (intel_crtc->lut_r[i] << 16) |
  1194. (intel_crtc->lut_g[i] << 8) |
  1195. intel_crtc->lut_b[i]);
  1196. }
  1197. }
  1198. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  1199. struct drm_file *file_priv,
  1200. uint32_t handle,
  1201. uint32_t width, uint32_t height)
  1202. {
  1203. struct drm_device *dev = crtc->dev;
  1204. struct drm_i915_private *dev_priv = dev->dev_private;
  1205. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1206. struct drm_gem_object *bo;
  1207. struct drm_i915_gem_object *obj_priv;
  1208. int pipe = intel_crtc->pipe;
  1209. uint32_t control = (pipe == 0) ? CURACNTR : CURBCNTR;
  1210. uint32_t base = (pipe == 0) ? CURABASE : CURBBASE;
  1211. uint32_t temp;
  1212. size_t addr;
  1213. int ret;
  1214. DRM_DEBUG("\n");
  1215. /* if we want to turn off the cursor ignore width and height */
  1216. if (!handle) {
  1217. DRM_DEBUG("cursor off\n");
  1218. temp = CURSOR_MODE_DISABLE;
  1219. addr = 0;
  1220. bo = NULL;
  1221. mutex_lock(&dev->struct_mutex);
  1222. goto finish;
  1223. }
  1224. /* Currently we only support 64x64 cursors */
  1225. if (width != 64 || height != 64) {
  1226. DRM_ERROR("we currently only support 64x64 cursors\n");
  1227. return -EINVAL;
  1228. }
  1229. bo = drm_gem_object_lookup(dev, file_priv, handle);
  1230. if (!bo)
  1231. return -ENOENT;
  1232. obj_priv = bo->driver_private;
  1233. if (bo->size < width * height * 4) {
  1234. DRM_ERROR("buffer is to small\n");
  1235. ret = -ENOMEM;
  1236. goto fail;
  1237. }
  1238. /* we only need to pin inside GTT if cursor is non-phy */
  1239. mutex_lock(&dev->struct_mutex);
  1240. if (!dev_priv->cursor_needs_physical) {
  1241. ret = i915_gem_object_pin(bo, PAGE_SIZE);
  1242. if (ret) {
  1243. DRM_ERROR("failed to pin cursor bo\n");
  1244. goto fail_locked;
  1245. }
  1246. addr = obj_priv->gtt_offset;
  1247. } else {
  1248. ret = i915_gem_attach_phys_object(dev, bo, (pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1);
  1249. if (ret) {
  1250. DRM_ERROR("failed to attach phys object\n");
  1251. goto fail_locked;
  1252. }
  1253. addr = obj_priv->phys_obj->handle->busaddr;
  1254. }
  1255. temp = 0;
  1256. /* set the pipe for the cursor */
  1257. temp |= (pipe << 28);
  1258. temp |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  1259. finish:
  1260. I915_WRITE(control, temp);
  1261. I915_WRITE(base, addr);
  1262. if (intel_crtc->cursor_bo) {
  1263. if (dev_priv->cursor_needs_physical) {
  1264. if (intel_crtc->cursor_bo != bo)
  1265. i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
  1266. } else
  1267. i915_gem_object_unpin(intel_crtc->cursor_bo);
  1268. drm_gem_object_unreference(intel_crtc->cursor_bo);
  1269. }
  1270. mutex_unlock(&dev->struct_mutex);
  1271. intel_crtc->cursor_addr = addr;
  1272. intel_crtc->cursor_bo = bo;
  1273. return 0;
  1274. fail:
  1275. mutex_lock(&dev->struct_mutex);
  1276. fail_locked:
  1277. drm_gem_object_unreference(bo);
  1278. mutex_unlock(&dev->struct_mutex);
  1279. return ret;
  1280. }
  1281. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  1282. {
  1283. struct drm_device *dev = crtc->dev;
  1284. struct drm_i915_private *dev_priv = dev->dev_private;
  1285. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1286. int pipe = intel_crtc->pipe;
  1287. uint32_t temp = 0;
  1288. uint32_t adder;
  1289. if (x < 0) {
  1290. temp |= (CURSOR_POS_SIGN << CURSOR_X_SHIFT);
  1291. x = -x;
  1292. }
  1293. if (y < 0) {
  1294. temp |= (CURSOR_POS_SIGN << CURSOR_Y_SHIFT);
  1295. y = -y;
  1296. }
  1297. temp |= ((x & CURSOR_POS_MASK) << CURSOR_X_SHIFT);
  1298. temp |= ((y & CURSOR_POS_MASK) << CURSOR_Y_SHIFT);
  1299. adder = intel_crtc->cursor_addr;
  1300. I915_WRITE((pipe == 0) ? CURAPOS : CURBPOS, temp);
  1301. I915_WRITE((pipe == 0) ? CURABASE : CURBBASE, adder);
  1302. return 0;
  1303. }
  1304. /** Sets the color ramps on behalf of RandR */
  1305. void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  1306. u16 blue, int regno)
  1307. {
  1308. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1309. intel_crtc->lut_r[regno] = red >> 8;
  1310. intel_crtc->lut_g[regno] = green >> 8;
  1311. intel_crtc->lut_b[regno] = blue >> 8;
  1312. }
  1313. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  1314. u16 *blue, uint32_t size)
  1315. {
  1316. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1317. int i;
  1318. if (size != 256)
  1319. return;
  1320. for (i = 0; i < 256; i++) {
  1321. intel_crtc->lut_r[i] = red[i] >> 8;
  1322. intel_crtc->lut_g[i] = green[i] >> 8;
  1323. intel_crtc->lut_b[i] = blue[i] >> 8;
  1324. }
  1325. intel_crtc_load_lut(crtc);
  1326. }
  1327. /**
  1328. * Get a pipe with a simple mode set on it for doing load-based monitor
  1329. * detection.
  1330. *
  1331. * It will be up to the load-detect code to adjust the pipe as appropriate for
  1332. * its requirements. The pipe will be connected to no other outputs.
  1333. *
  1334. * Currently this code will only succeed if there is a pipe with no outputs
  1335. * configured for it. In the future, it could choose to temporarily disable
  1336. * some outputs to free up a pipe for its use.
  1337. *
  1338. * \return crtc, or NULL if no pipes are available.
  1339. */
  1340. /* VESA 640x480x72Hz mode to set on the pipe */
  1341. static struct drm_display_mode load_detect_mode = {
  1342. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  1343. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  1344. };
  1345. struct drm_crtc *intel_get_load_detect_pipe(struct intel_output *intel_output,
  1346. struct drm_display_mode *mode,
  1347. int *dpms_mode)
  1348. {
  1349. struct intel_crtc *intel_crtc;
  1350. struct drm_crtc *possible_crtc;
  1351. struct drm_crtc *supported_crtc =NULL;
  1352. struct drm_encoder *encoder = &intel_output->enc;
  1353. struct drm_crtc *crtc = NULL;
  1354. struct drm_device *dev = encoder->dev;
  1355. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  1356. struct drm_crtc_helper_funcs *crtc_funcs;
  1357. int i = -1;
  1358. /*
  1359. * Algorithm gets a little messy:
  1360. * - if the connector already has an assigned crtc, use it (but make
  1361. * sure it's on first)
  1362. * - try to find the first unused crtc that can drive this connector,
  1363. * and use that if we find one
  1364. * - if there are no unused crtcs available, try to use the first
  1365. * one we found that supports the connector
  1366. */
  1367. /* See if we already have a CRTC for this connector */
  1368. if (encoder->crtc) {
  1369. crtc = encoder->crtc;
  1370. /* Make sure the crtc and connector are running */
  1371. intel_crtc = to_intel_crtc(crtc);
  1372. *dpms_mode = intel_crtc->dpms_mode;
  1373. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  1374. crtc_funcs = crtc->helper_private;
  1375. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  1376. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  1377. }
  1378. return crtc;
  1379. }
  1380. /* Find an unused one (if possible) */
  1381. list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
  1382. i++;
  1383. if (!(encoder->possible_crtcs & (1 << i)))
  1384. continue;
  1385. if (!possible_crtc->enabled) {
  1386. crtc = possible_crtc;
  1387. break;
  1388. }
  1389. if (!supported_crtc)
  1390. supported_crtc = possible_crtc;
  1391. }
  1392. /*
  1393. * If we didn't find an unused CRTC, don't use any.
  1394. */
  1395. if (!crtc) {
  1396. return NULL;
  1397. }
  1398. encoder->crtc = crtc;
  1399. intel_output->load_detect_temp = true;
  1400. intel_crtc = to_intel_crtc(crtc);
  1401. *dpms_mode = intel_crtc->dpms_mode;
  1402. if (!crtc->enabled) {
  1403. if (!mode)
  1404. mode = &load_detect_mode;
  1405. drm_crtc_helper_set_mode(crtc, mode, 0, 0, crtc->fb);
  1406. } else {
  1407. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  1408. crtc_funcs = crtc->helper_private;
  1409. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  1410. }
  1411. /* Add this connector to the crtc */
  1412. encoder_funcs->mode_set(encoder, &crtc->mode, &crtc->mode);
  1413. encoder_funcs->commit(encoder);
  1414. }
  1415. /* let the connector get through one full cycle before testing */
  1416. intel_wait_for_vblank(dev);
  1417. return crtc;
  1418. }
  1419. void intel_release_load_detect_pipe(struct intel_output *intel_output, int dpms_mode)
  1420. {
  1421. struct drm_encoder *encoder = &intel_output->enc;
  1422. struct drm_device *dev = encoder->dev;
  1423. struct drm_crtc *crtc = encoder->crtc;
  1424. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  1425. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  1426. if (intel_output->load_detect_temp) {
  1427. encoder->crtc = NULL;
  1428. intel_output->load_detect_temp = false;
  1429. crtc->enabled = drm_helper_crtc_in_use(crtc);
  1430. drm_helper_disable_unused_functions(dev);
  1431. }
  1432. /* Switch crtc and output back off if necessary */
  1433. if (crtc->enabled && dpms_mode != DRM_MODE_DPMS_ON) {
  1434. if (encoder->crtc == crtc)
  1435. encoder_funcs->dpms(encoder, dpms_mode);
  1436. crtc_funcs->dpms(crtc, dpms_mode);
  1437. }
  1438. }
  1439. /* Returns the clock of the currently programmed mode of the given pipe. */
  1440. static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
  1441. {
  1442. struct drm_i915_private *dev_priv = dev->dev_private;
  1443. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1444. int pipe = intel_crtc->pipe;
  1445. u32 dpll = I915_READ((pipe == 0) ? DPLL_A : DPLL_B);
  1446. u32 fp;
  1447. intel_clock_t clock;
  1448. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  1449. fp = I915_READ((pipe == 0) ? FPA0 : FPB0);
  1450. else
  1451. fp = I915_READ((pipe == 0) ? FPA1 : FPB1);
  1452. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  1453. if (IS_IGD(dev)) {
  1454. clock.n = ffs((fp & FP_N_IGD_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  1455. clock.m2 = (fp & FP_M2_IGD_DIV_MASK) >> FP_M2_DIV_SHIFT;
  1456. } else {
  1457. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  1458. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  1459. }
  1460. if (IS_I9XX(dev)) {
  1461. if (IS_IGD(dev))
  1462. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_IGD) >>
  1463. DPLL_FPA01_P1_POST_DIV_SHIFT_IGD);
  1464. else
  1465. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  1466. DPLL_FPA01_P1_POST_DIV_SHIFT);
  1467. switch (dpll & DPLL_MODE_MASK) {
  1468. case DPLLB_MODE_DAC_SERIAL:
  1469. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  1470. 5 : 10;
  1471. break;
  1472. case DPLLB_MODE_LVDS:
  1473. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  1474. 7 : 14;
  1475. break;
  1476. default:
  1477. DRM_DEBUG("Unknown DPLL mode %08x in programmed "
  1478. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  1479. return 0;
  1480. }
  1481. /* XXX: Handle the 100Mhz refclk */
  1482. intel_clock(dev, 96000, &clock);
  1483. } else {
  1484. bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
  1485. if (is_lvds) {
  1486. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  1487. DPLL_FPA01_P1_POST_DIV_SHIFT);
  1488. clock.p2 = 14;
  1489. if ((dpll & PLL_REF_INPUT_MASK) ==
  1490. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  1491. /* XXX: might not be 66MHz */
  1492. intel_clock(dev, 66000, &clock);
  1493. } else
  1494. intel_clock(dev, 48000, &clock);
  1495. } else {
  1496. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  1497. clock.p1 = 2;
  1498. else {
  1499. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  1500. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  1501. }
  1502. if (dpll & PLL_P2_DIVIDE_BY_4)
  1503. clock.p2 = 4;
  1504. else
  1505. clock.p2 = 2;
  1506. intel_clock(dev, 48000, &clock);
  1507. }
  1508. }
  1509. /* XXX: It would be nice to validate the clocks, but we can't reuse
  1510. * i830PllIsValid() because it relies on the xf86_config connector
  1511. * configuration being accurate, which it isn't necessarily.
  1512. */
  1513. return clock.dot;
  1514. }
  1515. /** Returns the currently programmed mode of the given pipe. */
  1516. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  1517. struct drm_crtc *crtc)
  1518. {
  1519. struct drm_i915_private *dev_priv = dev->dev_private;
  1520. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1521. int pipe = intel_crtc->pipe;
  1522. struct drm_display_mode *mode;
  1523. int htot = I915_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
  1524. int hsync = I915_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
  1525. int vtot = I915_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
  1526. int vsync = I915_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
  1527. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  1528. if (!mode)
  1529. return NULL;
  1530. mode->clock = intel_crtc_clock_get(dev, crtc);
  1531. mode->hdisplay = (htot & 0xffff) + 1;
  1532. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  1533. mode->hsync_start = (hsync & 0xffff) + 1;
  1534. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  1535. mode->vdisplay = (vtot & 0xffff) + 1;
  1536. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  1537. mode->vsync_start = (vsync & 0xffff) + 1;
  1538. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  1539. drm_mode_set_name(mode);
  1540. drm_mode_set_crtcinfo(mode, 0);
  1541. return mode;
  1542. }
  1543. static void intel_crtc_destroy(struct drm_crtc *crtc)
  1544. {
  1545. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1546. drm_crtc_cleanup(crtc);
  1547. kfree(intel_crtc);
  1548. }
  1549. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  1550. .dpms = intel_crtc_dpms,
  1551. .mode_fixup = intel_crtc_mode_fixup,
  1552. .mode_set = intel_crtc_mode_set,
  1553. .mode_set_base = intel_pipe_set_base,
  1554. .prepare = intel_crtc_prepare,
  1555. .commit = intel_crtc_commit,
  1556. };
  1557. static const struct drm_crtc_funcs intel_crtc_funcs = {
  1558. .cursor_set = intel_crtc_cursor_set,
  1559. .cursor_move = intel_crtc_cursor_move,
  1560. .gamma_set = intel_crtc_gamma_set,
  1561. .set_config = drm_crtc_helper_set_config,
  1562. .destroy = intel_crtc_destroy,
  1563. };
  1564. static void intel_crtc_init(struct drm_device *dev, int pipe)
  1565. {
  1566. struct intel_crtc *intel_crtc;
  1567. int i;
  1568. intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  1569. if (intel_crtc == NULL)
  1570. return;
  1571. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  1572. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  1573. intel_crtc->pipe = pipe;
  1574. for (i = 0; i < 256; i++) {
  1575. intel_crtc->lut_r[i] = i;
  1576. intel_crtc->lut_g[i] = i;
  1577. intel_crtc->lut_b[i] = i;
  1578. }
  1579. intel_crtc->cursor_addr = 0;
  1580. intel_crtc->dpms_mode = DRM_MODE_DPMS_OFF;
  1581. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  1582. intel_crtc->mode_set.crtc = &intel_crtc->base;
  1583. intel_crtc->mode_set.connectors = (struct drm_connector **)(intel_crtc + 1);
  1584. intel_crtc->mode_set.num_connectors = 0;
  1585. if (i915_fbpercrtc) {
  1586. }
  1587. }
  1588. struct drm_crtc *intel_get_crtc_from_pipe(struct drm_device *dev, int pipe)
  1589. {
  1590. struct drm_crtc *crtc = NULL;
  1591. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1592. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1593. if (intel_crtc->pipe == pipe)
  1594. break;
  1595. }
  1596. return crtc;
  1597. }
  1598. static int intel_connector_clones(struct drm_device *dev, int type_mask)
  1599. {
  1600. int index_mask = 0;
  1601. struct drm_connector *connector;
  1602. int entry = 0;
  1603. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1604. struct intel_output *intel_output = to_intel_output(connector);
  1605. if (type_mask & (1 << intel_output->type))
  1606. index_mask |= (1 << entry);
  1607. entry++;
  1608. }
  1609. return index_mask;
  1610. }
  1611. static void intel_setup_outputs(struct drm_device *dev)
  1612. {
  1613. struct drm_i915_private *dev_priv = dev->dev_private;
  1614. struct drm_connector *connector;
  1615. intel_crt_init(dev);
  1616. /* Set up integrated LVDS */
  1617. if (IS_MOBILE(dev) && !IS_I830(dev))
  1618. intel_lvds_init(dev);
  1619. if (IS_I9XX(dev)) {
  1620. int found;
  1621. u32 reg;
  1622. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  1623. found = intel_sdvo_init(dev, SDVOB);
  1624. if (!found && SUPPORTS_INTEGRATED_HDMI(dev))
  1625. intel_hdmi_init(dev, SDVOB);
  1626. }
  1627. /* Before G4X SDVOC doesn't have its own detect register */
  1628. if (IS_G4X(dev))
  1629. reg = SDVOC;
  1630. else
  1631. reg = SDVOB;
  1632. if (I915_READ(reg) & SDVO_DETECTED) {
  1633. found = intel_sdvo_init(dev, SDVOC);
  1634. if (!found && SUPPORTS_INTEGRATED_HDMI(dev))
  1635. intel_hdmi_init(dev, SDVOC);
  1636. }
  1637. } else
  1638. intel_dvo_init(dev);
  1639. if (IS_I9XX(dev) && IS_MOBILE(dev))
  1640. intel_tv_init(dev);
  1641. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1642. struct intel_output *intel_output = to_intel_output(connector);
  1643. struct drm_encoder *encoder = &intel_output->enc;
  1644. int crtc_mask = 0, clone_mask = 0;
  1645. /* valid crtcs */
  1646. switch(intel_output->type) {
  1647. case INTEL_OUTPUT_HDMI:
  1648. crtc_mask = ((1 << 0)|
  1649. (1 << 1));
  1650. clone_mask = ((1 << INTEL_OUTPUT_HDMI));
  1651. break;
  1652. case INTEL_OUTPUT_DVO:
  1653. case INTEL_OUTPUT_SDVO:
  1654. crtc_mask = ((1 << 0)|
  1655. (1 << 1));
  1656. clone_mask = ((1 << INTEL_OUTPUT_ANALOG) |
  1657. (1 << INTEL_OUTPUT_DVO) |
  1658. (1 << INTEL_OUTPUT_SDVO));
  1659. break;
  1660. case INTEL_OUTPUT_ANALOG:
  1661. crtc_mask = ((1 << 0)|
  1662. (1 << 1));
  1663. clone_mask = ((1 << INTEL_OUTPUT_ANALOG) |
  1664. (1 << INTEL_OUTPUT_DVO) |
  1665. (1 << INTEL_OUTPUT_SDVO));
  1666. break;
  1667. case INTEL_OUTPUT_LVDS:
  1668. crtc_mask = (1 << 1);
  1669. clone_mask = (1 << INTEL_OUTPUT_LVDS);
  1670. break;
  1671. case INTEL_OUTPUT_TVOUT:
  1672. crtc_mask = ((1 << 0) |
  1673. (1 << 1));
  1674. clone_mask = (1 << INTEL_OUTPUT_TVOUT);
  1675. break;
  1676. }
  1677. encoder->possible_crtcs = crtc_mask;
  1678. encoder->possible_clones = intel_connector_clones(dev, clone_mask);
  1679. }
  1680. }
  1681. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  1682. {
  1683. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1684. struct drm_device *dev = fb->dev;
  1685. if (fb->fbdev)
  1686. intelfb_remove(dev, fb);
  1687. drm_framebuffer_cleanup(fb);
  1688. mutex_lock(&dev->struct_mutex);
  1689. drm_gem_object_unreference(intel_fb->obj);
  1690. mutex_unlock(&dev->struct_mutex);
  1691. kfree(intel_fb);
  1692. }
  1693. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  1694. struct drm_file *file_priv,
  1695. unsigned int *handle)
  1696. {
  1697. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1698. struct drm_gem_object *object = intel_fb->obj;
  1699. return drm_gem_handle_create(file_priv, object, handle);
  1700. }
  1701. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  1702. .destroy = intel_user_framebuffer_destroy,
  1703. .create_handle = intel_user_framebuffer_create_handle,
  1704. };
  1705. int intel_framebuffer_create(struct drm_device *dev,
  1706. struct drm_mode_fb_cmd *mode_cmd,
  1707. struct drm_framebuffer **fb,
  1708. struct drm_gem_object *obj)
  1709. {
  1710. struct intel_framebuffer *intel_fb;
  1711. int ret;
  1712. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  1713. if (!intel_fb)
  1714. return -ENOMEM;
  1715. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  1716. if (ret) {
  1717. DRM_ERROR("framebuffer init failed %d\n", ret);
  1718. return ret;
  1719. }
  1720. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  1721. intel_fb->obj = obj;
  1722. *fb = &intel_fb->base;
  1723. return 0;
  1724. }
  1725. static struct drm_framebuffer *
  1726. intel_user_framebuffer_create(struct drm_device *dev,
  1727. struct drm_file *filp,
  1728. struct drm_mode_fb_cmd *mode_cmd)
  1729. {
  1730. struct drm_gem_object *obj;
  1731. struct drm_framebuffer *fb;
  1732. int ret;
  1733. obj = drm_gem_object_lookup(dev, filp, mode_cmd->handle);
  1734. if (!obj)
  1735. return NULL;
  1736. ret = intel_framebuffer_create(dev, mode_cmd, &fb, obj);
  1737. if (ret) {
  1738. mutex_lock(&dev->struct_mutex);
  1739. drm_gem_object_unreference(obj);
  1740. mutex_unlock(&dev->struct_mutex);
  1741. return NULL;
  1742. }
  1743. return fb;
  1744. }
  1745. static const struct drm_mode_config_funcs intel_mode_funcs = {
  1746. .fb_create = intel_user_framebuffer_create,
  1747. .fb_changed = intelfb_probe,
  1748. };
  1749. void intel_modeset_init(struct drm_device *dev)
  1750. {
  1751. int num_pipe;
  1752. int i;
  1753. drm_mode_config_init(dev);
  1754. dev->mode_config.min_width = 0;
  1755. dev->mode_config.min_height = 0;
  1756. dev->mode_config.funcs = (void *)&intel_mode_funcs;
  1757. if (IS_I965G(dev)) {
  1758. dev->mode_config.max_width = 8192;
  1759. dev->mode_config.max_height = 8192;
  1760. } else {
  1761. dev->mode_config.max_width = 2048;
  1762. dev->mode_config.max_height = 2048;
  1763. }
  1764. /* set memory base */
  1765. if (IS_I9XX(dev))
  1766. dev->mode_config.fb_base = pci_resource_start(dev->pdev, 2);
  1767. else
  1768. dev->mode_config.fb_base = pci_resource_start(dev->pdev, 0);
  1769. if (IS_MOBILE(dev) || IS_I9XX(dev))
  1770. num_pipe = 2;
  1771. else
  1772. num_pipe = 1;
  1773. DRM_DEBUG("%d display pipe%s available.\n",
  1774. num_pipe, num_pipe > 1 ? "s" : "");
  1775. for (i = 0; i < num_pipe; i++) {
  1776. intel_crtc_init(dev, i);
  1777. }
  1778. intel_setup_outputs(dev);
  1779. }
  1780. void intel_modeset_cleanup(struct drm_device *dev)
  1781. {
  1782. drm_mode_config_cleanup(dev);
  1783. }
  1784. /* current intel driver doesn't take advantage of encoders
  1785. always give back the encoder for the connector
  1786. */
  1787. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  1788. {
  1789. struct intel_output *intel_output = to_intel_output(connector);
  1790. return &intel_output->enc;
  1791. }