intel_display.c 61 KB

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